xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision f88d42f1d0272c46390434607b0f5de3889d157d)
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 
83036a4a7dSZhenyu Wang /* For display hotplug interrupt */
84995b6762SChris Wilson static void
85f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
86036a4a7dSZhenyu Wang {
874bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
884bc9d430SDaniel Vetter 
89c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
90c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
91c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.deimr &= ~mask;
92c67a470bSPaulo Zanoni 		return;
93c67a470bSPaulo Zanoni 	}
94c67a470bSPaulo Zanoni 
951ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
961ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
971ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
983143a2bfSChris Wilson 		POSTING_READ(DEIMR);
99036a4a7dSZhenyu Wang 	}
100036a4a7dSZhenyu Wang }
101036a4a7dSZhenyu Wang 
1020ff9800aSPaulo Zanoni static void
103f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
104036a4a7dSZhenyu Wang {
1054bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1064bc9d430SDaniel Vetter 
107c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
108c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
109c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.deimr |= mask;
110c67a470bSPaulo Zanoni 		return;
111c67a470bSPaulo Zanoni 	}
112c67a470bSPaulo Zanoni 
1131ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1141ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1151ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1163143a2bfSChris Wilson 		POSTING_READ(DEIMR);
117036a4a7dSZhenyu Wang 	}
118036a4a7dSZhenyu Wang }
119036a4a7dSZhenyu Wang 
12043eaea13SPaulo Zanoni /**
12143eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
12243eaea13SPaulo Zanoni  * @dev_priv: driver private
12343eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
12443eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
12543eaea13SPaulo Zanoni  */
12643eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
12743eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
12843eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
12943eaea13SPaulo Zanoni {
13043eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
13143eaea13SPaulo Zanoni 
132c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
133c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
134c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gtimr &= ~interrupt_mask;
135c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gtimr |= (~enabled_irq_mask &
136c67a470bSPaulo Zanoni 						interrupt_mask);
137c67a470bSPaulo Zanoni 		return;
138c67a470bSPaulo Zanoni 	}
139c67a470bSPaulo Zanoni 
14043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
14143eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
14243eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
14343eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
14443eaea13SPaulo Zanoni }
14543eaea13SPaulo Zanoni 
14643eaea13SPaulo Zanoni void ilk_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
14743eaea13SPaulo Zanoni {
14843eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
14943eaea13SPaulo Zanoni }
15043eaea13SPaulo Zanoni 
15143eaea13SPaulo Zanoni void ilk_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
15243eaea13SPaulo Zanoni {
15343eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
15443eaea13SPaulo Zanoni }
15543eaea13SPaulo Zanoni 
156edbfdb45SPaulo Zanoni /**
157edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
158edbfdb45SPaulo Zanoni   * @dev_priv: driver private
159edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
160edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
161edbfdb45SPaulo Zanoni   */
162edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
163edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
164edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
165edbfdb45SPaulo Zanoni {
166605cd25bSPaulo Zanoni 	uint32_t new_val;
167edbfdb45SPaulo Zanoni 
168edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
169edbfdb45SPaulo Zanoni 
170c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
171c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
172c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gen6_pmimr &= ~interrupt_mask;
173c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gen6_pmimr |= (~enabled_irq_mask &
174c67a470bSPaulo Zanoni 						     interrupt_mask);
175c67a470bSPaulo Zanoni 		return;
176c67a470bSPaulo Zanoni 	}
177c67a470bSPaulo Zanoni 
178605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
179f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
180f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
181f52ecbcfSPaulo Zanoni 
182605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
183605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
184605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
185edbfdb45SPaulo Zanoni 		POSTING_READ(GEN6_PMIMR);
186edbfdb45SPaulo Zanoni 	}
187f52ecbcfSPaulo Zanoni }
188edbfdb45SPaulo Zanoni 
189edbfdb45SPaulo Zanoni void snb_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
190edbfdb45SPaulo Zanoni {
191edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
192edbfdb45SPaulo Zanoni }
193edbfdb45SPaulo Zanoni 
194edbfdb45SPaulo Zanoni void snb_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
195edbfdb45SPaulo Zanoni {
196edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, 0);
197edbfdb45SPaulo Zanoni }
198edbfdb45SPaulo Zanoni 
1998664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
2008664281bSPaulo Zanoni {
2018664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2028664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2038664281bSPaulo Zanoni 	enum pipe pipe;
2048664281bSPaulo Zanoni 
2054bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2064bc9d430SDaniel Vetter 
2078664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2088664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2098664281bSPaulo Zanoni 
2108664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
2118664281bSPaulo Zanoni 			return false;
2128664281bSPaulo Zanoni 	}
2138664281bSPaulo Zanoni 
2148664281bSPaulo Zanoni 	return true;
2158664281bSPaulo Zanoni }
2168664281bSPaulo Zanoni 
2178664281bSPaulo Zanoni static bool cpt_can_enable_serr_int(struct drm_device *dev)
2188664281bSPaulo Zanoni {
2198664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2208664281bSPaulo Zanoni 	enum pipe pipe;
2218664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2228664281bSPaulo Zanoni 
223fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
224fee884edSDaniel Vetter 
2258664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2268664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2278664281bSPaulo Zanoni 
2288664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
2298664281bSPaulo Zanoni 			return false;
2308664281bSPaulo Zanoni 	}
2318664281bSPaulo Zanoni 
2328664281bSPaulo Zanoni 	return true;
2338664281bSPaulo Zanoni }
2348664281bSPaulo Zanoni 
2352d9d2b0bSVille Syrjälä static void i9xx_clear_fifo_underrun(struct drm_device *dev, enum pipe pipe)
2362d9d2b0bSVille Syrjälä {
2372d9d2b0bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
2382d9d2b0bSVille Syrjälä 	u32 reg = PIPESTAT(pipe);
2392d9d2b0bSVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
2402d9d2b0bSVille Syrjälä 
2412d9d2b0bSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
2422d9d2b0bSVille Syrjälä 
2432d9d2b0bSVille Syrjälä 	I915_WRITE(reg, pipestat | PIPE_FIFO_UNDERRUN_STATUS);
2442d9d2b0bSVille Syrjälä 	POSTING_READ(reg);
2452d9d2b0bSVille Syrjälä }
2462d9d2b0bSVille Syrjälä 
2478664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
2488664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
2498664281bSPaulo Zanoni {
2508664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2518664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
2528664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
2538664281bSPaulo Zanoni 
2548664281bSPaulo Zanoni 	if (enable)
2558664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
2568664281bSPaulo Zanoni 	else
2578664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
2588664281bSPaulo Zanoni }
2598664281bSPaulo Zanoni 
2608664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
2617336df65SDaniel Vetter 						  enum pipe pipe, bool enable)
2628664281bSPaulo Zanoni {
2638664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2648664281bSPaulo Zanoni 	if (enable) {
2657336df65SDaniel Vetter 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
2667336df65SDaniel Vetter 
2678664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
2688664281bSPaulo Zanoni 			return;
2698664281bSPaulo Zanoni 
2708664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
2718664281bSPaulo Zanoni 	} else {
2727336df65SDaniel Vetter 		bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
2737336df65SDaniel Vetter 
2747336df65SDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
2758664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
2767336df65SDaniel Vetter 
2777336df65SDaniel Vetter 		if (!was_enabled &&
2787336df65SDaniel Vetter 		    (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
2797336df65SDaniel Vetter 			DRM_DEBUG_KMS("uncleared fifo underrun on pipe %c\n",
2807336df65SDaniel Vetter 				      pipe_name(pipe));
2817336df65SDaniel Vetter 		}
2828664281bSPaulo Zanoni 	}
2838664281bSPaulo Zanoni }
2848664281bSPaulo Zanoni 
28538d83c96SDaniel Vetter static void broadwell_set_fifo_underrun_reporting(struct drm_device *dev,
28638d83c96SDaniel Vetter 						  enum pipe pipe, bool enable)
28738d83c96SDaniel Vetter {
28838d83c96SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
28938d83c96SDaniel Vetter 
29038d83c96SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
29138d83c96SDaniel Vetter 
29238d83c96SDaniel Vetter 	if (enable)
29338d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_FIFO_UNDERRUN;
29438d83c96SDaniel Vetter 	else
29538d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_FIFO_UNDERRUN;
29638d83c96SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
29738d83c96SDaniel Vetter 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
29838d83c96SDaniel Vetter }
29938d83c96SDaniel Vetter 
300fee884edSDaniel Vetter /**
301fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
302fee884edSDaniel Vetter  * @dev_priv: driver private
303fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
304fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
305fee884edSDaniel Vetter  */
306fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
307fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
308fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
309fee884edSDaniel Vetter {
310fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
311fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
312fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
313fee884edSDaniel Vetter 
314fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
315fee884edSDaniel Vetter 
316c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled &&
317c67a470bSPaulo Zanoni 	    (interrupt_mask & SDE_HOTPLUG_MASK_CPT)) {
318c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
319c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr &= ~interrupt_mask;
320c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr |= (~enabled_irq_mask &
321c67a470bSPaulo Zanoni 						 interrupt_mask);
322c67a470bSPaulo Zanoni 		return;
323c67a470bSPaulo Zanoni 	}
324c67a470bSPaulo Zanoni 
325fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
326fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
327fee884edSDaniel Vetter }
328fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
329fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
330fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
331fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
332fee884edSDaniel Vetter 
333de28075dSDaniel Vetter static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
334de28075dSDaniel Vetter 					    enum transcoder pch_transcoder,
3358664281bSPaulo Zanoni 					    bool enable)
3368664281bSPaulo Zanoni {
3378664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
338de28075dSDaniel Vetter 	uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
339de28075dSDaniel Vetter 		       SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
3408664281bSPaulo Zanoni 
3418664281bSPaulo Zanoni 	if (enable)
342fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
3438664281bSPaulo Zanoni 	else
344fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
3458664281bSPaulo Zanoni }
3468664281bSPaulo Zanoni 
3478664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
3488664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
3498664281bSPaulo Zanoni 					    bool enable)
3508664281bSPaulo Zanoni {
3518664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3528664281bSPaulo Zanoni 
3538664281bSPaulo Zanoni 	if (enable) {
3541dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
3551dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
3561dd246fbSDaniel Vetter 
3578664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
3588664281bSPaulo Zanoni 			return;
3598664281bSPaulo Zanoni 
360fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3618664281bSPaulo Zanoni 	} else {
3621dd246fbSDaniel Vetter 		uint32_t tmp = I915_READ(SERR_INT);
3631dd246fbSDaniel Vetter 		bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
3641dd246fbSDaniel Vetter 
3651dd246fbSDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
366fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3671dd246fbSDaniel Vetter 
3681dd246fbSDaniel Vetter 		if (!was_enabled &&
3691dd246fbSDaniel Vetter 		    (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
3701dd246fbSDaniel Vetter 			DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
3711dd246fbSDaniel Vetter 				      transcoder_name(pch_transcoder));
3721dd246fbSDaniel Vetter 		}
3738664281bSPaulo Zanoni 	}
3748664281bSPaulo Zanoni }
3758664281bSPaulo Zanoni 
3768664281bSPaulo Zanoni /**
3778664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
3788664281bSPaulo Zanoni  * @dev: drm device
3798664281bSPaulo Zanoni  * @pipe: pipe
3808664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
3818664281bSPaulo Zanoni  *
3828664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
3838664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
3848664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
3858664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
3868664281bSPaulo Zanoni  * bit for all the pipes.
3878664281bSPaulo Zanoni  *
3888664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
3898664281bSPaulo Zanoni  */
390*f88d42f1SImre Deak bool __intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
3918664281bSPaulo Zanoni 					     enum pipe pipe, bool enable)
3928664281bSPaulo Zanoni {
3938664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3948664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
3958664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3968664281bSPaulo Zanoni 	bool ret;
3978664281bSPaulo Zanoni 
3988664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
3998664281bSPaulo Zanoni 
4008664281bSPaulo Zanoni 	if (enable == ret)
4018664281bSPaulo Zanoni 		goto done;
4028664281bSPaulo Zanoni 
4038664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
4048664281bSPaulo Zanoni 
4052d9d2b0bSVille Syrjälä 	if (enable && (INTEL_INFO(dev)->gen < 5 || IS_VALLEYVIEW(dev)))
4062d9d2b0bSVille Syrjälä 		i9xx_clear_fifo_underrun(dev, pipe);
4072d9d2b0bSVille Syrjälä 	else if (IS_GEN5(dev) || IS_GEN6(dev))
4088664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
4098664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
4107336df65SDaniel Vetter 		ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
41138d83c96SDaniel Vetter 	else if (IS_GEN8(dev))
41238d83c96SDaniel Vetter 		broadwell_set_fifo_underrun_reporting(dev, pipe, enable);
4138664281bSPaulo Zanoni 
4148664281bSPaulo Zanoni done:
415*f88d42f1SImre Deak 	return ret;
416*f88d42f1SImre Deak }
417*f88d42f1SImre Deak 
418*f88d42f1SImre Deak bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
419*f88d42f1SImre Deak 					   enum pipe pipe, bool enable)
420*f88d42f1SImre Deak {
421*f88d42f1SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
422*f88d42f1SImre Deak 	unsigned long flags;
423*f88d42f1SImre Deak 	bool ret;
424*f88d42f1SImre Deak 
425*f88d42f1SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
426*f88d42f1SImre Deak 	ret = __intel_set_cpu_fifo_underrun_reporting(dev, pipe, enable);
4278664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
428*f88d42f1SImre Deak 
4298664281bSPaulo Zanoni 	return ret;
4308664281bSPaulo Zanoni }
4318664281bSPaulo Zanoni 
43291d181ddSImre Deak static bool __cpu_fifo_underrun_reporting_enabled(struct drm_device *dev,
43391d181ddSImre Deak 						  enum pipe pipe)
43491d181ddSImre Deak {
43591d181ddSImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
43691d181ddSImre Deak 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
43791d181ddSImre Deak 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
43891d181ddSImre Deak 
43991d181ddSImre Deak 	return !intel_crtc->cpu_fifo_underrun_disabled;
44091d181ddSImre Deak }
44191d181ddSImre Deak 
4428664281bSPaulo Zanoni /**
4438664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
4448664281bSPaulo Zanoni  * @dev: drm device
4458664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
4468664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
4478664281bSPaulo Zanoni  *
4488664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
4498664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
4508664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
4518664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
4528664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
4538664281bSPaulo Zanoni  *
4548664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
4558664281bSPaulo Zanoni  */
4568664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
4578664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
4588664281bSPaulo Zanoni 					   bool enable)
4598664281bSPaulo Zanoni {
4608664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
461de28075dSDaniel Vetter 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
462de28075dSDaniel Vetter 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4638664281bSPaulo Zanoni 	unsigned long flags;
4648664281bSPaulo Zanoni 	bool ret;
4658664281bSPaulo Zanoni 
466de28075dSDaniel Vetter 	/*
467de28075dSDaniel Vetter 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
468de28075dSDaniel Vetter 	 * has only one pch transcoder A that all pipes can use. To avoid racy
469de28075dSDaniel Vetter 	 * pch transcoder -> pipe lookups from interrupt code simply store the
470de28075dSDaniel Vetter 	 * underrun statistics in crtc A. Since we never expose this anywhere
471de28075dSDaniel Vetter 	 * nor use it outside of the fifo underrun code here using the "wrong"
472de28075dSDaniel Vetter 	 * crtc on LPT won't cause issues.
473de28075dSDaniel Vetter 	 */
4748664281bSPaulo Zanoni 
4758664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
4768664281bSPaulo Zanoni 
4778664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
4788664281bSPaulo Zanoni 
4798664281bSPaulo Zanoni 	if (enable == ret)
4808664281bSPaulo Zanoni 		goto done;
4818664281bSPaulo Zanoni 
4828664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
4838664281bSPaulo Zanoni 
4848664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
485de28075dSDaniel Vetter 		ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4868664281bSPaulo Zanoni 	else
4878664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4888664281bSPaulo Zanoni 
4898664281bSPaulo Zanoni done:
4908664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
4918664281bSPaulo Zanoni 	return ret;
4928664281bSPaulo Zanoni }
4938664281bSPaulo Zanoni 
4948664281bSPaulo Zanoni 
495b5ea642aSDaniel Vetter static void
496755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
497755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
4987c463586SKeith Packard {
4999db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
500755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5017c463586SKeith Packard 
502b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
503b79480baSDaniel Vetter 
504755e9019SImre Deak 	if (WARN_ON_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
505755e9019SImre Deak 	                 status_mask & ~PIPESTAT_INT_STATUS_MASK))
506755e9019SImre Deak 		return;
507755e9019SImre Deak 
508755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
50946c06a30SVille Syrjälä 		return;
51046c06a30SVille Syrjälä 
51191d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
51291d181ddSImre Deak 
5137c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
514755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
51546c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5163143a2bfSChris Wilson 	POSTING_READ(reg);
5177c463586SKeith Packard }
5187c463586SKeith Packard 
519b5ea642aSDaniel Vetter static void
520755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
521755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5227c463586SKeith Packard {
5239db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
524755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5257c463586SKeith Packard 
526b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
527b79480baSDaniel Vetter 
528755e9019SImre Deak 	if (WARN_ON_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
529755e9019SImre Deak 	                 status_mask & ~PIPESTAT_INT_STATUS_MASK))
53046c06a30SVille Syrjälä 		return;
53146c06a30SVille Syrjälä 
532755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
533755e9019SImre Deak 		return;
534755e9019SImre Deak 
53591d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
53691d181ddSImre Deak 
537755e9019SImre Deak 	pipestat &= ~enable_mask;
53846c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5393143a2bfSChris Wilson 	POSTING_READ(reg);
5407c463586SKeith Packard }
5417c463586SKeith Packard 
54210c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
54310c59c51SImre Deak {
54410c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
54510c59c51SImre Deak 
54610c59c51SImre Deak 	/*
54710c59c51SImre Deak 	 * On pipe A we don't support the PSR interrupt yet, on pipe B the
54810c59c51SImre Deak 	 * same bit MBZ.
54910c59c51SImre Deak 	 */
55010c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
55110c59c51SImre Deak 		return 0;
55210c59c51SImre Deak 
55310c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
55410c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
55510c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
55610c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
55710c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
55810c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
55910c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
56010c59c51SImre Deak 
56110c59c51SImre Deak 	return enable_mask;
56210c59c51SImre Deak }
56310c59c51SImre Deak 
564755e9019SImre Deak void
565755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
566755e9019SImre Deak 		     u32 status_mask)
567755e9019SImre Deak {
568755e9019SImre Deak 	u32 enable_mask;
569755e9019SImre Deak 
57010c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
57110c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
57210c59c51SImre Deak 							   status_mask);
57310c59c51SImre Deak 	else
574755e9019SImre Deak 		enable_mask = status_mask << 16;
575755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
576755e9019SImre Deak }
577755e9019SImre Deak 
578755e9019SImre Deak void
579755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
580755e9019SImre Deak 		      u32 status_mask)
581755e9019SImre Deak {
582755e9019SImre Deak 	u32 enable_mask;
583755e9019SImre Deak 
58410c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
58510c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
58610c59c51SImre Deak 							   status_mask);
58710c59c51SImre Deak 	else
588755e9019SImre Deak 		enable_mask = status_mask << 16;
589755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
590755e9019SImre Deak }
591755e9019SImre Deak 
592c0e09200SDave Airlie /**
593f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
59401c66889SZhao Yakui  */
595f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
59601c66889SZhao Yakui {
5971ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
5981ec14ad3SChris Wilson 	unsigned long irqflags;
5991ec14ad3SChris Wilson 
600f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
601f49e38ddSJani Nikula 		return;
602f49e38ddSJani Nikula 
6031ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
60401c66889SZhao Yakui 
605755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
606a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
6073b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
608755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6091ec14ad3SChris Wilson 
6101ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
61101c66889SZhao Yakui }
61201c66889SZhao Yakui 
61301c66889SZhao Yakui /**
6140a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
6150a3e67a4SJesse Barnes  * @dev: DRM device
6160a3e67a4SJesse Barnes  * @pipe: pipe to check
6170a3e67a4SJesse Barnes  *
6180a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
6190a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
6200a3e67a4SJesse Barnes  * before reading such registers if unsure.
6210a3e67a4SJesse Barnes  */
6220a3e67a4SJesse Barnes static int
6230a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
6240a3e67a4SJesse Barnes {
6250a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
626702e7a56SPaulo Zanoni 
627a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
628a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
629a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
630a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
63171f8ba6bSPaulo Zanoni 
632a01025afSDaniel Vetter 		return intel_crtc->active;
633a01025afSDaniel Vetter 	} else {
634a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
635a01025afSDaniel Vetter 	}
6360a3e67a4SJesse Barnes }
6370a3e67a4SJesse Barnes 
6384cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
6394cdb83ecSVille Syrjälä {
6404cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
6414cdb83ecSVille Syrjälä 	return 0;
6424cdb83ecSVille Syrjälä }
6434cdb83ecSVille Syrjälä 
64442f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
64542f52ef8SKeith Packard  * we use as a pipe index
64642f52ef8SKeith Packard  */
647f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
6480a3e67a4SJesse Barnes {
6490a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6500a3e67a4SJesse Barnes 	unsigned long high_frame;
6510a3e67a4SJesse Barnes 	unsigned long low_frame;
652391f75e2SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start;
6530a3e67a4SJesse Barnes 
6540a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
65544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
6569db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
6570a3e67a4SJesse Barnes 		return 0;
6580a3e67a4SJesse Barnes 	}
6590a3e67a4SJesse Barnes 
660391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
661391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
662391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
663391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
664391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
665391f75e2SVille Syrjälä 
666391f75e2SVille Syrjälä 		vbl_start = mode->crtc_vblank_start * mode->crtc_htotal;
667391f75e2SVille Syrjälä 	} else {
668391f75e2SVille Syrjälä 		enum transcoder cpu_transcoder =
669391f75e2SVille Syrjälä 			intel_pipe_to_cpu_transcoder(dev_priv, pipe);
670391f75e2SVille Syrjälä 		u32 htotal;
671391f75e2SVille Syrjälä 
672391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
673391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
674391f75e2SVille Syrjälä 
675391f75e2SVille Syrjälä 		vbl_start *= htotal;
676391f75e2SVille Syrjälä 	}
677391f75e2SVille Syrjälä 
6789db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6799db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6805eddb70bSChris Wilson 
6810a3e67a4SJesse Barnes 	/*
6820a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6830a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6840a3e67a4SJesse Barnes 	 * register.
6850a3e67a4SJesse Barnes 	 */
6860a3e67a4SJesse Barnes 	do {
6875eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
688391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
6895eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
6900a3e67a4SJesse Barnes 	} while (high1 != high2);
6910a3e67a4SJesse Barnes 
6925eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
693391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6945eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
695391f75e2SVille Syrjälä 
696391f75e2SVille Syrjälä 	/*
697391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
698391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
699391f75e2SVille Syrjälä 	 * counter against vblank start.
700391f75e2SVille Syrjälä 	 */
701edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7020a3e67a4SJesse Barnes }
7030a3e67a4SJesse Barnes 
704f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
7059880b7a5SJesse Barnes {
7069880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7079db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
7089880b7a5SJesse Barnes 
7099880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
71044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
7119db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7129880b7a5SJesse Barnes 		return 0;
7139880b7a5SJesse Barnes 	}
7149880b7a5SJesse Barnes 
7159880b7a5SJesse Barnes 	return I915_READ(reg);
7169880b7a5SJesse Barnes }
7179880b7a5SJesse Barnes 
718ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
719ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
720ad3543edSMario Kleiner #define __raw_i915_read16(dev_priv__, reg__) readw((dev_priv__)->regs + (reg__))
721ad3543edSMario Kleiner 
722095163baSVille Syrjälä static bool ilk_pipe_in_vblank_locked(struct drm_device *dev, enum pipe pipe)
72354ddcbd2SVille Syrjälä {
72454ddcbd2SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
72554ddcbd2SVille Syrjälä 	uint32_t status;
72654ddcbd2SVille Syrjälä 
727095163baSVille Syrjälä 	if (INTEL_INFO(dev)->gen < 7) {
72854ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
72954ddcbd2SVille Syrjälä 			DE_PIPEA_VBLANK :
73054ddcbd2SVille Syrjälä 			DE_PIPEB_VBLANK;
73154ddcbd2SVille Syrjälä 	} else {
73254ddcbd2SVille Syrjälä 		switch (pipe) {
73354ddcbd2SVille Syrjälä 		default:
73454ddcbd2SVille Syrjälä 		case PIPE_A:
73554ddcbd2SVille Syrjälä 			status = DE_PIPEA_VBLANK_IVB;
73654ddcbd2SVille Syrjälä 			break;
73754ddcbd2SVille Syrjälä 		case PIPE_B:
73854ddcbd2SVille Syrjälä 			status = DE_PIPEB_VBLANK_IVB;
73954ddcbd2SVille Syrjälä 			break;
74054ddcbd2SVille Syrjälä 		case PIPE_C:
74154ddcbd2SVille Syrjälä 			status = DE_PIPEC_VBLANK_IVB;
74254ddcbd2SVille Syrjälä 			break;
74354ddcbd2SVille Syrjälä 		}
74454ddcbd2SVille Syrjälä 	}
745ad3543edSMario Kleiner 
746095163baSVille Syrjälä 	return __raw_i915_read32(dev_priv, DEISR) & status;
74754ddcbd2SVille Syrjälä }
74854ddcbd2SVille Syrjälä 
749f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
750abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
751abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
7520af7e4dfSMario Kleiner {
753c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
754c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
755c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
7573aa18df8SVille Syrjälä 	int position;
7580af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
7590af7e4dfSMario Kleiner 	bool in_vbl = true;
7600af7e4dfSMario Kleiner 	int ret = 0;
761ad3543edSMario Kleiner 	unsigned long irqflags;
7620af7e4dfSMario Kleiner 
763c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
7640af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
7659db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7660af7e4dfSMario Kleiner 		return 0;
7670af7e4dfSMario Kleiner 	}
7680af7e4dfSMario Kleiner 
769c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
770c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
771c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
772c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7730af7e4dfSMario Kleiner 
774d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
775d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
776d31faf65SVille Syrjälä 		vbl_end /= 2;
777d31faf65SVille Syrjälä 		vtotal /= 2;
778d31faf65SVille Syrjälä 	}
779d31faf65SVille Syrjälä 
780c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
781c2baf4b7SVille Syrjälä 
782ad3543edSMario Kleiner 	/*
783ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
784ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
785ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
786ad3543edSMario Kleiner 	 */
787ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
788ad3543edSMario Kleiner 
789ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
790ad3543edSMario Kleiner 
791ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
792ad3543edSMario Kleiner 	if (stime)
793ad3543edSMario Kleiner 		*stime = ktime_get();
794ad3543edSMario Kleiner 
7957c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7960af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
7970af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
7980af7e4dfSMario Kleiner 		 */
7997c06b08aSVille Syrjälä 		if (IS_GEN2(dev))
800ad3543edSMario Kleiner 			position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
8017c06b08aSVille Syrjälä 		else
802ad3543edSMario Kleiner 			position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
80354ddcbd2SVille Syrjälä 
804095163baSVille Syrjälä 		if (HAS_PCH_SPLIT(dev)) {
80554ddcbd2SVille Syrjälä 			/*
80654ddcbd2SVille Syrjälä 			 * The scanline counter increments at the leading edge
80754ddcbd2SVille Syrjälä 			 * of hsync, ie. it completely misses the active portion
80854ddcbd2SVille Syrjälä 			 * of the line. Fix up the counter at both edges of vblank
80954ddcbd2SVille Syrjälä 			 * to get a more accurate picture whether we're in vblank
81054ddcbd2SVille Syrjälä 			 * or not.
81154ddcbd2SVille Syrjälä 			 */
812095163baSVille Syrjälä 			in_vbl = ilk_pipe_in_vblank_locked(dev, pipe);
81354ddcbd2SVille Syrjälä 			if ((in_vbl && position == vbl_start - 1) ||
81454ddcbd2SVille Syrjälä 			    (!in_vbl && position == vbl_end - 1))
81554ddcbd2SVille Syrjälä 				position = (position + 1) % vtotal;
8160af7e4dfSMario Kleiner 		} else {
817095163baSVille Syrjälä 			/*
818095163baSVille Syrjälä 			 * ISR vblank status bits don't work the way we'd want
819095163baSVille Syrjälä 			 * them to work on non-PCH platforms (for
820095163baSVille Syrjälä 			 * ilk_pipe_in_vblank_locked()), and there doesn't
821095163baSVille Syrjälä 			 * appear any other way to determine if we're currently
822095163baSVille Syrjälä 			 * in vblank.
823095163baSVille Syrjälä 			 *
824095163baSVille Syrjälä 			 * Instead let's assume that we're already in vblank if
825095163baSVille Syrjälä 			 * we got called from the vblank interrupt and the
826095163baSVille Syrjälä 			 * scanline counter value indicates that we're on the
827095163baSVille Syrjälä 			 * line just prior to vblank start. This should result
828095163baSVille Syrjälä 			 * in the correct answer, unless the vblank interrupt
829095163baSVille Syrjälä 			 * delivery really got delayed for almost exactly one
830095163baSVille Syrjälä 			 * full frame/field.
831095163baSVille Syrjälä 			 */
832095163baSVille Syrjälä 			if (flags & DRM_CALLED_FROM_VBLIRQ &&
833095163baSVille Syrjälä 			    position == vbl_start - 1) {
834095163baSVille Syrjälä 				position = (position + 1) % vtotal;
835095163baSVille Syrjälä 
836095163baSVille Syrjälä 				/* Signal this correction as "applied". */
837095163baSVille Syrjälä 				ret |= 0x8;
838095163baSVille Syrjälä 			}
839095163baSVille Syrjälä 		}
840095163baSVille Syrjälä 	} else {
8410af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8420af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8430af7e4dfSMario Kleiner 		 * scanout position.
8440af7e4dfSMario Kleiner 		 */
845ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8460af7e4dfSMario Kleiner 
8473aa18df8SVille Syrjälä 		/* convert to pixel counts */
8483aa18df8SVille Syrjälä 		vbl_start *= htotal;
8493aa18df8SVille Syrjälä 		vbl_end *= htotal;
8503aa18df8SVille Syrjälä 		vtotal *= htotal;
8513aa18df8SVille Syrjälä 	}
8523aa18df8SVille Syrjälä 
853ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
854ad3543edSMario Kleiner 	if (etime)
855ad3543edSMario Kleiner 		*etime = ktime_get();
856ad3543edSMario Kleiner 
857ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
858ad3543edSMario Kleiner 
859ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
860ad3543edSMario Kleiner 
8613aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8623aa18df8SVille Syrjälä 
8633aa18df8SVille Syrjälä 	/*
8643aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8653aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8663aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8673aa18df8SVille Syrjälä 	 * up since vbl_end.
8683aa18df8SVille Syrjälä 	 */
8693aa18df8SVille Syrjälä 	if (position >= vbl_start)
8703aa18df8SVille Syrjälä 		position -= vbl_end;
8713aa18df8SVille Syrjälä 	else
8723aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8733aa18df8SVille Syrjälä 
8747c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8753aa18df8SVille Syrjälä 		*vpos = position;
8763aa18df8SVille Syrjälä 		*hpos = 0;
8773aa18df8SVille Syrjälä 	} else {
8780af7e4dfSMario Kleiner 		*vpos = position / htotal;
8790af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8800af7e4dfSMario Kleiner 	}
8810af7e4dfSMario Kleiner 
8820af7e4dfSMario Kleiner 	/* In vblank? */
8830af7e4dfSMario Kleiner 	if (in_vbl)
8840af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
8850af7e4dfSMario Kleiner 
8860af7e4dfSMario Kleiner 	return ret;
8870af7e4dfSMario Kleiner }
8880af7e4dfSMario Kleiner 
889f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
8900af7e4dfSMario Kleiner 			      int *max_error,
8910af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
8920af7e4dfSMario Kleiner 			      unsigned flags)
8930af7e4dfSMario Kleiner {
8944041b853SChris Wilson 	struct drm_crtc *crtc;
8950af7e4dfSMario Kleiner 
8967eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
8974041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
8980af7e4dfSMario Kleiner 		return -EINVAL;
8990af7e4dfSMario Kleiner 	}
9000af7e4dfSMario Kleiner 
9010af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9024041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9034041b853SChris Wilson 	if (crtc == NULL) {
9044041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9054041b853SChris Wilson 		return -EINVAL;
9064041b853SChris Wilson 	}
9074041b853SChris Wilson 
9084041b853SChris Wilson 	if (!crtc->enabled) {
9094041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
9104041b853SChris Wilson 		return -EBUSY;
9114041b853SChris Wilson 	}
9120af7e4dfSMario Kleiner 
9130af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9144041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9154041b853SChris Wilson 						     vblank_time, flags,
9167da903efSVille Syrjälä 						     crtc,
9177da903efSVille Syrjälä 						     &to_intel_crtc(crtc)->config.adjusted_mode);
9180af7e4dfSMario Kleiner }
9190af7e4dfSMario Kleiner 
92067c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
92167c347ffSJani Nikula 				struct drm_connector *connector)
922321a1b30SEgbert Eich {
923321a1b30SEgbert Eich 	enum drm_connector_status old_status;
924321a1b30SEgbert Eich 
925321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
926321a1b30SEgbert Eich 	old_status = connector->status;
927321a1b30SEgbert Eich 
928321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
92967c347ffSJani Nikula 	if (old_status == connector->status)
93067c347ffSJani Nikula 		return false;
93167c347ffSJani Nikula 
93267c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
933321a1b30SEgbert Eich 		      connector->base.id,
934321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
93567c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
93667c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
93767c347ffSJani Nikula 
93867c347ffSJani Nikula 	return true;
939321a1b30SEgbert Eich }
940321a1b30SEgbert Eich 
9415ca58282SJesse Barnes /*
9425ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
9435ca58282SJesse Barnes  */
944ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
945ac4c16c5SEgbert Eich 
9465ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
9475ca58282SJesse Barnes {
9485ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
9495ca58282SJesse Barnes 						    hotplug_work);
9505ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
951c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
952cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
953cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
954cd569aedSEgbert Eich 	struct drm_connector *connector;
955cd569aedSEgbert Eich 	unsigned long irqflags;
956cd569aedSEgbert Eich 	bool hpd_disabled = false;
957321a1b30SEgbert Eich 	bool changed = false;
958142e2398SEgbert Eich 	u32 hpd_event_bits;
9595ca58282SJesse Barnes 
96052d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
96152d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
96252d7ecedSDaniel Vetter 		return;
96352d7ecedSDaniel Vetter 
964a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
965e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
966e67189abSJesse Barnes 
967cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
968142e2398SEgbert Eich 
969142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
970142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
971cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
972cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
973cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
974cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
975cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
976cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
977cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
978cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
979cd569aedSEgbert Eich 				drm_get_connector_name(connector));
980cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
981cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
982cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
983cd569aedSEgbert Eich 			hpd_disabled = true;
984cd569aedSEgbert Eich 		}
985142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
986142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
987142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
988142e2398SEgbert Eich 		}
989cd569aedSEgbert Eich 	}
990cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
991cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
992cd569aedSEgbert Eich 	  * some connectors */
993ac4c16c5SEgbert Eich 	if (hpd_disabled) {
994cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
995ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
996ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
997ac4c16c5SEgbert Eich 	}
998cd569aedSEgbert Eich 
999cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1000cd569aedSEgbert Eich 
1001321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
1002321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
1003321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
1004321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1005cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
1006cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
1007321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
1008321a1b30SEgbert Eich 				changed = true;
1009321a1b30SEgbert Eich 		}
1010321a1b30SEgbert Eich 	}
101140ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
101240ee3381SKeith Packard 
1013321a1b30SEgbert Eich 	if (changed)
1014321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
10155ca58282SJesse Barnes }
10165ca58282SJesse Barnes 
10173ca1ccedSVille Syrjälä static void intel_hpd_irq_uninstall(struct drm_i915_private *dev_priv)
10183ca1ccedSVille Syrjälä {
10193ca1ccedSVille Syrjälä 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
10203ca1ccedSVille Syrjälä }
10213ca1ccedSVille Syrjälä 
1022d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
1023f97108d1SJesse Barnes {
1024f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
1025b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
10269270388eSDaniel Vetter 	u8 new_delay;
10279270388eSDaniel Vetter 
1028d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1029f97108d1SJesse Barnes 
103073edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
103173edd18fSDaniel Vetter 
103220e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
10339270388eSDaniel Vetter 
10347648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1035b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1036b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1037f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1038f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1039f97108d1SJesse Barnes 
1040f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1041b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
104220e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
104320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
104420e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
104520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1046b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
104720e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
104820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
104920e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
105020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1051f97108d1SJesse Barnes 	}
1052f97108d1SJesse Barnes 
10537648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
105420e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1055f97108d1SJesse Barnes 
1056d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
10579270388eSDaniel Vetter 
1058f97108d1SJesse Barnes 	return;
1059f97108d1SJesse Barnes }
1060f97108d1SJesse Barnes 
1061549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
1062549f7365SChris Wilson 			struct intel_ring_buffer *ring)
1063549f7365SChris Wilson {
1064475553deSChris Wilson 	if (ring->obj == NULL)
1065475553deSChris Wilson 		return;
1066475553deSChris Wilson 
1067814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
10689862e600SChris Wilson 
1069549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
107010cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
1071549f7365SChris Wilson }
1072549f7365SChris Wilson 
107376c3552fSDeepak S void gen6_set_pm_mask(struct drm_i915_private *dev_priv,
107427544369SDeepak S 			     u32 pm_iir, int new_delay)
107527544369SDeepak S {
107627544369SDeepak S 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
107727544369SDeepak S 		if (new_delay >= dev_priv->rps.max_delay) {
107827544369SDeepak S 			/* Mask UP THRESHOLD Interrupts */
107927544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
108027544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) |
108127544369SDeepak S 				   GEN6_PM_RP_UP_THRESHOLD);
108227544369SDeepak S 			dev_priv->rps.rp_up_masked = true;
108327544369SDeepak S 		}
108427544369SDeepak S 		if (dev_priv->rps.rp_down_masked) {
108527544369SDeepak S 			/* UnMask DOWN THRESHOLD Interrupts */
108627544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
108727544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) &
108827544369SDeepak S 				   ~GEN6_PM_RP_DOWN_THRESHOLD);
108927544369SDeepak S 			dev_priv->rps.rp_down_masked = false;
109027544369SDeepak S 		}
109127544369SDeepak S 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
109227544369SDeepak S 		if (new_delay <= dev_priv->rps.min_delay) {
109327544369SDeepak S 			/* Mask DOWN THRESHOLD Interrupts */
109427544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
109527544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) |
109627544369SDeepak S 				   GEN6_PM_RP_DOWN_THRESHOLD);
109727544369SDeepak S 			dev_priv->rps.rp_down_masked = true;
109827544369SDeepak S 		}
109927544369SDeepak S 
110027544369SDeepak S 		if (dev_priv->rps.rp_up_masked) {
110127544369SDeepak S 			/* UnMask UP THRESHOLD Interrupts */
110227544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
110327544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) &
110427544369SDeepak S 				   ~GEN6_PM_RP_UP_THRESHOLD);
110527544369SDeepak S 			dev_priv->rps.rp_up_masked = false;
110627544369SDeepak S 		}
110727544369SDeepak S 	}
110827544369SDeepak S }
110927544369SDeepak S 
11104912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11113b8d8d91SJesse Barnes {
11124912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
1113c6a828d3SDaniel Vetter 						    rps.work);
1114edbfdb45SPaulo Zanoni 	u32 pm_iir;
1115dd75fdc8SChris Wilson 	int new_delay, adj;
11163b8d8d91SJesse Barnes 
111759cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1118c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1119c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
11204848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
1121edbfdb45SPaulo Zanoni 	snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
112259cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11234912d041SBen Widawsky 
112460611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
112560611c13SPaulo Zanoni 	WARN_ON(pm_iir & ~GEN6_PM_RPS_EVENTS);
112660611c13SPaulo Zanoni 
11274848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
11283b8d8d91SJesse Barnes 		return;
11293b8d8d91SJesse Barnes 
11304fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11317b9e0ae6SChris Wilson 
1132dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
11337425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1134dd75fdc8SChris Wilson 		if (adj > 0)
1135dd75fdc8SChris Wilson 			adj *= 2;
1136dd75fdc8SChris Wilson 		else
1137dd75fdc8SChris Wilson 			adj = 1;
1138dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
11397425034aSVille Syrjälä 
11407425034aSVille Syrjälä 		/*
11417425034aSVille Syrjälä 		 * For better performance, jump directly
11427425034aSVille Syrjälä 		 * to RPe if we're below it.
11437425034aSVille Syrjälä 		 */
1144dd75fdc8SChris Wilson 		if (new_delay < dev_priv->rps.rpe_delay)
11457425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
1146dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1147dd75fdc8SChris Wilson 		if (dev_priv->rps.cur_delay > dev_priv->rps.rpe_delay)
1148dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.rpe_delay;
1149dd75fdc8SChris Wilson 		else
1150dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.min_delay;
1151dd75fdc8SChris Wilson 		adj = 0;
1152dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1153dd75fdc8SChris Wilson 		if (adj < 0)
1154dd75fdc8SChris Wilson 			adj *= 2;
1155dd75fdc8SChris Wilson 		else
1156dd75fdc8SChris Wilson 			adj = -1;
1157dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
1158dd75fdc8SChris Wilson 	} else { /* unknown event */
1159dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay;
1160dd75fdc8SChris Wilson 	}
11613b8d8d91SJesse Barnes 
116279249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
116379249636SBen Widawsky 	 * interrupt
116479249636SBen Widawsky 	 */
11651272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
11661272e7b8SVille Syrjälä 			    dev_priv->rps.min_delay, dev_priv->rps.max_delay);
116727544369SDeepak S 
116827544369SDeepak S 	gen6_set_pm_mask(dev_priv, pm_iir, new_delay);
1169dd75fdc8SChris Wilson 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_delay;
1170dd75fdc8SChris Wilson 
11710a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
11720a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
11730a073b84SJesse Barnes 	else
11744912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
11753b8d8d91SJesse Barnes 
11764fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11773b8d8d91SJesse Barnes }
11783b8d8d91SJesse Barnes 
1179e3689190SBen Widawsky 
1180e3689190SBen Widawsky /**
1181e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1182e3689190SBen Widawsky  * occurred.
1183e3689190SBen Widawsky  * @work: workqueue struct
1184e3689190SBen Widawsky  *
1185e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1186e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1187e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1188e3689190SBen Widawsky  */
1189e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1190e3689190SBen Widawsky {
1191e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
1192a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
1193e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
119435a85ac6SBen Widawsky 	char *parity_event[6];
1195e3689190SBen Widawsky 	uint32_t misccpctl;
1196e3689190SBen Widawsky 	unsigned long flags;
119735a85ac6SBen Widawsky 	uint8_t slice = 0;
1198e3689190SBen Widawsky 
1199e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1200e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1201e3689190SBen Widawsky 	 * any time we access those registers.
1202e3689190SBen Widawsky 	 */
1203e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1204e3689190SBen Widawsky 
120535a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
120635a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
120735a85ac6SBen Widawsky 		goto out;
120835a85ac6SBen Widawsky 
1209e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1210e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1211e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1212e3689190SBen Widawsky 
121335a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
121435a85ac6SBen Widawsky 		u32 reg;
121535a85ac6SBen Widawsky 
121635a85ac6SBen Widawsky 		slice--;
121735a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
121835a85ac6SBen Widawsky 			break;
121935a85ac6SBen Widawsky 
122035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
122135a85ac6SBen Widawsky 
122235a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
122335a85ac6SBen Widawsky 
122435a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1225e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1226e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1227e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1228e3689190SBen Widawsky 
122935a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
123035a85ac6SBen Widawsky 		POSTING_READ(reg);
1231e3689190SBen Widawsky 
1232cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1233e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1234e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1235e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
123635a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
123735a85ac6SBen Widawsky 		parity_event[5] = NULL;
1238e3689190SBen Widawsky 
12395bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1240e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1241e3689190SBen Widawsky 
124235a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
124335a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1244e3689190SBen Widawsky 
124535a85ac6SBen Widawsky 		kfree(parity_event[4]);
1246e3689190SBen Widawsky 		kfree(parity_event[3]);
1247e3689190SBen Widawsky 		kfree(parity_event[2]);
1248e3689190SBen Widawsky 		kfree(parity_event[1]);
1249e3689190SBen Widawsky 	}
1250e3689190SBen Widawsky 
125135a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
125235a85ac6SBen Widawsky 
125335a85ac6SBen Widawsky out:
125435a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
125535a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
125635a85ac6SBen Widawsky 	ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
125735a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
125835a85ac6SBen Widawsky 
125935a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
126035a85ac6SBen Widawsky }
126135a85ac6SBen Widawsky 
126235a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1263e3689190SBen Widawsky {
1264e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1265e3689190SBen Widawsky 
1266040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1267e3689190SBen Widawsky 		return;
1268e3689190SBen Widawsky 
1269d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
127035a85ac6SBen Widawsky 	ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1271d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1272e3689190SBen Widawsky 
127335a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
127435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
127535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
127635a85ac6SBen Widawsky 
127735a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
127835a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
127935a85ac6SBen Widawsky 
1280a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1281e3689190SBen Widawsky }
1282e3689190SBen Widawsky 
1283f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1284f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1285f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1286f1af8fc1SPaulo Zanoni {
1287f1af8fc1SPaulo Zanoni 	if (gt_iir &
1288f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1289f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1290f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1291f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1292f1af8fc1SPaulo Zanoni }
1293f1af8fc1SPaulo Zanoni 
1294e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1295e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1296e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1297e7b4c6b1SDaniel Vetter {
1298e7b4c6b1SDaniel Vetter 
1299cc609d5dSBen Widawsky 	if (gt_iir &
1300cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1301e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1302cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1303e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1304cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1305e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1306e7b4c6b1SDaniel Vetter 
1307cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1308cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1309cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
131058174462SMika Kuoppala 		i915_handle_error(dev, false, "GT error interrupt 0x%08x",
131158174462SMika Kuoppala 				  gt_iir);
1312e7b4c6b1SDaniel Vetter 	}
1313e3689190SBen Widawsky 
131435a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
131535a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1316e7b4c6b1SDaniel Vetter }
1317e7b4c6b1SDaniel Vetter 
1318abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1319abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1320abd58f01SBen Widawsky 				       u32 master_ctl)
1321abd58f01SBen Widawsky {
1322abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1323abd58f01SBen Widawsky 	uint32_t tmp = 0;
1324abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1325abd58f01SBen Widawsky 
1326abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1327abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1328abd58f01SBen Widawsky 		if (tmp) {
1329abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1330abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1331abd58f01SBen Widawsky 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1332abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1333abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[RCS]);
1334abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1335abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[BCS]);
1336abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1337abd58f01SBen Widawsky 		} else
1338abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1339abd58f01SBen Widawsky 	}
1340abd58f01SBen Widawsky 
1341abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VCS1_IRQ) {
1342abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1343abd58f01SBen Widawsky 		if (tmp) {
1344abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1345abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1346abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1347abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VCS]);
1348abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1349abd58f01SBen Widawsky 		} else
1350abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1351abd58f01SBen Widawsky 	}
1352abd58f01SBen Widawsky 
1353abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1354abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1355abd58f01SBen Widawsky 		if (tmp) {
1356abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1357abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1358abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1359abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VECS]);
1360abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1361abd58f01SBen Widawsky 		} else
1362abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1363abd58f01SBen Widawsky 	}
1364abd58f01SBen Widawsky 
1365abd58f01SBen Widawsky 	return ret;
1366abd58f01SBen Widawsky }
1367abd58f01SBen Widawsky 
1368b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1369b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1370b543fb04SEgbert Eich 
137110a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1372b543fb04SEgbert Eich 					 u32 hotplug_trigger,
1373b543fb04SEgbert Eich 					 const u32 *hpd)
1374b543fb04SEgbert Eich {
1375b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
1376b543fb04SEgbert Eich 	int i;
137710a504deSDaniel Vetter 	bool storm_detected = false;
1378b543fb04SEgbert Eich 
137991d131d2SDaniel Vetter 	if (!hotplug_trigger)
138091d131d2SDaniel Vetter 		return;
138191d131d2SDaniel Vetter 
1382cc9bd499SImre Deak 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
1383cc9bd499SImre Deak 			  hotplug_trigger);
1384cc9bd499SImre Deak 
1385b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1386b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
1387821450c6SEgbert Eich 
13883432087eSChris Wilson 		WARN_ONCE(hpd[i] & hotplug_trigger &&
13898b5565b8SChris Wilson 			  dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED,
1390cba1c073SChris Wilson 			  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1391cba1c073SChris Wilson 			  hotplug_trigger, i, hpd[i]);
1392b8f102e8SEgbert Eich 
1393b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1394b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1395b543fb04SEgbert Eich 			continue;
1396b543fb04SEgbert Eich 
1397bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1398b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1399b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1400b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1401b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1402b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1403b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1404b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1405b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1406142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1407b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
140810a504deSDaniel Vetter 			storm_detected = true;
1409b543fb04SEgbert Eich 		} else {
1410b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1411b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1412b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1413b543fb04SEgbert Eich 		}
1414b543fb04SEgbert Eich 	}
1415b543fb04SEgbert Eich 
141610a504deSDaniel Vetter 	if (storm_detected)
141710a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1418b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
14195876fa0dSDaniel Vetter 
1420645416f5SDaniel Vetter 	/*
1421645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1422645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1423645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1424645416f5SDaniel Vetter 	 * deadlock.
1425645416f5SDaniel Vetter 	 */
1426645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1427b543fb04SEgbert Eich }
1428b543fb04SEgbert Eich 
1429515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1430515ac2bbSDaniel Vetter {
143128c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
143228c70f16SDaniel Vetter 
143328c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1434515ac2bbSDaniel Vetter }
1435515ac2bbSDaniel Vetter 
1436ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1437ce99c256SDaniel Vetter {
14389ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
14399ee32feaSDaniel Vetter 
14409ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1441ce99c256SDaniel Vetter }
1442ce99c256SDaniel Vetter 
14438bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1444277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1445eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1446eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
14478bc5e955SDaniel Vetter 					 uint32_t crc4)
14488bf1e9f1SShuang He {
14498bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
14508bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
14518bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1452ac2300d4SDamien Lespiau 	int head, tail;
1453b2c88f5bSDamien Lespiau 
1454d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1455d538bbdfSDamien Lespiau 
14560c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1457d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
14580c912c79SDamien Lespiau 		DRM_ERROR("spurious interrupt\n");
14590c912c79SDamien Lespiau 		return;
14600c912c79SDamien Lespiau 	}
14610c912c79SDamien Lespiau 
1462d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1463d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1464b2c88f5bSDamien Lespiau 
1465b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1466d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1467b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1468b2c88f5bSDamien Lespiau 		return;
1469b2c88f5bSDamien Lespiau 	}
1470b2c88f5bSDamien Lespiau 
1471b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
14728bf1e9f1SShuang He 
14738bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1474eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1475eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1476eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1477eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1478eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1479b2c88f5bSDamien Lespiau 
1480b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1481d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1482d538bbdfSDamien Lespiau 
1483d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
148407144428SDamien Lespiau 
148507144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
14868bf1e9f1SShuang He }
1487277de95eSDaniel Vetter #else
1488277de95eSDaniel Vetter static inline void
1489277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1490277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1491277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1492277de95eSDaniel Vetter 			     uint32_t crc4) {}
1493277de95eSDaniel Vetter #endif
1494eba94eb9SDaniel Vetter 
1495277de95eSDaniel Vetter 
1496277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
14975a69b89fSDaniel Vetter {
14985a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
14995a69b89fSDaniel Vetter 
1500277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15015a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15025a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15035a69b89fSDaniel Vetter }
15045a69b89fSDaniel Vetter 
1505277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1506eba94eb9SDaniel Vetter {
1507eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1508eba94eb9SDaniel Vetter 
1509277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1510eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1511eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1512eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1513eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15148bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1515eba94eb9SDaniel Vetter }
15165b3a856bSDaniel Vetter 
1517277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15185b3a856bSDaniel Vetter {
15195b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15200b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15210b5c5ed0SDaniel Vetter 
15220b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
15230b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15240b5c5ed0SDaniel Vetter 	else
15250b5c5ed0SDaniel Vetter 		res1 = 0;
15260b5c5ed0SDaniel Vetter 
15270b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
15280b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
15290b5c5ed0SDaniel Vetter 	else
15300b5c5ed0SDaniel Vetter 		res2 = 0;
15315b3a856bSDaniel Vetter 
1532277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15330b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
15340b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
15350b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
15360b5c5ed0SDaniel Vetter 				     res1, res2);
15375b3a856bSDaniel Vetter }
15388bf1e9f1SShuang He 
15391403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
15401403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
15411403c0d4SPaulo Zanoni  * the work queue. */
15421403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1543baf02a1fSBen Widawsky {
154441a05a3aSDaniel Vetter 	if (pm_iir & GEN6_PM_RPS_EVENTS) {
154559cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
15464848405cSBen Widawsky 		dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
15474d3b3d5fSPaulo Zanoni 		snb_disable_pm_irq(dev_priv, pm_iir & GEN6_PM_RPS_EVENTS);
154859cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
15492adbee62SDaniel Vetter 
15502adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
155141a05a3aSDaniel Vetter 	}
1552baf02a1fSBen Widawsky 
15531403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
155412638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
155512638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
155612638c57SBen Widawsky 
155712638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
155858174462SMika Kuoppala 			i915_handle_error(dev_priv->dev, false,
155958174462SMika Kuoppala 					  "VEBOX CS error interrupt 0x%08x",
156058174462SMika Kuoppala 					  pm_iir);
156112638c57SBen Widawsky 		}
156212638c57SBen Widawsky 	}
15631403c0d4SPaulo Zanoni }
1564baf02a1fSBen Widawsky 
1565c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
15667e231dbeSJesse Barnes {
1567c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
156891d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
15697e231dbeSJesse Barnes 	int pipe;
15707e231dbeSJesse Barnes 
157158ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
15727e231dbeSJesse Barnes 	for_each_pipe(pipe) {
157391d181ddSImre Deak 		int reg;
1574bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
157591d181ddSImre Deak 
1576bbb5eebfSDaniel Vetter 		/*
1577bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1578bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1579bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1580bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1581bbb5eebfSDaniel Vetter 		 * handle.
1582bbb5eebfSDaniel Vetter 		 */
1583bbb5eebfSDaniel Vetter 		mask = 0;
1584bbb5eebfSDaniel Vetter 		if (__cpu_fifo_underrun_reporting_enabled(dev, pipe))
1585bbb5eebfSDaniel Vetter 			mask |= PIPE_FIFO_UNDERRUN_STATUS;
1586bbb5eebfSDaniel Vetter 
1587bbb5eebfSDaniel Vetter 		switch (pipe) {
1588bbb5eebfSDaniel Vetter 		case PIPE_A:
1589bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1590bbb5eebfSDaniel Vetter 			break;
1591bbb5eebfSDaniel Vetter 		case PIPE_B:
1592bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1593bbb5eebfSDaniel Vetter 			break;
1594bbb5eebfSDaniel Vetter 		}
1595bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1596bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1597bbb5eebfSDaniel Vetter 
1598bbb5eebfSDaniel Vetter 		if (!mask)
159991d181ddSImre Deak 			continue;
160091d181ddSImre Deak 
160191d181ddSImre Deak 		reg = PIPESTAT(pipe);
1602bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1603bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16047e231dbeSJesse Barnes 
16057e231dbeSJesse Barnes 		/*
16067e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16077e231dbeSJesse Barnes 		 */
160891d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
160991d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
16107e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
16117e231dbeSJesse Barnes 	}
161258ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
16137e231dbeSJesse Barnes 
161431acc7f5SJesse Barnes 	for_each_pipe(pipe) {
16157b5562d4SJesse Barnes 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
161631acc7f5SJesse Barnes 			drm_handle_vblank(dev, pipe);
161731acc7f5SJesse Barnes 
1618579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
161931acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
162031acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
162131acc7f5SJesse Barnes 		}
16224356d586SDaniel Vetter 
16234356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1624277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
16252d9d2b0bSVille Syrjälä 
16262d9d2b0bSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
16272d9d2b0bSVille Syrjälä 		    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
1628fc2c807bSVille Syrjälä 			DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
162931acc7f5SJesse Barnes 	}
163031acc7f5SJesse Barnes 
1631c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1632c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1633c1874ed7SImre Deak }
1634c1874ed7SImre Deak 
1635c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1636c1874ed7SImre Deak {
1637c1874ed7SImre Deak 	struct drm_device *dev = (struct drm_device *) arg;
1638c1874ed7SImre Deak 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1639c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1640c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1641c1874ed7SImre Deak 
1642c1874ed7SImre Deak 	while (true) {
1643c1874ed7SImre Deak 		iir = I915_READ(VLV_IIR);
1644c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1645c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
1646c1874ed7SImre Deak 
1647c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1648c1874ed7SImre Deak 			goto out;
1649c1874ed7SImre Deak 
1650c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1651c1874ed7SImre Deak 
1652c1874ed7SImre Deak 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1653c1874ed7SImre Deak 
1654c1874ed7SImre Deak 		valleyview_pipestat_irq_handler(dev, iir);
1655c1874ed7SImre Deak 
16567e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
16577e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
16587e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1659b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
16607e231dbeSJesse Barnes 
166110a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
166291d131d2SDaniel Vetter 
16634aeebd74SDaniel Vetter 			if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
16644aeebd74SDaniel Vetter 				dp_aux_irq_handler(dev);
16654aeebd74SDaniel Vetter 
16667e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
16677e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
16687e231dbeSJesse Barnes 		}
16697e231dbeSJesse Barnes 
16707e231dbeSJesse Barnes 
167160611c13SPaulo Zanoni 		if (pm_iir)
1672d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
16737e231dbeSJesse Barnes 
16747e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
16757e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
16767e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
16777e231dbeSJesse Barnes 	}
16787e231dbeSJesse Barnes 
16797e231dbeSJesse Barnes out:
16807e231dbeSJesse Barnes 	return ret;
16817e231dbeSJesse Barnes }
16827e231dbeSJesse Barnes 
168323e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1684776ad806SJesse Barnes {
1685776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16869db4a9c7SJesse Barnes 	int pipe;
1687b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1688776ad806SJesse Barnes 
168910a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
169091d131d2SDaniel Vetter 
1691cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1692cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1693776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1694cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1695cfc33bf7SVille Syrjälä 				 port_name(port));
1696cfc33bf7SVille Syrjälä 	}
1697776ad806SJesse Barnes 
1698ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1699ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1700ce99c256SDaniel Vetter 
1701776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1702515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1703776ad806SJesse Barnes 
1704776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1705776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1706776ad806SJesse Barnes 
1707776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1708776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1709776ad806SJesse Barnes 
1710776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1711776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1712776ad806SJesse Barnes 
17139db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
17149db4a9c7SJesse Barnes 		for_each_pipe(pipe)
17159db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
17169db4a9c7SJesse Barnes 					 pipe_name(pipe),
17179db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1718776ad806SJesse Barnes 
1719776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1720776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1721776ad806SJesse Barnes 
1722776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1723776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1724776ad806SJesse Barnes 
1725776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
17268664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
17278664281bSPaulo Zanoni 							  false))
1728fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
17298664281bSPaulo Zanoni 
17308664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
17318664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
17328664281bSPaulo Zanoni 							  false))
1733fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
17348664281bSPaulo Zanoni }
17358664281bSPaulo Zanoni 
17368664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
17378664281bSPaulo Zanoni {
17388664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
17398664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
17405a69b89fSDaniel Vetter 	enum pipe pipe;
17418664281bSPaulo Zanoni 
1742de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1743de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1744de032bf4SPaulo Zanoni 
17455a69b89fSDaniel Vetter 	for_each_pipe(pipe) {
17465a69b89fSDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
17475a69b89fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
17485a69b89fSDaniel Vetter 								  false))
1749fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
17505a69b89fSDaniel Vetter 					  pipe_name(pipe));
17515a69b89fSDaniel Vetter 		}
17528664281bSPaulo Zanoni 
17535a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
17545a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1755277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
17565a69b89fSDaniel Vetter 			else
1757277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
17585a69b89fSDaniel Vetter 		}
17595a69b89fSDaniel Vetter 	}
17608bf1e9f1SShuang He 
17618664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
17628664281bSPaulo Zanoni }
17638664281bSPaulo Zanoni 
17648664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
17658664281bSPaulo Zanoni {
17668664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
17678664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
17688664281bSPaulo Zanoni 
1769de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1770de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1771de032bf4SPaulo Zanoni 
17728664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
17738664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
17748664281bSPaulo Zanoni 							  false))
1775fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
17768664281bSPaulo Zanoni 
17778664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
17788664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
17798664281bSPaulo Zanoni 							  false))
1780fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
17818664281bSPaulo Zanoni 
17828664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
17838664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
17848664281bSPaulo Zanoni 							  false))
1785fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder C FIFO underrun\n");
17868664281bSPaulo Zanoni 
17878664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1788776ad806SJesse Barnes }
1789776ad806SJesse Barnes 
179023e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
179123e81d69SAdam Jackson {
179223e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
179323e81d69SAdam Jackson 	int pipe;
1794b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
179523e81d69SAdam Jackson 
179610a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
179791d131d2SDaniel Vetter 
1798cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1799cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
180023e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1801cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1802cfc33bf7SVille Syrjälä 				 port_name(port));
1803cfc33bf7SVille Syrjälä 	}
180423e81d69SAdam Jackson 
180523e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1806ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
180723e81d69SAdam Jackson 
180823e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1809515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
181023e81d69SAdam Jackson 
181123e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
181223e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
181323e81d69SAdam Jackson 
181423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
181523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
181623e81d69SAdam Jackson 
181723e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
181823e81d69SAdam Jackson 		for_each_pipe(pipe)
181923e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
182023e81d69SAdam Jackson 					 pipe_name(pipe),
182123e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
18228664281bSPaulo Zanoni 
18238664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
18248664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
182523e81d69SAdam Jackson }
182623e81d69SAdam Jackson 
1827c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1828c008bc6eSPaulo Zanoni {
1829c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
183040da17c2SDaniel Vetter 	enum pipe pipe;
1831c008bc6eSPaulo Zanoni 
1832c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1833c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1834c008bc6eSPaulo Zanoni 
1835c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1836c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1837c008bc6eSPaulo Zanoni 
1838c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1839c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1840c008bc6eSPaulo Zanoni 
184140da17c2SDaniel Vetter 	for_each_pipe(pipe) {
184240da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
184340da17c2SDaniel Vetter 			drm_handle_vblank(dev, pipe);
1844c008bc6eSPaulo Zanoni 
184540da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
184640da17c2SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
1847fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
184840da17c2SDaniel Vetter 					  pipe_name(pipe));
1849c008bc6eSPaulo Zanoni 
185040da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
185140da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
18525b3a856bSDaniel Vetter 
185340da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
185440da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
185540da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
185640da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
1857c008bc6eSPaulo Zanoni 		}
1858c008bc6eSPaulo Zanoni 	}
1859c008bc6eSPaulo Zanoni 
1860c008bc6eSPaulo Zanoni 	/* check event from PCH */
1861c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1862c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1863c008bc6eSPaulo Zanoni 
1864c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1865c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1866c008bc6eSPaulo Zanoni 		else
1867c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1868c008bc6eSPaulo Zanoni 
1869c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1870c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1871c008bc6eSPaulo Zanoni 	}
1872c008bc6eSPaulo Zanoni 
1873c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1874c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1875c008bc6eSPaulo Zanoni }
1876c008bc6eSPaulo Zanoni 
18779719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
18789719fb98SPaulo Zanoni {
18799719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
188007d27e20SDamien Lespiau 	enum pipe pipe;
18819719fb98SPaulo Zanoni 
18829719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
18839719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
18849719fb98SPaulo Zanoni 
18859719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
18869719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
18879719fb98SPaulo Zanoni 
18889719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
18899719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
18909719fb98SPaulo Zanoni 
189107d27e20SDamien Lespiau 	for_each_pipe(pipe) {
189207d27e20SDamien Lespiau 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
189307d27e20SDamien Lespiau 			drm_handle_vblank(dev, pipe);
189440da17c2SDaniel Vetter 
189540da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
189607d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
189707d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
189807d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
18999719fb98SPaulo Zanoni 		}
19009719fb98SPaulo Zanoni 	}
19019719fb98SPaulo Zanoni 
19029719fb98SPaulo Zanoni 	/* check event from PCH */
19039719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
19049719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
19059719fb98SPaulo Zanoni 
19069719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
19079719fb98SPaulo Zanoni 
19089719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
19099719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
19109719fb98SPaulo Zanoni 	}
19119719fb98SPaulo Zanoni }
19129719fb98SPaulo Zanoni 
1913f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1914b1f14ad0SJesse Barnes {
1915b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1916b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1917f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
19180e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1919b1f14ad0SJesse Barnes 
19208664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
19218664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1922907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
19238664281bSPaulo Zanoni 
1924b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1925b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1926b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
192723a78516SPaulo Zanoni 	POSTING_READ(DEIER);
19280e43406bSChris Wilson 
192944498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
193044498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
193144498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
193244498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
193344498aeaSPaulo Zanoni 	 * due to its back queue). */
1934ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
193544498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
193644498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
193744498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1938ab5c608bSBen Widawsky 	}
193944498aeaSPaulo Zanoni 
19400e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
19410e43406bSChris Wilson 	if (gt_iir) {
1942d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
19430e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1944d8fc8a47SPaulo Zanoni 		else
1945d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
19460e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
19470e43406bSChris Wilson 		ret = IRQ_HANDLED;
19480e43406bSChris Wilson 	}
1949b1f14ad0SJesse Barnes 
1950b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
19510e43406bSChris Wilson 	if (de_iir) {
1952f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
19539719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1954f1af8fc1SPaulo Zanoni 		else
1955f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
19560e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
19570e43406bSChris Wilson 		ret = IRQ_HANDLED;
19580e43406bSChris Wilson 	}
19590e43406bSChris Wilson 
1960f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
1961f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
19620e43406bSChris Wilson 		if (pm_iir) {
1963d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
1964b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
19650e43406bSChris Wilson 			ret = IRQ_HANDLED;
19660e43406bSChris Wilson 		}
1967f1af8fc1SPaulo Zanoni 	}
1968b1f14ad0SJesse Barnes 
1969b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1970b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1971ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
197244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
197344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1974ab5c608bSBen Widawsky 	}
1975b1f14ad0SJesse Barnes 
1976b1f14ad0SJesse Barnes 	return ret;
1977b1f14ad0SJesse Barnes }
1978b1f14ad0SJesse Barnes 
1979abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
1980abd58f01SBen Widawsky {
1981abd58f01SBen Widawsky 	struct drm_device *dev = arg;
1982abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
1983abd58f01SBen Widawsky 	u32 master_ctl;
1984abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1985abd58f01SBen Widawsky 	uint32_t tmp = 0;
1986c42664ccSDaniel Vetter 	enum pipe pipe;
1987abd58f01SBen Widawsky 
1988abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
1989abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
1990abd58f01SBen Widawsky 	if (!master_ctl)
1991abd58f01SBen Widawsky 		return IRQ_NONE;
1992abd58f01SBen Widawsky 
1993abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
1994abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
1995abd58f01SBen Widawsky 
1996abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
1997abd58f01SBen Widawsky 
1998abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
1999abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2000abd58f01SBen Widawsky 		if (tmp & GEN8_DE_MISC_GSE)
2001abd58f01SBen Widawsky 			intel_opregion_asle_intr(dev);
2002abd58f01SBen Widawsky 		else if (tmp)
2003abd58f01SBen Widawsky 			DRM_ERROR("Unexpected DE Misc interrupt\n");
2004abd58f01SBen Widawsky 		else
2005abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2006abd58f01SBen Widawsky 
2007abd58f01SBen Widawsky 		if (tmp) {
2008abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2009abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2010abd58f01SBen Widawsky 		}
2011abd58f01SBen Widawsky 	}
2012abd58f01SBen Widawsky 
20136d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
20146d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
20156d766f02SDaniel Vetter 		if (tmp & GEN8_AUX_CHANNEL_A)
20166d766f02SDaniel Vetter 			dp_aux_irq_handler(dev);
20176d766f02SDaniel Vetter 		else if (tmp)
20186d766f02SDaniel Vetter 			DRM_ERROR("Unexpected DE Port interrupt\n");
20196d766f02SDaniel Vetter 		else
20206d766f02SDaniel Vetter 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
20216d766f02SDaniel Vetter 
20226d766f02SDaniel Vetter 		if (tmp) {
20236d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
20246d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
20256d766f02SDaniel Vetter 		}
20266d766f02SDaniel Vetter 	}
20276d766f02SDaniel Vetter 
2028abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2029abd58f01SBen Widawsky 		uint32_t pipe_iir;
2030abd58f01SBen Widawsky 
2031c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2032c42664ccSDaniel Vetter 			continue;
2033c42664ccSDaniel Vetter 
2034abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2035abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_VBLANK)
2036abd58f01SBen Widawsky 			drm_handle_vblank(dev, pipe);
2037abd58f01SBen Widawsky 
2038abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_FLIP_DONE) {
2039abd58f01SBen Widawsky 			intel_prepare_page_flip(dev, pipe);
2040abd58f01SBen Widawsky 			intel_finish_page_flip_plane(dev, pipe);
2041abd58f01SBen Widawsky 		}
2042abd58f01SBen Widawsky 
20430fbe7870SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
20440fbe7870SDaniel Vetter 			hsw_pipe_crc_irq_handler(dev, pipe);
20450fbe7870SDaniel Vetter 
204638d83c96SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN) {
204738d83c96SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
204838d83c96SDaniel Vetter 								  false))
2049fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
205038d83c96SDaniel Vetter 					  pipe_name(pipe));
205138d83c96SDaniel Vetter 		}
205238d83c96SDaniel Vetter 
205330100f2bSDaniel Vetter 		if (pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS) {
205430100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
205530100f2bSDaniel Vetter 				  pipe_name(pipe),
205630100f2bSDaniel Vetter 				  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
205730100f2bSDaniel Vetter 		}
2058abd58f01SBen Widawsky 
2059abd58f01SBen Widawsky 		if (pipe_iir) {
2060abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2061abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2062c42664ccSDaniel Vetter 		} else
2063abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2064abd58f01SBen Widawsky 	}
2065abd58f01SBen Widawsky 
206692d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
206792d03a80SDaniel Vetter 		/*
206892d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
206992d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
207092d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
207192d03a80SDaniel Vetter 		 */
207292d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
207392d03a80SDaniel Vetter 
207492d03a80SDaniel Vetter 		cpt_irq_handler(dev, pch_iir);
207592d03a80SDaniel Vetter 
207692d03a80SDaniel Vetter 		if (pch_iir) {
207792d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
207892d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
207992d03a80SDaniel Vetter 		}
208092d03a80SDaniel Vetter 	}
208192d03a80SDaniel Vetter 
2082abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2083abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2084abd58f01SBen Widawsky 
2085abd58f01SBen Widawsky 	return ret;
2086abd58f01SBen Widawsky }
2087abd58f01SBen Widawsky 
208817e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
208917e1df07SDaniel Vetter 			       bool reset_completed)
209017e1df07SDaniel Vetter {
209117e1df07SDaniel Vetter 	struct intel_ring_buffer *ring;
209217e1df07SDaniel Vetter 	int i;
209317e1df07SDaniel Vetter 
209417e1df07SDaniel Vetter 	/*
209517e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
209617e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
209717e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
209817e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
209917e1df07SDaniel Vetter 	 */
210017e1df07SDaniel Vetter 
210117e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
210217e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
210317e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
210417e1df07SDaniel Vetter 
210517e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
210617e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
210717e1df07SDaniel Vetter 
210817e1df07SDaniel Vetter 	/*
210917e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
211017e1df07SDaniel Vetter 	 * reset state is cleared.
211117e1df07SDaniel Vetter 	 */
211217e1df07SDaniel Vetter 	if (reset_completed)
211317e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
211417e1df07SDaniel Vetter }
211517e1df07SDaniel Vetter 
21168a905236SJesse Barnes /**
21178a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
21188a905236SJesse Barnes  * @work: work struct
21198a905236SJesse Barnes  *
21208a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
21218a905236SJesse Barnes  * was detected.
21228a905236SJesse Barnes  */
21238a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
21248a905236SJesse Barnes {
21251f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
21261f83fee0SDaniel Vetter 						    work);
21271f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
21281f83fee0SDaniel Vetter 						    gpu_error);
21298a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
2130cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2131cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2132cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
213317e1df07SDaniel Vetter 	int ret;
21348a905236SJesse Barnes 
21355bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
21368a905236SJesse Barnes 
21377db0ba24SDaniel Vetter 	/*
21387db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
21397db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
21407db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
21417db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
21427db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
21437db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
21447db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
21457db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
21467db0ba24SDaniel Vetter 	 */
21477db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
214844d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
21495bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
21507db0ba24SDaniel Vetter 				   reset_event);
21511f83fee0SDaniel Vetter 
215217e1df07SDaniel Vetter 		/*
215317e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
215417e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
215517e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
215617e1df07SDaniel Vetter 		 * deadlocks with the reset work.
215717e1df07SDaniel Vetter 		 */
2158f69061beSDaniel Vetter 		ret = i915_reset(dev);
2159f69061beSDaniel Vetter 
216017e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
216117e1df07SDaniel Vetter 
2162f69061beSDaniel Vetter 		if (ret == 0) {
2163f69061beSDaniel Vetter 			/*
2164f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2165f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2166f69061beSDaniel Vetter 			 * complete.
2167f69061beSDaniel Vetter 			 *
2168f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2169f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2170f69061beSDaniel Vetter 			 * updates before
2171f69061beSDaniel Vetter 			 * the counter increment.
2172f69061beSDaniel Vetter 			 */
2173f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
2174f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2175f69061beSDaniel Vetter 
21765bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2177f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
21781f83fee0SDaniel Vetter 		} else {
21792ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2180f316a42cSBen Gamari 		}
21811f83fee0SDaniel Vetter 
218217e1df07SDaniel Vetter 		/*
218317e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
218417e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
218517e1df07SDaniel Vetter 		 */
218617e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2187f316a42cSBen Gamari 	}
21888a905236SJesse Barnes }
21898a905236SJesse Barnes 
219035aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2191c0e09200SDave Airlie {
21928a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2193bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
219463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2195050ee91fSBen Widawsky 	int pipe, i;
219663eeaf38SJesse Barnes 
219735aed2e6SChris Wilson 	if (!eir)
219835aed2e6SChris Wilson 		return;
219963eeaf38SJesse Barnes 
2200a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
22018a905236SJesse Barnes 
2202bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2203bd9854f9SBen Widawsky 
22048a905236SJesse Barnes 	if (IS_G4X(dev)) {
22058a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
22068a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
22078a905236SJesse Barnes 
2208a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2209a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2210050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2211050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2212a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2213a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
22148a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
22153143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
22168a905236SJesse Barnes 		}
22178a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
22188a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2219a70491ccSJoe Perches 			pr_err("page table error\n");
2220a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
22218a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
22223143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
22238a905236SJesse Barnes 		}
22248a905236SJesse Barnes 	}
22258a905236SJesse Barnes 
2226a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
222763eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
222863eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2229a70491ccSJoe Perches 			pr_err("page table error\n");
2230a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
223163eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
22323143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
223363eeaf38SJesse Barnes 		}
22348a905236SJesse Barnes 	}
22358a905236SJesse Barnes 
223663eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2237a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
22389db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2239a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
22409db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
224163eeaf38SJesse Barnes 		/* pipestat has already been acked */
224263eeaf38SJesse Barnes 	}
224363eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2244a70491ccSJoe Perches 		pr_err("instruction error\n");
2245a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2246050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2247050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2248a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
224963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
225063eeaf38SJesse Barnes 
2251a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2252a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2253a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
225463eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
22553143a2bfSChris Wilson 			POSTING_READ(IPEIR);
225663eeaf38SJesse Barnes 		} else {
225763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
225863eeaf38SJesse Barnes 
2259a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2260a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2261a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2262a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
226363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
22643143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
226563eeaf38SJesse Barnes 		}
226663eeaf38SJesse Barnes 	}
226763eeaf38SJesse Barnes 
226863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
22693143a2bfSChris Wilson 	POSTING_READ(EIR);
227063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
227163eeaf38SJesse Barnes 	if (eir) {
227263eeaf38SJesse Barnes 		/*
227363eeaf38SJesse Barnes 		 * some errors might have become stuck,
227463eeaf38SJesse Barnes 		 * mask them.
227563eeaf38SJesse Barnes 		 */
227663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
227763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
227863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
227963eeaf38SJesse Barnes 	}
228035aed2e6SChris Wilson }
228135aed2e6SChris Wilson 
228235aed2e6SChris Wilson /**
228335aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
228435aed2e6SChris Wilson  * @dev: drm device
228535aed2e6SChris Wilson  *
228635aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
228735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
228835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
228935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
229035aed2e6SChris Wilson  * of a ring dump etc.).
229135aed2e6SChris Wilson  */
229258174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
229358174462SMika Kuoppala 		       const char *fmt, ...)
229435aed2e6SChris Wilson {
229535aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
229658174462SMika Kuoppala 	va_list args;
229758174462SMika Kuoppala 	char error_msg[80];
229835aed2e6SChris Wilson 
229958174462SMika Kuoppala 	va_start(args, fmt);
230058174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
230158174462SMika Kuoppala 	va_end(args);
230258174462SMika Kuoppala 
230358174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
230435aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
23058a905236SJesse Barnes 
2306ba1234d1SBen Gamari 	if (wedged) {
2307f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2308f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2309ba1234d1SBen Gamari 
231011ed50ecSBen Gamari 		/*
231117e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
231217e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
231317e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
231417e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
231517e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
231617e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
231717e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
231817e1df07SDaniel Vetter 		 *
231917e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
232017e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
232117e1df07SDaniel Vetter 		 * counter atomic_t.
232211ed50ecSBen Gamari 		 */
232317e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
232411ed50ecSBen Gamari 	}
232511ed50ecSBen Gamari 
2326122f46baSDaniel Vetter 	/*
2327122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
2328122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
2329122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2330122f46baSDaniel Vetter 	 * code will deadlock.
2331122f46baSDaniel Vetter 	 */
2332122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
23338a905236SJesse Barnes }
23348a905236SJesse Barnes 
233521ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
23364e5359cdSSimon Farnsworth {
23374e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
23384e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
23394e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
234005394f39SChris Wilson 	struct drm_i915_gem_object *obj;
23414e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
23424e5359cdSSimon Farnsworth 	unsigned long flags;
23434e5359cdSSimon Farnsworth 	bool stall_detected;
23444e5359cdSSimon Farnsworth 
23454e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
23464e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
23474e5359cdSSimon Farnsworth 		return;
23484e5359cdSSimon Farnsworth 
23494e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
23504e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
23514e5359cdSSimon Farnsworth 
2352e7d841caSChris Wilson 	if (work == NULL ||
2353e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2354e7d841caSChris Wilson 	    !work->enable_stall_check) {
23554e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
23564e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
23574e5359cdSSimon Farnsworth 		return;
23584e5359cdSSimon Farnsworth 	}
23594e5359cdSSimon Farnsworth 
23604e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
236105394f39SChris Wilson 	obj = work->pending_flip_obj;
2362a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
23639db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2364446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2365f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
23664e5359cdSSimon Farnsworth 	} else {
23679db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
2368f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
236901f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
23704e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
23714e5359cdSSimon Farnsworth 	}
23724e5359cdSSimon Farnsworth 
23734e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
23744e5359cdSSimon Farnsworth 
23754e5359cdSSimon Farnsworth 	if (stall_detected) {
23764e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
23774e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
23784e5359cdSSimon Farnsworth 	}
23794e5359cdSSimon Farnsworth }
23804e5359cdSSimon Farnsworth 
238142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
238242f52ef8SKeith Packard  * we use as a pipe index
238342f52ef8SKeith Packard  */
2384f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
23850a3e67a4SJesse Barnes {
23860a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2387e9d21d7fSKeith Packard 	unsigned long irqflags;
238871e0ffa5SJesse Barnes 
23895eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
239071e0ffa5SJesse Barnes 		return -EINVAL;
23910a3e67a4SJesse Barnes 
23921ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2393f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
23947c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2395755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
23960a3e67a4SJesse Barnes 	else
23977c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2398755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
23998692d00eSChris Wilson 
24008692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
24013d13ef2eSDamien Lespiau 	if (INTEL_INFO(dev)->gen == 3)
24026b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
24031ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
24048692d00eSChris Wilson 
24050a3e67a4SJesse Barnes 	return 0;
24060a3e67a4SJesse Barnes }
24070a3e67a4SJesse Barnes 
2408f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2409f796cf8fSJesse Barnes {
2410f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2411f796cf8fSJesse Barnes 	unsigned long irqflags;
2412b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
241340da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2414f796cf8fSJesse Barnes 
2415f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2416f796cf8fSJesse Barnes 		return -EINVAL;
2417f796cf8fSJesse Barnes 
2418f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2419b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2420b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2421b1f14ad0SJesse Barnes 
2422b1f14ad0SJesse Barnes 	return 0;
2423b1f14ad0SJesse Barnes }
2424b1f14ad0SJesse Barnes 
24257e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
24267e231dbeSJesse Barnes {
24277e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24287e231dbeSJesse Barnes 	unsigned long irqflags;
24297e231dbeSJesse Barnes 
24307e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
24317e231dbeSJesse Barnes 		return -EINVAL;
24327e231dbeSJesse Barnes 
24337e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
243431acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2435755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
24367e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
24377e231dbeSJesse Barnes 
24387e231dbeSJesse Barnes 	return 0;
24397e231dbeSJesse Barnes }
24407e231dbeSJesse Barnes 
2441abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2442abd58f01SBen Widawsky {
2443abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2444abd58f01SBen Widawsky 	unsigned long irqflags;
2445abd58f01SBen Widawsky 
2446abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2447abd58f01SBen Widawsky 		return -EINVAL;
2448abd58f01SBen Widawsky 
2449abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
24507167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
24517167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2452abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2453abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2454abd58f01SBen Widawsky 	return 0;
2455abd58f01SBen Widawsky }
2456abd58f01SBen Widawsky 
245742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
245842f52ef8SKeith Packard  * we use as a pipe index
245942f52ef8SKeith Packard  */
2460f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
24610a3e67a4SJesse Barnes {
24620a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2463e9d21d7fSKeith Packard 	unsigned long irqflags;
24640a3e67a4SJesse Barnes 
24651ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
24663d13ef2eSDamien Lespiau 	if (INTEL_INFO(dev)->gen == 3)
24676b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
24688692d00eSChris Wilson 
24697c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2470755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2471755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
24721ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
24730a3e67a4SJesse Barnes }
24740a3e67a4SJesse Barnes 
2475f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2476f796cf8fSJesse Barnes {
2477f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2478f796cf8fSJesse Barnes 	unsigned long irqflags;
2479b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
248040da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2481f796cf8fSJesse Barnes 
2482f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2483b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2484b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2485b1f14ad0SJesse Barnes }
2486b1f14ad0SJesse Barnes 
24877e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
24887e231dbeSJesse Barnes {
24897e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24907e231dbeSJesse Barnes 	unsigned long irqflags;
24917e231dbeSJesse Barnes 
24927e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
249331acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2494755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
24957e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
24967e231dbeSJesse Barnes }
24977e231dbeSJesse Barnes 
2498abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2499abd58f01SBen Widawsky {
2500abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2501abd58f01SBen Widawsky 	unsigned long irqflags;
2502abd58f01SBen Widawsky 
2503abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2504abd58f01SBen Widawsky 		return;
2505abd58f01SBen Widawsky 
2506abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
25077167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
25087167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2509abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2510abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2511abd58f01SBen Widawsky }
2512abd58f01SBen Widawsky 
2513893eead0SChris Wilson static u32
2514893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2515852835f3SZou Nan hai {
2516893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2517893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2518893eead0SChris Wilson }
2519893eead0SChris Wilson 
25209107e9d2SChris Wilson static bool
25219107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2522893eead0SChris Wilson {
25239107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
25249107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2525f65d9421SBen Gamari }
2526f65d9421SBen Gamari 
25276274f212SChris Wilson static struct intel_ring_buffer *
25286274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2529a24a11e6SChris Wilson {
2530a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
25316274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2532a24a11e6SChris Wilson 
2533a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2534a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2535a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
25366274f212SChris Wilson 		return NULL;
2537a24a11e6SChris Wilson 
2538a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2539a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2540a24a11e6SChris Wilson 	 */
25416274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2542a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2543a24a11e6SChris Wilson 	do {
2544a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2545a24a11e6SChris Wilson 		if (cmd == ipehr)
2546a24a11e6SChris Wilson 			break;
2547a24a11e6SChris Wilson 
2548a24a11e6SChris Wilson 		acthd -= 4;
2549a24a11e6SChris Wilson 		if (acthd < acthd_min)
25506274f212SChris Wilson 			return NULL;
2551a24a11e6SChris Wilson 	} while (1);
2552a24a11e6SChris Wilson 
25536274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
25546274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2555a24a11e6SChris Wilson }
2556a24a11e6SChris Wilson 
25576274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
25586274f212SChris Wilson {
25596274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
25606274f212SChris Wilson 	struct intel_ring_buffer *signaller;
25616274f212SChris Wilson 	u32 seqno, ctl;
25626274f212SChris Wilson 
25636274f212SChris Wilson 	ring->hangcheck.deadlock = true;
25646274f212SChris Wilson 
25656274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
25666274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
25676274f212SChris Wilson 		return -1;
25686274f212SChris Wilson 
25696274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
25706274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
25716274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
25726274f212SChris Wilson 		return -1;
25736274f212SChris Wilson 
25746274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
25756274f212SChris Wilson }
25766274f212SChris Wilson 
25776274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
25786274f212SChris Wilson {
25796274f212SChris Wilson 	struct intel_ring_buffer *ring;
25806274f212SChris Wilson 	int i;
25816274f212SChris Wilson 
25826274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
25836274f212SChris Wilson 		ring->hangcheck.deadlock = false;
25846274f212SChris Wilson }
25856274f212SChris Wilson 
2586ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2587ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
25881ec14ad3SChris Wilson {
25891ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
25901ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
25919107e9d2SChris Wilson 	u32 tmp;
25929107e9d2SChris Wilson 
25936274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2594f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
25956274f212SChris Wilson 
25969107e9d2SChris Wilson 	if (IS_GEN2(dev))
2597f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
25989107e9d2SChris Wilson 
25999107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
26009107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
26019107e9d2SChris Wilson 	 * and break the hang. This should work on
26029107e9d2SChris Wilson 	 * all but the second generation chipsets.
26039107e9d2SChris Wilson 	 */
26049107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
26051ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
260658174462SMika Kuoppala 		i915_handle_error(dev, false,
260758174462SMika Kuoppala 				  "Kicking stuck wait on %s",
26081ec14ad3SChris Wilson 				  ring->name);
26091ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2610f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
26111ec14ad3SChris Wilson 	}
2612a24a11e6SChris Wilson 
26136274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
26146274f212SChris Wilson 		switch (semaphore_passed(ring)) {
26156274f212SChris Wilson 		default:
2616f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
26176274f212SChris Wilson 		case 1:
261858174462SMika Kuoppala 			i915_handle_error(dev, false,
261958174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2620a24a11e6SChris Wilson 					  ring->name);
2621a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2622f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
26236274f212SChris Wilson 		case 0:
2624f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
26256274f212SChris Wilson 		}
26269107e9d2SChris Wilson 	}
26279107e9d2SChris Wilson 
2628f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2629a24a11e6SChris Wilson }
2630d1e61e7fSChris Wilson 
2631f65d9421SBen Gamari /**
2632f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
263305407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
263405407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
263505407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
263605407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
263705407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2638f65d9421SBen Gamari  */
2639a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2640f65d9421SBen Gamari {
2641f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2642f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2643b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2644b4519513SChris Wilson 	int i;
264505407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
26469107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
26479107e9d2SChris Wilson #define BUSY 1
26489107e9d2SChris Wilson #define KICK 5
26499107e9d2SChris Wilson #define HUNG 20
2650893eead0SChris Wilson 
2651d330a953SJani Nikula 	if (!i915.enable_hangcheck)
26523e0dc6b0SBen Widawsky 		return;
26533e0dc6b0SBen Widawsky 
2654b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
265505407ff8SMika Kuoppala 		u32 seqno, acthd;
26569107e9d2SChris Wilson 		bool busy = true;
2657b4519513SChris Wilson 
26586274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
26596274f212SChris Wilson 
266005407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
266105407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
266205407ff8SMika Kuoppala 
266305407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
26649107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2665da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2666da661464SMika Kuoppala 
26679107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
26689107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2669094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2670f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
26719107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
26729107e9d2SChris Wilson 								  ring->name);
2673f4adcd24SDaniel Vetter 						else
2674f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2675f4adcd24SDaniel Vetter 								 ring->name);
26769107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2677094f9a54SChris Wilson 					}
2678094f9a54SChris Wilson 					/* Safeguard against driver failure */
2679094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
26809107e9d2SChris Wilson 				} else
26819107e9d2SChris Wilson 					busy = false;
268205407ff8SMika Kuoppala 			} else {
26836274f212SChris Wilson 				/* We always increment the hangcheck score
26846274f212SChris Wilson 				 * if the ring is busy and still processing
26856274f212SChris Wilson 				 * the same request, so that no single request
26866274f212SChris Wilson 				 * can run indefinitely (such as a chain of
26876274f212SChris Wilson 				 * batches). The only time we do not increment
26886274f212SChris Wilson 				 * the hangcheck score on this ring, if this
26896274f212SChris Wilson 				 * ring is in a legitimate wait for another
26906274f212SChris Wilson 				 * ring. In that case the waiting ring is a
26916274f212SChris Wilson 				 * victim and we want to be sure we catch the
26926274f212SChris Wilson 				 * right culprit. Then every time we do kick
26936274f212SChris Wilson 				 * the ring, add a small increment to the
26946274f212SChris Wilson 				 * score so that we can catch a batch that is
26956274f212SChris Wilson 				 * being repeatedly kicked and so responsible
26966274f212SChris Wilson 				 * for stalling the machine.
26979107e9d2SChris Wilson 				 */
2698ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2699ad8beaeaSMika Kuoppala 								    acthd);
2700ad8beaeaSMika Kuoppala 
2701ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2702da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2703f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
27046274f212SChris Wilson 					break;
2705f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2706ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
27076274f212SChris Wilson 					break;
2708f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2709ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
27106274f212SChris Wilson 					break;
2711f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2712ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
27136274f212SChris Wilson 					stuck[i] = true;
27146274f212SChris Wilson 					break;
27156274f212SChris Wilson 				}
271605407ff8SMika Kuoppala 			}
27179107e9d2SChris Wilson 		} else {
2718da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2719da661464SMika Kuoppala 
27209107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
27219107e9d2SChris Wilson 			 * attempts across multiple batches.
27229107e9d2SChris Wilson 			 */
27239107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
27249107e9d2SChris Wilson 				ring->hangcheck.score--;
2725cbb465e7SChris Wilson 		}
2726f65d9421SBen Gamari 
272705407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
272805407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
27299107e9d2SChris Wilson 		busy_count += busy;
273005407ff8SMika Kuoppala 	}
273105407ff8SMika Kuoppala 
273205407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2733b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2734b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
273505407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2736a43adf07SChris Wilson 				 ring->name);
2737a43adf07SChris Wilson 			rings_hung++;
273805407ff8SMika Kuoppala 		}
273905407ff8SMika Kuoppala 	}
274005407ff8SMika Kuoppala 
274105407ff8SMika Kuoppala 	if (rings_hung)
274258174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
274305407ff8SMika Kuoppala 
274405407ff8SMika Kuoppala 	if (busy_count)
274505407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
274605407ff8SMika Kuoppala 		 * being added */
274710cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
274810cd45b6SMika Kuoppala }
274910cd45b6SMika Kuoppala 
275010cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
275110cd45b6SMika Kuoppala {
275210cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
2753d330a953SJani Nikula 	if (!i915.enable_hangcheck)
275410cd45b6SMika Kuoppala 		return;
275510cd45b6SMika Kuoppala 
275699584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
275710cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2758f65d9421SBen Gamari }
2759f65d9421SBen Gamari 
276091738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
276191738a95SPaulo Zanoni {
276291738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
276391738a95SPaulo Zanoni 
276491738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
276591738a95SPaulo Zanoni 		return;
276691738a95SPaulo Zanoni 
276791738a95SPaulo Zanoni 	/* south display irq */
276891738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
276991738a95SPaulo Zanoni 	/*
277091738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
277191738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
277291738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
277391738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
277491738a95SPaulo Zanoni 	 */
277591738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
277691738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
277791738a95SPaulo Zanoni }
277891738a95SPaulo Zanoni 
2779d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2780d18ea1b5SDaniel Vetter {
2781d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2782d18ea1b5SDaniel Vetter 
2783d18ea1b5SDaniel Vetter 	/* and GT */
2784d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2785d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2786d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2787d18ea1b5SDaniel Vetter 
2788d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2789d18ea1b5SDaniel Vetter 		/* and PM */
2790d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2791d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2792d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2793d18ea1b5SDaniel Vetter 	}
2794d18ea1b5SDaniel Vetter }
2795d18ea1b5SDaniel Vetter 
2796c0e09200SDave Airlie /* drm_dma.h hooks
2797c0e09200SDave Airlie */
2798f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2799036a4a7dSZhenyu Wang {
2800036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2801036a4a7dSZhenyu Wang 
2802036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2803bdfcdb63SDaniel Vetter 
2804036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2805036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
28063143a2bfSChris Wilson 	POSTING_READ(DEIER);
2807036a4a7dSZhenyu Wang 
2808d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2809c650156aSZhenyu Wang 
281091738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
28117d99163dSBen Widawsky }
28127d99163dSBen Widawsky 
28137e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
28147e231dbeSJesse Barnes {
28157e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
28167e231dbeSJesse Barnes 	int pipe;
28177e231dbeSJesse Barnes 
28187e231dbeSJesse Barnes 	/* VLV magic */
28197e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
28207e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
28217e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
28227e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
28237e231dbeSJesse Barnes 
28247e231dbeSJesse Barnes 	/* and GT */
28257e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
28267e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2827d18ea1b5SDaniel Vetter 
2828d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
28297e231dbeSJesse Barnes 
28307e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
28317e231dbeSJesse Barnes 
28327e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
28337e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
28347e231dbeSJesse Barnes 	for_each_pipe(pipe)
28357e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
28367e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28377e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
28387e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
28397e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
28407e231dbeSJesse Barnes }
28417e231dbeSJesse Barnes 
2842abd58f01SBen Widawsky static void gen8_irq_preinstall(struct drm_device *dev)
2843abd58f01SBen Widawsky {
2844abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2845abd58f01SBen Widawsky 	int pipe;
2846abd58f01SBen Widawsky 
2847abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2848abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2849abd58f01SBen Widawsky 
2850abd58f01SBen Widawsky 	/* IIR can theoretically queue up two events. Be paranoid */
2851abd58f01SBen Widawsky #define GEN8_IRQ_INIT_NDX(type, which) do { \
2852abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
2853abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IMR(which)); \
2854abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER(which), 0); \
2855abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2856abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IIR(which)); \
2857abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2858abd58f01SBen Widawsky 	} while (0)
2859abd58f01SBen Widawsky 
2860abd58f01SBen Widawsky #define GEN8_IRQ_INIT(type) do { \
2861abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
2862abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IMR); \
2863abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER, 0); \
2864abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2865abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IIR); \
2866abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2867abd58f01SBen Widawsky 	} while (0)
2868abd58f01SBen Widawsky 
2869abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 0);
2870abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 1);
2871abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 2);
2872abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 3);
2873abd58f01SBen Widawsky 
2874abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2875abd58f01SBen Widawsky 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe);
2876abd58f01SBen Widawsky 	}
2877abd58f01SBen Widawsky 
2878abd58f01SBen Widawsky 	GEN8_IRQ_INIT(DE_PORT);
2879abd58f01SBen Widawsky 	GEN8_IRQ_INIT(DE_MISC);
2880abd58f01SBen Widawsky 	GEN8_IRQ_INIT(PCU);
2881abd58f01SBen Widawsky #undef GEN8_IRQ_INIT
2882abd58f01SBen Widawsky #undef GEN8_IRQ_INIT_NDX
2883abd58f01SBen Widawsky 
2884abd58f01SBen Widawsky 	POSTING_READ(GEN8_PCU_IIR);
288509f2344dSJesse Barnes 
288609f2344dSJesse Barnes 	ibx_irq_preinstall(dev);
2887abd58f01SBen Widawsky }
2888abd58f01SBen Widawsky 
288982a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
289082a28bcfSDaniel Vetter {
289182a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
289282a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
289382a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2894fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
289582a28bcfSDaniel Vetter 
289682a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2897fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
289882a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2899cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2900fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
290182a28bcfSDaniel Vetter 	} else {
2902fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
290382a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2904cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2905fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
290682a28bcfSDaniel Vetter 	}
290782a28bcfSDaniel Vetter 
2908fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
290982a28bcfSDaniel Vetter 
29107fe0b973SKeith Packard 	/*
29117fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
29127fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
29137fe0b973SKeith Packard 	 *
29147fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
29157fe0b973SKeith Packard 	 */
29167fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
29177fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
29187fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
29197fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
29207fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
29217fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
29227fe0b973SKeith Packard }
29237fe0b973SKeith Packard 
2924d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2925d46da437SPaulo Zanoni {
2926d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
292782a28bcfSDaniel Vetter 	u32 mask;
2928d46da437SPaulo Zanoni 
2929692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2930692a04cfSDaniel Vetter 		return;
2931692a04cfSDaniel Vetter 
29328664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
29338664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2934de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
29358664281bSPaulo Zanoni 	} else {
29368664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
29378664281bSPaulo Zanoni 
29388664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
29398664281bSPaulo Zanoni 	}
2940ab5c608bSBen Widawsky 
2941d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2942d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2943d46da437SPaulo Zanoni }
2944d46da437SPaulo Zanoni 
29450a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
29460a9a8c91SDaniel Vetter {
29470a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
29480a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
29490a9a8c91SDaniel Vetter 
29500a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
29510a9a8c91SDaniel Vetter 
29520a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
2953040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
29540a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
295535a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
295635a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
29570a9a8c91SDaniel Vetter 	}
29580a9a8c91SDaniel Vetter 
29590a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
29600a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
29610a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
29620a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
29630a9a8c91SDaniel Vetter 	} else {
29640a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
29650a9a8c91SDaniel Vetter 	}
29660a9a8c91SDaniel Vetter 
29670a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
29680a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
29690a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
29700a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
29710a9a8c91SDaniel Vetter 
29720a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
29730a9a8c91SDaniel Vetter 		pm_irqs |= GEN6_PM_RPS_EVENTS;
29740a9a8c91SDaniel Vetter 
29750a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
29760a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
29770a9a8c91SDaniel Vetter 
2978605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
29790a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2980605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
29810a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
29820a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
29830a9a8c91SDaniel Vetter 	}
29840a9a8c91SDaniel Vetter }
29850a9a8c91SDaniel Vetter 
2986f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2987036a4a7dSZhenyu Wang {
29884bc9d430SDaniel Vetter 	unsigned long irqflags;
2989036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29908e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
29918e76f8dcSPaulo Zanoni 
29928e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
29938e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
29948e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
29958e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
29968e76f8dcSPaulo Zanoni 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
29978e76f8dcSPaulo Zanoni 				DE_ERR_INT_IVB);
29988e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
29998e76f8dcSPaulo Zanoni 			      DE_PIPEA_VBLANK_IVB);
30008e76f8dcSPaulo Zanoni 
30018e76f8dcSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
30028e76f8dcSPaulo Zanoni 	} else {
30038e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3004ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
30055b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
30065b3a856bSDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
30075b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
30085b3a856bSDaniel Vetter 				DE_POISON);
30098e76f8dcSPaulo Zanoni 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
30108e76f8dcSPaulo Zanoni 	}
3011036a4a7dSZhenyu Wang 
30121ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3013036a4a7dSZhenyu Wang 
3014036a4a7dSZhenyu Wang 	/* should always can generate irq */
3015036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
30161ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
30178e76f8dcSPaulo Zanoni 	I915_WRITE(DEIER, display_mask | extra_mask);
30183143a2bfSChris Wilson 	POSTING_READ(DEIER);
3019036a4a7dSZhenyu Wang 
30200a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3021036a4a7dSZhenyu Wang 
3022d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
30237fe0b973SKeith Packard 
3024f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
30256005ce42SDaniel Vetter 		/* Enable PCU event interrupts
30266005ce42SDaniel Vetter 		 *
30276005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
30284bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
30294bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
30304bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3031f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
30324bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3033f97108d1SJesse Barnes 	}
3034f97108d1SJesse Barnes 
3035036a4a7dSZhenyu Wang 	return 0;
3036036a4a7dSZhenyu Wang }
3037036a4a7dSZhenyu Wang 
3038f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3039f8b79e58SImre Deak {
3040f8b79e58SImre Deak 	u32 pipestat_mask;
3041f8b79e58SImre Deak 	u32 iir_mask;
3042f8b79e58SImre Deak 
3043f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3044f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3045f8b79e58SImre Deak 
3046f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3047f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3048f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3049f8b79e58SImre Deak 
3050f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3051f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3052f8b79e58SImre Deak 
3053f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3054f8b79e58SImre Deak 					       PIPE_GMBUS_INTERRUPT_STATUS);
3055f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3056f8b79e58SImre Deak 
3057f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3058f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3059f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3060f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3061f8b79e58SImre Deak 
3062f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3063f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3064f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3065f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3066f8b79e58SImre Deak 	POSTING_READ(VLV_IER);
3067f8b79e58SImre Deak }
3068f8b79e58SImre Deak 
3069f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3070f8b79e58SImre Deak {
3071f8b79e58SImre Deak 	u32 pipestat_mask;
3072f8b79e58SImre Deak 	u32 iir_mask;
3073f8b79e58SImre Deak 
3074f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3075f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3076f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
3077f8b79e58SImre Deak 
3078f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3079f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3080f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3081f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3082f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3083f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3084f8b79e58SImre Deak 
3085f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3086f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3087f8b79e58SImre Deak 
3088f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3089f8b79e58SImre Deak 					        PIPE_GMBUS_INTERRUPT_STATUS);
3090f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3091f8b79e58SImre Deak 
3092f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3093f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3094f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3095f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3096f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3097f8b79e58SImre Deak }
3098f8b79e58SImre Deak 
3099f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3100f8b79e58SImre Deak {
3101f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3102f8b79e58SImre Deak 
3103f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3104f8b79e58SImre Deak 		return;
3105f8b79e58SImre Deak 
3106f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3107f8b79e58SImre Deak 
3108f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3109f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3110f8b79e58SImre Deak }
3111f8b79e58SImre Deak 
3112f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3113f8b79e58SImre Deak {
3114f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3115f8b79e58SImre Deak 
3116f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3117f8b79e58SImre Deak 		return;
3118f8b79e58SImre Deak 
3119f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3120f8b79e58SImre Deak 
3121f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3122f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3123f8b79e58SImre Deak }
3124f8b79e58SImre Deak 
31257e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
31267e231dbeSJesse Barnes {
31277e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3128b79480baSDaniel Vetter 	unsigned long irqflags;
31297e231dbeSJesse Barnes 
3130f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
31317e231dbeSJesse Barnes 
313220afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
313320afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
313420afbda2SDaniel Vetter 
31357e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3136f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
31377e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31387e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
31397e231dbeSJesse Barnes 
3140b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3141b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3142b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3143f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3144f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3145b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
314631acc7f5SJesse Barnes 
31477e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31487e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31497e231dbeSJesse Barnes 
31500a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
31517e231dbeSJesse Barnes 
31527e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
31537e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
31547e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
31557e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
31567e231dbeSJesse Barnes #endif
31577e231dbeSJesse Barnes 
31587e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
315920afbda2SDaniel Vetter 
316020afbda2SDaniel Vetter 	return 0;
316120afbda2SDaniel Vetter }
316220afbda2SDaniel Vetter 
3163abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3164abd58f01SBen Widawsky {
3165abd58f01SBen Widawsky 	int i;
3166abd58f01SBen Widawsky 
3167abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3168abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3169abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3170abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
3171abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3172abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
3173abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3174abd58f01SBen Widawsky 		0,
3175abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3176abd58f01SBen Widawsky 		};
3177abd58f01SBen Widawsky 
3178abd58f01SBen Widawsky 	for (i = 0; i < ARRAY_SIZE(gt_interrupts); i++) {
3179abd58f01SBen Widawsky 		u32 tmp = I915_READ(GEN8_GT_IIR(i));
3180abd58f01SBen Widawsky 		if (tmp)
3181abd58f01SBen Widawsky 			DRM_ERROR("Interrupt (%d) should have been masked in pre-install 0x%08x\n",
3182abd58f01SBen Widawsky 				  i, tmp);
3183abd58f01SBen Widawsky 		I915_WRITE(GEN8_GT_IMR(i), ~gt_interrupts[i]);
3184abd58f01SBen Widawsky 		I915_WRITE(GEN8_GT_IER(i), gt_interrupts[i]);
3185abd58f01SBen Widawsky 	}
3186abd58f01SBen Widawsky 	POSTING_READ(GEN8_GT_IER(0));
3187abd58f01SBen Widawsky }
3188abd58f01SBen Widawsky 
3189abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3190abd58f01SBen Widawsky {
3191abd58f01SBen Widawsky 	struct drm_device *dev = dev_priv->dev;
319213b3a0a7SDaniel Vetter 	uint32_t de_pipe_masked = GEN8_PIPE_FLIP_DONE |
31930fbe7870SDaniel Vetter 		GEN8_PIPE_CDCLK_CRC_DONE |
319438d83c96SDaniel Vetter 		GEN8_PIPE_FIFO_UNDERRUN |
319530100f2bSDaniel Vetter 		GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
319613b3a0a7SDaniel Vetter 	uint32_t de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK;
3197abd58f01SBen Widawsky 	int pipe;
319813b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
319913b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
320013b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3201abd58f01SBen Widawsky 
3202abd58f01SBen Widawsky 	for_each_pipe(pipe) {
3203abd58f01SBen Widawsky 		u32 tmp = I915_READ(GEN8_DE_PIPE_IIR(pipe));
3204abd58f01SBen Widawsky 		if (tmp)
3205abd58f01SBen Widawsky 			DRM_ERROR("Interrupt (%d) should have been masked in pre-install 0x%08x\n",
3206abd58f01SBen Widawsky 				  pipe, tmp);
3207abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
3208abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IER(pipe), de_pipe_enables);
3209abd58f01SBen Widawsky 	}
3210abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_ISR(0));
3211abd58f01SBen Widawsky 
32126d766f02SDaniel Vetter 	I915_WRITE(GEN8_DE_PORT_IMR, ~GEN8_AUX_CHANNEL_A);
32136d766f02SDaniel Vetter 	I915_WRITE(GEN8_DE_PORT_IER, GEN8_AUX_CHANNEL_A);
3214abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PORT_IER);
3215abd58f01SBen Widawsky }
3216abd58f01SBen Widawsky 
3217abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3218abd58f01SBen Widawsky {
3219abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3220abd58f01SBen Widawsky 
3221abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3222abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3223abd58f01SBen Widawsky 
3224abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3225abd58f01SBen Widawsky 
3226abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3227abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3228abd58f01SBen Widawsky 
3229abd58f01SBen Widawsky 	return 0;
3230abd58f01SBen Widawsky }
3231abd58f01SBen Widawsky 
3232abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3233abd58f01SBen Widawsky {
3234abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3235abd58f01SBen Widawsky 	int pipe;
3236abd58f01SBen Widawsky 
3237abd58f01SBen Widawsky 	if (!dev_priv)
3238abd58f01SBen Widawsky 		return;
3239abd58f01SBen Widawsky 
3240abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3241abd58f01SBen Widawsky 
3242abd58f01SBen Widawsky #define GEN8_IRQ_FINI_NDX(type, which) do { \
3243abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
3244abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER(which), 0); \
3245abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
3246abd58f01SBen Widawsky 	} while (0)
3247abd58f01SBen Widawsky 
3248abd58f01SBen Widawsky #define GEN8_IRQ_FINI(type) do { \
3249abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
3250abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER, 0); \
3251abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
3252abd58f01SBen Widawsky 	} while (0)
3253abd58f01SBen Widawsky 
3254abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 0);
3255abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 1);
3256abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 2);
3257abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 3);
3258abd58f01SBen Widawsky 
3259abd58f01SBen Widawsky 	for_each_pipe(pipe) {
3260abd58f01SBen Widawsky 		GEN8_IRQ_FINI_NDX(DE_PIPE, pipe);
3261abd58f01SBen Widawsky 	}
3262abd58f01SBen Widawsky 
3263abd58f01SBen Widawsky 	GEN8_IRQ_FINI(DE_PORT);
3264abd58f01SBen Widawsky 	GEN8_IRQ_FINI(DE_MISC);
3265abd58f01SBen Widawsky 	GEN8_IRQ_FINI(PCU);
3266abd58f01SBen Widawsky #undef GEN8_IRQ_FINI
3267abd58f01SBen Widawsky #undef GEN8_IRQ_FINI_NDX
3268abd58f01SBen Widawsky 
3269abd58f01SBen Widawsky 	POSTING_READ(GEN8_PCU_IIR);
3270abd58f01SBen Widawsky }
3271abd58f01SBen Widawsky 
32727e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
32737e231dbeSJesse Barnes {
32747e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3275f8b79e58SImre Deak 	unsigned long irqflags;
32767e231dbeSJesse Barnes 	int pipe;
32777e231dbeSJesse Barnes 
32787e231dbeSJesse Barnes 	if (!dev_priv)
32797e231dbeSJesse Barnes 		return;
32807e231dbeSJesse Barnes 
32813ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3282ac4c16c5SEgbert Eich 
32837e231dbeSJesse Barnes 	for_each_pipe(pipe)
32847e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
32857e231dbeSJesse Barnes 
32867e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
32877e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
32887e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3289f8b79e58SImre Deak 
3290f8b79e58SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3291f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3292f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3293f8b79e58SImre Deak 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3294f8b79e58SImre Deak 
3295f8b79e58SImre Deak 	dev_priv->irq_mask = 0;
3296f8b79e58SImre Deak 
32977e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
32987e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
32997e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
33007e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
33017e231dbeSJesse Barnes }
33027e231dbeSJesse Barnes 
3303f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3304036a4a7dSZhenyu Wang {
3305036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
33064697995bSJesse Barnes 
33074697995bSJesse Barnes 	if (!dev_priv)
33084697995bSJesse Barnes 		return;
33094697995bSJesse Barnes 
33103ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3311ac4c16c5SEgbert Eich 
3312036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
3313036a4a7dSZhenyu Wang 
3314036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
3315036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
3316036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
33178664281bSPaulo Zanoni 	if (IS_GEN7(dev))
33188664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
3319036a4a7dSZhenyu Wang 
3320036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
3321036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
3322036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
3323192aac1fSKeith Packard 
3324ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
3325ab5c608bSBen Widawsky 		return;
3326ab5c608bSBen Widawsky 
3327192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
3328192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
3329192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
33308664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
33318664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
3332036a4a7dSZhenyu Wang }
3333036a4a7dSZhenyu Wang 
3334c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3335c2798b19SChris Wilson {
3336c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3337c2798b19SChris Wilson 	int pipe;
3338c2798b19SChris Wilson 
3339c2798b19SChris Wilson 	for_each_pipe(pipe)
3340c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3341c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3342c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3343c2798b19SChris Wilson 	POSTING_READ16(IER);
3344c2798b19SChris Wilson }
3345c2798b19SChris Wilson 
3346c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3347c2798b19SChris Wilson {
3348c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3349379ef82dSDaniel Vetter 	unsigned long irqflags;
3350c2798b19SChris Wilson 
3351c2798b19SChris Wilson 	I915_WRITE16(EMR,
3352c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3353c2798b19SChris Wilson 
3354c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3355c2798b19SChris Wilson 	dev_priv->irq_mask =
3356c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3357c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3358c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3359c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3360c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3361c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3362c2798b19SChris Wilson 
3363c2798b19SChris Wilson 	I915_WRITE16(IER,
3364c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3365c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3366c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3367c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3368c2798b19SChris Wilson 	POSTING_READ16(IER);
3369c2798b19SChris Wilson 
3370379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3371379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3372379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3373755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3374755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3375379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3376379ef82dSDaniel Vetter 
3377c2798b19SChris Wilson 	return 0;
3378c2798b19SChris Wilson }
3379c2798b19SChris Wilson 
338090a72f87SVille Syrjälä /*
338190a72f87SVille Syrjälä  * Returns true when a page flip has completed.
338290a72f87SVille Syrjälä  */
338390a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
33841f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
338590a72f87SVille Syrjälä {
338690a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
33871f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
338890a72f87SVille Syrjälä 
338990a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
339090a72f87SVille Syrjälä 		return false;
339190a72f87SVille Syrjälä 
339290a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
339390a72f87SVille Syrjälä 		return false;
339490a72f87SVille Syrjälä 
33951f1c2e24SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
339690a72f87SVille Syrjälä 
339790a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
339890a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
339990a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
340090a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
340190a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
340290a72f87SVille Syrjälä 	 */
340390a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
340490a72f87SVille Syrjälä 		return false;
340590a72f87SVille Syrjälä 
340690a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
340790a72f87SVille Syrjälä 
340890a72f87SVille Syrjälä 	return true;
340990a72f87SVille Syrjälä }
341090a72f87SVille Syrjälä 
3411ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3412c2798b19SChris Wilson {
3413c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3414c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3415c2798b19SChris Wilson 	u16 iir, new_iir;
3416c2798b19SChris Wilson 	u32 pipe_stats[2];
3417c2798b19SChris Wilson 	unsigned long irqflags;
3418c2798b19SChris Wilson 	int pipe;
3419c2798b19SChris Wilson 	u16 flip_mask =
3420c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3421c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3422c2798b19SChris Wilson 
3423c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3424c2798b19SChris Wilson 	if (iir == 0)
3425c2798b19SChris Wilson 		return IRQ_NONE;
3426c2798b19SChris Wilson 
3427c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3428c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3429c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3430c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3431c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3432c2798b19SChris Wilson 		 */
3433c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3434c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
343558174462SMika Kuoppala 			i915_handle_error(dev, false,
343658174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
343758174462SMika Kuoppala 					  iir);
3438c2798b19SChris Wilson 
3439c2798b19SChris Wilson 		for_each_pipe(pipe) {
3440c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3441c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3442c2798b19SChris Wilson 
3443c2798b19SChris Wilson 			/*
3444c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3445c2798b19SChris Wilson 			 */
34462d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3447c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3448c2798b19SChris Wilson 		}
3449c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3450c2798b19SChris Wilson 
3451c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3452c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3453c2798b19SChris Wilson 
3454d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3455c2798b19SChris Wilson 
3456c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3457c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3458c2798b19SChris Wilson 
34594356d586SDaniel Vetter 		for_each_pipe(pipe) {
34601f1c2e24SVille Syrjälä 			int plane = pipe;
34613a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
34621f1c2e24SVille Syrjälä 				plane = !plane;
34631f1c2e24SVille Syrjälä 
34644356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
34651f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
34661f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3467c2798b19SChris Wilson 
34684356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3469277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
34702d9d2b0bSVille Syrjälä 
34712d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
34722d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3473fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
34744356d586SDaniel Vetter 		}
3475c2798b19SChris Wilson 
3476c2798b19SChris Wilson 		iir = new_iir;
3477c2798b19SChris Wilson 	}
3478c2798b19SChris Wilson 
3479c2798b19SChris Wilson 	return IRQ_HANDLED;
3480c2798b19SChris Wilson }
3481c2798b19SChris Wilson 
3482c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3483c2798b19SChris Wilson {
3484c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3485c2798b19SChris Wilson 	int pipe;
3486c2798b19SChris Wilson 
3487c2798b19SChris Wilson 	for_each_pipe(pipe) {
3488c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3489c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3490c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3491c2798b19SChris Wilson 	}
3492c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3493c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3494c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3495c2798b19SChris Wilson }
3496c2798b19SChris Wilson 
3497a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3498a266c7d5SChris Wilson {
3499a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3500a266c7d5SChris Wilson 	int pipe;
3501a266c7d5SChris Wilson 
3502a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3503a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3504a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3505a266c7d5SChris Wilson 	}
3506a266c7d5SChris Wilson 
350700d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3508a266c7d5SChris Wilson 	for_each_pipe(pipe)
3509a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3510a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3511a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3512a266c7d5SChris Wilson 	POSTING_READ(IER);
3513a266c7d5SChris Wilson }
3514a266c7d5SChris Wilson 
3515a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3516a266c7d5SChris Wilson {
3517a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
351838bde180SChris Wilson 	u32 enable_mask;
3519379ef82dSDaniel Vetter 	unsigned long irqflags;
3520a266c7d5SChris Wilson 
352138bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
352238bde180SChris Wilson 
352338bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
352438bde180SChris Wilson 	dev_priv->irq_mask =
352538bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
352638bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
352738bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
352838bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
352938bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
353038bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
353138bde180SChris Wilson 
353238bde180SChris Wilson 	enable_mask =
353338bde180SChris Wilson 		I915_ASLE_INTERRUPT |
353438bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
353538bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
353638bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
353738bde180SChris Wilson 		I915_USER_INTERRUPT;
353838bde180SChris Wilson 
3539a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
354020afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
354120afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
354220afbda2SDaniel Vetter 
3543a266c7d5SChris Wilson 		/* Enable in IER... */
3544a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3545a266c7d5SChris Wilson 		/* and unmask in IMR */
3546a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3547a266c7d5SChris Wilson 	}
3548a266c7d5SChris Wilson 
3549a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3550a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3551a266c7d5SChris Wilson 	POSTING_READ(IER);
3552a266c7d5SChris Wilson 
3553f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
355420afbda2SDaniel Vetter 
3555379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3556379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3557379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3558755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3559755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3560379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3561379ef82dSDaniel Vetter 
356220afbda2SDaniel Vetter 	return 0;
356320afbda2SDaniel Vetter }
356420afbda2SDaniel Vetter 
356590a72f87SVille Syrjälä /*
356690a72f87SVille Syrjälä  * Returns true when a page flip has completed.
356790a72f87SVille Syrjälä  */
356890a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
356990a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
357090a72f87SVille Syrjälä {
357190a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
357290a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
357390a72f87SVille Syrjälä 
357490a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
357590a72f87SVille Syrjälä 		return false;
357690a72f87SVille Syrjälä 
357790a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
357890a72f87SVille Syrjälä 		return false;
357990a72f87SVille Syrjälä 
358090a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
358190a72f87SVille Syrjälä 
358290a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
358390a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
358490a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
358590a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
358690a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
358790a72f87SVille Syrjälä 	 */
358890a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
358990a72f87SVille Syrjälä 		return false;
359090a72f87SVille Syrjälä 
359190a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
359290a72f87SVille Syrjälä 
359390a72f87SVille Syrjälä 	return true;
359490a72f87SVille Syrjälä }
359590a72f87SVille Syrjälä 
3596ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3597a266c7d5SChris Wilson {
3598a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3599a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
36008291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3601a266c7d5SChris Wilson 	unsigned long irqflags;
360238bde180SChris Wilson 	u32 flip_mask =
360338bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
360438bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
360538bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3606a266c7d5SChris Wilson 
3607a266c7d5SChris Wilson 	iir = I915_READ(IIR);
360838bde180SChris Wilson 	do {
360938bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
36108291ee90SChris Wilson 		bool blc_event = false;
3611a266c7d5SChris Wilson 
3612a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3613a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3614a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3615a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3616a266c7d5SChris Wilson 		 */
3617a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3618a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
361958174462SMika Kuoppala 			i915_handle_error(dev, false,
362058174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
362158174462SMika Kuoppala 					  iir);
3622a266c7d5SChris Wilson 
3623a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3624a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3625a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3626a266c7d5SChris Wilson 
362738bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3628a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3629a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
363038bde180SChris Wilson 				irq_received = true;
3631a266c7d5SChris Wilson 			}
3632a266c7d5SChris Wilson 		}
3633a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3634a266c7d5SChris Wilson 
3635a266c7d5SChris Wilson 		if (!irq_received)
3636a266c7d5SChris Wilson 			break;
3637a266c7d5SChris Wilson 
3638a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3639a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3640a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3641a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3642b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3643a266c7d5SChris Wilson 
364410a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
364591d131d2SDaniel Vetter 
3646a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
364738bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3648a266c7d5SChris Wilson 		}
3649a266c7d5SChris Wilson 
365038bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3651a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3652a266c7d5SChris Wilson 
3653a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3654a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3655a266c7d5SChris Wilson 
3656a266c7d5SChris Wilson 		for_each_pipe(pipe) {
365738bde180SChris Wilson 			int plane = pipe;
36583a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
365938bde180SChris Wilson 				plane = !plane;
36605e2032d4SVille Syrjälä 
366190a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
366290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
366390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3664a266c7d5SChris Wilson 
3665a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3666a266c7d5SChris Wilson 				blc_event = true;
36674356d586SDaniel Vetter 
36684356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3669277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
36702d9d2b0bSVille Syrjälä 
36712d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
36722d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3673fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
3674a266c7d5SChris Wilson 		}
3675a266c7d5SChris Wilson 
3676a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3677a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3678a266c7d5SChris Wilson 
3679a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3680a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3681a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3682a266c7d5SChris Wilson 		 * we would never get another interrupt.
3683a266c7d5SChris Wilson 		 *
3684a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3685a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3686a266c7d5SChris Wilson 		 * another one.
3687a266c7d5SChris Wilson 		 *
3688a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3689a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3690a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3691a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3692a266c7d5SChris Wilson 		 * stray interrupts.
3693a266c7d5SChris Wilson 		 */
369438bde180SChris Wilson 		ret = IRQ_HANDLED;
3695a266c7d5SChris Wilson 		iir = new_iir;
369638bde180SChris Wilson 	} while (iir & ~flip_mask);
3697a266c7d5SChris Wilson 
3698d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
36998291ee90SChris Wilson 
3700a266c7d5SChris Wilson 	return ret;
3701a266c7d5SChris Wilson }
3702a266c7d5SChris Wilson 
3703a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3704a266c7d5SChris Wilson {
3705a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3706a266c7d5SChris Wilson 	int pipe;
3707a266c7d5SChris Wilson 
37083ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3709ac4c16c5SEgbert Eich 
3710a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3711a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3712a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3713a266c7d5SChris Wilson 	}
3714a266c7d5SChris Wilson 
371500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
371655b39755SChris Wilson 	for_each_pipe(pipe) {
371755b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3718a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
371955b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
372055b39755SChris Wilson 	}
3721a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3722a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3723a266c7d5SChris Wilson 
3724a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3725a266c7d5SChris Wilson }
3726a266c7d5SChris Wilson 
3727a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3728a266c7d5SChris Wilson {
3729a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3730a266c7d5SChris Wilson 	int pipe;
3731a266c7d5SChris Wilson 
3732a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3733a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3734a266c7d5SChris Wilson 
3735a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3736a266c7d5SChris Wilson 	for_each_pipe(pipe)
3737a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3738a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3739a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3740a266c7d5SChris Wilson 	POSTING_READ(IER);
3741a266c7d5SChris Wilson }
3742a266c7d5SChris Wilson 
3743a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3744a266c7d5SChris Wilson {
3745a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3746bbba0a97SChris Wilson 	u32 enable_mask;
3747a266c7d5SChris Wilson 	u32 error_mask;
3748b79480baSDaniel Vetter 	unsigned long irqflags;
3749a266c7d5SChris Wilson 
3750a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3751bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3752adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3753bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3754bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3755bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3756bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3757bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3758bbba0a97SChris Wilson 
3759bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
376021ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
376121ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3762bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3763bbba0a97SChris Wilson 
3764bbba0a97SChris Wilson 	if (IS_G4X(dev))
3765bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3766a266c7d5SChris Wilson 
3767b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3768b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3769b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3770755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3771755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3772755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3773b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3774a266c7d5SChris Wilson 
3775a266c7d5SChris Wilson 	/*
3776a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3777a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3778a266c7d5SChris Wilson 	 */
3779a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3780a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3781a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3782a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3783a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3784a266c7d5SChris Wilson 	} else {
3785a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3786a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3787a266c7d5SChris Wilson 	}
3788a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3789a266c7d5SChris Wilson 
3790a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3791a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3792a266c7d5SChris Wilson 	POSTING_READ(IER);
3793a266c7d5SChris Wilson 
379420afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
379520afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
379620afbda2SDaniel Vetter 
3797f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
379820afbda2SDaniel Vetter 
379920afbda2SDaniel Vetter 	return 0;
380020afbda2SDaniel Vetter }
380120afbda2SDaniel Vetter 
3802bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
380320afbda2SDaniel Vetter {
380420afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3805e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3806cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
380720afbda2SDaniel Vetter 	u32 hotplug_en;
380820afbda2SDaniel Vetter 
3809b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3810b5ea2d56SDaniel Vetter 
3811bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3812bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3813bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3814adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3815e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3816cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3817cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3818cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3819a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3820a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3821a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3822a266c7d5SChris Wilson 		*/
3823a266c7d5SChris Wilson 		if (IS_G4X(dev))
3824a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
382585fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3826a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3827a266c7d5SChris Wilson 
3828a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3829a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3830a266c7d5SChris Wilson 	}
3831bac56d5bSEgbert Eich }
3832a266c7d5SChris Wilson 
3833ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3834a266c7d5SChris Wilson {
3835a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3836a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3837a266c7d5SChris Wilson 	u32 iir, new_iir;
3838a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3839a266c7d5SChris Wilson 	unsigned long irqflags;
3840a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
384121ad8330SVille Syrjälä 	u32 flip_mask =
384221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
384321ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3844a266c7d5SChris Wilson 
3845a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3846a266c7d5SChris Wilson 
3847a266c7d5SChris Wilson 	for (;;) {
3848501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
38492c8ba29fSChris Wilson 		bool blc_event = false;
38502c8ba29fSChris Wilson 
3851a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3852a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3853a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3854a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3855a266c7d5SChris Wilson 		 */
3856a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3857a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
385858174462SMika Kuoppala 			i915_handle_error(dev, false,
385958174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
386058174462SMika Kuoppala 					  iir);
3861a266c7d5SChris Wilson 
3862a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3863a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3864a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3865a266c7d5SChris Wilson 
3866a266c7d5SChris Wilson 			/*
3867a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3868a266c7d5SChris Wilson 			 */
3869a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3870a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3871501e01d7SVille Syrjälä 				irq_received = true;
3872a266c7d5SChris Wilson 			}
3873a266c7d5SChris Wilson 		}
3874a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3875a266c7d5SChris Wilson 
3876a266c7d5SChris Wilson 		if (!irq_received)
3877a266c7d5SChris Wilson 			break;
3878a266c7d5SChris Wilson 
3879a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3880a266c7d5SChris Wilson 
3881a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3882adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3883a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3884b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3885b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
38864f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3887a266c7d5SChris Wilson 
388810a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
3889704cfb87SDaniel Vetter 					      IS_G4X(dev) ? hpd_status_g4x : hpd_status_i915);
389091d131d2SDaniel Vetter 
38914aeebd74SDaniel Vetter 			if (IS_G4X(dev) &&
38924aeebd74SDaniel Vetter 			    (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X))
38934aeebd74SDaniel Vetter 				dp_aux_irq_handler(dev);
38944aeebd74SDaniel Vetter 
3895a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3896a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3897a266c7d5SChris Wilson 		}
3898a266c7d5SChris Wilson 
389921ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3900a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3901a266c7d5SChris Wilson 
3902a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3903a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3904a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3905a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3906a266c7d5SChris Wilson 
3907a266c7d5SChris Wilson 		for_each_pipe(pipe) {
39082c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
390990a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
391090a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3911a266c7d5SChris Wilson 
3912a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3913a266c7d5SChris Wilson 				blc_event = true;
39144356d586SDaniel Vetter 
39154356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3916277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
3917a266c7d5SChris Wilson 
39182d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
39192d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3920fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
39212d9d2b0bSVille Syrjälä 		}
3922a266c7d5SChris Wilson 
3923a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3924a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3925a266c7d5SChris Wilson 
3926515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3927515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3928515ac2bbSDaniel Vetter 
3929a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3930a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3931a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3932a266c7d5SChris Wilson 		 * we would never get another interrupt.
3933a266c7d5SChris Wilson 		 *
3934a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3935a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3936a266c7d5SChris Wilson 		 * another one.
3937a266c7d5SChris Wilson 		 *
3938a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3939a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3940a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3941a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3942a266c7d5SChris Wilson 		 * stray interrupts.
3943a266c7d5SChris Wilson 		 */
3944a266c7d5SChris Wilson 		iir = new_iir;
3945a266c7d5SChris Wilson 	}
3946a266c7d5SChris Wilson 
3947d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
39482c8ba29fSChris Wilson 
3949a266c7d5SChris Wilson 	return ret;
3950a266c7d5SChris Wilson }
3951a266c7d5SChris Wilson 
3952a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3953a266c7d5SChris Wilson {
3954a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3955a266c7d5SChris Wilson 	int pipe;
3956a266c7d5SChris Wilson 
3957a266c7d5SChris Wilson 	if (!dev_priv)
3958a266c7d5SChris Wilson 		return;
3959a266c7d5SChris Wilson 
39603ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3961ac4c16c5SEgbert Eich 
3962a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3963a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3964a266c7d5SChris Wilson 
3965a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3966a266c7d5SChris Wilson 	for_each_pipe(pipe)
3967a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3968a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3969a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3970a266c7d5SChris Wilson 
3971a266c7d5SChris Wilson 	for_each_pipe(pipe)
3972a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3973a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3974a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3975a266c7d5SChris Wilson }
3976a266c7d5SChris Wilson 
39773ca1ccedSVille Syrjälä static void intel_hpd_irq_reenable(unsigned long data)
3978ac4c16c5SEgbert Eich {
3979ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3980ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3981ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3982ac4c16c5SEgbert Eich 	unsigned long irqflags;
3983ac4c16c5SEgbert Eich 	int i;
3984ac4c16c5SEgbert Eich 
3985ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3986ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3987ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3988ac4c16c5SEgbert Eich 
3989ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3990ac4c16c5SEgbert Eich 			continue;
3991ac4c16c5SEgbert Eich 
3992ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3993ac4c16c5SEgbert Eich 
3994ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3995ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3996ac4c16c5SEgbert Eich 
3997ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3998ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3999ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4000ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
4001ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4002ac4c16c5SEgbert Eich 				if (!connector->polled)
4003ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4004ac4c16c5SEgbert Eich 			}
4005ac4c16c5SEgbert Eich 		}
4006ac4c16c5SEgbert Eich 	}
4007ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4008ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
4009ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4010ac4c16c5SEgbert Eich }
4011ac4c16c5SEgbert Eich 
4012f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
4013f71d4af4SJesse Barnes {
40148b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
40158b2e326dSChris Wilson 
40168b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
401799584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
4018c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4019a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
40208b2e326dSChris Wilson 
402199584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
402299584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
402361bac78eSDaniel Vetter 		    (unsigned long) dev);
40243ca1ccedSVille Syrjälä 	setup_timer(&dev_priv->hotplug_reenable_timer, intel_hpd_irq_reenable,
4025ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
402661bac78eSDaniel Vetter 
402797a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
40289ee32feaSDaniel Vetter 
40294cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
40304cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
40314cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
40324cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
4033f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4034f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4035391f75e2SVille Syrjälä 	} else {
4036391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4037391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4038f71d4af4SJesse Barnes 	}
4039f71d4af4SJesse Barnes 
4040c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
4041f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4042f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4043c2baf4b7SVille Syrjälä 	}
4044f71d4af4SJesse Barnes 
40457e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
40467e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
40477e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
40487e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
40497e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
40507e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
40517e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4052fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4053abd58f01SBen Widawsky 	} else if (IS_GEN8(dev)) {
4054abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4055abd58f01SBen Widawsky 		dev->driver->irq_preinstall = gen8_irq_preinstall;
4056abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4057abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4058abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4059abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4060abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4061f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4062f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4063f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
4064f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4065f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4066f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4067f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
406882a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4069f71d4af4SJesse Barnes 	} else {
4070c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
4071c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4072c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4073c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4074c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4075a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
4076a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4077a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4078a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4079a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
408020afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4081c2798b19SChris Wilson 		} else {
4082a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4083a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4084a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4085a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4086bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4087c2798b19SChris Wilson 		}
4088f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4089f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4090f71d4af4SJesse Barnes 	}
4091f71d4af4SJesse Barnes }
409220afbda2SDaniel Vetter 
409320afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
409420afbda2SDaniel Vetter {
409520afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
4096821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4097821450c6SEgbert Eich 	struct drm_connector *connector;
4098b5ea2d56SDaniel Vetter 	unsigned long irqflags;
4099821450c6SEgbert Eich 	int i;
410020afbda2SDaniel Vetter 
4101821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4102821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4103821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4104821450c6SEgbert Eich 	}
4105821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4106821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4107821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
4108821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
4109821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4110821450c6SEgbert Eich 	}
4111b5ea2d56SDaniel Vetter 
4112b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4113b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4114b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
411520afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
411620afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4117b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
411820afbda2SDaniel Vetter }
4119c67a470bSPaulo Zanoni 
4120c67a470bSPaulo Zanoni /* Disable interrupts so we can allow Package C8+. */
4121c67a470bSPaulo Zanoni void hsw_pc8_disable_interrupts(struct drm_device *dev)
4122c67a470bSPaulo Zanoni {
4123c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4124c67a470bSPaulo Zanoni 	unsigned long irqflags;
4125c67a470bSPaulo Zanoni 
4126c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4127c67a470bSPaulo Zanoni 
4128c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.deimr = I915_READ(DEIMR);
4129c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.sdeimr = I915_READ(SDEIMR);
4130c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtimr = I915_READ(GTIMR);
4131c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtier = I915_READ(GTIER);
4132c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
4133c67a470bSPaulo Zanoni 
41341f2d4531SPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, 0xffffffff);
41351f2d4531SPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, 0xffffffff);
4136c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
4137c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
4138c67a470bSPaulo Zanoni 
4139c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = true;
4140c67a470bSPaulo Zanoni 
4141c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4142c67a470bSPaulo Zanoni }
4143c67a470bSPaulo Zanoni 
4144c67a470bSPaulo Zanoni /* Restore interrupts so we can recover from Package C8+. */
4145c67a470bSPaulo Zanoni void hsw_pc8_restore_interrupts(struct drm_device *dev)
4146c67a470bSPaulo Zanoni {
4147c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4148c67a470bSPaulo Zanoni 	unsigned long irqflags;
41491f2d4531SPaulo Zanoni 	uint32_t val;
4150c67a470bSPaulo Zanoni 
4151c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4152c67a470bSPaulo Zanoni 
4153c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
41541f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "DEIMR is 0x%08x\n", val);
4155c67a470bSPaulo Zanoni 
41561f2d4531SPaulo Zanoni 	val = I915_READ(SDEIMR);
41571f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "SDEIMR is 0x%08x\n", val);
4158c67a470bSPaulo Zanoni 
4159c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
41601f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "GTIMR is 0x%08x\n", val);
4161c67a470bSPaulo Zanoni 
4162c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
41631f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "GEN6_PMIMR is 0x%08x\n", val);
4164c67a470bSPaulo Zanoni 
4165c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = false;
4166c67a470bSPaulo Zanoni 
4167c67a470bSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pc8.regsave.deimr);
41681f2d4531SPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv, ~dev_priv->pc8.regsave.sdeimr);
4169c67a470bSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pc8.regsave.gtimr);
4170c67a470bSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pc8.regsave.gen6_pmimr);
4171c67a470bSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pc8.regsave.gtier);
4172c67a470bSPaulo Zanoni 
4173c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4174c67a470bSPaulo Zanoni }
4175