xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 0961021aef788e9d0d4e34256655d6c761ef716b)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33b2c88f5bSDamien Lespiau #include <linux/circ_buf.h>
34760285e7SDavid Howells #include <drm/drmP.h>
35760285e7SDavid Howells #include <drm/i915_drm.h>
36c0e09200SDave Airlie #include "i915_drv.h"
371c5d22f7SChris Wilson #include "i915_trace.h"
3879e53945SJesse Barnes #include "intel_drv.h"
39c0e09200SDave Airlie 
40e5868a31SEgbert Eich static const u32 hpd_ibx[] = {
41e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
42e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
43e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
44e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
45e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
46e5868a31SEgbert Eich };
47e5868a31SEgbert Eich 
48e5868a31SEgbert Eich static const u32 hpd_cpt[] = {
49e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
5073c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
51e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
52e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
53e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
54e5868a31SEgbert Eich };
55e5868a31SEgbert Eich 
56e5868a31SEgbert Eich static const u32 hpd_mask_i915[] = {
57e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
58e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
59e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
60e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
61e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
62e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
63e5868a31SEgbert Eich };
64e5868a31SEgbert Eich 
65704cfb87SDaniel Vetter static const u32 hpd_status_g4x[] = {
66e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
67e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
68e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
69e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
70e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
71e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
72e5868a31SEgbert Eich };
73e5868a31SEgbert Eich 
74e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
75e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
76e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
77e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
78e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
79e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
80e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
81e5868a31SEgbert Eich };
82e5868a31SEgbert Eich 
835c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
84f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
855c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
865c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
875c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
885c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
895c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
905c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
915c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
925c502442SPaulo Zanoni } while (0)
935c502442SPaulo Zanoni 
94f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
95a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
965c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
97a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
985c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
995c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1005c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1015c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
102a9d356a6SPaulo Zanoni } while (0)
103a9d356a6SPaulo Zanoni 
104337ba017SPaulo Zanoni /*
105337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
106337ba017SPaulo Zanoni  */
107337ba017SPaulo Zanoni #define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
108337ba017SPaulo Zanoni 	u32 val = I915_READ(reg); \
109337ba017SPaulo Zanoni 	if (val) { \
110337ba017SPaulo Zanoni 		WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
111337ba017SPaulo Zanoni 		     (reg), val); \
112337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
113337ba017SPaulo Zanoni 		POSTING_READ(reg); \
114337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
115337ba017SPaulo Zanoni 		POSTING_READ(reg); \
116337ba017SPaulo Zanoni 	} \
117337ba017SPaulo Zanoni } while (0)
118337ba017SPaulo Zanoni 
11935079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
120337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
12135079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
12235079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
12335079899SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IER(which)); \
12435079899SPaulo Zanoni } while (0)
12535079899SPaulo Zanoni 
12635079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
127337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
12835079899SPaulo Zanoni 	I915_WRITE(type##IMR, (imr_val)); \
12935079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
13035079899SPaulo Zanoni 	POSTING_READ(type##IER); \
13135079899SPaulo Zanoni } while (0)
13235079899SPaulo Zanoni 
133036a4a7dSZhenyu Wang /* For display hotplug interrupt */
134995b6762SChris Wilson static void
1352d1013ddSJani Nikula ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
136036a4a7dSZhenyu Wang {
1374bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1384bc9d430SDaniel Vetter 
139730488b2SPaulo Zanoni 	if (WARN_ON(dev_priv->pm.irqs_disabled))
140c67a470bSPaulo Zanoni 		return;
141c67a470bSPaulo Zanoni 
1421ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
1431ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
1441ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1453143a2bfSChris Wilson 		POSTING_READ(DEIMR);
146036a4a7dSZhenyu Wang 	}
147036a4a7dSZhenyu Wang }
148036a4a7dSZhenyu Wang 
1490ff9800aSPaulo Zanoni static void
1502d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
151036a4a7dSZhenyu Wang {
1524bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1534bc9d430SDaniel Vetter 
154730488b2SPaulo Zanoni 	if (WARN_ON(dev_priv->pm.irqs_disabled))
155c67a470bSPaulo Zanoni 		return;
156c67a470bSPaulo Zanoni 
1571ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1581ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1591ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1603143a2bfSChris Wilson 		POSTING_READ(DEIMR);
161036a4a7dSZhenyu Wang 	}
162036a4a7dSZhenyu Wang }
163036a4a7dSZhenyu Wang 
16443eaea13SPaulo Zanoni /**
16543eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
16643eaea13SPaulo Zanoni  * @dev_priv: driver private
16743eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
16843eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
16943eaea13SPaulo Zanoni  */
17043eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
17143eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
17243eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
17343eaea13SPaulo Zanoni {
17443eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
17543eaea13SPaulo Zanoni 
176730488b2SPaulo Zanoni 	if (WARN_ON(dev_priv->pm.irqs_disabled))
177c67a470bSPaulo Zanoni 		return;
178c67a470bSPaulo Zanoni 
17943eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
18043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
18143eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
18243eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
18343eaea13SPaulo Zanoni }
18443eaea13SPaulo Zanoni 
18543eaea13SPaulo Zanoni void ilk_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
18643eaea13SPaulo Zanoni {
18743eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
18843eaea13SPaulo Zanoni }
18943eaea13SPaulo Zanoni 
19043eaea13SPaulo Zanoni void ilk_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
19143eaea13SPaulo Zanoni {
19243eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
19343eaea13SPaulo Zanoni }
19443eaea13SPaulo Zanoni 
195edbfdb45SPaulo Zanoni /**
196edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
197edbfdb45SPaulo Zanoni   * @dev_priv: driver private
198edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
199edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
200edbfdb45SPaulo Zanoni   */
201edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
202edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
203edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
204edbfdb45SPaulo Zanoni {
205605cd25bSPaulo Zanoni 	uint32_t new_val;
206edbfdb45SPaulo Zanoni 
207edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
208edbfdb45SPaulo Zanoni 
209730488b2SPaulo Zanoni 	if (WARN_ON(dev_priv->pm.irqs_disabled))
210c67a470bSPaulo Zanoni 		return;
211c67a470bSPaulo Zanoni 
212605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
213f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
214f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
215f52ecbcfSPaulo Zanoni 
216605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
217605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
218605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
219edbfdb45SPaulo Zanoni 		POSTING_READ(GEN6_PMIMR);
220edbfdb45SPaulo Zanoni 	}
221f52ecbcfSPaulo Zanoni }
222edbfdb45SPaulo Zanoni 
223edbfdb45SPaulo Zanoni void snb_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
224edbfdb45SPaulo Zanoni {
225edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
226edbfdb45SPaulo Zanoni }
227edbfdb45SPaulo Zanoni 
228edbfdb45SPaulo Zanoni void snb_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
229edbfdb45SPaulo Zanoni {
230edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, 0);
231edbfdb45SPaulo Zanoni }
232edbfdb45SPaulo Zanoni 
2338664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
2348664281bSPaulo Zanoni {
2358664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2368664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2378664281bSPaulo Zanoni 	enum pipe pipe;
2388664281bSPaulo Zanoni 
2394bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2404bc9d430SDaniel Vetter 
2418664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2428664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2438664281bSPaulo Zanoni 
2448664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
2458664281bSPaulo Zanoni 			return false;
2468664281bSPaulo Zanoni 	}
2478664281bSPaulo Zanoni 
2488664281bSPaulo Zanoni 	return true;
2498664281bSPaulo Zanoni }
2508664281bSPaulo Zanoni 
251*0961021aSBen Widawsky /**
252*0961021aSBen Widawsky   * bdw_update_pm_irq - update GT interrupt 2
253*0961021aSBen Widawsky   * @dev_priv: driver private
254*0961021aSBen Widawsky   * @interrupt_mask: mask of interrupt bits to update
255*0961021aSBen Widawsky   * @enabled_irq_mask: mask of interrupt bits to enable
256*0961021aSBen Widawsky   *
257*0961021aSBen Widawsky   * Copied from the snb function, updated with relevant register offsets
258*0961021aSBen Widawsky   */
259*0961021aSBen Widawsky static void bdw_update_pm_irq(struct drm_i915_private *dev_priv,
260*0961021aSBen Widawsky 			      uint32_t interrupt_mask,
261*0961021aSBen Widawsky 			      uint32_t enabled_irq_mask)
262*0961021aSBen Widawsky {
263*0961021aSBen Widawsky 	uint32_t new_val;
264*0961021aSBen Widawsky 
265*0961021aSBen Widawsky 	assert_spin_locked(&dev_priv->irq_lock);
266*0961021aSBen Widawsky 
267*0961021aSBen Widawsky 	if (WARN_ON(dev_priv->pm.irqs_disabled))
268*0961021aSBen Widawsky 		return;
269*0961021aSBen Widawsky 
270*0961021aSBen Widawsky 	new_val = dev_priv->pm_irq_mask;
271*0961021aSBen Widawsky 	new_val &= ~interrupt_mask;
272*0961021aSBen Widawsky 	new_val |= (~enabled_irq_mask & interrupt_mask);
273*0961021aSBen Widawsky 
274*0961021aSBen Widawsky 	if (new_val != dev_priv->pm_irq_mask) {
275*0961021aSBen Widawsky 		dev_priv->pm_irq_mask = new_val;
276*0961021aSBen Widawsky 		I915_WRITE(GEN8_GT_IMR(2), dev_priv->pm_irq_mask);
277*0961021aSBen Widawsky 		POSTING_READ(GEN8_GT_IMR(2));
278*0961021aSBen Widawsky 	}
279*0961021aSBen Widawsky }
280*0961021aSBen Widawsky 
281*0961021aSBen Widawsky void bdw_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
282*0961021aSBen Widawsky {
283*0961021aSBen Widawsky 	bdw_update_pm_irq(dev_priv, mask, mask);
284*0961021aSBen Widawsky }
285*0961021aSBen Widawsky 
286*0961021aSBen Widawsky void bdw_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
287*0961021aSBen Widawsky {
288*0961021aSBen Widawsky 	bdw_update_pm_irq(dev_priv, mask, 0);
289*0961021aSBen Widawsky }
290*0961021aSBen 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 
3092d9d2b0bSVille Syrjälä static void i9xx_clear_fifo_underrun(struct drm_device *dev, enum pipe pipe)
3102d9d2b0bSVille Syrjälä {
3112d9d2b0bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3122d9d2b0bSVille Syrjälä 	u32 reg = PIPESTAT(pipe);
3132d9d2b0bSVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3142d9d2b0bSVille Syrjälä 
3152d9d2b0bSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
3162d9d2b0bSVille Syrjälä 
3172d9d2b0bSVille Syrjälä 	I915_WRITE(reg, pipestat | PIPE_FIFO_UNDERRUN_STATUS);
3182d9d2b0bSVille Syrjälä 	POSTING_READ(reg);
3192d9d2b0bSVille Syrjälä }
3202d9d2b0bSVille Syrjälä 
3218664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
3228664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
3238664281bSPaulo Zanoni {
3248664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3258664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
3268664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
3278664281bSPaulo Zanoni 
3288664281bSPaulo Zanoni 	if (enable)
3298664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
3308664281bSPaulo Zanoni 	else
3318664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
3328664281bSPaulo Zanoni }
3338664281bSPaulo Zanoni 
3348664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
3357336df65SDaniel Vetter 						  enum pipe pipe, bool enable)
3368664281bSPaulo Zanoni {
3378664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3388664281bSPaulo Zanoni 	if (enable) {
3397336df65SDaniel Vetter 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
3407336df65SDaniel Vetter 
3418664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
3428664281bSPaulo Zanoni 			return;
3438664281bSPaulo Zanoni 
3448664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
3458664281bSPaulo Zanoni 	} else {
3467336df65SDaniel Vetter 		bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
3477336df65SDaniel Vetter 
3487336df65SDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
3498664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
3507336df65SDaniel Vetter 
3517336df65SDaniel Vetter 		if (!was_enabled &&
3527336df65SDaniel Vetter 		    (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
3537336df65SDaniel Vetter 			DRM_DEBUG_KMS("uncleared fifo underrun on pipe %c\n",
3547336df65SDaniel Vetter 				      pipe_name(pipe));
3557336df65SDaniel Vetter 		}
3568664281bSPaulo Zanoni 	}
3578664281bSPaulo Zanoni }
3588664281bSPaulo Zanoni 
35938d83c96SDaniel Vetter static void broadwell_set_fifo_underrun_reporting(struct drm_device *dev,
36038d83c96SDaniel Vetter 						  enum pipe pipe, bool enable)
36138d83c96SDaniel Vetter {
36238d83c96SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
36338d83c96SDaniel Vetter 
36438d83c96SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
36538d83c96SDaniel Vetter 
36638d83c96SDaniel Vetter 	if (enable)
36738d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_FIFO_UNDERRUN;
36838d83c96SDaniel Vetter 	else
36938d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_FIFO_UNDERRUN;
37038d83c96SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
37138d83c96SDaniel Vetter 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
37238d83c96SDaniel Vetter }
37338d83c96SDaniel Vetter 
374fee884edSDaniel Vetter /**
375fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
376fee884edSDaniel Vetter  * @dev_priv: driver private
377fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
378fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
379fee884edSDaniel Vetter  */
380fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
381fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
382fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
383fee884edSDaniel Vetter {
384fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
385fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
386fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
387fee884edSDaniel Vetter 
388fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
389fee884edSDaniel Vetter 
390730488b2SPaulo Zanoni 	if (WARN_ON(dev_priv->pm.irqs_disabled))
391c67a470bSPaulo Zanoni 		return;
392c67a470bSPaulo Zanoni 
393fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
394fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
395fee884edSDaniel Vetter }
396fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
397fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
398fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
399fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
400fee884edSDaniel Vetter 
401de28075dSDaniel Vetter static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
402de28075dSDaniel Vetter 					    enum transcoder pch_transcoder,
4038664281bSPaulo Zanoni 					    bool enable)
4048664281bSPaulo Zanoni {
4058664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
406de28075dSDaniel Vetter 	uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
407de28075dSDaniel Vetter 		       SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
4088664281bSPaulo Zanoni 
4098664281bSPaulo Zanoni 	if (enable)
410fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
4118664281bSPaulo Zanoni 	else
412fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
4138664281bSPaulo Zanoni }
4148664281bSPaulo Zanoni 
4158664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
4168664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
4178664281bSPaulo Zanoni 					    bool enable)
4188664281bSPaulo Zanoni {
4198664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4208664281bSPaulo Zanoni 
4218664281bSPaulo Zanoni 	if (enable) {
4221dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
4231dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
4241dd246fbSDaniel Vetter 
4258664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
4268664281bSPaulo Zanoni 			return;
4278664281bSPaulo Zanoni 
428fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
4298664281bSPaulo Zanoni 	} else {
4301dd246fbSDaniel Vetter 		uint32_t tmp = I915_READ(SERR_INT);
4311dd246fbSDaniel Vetter 		bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
4321dd246fbSDaniel Vetter 
4331dd246fbSDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
434fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
4351dd246fbSDaniel Vetter 
4361dd246fbSDaniel Vetter 		if (!was_enabled &&
4371dd246fbSDaniel Vetter 		    (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
4381dd246fbSDaniel Vetter 			DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
4391dd246fbSDaniel Vetter 				      transcoder_name(pch_transcoder));
4401dd246fbSDaniel Vetter 		}
4418664281bSPaulo Zanoni 	}
4428664281bSPaulo Zanoni }
4438664281bSPaulo Zanoni 
4448664281bSPaulo Zanoni /**
4458664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
4468664281bSPaulo Zanoni  * @dev: drm device
4478664281bSPaulo Zanoni  * @pipe: pipe
4488664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
4498664281bSPaulo Zanoni  *
4508664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
4518664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
4528664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
4538664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
4548664281bSPaulo Zanoni  * bit for all the pipes.
4558664281bSPaulo Zanoni  *
4568664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
4578664281bSPaulo Zanoni  */
458f88d42f1SImre Deak bool __intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
4598664281bSPaulo Zanoni 					     enum pipe pipe, bool enable)
4608664281bSPaulo Zanoni {
4618664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4628664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
4638664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4648664281bSPaulo Zanoni 	bool ret;
4658664281bSPaulo Zanoni 
46677961eb9SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
46777961eb9SImre Deak 
4688664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
4698664281bSPaulo Zanoni 
4708664281bSPaulo Zanoni 	if (enable == ret)
4718664281bSPaulo Zanoni 		goto done;
4728664281bSPaulo Zanoni 
4738664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
4748664281bSPaulo Zanoni 
4752d9d2b0bSVille Syrjälä 	if (enable && (INTEL_INFO(dev)->gen < 5 || IS_VALLEYVIEW(dev)))
4762d9d2b0bSVille Syrjälä 		i9xx_clear_fifo_underrun(dev, pipe);
4772d9d2b0bSVille Syrjälä 	else if (IS_GEN5(dev) || IS_GEN6(dev))
4788664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
4798664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
4807336df65SDaniel Vetter 		ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
48138d83c96SDaniel Vetter 	else if (IS_GEN8(dev))
48238d83c96SDaniel Vetter 		broadwell_set_fifo_underrun_reporting(dev, pipe, enable);
4838664281bSPaulo Zanoni 
4848664281bSPaulo Zanoni done:
485f88d42f1SImre Deak 	return ret;
486f88d42f1SImre Deak }
487f88d42f1SImre Deak 
488f88d42f1SImre Deak bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
489f88d42f1SImre Deak 					   enum pipe pipe, bool enable)
490f88d42f1SImre Deak {
491f88d42f1SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
492f88d42f1SImre Deak 	unsigned long flags;
493f88d42f1SImre Deak 	bool ret;
494f88d42f1SImre Deak 
495f88d42f1SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
496f88d42f1SImre Deak 	ret = __intel_set_cpu_fifo_underrun_reporting(dev, pipe, enable);
4978664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
498f88d42f1SImre Deak 
4998664281bSPaulo Zanoni 	return ret;
5008664281bSPaulo Zanoni }
5018664281bSPaulo Zanoni 
50291d181ddSImre Deak static bool __cpu_fifo_underrun_reporting_enabled(struct drm_device *dev,
50391d181ddSImre Deak 						  enum pipe pipe)
50491d181ddSImre Deak {
50591d181ddSImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
50691d181ddSImre Deak 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
50791d181ddSImre Deak 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
50891d181ddSImre Deak 
50991d181ddSImre Deak 	return !intel_crtc->cpu_fifo_underrun_disabled;
51091d181ddSImre Deak }
51191d181ddSImre Deak 
5128664281bSPaulo Zanoni /**
5138664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
5148664281bSPaulo Zanoni  * @dev: drm device
5158664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
5168664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
5178664281bSPaulo Zanoni  *
5188664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
5198664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
5208664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
5218664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
5228664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
5238664281bSPaulo Zanoni  *
5248664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
5258664281bSPaulo Zanoni  */
5268664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
5278664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
5288664281bSPaulo Zanoni 					   bool enable)
5298664281bSPaulo Zanoni {
5308664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
531de28075dSDaniel Vetter 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
532de28075dSDaniel Vetter 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5338664281bSPaulo Zanoni 	unsigned long flags;
5348664281bSPaulo Zanoni 	bool ret;
5358664281bSPaulo Zanoni 
536de28075dSDaniel Vetter 	/*
537de28075dSDaniel Vetter 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
538de28075dSDaniel Vetter 	 * has only one pch transcoder A that all pipes can use. To avoid racy
539de28075dSDaniel Vetter 	 * pch transcoder -> pipe lookups from interrupt code simply store the
540de28075dSDaniel Vetter 	 * underrun statistics in crtc A. Since we never expose this anywhere
541de28075dSDaniel Vetter 	 * nor use it outside of the fifo underrun code here using the "wrong"
542de28075dSDaniel Vetter 	 * crtc on LPT won't cause issues.
543de28075dSDaniel Vetter 	 */
5448664281bSPaulo Zanoni 
5458664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
5468664281bSPaulo Zanoni 
5478664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
5488664281bSPaulo Zanoni 
5498664281bSPaulo Zanoni 	if (enable == ret)
5508664281bSPaulo Zanoni 		goto done;
5518664281bSPaulo Zanoni 
5528664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
5538664281bSPaulo Zanoni 
5548664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
555de28075dSDaniel Vetter 		ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
5568664281bSPaulo Zanoni 	else
5578664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
5588664281bSPaulo Zanoni 
5598664281bSPaulo Zanoni done:
5608664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
5618664281bSPaulo Zanoni 	return ret;
5628664281bSPaulo Zanoni }
5638664281bSPaulo Zanoni 
5648664281bSPaulo Zanoni 
565b5ea642aSDaniel Vetter static void
566755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
567755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5687c463586SKeith Packard {
5699db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
570755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5717c463586SKeith Packard 
572b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
573b79480baSDaniel Vetter 
57404feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
57504feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
57604feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
57704feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
578755e9019SImre Deak 		return;
579755e9019SImre Deak 
580755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
58146c06a30SVille Syrjälä 		return;
58246c06a30SVille Syrjälä 
58391d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
58491d181ddSImre Deak 
5857c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
586755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
58746c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5883143a2bfSChris Wilson 	POSTING_READ(reg);
5897c463586SKeith Packard }
5907c463586SKeith Packard 
591b5ea642aSDaniel Vetter static void
592755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
593755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5947c463586SKeith Packard {
5959db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
596755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5977c463586SKeith Packard 
598b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
599b79480baSDaniel Vetter 
60004feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
60104feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
60204feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
60304feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
60446c06a30SVille Syrjälä 		return;
60546c06a30SVille Syrjälä 
606755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
607755e9019SImre Deak 		return;
608755e9019SImre Deak 
60991d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
61091d181ddSImre Deak 
611755e9019SImre Deak 	pipestat &= ~enable_mask;
61246c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
6133143a2bfSChris Wilson 	POSTING_READ(reg);
6147c463586SKeith Packard }
6157c463586SKeith Packard 
61610c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
61710c59c51SImre Deak {
61810c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
61910c59c51SImre Deak 
62010c59c51SImre Deak 	/*
62110c59c51SImre Deak 	 * On pipe A we don't support the PSR interrupt yet, on pipe B the
62210c59c51SImre Deak 	 * same bit MBZ.
62310c59c51SImre Deak 	 */
62410c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
62510c59c51SImre Deak 		return 0;
62610c59c51SImre Deak 
62710c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
62810c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
62910c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
63010c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
63110c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
63210c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
63310c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
63410c59c51SImre Deak 
63510c59c51SImre Deak 	return enable_mask;
63610c59c51SImre Deak }
63710c59c51SImre Deak 
638755e9019SImre Deak void
639755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
640755e9019SImre Deak 		     u32 status_mask)
641755e9019SImre Deak {
642755e9019SImre Deak 	u32 enable_mask;
643755e9019SImre Deak 
64410c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
64510c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
64610c59c51SImre Deak 							   status_mask);
64710c59c51SImre Deak 	else
648755e9019SImre Deak 		enable_mask = status_mask << 16;
649755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
650755e9019SImre Deak }
651755e9019SImre Deak 
652755e9019SImre Deak void
653755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
654755e9019SImre Deak 		      u32 status_mask)
655755e9019SImre Deak {
656755e9019SImre Deak 	u32 enable_mask;
657755e9019SImre Deak 
65810c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
65910c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
66010c59c51SImre Deak 							   status_mask);
66110c59c51SImre Deak 	else
662755e9019SImre Deak 		enable_mask = status_mask << 16;
663755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
664755e9019SImre Deak }
665755e9019SImre Deak 
666c0e09200SDave Airlie /**
667f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
66801c66889SZhao Yakui  */
669f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
67001c66889SZhao Yakui {
6712d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6721ec14ad3SChris Wilson 	unsigned long irqflags;
6731ec14ad3SChris Wilson 
674f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
675f49e38ddSJani Nikula 		return;
676f49e38ddSJani Nikula 
6771ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
67801c66889SZhao Yakui 
679755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
680a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
6813b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
682755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6831ec14ad3SChris Wilson 
6841ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
68501c66889SZhao Yakui }
68601c66889SZhao Yakui 
68701c66889SZhao Yakui /**
6880a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
6890a3e67a4SJesse Barnes  * @dev: DRM device
6900a3e67a4SJesse Barnes  * @pipe: pipe to check
6910a3e67a4SJesse Barnes  *
6920a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
6930a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
6940a3e67a4SJesse Barnes  * before reading such registers if unsure.
6950a3e67a4SJesse Barnes  */
6960a3e67a4SJesse Barnes static int
6970a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
6980a3e67a4SJesse Barnes {
6992d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
700702e7a56SPaulo Zanoni 
701a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
702a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
703a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
704a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
70571f8ba6bSPaulo Zanoni 
706a01025afSDaniel Vetter 		return intel_crtc->active;
707a01025afSDaniel Vetter 	} else {
708a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
709a01025afSDaniel Vetter 	}
7100a3e67a4SJesse Barnes }
7110a3e67a4SJesse Barnes 
7124cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
7134cdb83ecSVille Syrjälä {
7144cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
7154cdb83ecSVille Syrjälä 	return 0;
7164cdb83ecSVille Syrjälä }
7174cdb83ecSVille Syrjälä 
71842f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
71942f52ef8SKeith Packard  * we use as a pipe index
72042f52ef8SKeith Packard  */
721f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
7220a3e67a4SJesse Barnes {
7232d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
7240a3e67a4SJesse Barnes 	unsigned long high_frame;
7250a3e67a4SJesse Barnes 	unsigned long low_frame;
726391f75e2SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start;
7270a3e67a4SJesse Barnes 
7280a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
72944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
7309db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
7310a3e67a4SJesse Barnes 		return 0;
7320a3e67a4SJesse Barnes 	}
7330a3e67a4SJesse Barnes 
734391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
735391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
736391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
737391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
738391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
739391f75e2SVille Syrjälä 
740391f75e2SVille Syrjälä 		vbl_start = mode->crtc_vblank_start * mode->crtc_htotal;
741391f75e2SVille Syrjälä 	} else {
742a2d213ddSDaniel Vetter 		enum transcoder cpu_transcoder = (enum transcoder) pipe;
743391f75e2SVille Syrjälä 		u32 htotal;
744391f75e2SVille Syrjälä 
745391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
746391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
747391f75e2SVille Syrjälä 
748391f75e2SVille Syrjälä 		vbl_start *= htotal;
749391f75e2SVille Syrjälä 	}
750391f75e2SVille Syrjälä 
7519db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7529db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7535eddb70bSChris Wilson 
7540a3e67a4SJesse Barnes 	/*
7550a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7560a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7570a3e67a4SJesse Barnes 	 * register.
7580a3e67a4SJesse Barnes 	 */
7590a3e67a4SJesse Barnes 	do {
7605eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
761391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
7625eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
7630a3e67a4SJesse Barnes 	} while (high1 != high2);
7640a3e67a4SJesse Barnes 
7655eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
766391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7675eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
768391f75e2SVille Syrjälä 
769391f75e2SVille Syrjälä 	/*
770391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
771391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
772391f75e2SVille Syrjälä 	 * counter against vblank start.
773391f75e2SVille Syrjälä 	 */
774edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7750a3e67a4SJesse Barnes }
7760a3e67a4SJesse Barnes 
777f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
7789880b7a5SJesse Barnes {
7792d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
7809db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
7819880b7a5SJesse Barnes 
7829880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
78344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
7849db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7859880b7a5SJesse Barnes 		return 0;
7869880b7a5SJesse Barnes 	}
7879880b7a5SJesse Barnes 
7889880b7a5SJesse Barnes 	return I915_READ(reg);
7899880b7a5SJesse Barnes }
7909880b7a5SJesse Barnes 
791ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
792ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
793ad3543edSMario Kleiner 
794a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
795a225f079SVille Syrjälä {
796a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
797a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
798a225f079SVille Syrjälä 	const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
799a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
800a225f079SVille Syrjälä 	int vtotal = mode->crtc_vtotal;
801a225f079SVille Syrjälä 	int position;
802a225f079SVille Syrjälä 
803a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
804a225f079SVille Syrjälä 		vtotal /= 2;
805a225f079SVille Syrjälä 
806a225f079SVille Syrjälä 	if (IS_GEN2(dev))
807a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
808a225f079SVille Syrjälä 	else
809a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
810a225f079SVille Syrjälä 
811a225f079SVille Syrjälä 	/*
812a225f079SVille Syrjälä 	 * Scanline counter increments at leading edge of hsync, and
813a225f079SVille Syrjälä 	 * it starts counting from vtotal-1 on the first active line.
814a225f079SVille Syrjälä 	 * That means the scanline counter value is always one less
815a225f079SVille Syrjälä 	 * than what we would expect. Ie. just after start of vblank,
816a225f079SVille Syrjälä 	 * which also occurs at start of hsync (on the last active line),
817a225f079SVille Syrjälä 	 * the scanline counter will read vblank_start-1.
818a225f079SVille Syrjälä 	 */
819a225f079SVille Syrjälä 	return (position + 1) % vtotal;
820a225f079SVille Syrjälä }
821a225f079SVille Syrjälä 
822f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
823abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
824abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
8250af7e4dfSMario Kleiner {
826c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
827c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
828c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
829c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
8303aa18df8SVille Syrjälä 	int position;
83178e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
8320af7e4dfSMario Kleiner 	bool in_vbl = true;
8330af7e4dfSMario Kleiner 	int ret = 0;
834ad3543edSMario Kleiner 	unsigned long irqflags;
8350af7e4dfSMario Kleiner 
836c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
8370af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
8389db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
8390af7e4dfSMario Kleiner 		return 0;
8400af7e4dfSMario Kleiner 	}
8410af7e4dfSMario Kleiner 
842c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
84378e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
844c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
845c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
846c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8470af7e4dfSMario Kleiner 
848d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
849d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
850d31faf65SVille Syrjälä 		vbl_end /= 2;
851d31faf65SVille Syrjälä 		vtotal /= 2;
852d31faf65SVille Syrjälä 	}
853d31faf65SVille Syrjälä 
854c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
855c2baf4b7SVille Syrjälä 
856ad3543edSMario Kleiner 	/*
857ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
858ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
859ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
860ad3543edSMario Kleiner 	 */
861ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
862ad3543edSMario Kleiner 
863ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
864ad3543edSMario Kleiner 
865ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
866ad3543edSMario Kleiner 	if (stime)
867ad3543edSMario Kleiner 		*stime = ktime_get();
868ad3543edSMario Kleiner 
8697c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8700af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8710af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8720af7e4dfSMario Kleiner 		 */
873a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8740af7e4dfSMario Kleiner 	} else {
8750af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8760af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8770af7e4dfSMario Kleiner 		 * scanout position.
8780af7e4dfSMario Kleiner 		 */
879ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8800af7e4dfSMario Kleiner 
8813aa18df8SVille Syrjälä 		/* convert to pixel counts */
8823aa18df8SVille Syrjälä 		vbl_start *= htotal;
8833aa18df8SVille Syrjälä 		vbl_end *= htotal;
8843aa18df8SVille Syrjälä 		vtotal *= htotal;
88578e8fc6bSVille Syrjälä 
88678e8fc6bSVille Syrjälä 		/*
88778e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
88878e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
88978e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
89078e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
89178e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
89278e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
89378e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
89478e8fc6bSVille Syrjälä 		 */
89578e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
8963aa18df8SVille Syrjälä 	}
8973aa18df8SVille Syrjälä 
898ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
899ad3543edSMario Kleiner 	if (etime)
900ad3543edSMario Kleiner 		*etime = ktime_get();
901ad3543edSMario Kleiner 
902ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
903ad3543edSMario Kleiner 
904ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
905ad3543edSMario Kleiner 
9063aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
9073aa18df8SVille Syrjälä 
9083aa18df8SVille Syrjälä 	/*
9093aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
9103aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
9113aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
9123aa18df8SVille Syrjälä 	 * up since vbl_end.
9133aa18df8SVille Syrjälä 	 */
9143aa18df8SVille Syrjälä 	if (position >= vbl_start)
9153aa18df8SVille Syrjälä 		position -= vbl_end;
9163aa18df8SVille Syrjälä 	else
9173aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
9183aa18df8SVille Syrjälä 
9197c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
9203aa18df8SVille Syrjälä 		*vpos = position;
9213aa18df8SVille Syrjälä 		*hpos = 0;
9223aa18df8SVille Syrjälä 	} else {
9230af7e4dfSMario Kleiner 		*vpos = position / htotal;
9240af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
9250af7e4dfSMario Kleiner 	}
9260af7e4dfSMario Kleiner 
9270af7e4dfSMario Kleiner 	/* In vblank? */
9280af7e4dfSMario Kleiner 	if (in_vbl)
9290af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
9300af7e4dfSMario Kleiner 
9310af7e4dfSMario Kleiner 	return ret;
9320af7e4dfSMario Kleiner }
9330af7e4dfSMario Kleiner 
934a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
935a225f079SVille Syrjälä {
936a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
937a225f079SVille Syrjälä 	unsigned long irqflags;
938a225f079SVille Syrjälä 	int position;
939a225f079SVille Syrjälä 
940a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
941a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
942a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
943a225f079SVille Syrjälä 
944a225f079SVille Syrjälä 	return position;
945a225f079SVille Syrjälä }
946a225f079SVille Syrjälä 
947f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
9480af7e4dfSMario Kleiner 			      int *max_error,
9490af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9500af7e4dfSMario Kleiner 			      unsigned flags)
9510af7e4dfSMario Kleiner {
9524041b853SChris Wilson 	struct drm_crtc *crtc;
9530af7e4dfSMario Kleiner 
9547eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
9554041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9560af7e4dfSMario Kleiner 		return -EINVAL;
9570af7e4dfSMario Kleiner 	}
9580af7e4dfSMario Kleiner 
9590af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9604041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9614041b853SChris Wilson 	if (crtc == NULL) {
9624041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9634041b853SChris Wilson 		return -EINVAL;
9644041b853SChris Wilson 	}
9654041b853SChris Wilson 
9664041b853SChris Wilson 	if (!crtc->enabled) {
9674041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
9684041b853SChris Wilson 		return -EBUSY;
9694041b853SChris Wilson 	}
9700af7e4dfSMario Kleiner 
9710af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9724041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9734041b853SChris Wilson 						     vblank_time, flags,
9747da903efSVille Syrjälä 						     crtc,
9757da903efSVille Syrjälä 						     &to_intel_crtc(crtc)->config.adjusted_mode);
9760af7e4dfSMario Kleiner }
9770af7e4dfSMario Kleiner 
97867c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
97967c347ffSJani Nikula 				struct drm_connector *connector)
980321a1b30SEgbert Eich {
981321a1b30SEgbert Eich 	enum drm_connector_status old_status;
982321a1b30SEgbert Eich 
983321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
984321a1b30SEgbert Eich 	old_status = connector->status;
985321a1b30SEgbert Eich 
986321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
98767c347ffSJani Nikula 	if (old_status == connector->status)
98867c347ffSJani Nikula 		return false;
98967c347ffSJani Nikula 
99067c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
991321a1b30SEgbert Eich 		      connector->base.id,
992321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
99367c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
99467c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
99567c347ffSJani Nikula 
99667c347ffSJani Nikula 	return true;
997321a1b30SEgbert Eich }
998321a1b30SEgbert Eich 
9995ca58282SJesse Barnes /*
10005ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
10015ca58282SJesse Barnes  */
1002ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
1003ac4c16c5SEgbert Eich 
10045ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
10055ca58282SJesse Barnes {
10062d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10072d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, hotplug_work);
10085ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1009c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
1010cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
1011cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
1012cd569aedSEgbert Eich 	struct drm_connector *connector;
1013cd569aedSEgbert Eich 	unsigned long irqflags;
1014cd569aedSEgbert Eich 	bool hpd_disabled = false;
1015321a1b30SEgbert Eich 	bool changed = false;
1016142e2398SEgbert Eich 	u32 hpd_event_bits;
10175ca58282SJesse Barnes 
101852d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
101952d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
102052d7ecedSDaniel Vetter 		return;
102152d7ecedSDaniel Vetter 
1022a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
1023e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
1024e67189abSJesse Barnes 
1025cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1026142e2398SEgbert Eich 
1027142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
1028142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
1029cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
1030cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
1031cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
1032cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
1033cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
1034cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
1035cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
1036cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
1037cd569aedSEgbert Eich 				drm_get_connector_name(connector));
1038cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
1039cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
1040cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
1041cd569aedSEgbert Eich 			hpd_disabled = true;
1042cd569aedSEgbert Eich 		}
1043142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1044142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
1045142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
1046142e2398SEgbert Eich 		}
1047cd569aedSEgbert Eich 	}
1048cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
1049cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
1050cd569aedSEgbert Eich 	  * some connectors */
1051ac4c16c5SEgbert Eich 	if (hpd_disabled) {
1052cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
1053ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
1054ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
1055ac4c16c5SEgbert Eich 	}
1056cd569aedSEgbert Eich 
1057cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1058cd569aedSEgbert Eich 
1059321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
1060321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
1061321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
1062321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1063cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
1064cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
1065321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
1066321a1b30SEgbert Eich 				changed = true;
1067321a1b30SEgbert Eich 		}
1068321a1b30SEgbert Eich 	}
106940ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
107040ee3381SKeith Packard 
1071321a1b30SEgbert Eich 	if (changed)
1072321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
10735ca58282SJesse Barnes }
10745ca58282SJesse Barnes 
10753ca1ccedSVille Syrjälä static void intel_hpd_irq_uninstall(struct drm_i915_private *dev_priv)
10763ca1ccedSVille Syrjälä {
10773ca1ccedSVille Syrjälä 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
10783ca1ccedSVille Syrjälä }
10793ca1ccedSVille Syrjälä 
1080d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
1081f97108d1SJesse Barnes {
10822d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1083b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
10849270388eSDaniel Vetter 	u8 new_delay;
10859270388eSDaniel Vetter 
1086d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1087f97108d1SJesse Barnes 
108873edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
108973edd18fSDaniel Vetter 
109020e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
10919270388eSDaniel Vetter 
10927648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1093b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1094b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1095f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1096f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1097f97108d1SJesse Barnes 
1098f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1099b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
110020e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
110120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
110220e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
110320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1104b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
110520e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
110620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
110720e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
110820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1109f97108d1SJesse Barnes 	}
1110f97108d1SJesse Barnes 
11117648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
111220e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1113f97108d1SJesse Barnes 
1114d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
11159270388eSDaniel Vetter 
1116f97108d1SJesse Barnes 	return;
1117f97108d1SJesse Barnes }
1118f97108d1SJesse Barnes 
1119549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
1120549f7365SChris Wilson 			struct intel_ring_buffer *ring)
1121549f7365SChris Wilson {
1122475553deSChris Wilson 	if (ring->obj == NULL)
1123475553deSChris Wilson 		return;
1124475553deSChris Wilson 
1125814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
11269862e600SChris Wilson 
1127549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
112810cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
1129549f7365SChris Wilson }
1130549f7365SChris Wilson 
11314912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11323b8d8d91SJesse Barnes {
11332d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11342d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
1135edbfdb45SPaulo Zanoni 	u32 pm_iir;
1136dd75fdc8SChris Wilson 	int new_delay, adj;
11373b8d8d91SJesse Barnes 
113859cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1139c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1140c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1141*0961021aSBen Widawsky 	if (IS_BROADWELL(dev_priv->dev))
1142*0961021aSBen Widawsky 		bdw_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
1143*0961021aSBen Widawsky 	else {
1144*0961021aSBen Widawsky 		/* Make sure not to corrupt PMIMR state used by ringbuffer */
1145a6706b45SDeepak S 		snb_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
1146*0961021aSBen Widawsky 	}
114759cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11484912d041SBen Widawsky 
114960611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1150a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
115160611c13SPaulo Zanoni 
1152a6706b45SDeepak S 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
11533b8d8d91SJesse Barnes 		return;
11543b8d8d91SJesse Barnes 
11554fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11567b9e0ae6SChris Wilson 
1157dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
11587425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1159dd75fdc8SChris Wilson 		if (adj > 0)
1160dd75fdc8SChris Wilson 			adj *= 2;
1161dd75fdc8SChris Wilson 		else
1162dd75fdc8SChris Wilson 			adj = 1;
1163b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
11647425034aSVille Syrjälä 
11657425034aSVille Syrjälä 		/*
11667425034aSVille Syrjälä 		 * For better performance, jump directly
11677425034aSVille Syrjälä 		 * to RPe if we're below it.
11687425034aSVille Syrjälä 		 */
1169b39fb297SBen Widawsky 		if (new_delay < dev_priv->rps.efficient_freq)
1170b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1171dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1172b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1173b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1174dd75fdc8SChris Wilson 		else
1175b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1176dd75fdc8SChris Wilson 		adj = 0;
1177dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1178dd75fdc8SChris Wilson 		if (adj < 0)
1179dd75fdc8SChris Wilson 			adj *= 2;
1180dd75fdc8SChris Wilson 		else
1181dd75fdc8SChris Wilson 			adj = -1;
1182b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
1183dd75fdc8SChris Wilson 	} else { /* unknown event */
1184b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq;
1185dd75fdc8SChris Wilson 	}
11863b8d8d91SJesse Barnes 
118779249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
118879249636SBen Widawsky 	 * interrupt
118979249636SBen Widawsky 	 */
11901272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
1191b39fb297SBen Widawsky 			    dev_priv->rps.min_freq_softlimit,
1192b39fb297SBen Widawsky 			    dev_priv->rps.max_freq_softlimit);
119327544369SDeepak S 
1194b39fb297SBen Widawsky 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
1195dd75fdc8SChris Wilson 
11960a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
11970a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
11980a073b84SJesse Barnes 	else
11994912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
12003b8d8d91SJesse Barnes 
12014fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
12023b8d8d91SJesse Barnes }
12033b8d8d91SJesse Barnes 
1204e3689190SBen Widawsky 
1205e3689190SBen Widawsky /**
1206e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1207e3689190SBen Widawsky  * occurred.
1208e3689190SBen Widawsky  * @work: workqueue struct
1209e3689190SBen Widawsky  *
1210e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1211e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1212e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1213e3689190SBen Widawsky  */
1214e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1215e3689190SBen Widawsky {
12162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
12172d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1218e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
121935a85ac6SBen Widawsky 	char *parity_event[6];
1220e3689190SBen Widawsky 	uint32_t misccpctl;
1221e3689190SBen Widawsky 	unsigned long flags;
122235a85ac6SBen Widawsky 	uint8_t slice = 0;
1223e3689190SBen Widawsky 
1224e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1225e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1226e3689190SBen Widawsky 	 * any time we access those registers.
1227e3689190SBen Widawsky 	 */
1228e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1229e3689190SBen Widawsky 
123035a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
123135a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
123235a85ac6SBen Widawsky 		goto out;
123335a85ac6SBen Widawsky 
1234e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1235e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1236e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1237e3689190SBen Widawsky 
123835a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
123935a85ac6SBen Widawsky 		u32 reg;
124035a85ac6SBen Widawsky 
124135a85ac6SBen Widawsky 		slice--;
124235a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
124335a85ac6SBen Widawsky 			break;
124435a85ac6SBen Widawsky 
124535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
124635a85ac6SBen Widawsky 
124735a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
124835a85ac6SBen Widawsky 
124935a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1250e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1251e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1252e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1253e3689190SBen Widawsky 
125435a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
125535a85ac6SBen Widawsky 		POSTING_READ(reg);
1256e3689190SBen Widawsky 
1257cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1258e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1259e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1260e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
126135a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
126235a85ac6SBen Widawsky 		parity_event[5] = NULL;
1263e3689190SBen Widawsky 
12645bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1265e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1266e3689190SBen Widawsky 
126735a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
126835a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1269e3689190SBen Widawsky 
127035a85ac6SBen Widawsky 		kfree(parity_event[4]);
1271e3689190SBen Widawsky 		kfree(parity_event[3]);
1272e3689190SBen Widawsky 		kfree(parity_event[2]);
1273e3689190SBen Widawsky 		kfree(parity_event[1]);
1274e3689190SBen Widawsky 	}
1275e3689190SBen Widawsky 
127635a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
127735a85ac6SBen Widawsky 
127835a85ac6SBen Widawsky out:
127935a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
128035a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
128135a85ac6SBen Widawsky 	ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
128235a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
128335a85ac6SBen Widawsky 
128435a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
128535a85ac6SBen Widawsky }
128635a85ac6SBen Widawsky 
128735a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1288e3689190SBen Widawsky {
12892d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1290e3689190SBen Widawsky 
1291040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1292e3689190SBen Widawsky 		return;
1293e3689190SBen Widawsky 
1294d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
129535a85ac6SBen Widawsky 	ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1296d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1297e3689190SBen Widawsky 
129835a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
129935a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
130035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
130135a85ac6SBen Widawsky 
130235a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
130335a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
130435a85ac6SBen Widawsky 
1305a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1306e3689190SBen Widawsky }
1307e3689190SBen Widawsky 
1308f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1309f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1310f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1311f1af8fc1SPaulo Zanoni {
1312f1af8fc1SPaulo Zanoni 	if (gt_iir &
1313f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1314f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1315f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1316f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1317f1af8fc1SPaulo Zanoni }
1318f1af8fc1SPaulo Zanoni 
1319e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1320e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1321e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1322e7b4c6b1SDaniel Vetter {
1323e7b4c6b1SDaniel Vetter 
1324cc609d5dSBen Widawsky 	if (gt_iir &
1325cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1326e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1327cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1328e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1329cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1330e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1331e7b4c6b1SDaniel Vetter 
1332cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1333cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1334cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
133558174462SMika Kuoppala 		i915_handle_error(dev, false, "GT error interrupt 0x%08x",
133658174462SMika Kuoppala 				  gt_iir);
1337e7b4c6b1SDaniel Vetter 	}
1338e3689190SBen Widawsky 
133935a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
134035a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1341e7b4c6b1SDaniel Vetter }
1342e7b4c6b1SDaniel Vetter 
1343*0961021aSBen Widawsky static void gen8_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1344*0961021aSBen Widawsky {
1345*0961021aSBen Widawsky 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
1346*0961021aSBen Widawsky 		return;
1347*0961021aSBen Widawsky 
1348*0961021aSBen Widawsky 	spin_lock(&dev_priv->irq_lock);
1349*0961021aSBen Widawsky 	dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1350*0961021aSBen Widawsky 	bdw_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1351*0961021aSBen Widawsky 	spin_unlock(&dev_priv->irq_lock);
1352*0961021aSBen Widawsky 
1353*0961021aSBen Widawsky 	queue_work(dev_priv->wq, &dev_priv->rps.work);
1354*0961021aSBen Widawsky }
1355*0961021aSBen Widawsky 
1356abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1357abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1358abd58f01SBen Widawsky 				       u32 master_ctl)
1359abd58f01SBen Widawsky {
1360abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1361abd58f01SBen Widawsky 	uint32_t tmp = 0;
1362abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1363abd58f01SBen Widawsky 
1364abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1365abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1366abd58f01SBen Widawsky 		if (tmp) {
1367abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1368abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1369abd58f01SBen Widawsky 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1370abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1371abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[RCS]);
1372abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1373abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[BCS]);
1374abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1375abd58f01SBen Widawsky 		} else
1376abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1377abd58f01SBen Widawsky 	}
1378abd58f01SBen Widawsky 
137985f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1380abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1381abd58f01SBen Widawsky 		if (tmp) {
1382abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1383abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1384abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1385abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VCS]);
138685f9b5f9SZhao Yakui 			vcs = tmp >> GEN8_VCS2_IRQ_SHIFT;
138785f9b5f9SZhao Yakui 			if (vcs & GT_RENDER_USER_INTERRUPT)
138885f9b5f9SZhao Yakui 				notify_ring(dev, &dev_priv->ring[VCS2]);
1389abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1390abd58f01SBen Widawsky 		} else
1391abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1392abd58f01SBen Widawsky 	}
1393abd58f01SBen Widawsky 
1394*0961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
1395*0961021aSBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(2));
1396*0961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
1397*0961021aSBen Widawsky 			ret = IRQ_HANDLED;
1398*0961021aSBen Widawsky 			gen8_rps_irq_handler(dev_priv, tmp);
1399*0961021aSBen Widawsky 			I915_WRITE(GEN8_GT_IIR(2),
1400*0961021aSBen Widawsky 				   tmp & dev_priv->pm_rps_events);
1401*0961021aSBen Widawsky 		} else
1402*0961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
1403*0961021aSBen Widawsky 	}
1404*0961021aSBen Widawsky 
1405abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1406abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1407abd58f01SBen Widawsky 		if (tmp) {
1408abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1409abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1410abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1411abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VECS]);
1412abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1413abd58f01SBen Widawsky 		} else
1414abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1415abd58f01SBen Widawsky 	}
1416abd58f01SBen Widawsky 
1417abd58f01SBen Widawsky 	return ret;
1418abd58f01SBen Widawsky }
1419abd58f01SBen Widawsky 
1420b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1421b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1422b543fb04SEgbert Eich 
142310a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1424b543fb04SEgbert Eich 					 u32 hotplug_trigger,
1425b543fb04SEgbert Eich 					 const u32 *hpd)
1426b543fb04SEgbert Eich {
14272d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1428b543fb04SEgbert Eich 	int i;
142910a504deSDaniel Vetter 	bool storm_detected = false;
1430b543fb04SEgbert Eich 
143191d131d2SDaniel Vetter 	if (!hotplug_trigger)
143291d131d2SDaniel Vetter 		return;
143391d131d2SDaniel Vetter 
1434cc9bd499SImre Deak 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
1435cc9bd499SImre Deak 			  hotplug_trigger);
1436cc9bd499SImre Deak 
1437b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1438b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
1439821450c6SEgbert Eich 
14403ff04a16SDaniel Vetter 		if (hpd[i] & hotplug_trigger &&
14413ff04a16SDaniel Vetter 		    dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
14423ff04a16SDaniel Vetter 			/*
14433ff04a16SDaniel Vetter 			 * On GMCH platforms the interrupt mask bits only
14443ff04a16SDaniel Vetter 			 * prevent irq generation, not the setting of the
14453ff04a16SDaniel Vetter 			 * hotplug bits itself. So only WARN about unexpected
14463ff04a16SDaniel Vetter 			 * interrupts on saner platforms.
14473ff04a16SDaniel Vetter 			 */
14483ff04a16SDaniel Vetter 			WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
1449cba1c073SChris Wilson 				  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1450cba1c073SChris Wilson 				  hotplug_trigger, i, hpd[i]);
1451b8f102e8SEgbert Eich 
14523ff04a16SDaniel Vetter 			continue;
14533ff04a16SDaniel Vetter 		}
14543ff04a16SDaniel Vetter 
1455b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1456b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1457b543fb04SEgbert Eich 			continue;
1458b543fb04SEgbert Eich 
1459bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1460b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1461b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1462b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1463b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1464b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1465b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1466b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1467b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1468142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1469b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
147010a504deSDaniel Vetter 			storm_detected = true;
1471b543fb04SEgbert Eich 		} else {
1472b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1473b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1474b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1475b543fb04SEgbert Eich 		}
1476b543fb04SEgbert Eich 	}
1477b543fb04SEgbert Eich 
147810a504deSDaniel Vetter 	if (storm_detected)
147910a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1480b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
14815876fa0dSDaniel Vetter 
1482645416f5SDaniel Vetter 	/*
1483645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1484645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1485645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1486645416f5SDaniel Vetter 	 * deadlock.
1487645416f5SDaniel Vetter 	 */
1488645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1489b543fb04SEgbert Eich }
1490b543fb04SEgbert Eich 
1491515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1492515ac2bbSDaniel Vetter {
14932d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
149428c70f16SDaniel Vetter 
149528c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1496515ac2bbSDaniel Vetter }
1497515ac2bbSDaniel Vetter 
1498ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1499ce99c256SDaniel Vetter {
15002d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
15019ee32feaSDaniel Vetter 
15029ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1503ce99c256SDaniel Vetter }
1504ce99c256SDaniel Vetter 
15058bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1506277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1507eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1508eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15098bc5e955SDaniel Vetter 					 uint32_t crc4)
15108bf1e9f1SShuang He {
15118bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
15128bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15138bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1514ac2300d4SDamien Lespiau 	int head, tail;
1515b2c88f5bSDamien Lespiau 
1516d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1517d538bbdfSDamien Lespiau 
15180c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1519d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
15200c912c79SDamien Lespiau 		DRM_ERROR("spurious interrupt\n");
15210c912c79SDamien Lespiau 		return;
15220c912c79SDamien Lespiau 	}
15230c912c79SDamien Lespiau 
1524d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1525d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1526b2c88f5bSDamien Lespiau 
1527b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1528d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1529b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1530b2c88f5bSDamien Lespiau 		return;
1531b2c88f5bSDamien Lespiau 	}
1532b2c88f5bSDamien Lespiau 
1533b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15348bf1e9f1SShuang He 
15358bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1536eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1537eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1538eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1539eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1540eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1541b2c88f5bSDamien Lespiau 
1542b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1543d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1544d538bbdfSDamien Lespiau 
1545d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
154607144428SDamien Lespiau 
154707144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15488bf1e9f1SShuang He }
1549277de95eSDaniel Vetter #else
1550277de95eSDaniel Vetter static inline void
1551277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1552277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1553277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1554277de95eSDaniel Vetter 			     uint32_t crc4) {}
1555277de95eSDaniel Vetter #endif
1556eba94eb9SDaniel Vetter 
1557277de95eSDaniel Vetter 
1558277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15595a69b89fSDaniel Vetter {
15605a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15615a69b89fSDaniel Vetter 
1562277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15635a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15645a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15655a69b89fSDaniel Vetter }
15665a69b89fSDaniel Vetter 
1567277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1568eba94eb9SDaniel Vetter {
1569eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1570eba94eb9SDaniel Vetter 
1571277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1572eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1573eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1574eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1575eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15768bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1577eba94eb9SDaniel Vetter }
15785b3a856bSDaniel Vetter 
1579277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15805b3a856bSDaniel Vetter {
15815b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15820b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15830b5c5ed0SDaniel Vetter 
15840b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
15850b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15860b5c5ed0SDaniel Vetter 	else
15870b5c5ed0SDaniel Vetter 		res1 = 0;
15880b5c5ed0SDaniel Vetter 
15890b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
15900b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
15910b5c5ed0SDaniel Vetter 	else
15920b5c5ed0SDaniel Vetter 		res2 = 0;
15935b3a856bSDaniel Vetter 
1594277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15950b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
15960b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
15970b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
15980b5c5ed0SDaniel Vetter 				     res1, res2);
15995b3a856bSDaniel Vetter }
16008bf1e9f1SShuang He 
16011403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16021403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16031403c0d4SPaulo Zanoni  * the work queue. */
16041403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1605baf02a1fSBen Widawsky {
1606a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
160759cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1608a6706b45SDeepak S 		dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1609a6706b45SDeepak S 		snb_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
161059cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
16112adbee62SDaniel Vetter 
16122adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
161341a05a3aSDaniel Vetter 	}
1614baf02a1fSBen Widawsky 
16151403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
161612638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
161712638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
161812638c57SBen Widawsky 
161912638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
162058174462SMika Kuoppala 			i915_handle_error(dev_priv->dev, false,
162158174462SMika Kuoppala 					  "VEBOX CS error interrupt 0x%08x",
162258174462SMika Kuoppala 					  pm_iir);
162312638c57SBen Widawsky 		}
162412638c57SBen Widawsky 	}
16251403c0d4SPaulo Zanoni }
1626baf02a1fSBen Widawsky 
16278d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
16288d7849dbSVille Syrjälä {
16298d7849dbSVille Syrjälä 	struct intel_crtc *crtc;
16308d7849dbSVille Syrjälä 
16318d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
16328d7849dbSVille Syrjälä 		return false;
16338d7849dbSVille Syrjälä 
16348d7849dbSVille Syrjälä 	crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
16358d7849dbSVille Syrjälä 	wake_up(&crtc->vbl_wait);
16368d7849dbSVille Syrjälä 
16378d7849dbSVille Syrjälä 	return true;
16388d7849dbSVille Syrjälä }
16398d7849dbSVille Syrjälä 
1640c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
16417e231dbeSJesse Barnes {
1642c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
164391d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
16447e231dbeSJesse Barnes 	int pipe;
16457e231dbeSJesse Barnes 
164658ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
16477e231dbeSJesse Barnes 	for_each_pipe(pipe) {
164891d181ddSImre Deak 		int reg;
1649bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
165091d181ddSImre Deak 
1651bbb5eebfSDaniel Vetter 		/*
1652bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1653bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1654bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1655bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1656bbb5eebfSDaniel Vetter 		 * handle.
1657bbb5eebfSDaniel Vetter 		 */
1658bbb5eebfSDaniel Vetter 		mask = 0;
1659bbb5eebfSDaniel Vetter 		if (__cpu_fifo_underrun_reporting_enabled(dev, pipe))
1660bbb5eebfSDaniel Vetter 			mask |= PIPE_FIFO_UNDERRUN_STATUS;
1661bbb5eebfSDaniel Vetter 
1662bbb5eebfSDaniel Vetter 		switch (pipe) {
1663bbb5eebfSDaniel Vetter 		case PIPE_A:
1664bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1665bbb5eebfSDaniel Vetter 			break;
1666bbb5eebfSDaniel Vetter 		case PIPE_B:
1667bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1668bbb5eebfSDaniel Vetter 			break;
1669bbb5eebfSDaniel Vetter 		}
1670bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1671bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1672bbb5eebfSDaniel Vetter 
1673bbb5eebfSDaniel Vetter 		if (!mask)
167491d181ddSImre Deak 			continue;
167591d181ddSImre Deak 
167691d181ddSImre Deak 		reg = PIPESTAT(pipe);
1677bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1678bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16797e231dbeSJesse Barnes 
16807e231dbeSJesse Barnes 		/*
16817e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16827e231dbeSJesse Barnes 		 */
168391d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
168491d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
16857e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
16867e231dbeSJesse Barnes 	}
168758ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
16887e231dbeSJesse Barnes 
168931acc7f5SJesse Barnes 	for_each_pipe(pipe) {
16907b5562d4SJesse Barnes 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
16918d7849dbSVille Syrjälä 			intel_pipe_handle_vblank(dev, pipe);
169231acc7f5SJesse Barnes 
1693579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
169431acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
169531acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
169631acc7f5SJesse Barnes 		}
16974356d586SDaniel Vetter 
16984356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1699277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
17002d9d2b0bSVille Syrjälä 
17012d9d2b0bSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
17022d9d2b0bSVille Syrjälä 		    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
1703fc2c807bSVille Syrjälä 			DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
170431acc7f5SJesse Barnes 	}
170531acc7f5SJesse Barnes 
1706c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1707c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1708c1874ed7SImre Deak }
1709c1874ed7SImre Deak 
171016c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
171116c6c56bSVille Syrjälä {
171216c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
171316c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
171416c6c56bSVille Syrjälä 
171516c6c56bSVille Syrjälä 	if (IS_G4X(dev)) {
171616c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
171716c6c56bSVille Syrjälä 
171816c6c56bSVille Syrjälä 		intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_g4x);
171916c6c56bSVille Syrjälä 	} else {
172016c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
172116c6c56bSVille Syrjälä 
172216c6c56bSVille Syrjälä 		intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
172316c6c56bSVille Syrjälä 	}
172416c6c56bSVille Syrjälä 
172516c6c56bSVille Syrjälä 	if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
172616c6c56bSVille Syrjälä 	    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
172716c6c56bSVille Syrjälä 		dp_aux_irq_handler(dev);
172816c6c56bSVille Syrjälä 
172916c6c56bSVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
173016c6c56bSVille Syrjälä 	/*
173116c6c56bSVille Syrjälä 	 * Make sure hotplug status is cleared before we clear IIR, or else we
173216c6c56bSVille Syrjälä 	 * may miss hotplug events.
173316c6c56bSVille Syrjälä 	 */
173416c6c56bSVille Syrjälä 	POSTING_READ(PORT_HOTPLUG_STAT);
173516c6c56bSVille Syrjälä }
173616c6c56bSVille Syrjälä 
1737c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1738c1874ed7SImre Deak {
173945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
17402d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1741c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1742c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1743c1874ed7SImre Deak 
1744c1874ed7SImre Deak 	while (true) {
1745c1874ed7SImre Deak 		iir = I915_READ(VLV_IIR);
1746c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1747c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
1748c1874ed7SImre Deak 
1749c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1750c1874ed7SImre Deak 			goto out;
1751c1874ed7SImre Deak 
1752c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1753c1874ed7SImre Deak 
1754c1874ed7SImre Deak 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1755c1874ed7SImre Deak 
1756c1874ed7SImre Deak 		valleyview_pipestat_irq_handler(dev, iir);
1757c1874ed7SImre Deak 
17587e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
175916c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
176016c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
17617e231dbeSJesse Barnes 
176260611c13SPaulo Zanoni 		if (pm_iir)
1763d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
17647e231dbeSJesse Barnes 
17657e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
17667e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
17677e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
17687e231dbeSJesse Barnes 	}
17697e231dbeSJesse Barnes 
17707e231dbeSJesse Barnes out:
17717e231dbeSJesse Barnes 	return ret;
17727e231dbeSJesse Barnes }
17737e231dbeSJesse Barnes 
177443f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
177543f328d7SVille Syrjälä {
177645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
177743f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
177843f328d7SVille Syrjälä 	u32 master_ctl, iir;
177943f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
178043f328d7SVille Syrjälä 	unsigned int pipes = 0;
178143f328d7SVille Syrjälä 
178243f328d7SVille Syrjälä 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
178343f328d7SVille Syrjälä 
178443f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
178543f328d7SVille Syrjälä 
178643f328d7SVille Syrjälä 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
178743f328d7SVille Syrjälä 
178843f328d7SVille Syrjälä 	iir = I915_READ(VLV_IIR);
178943f328d7SVille Syrjälä 
179043f328d7SVille Syrjälä 	if (iir & (I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT | I915_DISPLAY_PIPE_A_EVENT_INTERRUPT))
179143f328d7SVille Syrjälä 		pipes |= 1 << 0;
179243f328d7SVille Syrjälä 	if (iir & (I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT | I915_DISPLAY_PIPE_B_EVENT_INTERRUPT))
179343f328d7SVille Syrjälä 		pipes |= 1 << 1;
179443f328d7SVille Syrjälä 	if (iir & (I915_DISPLAY_PIPE_C_VBLANK_INTERRUPT | I915_DISPLAY_PIPE_C_EVENT_INTERRUPT))
179543f328d7SVille Syrjälä 		pipes |= 1 << 2;
179643f328d7SVille Syrjälä 
179743f328d7SVille Syrjälä 	if (pipes) {
179843f328d7SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
179943f328d7SVille Syrjälä 		unsigned long irqflags;
180043f328d7SVille Syrjälä 		int pipe;
180143f328d7SVille Syrjälä 
180243f328d7SVille Syrjälä 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
180343f328d7SVille Syrjälä 		for_each_pipe(pipe) {
180443f328d7SVille Syrjälä 			unsigned int reg;
180543f328d7SVille Syrjälä 
180643f328d7SVille Syrjälä 			if (!(pipes & (1 << pipe)))
180743f328d7SVille Syrjälä 				continue;
180843f328d7SVille Syrjälä 
180943f328d7SVille Syrjälä 			reg = PIPESTAT(pipe);
181043f328d7SVille Syrjälä 			pipe_stats[pipe] = I915_READ(reg);
181143f328d7SVille Syrjälä 
181243f328d7SVille Syrjälä 			/*
181343f328d7SVille Syrjälä 			 * Clear the PIPE*STAT regs before the IIR
181443f328d7SVille Syrjälä 			 */
181543f328d7SVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff) {
181643f328d7SVille Syrjälä 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
181743f328d7SVille Syrjälä 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
181843f328d7SVille Syrjälä 							 pipe_name(pipe));
181943f328d7SVille Syrjälä 				I915_WRITE(reg, pipe_stats[pipe]);
182043f328d7SVille Syrjälä 			}
182143f328d7SVille Syrjälä 		}
182243f328d7SVille Syrjälä 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
182343f328d7SVille Syrjälä 
182443f328d7SVille Syrjälä 		for_each_pipe(pipe) {
182543f328d7SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
182643f328d7SVille Syrjälä 				drm_handle_vblank(dev, pipe);
182743f328d7SVille Syrjälä 
182843f328d7SVille Syrjälä 			if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
182943f328d7SVille Syrjälä 				intel_prepare_page_flip(dev, pipe);
183043f328d7SVille Syrjälä 				intel_finish_page_flip(dev, pipe);
183143f328d7SVille Syrjälä 			}
183243f328d7SVille Syrjälä 		}
183343f328d7SVille Syrjälä 
183443f328d7SVille Syrjälä 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
183543f328d7SVille Syrjälä 			gmbus_irq_handler(dev);
183643f328d7SVille Syrjälä 
183743f328d7SVille Syrjälä 		ret = IRQ_HANDLED;
183843f328d7SVille Syrjälä 	}
183943f328d7SVille Syrjälä 
184043f328d7SVille Syrjälä 	/* Consume port.  Then clear IIR or we'll miss events */
184143f328d7SVille Syrjälä 	if (iir & I915_DISPLAY_PORT_INTERRUPT) {
184243f328d7SVille Syrjälä 		u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
184343f328d7SVille Syrjälä 
184443f328d7SVille Syrjälä 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
184543f328d7SVille Syrjälä 
184643f328d7SVille Syrjälä 		DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
184743f328d7SVille Syrjälä 				 hotplug_status);
184843f328d7SVille Syrjälä 		if (hotplug_status & HOTPLUG_INT_STATUS_I915)
184943f328d7SVille Syrjälä 			queue_work(dev_priv->wq,
185043f328d7SVille Syrjälä 				   &dev_priv->hotplug_work);
185143f328d7SVille Syrjälä 
185243f328d7SVille Syrjälä 		ret = IRQ_HANDLED;
185343f328d7SVille Syrjälä 	}
185443f328d7SVille Syrjälä 
185543f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, iir);
185643f328d7SVille Syrjälä 
185743f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
185843f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
185943f328d7SVille Syrjälä 
186043f328d7SVille Syrjälä 	return ret;
186143f328d7SVille Syrjälä }
186243f328d7SVille Syrjälä 
186323e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1864776ad806SJesse Barnes {
18652d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
18669db4a9c7SJesse Barnes 	int pipe;
1867b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1868776ad806SJesse Barnes 
186910a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
187091d131d2SDaniel Vetter 
1871cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1872cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1873776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1874cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1875cfc33bf7SVille Syrjälä 				 port_name(port));
1876cfc33bf7SVille Syrjälä 	}
1877776ad806SJesse Barnes 
1878ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1879ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1880ce99c256SDaniel Vetter 
1881776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1882515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1883776ad806SJesse Barnes 
1884776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1885776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1886776ad806SJesse Barnes 
1887776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1888776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1889776ad806SJesse Barnes 
1890776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1891776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1892776ad806SJesse Barnes 
18939db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
18949db4a9c7SJesse Barnes 		for_each_pipe(pipe)
18959db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
18969db4a9c7SJesse Barnes 					 pipe_name(pipe),
18979db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1898776ad806SJesse Barnes 
1899776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1900776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1901776ad806SJesse Barnes 
1902776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1903776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1904776ad806SJesse Barnes 
1905776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
19068664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
19078664281bSPaulo Zanoni 							  false))
1908fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
19098664281bSPaulo Zanoni 
19108664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19118664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
19128664281bSPaulo Zanoni 							  false))
1913fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
19148664281bSPaulo Zanoni }
19158664281bSPaulo Zanoni 
19168664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
19178664281bSPaulo Zanoni {
19188664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19198664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19205a69b89fSDaniel Vetter 	enum pipe pipe;
19218664281bSPaulo Zanoni 
1922de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1923de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1924de032bf4SPaulo Zanoni 
19255a69b89fSDaniel Vetter 	for_each_pipe(pipe) {
19265a69b89fSDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
19275a69b89fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
19285a69b89fSDaniel Vetter 								  false))
1929fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
19305a69b89fSDaniel Vetter 					  pipe_name(pipe));
19315a69b89fSDaniel Vetter 		}
19328664281bSPaulo Zanoni 
19335a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19345a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1935277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19365a69b89fSDaniel Vetter 			else
1937277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19385a69b89fSDaniel Vetter 		}
19395a69b89fSDaniel Vetter 	}
19408bf1e9f1SShuang He 
19418664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19428664281bSPaulo Zanoni }
19438664281bSPaulo Zanoni 
19448664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19458664281bSPaulo Zanoni {
19468664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19478664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
19488664281bSPaulo Zanoni 
1949de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1950de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1951de032bf4SPaulo Zanoni 
19528664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
19538664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
19548664281bSPaulo Zanoni 							  false))
1955fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
19568664281bSPaulo Zanoni 
19578664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
19588664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
19598664281bSPaulo Zanoni 							  false))
1960fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
19618664281bSPaulo Zanoni 
19628664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
19638664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
19648664281bSPaulo Zanoni 							  false))
1965fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder C FIFO underrun\n");
19668664281bSPaulo Zanoni 
19678664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1968776ad806SJesse Barnes }
1969776ad806SJesse Barnes 
197023e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
197123e81d69SAdam Jackson {
19722d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
197323e81d69SAdam Jackson 	int pipe;
1974b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
197523e81d69SAdam Jackson 
197610a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
197791d131d2SDaniel Vetter 
1978cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1979cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
198023e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1981cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1982cfc33bf7SVille Syrjälä 				 port_name(port));
1983cfc33bf7SVille Syrjälä 	}
198423e81d69SAdam Jackson 
198523e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1986ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
198723e81d69SAdam Jackson 
198823e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1989515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
199023e81d69SAdam Jackson 
199123e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
199223e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
199323e81d69SAdam Jackson 
199423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
199523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
199623e81d69SAdam Jackson 
199723e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
199823e81d69SAdam Jackson 		for_each_pipe(pipe)
199923e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
200023e81d69SAdam Jackson 					 pipe_name(pipe),
200123e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
20028664281bSPaulo Zanoni 
20038664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
20048664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
200523e81d69SAdam Jackson }
200623e81d69SAdam Jackson 
2007c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
2008c008bc6eSPaulo Zanoni {
2009c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
201040da17c2SDaniel Vetter 	enum pipe pipe;
2011c008bc6eSPaulo Zanoni 
2012c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2013c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2014c008bc6eSPaulo Zanoni 
2015c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2016c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2017c008bc6eSPaulo Zanoni 
2018c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2019c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2020c008bc6eSPaulo Zanoni 
202140da17c2SDaniel Vetter 	for_each_pipe(pipe) {
202240da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
20238d7849dbSVille Syrjälä 			intel_pipe_handle_vblank(dev, pipe);
2024c008bc6eSPaulo Zanoni 
202540da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
202640da17c2SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
2027fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
202840da17c2SDaniel Vetter 					  pipe_name(pipe));
2029c008bc6eSPaulo Zanoni 
203040da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
203140da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20325b3a856bSDaniel Vetter 
203340da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
203440da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
203540da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
203640da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2037c008bc6eSPaulo Zanoni 		}
2038c008bc6eSPaulo Zanoni 	}
2039c008bc6eSPaulo Zanoni 
2040c008bc6eSPaulo Zanoni 	/* check event from PCH */
2041c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2042c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2043c008bc6eSPaulo Zanoni 
2044c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2045c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2046c008bc6eSPaulo Zanoni 		else
2047c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2048c008bc6eSPaulo Zanoni 
2049c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2050c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2051c008bc6eSPaulo Zanoni 	}
2052c008bc6eSPaulo Zanoni 
2053c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2054c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2055c008bc6eSPaulo Zanoni }
2056c008bc6eSPaulo Zanoni 
20579719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
20589719fb98SPaulo Zanoni {
20599719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
206007d27e20SDamien Lespiau 	enum pipe pipe;
20619719fb98SPaulo Zanoni 
20629719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
20639719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
20649719fb98SPaulo Zanoni 
20659719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
20669719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
20679719fb98SPaulo Zanoni 
20689719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
20699719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
20709719fb98SPaulo Zanoni 
207107d27e20SDamien Lespiau 	for_each_pipe(pipe) {
207207d27e20SDamien Lespiau 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
20738d7849dbSVille Syrjälä 			intel_pipe_handle_vblank(dev, pipe);
207440da17c2SDaniel Vetter 
207540da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
207607d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
207707d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
207807d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
20799719fb98SPaulo Zanoni 		}
20809719fb98SPaulo Zanoni 	}
20819719fb98SPaulo Zanoni 
20829719fb98SPaulo Zanoni 	/* check event from PCH */
20839719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
20849719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
20859719fb98SPaulo Zanoni 
20869719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
20879719fb98SPaulo Zanoni 
20889719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
20899719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
20909719fb98SPaulo Zanoni 	}
20919719fb98SPaulo Zanoni }
20929719fb98SPaulo Zanoni 
2093f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2094b1f14ad0SJesse Barnes {
209545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
20962d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2097f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
20980e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2099b1f14ad0SJesse Barnes 
21008664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
21018664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2102907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
21038664281bSPaulo Zanoni 
2104b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2105b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2106b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
210723a78516SPaulo Zanoni 	POSTING_READ(DEIER);
21080e43406bSChris Wilson 
210944498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
211044498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
211144498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
211244498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
211344498aeaSPaulo Zanoni 	 * due to its back queue). */
2114ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
211544498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
211644498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
211744498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2118ab5c608bSBen Widawsky 	}
211944498aeaSPaulo Zanoni 
21200e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
21210e43406bSChris Wilson 	if (gt_iir) {
2122d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
21230e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2124d8fc8a47SPaulo Zanoni 		else
2125d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
21260e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
21270e43406bSChris Wilson 		ret = IRQ_HANDLED;
21280e43406bSChris Wilson 	}
2129b1f14ad0SJesse Barnes 
2130b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
21310e43406bSChris Wilson 	if (de_iir) {
2132f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
21339719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2134f1af8fc1SPaulo Zanoni 		else
2135f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
21360e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
21370e43406bSChris Wilson 		ret = IRQ_HANDLED;
21380e43406bSChris Wilson 	}
21390e43406bSChris Wilson 
2140f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2141f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
21420e43406bSChris Wilson 		if (pm_iir) {
2143d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
2144b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
21450e43406bSChris Wilson 			ret = IRQ_HANDLED;
21460e43406bSChris Wilson 		}
2147f1af8fc1SPaulo Zanoni 	}
2148b1f14ad0SJesse Barnes 
2149b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2150b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2151ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
215244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
215344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2154ab5c608bSBen Widawsky 	}
2155b1f14ad0SJesse Barnes 
2156b1f14ad0SJesse Barnes 	return ret;
2157b1f14ad0SJesse Barnes }
2158b1f14ad0SJesse Barnes 
2159abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2160abd58f01SBen Widawsky {
2161abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2162abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2163abd58f01SBen Widawsky 	u32 master_ctl;
2164abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2165abd58f01SBen Widawsky 	uint32_t tmp = 0;
2166c42664ccSDaniel Vetter 	enum pipe pipe;
2167abd58f01SBen Widawsky 
2168abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
2169abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2170abd58f01SBen Widawsky 	if (!master_ctl)
2171abd58f01SBen Widawsky 		return IRQ_NONE;
2172abd58f01SBen Widawsky 
2173abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2174abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2175abd58f01SBen Widawsky 
2176abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
2177abd58f01SBen Widawsky 
2178abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2179abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2180abd58f01SBen Widawsky 		if (tmp & GEN8_DE_MISC_GSE)
2181abd58f01SBen Widawsky 			intel_opregion_asle_intr(dev);
2182abd58f01SBen Widawsky 		else if (tmp)
2183abd58f01SBen Widawsky 			DRM_ERROR("Unexpected DE Misc interrupt\n");
2184abd58f01SBen Widawsky 		else
2185abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2186abd58f01SBen Widawsky 
2187abd58f01SBen Widawsky 		if (tmp) {
2188abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2189abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2190abd58f01SBen Widawsky 		}
2191abd58f01SBen Widawsky 	}
2192abd58f01SBen Widawsky 
21936d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
21946d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
21956d766f02SDaniel Vetter 		if (tmp & GEN8_AUX_CHANNEL_A)
21966d766f02SDaniel Vetter 			dp_aux_irq_handler(dev);
21976d766f02SDaniel Vetter 		else if (tmp)
21986d766f02SDaniel Vetter 			DRM_ERROR("Unexpected DE Port interrupt\n");
21996d766f02SDaniel Vetter 		else
22006d766f02SDaniel Vetter 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22016d766f02SDaniel Vetter 
22026d766f02SDaniel Vetter 		if (tmp) {
22036d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
22046d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
22056d766f02SDaniel Vetter 		}
22066d766f02SDaniel Vetter 	}
22076d766f02SDaniel Vetter 
2208abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2209abd58f01SBen Widawsky 		uint32_t pipe_iir;
2210abd58f01SBen Widawsky 
2211c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2212c42664ccSDaniel Vetter 			continue;
2213c42664ccSDaniel Vetter 
2214abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2215abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_VBLANK)
22168d7849dbSVille Syrjälä 			intel_pipe_handle_vblank(dev, pipe);
2217abd58f01SBen Widawsky 
2218d0e1f1cbSDamien Lespiau 		if (pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE) {
2219abd58f01SBen Widawsky 			intel_prepare_page_flip(dev, pipe);
2220abd58f01SBen Widawsky 			intel_finish_page_flip_plane(dev, pipe);
2221abd58f01SBen Widawsky 		}
2222abd58f01SBen Widawsky 
22230fbe7870SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
22240fbe7870SDaniel Vetter 			hsw_pipe_crc_irq_handler(dev, pipe);
22250fbe7870SDaniel Vetter 
222638d83c96SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN) {
222738d83c96SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
222838d83c96SDaniel Vetter 								  false))
2229fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
223038d83c96SDaniel Vetter 					  pipe_name(pipe));
223138d83c96SDaniel Vetter 		}
223238d83c96SDaniel Vetter 
223330100f2bSDaniel Vetter 		if (pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS) {
223430100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
223530100f2bSDaniel Vetter 				  pipe_name(pipe),
223630100f2bSDaniel Vetter 				  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
223730100f2bSDaniel Vetter 		}
2238abd58f01SBen Widawsky 
2239abd58f01SBen Widawsky 		if (pipe_iir) {
2240abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2241abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2242c42664ccSDaniel Vetter 		} else
2243abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2244abd58f01SBen Widawsky 	}
2245abd58f01SBen Widawsky 
224692d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
224792d03a80SDaniel Vetter 		/*
224892d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
224992d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
225092d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
225192d03a80SDaniel Vetter 		 */
225292d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
225392d03a80SDaniel Vetter 
225492d03a80SDaniel Vetter 		cpt_irq_handler(dev, pch_iir);
225592d03a80SDaniel Vetter 
225692d03a80SDaniel Vetter 		if (pch_iir) {
225792d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
225892d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
225992d03a80SDaniel Vetter 		}
226092d03a80SDaniel Vetter 	}
226192d03a80SDaniel Vetter 
2262abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2263abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2264abd58f01SBen Widawsky 
2265abd58f01SBen Widawsky 	return ret;
2266abd58f01SBen Widawsky }
2267abd58f01SBen Widawsky 
226817e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
226917e1df07SDaniel Vetter 			       bool reset_completed)
227017e1df07SDaniel Vetter {
227117e1df07SDaniel Vetter 	struct intel_ring_buffer *ring;
227217e1df07SDaniel Vetter 	int i;
227317e1df07SDaniel Vetter 
227417e1df07SDaniel Vetter 	/*
227517e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
227617e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
227717e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
227817e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
227917e1df07SDaniel Vetter 	 */
228017e1df07SDaniel Vetter 
228117e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
228217e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
228317e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
228417e1df07SDaniel Vetter 
228517e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
228617e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
228717e1df07SDaniel Vetter 
228817e1df07SDaniel Vetter 	/*
228917e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
229017e1df07SDaniel Vetter 	 * reset state is cleared.
229117e1df07SDaniel Vetter 	 */
229217e1df07SDaniel Vetter 	if (reset_completed)
229317e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
229417e1df07SDaniel Vetter }
229517e1df07SDaniel Vetter 
22968a905236SJesse Barnes /**
22978a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
22988a905236SJesse Barnes  * @work: work struct
22998a905236SJesse Barnes  *
23008a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23018a905236SJesse Barnes  * was detected.
23028a905236SJesse Barnes  */
23038a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
23048a905236SJesse Barnes {
23051f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
23061f83fee0SDaniel Vetter 						    work);
23072d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
23082d1013ddSJani Nikula 		container_of(error, struct drm_i915_private, gpu_error);
23098a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
2310cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2311cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2312cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
231317e1df07SDaniel Vetter 	int ret;
23148a905236SJesse Barnes 
23155bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
23168a905236SJesse Barnes 
23177db0ba24SDaniel Vetter 	/*
23187db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
23197db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
23207db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
23217db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
23227db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
23237db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
23247db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
23257db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
23267db0ba24SDaniel Vetter 	 */
23277db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
232844d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
23295bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
23307db0ba24SDaniel Vetter 				   reset_event);
23311f83fee0SDaniel Vetter 
233217e1df07SDaniel Vetter 		/*
2333f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2334f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2335f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2336f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2337f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2338f454c694SImre Deak 		 */
2339f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
2340f454c694SImre Deak 		/*
234117e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
234217e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
234317e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
234417e1df07SDaniel Vetter 		 * deadlocks with the reset work.
234517e1df07SDaniel Vetter 		 */
2346f69061beSDaniel Vetter 		ret = i915_reset(dev);
2347f69061beSDaniel Vetter 
234817e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
234917e1df07SDaniel Vetter 
2350f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2351f454c694SImre Deak 
2352f69061beSDaniel Vetter 		if (ret == 0) {
2353f69061beSDaniel Vetter 			/*
2354f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2355f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2356f69061beSDaniel Vetter 			 * complete.
2357f69061beSDaniel Vetter 			 *
2358f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2359f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2360f69061beSDaniel Vetter 			 * updates before
2361f69061beSDaniel Vetter 			 * the counter increment.
2362f69061beSDaniel Vetter 			 */
2363f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
2364f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2365f69061beSDaniel Vetter 
23665bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2367f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
23681f83fee0SDaniel Vetter 		} else {
23692ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2370f316a42cSBen Gamari 		}
23711f83fee0SDaniel Vetter 
237217e1df07SDaniel Vetter 		/*
237317e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
237417e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
237517e1df07SDaniel Vetter 		 */
237617e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2377f316a42cSBen Gamari 	}
23788a905236SJesse Barnes }
23798a905236SJesse Barnes 
238035aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2381c0e09200SDave Airlie {
23828a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2383bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
238463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2385050ee91fSBen Widawsky 	int pipe, i;
238663eeaf38SJesse Barnes 
238735aed2e6SChris Wilson 	if (!eir)
238835aed2e6SChris Wilson 		return;
238963eeaf38SJesse Barnes 
2390a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
23918a905236SJesse Barnes 
2392bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2393bd9854f9SBen Widawsky 
23948a905236SJesse Barnes 	if (IS_G4X(dev)) {
23958a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
23968a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
23978a905236SJesse Barnes 
2398a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2399a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2400050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2401050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2402a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2403a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
24048a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24053143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
24068a905236SJesse Barnes 		}
24078a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
24088a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2409a70491ccSJoe Perches 			pr_err("page table error\n");
2410a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
24118a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24123143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
24138a905236SJesse Barnes 		}
24148a905236SJesse Barnes 	}
24158a905236SJesse Barnes 
2416a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
241763eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
241863eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2419a70491ccSJoe Perches 			pr_err("page table error\n");
2420a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
242163eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24223143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
242363eeaf38SJesse Barnes 		}
24248a905236SJesse Barnes 	}
24258a905236SJesse Barnes 
242663eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2427a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
24289db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2429a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
24309db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
243163eeaf38SJesse Barnes 		/* pipestat has already been acked */
243263eeaf38SJesse Barnes 	}
243363eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2434a70491ccSJoe Perches 		pr_err("instruction error\n");
2435a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2436050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2437050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2438a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
243963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
244063eeaf38SJesse Barnes 
2441a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2442a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2443a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
244463eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
24453143a2bfSChris Wilson 			POSTING_READ(IPEIR);
244663eeaf38SJesse Barnes 		} else {
244763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
244863eeaf38SJesse Barnes 
2449a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2450a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2451a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2452a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
245363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24543143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
245563eeaf38SJesse Barnes 		}
245663eeaf38SJesse Barnes 	}
245763eeaf38SJesse Barnes 
245863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
24593143a2bfSChris Wilson 	POSTING_READ(EIR);
246063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
246163eeaf38SJesse Barnes 	if (eir) {
246263eeaf38SJesse Barnes 		/*
246363eeaf38SJesse Barnes 		 * some errors might have become stuck,
246463eeaf38SJesse Barnes 		 * mask them.
246563eeaf38SJesse Barnes 		 */
246663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
246763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
246863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
246963eeaf38SJesse Barnes 	}
247035aed2e6SChris Wilson }
247135aed2e6SChris Wilson 
247235aed2e6SChris Wilson /**
247335aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
247435aed2e6SChris Wilson  * @dev: drm device
247535aed2e6SChris Wilson  *
247635aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
247735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
247835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
247935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
248035aed2e6SChris Wilson  * of a ring dump etc.).
248135aed2e6SChris Wilson  */
248258174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
248358174462SMika Kuoppala 		       const char *fmt, ...)
248435aed2e6SChris Wilson {
248535aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
248658174462SMika Kuoppala 	va_list args;
248758174462SMika Kuoppala 	char error_msg[80];
248835aed2e6SChris Wilson 
248958174462SMika Kuoppala 	va_start(args, fmt);
249058174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
249158174462SMika Kuoppala 	va_end(args);
249258174462SMika Kuoppala 
249358174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
249435aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
24958a905236SJesse Barnes 
2496ba1234d1SBen Gamari 	if (wedged) {
2497f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2498f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2499ba1234d1SBen Gamari 
250011ed50ecSBen Gamari 		/*
250117e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
250217e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
250317e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
250417e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
250517e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
250617e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
250717e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
250817e1df07SDaniel Vetter 		 *
250917e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
251017e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
251117e1df07SDaniel Vetter 		 * counter atomic_t.
251211ed50ecSBen Gamari 		 */
251317e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
251411ed50ecSBen Gamari 	}
251511ed50ecSBen Gamari 
2516122f46baSDaniel Vetter 	/*
2517122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
2518122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
2519122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2520122f46baSDaniel Vetter 	 * code will deadlock.
2521122f46baSDaniel Vetter 	 */
2522122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
25238a905236SJesse Barnes }
25248a905236SJesse Barnes 
252521ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
25264e5359cdSSimon Farnsworth {
25272d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
25284e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
25294e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
253005394f39SChris Wilson 	struct drm_i915_gem_object *obj;
25314e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
25324e5359cdSSimon Farnsworth 	unsigned long flags;
25334e5359cdSSimon Farnsworth 	bool stall_detected;
25344e5359cdSSimon Farnsworth 
25354e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
25364e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
25374e5359cdSSimon Farnsworth 		return;
25384e5359cdSSimon Farnsworth 
25394e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
25404e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
25414e5359cdSSimon Farnsworth 
2542e7d841caSChris Wilson 	if (work == NULL ||
2543e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2544e7d841caSChris Wilson 	    !work->enable_stall_check) {
25454e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
25464e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
25474e5359cdSSimon Farnsworth 		return;
25484e5359cdSSimon Farnsworth 	}
25494e5359cdSSimon Farnsworth 
25504e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
255105394f39SChris Wilson 	obj = work->pending_flip_obj;
2552a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
25539db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2554446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2555f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
25564e5359cdSSimon Farnsworth 	} else {
25579db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
2558f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
2559f4510a27SMatt Roper 							crtc->y * crtc->primary->fb->pitches[0] +
2560f4510a27SMatt Roper 							crtc->x * crtc->primary->fb->bits_per_pixel/8);
25614e5359cdSSimon Farnsworth 	}
25624e5359cdSSimon Farnsworth 
25634e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
25644e5359cdSSimon Farnsworth 
25654e5359cdSSimon Farnsworth 	if (stall_detected) {
25664e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
25674e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
25684e5359cdSSimon Farnsworth 	}
25694e5359cdSSimon Farnsworth }
25704e5359cdSSimon Farnsworth 
257142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
257242f52ef8SKeith Packard  * we use as a pipe index
257342f52ef8SKeith Packard  */
2574f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
25750a3e67a4SJesse Barnes {
25762d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2577e9d21d7fSKeith Packard 	unsigned long irqflags;
257871e0ffa5SJesse Barnes 
25795eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
258071e0ffa5SJesse Barnes 		return -EINVAL;
25810a3e67a4SJesse Barnes 
25821ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2583f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
25847c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2585755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
25860a3e67a4SJesse Barnes 	else
25877c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2588755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
25898692d00eSChris Wilson 
25908692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
25913d13ef2eSDamien Lespiau 	if (INTEL_INFO(dev)->gen == 3)
25926b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
25931ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25948692d00eSChris Wilson 
25950a3e67a4SJesse Barnes 	return 0;
25960a3e67a4SJesse Barnes }
25970a3e67a4SJesse Barnes 
2598f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2599f796cf8fSJesse Barnes {
26002d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2601f796cf8fSJesse Barnes 	unsigned long irqflags;
2602b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
260340da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2604f796cf8fSJesse Barnes 
2605f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2606f796cf8fSJesse Barnes 		return -EINVAL;
2607f796cf8fSJesse Barnes 
2608f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2609b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2610b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2611b1f14ad0SJesse Barnes 
2612b1f14ad0SJesse Barnes 	return 0;
2613b1f14ad0SJesse Barnes }
2614b1f14ad0SJesse Barnes 
26157e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
26167e231dbeSJesse Barnes {
26172d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26187e231dbeSJesse Barnes 	unsigned long irqflags;
26197e231dbeSJesse Barnes 
26207e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
26217e231dbeSJesse Barnes 		return -EINVAL;
26227e231dbeSJesse Barnes 
26237e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
262431acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2625755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
26267e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26277e231dbeSJesse Barnes 
26287e231dbeSJesse Barnes 	return 0;
26297e231dbeSJesse Barnes }
26307e231dbeSJesse Barnes 
2631abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2632abd58f01SBen Widawsky {
2633abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2634abd58f01SBen Widawsky 	unsigned long irqflags;
2635abd58f01SBen Widawsky 
2636abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2637abd58f01SBen Widawsky 		return -EINVAL;
2638abd58f01SBen Widawsky 
2639abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26407167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
26417167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2642abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2643abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2644abd58f01SBen Widawsky 	return 0;
2645abd58f01SBen Widawsky }
2646abd58f01SBen Widawsky 
264742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
264842f52ef8SKeith Packard  * we use as a pipe index
264942f52ef8SKeith Packard  */
2650f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26510a3e67a4SJesse Barnes {
26522d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2653e9d21d7fSKeith Packard 	unsigned long irqflags;
26540a3e67a4SJesse Barnes 
26551ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26563d13ef2eSDamien Lespiau 	if (INTEL_INFO(dev)->gen == 3)
26576b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
26588692d00eSChris Wilson 
26597c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2660755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2661755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26621ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26630a3e67a4SJesse Barnes }
26640a3e67a4SJesse Barnes 
2665f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2666f796cf8fSJesse Barnes {
26672d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2668f796cf8fSJesse Barnes 	unsigned long irqflags;
2669b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
267040da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2671f796cf8fSJesse Barnes 
2672f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2673b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2674b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2675b1f14ad0SJesse Barnes }
2676b1f14ad0SJesse Barnes 
26777e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
26787e231dbeSJesse Barnes {
26792d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26807e231dbeSJesse Barnes 	unsigned long irqflags;
26817e231dbeSJesse Barnes 
26827e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
268331acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2684755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26857e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26867e231dbeSJesse Barnes }
26877e231dbeSJesse Barnes 
2688abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2689abd58f01SBen Widawsky {
2690abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2691abd58f01SBen Widawsky 	unsigned long irqflags;
2692abd58f01SBen Widawsky 
2693abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2694abd58f01SBen Widawsky 		return;
2695abd58f01SBen Widawsky 
2696abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26977167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
26987167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2699abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2700abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2701abd58f01SBen Widawsky }
2702abd58f01SBen Widawsky 
2703893eead0SChris Wilson static u32
2704893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2705852835f3SZou Nan hai {
2706893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2707893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2708893eead0SChris Wilson }
2709893eead0SChris Wilson 
27109107e9d2SChris Wilson static bool
27119107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2712893eead0SChris Wilson {
27139107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
27149107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2715f65d9421SBen Gamari }
2716f65d9421SBen Gamari 
2717a028c4b0SDaniel Vetter static bool
2718a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2719a028c4b0SDaniel Vetter {
2720a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2721a028c4b0SDaniel Vetter 		/*
2722a028c4b0SDaniel Vetter 		 * FIXME: gen8 semaphore support - currently we don't emit
2723a028c4b0SDaniel Vetter 		 * semaphores on bdw anyway, but this needs to be addressed when
2724a028c4b0SDaniel Vetter 		 * we merge that code.
2725a028c4b0SDaniel Vetter 		 */
2726a028c4b0SDaniel Vetter 		return false;
2727a028c4b0SDaniel Vetter 	} else {
2728a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2729a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2730a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2731a028c4b0SDaniel Vetter 	}
2732a028c4b0SDaniel Vetter }
2733a028c4b0SDaniel Vetter 
27346274f212SChris Wilson static struct intel_ring_buffer *
2735921d42eaSDaniel Vetter semaphore_wait_to_signaller_ring(struct intel_ring_buffer *ring, u32 ipehr)
2736921d42eaSDaniel Vetter {
2737921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2738921d42eaSDaniel Vetter 	struct intel_ring_buffer *signaller;
2739921d42eaSDaniel Vetter 	int i;
2740921d42eaSDaniel Vetter 
2741921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2742921d42eaSDaniel Vetter 		/*
2743921d42eaSDaniel Vetter 		 * FIXME: gen8 semaphore support - currently we don't emit
2744921d42eaSDaniel Vetter 		 * semaphores on bdw anyway, but this needs to be addressed when
2745921d42eaSDaniel Vetter 		 * we merge that code.
2746921d42eaSDaniel Vetter 		 */
2747921d42eaSDaniel Vetter 		return NULL;
2748921d42eaSDaniel Vetter 	} else {
2749921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2750921d42eaSDaniel Vetter 
2751921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2752921d42eaSDaniel Vetter 			if(ring == signaller)
2753921d42eaSDaniel Vetter 				continue;
2754921d42eaSDaniel Vetter 
2755ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2756921d42eaSDaniel Vetter 				return signaller;
2757921d42eaSDaniel Vetter 		}
2758921d42eaSDaniel Vetter 	}
2759921d42eaSDaniel Vetter 
2760921d42eaSDaniel Vetter 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x\n",
2761921d42eaSDaniel Vetter 		  ring->id, ipehr);
2762921d42eaSDaniel Vetter 
2763921d42eaSDaniel Vetter 	return NULL;
2764921d42eaSDaniel Vetter }
2765921d42eaSDaniel Vetter 
27666274f212SChris Wilson static struct intel_ring_buffer *
27676274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2768a24a11e6SChris Wilson {
2769a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
277088fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
277188fe429dSDaniel Vetter 	int i;
2772a24a11e6SChris Wilson 
2773a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2774a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
27756274f212SChris Wilson 		return NULL;
2776a24a11e6SChris Wilson 
277788fe429dSDaniel Vetter 	/*
277888fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
277988fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
278088fe429dSDaniel Vetter 	 * dwords. Note that we don't care about ACTHD here since that might
278188fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
278288fe429dSDaniel Vetter 	 * ringbuffer itself.
2783a24a11e6SChris Wilson 	 */
278488fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
278588fe429dSDaniel Vetter 
278688fe429dSDaniel Vetter 	for (i = 4; i; --i) {
278788fe429dSDaniel Vetter 		/*
278888fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
278988fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
279088fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
279188fe429dSDaniel Vetter 		 */
279288fe429dSDaniel Vetter 		head &= ring->size - 1;
279388fe429dSDaniel Vetter 
279488fe429dSDaniel Vetter 		/* This here seems to blow up */
279588fe429dSDaniel Vetter 		cmd = ioread32(ring->virtual_start + head);
2796a24a11e6SChris Wilson 		if (cmd == ipehr)
2797a24a11e6SChris Wilson 			break;
2798a24a11e6SChris Wilson 
279988fe429dSDaniel Vetter 		head -= 4;
280088fe429dSDaniel Vetter 	}
2801a24a11e6SChris Wilson 
280288fe429dSDaniel Vetter 	if (!i)
280388fe429dSDaniel Vetter 		return NULL;
280488fe429dSDaniel Vetter 
280588fe429dSDaniel Vetter 	*seqno = ioread32(ring->virtual_start + head + 4) + 1;
2806921d42eaSDaniel Vetter 	return semaphore_wait_to_signaller_ring(ring, ipehr);
2807a24a11e6SChris Wilson }
2808a24a11e6SChris Wilson 
28096274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
28106274f212SChris Wilson {
28116274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
28126274f212SChris Wilson 	struct intel_ring_buffer *signaller;
28136274f212SChris Wilson 	u32 seqno, ctl;
28146274f212SChris Wilson 
28156274f212SChris Wilson 	ring->hangcheck.deadlock = true;
28166274f212SChris Wilson 
28176274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
28186274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
28196274f212SChris Wilson 		return -1;
28206274f212SChris Wilson 
28216274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
28226274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
28236274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
28246274f212SChris Wilson 		return -1;
28256274f212SChris Wilson 
28266274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
28276274f212SChris Wilson }
28286274f212SChris Wilson 
28296274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
28306274f212SChris Wilson {
28316274f212SChris Wilson 	struct intel_ring_buffer *ring;
28326274f212SChris Wilson 	int i;
28336274f212SChris Wilson 
28346274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
28356274f212SChris Wilson 		ring->hangcheck.deadlock = false;
28366274f212SChris Wilson }
28376274f212SChris Wilson 
2838ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
283950877445SChris Wilson ring_stuck(struct intel_ring_buffer *ring, u64 acthd)
28401ec14ad3SChris Wilson {
28411ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28421ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28439107e9d2SChris Wilson 	u32 tmp;
28449107e9d2SChris Wilson 
28456274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2846f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
28476274f212SChris Wilson 
28489107e9d2SChris Wilson 	if (IS_GEN2(dev))
2849f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
28509107e9d2SChris Wilson 
28519107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
28529107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
28539107e9d2SChris Wilson 	 * and break the hang. This should work on
28549107e9d2SChris Wilson 	 * all but the second generation chipsets.
28559107e9d2SChris Wilson 	 */
28569107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
28571ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
285858174462SMika Kuoppala 		i915_handle_error(dev, false,
285958174462SMika Kuoppala 				  "Kicking stuck wait on %s",
28601ec14ad3SChris Wilson 				  ring->name);
28611ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2862f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
28631ec14ad3SChris Wilson 	}
2864a24a11e6SChris Wilson 
28656274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
28666274f212SChris Wilson 		switch (semaphore_passed(ring)) {
28676274f212SChris Wilson 		default:
2868f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
28696274f212SChris Wilson 		case 1:
287058174462SMika Kuoppala 			i915_handle_error(dev, false,
287158174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2872a24a11e6SChris Wilson 					  ring->name);
2873a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2874f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
28756274f212SChris Wilson 		case 0:
2876f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
28776274f212SChris Wilson 		}
28789107e9d2SChris Wilson 	}
28799107e9d2SChris Wilson 
2880f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2881a24a11e6SChris Wilson }
2882d1e61e7fSChris Wilson 
2883f65d9421SBen Gamari /**
2884f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
288505407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
288605407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
288705407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
288805407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
288905407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2890f65d9421SBen Gamari  */
2891a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2892f65d9421SBen Gamari {
2893f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
28942d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2895b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2896b4519513SChris Wilson 	int i;
289705407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
28989107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
28999107e9d2SChris Wilson #define BUSY 1
29009107e9d2SChris Wilson #define KICK 5
29019107e9d2SChris Wilson #define HUNG 20
2902893eead0SChris Wilson 
2903d330a953SJani Nikula 	if (!i915.enable_hangcheck)
29043e0dc6b0SBen Widawsky 		return;
29053e0dc6b0SBen Widawsky 
2906b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
290750877445SChris Wilson 		u64 acthd;
290850877445SChris Wilson 		u32 seqno;
29099107e9d2SChris Wilson 		bool busy = true;
2910b4519513SChris Wilson 
29116274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
29126274f212SChris Wilson 
291305407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
291405407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
291505407ff8SMika Kuoppala 
291605407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
29179107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2918da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2919da661464SMika Kuoppala 
29209107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
29219107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2922094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2923f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
29249107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
29259107e9d2SChris Wilson 								  ring->name);
2926f4adcd24SDaniel Vetter 						else
2927f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2928f4adcd24SDaniel Vetter 								 ring->name);
29299107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2930094f9a54SChris Wilson 					}
2931094f9a54SChris Wilson 					/* Safeguard against driver failure */
2932094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
29339107e9d2SChris Wilson 				} else
29349107e9d2SChris Wilson 					busy = false;
293505407ff8SMika Kuoppala 			} else {
29366274f212SChris Wilson 				/* We always increment the hangcheck score
29376274f212SChris Wilson 				 * if the ring is busy and still processing
29386274f212SChris Wilson 				 * the same request, so that no single request
29396274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29406274f212SChris Wilson 				 * batches). The only time we do not increment
29416274f212SChris Wilson 				 * the hangcheck score on this ring, if this
29426274f212SChris Wilson 				 * ring is in a legitimate wait for another
29436274f212SChris Wilson 				 * ring. In that case the waiting ring is a
29446274f212SChris Wilson 				 * victim and we want to be sure we catch the
29456274f212SChris Wilson 				 * right culprit. Then every time we do kick
29466274f212SChris Wilson 				 * the ring, add a small increment to the
29476274f212SChris Wilson 				 * score so that we can catch a batch that is
29486274f212SChris Wilson 				 * being repeatedly kicked and so responsible
29496274f212SChris Wilson 				 * for stalling the machine.
29509107e9d2SChris Wilson 				 */
2951ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2952ad8beaeaSMika Kuoppala 								    acthd);
2953ad8beaeaSMika Kuoppala 
2954ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2955da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2956f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
29576274f212SChris Wilson 					break;
2958f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2959ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
29606274f212SChris Wilson 					break;
2961f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2962ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
29636274f212SChris Wilson 					break;
2964f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2965ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
29666274f212SChris Wilson 					stuck[i] = true;
29676274f212SChris Wilson 					break;
29686274f212SChris Wilson 				}
296905407ff8SMika Kuoppala 			}
29709107e9d2SChris Wilson 		} else {
2971da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2972da661464SMika Kuoppala 
29739107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
29749107e9d2SChris Wilson 			 * attempts across multiple batches.
29759107e9d2SChris Wilson 			 */
29769107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
29779107e9d2SChris Wilson 				ring->hangcheck.score--;
2978cbb465e7SChris Wilson 		}
2979f65d9421SBen Gamari 
298005407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
298105407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
29829107e9d2SChris Wilson 		busy_count += busy;
298305407ff8SMika Kuoppala 	}
298405407ff8SMika Kuoppala 
298505407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2986b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2987b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
298805407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2989a43adf07SChris Wilson 				 ring->name);
2990a43adf07SChris Wilson 			rings_hung++;
299105407ff8SMika Kuoppala 		}
299205407ff8SMika Kuoppala 	}
299305407ff8SMika Kuoppala 
299405407ff8SMika Kuoppala 	if (rings_hung)
299558174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
299605407ff8SMika Kuoppala 
299705407ff8SMika Kuoppala 	if (busy_count)
299805407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
299905407ff8SMika Kuoppala 		 * being added */
300010cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
300110cd45b6SMika Kuoppala }
300210cd45b6SMika Kuoppala 
300310cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
300410cd45b6SMika Kuoppala {
300510cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
3006d330a953SJani Nikula 	if (!i915.enable_hangcheck)
300710cd45b6SMika Kuoppala 		return;
300810cd45b6SMika Kuoppala 
300999584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
301010cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
3011f65d9421SBen Gamari }
3012f65d9421SBen Gamari 
30131c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
301491738a95SPaulo Zanoni {
301591738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
301691738a95SPaulo Zanoni 
301791738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
301891738a95SPaulo Zanoni 		return;
301991738a95SPaulo Zanoni 
3020f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3021105b122eSPaulo Zanoni 
3022105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3023105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3024622364b6SPaulo Zanoni }
3025105b122eSPaulo Zanoni 
302691738a95SPaulo Zanoni /*
3027622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3028622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3029622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3030622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3031622364b6SPaulo Zanoni  *
3032622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
303391738a95SPaulo Zanoni  */
3034622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3035622364b6SPaulo Zanoni {
3036622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3037622364b6SPaulo Zanoni 
3038622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3039622364b6SPaulo Zanoni 		return;
3040622364b6SPaulo Zanoni 
3041622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
304291738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
304391738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
304491738a95SPaulo Zanoni }
304591738a95SPaulo Zanoni 
30467c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3047d18ea1b5SDaniel Vetter {
3048d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3049d18ea1b5SDaniel Vetter 
3050f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3051a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3052f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3053d18ea1b5SDaniel Vetter }
3054d18ea1b5SDaniel Vetter 
3055c0e09200SDave Airlie /* drm_dma.h hooks
3056c0e09200SDave Airlie */
3057be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3058036a4a7dSZhenyu Wang {
30592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3060036a4a7dSZhenyu Wang 
30610c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3062bdfcdb63SDaniel Vetter 
3063f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3064c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3065c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3066036a4a7dSZhenyu Wang 
30677c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3068c650156aSZhenyu Wang 
30691c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
30707d99163dSBen Widawsky }
30717d99163dSBen Widawsky 
3072be30b29fSPaulo Zanoni static void ironlake_irq_preinstall(struct drm_device *dev)
3073be30b29fSPaulo Zanoni {
3074be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
30757d99163dSBen Widawsky }
30767d99163dSBen Widawsky 
30777e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
30787e231dbeSJesse Barnes {
30792d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
30807e231dbeSJesse Barnes 	int pipe;
30817e231dbeSJesse Barnes 
30827e231dbeSJesse Barnes 	/* VLV magic */
30837e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
30847e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
30857e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
30867e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
30877e231dbeSJesse Barnes 
30887e231dbeSJesse Barnes 	/* and GT */
30897e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
30907e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
3091d18ea1b5SDaniel Vetter 
30927c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
30937e231dbeSJesse Barnes 
30947e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
30957e231dbeSJesse Barnes 
30967e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
30977e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
30987e231dbeSJesse Barnes 	for_each_pipe(pipe)
30997e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
31007e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31017e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
31027e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
31037e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
31047e231dbeSJesse Barnes }
31057e231dbeSJesse Barnes 
3106823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3107abd58f01SBen Widawsky {
3108abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3109abd58f01SBen Widawsky 	int pipe;
3110abd58f01SBen Widawsky 
3111abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3112abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3113abd58f01SBen Widawsky 
3114f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 0);
3115f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 1);
3116f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 2);
3117f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 3);
3118abd58f01SBen Widawsky 
3119823f6b38SPaulo Zanoni 	for_each_pipe(pipe)
3120f86f3fb0SPaulo Zanoni 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3121abd58f01SBen Widawsky 
3122f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3123f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3124f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3125abd58f01SBen Widawsky 
31261c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
3127abd58f01SBen Widawsky }
3128abd58f01SBen Widawsky 
3129823f6b38SPaulo Zanoni static void gen8_irq_preinstall(struct drm_device *dev)
3130823f6b38SPaulo Zanoni {
3131823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3132abd58f01SBen Widawsky }
3133abd58f01SBen Widawsky 
313443f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
313543f328d7SVille Syrjälä {
313643f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
313743f328d7SVille Syrjälä 	int pipe;
313843f328d7SVille Syrjälä 
313943f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
314043f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
314143f328d7SVille Syrjälä 
314243f328d7SVille Syrjälä 	GEN8_IRQ_RESET_NDX(GT, 0);
314343f328d7SVille Syrjälä 	GEN8_IRQ_RESET_NDX(GT, 1);
314443f328d7SVille Syrjälä 	GEN8_IRQ_RESET_NDX(GT, 2);
314543f328d7SVille Syrjälä 	GEN8_IRQ_RESET_NDX(GT, 3);
314643f328d7SVille Syrjälä 
314743f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
314843f328d7SVille Syrjälä 
314943f328d7SVille Syrjälä 	POSTING_READ(GEN8_PCU_IIR);
315043f328d7SVille Syrjälä 
315143f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
315243f328d7SVille Syrjälä 
315343f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
315443f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
315543f328d7SVille Syrjälä 
315643f328d7SVille Syrjälä 	for_each_pipe(pipe)
315743f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
315843f328d7SVille Syrjälä 
315943f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, 0xffffffff);
316043f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, 0x0);
316143f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
316243f328d7SVille Syrjälä 	POSTING_READ(VLV_IIR);
316343f328d7SVille Syrjälä }
316443f328d7SVille Syrjälä 
316582a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
316682a28bcfSDaniel Vetter {
31672d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
316882a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
316982a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
3170fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
317182a28bcfSDaniel Vetter 
317282a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3173fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
317482a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3175cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3176fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
317782a28bcfSDaniel Vetter 	} else {
3178fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
317982a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3180cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3181fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
318282a28bcfSDaniel Vetter 	}
318382a28bcfSDaniel Vetter 
3184fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
318582a28bcfSDaniel Vetter 
31867fe0b973SKeith Packard 	/*
31877fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
31887fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
31897fe0b973SKeith Packard 	 *
31907fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
31917fe0b973SKeith Packard 	 */
31927fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31937fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
31947fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
31957fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
31967fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
31977fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31987fe0b973SKeith Packard }
31997fe0b973SKeith Packard 
3200d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3201d46da437SPaulo Zanoni {
32022d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
320382a28bcfSDaniel Vetter 	u32 mask;
3204d46da437SPaulo Zanoni 
3205692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3206692a04cfSDaniel Vetter 		return;
3207692a04cfSDaniel Vetter 
3208105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
32095c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3210105b122eSPaulo Zanoni 	else
32115c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32128664281bSPaulo Zanoni 
3213337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3214d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3215d46da437SPaulo Zanoni }
3216d46da437SPaulo Zanoni 
32170a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
32180a9a8c91SDaniel Vetter {
32190a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
32200a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
32210a9a8c91SDaniel Vetter 
32220a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
32230a9a8c91SDaniel Vetter 
32240a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3225040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
32260a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
322735a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
322835a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
32290a9a8c91SDaniel Vetter 	}
32300a9a8c91SDaniel Vetter 
32310a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
32320a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
32330a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
32340a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
32350a9a8c91SDaniel Vetter 	} else {
32360a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
32370a9a8c91SDaniel Vetter 	}
32380a9a8c91SDaniel Vetter 
323935079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
32400a9a8c91SDaniel Vetter 
32410a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
3242a6706b45SDeepak S 		pm_irqs |= dev_priv->pm_rps_events;
32430a9a8c91SDaniel Vetter 
32440a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
32450a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
32460a9a8c91SDaniel Vetter 
3247605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
324835079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
32490a9a8c91SDaniel Vetter 	}
32500a9a8c91SDaniel Vetter }
32510a9a8c91SDaniel Vetter 
3252f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3253036a4a7dSZhenyu Wang {
32544bc9d430SDaniel Vetter 	unsigned long irqflags;
32552d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
32568e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
32578e76f8dcSPaulo Zanoni 
32588e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
32598e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
32608e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
32618e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
32625c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
32638e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
32645c673b60SDaniel Vetter 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
32658e76f8dcSPaulo Zanoni 	} else {
32668e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3267ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
32685b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
32695b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
32705b3a856bSDaniel Vetter 				DE_POISON);
32715c673b60SDaniel Vetter 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
32725c673b60SDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
32738e76f8dcSPaulo Zanoni 	}
3274036a4a7dSZhenyu Wang 
32751ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3276036a4a7dSZhenyu Wang 
32770c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
32780c841212SPaulo Zanoni 
3279622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3280622364b6SPaulo Zanoni 
328135079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3282036a4a7dSZhenyu Wang 
32830a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3284036a4a7dSZhenyu Wang 
3285d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
32867fe0b973SKeith Packard 
3287f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
32886005ce42SDaniel Vetter 		/* Enable PCU event interrupts
32896005ce42SDaniel Vetter 		 *
32906005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
32914bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
32924bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
32934bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3294f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
32954bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3296f97108d1SJesse Barnes 	}
3297f97108d1SJesse Barnes 
3298036a4a7dSZhenyu Wang 	return 0;
3299036a4a7dSZhenyu Wang }
3300036a4a7dSZhenyu Wang 
3301f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3302f8b79e58SImre Deak {
3303f8b79e58SImre Deak 	u32 pipestat_mask;
3304f8b79e58SImre Deak 	u32 iir_mask;
3305f8b79e58SImre Deak 
3306f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3307f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3308f8b79e58SImre Deak 
3309f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3310f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3311f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3312f8b79e58SImre Deak 
3313f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3314f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3315f8b79e58SImre Deak 
3316f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3317f8b79e58SImre Deak 					       PIPE_GMBUS_INTERRUPT_STATUS);
3318f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3319f8b79e58SImre Deak 
3320f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3321f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3322f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3323f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3324f8b79e58SImre Deak 
3325f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3326f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3327f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3328f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3329f8b79e58SImre Deak 	POSTING_READ(VLV_IER);
3330f8b79e58SImre Deak }
3331f8b79e58SImre Deak 
3332f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3333f8b79e58SImre Deak {
3334f8b79e58SImre Deak 	u32 pipestat_mask;
3335f8b79e58SImre Deak 	u32 iir_mask;
3336f8b79e58SImre Deak 
3337f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3338f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33396c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3340f8b79e58SImre Deak 
3341f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3342f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3343f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3344f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3345f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3346f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3347f8b79e58SImre Deak 
3348f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3349f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3350f8b79e58SImre Deak 
3351f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3352f8b79e58SImre Deak 					        PIPE_GMBUS_INTERRUPT_STATUS);
3353f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3354f8b79e58SImre Deak 
3355f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3356f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3357f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3358f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3359f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3360f8b79e58SImre Deak }
3361f8b79e58SImre Deak 
3362f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3363f8b79e58SImre Deak {
3364f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3365f8b79e58SImre Deak 
3366f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3367f8b79e58SImre Deak 		return;
3368f8b79e58SImre Deak 
3369f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3370f8b79e58SImre Deak 
3371f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3372f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3373f8b79e58SImre Deak }
3374f8b79e58SImre Deak 
3375f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3376f8b79e58SImre Deak {
3377f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3378f8b79e58SImre Deak 
3379f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3380f8b79e58SImre Deak 		return;
3381f8b79e58SImre Deak 
3382f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3383f8b79e58SImre Deak 
3384f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3385f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3386f8b79e58SImre Deak }
3387f8b79e58SImre Deak 
33887e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
33897e231dbeSJesse Barnes {
33902d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3391b79480baSDaniel Vetter 	unsigned long irqflags;
33927e231dbeSJesse Barnes 
3393f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
33947e231dbeSJesse Barnes 
339520afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
339620afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
339720afbda2SDaniel Vetter 
33987e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3399f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
34007e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34017e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
34027e231dbeSJesse Barnes 
3403b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3404b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3405b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3406f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3407f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3408b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
340931acc7f5SJesse Barnes 
34107e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34117e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34127e231dbeSJesse Barnes 
34130a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
34147e231dbeSJesse Barnes 
34157e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
34167e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
34177e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
34187e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
34197e231dbeSJesse Barnes #endif
34207e231dbeSJesse Barnes 
34217e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
342220afbda2SDaniel Vetter 
342320afbda2SDaniel Vetter 	return 0;
342420afbda2SDaniel Vetter }
342520afbda2SDaniel Vetter 
3426abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3427abd58f01SBen Widawsky {
3428abd58f01SBen Widawsky 	int i;
3429abd58f01SBen Widawsky 
3430abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3431abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3432abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3433abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
3434abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3435abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
3436abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3437abd58f01SBen Widawsky 		0,
3438abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3439abd58f01SBen Widawsky 		};
3440abd58f01SBen Widawsky 
3441337ba017SPaulo Zanoni 	for (i = 0; i < ARRAY_SIZE(gt_interrupts); i++)
344235079899SPaulo Zanoni 		GEN8_IRQ_INIT_NDX(GT, i, ~gt_interrupts[i], gt_interrupts[i]);
3443*0961021aSBen Widawsky 
3444*0961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
3445abd58f01SBen Widawsky }
3446abd58f01SBen Widawsky 
3447abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3448abd58f01SBen Widawsky {
3449abd58f01SBen Widawsky 	struct drm_device *dev = dev_priv->dev;
3450d0e1f1cbSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_PRIMARY_FLIP_DONE |
34510fbe7870SDaniel Vetter 		GEN8_PIPE_CDCLK_CRC_DONE |
345230100f2bSDaniel Vetter 		GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
34535c673b60SDaniel Vetter 	uint32_t de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
34545c673b60SDaniel Vetter 		GEN8_PIPE_FIFO_UNDERRUN;
3455abd58f01SBen Widawsky 	int pipe;
345613b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
345713b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
345813b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3459abd58f01SBen Widawsky 
3460337ba017SPaulo Zanoni 	for_each_pipe(pipe)
346135079899SPaulo Zanoni 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe, dev_priv->de_irq_mask[pipe],
346235079899SPaulo Zanoni 				  de_pipe_enables);
3463abd58f01SBen Widawsky 
346435079899SPaulo Zanoni 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~GEN8_AUX_CHANNEL_A, GEN8_AUX_CHANNEL_A);
3465abd58f01SBen Widawsky }
3466abd58f01SBen Widawsky 
3467abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3468abd58f01SBen Widawsky {
3469abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3470abd58f01SBen Widawsky 
3471622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3472622364b6SPaulo Zanoni 
3473abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3474abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3475abd58f01SBen Widawsky 
3476abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3477abd58f01SBen Widawsky 
3478abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3479abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3480abd58f01SBen Widawsky 
3481abd58f01SBen Widawsky 	return 0;
3482abd58f01SBen Widawsky }
3483abd58f01SBen Widawsky 
348443f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
348543f328d7SVille Syrjälä {
348643f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
348743f328d7SVille Syrjälä 	u32 enable_mask = I915_DISPLAY_PORT_INTERRUPT |
348843f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
348943f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
349043f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
349143f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT |
349243f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
349343f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_C_VBLANK_INTERRUPT;
349443f328d7SVille Syrjälä 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
349543f328d7SVille Syrjälä 	unsigned long irqflags;
349643f328d7SVille Syrjälä 	int pipe;
349743f328d7SVille Syrjälä 
349843f328d7SVille Syrjälä 	/*
349943f328d7SVille Syrjälä 	 * Leave vblank interrupts masked initially.  enable/disable will
350043f328d7SVille Syrjälä 	 * toggle them based on usage.
350143f328d7SVille Syrjälä 	 */
350243f328d7SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask |
350343f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
350443f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT |
350543f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_C_VBLANK_INTERRUPT;
350643f328d7SVille Syrjälä 
350743f328d7SVille Syrjälä 	for_each_pipe(pipe)
350843f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
350943f328d7SVille Syrjälä 
351043f328d7SVille Syrjälä 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
351143f328d7SVille Syrjälä 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
351243f328d7SVille Syrjälä 	for_each_pipe(pipe)
351343f328d7SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_enable);
351443f328d7SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
351543f328d7SVille Syrjälä 
351643f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
351743f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
351843f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, enable_mask);
351943f328d7SVille Syrjälä 
352043f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
352143f328d7SVille Syrjälä 
352243f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
352343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
352443f328d7SVille Syrjälä 
352543f328d7SVille Syrjälä 	return 0;
352643f328d7SVille Syrjälä }
352743f328d7SVille Syrjälä 
3528abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3529abd58f01SBen Widawsky {
3530abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3531abd58f01SBen Widawsky 
3532abd58f01SBen Widawsky 	if (!dev_priv)
3533abd58f01SBen Widawsky 		return;
3534abd58f01SBen Widawsky 
3535d4eb6b10SPaulo Zanoni 	intel_hpd_irq_uninstall(dev_priv);
3536abd58f01SBen Widawsky 
3537823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3538abd58f01SBen Widawsky }
3539abd58f01SBen Widawsky 
35407e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
35417e231dbeSJesse Barnes {
35422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3543f8b79e58SImre Deak 	unsigned long irqflags;
35447e231dbeSJesse Barnes 	int pipe;
35457e231dbeSJesse Barnes 
35467e231dbeSJesse Barnes 	if (!dev_priv)
35477e231dbeSJesse Barnes 		return;
35487e231dbeSJesse Barnes 
3549843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3550843d0e7dSImre Deak 
35513ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3552ac4c16c5SEgbert Eich 
35537e231dbeSJesse Barnes 	for_each_pipe(pipe)
35547e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
35557e231dbeSJesse Barnes 
35567e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
35577e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
35587e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3559f8b79e58SImre Deak 
3560f8b79e58SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3561f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3562f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3563f8b79e58SImre Deak 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3564f8b79e58SImre Deak 
3565f8b79e58SImre Deak 	dev_priv->irq_mask = 0;
3566f8b79e58SImre Deak 
35677e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
35687e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
35697e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
35707e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
35717e231dbeSJesse Barnes }
35727e231dbeSJesse Barnes 
357343f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
357443f328d7SVille Syrjälä {
357543f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
357643f328d7SVille Syrjälä 	int pipe;
357743f328d7SVille Syrjälä 
357843f328d7SVille Syrjälä 	if (!dev_priv)
357943f328d7SVille Syrjälä 		return;
358043f328d7SVille Syrjälä 
358143f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
358243f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
358343f328d7SVille Syrjälä 
358443f328d7SVille Syrjälä #define GEN8_IRQ_FINI_NDX(type, which)				\
358543f328d7SVille Syrjälä do {								\
358643f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff);	\
358743f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IER(which), 0);		\
358843f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff);	\
358943f328d7SVille Syrjälä 	POSTING_READ(GEN8_##type##_IIR(which));			\
359043f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff);	\
359143f328d7SVille Syrjälä } while (0)
359243f328d7SVille Syrjälä 
359343f328d7SVille Syrjälä #define GEN8_IRQ_FINI(type)				\
359443f328d7SVille Syrjälä do {							\
359543f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR, 0xffffffff);	\
359643f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IER, 0);		\
359743f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR, 0xffffffff);	\
359843f328d7SVille Syrjälä 	POSTING_READ(GEN8_##type##_IIR);		\
359943f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR, 0xffffffff);	\
360043f328d7SVille Syrjälä } while (0)
360143f328d7SVille Syrjälä 
360243f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 0);
360343f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 1);
360443f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 2);
360543f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 3);
360643f328d7SVille Syrjälä 
360743f328d7SVille Syrjälä 	GEN8_IRQ_FINI(PCU);
360843f328d7SVille Syrjälä 
360943f328d7SVille Syrjälä #undef GEN8_IRQ_FINI
361043f328d7SVille Syrjälä #undef GEN8_IRQ_FINI_NDX
361143f328d7SVille Syrjälä 
361243f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
361343f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
361443f328d7SVille Syrjälä 
361543f328d7SVille Syrjälä 	for_each_pipe(pipe)
361643f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
361743f328d7SVille Syrjälä 
361843f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, 0xffffffff);
361943f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, 0x0);
362043f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
362143f328d7SVille Syrjälä 	POSTING_READ(VLV_IIR);
362243f328d7SVille Syrjälä }
362343f328d7SVille Syrjälä 
3624f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3625036a4a7dSZhenyu Wang {
36262d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36274697995bSJesse Barnes 
36284697995bSJesse Barnes 	if (!dev_priv)
36294697995bSJesse Barnes 		return;
36304697995bSJesse Barnes 
36313ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3632ac4c16c5SEgbert Eich 
3633be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3634036a4a7dSZhenyu Wang }
3635036a4a7dSZhenyu Wang 
3636c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3637c2798b19SChris Wilson {
36382d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3639c2798b19SChris Wilson 	int pipe;
3640c2798b19SChris Wilson 
3641c2798b19SChris Wilson 	for_each_pipe(pipe)
3642c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3643c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3644c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3645c2798b19SChris Wilson 	POSTING_READ16(IER);
3646c2798b19SChris Wilson }
3647c2798b19SChris Wilson 
3648c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3649c2798b19SChris Wilson {
36502d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3651379ef82dSDaniel Vetter 	unsigned long irqflags;
3652c2798b19SChris Wilson 
3653c2798b19SChris Wilson 	I915_WRITE16(EMR,
3654c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3655c2798b19SChris Wilson 
3656c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3657c2798b19SChris Wilson 	dev_priv->irq_mask =
3658c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3659c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3660c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3661c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3662c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3663c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3664c2798b19SChris Wilson 
3665c2798b19SChris Wilson 	I915_WRITE16(IER,
3666c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3667c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3668c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3669c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3670c2798b19SChris Wilson 	POSTING_READ16(IER);
3671c2798b19SChris Wilson 
3672379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3673379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3674379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3675755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3676755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3677379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3678379ef82dSDaniel Vetter 
3679c2798b19SChris Wilson 	return 0;
3680c2798b19SChris Wilson }
3681c2798b19SChris Wilson 
368290a72f87SVille Syrjälä /*
368390a72f87SVille Syrjälä  * Returns true when a page flip has completed.
368490a72f87SVille Syrjälä  */
368590a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
36861f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
368790a72f87SVille Syrjälä {
36882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36891f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
369090a72f87SVille Syrjälä 
36918d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
369290a72f87SVille Syrjälä 		return false;
369390a72f87SVille Syrjälä 
369490a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
369590a72f87SVille Syrjälä 		return false;
369690a72f87SVille Syrjälä 
36971f1c2e24SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
369890a72f87SVille Syrjälä 
369990a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
370090a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
370190a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
370290a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
370390a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
370490a72f87SVille Syrjälä 	 */
370590a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
370690a72f87SVille Syrjälä 		return false;
370790a72f87SVille Syrjälä 
370890a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
370990a72f87SVille Syrjälä 
371090a72f87SVille Syrjälä 	return true;
371190a72f87SVille Syrjälä }
371290a72f87SVille Syrjälä 
3713ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3714c2798b19SChris Wilson {
371545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
37162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3717c2798b19SChris Wilson 	u16 iir, new_iir;
3718c2798b19SChris Wilson 	u32 pipe_stats[2];
3719c2798b19SChris Wilson 	unsigned long irqflags;
3720c2798b19SChris Wilson 	int pipe;
3721c2798b19SChris Wilson 	u16 flip_mask =
3722c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3723c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3724c2798b19SChris Wilson 
3725c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3726c2798b19SChris Wilson 	if (iir == 0)
3727c2798b19SChris Wilson 		return IRQ_NONE;
3728c2798b19SChris Wilson 
3729c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3730c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3731c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3732c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3733c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3734c2798b19SChris Wilson 		 */
3735c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3736c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
373758174462SMika Kuoppala 			i915_handle_error(dev, false,
373858174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
373958174462SMika Kuoppala 					  iir);
3740c2798b19SChris Wilson 
3741c2798b19SChris Wilson 		for_each_pipe(pipe) {
3742c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3743c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3744c2798b19SChris Wilson 
3745c2798b19SChris Wilson 			/*
3746c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3747c2798b19SChris Wilson 			 */
37482d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3749c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3750c2798b19SChris Wilson 		}
3751c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3752c2798b19SChris Wilson 
3753c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3754c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3755c2798b19SChris Wilson 
3756d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3757c2798b19SChris Wilson 
3758c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3759c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3760c2798b19SChris Wilson 
37614356d586SDaniel Vetter 		for_each_pipe(pipe) {
37621f1c2e24SVille Syrjälä 			int plane = pipe;
37633a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
37641f1c2e24SVille Syrjälä 				plane = !plane;
37651f1c2e24SVille Syrjälä 
37664356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
37671f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
37681f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3769c2798b19SChris Wilson 
37704356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3771277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
37722d9d2b0bSVille Syrjälä 
37732d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
37742d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3775fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
37764356d586SDaniel Vetter 		}
3777c2798b19SChris Wilson 
3778c2798b19SChris Wilson 		iir = new_iir;
3779c2798b19SChris Wilson 	}
3780c2798b19SChris Wilson 
3781c2798b19SChris Wilson 	return IRQ_HANDLED;
3782c2798b19SChris Wilson }
3783c2798b19SChris Wilson 
3784c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3785c2798b19SChris Wilson {
37862d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3787c2798b19SChris Wilson 	int pipe;
3788c2798b19SChris Wilson 
3789c2798b19SChris Wilson 	for_each_pipe(pipe) {
3790c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3791c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3792c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3793c2798b19SChris Wilson 	}
3794c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3795c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3796c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3797c2798b19SChris Wilson }
3798c2798b19SChris Wilson 
3799a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3800a266c7d5SChris Wilson {
38012d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3802a266c7d5SChris Wilson 	int pipe;
3803a266c7d5SChris Wilson 
3804a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3805a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3806a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3807a266c7d5SChris Wilson 	}
3808a266c7d5SChris Wilson 
380900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3810a266c7d5SChris Wilson 	for_each_pipe(pipe)
3811a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3812a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3813a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3814a266c7d5SChris Wilson 	POSTING_READ(IER);
3815a266c7d5SChris Wilson }
3816a266c7d5SChris Wilson 
3817a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3818a266c7d5SChris Wilson {
38192d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
382038bde180SChris Wilson 	u32 enable_mask;
3821379ef82dSDaniel Vetter 	unsigned long irqflags;
3822a266c7d5SChris Wilson 
382338bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
382438bde180SChris Wilson 
382538bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
382638bde180SChris Wilson 	dev_priv->irq_mask =
382738bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
382838bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
382938bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
383038bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
383138bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
383238bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
383338bde180SChris Wilson 
383438bde180SChris Wilson 	enable_mask =
383538bde180SChris Wilson 		I915_ASLE_INTERRUPT |
383638bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
383738bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
383838bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
383938bde180SChris Wilson 		I915_USER_INTERRUPT;
384038bde180SChris Wilson 
3841a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
384220afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
384320afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
384420afbda2SDaniel Vetter 
3845a266c7d5SChris Wilson 		/* Enable in IER... */
3846a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3847a266c7d5SChris Wilson 		/* and unmask in IMR */
3848a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3849a266c7d5SChris Wilson 	}
3850a266c7d5SChris Wilson 
3851a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3852a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3853a266c7d5SChris Wilson 	POSTING_READ(IER);
3854a266c7d5SChris Wilson 
3855f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
385620afbda2SDaniel Vetter 
3857379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3858379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3859379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3860755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3861755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3862379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3863379ef82dSDaniel Vetter 
386420afbda2SDaniel Vetter 	return 0;
386520afbda2SDaniel Vetter }
386620afbda2SDaniel Vetter 
386790a72f87SVille Syrjälä /*
386890a72f87SVille Syrjälä  * Returns true when a page flip has completed.
386990a72f87SVille Syrjälä  */
387090a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
387190a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
387290a72f87SVille Syrjälä {
38732d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
387490a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
387590a72f87SVille Syrjälä 
38768d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
387790a72f87SVille Syrjälä 		return false;
387890a72f87SVille Syrjälä 
387990a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
388090a72f87SVille Syrjälä 		return false;
388190a72f87SVille Syrjälä 
388290a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
388390a72f87SVille Syrjälä 
388490a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
388590a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
388690a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
388790a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
388890a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
388990a72f87SVille Syrjälä 	 */
389090a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
389190a72f87SVille Syrjälä 		return false;
389290a72f87SVille Syrjälä 
389390a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
389490a72f87SVille Syrjälä 
389590a72f87SVille Syrjälä 	return true;
389690a72f87SVille Syrjälä }
389790a72f87SVille Syrjälä 
3898ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3899a266c7d5SChris Wilson {
390045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39012d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39028291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3903a266c7d5SChris Wilson 	unsigned long irqflags;
390438bde180SChris Wilson 	u32 flip_mask =
390538bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
390638bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
390738bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3908a266c7d5SChris Wilson 
3909a266c7d5SChris Wilson 	iir = I915_READ(IIR);
391038bde180SChris Wilson 	do {
391138bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
39128291ee90SChris Wilson 		bool blc_event = false;
3913a266c7d5SChris Wilson 
3914a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3915a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3916a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3917a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3918a266c7d5SChris Wilson 		 */
3919a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3920a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
392158174462SMika Kuoppala 			i915_handle_error(dev, false,
392258174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
392358174462SMika Kuoppala 					  iir);
3924a266c7d5SChris Wilson 
3925a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3926a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3927a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3928a266c7d5SChris Wilson 
392938bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3930a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3931a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
393238bde180SChris Wilson 				irq_received = true;
3933a266c7d5SChris Wilson 			}
3934a266c7d5SChris Wilson 		}
3935a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3936a266c7d5SChris Wilson 
3937a266c7d5SChris Wilson 		if (!irq_received)
3938a266c7d5SChris Wilson 			break;
3939a266c7d5SChris Wilson 
3940a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
394116c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
394216c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
394316c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3944a266c7d5SChris Wilson 
394538bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3946a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3947a266c7d5SChris Wilson 
3948a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3949a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3950a266c7d5SChris Wilson 
3951a266c7d5SChris Wilson 		for_each_pipe(pipe) {
395238bde180SChris Wilson 			int plane = pipe;
39533a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
395438bde180SChris Wilson 				plane = !plane;
39555e2032d4SVille Syrjälä 
395690a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
395790a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
395890a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3959a266c7d5SChris Wilson 
3960a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3961a266c7d5SChris Wilson 				blc_event = true;
39624356d586SDaniel Vetter 
39634356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3964277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
39652d9d2b0bSVille Syrjälä 
39662d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
39672d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3968fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
3969a266c7d5SChris Wilson 		}
3970a266c7d5SChris Wilson 
3971a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3972a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3973a266c7d5SChris Wilson 
3974a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3975a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3976a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3977a266c7d5SChris Wilson 		 * we would never get another interrupt.
3978a266c7d5SChris Wilson 		 *
3979a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3980a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3981a266c7d5SChris Wilson 		 * another one.
3982a266c7d5SChris Wilson 		 *
3983a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3984a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3985a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3986a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3987a266c7d5SChris Wilson 		 * stray interrupts.
3988a266c7d5SChris Wilson 		 */
398938bde180SChris Wilson 		ret = IRQ_HANDLED;
3990a266c7d5SChris Wilson 		iir = new_iir;
399138bde180SChris Wilson 	} while (iir & ~flip_mask);
3992a266c7d5SChris Wilson 
3993d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
39948291ee90SChris Wilson 
3995a266c7d5SChris Wilson 	return ret;
3996a266c7d5SChris Wilson }
3997a266c7d5SChris Wilson 
3998a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3999a266c7d5SChris Wilson {
40002d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4001a266c7d5SChris Wilson 	int pipe;
4002a266c7d5SChris Wilson 
40033ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
4004ac4c16c5SEgbert Eich 
4005a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
4006a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
4007a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4008a266c7d5SChris Wilson 	}
4009a266c7d5SChris Wilson 
401000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
401155b39755SChris Wilson 	for_each_pipe(pipe) {
401255b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4013a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
401455b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
401555b39755SChris Wilson 	}
4016a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4017a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4018a266c7d5SChris Wilson 
4019a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4020a266c7d5SChris Wilson }
4021a266c7d5SChris Wilson 
4022a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4023a266c7d5SChris Wilson {
40242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4025a266c7d5SChris Wilson 	int pipe;
4026a266c7d5SChris Wilson 
4027a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4028a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4029a266c7d5SChris Wilson 
4030a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4031a266c7d5SChris Wilson 	for_each_pipe(pipe)
4032a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4033a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4034a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4035a266c7d5SChris Wilson 	POSTING_READ(IER);
4036a266c7d5SChris Wilson }
4037a266c7d5SChris Wilson 
4038a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4039a266c7d5SChris Wilson {
40402d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4041bbba0a97SChris Wilson 	u32 enable_mask;
4042a266c7d5SChris Wilson 	u32 error_mask;
4043b79480baSDaniel Vetter 	unsigned long irqflags;
4044a266c7d5SChris Wilson 
4045a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4046bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4047adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4048bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4049bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4050bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4051bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4052bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4053bbba0a97SChris Wilson 
4054bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
405521ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
405621ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4057bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4058bbba0a97SChris Wilson 
4059bbba0a97SChris Wilson 	if (IS_G4X(dev))
4060bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4061a266c7d5SChris Wilson 
4062b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4063b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4064b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4065755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4066755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4067755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4068b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4069a266c7d5SChris Wilson 
4070a266c7d5SChris Wilson 	/*
4071a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4072a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4073a266c7d5SChris Wilson 	 */
4074a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4075a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4076a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4077a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4078a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4079a266c7d5SChris Wilson 	} else {
4080a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4081a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4082a266c7d5SChris Wilson 	}
4083a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4084a266c7d5SChris Wilson 
4085a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4086a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4087a266c7d5SChris Wilson 	POSTING_READ(IER);
4088a266c7d5SChris Wilson 
408920afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
409020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
409120afbda2SDaniel Vetter 
4092f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
409320afbda2SDaniel Vetter 
409420afbda2SDaniel Vetter 	return 0;
409520afbda2SDaniel Vetter }
409620afbda2SDaniel Vetter 
4097bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
409820afbda2SDaniel Vetter {
40992d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4100e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4101cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
410220afbda2SDaniel Vetter 	u32 hotplug_en;
410320afbda2SDaniel Vetter 
4104b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4105b5ea2d56SDaniel Vetter 
4106bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
4107bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4108bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4109adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
4110e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
4111cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
4112cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4113cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
4114a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
4115a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
4116a266c7d5SChris Wilson 		   seconds later.  So just do it once.
4117a266c7d5SChris Wilson 		*/
4118a266c7d5SChris Wilson 		if (IS_G4X(dev))
4119a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
412085fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4121a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4122a266c7d5SChris Wilson 
4123a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
4124a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4125a266c7d5SChris Wilson 	}
4126bac56d5bSEgbert Eich }
4127a266c7d5SChris Wilson 
4128ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4129a266c7d5SChris Wilson {
413045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41312d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4132a266c7d5SChris Wilson 	u32 iir, new_iir;
4133a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4134a266c7d5SChris Wilson 	unsigned long irqflags;
4135a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
413621ad8330SVille Syrjälä 	u32 flip_mask =
413721ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
413821ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4139a266c7d5SChris Wilson 
4140a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4141a266c7d5SChris Wilson 
4142a266c7d5SChris Wilson 	for (;;) {
4143501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
41442c8ba29fSChris Wilson 		bool blc_event = false;
41452c8ba29fSChris Wilson 
4146a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4147a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4148a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4149a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4150a266c7d5SChris Wilson 		 */
4151a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4152a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
415358174462SMika Kuoppala 			i915_handle_error(dev, false,
415458174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
415558174462SMika Kuoppala 					  iir);
4156a266c7d5SChris Wilson 
4157a266c7d5SChris Wilson 		for_each_pipe(pipe) {
4158a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4159a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4160a266c7d5SChris Wilson 
4161a266c7d5SChris Wilson 			/*
4162a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4163a266c7d5SChris Wilson 			 */
4164a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4165a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4166501e01d7SVille Syrjälä 				irq_received = true;
4167a266c7d5SChris Wilson 			}
4168a266c7d5SChris Wilson 		}
4169a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4170a266c7d5SChris Wilson 
4171a266c7d5SChris Wilson 		if (!irq_received)
4172a266c7d5SChris Wilson 			break;
4173a266c7d5SChris Wilson 
4174a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4175a266c7d5SChris Wilson 
4176a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
417716c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
417816c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4179a266c7d5SChris Wilson 
418021ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4181a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4182a266c7d5SChris Wilson 
4183a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4184a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
4185a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
4186a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
4187a266c7d5SChris Wilson 
4188a266c7d5SChris Wilson 		for_each_pipe(pipe) {
41892c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
419090a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
419190a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4192a266c7d5SChris Wilson 
4193a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4194a266c7d5SChris Wilson 				blc_event = true;
41954356d586SDaniel Vetter 
41964356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4197277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4198a266c7d5SChris Wilson 
41992d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
42002d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
4201fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
42022d9d2b0bSVille Syrjälä 		}
4203a266c7d5SChris Wilson 
4204a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4205a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4206a266c7d5SChris Wilson 
4207515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4208515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4209515ac2bbSDaniel Vetter 
4210a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4211a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4212a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4213a266c7d5SChris Wilson 		 * we would never get another interrupt.
4214a266c7d5SChris Wilson 		 *
4215a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4216a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4217a266c7d5SChris Wilson 		 * another one.
4218a266c7d5SChris Wilson 		 *
4219a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4220a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4221a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4222a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4223a266c7d5SChris Wilson 		 * stray interrupts.
4224a266c7d5SChris Wilson 		 */
4225a266c7d5SChris Wilson 		iir = new_iir;
4226a266c7d5SChris Wilson 	}
4227a266c7d5SChris Wilson 
4228d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
42292c8ba29fSChris Wilson 
4230a266c7d5SChris Wilson 	return ret;
4231a266c7d5SChris Wilson }
4232a266c7d5SChris Wilson 
4233a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4234a266c7d5SChris Wilson {
42352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4236a266c7d5SChris Wilson 	int pipe;
4237a266c7d5SChris Wilson 
4238a266c7d5SChris Wilson 	if (!dev_priv)
4239a266c7d5SChris Wilson 		return;
4240a266c7d5SChris Wilson 
42413ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
4242ac4c16c5SEgbert Eich 
4243a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4244a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4245a266c7d5SChris Wilson 
4246a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4247a266c7d5SChris Wilson 	for_each_pipe(pipe)
4248a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4249a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4250a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4251a266c7d5SChris Wilson 
4252a266c7d5SChris Wilson 	for_each_pipe(pipe)
4253a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4254a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4255a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4256a266c7d5SChris Wilson }
4257a266c7d5SChris Wilson 
42583ca1ccedSVille Syrjälä static void intel_hpd_irq_reenable(unsigned long data)
4259ac4c16c5SEgbert Eich {
42602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = (struct drm_i915_private *)data;
4261ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
4262ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4263ac4c16c5SEgbert Eich 	unsigned long irqflags;
4264ac4c16c5SEgbert Eich 	int i;
4265ac4c16c5SEgbert Eich 
4266ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4267ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4268ac4c16c5SEgbert Eich 		struct drm_connector *connector;
4269ac4c16c5SEgbert Eich 
4270ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4271ac4c16c5SEgbert Eich 			continue;
4272ac4c16c5SEgbert Eich 
4273ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4274ac4c16c5SEgbert Eich 
4275ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
4276ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
4277ac4c16c5SEgbert Eich 
4278ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4279ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4280ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4281ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
4282ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4283ac4c16c5SEgbert Eich 				if (!connector->polled)
4284ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4285ac4c16c5SEgbert Eich 			}
4286ac4c16c5SEgbert Eich 		}
4287ac4c16c5SEgbert Eich 	}
4288ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4289ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
4290ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4291ac4c16c5SEgbert Eich }
4292ac4c16c5SEgbert Eich 
4293f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
4294f71d4af4SJesse Barnes {
42958b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
42968b2e326dSChris Wilson 
42978b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
429899584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
4299c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4300a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
43018b2e326dSChris Wilson 
4302a6706b45SDeepak S 	/* Let's track the enabled rps events */
4303a6706b45SDeepak S 	dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4304a6706b45SDeepak S 
430599584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
430699584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
430761bac78eSDaniel Vetter 		    (unsigned long) dev);
43083ca1ccedSVille Syrjälä 	setup_timer(&dev_priv->hotplug_reenable_timer, intel_hpd_irq_reenable,
4309ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
431061bac78eSDaniel Vetter 
431197a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
43129ee32feaSDaniel Vetter 
43134cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
43144cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
43154cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
43164cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
4317f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4318f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4319391f75e2SVille Syrjälä 	} else {
4320391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4321391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4322f71d4af4SJesse Barnes 	}
4323f71d4af4SJesse Barnes 
4324c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
4325f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4326f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4327c2baf4b7SVille Syrjälä 	}
4328f71d4af4SJesse Barnes 
432943f328d7SVille Syrjälä 	if (IS_CHERRYVIEW(dev)) {
433043f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
433143f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
433243f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
433343f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
433443f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
433543f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
433643f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
433743f328d7SVille Syrjälä 	} else if (IS_VALLEYVIEW(dev)) {
43387e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
43397e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
43407e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
43417e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
43427e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
43437e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4344fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4345abd58f01SBen Widawsky 	} else if (IS_GEN8(dev)) {
4346abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4347abd58f01SBen Widawsky 		dev->driver->irq_preinstall = gen8_irq_preinstall;
4348abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4349abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4350abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4351abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4352abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4353f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4354f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4355f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
4356f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4357f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4358f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4359f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
436082a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4361f71d4af4SJesse Barnes 	} else {
4362c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
4363c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4364c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4365c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4366c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4367a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
4368a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4369a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4370a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4371a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
437220afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4373c2798b19SChris Wilson 		} else {
4374a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4375a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4376a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4377a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4378bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4379c2798b19SChris Wilson 		}
4380f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4381f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4382f71d4af4SJesse Barnes 	}
4383f71d4af4SJesse Barnes }
438420afbda2SDaniel Vetter 
438520afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
438620afbda2SDaniel Vetter {
438720afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
4388821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4389821450c6SEgbert Eich 	struct drm_connector *connector;
4390b5ea2d56SDaniel Vetter 	unsigned long irqflags;
4391821450c6SEgbert Eich 	int i;
439220afbda2SDaniel Vetter 
4393821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4394821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4395821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4396821450c6SEgbert Eich 	}
4397821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4398821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4399821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
4400821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
4401821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4402821450c6SEgbert Eich 	}
4403b5ea2d56SDaniel Vetter 
4404b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4405b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4406b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
440720afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
440820afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4409b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
441020afbda2SDaniel Vetter }
4411c67a470bSPaulo Zanoni 
44125d584b2eSPaulo Zanoni /* Disable interrupts so we can allow runtime PM. */
4413730488b2SPaulo Zanoni void intel_runtime_pm_disable_interrupts(struct drm_device *dev)
4414c67a470bSPaulo Zanoni {
4415c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4416c67a470bSPaulo Zanoni 
4417730488b2SPaulo Zanoni 	dev->driver->irq_uninstall(dev);
44185d584b2eSPaulo Zanoni 	dev_priv->pm.irqs_disabled = true;
4419c67a470bSPaulo Zanoni }
4420c67a470bSPaulo Zanoni 
44215d584b2eSPaulo Zanoni /* Restore interrupts so we can recover from runtime PM. */
4422730488b2SPaulo Zanoni void intel_runtime_pm_restore_interrupts(struct drm_device *dev)
4423c67a470bSPaulo Zanoni {
4424c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4425c67a470bSPaulo Zanoni 
44265d584b2eSPaulo Zanoni 	dev_priv->pm.irqs_disabled = false;
4427730488b2SPaulo Zanoni 	dev->driver->irq_preinstall(dev);
4428730488b2SPaulo Zanoni 	dev->driver->irq_postinstall(dev);
4429c67a470bSPaulo Zanoni }
4430