xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 4aeebd7443e36b0a40032e518a9338f48bd27efc)
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 
65e5868a31SEgbert Eich static const u32 hpd_status_gen4[] = {
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 
2358664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
2368664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
2378664281bSPaulo Zanoni {
2388664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2398664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
2408664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
2418664281bSPaulo Zanoni 
2428664281bSPaulo Zanoni 	if (enable)
2438664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
2448664281bSPaulo Zanoni 	else
2458664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
2468664281bSPaulo Zanoni }
2478664281bSPaulo Zanoni 
2488664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
2497336df65SDaniel Vetter 						  enum pipe pipe, bool enable)
2508664281bSPaulo Zanoni {
2518664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2528664281bSPaulo Zanoni 	if (enable) {
2537336df65SDaniel Vetter 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
2547336df65SDaniel Vetter 
2558664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
2568664281bSPaulo Zanoni 			return;
2578664281bSPaulo Zanoni 
2588664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
2598664281bSPaulo Zanoni 	} else {
2607336df65SDaniel Vetter 		bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
2617336df65SDaniel Vetter 
2627336df65SDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
2638664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
2647336df65SDaniel Vetter 
2657336df65SDaniel Vetter 		if (!was_enabled &&
2667336df65SDaniel Vetter 		    (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
2677336df65SDaniel Vetter 			DRM_DEBUG_KMS("uncleared fifo underrun on pipe %c\n",
2687336df65SDaniel Vetter 				      pipe_name(pipe));
2697336df65SDaniel Vetter 		}
2708664281bSPaulo Zanoni 	}
2718664281bSPaulo Zanoni }
2728664281bSPaulo Zanoni 
27338d83c96SDaniel Vetter static void broadwell_set_fifo_underrun_reporting(struct drm_device *dev,
27438d83c96SDaniel Vetter 						  enum pipe pipe, bool enable)
27538d83c96SDaniel Vetter {
27638d83c96SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
27738d83c96SDaniel Vetter 
27838d83c96SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
27938d83c96SDaniel Vetter 
28038d83c96SDaniel Vetter 	if (enable)
28138d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_FIFO_UNDERRUN;
28238d83c96SDaniel Vetter 	else
28338d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_FIFO_UNDERRUN;
28438d83c96SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
28538d83c96SDaniel Vetter 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
28638d83c96SDaniel Vetter }
28738d83c96SDaniel Vetter 
288fee884edSDaniel Vetter /**
289fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
290fee884edSDaniel Vetter  * @dev_priv: driver private
291fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
292fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
293fee884edSDaniel Vetter  */
294fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
295fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
296fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
297fee884edSDaniel Vetter {
298fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
299fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
300fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
301fee884edSDaniel Vetter 
302fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
303fee884edSDaniel Vetter 
304c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled &&
305c67a470bSPaulo Zanoni 	    (interrupt_mask & SDE_HOTPLUG_MASK_CPT)) {
306c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
307c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr &= ~interrupt_mask;
308c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr |= (~enabled_irq_mask &
309c67a470bSPaulo Zanoni 						 interrupt_mask);
310c67a470bSPaulo Zanoni 		return;
311c67a470bSPaulo Zanoni 	}
312c67a470bSPaulo Zanoni 
313fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
314fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
315fee884edSDaniel Vetter }
316fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
317fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
318fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
319fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
320fee884edSDaniel Vetter 
321de28075dSDaniel Vetter static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
322de28075dSDaniel Vetter 					    enum transcoder pch_transcoder,
3238664281bSPaulo Zanoni 					    bool enable)
3248664281bSPaulo Zanoni {
3258664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
326de28075dSDaniel Vetter 	uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
327de28075dSDaniel Vetter 		       SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
3288664281bSPaulo Zanoni 
3298664281bSPaulo Zanoni 	if (enable)
330fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
3318664281bSPaulo Zanoni 	else
332fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
3338664281bSPaulo Zanoni }
3348664281bSPaulo Zanoni 
3358664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
3368664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
3378664281bSPaulo Zanoni 					    bool enable)
3388664281bSPaulo Zanoni {
3398664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3408664281bSPaulo Zanoni 
3418664281bSPaulo Zanoni 	if (enable) {
3421dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
3431dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
3441dd246fbSDaniel Vetter 
3458664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
3468664281bSPaulo Zanoni 			return;
3478664281bSPaulo Zanoni 
348fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3498664281bSPaulo Zanoni 	} else {
3501dd246fbSDaniel Vetter 		uint32_t tmp = I915_READ(SERR_INT);
3511dd246fbSDaniel Vetter 		bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
3521dd246fbSDaniel Vetter 
3531dd246fbSDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
354fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3551dd246fbSDaniel Vetter 
3561dd246fbSDaniel Vetter 		if (!was_enabled &&
3571dd246fbSDaniel Vetter 		    (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
3581dd246fbSDaniel Vetter 			DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
3591dd246fbSDaniel Vetter 				      transcoder_name(pch_transcoder));
3601dd246fbSDaniel Vetter 		}
3618664281bSPaulo Zanoni 	}
3628664281bSPaulo Zanoni }
3638664281bSPaulo Zanoni 
3648664281bSPaulo Zanoni /**
3658664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
3668664281bSPaulo Zanoni  * @dev: drm device
3678664281bSPaulo Zanoni  * @pipe: pipe
3688664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
3698664281bSPaulo Zanoni  *
3708664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
3718664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
3728664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
3738664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
3748664281bSPaulo Zanoni  * bit for all the pipes.
3758664281bSPaulo Zanoni  *
3768664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
3778664281bSPaulo Zanoni  */
3788664281bSPaulo Zanoni bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
3798664281bSPaulo Zanoni 					   enum pipe pipe, bool enable)
3808664281bSPaulo Zanoni {
3818664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3828664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
3838664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3848664281bSPaulo Zanoni 	unsigned long flags;
3858664281bSPaulo Zanoni 	bool ret;
3868664281bSPaulo Zanoni 
3878664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
3888664281bSPaulo Zanoni 
3898664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
3908664281bSPaulo Zanoni 
3918664281bSPaulo Zanoni 	if (enable == ret)
3928664281bSPaulo Zanoni 		goto done;
3938664281bSPaulo Zanoni 
3948664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
3958664281bSPaulo Zanoni 
3968664281bSPaulo Zanoni 	if (IS_GEN5(dev) || IS_GEN6(dev))
3978664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
3988664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
3997336df65SDaniel Vetter 		ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
40038d83c96SDaniel Vetter 	else if (IS_GEN8(dev))
40138d83c96SDaniel Vetter 		broadwell_set_fifo_underrun_reporting(dev, pipe, enable);
4028664281bSPaulo Zanoni 
4038664281bSPaulo Zanoni done:
4048664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
4058664281bSPaulo Zanoni 	return ret;
4068664281bSPaulo Zanoni }
4078664281bSPaulo Zanoni 
4088664281bSPaulo Zanoni /**
4098664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
4108664281bSPaulo Zanoni  * @dev: drm device
4118664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
4128664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
4138664281bSPaulo Zanoni  *
4148664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
4158664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
4168664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
4178664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
4188664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
4198664281bSPaulo Zanoni  *
4208664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
4218664281bSPaulo Zanoni  */
4228664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
4238664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
4248664281bSPaulo Zanoni 					   bool enable)
4258664281bSPaulo Zanoni {
4268664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
427de28075dSDaniel Vetter 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
428de28075dSDaniel Vetter 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4298664281bSPaulo Zanoni 	unsigned long flags;
4308664281bSPaulo Zanoni 	bool ret;
4318664281bSPaulo Zanoni 
432de28075dSDaniel Vetter 	/*
433de28075dSDaniel Vetter 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
434de28075dSDaniel Vetter 	 * has only one pch transcoder A that all pipes can use. To avoid racy
435de28075dSDaniel Vetter 	 * pch transcoder -> pipe lookups from interrupt code simply store the
436de28075dSDaniel Vetter 	 * underrun statistics in crtc A. Since we never expose this anywhere
437de28075dSDaniel Vetter 	 * nor use it outside of the fifo underrun code here using the "wrong"
438de28075dSDaniel Vetter 	 * crtc on LPT won't cause issues.
439de28075dSDaniel Vetter 	 */
4408664281bSPaulo Zanoni 
4418664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
4428664281bSPaulo Zanoni 
4438664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
4448664281bSPaulo Zanoni 
4458664281bSPaulo Zanoni 	if (enable == ret)
4468664281bSPaulo Zanoni 		goto done;
4478664281bSPaulo Zanoni 
4488664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
4498664281bSPaulo Zanoni 
4508664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
451de28075dSDaniel Vetter 		ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4528664281bSPaulo Zanoni 	else
4538664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4548664281bSPaulo Zanoni 
4558664281bSPaulo Zanoni done:
4568664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
4578664281bSPaulo Zanoni 	return ret;
4588664281bSPaulo Zanoni }
4598664281bSPaulo Zanoni 
4608664281bSPaulo Zanoni 
4617c463586SKeith Packard void
4623b6c42e8SDaniel Vetter i915_enable_pipestat(drm_i915_private_t *dev_priv, enum pipe pipe, u32 mask)
4637c463586SKeith Packard {
4649db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
46546c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
4667c463586SKeith Packard 
467b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
468b79480baSDaniel Vetter 
46946c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
47046c06a30SVille Syrjälä 		return;
47146c06a30SVille Syrjälä 
4727c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
47346c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
47446c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4753143a2bfSChris Wilson 	POSTING_READ(reg);
4767c463586SKeith Packard }
4777c463586SKeith Packard 
4787c463586SKeith Packard void
4793b6c42e8SDaniel Vetter i915_disable_pipestat(drm_i915_private_t *dev_priv, enum pipe pipe, u32 mask)
4807c463586SKeith Packard {
4819db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
48246c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
4837c463586SKeith Packard 
484b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
485b79480baSDaniel Vetter 
48646c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
48746c06a30SVille Syrjälä 		return;
48846c06a30SVille Syrjälä 
48946c06a30SVille Syrjälä 	pipestat &= ~mask;
49046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4913143a2bfSChris Wilson 	POSTING_READ(reg);
4927c463586SKeith Packard }
4937c463586SKeith Packard 
494c0e09200SDave Airlie /**
495f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
49601c66889SZhao Yakui  */
497f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
49801c66889SZhao Yakui {
4991ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
5001ec14ad3SChris Wilson 	unsigned long irqflags;
5011ec14ad3SChris Wilson 
502f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
503f49e38ddSJani Nikula 		return;
504f49e38ddSJani Nikula 
5051ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
50601c66889SZhao Yakui 
5073b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_ENABLE);
508a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
5093b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
5103b6c42e8SDaniel Vetter 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
5111ec14ad3SChris Wilson 
5121ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
51301c66889SZhao Yakui }
51401c66889SZhao Yakui 
51501c66889SZhao Yakui /**
5160a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
5170a3e67a4SJesse Barnes  * @dev: DRM device
5180a3e67a4SJesse Barnes  * @pipe: pipe to check
5190a3e67a4SJesse Barnes  *
5200a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
5210a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
5220a3e67a4SJesse Barnes  * before reading such registers if unsure.
5230a3e67a4SJesse Barnes  */
5240a3e67a4SJesse Barnes static int
5250a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
5260a3e67a4SJesse Barnes {
5270a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
528702e7a56SPaulo Zanoni 
529a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
530a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
531a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
532a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
53371f8ba6bSPaulo Zanoni 
534a01025afSDaniel Vetter 		return intel_crtc->active;
535a01025afSDaniel Vetter 	} else {
536a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
537a01025afSDaniel Vetter 	}
5380a3e67a4SJesse Barnes }
5390a3e67a4SJesse Barnes 
5404cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
5414cdb83ecSVille Syrjälä {
5424cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
5434cdb83ecSVille Syrjälä 	return 0;
5444cdb83ecSVille Syrjälä }
5454cdb83ecSVille Syrjälä 
54642f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
54742f52ef8SKeith Packard  * we use as a pipe index
54842f52ef8SKeith Packard  */
549f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5500a3e67a4SJesse Barnes {
5510a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5520a3e67a4SJesse Barnes 	unsigned long high_frame;
5530a3e67a4SJesse Barnes 	unsigned long low_frame;
554391f75e2SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start;
5550a3e67a4SJesse Barnes 
5560a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
55744d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5589db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
5590a3e67a4SJesse Barnes 		return 0;
5600a3e67a4SJesse Barnes 	}
5610a3e67a4SJesse Barnes 
562391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
563391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
564391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
565391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
566391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
567391f75e2SVille Syrjälä 
568391f75e2SVille Syrjälä 		vbl_start = mode->crtc_vblank_start * mode->crtc_htotal;
569391f75e2SVille Syrjälä 	} else {
570391f75e2SVille Syrjälä 		enum transcoder cpu_transcoder =
571391f75e2SVille Syrjälä 			intel_pipe_to_cpu_transcoder(dev_priv, pipe);
572391f75e2SVille Syrjälä 		u32 htotal;
573391f75e2SVille Syrjälä 
574391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
575391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
576391f75e2SVille Syrjälä 
577391f75e2SVille Syrjälä 		vbl_start *= htotal;
578391f75e2SVille Syrjälä 	}
579391f75e2SVille Syrjälä 
5809db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5819db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5825eddb70bSChris Wilson 
5830a3e67a4SJesse Barnes 	/*
5840a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5850a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
5860a3e67a4SJesse Barnes 	 * register.
5870a3e67a4SJesse Barnes 	 */
5880a3e67a4SJesse Barnes 	do {
5895eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
590391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
5915eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
5920a3e67a4SJesse Barnes 	} while (high1 != high2);
5930a3e67a4SJesse Barnes 
5945eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
595391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
5965eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
597391f75e2SVille Syrjälä 
598391f75e2SVille Syrjälä 	/*
599391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
600391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
601391f75e2SVille Syrjälä 	 * counter against vblank start.
602391f75e2SVille Syrjälä 	 */
603edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
6040a3e67a4SJesse Barnes }
6050a3e67a4SJesse Barnes 
606f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
6079880b7a5SJesse Barnes {
6089880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6099db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
6109880b7a5SJesse Barnes 
6119880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
61244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
6139db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6149880b7a5SJesse Barnes 		return 0;
6159880b7a5SJesse Barnes 	}
6169880b7a5SJesse Barnes 
6179880b7a5SJesse Barnes 	return I915_READ(reg);
6189880b7a5SJesse Barnes }
6199880b7a5SJesse Barnes 
620ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
621ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
622ad3543edSMario Kleiner #define __raw_i915_read16(dev_priv__, reg__) readw((dev_priv__)->regs + (reg__))
623ad3543edSMario Kleiner 
624ad3543edSMario Kleiner static bool intel_pipe_in_vblank_locked(struct drm_device *dev, enum pipe pipe)
62554ddcbd2SVille Syrjälä {
62654ddcbd2SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
62754ddcbd2SVille Syrjälä 	uint32_t status;
628ad3543edSMario Kleiner 	int reg;
62954ddcbd2SVille Syrjälä 
63054ddcbd2SVille Syrjälä 	if (IS_VALLEYVIEW(dev)) {
63154ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
63254ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
63354ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
63454ddcbd2SVille Syrjälä 
635ad3543edSMario Kleiner 		reg = VLV_ISR;
6367c06b08aSVille Syrjälä 	} else if (IS_GEN2(dev)) {
6377c06b08aSVille Syrjälä 		status = pipe == PIPE_A ?
6387c06b08aSVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
6397c06b08aSVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
6407c06b08aSVille Syrjälä 
641ad3543edSMario Kleiner 		reg = ISR;
6427c06b08aSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen < 5) {
64354ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
64454ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
64554ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
64654ddcbd2SVille Syrjälä 
647ad3543edSMario Kleiner 		reg = ISR;
64854ddcbd2SVille Syrjälä 	} else if (INTEL_INFO(dev)->gen < 7) {
64954ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
65054ddcbd2SVille Syrjälä 			DE_PIPEA_VBLANK :
65154ddcbd2SVille Syrjälä 			DE_PIPEB_VBLANK;
65254ddcbd2SVille Syrjälä 
653ad3543edSMario Kleiner 		reg = DEISR;
65454ddcbd2SVille Syrjälä 	} else {
65554ddcbd2SVille Syrjälä 		switch (pipe) {
65654ddcbd2SVille Syrjälä 		default:
65754ddcbd2SVille Syrjälä 		case PIPE_A:
65854ddcbd2SVille Syrjälä 			status = DE_PIPEA_VBLANK_IVB;
65954ddcbd2SVille Syrjälä 			break;
66054ddcbd2SVille Syrjälä 		case PIPE_B:
66154ddcbd2SVille Syrjälä 			status = DE_PIPEB_VBLANK_IVB;
66254ddcbd2SVille Syrjälä 			break;
66354ddcbd2SVille Syrjälä 		case PIPE_C:
66454ddcbd2SVille Syrjälä 			status = DE_PIPEC_VBLANK_IVB;
66554ddcbd2SVille Syrjälä 			break;
66654ddcbd2SVille Syrjälä 		}
66754ddcbd2SVille Syrjälä 
668ad3543edSMario Kleiner 		reg = DEISR;
66954ddcbd2SVille Syrjälä 	}
670ad3543edSMario Kleiner 
671ad3543edSMario Kleiner 	if (IS_GEN2(dev))
672ad3543edSMario Kleiner 		return __raw_i915_read16(dev_priv, reg) & status;
673ad3543edSMario Kleiner 	else
674ad3543edSMario Kleiner 		return __raw_i915_read32(dev_priv, reg) & status;
67554ddcbd2SVille Syrjälä }
67654ddcbd2SVille Syrjälä 
677f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
678ad3543edSMario Kleiner 			     int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
6790af7e4dfSMario Kleiner {
680c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
681c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
682c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
683c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
6843aa18df8SVille Syrjälä 	int position;
6850af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
6860af7e4dfSMario Kleiner 	bool in_vbl = true;
6870af7e4dfSMario Kleiner 	int ret = 0;
688ad3543edSMario Kleiner 	unsigned long irqflags;
6890af7e4dfSMario Kleiner 
690c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
6910af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
6929db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6930af7e4dfSMario Kleiner 		return 0;
6940af7e4dfSMario Kleiner 	}
6950af7e4dfSMario Kleiner 
696c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
697c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
698c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
699c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7000af7e4dfSMario Kleiner 
701c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
702c2baf4b7SVille Syrjälä 
703ad3543edSMario Kleiner 	/*
704ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
705ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
706ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
707ad3543edSMario Kleiner 	 */
708ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
709ad3543edSMario Kleiner 
710ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
711ad3543edSMario Kleiner 
712ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
713ad3543edSMario Kleiner 	if (stime)
714ad3543edSMario Kleiner 		*stime = ktime_get();
715ad3543edSMario Kleiner 
7167c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7170af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
7180af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
7190af7e4dfSMario Kleiner 		 */
7207c06b08aSVille Syrjälä 		if (IS_GEN2(dev))
721ad3543edSMario Kleiner 			position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
7227c06b08aSVille Syrjälä 		else
723ad3543edSMario Kleiner 			position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
72454ddcbd2SVille Syrjälä 
72554ddcbd2SVille Syrjälä 		/*
72654ddcbd2SVille Syrjälä 		 * The scanline counter increments at the leading edge
72754ddcbd2SVille Syrjälä 		 * of hsync, ie. it completely misses the active portion
72854ddcbd2SVille Syrjälä 		 * of the line. Fix up the counter at both edges of vblank
72954ddcbd2SVille Syrjälä 		 * to get a more accurate picture whether we're in vblank
73054ddcbd2SVille Syrjälä 		 * or not.
73154ddcbd2SVille Syrjälä 		 */
732ad3543edSMario Kleiner 		in_vbl = intel_pipe_in_vblank_locked(dev, pipe);
73354ddcbd2SVille Syrjälä 		if ((in_vbl && position == vbl_start - 1) ||
73454ddcbd2SVille Syrjälä 		    (!in_vbl && position == vbl_end - 1))
73554ddcbd2SVille Syrjälä 			position = (position + 1) % vtotal;
7360af7e4dfSMario Kleiner 	} else {
7370af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
7380af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
7390af7e4dfSMario Kleiner 		 * scanout position.
7400af7e4dfSMario Kleiner 		 */
741ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
7420af7e4dfSMario Kleiner 
7433aa18df8SVille Syrjälä 		/* convert to pixel counts */
7443aa18df8SVille Syrjälä 		vbl_start *= htotal;
7453aa18df8SVille Syrjälä 		vbl_end *= htotal;
7463aa18df8SVille Syrjälä 		vtotal *= htotal;
7473aa18df8SVille Syrjälä 	}
7483aa18df8SVille Syrjälä 
749ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
750ad3543edSMario Kleiner 	if (etime)
751ad3543edSMario Kleiner 		*etime = ktime_get();
752ad3543edSMario Kleiner 
753ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
754ad3543edSMario Kleiner 
755ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
756ad3543edSMario Kleiner 
7573aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
7583aa18df8SVille Syrjälä 
7593aa18df8SVille Syrjälä 	/*
7603aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
7613aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
7623aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
7633aa18df8SVille Syrjälä 	 * up since vbl_end.
7643aa18df8SVille Syrjälä 	 */
7653aa18df8SVille Syrjälä 	if (position >= vbl_start)
7663aa18df8SVille Syrjälä 		position -= vbl_end;
7673aa18df8SVille Syrjälä 	else
7683aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
7693aa18df8SVille Syrjälä 
7707c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7713aa18df8SVille Syrjälä 		*vpos = position;
7723aa18df8SVille Syrjälä 		*hpos = 0;
7733aa18df8SVille Syrjälä 	} else {
7740af7e4dfSMario Kleiner 		*vpos = position / htotal;
7750af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
7760af7e4dfSMario Kleiner 	}
7770af7e4dfSMario Kleiner 
7780af7e4dfSMario Kleiner 	/* In vblank? */
7790af7e4dfSMario Kleiner 	if (in_vbl)
7800af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
7810af7e4dfSMario Kleiner 
7820af7e4dfSMario Kleiner 	return ret;
7830af7e4dfSMario Kleiner }
7840af7e4dfSMario Kleiner 
785f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
7860af7e4dfSMario Kleiner 			      int *max_error,
7870af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
7880af7e4dfSMario Kleiner 			      unsigned flags)
7890af7e4dfSMario Kleiner {
7904041b853SChris Wilson 	struct drm_crtc *crtc;
7910af7e4dfSMario Kleiner 
7927eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
7934041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7940af7e4dfSMario Kleiner 		return -EINVAL;
7950af7e4dfSMario Kleiner 	}
7960af7e4dfSMario Kleiner 
7970af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
7984041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
7994041b853SChris Wilson 	if (crtc == NULL) {
8004041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
8014041b853SChris Wilson 		return -EINVAL;
8024041b853SChris Wilson 	}
8034041b853SChris Wilson 
8044041b853SChris Wilson 	if (!crtc->enabled) {
8054041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
8064041b853SChris Wilson 		return -EBUSY;
8074041b853SChris Wilson 	}
8080af7e4dfSMario Kleiner 
8090af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
8104041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
8114041b853SChris Wilson 						     vblank_time, flags,
8124041b853SChris Wilson 						     crtc);
8130af7e4dfSMario Kleiner }
8140af7e4dfSMario Kleiner 
81567c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
81667c347ffSJani Nikula 				struct drm_connector *connector)
817321a1b30SEgbert Eich {
818321a1b30SEgbert Eich 	enum drm_connector_status old_status;
819321a1b30SEgbert Eich 
820321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
821321a1b30SEgbert Eich 	old_status = connector->status;
822321a1b30SEgbert Eich 
823321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
82467c347ffSJani Nikula 	if (old_status == connector->status)
82567c347ffSJani Nikula 		return false;
82667c347ffSJani Nikula 
82767c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
828321a1b30SEgbert Eich 		      connector->base.id,
829321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
83067c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
83167c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
83267c347ffSJani Nikula 
83367c347ffSJani Nikula 	return true;
834321a1b30SEgbert Eich }
835321a1b30SEgbert Eich 
8365ca58282SJesse Barnes /*
8375ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
8385ca58282SJesse Barnes  */
839ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
840ac4c16c5SEgbert Eich 
8415ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
8425ca58282SJesse Barnes {
8435ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
8445ca58282SJesse Barnes 						    hotplug_work);
8455ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
846c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
847cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
848cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
849cd569aedSEgbert Eich 	struct drm_connector *connector;
850cd569aedSEgbert Eich 	unsigned long irqflags;
851cd569aedSEgbert Eich 	bool hpd_disabled = false;
852321a1b30SEgbert Eich 	bool changed = false;
853142e2398SEgbert Eich 	u32 hpd_event_bits;
8545ca58282SJesse Barnes 
85552d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
85652d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
85752d7ecedSDaniel Vetter 		return;
85852d7ecedSDaniel Vetter 
859a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
860e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
861e67189abSJesse Barnes 
862cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
863142e2398SEgbert Eich 
864142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
865142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
866cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
867cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
868cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
869cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
870cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
871cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
872cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
873cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
874cd569aedSEgbert Eich 				drm_get_connector_name(connector));
875cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
876cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
877cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
878cd569aedSEgbert Eich 			hpd_disabled = true;
879cd569aedSEgbert Eich 		}
880142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
881142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
882142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
883142e2398SEgbert Eich 		}
884cd569aedSEgbert Eich 	}
885cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
886cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
887cd569aedSEgbert Eich 	  * some connectors */
888ac4c16c5SEgbert Eich 	if (hpd_disabled) {
889cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
890ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
891ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
892ac4c16c5SEgbert Eich 	}
893cd569aedSEgbert Eich 
894cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
895cd569aedSEgbert Eich 
896321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
897321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
898321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
899321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
900cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
901cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
902321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
903321a1b30SEgbert Eich 				changed = true;
904321a1b30SEgbert Eich 		}
905321a1b30SEgbert Eich 	}
90640ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
90740ee3381SKeith Packard 
908321a1b30SEgbert Eich 	if (changed)
909321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
9105ca58282SJesse Barnes }
9115ca58282SJesse Barnes 
912d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
913f97108d1SJesse Barnes {
914f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
915b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9169270388eSDaniel Vetter 	u8 new_delay;
9179270388eSDaniel Vetter 
918d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
919f97108d1SJesse Barnes 
92073edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
92173edd18fSDaniel Vetter 
92220e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9239270388eSDaniel Vetter 
9247648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
925b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
926b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
927f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
928f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
929f97108d1SJesse Barnes 
930f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
931b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
93220e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
93320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
93420e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
93520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
936b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
93720e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
93820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
93920e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
94020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
941f97108d1SJesse Barnes 	}
942f97108d1SJesse Barnes 
9437648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
94420e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
945f97108d1SJesse Barnes 
946d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9479270388eSDaniel Vetter 
948f97108d1SJesse Barnes 	return;
949f97108d1SJesse Barnes }
950f97108d1SJesse Barnes 
951549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
952549f7365SChris Wilson 			struct intel_ring_buffer *ring)
953549f7365SChris Wilson {
954475553deSChris Wilson 	if (ring->obj == NULL)
955475553deSChris Wilson 		return;
956475553deSChris Wilson 
957814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
9589862e600SChris Wilson 
959549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
96010cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
961549f7365SChris Wilson }
962549f7365SChris Wilson 
9634912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
9643b8d8d91SJesse Barnes {
9654912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
966c6a828d3SDaniel Vetter 						    rps.work);
967edbfdb45SPaulo Zanoni 	u32 pm_iir;
968dd75fdc8SChris Wilson 	int new_delay, adj;
9693b8d8d91SJesse Barnes 
97059cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
971c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
972c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
9734848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
974edbfdb45SPaulo Zanoni 	snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
97559cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
9764912d041SBen Widawsky 
97760611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
97860611c13SPaulo Zanoni 	WARN_ON(pm_iir & ~GEN6_PM_RPS_EVENTS);
97960611c13SPaulo Zanoni 
9804848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
9813b8d8d91SJesse Barnes 		return;
9823b8d8d91SJesse Barnes 
9834fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
9847b9e0ae6SChris Wilson 
985dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
9867425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
987dd75fdc8SChris Wilson 		if (adj > 0)
988dd75fdc8SChris Wilson 			adj *= 2;
989dd75fdc8SChris Wilson 		else
990dd75fdc8SChris Wilson 			adj = 1;
991dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
9927425034aSVille Syrjälä 
9937425034aSVille Syrjälä 		/*
9947425034aSVille Syrjälä 		 * For better performance, jump directly
9957425034aSVille Syrjälä 		 * to RPe if we're below it.
9967425034aSVille Syrjälä 		 */
997dd75fdc8SChris Wilson 		if (new_delay < dev_priv->rps.rpe_delay)
9987425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
999dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1000dd75fdc8SChris Wilson 		if (dev_priv->rps.cur_delay > dev_priv->rps.rpe_delay)
1001dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.rpe_delay;
1002dd75fdc8SChris Wilson 		else
1003dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.min_delay;
1004dd75fdc8SChris Wilson 		adj = 0;
1005dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1006dd75fdc8SChris Wilson 		if (adj < 0)
1007dd75fdc8SChris Wilson 			adj *= 2;
1008dd75fdc8SChris Wilson 		else
1009dd75fdc8SChris Wilson 			adj = -1;
1010dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
1011dd75fdc8SChris Wilson 	} else { /* unknown event */
1012dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay;
1013dd75fdc8SChris Wilson 	}
10143b8d8d91SJesse Barnes 
101579249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
101679249636SBen Widawsky 	 * interrupt
101779249636SBen Widawsky 	 */
10181272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
10191272e7b8SVille Syrjälä 			    dev_priv->rps.min_delay, dev_priv->rps.max_delay);
1020dd75fdc8SChris Wilson 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_delay;
1021dd75fdc8SChris Wilson 
10220a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
10230a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
10240a073b84SJesse Barnes 	else
10254912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
10263b8d8d91SJesse Barnes 
10274fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
10283b8d8d91SJesse Barnes }
10293b8d8d91SJesse Barnes 
1030e3689190SBen Widawsky 
1031e3689190SBen Widawsky /**
1032e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1033e3689190SBen Widawsky  * occurred.
1034e3689190SBen Widawsky  * @work: workqueue struct
1035e3689190SBen Widawsky  *
1036e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1037e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1038e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1039e3689190SBen Widawsky  */
1040e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1041e3689190SBen Widawsky {
1042e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
1043a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
1044e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
104535a85ac6SBen Widawsky 	char *parity_event[6];
1046e3689190SBen Widawsky 	uint32_t misccpctl;
1047e3689190SBen Widawsky 	unsigned long flags;
104835a85ac6SBen Widawsky 	uint8_t slice = 0;
1049e3689190SBen Widawsky 
1050e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1051e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1052e3689190SBen Widawsky 	 * any time we access those registers.
1053e3689190SBen Widawsky 	 */
1054e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1055e3689190SBen Widawsky 
105635a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
105735a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
105835a85ac6SBen Widawsky 		goto out;
105935a85ac6SBen Widawsky 
1060e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1061e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1062e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1063e3689190SBen Widawsky 
106435a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
106535a85ac6SBen Widawsky 		u32 reg;
106635a85ac6SBen Widawsky 
106735a85ac6SBen Widawsky 		slice--;
106835a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
106935a85ac6SBen Widawsky 			break;
107035a85ac6SBen Widawsky 
107135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
107235a85ac6SBen Widawsky 
107335a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
107435a85ac6SBen Widawsky 
107535a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1076e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1077e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1078e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1079e3689190SBen Widawsky 
108035a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
108135a85ac6SBen Widawsky 		POSTING_READ(reg);
1082e3689190SBen Widawsky 
1083cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1084e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1085e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1086e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
108735a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
108835a85ac6SBen Widawsky 		parity_event[5] = NULL;
1089e3689190SBen Widawsky 
10905bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1091e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1092e3689190SBen Widawsky 
109335a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
109435a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1095e3689190SBen Widawsky 
109635a85ac6SBen Widawsky 		kfree(parity_event[4]);
1097e3689190SBen Widawsky 		kfree(parity_event[3]);
1098e3689190SBen Widawsky 		kfree(parity_event[2]);
1099e3689190SBen Widawsky 		kfree(parity_event[1]);
1100e3689190SBen Widawsky 	}
1101e3689190SBen Widawsky 
110235a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
110335a85ac6SBen Widawsky 
110435a85ac6SBen Widawsky out:
110535a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
110635a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
110735a85ac6SBen Widawsky 	ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
110835a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
110935a85ac6SBen Widawsky 
111035a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
111135a85ac6SBen Widawsky }
111235a85ac6SBen Widawsky 
111335a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1114e3689190SBen Widawsky {
1115e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1116e3689190SBen Widawsky 
1117040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1118e3689190SBen Widawsky 		return;
1119e3689190SBen Widawsky 
1120d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
112135a85ac6SBen Widawsky 	ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1122d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1123e3689190SBen Widawsky 
112435a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
112535a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
112635a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
112735a85ac6SBen Widawsky 
112835a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
112935a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
113035a85ac6SBen Widawsky 
1131a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1132e3689190SBen Widawsky }
1133e3689190SBen Widawsky 
1134f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1135f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1136f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1137f1af8fc1SPaulo Zanoni {
1138f1af8fc1SPaulo Zanoni 	if (gt_iir &
1139f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1140f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1141f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1142f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1143f1af8fc1SPaulo Zanoni }
1144f1af8fc1SPaulo Zanoni 
1145e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1146e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1147e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1148e7b4c6b1SDaniel Vetter {
1149e7b4c6b1SDaniel Vetter 
1150cc609d5dSBen Widawsky 	if (gt_iir &
1151cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1152e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1153cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1154e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1155cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1156e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1157e7b4c6b1SDaniel Vetter 
1158cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1159cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1160cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
1161e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
1162e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
1163e7b4c6b1SDaniel Vetter 	}
1164e3689190SBen Widawsky 
116535a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
116635a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1167e7b4c6b1SDaniel Vetter }
1168e7b4c6b1SDaniel Vetter 
1169abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1170abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1171abd58f01SBen Widawsky 				       u32 master_ctl)
1172abd58f01SBen Widawsky {
1173abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1174abd58f01SBen Widawsky 	uint32_t tmp = 0;
1175abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1176abd58f01SBen Widawsky 
1177abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1178abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1179abd58f01SBen Widawsky 		if (tmp) {
1180abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1181abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1182abd58f01SBen Widawsky 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1183abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1184abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[RCS]);
1185abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1186abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[BCS]);
1187abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1188abd58f01SBen Widawsky 		} else
1189abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1190abd58f01SBen Widawsky 	}
1191abd58f01SBen Widawsky 
1192abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VCS1_IRQ) {
1193abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1194abd58f01SBen Widawsky 		if (tmp) {
1195abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1196abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1197abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1198abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VCS]);
1199abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1200abd58f01SBen Widawsky 		} else
1201abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1202abd58f01SBen Widawsky 	}
1203abd58f01SBen Widawsky 
1204abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1205abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1206abd58f01SBen Widawsky 		if (tmp) {
1207abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1208abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1209abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1210abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VECS]);
1211abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1212abd58f01SBen Widawsky 		} else
1213abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1214abd58f01SBen Widawsky 	}
1215abd58f01SBen Widawsky 
1216abd58f01SBen Widawsky 	return ret;
1217abd58f01SBen Widawsky }
1218abd58f01SBen Widawsky 
1219b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1220b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1221b543fb04SEgbert Eich 
122210a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1223b543fb04SEgbert Eich 					 u32 hotplug_trigger,
1224b543fb04SEgbert Eich 					 const u32 *hpd)
1225b543fb04SEgbert Eich {
1226b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
1227b543fb04SEgbert Eich 	int i;
122810a504deSDaniel Vetter 	bool storm_detected = false;
1229b543fb04SEgbert Eich 
123091d131d2SDaniel Vetter 	if (!hotplug_trigger)
123191d131d2SDaniel Vetter 		return;
123291d131d2SDaniel Vetter 
1233b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1234b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
1235821450c6SEgbert Eich 
1236b8f102e8SEgbert Eich 		WARN(((hpd[i] & hotplug_trigger) &&
1237b8f102e8SEgbert Eich 		      dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED),
1238b8f102e8SEgbert Eich 		     "Received HPD interrupt although disabled\n");
1239b8f102e8SEgbert Eich 
1240b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1241b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1242b543fb04SEgbert Eich 			continue;
1243b543fb04SEgbert Eich 
1244bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1245b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1246b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1247b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1248b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1249b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1250b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1251b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1252b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1253142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1254b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
125510a504deSDaniel Vetter 			storm_detected = true;
1256b543fb04SEgbert Eich 		} else {
1257b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1258b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1259b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1260b543fb04SEgbert Eich 		}
1261b543fb04SEgbert Eich 	}
1262b543fb04SEgbert Eich 
126310a504deSDaniel Vetter 	if (storm_detected)
126410a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1265b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
12665876fa0dSDaniel Vetter 
1267645416f5SDaniel Vetter 	/*
1268645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1269645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1270645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1271645416f5SDaniel Vetter 	 * deadlock.
1272645416f5SDaniel Vetter 	 */
1273645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1274b543fb04SEgbert Eich }
1275b543fb04SEgbert Eich 
1276515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1277515ac2bbSDaniel Vetter {
127828c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
127928c70f16SDaniel Vetter 
128028c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1281515ac2bbSDaniel Vetter }
1282515ac2bbSDaniel Vetter 
1283ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1284ce99c256SDaniel Vetter {
12859ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
12869ee32feaSDaniel Vetter 
12879ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1288ce99c256SDaniel Vetter }
1289ce99c256SDaniel Vetter 
12908bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1291277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1292eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1293eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
12948bc5e955SDaniel Vetter 					 uint32_t crc4)
12958bf1e9f1SShuang He {
12968bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
12978bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
12988bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1299ac2300d4SDamien Lespiau 	int head, tail;
1300b2c88f5bSDamien Lespiau 
1301d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1302d538bbdfSDamien Lespiau 
13030c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1304d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
13050c912c79SDamien Lespiau 		DRM_ERROR("spurious interrupt\n");
13060c912c79SDamien Lespiau 		return;
13070c912c79SDamien Lespiau 	}
13080c912c79SDamien Lespiau 
1309d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1310d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1311b2c88f5bSDamien Lespiau 
1312b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1313d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1314b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1315b2c88f5bSDamien Lespiau 		return;
1316b2c88f5bSDamien Lespiau 	}
1317b2c88f5bSDamien Lespiau 
1318b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
13198bf1e9f1SShuang He 
13208bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1321eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1322eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1323eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1324eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1325eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1326b2c88f5bSDamien Lespiau 
1327b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1328d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1329d538bbdfSDamien Lespiau 
1330d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
133107144428SDamien Lespiau 
133207144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
13338bf1e9f1SShuang He }
1334277de95eSDaniel Vetter #else
1335277de95eSDaniel Vetter static inline void
1336277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1337277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1338277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1339277de95eSDaniel Vetter 			     uint32_t crc4) {}
1340277de95eSDaniel Vetter #endif
1341eba94eb9SDaniel Vetter 
1342277de95eSDaniel Vetter 
1343277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
13445a69b89fSDaniel Vetter {
13455a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
13465a69b89fSDaniel Vetter 
1347277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
13485a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
13495a69b89fSDaniel Vetter 				     0, 0, 0, 0);
13505a69b89fSDaniel Vetter }
13515a69b89fSDaniel Vetter 
1352277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1353eba94eb9SDaniel Vetter {
1354eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1355eba94eb9SDaniel Vetter 
1356277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1357eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1358eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1359eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1360eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
13618bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1362eba94eb9SDaniel Vetter }
13635b3a856bSDaniel Vetter 
1364277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
13655b3a856bSDaniel Vetter {
13665b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
13670b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
13680b5c5ed0SDaniel Vetter 
13690b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
13700b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
13710b5c5ed0SDaniel Vetter 	else
13720b5c5ed0SDaniel Vetter 		res1 = 0;
13730b5c5ed0SDaniel Vetter 
13740b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
13750b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
13760b5c5ed0SDaniel Vetter 	else
13770b5c5ed0SDaniel Vetter 		res2 = 0;
13785b3a856bSDaniel Vetter 
1379277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
13800b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
13810b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
13820b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
13830b5c5ed0SDaniel Vetter 				     res1, res2);
13845b3a856bSDaniel Vetter }
13858bf1e9f1SShuang He 
13861403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
13871403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
13881403c0d4SPaulo Zanoni  * the work queue. */
13891403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1390baf02a1fSBen Widawsky {
139141a05a3aSDaniel Vetter 	if (pm_iir & GEN6_PM_RPS_EVENTS) {
139259cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
13934848405cSBen Widawsky 		dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
13944d3b3d5fSPaulo Zanoni 		snb_disable_pm_irq(dev_priv, pm_iir & GEN6_PM_RPS_EVENTS);
139559cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
13962adbee62SDaniel Vetter 
13972adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
139841a05a3aSDaniel Vetter 	}
1399baf02a1fSBen Widawsky 
14001403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
140112638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
140212638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
140312638c57SBen Widawsky 
140412638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
140512638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
140612638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
140712638c57SBen Widawsky 		}
140812638c57SBen Widawsky 	}
14091403c0d4SPaulo Zanoni }
1410baf02a1fSBen Widawsky 
1411ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
14127e231dbeSJesse Barnes {
14137e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
14147e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
14157e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
14167e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
14177e231dbeSJesse Barnes 	unsigned long irqflags;
14187e231dbeSJesse Barnes 	int pipe;
14197e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
14207e231dbeSJesse Barnes 
14217e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
14227e231dbeSJesse Barnes 
14237e231dbeSJesse Barnes 	while (true) {
14247e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
14257e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
14267e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
14277e231dbeSJesse Barnes 
14287e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
14297e231dbeSJesse Barnes 			goto out;
14307e231dbeSJesse Barnes 
14317e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
14327e231dbeSJesse Barnes 
1433e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
14347e231dbeSJesse Barnes 
14357e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
14367e231dbeSJesse Barnes 		for_each_pipe(pipe) {
14377e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
14387e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
14397e231dbeSJesse Barnes 
14407e231dbeSJesse Barnes 			/*
14417e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
14427e231dbeSJesse Barnes 			 */
14437e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
14447e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
14457e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
14467e231dbeSJesse Barnes 							 pipe_name(pipe));
14477e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
14487e231dbeSJesse Barnes 			}
14497e231dbeSJesse Barnes 		}
14507e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14517e231dbeSJesse Barnes 
145231acc7f5SJesse Barnes 		for_each_pipe(pipe) {
14537b5562d4SJesse Barnes 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
145431acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
145531acc7f5SJesse Barnes 
145631acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
145731acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
145831acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
145931acc7f5SJesse Barnes 			}
14604356d586SDaniel Vetter 
14614356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1462277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
146331acc7f5SJesse Barnes 		}
146431acc7f5SJesse Barnes 
14657e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
14667e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
14677e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1468b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
14697e231dbeSJesse Barnes 
14707e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
14717e231dbeSJesse Barnes 					 hotplug_status);
147291d131d2SDaniel Vetter 
147310a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
147491d131d2SDaniel Vetter 
1475*4aeebd74SDaniel Vetter 			if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1476*4aeebd74SDaniel Vetter 				dp_aux_irq_handler(dev);
1477*4aeebd74SDaniel Vetter 
14787e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
14797e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
14807e231dbeSJesse Barnes 		}
14817e231dbeSJesse Barnes 
1482515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1483515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
14847e231dbeSJesse Barnes 
148560611c13SPaulo Zanoni 		if (pm_iir)
1486d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
14877e231dbeSJesse Barnes 
14887e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
14897e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
14907e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
14917e231dbeSJesse Barnes 	}
14927e231dbeSJesse Barnes 
14937e231dbeSJesse Barnes out:
14947e231dbeSJesse Barnes 	return ret;
14957e231dbeSJesse Barnes }
14967e231dbeSJesse Barnes 
149723e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1498776ad806SJesse Barnes {
1499776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
15009db4a9c7SJesse Barnes 	int pipe;
1501b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1502776ad806SJesse Barnes 
150310a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
150491d131d2SDaniel Vetter 
1505cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1506cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1507776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1508cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1509cfc33bf7SVille Syrjälä 				 port_name(port));
1510cfc33bf7SVille Syrjälä 	}
1511776ad806SJesse Barnes 
1512ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1513ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1514ce99c256SDaniel Vetter 
1515776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1516515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1517776ad806SJesse Barnes 
1518776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1519776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1520776ad806SJesse Barnes 
1521776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1522776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1523776ad806SJesse Barnes 
1524776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1525776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1526776ad806SJesse Barnes 
15279db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
15289db4a9c7SJesse Barnes 		for_each_pipe(pipe)
15299db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
15309db4a9c7SJesse Barnes 					 pipe_name(pipe),
15319db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1532776ad806SJesse Barnes 
1533776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1534776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1535776ad806SJesse Barnes 
1536776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1537776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1538776ad806SJesse Barnes 
1539776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
15408664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
15418664281bSPaulo Zanoni 							  false))
15428664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
15438664281bSPaulo Zanoni 
15448664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
15458664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
15468664281bSPaulo Zanoni 							  false))
15478664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
15488664281bSPaulo Zanoni }
15498664281bSPaulo Zanoni 
15508664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
15518664281bSPaulo Zanoni {
15528664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
15538664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
15545a69b89fSDaniel Vetter 	enum pipe pipe;
15558664281bSPaulo Zanoni 
1556de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1557de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1558de032bf4SPaulo Zanoni 
15595a69b89fSDaniel Vetter 	for_each_pipe(pipe) {
15605a69b89fSDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
15615a69b89fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
15625a69b89fSDaniel Vetter 								  false))
15635a69b89fSDaniel Vetter 				DRM_DEBUG_DRIVER("Pipe %c FIFO underrun\n",
15645a69b89fSDaniel Vetter 						 pipe_name(pipe));
15655a69b89fSDaniel Vetter 		}
15668664281bSPaulo Zanoni 
15675a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
15685a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1569277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
15705a69b89fSDaniel Vetter 			else
1571277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
15725a69b89fSDaniel Vetter 		}
15735a69b89fSDaniel Vetter 	}
15748bf1e9f1SShuang He 
15758664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
15768664281bSPaulo Zanoni }
15778664281bSPaulo Zanoni 
15788664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
15798664281bSPaulo Zanoni {
15808664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
15818664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
15828664281bSPaulo Zanoni 
1583de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1584de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1585de032bf4SPaulo Zanoni 
15868664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
15878664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
15888664281bSPaulo Zanoni 							  false))
15898664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
15908664281bSPaulo Zanoni 
15918664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
15928664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
15938664281bSPaulo Zanoni 							  false))
15948664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
15958664281bSPaulo Zanoni 
15968664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
15978664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
15988664281bSPaulo Zanoni 							  false))
15998664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
16008664281bSPaulo Zanoni 
16018664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1602776ad806SJesse Barnes }
1603776ad806SJesse Barnes 
160423e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
160523e81d69SAdam Jackson {
160623e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
160723e81d69SAdam Jackson 	int pipe;
1608b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
160923e81d69SAdam Jackson 
161010a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
161191d131d2SDaniel Vetter 
1612cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1613cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
161423e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1615cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1616cfc33bf7SVille Syrjälä 				 port_name(port));
1617cfc33bf7SVille Syrjälä 	}
161823e81d69SAdam Jackson 
161923e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1620ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
162123e81d69SAdam Jackson 
162223e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1623515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
162423e81d69SAdam Jackson 
162523e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
162623e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
162723e81d69SAdam Jackson 
162823e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
162923e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
163023e81d69SAdam Jackson 
163123e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
163223e81d69SAdam Jackson 		for_each_pipe(pipe)
163323e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
163423e81d69SAdam Jackson 					 pipe_name(pipe),
163523e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
16368664281bSPaulo Zanoni 
16378664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
16388664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
163923e81d69SAdam Jackson }
164023e81d69SAdam Jackson 
1641c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1642c008bc6eSPaulo Zanoni {
1643c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
164440da17c2SDaniel Vetter 	enum pipe pipe;
1645c008bc6eSPaulo Zanoni 
1646c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1647c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1648c008bc6eSPaulo Zanoni 
1649c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1650c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1651c008bc6eSPaulo Zanoni 
1652c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1653c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1654c008bc6eSPaulo Zanoni 
165540da17c2SDaniel Vetter 	for_each_pipe(pipe) {
165640da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
165740da17c2SDaniel Vetter 			drm_handle_vblank(dev, pipe);
1658c008bc6eSPaulo Zanoni 
165940da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
166040da17c2SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
166140da17c2SDaniel Vetter 				DRM_DEBUG_DRIVER("Pipe %c FIFO underrun\n",
166240da17c2SDaniel Vetter 						 pipe_name(pipe));
1663c008bc6eSPaulo Zanoni 
166440da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
166540da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
16665b3a856bSDaniel Vetter 
166740da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
166840da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
166940da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
167040da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
1671c008bc6eSPaulo Zanoni 		}
1672c008bc6eSPaulo Zanoni 	}
1673c008bc6eSPaulo Zanoni 
1674c008bc6eSPaulo Zanoni 	/* check event from PCH */
1675c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1676c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1677c008bc6eSPaulo Zanoni 
1678c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1679c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1680c008bc6eSPaulo Zanoni 		else
1681c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1682c008bc6eSPaulo Zanoni 
1683c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1684c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1685c008bc6eSPaulo Zanoni 	}
1686c008bc6eSPaulo Zanoni 
1687c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1688c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1689c008bc6eSPaulo Zanoni }
1690c008bc6eSPaulo Zanoni 
16919719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
16929719fb98SPaulo Zanoni {
16939719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
16943b6c42e8SDaniel Vetter 	enum pipe i;
16959719fb98SPaulo Zanoni 
16969719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
16979719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
16989719fb98SPaulo Zanoni 
16999719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
17009719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
17019719fb98SPaulo Zanoni 
17029719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
17039719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
17049719fb98SPaulo Zanoni 
17053b6c42e8SDaniel Vetter 	for_each_pipe(i) {
170640da17c2SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(i)))
17079719fb98SPaulo Zanoni 			drm_handle_vblank(dev, i);
170840da17c2SDaniel Vetter 
170940da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
171040da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(i)) {
17119719fb98SPaulo Zanoni 			intel_prepare_page_flip(dev, i);
17129719fb98SPaulo Zanoni 			intel_finish_page_flip_plane(dev, i);
17139719fb98SPaulo Zanoni 		}
17149719fb98SPaulo Zanoni 	}
17159719fb98SPaulo Zanoni 
17169719fb98SPaulo Zanoni 	/* check event from PCH */
17179719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
17189719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
17199719fb98SPaulo Zanoni 
17209719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
17219719fb98SPaulo Zanoni 
17229719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
17239719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
17249719fb98SPaulo Zanoni 	}
17259719fb98SPaulo Zanoni }
17269719fb98SPaulo Zanoni 
1727f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1728b1f14ad0SJesse Barnes {
1729b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1730b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1731f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
17320e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1733b1f14ad0SJesse Barnes 
1734b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1735b1f14ad0SJesse Barnes 
17368664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
17378664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1738907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
17398664281bSPaulo Zanoni 
1740b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1741b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1742b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
174323a78516SPaulo Zanoni 	POSTING_READ(DEIER);
17440e43406bSChris Wilson 
174544498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
174644498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
174744498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
174844498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
174944498aeaSPaulo Zanoni 	 * due to its back queue). */
1750ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
175144498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
175244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
175344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1754ab5c608bSBen Widawsky 	}
175544498aeaSPaulo Zanoni 
17560e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
17570e43406bSChris Wilson 	if (gt_iir) {
1758d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
17590e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1760d8fc8a47SPaulo Zanoni 		else
1761d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
17620e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
17630e43406bSChris Wilson 		ret = IRQ_HANDLED;
17640e43406bSChris Wilson 	}
1765b1f14ad0SJesse Barnes 
1766b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
17670e43406bSChris Wilson 	if (de_iir) {
1768f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
17699719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1770f1af8fc1SPaulo Zanoni 		else
1771f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
17720e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
17730e43406bSChris Wilson 		ret = IRQ_HANDLED;
17740e43406bSChris Wilson 	}
17750e43406bSChris Wilson 
1776f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
1777f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
17780e43406bSChris Wilson 		if (pm_iir) {
1779d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
1780b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
17810e43406bSChris Wilson 			ret = IRQ_HANDLED;
17820e43406bSChris Wilson 		}
1783f1af8fc1SPaulo Zanoni 	}
1784b1f14ad0SJesse Barnes 
1785b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1786b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1787ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
178844498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
178944498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1790ab5c608bSBen Widawsky 	}
1791b1f14ad0SJesse Barnes 
1792b1f14ad0SJesse Barnes 	return ret;
1793b1f14ad0SJesse Barnes }
1794b1f14ad0SJesse Barnes 
1795abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
1796abd58f01SBen Widawsky {
1797abd58f01SBen Widawsky 	struct drm_device *dev = arg;
1798abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
1799abd58f01SBen Widawsky 	u32 master_ctl;
1800abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1801abd58f01SBen Widawsky 	uint32_t tmp = 0;
1802c42664ccSDaniel Vetter 	enum pipe pipe;
1803abd58f01SBen Widawsky 
1804abd58f01SBen Widawsky 	atomic_inc(&dev_priv->irq_received);
1805abd58f01SBen Widawsky 
1806abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
1807abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
1808abd58f01SBen Widawsky 	if (!master_ctl)
1809abd58f01SBen Widawsky 		return IRQ_NONE;
1810abd58f01SBen Widawsky 
1811abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
1812abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
1813abd58f01SBen Widawsky 
1814abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
1815abd58f01SBen Widawsky 
1816abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
1817abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
1818abd58f01SBen Widawsky 		if (tmp & GEN8_DE_MISC_GSE)
1819abd58f01SBen Widawsky 			intel_opregion_asle_intr(dev);
1820abd58f01SBen Widawsky 		else if (tmp)
1821abd58f01SBen Widawsky 			DRM_ERROR("Unexpected DE Misc interrupt\n");
1822abd58f01SBen Widawsky 		else
1823abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
1824abd58f01SBen Widawsky 
1825abd58f01SBen Widawsky 		if (tmp) {
1826abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
1827abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1828abd58f01SBen Widawsky 		}
1829abd58f01SBen Widawsky 	}
1830abd58f01SBen Widawsky 
18316d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
18326d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
18336d766f02SDaniel Vetter 		if (tmp & GEN8_AUX_CHANNEL_A)
18346d766f02SDaniel Vetter 			dp_aux_irq_handler(dev);
18356d766f02SDaniel Vetter 		else if (tmp)
18366d766f02SDaniel Vetter 			DRM_ERROR("Unexpected DE Port interrupt\n");
18376d766f02SDaniel Vetter 		else
18386d766f02SDaniel Vetter 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
18396d766f02SDaniel Vetter 
18406d766f02SDaniel Vetter 		if (tmp) {
18416d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
18426d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
18436d766f02SDaniel Vetter 		}
18446d766f02SDaniel Vetter 	}
18456d766f02SDaniel Vetter 
1846abd58f01SBen Widawsky 	for_each_pipe(pipe) {
1847abd58f01SBen Widawsky 		uint32_t pipe_iir;
1848abd58f01SBen Widawsky 
1849c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
1850c42664ccSDaniel Vetter 			continue;
1851c42664ccSDaniel Vetter 
1852abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
1853abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_VBLANK)
1854abd58f01SBen Widawsky 			drm_handle_vblank(dev, pipe);
1855abd58f01SBen Widawsky 
1856abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_FLIP_DONE) {
1857abd58f01SBen Widawsky 			intel_prepare_page_flip(dev, pipe);
1858abd58f01SBen Widawsky 			intel_finish_page_flip_plane(dev, pipe);
1859abd58f01SBen Widawsky 		}
1860abd58f01SBen Widawsky 
18610fbe7870SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
18620fbe7870SDaniel Vetter 			hsw_pipe_crc_irq_handler(dev, pipe);
18630fbe7870SDaniel Vetter 
186438d83c96SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN) {
186538d83c96SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
186638d83c96SDaniel Vetter 								  false))
186738d83c96SDaniel Vetter 				DRM_DEBUG_DRIVER("Pipe %c FIFO underrun\n",
186838d83c96SDaniel Vetter 						 pipe_name(pipe));
186938d83c96SDaniel Vetter 		}
187038d83c96SDaniel Vetter 
187130100f2bSDaniel Vetter 		if (pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS) {
187230100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
187330100f2bSDaniel Vetter 				  pipe_name(pipe),
187430100f2bSDaniel Vetter 				  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
187530100f2bSDaniel Vetter 		}
1876abd58f01SBen Widawsky 
1877abd58f01SBen Widawsky 		if (pipe_iir) {
1878abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1879abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
1880c42664ccSDaniel Vetter 		} else
1881abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
1882abd58f01SBen Widawsky 	}
1883abd58f01SBen Widawsky 
188492d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
188592d03a80SDaniel Vetter 		/*
188692d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
188792d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
188892d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
188992d03a80SDaniel Vetter 		 */
189092d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
189192d03a80SDaniel Vetter 
189292d03a80SDaniel Vetter 		cpt_irq_handler(dev, pch_iir);
189392d03a80SDaniel Vetter 
189492d03a80SDaniel Vetter 		if (pch_iir) {
189592d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
189692d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
189792d03a80SDaniel Vetter 		}
189892d03a80SDaniel Vetter 	}
189992d03a80SDaniel Vetter 
1900abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
1901abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
1902abd58f01SBen Widawsky 
1903abd58f01SBen Widawsky 	return ret;
1904abd58f01SBen Widawsky }
1905abd58f01SBen Widawsky 
190617e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
190717e1df07SDaniel Vetter 			       bool reset_completed)
190817e1df07SDaniel Vetter {
190917e1df07SDaniel Vetter 	struct intel_ring_buffer *ring;
191017e1df07SDaniel Vetter 	int i;
191117e1df07SDaniel Vetter 
191217e1df07SDaniel Vetter 	/*
191317e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
191417e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
191517e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
191617e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
191717e1df07SDaniel Vetter 	 */
191817e1df07SDaniel Vetter 
191917e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
192017e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
192117e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
192217e1df07SDaniel Vetter 
192317e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
192417e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
192517e1df07SDaniel Vetter 
192617e1df07SDaniel Vetter 	/*
192717e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
192817e1df07SDaniel Vetter 	 * reset state is cleared.
192917e1df07SDaniel Vetter 	 */
193017e1df07SDaniel Vetter 	if (reset_completed)
193117e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
193217e1df07SDaniel Vetter }
193317e1df07SDaniel Vetter 
19348a905236SJesse Barnes /**
19358a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
19368a905236SJesse Barnes  * @work: work struct
19378a905236SJesse Barnes  *
19388a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
19398a905236SJesse Barnes  * was detected.
19408a905236SJesse Barnes  */
19418a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
19428a905236SJesse Barnes {
19431f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
19441f83fee0SDaniel Vetter 						    work);
19451f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
19461f83fee0SDaniel Vetter 						    gpu_error);
19478a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1948cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1949cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1950cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
195117e1df07SDaniel Vetter 	int ret;
19528a905236SJesse Barnes 
19535bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
19548a905236SJesse Barnes 
19557db0ba24SDaniel Vetter 	/*
19567db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
19577db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
19587db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
19597db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
19607db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
19617db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
19627db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
19637db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
19647db0ba24SDaniel Vetter 	 */
19657db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
196644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
19675bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
19687db0ba24SDaniel Vetter 				   reset_event);
19691f83fee0SDaniel Vetter 
197017e1df07SDaniel Vetter 		/*
197117e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
197217e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
197317e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
197417e1df07SDaniel Vetter 		 * deadlocks with the reset work.
197517e1df07SDaniel Vetter 		 */
1976f69061beSDaniel Vetter 		ret = i915_reset(dev);
1977f69061beSDaniel Vetter 
197817e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
197917e1df07SDaniel Vetter 
1980f69061beSDaniel Vetter 		if (ret == 0) {
1981f69061beSDaniel Vetter 			/*
1982f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1983f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1984f69061beSDaniel Vetter 			 * complete.
1985f69061beSDaniel Vetter 			 *
1986f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1987f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1988f69061beSDaniel Vetter 			 * updates before
1989f69061beSDaniel Vetter 			 * the counter increment.
1990f69061beSDaniel Vetter 			 */
1991f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1992f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1993f69061beSDaniel Vetter 
19945bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
1995f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
19961f83fee0SDaniel Vetter 		} else {
19972ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
1998f316a42cSBen Gamari 		}
19991f83fee0SDaniel Vetter 
200017e1df07SDaniel Vetter 		/*
200117e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
200217e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
200317e1df07SDaniel Vetter 		 */
200417e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2005f316a42cSBen Gamari 	}
20068a905236SJesse Barnes }
20078a905236SJesse Barnes 
200835aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2009c0e09200SDave Airlie {
20108a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2011bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
201263eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2013050ee91fSBen Widawsky 	int pipe, i;
201463eeaf38SJesse Barnes 
201535aed2e6SChris Wilson 	if (!eir)
201635aed2e6SChris Wilson 		return;
201763eeaf38SJesse Barnes 
2018a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
20198a905236SJesse Barnes 
2020bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2021bd9854f9SBen Widawsky 
20228a905236SJesse Barnes 	if (IS_G4X(dev)) {
20238a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
20248a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
20258a905236SJesse Barnes 
2026a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2027a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2028050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2029050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2030a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2031a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
20328a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20333143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
20348a905236SJesse Barnes 		}
20358a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
20368a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2037a70491ccSJoe Perches 			pr_err("page table error\n");
2038a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
20398a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20403143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
20418a905236SJesse Barnes 		}
20428a905236SJesse Barnes 	}
20438a905236SJesse Barnes 
2044a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
204563eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
204663eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2047a70491ccSJoe Perches 			pr_err("page table error\n");
2048a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
204963eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20503143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
205163eeaf38SJesse Barnes 		}
20528a905236SJesse Barnes 	}
20538a905236SJesse Barnes 
205463eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2055a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
20569db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2057a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
20589db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
205963eeaf38SJesse Barnes 		/* pipestat has already been acked */
206063eeaf38SJesse Barnes 	}
206163eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2062a70491ccSJoe Perches 		pr_err("instruction error\n");
2063a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2064050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2065050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2066a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
206763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
206863eeaf38SJesse Barnes 
2069a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2070a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2071a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
207263eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
20733143a2bfSChris Wilson 			POSTING_READ(IPEIR);
207463eeaf38SJesse Barnes 		} else {
207563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
207663eeaf38SJesse Barnes 
2077a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2078a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2079a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2080a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
208163eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20823143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
208363eeaf38SJesse Barnes 		}
208463eeaf38SJesse Barnes 	}
208563eeaf38SJesse Barnes 
208663eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
20873143a2bfSChris Wilson 	POSTING_READ(EIR);
208863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
208963eeaf38SJesse Barnes 	if (eir) {
209063eeaf38SJesse Barnes 		/*
209163eeaf38SJesse Barnes 		 * some errors might have become stuck,
209263eeaf38SJesse Barnes 		 * mask them.
209363eeaf38SJesse Barnes 		 */
209463eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
209563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
209663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
209763eeaf38SJesse Barnes 	}
209835aed2e6SChris Wilson }
209935aed2e6SChris Wilson 
210035aed2e6SChris Wilson /**
210135aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
210235aed2e6SChris Wilson  * @dev: drm device
210335aed2e6SChris Wilson  *
210435aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
210535aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
210635aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
210735aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
210835aed2e6SChris Wilson  * of a ring dump etc.).
210935aed2e6SChris Wilson  */
2110527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
211135aed2e6SChris Wilson {
211235aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
211335aed2e6SChris Wilson 
211435aed2e6SChris Wilson 	i915_capture_error_state(dev);
211535aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
21168a905236SJesse Barnes 
2117ba1234d1SBen Gamari 	if (wedged) {
2118f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2119f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2120ba1234d1SBen Gamari 
212111ed50ecSBen Gamari 		/*
212217e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
212317e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
212417e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
212517e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
212617e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
212717e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
212817e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
212917e1df07SDaniel Vetter 		 *
213017e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
213117e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
213217e1df07SDaniel Vetter 		 * counter atomic_t.
213311ed50ecSBen Gamari 		 */
213417e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
213511ed50ecSBen Gamari 	}
213611ed50ecSBen Gamari 
2137122f46baSDaniel Vetter 	/*
2138122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
2139122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
2140122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2141122f46baSDaniel Vetter 	 * code will deadlock.
2142122f46baSDaniel Vetter 	 */
2143122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
21448a905236SJesse Barnes }
21458a905236SJesse Barnes 
214621ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
21474e5359cdSSimon Farnsworth {
21484e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
21494e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
21504e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
215105394f39SChris Wilson 	struct drm_i915_gem_object *obj;
21524e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
21534e5359cdSSimon Farnsworth 	unsigned long flags;
21544e5359cdSSimon Farnsworth 	bool stall_detected;
21554e5359cdSSimon Farnsworth 
21564e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
21574e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
21584e5359cdSSimon Farnsworth 		return;
21594e5359cdSSimon Farnsworth 
21604e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
21614e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
21624e5359cdSSimon Farnsworth 
2163e7d841caSChris Wilson 	if (work == NULL ||
2164e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2165e7d841caSChris Wilson 	    !work->enable_stall_check) {
21664e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
21674e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
21684e5359cdSSimon Farnsworth 		return;
21694e5359cdSSimon Farnsworth 	}
21704e5359cdSSimon Farnsworth 
21714e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
217205394f39SChris Wilson 	obj = work->pending_flip_obj;
2173a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
21749db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2175446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2176f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
21774e5359cdSSimon Farnsworth 	} else {
21789db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
2179f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
218001f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
21814e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
21824e5359cdSSimon Farnsworth 	}
21834e5359cdSSimon Farnsworth 
21844e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
21854e5359cdSSimon Farnsworth 
21864e5359cdSSimon Farnsworth 	if (stall_detected) {
21874e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
21884e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
21894e5359cdSSimon Farnsworth 	}
21904e5359cdSSimon Farnsworth }
21914e5359cdSSimon Farnsworth 
219242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
219342f52ef8SKeith Packard  * we use as a pipe index
219442f52ef8SKeith Packard  */
2195f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
21960a3e67a4SJesse Barnes {
21970a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2198e9d21d7fSKeith Packard 	unsigned long irqflags;
219971e0ffa5SJesse Barnes 
22005eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
220171e0ffa5SJesse Barnes 		return -EINVAL;
22020a3e67a4SJesse Barnes 
22031ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2204f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
22057c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
22067c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
22070a3e67a4SJesse Barnes 	else
22087c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
22097c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
22108692d00eSChris Wilson 
22118692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
22128692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
22136b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
22141ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22158692d00eSChris Wilson 
22160a3e67a4SJesse Barnes 	return 0;
22170a3e67a4SJesse Barnes }
22180a3e67a4SJesse Barnes 
2219f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2220f796cf8fSJesse Barnes {
2221f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2222f796cf8fSJesse Barnes 	unsigned long irqflags;
2223b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
222440da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2225f796cf8fSJesse Barnes 
2226f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2227f796cf8fSJesse Barnes 		return -EINVAL;
2228f796cf8fSJesse Barnes 
2229f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2230b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2231b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2232b1f14ad0SJesse Barnes 
2233b1f14ad0SJesse Barnes 	return 0;
2234b1f14ad0SJesse Barnes }
2235b1f14ad0SJesse Barnes 
22367e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
22377e231dbeSJesse Barnes {
22387e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22397e231dbeSJesse Barnes 	unsigned long irqflags;
224031acc7f5SJesse Barnes 	u32 imr;
22417e231dbeSJesse Barnes 
22427e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
22437e231dbeSJesse Barnes 		return -EINVAL;
22447e231dbeSJesse Barnes 
22457e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22467e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
22473b6c42e8SDaniel Vetter 	if (pipe == PIPE_A)
22487e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
224931acc7f5SJesse Barnes 	else
22507e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22517e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
225231acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
225331acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
22547e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22557e231dbeSJesse Barnes 
22567e231dbeSJesse Barnes 	return 0;
22577e231dbeSJesse Barnes }
22587e231dbeSJesse Barnes 
2259abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2260abd58f01SBen Widawsky {
2261abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2262abd58f01SBen Widawsky 	unsigned long irqflags;
2263abd58f01SBen Widawsky 
2264abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2265abd58f01SBen Widawsky 		return -EINVAL;
2266abd58f01SBen Widawsky 
2267abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22687167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
22697167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2270abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2271abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2272abd58f01SBen Widawsky 	return 0;
2273abd58f01SBen Widawsky }
2274abd58f01SBen Widawsky 
227542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
227642f52ef8SKeith Packard  * we use as a pipe index
227742f52ef8SKeith Packard  */
2278f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
22790a3e67a4SJesse Barnes {
22800a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2281e9d21d7fSKeith Packard 	unsigned long irqflags;
22820a3e67a4SJesse Barnes 
22831ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22848692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
22856b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
22868692d00eSChris Wilson 
22877c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
22887c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
22897c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
22901ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22910a3e67a4SJesse Barnes }
22920a3e67a4SJesse Barnes 
2293f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2294f796cf8fSJesse Barnes {
2295f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2296f796cf8fSJesse Barnes 	unsigned long irqflags;
2297b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
229840da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2299f796cf8fSJesse Barnes 
2300f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2301b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2302b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2303b1f14ad0SJesse Barnes }
2304b1f14ad0SJesse Barnes 
23057e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
23067e231dbeSJesse Barnes {
23077e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23087e231dbeSJesse Barnes 	unsigned long irqflags;
230931acc7f5SJesse Barnes 	u32 imr;
23107e231dbeSJesse Barnes 
23117e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
231231acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
231331acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
23147e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
23153b6c42e8SDaniel Vetter 	if (pipe == PIPE_A)
23167e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
231731acc7f5SJesse Barnes 	else
23187e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23197e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
23207e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
23217e231dbeSJesse Barnes }
23227e231dbeSJesse Barnes 
2323abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2324abd58f01SBen Widawsky {
2325abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2326abd58f01SBen Widawsky 	unsigned long irqflags;
2327abd58f01SBen Widawsky 
2328abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2329abd58f01SBen Widawsky 		return;
2330abd58f01SBen Widawsky 
2331abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
23327167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
23337167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2334abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2335abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2336abd58f01SBen Widawsky }
2337abd58f01SBen Widawsky 
2338893eead0SChris Wilson static u32
2339893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2340852835f3SZou Nan hai {
2341893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2342893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2343893eead0SChris Wilson }
2344893eead0SChris Wilson 
23459107e9d2SChris Wilson static bool
23469107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2347893eead0SChris Wilson {
23489107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
23499107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2350f65d9421SBen Gamari }
2351f65d9421SBen Gamari 
23526274f212SChris Wilson static struct intel_ring_buffer *
23536274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2354a24a11e6SChris Wilson {
2355a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
23566274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2357a24a11e6SChris Wilson 
2358a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2359a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2360a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
23616274f212SChris Wilson 		return NULL;
2362a24a11e6SChris Wilson 
2363a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2364a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2365a24a11e6SChris Wilson 	 */
23666274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2367a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2368a24a11e6SChris Wilson 	do {
2369a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2370a24a11e6SChris Wilson 		if (cmd == ipehr)
2371a24a11e6SChris Wilson 			break;
2372a24a11e6SChris Wilson 
2373a24a11e6SChris Wilson 		acthd -= 4;
2374a24a11e6SChris Wilson 		if (acthd < acthd_min)
23756274f212SChris Wilson 			return NULL;
2376a24a11e6SChris Wilson 	} while (1);
2377a24a11e6SChris Wilson 
23786274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
23796274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2380a24a11e6SChris Wilson }
2381a24a11e6SChris Wilson 
23826274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
23836274f212SChris Wilson {
23846274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
23856274f212SChris Wilson 	struct intel_ring_buffer *signaller;
23866274f212SChris Wilson 	u32 seqno, ctl;
23876274f212SChris Wilson 
23886274f212SChris Wilson 	ring->hangcheck.deadlock = true;
23896274f212SChris Wilson 
23906274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
23916274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
23926274f212SChris Wilson 		return -1;
23936274f212SChris Wilson 
23946274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
23956274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
23966274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
23976274f212SChris Wilson 		return -1;
23986274f212SChris Wilson 
23996274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
24006274f212SChris Wilson }
24016274f212SChris Wilson 
24026274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
24036274f212SChris Wilson {
24046274f212SChris Wilson 	struct intel_ring_buffer *ring;
24056274f212SChris Wilson 	int i;
24066274f212SChris Wilson 
24076274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
24086274f212SChris Wilson 		ring->hangcheck.deadlock = false;
24096274f212SChris Wilson }
24106274f212SChris Wilson 
2411ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2412ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
24131ec14ad3SChris Wilson {
24141ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
24151ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
24169107e9d2SChris Wilson 	u32 tmp;
24179107e9d2SChris Wilson 
24186274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2419f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
24206274f212SChris Wilson 
24219107e9d2SChris Wilson 	if (IS_GEN2(dev))
2422f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
24239107e9d2SChris Wilson 
24249107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
24259107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
24269107e9d2SChris Wilson 	 * and break the hang. This should work on
24279107e9d2SChris Wilson 	 * all but the second generation chipsets.
24289107e9d2SChris Wilson 	 */
24299107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
24301ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
24311ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
24321ec14ad3SChris Wilson 			  ring->name);
243309e14bf3SChris Wilson 		i915_handle_error(dev, false);
24341ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2435f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
24361ec14ad3SChris Wilson 	}
2437a24a11e6SChris Wilson 
24386274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
24396274f212SChris Wilson 		switch (semaphore_passed(ring)) {
24406274f212SChris Wilson 		default:
2441f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
24426274f212SChris Wilson 		case 1:
2443a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
2444a24a11e6SChris Wilson 				  ring->name);
244509e14bf3SChris Wilson 			i915_handle_error(dev, false);
2446a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2447f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
24486274f212SChris Wilson 		case 0:
2449f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
24506274f212SChris Wilson 		}
24519107e9d2SChris Wilson 	}
24529107e9d2SChris Wilson 
2453f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2454a24a11e6SChris Wilson }
2455d1e61e7fSChris Wilson 
2456f65d9421SBen Gamari /**
2457f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
245805407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
245905407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
246005407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
246105407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
246205407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2463f65d9421SBen Gamari  */
2464a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2465f65d9421SBen Gamari {
2466f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2467f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2468b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2469b4519513SChris Wilson 	int i;
247005407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
24719107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
24729107e9d2SChris Wilson #define BUSY 1
24739107e9d2SChris Wilson #define KICK 5
24749107e9d2SChris Wilson #define HUNG 20
24759107e9d2SChris Wilson #define FIRE 30
2476893eead0SChris Wilson 
24773e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
24783e0dc6b0SBen Widawsky 		return;
24793e0dc6b0SBen Widawsky 
2480b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
248105407ff8SMika Kuoppala 		u32 seqno, acthd;
24829107e9d2SChris Wilson 		bool busy = true;
2483b4519513SChris Wilson 
24846274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
24856274f212SChris Wilson 
248605407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
248705407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
248805407ff8SMika Kuoppala 
248905407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
24909107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2491da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2492da661464SMika Kuoppala 
24939107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
24949107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2495094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2496f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
24979107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
24989107e9d2SChris Wilson 								  ring->name);
2499f4adcd24SDaniel Vetter 						else
2500f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2501f4adcd24SDaniel Vetter 								 ring->name);
25029107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2503094f9a54SChris Wilson 					}
2504094f9a54SChris Wilson 					/* Safeguard against driver failure */
2505094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
25069107e9d2SChris Wilson 				} else
25079107e9d2SChris Wilson 					busy = false;
250805407ff8SMika Kuoppala 			} else {
25096274f212SChris Wilson 				/* We always increment the hangcheck score
25106274f212SChris Wilson 				 * if the ring is busy and still processing
25116274f212SChris Wilson 				 * the same request, so that no single request
25126274f212SChris Wilson 				 * can run indefinitely (such as a chain of
25136274f212SChris Wilson 				 * batches). The only time we do not increment
25146274f212SChris Wilson 				 * the hangcheck score on this ring, if this
25156274f212SChris Wilson 				 * ring is in a legitimate wait for another
25166274f212SChris Wilson 				 * ring. In that case the waiting ring is a
25176274f212SChris Wilson 				 * victim and we want to be sure we catch the
25186274f212SChris Wilson 				 * right culprit. Then every time we do kick
25196274f212SChris Wilson 				 * the ring, add a small increment to the
25206274f212SChris Wilson 				 * score so that we can catch a batch that is
25216274f212SChris Wilson 				 * being repeatedly kicked and so responsible
25226274f212SChris Wilson 				 * for stalling the machine.
25239107e9d2SChris Wilson 				 */
2524ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2525ad8beaeaSMika Kuoppala 								    acthd);
2526ad8beaeaSMika Kuoppala 
2527ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2528da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2529f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
25306274f212SChris Wilson 					break;
2531f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2532ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
25336274f212SChris Wilson 					break;
2534f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2535ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
25366274f212SChris Wilson 					break;
2537f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2538ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
25396274f212SChris Wilson 					stuck[i] = true;
25406274f212SChris Wilson 					break;
25416274f212SChris Wilson 				}
254205407ff8SMika Kuoppala 			}
25439107e9d2SChris Wilson 		} else {
2544da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2545da661464SMika Kuoppala 
25469107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
25479107e9d2SChris Wilson 			 * attempts across multiple batches.
25489107e9d2SChris Wilson 			 */
25499107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
25509107e9d2SChris Wilson 				ring->hangcheck.score--;
2551cbb465e7SChris Wilson 		}
2552f65d9421SBen Gamari 
255305407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
255405407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
25559107e9d2SChris Wilson 		busy_count += busy;
255605407ff8SMika Kuoppala 	}
255705407ff8SMika Kuoppala 
255805407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
25599107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2560b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
256105407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2562a43adf07SChris Wilson 				 ring->name);
2563a43adf07SChris Wilson 			rings_hung++;
256405407ff8SMika Kuoppala 		}
256505407ff8SMika Kuoppala 	}
256605407ff8SMika Kuoppala 
256705407ff8SMika Kuoppala 	if (rings_hung)
256805407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
256905407ff8SMika Kuoppala 
257005407ff8SMika Kuoppala 	if (busy_count)
257105407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
257205407ff8SMika Kuoppala 		 * being added */
257310cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
257410cd45b6SMika Kuoppala }
257510cd45b6SMika Kuoppala 
257610cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
257710cd45b6SMika Kuoppala {
257810cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
257910cd45b6SMika Kuoppala 	if (!i915_enable_hangcheck)
258010cd45b6SMika Kuoppala 		return;
258110cd45b6SMika Kuoppala 
258299584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
258310cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2584f65d9421SBen Gamari }
2585f65d9421SBen Gamari 
258691738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
258791738a95SPaulo Zanoni {
258891738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
258991738a95SPaulo Zanoni 
259091738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
259191738a95SPaulo Zanoni 		return;
259291738a95SPaulo Zanoni 
259391738a95SPaulo Zanoni 	/* south display irq */
259491738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
259591738a95SPaulo Zanoni 	/*
259691738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
259791738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
259891738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
259991738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
260091738a95SPaulo Zanoni 	 */
260191738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
260291738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
260391738a95SPaulo Zanoni }
260491738a95SPaulo Zanoni 
2605d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2606d18ea1b5SDaniel Vetter {
2607d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2608d18ea1b5SDaniel Vetter 
2609d18ea1b5SDaniel Vetter 	/* and GT */
2610d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2611d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2612d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2613d18ea1b5SDaniel Vetter 
2614d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2615d18ea1b5SDaniel Vetter 		/* and PM */
2616d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2617d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2618d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2619d18ea1b5SDaniel Vetter 	}
2620d18ea1b5SDaniel Vetter }
2621d18ea1b5SDaniel Vetter 
2622c0e09200SDave Airlie /* drm_dma.h hooks
2623c0e09200SDave Airlie */
2624f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2625036a4a7dSZhenyu Wang {
2626036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2627036a4a7dSZhenyu Wang 
26284697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
26294697995bSJesse Barnes 
2630036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2631bdfcdb63SDaniel Vetter 
2632036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2633036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
26343143a2bfSChris Wilson 	POSTING_READ(DEIER);
2635036a4a7dSZhenyu Wang 
2636d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2637c650156aSZhenyu Wang 
263891738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
26397d99163dSBen Widawsky }
26407d99163dSBen Widawsky 
26417e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
26427e231dbeSJesse Barnes {
26437e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26447e231dbeSJesse Barnes 	int pipe;
26457e231dbeSJesse Barnes 
26467e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
26477e231dbeSJesse Barnes 
26487e231dbeSJesse Barnes 	/* VLV magic */
26497e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
26507e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
26517e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
26527e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
26537e231dbeSJesse Barnes 
26547e231dbeSJesse Barnes 	/* and GT */
26557e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
26567e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2657d18ea1b5SDaniel Vetter 
2658d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
26597e231dbeSJesse Barnes 
26607e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
26617e231dbeSJesse Barnes 
26627e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
26637e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
26647e231dbeSJesse Barnes 	for_each_pipe(pipe)
26657e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
26667e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26677e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
26687e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
26697e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
26707e231dbeSJesse Barnes }
26717e231dbeSJesse Barnes 
2672abd58f01SBen Widawsky static void gen8_irq_preinstall(struct drm_device *dev)
2673abd58f01SBen Widawsky {
2674abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2675abd58f01SBen Widawsky 	int pipe;
2676abd58f01SBen Widawsky 
2677abd58f01SBen Widawsky 	atomic_set(&dev_priv->irq_received, 0);
2678abd58f01SBen Widawsky 
2679abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2680abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2681abd58f01SBen Widawsky 
2682abd58f01SBen Widawsky 	/* IIR can theoretically queue up two events. Be paranoid */
2683abd58f01SBen Widawsky #define GEN8_IRQ_INIT_NDX(type, which) do { \
2684abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
2685abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IMR(which)); \
2686abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER(which), 0); \
2687abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2688abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IIR(which)); \
2689abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2690abd58f01SBen Widawsky 	} while (0)
2691abd58f01SBen Widawsky 
2692abd58f01SBen Widawsky #define GEN8_IRQ_INIT(type) do { \
2693abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
2694abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IMR); \
2695abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER, 0); \
2696abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2697abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IIR); \
2698abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2699abd58f01SBen Widawsky 	} while (0)
2700abd58f01SBen Widawsky 
2701abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 0);
2702abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 1);
2703abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 2);
2704abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 3);
2705abd58f01SBen Widawsky 
2706abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2707abd58f01SBen Widawsky 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe);
2708abd58f01SBen Widawsky 	}
2709abd58f01SBen Widawsky 
2710abd58f01SBen Widawsky 	GEN8_IRQ_INIT(DE_PORT);
2711abd58f01SBen Widawsky 	GEN8_IRQ_INIT(DE_MISC);
2712abd58f01SBen Widawsky 	GEN8_IRQ_INIT(PCU);
2713abd58f01SBen Widawsky #undef GEN8_IRQ_INIT
2714abd58f01SBen Widawsky #undef GEN8_IRQ_INIT_NDX
2715abd58f01SBen Widawsky 
2716abd58f01SBen Widawsky 	POSTING_READ(GEN8_PCU_IIR);
2717abd58f01SBen Widawsky }
2718abd58f01SBen Widawsky 
271982a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
272082a28bcfSDaniel Vetter {
272182a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
272282a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
272382a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2724fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
272582a28bcfSDaniel Vetter 
272682a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2727fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
272882a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2729cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2730fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
273182a28bcfSDaniel Vetter 	} else {
2732fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
273382a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2734cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2735fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
273682a28bcfSDaniel Vetter 	}
273782a28bcfSDaniel Vetter 
2738fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
273982a28bcfSDaniel Vetter 
27407fe0b973SKeith Packard 	/*
27417fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
27427fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
27437fe0b973SKeith Packard 	 *
27447fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
27457fe0b973SKeith Packard 	 */
27467fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
27477fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
27487fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
27497fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
27507fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
27517fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
27527fe0b973SKeith Packard }
27537fe0b973SKeith Packard 
2754d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2755d46da437SPaulo Zanoni {
2756d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
275782a28bcfSDaniel Vetter 	u32 mask;
2758d46da437SPaulo Zanoni 
2759692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2760692a04cfSDaniel Vetter 		return;
2761692a04cfSDaniel Vetter 
27628664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
27638664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2764de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
27658664281bSPaulo Zanoni 	} else {
27668664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
27678664281bSPaulo Zanoni 
27688664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
27698664281bSPaulo Zanoni 	}
2770ab5c608bSBen Widawsky 
2771d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2772d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2773d46da437SPaulo Zanoni }
2774d46da437SPaulo Zanoni 
27750a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
27760a9a8c91SDaniel Vetter {
27770a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
27780a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
27790a9a8c91SDaniel Vetter 
27800a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
27810a9a8c91SDaniel Vetter 
27820a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
2783040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
27840a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
278535a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
278635a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
27870a9a8c91SDaniel Vetter 	}
27880a9a8c91SDaniel Vetter 
27890a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
27900a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
27910a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
27920a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
27930a9a8c91SDaniel Vetter 	} else {
27940a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
27950a9a8c91SDaniel Vetter 	}
27960a9a8c91SDaniel Vetter 
27970a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
27980a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
27990a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
28000a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
28010a9a8c91SDaniel Vetter 
28020a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
28030a9a8c91SDaniel Vetter 		pm_irqs |= GEN6_PM_RPS_EVENTS;
28040a9a8c91SDaniel Vetter 
28050a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
28060a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
28070a9a8c91SDaniel Vetter 
2808605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
28090a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2810605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
28110a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
28120a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
28130a9a8c91SDaniel Vetter 	}
28140a9a8c91SDaniel Vetter }
28150a9a8c91SDaniel Vetter 
2816f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2817036a4a7dSZhenyu Wang {
28184bc9d430SDaniel Vetter 	unsigned long irqflags;
2819036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
28208e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
28218e76f8dcSPaulo Zanoni 
28228e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
28238e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
28248e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
28258e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
28268e76f8dcSPaulo Zanoni 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
28278e76f8dcSPaulo Zanoni 				DE_ERR_INT_IVB);
28288e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
28298e76f8dcSPaulo Zanoni 			      DE_PIPEA_VBLANK_IVB);
28308e76f8dcSPaulo Zanoni 
28318e76f8dcSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
28328e76f8dcSPaulo Zanoni 	} else {
28338e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2834ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
28355b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
28365b3a856bSDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
28375b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
28385b3a856bSDaniel Vetter 				DE_POISON);
28398e76f8dcSPaulo Zanoni 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
28408e76f8dcSPaulo Zanoni 	}
2841036a4a7dSZhenyu Wang 
28421ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2843036a4a7dSZhenyu Wang 
2844036a4a7dSZhenyu Wang 	/* should always can generate irq */
2845036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
28461ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
28478e76f8dcSPaulo Zanoni 	I915_WRITE(DEIER, display_mask | extra_mask);
28483143a2bfSChris Wilson 	POSTING_READ(DEIER);
2849036a4a7dSZhenyu Wang 
28500a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
2851036a4a7dSZhenyu Wang 
2852d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
28537fe0b973SKeith Packard 
2854f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
28556005ce42SDaniel Vetter 		/* Enable PCU event interrupts
28566005ce42SDaniel Vetter 		 *
28576005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
28584bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
28594bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
28604bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2861f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
28624bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2863f97108d1SJesse Barnes 	}
2864f97108d1SJesse Barnes 
2865036a4a7dSZhenyu Wang 	return 0;
2866036a4a7dSZhenyu Wang }
2867036a4a7dSZhenyu Wang 
28687e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
28697e231dbeSJesse Barnes {
28707e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
28717e231dbeSJesse Barnes 	u32 enable_mask;
2872379ef82dSDaniel Vetter 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV |
2873379ef82dSDaniel Vetter 		PIPE_CRC_DONE_ENABLE;
2874b79480baSDaniel Vetter 	unsigned long irqflags;
28757e231dbeSJesse Barnes 
28767e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
287731acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
287831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
287931acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
28807e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
28817e231dbeSJesse Barnes 
288231acc7f5SJesse Barnes 	/*
288331acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
288431acc7f5SJesse Barnes 	 * toggle them based on usage.
288531acc7f5SJesse Barnes 	 */
288631acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
288731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
288831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
28897e231dbeSJesse Barnes 
289020afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
289120afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
289220afbda2SDaniel Vetter 
28937e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
28947e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
28957e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28967e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
28977e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
28987e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
28997e231dbeSJesse Barnes 
2900b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2901b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2902b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
29033b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_enable);
29043b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_EVENT_ENABLE);
29053b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_enable);
2906b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
290731acc7f5SJesse Barnes 
29087e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
29097e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
29107e231dbeSJesse Barnes 
29110a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
29127e231dbeSJesse Barnes 
29137e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
29147e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
29157e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
29167e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
29177e231dbeSJesse Barnes #endif
29187e231dbeSJesse Barnes 
29197e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
292020afbda2SDaniel Vetter 
292120afbda2SDaniel Vetter 	return 0;
292220afbda2SDaniel Vetter }
292320afbda2SDaniel Vetter 
2924abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
2925abd58f01SBen Widawsky {
2926abd58f01SBen Widawsky 	int i;
2927abd58f01SBen Widawsky 
2928abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
2929abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
2930abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
2931abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
2932abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
2933abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
2934abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
2935abd58f01SBen Widawsky 		0,
2936abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT
2937abd58f01SBen Widawsky 		};
2938abd58f01SBen Widawsky 
2939abd58f01SBen Widawsky 	for (i = 0; i < ARRAY_SIZE(gt_interrupts); i++) {
2940abd58f01SBen Widawsky 		u32 tmp = I915_READ(GEN8_GT_IIR(i));
2941abd58f01SBen Widawsky 		if (tmp)
2942abd58f01SBen Widawsky 			DRM_ERROR("Interrupt (%d) should have been masked in pre-install 0x%08x\n",
2943abd58f01SBen Widawsky 				  i, tmp);
2944abd58f01SBen Widawsky 		I915_WRITE(GEN8_GT_IMR(i), ~gt_interrupts[i]);
2945abd58f01SBen Widawsky 		I915_WRITE(GEN8_GT_IER(i), gt_interrupts[i]);
2946abd58f01SBen Widawsky 	}
2947abd58f01SBen Widawsky 	POSTING_READ(GEN8_GT_IER(0));
2948abd58f01SBen Widawsky }
2949abd58f01SBen Widawsky 
2950abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
2951abd58f01SBen Widawsky {
2952abd58f01SBen Widawsky 	struct drm_device *dev = dev_priv->dev;
295313b3a0a7SDaniel Vetter 	uint32_t de_pipe_masked = GEN8_PIPE_FLIP_DONE |
29540fbe7870SDaniel Vetter 		GEN8_PIPE_CDCLK_CRC_DONE |
295538d83c96SDaniel Vetter 		GEN8_PIPE_FIFO_UNDERRUN |
295630100f2bSDaniel Vetter 		GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
295713b3a0a7SDaniel Vetter 	uint32_t de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK;
2958abd58f01SBen Widawsky 	int pipe;
295913b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
296013b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
296113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
2962abd58f01SBen Widawsky 
2963abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2964abd58f01SBen Widawsky 		u32 tmp = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2965abd58f01SBen Widawsky 		if (tmp)
2966abd58f01SBen Widawsky 			DRM_ERROR("Interrupt (%d) should have been masked in pre-install 0x%08x\n",
2967abd58f01SBen Widawsky 				  pipe, tmp);
2968abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2969abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IER(pipe), de_pipe_enables);
2970abd58f01SBen Widawsky 	}
2971abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_ISR(0));
2972abd58f01SBen Widawsky 
29736d766f02SDaniel Vetter 	I915_WRITE(GEN8_DE_PORT_IMR, ~GEN8_AUX_CHANNEL_A);
29746d766f02SDaniel Vetter 	I915_WRITE(GEN8_DE_PORT_IER, GEN8_AUX_CHANNEL_A);
2975abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PORT_IER);
2976abd58f01SBen Widawsky }
2977abd58f01SBen Widawsky 
2978abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
2979abd58f01SBen Widawsky {
2980abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2981abd58f01SBen Widawsky 
2982abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
2983abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
2984abd58f01SBen Widawsky 
2985abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
2986abd58f01SBen Widawsky 
2987abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
2988abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2989abd58f01SBen Widawsky 
2990abd58f01SBen Widawsky 	return 0;
2991abd58f01SBen Widawsky }
2992abd58f01SBen Widawsky 
2993abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
2994abd58f01SBen Widawsky {
2995abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2996abd58f01SBen Widawsky 	int pipe;
2997abd58f01SBen Widawsky 
2998abd58f01SBen Widawsky 	if (!dev_priv)
2999abd58f01SBen Widawsky 		return;
3000abd58f01SBen Widawsky 
3001abd58f01SBen Widawsky 	atomic_set(&dev_priv->irq_received, 0);
3002abd58f01SBen Widawsky 
3003abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3004abd58f01SBen Widawsky 
3005abd58f01SBen Widawsky #define GEN8_IRQ_FINI_NDX(type, which) do { \
3006abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
3007abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER(which), 0); \
3008abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
3009abd58f01SBen Widawsky 	} while (0)
3010abd58f01SBen Widawsky 
3011abd58f01SBen Widawsky #define GEN8_IRQ_FINI(type) do { \
3012abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
3013abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER, 0); \
3014abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
3015abd58f01SBen Widawsky 	} while (0)
3016abd58f01SBen Widawsky 
3017abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 0);
3018abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 1);
3019abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 2);
3020abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 3);
3021abd58f01SBen Widawsky 
3022abd58f01SBen Widawsky 	for_each_pipe(pipe) {
3023abd58f01SBen Widawsky 		GEN8_IRQ_FINI_NDX(DE_PIPE, pipe);
3024abd58f01SBen Widawsky 	}
3025abd58f01SBen Widawsky 
3026abd58f01SBen Widawsky 	GEN8_IRQ_FINI(DE_PORT);
3027abd58f01SBen Widawsky 	GEN8_IRQ_FINI(DE_MISC);
3028abd58f01SBen Widawsky 	GEN8_IRQ_FINI(PCU);
3029abd58f01SBen Widawsky #undef GEN8_IRQ_FINI
3030abd58f01SBen Widawsky #undef GEN8_IRQ_FINI_NDX
3031abd58f01SBen Widawsky 
3032abd58f01SBen Widawsky 	POSTING_READ(GEN8_PCU_IIR);
3033abd58f01SBen Widawsky }
3034abd58f01SBen Widawsky 
30357e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
30367e231dbeSJesse Barnes {
30377e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
30387e231dbeSJesse Barnes 	int pipe;
30397e231dbeSJesse Barnes 
30407e231dbeSJesse Barnes 	if (!dev_priv)
30417e231dbeSJesse Barnes 		return;
30427e231dbeSJesse Barnes 
3043ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3044ac4c16c5SEgbert Eich 
30457e231dbeSJesse Barnes 	for_each_pipe(pipe)
30467e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
30477e231dbeSJesse Barnes 
30487e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
30497e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
30507e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
30517e231dbeSJesse Barnes 	for_each_pipe(pipe)
30527e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
30537e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
30547e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
30557e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
30567e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
30577e231dbeSJesse Barnes }
30587e231dbeSJesse Barnes 
3059f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3060036a4a7dSZhenyu Wang {
3061036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
30624697995bSJesse Barnes 
30634697995bSJesse Barnes 	if (!dev_priv)
30644697995bSJesse Barnes 		return;
30654697995bSJesse Barnes 
3066ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3067ac4c16c5SEgbert Eich 
3068036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
3069036a4a7dSZhenyu Wang 
3070036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
3071036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
3072036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
30738664281bSPaulo Zanoni 	if (IS_GEN7(dev))
30748664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
3075036a4a7dSZhenyu Wang 
3076036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
3077036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
3078036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
3079192aac1fSKeith Packard 
3080ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
3081ab5c608bSBen Widawsky 		return;
3082ab5c608bSBen Widawsky 
3083192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
3084192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
3085192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
30868664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
30878664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
3088036a4a7dSZhenyu Wang }
3089036a4a7dSZhenyu Wang 
3090c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3091c2798b19SChris Wilson {
3092c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3093c2798b19SChris Wilson 	int pipe;
3094c2798b19SChris Wilson 
3095c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3096c2798b19SChris Wilson 
3097c2798b19SChris Wilson 	for_each_pipe(pipe)
3098c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3099c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3100c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3101c2798b19SChris Wilson 	POSTING_READ16(IER);
3102c2798b19SChris Wilson }
3103c2798b19SChris Wilson 
3104c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3105c2798b19SChris Wilson {
3106c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3107379ef82dSDaniel Vetter 	unsigned long irqflags;
3108c2798b19SChris Wilson 
3109c2798b19SChris Wilson 	I915_WRITE16(EMR,
3110c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3111c2798b19SChris Wilson 
3112c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3113c2798b19SChris Wilson 	dev_priv->irq_mask =
3114c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3115c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3116c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3117c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3118c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3119c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3120c2798b19SChris Wilson 
3121c2798b19SChris Wilson 	I915_WRITE16(IER,
3122c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3123c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3124c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3125c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3126c2798b19SChris Wilson 	POSTING_READ16(IER);
3127c2798b19SChris Wilson 
3128379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3129379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3130379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
31313b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_ENABLE);
31323b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_ENABLE);
3133379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3134379ef82dSDaniel Vetter 
3135c2798b19SChris Wilson 	return 0;
3136c2798b19SChris Wilson }
3137c2798b19SChris Wilson 
313890a72f87SVille Syrjälä /*
313990a72f87SVille Syrjälä  * Returns true when a page flip has completed.
314090a72f87SVille Syrjälä  */
314190a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
314290a72f87SVille Syrjälä 			       int pipe, u16 iir)
314390a72f87SVille Syrjälä {
314490a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
314590a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
314690a72f87SVille Syrjälä 
314790a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
314890a72f87SVille Syrjälä 		return false;
314990a72f87SVille Syrjälä 
315090a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
315190a72f87SVille Syrjälä 		return false;
315290a72f87SVille Syrjälä 
315390a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
315490a72f87SVille Syrjälä 
315590a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
315690a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
315790a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
315890a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
315990a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
316090a72f87SVille Syrjälä 	 */
316190a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
316290a72f87SVille Syrjälä 		return false;
316390a72f87SVille Syrjälä 
316490a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
316590a72f87SVille Syrjälä 
316690a72f87SVille Syrjälä 	return true;
316790a72f87SVille Syrjälä }
316890a72f87SVille Syrjälä 
3169ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3170c2798b19SChris Wilson {
3171c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3172c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3173c2798b19SChris Wilson 	u16 iir, new_iir;
3174c2798b19SChris Wilson 	u32 pipe_stats[2];
3175c2798b19SChris Wilson 	unsigned long irqflags;
3176c2798b19SChris Wilson 	int pipe;
3177c2798b19SChris Wilson 	u16 flip_mask =
3178c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3179c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3180c2798b19SChris Wilson 
3181c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3182c2798b19SChris Wilson 
3183c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3184c2798b19SChris Wilson 	if (iir == 0)
3185c2798b19SChris Wilson 		return IRQ_NONE;
3186c2798b19SChris Wilson 
3187c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3188c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3189c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3190c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3191c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3192c2798b19SChris Wilson 		 */
3193c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3194c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3195c2798b19SChris Wilson 			i915_handle_error(dev, false);
3196c2798b19SChris Wilson 
3197c2798b19SChris Wilson 		for_each_pipe(pipe) {
3198c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3199c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3200c2798b19SChris Wilson 
3201c2798b19SChris Wilson 			/*
3202c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3203c2798b19SChris Wilson 			 */
3204c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3205c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3206c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3207c2798b19SChris Wilson 							 pipe_name(pipe));
3208c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3209c2798b19SChris Wilson 			}
3210c2798b19SChris Wilson 		}
3211c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3212c2798b19SChris Wilson 
3213c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3214c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3215c2798b19SChris Wilson 
3216d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3217c2798b19SChris Wilson 
3218c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3219c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3220c2798b19SChris Wilson 
32214356d586SDaniel Vetter 		for_each_pipe(pipe) {
32224356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
32234356d586SDaniel Vetter 			    i8xx_handle_vblank(dev, pipe, iir))
32244356d586SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3225c2798b19SChris Wilson 
32264356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3227277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
32284356d586SDaniel Vetter 		}
3229c2798b19SChris Wilson 
3230c2798b19SChris Wilson 		iir = new_iir;
3231c2798b19SChris Wilson 	}
3232c2798b19SChris Wilson 
3233c2798b19SChris Wilson 	return IRQ_HANDLED;
3234c2798b19SChris Wilson }
3235c2798b19SChris Wilson 
3236c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3237c2798b19SChris Wilson {
3238c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3239c2798b19SChris Wilson 	int pipe;
3240c2798b19SChris Wilson 
3241c2798b19SChris Wilson 	for_each_pipe(pipe) {
3242c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3243c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3244c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3245c2798b19SChris Wilson 	}
3246c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3247c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3248c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3249c2798b19SChris Wilson }
3250c2798b19SChris Wilson 
3251a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3252a266c7d5SChris Wilson {
3253a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3254a266c7d5SChris Wilson 	int pipe;
3255a266c7d5SChris Wilson 
3256a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3257a266c7d5SChris Wilson 
3258a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3259a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3260a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3261a266c7d5SChris Wilson 	}
3262a266c7d5SChris Wilson 
326300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3264a266c7d5SChris Wilson 	for_each_pipe(pipe)
3265a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3266a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3267a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3268a266c7d5SChris Wilson 	POSTING_READ(IER);
3269a266c7d5SChris Wilson }
3270a266c7d5SChris Wilson 
3271a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3272a266c7d5SChris Wilson {
3273a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
327438bde180SChris Wilson 	u32 enable_mask;
3275379ef82dSDaniel Vetter 	unsigned long irqflags;
3276a266c7d5SChris Wilson 
327738bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
327838bde180SChris Wilson 
327938bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
328038bde180SChris Wilson 	dev_priv->irq_mask =
328138bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
328238bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
328338bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
328438bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
328538bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
328638bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
328738bde180SChris Wilson 
328838bde180SChris Wilson 	enable_mask =
328938bde180SChris Wilson 		I915_ASLE_INTERRUPT |
329038bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
329138bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
329238bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
329338bde180SChris Wilson 		I915_USER_INTERRUPT;
329438bde180SChris Wilson 
3295a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
329620afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
329720afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
329820afbda2SDaniel Vetter 
3299a266c7d5SChris Wilson 		/* Enable in IER... */
3300a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3301a266c7d5SChris Wilson 		/* and unmask in IMR */
3302a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3303a266c7d5SChris Wilson 	}
3304a266c7d5SChris Wilson 
3305a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3306a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3307a266c7d5SChris Wilson 	POSTING_READ(IER);
3308a266c7d5SChris Wilson 
3309f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
331020afbda2SDaniel Vetter 
3311379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3312379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3313379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
33143b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_ENABLE);
33153b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_ENABLE);
3316379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3317379ef82dSDaniel Vetter 
331820afbda2SDaniel Vetter 	return 0;
331920afbda2SDaniel Vetter }
332020afbda2SDaniel Vetter 
332190a72f87SVille Syrjälä /*
332290a72f87SVille Syrjälä  * Returns true when a page flip has completed.
332390a72f87SVille Syrjälä  */
332490a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
332590a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
332690a72f87SVille Syrjälä {
332790a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
332890a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
332990a72f87SVille Syrjälä 
333090a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
333190a72f87SVille Syrjälä 		return false;
333290a72f87SVille Syrjälä 
333390a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
333490a72f87SVille Syrjälä 		return false;
333590a72f87SVille Syrjälä 
333690a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
333790a72f87SVille Syrjälä 
333890a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
333990a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
334090a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
334190a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
334290a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
334390a72f87SVille Syrjälä 	 */
334490a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
334590a72f87SVille Syrjälä 		return false;
334690a72f87SVille Syrjälä 
334790a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
334890a72f87SVille Syrjälä 
334990a72f87SVille Syrjälä 	return true;
335090a72f87SVille Syrjälä }
335190a72f87SVille Syrjälä 
3352ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3353a266c7d5SChris Wilson {
3354a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3355a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
33568291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3357a266c7d5SChris Wilson 	unsigned long irqflags;
335838bde180SChris Wilson 	u32 flip_mask =
335938bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
336038bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
336138bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3362a266c7d5SChris Wilson 
3363a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3364a266c7d5SChris Wilson 
3365a266c7d5SChris Wilson 	iir = I915_READ(IIR);
336638bde180SChris Wilson 	do {
336738bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
33688291ee90SChris Wilson 		bool blc_event = false;
3369a266c7d5SChris Wilson 
3370a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3371a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3372a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3373a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3374a266c7d5SChris Wilson 		 */
3375a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3376a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3377a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3378a266c7d5SChris Wilson 
3379a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3380a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3381a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3382a266c7d5SChris Wilson 
338338bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3384a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3385a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3386a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3387a266c7d5SChris Wilson 							 pipe_name(pipe));
3388a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
338938bde180SChris Wilson 				irq_received = true;
3390a266c7d5SChris Wilson 			}
3391a266c7d5SChris Wilson 		}
3392a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3393a266c7d5SChris Wilson 
3394a266c7d5SChris Wilson 		if (!irq_received)
3395a266c7d5SChris Wilson 			break;
3396a266c7d5SChris Wilson 
3397a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3398a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3399a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3400a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3401b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3402a266c7d5SChris Wilson 
3403a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3404a266c7d5SChris Wilson 				  hotplug_status);
340591d131d2SDaniel Vetter 
340610a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
340791d131d2SDaniel Vetter 
3408a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
340938bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3410a266c7d5SChris Wilson 		}
3411a266c7d5SChris Wilson 
341238bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3413a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3414a266c7d5SChris Wilson 
3415a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3416a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3417a266c7d5SChris Wilson 
3418a266c7d5SChris Wilson 		for_each_pipe(pipe) {
341938bde180SChris Wilson 			int plane = pipe;
342038bde180SChris Wilson 			if (IS_MOBILE(dev))
342138bde180SChris Wilson 				plane = !plane;
34225e2032d4SVille Syrjälä 
342390a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
342490a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
342590a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3426a266c7d5SChris Wilson 
3427a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3428a266c7d5SChris Wilson 				blc_event = true;
34294356d586SDaniel Vetter 
34304356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3431277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
3432a266c7d5SChris Wilson 		}
3433a266c7d5SChris Wilson 
3434a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3435a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3436a266c7d5SChris Wilson 
3437a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3438a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3439a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3440a266c7d5SChris Wilson 		 * we would never get another interrupt.
3441a266c7d5SChris Wilson 		 *
3442a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3443a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3444a266c7d5SChris Wilson 		 * another one.
3445a266c7d5SChris Wilson 		 *
3446a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3447a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3448a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3449a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3450a266c7d5SChris Wilson 		 * stray interrupts.
3451a266c7d5SChris Wilson 		 */
345238bde180SChris Wilson 		ret = IRQ_HANDLED;
3453a266c7d5SChris Wilson 		iir = new_iir;
345438bde180SChris Wilson 	} while (iir & ~flip_mask);
3455a266c7d5SChris Wilson 
3456d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
34578291ee90SChris Wilson 
3458a266c7d5SChris Wilson 	return ret;
3459a266c7d5SChris Wilson }
3460a266c7d5SChris Wilson 
3461a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3462a266c7d5SChris Wilson {
3463a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3464a266c7d5SChris Wilson 	int pipe;
3465a266c7d5SChris Wilson 
3466ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3467ac4c16c5SEgbert Eich 
3468a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3469a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3470a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3471a266c7d5SChris Wilson 	}
3472a266c7d5SChris Wilson 
347300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
347455b39755SChris Wilson 	for_each_pipe(pipe) {
347555b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3476a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
347755b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
347855b39755SChris Wilson 	}
3479a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3480a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3481a266c7d5SChris Wilson 
3482a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3483a266c7d5SChris Wilson }
3484a266c7d5SChris Wilson 
3485a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3486a266c7d5SChris Wilson {
3487a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3488a266c7d5SChris Wilson 	int pipe;
3489a266c7d5SChris Wilson 
3490a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3491a266c7d5SChris Wilson 
3492a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3493a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3494a266c7d5SChris Wilson 
3495a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3496a266c7d5SChris Wilson 	for_each_pipe(pipe)
3497a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3498a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3499a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3500a266c7d5SChris Wilson 	POSTING_READ(IER);
3501a266c7d5SChris Wilson }
3502a266c7d5SChris Wilson 
3503a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3504a266c7d5SChris Wilson {
3505a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3506bbba0a97SChris Wilson 	u32 enable_mask;
3507a266c7d5SChris Wilson 	u32 error_mask;
3508b79480baSDaniel Vetter 	unsigned long irqflags;
3509a266c7d5SChris Wilson 
3510a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3511bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3512adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3513bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3514bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3515bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3516bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3517bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3518bbba0a97SChris Wilson 
3519bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
352021ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
352121ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3522bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3523bbba0a97SChris Wilson 
3524bbba0a97SChris Wilson 	if (IS_G4X(dev))
3525bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3526a266c7d5SChris Wilson 
3527b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3528b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3529b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
35303b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_EVENT_ENABLE);
35313b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_ENABLE);
35323b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_ENABLE);
3533b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3534a266c7d5SChris Wilson 
3535a266c7d5SChris Wilson 	/*
3536a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3537a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3538a266c7d5SChris Wilson 	 */
3539a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3540a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3541a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3542a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3543a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3544a266c7d5SChris Wilson 	} else {
3545a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3546a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3547a266c7d5SChris Wilson 	}
3548a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3549a266c7d5SChris Wilson 
3550a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3551a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3552a266c7d5SChris Wilson 	POSTING_READ(IER);
3553a266c7d5SChris Wilson 
355420afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
355520afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
355620afbda2SDaniel Vetter 
3557f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
355820afbda2SDaniel Vetter 
355920afbda2SDaniel Vetter 	return 0;
356020afbda2SDaniel Vetter }
356120afbda2SDaniel Vetter 
3562bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
356320afbda2SDaniel Vetter {
356420afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3565e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3566cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
356720afbda2SDaniel Vetter 	u32 hotplug_en;
356820afbda2SDaniel Vetter 
3569b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3570b5ea2d56SDaniel Vetter 
3571bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3572bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3573bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3574adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3575e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3576cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3577cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3578cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3579a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3580a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3581a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3582a266c7d5SChris Wilson 		*/
3583a266c7d5SChris Wilson 		if (IS_G4X(dev))
3584a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
358585fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3586a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3587a266c7d5SChris Wilson 
3588a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3589a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3590a266c7d5SChris Wilson 	}
3591bac56d5bSEgbert Eich }
3592a266c7d5SChris Wilson 
3593ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3594a266c7d5SChris Wilson {
3595a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3596a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3597a266c7d5SChris Wilson 	u32 iir, new_iir;
3598a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3599a266c7d5SChris Wilson 	unsigned long irqflags;
3600a266c7d5SChris Wilson 	int irq_received;
3601a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
360221ad8330SVille Syrjälä 	u32 flip_mask =
360321ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
360421ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3605a266c7d5SChris Wilson 
3606a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3607a266c7d5SChris Wilson 
3608a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3609a266c7d5SChris Wilson 
3610a266c7d5SChris Wilson 	for (;;) {
36112c8ba29fSChris Wilson 		bool blc_event = false;
36122c8ba29fSChris Wilson 
361321ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3614a266c7d5SChris Wilson 
3615a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3616a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3617a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3618a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3619a266c7d5SChris Wilson 		 */
3620a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3621a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3622a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3623a266c7d5SChris Wilson 
3624a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3625a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3626a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3627a266c7d5SChris Wilson 
3628a266c7d5SChris Wilson 			/*
3629a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3630a266c7d5SChris Wilson 			 */
3631a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3632a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3633a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3634a266c7d5SChris Wilson 							 pipe_name(pipe));
3635a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3636a266c7d5SChris Wilson 				irq_received = 1;
3637a266c7d5SChris Wilson 			}
3638a266c7d5SChris Wilson 		}
3639a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3640a266c7d5SChris Wilson 
3641a266c7d5SChris Wilson 		if (!irq_received)
3642a266c7d5SChris Wilson 			break;
3643a266c7d5SChris Wilson 
3644a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3645a266c7d5SChris Wilson 
3646a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3647adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3648a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3649b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3650b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
36514f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3652a266c7d5SChris Wilson 
3653a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3654a266c7d5SChris Wilson 				  hotplug_status);
365591d131d2SDaniel Vetter 
365610a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
365710a504deSDaniel Vetter 					      IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
365891d131d2SDaniel Vetter 
3659*4aeebd74SDaniel Vetter 			if (IS_G4X(dev) &&
3660*4aeebd74SDaniel Vetter 			    (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X))
3661*4aeebd74SDaniel Vetter 				dp_aux_irq_handler(dev);
3662*4aeebd74SDaniel Vetter 
3663a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3664a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3665a266c7d5SChris Wilson 		}
3666a266c7d5SChris Wilson 
366721ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3668a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3669a266c7d5SChris Wilson 
3670a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3671a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3672a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3673a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3674a266c7d5SChris Wilson 
3675a266c7d5SChris Wilson 		for_each_pipe(pipe) {
36762c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
367790a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
367890a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3679a266c7d5SChris Wilson 
3680a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3681a266c7d5SChris Wilson 				blc_event = true;
36824356d586SDaniel Vetter 
36834356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3684277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
3685a266c7d5SChris Wilson 		}
3686a266c7d5SChris Wilson 
3687a266c7d5SChris Wilson 
3688a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3689a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3690a266c7d5SChris Wilson 
3691515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3692515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3693515ac2bbSDaniel Vetter 
3694a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3695a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3696a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3697a266c7d5SChris Wilson 		 * we would never get another interrupt.
3698a266c7d5SChris Wilson 		 *
3699a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3700a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3701a266c7d5SChris Wilson 		 * another one.
3702a266c7d5SChris Wilson 		 *
3703a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3704a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3705a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3706a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3707a266c7d5SChris Wilson 		 * stray interrupts.
3708a266c7d5SChris Wilson 		 */
3709a266c7d5SChris Wilson 		iir = new_iir;
3710a266c7d5SChris Wilson 	}
3711a266c7d5SChris Wilson 
3712d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
37132c8ba29fSChris Wilson 
3714a266c7d5SChris Wilson 	return ret;
3715a266c7d5SChris Wilson }
3716a266c7d5SChris Wilson 
3717a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3718a266c7d5SChris Wilson {
3719a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3720a266c7d5SChris Wilson 	int pipe;
3721a266c7d5SChris Wilson 
3722a266c7d5SChris Wilson 	if (!dev_priv)
3723a266c7d5SChris Wilson 		return;
3724a266c7d5SChris Wilson 
3725ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3726ac4c16c5SEgbert Eich 
3727a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3728a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3729a266c7d5SChris Wilson 
3730a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3731a266c7d5SChris Wilson 	for_each_pipe(pipe)
3732a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3733a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3734a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3735a266c7d5SChris Wilson 
3736a266c7d5SChris Wilson 	for_each_pipe(pipe)
3737a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3738a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3739a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3740a266c7d5SChris Wilson }
3741a266c7d5SChris Wilson 
3742ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3743ac4c16c5SEgbert Eich {
3744ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3745ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3746ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3747ac4c16c5SEgbert Eich 	unsigned long irqflags;
3748ac4c16c5SEgbert Eich 	int i;
3749ac4c16c5SEgbert Eich 
3750ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3751ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3752ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3753ac4c16c5SEgbert Eich 
3754ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3755ac4c16c5SEgbert Eich 			continue;
3756ac4c16c5SEgbert Eich 
3757ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3758ac4c16c5SEgbert Eich 
3759ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3760ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3761ac4c16c5SEgbert Eich 
3762ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3763ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3764ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3765ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3766ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3767ac4c16c5SEgbert Eich 				if (!connector->polled)
3768ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3769ac4c16c5SEgbert Eich 			}
3770ac4c16c5SEgbert Eich 		}
3771ac4c16c5SEgbert Eich 	}
3772ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3773ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3774ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3775ac4c16c5SEgbert Eich }
3776ac4c16c5SEgbert Eich 
3777f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3778f71d4af4SJesse Barnes {
37798b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
37808b2e326dSChris Wilson 
37818b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
378299584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3783c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3784a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
37858b2e326dSChris Wilson 
378699584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
378799584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
378861bac78eSDaniel Vetter 		    (unsigned long) dev);
3789ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3790ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
379161bac78eSDaniel Vetter 
379297a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
37939ee32feaSDaniel Vetter 
37944cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
37954cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
37964cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
37974cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3798f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3799f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3800391f75e2SVille Syrjälä 	} else {
3801391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
3802391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
3803f71d4af4SJesse Barnes 	}
3804f71d4af4SJesse Barnes 
3805c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
3806f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3807f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3808c2baf4b7SVille Syrjälä 	}
3809f71d4af4SJesse Barnes 
38107e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
38117e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
38127e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
38137e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
38147e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
38157e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
38167e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3817fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3818abd58f01SBen Widawsky 	} else if (IS_GEN8(dev)) {
3819abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
3820abd58f01SBen Widawsky 		dev->driver->irq_preinstall = gen8_irq_preinstall;
3821abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
3822abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
3823abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
3824abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
3825abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3826f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3827f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3828f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3829f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3830f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3831f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3832f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
383382a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3834f71d4af4SJesse Barnes 	} else {
3835c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3836c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3837c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3838c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3839c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3840a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3841a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3842a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3843a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3844a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
384520afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3846c2798b19SChris Wilson 		} else {
3847a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3848a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3849a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3850a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3851bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3852c2798b19SChris Wilson 		}
3853f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3854f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3855f71d4af4SJesse Barnes 	}
3856f71d4af4SJesse Barnes }
385720afbda2SDaniel Vetter 
385820afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
385920afbda2SDaniel Vetter {
386020afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3861821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3862821450c6SEgbert Eich 	struct drm_connector *connector;
3863b5ea2d56SDaniel Vetter 	unsigned long irqflags;
3864821450c6SEgbert Eich 	int i;
386520afbda2SDaniel Vetter 
3866821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3867821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3868821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3869821450c6SEgbert Eich 	}
3870821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3871821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3872821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3873821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3874821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3875821450c6SEgbert Eich 	}
3876b5ea2d56SDaniel Vetter 
3877b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3878b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
3879b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
388020afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
388120afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
3882b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
388320afbda2SDaniel Vetter }
3884c67a470bSPaulo Zanoni 
3885c67a470bSPaulo Zanoni /* Disable interrupts so we can allow Package C8+. */
3886c67a470bSPaulo Zanoni void hsw_pc8_disable_interrupts(struct drm_device *dev)
3887c67a470bSPaulo Zanoni {
3888c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3889c67a470bSPaulo Zanoni 	unsigned long irqflags;
3890c67a470bSPaulo Zanoni 
3891c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3892c67a470bSPaulo Zanoni 
3893c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.deimr = I915_READ(DEIMR);
3894c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.sdeimr = I915_READ(SDEIMR);
3895c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtimr = I915_READ(GTIMR);
3896c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtier = I915_READ(GTIER);
3897c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
3898c67a470bSPaulo Zanoni 
3899c67a470bSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, ~DE_PCH_EVENT_IVB);
3900c67a470bSPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, ~SDE_HOTPLUG_MASK_CPT);
3901c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
3902c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
3903c67a470bSPaulo Zanoni 
3904c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = true;
3905c67a470bSPaulo Zanoni 
3906c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3907c67a470bSPaulo Zanoni }
3908c67a470bSPaulo Zanoni 
3909c67a470bSPaulo Zanoni /* Restore interrupts so we can recover from Package C8+. */
3910c67a470bSPaulo Zanoni void hsw_pc8_restore_interrupts(struct drm_device *dev)
3911c67a470bSPaulo Zanoni {
3912c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3913c67a470bSPaulo Zanoni 	unsigned long irqflags;
3914c67a470bSPaulo Zanoni 	uint32_t val, expected;
3915c67a470bSPaulo Zanoni 
3916c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3917c67a470bSPaulo Zanoni 
3918c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
3919c67a470bSPaulo Zanoni 	expected = ~DE_PCH_EVENT_IVB;
3920c67a470bSPaulo Zanoni 	WARN(val != expected, "DEIMR is 0x%08x, not 0x%08x\n", val, expected);
3921c67a470bSPaulo Zanoni 
3922c67a470bSPaulo Zanoni 	val = I915_READ(SDEIMR) & ~SDE_HOTPLUG_MASK_CPT;
3923c67a470bSPaulo Zanoni 	expected = ~SDE_HOTPLUG_MASK_CPT;
3924c67a470bSPaulo Zanoni 	WARN(val != expected, "SDEIMR non-HPD bits are 0x%08x, not 0x%08x\n",
3925c67a470bSPaulo Zanoni 	     val, expected);
3926c67a470bSPaulo Zanoni 
3927c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
3928c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3929c67a470bSPaulo Zanoni 	WARN(val != expected, "GTIMR is 0x%08x, not 0x%08x\n", val, expected);
3930c67a470bSPaulo Zanoni 
3931c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
3932c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3933c67a470bSPaulo Zanoni 	WARN(val != expected, "GEN6_PMIMR is 0x%08x, not 0x%08x\n", val,
3934c67a470bSPaulo Zanoni 	     expected);
3935c67a470bSPaulo Zanoni 
3936c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = false;
3937c67a470bSPaulo Zanoni 
3938c67a470bSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pc8.regsave.deimr);
3939c67a470bSPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv,
3940c67a470bSPaulo Zanoni 				     ~dev_priv->pc8.regsave.sdeimr &
3941c67a470bSPaulo Zanoni 				     ~SDE_HOTPLUG_MASK_CPT);
3942c67a470bSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pc8.regsave.gtimr);
3943c67a470bSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pc8.regsave.gen6_pmimr);
3944c67a470bSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pc8.regsave.gtier);
3945c67a470bSPaulo Zanoni 
3946c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3947c67a470bSPaulo Zanoni }
3948