xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 1272e7b854e768ede5279de57b78a54cb39f5da5)
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 
273fee884edSDaniel Vetter /**
274fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
275fee884edSDaniel Vetter  * @dev_priv: driver private
276fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
277fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
278fee884edSDaniel Vetter  */
279fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
280fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
281fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
282fee884edSDaniel Vetter {
283fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
284fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
285fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
286fee884edSDaniel Vetter 
287fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
288fee884edSDaniel Vetter 
289c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled &&
290c67a470bSPaulo Zanoni 	    (interrupt_mask & SDE_HOTPLUG_MASK_CPT)) {
291c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
292c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr &= ~interrupt_mask;
293c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr |= (~enabled_irq_mask &
294c67a470bSPaulo Zanoni 						 interrupt_mask);
295c67a470bSPaulo Zanoni 		return;
296c67a470bSPaulo Zanoni 	}
297c67a470bSPaulo Zanoni 
298fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
299fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
300fee884edSDaniel Vetter }
301fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
302fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
303fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
304fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
305fee884edSDaniel Vetter 
306de28075dSDaniel Vetter static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
307de28075dSDaniel Vetter 					    enum transcoder pch_transcoder,
3088664281bSPaulo Zanoni 					    bool enable)
3098664281bSPaulo Zanoni {
3108664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
311de28075dSDaniel Vetter 	uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
312de28075dSDaniel Vetter 		       SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
3138664281bSPaulo Zanoni 
3148664281bSPaulo Zanoni 	if (enable)
315fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
3168664281bSPaulo Zanoni 	else
317fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
3188664281bSPaulo Zanoni }
3198664281bSPaulo Zanoni 
3208664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
3218664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
3228664281bSPaulo Zanoni 					    bool enable)
3238664281bSPaulo Zanoni {
3248664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3258664281bSPaulo Zanoni 
3268664281bSPaulo Zanoni 	if (enable) {
3271dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
3281dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
3291dd246fbSDaniel Vetter 
3308664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
3318664281bSPaulo Zanoni 			return;
3328664281bSPaulo Zanoni 
333fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3348664281bSPaulo Zanoni 	} else {
3351dd246fbSDaniel Vetter 		uint32_t tmp = I915_READ(SERR_INT);
3361dd246fbSDaniel Vetter 		bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
3371dd246fbSDaniel Vetter 
3381dd246fbSDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
339fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3401dd246fbSDaniel Vetter 
3411dd246fbSDaniel Vetter 		if (!was_enabled &&
3421dd246fbSDaniel Vetter 		    (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
3431dd246fbSDaniel Vetter 			DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
3441dd246fbSDaniel Vetter 				      transcoder_name(pch_transcoder));
3451dd246fbSDaniel Vetter 		}
3468664281bSPaulo Zanoni 	}
3478664281bSPaulo Zanoni }
3488664281bSPaulo Zanoni 
3498664281bSPaulo Zanoni /**
3508664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
3518664281bSPaulo Zanoni  * @dev: drm device
3528664281bSPaulo Zanoni  * @pipe: pipe
3538664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
3548664281bSPaulo Zanoni  *
3558664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
3568664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
3578664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
3588664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
3598664281bSPaulo Zanoni  * bit for all the pipes.
3608664281bSPaulo Zanoni  *
3618664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
3628664281bSPaulo Zanoni  */
3638664281bSPaulo Zanoni bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
3648664281bSPaulo Zanoni 					   enum pipe pipe, bool enable)
3658664281bSPaulo Zanoni {
3668664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3678664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
3688664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3698664281bSPaulo Zanoni 	unsigned long flags;
3708664281bSPaulo Zanoni 	bool ret;
3718664281bSPaulo Zanoni 
3728664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
3738664281bSPaulo Zanoni 
3748664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
3758664281bSPaulo Zanoni 
3768664281bSPaulo Zanoni 	if (enable == ret)
3778664281bSPaulo Zanoni 		goto done;
3788664281bSPaulo Zanoni 
3798664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
3808664281bSPaulo Zanoni 
3818664281bSPaulo Zanoni 	if (IS_GEN5(dev) || IS_GEN6(dev))
3828664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
3838664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
3847336df65SDaniel Vetter 		ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
3858664281bSPaulo Zanoni 
3868664281bSPaulo Zanoni done:
3878664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
3888664281bSPaulo Zanoni 	return ret;
3898664281bSPaulo Zanoni }
3908664281bSPaulo Zanoni 
3918664281bSPaulo Zanoni /**
3928664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
3938664281bSPaulo Zanoni  * @dev: drm device
3948664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
3958664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
3968664281bSPaulo Zanoni  *
3978664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
3988664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
3998664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
4008664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
4018664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
4028664281bSPaulo Zanoni  *
4038664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
4048664281bSPaulo Zanoni  */
4058664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
4068664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
4078664281bSPaulo Zanoni 					   bool enable)
4088664281bSPaulo Zanoni {
4098664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
410de28075dSDaniel Vetter 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
411de28075dSDaniel Vetter 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4128664281bSPaulo Zanoni 	unsigned long flags;
4138664281bSPaulo Zanoni 	bool ret;
4148664281bSPaulo Zanoni 
415de28075dSDaniel Vetter 	/*
416de28075dSDaniel Vetter 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
417de28075dSDaniel Vetter 	 * has only one pch transcoder A that all pipes can use. To avoid racy
418de28075dSDaniel Vetter 	 * pch transcoder -> pipe lookups from interrupt code simply store the
419de28075dSDaniel Vetter 	 * underrun statistics in crtc A. Since we never expose this anywhere
420de28075dSDaniel Vetter 	 * nor use it outside of the fifo underrun code here using the "wrong"
421de28075dSDaniel Vetter 	 * crtc on LPT won't cause issues.
422de28075dSDaniel Vetter 	 */
4238664281bSPaulo Zanoni 
4248664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
4258664281bSPaulo Zanoni 
4268664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
4278664281bSPaulo Zanoni 
4288664281bSPaulo Zanoni 	if (enable == ret)
4298664281bSPaulo Zanoni 		goto done;
4308664281bSPaulo Zanoni 
4318664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
4328664281bSPaulo Zanoni 
4338664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
434de28075dSDaniel Vetter 		ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4358664281bSPaulo Zanoni 	else
4368664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4378664281bSPaulo Zanoni 
4388664281bSPaulo Zanoni done:
4398664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
4408664281bSPaulo Zanoni 	return ret;
4418664281bSPaulo Zanoni }
4428664281bSPaulo Zanoni 
4438664281bSPaulo Zanoni 
4447c463586SKeith Packard void
4453b6c42e8SDaniel Vetter i915_enable_pipestat(drm_i915_private_t *dev_priv, enum pipe pipe, u32 mask)
4467c463586SKeith Packard {
4479db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
44846c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
4497c463586SKeith Packard 
450b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
451b79480baSDaniel Vetter 
45246c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
45346c06a30SVille Syrjälä 		return;
45446c06a30SVille Syrjälä 
4557c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
45646c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
45746c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4583143a2bfSChris Wilson 	POSTING_READ(reg);
4597c463586SKeith Packard }
4607c463586SKeith Packard 
4617c463586SKeith Packard void
4623b6c42e8SDaniel Vetter i915_disable_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) == 0)
47046c06a30SVille Syrjälä 		return;
47146c06a30SVille Syrjälä 
47246c06a30SVille Syrjälä 	pipestat &= ~mask;
47346c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4743143a2bfSChris Wilson 	POSTING_READ(reg);
4757c463586SKeith Packard }
4767c463586SKeith Packard 
477c0e09200SDave Airlie /**
478f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
47901c66889SZhao Yakui  */
480f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
48101c66889SZhao Yakui {
4821ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
4831ec14ad3SChris Wilson 	unsigned long irqflags;
4841ec14ad3SChris Wilson 
485f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
486f49e38ddSJani Nikula 		return;
487f49e38ddSJani Nikula 
4881ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
48901c66889SZhao Yakui 
4903b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_ENABLE);
491a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
4923b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
4933b6c42e8SDaniel Vetter 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
4941ec14ad3SChris Wilson 
4951ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
49601c66889SZhao Yakui }
49701c66889SZhao Yakui 
49801c66889SZhao Yakui /**
4990a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
5000a3e67a4SJesse Barnes  * @dev: DRM device
5010a3e67a4SJesse Barnes  * @pipe: pipe to check
5020a3e67a4SJesse Barnes  *
5030a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
5040a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
5050a3e67a4SJesse Barnes  * before reading such registers if unsure.
5060a3e67a4SJesse Barnes  */
5070a3e67a4SJesse Barnes static int
5080a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
5090a3e67a4SJesse Barnes {
5100a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
511702e7a56SPaulo Zanoni 
512a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
513a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
514a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
515a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51671f8ba6bSPaulo Zanoni 
517a01025afSDaniel Vetter 		return intel_crtc->active;
518a01025afSDaniel Vetter 	} else {
519a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
520a01025afSDaniel Vetter 	}
5210a3e67a4SJesse Barnes }
5220a3e67a4SJesse Barnes 
5234cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
5244cdb83ecSVille Syrjälä {
5254cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
5264cdb83ecSVille Syrjälä 	return 0;
5274cdb83ecSVille Syrjälä }
5284cdb83ecSVille Syrjälä 
52942f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
53042f52ef8SKeith Packard  * we use as a pipe index
53142f52ef8SKeith Packard  */
532f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5330a3e67a4SJesse Barnes {
5340a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5350a3e67a4SJesse Barnes 	unsigned long high_frame;
5360a3e67a4SJesse Barnes 	unsigned long low_frame;
537391f75e2SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start;
5380a3e67a4SJesse Barnes 
5390a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
54044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5419db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
5420a3e67a4SJesse Barnes 		return 0;
5430a3e67a4SJesse Barnes 	}
5440a3e67a4SJesse Barnes 
545391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
546391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
547391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
548391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
549391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
550391f75e2SVille Syrjälä 
551391f75e2SVille Syrjälä 		vbl_start = mode->crtc_vblank_start * mode->crtc_htotal;
552391f75e2SVille Syrjälä 	} else {
553391f75e2SVille Syrjälä 		enum transcoder cpu_transcoder =
554391f75e2SVille Syrjälä 			intel_pipe_to_cpu_transcoder(dev_priv, pipe);
555391f75e2SVille Syrjälä 		u32 htotal;
556391f75e2SVille Syrjälä 
557391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
558391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
559391f75e2SVille Syrjälä 
560391f75e2SVille Syrjälä 		vbl_start *= htotal;
561391f75e2SVille Syrjälä 	}
562391f75e2SVille Syrjälä 
5639db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5649db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5655eddb70bSChris Wilson 
5660a3e67a4SJesse Barnes 	/*
5670a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5680a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
5690a3e67a4SJesse Barnes 	 * register.
5700a3e67a4SJesse Barnes 	 */
5710a3e67a4SJesse Barnes 	do {
5725eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
573391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
5745eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
5750a3e67a4SJesse Barnes 	} while (high1 != high2);
5760a3e67a4SJesse Barnes 
5775eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
578391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
5795eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
580391f75e2SVille Syrjälä 
581391f75e2SVille Syrjälä 	/*
582391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
583391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
584391f75e2SVille Syrjälä 	 * counter against vblank start.
585391f75e2SVille Syrjälä 	 */
586edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
5870a3e67a4SJesse Barnes }
5880a3e67a4SJesse Barnes 
589f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
5909880b7a5SJesse Barnes {
5919880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5929db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
5939880b7a5SJesse Barnes 
5949880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
59544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5969db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
5979880b7a5SJesse Barnes 		return 0;
5989880b7a5SJesse Barnes 	}
5999880b7a5SJesse Barnes 
6009880b7a5SJesse Barnes 	return I915_READ(reg);
6019880b7a5SJesse Barnes }
6029880b7a5SJesse Barnes 
6037c06b08aSVille Syrjälä static bool intel_pipe_in_vblank(struct drm_device *dev, enum pipe pipe)
60454ddcbd2SVille Syrjälä {
60554ddcbd2SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
60654ddcbd2SVille Syrjälä 	uint32_t status;
60754ddcbd2SVille Syrjälä 
60854ddcbd2SVille Syrjälä 	if (IS_VALLEYVIEW(dev)) {
60954ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
61054ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
61154ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
61254ddcbd2SVille Syrjälä 
61354ddcbd2SVille Syrjälä 		return I915_READ(VLV_ISR) & status;
6147c06b08aSVille Syrjälä 	} else if (IS_GEN2(dev)) {
6157c06b08aSVille Syrjälä 		status = pipe == PIPE_A ?
6167c06b08aSVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
6177c06b08aSVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
6187c06b08aSVille Syrjälä 
6197c06b08aSVille Syrjälä 		return I915_READ16(ISR) & status;
6207c06b08aSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen < 5) {
62154ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
62254ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
62354ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
62454ddcbd2SVille Syrjälä 
62554ddcbd2SVille Syrjälä 		return I915_READ(ISR) & status;
62654ddcbd2SVille Syrjälä 	} else if (INTEL_INFO(dev)->gen < 7) {
62754ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
62854ddcbd2SVille Syrjälä 			DE_PIPEA_VBLANK :
62954ddcbd2SVille Syrjälä 			DE_PIPEB_VBLANK;
63054ddcbd2SVille Syrjälä 
63154ddcbd2SVille Syrjälä 		return I915_READ(DEISR) & status;
63254ddcbd2SVille Syrjälä 	} else {
63354ddcbd2SVille Syrjälä 		switch (pipe) {
63454ddcbd2SVille Syrjälä 		default:
63554ddcbd2SVille Syrjälä 		case PIPE_A:
63654ddcbd2SVille Syrjälä 			status = DE_PIPEA_VBLANK_IVB;
63754ddcbd2SVille Syrjälä 			break;
63854ddcbd2SVille Syrjälä 		case PIPE_B:
63954ddcbd2SVille Syrjälä 			status = DE_PIPEB_VBLANK_IVB;
64054ddcbd2SVille Syrjälä 			break;
64154ddcbd2SVille Syrjälä 		case PIPE_C:
64254ddcbd2SVille Syrjälä 			status = DE_PIPEC_VBLANK_IVB;
64354ddcbd2SVille Syrjälä 			break;
64454ddcbd2SVille Syrjälä 		}
64554ddcbd2SVille Syrjälä 
64654ddcbd2SVille Syrjälä 		return I915_READ(DEISR) & status;
64754ddcbd2SVille Syrjälä 	}
64854ddcbd2SVille Syrjälä }
64954ddcbd2SVille Syrjälä 
650f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
6510af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
6520af7e4dfSMario Kleiner {
653c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
654c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
655c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
656c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
6573aa18df8SVille Syrjälä 	int position;
6580af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
6590af7e4dfSMario Kleiner 	bool in_vbl = true;
6600af7e4dfSMario Kleiner 	int ret = 0;
6610af7e4dfSMario Kleiner 
662c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
6630af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
6649db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6650af7e4dfSMario Kleiner 		return 0;
6660af7e4dfSMario Kleiner 	}
6670af7e4dfSMario Kleiner 
668c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
669c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
670c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
671c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
6720af7e4dfSMario Kleiner 
673c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
674c2baf4b7SVille Syrjälä 
6757c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
6760af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
6770af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
6780af7e4dfSMario Kleiner 		 */
6797c06b08aSVille Syrjälä 		if (IS_GEN2(dev))
6807c06b08aSVille Syrjälä 			position = I915_READ(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
6817c06b08aSVille Syrjälä 		else
6827c06b08aSVille Syrjälä 			position = I915_READ(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
68354ddcbd2SVille Syrjälä 
68454ddcbd2SVille Syrjälä 		/*
68554ddcbd2SVille Syrjälä 		 * The scanline counter increments at the leading edge
68654ddcbd2SVille Syrjälä 		 * of hsync, ie. it completely misses the active portion
68754ddcbd2SVille Syrjälä 		 * of the line. Fix up the counter at both edges of vblank
68854ddcbd2SVille Syrjälä 		 * to get a more accurate picture whether we're in vblank
68954ddcbd2SVille Syrjälä 		 * or not.
69054ddcbd2SVille Syrjälä 		 */
6917c06b08aSVille Syrjälä 		in_vbl = intel_pipe_in_vblank(dev, pipe);
69254ddcbd2SVille Syrjälä 		if ((in_vbl && position == vbl_start - 1) ||
69354ddcbd2SVille Syrjälä 		    (!in_vbl && position == vbl_end - 1))
69454ddcbd2SVille Syrjälä 			position = (position + 1) % vtotal;
6950af7e4dfSMario Kleiner 	} else {
6960af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
6970af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
6980af7e4dfSMario Kleiner 		 * scanout position.
6990af7e4dfSMario Kleiner 		 */
7000af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
7010af7e4dfSMario Kleiner 
7023aa18df8SVille Syrjälä 		/* convert to pixel counts */
7033aa18df8SVille Syrjälä 		vbl_start *= htotal;
7043aa18df8SVille Syrjälä 		vbl_end *= htotal;
7053aa18df8SVille Syrjälä 		vtotal *= htotal;
7063aa18df8SVille Syrjälä 	}
7073aa18df8SVille Syrjälä 
7083aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
7093aa18df8SVille Syrjälä 
7103aa18df8SVille Syrjälä 	/*
7113aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
7123aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
7133aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
7143aa18df8SVille Syrjälä 	 * up since vbl_end.
7153aa18df8SVille Syrjälä 	 */
7163aa18df8SVille Syrjälä 	if (position >= vbl_start)
7173aa18df8SVille Syrjälä 		position -= vbl_end;
7183aa18df8SVille Syrjälä 	else
7193aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
7203aa18df8SVille Syrjälä 
7217c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7223aa18df8SVille Syrjälä 		*vpos = position;
7233aa18df8SVille Syrjälä 		*hpos = 0;
7243aa18df8SVille Syrjälä 	} else {
7250af7e4dfSMario Kleiner 		*vpos = position / htotal;
7260af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
7270af7e4dfSMario Kleiner 	}
7280af7e4dfSMario Kleiner 
7290af7e4dfSMario Kleiner 	/* In vblank? */
7300af7e4dfSMario Kleiner 	if (in_vbl)
7310af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
7320af7e4dfSMario Kleiner 
7330af7e4dfSMario Kleiner 	return ret;
7340af7e4dfSMario Kleiner }
7350af7e4dfSMario Kleiner 
736f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
7370af7e4dfSMario Kleiner 			      int *max_error,
7380af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
7390af7e4dfSMario Kleiner 			      unsigned flags)
7400af7e4dfSMario Kleiner {
7414041b853SChris Wilson 	struct drm_crtc *crtc;
7420af7e4dfSMario Kleiner 
7437eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
7444041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7450af7e4dfSMario Kleiner 		return -EINVAL;
7460af7e4dfSMario Kleiner 	}
7470af7e4dfSMario Kleiner 
7480af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
7494041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
7504041b853SChris Wilson 	if (crtc == NULL) {
7514041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7524041b853SChris Wilson 		return -EINVAL;
7534041b853SChris Wilson 	}
7544041b853SChris Wilson 
7554041b853SChris Wilson 	if (!crtc->enabled) {
7564041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
7574041b853SChris Wilson 		return -EBUSY;
7584041b853SChris Wilson 	}
7590af7e4dfSMario Kleiner 
7600af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
7614041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
7624041b853SChris Wilson 						     vblank_time, flags,
7634041b853SChris Wilson 						     crtc);
7640af7e4dfSMario Kleiner }
7650af7e4dfSMario Kleiner 
76667c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
76767c347ffSJani Nikula 				struct drm_connector *connector)
768321a1b30SEgbert Eich {
769321a1b30SEgbert Eich 	enum drm_connector_status old_status;
770321a1b30SEgbert Eich 
771321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
772321a1b30SEgbert Eich 	old_status = connector->status;
773321a1b30SEgbert Eich 
774321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
77567c347ffSJani Nikula 	if (old_status == connector->status)
77667c347ffSJani Nikula 		return false;
77767c347ffSJani Nikula 
77867c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
779321a1b30SEgbert Eich 		      connector->base.id,
780321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
78167c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
78267c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
78367c347ffSJani Nikula 
78467c347ffSJani Nikula 	return true;
785321a1b30SEgbert Eich }
786321a1b30SEgbert Eich 
7875ca58282SJesse Barnes /*
7885ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
7895ca58282SJesse Barnes  */
790ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
791ac4c16c5SEgbert Eich 
7925ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
7935ca58282SJesse Barnes {
7945ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
7955ca58282SJesse Barnes 						    hotplug_work);
7965ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
797c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
798cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
799cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
800cd569aedSEgbert Eich 	struct drm_connector *connector;
801cd569aedSEgbert Eich 	unsigned long irqflags;
802cd569aedSEgbert Eich 	bool hpd_disabled = false;
803321a1b30SEgbert Eich 	bool changed = false;
804142e2398SEgbert Eich 	u32 hpd_event_bits;
8055ca58282SJesse Barnes 
80652d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
80752d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
80852d7ecedSDaniel Vetter 		return;
80952d7ecedSDaniel Vetter 
810a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
811e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
812e67189abSJesse Barnes 
813cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
814142e2398SEgbert Eich 
815142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
816142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
817cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
818cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
819cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
820cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
821cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
822cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
823cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
824cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
825cd569aedSEgbert Eich 				drm_get_connector_name(connector));
826cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
827cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
828cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
829cd569aedSEgbert Eich 			hpd_disabled = true;
830cd569aedSEgbert Eich 		}
831142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
832142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
833142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
834142e2398SEgbert Eich 		}
835cd569aedSEgbert Eich 	}
836cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
837cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
838cd569aedSEgbert Eich 	  * some connectors */
839ac4c16c5SEgbert Eich 	if (hpd_disabled) {
840cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
841ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
842ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
843ac4c16c5SEgbert Eich 	}
844cd569aedSEgbert Eich 
845cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
846cd569aedSEgbert Eich 
847321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
848321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
849321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
850321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
851cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
852cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
853321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
854321a1b30SEgbert Eich 				changed = true;
855321a1b30SEgbert Eich 		}
856321a1b30SEgbert Eich 	}
85740ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
85840ee3381SKeith Packard 
859321a1b30SEgbert Eich 	if (changed)
860321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
8615ca58282SJesse Barnes }
8625ca58282SJesse Barnes 
863d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
864f97108d1SJesse Barnes {
865f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
866b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
8679270388eSDaniel Vetter 	u8 new_delay;
8689270388eSDaniel Vetter 
869d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
870f97108d1SJesse Barnes 
87173edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
87273edd18fSDaniel Vetter 
87320e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
8749270388eSDaniel Vetter 
8757648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
876b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
877b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
878f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
879f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
880f97108d1SJesse Barnes 
881f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
882b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
88320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
88420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
88520e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
88620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
887b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
88820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
88920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
89020e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
89120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
892f97108d1SJesse Barnes 	}
893f97108d1SJesse Barnes 
8947648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
89520e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
896f97108d1SJesse Barnes 
897d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
8989270388eSDaniel Vetter 
899f97108d1SJesse Barnes 	return;
900f97108d1SJesse Barnes }
901f97108d1SJesse Barnes 
902549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
903549f7365SChris Wilson 			struct intel_ring_buffer *ring)
904549f7365SChris Wilson {
905475553deSChris Wilson 	if (ring->obj == NULL)
906475553deSChris Wilson 		return;
907475553deSChris Wilson 
908814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
9099862e600SChris Wilson 
910549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
91110cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
912549f7365SChris Wilson }
913549f7365SChris Wilson 
9144912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
9153b8d8d91SJesse Barnes {
9164912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
917c6a828d3SDaniel Vetter 						    rps.work);
918edbfdb45SPaulo Zanoni 	u32 pm_iir;
919dd75fdc8SChris Wilson 	int new_delay, adj;
9203b8d8d91SJesse Barnes 
92159cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
922c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
923c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
9244848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
925edbfdb45SPaulo Zanoni 	snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
92659cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
9274912d041SBen Widawsky 
92860611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
92960611c13SPaulo Zanoni 	WARN_ON(pm_iir & ~GEN6_PM_RPS_EVENTS);
93060611c13SPaulo Zanoni 
9314848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
9323b8d8d91SJesse Barnes 		return;
9333b8d8d91SJesse Barnes 
9344fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
9357b9e0ae6SChris Wilson 
936dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
9377425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
938dd75fdc8SChris Wilson 		if (adj > 0)
939dd75fdc8SChris Wilson 			adj *= 2;
940dd75fdc8SChris Wilson 		else
941dd75fdc8SChris Wilson 			adj = 1;
942dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
9437425034aSVille Syrjälä 
9447425034aSVille Syrjälä 		/*
9457425034aSVille Syrjälä 		 * For better performance, jump directly
9467425034aSVille Syrjälä 		 * to RPe if we're below it.
9477425034aSVille Syrjälä 		 */
948dd75fdc8SChris Wilson 		if (new_delay < dev_priv->rps.rpe_delay)
9497425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
950dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
951dd75fdc8SChris Wilson 		if (dev_priv->rps.cur_delay > dev_priv->rps.rpe_delay)
952dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.rpe_delay;
953dd75fdc8SChris Wilson 		else
954dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.min_delay;
955dd75fdc8SChris Wilson 		adj = 0;
956dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
957dd75fdc8SChris Wilson 		if (adj < 0)
958dd75fdc8SChris Wilson 			adj *= 2;
959dd75fdc8SChris Wilson 		else
960dd75fdc8SChris Wilson 			adj = -1;
961dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
962dd75fdc8SChris Wilson 	} else { /* unknown event */
963dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay;
964dd75fdc8SChris Wilson 	}
9653b8d8d91SJesse Barnes 
96679249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
96779249636SBen Widawsky 	 * interrupt
96879249636SBen Widawsky 	 */
969*1272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
970*1272e7b8SVille Syrjälä 			    dev_priv->rps.min_delay, dev_priv->rps.max_delay);
971dd75fdc8SChris Wilson 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_delay;
972dd75fdc8SChris Wilson 
9730a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
9740a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
9750a073b84SJesse Barnes 	else
9764912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
9773b8d8d91SJesse Barnes 
9784fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
9793b8d8d91SJesse Barnes }
9803b8d8d91SJesse Barnes 
981e3689190SBen Widawsky 
982e3689190SBen Widawsky /**
983e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
984e3689190SBen Widawsky  * occurred.
985e3689190SBen Widawsky  * @work: workqueue struct
986e3689190SBen Widawsky  *
987e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
988e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
989e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
990e3689190SBen Widawsky  */
991e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
992e3689190SBen Widawsky {
993e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
994a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
995e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
99635a85ac6SBen Widawsky 	char *parity_event[6];
997e3689190SBen Widawsky 	uint32_t misccpctl;
998e3689190SBen Widawsky 	unsigned long flags;
99935a85ac6SBen Widawsky 	uint8_t slice = 0;
1000e3689190SBen Widawsky 
1001e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1002e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1003e3689190SBen Widawsky 	 * any time we access those registers.
1004e3689190SBen Widawsky 	 */
1005e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1006e3689190SBen Widawsky 
100735a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
100835a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
100935a85ac6SBen Widawsky 		goto out;
101035a85ac6SBen Widawsky 
1011e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1012e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1013e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1014e3689190SBen Widawsky 
101535a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
101635a85ac6SBen Widawsky 		u32 reg;
101735a85ac6SBen Widawsky 
101835a85ac6SBen Widawsky 		slice--;
101935a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
102035a85ac6SBen Widawsky 			break;
102135a85ac6SBen Widawsky 
102235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
102335a85ac6SBen Widawsky 
102435a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
102535a85ac6SBen Widawsky 
102635a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1027e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1028e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1029e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1030e3689190SBen Widawsky 
103135a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
103235a85ac6SBen Widawsky 		POSTING_READ(reg);
1033e3689190SBen Widawsky 
1034cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1035e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1036e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1037e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
103835a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
103935a85ac6SBen Widawsky 		parity_event[5] = NULL;
1040e3689190SBen Widawsky 
1041e3689190SBen Widawsky 		kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
1042e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1043e3689190SBen Widawsky 
104435a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
104535a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1046e3689190SBen Widawsky 
104735a85ac6SBen Widawsky 		kfree(parity_event[4]);
1048e3689190SBen Widawsky 		kfree(parity_event[3]);
1049e3689190SBen Widawsky 		kfree(parity_event[2]);
1050e3689190SBen Widawsky 		kfree(parity_event[1]);
1051e3689190SBen Widawsky 	}
1052e3689190SBen Widawsky 
105335a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
105435a85ac6SBen Widawsky 
105535a85ac6SBen Widawsky out:
105635a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
105735a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
105835a85ac6SBen Widawsky 	ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
105935a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
106035a85ac6SBen Widawsky 
106135a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
106235a85ac6SBen Widawsky }
106335a85ac6SBen Widawsky 
106435a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1065e3689190SBen Widawsky {
1066e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1067e3689190SBen Widawsky 
1068040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1069e3689190SBen Widawsky 		return;
1070e3689190SBen Widawsky 
1071d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
107235a85ac6SBen Widawsky 	ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1073d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1074e3689190SBen Widawsky 
107535a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
107635a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
107735a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
107835a85ac6SBen Widawsky 
107935a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
108035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
108135a85ac6SBen Widawsky 
1082a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1083e3689190SBen Widawsky }
1084e3689190SBen Widawsky 
1085f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1086f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1087f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1088f1af8fc1SPaulo Zanoni {
1089f1af8fc1SPaulo Zanoni 	if (gt_iir &
1090f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1091f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1092f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1093f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1094f1af8fc1SPaulo Zanoni }
1095f1af8fc1SPaulo Zanoni 
1096e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1097e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1098e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1099e7b4c6b1SDaniel Vetter {
1100e7b4c6b1SDaniel Vetter 
1101cc609d5dSBen Widawsky 	if (gt_iir &
1102cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1103e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1104cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1105e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1106cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1107e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1108e7b4c6b1SDaniel Vetter 
1109cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1110cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1111cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
1112e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
1113e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
1114e7b4c6b1SDaniel Vetter 	}
1115e3689190SBen Widawsky 
111635a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
111735a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1118e7b4c6b1SDaniel Vetter }
1119e7b4c6b1SDaniel Vetter 
1120b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1121b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1122b543fb04SEgbert Eich 
112310a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1124b543fb04SEgbert Eich 					 u32 hotplug_trigger,
1125b543fb04SEgbert Eich 					 const u32 *hpd)
1126b543fb04SEgbert Eich {
1127b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
1128b543fb04SEgbert Eich 	int i;
112910a504deSDaniel Vetter 	bool storm_detected = false;
1130b543fb04SEgbert Eich 
113191d131d2SDaniel Vetter 	if (!hotplug_trigger)
113291d131d2SDaniel Vetter 		return;
113391d131d2SDaniel Vetter 
1134b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1135b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
1136821450c6SEgbert Eich 
1137b8f102e8SEgbert Eich 		WARN(((hpd[i] & hotplug_trigger) &&
1138b8f102e8SEgbert Eich 		      dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED),
1139b8f102e8SEgbert Eich 		     "Received HPD interrupt although disabled\n");
1140b8f102e8SEgbert Eich 
1141b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1142b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1143b543fb04SEgbert Eich 			continue;
1144b543fb04SEgbert Eich 
1145bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1146b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1147b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1148b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1149b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1150b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1151b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1152b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1153b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1154142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1155b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
115610a504deSDaniel Vetter 			storm_detected = true;
1157b543fb04SEgbert Eich 		} else {
1158b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1159b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1160b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1161b543fb04SEgbert Eich 		}
1162b543fb04SEgbert Eich 	}
1163b543fb04SEgbert Eich 
116410a504deSDaniel Vetter 	if (storm_detected)
116510a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1166b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
11675876fa0dSDaniel Vetter 
1168645416f5SDaniel Vetter 	/*
1169645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1170645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1171645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1172645416f5SDaniel Vetter 	 * deadlock.
1173645416f5SDaniel Vetter 	 */
1174645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1175b543fb04SEgbert Eich }
1176b543fb04SEgbert Eich 
1177515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1178515ac2bbSDaniel Vetter {
117928c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
118028c70f16SDaniel Vetter 
118128c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1182515ac2bbSDaniel Vetter }
1183515ac2bbSDaniel Vetter 
1184ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1185ce99c256SDaniel Vetter {
11869ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
11879ee32feaSDaniel Vetter 
11889ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1189ce99c256SDaniel Vetter }
1190ce99c256SDaniel Vetter 
11918bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1192277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1193eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1194eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
11958bc5e955SDaniel Vetter 					 uint32_t crc4)
11968bf1e9f1SShuang He {
11978bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
11988bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
11998bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1200ac2300d4SDamien Lespiau 	int head, tail;
1201b2c88f5bSDamien Lespiau 
1202d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1203d538bbdfSDamien Lespiau 
12040c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1205d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
12060c912c79SDamien Lespiau 		DRM_ERROR("spurious interrupt\n");
12070c912c79SDamien Lespiau 		return;
12080c912c79SDamien Lespiau 	}
12090c912c79SDamien Lespiau 
1210d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1211d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1212b2c88f5bSDamien Lespiau 
1213b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1214d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1215b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1216b2c88f5bSDamien Lespiau 		return;
1217b2c88f5bSDamien Lespiau 	}
1218b2c88f5bSDamien Lespiau 
1219b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
12208bf1e9f1SShuang He 
12218bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1222eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1223eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1224eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1225eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1226eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1227b2c88f5bSDamien Lespiau 
1228b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1229d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1230d538bbdfSDamien Lespiau 
1231d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
123207144428SDamien Lespiau 
123307144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
12348bf1e9f1SShuang He }
1235277de95eSDaniel Vetter #else
1236277de95eSDaniel Vetter static inline void
1237277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1238277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1239277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1240277de95eSDaniel Vetter 			     uint32_t crc4) {}
1241277de95eSDaniel Vetter #endif
1242eba94eb9SDaniel Vetter 
1243277de95eSDaniel Vetter 
1244277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
12455a69b89fSDaniel Vetter {
12465a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
12475a69b89fSDaniel Vetter 
1248277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
12495a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
12505a69b89fSDaniel Vetter 				     0, 0, 0, 0);
12515a69b89fSDaniel Vetter }
12525a69b89fSDaniel Vetter 
1253277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1254eba94eb9SDaniel Vetter {
1255eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1256eba94eb9SDaniel Vetter 
1257277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1258eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1259eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1260eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1261eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
12628bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1263eba94eb9SDaniel Vetter }
12645b3a856bSDaniel Vetter 
1265277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
12665b3a856bSDaniel Vetter {
12675b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
12680b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
12690b5c5ed0SDaniel Vetter 
12700b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
12710b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
12720b5c5ed0SDaniel Vetter 	else
12730b5c5ed0SDaniel Vetter 		res1 = 0;
12740b5c5ed0SDaniel Vetter 
12750b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
12760b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
12770b5c5ed0SDaniel Vetter 	else
12780b5c5ed0SDaniel Vetter 		res2 = 0;
12795b3a856bSDaniel Vetter 
1280277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
12810b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
12820b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
12830b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
12840b5c5ed0SDaniel Vetter 				     res1, res2);
12855b3a856bSDaniel Vetter }
12868bf1e9f1SShuang He 
12871403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
12881403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
12891403c0d4SPaulo Zanoni  * the work queue. */
12901403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1291baf02a1fSBen Widawsky {
129241a05a3aSDaniel Vetter 	if (pm_iir & GEN6_PM_RPS_EVENTS) {
129359cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
12944848405cSBen Widawsky 		dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
12954d3b3d5fSPaulo Zanoni 		snb_disable_pm_irq(dev_priv, pm_iir & GEN6_PM_RPS_EVENTS);
129659cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
12972adbee62SDaniel Vetter 
12982adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
129941a05a3aSDaniel Vetter 	}
1300baf02a1fSBen Widawsky 
13011403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
130212638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
130312638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
130412638c57SBen Widawsky 
130512638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
130612638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
130712638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
130812638c57SBen Widawsky 		}
130912638c57SBen Widawsky 	}
13101403c0d4SPaulo Zanoni }
1311baf02a1fSBen Widawsky 
1312ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
13137e231dbeSJesse Barnes {
13147e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
13157e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13167e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
13177e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
13187e231dbeSJesse Barnes 	unsigned long irqflags;
13197e231dbeSJesse Barnes 	int pipe;
13207e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
13217e231dbeSJesse Barnes 
13227e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
13237e231dbeSJesse Barnes 
13247e231dbeSJesse Barnes 	while (true) {
13257e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
13267e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
13277e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
13287e231dbeSJesse Barnes 
13297e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
13307e231dbeSJesse Barnes 			goto out;
13317e231dbeSJesse Barnes 
13327e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
13337e231dbeSJesse Barnes 
1334e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
13357e231dbeSJesse Barnes 
13367e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
13377e231dbeSJesse Barnes 		for_each_pipe(pipe) {
13387e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
13397e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
13407e231dbeSJesse Barnes 
13417e231dbeSJesse Barnes 			/*
13427e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
13437e231dbeSJesse Barnes 			 */
13447e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
13457e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
13467e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
13477e231dbeSJesse Barnes 							 pipe_name(pipe));
13487e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
13497e231dbeSJesse Barnes 			}
13507e231dbeSJesse Barnes 		}
13517e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
13527e231dbeSJesse Barnes 
135331acc7f5SJesse Barnes 		for_each_pipe(pipe) {
135431acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
135531acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
135631acc7f5SJesse Barnes 
135731acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
135831acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
135931acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
136031acc7f5SJesse Barnes 			}
13614356d586SDaniel Vetter 
13624356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1363277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
136431acc7f5SJesse Barnes 		}
136531acc7f5SJesse Barnes 
13667e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
13677e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
13687e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1369b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
13707e231dbeSJesse Barnes 
13717e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
13727e231dbeSJesse Barnes 					 hotplug_status);
137391d131d2SDaniel Vetter 
137410a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
137591d131d2SDaniel Vetter 
13767e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
13777e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
13787e231dbeSJesse Barnes 		}
13797e231dbeSJesse Barnes 
1380515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1381515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
13827e231dbeSJesse Barnes 
138360611c13SPaulo Zanoni 		if (pm_iir)
1384d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
13857e231dbeSJesse Barnes 
13867e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
13877e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
13887e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
13897e231dbeSJesse Barnes 	}
13907e231dbeSJesse Barnes 
13917e231dbeSJesse Barnes out:
13927e231dbeSJesse Barnes 	return ret;
13937e231dbeSJesse Barnes }
13947e231dbeSJesse Barnes 
139523e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1396776ad806SJesse Barnes {
1397776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13989db4a9c7SJesse Barnes 	int pipe;
1399b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1400776ad806SJesse Barnes 
140110a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
140291d131d2SDaniel Vetter 
1403cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1404cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1405776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1406cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1407cfc33bf7SVille Syrjälä 				 port_name(port));
1408cfc33bf7SVille Syrjälä 	}
1409776ad806SJesse Barnes 
1410ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1411ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1412ce99c256SDaniel Vetter 
1413776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1414515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1415776ad806SJesse Barnes 
1416776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1417776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1418776ad806SJesse Barnes 
1419776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1420776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1421776ad806SJesse Barnes 
1422776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1423776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1424776ad806SJesse Barnes 
14259db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
14269db4a9c7SJesse Barnes 		for_each_pipe(pipe)
14279db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
14289db4a9c7SJesse Barnes 					 pipe_name(pipe),
14299db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1430776ad806SJesse Barnes 
1431776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1432776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1433776ad806SJesse Barnes 
1434776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1435776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1436776ad806SJesse Barnes 
1437776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
14388664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
14398664281bSPaulo Zanoni 							  false))
14408664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
14418664281bSPaulo Zanoni 
14428664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
14438664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
14448664281bSPaulo Zanoni 							  false))
14458664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
14468664281bSPaulo Zanoni }
14478664281bSPaulo Zanoni 
14488664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
14498664281bSPaulo Zanoni {
14508664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
14518664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
14525a69b89fSDaniel Vetter 	enum pipe pipe;
14538664281bSPaulo Zanoni 
1454de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1455de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1456de032bf4SPaulo Zanoni 
14575a69b89fSDaniel Vetter 	for_each_pipe(pipe) {
14585a69b89fSDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
14595a69b89fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
14605a69b89fSDaniel Vetter 								  false))
14615a69b89fSDaniel Vetter 				DRM_DEBUG_DRIVER("Pipe %c FIFO underrun\n",
14625a69b89fSDaniel Vetter 						 pipe_name(pipe));
14635a69b89fSDaniel Vetter 		}
14648664281bSPaulo Zanoni 
14655a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
14665a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1467277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
14685a69b89fSDaniel Vetter 			else
1469277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
14705a69b89fSDaniel Vetter 		}
14715a69b89fSDaniel Vetter 	}
14728bf1e9f1SShuang He 
14738664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
14748664281bSPaulo Zanoni }
14758664281bSPaulo Zanoni 
14768664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
14778664281bSPaulo Zanoni {
14788664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
14798664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
14808664281bSPaulo Zanoni 
1481de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1482de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1483de032bf4SPaulo Zanoni 
14848664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
14858664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
14868664281bSPaulo Zanoni 							  false))
14878664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
14888664281bSPaulo Zanoni 
14898664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
14908664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
14918664281bSPaulo Zanoni 							  false))
14928664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
14938664281bSPaulo Zanoni 
14948664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
14958664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
14968664281bSPaulo Zanoni 							  false))
14978664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
14988664281bSPaulo Zanoni 
14998664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1500776ad806SJesse Barnes }
1501776ad806SJesse Barnes 
150223e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
150323e81d69SAdam Jackson {
150423e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
150523e81d69SAdam Jackson 	int pipe;
1506b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
150723e81d69SAdam Jackson 
150810a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
150991d131d2SDaniel Vetter 
1510cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1511cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
151223e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1513cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1514cfc33bf7SVille Syrjälä 				 port_name(port));
1515cfc33bf7SVille Syrjälä 	}
151623e81d69SAdam Jackson 
151723e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1518ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
151923e81d69SAdam Jackson 
152023e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1521515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
152223e81d69SAdam Jackson 
152323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
152423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
152523e81d69SAdam Jackson 
152623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
152723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
152823e81d69SAdam Jackson 
152923e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
153023e81d69SAdam Jackson 		for_each_pipe(pipe)
153123e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
153223e81d69SAdam Jackson 					 pipe_name(pipe),
153323e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
15348664281bSPaulo Zanoni 
15358664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
15368664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
153723e81d69SAdam Jackson }
153823e81d69SAdam Jackson 
1539c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1540c008bc6eSPaulo Zanoni {
1541c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
154240da17c2SDaniel Vetter 	enum pipe pipe;
1543c008bc6eSPaulo Zanoni 
1544c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1545c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1546c008bc6eSPaulo Zanoni 
1547c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1548c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1549c008bc6eSPaulo Zanoni 
1550c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1551c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1552c008bc6eSPaulo Zanoni 
155340da17c2SDaniel Vetter 	for_each_pipe(pipe) {
155440da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
155540da17c2SDaniel Vetter 			drm_handle_vblank(dev, pipe);
1556c008bc6eSPaulo Zanoni 
155740da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
155840da17c2SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
155940da17c2SDaniel Vetter 				DRM_DEBUG_DRIVER("Pipe %c FIFO underrun\n",
156040da17c2SDaniel Vetter 						 pipe_name(pipe));
1561c008bc6eSPaulo Zanoni 
156240da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
156340da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
15645b3a856bSDaniel Vetter 
156540da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
156640da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
156740da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
156840da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
1569c008bc6eSPaulo Zanoni 		}
1570c008bc6eSPaulo Zanoni 	}
1571c008bc6eSPaulo Zanoni 
1572c008bc6eSPaulo Zanoni 	/* check event from PCH */
1573c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1574c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1575c008bc6eSPaulo Zanoni 
1576c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1577c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1578c008bc6eSPaulo Zanoni 		else
1579c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1580c008bc6eSPaulo Zanoni 
1581c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1582c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1583c008bc6eSPaulo Zanoni 	}
1584c008bc6eSPaulo Zanoni 
1585c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1586c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1587c008bc6eSPaulo Zanoni }
1588c008bc6eSPaulo Zanoni 
15899719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
15909719fb98SPaulo Zanoni {
15919719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
15923b6c42e8SDaniel Vetter 	enum pipe i;
15939719fb98SPaulo Zanoni 
15949719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
15959719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
15969719fb98SPaulo Zanoni 
15979719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
15989719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
15999719fb98SPaulo Zanoni 
16009719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
16019719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
16029719fb98SPaulo Zanoni 
16033b6c42e8SDaniel Vetter 	for_each_pipe(i) {
160440da17c2SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(i)))
16059719fb98SPaulo Zanoni 			drm_handle_vblank(dev, i);
160640da17c2SDaniel Vetter 
160740da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
160840da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(i)) {
16099719fb98SPaulo Zanoni 			intel_prepare_page_flip(dev, i);
16109719fb98SPaulo Zanoni 			intel_finish_page_flip_plane(dev, i);
16119719fb98SPaulo Zanoni 		}
16129719fb98SPaulo Zanoni 	}
16139719fb98SPaulo Zanoni 
16149719fb98SPaulo Zanoni 	/* check event from PCH */
16159719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
16169719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
16179719fb98SPaulo Zanoni 
16189719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
16199719fb98SPaulo Zanoni 
16209719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
16219719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
16229719fb98SPaulo Zanoni 	}
16239719fb98SPaulo Zanoni }
16249719fb98SPaulo Zanoni 
1625f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1626b1f14ad0SJesse Barnes {
1627b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1628b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1629f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
16300e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1631b1f14ad0SJesse Barnes 
1632b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1633b1f14ad0SJesse Barnes 
16348664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
16358664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1636907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
16378664281bSPaulo Zanoni 
1638b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1639b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1640b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
164123a78516SPaulo Zanoni 	POSTING_READ(DEIER);
16420e43406bSChris Wilson 
164344498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
164444498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
164544498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
164644498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
164744498aeaSPaulo Zanoni 	 * due to its back queue). */
1648ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
164944498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
165044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
165144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1652ab5c608bSBen Widawsky 	}
165344498aeaSPaulo Zanoni 
16540e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
16550e43406bSChris Wilson 	if (gt_iir) {
1656d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
16570e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1658d8fc8a47SPaulo Zanoni 		else
1659d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
16600e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
16610e43406bSChris Wilson 		ret = IRQ_HANDLED;
16620e43406bSChris Wilson 	}
1663b1f14ad0SJesse Barnes 
1664b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
16650e43406bSChris Wilson 	if (de_iir) {
1666f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
16679719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1668f1af8fc1SPaulo Zanoni 		else
1669f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
16700e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
16710e43406bSChris Wilson 		ret = IRQ_HANDLED;
16720e43406bSChris Wilson 	}
16730e43406bSChris Wilson 
1674f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
1675f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
16760e43406bSChris Wilson 		if (pm_iir) {
1677d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
1678b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
16790e43406bSChris Wilson 			ret = IRQ_HANDLED;
16800e43406bSChris Wilson 		}
1681f1af8fc1SPaulo Zanoni 	}
1682b1f14ad0SJesse Barnes 
1683b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1684b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1685ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
168644498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
168744498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1688ab5c608bSBen Widawsky 	}
1689b1f14ad0SJesse Barnes 
1690b1f14ad0SJesse Barnes 	return ret;
1691b1f14ad0SJesse Barnes }
1692b1f14ad0SJesse Barnes 
169317e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
169417e1df07SDaniel Vetter 			       bool reset_completed)
169517e1df07SDaniel Vetter {
169617e1df07SDaniel Vetter 	struct intel_ring_buffer *ring;
169717e1df07SDaniel Vetter 	int i;
169817e1df07SDaniel Vetter 
169917e1df07SDaniel Vetter 	/*
170017e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
170117e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
170217e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
170317e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
170417e1df07SDaniel Vetter 	 */
170517e1df07SDaniel Vetter 
170617e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
170717e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
170817e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
170917e1df07SDaniel Vetter 
171017e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
171117e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
171217e1df07SDaniel Vetter 
171317e1df07SDaniel Vetter 	/*
171417e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
171517e1df07SDaniel Vetter 	 * reset state is cleared.
171617e1df07SDaniel Vetter 	 */
171717e1df07SDaniel Vetter 	if (reset_completed)
171817e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
171917e1df07SDaniel Vetter }
172017e1df07SDaniel Vetter 
17218a905236SJesse Barnes /**
17228a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
17238a905236SJesse Barnes  * @work: work struct
17248a905236SJesse Barnes  *
17258a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
17268a905236SJesse Barnes  * was detected.
17278a905236SJesse Barnes  */
17288a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
17298a905236SJesse Barnes {
17301f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
17311f83fee0SDaniel Vetter 						    work);
17321f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
17331f83fee0SDaniel Vetter 						    gpu_error);
17348a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1735cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1736cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1737cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
173817e1df07SDaniel Vetter 	int ret;
17398a905236SJesse Barnes 
1740f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
17418a905236SJesse Barnes 
17427db0ba24SDaniel Vetter 	/*
17437db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
17447db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
17457db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
17467db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
17477db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
17487db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
17497db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
17507db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
17517db0ba24SDaniel Vetter 	 */
17527db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
175344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
17547db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
17557db0ba24SDaniel Vetter 				   reset_event);
17561f83fee0SDaniel Vetter 
175717e1df07SDaniel Vetter 		/*
175817e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
175917e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
176017e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
176117e1df07SDaniel Vetter 		 * deadlocks with the reset work.
176217e1df07SDaniel Vetter 		 */
1763f69061beSDaniel Vetter 		ret = i915_reset(dev);
1764f69061beSDaniel Vetter 
176517e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
176617e1df07SDaniel Vetter 
1767f69061beSDaniel Vetter 		if (ret == 0) {
1768f69061beSDaniel Vetter 			/*
1769f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1770f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1771f69061beSDaniel Vetter 			 * complete.
1772f69061beSDaniel Vetter 			 *
1773f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1774f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1775f69061beSDaniel Vetter 			 * updates before
1776f69061beSDaniel Vetter 			 * the counter increment.
1777f69061beSDaniel Vetter 			 */
1778f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1779f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1780f69061beSDaniel Vetter 
1781f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1782f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
17831f83fee0SDaniel Vetter 		} else {
17841f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1785f316a42cSBen Gamari 		}
17861f83fee0SDaniel Vetter 
178717e1df07SDaniel Vetter 		/*
178817e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
178917e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
179017e1df07SDaniel Vetter 		 */
179117e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
1792f316a42cSBen Gamari 	}
17938a905236SJesse Barnes }
17948a905236SJesse Barnes 
179535aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1796c0e09200SDave Airlie {
17978a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1798bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
179963eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1800050ee91fSBen Widawsky 	int pipe, i;
180163eeaf38SJesse Barnes 
180235aed2e6SChris Wilson 	if (!eir)
180335aed2e6SChris Wilson 		return;
180463eeaf38SJesse Barnes 
1805a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
18068a905236SJesse Barnes 
1807bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1808bd9854f9SBen Widawsky 
18098a905236SJesse Barnes 	if (IS_G4X(dev)) {
18108a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
18118a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
18128a905236SJesse Barnes 
1813a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1814a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1815050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1816050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1817a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1818a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
18198a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
18203143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
18218a905236SJesse Barnes 		}
18228a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
18238a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1824a70491ccSJoe Perches 			pr_err("page table error\n");
1825a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
18268a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
18273143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
18288a905236SJesse Barnes 		}
18298a905236SJesse Barnes 	}
18308a905236SJesse Barnes 
1831a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
183263eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
183363eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1834a70491ccSJoe Perches 			pr_err("page table error\n");
1835a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
183663eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
18373143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
183863eeaf38SJesse Barnes 		}
18398a905236SJesse Barnes 	}
18408a905236SJesse Barnes 
184163eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1842a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
18439db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1844a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
18459db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
184663eeaf38SJesse Barnes 		/* pipestat has already been acked */
184763eeaf38SJesse Barnes 	}
184863eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1849a70491ccSJoe Perches 		pr_err("instruction error\n");
1850a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1851050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1852050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1853a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
185463eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
185563eeaf38SJesse Barnes 
1856a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1857a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1858a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
185963eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
18603143a2bfSChris Wilson 			POSTING_READ(IPEIR);
186163eeaf38SJesse Barnes 		} else {
186263eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
186363eeaf38SJesse Barnes 
1864a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1865a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1866a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1867a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
186863eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
18693143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
187063eeaf38SJesse Barnes 		}
187163eeaf38SJesse Barnes 	}
187263eeaf38SJesse Barnes 
187363eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
18743143a2bfSChris Wilson 	POSTING_READ(EIR);
187563eeaf38SJesse Barnes 	eir = I915_READ(EIR);
187663eeaf38SJesse Barnes 	if (eir) {
187763eeaf38SJesse Barnes 		/*
187863eeaf38SJesse Barnes 		 * some errors might have become stuck,
187963eeaf38SJesse Barnes 		 * mask them.
188063eeaf38SJesse Barnes 		 */
188163eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
188263eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
188363eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
188463eeaf38SJesse Barnes 	}
188535aed2e6SChris Wilson }
188635aed2e6SChris Wilson 
188735aed2e6SChris Wilson /**
188835aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
188935aed2e6SChris Wilson  * @dev: drm device
189035aed2e6SChris Wilson  *
189135aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
189235aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
189335aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
189435aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
189535aed2e6SChris Wilson  * of a ring dump etc.).
189635aed2e6SChris Wilson  */
1897527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
189835aed2e6SChris Wilson {
189935aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
190035aed2e6SChris Wilson 
190135aed2e6SChris Wilson 	i915_capture_error_state(dev);
190235aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
19038a905236SJesse Barnes 
1904ba1234d1SBen Gamari 	if (wedged) {
1905f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1906f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
1907ba1234d1SBen Gamari 
190811ed50ecSBen Gamari 		/*
190917e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
191017e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
191117e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
191217e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
191317e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
191417e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
191517e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
191617e1df07SDaniel Vetter 		 *
191717e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
191817e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
191917e1df07SDaniel Vetter 		 * counter atomic_t.
192011ed50ecSBen Gamari 		 */
192117e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
192211ed50ecSBen Gamari 	}
192311ed50ecSBen Gamari 
1924122f46baSDaniel Vetter 	/*
1925122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
1926122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
1927122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
1928122f46baSDaniel Vetter 	 * code will deadlock.
1929122f46baSDaniel Vetter 	 */
1930122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
19318a905236SJesse Barnes }
19328a905236SJesse Barnes 
193321ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
19344e5359cdSSimon Farnsworth {
19354e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
19364e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
19374e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
193805394f39SChris Wilson 	struct drm_i915_gem_object *obj;
19394e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
19404e5359cdSSimon Farnsworth 	unsigned long flags;
19414e5359cdSSimon Farnsworth 	bool stall_detected;
19424e5359cdSSimon Farnsworth 
19434e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
19444e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
19454e5359cdSSimon Farnsworth 		return;
19464e5359cdSSimon Farnsworth 
19474e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
19484e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
19494e5359cdSSimon Farnsworth 
1950e7d841caSChris Wilson 	if (work == NULL ||
1951e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1952e7d841caSChris Wilson 	    !work->enable_stall_check) {
19534e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
19544e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
19554e5359cdSSimon Farnsworth 		return;
19564e5359cdSSimon Farnsworth 	}
19574e5359cdSSimon Farnsworth 
19584e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
195905394f39SChris Wilson 	obj = work->pending_flip_obj;
1960a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
19619db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1962446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1963f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
19644e5359cdSSimon Farnsworth 	} else {
19659db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
1966f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
196701f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
19684e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
19694e5359cdSSimon Farnsworth 	}
19704e5359cdSSimon Farnsworth 
19714e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
19724e5359cdSSimon Farnsworth 
19734e5359cdSSimon Farnsworth 	if (stall_detected) {
19744e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
19754e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
19764e5359cdSSimon Farnsworth 	}
19774e5359cdSSimon Farnsworth }
19784e5359cdSSimon Farnsworth 
197942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
198042f52ef8SKeith Packard  * we use as a pipe index
198142f52ef8SKeith Packard  */
1982f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
19830a3e67a4SJesse Barnes {
19840a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1985e9d21d7fSKeith Packard 	unsigned long irqflags;
198671e0ffa5SJesse Barnes 
19875eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
198871e0ffa5SJesse Barnes 		return -EINVAL;
19890a3e67a4SJesse Barnes 
19901ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1991f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
19927c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
19937c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
19940a3e67a4SJesse Barnes 	else
19957c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
19967c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
19978692d00eSChris Wilson 
19988692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
19998692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
20006b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
20011ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
20028692d00eSChris Wilson 
20030a3e67a4SJesse Barnes 	return 0;
20040a3e67a4SJesse Barnes }
20050a3e67a4SJesse Barnes 
2006f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2007f796cf8fSJesse Barnes {
2008f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2009f796cf8fSJesse Barnes 	unsigned long irqflags;
2010b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
201140da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2012f796cf8fSJesse Barnes 
2013f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2014f796cf8fSJesse Barnes 		return -EINVAL;
2015f796cf8fSJesse Barnes 
2016f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2017b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2018b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2019b1f14ad0SJesse Barnes 
2020b1f14ad0SJesse Barnes 	return 0;
2021b1f14ad0SJesse Barnes }
2022b1f14ad0SJesse Barnes 
20237e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
20247e231dbeSJesse Barnes {
20257e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20267e231dbeSJesse Barnes 	unsigned long irqflags;
202731acc7f5SJesse Barnes 	u32 imr;
20287e231dbeSJesse Barnes 
20297e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
20307e231dbeSJesse Barnes 		return -EINVAL;
20317e231dbeSJesse Barnes 
20327e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
20337e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
20343b6c42e8SDaniel Vetter 	if (pipe == PIPE_A)
20357e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
203631acc7f5SJesse Barnes 	else
20377e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
20387e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
203931acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
204031acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
20417e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
20427e231dbeSJesse Barnes 
20437e231dbeSJesse Barnes 	return 0;
20447e231dbeSJesse Barnes }
20457e231dbeSJesse Barnes 
204642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
204742f52ef8SKeith Packard  * we use as a pipe index
204842f52ef8SKeith Packard  */
2049f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
20500a3e67a4SJesse Barnes {
20510a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2052e9d21d7fSKeith Packard 	unsigned long irqflags;
20530a3e67a4SJesse Barnes 
20541ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
20558692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
20566b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
20578692d00eSChris Wilson 
20587c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
20597c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
20607c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
20611ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
20620a3e67a4SJesse Barnes }
20630a3e67a4SJesse Barnes 
2064f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2065f796cf8fSJesse Barnes {
2066f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2067f796cf8fSJesse Barnes 	unsigned long irqflags;
2068b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
206940da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2070f796cf8fSJesse Barnes 
2071f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2072b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2073b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2074b1f14ad0SJesse Barnes }
2075b1f14ad0SJesse Barnes 
20767e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
20777e231dbeSJesse Barnes {
20787e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20797e231dbeSJesse Barnes 	unsigned long irqflags;
208031acc7f5SJesse Barnes 	u32 imr;
20817e231dbeSJesse Barnes 
20827e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
208331acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
208431acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
20857e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
20863b6c42e8SDaniel Vetter 	if (pipe == PIPE_A)
20877e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
208831acc7f5SJesse Barnes 	else
20897e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
20907e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
20917e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
20927e231dbeSJesse Barnes }
20937e231dbeSJesse Barnes 
2094893eead0SChris Wilson static u32
2095893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2096852835f3SZou Nan hai {
2097893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2098893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2099893eead0SChris Wilson }
2100893eead0SChris Wilson 
21019107e9d2SChris Wilson static bool
21029107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2103893eead0SChris Wilson {
21049107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
21059107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2106f65d9421SBen Gamari }
2107f65d9421SBen Gamari 
21086274f212SChris Wilson static struct intel_ring_buffer *
21096274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2110a24a11e6SChris Wilson {
2111a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
21126274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2113a24a11e6SChris Wilson 
2114a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2115a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2116a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
21176274f212SChris Wilson 		return NULL;
2118a24a11e6SChris Wilson 
2119a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2120a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2121a24a11e6SChris Wilson 	 */
21226274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2123a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2124a24a11e6SChris Wilson 	do {
2125a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2126a24a11e6SChris Wilson 		if (cmd == ipehr)
2127a24a11e6SChris Wilson 			break;
2128a24a11e6SChris Wilson 
2129a24a11e6SChris Wilson 		acthd -= 4;
2130a24a11e6SChris Wilson 		if (acthd < acthd_min)
21316274f212SChris Wilson 			return NULL;
2132a24a11e6SChris Wilson 	} while (1);
2133a24a11e6SChris Wilson 
21346274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
21356274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2136a24a11e6SChris Wilson }
2137a24a11e6SChris Wilson 
21386274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
21396274f212SChris Wilson {
21406274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
21416274f212SChris Wilson 	struct intel_ring_buffer *signaller;
21426274f212SChris Wilson 	u32 seqno, ctl;
21436274f212SChris Wilson 
21446274f212SChris Wilson 	ring->hangcheck.deadlock = true;
21456274f212SChris Wilson 
21466274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
21476274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
21486274f212SChris Wilson 		return -1;
21496274f212SChris Wilson 
21506274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
21516274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
21526274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
21536274f212SChris Wilson 		return -1;
21546274f212SChris Wilson 
21556274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
21566274f212SChris Wilson }
21576274f212SChris Wilson 
21586274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
21596274f212SChris Wilson {
21606274f212SChris Wilson 	struct intel_ring_buffer *ring;
21616274f212SChris Wilson 	int i;
21626274f212SChris Wilson 
21636274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
21646274f212SChris Wilson 		ring->hangcheck.deadlock = false;
21656274f212SChris Wilson }
21666274f212SChris Wilson 
2167ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2168ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
21691ec14ad3SChris Wilson {
21701ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
21711ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
21729107e9d2SChris Wilson 	u32 tmp;
21739107e9d2SChris Wilson 
21746274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2175f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
21766274f212SChris Wilson 
21779107e9d2SChris Wilson 	if (IS_GEN2(dev))
2178f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
21799107e9d2SChris Wilson 
21809107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
21819107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
21829107e9d2SChris Wilson 	 * and break the hang. This should work on
21839107e9d2SChris Wilson 	 * all but the second generation chipsets.
21849107e9d2SChris Wilson 	 */
21859107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
21861ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
21871ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
21881ec14ad3SChris Wilson 			  ring->name);
218909e14bf3SChris Wilson 		i915_handle_error(dev, false);
21901ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2191f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
21921ec14ad3SChris Wilson 	}
2193a24a11e6SChris Wilson 
21946274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
21956274f212SChris Wilson 		switch (semaphore_passed(ring)) {
21966274f212SChris Wilson 		default:
2197f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
21986274f212SChris Wilson 		case 1:
2199a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
2200a24a11e6SChris Wilson 				  ring->name);
220109e14bf3SChris Wilson 			i915_handle_error(dev, false);
2202a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2203f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
22046274f212SChris Wilson 		case 0:
2205f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
22066274f212SChris Wilson 		}
22079107e9d2SChris Wilson 	}
22089107e9d2SChris Wilson 
2209f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2210a24a11e6SChris Wilson }
2211d1e61e7fSChris Wilson 
2212f65d9421SBen Gamari /**
2213f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
221405407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
221505407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
221605407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
221705407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
221805407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2219f65d9421SBen Gamari  */
2220a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2221f65d9421SBen Gamari {
2222f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2223f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2224b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2225b4519513SChris Wilson 	int i;
222605407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
22279107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
22289107e9d2SChris Wilson #define BUSY 1
22299107e9d2SChris Wilson #define KICK 5
22309107e9d2SChris Wilson #define HUNG 20
22319107e9d2SChris Wilson #define FIRE 30
2232893eead0SChris Wilson 
22333e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
22343e0dc6b0SBen Widawsky 		return;
22353e0dc6b0SBen Widawsky 
2236b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
223705407ff8SMika Kuoppala 		u32 seqno, acthd;
22389107e9d2SChris Wilson 		bool busy = true;
2239b4519513SChris Wilson 
22406274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
22416274f212SChris Wilson 
224205407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
224305407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
224405407ff8SMika Kuoppala 
224505407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
22469107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2247da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2248da661464SMika Kuoppala 
22499107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
22509107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2251094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2252f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
22539107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
22549107e9d2SChris Wilson 								  ring->name);
2255f4adcd24SDaniel Vetter 						else
2256f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2257f4adcd24SDaniel Vetter 								 ring->name);
22589107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2259094f9a54SChris Wilson 					}
2260094f9a54SChris Wilson 					/* Safeguard against driver failure */
2261094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
22629107e9d2SChris Wilson 				} else
22639107e9d2SChris Wilson 					busy = false;
226405407ff8SMika Kuoppala 			} else {
22656274f212SChris Wilson 				/* We always increment the hangcheck score
22666274f212SChris Wilson 				 * if the ring is busy and still processing
22676274f212SChris Wilson 				 * the same request, so that no single request
22686274f212SChris Wilson 				 * can run indefinitely (such as a chain of
22696274f212SChris Wilson 				 * batches). The only time we do not increment
22706274f212SChris Wilson 				 * the hangcheck score on this ring, if this
22716274f212SChris Wilson 				 * ring is in a legitimate wait for another
22726274f212SChris Wilson 				 * ring. In that case the waiting ring is a
22736274f212SChris Wilson 				 * victim and we want to be sure we catch the
22746274f212SChris Wilson 				 * right culprit. Then every time we do kick
22756274f212SChris Wilson 				 * the ring, add a small increment to the
22766274f212SChris Wilson 				 * score so that we can catch a batch that is
22776274f212SChris Wilson 				 * being repeatedly kicked and so responsible
22786274f212SChris Wilson 				 * for stalling the machine.
22799107e9d2SChris Wilson 				 */
2280ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2281ad8beaeaSMika Kuoppala 								    acthd);
2282ad8beaeaSMika Kuoppala 
2283ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2284da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2285f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
22866274f212SChris Wilson 					break;
2287f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2288ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
22896274f212SChris Wilson 					break;
2290f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2291ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
22926274f212SChris Wilson 					break;
2293f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2294ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
22956274f212SChris Wilson 					stuck[i] = true;
22966274f212SChris Wilson 					break;
22976274f212SChris Wilson 				}
229805407ff8SMika Kuoppala 			}
22999107e9d2SChris Wilson 		} else {
2300da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2301da661464SMika Kuoppala 
23029107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
23039107e9d2SChris Wilson 			 * attempts across multiple batches.
23049107e9d2SChris Wilson 			 */
23059107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
23069107e9d2SChris Wilson 				ring->hangcheck.score--;
2307cbb465e7SChris Wilson 		}
2308f65d9421SBen Gamari 
230905407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
231005407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
23119107e9d2SChris Wilson 		busy_count += busy;
231205407ff8SMika Kuoppala 	}
231305407ff8SMika Kuoppala 
231405407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
23159107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2316b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
231705407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2318a43adf07SChris Wilson 				 ring->name);
2319a43adf07SChris Wilson 			rings_hung++;
232005407ff8SMika Kuoppala 		}
232105407ff8SMika Kuoppala 	}
232205407ff8SMika Kuoppala 
232305407ff8SMika Kuoppala 	if (rings_hung)
232405407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
232505407ff8SMika Kuoppala 
232605407ff8SMika Kuoppala 	if (busy_count)
232705407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
232805407ff8SMika Kuoppala 		 * being added */
232910cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
233010cd45b6SMika Kuoppala }
233110cd45b6SMika Kuoppala 
233210cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
233310cd45b6SMika Kuoppala {
233410cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
233510cd45b6SMika Kuoppala 	if (!i915_enable_hangcheck)
233610cd45b6SMika Kuoppala 		return;
233710cd45b6SMika Kuoppala 
233899584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
233910cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2340f65d9421SBen Gamari }
2341f65d9421SBen Gamari 
234291738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
234391738a95SPaulo Zanoni {
234491738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
234591738a95SPaulo Zanoni 
234691738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
234791738a95SPaulo Zanoni 		return;
234891738a95SPaulo Zanoni 
234991738a95SPaulo Zanoni 	/* south display irq */
235091738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
235191738a95SPaulo Zanoni 	/*
235291738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
235391738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
235491738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
235591738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
235691738a95SPaulo Zanoni 	 */
235791738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
235891738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
235991738a95SPaulo Zanoni }
236091738a95SPaulo Zanoni 
2361d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2362d18ea1b5SDaniel Vetter {
2363d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2364d18ea1b5SDaniel Vetter 
2365d18ea1b5SDaniel Vetter 	/* and GT */
2366d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2367d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2368d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2369d18ea1b5SDaniel Vetter 
2370d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2371d18ea1b5SDaniel Vetter 		/* and PM */
2372d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2373d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2374d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2375d18ea1b5SDaniel Vetter 	}
2376d18ea1b5SDaniel Vetter }
2377d18ea1b5SDaniel Vetter 
2378c0e09200SDave Airlie /* drm_dma.h hooks
2379c0e09200SDave Airlie */
2380f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2381036a4a7dSZhenyu Wang {
2382036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2383036a4a7dSZhenyu Wang 
23844697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
23854697995bSJesse Barnes 
2386036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2387bdfcdb63SDaniel Vetter 
2388036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2389036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
23903143a2bfSChris Wilson 	POSTING_READ(DEIER);
2391036a4a7dSZhenyu Wang 
2392d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2393c650156aSZhenyu Wang 
239491738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
23957d99163dSBen Widawsky }
23967d99163dSBen Widawsky 
23977e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
23987e231dbeSJesse Barnes {
23997e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24007e231dbeSJesse Barnes 	int pipe;
24017e231dbeSJesse Barnes 
24027e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
24037e231dbeSJesse Barnes 
24047e231dbeSJesse Barnes 	/* VLV magic */
24057e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
24067e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
24077e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
24087e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
24097e231dbeSJesse Barnes 
24107e231dbeSJesse Barnes 	/* and GT */
24117e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
24127e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2413d18ea1b5SDaniel Vetter 
2414d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
24157e231dbeSJesse Barnes 
24167e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
24177e231dbeSJesse Barnes 
24187e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
24197e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
24207e231dbeSJesse Barnes 	for_each_pipe(pipe)
24217e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
24227e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
24237e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
24247e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
24257e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
24267e231dbeSJesse Barnes }
24277e231dbeSJesse Barnes 
242882a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
242982a28bcfSDaniel Vetter {
243082a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
243182a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
243282a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2433fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
243482a28bcfSDaniel Vetter 
243582a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2436fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
243782a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2438cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2439fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
244082a28bcfSDaniel Vetter 	} else {
2441fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
244282a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2443cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2444fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
244582a28bcfSDaniel Vetter 	}
244682a28bcfSDaniel Vetter 
2447fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
244882a28bcfSDaniel Vetter 
24497fe0b973SKeith Packard 	/*
24507fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
24517fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
24527fe0b973SKeith Packard 	 *
24537fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
24547fe0b973SKeith Packard 	 */
24557fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
24567fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
24577fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
24587fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
24597fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
24607fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
24617fe0b973SKeith Packard }
24627fe0b973SKeith Packard 
2463d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2464d46da437SPaulo Zanoni {
2465d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
246682a28bcfSDaniel Vetter 	u32 mask;
2467d46da437SPaulo Zanoni 
2468692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2469692a04cfSDaniel Vetter 		return;
2470692a04cfSDaniel Vetter 
24718664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
24728664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2473de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
24748664281bSPaulo Zanoni 	} else {
24758664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
24768664281bSPaulo Zanoni 
24778664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
24788664281bSPaulo Zanoni 	}
2479ab5c608bSBen Widawsky 
2480d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2481d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2482d46da437SPaulo Zanoni }
2483d46da437SPaulo Zanoni 
24840a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
24850a9a8c91SDaniel Vetter {
24860a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
24870a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
24880a9a8c91SDaniel Vetter 
24890a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
24900a9a8c91SDaniel Vetter 
24910a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
2492040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
24930a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
249435a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
249535a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
24960a9a8c91SDaniel Vetter 	}
24970a9a8c91SDaniel Vetter 
24980a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
24990a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
25000a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
25010a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
25020a9a8c91SDaniel Vetter 	} else {
25030a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
25040a9a8c91SDaniel Vetter 	}
25050a9a8c91SDaniel Vetter 
25060a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
25070a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
25080a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
25090a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
25100a9a8c91SDaniel Vetter 
25110a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
25120a9a8c91SDaniel Vetter 		pm_irqs |= GEN6_PM_RPS_EVENTS;
25130a9a8c91SDaniel Vetter 
25140a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
25150a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
25160a9a8c91SDaniel Vetter 
2517605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
25180a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2519605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
25200a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
25210a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
25220a9a8c91SDaniel Vetter 	}
25230a9a8c91SDaniel Vetter }
25240a9a8c91SDaniel Vetter 
2525f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2526036a4a7dSZhenyu Wang {
25274bc9d430SDaniel Vetter 	unsigned long irqflags;
2528036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25298e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
25308e76f8dcSPaulo Zanoni 
25318e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
25328e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
25338e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
25348e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
25358e76f8dcSPaulo Zanoni 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
25368e76f8dcSPaulo Zanoni 				DE_ERR_INT_IVB);
25378e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
25388e76f8dcSPaulo Zanoni 			      DE_PIPEA_VBLANK_IVB);
25398e76f8dcSPaulo Zanoni 
25408e76f8dcSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
25418e76f8dcSPaulo Zanoni 	} else {
25428e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2543ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
25445b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
25455b3a856bSDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
25465b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
25475b3a856bSDaniel Vetter 				DE_POISON);
25488e76f8dcSPaulo Zanoni 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
25498e76f8dcSPaulo Zanoni 	}
2550036a4a7dSZhenyu Wang 
25511ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2552036a4a7dSZhenyu Wang 
2553036a4a7dSZhenyu Wang 	/* should always can generate irq */
2554036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
25551ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
25568e76f8dcSPaulo Zanoni 	I915_WRITE(DEIER, display_mask | extra_mask);
25573143a2bfSChris Wilson 	POSTING_READ(DEIER);
2558036a4a7dSZhenyu Wang 
25590a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
2560036a4a7dSZhenyu Wang 
2561d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
25627fe0b973SKeith Packard 
2563f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
25646005ce42SDaniel Vetter 		/* Enable PCU event interrupts
25656005ce42SDaniel Vetter 		 *
25666005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
25674bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
25684bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
25694bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2570f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
25714bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2572f97108d1SJesse Barnes 	}
2573f97108d1SJesse Barnes 
2574036a4a7dSZhenyu Wang 	return 0;
2575036a4a7dSZhenyu Wang }
2576036a4a7dSZhenyu Wang 
25777e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
25787e231dbeSJesse Barnes {
25797e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25807e231dbeSJesse Barnes 	u32 enable_mask;
2581379ef82dSDaniel Vetter 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV |
2582379ef82dSDaniel Vetter 		PIPE_CRC_DONE_ENABLE;
2583b79480baSDaniel Vetter 	unsigned long irqflags;
25847e231dbeSJesse Barnes 
25857e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
258631acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
258731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
258831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
25897e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
25907e231dbeSJesse Barnes 
259131acc7f5SJesse Barnes 	/*
259231acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
259331acc7f5SJesse Barnes 	 * toggle them based on usage.
259431acc7f5SJesse Barnes 	 */
259531acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
259631acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
259731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
25987e231dbeSJesse Barnes 
259920afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
260020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
260120afbda2SDaniel Vetter 
26027e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
26037e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
26047e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26057e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
26067e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
26077e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
26087e231dbeSJesse Barnes 
2609b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2610b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2611b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26123b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_enable);
26133b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_EVENT_ENABLE);
26143b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_enable);
2615b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
261631acc7f5SJesse Barnes 
26177e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26187e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26197e231dbeSJesse Barnes 
26200a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
26217e231dbeSJesse Barnes 
26227e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
26237e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
26247e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
26257e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
26267e231dbeSJesse Barnes #endif
26277e231dbeSJesse Barnes 
26287e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
262920afbda2SDaniel Vetter 
263020afbda2SDaniel Vetter 	return 0;
263120afbda2SDaniel Vetter }
263220afbda2SDaniel Vetter 
26337e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
26347e231dbeSJesse Barnes {
26357e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26367e231dbeSJesse Barnes 	int pipe;
26377e231dbeSJesse Barnes 
26387e231dbeSJesse Barnes 	if (!dev_priv)
26397e231dbeSJesse Barnes 		return;
26407e231dbeSJesse Barnes 
2641ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2642ac4c16c5SEgbert Eich 
26437e231dbeSJesse Barnes 	for_each_pipe(pipe)
26447e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
26457e231dbeSJesse Barnes 
26467e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
26477e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
26487e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
26497e231dbeSJesse Barnes 	for_each_pipe(pipe)
26507e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
26517e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26527e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
26537e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
26547e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
26557e231dbeSJesse Barnes }
26567e231dbeSJesse Barnes 
2657f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2658036a4a7dSZhenyu Wang {
2659036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26604697995bSJesse Barnes 
26614697995bSJesse Barnes 	if (!dev_priv)
26624697995bSJesse Barnes 		return;
26634697995bSJesse Barnes 
2664ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2665ac4c16c5SEgbert Eich 
2666036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2667036a4a7dSZhenyu Wang 
2668036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2669036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2670036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
26718664281bSPaulo Zanoni 	if (IS_GEN7(dev))
26728664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2673036a4a7dSZhenyu Wang 
2674036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2675036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2676036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2677192aac1fSKeith Packard 
2678ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2679ab5c608bSBen Widawsky 		return;
2680ab5c608bSBen Widawsky 
2681192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2682192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2683192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
26848664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
26858664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2686036a4a7dSZhenyu Wang }
2687036a4a7dSZhenyu Wang 
2688c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2689c2798b19SChris Wilson {
2690c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2691c2798b19SChris Wilson 	int pipe;
2692c2798b19SChris Wilson 
2693c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2694c2798b19SChris Wilson 
2695c2798b19SChris Wilson 	for_each_pipe(pipe)
2696c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2697c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2698c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2699c2798b19SChris Wilson 	POSTING_READ16(IER);
2700c2798b19SChris Wilson }
2701c2798b19SChris Wilson 
2702c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2703c2798b19SChris Wilson {
2704c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2705379ef82dSDaniel Vetter 	unsigned long irqflags;
2706c2798b19SChris Wilson 
2707c2798b19SChris Wilson 	I915_WRITE16(EMR,
2708c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2709c2798b19SChris Wilson 
2710c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2711c2798b19SChris Wilson 	dev_priv->irq_mask =
2712c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2713c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2714c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2715c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2716c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2717c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2718c2798b19SChris Wilson 
2719c2798b19SChris Wilson 	I915_WRITE16(IER,
2720c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2721c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2722c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2723c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2724c2798b19SChris Wilson 	POSTING_READ16(IER);
2725c2798b19SChris Wilson 
2726379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2727379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2728379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27293b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_ENABLE);
27303b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_ENABLE);
2731379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2732379ef82dSDaniel Vetter 
2733c2798b19SChris Wilson 	return 0;
2734c2798b19SChris Wilson }
2735c2798b19SChris Wilson 
273690a72f87SVille Syrjälä /*
273790a72f87SVille Syrjälä  * Returns true when a page flip has completed.
273890a72f87SVille Syrjälä  */
273990a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
274090a72f87SVille Syrjälä 			       int pipe, u16 iir)
274190a72f87SVille Syrjälä {
274290a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
274390a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
274490a72f87SVille Syrjälä 
274590a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
274690a72f87SVille Syrjälä 		return false;
274790a72f87SVille Syrjälä 
274890a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
274990a72f87SVille Syrjälä 		return false;
275090a72f87SVille Syrjälä 
275190a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
275290a72f87SVille Syrjälä 
275390a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
275490a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
275590a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
275690a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
275790a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
275890a72f87SVille Syrjälä 	 */
275990a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
276090a72f87SVille Syrjälä 		return false;
276190a72f87SVille Syrjälä 
276290a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
276390a72f87SVille Syrjälä 
276490a72f87SVille Syrjälä 	return true;
276590a72f87SVille Syrjälä }
276690a72f87SVille Syrjälä 
2767ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2768c2798b19SChris Wilson {
2769c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2770c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2771c2798b19SChris Wilson 	u16 iir, new_iir;
2772c2798b19SChris Wilson 	u32 pipe_stats[2];
2773c2798b19SChris Wilson 	unsigned long irqflags;
2774c2798b19SChris Wilson 	int pipe;
2775c2798b19SChris Wilson 	u16 flip_mask =
2776c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2777c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2778c2798b19SChris Wilson 
2779c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2780c2798b19SChris Wilson 
2781c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2782c2798b19SChris Wilson 	if (iir == 0)
2783c2798b19SChris Wilson 		return IRQ_NONE;
2784c2798b19SChris Wilson 
2785c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2786c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2787c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2788c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2789c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2790c2798b19SChris Wilson 		 */
2791c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2792c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2793c2798b19SChris Wilson 			i915_handle_error(dev, false);
2794c2798b19SChris Wilson 
2795c2798b19SChris Wilson 		for_each_pipe(pipe) {
2796c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2797c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2798c2798b19SChris Wilson 
2799c2798b19SChris Wilson 			/*
2800c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2801c2798b19SChris Wilson 			 */
2802c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2803c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2804c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2805c2798b19SChris Wilson 							 pipe_name(pipe));
2806c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2807c2798b19SChris Wilson 			}
2808c2798b19SChris Wilson 		}
2809c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2810c2798b19SChris Wilson 
2811c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2812c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2813c2798b19SChris Wilson 
2814d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2815c2798b19SChris Wilson 
2816c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2817c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2818c2798b19SChris Wilson 
28194356d586SDaniel Vetter 		for_each_pipe(pipe) {
28204356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
28214356d586SDaniel Vetter 			    i8xx_handle_vblank(dev, pipe, iir))
28224356d586SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
2823c2798b19SChris Wilson 
28244356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
2825277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
28264356d586SDaniel Vetter 		}
2827c2798b19SChris Wilson 
2828c2798b19SChris Wilson 		iir = new_iir;
2829c2798b19SChris Wilson 	}
2830c2798b19SChris Wilson 
2831c2798b19SChris Wilson 	return IRQ_HANDLED;
2832c2798b19SChris Wilson }
2833c2798b19SChris Wilson 
2834c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2835c2798b19SChris Wilson {
2836c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2837c2798b19SChris Wilson 	int pipe;
2838c2798b19SChris Wilson 
2839c2798b19SChris Wilson 	for_each_pipe(pipe) {
2840c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2841c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2842c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2843c2798b19SChris Wilson 	}
2844c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2845c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2846c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2847c2798b19SChris Wilson }
2848c2798b19SChris Wilson 
2849a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2850a266c7d5SChris Wilson {
2851a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2852a266c7d5SChris Wilson 	int pipe;
2853a266c7d5SChris Wilson 
2854a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2855a266c7d5SChris Wilson 
2856a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2857a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2858a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2859a266c7d5SChris Wilson 	}
2860a266c7d5SChris Wilson 
286100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2862a266c7d5SChris Wilson 	for_each_pipe(pipe)
2863a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2864a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2865a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2866a266c7d5SChris Wilson 	POSTING_READ(IER);
2867a266c7d5SChris Wilson }
2868a266c7d5SChris Wilson 
2869a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2870a266c7d5SChris Wilson {
2871a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
287238bde180SChris Wilson 	u32 enable_mask;
2873379ef82dSDaniel Vetter 	unsigned long irqflags;
2874a266c7d5SChris Wilson 
287538bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
287638bde180SChris Wilson 
287738bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
287838bde180SChris Wilson 	dev_priv->irq_mask =
287938bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
288038bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
288138bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
288238bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
288338bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
288438bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
288538bde180SChris Wilson 
288638bde180SChris Wilson 	enable_mask =
288738bde180SChris Wilson 		I915_ASLE_INTERRUPT |
288838bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
288938bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
289038bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
289138bde180SChris Wilson 		I915_USER_INTERRUPT;
289238bde180SChris Wilson 
2893a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
289420afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
289520afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
289620afbda2SDaniel Vetter 
2897a266c7d5SChris Wilson 		/* Enable in IER... */
2898a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2899a266c7d5SChris Wilson 		/* and unmask in IMR */
2900a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2901a266c7d5SChris Wilson 	}
2902a266c7d5SChris Wilson 
2903a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2904a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2905a266c7d5SChris Wilson 	POSTING_READ(IER);
2906a266c7d5SChris Wilson 
2907f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
290820afbda2SDaniel Vetter 
2909379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2910379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2911379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
29123b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_ENABLE);
29133b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_ENABLE);
2914379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2915379ef82dSDaniel Vetter 
291620afbda2SDaniel Vetter 	return 0;
291720afbda2SDaniel Vetter }
291820afbda2SDaniel Vetter 
291990a72f87SVille Syrjälä /*
292090a72f87SVille Syrjälä  * Returns true when a page flip has completed.
292190a72f87SVille Syrjälä  */
292290a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
292390a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
292490a72f87SVille Syrjälä {
292590a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
292690a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
292790a72f87SVille Syrjälä 
292890a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
292990a72f87SVille Syrjälä 		return false;
293090a72f87SVille Syrjälä 
293190a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
293290a72f87SVille Syrjälä 		return false;
293390a72f87SVille Syrjälä 
293490a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
293590a72f87SVille Syrjälä 
293690a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
293790a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
293890a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
293990a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
294090a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
294190a72f87SVille Syrjälä 	 */
294290a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
294390a72f87SVille Syrjälä 		return false;
294490a72f87SVille Syrjälä 
294590a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
294690a72f87SVille Syrjälä 
294790a72f87SVille Syrjälä 	return true;
294890a72f87SVille Syrjälä }
294990a72f87SVille Syrjälä 
2950ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2951a266c7d5SChris Wilson {
2952a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2953a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29548291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2955a266c7d5SChris Wilson 	unsigned long irqflags;
295638bde180SChris Wilson 	u32 flip_mask =
295738bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
295838bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
295938bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2960a266c7d5SChris Wilson 
2961a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2962a266c7d5SChris Wilson 
2963a266c7d5SChris Wilson 	iir = I915_READ(IIR);
296438bde180SChris Wilson 	do {
296538bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
29668291ee90SChris Wilson 		bool blc_event = false;
2967a266c7d5SChris Wilson 
2968a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2969a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2970a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2971a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2972a266c7d5SChris Wilson 		 */
2973a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2974a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2975a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2976a266c7d5SChris Wilson 
2977a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2978a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2979a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2980a266c7d5SChris Wilson 
298138bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2982a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2983a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2984a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2985a266c7d5SChris Wilson 							 pipe_name(pipe));
2986a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
298738bde180SChris Wilson 				irq_received = true;
2988a266c7d5SChris Wilson 			}
2989a266c7d5SChris Wilson 		}
2990a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2991a266c7d5SChris Wilson 
2992a266c7d5SChris Wilson 		if (!irq_received)
2993a266c7d5SChris Wilson 			break;
2994a266c7d5SChris Wilson 
2995a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2996a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2997a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2998a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2999b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3000a266c7d5SChris Wilson 
3001a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3002a266c7d5SChris Wilson 				  hotplug_status);
300391d131d2SDaniel Vetter 
300410a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
300591d131d2SDaniel Vetter 
3006a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
300738bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3008a266c7d5SChris Wilson 		}
3009a266c7d5SChris Wilson 
301038bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3011a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3012a266c7d5SChris Wilson 
3013a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3014a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3015a266c7d5SChris Wilson 
3016a266c7d5SChris Wilson 		for_each_pipe(pipe) {
301738bde180SChris Wilson 			int plane = pipe;
301838bde180SChris Wilson 			if (IS_MOBILE(dev))
301938bde180SChris Wilson 				plane = !plane;
30205e2032d4SVille Syrjälä 
302190a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
302290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
302390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3024a266c7d5SChris Wilson 
3025a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3026a266c7d5SChris Wilson 				blc_event = true;
30274356d586SDaniel Vetter 
30284356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3029277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
3030a266c7d5SChris Wilson 		}
3031a266c7d5SChris Wilson 
3032a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3033a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3034a266c7d5SChris Wilson 
3035a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3036a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3037a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3038a266c7d5SChris Wilson 		 * we would never get another interrupt.
3039a266c7d5SChris Wilson 		 *
3040a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3041a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3042a266c7d5SChris Wilson 		 * another one.
3043a266c7d5SChris Wilson 		 *
3044a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3045a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3046a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3047a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3048a266c7d5SChris Wilson 		 * stray interrupts.
3049a266c7d5SChris Wilson 		 */
305038bde180SChris Wilson 		ret = IRQ_HANDLED;
3051a266c7d5SChris Wilson 		iir = new_iir;
305238bde180SChris Wilson 	} while (iir & ~flip_mask);
3053a266c7d5SChris Wilson 
3054d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
30558291ee90SChris Wilson 
3056a266c7d5SChris Wilson 	return ret;
3057a266c7d5SChris Wilson }
3058a266c7d5SChris Wilson 
3059a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3060a266c7d5SChris Wilson {
3061a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3062a266c7d5SChris Wilson 	int pipe;
3063a266c7d5SChris Wilson 
3064ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3065ac4c16c5SEgbert Eich 
3066a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3067a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3068a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3069a266c7d5SChris Wilson 	}
3070a266c7d5SChris Wilson 
307100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
307255b39755SChris Wilson 	for_each_pipe(pipe) {
307355b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3074a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
307555b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
307655b39755SChris Wilson 	}
3077a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3078a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3079a266c7d5SChris Wilson 
3080a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3081a266c7d5SChris Wilson }
3082a266c7d5SChris Wilson 
3083a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3084a266c7d5SChris Wilson {
3085a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3086a266c7d5SChris Wilson 	int pipe;
3087a266c7d5SChris Wilson 
3088a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3089a266c7d5SChris Wilson 
3090a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3091a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3092a266c7d5SChris Wilson 
3093a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3094a266c7d5SChris Wilson 	for_each_pipe(pipe)
3095a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3096a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3097a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3098a266c7d5SChris Wilson 	POSTING_READ(IER);
3099a266c7d5SChris Wilson }
3100a266c7d5SChris Wilson 
3101a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3102a266c7d5SChris Wilson {
3103a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3104bbba0a97SChris Wilson 	u32 enable_mask;
3105a266c7d5SChris Wilson 	u32 error_mask;
3106b79480baSDaniel Vetter 	unsigned long irqflags;
3107a266c7d5SChris Wilson 
3108a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3109bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3110adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3111bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3112bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3113bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3114bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3115bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3116bbba0a97SChris Wilson 
3117bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
311821ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
311921ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3120bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3121bbba0a97SChris Wilson 
3122bbba0a97SChris Wilson 	if (IS_G4X(dev))
3123bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3124a266c7d5SChris Wilson 
3125b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3126b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3127b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
31283b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_EVENT_ENABLE);
31293b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_ENABLE);
31303b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_ENABLE);
3131b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3132a266c7d5SChris Wilson 
3133a266c7d5SChris Wilson 	/*
3134a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3135a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3136a266c7d5SChris Wilson 	 */
3137a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3138a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3139a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3140a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3141a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3142a266c7d5SChris Wilson 	} else {
3143a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3144a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3145a266c7d5SChris Wilson 	}
3146a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3147a266c7d5SChris Wilson 
3148a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3149a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3150a266c7d5SChris Wilson 	POSTING_READ(IER);
3151a266c7d5SChris Wilson 
315220afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
315320afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
315420afbda2SDaniel Vetter 
3155f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
315620afbda2SDaniel Vetter 
315720afbda2SDaniel Vetter 	return 0;
315820afbda2SDaniel Vetter }
315920afbda2SDaniel Vetter 
3160bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
316120afbda2SDaniel Vetter {
316220afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3163e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3164cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
316520afbda2SDaniel Vetter 	u32 hotplug_en;
316620afbda2SDaniel Vetter 
3167b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3168b5ea2d56SDaniel Vetter 
3169bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3170bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3171bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3172adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3173e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3174cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3175cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3176cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3177a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3178a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3179a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3180a266c7d5SChris Wilson 		*/
3181a266c7d5SChris Wilson 		if (IS_G4X(dev))
3182a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
318385fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3184a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3185a266c7d5SChris Wilson 
3186a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3187a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3188a266c7d5SChris Wilson 	}
3189bac56d5bSEgbert Eich }
3190a266c7d5SChris Wilson 
3191ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3192a266c7d5SChris Wilson {
3193a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3194a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3195a266c7d5SChris Wilson 	u32 iir, new_iir;
3196a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3197a266c7d5SChris Wilson 	unsigned long irqflags;
3198a266c7d5SChris Wilson 	int irq_received;
3199a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
320021ad8330SVille Syrjälä 	u32 flip_mask =
320121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
320221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3203a266c7d5SChris Wilson 
3204a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3205a266c7d5SChris Wilson 
3206a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3207a266c7d5SChris Wilson 
3208a266c7d5SChris Wilson 	for (;;) {
32092c8ba29fSChris Wilson 		bool blc_event = false;
32102c8ba29fSChris Wilson 
321121ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3212a266c7d5SChris Wilson 
3213a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3214a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3215a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3216a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3217a266c7d5SChris Wilson 		 */
3218a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3219a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3220a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3221a266c7d5SChris Wilson 
3222a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3223a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3224a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3225a266c7d5SChris Wilson 
3226a266c7d5SChris Wilson 			/*
3227a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3228a266c7d5SChris Wilson 			 */
3229a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3230a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3231a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3232a266c7d5SChris Wilson 							 pipe_name(pipe));
3233a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3234a266c7d5SChris Wilson 				irq_received = 1;
3235a266c7d5SChris Wilson 			}
3236a266c7d5SChris Wilson 		}
3237a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3238a266c7d5SChris Wilson 
3239a266c7d5SChris Wilson 		if (!irq_received)
3240a266c7d5SChris Wilson 			break;
3241a266c7d5SChris Wilson 
3242a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3243a266c7d5SChris Wilson 
3244a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3245adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3246a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3247b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3248b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
32494f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3250a266c7d5SChris Wilson 
3251a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3252a266c7d5SChris Wilson 				  hotplug_status);
325391d131d2SDaniel Vetter 
325410a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
325510a504deSDaniel Vetter 					      IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
325691d131d2SDaniel Vetter 
3257a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3258a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3259a266c7d5SChris Wilson 		}
3260a266c7d5SChris Wilson 
326121ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3262a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3263a266c7d5SChris Wilson 
3264a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3265a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3266a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3267a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3268a266c7d5SChris Wilson 
3269a266c7d5SChris Wilson 		for_each_pipe(pipe) {
32702c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
327190a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
327290a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3273a266c7d5SChris Wilson 
3274a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3275a266c7d5SChris Wilson 				blc_event = true;
32764356d586SDaniel Vetter 
32774356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3278277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
3279a266c7d5SChris Wilson 		}
3280a266c7d5SChris Wilson 
3281a266c7d5SChris Wilson 
3282a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3283a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3284a266c7d5SChris Wilson 
3285515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3286515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3287515ac2bbSDaniel Vetter 
3288a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3289a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3290a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3291a266c7d5SChris Wilson 		 * we would never get another interrupt.
3292a266c7d5SChris Wilson 		 *
3293a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3294a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3295a266c7d5SChris Wilson 		 * another one.
3296a266c7d5SChris Wilson 		 *
3297a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3298a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3299a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3300a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3301a266c7d5SChris Wilson 		 * stray interrupts.
3302a266c7d5SChris Wilson 		 */
3303a266c7d5SChris Wilson 		iir = new_iir;
3304a266c7d5SChris Wilson 	}
3305a266c7d5SChris Wilson 
3306d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
33072c8ba29fSChris Wilson 
3308a266c7d5SChris Wilson 	return ret;
3309a266c7d5SChris Wilson }
3310a266c7d5SChris Wilson 
3311a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3312a266c7d5SChris Wilson {
3313a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3314a266c7d5SChris Wilson 	int pipe;
3315a266c7d5SChris Wilson 
3316a266c7d5SChris Wilson 	if (!dev_priv)
3317a266c7d5SChris Wilson 		return;
3318a266c7d5SChris Wilson 
3319ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3320ac4c16c5SEgbert Eich 
3321a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3322a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3323a266c7d5SChris Wilson 
3324a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3325a266c7d5SChris Wilson 	for_each_pipe(pipe)
3326a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3327a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3328a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3329a266c7d5SChris Wilson 
3330a266c7d5SChris Wilson 	for_each_pipe(pipe)
3331a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3332a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3333a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3334a266c7d5SChris Wilson }
3335a266c7d5SChris Wilson 
3336ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3337ac4c16c5SEgbert Eich {
3338ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3339ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3340ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3341ac4c16c5SEgbert Eich 	unsigned long irqflags;
3342ac4c16c5SEgbert Eich 	int i;
3343ac4c16c5SEgbert Eich 
3344ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3345ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3346ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3347ac4c16c5SEgbert Eich 
3348ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3349ac4c16c5SEgbert Eich 			continue;
3350ac4c16c5SEgbert Eich 
3351ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3352ac4c16c5SEgbert Eich 
3353ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3354ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3355ac4c16c5SEgbert Eich 
3356ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3357ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3358ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3359ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3360ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3361ac4c16c5SEgbert Eich 				if (!connector->polled)
3362ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3363ac4c16c5SEgbert Eich 			}
3364ac4c16c5SEgbert Eich 		}
3365ac4c16c5SEgbert Eich 	}
3366ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3367ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3368ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3369ac4c16c5SEgbert Eich }
3370ac4c16c5SEgbert Eich 
3371f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3372f71d4af4SJesse Barnes {
33738b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
33748b2e326dSChris Wilson 
33758b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
337699584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3377c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3378a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
33798b2e326dSChris Wilson 
338099584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
338199584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
338261bac78eSDaniel Vetter 		    (unsigned long) dev);
3383ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3384ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
338561bac78eSDaniel Vetter 
338697a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
33879ee32feaSDaniel Vetter 
33884cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
33894cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
33904cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
33914cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3392f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3393f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3394391f75e2SVille Syrjälä 	} else {
3395391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
3396391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
3397f71d4af4SJesse Barnes 	}
3398f71d4af4SJesse Barnes 
3399c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
3400f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3401f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3402c2baf4b7SVille Syrjälä 	}
3403f71d4af4SJesse Barnes 
34047e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
34057e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
34067e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
34077e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
34087e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
34097e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
34107e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3411fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3412f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3413f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3414f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3415f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3416f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3417f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3418f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
341982a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3420f71d4af4SJesse Barnes 	} else {
3421c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3422c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3423c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3424c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3425c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3426a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3427a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3428a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3429a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3430a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
343120afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3432c2798b19SChris Wilson 		} else {
3433a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3434a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3435a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3436a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3437bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3438c2798b19SChris Wilson 		}
3439f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3440f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3441f71d4af4SJesse Barnes 	}
3442f71d4af4SJesse Barnes }
344320afbda2SDaniel Vetter 
344420afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
344520afbda2SDaniel Vetter {
344620afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3447821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3448821450c6SEgbert Eich 	struct drm_connector *connector;
3449b5ea2d56SDaniel Vetter 	unsigned long irqflags;
3450821450c6SEgbert Eich 	int i;
345120afbda2SDaniel Vetter 
3452821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3453821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3454821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3455821450c6SEgbert Eich 	}
3456821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3457821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3458821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3459821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3460821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3461821450c6SEgbert Eich 	}
3462b5ea2d56SDaniel Vetter 
3463b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3464b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
3465b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
346620afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
346720afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
3468b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
346920afbda2SDaniel Vetter }
3470c67a470bSPaulo Zanoni 
3471c67a470bSPaulo Zanoni /* Disable interrupts so we can allow Package C8+. */
3472c67a470bSPaulo Zanoni void hsw_pc8_disable_interrupts(struct drm_device *dev)
3473c67a470bSPaulo Zanoni {
3474c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3475c67a470bSPaulo Zanoni 	unsigned long irqflags;
3476c67a470bSPaulo Zanoni 
3477c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3478c67a470bSPaulo Zanoni 
3479c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.deimr = I915_READ(DEIMR);
3480c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.sdeimr = I915_READ(SDEIMR);
3481c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtimr = I915_READ(GTIMR);
3482c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtier = I915_READ(GTIER);
3483c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
3484c67a470bSPaulo Zanoni 
3485c67a470bSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, ~DE_PCH_EVENT_IVB);
3486c67a470bSPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, ~SDE_HOTPLUG_MASK_CPT);
3487c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
3488c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
3489c67a470bSPaulo Zanoni 
3490c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = true;
3491c67a470bSPaulo Zanoni 
3492c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3493c67a470bSPaulo Zanoni }
3494c67a470bSPaulo Zanoni 
3495c67a470bSPaulo Zanoni /* Restore interrupts so we can recover from Package C8+. */
3496c67a470bSPaulo Zanoni void hsw_pc8_restore_interrupts(struct drm_device *dev)
3497c67a470bSPaulo Zanoni {
3498c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3499c67a470bSPaulo Zanoni 	unsigned long irqflags;
3500c67a470bSPaulo Zanoni 	uint32_t val, expected;
3501c67a470bSPaulo Zanoni 
3502c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3503c67a470bSPaulo Zanoni 
3504c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
3505c67a470bSPaulo Zanoni 	expected = ~DE_PCH_EVENT_IVB;
3506c67a470bSPaulo Zanoni 	WARN(val != expected, "DEIMR is 0x%08x, not 0x%08x\n", val, expected);
3507c67a470bSPaulo Zanoni 
3508c67a470bSPaulo Zanoni 	val = I915_READ(SDEIMR) & ~SDE_HOTPLUG_MASK_CPT;
3509c67a470bSPaulo Zanoni 	expected = ~SDE_HOTPLUG_MASK_CPT;
3510c67a470bSPaulo Zanoni 	WARN(val != expected, "SDEIMR non-HPD bits are 0x%08x, not 0x%08x\n",
3511c67a470bSPaulo Zanoni 	     val, expected);
3512c67a470bSPaulo Zanoni 
3513c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
3514c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3515c67a470bSPaulo Zanoni 	WARN(val != expected, "GTIMR is 0x%08x, not 0x%08x\n", val, expected);
3516c67a470bSPaulo Zanoni 
3517c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
3518c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3519c67a470bSPaulo Zanoni 	WARN(val != expected, "GEN6_PMIMR is 0x%08x, not 0x%08x\n", val,
3520c67a470bSPaulo Zanoni 	     expected);
3521c67a470bSPaulo Zanoni 
3522c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = false;
3523c67a470bSPaulo Zanoni 
3524c67a470bSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pc8.regsave.deimr);
3525c67a470bSPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv,
3526c67a470bSPaulo Zanoni 				     ~dev_priv->pc8.regsave.sdeimr &
3527c67a470bSPaulo Zanoni 				     ~SDE_HOTPLUG_MASK_CPT);
3528c67a470bSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pc8.regsave.gtimr);
3529c67a470bSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pc8.regsave.gen6_pmimr);
3530c67a470bSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pc8.regsave.gtier);
3531c67a470bSPaulo Zanoni 
3532c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3533c67a470bSPaulo Zanoni }
3534