xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 8bc5e955f4819331ed470b251043bbfd5e2aab71)
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
4457c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int 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
4627c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int 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 
490f898780bSJani Nikula 	i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
491a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
492f898780bSJani Nikula 		i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
4931ec14ad3SChris Wilson 
4941ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
49501c66889SZhao Yakui }
49601c66889SZhao Yakui 
49701c66889SZhao Yakui /**
4980a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
4990a3e67a4SJesse Barnes  * @dev: DRM device
5000a3e67a4SJesse Barnes  * @pipe: pipe to check
5010a3e67a4SJesse Barnes  *
5020a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
5030a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
5040a3e67a4SJesse Barnes  * before reading such registers if unsure.
5050a3e67a4SJesse Barnes  */
5060a3e67a4SJesse Barnes static int
5070a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
5080a3e67a4SJesse Barnes {
5090a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
510702e7a56SPaulo Zanoni 
511a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
512a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
513a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
514a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51571f8ba6bSPaulo Zanoni 
516a01025afSDaniel Vetter 		return intel_crtc->active;
517a01025afSDaniel Vetter 	} else {
518a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
519a01025afSDaniel Vetter 	}
5200a3e67a4SJesse Barnes }
5210a3e67a4SJesse Barnes 
5224cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
5234cdb83ecSVille Syrjälä {
5244cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
5254cdb83ecSVille Syrjälä 	return 0;
5264cdb83ecSVille Syrjälä }
5274cdb83ecSVille Syrjälä 
52842f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
52942f52ef8SKeith Packard  * we use as a pipe index
53042f52ef8SKeith Packard  */
531f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5320a3e67a4SJesse Barnes {
5330a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5340a3e67a4SJesse Barnes 	unsigned long high_frame;
5350a3e67a4SJesse Barnes 	unsigned long low_frame;
536391f75e2SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start;
5370a3e67a4SJesse Barnes 
5380a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
53944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5409db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
5410a3e67a4SJesse Barnes 		return 0;
5420a3e67a4SJesse Barnes 	}
5430a3e67a4SJesse Barnes 
544391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
545391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
546391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
547391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
548391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
549391f75e2SVille Syrjälä 
550391f75e2SVille Syrjälä 		vbl_start = mode->crtc_vblank_start * mode->crtc_htotal;
551391f75e2SVille Syrjälä 	} else {
552391f75e2SVille Syrjälä 		enum transcoder cpu_transcoder =
553391f75e2SVille Syrjälä 			intel_pipe_to_cpu_transcoder(dev_priv, pipe);
554391f75e2SVille Syrjälä 		u32 htotal;
555391f75e2SVille Syrjälä 
556391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
557391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
558391f75e2SVille Syrjälä 
559391f75e2SVille Syrjälä 		vbl_start *= htotal;
560391f75e2SVille Syrjälä 	}
561391f75e2SVille Syrjälä 
5629db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5639db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5645eddb70bSChris Wilson 
5650a3e67a4SJesse Barnes 	/*
5660a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5670a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
5680a3e67a4SJesse Barnes 	 * register.
5690a3e67a4SJesse Barnes 	 */
5700a3e67a4SJesse Barnes 	do {
5715eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
572391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
5735eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
5740a3e67a4SJesse Barnes 	} while (high1 != high2);
5750a3e67a4SJesse Barnes 
5765eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
577391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
5785eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
579391f75e2SVille Syrjälä 
580391f75e2SVille Syrjälä 	/*
581391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
582391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
583391f75e2SVille Syrjälä 	 * counter against vblank start.
584391f75e2SVille Syrjälä 	 */
585391f75e2SVille Syrjälä 	return ((high1 << 8) | low) + (pixel >= vbl_start);
5860a3e67a4SJesse Barnes }
5870a3e67a4SJesse Barnes 
588f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
5899880b7a5SJesse Barnes {
5909880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5919db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
5929880b7a5SJesse Barnes 
5939880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
59444d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5959db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
5969880b7a5SJesse Barnes 		return 0;
5979880b7a5SJesse Barnes 	}
5989880b7a5SJesse Barnes 
5999880b7a5SJesse Barnes 	return I915_READ(reg);
6009880b7a5SJesse Barnes }
6019880b7a5SJesse Barnes 
6027c06b08aSVille Syrjälä static bool intel_pipe_in_vblank(struct drm_device *dev, enum pipe pipe)
60354ddcbd2SVille Syrjälä {
60454ddcbd2SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
60554ddcbd2SVille Syrjälä 	uint32_t status;
60654ddcbd2SVille Syrjälä 
60754ddcbd2SVille Syrjälä 	if (IS_VALLEYVIEW(dev)) {
60854ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
60954ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
61054ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
61154ddcbd2SVille Syrjälä 
61254ddcbd2SVille Syrjälä 		return I915_READ(VLV_ISR) & status;
6137c06b08aSVille Syrjälä 	} else if (IS_GEN2(dev)) {
6147c06b08aSVille Syrjälä 		status = pipe == PIPE_A ?
6157c06b08aSVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
6167c06b08aSVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
6177c06b08aSVille Syrjälä 
6187c06b08aSVille Syrjälä 		return I915_READ16(ISR) & status;
6197c06b08aSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen < 5) {
62054ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
62154ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
62254ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
62354ddcbd2SVille Syrjälä 
62454ddcbd2SVille Syrjälä 		return I915_READ(ISR) & status;
62554ddcbd2SVille Syrjälä 	} else if (INTEL_INFO(dev)->gen < 7) {
62654ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
62754ddcbd2SVille Syrjälä 			DE_PIPEA_VBLANK :
62854ddcbd2SVille Syrjälä 			DE_PIPEB_VBLANK;
62954ddcbd2SVille Syrjälä 
63054ddcbd2SVille Syrjälä 		return I915_READ(DEISR) & status;
63154ddcbd2SVille Syrjälä 	} else {
63254ddcbd2SVille Syrjälä 		switch (pipe) {
63354ddcbd2SVille Syrjälä 		default:
63454ddcbd2SVille Syrjälä 		case PIPE_A:
63554ddcbd2SVille Syrjälä 			status = DE_PIPEA_VBLANK_IVB;
63654ddcbd2SVille Syrjälä 			break;
63754ddcbd2SVille Syrjälä 		case PIPE_B:
63854ddcbd2SVille Syrjälä 			status = DE_PIPEB_VBLANK_IVB;
63954ddcbd2SVille Syrjälä 			break;
64054ddcbd2SVille Syrjälä 		case PIPE_C:
64154ddcbd2SVille Syrjälä 			status = DE_PIPEC_VBLANK_IVB;
64254ddcbd2SVille Syrjälä 			break;
64354ddcbd2SVille Syrjälä 		}
64454ddcbd2SVille Syrjälä 
64554ddcbd2SVille Syrjälä 		return I915_READ(DEISR) & status;
64654ddcbd2SVille Syrjälä 	}
64754ddcbd2SVille Syrjälä }
64854ddcbd2SVille Syrjälä 
649f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
6500af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
6510af7e4dfSMario Kleiner {
652c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
653c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
654c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
655c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
6563aa18df8SVille Syrjälä 	int position;
6570af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
6580af7e4dfSMario Kleiner 	bool in_vbl = true;
6590af7e4dfSMario Kleiner 	int ret = 0;
6600af7e4dfSMario Kleiner 
661c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
6620af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
6639db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6640af7e4dfSMario Kleiner 		return 0;
6650af7e4dfSMario Kleiner 	}
6660af7e4dfSMario Kleiner 
667c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
668c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
669c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
670c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
6710af7e4dfSMario Kleiner 
672c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
673c2baf4b7SVille Syrjälä 
6747c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
6750af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
6760af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
6770af7e4dfSMario Kleiner 		 */
6787c06b08aSVille Syrjälä 		if (IS_GEN2(dev))
6797c06b08aSVille Syrjälä 			position = I915_READ(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
6807c06b08aSVille Syrjälä 		else
6817c06b08aSVille Syrjälä 			position = I915_READ(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
68254ddcbd2SVille Syrjälä 
68354ddcbd2SVille Syrjälä 		/*
68454ddcbd2SVille Syrjälä 		 * The scanline counter increments at the leading edge
68554ddcbd2SVille Syrjälä 		 * of hsync, ie. it completely misses the active portion
68654ddcbd2SVille Syrjälä 		 * of the line. Fix up the counter at both edges of vblank
68754ddcbd2SVille Syrjälä 		 * to get a more accurate picture whether we're in vblank
68854ddcbd2SVille Syrjälä 		 * or not.
68954ddcbd2SVille Syrjälä 		 */
6907c06b08aSVille Syrjälä 		in_vbl = intel_pipe_in_vblank(dev, pipe);
69154ddcbd2SVille Syrjälä 		if ((in_vbl && position == vbl_start - 1) ||
69254ddcbd2SVille Syrjälä 		    (!in_vbl && position == vbl_end - 1))
69354ddcbd2SVille Syrjälä 			position = (position + 1) % vtotal;
6940af7e4dfSMario Kleiner 	} else {
6950af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
6960af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
6970af7e4dfSMario Kleiner 		 * scanout position.
6980af7e4dfSMario Kleiner 		 */
6990af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
7000af7e4dfSMario Kleiner 
7013aa18df8SVille Syrjälä 		/* convert to pixel counts */
7023aa18df8SVille Syrjälä 		vbl_start *= htotal;
7033aa18df8SVille Syrjälä 		vbl_end *= htotal;
7043aa18df8SVille Syrjälä 		vtotal *= htotal;
7053aa18df8SVille Syrjälä 	}
7063aa18df8SVille Syrjälä 
7073aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
7083aa18df8SVille Syrjälä 
7093aa18df8SVille Syrjälä 	/*
7103aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
7113aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
7123aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
7133aa18df8SVille Syrjälä 	 * up since vbl_end.
7143aa18df8SVille Syrjälä 	 */
7153aa18df8SVille Syrjälä 	if (position >= vbl_start)
7163aa18df8SVille Syrjälä 		position -= vbl_end;
7173aa18df8SVille Syrjälä 	else
7183aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
7193aa18df8SVille Syrjälä 
7207c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7213aa18df8SVille Syrjälä 		*vpos = position;
7223aa18df8SVille Syrjälä 		*hpos = 0;
7233aa18df8SVille Syrjälä 	} else {
7240af7e4dfSMario Kleiner 		*vpos = position / htotal;
7250af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
7260af7e4dfSMario Kleiner 	}
7270af7e4dfSMario Kleiner 
7280af7e4dfSMario Kleiner 	/* In vblank? */
7290af7e4dfSMario Kleiner 	if (in_vbl)
7300af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
7310af7e4dfSMario Kleiner 
7320af7e4dfSMario Kleiner 	return ret;
7330af7e4dfSMario Kleiner }
7340af7e4dfSMario Kleiner 
735f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
7360af7e4dfSMario Kleiner 			      int *max_error,
7370af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
7380af7e4dfSMario Kleiner 			      unsigned flags)
7390af7e4dfSMario Kleiner {
7404041b853SChris Wilson 	struct drm_crtc *crtc;
7410af7e4dfSMario Kleiner 
7427eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
7434041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7440af7e4dfSMario Kleiner 		return -EINVAL;
7450af7e4dfSMario Kleiner 	}
7460af7e4dfSMario Kleiner 
7470af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
7484041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
7494041b853SChris Wilson 	if (crtc == NULL) {
7504041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7514041b853SChris Wilson 		return -EINVAL;
7524041b853SChris Wilson 	}
7534041b853SChris Wilson 
7544041b853SChris Wilson 	if (!crtc->enabled) {
7554041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
7564041b853SChris Wilson 		return -EBUSY;
7574041b853SChris Wilson 	}
7580af7e4dfSMario Kleiner 
7590af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
7604041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
7614041b853SChris Wilson 						     vblank_time, flags,
7624041b853SChris Wilson 						     crtc);
7630af7e4dfSMario Kleiner }
7640af7e4dfSMario Kleiner 
76567c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
76667c347ffSJani Nikula 				struct drm_connector *connector)
767321a1b30SEgbert Eich {
768321a1b30SEgbert Eich 	enum drm_connector_status old_status;
769321a1b30SEgbert Eich 
770321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
771321a1b30SEgbert Eich 	old_status = connector->status;
772321a1b30SEgbert Eich 
773321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
77467c347ffSJani Nikula 	if (old_status == connector->status)
77567c347ffSJani Nikula 		return false;
77667c347ffSJani Nikula 
77767c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
778321a1b30SEgbert Eich 		      connector->base.id,
779321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
78067c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
78167c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
78267c347ffSJani Nikula 
78367c347ffSJani Nikula 	return true;
784321a1b30SEgbert Eich }
785321a1b30SEgbert Eich 
7865ca58282SJesse Barnes /*
7875ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
7885ca58282SJesse Barnes  */
789ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
790ac4c16c5SEgbert Eich 
7915ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
7925ca58282SJesse Barnes {
7935ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
7945ca58282SJesse Barnes 						    hotplug_work);
7955ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
796c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
797cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
798cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
799cd569aedSEgbert Eich 	struct drm_connector *connector;
800cd569aedSEgbert Eich 	unsigned long irqflags;
801cd569aedSEgbert Eich 	bool hpd_disabled = false;
802321a1b30SEgbert Eich 	bool changed = false;
803142e2398SEgbert Eich 	u32 hpd_event_bits;
8045ca58282SJesse Barnes 
80552d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
80652d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
80752d7ecedSDaniel Vetter 		return;
80852d7ecedSDaniel Vetter 
809a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
810e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
811e67189abSJesse Barnes 
812cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
813142e2398SEgbert Eich 
814142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
815142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
816cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
817cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
818cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
819cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
820cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
821cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
822cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
823cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
824cd569aedSEgbert Eich 				drm_get_connector_name(connector));
825cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
826cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
827cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
828cd569aedSEgbert Eich 			hpd_disabled = true;
829cd569aedSEgbert Eich 		}
830142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
831142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
832142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
833142e2398SEgbert Eich 		}
834cd569aedSEgbert Eich 	}
835cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
836cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
837cd569aedSEgbert Eich 	  * some connectors */
838ac4c16c5SEgbert Eich 	if (hpd_disabled) {
839cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
840ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
841ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
842ac4c16c5SEgbert Eich 	}
843cd569aedSEgbert Eich 
844cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
845cd569aedSEgbert Eich 
846321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
847321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
848321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
849321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
850cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
851cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
852321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
853321a1b30SEgbert Eich 				changed = true;
854321a1b30SEgbert Eich 		}
855321a1b30SEgbert Eich 	}
85640ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
85740ee3381SKeith Packard 
858321a1b30SEgbert Eich 	if (changed)
859321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
8605ca58282SJesse Barnes }
8615ca58282SJesse Barnes 
862d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
863f97108d1SJesse Barnes {
864f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
865b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
8669270388eSDaniel Vetter 	u8 new_delay;
8679270388eSDaniel Vetter 
868d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
869f97108d1SJesse Barnes 
87073edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
87173edd18fSDaniel Vetter 
87220e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
8739270388eSDaniel Vetter 
8747648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
875b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
876b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
877f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
878f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
879f97108d1SJesse Barnes 
880f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
881b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
88220e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
88320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
88420e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
88520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
886b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
88720e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
88820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
88920e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
89020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
891f97108d1SJesse Barnes 	}
892f97108d1SJesse Barnes 
8937648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
89420e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
895f97108d1SJesse Barnes 
896d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
8979270388eSDaniel Vetter 
898f97108d1SJesse Barnes 	return;
899f97108d1SJesse Barnes }
900f97108d1SJesse Barnes 
901549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
902549f7365SChris Wilson 			struct intel_ring_buffer *ring)
903549f7365SChris Wilson {
904475553deSChris Wilson 	if (ring->obj == NULL)
905475553deSChris Wilson 		return;
906475553deSChris Wilson 
907814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
9089862e600SChris Wilson 
909549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
91010cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
911549f7365SChris Wilson }
912549f7365SChris Wilson 
9134912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
9143b8d8d91SJesse Barnes {
9154912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
916c6a828d3SDaniel Vetter 						    rps.work);
917edbfdb45SPaulo Zanoni 	u32 pm_iir;
918dd75fdc8SChris Wilson 	int new_delay, adj;
9193b8d8d91SJesse Barnes 
92059cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
921c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
922c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
9234848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
924edbfdb45SPaulo Zanoni 	snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
92559cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
9264912d041SBen Widawsky 
92760611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
92860611c13SPaulo Zanoni 	WARN_ON(pm_iir & ~GEN6_PM_RPS_EVENTS);
92960611c13SPaulo Zanoni 
9304848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
9313b8d8d91SJesse Barnes 		return;
9323b8d8d91SJesse Barnes 
9334fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
9347b9e0ae6SChris Wilson 
935dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
9367425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
937dd75fdc8SChris Wilson 		if (adj > 0)
938dd75fdc8SChris Wilson 			adj *= 2;
939dd75fdc8SChris Wilson 		else
940dd75fdc8SChris Wilson 			adj = 1;
941dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
9427425034aSVille Syrjälä 
9437425034aSVille Syrjälä 		/*
9447425034aSVille Syrjälä 		 * For better performance, jump directly
9457425034aSVille Syrjälä 		 * to RPe if we're below it.
9467425034aSVille Syrjälä 		 */
947dd75fdc8SChris Wilson 		if (new_delay < dev_priv->rps.rpe_delay)
9487425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
949dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
950dd75fdc8SChris Wilson 		if (dev_priv->rps.cur_delay > dev_priv->rps.rpe_delay)
951dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.rpe_delay;
952dd75fdc8SChris Wilson 		else
953dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.min_delay;
954dd75fdc8SChris Wilson 		adj = 0;
955dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
956dd75fdc8SChris Wilson 		if (adj < 0)
957dd75fdc8SChris Wilson 			adj *= 2;
958dd75fdc8SChris Wilson 		else
959dd75fdc8SChris Wilson 			adj = -1;
960dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
961dd75fdc8SChris Wilson 	} else { /* unknown event */
962dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay;
963dd75fdc8SChris Wilson 	}
9643b8d8d91SJesse Barnes 
96579249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
96679249636SBen Widawsky 	 * interrupt
96779249636SBen Widawsky 	 */
968dd75fdc8SChris Wilson 	if (new_delay < (int)dev_priv->rps.min_delay)
969dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.min_delay;
970dd75fdc8SChris Wilson 	if (new_delay > (int)dev_priv->rps.max_delay)
971dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.max_delay;
972dd75fdc8SChris Wilson 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_delay;
973dd75fdc8SChris Wilson 
9740a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
9750a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
9760a073b84SJesse Barnes 	else
9774912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
9783b8d8d91SJesse Barnes 
9794fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
9803b8d8d91SJesse Barnes }
9813b8d8d91SJesse Barnes 
982e3689190SBen Widawsky 
983e3689190SBen Widawsky /**
984e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
985e3689190SBen Widawsky  * occurred.
986e3689190SBen Widawsky  * @work: workqueue struct
987e3689190SBen Widawsky  *
988e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
989e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
990e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
991e3689190SBen Widawsky  */
992e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
993e3689190SBen Widawsky {
994e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
995a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
996e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
99735a85ac6SBen Widawsky 	char *parity_event[6];
998e3689190SBen Widawsky 	uint32_t misccpctl;
999e3689190SBen Widawsky 	unsigned long flags;
100035a85ac6SBen Widawsky 	uint8_t slice = 0;
1001e3689190SBen Widawsky 
1002e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1003e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1004e3689190SBen Widawsky 	 * any time we access those registers.
1005e3689190SBen Widawsky 	 */
1006e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1007e3689190SBen Widawsky 
100835a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
100935a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
101035a85ac6SBen Widawsky 		goto out;
101135a85ac6SBen Widawsky 
1012e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1013e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1014e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1015e3689190SBen Widawsky 
101635a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
101735a85ac6SBen Widawsky 		u32 reg;
101835a85ac6SBen Widawsky 
101935a85ac6SBen Widawsky 		slice--;
102035a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
102135a85ac6SBen Widawsky 			break;
102235a85ac6SBen Widawsky 
102335a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
102435a85ac6SBen Widawsky 
102535a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
102635a85ac6SBen Widawsky 
102735a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1028e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1029e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1030e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1031e3689190SBen Widawsky 
103235a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
103335a85ac6SBen Widawsky 		POSTING_READ(reg);
1034e3689190SBen Widawsky 
1035cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1036e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1037e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1038e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
103935a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
104035a85ac6SBen Widawsky 		parity_event[5] = NULL;
1041e3689190SBen Widawsky 
1042e3689190SBen Widawsky 		kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
1043e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1044e3689190SBen Widawsky 
104535a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
104635a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1047e3689190SBen Widawsky 
104835a85ac6SBen Widawsky 		kfree(parity_event[4]);
1049e3689190SBen Widawsky 		kfree(parity_event[3]);
1050e3689190SBen Widawsky 		kfree(parity_event[2]);
1051e3689190SBen Widawsky 		kfree(parity_event[1]);
1052e3689190SBen Widawsky 	}
1053e3689190SBen Widawsky 
105435a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
105535a85ac6SBen Widawsky 
105635a85ac6SBen Widawsky out:
105735a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
105835a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
105935a85ac6SBen Widawsky 	ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
106035a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
106135a85ac6SBen Widawsky 
106235a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
106335a85ac6SBen Widawsky }
106435a85ac6SBen Widawsky 
106535a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1066e3689190SBen Widawsky {
1067e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1068e3689190SBen Widawsky 
1069040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1070e3689190SBen Widawsky 		return;
1071e3689190SBen Widawsky 
1072d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
107335a85ac6SBen Widawsky 	ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1074d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1075e3689190SBen Widawsky 
107635a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
107735a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
107835a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
107935a85ac6SBen Widawsky 
108035a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
108135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
108235a85ac6SBen Widawsky 
1083a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1084e3689190SBen Widawsky }
1085e3689190SBen Widawsky 
1086f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1087f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1088f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1089f1af8fc1SPaulo Zanoni {
1090f1af8fc1SPaulo Zanoni 	if (gt_iir &
1091f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1092f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1093f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1094f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1095f1af8fc1SPaulo Zanoni }
1096f1af8fc1SPaulo Zanoni 
1097e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1098e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1099e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1100e7b4c6b1SDaniel Vetter {
1101e7b4c6b1SDaniel Vetter 
1102cc609d5dSBen Widawsky 	if (gt_iir &
1103cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1104e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1105cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1106e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1107cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1108e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1109e7b4c6b1SDaniel Vetter 
1110cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1111cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1112cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
1113e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
1114e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
1115e7b4c6b1SDaniel Vetter 	}
1116e3689190SBen Widawsky 
111735a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
111835a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1119e7b4c6b1SDaniel Vetter }
1120e7b4c6b1SDaniel Vetter 
1121b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1122b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1123b543fb04SEgbert Eich 
112410a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1125b543fb04SEgbert Eich 					 u32 hotplug_trigger,
1126b543fb04SEgbert Eich 					 const u32 *hpd)
1127b543fb04SEgbert Eich {
1128b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
1129b543fb04SEgbert Eich 	int i;
113010a504deSDaniel Vetter 	bool storm_detected = false;
1131b543fb04SEgbert Eich 
113291d131d2SDaniel Vetter 	if (!hotplug_trigger)
113391d131d2SDaniel Vetter 		return;
113491d131d2SDaniel Vetter 
1135b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1136b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
1137821450c6SEgbert Eich 
1138b8f102e8SEgbert Eich 		WARN(((hpd[i] & hotplug_trigger) &&
1139b8f102e8SEgbert Eich 		      dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED),
1140b8f102e8SEgbert Eich 		     "Received HPD interrupt although disabled\n");
1141b8f102e8SEgbert Eich 
1142b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1143b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1144b543fb04SEgbert Eich 			continue;
1145b543fb04SEgbert Eich 
1146bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1147b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1148b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1149b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1150b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1151b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1152b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1153b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1154b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1155142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1156b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
115710a504deSDaniel Vetter 			storm_detected = true;
1158b543fb04SEgbert Eich 		} else {
1159b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1160b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1161b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1162b543fb04SEgbert Eich 		}
1163b543fb04SEgbert Eich 	}
1164b543fb04SEgbert Eich 
116510a504deSDaniel Vetter 	if (storm_detected)
116610a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1167b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
11685876fa0dSDaniel Vetter 
1169645416f5SDaniel Vetter 	/*
1170645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1171645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1172645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1173645416f5SDaniel Vetter 	 * deadlock.
1174645416f5SDaniel Vetter 	 */
1175645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1176b543fb04SEgbert Eich }
1177b543fb04SEgbert Eich 
1178515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1179515ac2bbSDaniel Vetter {
118028c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
118128c70f16SDaniel Vetter 
118228c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1183515ac2bbSDaniel Vetter }
1184515ac2bbSDaniel Vetter 
1185ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1186ce99c256SDaniel Vetter {
11879ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
11889ee32feaSDaniel Vetter 
11899ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1190ce99c256SDaniel Vetter }
1191ce99c256SDaniel Vetter 
11928bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1193eba94eb9SDaniel Vetter static void display_pipe_crc_update(struct drm_device *dev, enum pipe pipe,
1194eba94eb9SDaniel Vetter 				    uint32_t crc0, uint32_t crc1,
1195eba94eb9SDaniel Vetter 				    uint32_t crc2, uint32_t crc3,
1196*8bc5e955SDaniel Vetter 				    uint32_t crc4)
11978bf1e9f1SShuang He {
11988bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
11998bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
12008bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1201ac2300d4SDamien Lespiau 	int head, tail;
1202b2c88f5bSDamien Lespiau 
12030c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
12040c912c79SDamien Lespiau 		DRM_ERROR("spurious interrupt\n");
12050c912c79SDamien Lespiau 		return;
12060c912c79SDamien Lespiau 	}
12070c912c79SDamien Lespiau 
1208b2c88f5bSDamien Lespiau 	head = atomic_read(&pipe_crc->head);
1209b2c88f5bSDamien Lespiau 	tail = atomic_read(&pipe_crc->tail);
1210b2c88f5bSDamien Lespiau 
1211b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1212b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1213b2c88f5bSDamien Lespiau 		return;
1214b2c88f5bSDamien Lespiau 	}
1215b2c88f5bSDamien Lespiau 
1216b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
12178bf1e9f1SShuang He 
1218*8bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1219eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1220eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1221eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1222eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1223eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1224b2c88f5bSDamien Lespiau 
1225b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1226b2c88f5bSDamien Lespiau 	atomic_set(&pipe_crc->head, head);
122707144428SDamien Lespiau 
122807144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
12298bf1e9f1SShuang He }
1230eba94eb9SDaniel Vetter 
1231eba94eb9SDaniel Vetter static void ivb_pipe_crc_update(struct drm_device *dev, enum pipe pipe)
1232eba94eb9SDaniel Vetter {
1233eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1234eba94eb9SDaniel Vetter 
1235eba94eb9SDaniel Vetter 	display_pipe_crc_update(dev, pipe,
1236eba94eb9SDaniel Vetter 				I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1237eba94eb9SDaniel Vetter 				I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1238eba94eb9SDaniel Vetter 				I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1239eba94eb9SDaniel Vetter 				I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
1240*8bc5e955SDaniel Vetter 				I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1241eba94eb9SDaniel Vetter }
12425b3a856bSDaniel Vetter 
12435b3a856bSDaniel Vetter static void ilk_pipe_crc_update(struct drm_device *dev, enum pipe pipe)
12445b3a856bSDaniel Vetter {
12455b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
12465b3a856bSDaniel Vetter 
12475b3a856bSDaniel Vetter 	display_pipe_crc_update(dev, pipe,
12485b3a856bSDaniel Vetter 				I915_READ(PIPE_CRC_RES_RED_ILK(pipe)),
12495b3a856bSDaniel Vetter 				I915_READ(PIPE_CRC_RES_GREEN_ILK(pipe)),
12505b3a856bSDaniel Vetter 				I915_READ(PIPE_CRC_RES_BLUE_ILK(pipe)),
12515b3a856bSDaniel Vetter 				I915_READ(PIPE_CRC_RES_RES1_ILK(pipe)),
1252*8bc5e955SDaniel Vetter 				I915_READ(PIPE_CRC_RES_RES2_ILK(pipe)));
12535b3a856bSDaniel Vetter }
12548bf1e9f1SShuang He #else
1255f8c168faSDaniel Vetter static inline void ivb_pipe_crc_update(struct drm_device *dev, int pipe) {}
12565b3a856bSDaniel Vetter static inline void ilk_pipe_crc_update(struct drm_device *dev, int pipe) {}
12578bf1e9f1SShuang He #endif
12588bf1e9f1SShuang He 
12591403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
12601403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
12611403c0d4SPaulo Zanoni  * the work queue. */
12621403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1263baf02a1fSBen Widawsky {
126441a05a3aSDaniel Vetter 	if (pm_iir & GEN6_PM_RPS_EVENTS) {
126559cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
12664848405cSBen Widawsky 		dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
12674d3b3d5fSPaulo Zanoni 		snb_disable_pm_irq(dev_priv, pm_iir & GEN6_PM_RPS_EVENTS);
126859cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
12692adbee62SDaniel Vetter 
12702adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
127141a05a3aSDaniel Vetter 	}
1272baf02a1fSBen Widawsky 
12731403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
127412638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
127512638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
127612638c57SBen Widawsky 
127712638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
127812638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
127912638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
128012638c57SBen Widawsky 		}
128112638c57SBen Widawsky 	}
12821403c0d4SPaulo Zanoni }
1283baf02a1fSBen Widawsky 
1284ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
12857e231dbeSJesse Barnes {
12867e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
12877e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
12887e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
12897e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
12907e231dbeSJesse Barnes 	unsigned long irqflags;
12917e231dbeSJesse Barnes 	int pipe;
12927e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
12937e231dbeSJesse Barnes 
12947e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
12957e231dbeSJesse Barnes 
12967e231dbeSJesse Barnes 	while (true) {
12977e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
12987e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
12997e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
13007e231dbeSJesse Barnes 
13017e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
13027e231dbeSJesse Barnes 			goto out;
13037e231dbeSJesse Barnes 
13047e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
13057e231dbeSJesse Barnes 
1306e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
13077e231dbeSJesse Barnes 
13087e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
13097e231dbeSJesse Barnes 		for_each_pipe(pipe) {
13107e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
13117e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
13127e231dbeSJesse Barnes 
13137e231dbeSJesse Barnes 			/*
13147e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
13157e231dbeSJesse Barnes 			 */
13167e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
13177e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
13187e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
13197e231dbeSJesse Barnes 							 pipe_name(pipe));
13207e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
13217e231dbeSJesse Barnes 			}
13227e231dbeSJesse Barnes 		}
13237e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
13247e231dbeSJesse Barnes 
132531acc7f5SJesse Barnes 		for_each_pipe(pipe) {
132631acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
132731acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
132831acc7f5SJesse Barnes 
132931acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
133031acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
133131acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
133231acc7f5SJesse Barnes 			}
133331acc7f5SJesse Barnes 		}
133431acc7f5SJesse Barnes 
13357e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
13367e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
13377e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1338b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
13397e231dbeSJesse Barnes 
13407e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
13417e231dbeSJesse Barnes 					 hotplug_status);
134291d131d2SDaniel Vetter 
134310a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
134491d131d2SDaniel Vetter 
13457e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
13467e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
13477e231dbeSJesse Barnes 		}
13487e231dbeSJesse Barnes 
1349515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1350515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
13517e231dbeSJesse Barnes 
135260611c13SPaulo Zanoni 		if (pm_iir)
1353d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
13547e231dbeSJesse Barnes 
13557e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
13567e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
13577e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
13587e231dbeSJesse Barnes 	}
13597e231dbeSJesse Barnes 
13607e231dbeSJesse Barnes out:
13617e231dbeSJesse Barnes 	return ret;
13627e231dbeSJesse Barnes }
13637e231dbeSJesse Barnes 
136423e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1365776ad806SJesse Barnes {
1366776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13679db4a9c7SJesse Barnes 	int pipe;
1368b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1369776ad806SJesse Barnes 
137010a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
137191d131d2SDaniel Vetter 
1372cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1373cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1374776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1375cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1376cfc33bf7SVille Syrjälä 				 port_name(port));
1377cfc33bf7SVille Syrjälä 	}
1378776ad806SJesse Barnes 
1379ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1380ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1381ce99c256SDaniel Vetter 
1382776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1383515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1384776ad806SJesse Barnes 
1385776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1386776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1387776ad806SJesse Barnes 
1388776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1389776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1390776ad806SJesse Barnes 
1391776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1392776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1393776ad806SJesse Barnes 
13949db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
13959db4a9c7SJesse Barnes 		for_each_pipe(pipe)
13969db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
13979db4a9c7SJesse Barnes 					 pipe_name(pipe),
13989db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1399776ad806SJesse Barnes 
1400776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1401776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1402776ad806SJesse Barnes 
1403776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1404776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1405776ad806SJesse Barnes 
1406776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
14078664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
14088664281bSPaulo Zanoni 							  false))
14098664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
14108664281bSPaulo Zanoni 
14118664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
14128664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
14138664281bSPaulo Zanoni 							  false))
14148664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
14158664281bSPaulo Zanoni }
14168664281bSPaulo Zanoni 
14178664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
14188664281bSPaulo Zanoni {
14198664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
14208664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
14218664281bSPaulo Zanoni 
1422de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1423de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1424de032bf4SPaulo Zanoni 
14258664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_A)
14268664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
14278664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
14288664281bSPaulo Zanoni 
14298664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_B)
14308664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
14318664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
14328664281bSPaulo Zanoni 
14338664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_C)
14348664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
14358664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
14368664281bSPaulo Zanoni 
14378bf1e9f1SShuang He 	if (err_int & ERR_INT_PIPE_CRC_DONE_A)
14388bf1e9f1SShuang He 		ivb_pipe_crc_update(dev, PIPE_A);
14398bf1e9f1SShuang He 
14408bf1e9f1SShuang He 	if (err_int & ERR_INT_PIPE_CRC_DONE_B)
14418bf1e9f1SShuang He 		ivb_pipe_crc_update(dev, PIPE_B);
14428bf1e9f1SShuang He 
14438bf1e9f1SShuang He 	if (err_int & ERR_INT_PIPE_CRC_DONE_C)
14448bf1e9f1SShuang He 		ivb_pipe_crc_update(dev, PIPE_C);
14458bf1e9f1SShuang He 
14468664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
14478664281bSPaulo Zanoni }
14488664281bSPaulo Zanoni 
14498664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
14508664281bSPaulo Zanoni {
14518664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
14528664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
14538664281bSPaulo Zanoni 
1454de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1455de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1456de032bf4SPaulo Zanoni 
14578664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
14588664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
14598664281bSPaulo Zanoni 							  false))
14608664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
14618664281bSPaulo Zanoni 
14628664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
14638664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
14648664281bSPaulo Zanoni 							  false))
14658664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
14668664281bSPaulo Zanoni 
14678664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
14688664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
14698664281bSPaulo Zanoni 							  false))
14708664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
14718664281bSPaulo Zanoni 
14728664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1473776ad806SJesse Barnes }
1474776ad806SJesse Barnes 
147523e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
147623e81d69SAdam Jackson {
147723e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
147823e81d69SAdam Jackson 	int pipe;
1479b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
148023e81d69SAdam Jackson 
148110a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
148291d131d2SDaniel Vetter 
1483cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1484cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
148523e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1486cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1487cfc33bf7SVille Syrjälä 				 port_name(port));
1488cfc33bf7SVille Syrjälä 	}
148923e81d69SAdam Jackson 
149023e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1491ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
149223e81d69SAdam Jackson 
149323e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1494515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
149523e81d69SAdam Jackson 
149623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
149723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
149823e81d69SAdam Jackson 
149923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
150023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
150123e81d69SAdam Jackson 
150223e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
150323e81d69SAdam Jackson 		for_each_pipe(pipe)
150423e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
150523e81d69SAdam Jackson 					 pipe_name(pipe),
150623e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
15078664281bSPaulo Zanoni 
15088664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
15098664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
151023e81d69SAdam Jackson }
151123e81d69SAdam Jackson 
1512c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1513c008bc6eSPaulo Zanoni {
1514c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1515c008bc6eSPaulo Zanoni 
1516c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1517c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1518c008bc6eSPaulo Zanoni 
1519c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1520c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1521c008bc6eSPaulo Zanoni 
1522c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEA_VBLANK)
1523c008bc6eSPaulo Zanoni 		drm_handle_vblank(dev, 0);
1524c008bc6eSPaulo Zanoni 
1525c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEB_VBLANK)
1526c008bc6eSPaulo Zanoni 		drm_handle_vblank(dev, 1);
1527c008bc6eSPaulo Zanoni 
1528c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1529c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1530c008bc6eSPaulo Zanoni 
1531c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
1532c008bc6eSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1533c008bc6eSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1534c008bc6eSPaulo Zanoni 
1535c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
1536c008bc6eSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1537c008bc6eSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1538c008bc6eSPaulo Zanoni 
15395b3a856bSDaniel Vetter 	if (de_iir & DE_PIPEA_CRC_DONE)
15405b3a856bSDaniel Vetter 		ilk_pipe_crc_update(dev, PIPE_A);
15415b3a856bSDaniel Vetter 
15425b3a856bSDaniel Vetter 	if (de_iir & DE_PIPEB_CRC_DONE)
15435b3a856bSDaniel Vetter 		ilk_pipe_crc_update(dev, PIPE_B);
15445b3a856bSDaniel Vetter 
1545c008bc6eSPaulo Zanoni 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1546c008bc6eSPaulo Zanoni 		intel_prepare_page_flip(dev, 0);
1547c008bc6eSPaulo Zanoni 		intel_finish_page_flip_plane(dev, 0);
1548c008bc6eSPaulo Zanoni 	}
1549c008bc6eSPaulo Zanoni 
1550c008bc6eSPaulo Zanoni 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1551c008bc6eSPaulo Zanoni 		intel_prepare_page_flip(dev, 1);
1552c008bc6eSPaulo Zanoni 		intel_finish_page_flip_plane(dev, 1);
1553c008bc6eSPaulo Zanoni 	}
1554c008bc6eSPaulo Zanoni 
1555c008bc6eSPaulo Zanoni 	/* check event from PCH */
1556c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1557c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1558c008bc6eSPaulo Zanoni 
1559c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1560c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1561c008bc6eSPaulo Zanoni 		else
1562c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1563c008bc6eSPaulo Zanoni 
1564c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1565c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1566c008bc6eSPaulo Zanoni 	}
1567c008bc6eSPaulo Zanoni 
1568c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1569c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1570c008bc6eSPaulo Zanoni }
1571c008bc6eSPaulo Zanoni 
15729719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
15739719fb98SPaulo Zanoni {
15749719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
15759719fb98SPaulo Zanoni 	int i;
15769719fb98SPaulo Zanoni 
15779719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
15789719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
15799719fb98SPaulo Zanoni 
15809719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
15819719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
15829719fb98SPaulo Zanoni 
15839719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
15849719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
15859719fb98SPaulo Zanoni 
15869719fb98SPaulo Zanoni 	for (i = 0; i < 3; i++) {
15879719fb98SPaulo Zanoni 		if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
15889719fb98SPaulo Zanoni 			drm_handle_vblank(dev, i);
15899719fb98SPaulo Zanoni 		if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
15909719fb98SPaulo Zanoni 			intel_prepare_page_flip(dev, i);
15919719fb98SPaulo Zanoni 			intel_finish_page_flip_plane(dev, i);
15929719fb98SPaulo Zanoni 		}
15939719fb98SPaulo Zanoni 	}
15949719fb98SPaulo Zanoni 
15959719fb98SPaulo Zanoni 	/* check event from PCH */
15969719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
15979719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
15989719fb98SPaulo Zanoni 
15999719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
16009719fb98SPaulo Zanoni 
16019719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
16029719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
16039719fb98SPaulo Zanoni 	}
16049719fb98SPaulo Zanoni }
16059719fb98SPaulo Zanoni 
1606f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1607b1f14ad0SJesse Barnes {
1608b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1609b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1610f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
16110e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1612b1f14ad0SJesse Barnes 
1613b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1614b1f14ad0SJesse Barnes 
16158664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
16168664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1617907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
16188664281bSPaulo Zanoni 
1619b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1620b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1621b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
162223a78516SPaulo Zanoni 	POSTING_READ(DEIER);
16230e43406bSChris Wilson 
162444498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
162544498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
162644498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
162744498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
162844498aeaSPaulo Zanoni 	 * due to its back queue). */
1629ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
163044498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
163144498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
163244498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1633ab5c608bSBen Widawsky 	}
163444498aeaSPaulo Zanoni 
16350e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
16360e43406bSChris Wilson 	if (gt_iir) {
1637d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
16380e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1639d8fc8a47SPaulo Zanoni 		else
1640d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
16410e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
16420e43406bSChris Wilson 		ret = IRQ_HANDLED;
16430e43406bSChris Wilson 	}
1644b1f14ad0SJesse Barnes 
1645b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
16460e43406bSChris Wilson 	if (de_iir) {
1647f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
16489719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1649f1af8fc1SPaulo Zanoni 		else
1650f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
16510e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
16520e43406bSChris Wilson 		ret = IRQ_HANDLED;
16530e43406bSChris Wilson 	}
16540e43406bSChris Wilson 
1655f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
1656f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
16570e43406bSChris Wilson 		if (pm_iir) {
1658d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
1659b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
16600e43406bSChris Wilson 			ret = IRQ_HANDLED;
16610e43406bSChris Wilson 		}
1662f1af8fc1SPaulo Zanoni 	}
1663b1f14ad0SJesse Barnes 
1664b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1665b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1666ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
166744498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
166844498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1669ab5c608bSBen Widawsky 	}
1670b1f14ad0SJesse Barnes 
1671b1f14ad0SJesse Barnes 	return ret;
1672b1f14ad0SJesse Barnes }
1673b1f14ad0SJesse Barnes 
167417e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
167517e1df07SDaniel Vetter 			       bool reset_completed)
167617e1df07SDaniel Vetter {
167717e1df07SDaniel Vetter 	struct intel_ring_buffer *ring;
167817e1df07SDaniel Vetter 	int i;
167917e1df07SDaniel Vetter 
168017e1df07SDaniel Vetter 	/*
168117e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
168217e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
168317e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
168417e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
168517e1df07SDaniel Vetter 	 */
168617e1df07SDaniel Vetter 
168717e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
168817e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
168917e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
169017e1df07SDaniel Vetter 
169117e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
169217e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
169317e1df07SDaniel Vetter 
169417e1df07SDaniel Vetter 	/*
169517e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
169617e1df07SDaniel Vetter 	 * reset state is cleared.
169717e1df07SDaniel Vetter 	 */
169817e1df07SDaniel Vetter 	if (reset_completed)
169917e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
170017e1df07SDaniel Vetter }
170117e1df07SDaniel Vetter 
17028a905236SJesse Barnes /**
17038a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
17048a905236SJesse Barnes  * @work: work struct
17058a905236SJesse Barnes  *
17068a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
17078a905236SJesse Barnes  * was detected.
17088a905236SJesse Barnes  */
17098a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
17108a905236SJesse Barnes {
17111f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
17121f83fee0SDaniel Vetter 						    work);
17131f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
17141f83fee0SDaniel Vetter 						    gpu_error);
17158a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1716cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1717cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1718cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
171917e1df07SDaniel Vetter 	int ret;
17208a905236SJesse Barnes 
1721f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
17228a905236SJesse Barnes 
17237db0ba24SDaniel Vetter 	/*
17247db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
17257db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
17267db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
17277db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
17287db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
17297db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
17307db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
17317db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
17327db0ba24SDaniel Vetter 	 */
17337db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
173444d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
17357db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
17367db0ba24SDaniel Vetter 				   reset_event);
17371f83fee0SDaniel Vetter 
173817e1df07SDaniel Vetter 		/*
173917e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
174017e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
174117e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
174217e1df07SDaniel Vetter 		 * deadlocks with the reset work.
174317e1df07SDaniel Vetter 		 */
1744f69061beSDaniel Vetter 		ret = i915_reset(dev);
1745f69061beSDaniel Vetter 
174617e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
174717e1df07SDaniel Vetter 
1748f69061beSDaniel Vetter 		if (ret == 0) {
1749f69061beSDaniel Vetter 			/*
1750f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1751f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1752f69061beSDaniel Vetter 			 * complete.
1753f69061beSDaniel Vetter 			 *
1754f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1755f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1756f69061beSDaniel Vetter 			 * updates before
1757f69061beSDaniel Vetter 			 * the counter increment.
1758f69061beSDaniel Vetter 			 */
1759f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1760f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1761f69061beSDaniel Vetter 
1762f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1763f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
17641f83fee0SDaniel Vetter 		} else {
17651f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1766f316a42cSBen Gamari 		}
17671f83fee0SDaniel Vetter 
176817e1df07SDaniel Vetter 		/*
176917e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
177017e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
177117e1df07SDaniel Vetter 		 */
177217e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
1773f316a42cSBen Gamari 	}
17748a905236SJesse Barnes }
17758a905236SJesse Barnes 
177635aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1777c0e09200SDave Airlie {
17788a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1779bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
178063eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1781050ee91fSBen Widawsky 	int pipe, i;
178263eeaf38SJesse Barnes 
178335aed2e6SChris Wilson 	if (!eir)
178435aed2e6SChris Wilson 		return;
178563eeaf38SJesse Barnes 
1786a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
17878a905236SJesse Barnes 
1788bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1789bd9854f9SBen Widawsky 
17908a905236SJesse Barnes 	if (IS_G4X(dev)) {
17918a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
17928a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
17938a905236SJesse Barnes 
1794a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1795a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1796050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1797050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1798a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1799a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
18008a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
18013143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
18028a905236SJesse Barnes 		}
18038a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
18048a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1805a70491ccSJoe Perches 			pr_err("page table error\n");
1806a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
18078a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
18083143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
18098a905236SJesse Barnes 		}
18108a905236SJesse Barnes 	}
18118a905236SJesse Barnes 
1812a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
181363eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
181463eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1815a70491ccSJoe Perches 			pr_err("page table error\n");
1816a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
181763eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
18183143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
181963eeaf38SJesse Barnes 		}
18208a905236SJesse Barnes 	}
18218a905236SJesse Barnes 
182263eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1823a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
18249db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1825a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
18269db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
182763eeaf38SJesse Barnes 		/* pipestat has already been acked */
182863eeaf38SJesse Barnes 	}
182963eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1830a70491ccSJoe Perches 		pr_err("instruction error\n");
1831a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1832050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1833050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1834a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
183563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
183663eeaf38SJesse Barnes 
1837a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1838a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1839a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
184063eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
18413143a2bfSChris Wilson 			POSTING_READ(IPEIR);
184263eeaf38SJesse Barnes 		} else {
184363eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
184463eeaf38SJesse Barnes 
1845a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1846a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1847a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1848a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
184963eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
18503143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
185163eeaf38SJesse Barnes 		}
185263eeaf38SJesse Barnes 	}
185363eeaf38SJesse Barnes 
185463eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
18553143a2bfSChris Wilson 	POSTING_READ(EIR);
185663eeaf38SJesse Barnes 	eir = I915_READ(EIR);
185763eeaf38SJesse Barnes 	if (eir) {
185863eeaf38SJesse Barnes 		/*
185963eeaf38SJesse Barnes 		 * some errors might have become stuck,
186063eeaf38SJesse Barnes 		 * mask them.
186163eeaf38SJesse Barnes 		 */
186263eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
186363eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
186463eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
186563eeaf38SJesse Barnes 	}
186635aed2e6SChris Wilson }
186735aed2e6SChris Wilson 
186835aed2e6SChris Wilson /**
186935aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
187035aed2e6SChris Wilson  * @dev: drm device
187135aed2e6SChris Wilson  *
187235aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
187335aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
187435aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
187535aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
187635aed2e6SChris Wilson  * of a ring dump etc.).
187735aed2e6SChris Wilson  */
1878527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
187935aed2e6SChris Wilson {
188035aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
188135aed2e6SChris Wilson 
188235aed2e6SChris Wilson 	i915_capture_error_state(dev);
188335aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
18848a905236SJesse Barnes 
1885ba1234d1SBen Gamari 	if (wedged) {
1886f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1887f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
1888ba1234d1SBen Gamari 
188911ed50ecSBen Gamari 		/*
189017e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
189117e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
189217e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
189317e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
189417e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
189517e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
189617e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
189717e1df07SDaniel Vetter 		 *
189817e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
189917e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
190017e1df07SDaniel Vetter 		 * counter atomic_t.
190111ed50ecSBen Gamari 		 */
190217e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
190311ed50ecSBen Gamari 	}
190411ed50ecSBen Gamari 
1905122f46baSDaniel Vetter 	/*
1906122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
1907122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
1908122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
1909122f46baSDaniel Vetter 	 * code will deadlock.
1910122f46baSDaniel Vetter 	 */
1911122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
19128a905236SJesse Barnes }
19138a905236SJesse Barnes 
191421ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
19154e5359cdSSimon Farnsworth {
19164e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
19174e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
19184e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
191905394f39SChris Wilson 	struct drm_i915_gem_object *obj;
19204e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
19214e5359cdSSimon Farnsworth 	unsigned long flags;
19224e5359cdSSimon Farnsworth 	bool stall_detected;
19234e5359cdSSimon Farnsworth 
19244e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
19254e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
19264e5359cdSSimon Farnsworth 		return;
19274e5359cdSSimon Farnsworth 
19284e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
19294e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
19304e5359cdSSimon Farnsworth 
1931e7d841caSChris Wilson 	if (work == NULL ||
1932e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1933e7d841caSChris Wilson 	    !work->enable_stall_check) {
19344e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
19354e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
19364e5359cdSSimon Farnsworth 		return;
19374e5359cdSSimon Farnsworth 	}
19384e5359cdSSimon Farnsworth 
19394e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
194005394f39SChris Wilson 	obj = work->pending_flip_obj;
1941a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
19429db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1943446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1944f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
19454e5359cdSSimon Farnsworth 	} else {
19469db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
1947f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
194801f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
19494e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
19504e5359cdSSimon Farnsworth 	}
19514e5359cdSSimon Farnsworth 
19524e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
19534e5359cdSSimon Farnsworth 
19544e5359cdSSimon Farnsworth 	if (stall_detected) {
19554e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
19564e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
19574e5359cdSSimon Farnsworth 	}
19584e5359cdSSimon Farnsworth }
19594e5359cdSSimon Farnsworth 
196042f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
196142f52ef8SKeith Packard  * we use as a pipe index
196242f52ef8SKeith Packard  */
1963f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
19640a3e67a4SJesse Barnes {
19650a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1966e9d21d7fSKeith Packard 	unsigned long irqflags;
196771e0ffa5SJesse Barnes 
19685eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
196971e0ffa5SJesse Barnes 		return -EINVAL;
19700a3e67a4SJesse Barnes 
19711ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1972f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
19737c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
19747c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
19750a3e67a4SJesse Barnes 	else
19767c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
19777c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
19788692d00eSChris Wilson 
19798692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
19808692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
19816b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
19821ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
19838692d00eSChris Wilson 
19840a3e67a4SJesse Barnes 	return 0;
19850a3e67a4SJesse Barnes }
19860a3e67a4SJesse Barnes 
1987f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1988f796cf8fSJesse Barnes {
1989f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1990f796cf8fSJesse Barnes 	unsigned long irqflags;
1991b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
1992b518421fSPaulo Zanoni 						     DE_PIPE_VBLANK_ILK(pipe);
1993f796cf8fSJesse Barnes 
1994f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1995f796cf8fSJesse Barnes 		return -EINVAL;
1996f796cf8fSJesse Barnes 
1997f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1998b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
1999b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2000b1f14ad0SJesse Barnes 
2001b1f14ad0SJesse Barnes 	return 0;
2002b1f14ad0SJesse Barnes }
2003b1f14ad0SJesse Barnes 
20047e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
20057e231dbeSJesse Barnes {
20067e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20077e231dbeSJesse Barnes 	unsigned long irqflags;
200831acc7f5SJesse Barnes 	u32 imr;
20097e231dbeSJesse Barnes 
20107e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
20117e231dbeSJesse Barnes 		return -EINVAL;
20127e231dbeSJesse Barnes 
20137e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
20147e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
201531acc7f5SJesse Barnes 	if (pipe == 0)
20167e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
201731acc7f5SJesse Barnes 	else
20187e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
20197e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
202031acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
202131acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
20227e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
20237e231dbeSJesse Barnes 
20247e231dbeSJesse Barnes 	return 0;
20257e231dbeSJesse Barnes }
20267e231dbeSJesse Barnes 
202742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
202842f52ef8SKeith Packard  * we use as a pipe index
202942f52ef8SKeith Packard  */
2030f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
20310a3e67a4SJesse Barnes {
20320a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2033e9d21d7fSKeith Packard 	unsigned long irqflags;
20340a3e67a4SJesse Barnes 
20351ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
20368692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
20376b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
20388692d00eSChris Wilson 
20397c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
20407c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
20417c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
20421ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
20430a3e67a4SJesse Barnes }
20440a3e67a4SJesse Barnes 
2045f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2046f796cf8fSJesse Barnes {
2047f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2048f796cf8fSJesse Barnes 	unsigned long irqflags;
2049b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
2050b518421fSPaulo Zanoni 						     DE_PIPE_VBLANK_ILK(pipe);
2051f796cf8fSJesse Barnes 
2052f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2053b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2054b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2055b1f14ad0SJesse Barnes }
2056b1f14ad0SJesse Barnes 
20577e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
20587e231dbeSJesse Barnes {
20597e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20607e231dbeSJesse Barnes 	unsigned long irqflags;
206131acc7f5SJesse Barnes 	u32 imr;
20627e231dbeSJesse Barnes 
20637e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
206431acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
206531acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
20667e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
206731acc7f5SJesse Barnes 	if (pipe == 0)
20687e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
206931acc7f5SJesse Barnes 	else
20707e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
20717e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
20727e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
20737e231dbeSJesse Barnes }
20747e231dbeSJesse Barnes 
2075893eead0SChris Wilson static u32
2076893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2077852835f3SZou Nan hai {
2078893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2079893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2080893eead0SChris Wilson }
2081893eead0SChris Wilson 
20829107e9d2SChris Wilson static bool
20839107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2084893eead0SChris Wilson {
20859107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
20869107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2087f65d9421SBen Gamari }
2088f65d9421SBen Gamari 
20896274f212SChris Wilson static struct intel_ring_buffer *
20906274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2091a24a11e6SChris Wilson {
2092a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
20936274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2094a24a11e6SChris Wilson 
2095a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2096a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2097a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
20986274f212SChris Wilson 		return NULL;
2099a24a11e6SChris Wilson 
2100a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2101a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2102a24a11e6SChris Wilson 	 */
21036274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2104a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2105a24a11e6SChris Wilson 	do {
2106a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2107a24a11e6SChris Wilson 		if (cmd == ipehr)
2108a24a11e6SChris Wilson 			break;
2109a24a11e6SChris Wilson 
2110a24a11e6SChris Wilson 		acthd -= 4;
2111a24a11e6SChris Wilson 		if (acthd < acthd_min)
21126274f212SChris Wilson 			return NULL;
2113a24a11e6SChris Wilson 	} while (1);
2114a24a11e6SChris Wilson 
21156274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
21166274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2117a24a11e6SChris Wilson }
2118a24a11e6SChris Wilson 
21196274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
21206274f212SChris Wilson {
21216274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
21226274f212SChris Wilson 	struct intel_ring_buffer *signaller;
21236274f212SChris Wilson 	u32 seqno, ctl;
21246274f212SChris Wilson 
21256274f212SChris Wilson 	ring->hangcheck.deadlock = true;
21266274f212SChris Wilson 
21276274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
21286274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
21296274f212SChris Wilson 		return -1;
21306274f212SChris Wilson 
21316274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
21326274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
21336274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
21346274f212SChris Wilson 		return -1;
21356274f212SChris Wilson 
21366274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
21376274f212SChris Wilson }
21386274f212SChris Wilson 
21396274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
21406274f212SChris Wilson {
21416274f212SChris Wilson 	struct intel_ring_buffer *ring;
21426274f212SChris Wilson 	int i;
21436274f212SChris Wilson 
21446274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
21456274f212SChris Wilson 		ring->hangcheck.deadlock = false;
21466274f212SChris Wilson }
21476274f212SChris Wilson 
2148ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2149ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
21501ec14ad3SChris Wilson {
21511ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
21521ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
21539107e9d2SChris Wilson 	u32 tmp;
21549107e9d2SChris Wilson 
21556274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2156f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
21576274f212SChris Wilson 
21589107e9d2SChris Wilson 	if (IS_GEN2(dev))
2159f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
21609107e9d2SChris Wilson 
21619107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
21629107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
21639107e9d2SChris Wilson 	 * and break the hang. This should work on
21649107e9d2SChris Wilson 	 * all but the second generation chipsets.
21659107e9d2SChris Wilson 	 */
21669107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
21671ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
21681ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
21691ec14ad3SChris Wilson 			  ring->name);
217009e14bf3SChris Wilson 		i915_handle_error(dev, false);
21711ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2172f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
21731ec14ad3SChris Wilson 	}
2174a24a11e6SChris Wilson 
21756274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
21766274f212SChris Wilson 		switch (semaphore_passed(ring)) {
21776274f212SChris Wilson 		default:
2178f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
21796274f212SChris Wilson 		case 1:
2180a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
2181a24a11e6SChris Wilson 				  ring->name);
218209e14bf3SChris Wilson 			i915_handle_error(dev, false);
2183a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2184f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
21856274f212SChris Wilson 		case 0:
2186f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
21876274f212SChris Wilson 		}
21889107e9d2SChris Wilson 	}
21899107e9d2SChris Wilson 
2190f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2191a24a11e6SChris Wilson }
2192d1e61e7fSChris Wilson 
2193f65d9421SBen Gamari /**
2194f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
219505407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
219605407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
219705407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
219805407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
219905407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2200f65d9421SBen Gamari  */
2201a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2202f65d9421SBen Gamari {
2203f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2204f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2205b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2206b4519513SChris Wilson 	int i;
220705407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
22089107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
22099107e9d2SChris Wilson #define BUSY 1
22109107e9d2SChris Wilson #define KICK 5
22119107e9d2SChris Wilson #define HUNG 20
22129107e9d2SChris Wilson #define FIRE 30
2213893eead0SChris Wilson 
22143e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
22153e0dc6b0SBen Widawsky 		return;
22163e0dc6b0SBen Widawsky 
2217b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
221805407ff8SMika Kuoppala 		u32 seqno, acthd;
22199107e9d2SChris Wilson 		bool busy = true;
2220b4519513SChris Wilson 
22216274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
22226274f212SChris Wilson 
222305407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
222405407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
222505407ff8SMika Kuoppala 
222605407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
22279107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2228da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2229da661464SMika Kuoppala 
22309107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
22319107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2232094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
22339107e9d2SChris Wilson 						DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
22349107e9d2SChris Wilson 							  ring->name);
22359107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2236094f9a54SChris Wilson 					}
2237094f9a54SChris Wilson 					/* Safeguard against driver failure */
2238094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
22399107e9d2SChris Wilson 				} else
22409107e9d2SChris Wilson 					busy = false;
224105407ff8SMika Kuoppala 			} else {
22426274f212SChris Wilson 				/* We always increment the hangcheck score
22436274f212SChris Wilson 				 * if the ring is busy and still processing
22446274f212SChris Wilson 				 * the same request, so that no single request
22456274f212SChris Wilson 				 * can run indefinitely (such as a chain of
22466274f212SChris Wilson 				 * batches). The only time we do not increment
22476274f212SChris Wilson 				 * the hangcheck score on this ring, if this
22486274f212SChris Wilson 				 * ring is in a legitimate wait for another
22496274f212SChris Wilson 				 * ring. In that case the waiting ring is a
22506274f212SChris Wilson 				 * victim and we want to be sure we catch the
22516274f212SChris Wilson 				 * right culprit. Then every time we do kick
22526274f212SChris Wilson 				 * the ring, add a small increment to the
22536274f212SChris Wilson 				 * score so that we can catch a batch that is
22546274f212SChris Wilson 				 * being repeatedly kicked and so responsible
22556274f212SChris Wilson 				 * for stalling the machine.
22569107e9d2SChris Wilson 				 */
2257ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2258ad8beaeaSMika Kuoppala 								    acthd);
2259ad8beaeaSMika Kuoppala 
2260ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2261da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2262f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
22636274f212SChris Wilson 					break;
2264f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2265ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
22666274f212SChris Wilson 					break;
2267f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2268ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
22696274f212SChris Wilson 					break;
2270f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2271ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
22726274f212SChris Wilson 					stuck[i] = true;
22736274f212SChris Wilson 					break;
22746274f212SChris Wilson 				}
227505407ff8SMika Kuoppala 			}
22769107e9d2SChris Wilson 		} else {
2277da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2278da661464SMika Kuoppala 
22799107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
22809107e9d2SChris Wilson 			 * attempts across multiple batches.
22819107e9d2SChris Wilson 			 */
22829107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
22839107e9d2SChris Wilson 				ring->hangcheck.score--;
2284cbb465e7SChris Wilson 		}
2285f65d9421SBen Gamari 
228605407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
228705407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
22889107e9d2SChris Wilson 		busy_count += busy;
228905407ff8SMika Kuoppala 	}
229005407ff8SMika Kuoppala 
229105407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
22929107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2293b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
229405407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2295a43adf07SChris Wilson 				 ring->name);
2296a43adf07SChris Wilson 			rings_hung++;
229705407ff8SMika Kuoppala 		}
229805407ff8SMika Kuoppala 	}
229905407ff8SMika Kuoppala 
230005407ff8SMika Kuoppala 	if (rings_hung)
230105407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
230205407ff8SMika Kuoppala 
230305407ff8SMika Kuoppala 	if (busy_count)
230405407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
230505407ff8SMika Kuoppala 		 * being added */
230610cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
230710cd45b6SMika Kuoppala }
230810cd45b6SMika Kuoppala 
230910cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
231010cd45b6SMika Kuoppala {
231110cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
231210cd45b6SMika Kuoppala 	if (!i915_enable_hangcheck)
231310cd45b6SMika Kuoppala 		return;
231410cd45b6SMika Kuoppala 
231599584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
231610cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2317f65d9421SBen Gamari }
2318f65d9421SBen Gamari 
231991738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
232091738a95SPaulo Zanoni {
232191738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
232291738a95SPaulo Zanoni 
232391738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
232491738a95SPaulo Zanoni 		return;
232591738a95SPaulo Zanoni 
232691738a95SPaulo Zanoni 	/* south display irq */
232791738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
232891738a95SPaulo Zanoni 	/*
232991738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
233091738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
233191738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
233291738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
233391738a95SPaulo Zanoni 	 */
233491738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
233591738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
233691738a95SPaulo Zanoni }
233791738a95SPaulo Zanoni 
2338d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2339d18ea1b5SDaniel Vetter {
2340d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2341d18ea1b5SDaniel Vetter 
2342d18ea1b5SDaniel Vetter 	/* and GT */
2343d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2344d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2345d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2346d18ea1b5SDaniel Vetter 
2347d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2348d18ea1b5SDaniel Vetter 		/* and PM */
2349d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2350d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2351d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2352d18ea1b5SDaniel Vetter 	}
2353d18ea1b5SDaniel Vetter }
2354d18ea1b5SDaniel Vetter 
2355c0e09200SDave Airlie /* drm_dma.h hooks
2356c0e09200SDave Airlie */
2357f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2358036a4a7dSZhenyu Wang {
2359036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2360036a4a7dSZhenyu Wang 
23614697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
23624697995bSJesse Barnes 
2363036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2364bdfcdb63SDaniel Vetter 
2365036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2366036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
23673143a2bfSChris Wilson 	POSTING_READ(DEIER);
2368036a4a7dSZhenyu Wang 
2369d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2370c650156aSZhenyu Wang 
237191738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
23727d99163dSBen Widawsky }
23737d99163dSBen Widawsky 
23747e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
23757e231dbeSJesse Barnes {
23767e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23777e231dbeSJesse Barnes 	int pipe;
23787e231dbeSJesse Barnes 
23797e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
23807e231dbeSJesse Barnes 
23817e231dbeSJesse Barnes 	/* VLV magic */
23827e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
23837e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
23847e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
23857e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
23867e231dbeSJesse Barnes 
23877e231dbeSJesse Barnes 	/* and GT */
23887e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
23897e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2390d18ea1b5SDaniel Vetter 
2391d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
23927e231dbeSJesse Barnes 
23937e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
23947e231dbeSJesse Barnes 
23957e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
23967e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
23977e231dbeSJesse Barnes 	for_each_pipe(pipe)
23987e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
23997e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
24007e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
24017e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
24027e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
24037e231dbeSJesse Barnes }
24047e231dbeSJesse Barnes 
240582a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
240682a28bcfSDaniel Vetter {
240782a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
240882a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
240982a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2410fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
241182a28bcfSDaniel Vetter 
241282a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2413fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
241482a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2415cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2416fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
241782a28bcfSDaniel Vetter 	} else {
2418fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
241982a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2420cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2421fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
242282a28bcfSDaniel Vetter 	}
242382a28bcfSDaniel Vetter 
2424fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
242582a28bcfSDaniel Vetter 
24267fe0b973SKeith Packard 	/*
24277fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
24287fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
24297fe0b973SKeith Packard 	 *
24307fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
24317fe0b973SKeith Packard 	 */
24327fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
24337fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
24347fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
24357fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
24367fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
24377fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
24387fe0b973SKeith Packard }
24397fe0b973SKeith Packard 
2440d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2441d46da437SPaulo Zanoni {
2442d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
244382a28bcfSDaniel Vetter 	u32 mask;
2444d46da437SPaulo Zanoni 
2445692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2446692a04cfSDaniel Vetter 		return;
2447692a04cfSDaniel Vetter 
24488664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
24498664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2450de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
24518664281bSPaulo Zanoni 	} else {
24528664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
24538664281bSPaulo Zanoni 
24548664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
24558664281bSPaulo Zanoni 	}
2456ab5c608bSBen Widawsky 
2457d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2458d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2459d46da437SPaulo Zanoni }
2460d46da437SPaulo Zanoni 
24610a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
24620a9a8c91SDaniel Vetter {
24630a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
24640a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
24650a9a8c91SDaniel Vetter 
24660a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
24670a9a8c91SDaniel Vetter 
24680a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
2469040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
24700a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
247135a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
247235a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
24730a9a8c91SDaniel Vetter 	}
24740a9a8c91SDaniel Vetter 
24750a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
24760a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
24770a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
24780a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
24790a9a8c91SDaniel Vetter 	} else {
24800a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
24810a9a8c91SDaniel Vetter 	}
24820a9a8c91SDaniel Vetter 
24830a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
24840a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
24850a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
24860a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
24870a9a8c91SDaniel Vetter 
24880a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
24890a9a8c91SDaniel Vetter 		pm_irqs |= GEN6_PM_RPS_EVENTS;
24900a9a8c91SDaniel Vetter 
24910a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
24920a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
24930a9a8c91SDaniel Vetter 
2494605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
24950a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2496605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
24970a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
24980a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
24990a9a8c91SDaniel Vetter 	}
25000a9a8c91SDaniel Vetter }
25010a9a8c91SDaniel Vetter 
2502f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2503036a4a7dSZhenyu Wang {
25044bc9d430SDaniel Vetter 	unsigned long irqflags;
2505036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25068e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
25078e76f8dcSPaulo Zanoni 
25088e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
25098e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
25108e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
25118e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
25128e76f8dcSPaulo Zanoni 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
25138e76f8dcSPaulo Zanoni 				DE_ERR_INT_IVB);
25148e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
25158e76f8dcSPaulo Zanoni 			      DE_PIPEA_VBLANK_IVB);
25168e76f8dcSPaulo Zanoni 
25178e76f8dcSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
25188e76f8dcSPaulo Zanoni 	} else {
25198e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2520ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
25215b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
25225b3a856bSDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
25235b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
25245b3a856bSDaniel Vetter 				DE_POISON);
25258e76f8dcSPaulo Zanoni 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
25268e76f8dcSPaulo Zanoni 	}
2527036a4a7dSZhenyu Wang 
25281ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2529036a4a7dSZhenyu Wang 
2530036a4a7dSZhenyu Wang 	/* should always can generate irq */
2531036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
25321ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
25338e76f8dcSPaulo Zanoni 	I915_WRITE(DEIER, display_mask | extra_mask);
25343143a2bfSChris Wilson 	POSTING_READ(DEIER);
2535036a4a7dSZhenyu Wang 
25360a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
2537036a4a7dSZhenyu Wang 
2538d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
25397fe0b973SKeith Packard 
2540f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
25416005ce42SDaniel Vetter 		/* Enable PCU event interrupts
25426005ce42SDaniel Vetter 		 *
25436005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
25444bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
25454bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
25464bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2547f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
25484bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2549f97108d1SJesse Barnes 	}
2550f97108d1SJesse Barnes 
2551036a4a7dSZhenyu Wang 	return 0;
2552036a4a7dSZhenyu Wang }
2553036a4a7dSZhenyu Wang 
25547e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
25557e231dbeSJesse Barnes {
25567e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25577e231dbeSJesse Barnes 	u32 enable_mask;
255831acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
2559b79480baSDaniel Vetter 	unsigned long irqflags;
25607e231dbeSJesse Barnes 
25617e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
256231acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
256331acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
256431acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
25657e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
25667e231dbeSJesse Barnes 
256731acc7f5SJesse Barnes 	/*
256831acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
256931acc7f5SJesse Barnes 	 * toggle them based on usage.
257031acc7f5SJesse Barnes 	 */
257131acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
257231acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
257331acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
25747e231dbeSJesse Barnes 
257520afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
257620afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
257720afbda2SDaniel Vetter 
25787e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
25797e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
25807e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
25817e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
25827e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
25837e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
25847e231dbeSJesse Barnes 
2585b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2586b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2587b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
258831acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2589515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
259031acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
2591b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
259231acc7f5SJesse Barnes 
25937e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
25947e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
25957e231dbeSJesse Barnes 
25960a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
25977e231dbeSJesse Barnes 
25987e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
25997e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
26007e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
26017e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
26027e231dbeSJesse Barnes #endif
26037e231dbeSJesse Barnes 
26047e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
260520afbda2SDaniel Vetter 
260620afbda2SDaniel Vetter 	return 0;
260720afbda2SDaniel Vetter }
260820afbda2SDaniel Vetter 
26097e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
26107e231dbeSJesse Barnes {
26117e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26127e231dbeSJesse Barnes 	int pipe;
26137e231dbeSJesse Barnes 
26147e231dbeSJesse Barnes 	if (!dev_priv)
26157e231dbeSJesse Barnes 		return;
26167e231dbeSJesse Barnes 
2617ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2618ac4c16c5SEgbert Eich 
26197e231dbeSJesse Barnes 	for_each_pipe(pipe)
26207e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
26217e231dbeSJesse Barnes 
26227e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
26237e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
26247e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
26257e231dbeSJesse Barnes 	for_each_pipe(pipe)
26267e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
26277e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26287e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
26297e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
26307e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
26317e231dbeSJesse Barnes }
26327e231dbeSJesse Barnes 
2633f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2634036a4a7dSZhenyu Wang {
2635036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26364697995bSJesse Barnes 
26374697995bSJesse Barnes 	if (!dev_priv)
26384697995bSJesse Barnes 		return;
26394697995bSJesse Barnes 
2640ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2641ac4c16c5SEgbert Eich 
2642036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2643036a4a7dSZhenyu Wang 
2644036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2645036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2646036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
26478664281bSPaulo Zanoni 	if (IS_GEN7(dev))
26488664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2649036a4a7dSZhenyu Wang 
2650036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2651036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2652036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2653192aac1fSKeith Packard 
2654ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2655ab5c608bSBen Widawsky 		return;
2656ab5c608bSBen Widawsky 
2657192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2658192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2659192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
26608664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
26618664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2662036a4a7dSZhenyu Wang }
2663036a4a7dSZhenyu Wang 
2664c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2665c2798b19SChris Wilson {
2666c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2667c2798b19SChris Wilson 	int pipe;
2668c2798b19SChris Wilson 
2669c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2670c2798b19SChris Wilson 
2671c2798b19SChris Wilson 	for_each_pipe(pipe)
2672c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2673c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2674c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2675c2798b19SChris Wilson 	POSTING_READ16(IER);
2676c2798b19SChris Wilson }
2677c2798b19SChris Wilson 
2678c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2679c2798b19SChris Wilson {
2680c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2681c2798b19SChris Wilson 
2682c2798b19SChris Wilson 	I915_WRITE16(EMR,
2683c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2684c2798b19SChris Wilson 
2685c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2686c2798b19SChris Wilson 	dev_priv->irq_mask =
2687c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2688c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2689c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2690c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2691c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2692c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2693c2798b19SChris Wilson 
2694c2798b19SChris Wilson 	I915_WRITE16(IER,
2695c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2696c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2697c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2698c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2699c2798b19SChris Wilson 	POSTING_READ16(IER);
2700c2798b19SChris Wilson 
2701c2798b19SChris Wilson 	return 0;
2702c2798b19SChris Wilson }
2703c2798b19SChris Wilson 
270490a72f87SVille Syrjälä /*
270590a72f87SVille Syrjälä  * Returns true when a page flip has completed.
270690a72f87SVille Syrjälä  */
270790a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
270890a72f87SVille Syrjälä 			       int pipe, u16 iir)
270990a72f87SVille Syrjälä {
271090a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
271190a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
271290a72f87SVille Syrjälä 
271390a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
271490a72f87SVille Syrjälä 		return false;
271590a72f87SVille Syrjälä 
271690a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
271790a72f87SVille Syrjälä 		return false;
271890a72f87SVille Syrjälä 
271990a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
272090a72f87SVille Syrjälä 
272190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
272290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
272390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
272490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
272590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
272690a72f87SVille Syrjälä 	 */
272790a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
272890a72f87SVille Syrjälä 		return false;
272990a72f87SVille Syrjälä 
273090a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
273190a72f87SVille Syrjälä 
273290a72f87SVille Syrjälä 	return true;
273390a72f87SVille Syrjälä }
273490a72f87SVille Syrjälä 
2735ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2736c2798b19SChris Wilson {
2737c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2738c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2739c2798b19SChris Wilson 	u16 iir, new_iir;
2740c2798b19SChris Wilson 	u32 pipe_stats[2];
2741c2798b19SChris Wilson 	unsigned long irqflags;
2742c2798b19SChris Wilson 	int pipe;
2743c2798b19SChris Wilson 	u16 flip_mask =
2744c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2745c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2746c2798b19SChris Wilson 
2747c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2748c2798b19SChris Wilson 
2749c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2750c2798b19SChris Wilson 	if (iir == 0)
2751c2798b19SChris Wilson 		return IRQ_NONE;
2752c2798b19SChris Wilson 
2753c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2754c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2755c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2756c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2757c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2758c2798b19SChris Wilson 		 */
2759c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2760c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2761c2798b19SChris Wilson 			i915_handle_error(dev, false);
2762c2798b19SChris Wilson 
2763c2798b19SChris Wilson 		for_each_pipe(pipe) {
2764c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2765c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2766c2798b19SChris Wilson 
2767c2798b19SChris Wilson 			/*
2768c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2769c2798b19SChris Wilson 			 */
2770c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2771c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2772c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2773c2798b19SChris Wilson 							 pipe_name(pipe));
2774c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2775c2798b19SChris Wilson 			}
2776c2798b19SChris Wilson 		}
2777c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2778c2798b19SChris Wilson 
2779c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2780c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2781c2798b19SChris Wilson 
2782d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2783c2798b19SChris Wilson 
2784c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2785c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2786c2798b19SChris Wilson 
2787c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
278890a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
278990a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2790c2798b19SChris Wilson 
2791c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
279290a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
279390a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2794c2798b19SChris Wilson 
2795c2798b19SChris Wilson 		iir = new_iir;
2796c2798b19SChris Wilson 	}
2797c2798b19SChris Wilson 
2798c2798b19SChris Wilson 	return IRQ_HANDLED;
2799c2798b19SChris Wilson }
2800c2798b19SChris Wilson 
2801c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2802c2798b19SChris Wilson {
2803c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2804c2798b19SChris Wilson 	int pipe;
2805c2798b19SChris Wilson 
2806c2798b19SChris Wilson 	for_each_pipe(pipe) {
2807c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2808c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2809c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2810c2798b19SChris Wilson 	}
2811c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2812c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2813c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2814c2798b19SChris Wilson }
2815c2798b19SChris Wilson 
2816a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2817a266c7d5SChris Wilson {
2818a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2819a266c7d5SChris Wilson 	int pipe;
2820a266c7d5SChris Wilson 
2821a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2822a266c7d5SChris Wilson 
2823a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2824a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2825a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2826a266c7d5SChris Wilson 	}
2827a266c7d5SChris Wilson 
282800d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2829a266c7d5SChris Wilson 	for_each_pipe(pipe)
2830a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2831a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2832a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2833a266c7d5SChris Wilson 	POSTING_READ(IER);
2834a266c7d5SChris Wilson }
2835a266c7d5SChris Wilson 
2836a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2837a266c7d5SChris Wilson {
2838a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
283938bde180SChris Wilson 	u32 enable_mask;
2840a266c7d5SChris Wilson 
284138bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
284238bde180SChris Wilson 
284338bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
284438bde180SChris Wilson 	dev_priv->irq_mask =
284538bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
284638bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
284738bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
284838bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
284938bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
285038bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
285138bde180SChris Wilson 
285238bde180SChris Wilson 	enable_mask =
285338bde180SChris Wilson 		I915_ASLE_INTERRUPT |
285438bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
285538bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
285638bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
285738bde180SChris Wilson 		I915_USER_INTERRUPT;
285838bde180SChris Wilson 
2859a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
286020afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
286120afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
286220afbda2SDaniel Vetter 
2863a266c7d5SChris Wilson 		/* Enable in IER... */
2864a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2865a266c7d5SChris Wilson 		/* and unmask in IMR */
2866a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2867a266c7d5SChris Wilson 	}
2868a266c7d5SChris Wilson 
2869a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2870a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2871a266c7d5SChris Wilson 	POSTING_READ(IER);
2872a266c7d5SChris Wilson 
2873f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
287420afbda2SDaniel Vetter 
287520afbda2SDaniel Vetter 	return 0;
287620afbda2SDaniel Vetter }
287720afbda2SDaniel Vetter 
287890a72f87SVille Syrjälä /*
287990a72f87SVille Syrjälä  * Returns true when a page flip has completed.
288090a72f87SVille Syrjälä  */
288190a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
288290a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
288390a72f87SVille Syrjälä {
288490a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
288590a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
288690a72f87SVille Syrjälä 
288790a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
288890a72f87SVille Syrjälä 		return false;
288990a72f87SVille Syrjälä 
289090a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
289190a72f87SVille Syrjälä 		return false;
289290a72f87SVille Syrjälä 
289390a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
289490a72f87SVille Syrjälä 
289590a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
289690a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
289790a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
289890a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
289990a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
290090a72f87SVille Syrjälä 	 */
290190a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
290290a72f87SVille Syrjälä 		return false;
290390a72f87SVille Syrjälä 
290490a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
290590a72f87SVille Syrjälä 
290690a72f87SVille Syrjälä 	return true;
290790a72f87SVille Syrjälä }
290890a72f87SVille Syrjälä 
2909ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2910a266c7d5SChris Wilson {
2911a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2912a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29138291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2914a266c7d5SChris Wilson 	unsigned long irqflags;
291538bde180SChris Wilson 	u32 flip_mask =
291638bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
291738bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
291838bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2919a266c7d5SChris Wilson 
2920a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2921a266c7d5SChris Wilson 
2922a266c7d5SChris Wilson 	iir = I915_READ(IIR);
292338bde180SChris Wilson 	do {
292438bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
29258291ee90SChris Wilson 		bool blc_event = false;
2926a266c7d5SChris Wilson 
2927a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2928a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2929a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2930a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2931a266c7d5SChris Wilson 		 */
2932a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2933a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2934a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2935a266c7d5SChris Wilson 
2936a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2937a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2938a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2939a266c7d5SChris Wilson 
294038bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2941a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2942a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2943a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2944a266c7d5SChris Wilson 							 pipe_name(pipe));
2945a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
294638bde180SChris Wilson 				irq_received = true;
2947a266c7d5SChris Wilson 			}
2948a266c7d5SChris Wilson 		}
2949a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2950a266c7d5SChris Wilson 
2951a266c7d5SChris Wilson 		if (!irq_received)
2952a266c7d5SChris Wilson 			break;
2953a266c7d5SChris Wilson 
2954a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2955a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2956a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2957a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2958b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
2959a266c7d5SChris Wilson 
2960a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2961a266c7d5SChris Wilson 				  hotplug_status);
296291d131d2SDaniel Vetter 
296310a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
296491d131d2SDaniel Vetter 
2965a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
296638bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2967a266c7d5SChris Wilson 		}
2968a266c7d5SChris Wilson 
296938bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2970a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2971a266c7d5SChris Wilson 
2972a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2973a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2974a266c7d5SChris Wilson 
2975a266c7d5SChris Wilson 		for_each_pipe(pipe) {
297638bde180SChris Wilson 			int plane = pipe;
297738bde180SChris Wilson 			if (IS_MOBILE(dev))
297838bde180SChris Wilson 				plane = !plane;
29795e2032d4SVille Syrjälä 
298090a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
298190a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
298290a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
2983a266c7d5SChris Wilson 
2984a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2985a266c7d5SChris Wilson 				blc_event = true;
2986a266c7d5SChris Wilson 		}
2987a266c7d5SChris Wilson 
2988a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2989a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2990a266c7d5SChris Wilson 
2991a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2992a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2993a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2994a266c7d5SChris Wilson 		 * we would never get another interrupt.
2995a266c7d5SChris Wilson 		 *
2996a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2997a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2998a266c7d5SChris Wilson 		 * another one.
2999a266c7d5SChris Wilson 		 *
3000a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3001a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3002a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3003a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3004a266c7d5SChris Wilson 		 * stray interrupts.
3005a266c7d5SChris Wilson 		 */
300638bde180SChris Wilson 		ret = IRQ_HANDLED;
3007a266c7d5SChris Wilson 		iir = new_iir;
300838bde180SChris Wilson 	} while (iir & ~flip_mask);
3009a266c7d5SChris Wilson 
3010d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
30118291ee90SChris Wilson 
3012a266c7d5SChris Wilson 	return ret;
3013a266c7d5SChris Wilson }
3014a266c7d5SChris Wilson 
3015a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3016a266c7d5SChris Wilson {
3017a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3018a266c7d5SChris Wilson 	int pipe;
3019a266c7d5SChris Wilson 
3020ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3021ac4c16c5SEgbert Eich 
3022a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3023a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3024a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3025a266c7d5SChris Wilson 	}
3026a266c7d5SChris Wilson 
302700d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
302855b39755SChris Wilson 	for_each_pipe(pipe) {
302955b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3030a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
303155b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
303255b39755SChris Wilson 	}
3033a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3034a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3035a266c7d5SChris Wilson 
3036a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3037a266c7d5SChris Wilson }
3038a266c7d5SChris Wilson 
3039a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3040a266c7d5SChris Wilson {
3041a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3042a266c7d5SChris Wilson 	int pipe;
3043a266c7d5SChris Wilson 
3044a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3045a266c7d5SChris Wilson 
3046a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3047a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3048a266c7d5SChris Wilson 
3049a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3050a266c7d5SChris Wilson 	for_each_pipe(pipe)
3051a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3052a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3053a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3054a266c7d5SChris Wilson 	POSTING_READ(IER);
3055a266c7d5SChris Wilson }
3056a266c7d5SChris Wilson 
3057a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3058a266c7d5SChris Wilson {
3059a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3060bbba0a97SChris Wilson 	u32 enable_mask;
3061a266c7d5SChris Wilson 	u32 error_mask;
3062b79480baSDaniel Vetter 	unsigned long irqflags;
3063a266c7d5SChris Wilson 
3064a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3065bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3066adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3067bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3068bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3069bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3070bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3071bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3072bbba0a97SChris Wilson 
3073bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
307421ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
307521ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3076bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3077bbba0a97SChris Wilson 
3078bbba0a97SChris Wilson 	if (IS_G4X(dev))
3079bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3080a266c7d5SChris Wilson 
3081b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3082b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3083b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3084515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
3085b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3086a266c7d5SChris Wilson 
3087a266c7d5SChris Wilson 	/*
3088a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3089a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3090a266c7d5SChris Wilson 	 */
3091a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3092a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3093a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3094a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3095a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3096a266c7d5SChris Wilson 	} else {
3097a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3098a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3099a266c7d5SChris Wilson 	}
3100a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3101a266c7d5SChris Wilson 
3102a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3103a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3104a266c7d5SChris Wilson 	POSTING_READ(IER);
3105a266c7d5SChris Wilson 
310620afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
310720afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
310820afbda2SDaniel Vetter 
3109f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
311020afbda2SDaniel Vetter 
311120afbda2SDaniel Vetter 	return 0;
311220afbda2SDaniel Vetter }
311320afbda2SDaniel Vetter 
3114bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
311520afbda2SDaniel Vetter {
311620afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3117e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3118cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
311920afbda2SDaniel Vetter 	u32 hotplug_en;
312020afbda2SDaniel Vetter 
3121b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3122b5ea2d56SDaniel Vetter 
3123bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3124bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3125bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3126adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3127e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3128cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3129cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3130cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3131a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3132a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3133a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3134a266c7d5SChris Wilson 		*/
3135a266c7d5SChris Wilson 		if (IS_G4X(dev))
3136a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
313785fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3138a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3139a266c7d5SChris Wilson 
3140a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3141a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3142a266c7d5SChris Wilson 	}
3143bac56d5bSEgbert Eich }
3144a266c7d5SChris Wilson 
3145ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3146a266c7d5SChris Wilson {
3147a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3148a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3149a266c7d5SChris Wilson 	u32 iir, new_iir;
3150a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3151a266c7d5SChris Wilson 	unsigned long irqflags;
3152a266c7d5SChris Wilson 	int irq_received;
3153a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
315421ad8330SVille Syrjälä 	u32 flip_mask =
315521ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
315621ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3157a266c7d5SChris Wilson 
3158a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3159a266c7d5SChris Wilson 
3160a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3161a266c7d5SChris Wilson 
3162a266c7d5SChris Wilson 	for (;;) {
31632c8ba29fSChris Wilson 		bool blc_event = false;
31642c8ba29fSChris Wilson 
316521ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3166a266c7d5SChris Wilson 
3167a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3168a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3169a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3170a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3171a266c7d5SChris Wilson 		 */
3172a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3173a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3174a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3175a266c7d5SChris Wilson 
3176a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3177a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3178a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3179a266c7d5SChris Wilson 
3180a266c7d5SChris Wilson 			/*
3181a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3182a266c7d5SChris Wilson 			 */
3183a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3184a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3185a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3186a266c7d5SChris Wilson 							 pipe_name(pipe));
3187a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3188a266c7d5SChris Wilson 				irq_received = 1;
3189a266c7d5SChris Wilson 			}
3190a266c7d5SChris Wilson 		}
3191a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3192a266c7d5SChris Wilson 
3193a266c7d5SChris Wilson 		if (!irq_received)
3194a266c7d5SChris Wilson 			break;
3195a266c7d5SChris Wilson 
3196a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3197a266c7d5SChris Wilson 
3198a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3199adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3200a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3201b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3202b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
32034f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3204a266c7d5SChris Wilson 
3205a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3206a266c7d5SChris Wilson 				  hotplug_status);
320791d131d2SDaniel Vetter 
320810a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
320910a504deSDaniel Vetter 					      IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
321091d131d2SDaniel Vetter 
3211a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3212a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3213a266c7d5SChris Wilson 		}
3214a266c7d5SChris Wilson 
321521ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3216a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3217a266c7d5SChris Wilson 
3218a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3219a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3220a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3221a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3222a266c7d5SChris Wilson 
3223a266c7d5SChris Wilson 		for_each_pipe(pipe) {
32242c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
322590a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
322690a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3227a266c7d5SChris Wilson 
3228a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3229a266c7d5SChris Wilson 				blc_event = true;
3230a266c7d5SChris Wilson 		}
3231a266c7d5SChris Wilson 
3232a266c7d5SChris Wilson 
3233a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3234a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3235a266c7d5SChris Wilson 
3236515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3237515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3238515ac2bbSDaniel Vetter 
3239a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3240a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3241a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3242a266c7d5SChris Wilson 		 * we would never get another interrupt.
3243a266c7d5SChris Wilson 		 *
3244a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3245a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3246a266c7d5SChris Wilson 		 * another one.
3247a266c7d5SChris Wilson 		 *
3248a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3249a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3250a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3251a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3252a266c7d5SChris Wilson 		 * stray interrupts.
3253a266c7d5SChris Wilson 		 */
3254a266c7d5SChris Wilson 		iir = new_iir;
3255a266c7d5SChris Wilson 	}
3256a266c7d5SChris Wilson 
3257d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
32582c8ba29fSChris Wilson 
3259a266c7d5SChris Wilson 	return ret;
3260a266c7d5SChris Wilson }
3261a266c7d5SChris Wilson 
3262a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3263a266c7d5SChris Wilson {
3264a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3265a266c7d5SChris Wilson 	int pipe;
3266a266c7d5SChris Wilson 
3267a266c7d5SChris Wilson 	if (!dev_priv)
3268a266c7d5SChris Wilson 		return;
3269a266c7d5SChris Wilson 
3270ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3271ac4c16c5SEgbert Eich 
3272a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3273a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3274a266c7d5SChris Wilson 
3275a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3276a266c7d5SChris Wilson 	for_each_pipe(pipe)
3277a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3278a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3279a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3280a266c7d5SChris Wilson 
3281a266c7d5SChris Wilson 	for_each_pipe(pipe)
3282a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3283a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3284a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3285a266c7d5SChris Wilson }
3286a266c7d5SChris Wilson 
3287ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3288ac4c16c5SEgbert Eich {
3289ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3290ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3291ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3292ac4c16c5SEgbert Eich 	unsigned long irqflags;
3293ac4c16c5SEgbert Eich 	int i;
3294ac4c16c5SEgbert Eich 
3295ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3296ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3297ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3298ac4c16c5SEgbert Eich 
3299ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3300ac4c16c5SEgbert Eich 			continue;
3301ac4c16c5SEgbert Eich 
3302ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3303ac4c16c5SEgbert Eich 
3304ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3305ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3306ac4c16c5SEgbert Eich 
3307ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3308ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3309ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3310ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3311ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3312ac4c16c5SEgbert Eich 				if (!connector->polled)
3313ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3314ac4c16c5SEgbert Eich 			}
3315ac4c16c5SEgbert Eich 		}
3316ac4c16c5SEgbert Eich 	}
3317ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3318ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3319ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3320ac4c16c5SEgbert Eich }
3321ac4c16c5SEgbert Eich 
3322f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3323f71d4af4SJesse Barnes {
33248b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
33258b2e326dSChris Wilson 
33268b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
332799584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3328c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3329a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
33308b2e326dSChris Wilson 
333199584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
333299584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
333361bac78eSDaniel Vetter 		    (unsigned long) dev);
3334ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3335ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
333661bac78eSDaniel Vetter 
333797a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
33389ee32feaSDaniel Vetter 
33394cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
33404cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
33414cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
33424cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3343f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3344f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3345391f75e2SVille Syrjälä 	} else {
3346391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
3347391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
3348f71d4af4SJesse Barnes 	}
3349f71d4af4SJesse Barnes 
3350c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
3351f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3352f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3353c2baf4b7SVille Syrjälä 	}
3354f71d4af4SJesse Barnes 
33557e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
33567e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
33577e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
33587e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
33597e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
33607e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
33617e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3362fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3363f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3364f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3365f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3366f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3367f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3368f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3369f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
337082a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3371f71d4af4SJesse Barnes 	} else {
3372c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3373c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3374c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3375c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3376c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3377a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3378a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3379a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3380a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3381a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
338220afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3383c2798b19SChris Wilson 		} else {
3384a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3385a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3386a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3387a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3388bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3389c2798b19SChris Wilson 		}
3390f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3391f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3392f71d4af4SJesse Barnes 	}
3393f71d4af4SJesse Barnes }
339420afbda2SDaniel Vetter 
339520afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
339620afbda2SDaniel Vetter {
339720afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3398821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3399821450c6SEgbert Eich 	struct drm_connector *connector;
3400b5ea2d56SDaniel Vetter 	unsigned long irqflags;
3401821450c6SEgbert Eich 	int i;
340220afbda2SDaniel Vetter 
3403821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3404821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3405821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3406821450c6SEgbert Eich 	}
3407821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3408821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3409821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3410821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3411821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3412821450c6SEgbert Eich 	}
3413b5ea2d56SDaniel Vetter 
3414b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3415b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
3416b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
341720afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
341820afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
3419b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
342020afbda2SDaniel Vetter }
3421c67a470bSPaulo Zanoni 
3422c67a470bSPaulo Zanoni /* Disable interrupts so we can allow Package C8+. */
3423c67a470bSPaulo Zanoni void hsw_pc8_disable_interrupts(struct drm_device *dev)
3424c67a470bSPaulo Zanoni {
3425c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3426c67a470bSPaulo Zanoni 	unsigned long irqflags;
3427c67a470bSPaulo Zanoni 
3428c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3429c67a470bSPaulo Zanoni 
3430c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.deimr = I915_READ(DEIMR);
3431c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.sdeimr = I915_READ(SDEIMR);
3432c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtimr = I915_READ(GTIMR);
3433c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtier = I915_READ(GTIER);
3434c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
3435c67a470bSPaulo Zanoni 
3436c67a470bSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, ~DE_PCH_EVENT_IVB);
3437c67a470bSPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, ~SDE_HOTPLUG_MASK_CPT);
3438c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
3439c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
3440c67a470bSPaulo Zanoni 
3441c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = true;
3442c67a470bSPaulo Zanoni 
3443c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3444c67a470bSPaulo Zanoni }
3445c67a470bSPaulo Zanoni 
3446c67a470bSPaulo Zanoni /* Restore interrupts so we can recover from Package C8+. */
3447c67a470bSPaulo Zanoni void hsw_pc8_restore_interrupts(struct drm_device *dev)
3448c67a470bSPaulo Zanoni {
3449c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3450c67a470bSPaulo Zanoni 	unsigned long irqflags;
3451c67a470bSPaulo Zanoni 	uint32_t val, expected;
3452c67a470bSPaulo Zanoni 
3453c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3454c67a470bSPaulo Zanoni 
3455c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
3456c67a470bSPaulo Zanoni 	expected = ~DE_PCH_EVENT_IVB;
3457c67a470bSPaulo Zanoni 	WARN(val != expected, "DEIMR is 0x%08x, not 0x%08x\n", val, expected);
3458c67a470bSPaulo Zanoni 
3459c67a470bSPaulo Zanoni 	val = I915_READ(SDEIMR) & ~SDE_HOTPLUG_MASK_CPT;
3460c67a470bSPaulo Zanoni 	expected = ~SDE_HOTPLUG_MASK_CPT;
3461c67a470bSPaulo Zanoni 	WARN(val != expected, "SDEIMR non-HPD bits are 0x%08x, not 0x%08x\n",
3462c67a470bSPaulo Zanoni 	     val, expected);
3463c67a470bSPaulo Zanoni 
3464c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
3465c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3466c67a470bSPaulo Zanoni 	WARN(val != expected, "GTIMR is 0x%08x, not 0x%08x\n", val, expected);
3467c67a470bSPaulo Zanoni 
3468c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
3469c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3470c67a470bSPaulo Zanoni 	WARN(val != expected, "GEN6_PMIMR is 0x%08x, not 0x%08x\n", val,
3471c67a470bSPaulo Zanoni 	     expected);
3472c67a470bSPaulo Zanoni 
3473c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = false;
3474c67a470bSPaulo Zanoni 
3475c67a470bSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pc8.regsave.deimr);
3476c67a470bSPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv,
3477c67a470bSPaulo Zanoni 				     ~dev_priv->pc8.regsave.sdeimr &
3478c67a470bSPaulo Zanoni 				     ~SDE_HOTPLUG_MASK_CPT);
3479c67a470bSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pc8.regsave.gtimr);
3480c67a470bSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pc8.regsave.gen6_pmimr);
3481c67a470bSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pc8.regsave.gtier);
3482c67a470bSPaulo Zanoni 
3483c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3484c67a470bSPaulo Zanoni }
3485