xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision e981e7b17f2b41970e7e2367d4225e0bb3310667)
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 
1399df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
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 
15406ffc778SPaulo Zanoni 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
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 
1769df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
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 
185480c8033SDaniel Vetter void gen5_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 
190480c8033SDaniel Vetter void gen5_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 
2099df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
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 
223480c8033SDaniel Vetter void gen6_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 
228480c8033SDaniel Vetter void gen6_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 
2679df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
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 
281480c8033SDaniel Vetter void gen8_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 
286480c8033SDaniel Vetter void gen8_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,
3382ae2a50cSDaniel Vetter 					     enum pipe pipe,
3392ae2a50cSDaniel Vetter 					     bool enable, bool old)
3402d9d2b0bSVille Syrjälä {
3412d9d2b0bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3422d9d2b0bSVille Syrjälä 	u32 reg = PIPESTAT(pipe);
343e69abff0SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0xffff0000;
3442d9d2b0bSVille Syrjälä 
3452d9d2b0bSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
3462d9d2b0bSVille Syrjälä 
347e69abff0SVille Syrjälä 	if (enable) {
3482d9d2b0bSVille Syrjälä 		I915_WRITE(reg, pipestat | PIPE_FIFO_UNDERRUN_STATUS);
3492d9d2b0bSVille Syrjälä 		POSTING_READ(reg);
350e69abff0SVille Syrjälä 	} else {
3512ae2a50cSDaniel Vetter 		if (old && pipestat & PIPE_FIFO_UNDERRUN_STATUS)
352e69abff0SVille Syrjälä 			DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
353e69abff0SVille Syrjälä 	}
3542d9d2b0bSVille Syrjälä }
3552d9d2b0bSVille Syrjälä 
3568664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
3578664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
3588664281bSPaulo Zanoni {
3598664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3608664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
3618664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
3628664281bSPaulo Zanoni 
3638664281bSPaulo Zanoni 	if (enable)
3648664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
3658664281bSPaulo Zanoni 	else
3668664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
3678664281bSPaulo Zanoni }
3688664281bSPaulo Zanoni 
3698664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
3702ae2a50cSDaniel Vetter 						  enum pipe pipe,
3712ae2a50cSDaniel Vetter 						  bool enable, bool old)
3728664281bSPaulo Zanoni {
3738664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3748664281bSPaulo Zanoni 	if (enable) {
3757336df65SDaniel Vetter 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
3767336df65SDaniel Vetter 
3778664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
3788664281bSPaulo Zanoni 			return;
3798664281bSPaulo Zanoni 
3808664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
3818664281bSPaulo Zanoni 	} else {
3828664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
3837336df65SDaniel Vetter 
3842ae2a50cSDaniel Vetter 		if (old &&
3852ae2a50cSDaniel Vetter 		    I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe)) {
386823c6909SVille Syrjälä 			DRM_ERROR("uncleared fifo underrun on pipe %c\n",
3877336df65SDaniel Vetter 				  pipe_name(pipe));
3887336df65SDaniel Vetter 		}
3898664281bSPaulo Zanoni 	}
3908664281bSPaulo Zanoni }
3918664281bSPaulo Zanoni 
39238d83c96SDaniel Vetter static void broadwell_set_fifo_underrun_reporting(struct drm_device *dev,
39338d83c96SDaniel Vetter 						  enum pipe pipe, bool enable)
39438d83c96SDaniel Vetter {
39538d83c96SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
39638d83c96SDaniel Vetter 
39738d83c96SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
39838d83c96SDaniel Vetter 
39938d83c96SDaniel Vetter 	if (enable)
40038d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_FIFO_UNDERRUN;
40138d83c96SDaniel Vetter 	else
40238d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_FIFO_UNDERRUN;
40338d83c96SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
40438d83c96SDaniel Vetter 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
40538d83c96SDaniel Vetter }
40638d83c96SDaniel Vetter 
407fee884edSDaniel Vetter /**
408fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
409fee884edSDaniel Vetter  * @dev_priv: driver private
410fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
411fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
412fee884edSDaniel Vetter  */
413fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
414fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
415fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
416fee884edSDaniel Vetter {
417fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
418fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
419fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
420fee884edSDaniel Vetter 
421fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
422fee884edSDaniel Vetter 
4239df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
424c67a470bSPaulo Zanoni 		return;
425c67a470bSPaulo Zanoni 
426fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
427fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
428fee884edSDaniel Vetter }
429fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
430fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
431fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
432fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
433fee884edSDaniel Vetter 
434de28075dSDaniel Vetter static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
435de28075dSDaniel Vetter 					    enum transcoder pch_transcoder,
4368664281bSPaulo Zanoni 					    bool enable)
4378664281bSPaulo Zanoni {
4388664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
439de28075dSDaniel Vetter 	uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
440de28075dSDaniel Vetter 		       SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
4418664281bSPaulo Zanoni 
4428664281bSPaulo Zanoni 	if (enable)
443fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
4448664281bSPaulo Zanoni 	else
445fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
4468664281bSPaulo Zanoni }
4478664281bSPaulo Zanoni 
4488664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
4498664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
4502ae2a50cSDaniel Vetter 					    bool enable, bool old)
4518664281bSPaulo Zanoni {
4528664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4538664281bSPaulo Zanoni 
4548664281bSPaulo Zanoni 	if (enable) {
4551dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
4561dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
4571dd246fbSDaniel Vetter 
4588664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
4598664281bSPaulo Zanoni 			return;
4608664281bSPaulo Zanoni 
461fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
4628664281bSPaulo Zanoni 	} else {
463fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
4641dd246fbSDaniel Vetter 
4652ae2a50cSDaniel Vetter 		if (old && I915_READ(SERR_INT) &
4662ae2a50cSDaniel Vetter 		    SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder)) {
467823c6909SVille Syrjälä 			DRM_ERROR("uncleared pch fifo underrun on pch transcoder %c\n",
4681dd246fbSDaniel Vetter 				  transcoder_name(pch_transcoder));
4691dd246fbSDaniel Vetter 		}
4708664281bSPaulo Zanoni 	}
4718664281bSPaulo Zanoni }
4728664281bSPaulo Zanoni 
4738664281bSPaulo Zanoni /**
4748664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
4758664281bSPaulo Zanoni  * @dev: drm device
4768664281bSPaulo Zanoni  * @pipe: pipe
4778664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
4788664281bSPaulo Zanoni  *
4798664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
4808664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
4818664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
4828664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
4838664281bSPaulo Zanoni  * bit for all the pipes.
4848664281bSPaulo Zanoni  *
4858664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
4868664281bSPaulo Zanoni  */
487c5ab3bc0SDaniel Vetter static bool __intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
4888664281bSPaulo Zanoni 						    enum pipe pipe, bool enable)
4898664281bSPaulo Zanoni {
4908664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4918664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
4928664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4932ae2a50cSDaniel Vetter 	bool old;
4948664281bSPaulo Zanoni 
49577961eb9SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
49677961eb9SImre Deak 
4972ae2a50cSDaniel Vetter 	old = !intel_crtc->cpu_fifo_underrun_disabled;
4988664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
4998664281bSPaulo Zanoni 
500e69abff0SVille Syrjälä 	if (INTEL_INFO(dev)->gen < 5 || IS_VALLEYVIEW(dev))
5012ae2a50cSDaniel Vetter 		i9xx_set_fifo_underrun_reporting(dev, pipe, enable, old);
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))
5052ae2a50cSDaniel Vetter 		ivybridge_set_fifo_underrun_reporting(dev, pipe, enable, old);
50638d83c96SDaniel Vetter 	else if (IS_GEN8(dev))
50738d83c96SDaniel Vetter 		broadwell_set_fifo_underrun_reporting(dev, pipe, enable);
5088664281bSPaulo Zanoni 
5092ae2a50cSDaniel Vetter 	return old;
510f88d42f1SImre Deak }
511f88d42f1SImre Deak 
512f88d42f1SImre Deak bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
513f88d42f1SImre Deak 					   enum pipe pipe, bool enable)
514f88d42f1SImre Deak {
515f88d42f1SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
516f88d42f1SImre Deak 	unsigned long flags;
517f88d42f1SImre Deak 	bool ret;
518f88d42f1SImre Deak 
519f88d42f1SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
520f88d42f1SImre Deak 	ret = __intel_set_cpu_fifo_underrun_reporting(dev, pipe, enable);
5218664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
522f88d42f1SImre Deak 
5238664281bSPaulo Zanoni 	return ret;
5248664281bSPaulo Zanoni }
5258664281bSPaulo Zanoni 
52691d181ddSImre Deak static bool __cpu_fifo_underrun_reporting_enabled(struct drm_device *dev,
52791d181ddSImre Deak 						  enum pipe pipe)
52891d181ddSImre Deak {
52991d181ddSImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
53091d181ddSImre Deak 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
53191d181ddSImre Deak 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
53291d181ddSImre Deak 
53391d181ddSImre Deak 	return !intel_crtc->cpu_fifo_underrun_disabled;
53491d181ddSImre Deak }
53591d181ddSImre Deak 
5368664281bSPaulo Zanoni /**
5378664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
5388664281bSPaulo Zanoni  * @dev: drm device
5398664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
5408664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
5418664281bSPaulo Zanoni  *
5428664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
5438664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
5448664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
5458664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
5468664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
5478664281bSPaulo Zanoni  *
5488664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
5498664281bSPaulo Zanoni  */
5508664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
5518664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
5528664281bSPaulo Zanoni 					   bool enable)
5538664281bSPaulo Zanoni {
5548664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
555de28075dSDaniel Vetter 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
556de28075dSDaniel Vetter 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5578664281bSPaulo Zanoni 	unsigned long flags;
5582ae2a50cSDaniel Vetter 	bool old;
5598664281bSPaulo Zanoni 
560de28075dSDaniel Vetter 	/*
561de28075dSDaniel Vetter 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
562de28075dSDaniel Vetter 	 * has only one pch transcoder A that all pipes can use. To avoid racy
563de28075dSDaniel Vetter 	 * pch transcoder -> pipe lookups from interrupt code simply store the
564de28075dSDaniel Vetter 	 * underrun statistics in crtc A. Since we never expose this anywhere
565de28075dSDaniel Vetter 	 * nor use it outside of the fifo underrun code here using the "wrong"
566de28075dSDaniel Vetter 	 * crtc on LPT won't cause issues.
567de28075dSDaniel Vetter 	 */
5688664281bSPaulo Zanoni 
5698664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
5708664281bSPaulo Zanoni 
5712ae2a50cSDaniel Vetter 	old = !intel_crtc->pch_fifo_underrun_disabled;
5728664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
5738664281bSPaulo Zanoni 
5748664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
575de28075dSDaniel Vetter 		ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
5768664281bSPaulo Zanoni 	else
5772ae2a50cSDaniel Vetter 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable, old);
5788664281bSPaulo Zanoni 
5798664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
5802ae2a50cSDaniel Vetter 	return old;
5818664281bSPaulo Zanoni }
5828664281bSPaulo Zanoni 
5838664281bSPaulo Zanoni 
584b5ea642aSDaniel Vetter static void
585755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
586755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5877c463586SKeith Packard {
5889db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
589755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5907c463586SKeith Packard 
591b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
592b79480baSDaniel Vetter 
59304feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
59404feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
59504feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
59604feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
597755e9019SImre Deak 		return;
598755e9019SImre Deak 
599755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
60046c06a30SVille Syrjälä 		return;
60146c06a30SVille Syrjälä 
60291d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
60391d181ddSImre Deak 
6047c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
605755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
60646c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
6073143a2bfSChris Wilson 	POSTING_READ(reg);
6087c463586SKeith Packard }
6097c463586SKeith Packard 
610b5ea642aSDaniel Vetter static void
611755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
612755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
6137c463586SKeith Packard {
6149db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
615755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
6167c463586SKeith Packard 
617b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
618b79480baSDaniel Vetter 
61904feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
62004feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
62104feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
62204feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
62346c06a30SVille Syrjälä 		return;
62446c06a30SVille Syrjälä 
625755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
626755e9019SImre Deak 		return;
627755e9019SImre Deak 
62891d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
62991d181ddSImre Deak 
630755e9019SImre Deak 	pipestat &= ~enable_mask;
63146c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
6323143a2bfSChris Wilson 	POSTING_READ(reg);
6337c463586SKeith Packard }
6347c463586SKeith Packard 
63510c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
63610c59c51SImre Deak {
63710c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
63810c59c51SImre Deak 
63910c59c51SImre Deak 	/*
640724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
641724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
64210c59c51SImre Deak 	 */
64310c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
64410c59c51SImre Deak 		return 0;
645724a6905SVille Syrjälä 	/*
646724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
647724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
648724a6905SVille Syrjälä 	 */
649724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
650724a6905SVille Syrjälä 		return 0;
65110c59c51SImre Deak 
65210c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
65310c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
65410c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
65510c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
65610c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
65710c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
65810c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
65910c59c51SImre Deak 
66010c59c51SImre Deak 	return enable_mask;
66110c59c51SImre Deak }
66210c59c51SImre Deak 
663755e9019SImre Deak void
664755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
665755e9019SImre Deak 		     u32 status_mask)
666755e9019SImre Deak {
667755e9019SImre Deak 	u32 enable_mask;
668755e9019SImre Deak 
66910c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
67010c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
67110c59c51SImre Deak 							   status_mask);
67210c59c51SImre Deak 	else
673755e9019SImre Deak 		enable_mask = status_mask << 16;
674755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
675755e9019SImre Deak }
676755e9019SImre Deak 
677755e9019SImre Deak void
678755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
679755e9019SImre Deak 		      u32 status_mask)
680755e9019SImre Deak {
681755e9019SImre Deak 	u32 enable_mask;
682755e9019SImre Deak 
68310c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
68410c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
68510c59c51SImre Deak 							   status_mask);
68610c59c51SImre Deak 	else
687755e9019SImre Deak 		enable_mask = status_mask << 16;
688755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
689755e9019SImre Deak }
690755e9019SImre Deak 
691c0e09200SDave Airlie /**
692f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
69301c66889SZhao Yakui  */
694f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
69501c66889SZhao Yakui {
6962d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6971ec14ad3SChris Wilson 	unsigned long irqflags;
6981ec14ad3SChris Wilson 
699f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
700f49e38ddSJani Nikula 		return;
701f49e38ddSJani Nikula 
7021ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
70301c66889SZhao Yakui 
704755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
705a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
7063b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
707755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
7081ec14ad3SChris Wilson 
7091ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
71001c66889SZhao Yakui }
71101c66889SZhao Yakui 
71201c66889SZhao Yakui /**
7130a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
7140a3e67a4SJesse Barnes  * @dev: DRM device
7150a3e67a4SJesse Barnes  * @pipe: pipe to check
7160a3e67a4SJesse Barnes  *
7170a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
7180a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
7190a3e67a4SJesse Barnes  * before reading such registers if unsure.
7200a3e67a4SJesse Barnes  */
7210a3e67a4SJesse Barnes static int
7220a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
7230a3e67a4SJesse Barnes {
7242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
725702e7a56SPaulo Zanoni 
726a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
727a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
728a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
729a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73071f8ba6bSPaulo Zanoni 
731a01025afSDaniel Vetter 		return intel_crtc->active;
732a01025afSDaniel Vetter 	} else {
733a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
734a01025afSDaniel Vetter 	}
7350a3e67a4SJesse Barnes }
7360a3e67a4SJesse Barnes 
737f75f3746SVille Syrjälä /*
738f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
739f75f3746SVille Syrjälä  * around the vertical blanking period.
740f75f3746SVille Syrjälä  *
741f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
742f75f3746SVille Syrjälä  *  vblank_start >= 3
743f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
744f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
745f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
746f75f3746SVille Syrjälä  *
747f75f3746SVille Syrjälä  *           start of vblank:
748f75f3746SVille Syrjälä  *           latch double buffered registers
749f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
750f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
751f75f3746SVille Syrjälä  *           |
752f75f3746SVille Syrjälä  *           |          frame start:
753f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
754f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
755f75f3746SVille Syrjälä  *           |          |
756f75f3746SVille Syrjälä  *           |          |  start of vsync:
757f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
758f75f3746SVille Syrjälä  *           |          |  |
759f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
760f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
761f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
762f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
763f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
764f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
765f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
766f75f3746SVille Syrjälä  *       |          |                                         |
767f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
768f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
769f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
770f75f3746SVille Syrjälä  *
771f75f3746SVille Syrjälä  * x  = horizontal active
772f75f3746SVille Syrjälä  * _  = horizontal blanking
773f75f3746SVille Syrjälä  * hs = horizontal sync
774f75f3746SVille Syrjälä  * va = vertical active
775f75f3746SVille Syrjälä  * vb = vertical blanking
776f75f3746SVille Syrjälä  * vs = vertical sync
777f75f3746SVille Syrjälä  * vbs = vblank_start (number)
778f75f3746SVille Syrjälä  *
779f75f3746SVille Syrjälä  * Summary:
780f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
781f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
782f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
783f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
784f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
785f75f3746SVille Syrjälä  */
786f75f3746SVille Syrjälä 
7874cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
7884cdb83ecSVille Syrjälä {
7894cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
7904cdb83ecSVille Syrjälä 	return 0;
7914cdb83ecSVille Syrjälä }
7924cdb83ecSVille Syrjälä 
79342f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
79442f52ef8SKeith Packard  * we use as a pipe index
79542f52ef8SKeith Packard  */
796f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
7970a3e67a4SJesse Barnes {
7982d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
7990a3e67a4SJesse Barnes 	unsigned long high_frame;
8000a3e67a4SJesse Barnes 	unsigned long low_frame;
8010b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
8020a3e67a4SJesse Barnes 
8030a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
80444d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
8059db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
8060a3e67a4SJesse Barnes 		return 0;
8070a3e67a4SJesse Barnes 	}
8080a3e67a4SJesse Barnes 
809391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
810391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
811391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
812391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
813391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
814391f75e2SVille Syrjälä 
8150b2a8e09SVille Syrjälä 		htotal = mode->crtc_htotal;
8160b2a8e09SVille Syrjälä 		hsync_start = mode->crtc_hsync_start;
8170b2a8e09SVille Syrjälä 		vbl_start = mode->crtc_vblank_start;
8180b2a8e09SVille Syrjälä 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
8190b2a8e09SVille Syrjälä 			vbl_start = DIV_ROUND_UP(vbl_start, 2);
820391f75e2SVille Syrjälä 	} else {
821a2d213ddSDaniel Vetter 		enum transcoder cpu_transcoder = (enum transcoder) pipe;
822391f75e2SVille Syrjälä 
823391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
8240b2a8e09SVille Syrjälä 		hsync_start = (I915_READ(HSYNC(cpu_transcoder))  & 0x1fff) + 1;
825391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
8260b2a8e09SVille Syrjälä 		if ((I915_READ(PIPECONF(cpu_transcoder)) &
8270b2a8e09SVille Syrjälä 		     PIPECONF_INTERLACE_MASK) != PIPECONF_PROGRESSIVE)
8280b2a8e09SVille Syrjälä 			vbl_start = DIV_ROUND_UP(vbl_start, 2);
829391f75e2SVille Syrjälä 	}
830391f75e2SVille Syrjälä 
8310b2a8e09SVille Syrjälä 	/* Convert to pixel count */
8320b2a8e09SVille Syrjälä 	vbl_start *= htotal;
8330b2a8e09SVille Syrjälä 
8340b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
8350b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
8360b2a8e09SVille Syrjälä 
8379db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
8389db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
8395eddb70bSChris Wilson 
8400a3e67a4SJesse Barnes 	/*
8410a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
8420a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
8430a3e67a4SJesse Barnes 	 * register.
8440a3e67a4SJesse Barnes 	 */
8450a3e67a4SJesse Barnes 	do {
8465eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
847391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
8485eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
8490a3e67a4SJesse Barnes 	} while (high1 != high2);
8500a3e67a4SJesse Barnes 
8515eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
852391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
8535eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
854391f75e2SVille Syrjälä 
855391f75e2SVille Syrjälä 	/*
856391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
857391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
858391f75e2SVille Syrjälä 	 * counter against vblank start.
859391f75e2SVille Syrjälä 	 */
860edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
8610a3e67a4SJesse Barnes }
8620a3e67a4SJesse Barnes 
863f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
8649880b7a5SJesse Barnes {
8652d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
8669db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
8679880b7a5SJesse Barnes 
8689880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
86944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
8709db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
8719880b7a5SJesse Barnes 		return 0;
8729880b7a5SJesse Barnes 	}
8739880b7a5SJesse Barnes 
8749880b7a5SJesse Barnes 	return I915_READ(reg);
8759880b7a5SJesse Barnes }
8769880b7a5SJesse Barnes 
877ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
878ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
879ad3543edSMario Kleiner 
880a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
881a225f079SVille Syrjälä {
882a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
883a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
884a225f079SVille Syrjälä 	const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
885a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
88680715b2fSVille Syrjälä 	int position, vtotal;
887a225f079SVille Syrjälä 
88880715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
889a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
890a225f079SVille Syrjälä 		vtotal /= 2;
891a225f079SVille Syrjälä 
892a225f079SVille Syrjälä 	if (IS_GEN2(dev))
893a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
894a225f079SVille Syrjälä 	else
895a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
896a225f079SVille Syrjälä 
897a225f079SVille Syrjälä 	/*
89880715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
89980715b2fSVille Syrjälä 	 * scanline_offset adjustment.
900a225f079SVille Syrjälä 	 */
90180715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
902a225f079SVille Syrjälä }
903a225f079SVille Syrjälä 
904f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
905abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
906abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
9070af7e4dfSMario Kleiner {
908c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
909c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
910c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
911c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
9123aa18df8SVille Syrjälä 	int position;
91378e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
9140af7e4dfSMario Kleiner 	bool in_vbl = true;
9150af7e4dfSMario Kleiner 	int ret = 0;
916ad3543edSMario Kleiner 	unsigned long irqflags;
9170af7e4dfSMario Kleiner 
918c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
9190af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
9209db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
9210af7e4dfSMario Kleiner 		return 0;
9220af7e4dfSMario Kleiner 	}
9230af7e4dfSMario Kleiner 
924c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
92578e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
926c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
927c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
928c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
9290af7e4dfSMario Kleiner 
930d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
931d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
932d31faf65SVille Syrjälä 		vbl_end /= 2;
933d31faf65SVille Syrjälä 		vtotal /= 2;
934d31faf65SVille Syrjälä 	}
935d31faf65SVille Syrjälä 
936c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
937c2baf4b7SVille Syrjälä 
938ad3543edSMario Kleiner 	/*
939ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
940ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
941ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
942ad3543edSMario Kleiner 	 */
943ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
944ad3543edSMario Kleiner 
945ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
946ad3543edSMario Kleiner 
947ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
948ad3543edSMario Kleiner 	if (stime)
949ad3543edSMario Kleiner 		*stime = ktime_get();
950ad3543edSMario Kleiner 
9517c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
9520af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
9530af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
9540af7e4dfSMario Kleiner 		 */
955a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
9560af7e4dfSMario Kleiner 	} else {
9570af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
9580af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
9590af7e4dfSMario Kleiner 		 * scanout position.
9600af7e4dfSMario Kleiner 		 */
961ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
9620af7e4dfSMario Kleiner 
9633aa18df8SVille Syrjälä 		/* convert to pixel counts */
9643aa18df8SVille Syrjälä 		vbl_start *= htotal;
9653aa18df8SVille Syrjälä 		vbl_end *= htotal;
9663aa18df8SVille Syrjälä 		vtotal *= htotal;
96778e8fc6bSVille Syrjälä 
96878e8fc6bSVille Syrjälä 		/*
9697e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
9707e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
9717e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
9727e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
9737e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
9747e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
9757e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
9767e78f1cbSVille Syrjälä 		 */
9777e78f1cbSVille Syrjälä 		if (position >= vtotal)
9787e78f1cbSVille Syrjälä 			position = vtotal - 1;
9797e78f1cbSVille Syrjälä 
9807e78f1cbSVille Syrjälä 		/*
98178e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
98278e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
98378e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
98478e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
98578e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
98678e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
98778e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
98878e8fc6bSVille Syrjälä 		 */
98978e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9903aa18df8SVille Syrjälä 	}
9913aa18df8SVille Syrjälä 
992ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
993ad3543edSMario Kleiner 	if (etime)
994ad3543edSMario Kleiner 		*etime = ktime_get();
995ad3543edSMario Kleiner 
996ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
997ad3543edSMario Kleiner 
998ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
999ad3543edSMario Kleiner 
10003aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
10013aa18df8SVille Syrjälä 
10023aa18df8SVille Syrjälä 	/*
10033aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
10043aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
10053aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
10063aa18df8SVille Syrjälä 	 * up since vbl_end.
10073aa18df8SVille Syrjälä 	 */
10083aa18df8SVille Syrjälä 	if (position >= vbl_start)
10093aa18df8SVille Syrjälä 		position -= vbl_end;
10103aa18df8SVille Syrjälä 	else
10113aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
10123aa18df8SVille Syrjälä 
10137c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
10143aa18df8SVille Syrjälä 		*vpos = position;
10153aa18df8SVille Syrjälä 		*hpos = 0;
10163aa18df8SVille Syrjälä 	} else {
10170af7e4dfSMario Kleiner 		*vpos = position / htotal;
10180af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
10190af7e4dfSMario Kleiner 	}
10200af7e4dfSMario Kleiner 
10210af7e4dfSMario Kleiner 	/* In vblank? */
10220af7e4dfSMario Kleiner 	if (in_vbl)
10230af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
10240af7e4dfSMario Kleiner 
10250af7e4dfSMario Kleiner 	return ret;
10260af7e4dfSMario Kleiner }
10270af7e4dfSMario Kleiner 
1028a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
1029a225f079SVille Syrjälä {
1030a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1031a225f079SVille Syrjälä 	unsigned long irqflags;
1032a225f079SVille Syrjälä 	int position;
1033a225f079SVille Syrjälä 
1034a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
1035a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
1036a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1037a225f079SVille Syrjälä 
1038a225f079SVille Syrjälä 	return position;
1039a225f079SVille Syrjälä }
1040a225f079SVille Syrjälä 
1041f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
10420af7e4dfSMario Kleiner 			      int *max_error,
10430af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
10440af7e4dfSMario Kleiner 			      unsigned flags)
10450af7e4dfSMario Kleiner {
10464041b853SChris Wilson 	struct drm_crtc *crtc;
10470af7e4dfSMario Kleiner 
10487eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
10494041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
10500af7e4dfSMario Kleiner 		return -EINVAL;
10510af7e4dfSMario Kleiner 	}
10520af7e4dfSMario Kleiner 
10530af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
10544041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
10554041b853SChris Wilson 	if (crtc == NULL) {
10564041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
10574041b853SChris Wilson 		return -EINVAL;
10584041b853SChris Wilson 	}
10594041b853SChris Wilson 
10604041b853SChris Wilson 	if (!crtc->enabled) {
10614041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
10624041b853SChris Wilson 		return -EBUSY;
10634041b853SChris Wilson 	}
10640af7e4dfSMario Kleiner 
10650af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
10664041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
10674041b853SChris Wilson 						     vblank_time, flags,
10687da903efSVille Syrjälä 						     crtc,
10697da903efSVille Syrjälä 						     &to_intel_crtc(crtc)->config.adjusted_mode);
10700af7e4dfSMario Kleiner }
10710af7e4dfSMario Kleiner 
107267c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
107367c347ffSJani Nikula 				struct drm_connector *connector)
1074321a1b30SEgbert Eich {
1075321a1b30SEgbert Eich 	enum drm_connector_status old_status;
1076321a1b30SEgbert Eich 
1077321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
1078321a1b30SEgbert Eich 	old_status = connector->status;
1079321a1b30SEgbert Eich 
1080321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
108167c347ffSJani Nikula 	if (old_status == connector->status)
108267c347ffSJani Nikula 		return false;
108367c347ffSJani Nikula 
108467c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
1085321a1b30SEgbert Eich 		      connector->base.id,
1086c23cc417SJani Nikula 		      connector->name,
108767c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
108867c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
108967c347ffSJani Nikula 
109067c347ffSJani Nikula 	return true;
1091321a1b30SEgbert Eich }
1092321a1b30SEgbert Eich 
109313cf5504SDave Airlie static void i915_digport_work_func(struct work_struct *work)
109413cf5504SDave Airlie {
109513cf5504SDave Airlie 	struct drm_i915_private *dev_priv =
109613cf5504SDave Airlie 		container_of(work, struct drm_i915_private, dig_port_work);
109713cf5504SDave Airlie 	unsigned long irqflags;
109813cf5504SDave Airlie 	u32 long_port_mask, short_port_mask;
109913cf5504SDave Airlie 	struct intel_digital_port *intel_dig_port;
110013cf5504SDave Airlie 	int i, ret;
110113cf5504SDave Airlie 	u32 old_bits = 0;
110213cf5504SDave Airlie 
110313cf5504SDave Airlie 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
110413cf5504SDave Airlie 	long_port_mask = dev_priv->long_hpd_port_mask;
110513cf5504SDave Airlie 	dev_priv->long_hpd_port_mask = 0;
110613cf5504SDave Airlie 	short_port_mask = dev_priv->short_hpd_port_mask;
110713cf5504SDave Airlie 	dev_priv->short_hpd_port_mask = 0;
110813cf5504SDave Airlie 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
110913cf5504SDave Airlie 
111013cf5504SDave Airlie 	for (i = 0; i < I915_MAX_PORTS; i++) {
111113cf5504SDave Airlie 		bool valid = false;
111213cf5504SDave Airlie 		bool long_hpd = false;
111313cf5504SDave Airlie 		intel_dig_port = dev_priv->hpd_irq_port[i];
111413cf5504SDave Airlie 		if (!intel_dig_port || !intel_dig_port->hpd_pulse)
111513cf5504SDave Airlie 			continue;
111613cf5504SDave Airlie 
111713cf5504SDave Airlie 		if (long_port_mask & (1 << i))  {
111813cf5504SDave Airlie 			valid = true;
111913cf5504SDave Airlie 			long_hpd = true;
112013cf5504SDave Airlie 		} else if (short_port_mask & (1 << i))
112113cf5504SDave Airlie 			valid = true;
112213cf5504SDave Airlie 
112313cf5504SDave Airlie 		if (valid) {
112413cf5504SDave Airlie 			ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
112513cf5504SDave Airlie 			if (ret == true) {
112613cf5504SDave Airlie 				/* if we get true fallback to old school hpd */
112713cf5504SDave Airlie 				old_bits |= (1 << intel_dig_port->base.hpd_pin);
112813cf5504SDave Airlie 			}
112913cf5504SDave Airlie 		}
113013cf5504SDave Airlie 	}
113113cf5504SDave Airlie 
113213cf5504SDave Airlie 	if (old_bits) {
113313cf5504SDave Airlie 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
113413cf5504SDave Airlie 		dev_priv->hpd_event_bits |= old_bits;
113513cf5504SDave Airlie 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
113613cf5504SDave Airlie 		schedule_work(&dev_priv->hotplug_work);
113713cf5504SDave Airlie 	}
113813cf5504SDave Airlie }
113913cf5504SDave Airlie 
11405ca58282SJesse Barnes /*
11415ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
11425ca58282SJesse Barnes  */
1143ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
1144ac4c16c5SEgbert Eich 
11455ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
11465ca58282SJesse Barnes {
11472d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11482d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, hotplug_work);
11495ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1150c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
1151cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
1152cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
1153cd569aedSEgbert Eich 	struct drm_connector *connector;
1154cd569aedSEgbert Eich 	unsigned long irqflags;
1155cd569aedSEgbert Eich 	bool hpd_disabled = false;
1156321a1b30SEgbert Eich 	bool changed = false;
1157142e2398SEgbert Eich 	u32 hpd_event_bits;
11585ca58282SJesse Barnes 
1159a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
1160e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
1161e67189abSJesse Barnes 
1162cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1163142e2398SEgbert Eich 
1164142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
1165142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
1166cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
1167cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
116836cd7444SDave Airlie 		if (!intel_connector->encoder)
116936cd7444SDave Airlie 			continue;
1170cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
1171cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
1172cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
1173cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
1174cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
1175cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
1176c23cc417SJani Nikula 				connector->name);
1177cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
1178cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
1179cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
1180cd569aedSEgbert Eich 			hpd_disabled = true;
1181cd569aedSEgbert Eich 		}
1182142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1183142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
1184c23cc417SJani Nikula 				      connector->name, intel_encoder->hpd_pin);
1185142e2398SEgbert Eich 		}
1186cd569aedSEgbert Eich 	}
1187cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
1188cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
1189cd569aedSEgbert Eich 	  * some connectors */
1190ac4c16c5SEgbert Eich 	if (hpd_disabled) {
1191cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
1192ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
1193ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
1194ac4c16c5SEgbert Eich 	}
1195cd569aedSEgbert Eich 
1196cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1197cd569aedSEgbert Eich 
1198321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
1199321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
120036cd7444SDave Airlie 		if (!intel_connector->encoder)
120136cd7444SDave Airlie 			continue;
1202321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
1203321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1204cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
1205cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
1206321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
1207321a1b30SEgbert Eich 				changed = true;
1208321a1b30SEgbert Eich 		}
1209321a1b30SEgbert Eich 	}
121040ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
121140ee3381SKeith Packard 
1212321a1b30SEgbert Eich 	if (changed)
1213321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
12145ca58282SJesse Barnes }
12155ca58282SJesse Barnes 
12163ca1ccedSVille Syrjälä static void intel_hpd_irq_uninstall(struct drm_i915_private *dev_priv)
12173ca1ccedSVille Syrjälä {
12183ca1ccedSVille Syrjälä 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
12193ca1ccedSVille Syrjälä }
12203ca1ccedSVille Syrjälä 
1221d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
1222f97108d1SJesse Barnes {
12232d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1224b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
12259270388eSDaniel Vetter 	u8 new_delay;
12269270388eSDaniel Vetter 
1227d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1228f97108d1SJesse Barnes 
122973edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
123073edd18fSDaniel Vetter 
123120e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
12329270388eSDaniel Vetter 
12337648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1234b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1235b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1236f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1237f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1238f97108d1SJesse Barnes 
1239f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1240b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
124120e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
124220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
124320e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
124420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1245b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
124620e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
124720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
124820e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
124920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1250f97108d1SJesse Barnes 	}
1251f97108d1SJesse Barnes 
12527648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
125320e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1254f97108d1SJesse Barnes 
1255d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
12569270388eSDaniel Vetter 
1257f97108d1SJesse Barnes 	return;
1258f97108d1SJesse Barnes }
1259f97108d1SJesse Barnes 
1260549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
1261a4872ba6SOscar Mateo 			struct intel_engine_cs *ring)
1262549f7365SChris Wilson {
126393b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
1264475553deSChris Wilson 		return;
1265475553deSChris Wilson 
1266814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
12679862e600SChris Wilson 
126884c33a64SSourab Gupta 	if (drm_core_check_feature(dev, DRIVER_MODESET))
126984c33a64SSourab Gupta 		intel_notify_mmio_flip(ring);
127084c33a64SSourab Gupta 
1271549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
127210cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
1273549f7365SChris Wilson }
1274549f7365SChris Wilson 
127531685c25SDeepak S static u32 vlv_c0_residency(struct drm_i915_private *dev_priv,
1276bf225f20SChris Wilson 			    struct intel_rps_ei *rps_ei)
127731685c25SDeepak S {
127831685c25SDeepak S 	u32 cz_ts, cz_freq_khz;
127931685c25SDeepak S 	u32 render_count, media_count;
128031685c25SDeepak S 	u32 elapsed_render, elapsed_media, elapsed_time;
128131685c25SDeepak S 	u32 residency = 0;
128231685c25SDeepak S 
128331685c25SDeepak S 	cz_ts = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
128431685c25SDeepak S 	cz_freq_khz = DIV_ROUND_CLOSEST(dev_priv->mem_freq * 1000, 4);
128531685c25SDeepak S 
128631685c25SDeepak S 	render_count = I915_READ(VLV_RENDER_C0_COUNT_REG);
128731685c25SDeepak S 	media_count = I915_READ(VLV_MEDIA_C0_COUNT_REG);
128831685c25SDeepak S 
1289bf225f20SChris Wilson 	if (rps_ei->cz_clock == 0) {
1290bf225f20SChris Wilson 		rps_ei->cz_clock = cz_ts;
1291bf225f20SChris Wilson 		rps_ei->render_c0 = render_count;
1292bf225f20SChris Wilson 		rps_ei->media_c0 = media_count;
129331685c25SDeepak S 
129431685c25SDeepak S 		return dev_priv->rps.cur_freq;
129531685c25SDeepak S 	}
129631685c25SDeepak S 
1297bf225f20SChris Wilson 	elapsed_time = cz_ts - rps_ei->cz_clock;
1298bf225f20SChris Wilson 	rps_ei->cz_clock = cz_ts;
129931685c25SDeepak S 
1300bf225f20SChris Wilson 	elapsed_render = render_count - rps_ei->render_c0;
1301bf225f20SChris Wilson 	rps_ei->render_c0 = render_count;
130231685c25SDeepak S 
1303bf225f20SChris Wilson 	elapsed_media = media_count - rps_ei->media_c0;
1304bf225f20SChris Wilson 	rps_ei->media_c0 = media_count;
130531685c25SDeepak S 
130631685c25SDeepak S 	/* Convert all the counters into common unit of milli sec */
130731685c25SDeepak S 	elapsed_time /= VLV_CZ_CLOCK_TO_MILLI_SEC;
130831685c25SDeepak S 	elapsed_render /=  cz_freq_khz;
130931685c25SDeepak S 	elapsed_media /= cz_freq_khz;
131031685c25SDeepak S 
131131685c25SDeepak S 	/*
131231685c25SDeepak S 	 * Calculate overall C0 residency percentage
131331685c25SDeepak S 	 * only if elapsed time is non zero
131431685c25SDeepak S 	 */
131531685c25SDeepak S 	if (elapsed_time) {
131631685c25SDeepak S 		residency =
131731685c25SDeepak S 			((max(elapsed_render, elapsed_media) * 100)
131831685c25SDeepak S 				/ elapsed_time);
131931685c25SDeepak S 	}
132031685c25SDeepak S 
132131685c25SDeepak S 	return residency;
132231685c25SDeepak S }
132331685c25SDeepak S 
132431685c25SDeepak S /**
132531685c25SDeepak S  * vlv_calc_delay_from_C0_counters - Increase/Decrease freq based on GPU
132631685c25SDeepak S  * busy-ness calculated from C0 counters of render & media power wells
132731685c25SDeepak S  * @dev_priv: DRM device private
132831685c25SDeepak S  *
132931685c25SDeepak S  */
13304fa79042SDamien Lespiau static int vlv_calc_delay_from_C0_counters(struct drm_i915_private *dev_priv)
133131685c25SDeepak S {
133231685c25SDeepak S 	u32 residency_C0_up = 0, residency_C0_down = 0;
13334fa79042SDamien Lespiau 	int new_delay, adj;
133431685c25SDeepak S 
133531685c25SDeepak S 	dev_priv->rps.ei_interrupt_count++;
133631685c25SDeepak S 
133731685c25SDeepak S 	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
133831685c25SDeepak S 
133931685c25SDeepak S 
1340bf225f20SChris Wilson 	if (dev_priv->rps.up_ei.cz_clock == 0) {
1341bf225f20SChris Wilson 		vlv_c0_residency(dev_priv, &dev_priv->rps.up_ei);
1342bf225f20SChris Wilson 		vlv_c0_residency(dev_priv, &dev_priv->rps.down_ei);
134331685c25SDeepak S 		return dev_priv->rps.cur_freq;
134431685c25SDeepak S 	}
134531685c25SDeepak S 
134631685c25SDeepak S 
134731685c25SDeepak S 	/*
134831685c25SDeepak S 	 * To down throttle, C0 residency should be less than down threshold
134931685c25SDeepak S 	 * for continous EI intervals. So calculate down EI counters
135031685c25SDeepak S 	 * once in VLV_INT_COUNT_FOR_DOWN_EI
135131685c25SDeepak S 	 */
135231685c25SDeepak S 	if (dev_priv->rps.ei_interrupt_count == VLV_INT_COUNT_FOR_DOWN_EI) {
135331685c25SDeepak S 
135431685c25SDeepak S 		dev_priv->rps.ei_interrupt_count = 0;
135531685c25SDeepak S 
135631685c25SDeepak S 		residency_C0_down = vlv_c0_residency(dev_priv,
1357bf225f20SChris Wilson 						     &dev_priv->rps.down_ei);
135831685c25SDeepak S 	} else {
135931685c25SDeepak S 		residency_C0_up = vlv_c0_residency(dev_priv,
1360bf225f20SChris Wilson 						   &dev_priv->rps.up_ei);
136131685c25SDeepak S 	}
136231685c25SDeepak S 
136331685c25SDeepak S 	new_delay = dev_priv->rps.cur_freq;
136431685c25SDeepak S 
136531685c25SDeepak S 	adj = dev_priv->rps.last_adj;
136631685c25SDeepak S 	/* C0 residency is greater than UP threshold. Increase Frequency */
136731685c25SDeepak S 	if (residency_C0_up >= VLV_RP_UP_EI_THRESHOLD) {
136831685c25SDeepak S 		if (adj > 0)
136931685c25SDeepak S 			adj *= 2;
137031685c25SDeepak S 		else
137131685c25SDeepak S 			adj = 1;
137231685c25SDeepak S 
137331685c25SDeepak S 		if (dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit)
137431685c25SDeepak S 			new_delay = dev_priv->rps.cur_freq + adj;
137531685c25SDeepak S 
137631685c25SDeepak S 		/*
137731685c25SDeepak S 		 * For better performance, jump directly
137831685c25SDeepak S 		 * to RPe if we're below it.
137931685c25SDeepak S 		 */
138031685c25SDeepak S 		if (new_delay < dev_priv->rps.efficient_freq)
138131685c25SDeepak S 			new_delay = dev_priv->rps.efficient_freq;
138231685c25SDeepak S 
138331685c25SDeepak S 	} else if (!dev_priv->rps.ei_interrupt_count &&
138431685c25SDeepak S 			(residency_C0_down < VLV_RP_DOWN_EI_THRESHOLD)) {
138531685c25SDeepak S 		if (adj < 0)
138631685c25SDeepak S 			adj *= 2;
138731685c25SDeepak S 		else
138831685c25SDeepak S 			adj = -1;
138931685c25SDeepak S 		/*
139031685c25SDeepak S 		 * This means, C0 residency is less than down threshold over
139131685c25SDeepak S 		 * a period of VLV_INT_COUNT_FOR_DOWN_EI. So, reduce the freq
139231685c25SDeepak S 		 */
139331685c25SDeepak S 		if (dev_priv->rps.cur_freq > dev_priv->rps.min_freq_softlimit)
139431685c25SDeepak S 			new_delay = dev_priv->rps.cur_freq + adj;
139531685c25SDeepak S 	}
139631685c25SDeepak S 
139731685c25SDeepak S 	return new_delay;
139831685c25SDeepak S }
139931685c25SDeepak S 
14004912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
14013b8d8d91SJesse Barnes {
14022d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
14032d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
1404edbfdb45SPaulo Zanoni 	u32 pm_iir;
1405dd75fdc8SChris Wilson 	int new_delay, adj;
14063b8d8d91SJesse Barnes 
140759cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1408c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1409c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
14106af257cdSDamien Lespiau 	if (INTEL_INFO(dev_priv->dev)->gen >= 8)
1411480c8033SDaniel Vetter 		gen8_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
14120961021aSBen Widawsky 	else {
14130961021aSBen Widawsky 		/* Make sure not to corrupt PMIMR state used by ringbuffer */
1414480c8033SDaniel Vetter 		gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
14150961021aSBen Widawsky 	}
141659cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
14174912d041SBen Widawsky 
141860611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1419a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
142060611c13SPaulo Zanoni 
1421a6706b45SDeepak S 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
14223b8d8d91SJesse Barnes 		return;
14233b8d8d91SJesse Barnes 
14244fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
14257b9e0ae6SChris Wilson 
1426dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
14277425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1428dd75fdc8SChris Wilson 		if (adj > 0)
1429dd75fdc8SChris Wilson 			adj *= 2;
143013a5660cSDeepak S 		else {
143113a5660cSDeepak S 			/* CHV needs even encode values */
143213a5660cSDeepak S 			adj = IS_CHERRYVIEW(dev_priv->dev) ? 2 : 1;
143313a5660cSDeepak S 		}
1434b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
14357425034aSVille Syrjälä 
14367425034aSVille Syrjälä 		/*
14377425034aSVille Syrjälä 		 * For better performance, jump directly
14387425034aSVille Syrjälä 		 * to RPe if we're below it.
14397425034aSVille Syrjälä 		 */
1440b39fb297SBen Widawsky 		if (new_delay < dev_priv->rps.efficient_freq)
1441b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1442dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1443b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1444b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1445dd75fdc8SChris Wilson 		else
1446b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1447dd75fdc8SChris Wilson 		adj = 0;
144831685c25SDeepak S 	} else if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
144931685c25SDeepak S 		new_delay = vlv_calc_delay_from_C0_counters(dev_priv);
1450dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1451dd75fdc8SChris Wilson 		if (adj < 0)
1452dd75fdc8SChris Wilson 			adj *= 2;
145313a5660cSDeepak S 		else {
145413a5660cSDeepak S 			/* CHV needs even encode values */
145513a5660cSDeepak S 			adj = IS_CHERRYVIEW(dev_priv->dev) ? -2 : -1;
145613a5660cSDeepak S 		}
1457b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
1458dd75fdc8SChris Wilson 	} else { /* unknown event */
1459b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq;
1460dd75fdc8SChris Wilson 	}
14613b8d8d91SJesse Barnes 
146279249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
146379249636SBen Widawsky 	 * interrupt
146479249636SBen Widawsky 	 */
14651272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
1466b39fb297SBen Widawsky 			    dev_priv->rps.min_freq_softlimit,
1467b39fb297SBen Widawsky 			    dev_priv->rps.max_freq_softlimit);
146827544369SDeepak S 
1469b39fb297SBen Widawsky 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
1470dd75fdc8SChris Wilson 
14710a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
14720a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
14730a073b84SJesse Barnes 	else
14744912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
14753b8d8d91SJesse Barnes 
14764fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
14773b8d8d91SJesse Barnes }
14783b8d8d91SJesse Barnes 
1479e3689190SBen Widawsky 
1480e3689190SBen Widawsky /**
1481e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1482e3689190SBen Widawsky  * occurred.
1483e3689190SBen Widawsky  * @work: workqueue struct
1484e3689190SBen Widawsky  *
1485e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1486e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1487e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1488e3689190SBen Widawsky  */
1489e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1490e3689190SBen Widawsky {
14912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
14922d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1493e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
149435a85ac6SBen Widawsky 	char *parity_event[6];
1495e3689190SBen Widawsky 	uint32_t misccpctl;
1496e3689190SBen Widawsky 	unsigned long flags;
149735a85ac6SBen Widawsky 	uint8_t slice = 0;
1498e3689190SBen Widawsky 
1499e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1500e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1501e3689190SBen Widawsky 	 * any time we access those registers.
1502e3689190SBen Widawsky 	 */
1503e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1504e3689190SBen Widawsky 
150535a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
150635a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
150735a85ac6SBen Widawsky 		goto out;
150835a85ac6SBen Widawsky 
1509e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1510e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1511e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1512e3689190SBen Widawsky 
151335a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
151435a85ac6SBen Widawsky 		u32 reg;
151535a85ac6SBen Widawsky 
151635a85ac6SBen Widawsky 		slice--;
151735a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
151835a85ac6SBen Widawsky 			break;
151935a85ac6SBen Widawsky 
152035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
152135a85ac6SBen Widawsky 
152235a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
152335a85ac6SBen Widawsky 
152435a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1525e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1526e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1527e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1528e3689190SBen Widawsky 
152935a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
153035a85ac6SBen Widawsky 		POSTING_READ(reg);
1531e3689190SBen Widawsky 
1532cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1533e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1534e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1535e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
153635a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
153735a85ac6SBen Widawsky 		parity_event[5] = NULL;
1538e3689190SBen Widawsky 
15395bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1540e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1541e3689190SBen Widawsky 
154235a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
154335a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1544e3689190SBen Widawsky 
154535a85ac6SBen Widawsky 		kfree(parity_event[4]);
1546e3689190SBen Widawsky 		kfree(parity_event[3]);
1547e3689190SBen Widawsky 		kfree(parity_event[2]);
1548e3689190SBen Widawsky 		kfree(parity_event[1]);
1549e3689190SBen Widawsky 	}
1550e3689190SBen Widawsky 
155135a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
155235a85ac6SBen Widawsky 
155335a85ac6SBen Widawsky out:
155435a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
155535a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
1556480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
155735a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
155835a85ac6SBen Widawsky 
155935a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
156035a85ac6SBen Widawsky }
156135a85ac6SBen Widawsky 
156235a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1563e3689190SBen Widawsky {
15642d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1565e3689190SBen Widawsky 
1566040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1567e3689190SBen Widawsky 		return;
1568e3689190SBen Widawsky 
1569d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1570480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1571d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1572e3689190SBen Widawsky 
157335a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
157435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
157535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
157635a85ac6SBen Widawsky 
157735a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
157835a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
157935a85ac6SBen Widawsky 
1580a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1581e3689190SBen Widawsky }
1582e3689190SBen Widawsky 
1583f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1584f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1585f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1586f1af8fc1SPaulo Zanoni {
1587f1af8fc1SPaulo Zanoni 	if (gt_iir &
1588f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1589f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1590f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1591f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1592f1af8fc1SPaulo Zanoni }
1593f1af8fc1SPaulo Zanoni 
1594e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1595e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1596e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1597e7b4c6b1SDaniel Vetter {
1598e7b4c6b1SDaniel Vetter 
1599cc609d5dSBen Widawsky 	if (gt_iir &
1600cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1601e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1602cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1603e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1604cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1605e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1606e7b4c6b1SDaniel Vetter 
1607cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1608cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1609cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
161058174462SMika Kuoppala 		i915_handle_error(dev, false, "GT error interrupt 0x%08x",
161158174462SMika Kuoppala 				  gt_iir);
1612e7b4c6b1SDaniel Vetter 	}
1613e3689190SBen Widawsky 
161435a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
161535a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1616e7b4c6b1SDaniel Vetter }
1617e7b4c6b1SDaniel Vetter 
16180961021aSBen Widawsky static void gen8_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
16190961021aSBen Widawsky {
16200961021aSBen Widawsky 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
16210961021aSBen Widawsky 		return;
16220961021aSBen Widawsky 
16230961021aSBen Widawsky 	spin_lock(&dev_priv->irq_lock);
16240961021aSBen Widawsky 	dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1625480c8033SDaniel Vetter 	gen8_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
16260961021aSBen Widawsky 	spin_unlock(&dev_priv->irq_lock);
16270961021aSBen Widawsky 
16280961021aSBen Widawsky 	queue_work(dev_priv->wq, &dev_priv->rps.work);
16290961021aSBen Widawsky }
16300961021aSBen Widawsky 
1631abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1632abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1633abd58f01SBen Widawsky 				       u32 master_ctl)
1634abd58f01SBen Widawsky {
1635*e981e7b1SThomas Daniel 	struct intel_engine_cs *ring;
1636abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1637abd58f01SBen Widawsky 	uint32_t tmp = 0;
1638abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1639abd58f01SBen Widawsky 
1640abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1641abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1642abd58f01SBen Widawsky 		if (tmp) {
164338cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1644abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1645*e981e7b1SThomas Daniel 
1646abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1647*e981e7b1SThomas Daniel 			ring = &dev_priv->ring[RCS];
1648abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1649*e981e7b1SThomas Daniel 				notify_ring(dev, ring);
1650*e981e7b1SThomas Daniel 			if (rcs & GT_CONTEXT_SWITCH_INTERRUPT)
1651*e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1652*e981e7b1SThomas Daniel 
1653*e981e7b1SThomas Daniel 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1654*e981e7b1SThomas Daniel 			ring = &dev_priv->ring[BCS];
1655abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1656*e981e7b1SThomas Daniel 				notify_ring(dev, ring);
1657*e981e7b1SThomas Daniel 			if (bcs & GT_CONTEXT_SWITCH_INTERRUPT)
1658*e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1659abd58f01SBen Widawsky 		} else
1660abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1661abd58f01SBen Widawsky 	}
1662abd58f01SBen Widawsky 
166385f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1664abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1665abd58f01SBen Widawsky 		if (tmp) {
166638cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1667abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1668*e981e7b1SThomas Daniel 
1669abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1670*e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VCS];
1671abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1672*e981e7b1SThomas Daniel 				notify_ring(dev, ring);
167373d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
1674*e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1675*e981e7b1SThomas Daniel 
167685f9b5f9SZhao Yakui 			vcs = tmp >> GEN8_VCS2_IRQ_SHIFT;
1677*e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VCS2];
167885f9b5f9SZhao Yakui 			if (vcs & GT_RENDER_USER_INTERRUPT)
1679*e981e7b1SThomas Daniel 				notify_ring(dev, ring);
168073d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
1681*e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1682abd58f01SBen Widawsky 		} else
1683abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1684abd58f01SBen Widawsky 	}
1685abd58f01SBen Widawsky 
16860961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
16870961021aSBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(2));
16880961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
16890961021aSBen Widawsky 			I915_WRITE(GEN8_GT_IIR(2),
16900961021aSBen Widawsky 				   tmp & dev_priv->pm_rps_events);
169138cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
169238cc46d7SOscar Mateo 			gen8_rps_irq_handler(dev_priv, tmp);
16930961021aSBen Widawsky 		} else
16940961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
16950961021aSBen Widawsky 	}
16960961021aSBen Widawsky 
1697abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1698abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1699abd58f01SBen Widawsky 		if (tmp) {
170038cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1701abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1702*e981e7b1SThomas Daniel 
1703abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1704*e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VECS];
1705abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1706*e981e7b1SThomas Daniel 				notify_ring(dev, ring);
170773d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
1708*e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1709abd58f01SBen Widawsky 		} else
1710abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1711abd58f01SBen Widawsky 	}
1712abd58f01SBen Widawsky 
1713abd58f01SBen Widawsky 	return ret;
1714abd58f01SBen Widawsky }
1715abd58f01SBen Widawsky 
1716b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1717b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1718b543fb04SEgbert Eich 
171913cf5504SDave Airlie static int ilk_port_to_hotplug_shift(enum port port)
172013cf5504SDave Airlie {
172113cf5504SDave Airlie 	switch (port) {
172213cf5504SDave Airlie 	case PORT_A:
172313cf5504SDave Airlie 	case PORT_E:
172413cf5504SDave Airlie 	default:
172513cf5504SDave Airlie 		return -1;
172613cf5504SDave Airlie 	case PORT_B:
172713cf5504SDave Airlie 		return 0;
172813cf5504SDave Airlie 	case PORT_C:
172913cf5504SDave Airlie 		return 8;
173013cf5504SDave Airlie 	case PORT_D:
173113cf5504SDave Airlie 		return 16;
173213cf5504SDave Airlie 	}
173313cf5504SDave Airlie }
173413cf5504SDave Airlie 
173513cf5504SDave Airlie static int g4x_port_to_hotplug_shift(enum port port)
173613cf5504SDave Airlie {
173713cf5504SDave Airlie 	switch (port) {
173813cf5504SDave Airlie 	case PORT_A:
173913cf5504SDave Airlie 	case PORT_E:
174013cf5504SDave Airlie 	default:
174113cf5504SDave Airlie 		return -1;
174213cf5504SDave Airlie 	case PORT_B:
174313cf5504SDave Airlie 		return 17;
174413cf5504SDave Airlie 	case PORT_C:
174513cf5504SDave Airlie 		return 19;
174613cf5504SDave Airlie 	case PORT_D:
174713cf5504SDave Airlie 		return 21;
174813cf5504SDave Airlie 	}
174913cf5504SDave Airlie }
175013cf5504SDave Airlie 
175113cf5504SDave Airlie static inline enum port get_port_from_pin(enum hpd_pin pin)
175213cf5504SDave Airlie {
175313cf5504SDave Airlie 	switch (pin) {
175413cf5504SDave Airlie 	case HPD_PORT_B:
175513cf5504SDave Airlie 		return PORT_B;
175613cf5504SDave Airlie 	case HPD_PORT_C:
175713cf5504SDave Airlie 		return PORT_C;
175813cf5504SDave Airlie 	case HPD_PORT_D:
175913cf5504SDave Airlie 		return PORT_D;
176013cf5504SDave Airlie 	default:
176113cf5504SDave Airlie 		return PORT_A; /* no hpd */
176213cf5504SDave Airlie 	}
176313cf5504SDave Airlie }
176413cf5504SDave Airlie 
176510a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1766b543fb04SEgbert Eich 					 u32 hotplug_trigger,
176713cf5504SDave Airlie 					 u32 dig_hotplug_reg,
1768b543fb04SEgbert Eich 					 const u32 *hpd)
1769b543fb04SEgbert Eich {
17702d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1771b543fb04SEgbert Eich 	int i;
177213cf5504SDave Airlie 	enum port port;
177310a504deSDaniel Vetter 	bool storm_detected = false;
177413cf5504SDave Airlie 	bool queue_dig = false, queue_hp = false;
177513cf5504SDave Airlie 	u32 dig_shift;
177613cf5504SDave Airlie 	u32 dig_port_mask = 0;
1777b543fb04SEgbert Eich 
177891d131d2SDaniel Vetter 	if (!hotplug_trigger)
177991d131d2SDaniel Vetter 		return;
178091d131d2SDaniel Vetter 
178113cf5504SDave Airlie 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x\n",
178213cf5504SDave Airlie 			 hotplug_trigger, dig_hotplug_reg);
1783cc9bd499SImre Deak 
1784b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1785b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
178613cf5504SDave Airlie 		if (!(hpd[i] & hotplug_trigger))
178713cf5504SDave Airlie 			continue;
1788821450c6SEgbert Eich 
178913cf5504SDave Airlie 		port = get_port_from_pin(i);
179013cf5504SDave Airlie 		if (port && dev_priv->hpd_irq_port[port]) {
179113cf5504SDave Airlie 			bool long_hpd;
179213cf5504SDave Airlie 
179313cf5504SDave Airlie 			if (IS_G4X(dev)) {
179413cf5504SDave Airlie 				dig_shift = g4x_port_to_hotplug_shift(port);
179513cf5504SDave Airlie 				long_hpd = (hotplug_trigger >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
179613cf5504SDave Airlie 			} else {
179713cf5504SDave Airlie 				dig_shift = ilk_port_to_hotplug_shift(port);
179813cf5504SDave Airlie 				long_hpd = (dig_hotplug_reg >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
179913cf5504SDave Airlie 			}
180013cf5504SDave Airlie 
180126fbb774SVille Syrjälä 			DRM_DEBUG_DRIVER("digital hpd port %c - %s\n",
180226fbb774SVille Syrjälä 					 port_name(port),
180326fbb774SVille Syrjälä 					 long_hpd ? "long" : "short");
180413cf5504SDave Airlie 			/* for long HPD pulses we want to have the digital queue happen,
180513cf5504SDave Airlie 			   but we still want HPD storm detection to function. */
180613cf5504SDave Airlie 			if (long_hpd) {
180713cf5504SDave Airlie 				dev_priv->long_hpd_port_mask |= (1 << port);
180813cf5504SDave Airlie 				dig_port_mask |= hpd[i];
180913cf5504SDave Airlie 			} else {
181013cf5504SDave Airlie 				/* for short HPD just trigger the digital queue */
181113cf5504SDave Airlie 				dev_priv->short_hpd_port_mask |= (1 << port);
181213cf5504SDave Airlie 				hotplug_trigger &= ~hpd[i];
181313cf5504SDave Airlie 			}
181413cf5504SDave Airlie 			queue_dig = true;
181513cf5504SDave Airlie 		}
181613cf5504SDave Airlie 	}
181713cf5504SDave Airlie 
181813cf5504SDave Airlie 	for (i = 1; i < HPD_NUM_PINS; i++) {
18193ff04a16SDaniel Vetter 		if (hpd[i] & hotplug_trigger &&
18203ff04a16SDaniel Vetter 		    dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
18213ff04a16SDaniel Vetter 			/*
18223ff04a16SDaniel Vetter 			 * On GMCH platforms the interrupt mask bits only
18233ff04a16SDaniel Vetter 			 * prevent irq generation, not the setting of the
18243ff04a16SDaniel Vetter 			 * hotplug bits itself. So only WARN about unexpected
18253ff04a16SDaniel Vetter 			 * interrupts on saner platforms.
18263ff04a16SDaniel Vetter 			 */
18273ff04a16SDaniel Vetter 			WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
1828cba1c073SChris Wilson 				  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1829cba1c073SChris Wilson 				  hotplug_trigger, i, hpd[i]);
1830b8f102e8SEgbert Eich 
18313ff04a16SDaniel Vetter 			continue;
18323ff04a16SDaniel Vetter 		}
18333ff04a16SDaniel Vetter 
1834b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1835b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1836b543fb04SEgbert Eich 			continue;
1837b543fb04SEgbert Eich 
183813cf5504SDave Airlie 		if (!(dig_port_mask & hpd[i])) {
1839bc5ead8cSJani Nikula 			dev_priv->hpd_event_bits |= (1 << i);
184013cf5504SDave Airlie 			queue_hp = true;
184113cf5504SDave Airlie 		}
184213cf5504SDave Airlie 
1843b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1844b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1845b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1846b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1847b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1848b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1849b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1850b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1851142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1852b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
185310a504deSDaniel Vetter 			storm_detected = true;
1854b543fb04SEgbert Eich 		} else {
1855b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1856b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1857b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1858b543fb04SEgbert Eich 		}
1859b543fb04SEgbert Eich 	}
1860b543fb04SEgbert Eich 
186110a504deSDaniel Vetter 	if (storm_detected)
186210a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1863b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
18645876fa0dSDaniel Vetter 
1865645416f5SDaniel Vetter 	/*
1866645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1867645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1868645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1869645416f5SDaniel Vetter 	 * deadlock.
1870645416f5SDaniel Vetter 	 */
187113cf5504SDave Airlie 	if (queue_dig)
18720e32b39cSDave Airlie 		queue_work(dev_priv->dp_wq, &dev_priv->dig_port_work);
187313cf5504SDave Airlie 	if (queue_hp)
1874645416f5SDaniel Vetter 		schedule_work(&dev_priv->hotplug_work);
1875b543fb04SEgbert Eich }
1876b543fb04SEgbert Eich 
1877515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1878515ac2bbSDaniel Vetter {
18792d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
188028c70f16SDaniel Vetter 
188128c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1882515ac2bbSDaniel Vetter }
1883515ac2bbSDaniel Vetter 
1884ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1885ce99c256SDaniel Vetter {
18862d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
18879ee32feaSDaniel Vetter 
18889ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1889ce99c256SDaniel Vetter }
1890ce99c256SDaniel Vetter 
18918bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1892277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1893eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1894eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
18958bc5e955SDaniel Vetter 					 uint32_t crc4)
18968bf1e9f1SShuang He {
18978bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
18988bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
18998bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1900ac2300d4SDamien Lespiau 	int head, tail;
1901b2c88f5bSDamien Lespiau 
1902d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1903d538bbdfSDamien Lespiau 
19040c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1905d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
19060c912c79SDamien Lespiau 		DRM_ERROR("spurious interrupt\n");
19070c912c79SDamien Lespiau 		return;
19080c912c79SDamien Lespiau 	}
19090c912c79SDamien Lespiau 
1910d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1911d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1912b2c88f5bSDamien Lespiau 
1913b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1914d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1915b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1916b2c88f5bSDamien Lespiau 		return;
1917b2c88f5bSDamien Lespiau 	}
1918b2c88f5bSDamien Lespiau 
1919b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
19208bf1e9f1SShuang He 
19218bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1922eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1923eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1924eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1925eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1926eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1927b2c88f5bSDamien Lespiau 
1928b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1929d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1930d538bbdfSDamien Lespiau 
1931d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
193207144428SDamien Lespiau 
193307144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
19348bf1e9f1SShuang He }
1935277de95eSDaniel Vetter #else
1936277de95eSDaniel Vetter static inline void
1937277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1938277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1939277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1940277de95eSDaniel Vetter 			     uint32_t crc4) {}
1941277de95eSDaniel Vetter #endif
1942eba94eb9SDaniel Vetter 
1943277de95eSDaniel Vetter 
1944277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
19455a69b89fSDaniel Vetter {
19465a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
19475a69b89fSDaniel Vetter 
1948277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
19495a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
19505a69b89fSDaniel Vetter 				     0, 0, 0, 0);
19515a69b89fSDaniel Vetter }
19525a69b89fSDaniel Vetter 
1953277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1954eba94eb9SDaniel Vetter {
1955eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1956eba94eb9SDaniel Vetter 
1957277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1958eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1959eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1960eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1961eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
19628bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1963eba94eb9SDaniel Vetter }
19645b3a856bSDaniel Vetter 
1965277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
19665b3a856bSDaniel Vetter {
19675b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
19680b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
19690b5c5ed0SDaniel Vetter 
19700b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
19710b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
19720b5c5ed0SDaniel Vetter 	else
19730b5c5ed0SDaniel Vetter 		res1 = 0;
19740b5c5ed0SDaniel Vetter 
19750b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
19760b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
19770b5c5ed0SDaniel Vetter 	else
19780b5c5ed0SDaniel Vetter 		res2 = 0;
19795b3a856bSDaniel Vetter 
1980277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
19810b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
19820b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
19830b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
19840b5c5ed0SDaniel Vetter 				     res1, res2);
19855b3a856bSDaniel Vetter }
19868bf1e9f1SShuang He 
19871403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
19881403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
19891403c0d4SPaulo Zanoni  * the work queue. */
19901403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1991baf02a1fSBen Widawsky {
1992a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
199359cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1994a6706b45SDeepak S 		dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1995480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
199659cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
19972adbee62SDaniel Vetter 
19982adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
199941a05a3aSDaniel Vetter 	}
2000baf02a1fSBen Widawsky 
20011403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
200212638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
200312638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
200412638c57SBen Widawsky 
200512638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
200658174462SMika Kuoppala 			i915_handle_error(dev_priv->dev, false,
200758174462SMika Kuoppala 					  "VEBOX CS error interrupt 0x%08x",
200858174462SMika Kuoppala 					  pm_iir);
200912638c57SBen Widawsky 		}
201012638c57SBen Widawsky 	}
20111403c0d4SPaulo Zanoni }
2012baf02a1fSBen Widawsky 
20138d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
20148d7849dbSVille Syrjälä {
20158d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
20168d7849dbSVille Syrjälä 		return false;
20178d7849dbSVille Syrjälä 
20188d7849dbSVille Syrjälä 	return true;
20198d7849dbSVille Syrjälä }
20208d7849dbSVille Syrjälä 
2021c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
20227e231dbeSJesse Barnes {
2023c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
202491d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
20257e231dbeSJesse Barnes 	int pipe;
20267e231dbeSJesse Barnes 
202758ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
20287e231dbeSJesse Barnes 	for_each_pipe(pipe) {
202991d181ddSImre Deak 		int reg;
2030bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
203191d181ddSImre Deak 
2032bbb5eebfSDaniel Vetter 		/*
2033bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
2034bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
2035bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
2036bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
2037bbb5eebfSDaniel Vetter 		 * handle.
2038bbb5eebfSDaniel Vetter 		 */
2039bbb5eebfSDaniel Vetter 		mask = 0;
2040bbb5eebfSDaniel Vetter 		if (__cpu_fifo_underrun_reporting_enabled(dev, pipe))
2041bbb5eebfSDaniel Vetter 			mask |= PIPE_FIFO_UNDERRUN_STATUS;
2042bbb5eebfSDaniel Vetter 
2043bbb5eebfSDaniel Vetter 		switch (pipe) {
2044bbb5eebfSDaniel Vetter 		case PIPE_A:
2045bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
2046bbb5eebfSDaniel Vetter 			break;
2047bbb5eebfSDaniel Vetter 		case PIPE_B:
2048bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
2049bbb5eebfSDaniel Vetter 			break;
20503278f67fSVille Syrjälä 		case PIPE_C:
20513278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
20523278f67fSVille Syrjälä 			break;
2053bbb5eebfSDaniel Vetter 		}
2054bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
2055bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
2056bbb5eebfSDaniel Vetter 
2057bbb5eebfSDaniel Vetter 		if (!mask)
205891d181ddSImre Deak 			continue;
205991d181ddSImre Deak 
206091d181ddSImre Deak 		reg = PIPESTAT(pipe);
2061bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
2062bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
20637e231dbeSJesse Barnes 
20647e231dbeSJesse Barnes 		/*
20657e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
20667e231dbeSJesse Barnes 		 */
206791d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
206891d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
20697e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
20707e231dbeSJesse Barnes 	}
207158ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
20727e231dbeSJesse Barnes 
207331acc7f5SJesse Barnes 	for_each_pipe(pipe) {
20747b5562d4SJesse Barnes 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
20758d7849dbSVille Syrjälä 			intel_pipe_handle_vblank(dev, pipe);
207631acc7f5SJesse Barnes 
2077579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
207831acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
207931acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
208031acc7f5SJesse Barnes 		}
20814356d586SDaniel Vetter 
20824356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
2083277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20842d9d2b0bSVille Syrjälä 
20852d9d2b0bSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
20862d9d2b0bSVille Syrjälä 		    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
2087fc2c807bSVille Syrjälä 			DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
208831acc7f5SJesse Barnes 	}
208931acc7f5SJesse Barnes 
2090c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
2091c1874ed7SImre Deak 		gmbus_irq_handler(dev);
2092c1874ed7SImre Deak }
2093c1874ed7SImre Deak 
209416c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
209516c6c56bSVille Syrjälä {
209616c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
209716c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
209816c6c56bSVille Syrjälä 
20993ff60f89SOscar Mateo 	if (hotplug_status) {
21003ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
21013ff60f89SOscar Mateo 		/*
21023ff60f89SOscar Mateo 		 * Make sure hotplug status is cleared before we clear IIR, or else we
21033ff60f89SOscar Mateo 		 * may miss hotplug events.
21043ff60f89SOscar Mateo 		 */
21053ff60f89SOscar Mateo 		POSTING_READ(PORT_HOTPLUG_STAT);
21063ff60f89SOscar Mateo 
210716c6c56bSVille Syrjälä 		if (IS_G4X(dev)) {
210816c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
210916c6c56bSVille Syrjälä 
211013cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_g4x);
211116c6c56bSVille Syrjälä 		} else {
211216c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
211316c6c56bSVille Syrjälä 
211413cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_i915);
211516c6c56bSVille Syrjälä 		}
211616c6c56bSVille Syrjälä 
211716c6c56bSVille Syrjälä 		if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
211816c6c56bSVille Syrjälä 		    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
211916c6c56bSVille Syrjälä 			dp_aux_irq_handler(dev);
21203ff60f89SOscar Mateo 	}
212116c6c56bSVille Syrjälä }
212216c6c56bSVille Syrjälä 
2123c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
2124c1874ed7SImre Deak {
212545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
21262d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2127c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
2128c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
2129c1874ed7SImre Deak 
2130c1874ed7SImre Deak 	while (true) {
21313ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
21323ff60f89SOscar Mateo 
2133c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
21343ff60f89SOscar Mateo 		if (gt_iir)
21353ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
21363ff60f89SOscar Mateo 
2137c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
21383ff60f89SOscar Mateo 		if (pm_iir)
21393ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
21403ff60f89SOscar Mateo 
21413ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
21423ff60f89SOscar Mateo 		if (iir) {
21433ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
21443ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
21453ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
21463ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
21473ff60f89SOscar Mateo 		}
2148c1874ed7SImre Deak 
2149c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
2150c1874ed7SImre Deak 			goto out;
2151c1874ed7SImre Deak 
2152c1874ed7SImre Deak 		ret = IRQ_HANDLED;
2153c1874ed7SImre Deak 
21543ff60f89SOscar Mateo 		if (gt_iir)
2155c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
215660611c13SPaulo Zanoni 		if (pm_iir)
2157d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
21583ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
21593ff60f89SOscar Mateo 		 * signalled in iir */
21603ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
21617e231dbeSJesse Barnes 	}
21627e231dbeSJesse Barnes 
21637e231dbeSJesse Barnes out:
21647e231dbeSJesse Barnes 	return ret;
21657e231dbeSJesse Barnes }
21667e231dbeSJesse Barnes 
216743f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
216843f328d7SVille Syrjälä {
216945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
217043f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
217143f328d7SVille Syrjälä 	u32 master_ctl, iir;
217243f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
217343f328d7SVille Syrjälä 
21748e5fd599SVille Syrjälä 	for (;;) {
21758e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
21763278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
21773278f67fSVille Syrjälä 
21783278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
21798e5fd599SVille Syrjälä 			break;
218043f328d7SVille Syrjälä 
218127b6c122SOscar Mateo 		ret = IRQ_HANDLED;
218227b6c122SOscar Mateo 
218343f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
218443f328d7SVille Syrjälä 
218527b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
218627b6c122SOscar Mateo 
218727b6c122SOscar Mateo 		if (iir) {
218827b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
218927b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
219027b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
219127b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
219227b6c122SOscar Mateo 		}
219327b6c122SOscar Mateo 
21943278f67fSVille Syrjälä 		gen8_gt_irq_handler(dev, dev_priv, master_ctl);
219543f328d7SVille Syrjälä 
219627b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
219727b6c122SOscar Mateo 		 * signalled in iir */
21983278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
219943f328d7SVille Syrjälä 
220043f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
220143f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
22028e5fd599SVille Syrjälä 	}
22033278f67fSVille Syrjälä 
220443f328d7SVille Syrjälä 	return ret;
220543f328d7SVille Syrjälä }
220643f328d7SVille Syrjälä 
220723e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
2208776ad806SJesse Barnes {
22092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
22109db4a9c7SJesse Barnes 	int pipe;
2211b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
221213cf5504SDave Airlie 	u32 dig_hotplug_reg;
2213776ad806SJesse Barnes 
221413cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
221513cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
221613cf5504SDave Airlie 
221713cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_ibx);
221891d131d2SDaniel Vetter 
2219cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
2220cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
2221776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
2222cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
2223cfc33bf7SVille Syrjälä 				 port_name(port));
2224cfc33bf7SVille Syrjälä 	}
2225776ad806SJesse Barnes 
2226ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
2227ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
2228ce99c256SDaniel Vetter 
2229776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
2230515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
2231776ad806SJesse Barnes 
2232776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
2233776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
2234776ad806SJesse Barnes 
2235776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
2236776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
2237776ad806SJesse Barnes 
2238776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
2239776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
2240776ad806SJesse Barnes 
22419db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
22429db4a9c7SJesse Barnes 		for_each_pipe(pipe)
22439db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
22449db4a9c7SJesse Barnes 					 pipe_name(pipe),
22459db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
2246776ad806SJesse Barnes 
2247776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
2248776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
2249776ad806SJesse Barnes 
2250776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
2251776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
2252776ad806SJesse Barnes 
2253776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
22548664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
22558664281bSPaulo Zanoni 							  false))
2256fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
22578664281bSPaulo Zanoni 
22588664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
22598664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
22608664281bSPaulo Zanoni 							  false))
2261fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
22628664281bSPaulo Zanoni }
22638664281bSPaulo Zanoni 
22648664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
22658664281bSPaulo Zanoni {
22668664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
22678664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
22685a69b89fSDaniel Vetter 	enum pipe pipe;
22698664281bSPaulo Zanoni 
2270de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
2271de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2272de032bf4SPaulo Zanoni 
22735a69b89fSDaniel Vetter 	for_each_pipe(pipe) {
22745a69b89fSDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
22755a69b89fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
22765a69b89fSDaniel Vetter 								  false))
2277fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
22785a69b89fSDaniel Vetter 					  pipe_name(pipe));
22795a69b89fSDaniel Vetter 		}
22808664281bSPaulo Zanoni 
22815a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
22825a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
2283277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
22845a69b89fSDaniel Vetter 			else
2285277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
22865a69b89fSDaniel Vetter 		}
22875a69b89fSDaniel Vetter 	}
22888bf1e9f1SShuang He 
22898664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
22908664281bSPaulo Zanoni }
22918664281bSPaulo Zanoni 
22928664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
22938664281bSPaulo Zanoni {
22948664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
22958664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
22968664281bSPaulo Zanoni 
2297de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2298de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2299de032bf4SPaulo Zanoni 
23008664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
23018664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
23028664281bSPaulo Zanoni 							  false))
2303fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
23048664281bSPaulo Zanoni 
23058664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
23068664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
23078664281bSPaulo Zanoni 							  false))
2308fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
23098664281bSPaulo Zanoni 
23108664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
23118664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
23128664281bSPaulo Zanoni 							  false))
2313fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder C FIFO underrun\n");
23148664281bSPaulo Zanoni 
23158664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2316776ad806SJesse Barnes }
2317776ad806SJesse Barnes 
231823e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
231923e81d69SAdam Jackson {
23202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
232123e81d69SAdam Jackson 	int pipe;
2322b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
232313cf5504SDave Airlie 	u32 dig_hotplug_reg;
232423e81d69SAdam Jackson 
232513cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
232613cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
232713cf5504SDave Airlie 
232813cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_cpt);
232991d131d2SDaniel Vetter 
2330cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2331cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
233223e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2333cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2334cfc33bf7SVille Syrjälä 				 port_name(port));
2335cfc33bf7SVille Syrjälä 	}
233623e81d69SAdam Jackson 
233723e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
2338ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
233923e81d69SAdam Jackson 
234023e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
2341515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
234223e81d69SAdam Jackson 
234323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
234423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
234523e81d69SAdam Jackson 
234623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
234723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
234823e81d69SAdam Jackson 
234923e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
235023e81d69SAdam Jackson 		for_each_pipe(pipe)
235123e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
235223e81d69SAdam Jackson 					 pipe_name(pipe),
235323e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
23548664281bSPaulo Zanoni 
23558664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
23568664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
235723e81d69SAdam Jackson }
235823e81d69SAdam Jackson 
2359c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
2360c008bc6eSPaulo Zanoni {
2361c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
236240da17c2SDaniel Vetter 	enum pipe pipe;
2363c008bc6eSPaulo Zanoni 
2364c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2365c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2366c008bc6eSPaulo Zanoni 
2367c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2368c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2369c008bc6eSPaulo Zanoni 
2370c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2371c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2372c008bc6eSPaulo Zanoni 
237340da17c2SDaniel Vetter 	for_each_pipe(pipe) {
237440da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
23758d7849dbSVille Syrjälä 			intel_pipe_handle_vblank(dev, pipe);
2376c008bc6eSPaulo Zanoni 
237740da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
237840da17c2SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
2379fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
238040da17c2SDaniel Vetter 					  pipe_name(pipe));
2381c008bc6eSPaulo Zanoni 
238240da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
238340da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
23845b3a856bSDaniel Vetter 
238540da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
238640da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
238740da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
238840da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2389c008bc6eSPaulo Zanoni 		}
2390c008bc6eSPaulo Zanoni 	}
2391c008bc6eSPaulo Zanoni 
2392c008bc6eSPaulo Zanoni 	/* check event from PCH */
2393c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2394c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2395c008bc6eSPaulo Zanoni 
2396c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2397c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2398c008bc6eSPaulo Zanoni 		else
2399c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2400c008bc6eSPaulo Zanoni 
2401c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2402c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2403c008bc6eSPaulo Zanoni 	}
2404c008bc6eSPaulo Zanoni 
2405c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2406c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2407c008bc6eSPaulo Zanoni }
2408c008bc6eSPaulo Zanoni 
24099719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
24109719fb98SPaulo Zanoni {
24119719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
241207d27e20SDamien Lespiau 	enum pipe pipe;
24139719fb98SPaulo Zanoni 
24149719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
24159719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
24169719fb98SPaulo Zanoni 
24179719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
24189719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
24199719fb98SPaulo Zanoni 
24209719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
24219719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
24229719fb98SPaulo Zanoni 
242307d27e20SDamien Lespiau 	for_each_pipe(pipe) {
242407d27e20SDamien Lespiau 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
24258d7849dbSVille Syrjälä 			intel_pipe_handle_vblank(dev, pipe);
242640da17c2SDaniel Vetter 
242740da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
242807d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
242907d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
243007d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
24319719fb98SPaulo Zanoni 		}
24329719fb98SPaulo Zanoni 	}
24339719fb98SPaulo Zanoni 
24349719fb98SPaulo Zanoni 	/* check event from PCH */
24359719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
24369719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
24379719fb98SPaulo Zanoni 
24389719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
24399719fb98SPaulo Zanoni 
24409719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
24419719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
24429719fb98SPaulo Zanoni 	}
24439719fb98SPaulo Zanoni }
24449719fb98SPaulo Zanoni 
244572c90f62SOscar Mateo /*
244672c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
244772c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
244872c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
244972c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
245072c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
245172c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
245272c90f62SOscar Mateo  */
2453f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2454b1f14ad0SJesse Barnes {
245545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
24562d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2457f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
24580e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2459b1f14ad0SJesse Barnes 
24608664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
24618664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2462907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
24638664281bSPaulo Zanoni 
2464b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2465b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2466b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
246723a78516SPaulo Zanoni 	POSTING_READ(DEIER);
24680e43406bSChris Wilson 
246944498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
247044498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
247144498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
247244498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
247344498aeaSPaulo Zanoni 	 * due to its back queue). */
2474ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
247544498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
247644498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
247744498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2478ab5c608bSBen Widawsky 	}
247944498aeaSPaulo Zanoni 
248072c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
248172c90f62SOscar Mateo 
24820e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
24830e43406bSChris Wilson 	if (gt_iir) {
248472c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
248572c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2486d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
24870e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2488d8fc8a47SPaulo Zanoni 		else
2489d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
24900e43406bSChris Wilson 	}
2491b1f14ad0SJesse Barnes 
2492b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
24930e43406bSChris Wilson 	if (de_iir) {
249472c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
249572c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2496f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
24979719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2498f1af8fc1SPaulo Zanoni 		else
2499f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
25000e43406bSChris Wilson 	}
25010e43406bSChris Wilson 
2502f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2503f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
25040e43406bSChris Wilson 		if (pm_iir) {
2505b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
25060e43406bSChris Wilson 			ret = IRQ_HANDLED;
250772c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
25080e43406bSChris Wilson 		}
2509f1af8fc1SPaulo Zanoni 	}
2510b1f14ad0SJesse Barnes 
2511b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2512b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2513ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
251444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
251544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2516ab5c608bSBen Widawsky 	}
2517b1f14ad0SJesse Barnes 
2518b1f14ad0SJesse Barnes 	return ret;
2519b1f14ad0SJesse Barnes }
2520b1f14ad0SJesse Barnes 
2521abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2522abd58f01SBen Widawsky {
2523abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2524abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2525abd58f01SBen Widawsky 	u32 master_ctl;
2526abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2527abd58f01SBen Widawsky 	uint32_t tmp = 0;
2528c42664ccSDaniel Vetter 	enum pipe pipe;
2529abd58f01SBen Widawsky 
2530abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
2531abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2532abd58f01SBen Widawsky 	if (!master_ctl)
2533abd58f01SBen Widawsky 		return IRQ_NONE;
2534abd58f01SBen Widawsky 
2535abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2536abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2537abd58f01SBen Widawsky 
253838cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
253938cc46d7SOscar Mateo 
2540abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
2541abd58f01SBen Widawsky 
2542abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2543abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2544abd58f01SBen Widawsky 		if (tmp) {
2545abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2546abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
254738cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
254838cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
254938cc46d7SOscar Mateo 			else
255038cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2551abd58f01SBen Widawsky 		}
255238cc46d7SOscar Mateo 		else
255338cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2554abd58f01SBen Widawsky 	}
2555abd58f01SBen Widawsky 
25566d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
25576d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
25586d766f02SDaniel Vetter 		if (tmp) {
25596d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
25606d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
256138cc46d7SOscar Mateo 			if (tmp & GEN8_AUX_CHANNEL_A)
256238cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
256338cc46d7SOscar Mateo 			else
256438cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
25656d766f02SDaniel Vetter 		}
256638cc46d7SOscar Mateo 		else
256738cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
25686d766f02SDaniel Vetter 	}
25696d766f02SDaniel Vetter 
2570abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2571abd58f01SBen Widawsky 		uint32_t pipe_iir;
2572abd58f01SBen Widawsky 
2573c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2574c42664ccSDaniel Vetter 			continue;
2575c42664ccSDaniel Vetter 
2576abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
257738cc46d7SOscar Mateo 		if (pipe_iir) {
257838cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
257938cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2580abd58f01SBen Widawsky 			if (pipe_iir & GEN8_PIPE_VBLANK)
25818d7849dbSVille Syrjälä 				intel_pipe_handle_vblank(dev, pipe);
2582abd58f01SBen Widawsky 
2583d0e1f1cbSDamien Lespiau 			if (pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE) {
2584abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2585abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2586abd58f01SBen Widawsky 			}
2587abd58f01SBen Widawsky 
25880fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
25890fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
25900fbe7870SDaniel Vetter 
259138d83c96SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN) {
259238d83c96SDaniel Vetter 				if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
259338d83c96SDaniel Vetter 									  false))
2594fc2c807bSVille Syrjälä 					DRM_ERROR("Pipe %c FIFO underrun\n",
259538d83c96SDaniel Vetter 						  pipe_name(pipe));
259638d83c96SDaniel Vetter 			}
259738d83c96SDaniel Vetter 
259830100f2bSDaniel Vetter 			if (pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS) {
259930100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
260030100f2bSDaniel Vetter 					  pipe_name(pipe),
260130100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
260230100f2bSDaniel Vetter 			}
2603c42664ccSDaniel Vetter 		} else
2604abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2605abd58f01SBen Widawsky 	}
2606abd58f01SBen Widawsky 
260792d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
260892d03a80SDaniel Vetter 		/*
260992d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
261092d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
261192d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
261292d03a80SDaniel Vetter 		 */
261392d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
261492d03a80SDaniel Vetter 		if (pch_iir) {
261592d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
261692d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
261738cc46d7SOscar Mateo 			cpt_irq_handler(dev, pch_iir);
261838cc46d7SOscar Mateo 		} else
261938cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
262038cc46d7SOscar Mateo 
262192d03a80SDaniel Vetter 	}
262292d03a80SDaniel Vetter 
2623abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2624abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2625abd58f01SBen Widawsky 
2626abd58f01SBen Widawsky 	return ret;
2627abd58f01SBen Widawsky }
2628abd58f01SBen Widawsky 
262917e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
263017e1df07SDaniel Vetter 			       bool reset_completed)
263117e1df07SDaniel Vetter {
2632a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
263317e1df07SDaniel Vetter 	int i;
263417e1df07SDaniel Vetter 
263517e1df07SDaniel Vetter 	/*
263617e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
263717e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
263817e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
263917e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
264017e1df07SDaniel Vetter 	 */
264117e1df07SDaniel Vetter 
264217e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
264317e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
264417e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
264517e1df07SDaniel Vetter 
264617e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
264717e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
264817e1df07SDaniel Vetter 
264917e1df07SDaniel Vetter 	/*
265017e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
265117e1df07SDaniel Vetter 	 * reset state is cleared.
265217e1df07SDaniel Vetter 	 */
265317e1df07SDaniel Vetter 	if (reset_completed)
265417e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
265517e1df07SDaniel Vetter }
265617e1df07SDaniel Vetter 
26578a905236SJesse Barnes /**
26588a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
26598a905236SJesse Barnes  * @work: work struct
26608a905236SJesse Barnes  *
26618a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
26628a905236SJesse Barnes  * was detected.
26638a905236SJesse Barnes  */
26648a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
26658a905236SJesse Barnes {
26661f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
26671f83fee0SDaniel Vetter 						    work);
26682d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
26692d1013ddSJani Nikula 		container_of(error, struct drm_i915_private, gpu_error);
26708a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
2671cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2672cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2673cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
267417e1df07SDaniel Vetter 	int ret;
26758a905236SJesse Barnes 
26765bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
26778a905236SJesse Barnes 
26787db0ba24SDaniel Vetter 	/*
26797db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
26807db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
26817db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
26827db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
26837db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
26847db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
26857db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
26867db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
26877db0ba24SDaniel Vetter 	 */
26887db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
268944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
26905bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
26917db0ba24SDaniel Vetter 				   reset_event);
26921f83fee0SDaniel Vetter 
269317e1df07SDaniel Vetter 		/*
2694f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2695f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2696f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2697f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2698f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2699f454c694SImre Deak 		 */
2700f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
2701f454c694SImre Deak 		/*
270217e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
270317e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
270417e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
270517e1df07SDaniel Vetter 		 * deadlocks with the reset work.
270617e1df07SDaniel Vetter 		 */
2707f69061beSDaniel Vetter 		ret = i915_reset(dev);
2708f69061beSDaniel Vetter 
270917e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
271017e1df07SDaniel Vetter 
2711f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2712f454c694SImre Deak 
2713f69061beSDaniel Vetter 		if (ret == 0) {
2714f69061beSDaniel Vetter 			/*
2715f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2716f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2717f69061beSDaniel Vetter 			 * complete.
2718f69061beSDaniel Vetter 			 *
2719f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2720f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2721f69061beSDaniel Vetter 			 * updates before
2722f69061beSDaniel Vetter 			 * the counter increment.
2723f69061beSDaniel Vetter 			 */
27244e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2725f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2726f69061beSDaniel Vetter 
27275bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2728f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
27291f83fee0SDaniel Vetter 		} else {
27302ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2731f316a42cSBen Gamari 		}
27321f83fee0SDaniel Vetter 
273317e1df07SDaniel Vetter 		/*
273417e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
273517e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
273617e1df07SDaniel Vetter 		 */
273717e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2738f316a42cSBen Gamari 	}
27398a905236SJesse Barnes }
27408a905236SJesse Barnes 
274135aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2742c0e09200SDave Airlie {
27438a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2744bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
274563eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2746050ee91fSBen Widawsky 	int pipe, i;
274763eeaf38SJesse Barnes 
274835aed2e6SChris Wilson 	if (!eir)
274935aed2e6SChris Wilson 		return;
275063eeaf38SJesse Barnes 
2751a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
27528a905236SJesse Barnes 
2753bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2754bd9854f9SBen Widawsky 
27558a905236SJesse Barnes 	if (IS_G4X(dev)) {
27568a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
27578a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
27588a905236SJesse Barnes 
2759a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2760a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2761050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2762050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2763a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2764a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
27658a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
27663143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
27678a905236SJesse Barnes 		}
27688a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
27698a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2770a70491ccSJoe Perches 			pr_err("page table error\n");
2771a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
27728a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
27733143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
27748a905236SJesse Barnes 		}
27758a905236SJesse Barnes 	}
27768a905236SJesse Barnes 
2777a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
277863eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
277963eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2780a70491ccSJoe Perches 			pr_err("page table error\n");
2781a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
278263eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
27833143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
278463eeaf38SJesse Barnes 		}
27858a905236SJesse Barnes 	}
27868a905236SJesse Barnes 
278763eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2788a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
27899db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2790a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
27919db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
279263eeaf38SJesse Barnes 		/* pipestat has already been acked */
279363eeaf38SJesse Barnes 	}
279463eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2795a70491ccSJoe Perches 		pr_err("instruction error\n");
2796a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2797050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2798050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2799a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
280063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
280163eeaf38SJesse Barnes 
2802a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2803a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2804a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
280563eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
28063143a2bfSChris Wilson 			POSTING_READ(IPEIR);
280763eeaf38SJesse Barnes 		} else {
280863eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
280963eeaf38SJesse Barnes 
2810a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2811a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2812a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2813a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
281463eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
28153143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
281663eeaf38SJesse Barnes 		}
281763eeaf38SJesse Barnes 	}
281863eeaf38SJesse Barnes 
281963eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
28203143a2bfSChris Wilson 	POSTING_READ(EIR);
282163eeaf38SJesse Barnes 	eir = I915_READ(EIR);
282263eeaf38SJesse Barnes 	if (eir) {
282363eeaf38SJesse Barnes 		/*
282463eeaf38SJesse Barnes 		 * some errors might have become stuck,
282563eeaf38SJesse Barnes 		 * mask them.
282663eeaf38SJesse Barnes 		 */
282763eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
282863eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
282963eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
283063eeaf38SJesse Barnes 	}
283135aed2e6SChris Wilson }
283235aed2e6SChris Wilson 
283335aed2e6SChris Wilson /**
283435aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
283535aed2e6SChris Wilson  * @dev: drm device
283635aed2e6SChris Wilson  *
283735aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
283835aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
283935aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
284035aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
284135aed2e6SChris Wilson  * of a ring dump etc.).
284235aed2e6SChris Wilson  */
284358174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
284458174462SMika Kuoppala 		       const char *fmt, ...)
284535aed2e6SChris Wilson {
284635aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
284758174462SMika Kuoppala 	va_list args;
284858174462SMika Kuoppala 	char error_msg[80];
284935aed2e6SChris Wilson 
285058174462SMika Kuoppala 	va_start(args, fmt);
285158174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
285258174462SMika Kuoppala 	va_end(args);
285358174462SMika Kuoppala 
285458174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
285535aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
28568a905236SJesse Barnes 
2857ba1234d1SBen Gamari 	if (wedged) {
2858f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2859f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2860ba1234d1SBen Gamari 
286111ed50ecSBen Gamari 		/*
286217e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
286317e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
286417e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
286517e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
286617e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
286717e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
286817e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
286917e1df07SDaniel Vetter 		 *
287017e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
287117e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
287217e1df07SDaniel Vetter 		 * counter atomic_t.
287311ed50ecSBen Gamari 		 */
287417e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
287511ed50ecSBen Gamari 	}
287611ed50ecSBen Gamari 
2877122f46baSDaniel Vetter 	/*
2878122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
2879122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
2880122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2881122f46baSDaniel Vetter 	 * code will deadlock.
2882122f46baSDaniel Vetter 	 */
2883122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
28848a905236SJesse Barnes }
28858a905236SJesse Barnes 
288621ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
28874e5359cdSSimon Farnsworth {
28882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
28894e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
28904e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
289105394f39SChris Wilson 	struct drm_i915_gem_object *obj;
28924e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
28934e5359cdSSimon Farnsworth 	unsigned long flags;
28944e5359cdSSimon Farnsworth 	bool stall_detected;
28954e5359cdSSimon Farnsworth 
28964e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
28974e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
28984e5359cdSSimon Farnsworth 		return;
28994e5359cdSSimon Farnsworth 
29004e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
29014e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
29024e5359cdSSimon Farnsworth 
2903e7d841caSChris Wilson 	if (work == NULL ||
2904e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2905e7d841caSChris Wilson 	    !work->enable_stall_check) {
29064e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
29074e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
29084e5359cdSSimon Farnsworth 		return;
29094e5359cdSSimon Farnsworth 	}
29104e5359cdSSimon Farnsworth 
29114e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
291205394f39SChris Wilson 	obj = work->pending_flip_obj;
2913a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
29149db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2915446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2916f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
29174e5359cdSSimon Farnsworth 	} else {
29189db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
2919f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
2920f4510a27SMatt Roper 							crtc->y * crtc->primary->fb->pitches[0] +
2921f4510a27SMatt Roper 							crtc->x * crtc->primary->fb->bits_per_pixel/8);
29224e5359cdSSimon Farnsworth 	}
29234e5359cdSSimon Farnsworth 
29244e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
29254e5359cdSSimon Farnsworth 
29264e5359cdSSimon Farnsworth 	if (stall_detected) {
29274e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
29284e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
29294e5359cdSSimon Farnsworth 	}
29304e5359cdSSimon Farnsworth }
29314e5359cdSSimon Farnsworth 
293242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
293342f52ef8SKeith Packard  * we use as a pipe index
293442f52ef8SKeith Packard  */
2935f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
29360a3e67a4SJesse Barnes {
29372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2938e9d21d7fSKeith Packard 	unsigned long irqflags;
293971e0ffa5SJesse Barnes 
29405eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
294171e0ffa5SJesse Barnes 		return -EINVAL;
29420a3e67a4SJesse Barnes 
29431ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2944f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
29457c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2946755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
29470a3e67a4SJesse Barnes 	else
29487c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2949755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
29501ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
29518692d00eSChris Wilson 
29520a3e67a4SJesse Barnes 	return 0;
29530a3e67a4SJesse Barnes }
29540a3e67a4SJesse Barnes 
2955f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2956f796cf8fSJesse Barnes {
29572d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2958f796cf8fSJesse Barnes 	unsigned long irqflags;
2959b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
296040da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2961f796cf8fSJesse Barnes 
2962f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2963f796cf8fSJesse Barnes 		return -EINVAL;
2964f796cf8fSJesse Barnes 
2965f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2966b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2967b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2968b1f14ad0SJesse Barnes 
2969b1f14ad0SJesse Barnes 	return 0;
2970b1f14ad0SJesse Barnes }
2971b1f14ad0SJesse Barnes 
29727e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
29737e231dbeSJesse Barnes {
29742d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
29757e231dbeSJesse Barnes 	unsigned long irqflags;
29767e231dbeSJesse Barnes 
29777e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
29787e231dbeSJesse Barnes 		return -EINVAL;
29797e231dbeSJesse Barnes 
29807e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
298131acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2982755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
29837e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
29847e231dbeSJesse Barnes 
29857e231dbeSJesse Barnes 	return 0;
29867e231dbeSJesse Barnes }
29877e231dbeSJesse Barnes 
2988abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2989abd58f01SBen Widawsky {
2990abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2991abd58f01SBen Widawsky 	unsigned long irqflags;
2992abd58f01SBen Widawsky 
2993abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2994abd58f01SBen Widawsky 		return -EINVAL;
2995abd58f01SBen Widawsky 
2996abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
29977167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
29987167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2999abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
3000abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3001abd58f01SBen Widawsky 	return 0;
3002abd58f01SBen Widawsky }
3003abd58f01SBen Widawsky 
300442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
300542f52ef8SKeith Packard  * we use as a pipe index
300642f52ef8SKeith Packard  */
3007f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
30080a3e67a4SJesse Barnes {
30092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3010e9d21d7fSKeith Packard 	unsigned long irqflags;
30110a3e67a4SJesse Barnes 
30121ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
30137c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
3014755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
3015755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
30161ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
30170a3e67a4SJesse Barnes }
30180a3e67a4SJesse Barnes 
3019f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
3020f796cf8fSJesse Barnes {
30212d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3022f796cf8fSJesse Barnes 	unsigned long irqflags;
3023b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
302440da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
3025f796cf8fSJesse Barnes 
3026f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3027b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
3028b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3029b1f14ad0SJesse Barnes }
3030b1f14ad0SJesse Barnes 
30317e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
30327e231dbeSJesse Barnes {
30332d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
30347e231dbeSJesse Barnes 	unsigned long irqflags;
30357e231dbeSJesse Barnes 
30367e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
303731acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
3038755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
30397e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
30407e231dbeSJesse Barnes }
30417e231dbeSJesse Barnes 
3042abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
3043abd58f01SBen Widawsky {
3044abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3045abd58f01SBen Widawsky 	unsigned long irqflags;
3046abd58f01SBen Widawsky 
3047abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
3048abd58f01SBen Widawsky 		return;
3049abd58f01SBen Widawsky 
3050abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
30517167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
30527167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
3053abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
3054abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3055abd58f01SBen Widawsky }
3056abd58f01SBen Widawsky 
3057893eead0SChris Wilson static u32
3058a4872ba6SOscar Mateo ring_last_seqno(struct intel_engine_cs *ring)
3059852835f3SZou Nan hai {
3060893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
3061893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
3062893eead0SChris Wilson }
3063893eead0SChris Wilson 
30649107e9d2SChris Wilson static bool
3065a4872ba6SOscar Mateo ring_idle(struct intel_engine_cs *ring, u32 seqno)
3066893eead0SChris Wilson {
30679107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
30689107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
3069f65d9421SBen Gamari }
3070f65d9421SBen Gamari 
3071a028c4b0SDaniel Vetter static bool
3072a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
3073a028c4b0SDaniel Vetter {
3074a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
3075a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
3076a028c4b0SDaniel Vetter 	} else {
3077a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
3078a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
3079a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
3080a028c4b0SDaniel Vetter 	}
3081a028c4b0SDaniel Vetter }
3082a028c4b0SDaniel Vetter 
3083a4872ba6SOscar Mateo static struct intel_engine_cs *
3084a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
3085921d42eaSDaniel Vetter {
3086921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
3087a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
3088921d42eaSDaniel Vetter 	int i;
3089921d42eaSDaniel Vetter 
3090921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
3091a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
3092a6cdb93aSRodrigo Vivi 			if (ring == signaller)
3093a6cdb93aSRodrigo Vivi 				continue;
3094a6cdb93aSRodrigo Vivi 
3095a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
3096a6cdb93aSRodrigo Vivi 				return signaller;
3097a6cdb93aSRodrigo Vivi 		}
3098921d42eaSDaniel Vetter 	} else {
3099921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
3100921d42eaSDaniel Vetter 
3101921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
3102921d42eaSDaniel Vetter 			if(ring == signaller)
3103921d42eaSDaniel Vetter 				continue;
3104921d42eaSDaniel Vetter 
3105ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
3106921d42eaSDaniel Vetter 				return signaller;
3107921d42eaSDaniel Vetter 		}
3108921d42eaSDaniel Vetter 	}
3109921d42eaSDaniel Vetter 
3110a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
3111a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
3112921d42eaSDaniel Vetter 
3113921d42eaSDaniel Vetter 	return NULL;
3114921d42eaSDaniel Vetter }
3115921d42eaSDaniel Vetter 
3116a4872ba6SOscar Mateo static struct intel_engine_cs *
3117a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
3118a24a11e6SChris Wilson {
3119a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
312088fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
3121a6cdb93aSRodrigo Vivi 	u64 offset = 0;
3122a6cdb93aSRodrigo Vivi 	int i, backwards;
3123a24a11e6SChris Wilson 
3124a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
3125a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
31266274f212SChris Wilson 		return NULL;
3127a24a11e6SChris Wilson 
312888fe429dSDaniel Vetter 	/*
312988fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
313088fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
3131a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
3132a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
313388fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
313488fe429dSDaniel Vetter 	 * ringbuffer itself.
3135a24a11e6SChris Wilson 	 */
313688fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
3137a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
313888fe429dSDaniel Vetter 
3139a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
314088fe429dSDaniel Vetter 		/*
314188fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
314288fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
314388fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
314488fe429dSDaniel Vetter 		 */
3145ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
314688fe429dSDaniel Vetter 
314788fe429dSDaniel Vetter 		/* This here seems to blow up */
3148ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
3149a24a11e6SChris Wilson 		if (cmd == ipehr)
3150a24a11e6SChris Wilson 			break;
3151a24a11e6SChris Wilson 
315288fe429dSDaniel Vetter 		head -= 4;
315388fe429dSDaniel Vetter 	}
3154a24a11e6SChris Wilson 
315588fe429dSDaniel Vetter 	if (!i)
315688fe429dSDaniel Vetter 		return NULL;
315788fe429dSDaniel Vetter 
3158ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
3159a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
3160a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
3161a6cdb93aSRodrigo Vivi 		offset <<= 32;
3162a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
3163a6cdb93aSRodrigo Vivi 	}
3164a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
3165a24a11e6SChris Wilson }
3166a24a11e6SChris Wilson 
3167a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
31686274f212SChris Wilson {
31696274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
3170a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
3171a0d036b0SChris Wilson 	u32 seqno;
31726274f212SChris Wilson 
31734be17381SChris Wilson 	ring->hangcheck.deadlock++;
31746274f212SChris Wilson 
31756274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
31764be17381SChris Wilson 	if (signaller == NULL)
31774be17381SChris Wilson 		return -1;
31784be17381SChris Wilson 
31794be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
31804be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
31816274f212SChris Wilson 		return -1;
31826274f212SChris Wilson 
31834be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
31844be17381SChris Wilson 		return 1;
31854be17381SChris Wilson 
3186a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
3187a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
3188a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
31894be17381SChris Wilson 		return -1;
31904be17381SChris Wilson 
31914be17381SChris Wilson 	return 0;
31926274f212SChris Wilson }
31936274f212SChris Wilson 
31946274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
31956274f212SChris Wilson {
3196a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
31976274f212SChris Wilson 	int i;
31986274f212SChris Wilson 
31996274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
32004be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
32016274f212SChris Wilson }
32026274f212SChris Wilson 
3203ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
3204a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
32051ec14ad3SChris Wilson {
32061ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
32071ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
32089107e9d2SChris Wilson 	u32 tmp;
32099107e9d2SChris Wilson 
3210f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
3211f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
3212f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
3213f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
3214f260fe7bSMika Kuoppala 		}
3215f260fe7bSMika Kuoppala 
3216f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
3217f260fe7bSMika Kuoppala 	}
32186274f212SChris Wilson 
32199107e9d2SChris Wilson 	if (IS_GEN2(dev))
3220f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
32219107e9d2SChris Wilson 
32229107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
32239107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
32249107e9d2SChris Wilson 	 * and break the hang. This should work on
32259107e9d2SChris Wilson 	 * all but the second generation chipsets.
32269107e9d2SChris Wilson 	 */
32279107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
32281ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
322958174462SMika Kuoppala 		i915_handle_error(dev, false,
323058174462SMika Kuoppala 				  "Kicking stuck wait on %s",
32311ec14ad3SChris Wilson 				  ring->name);
32321ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
3233f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
32341ec14ad3SChris Wilson 	}
3235a24a11e6SChris Wilson 
32366274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
32376274f212SChris Wilson 		switch (semaphore_passed(ring)) {
32386274f212SChris Wilson 		default:
3239f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
32406274f212SChris Wilson 		case 1:
324158174462SMika Kuoppala 			i915_handle_error(dev, false,
324258174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
3243a24a11e6SChris Wilson 					  ring->name);
3244a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
3245f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
32466274f212SChris Wilson 		case 0:
3247f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
32486274f212SChris Wilson 		}
32499107e9d2SChris Wilson 	}
32509107e9d2SChris Wilson 
3251f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
3252a24a11e6SChris Wilson }
3253d1e61e7fSChris Wilson 
3254f65d9421SBen Gamari /**
3255f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
325605407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
325705407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
325805407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
325905407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
326005407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
3261f65d9421SBen Gamari  */
3262a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
3263f65d9421SBen Gamari {
3264f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
32652d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3266a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
3267b4519513SChris Wilson 	int i;
326805407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
32699107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
32709107e9d2SChris Wilson #define BUSY 1
32719107e9d2SChris Wilson #define KICK 5
32729107e9d2SChris Wilson #define HUNG 20
3273893eead0SChris Wilson 
3274d330a953SJani Nikula 	if (!i915.enable_hangcheck)
32753e0dc6b0SBen Widawsky 		return;
32763e0dc6b0SBen Widawsky 
3277b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
327850877445SChris Wilson 		u64 acthd;
327950877445SChris Wilson 		u32 seqno;
32809107e9d2SChris Wilson 		bool busy = true;
3281b4519513SChris Wilson 
32826274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
32836274f212SChris Wilson 
328405407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
328505407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
328605407ff8SMika Kuoppala 
328705407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
32889107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
3289da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
3290da661464SMika Kuoppala 
32919107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
32929107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
3293094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
3294f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
32959107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
32969107e9d2SChris Wilson 								  ring->name);
3297f4adcd24SDaniel Vetter 						else
3298f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
3299f4adcd24SDaniel Vetter 								 ring->name);
33009107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
3301094f9a54SChris Wilson 					}
3302094f9a54SChris Wilson 					/* Safeguard against driver failure */
3303094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
33049107e9d2SChris Wilson 				} else
33059107e9d2SChris Wilson 					busy = false;
330605407ff8SMika Kuoppala 			} else {
33076274f212SChris Wilson 				/* We always increment the hangcheck score
33086274f212SChris Wilson 				 * if the ring is busy and still processing
33096274f212SChris Wilson 				 * the same request, so that no single request
33106274f212SChris Wilson 				 * can run indefinitely (such as a chain of
33116274f212SChris Wilson 				 * batches). The only time we do not increment
33126274f212SChris Wilson 				 * the hangcheck score on this ring, if this
33136274f212SChris Wilson 				 * ring is in a legitimate wait for another
33146274f212SChris Wilson 				 * ring. In that case the waiting ring is a
33156274f212SChris Wilson 				 * victim and we want to be sure we catch the
33166274f212SChris Wilson 				 * right culprit. Then every time we do kick
33176274f212SChris Wilson 				 * the ring, add a small increment to the
33186274f212SChris Wilson 				 * score so that we can catch a batch that is
33196274f212SChris Wilson 				 * being repeatedly kicked and so responsible
33206274f212SChris Wilson 				 * for stalling the machine.
33219107e9d2SChris Wilson 				 */
3322ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
3323ad8beaeaSMika Kuoppala 								    acthd);
3324ad8beaeaSMika Kuoppala 
3325ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
3326da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3327f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3328f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
3329f260fe7bSMika Kuoppala 					break;
3330f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
3331ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
33326274f212SChris Wilson 					break;
3333f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3334ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
33356274f212SChris Wilson 					break;
3336f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3337ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
33386274f212SChris Wilson 					stuck[i] = true;
33396274f212SChris Wilson 					break;
33406274f212SChris Wilson 				}
334105407ff8SMika Kuoppala 			}
33429107e9d2SChris Wilson 		} else {
3343da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
3344da661464SMika Kuoppala 
33459107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
33469107e9d2SChris Wilson 			 * attempts across multiple batches.
33479107e9d2SChris Wilson 			 */
33489107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
33499107e9d2SChris Wilson 				ring->hangcheck.score--;
3350f260fe7bSMika Kuoppala 
3351f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
3352cbb465e7SChris Wilson 		}
3353f65d9421SBen Gamari 
335405407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
335505407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
33569107e9d2SChris Wilson 		busy_count += busy;
335705407ff8SMika Kuoppala 	}
335805407ff8SMika Kuoppala 
335905407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
3360b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3361b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
336205407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
3363a43adf07SChris Wilson 				 ring->name);
3364a43adf07SChris Wilson 			rings_hung++;
336505407ff8SMika Kuoppala 		}
336605407ff8SMika Kuoppala 	}
336705407ff8SMika Kuoppala 
336805407ff8SMika Kuoppala 	if (rings_hung)
336958174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
337005407ff8SMika Kuoppala 
337105407ff8SMika Kuoppala 	if (busy_count)
337205407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
337305407ff8SMika Kuoppala 		 * being added */
337410cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
337510cd45b6SMika Kuoppala }
337610cd45b6SMika Kuoppala 
337710cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
337810cd45b6SMika Kuoppala {
337910cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
3380d330a953SJani Nikula 	if (!i915.enable_hangcheck)
338110cd45b6SMika Kuoppala 		return;
338210cd45b6SMika Kuoppala 
338399584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
338410cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
3385f65d9421SBen Gamari }
3386f65d9421SBen Gamari 
33871c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
338891738a95SPaulo Zanoni {
338991738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
339091738a95SPaulo Zanoni 
339191738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
339291738a95SPaulo Zanoni 		return;
339391738a95SPaulo Zanoni 
3394f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3395105b122eSPaulo Zanoni 
3396105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3397105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3398622364b6SPaulo Zanoni }
3399105b122eSPaulo Zanoni 
340091738a95SPaulo Zanoni /*
3401622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3402622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3403622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3404622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3405622364b6SPaulo Zanoni  *
3406622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
340791738a95SPaulo Zanoni  */
3408622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3409622364b6SPaulo Zanoni {
3410622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3411622364b6SPaulo Zanoni 
3412622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3413622364b6SPaulo Zanoni 		return;
3414622364b6SPaulo Zanoni 
3415622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
341691738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
341791738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
341891738a95SPaulo Zanoni }
341991738a95SPaulo Zanoni 
34207c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3421d18ea1b5SDaniel Vetter {
3422d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3423d18ea1b5SDaniel Vetter 
3424f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3425a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3426f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3427d18ea1b5SDaniel Vetter }
3428d18ea1b5SDaniel Vetter 
3429c0e09200SDave Airlie /* drm_dma.h hooks
3430c0e09200SDave Airlie */
3431be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3432036a4a7dSZhenyu Wang {
34332d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3434036a4a7dSZhenyu Wang 
34350c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3436bdfcdb63SDaniel Vetter 
3437f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3438c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3439c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3440036a4a7dSZhenyu Wang 
34417c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3442c650156aSZhenyu Wang 
34431c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
34447d99163dSBen Widawsky }
34457d99163dSBen Widawsky 
34467e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
34477e231dbeSJesse Barnes {
34482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
34497e231dbeSJesse Barnes 	int pipe;
34507e231dbeSJesse Barnes 
34517e231dbeSJesse Barnes 	/* VLV magic */
34527e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
34537e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
34547e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
34557e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
34567e231dbeSJesse Barnes 
34577e231dbeSJesse Barnes 	/* and GT */
34587e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
34597e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
3460d18ea1b5SDaniel Vetter 
34617c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
34627e231dbeSJesse Barnes 
34637e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
34647e231dbeSJesse Barnes 
34657e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
34667e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
34677e231dbeSJesse Barnes 	for_each_pipe(pipe)
34687e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
34697e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34707e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
34717e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
34727e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
34737e231dbeSJesse Barnes }
34747e231dbeSJesse Barnes 
3475d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3476d6e3cca3SDaniel Vetter {
3477d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3478d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3479d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3480d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3481d6e3cca3SDaniel Vetter }
3482d6e3cca3SDaniel Vetter 
3483823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3484abd58f01SBen Widawsky {
3485abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3486abd58f01SBen Widawsky 	int pipe;
3487abd58f01SBen Widawsky 
3488abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3489abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3490abd58f01SBen Widawsky 
3491d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3492abd58f01SBen Widawsky 
3493823f6b38SPaulo Zanoni 	for_each_pipe(pipe)
3494813bde43SPaulo Zanoni 		if (intel_display_power_enabled(dev_priv,
3495813bde43SPaulo Zanoni 						POWER_DOMAIN_PIPE(pipe)))
3496f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3497abd58f01SBen Widawsky 
3498f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3499f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3500f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3501abd58f01SBen Widawsky 
35021c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
3503abd58f01SBen Widawsky }
3504abd58f01SBen Widawsky 
3505d49bdb0eSPaulo Zanoni void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv)
3506d49bdb0eSPaulo Zanoni {
3507d49bdb0eSPaulo Zanoni 	unsigned long irqflags;
3508d49bdb0eSPaulo Zanoni 
3509d49bdb0eSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3510d49bdb0eSPaulo Zanoni 	GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B, dev_priv->de_irq_mask[PIPE_B],
3511d49bdb0eSPaulo Zanoni 			  ~dev_priv->de_irq_mask[PIPE_B]);
3512d49bdb0eSPaulo Zanoni 	GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C, dev_priv->de_irq_mask[PIPE_C],
3513d49bdb0eSPaulo Zanoni 			  ~dev_priv->de_irq_mask[PIPE_C]);
3514d49bdb0eSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3515d49bdb0eSPaulo Zanoni }
3516d49bdb0eSPaulo Zanoni 
351743f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
351843f328d7SVille Syrjälä {
351943f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
352043f328d7SVille Syrjälä 	int pipe;
352143f328d7SVille Syrjälä 
352243f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
352343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
352443f328d7SVille Syrjälä 
3525d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
352643f328d7SVille Syrjälä 
352743f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
352843f328d7SVille Syrjälä 
352943f328d7SVille Syrjälä 	POSTING_READ(GEN8_PCU_IIR);
353043f328d7SVille Syrjälä 
353143f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
353243f328d7SVille Syrjälä 
353343f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
353443f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
353543f328d7SVille Syrjälä 
353643f328d7SVille Syrjälä 	for_each_pipe(pipe)
353743f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
353843f328d7SVille Syrjälä 
353943f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, 0xffffffff);
354043f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, 0x0);
354143f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
354243f328d7SVille Syrjälä 	POSTING_READ(VLV_IIR);
354343f328d7SVille Syrjälä }
354443f328d7SVille Syrjälä 
354582a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
354682a28bcfSDaniel Vetter {
35472d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
354882a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
3549fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
355082a28bcfSDaniel Vetter 
355182a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3552fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
3553b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3554cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3555fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
355682a28bcfSDaniel Vetter 	} else {
3557fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
3558b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3559cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3560fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
356182a28bcfSDaniel Vetter 	}
356282a28bcfSDaniel Vetter 
3563fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
356482a28bcfSDaniel Vetter 
35657fe0b973SKeith Packard 	/*
35667fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
35677fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
35687fe0b973SKeith Packard 	 *
35697fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
35707fe0b973SKeith Packard 	 */
35717fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
35727fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
35737fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
35747fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
35757fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
35767fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
35777fe0b973SKeith Packard }
35787fe0b973SKeith Packard 
3579d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3580d46da437SPaulo Zanoni {
35812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
358282a28bcfSDaniel Vetter 	u32 mask;
3583d46da437SPaulo Zanoni 
3584692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3585692a04cfSDaniel Vetter 		return;
3586692a04cfSDaniel Vetter 
3587105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
35885c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3589105b122eSPaulo Zanoni 	else
35905c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
35918664281bSPaulo Zanoni 
3592337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3593d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3594d46da437SPaulo Zanoni }
3595d46da437SPaulo Zanoni 
35960a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
35970a9a8c91SDaniel Vetter {
35980a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
35990a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
36000a9a8c91SDaniel Vetter 
36010a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
36020a9a8c91SDaniel Vetter 
36030a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3604040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
36050a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
360635a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
360735a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
36080a9a8c91SDaniel Vetter 	}
36090a9a8c91SDaniel Vetter 
36100a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
36110a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
36120a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
36130a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
36140a9a8c91SDaniel Vetter 	} else {
36150a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
36160a9a8c91SDaniel Vetter 	}
36170a9a8c91SDaniel Vetter 
361835079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
36190a9a8c91SDaniel Vetter 
36200a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
3621a6706b45SDeepak S 		pm_irqs |= dev_priv->pm_rps_events;
36220a9a8c91SDaniel Vetter 
36230a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
36240a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
36250a9a8c91SDaniel Vetter 
3626605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
362735079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
36280a9a8c91SDaniel Vetter 	}
36290a9a8c91SDaniel Vetter }
36300a9a8c91SDaniel Vetter 
3631f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3632036a4a7dSZhenyu Wang {
36334bc9d430SDaniel Vetter 	unsigned long irqflags;
36342d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36358e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
36368e76f8dcSPaulo Zanoni 
36378e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
36388e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
36398e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
36408e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
36415c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
36428e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
36435c673b60SDaniel Vetter 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
36448e76f8dcSPaulo Zanoni 	} else {
36458e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3646ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
36475b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
36485b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
36495b3a856bSDaniel Vetter 				DE_POISON);
36505c673b60SDaniel Vetter 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
36515c673b60SDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
36528e76f8dcSPaulo Zanoni 	}
3653036a4a7dSZhenyu Wang 
36541ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3655036a4a7dSZhenyu Wang 
36560c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
36570c841212SPaulo Zanoni 
3658622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3659622364b6SPaulo Zanoni 
366035079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3661036a4a7dSZhenyu Wang 
36620a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3663036a4a7dSZhenyu Wang 
3664d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
36657fe0b973SKeith Packard 
3666f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
36676005ce42SDaniel Vetter 		/* Enable PCU event interrupts
36686005ce42SDaniel Vetter 		 *
36696005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
36704bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
36714bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
36724bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3673f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
36744bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3675f97108d1SJesse Barnes 	}
3676f97108d1SJesse Barnes 
3677036a4a7dSZhenyu Wang 	return 0;
3678036a4a7dSZhenyu Wang }
3679036a4a7dSZhenyu Wang 
3680f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3681f8b79e58SImre Deak {
3682f8b79e58SImre Deak 	u32 pipestat_mask;
3683f8b79e58SImre Deak 	u32 iir_mask;
3684f8b79e58SImre Deak 
3685f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3686f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3687f8b79e58SImre Deak 
3688f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3689f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3690f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3691f8b79e58SImre Deak 
3692f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3693f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3694f8b79e58SImre Deak 
3695f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3696f8b79e58SImre Deak 					       PIPE_GMBUS_INTERRUPT_STATUS);
3697f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3698f8b79e58SImre Deak 
3699f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3700f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3701f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3702f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3703f8b79e58SImre Deak 
3704f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3705f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3706f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3707f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3708f8b79e58SImre Deak 	POSTING_READ(VLV_IER);
3709f8b79e58SImre Deak }
3710f8b79e58SImre Deak 
3711f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3712f8b79e58SImre Deak {
3713f8b79e58SImre Deak 	u32 pipestat_mask;
3714f8b79e58SImre Deak 	u32 iir_mask;
3715f8b79e58SImre Deak 
3716f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3717f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
37186c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3719f8b79e58SImre Deak 
3720f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3721f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3722f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3723f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3724f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3725f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3726f8b79e58SImre Deak 
3727f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3728f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3729f8b79e58SImre Deak 
3730f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3731f8b79e58SImre Deak 					        PIPE_GMBUS_INTERRUPT_STATUS);
3732f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3733f8b79e58SImre Deak 
3734f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3735f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3736f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3737f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3738f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3739f8b79e58SImre Deak }
3740f8b79e58SImre Deak 
3741f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3742f8b79e58SImre Deak {
3743f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3744f8b79e58SImre Deak 
3745f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3746f8b79e58SImre Deak 		return;
3747f8b79e58SImre Deak 
3748f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3749f8b79e58SImre Deak 
3750f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3751f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3752f8b79e58SImre Deak }
3753f8b79e58SImre Deak 
3754f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3755f8b79e58SImre Deak {
3756f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3757f8b79e58SImre Deak 
3758f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3759f8b79e58SImre Deak 		return;
3760f8b79e58SImre Deak 
3761f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3762f8b79e58SImre Deak 
3763f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3764f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3765f8b79e58SImre Deak }
3766f8b79e58SImre Deak 
37677e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
37687e231dbeSJesse Barnes {
37692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3770b79480baSDaniel Vetter 	unsigned long irqflags;
37717e231dbeSJesse Barnes 
3772f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
37737e231dbeSJesse Barnes 
377420afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
377520afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
377620afbda2SDaniel Vetter 
37777e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3778f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
37797e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
37807e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
37817e231dbeSJesse Barnes 
3782b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3783b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3784b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3785f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3786f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3787b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
378831acc7f5SJesse Barnes 
37897e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
37907e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
37917e231dbeSJesse Barnes 
37920a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
37937e231dbeSJesse Barnes 
37947e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
37957e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
37967e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
37977e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
37987e231dbeSJesse Barnes #endif
37997e231dbeSJesse Barnes 
38007e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
380120afbda2SDaniel Vetter 
380220afbda2SDaniel Vetter 	return 0;
380320afbda2SDaniel Vetter }
380420afbda2SDaniel Vetter 
3805abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3806abd58f01SBen Widawsky {
3807abd58f01SBen Widawsky 	int i;
3808abd58f01SBen Widawsky 
3809abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3810abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3811abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
381273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3813abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
381473d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
381573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3816abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
381773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
381873d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
381973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3820abd58f01SBen Widawsky 		0,
382173d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
382273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3823abd58f01SBen Widawsky 		};
3824abd58f01SBen Widawsky 
3825337ba017SPaulo Zanoni 	for (i = 0; i < ARRAY_SIZE(gt_interrupts); i++)
382635079899SPaulo Zanoni 		GEN8_IRQ_INIT_NDX(GT, i, ~gt_interrupts[i], gt_interrupts[i]);
38270961021aSBen Widawsky 
38280961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
3829abd58f01SBen Widawsky }
3830abd58f01SBen Widawsky 
3831abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3832abd58f01SBen Widawsky {
3833abd58f01SBen Widawsky 	struct drm_device *dev = dev_priv->dev;
3834d0e1f1cbSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_PRIMARY_FLIP_DONE |
38350fbe7870SDaniel Vetter 		GEN8_PIPE_CDCLK_CRC_DONE |
383630100f2bSDaniel Vetter 		GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
38375c673b60SDaniel Vetter 	uint32_t de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
38385c673b60SDaniel Vetter 		GEN8_PIPE_FIFO_UNDERRUN;
3839abd58f01SBen Widawsky 	int pipe;
384013b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
384113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
384213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3843abd58f01SBen Widawsky 
3844337ba017SPaulo Zanoni 	for_each_pipe(pipe)
3845813bde43SPaulo Zanoni 		if (intel_display_power_enabled(dev_priv,
3846813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3847813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3848813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
384935079899SPaulo Zanoni 					  de_pipe_enables);
3850abd58f01SBen Widawsky 
385135079899SPaulo Zanoni 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~GEN8_AUX_CHANNEL_A, GEN8_AUX_CHANNEL_A);
3852abd58f01SBen Widawsky }
3853abd58f01SBen Widawsky 
3854abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3855abd58f01SBen Widawsky {
3856abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3857abd58f01SBen Widawsky 
3858622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3859622364b6SPaulo Zanoni 
3860abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3861abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3862abd58f01SBen Widawsky 
3863abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3864abd58f01SBen Widawsky 
3865abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3866abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3867abd58f01SBen Widawsky 
3868abd58f01SBen Widawsky 	return 0;
3869abd58f01SBen Widawsky }
3870abd58f01SBen Widawsky 
387143f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
387243f328d7SVille Syrjälä {
387343f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
387443f328d7SVille Syrjälä 	u32 enable_mask = I915_DISPLAY_PORT_INTERRUPT |
387543f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
387643f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
38773278f67fSVille Syrjälä 		I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
38783278f67fSVille Syrjälä 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_STATUS_VLV |
38793278f67fSVille Syrjälä 		PIPE_CRC_DONE_INTERRUPT_STATUS;
388043f328d7SVille Syrjälä 	unsigned long irqflags;
388143f328d7SVille Syrjälä 	int pipe;
388243f328d7SVille Syrjälä 
388343f328d7SVille Syrjälä 	/*
388443f328d7SVille Syrjälä 	 * Leave vblank interrupts masked initially.  enable/disable will
388543f328d7SVille Syrjälä 	 * toggle them based on usage.
388643f328d7SVille Syrjälä 	 */
38873278f67fSVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
388843f328d7SVille Syrjälä 
388943f328d7SVille Syrjälä 	for_each_pipe(pipe)
389043f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
389143f328d7SVille Syrjälä 
389243f328d7SVille Syrjälä 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
38933278f67fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
389443f328d7SVille Syrjälä 	for_each_pipe(pipe)
389543f328d7SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_enable);
389643f328d7SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
389743f328d7SVille Syrjälä 
389843f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
389943f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
390043f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, enable_mask);
390143f328d7SVille Syrjälä 
390243f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
390343f328d7SVille Syrjälä 
390443f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
390543f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
390643f328d7SVille Syrjälä 
390743f328d7SVille Syrjälä 	return 0;
390843f328d7SVille Syrjälä }
390943f328d7SVille Syrjälä 
3910abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3911abd58f01SBen Widawsky {
3912abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3913abd58f01SBen Widawsky 
3914abd58f01SBen Widawsky 	if (!dev_priv)
3915abd58f01SBen Widawsky 		return;
3916abd58f01SBen Widawsky 
3917d4eb6b10SPaulo Zanoni 	intel_hpd_irq_uninstall(dev_priv);
3918abd58f01SBen Widawsky 
3919823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3920abd58f01SBen Widawsky }
3921abd58f01SBen Widawsky 
39227e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
39237e231dbeSJesse Barnes {
39242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3925f8b79e58SImre Deak 	unsigned long irqflags;
39267e231dbeSJesse Barnes 	int pipe;
39277e231dbeSJesse Barnes 
39287e231dbeSJesse Barnes 	if (!dev_priv)
39297e231dbeSJesse Barnes 		return;
39307e231dbeSJesse Barnes 
3931843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3932843d0e7dSImre Deak 
39333ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3934ac4c16c5SEgbert Eich 
39357e231dbeSJesse Barnes 	for_each_pipe(pipe)
39367e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
39377e231dbeSJesse Barnes 
39387e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
39397e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
39407e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3941f8b79e58SImre Deak 
3942f8b79e58SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3943f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3944f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3945f8b79e58SImre Deak 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3946f8b79e58SImre Deak 
3947f8b79e58SImre Deak 	dev_priv->irq_mask = 0;
3948f8b79e58SImre Deak 
39497e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
39507e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
39517e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
39527e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
39537e231dbeSJesse Barnes }
39547e231dbeSJesse Barnes 
395543f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
395643f328d7SVille Syrjälä {
395743f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
395843f328d7SVille Syrjälä 	int pipe;
395943f328d7SVille Syrjälä 
396043f328d7SVille Syrjälä 	if (!dev_priv)
396143f328d7SVille Syrjälä 		return;
396243f328d7SVille Syrjälä 
396343f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
396443f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
396543f328d7SVille Syrjälä 
396643f328d7SVille Syrjälä #define GEN8_IRQ_FINI_NDX(type, which)				\
396743f328d7SVille Syrjälä do {								\
396843f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff);	\
396943f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IER(which), 0);		\
397043f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff);	\
397143f328d7SVille Syrjälä 	POSTING_READ(GEN8_##type##_IIR(which));			\
397243f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff);	\
397343f328d7SVille Syrjälä } while (0)
397443f328d7SVille Syrjälä 
397543f328d7SVille Syrjälä #define GEN8_IRQ_FINI(type)				\
397643f328d7SVille Syrjälä do {							\
397743f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR, 0xffffffff);	\
397843f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IER, 0);		\
397943f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR, 0xffffffff);	\
398043f328d7SVille Syrjälä 	POSTING_READ(GEN8_##type##_IIR);		\
398143f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR, 0xffffffff);	\
398243f328d7SVille Syrjälä } while (0)
398343f328d7SVille Syrjälä 
398443f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 0);
398543f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 1);
398643f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 2);
398743f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 3);
398843f328d7SVille Syrjälä 
398943f328d7SVille Syrjälä 	GEN8_IRQ_FINI(PCU);
399043f328d7SVille Syrjälä 
399143f328d7SVille Syrjälä #undef GEN8_IRQ_FINI
399243f328d7SVille Syrjälä #undef GEN8_IRQ_FINI_NDX
399343f328d7SVille Syrjälä 
399443f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
399543f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
399643f328d7SVille Syrjälä 
399743f328d7SVille Syrjälä 	for_each_pipe(pipe)
399843f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
399943f328d7SVille Syrjälä 
400043f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, 0xffffffff);
400143f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, 0x0);
400243f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
400343f328d7SVille Syrjälä 	POSTING_READ(VLV_IIR);
400443f328d7SVille Syrjälä }
400543f328d7SVille Syrjälä 
4006f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
4007036a4a7dSZhenyu Wang {
40082d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
40094697995bSJesse Barnes 
40104697995bSJesse Barnes 	if (!dev_priv)
40114697995bSJesse Barnes 		return;
40124697995bSJesse Barnes 
40133ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
4014ac4c16c5SEgbert Eich 
4015be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
4016036a4a7dSZhenyu Wang }
4017036a4a7dSZhenyu Wang 
4018c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
4019c2798b19SChris Wilson {
40202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4021c2798b19SChris Wilson 	int pipe;
4022c2798b19SChris Wilson 
4023c2798b19SChris Wilson 	for_each_pipe(pipe)
4024c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4025c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
4026c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
4027c2798b19SChris Wilson 	POSTING_READ16(IER);
4028c2798b19SChris Wilson }
4029c2798b19SChris Wilson 
4030c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
4031c2798b19SChris Wilson {
40322d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4033379ef82dSDaniel Vetter 	unsigned long irqflags;
4034c2798b19SChris Wilson 
4035c2798b19SChris Wilson 	I915_WRITE16(EMR,
4036c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
4037c2798b19SChris Wilson 
4038c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
4039c2798b19SChris Wilson 	dev_priv->irq_mask =
4040c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4041c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4042c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4043c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4044c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4045c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
4046c2798b19SChris Wilson 
4047c2798b19SChris Wilson 	I915_WRITE16(IER,
4048c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4049c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4050c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
4051c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
4052c2798b19SChris Wilson 	POSTING_READ16(IER);
4053c2798b19SChris Wilson 
4054379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4055379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4056379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4057755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4058755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4059379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4060379ef82dSDaniel Vetter 
4061c2798b19SChris Wilson 	return 0;
4062c2798b19SChris Wilson }
4063c2798b19SChris Wilson 
406490a72f87SVille Syrjälä /*
406590a72f87SVille Syrjälä  * Returns true when a page flip has completed.
406690a72f87SVille Syrjälä  */
406790a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
40681f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
406990a72f87SVille Syrjälä {
40702d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
40711f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
407290a72f87SVille Syrjälä 
40738d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
407490a72f87SVille Syrjälä 		return false;
407590a72f87SVille Syrjälä 
407690a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
407790a72f87SVille Syrjälä 		return false;
407890a72f87SVille Syrjälä 
40791f1c2e24SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
408090a72f87SVille Syrjälä 
408190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
408290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
408390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
408490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
408590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
408690a72f87SVille Syrjälä 	 */
408790a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
408890a72f87SVille Syrjälä 		return false;
408990a72f87SVille Syrjälä 
409090a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
409190a72f87SVille Syrjälä 
409290a72f87SVille Syrjälä 	return true;
409390a72f87SVille Syrjälä }
409490a72f87SVille Syrjälä 
4095ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
4096c2798b19SChris Wilson {
409745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
40982d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4099c2798b19SChris Wilson 	u16 iir, new_iir;
4100c2798b19SChris Wilson 	u32 pipe_stats[2];
4101c2798b19SChris Wilson 	unsigned long irqflags;
4102c2798b19SChris Wilson 	int pipe;
4103c2798b19SChris Wilson 	u16 flip_mask =
4104c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4105c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4106c2798b19SChris Wilson 
4107c2798b19SChris Wilson 	iir = I915_READ16(IIR);
4108c2798b19SChris Wilson 	if (iir == 0)
4109c2798b19SChris Wilson 		return IRQ_NONE;
4110c2798b19SChris Wilson 
4111c2798b19SChris Wilson 	while (iir & ~flip_mask) {
4112c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4113c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4114c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4115c2798b19SChris Wilson 		 * interrupts (for non-MSI).
4116c2798b19SChris Wilson 		 */
4117c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4118c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
411958174462SMika Kuoppala 			i915_handle_error(dev, false,
412058174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
412158174462SMika Kuoppala 					  iir);
4122c2798b19SChris Wilson 
4123c2798b19SChris Wilson 		for_each_pipe(pipe) {
4124c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
4125c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4126c2798b19SChris Wilson 
4127c2798b19SChris Wilson 			/*
4128c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4129c2798b19SChris Wilson 			 */
41302d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
4131c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4132c2798b19SChris Wilson 		}
4133c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4134c2798b19SChris Wilson 
4135c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
4136c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
4137c2798b19SChris Wilson 
4138d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
4139c2798b19SChris Wilson 
4140c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4141c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
4142c2798b19SChris Wilson 
41434356d586SDaniel Vetter 		for_each_pipe(pipe) {
41441f1c2e24SVille Syrjälä 			int plane = pipe;
41453a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
41461f1c2e24SVille Syrjälä 				plane = !plane;
41471f1c2e24SVille Syrjälä 
41484356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
41491f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
41501f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4151c2798b19SChris Wilson 
41524356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4153277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
41542d9d2b0bSVille Syrjälä 
41552d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
41562d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
4157fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
41584356d586SDaniel Vetter 		}
4159c2798b19SChris Wilson 
4160c2798b19SChris Wilson 		iir = new_iir;
4161c2798b19SChris Wilson 	}
4162c2798b19SChris Wilson 
4163c2798b19SChris Wilson 	return IRQ_HANDLED;
4164c2798b19SChris Wilson }
4165c2798b19SChris Wilson 
4166c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
4167c2798b19SChris Wilson {
41682d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4169c2798b19SChris Wilson 	int pipe;
4170c2798b19SChris Wilson 
4171c2798b19SChris Wilson 	for_each_pipe(pipe) {
4172c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
4173c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4174c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
4175c2798b19SChris Wilson 	}
4176c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
4177c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
4178c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
4179c2798b19SChris Wilson }
4180c2798b19SChris Wilson 
4181a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
4182a266c7d5SChris Wilson {
41832d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4184a266c7d5SChris Wilson 	int pipe;
4185a266c7d5SChris Wilson 
4186a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
4187a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
4188a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4189a266c7d5SChris Wilson 	}
4190a266c7d5SChris Wilson 
419100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
4192a266c7d5SChris Wilson 	for_each_pipe(pipe)
4193a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4194a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4195a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4196a266c7d5SChris Wilson 	POSTING_READ(IER);
4197a266c7d5SChris Wilson }
4198a266c7d5SChris Wilson 
4199a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4200a266c7d5SChris Wilson {
42012d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
420238bde180SChris Wilson 	u32 enable_mask;
4203379ef82dSDaniel Vetter 	unsigned long irqflags;
4204a266c7d5SChris Wilson 
420538bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
420638bde180SChris Wilson 
420738bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
420838bde180SChris Wilson 	dev_priv->irq_mask =
420938bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
421038bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
421138bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
421238bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
421338bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
421438bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
421538bde180SChris Wilson 
421638bde180SChris Wilson 	enable_mask =
421738bde180SChris Wilson 		I915_ASLE_INTERRUPT |
421838bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
421938bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
422038bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
422138bde180SChris Wilson 		I915_USER_INTERRUPT;
422238bde180SChris Wilson 
4223a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
422420afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
422520afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
422620afbda2SDaniel Vetter 
4227a266c7d5SChris Wilson 		/* Enable in IER... */
4228a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4229a266c7d5SChris Wilson 		/* and unmask in IMR */
4230a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4231a266c7d5SChris Wilson 	}
4232a266c7d5SChris Wilson 
4233a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4234a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4235a266c7d5SChris Wilson 	POSTING_READ(IER);
4236a266c7d5SChris Wilson 
4237f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
423820afbda2SDaniel Vetter 
4239379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4240379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4241379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4242755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4243755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4244379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4245379ef82dSDaniel Vetter 
424620afbda2SDaniel Vetter 	return 0;
424720afbda2SDaniel Vetter }
424820afbda2SDaniel Vetter 
424990a72f87SVille Syrjälä /*
425090a72f87SVille Syrjälä  * Returns true when a page flip has completed.
425190a72f87SVille Syrjälä  */
425290a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
425390a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
425490a72f87SVille Syrjälä {
42552d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
425690a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
425790a72f87SVille Syrjälä 
42588d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
425990a72f87SVille Syrjälä 		return false;
426090a72f87SVille Syrjälä 
426190a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
426290a72f87SVille Syrjälä 		return false;
426390a72f87SVille Syrjälä 
426490a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
426590a72f87SVille Syrjälä 
426690a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
426790a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
426890a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
426990a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
427090a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
427190a72f87SVille Syrjälä 	 */
427290a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
427390a72f87SVille Syrjälä 		return false;
427490a72f87SVille Syrjälä 
427590a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
427690a72f87SVille Syrjälä 
427790a72f87SVille Syrjälä 	return true;
427890a72f87SVille Syrjälä }
427990a72f87SVille Syrjälä 
4280ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4281a266c7d5SChris Wilson {
428245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
42832d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
42848291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
4285a266c7d5SChris Wilson 	unsigned long irqflags;
428638bde180SChris Wilson 	u32 flip_mask =
428738bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
428838bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
428938bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4290a266c7d5SChris Wilson 
4291a266c7d5SChris Wilson 	iir = I915_READ(IIR);
429238bde180SChris Wilson 	do {
429338bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
42948291ee90SChris Wilson 		bool blc_event = false;
4295a266c7d5SChris Wilson 
4296a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4297a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4298a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4299a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4300a266c7d5SChris Wilson 		 */
4301a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4302a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
430358174462SMika Kuoppala 			i915_handle_error(dev, false,
430458174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
430558174462SMika Kuoppala 					  iir);
4306a266c7d5SChris Wilson 
4307a266c7d5SChris Wilson 		for_each_pipe(pipe) {
4308a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4309a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4310a266c7d5SChris Wilson 
431138bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4312a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4313a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
431438bde180SChris Wilson 				irq_received = true;
4315a266c7d5SChris Wilson 			}
4316a266c7d5SChris Wilson 		}
4317a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4318a266c7d5SChris Wilson 
4319a266c7d5SChris Wilson 		if (!irq_received)
4320a266c7d5SChris Wilson 			break;
4321a266c7d5SChris Wilson 
4322a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
432316c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
432416c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
432516c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4326a266c7d5SChris Wilson 
432738bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4328a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4329a266c7d5SChris Wilson 
4330a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4331a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
4332a266c7d5SChris Wilson 
4333a266c7d5SChris Wilson 		for_each_pipe(pipe) {
433438bde180SChris Wilson 			int plane = pipe;
43353a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
433638bde180SChris Wilson 				plane = !plane;
43375e2032d4SVille Syrjälä 
433890a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
433990a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
434090a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4341a266c7d5SChris Wilson 
4342a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4343a266c7d5SChris Wilson 				blc_event = true;
43444356d586SDaniel Vetter 
43454356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4346277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
43472d9d2b0bSVille Syrjälä 
43482d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
43492d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
4350fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
4351a266c7d5SChris Wilson 		}
4352a266c7d5SChris Wilson 
4353a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4354a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4355a266c7d5SChris Wilson 
4356a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4357a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4358a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4359a266c7d5SChris Wilson 		 * we would never get another interrupt.
4360a266c7d5SChris Wilson 		 *
4361a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4362a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4363a266c7d5SChris Wilson 		 * another one.
4364a266c7d5SChris Wilson 		 *
4365a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4366a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4367a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4368a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4369a266c7d5SChris Wilson 		 * stray interrupts.
4370a266c7d5SChris Wilson 		 */
437138bde180SChris Wilson 		ret = IRQ_HANDLED;
4372a266c7d5SChris Wilson 		iir = new_iir;
437338bde180SChris Wilson 	} while (iir & ~flip_mask);
4374a266c7d5SChris Wilson 
4375d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
43768291ee90SChris Wilson 
4377a266c7d5SChris Wilson 	return ret;
4378a266c7d5SChris Wilson }
4379a266c7d5SChris Wilson 
4380a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4381a266c7d5SChris Wilson {
43822d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4383a266c7d5SChris Wilson 	int pipe;
4384a266c7d5SChris Wilson 
43853ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
4386ac4c16c5SEgbert Eich 
4387a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
4388a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
4389a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4390a266c7d5SChris Wilson 	}
4391a266c7d5SChris Wilson 
439200d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
439355b39755SChris Wilson 	for_each_pipe(pipe) {
439455b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4395a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
439655b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
439755b39755SChris Wilson 	}
4398a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4399a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4400a266c7d5SChris Wilson 
4401a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4402a266c7d5SChris Wilson }
4403a266c7d5SChris Wilson 
4404a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4405a266c7d5SChris Wilson {
44062d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4407a266c7d5SChris Wilson 	int pipe;
4408a266c7d5SChris Wilson 
4409a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4410a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4411a266c7d5SChris Wilson 
4412a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4413a266c7d5SChris Wilson 	for_each_pipe(pipe)
4414a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4415a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4416a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4417a266c7d5SChris Wilson 	POSTING_READ(IER);
4418a266c7d5SChris Wilson }
4419a266c7d5SChris Wilson 
4420a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4421a266c7d5SChris Wilson {
44222d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4423bbba0a97SChris Wilson 	u32 enable_mask;
4424a266c7d5SChris Wilson 	u32 error_mask;
4425b79480baSDaniel Vetter 	unsigned long irqflags;
4426a266c7d5SChris Wilson 
4427a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4428bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4429adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4430bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4431bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4432bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4433bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4434bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4435bbba0a97SChris Wilson 
4436bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
443721ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
443821ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4439bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4440bbba0a97SChris Wilson 
4441bbba0a97SChris Wilson 	if (IS_G4X(dev))
4442bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4443a266c7d5SChris Wilson 
4444b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4445b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4446b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4447755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4448755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4449755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4450b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4451a266c7d5SChris Wilson 
4452a266c7d5SChris Wilson 	/*
4453a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4454a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4455a266c7d5SChris Wilson 	 */
4456a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4457a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4458a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4459a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4460a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4461a266c7d5SChris Wilson 	} else {
4462a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4463a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4464a266c7d5SChris Wilson 	}
4465a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4466a266c7d5SChris Wilson 
4467a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4468a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4469a266c7d5SChris Wilson 	POSTING_READ(IER);
4470a266c7d5SChris Wilson 
447120afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
447220afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
447320afbda2SDaniel Vetter 
4474f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
447520afbda2SDaniel Vetter 
447620afbda2SDaniel Vetter 	return 0;
447720afbda2SDaniel Vetter }
447820afbda2SDaniel Vetter 
4479bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
448020afbda2SDaniel Vetter {
44812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4482cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
448320afbda2SDaniel Vetter 	u32 hotplug_en;
448420afbda2SDaniel Vetter 
4485b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4486b5ea2d56SDaniel Vetter 
4487bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
4488bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4489bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4490adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
4491e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
4492b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
4493cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4494cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
4495a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
4496a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
4497a266c7d5SChris Wilson 		   seconds later.  So just do it once.
4498a266c7d5SChris Wilson 		*/
4499a266c7d5SChris Wilson 		if (IS_G4X(dev))
4500a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
450185fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4502a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4503a266c7d5SChris Wilson 
4504a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
4505a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4506a266c7d5SChris Wilson 	}
4507bac56d5bSEgbert Eich }
4508a266c7d5SChris Wilson 
4509ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4510a266c7d5SChris Wilson {
451145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
45122d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4513a266c7d5SChris Wilson 	u32 iir, new_iir;
4514a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4515a266c7d5SChris Wilson 	unsigned long irqflags;
4516a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
451721ad8330SVille Syrjälä 	u32 flip_mask =
451821ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
451921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4520a266c7d5SChris Wilson 
4521a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4522a266c7d5SChris Wilson 
4523a266c7d5SChris Wilson 	for (;;) {
4524501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
45252c8ba29fSChris Wilson 		bool blc_event = false;
45262c8ba29fSChris Wilson 
4527a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4528a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4529a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4530a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4531a266c7d5SChris Wilson 		 */
4532a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4533a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
453458174462SMika Kuoppala 			i915_handle_error(dev, false,
453558174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
453658174462SMika Kuoppala 					  iir);
4537a266c7d5SChris Wilson 
4538a266c7d5SChris Wilson 		for_each_pipe(pipe) {
4539a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4540a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4541a266c7d5SChris Wilson 
4542a266c7d5SChris Wilson 			/*
4543a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4544a266c7d5SChris Wilson 			 */
4545a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4546a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4547501e01d7SVille Syrjälä 				irq_received = true;
4548a266c7d5SChris Wilson 			}
4549a266c7d5SChris Wilson 		}
4550a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4551a266c7d5SChris Wilson 
4552a266c7d5SChris Wilson 		if (!irq_received)
4553a266c7d5SChris Wilson 			break;
4554a266c7d5SChris Wilson 
4555a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4556a266c7d5SChris Wilson 
4557a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
455816c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
455916c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4560a266c7d5SChris Wilson 
456121ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4562a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4563a266c7d5SChris Wilson 
4564a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4565a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
4566a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
4567a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
4568a266c7d5SChris Wilson 
4569a266c7d5SChris Wilson 		for_each_pipe(pipe) {
45702c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
457190a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
457290a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4573a266c7d5SChris Wilson 
4574a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4575a266c7d5SChris Wilson 				blc_event = true;
45764356d586SDaniel Vetter 
45774356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4578277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4579a266c7d5SChris Wilson 
45802d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
45812d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
4582fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
45832d9d2b0bSVille Syrjälä 		}
4584a266c7d5SChris Wilson 
4585a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4586a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4587a266c7d5SChris Wilson 
4588515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4589515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4590515ac2bbSDaniel Vetter 
4591a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4592a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4593a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4594a266c7d5SChris Wilson 		 * we would never get another interrupt.
4595a266c7d5SChris Wilson 		 *
4596a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4597a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4598a266c7d5SChris Wilson 		 * another one.
4599a266c7d5SChris Wilson 		 *
4600a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4601a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4602a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4603a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4604a266c7d5SChris Wilson 		 * stray interrupts.
4605a266c7d5SChris Wilson 		 */
4606a266c7d5SChris Wilson 		iir = new_iir;
4607a266c7d5SChris Wilson 	}
4608a266c7d5SChris Wilson 
4609d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
46102c8ba29fSChris Wilson 
4611a266c7d5SChris Wilson 	return ret;
4612a266c7d5SChris Wilson }
4613a266c7d5SChris Wilson 
4614a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4615a266c7d5SChris Wilson {
46162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4617a266c7d5SChris Wilson 	int pipe;
4618a266c7d5SChris Wilson 
4619a266c7d5SChris Wilson 	if (!dev_priv)
4620a266c7d5SChris Wilson 		return;
4621a266c7d5SChris Wilson 
46223ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
4623ac4c16c5SEgbert Eich 
4624a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4625a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4626a266c7d5SChris Wilson 
4627a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4628a266c7d5SChris Wilson 	for_each_pipe(pipe)
4629a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4630a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4631a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4632a266c7d5SChris Wilson 
4633a266c7d5SChris Wilson 	for_each_pipe(pipe)
4634a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4635a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4636a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4637a266c7d5SChris Wilson }
4638a266c7d5SChris Wilson 
46393ca1ccedSVille Syrjälä static void intel_hpd_irq_reenable(unsigned long data)
4640ac4c16c5SEgbert Eich {
46412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = (struct drm_i915_private *)data;
4642ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
4643ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4644ac4c16c5SEgbert Eich 	unsigned long irqflags;
4645ac4c16c5SEgbert Eich 	int i;
4646ac4c16c5SEgbert Eich 
4647ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4648ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4649ac4c16c5SEgbert Eich 		struct drm_connector *connector;
4650ac4c16c5SEgbert Eich 
4651ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4652ac4c16c5SEgbert Eich 			continue;
4653ac4c16c5SEgbert Eich 
4654ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4655ac4c16c5SEgbert Eich 
4656ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
4657ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
4658ac4c16c5SEgbert Eich 
4659ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4660ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4661ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4662c23cc417SJani Nikula 							 connector->name);
4663ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4664ac4c16c5SEgbert Eich 				if (!connector->polled)
4665ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4666ac4c16c5SEgbert Eich 			}
4667ac4c16c5SEgbert Eich 		}
4668ac4c16c5SEgbert Eich 	}
4669ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4670ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
4671ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4672ac4c16c5SEgbert Eich }
4673ac4c16c5SEgbert Eich 
4674f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
4675f71d4af4SJesse Barnes {
46768b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
46778b2e326dSChris Wilson 
46788b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
467913cf5504SDave Airlie 	INIT_WORK(&dev_priv->dig_port_work, i915_digport_work_func);
468099584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
4681c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4682a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
46838b2e326dSChris Wilson 
4684a6706b45SDeepak S 	/* Let's track the enabled rps events */
468531685c25SDeepak S 	if (IS_VALLEYVIEW(dev))
468631685c25SDeepak S 		/* WaGsvRC0ResidenncyMethod:VLV */
468731685c25SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
468831685c25SDeepak S 	else
4689a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4690a6706b45SDeepak S 
469199584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
469299584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
469361bac78eSDaniel Vetter 		    (unsigned long) dev);
46943ca1ccedSVille Syrjälä 	setup_timer(&dev_priv->hotplug_reenable_timer, intel_hpd_irq_reenable,
4695ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
469661bac78eSDaniel Vetter 
469797a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
46989ee32feaSDaniel Vetter 
469995f25bedSJesse Barnes 	/* Haven't installed the IRQ handler yet */
470095f25bedSJesse Barnes 	dev_priv->pm._irqs_disabled = true;
470195f25bedSJesse Barnes 
47024cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
47034cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
47044cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
47054cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
4706f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4707f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4708391f75e2SVille Syrjälä 	} else {
4709391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4710391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4711f71d4af4SJesse Barnes 	}
4712f71d4af4SJesse Barnes 
4713c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
4714f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4715f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4716c2baf4b7SVille Syrjälä 	}
4717f71d4af4SJesse Barnes 
471843f328d7SVille Syrjälä 	if (IS_CHERRYVIEW(dev)) {
471943f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
472043f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
472143f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
472243f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
472343f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
472443f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
472543f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
472643f328d7SVille Syrjälä 	} else if (IS_VALLEYVIEW(dev)) {
47277e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
47287e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
47297e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
47307e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
47317e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
47327e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4733fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4734abd58f01SBen Widawsky 	} else if (IS_GEN8(dev)) {
4735abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4736723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4737abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4738abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4739abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4740abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4741abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4742f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4743f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4744723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4745f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4746f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4747f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4748f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
474982a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4750f71d4af4SJesse Barnes 	} else {
4751c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
4752c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4753c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4754c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4755c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4756a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
4757a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4758a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4759a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4760a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
476120afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4762c2798b19SChris Wilson 		} else {
4763a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4764a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4765a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4766a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4767bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4768c2798b19SChris Wilson 		}
4769f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4770f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4771f71d4af4SJesse Barnes 	}
4772f71d4af4SJesse Barnes }
477320afbda2SDaniel Vetter 
477420afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
477520afbda2SDaniel Vetter {
477620afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
4777821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4778821450c6SEgbert Eich 	struct drm_connector *connector;
4779b5ea2d56SDaniel Vetter 	unsigned long irqflags;
4780821450c6SEgbert Eich 	int i;
478120afbda2SDaniel Vetter 
4782821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4783821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4784821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4785821450c6SEgbert Eich 	}
4786821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4787821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4788821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
47890e32b39cSDave Airlie 		if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
47900e32b39cSDave Airlie 			connector->polled = DRM_CONNECTOR_POLL_HPD;
47910e32b39cSDave Airlie 		if (intel_connector->mst_port)
4792821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4793821450c6SEgbert Eich 	}
4794b5ea2d56SDaniel Vetter 
4795b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4796b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4797b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
479820afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
479920afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4800b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
480120afbda2SDaniel Vetter }
4802c67a470bSPaulo Zanoni 
48035d584b2eSPaulo Zanoni /* Disable interrupts so we can allow runtime PM. */
4804730488b2SPaulo Zanoni void intel_runtime_pm_disable_interrupts(struct drm_device *dev)
4805c67a470bSPaulo Zanoni {
4806c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4807c67a470bSPaulo Zanoni 
4808730488b2SPaulo Zanoni 	dev->driver->irq_uninstall(dev);
48099df7575fSJesse Barnes 	dev_priv->pm._irqs_disabled = true;
4810c67a470bSPaulo Zanoni }
4811c67a470bSPaulo Zanoni 
48125d584b2eSPaulo Zanoni /* Restore interrupts so we can recover from runtime PM. */
4813730488b2SPaulo Zanoni void intel_runtime_pm_restore_interrupts(struct drm_device *dev)
4814c67a470bSPaulo Zanoni {
4815c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4816c67a470bSPaulo Zanoni 
48179df7575fSJesse Barnes 	dev_priv->pm._irqs_disabled = false;
4818730488b2SPaulo Zanoni 	dev->driver->irq_preinstall(dev);
4819730488b2SPaulo Zanoni 	dev->driver->irq_postinstall(dev);
4820c67a470bSPaulo Zanoni }
4821