xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 5a69b89f853fb35adf51b8b45c026bad0934bf97)
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,
11968bc5e955SDaniel 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 
12188bc5e955SDaniel 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 
1231*5a69b89fSDaniel Vetter static void hsw_pipe_crc_update(struct drm_device *dev, enum pipe pipe)
1232*5a69b89fSDaniel Vetter {
1233*5a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1234*5a69b89fSDaniel Vetter 
1235*5a69b89fSDaniel Vetter 	display_pipe_crc_update(dev, pipe,
1236*5a69b89fSDaniel Vetter 				I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1237*5a69b89fSDaniel Vetter 				0, 0, 0, 0);
1238*5a69b89fSDaniel Vetter }
1239*5a69b89fSDaniel Vetter 
1240eba94eb9SDaniel Vetter static void ivb_pipe_crc_update(struct drm_device *dev, enum pipe pipe)
1241eba94eb9SDaniel Vetter {
1242eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1243eba94eb9SDaniel Vetter 
1244eba94eb9SDaniel Vetter 	display_pipe_crc_update(dev, pipe,
1245eba94eb9SDaniel Vetter 				I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1246eba94eb9SDaniel Vetter 				I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1247eba94eb9SDaniel Vetter 				I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1248eba94eb9SDaniel Vetter 				I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
12498bc5e955SDaniel Vetter 				I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1250eba94eb9SDaniel Vetter }
12515b3a856bSDaniel Vetter 
12525b3a856bSDaniel Vetter static void ilk_pipe_crc_update(struct drm_device *dev, enum pipe pipe)
12535b3a856bSDaniel Vetter {
12545b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
12555b3a856bSDaniel Vetter 
12565b3a856bSDaniel Vetter 	display_pipe_crc_update(dev, pipe,
12575b3a856bSDaniel Vetter 				I915_READ(PIPE_CRC_RES_RED_ILK(pipe)),
12585b3a856bSDaniel Vetter 				I915_READ(PIPE_CRC_RES_GREEN_ILK(pipe)),
12595b3a856bSDaniel Vetter 				I915_READ(PIPE_CRC_RES_BLUE_ILK(pipe)),
12605b3a856bSDaniel Vetter 				I915_READ(PIPE_CRC_RES_RES1_ILK(pipe)),
12618bc5e955SDaniel Vetter 				I915_READ(PIPE_CRC_RES_RES2_ILK(pipe)));
12625b3a856bSDaniel Vetter }
12638bf1e9f1SShuang He #else
1264*5a69b89fSDaniel Vetter static inline void hsw_pipe_crc_update(struct drm_device *dev, int pipe) {}
1265f8c168faSDaniel Vetter static inline void ivb_pipe_crc_update(struct drm_device *dev, int pipe) {}
12665b3a856bSDaniel Vetter static inline void ilk_pipe_crc_update(struct drm_device *dev, int pipe) {}
12678bf1e9f1SShuang He #endif
12688bf1e9f1SShuang He 
12691403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
12701403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
12711403c0d4SPaulo Zanoni  * the work queue. */
12721403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1273baf02a1fSBen Widawsky {
127441a05a3aSDaniel Vetter 	if (pm_iir & GEN6_PM_RPS_EVENTS) {
127559cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
12764848405cSBen Widawsky 		dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
12774d3b3d5fSPaulo Zanoni 		snb_disable_pm_irq(dev_priv, pm_iir & GEN6_PM_RPS_EVENTS);
127859cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
12792adbee62SDaniel Vetter 
12802adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
128141a05a3aSDaniel Vetter 	}
1282baf02a1fSBen Widawsky 
12831403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
128412638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
128512638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
128612638c57SBen Widawsky 
128712638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
128812638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
128912638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
129012638c57SBen Widawsky 		}
129112638c57SBen Widawsky 	}
12921403c0d4SPaulo Zanoni }
1293baf02a1fSBen Widawsky 
1294ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
12957e231dbeSJesse Barnes {
12967e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
12977e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
12987e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
12997e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
13007e231dbeSJesse Barnes 	unsigned long irqflags;
13017e231dbeSJesse Barnes 	int pipe;
13027e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
13037e231dbeSJesse Barnes 
13047e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
13057e231dbeSJesse Barnes 
13067e231dbeSJesse Barnes 	while (true) {
13077e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
13087e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
13097e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
13107e231dbeSJesse Barnes 
13117e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
13127e231dbeSJesse Barnes 			goto out;
13137e231dbeSJesse Barnes 
13147e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
13157e231dbeSJesse Barnes 
1316e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
13177e231dbeSJesse Barnes 
13187e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
13197e231dbeSJesse Barnes 		for_each_pipe(pipe) {
13207e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
13217e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
13227e231dbeSJesse Barnes 
13237e231dbeSJesse Barnes 			/*
13247e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
13257e231dbeSJesse Barnes 			 */
13267e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
13277e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
13287e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
13297e231dbeSJesse Barnes 							 pipe_name(pipe));
13307e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
13317e231dbeSJesse Barnes 			}
13327e231dbeSJesse Barnes 		}
13337e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
13347e231dbeSJesse Barnes 
133531acc7f5SJesse Barnes 		for_each_pipe(pipe) {
133631acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
133731acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
133831acc7f5SJesse Barnes 
133931acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
134031acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
134131acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
134231acc7f5SJesse Barnes 			}
134331acc7f5SJesse Barnes 		}
134431acc7f5SJesse Barnes 
13457e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
13467e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
13477e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1348b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
13497e231dbeSJesse Barnes 
13507e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
13517e231dbeSJesse Barnes 					 hotplug_status);
135291d131d2SDaniel Vetter 
135310a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
135491d131d2SDaniel Vetter 
13557e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
13567e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
13577e231dbeSJesse Barnes 		}
13587e231dbeSJesse Barnes 
1359515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1360515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
13617e231dbeSJesse Barnes 
136260611c13SPaulo Zanoni 		if (pm_iir)
1363d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
13647e231dbeSJesse Barnes 
13657e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
13667e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
13677e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
13687e231dbeSJesse Barnes 	}
13697e231dbeSJesse Barnes 
13707e231dbeSJesse Barnes out:
13717e231dbeSJesse Barnes 	return ret;
13727e231dbeSJesse Barnes }
13737e231dbeSJesse Barnes 
137423e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1375776ad806SJesse Barnes {
1376776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13779db4a9c7SJesse Barnes 	int pipe;
1378b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1379776ad806SJesse Barnes 
138010a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
138191d131d2SDaniel Vetter 
1382cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1383cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1384776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1385cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1386cfc33bf7SVille Syrjälä 				 port_name(port));
1387cfc33bf7SVille Syrjälä 	}
1388776ad806SJesse Barnes 
1389ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1390ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1391ce99c256SDaniel Vetter 
1392776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1393515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1394776ad806SJesse Barnes 
1395776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1396776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1397776ad806SJesse Barnes 
1398776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1399776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1400776ad806SJesse Barnes 
1401776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1402776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1403776ad806SJesse Barnes 
14049db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
14059db4a9c7SJesse Barnes 		for_each_pipe(pipe)
14069db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
14079db4a9c7SJesse Barnes 					 pipe_name(pipe),
14089db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1409776ad806SJesse Barnes 
1410776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1411776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1412776ad806SJesse Barnes 
1413776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1414776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1415776ad806SJesse Barnes 
1416776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
14178664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
14188664281bSPaulo Zanoni 							  false))
14198664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
14208664281bSPaulo Zanoni 
14218664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
14228664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
14238664281bSPaulo Zanoni 							  false))
14248664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
14258664281bSPaulo Zanoni }
14268664281bSPaulo Zanoni 
14278664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
14288664281bSPaulo Zanoni {
14298664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
14308664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
1431*5a69b89fSDaniel Vetter 	enum pipe pipe;
14328664281bSPaulo Zanoni 
1433de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1434de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1435de032bf4SPaulo Zanoni 
1436*5a69b89fSDaniel Vetter 	for_each_pipe(pipe) {
1437*5a69b89fSDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
1438*5a69b89fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
1439*5a69b89fSDaniel Vetter 								  false))
1440*5a69b89fSDaniel Vetter 				DRM_DEBUG_DRIVER("Pipe %c FIFO underrun\n",
1441*5a69b89fSDaniel Vetter 						 pipe_name(pipe));
1442*5a69b89fSDaniel Vetter 		}
14438664281bSPaulo Zanoni 
1444*5a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
1445*5a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1446*5a69b89fSDaniel Vetter 				ivb_pipe_crc_update(dev, pipe);
1447*5a69b89fSDaniel Vetter 			else
1448*5a69b89fSDaniel Vetter 				hsw_pipe_crc_update(dev, pipe);
1449*5a69b89fSDaniel Vetter 		}
1450*5a69b89fSDaniel Vetter 	}
14518bf1e9f1SShuang He 
14528664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
14538664281bSPaulo Zanoni }
14548664281bSPaulo Zanoni 
14558664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
14568664281bSPaulo Zanoni {
14578664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
14588664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
14598664281bSPaulo Zanoni 
1460de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1461de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1462de032bf4SPaulo Zanoni 
14638664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
14648664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
14658664281bSPaulo Zanoni 							  false))
14668664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
14678664281bSPaulo Zanoni 
14688664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
14698664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
14708664281bSPaulo Zanoni 							  false))
14718664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
14728664281bSPaulo Zanoni 
14738664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
14748664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
14758664281bSPaulo Zanoni 							  false))
14768664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
14778664281bSPaulo Zanoni 
14788664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1479776ad806SJesse Barnes }
1480776ad806SJesse Barnes 
148123e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
148223e81d69SAdam Jackson {
148323e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
148423e81d69SAdam Jackson 	int pipe;
1485b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
148623e81d69SAdam Jackson 
148710a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
148891d131d2SDaniel Vetter 
1489cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1490cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
149123e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1492cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1493cfc33bf7SVille Syrjälä 				 port_name(port));
1494cfc33bf7SVille Syrjälä 	}
149523e81d69SAdam Jackson 
149623e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1497ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
149823e81d69SAdam Jackson 
149923e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1500515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
150123e81d69SAdam Jackson 
150223e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
150323e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
150423e81d69SAdam Jackson 
150523e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
150623e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
150723e81d69SAdam Jackson 
150823e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
150923e81d69SAdam Jackson 		for_each_pipe(pipe)
151023e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
151123e81d69SAdam Jackson 					 pipe_name(pipe),
151223e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
15138664281bSPaulo Zanoni 
15148664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
15158664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
151623e81d69SAdam Jackson }
151723e81d69SAdam Jackson 
1518c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1519c008bc6eSPaulo Zanoni {
1520c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1521c008bc6eSPaulo Zanoni 
1522c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1523c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1524c008bc6eSPaulo Zanoni 
1525c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1526c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1527c008bc6eSPaulo Zanoni 
1528c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEA_VBLANK)
1529c008bc6eSPaulo Zanoni 		drm_handle_vblank(dev, 0);
1530c008bc6eSPaulo Zanoni 
1531c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEB_VBLANK)
1532c008bc6eSPaulo Zanoni 		drm_handle_vblank(dev, 1);
1533c008bc6eSPaulo Zanoni 
1534c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1535c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1536c008bc6eSPaulo Zanoni 
1537c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
1538c008bc6eSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1539c008bc6eSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1540c008bc6eSPaulo Zanoni 
1541c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
1542c008bc6eSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1543c008bc6eSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1544c008bc6eSPaulo Zanoni 
15455b3a856bSDaniel Vetter 	if (de_iir & DE_PIPEA_CRC_DONE)
15465b3a856bSDaniel Vetter 		ilk_pipe_crc_update(dev, PIPE_A);
15475b3a856bSDaniel Vetter 
15485b3a856bSDaniel Vetter 	if (de_iir & DE_PIPEB_CRC_DONE)
15495b3a856bSDaniel Vetter 		ilk_pipe_crc_update(dev, PIPE_B);
15505b3a856bSDaniel Vetter 
1551c008bc6eSPaulo Zanoni 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1552c008bc6eSPaulo Zanoni 		intel_prepare_page_flip(dev, 0);
1553c008bc6eSPaulo Zanoni 		intel_finish_page_flip_plane(dev, 0);
1554c008bc6eSPaulo Zanoni 	}
1555c008bc6eSPaulo Zanoni 
1556c008bc6eSPaulo Zanoni 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1557c008bc6eSPaulo Zanoni 		intel_prepare_page_flip(dev, 1);
1558c008bc6eSPaulo Zanoni 		intel_finish_page_flip_plane(dev, 1);
1559c008bc6eSPaulo Zanoni 	}
1560c008bc6eSPaulo Zanoni 
1561c008bc6eSPaulo Zanoni 	/* check event from PCH */
1562c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1563c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1564c008bc6eSPaulo Zanoni 
1565c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1566c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1567c008bc6eSPaulo Zanoni 		else
1568c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1569c008bc6eSPaulo Zanoni 
1570c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1571c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1572c008bc6eSPaulo Zanoni 	}
1573c008bc6eSPaulo Zanoni 
1574c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1575c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1576c008bc6eSPaulo Zanoni }
1577c008bc6eSPaulo Zanoni 
15789719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
15799719fb98SPaulo Zanoni {
15809719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
15819719fb98SPaulo Zanoni 	int i;
15829719fb98SPaulo Zanoni 
15839719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
15849719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
15859719fb98SPaulo Zanoni 
15869719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
15879719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
15889719fb98SPaulo Zanoni 
15899719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
15909719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
15919719fb98SPaulo Zanoni 
15929719fb98SPaulo Zanoni 	for (i = 0; i < 3; i++) {
15939719fb98SPaulo Zanoni 		if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
15949719fb98SPaulo Zanoni 			drm_handle_vblank(dev, i);
15959719fb98SPaulo Zanoni 		if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
15969719fb98SPaulo Zanoni 			intel_prepare_page_flip(dev, i);
15979719fb98SPaulo Zanoni 			intel_finish_page_flip_plane(dev, i);
15989719fb98SPaulo Zanoni 		}
15999719fb98SPaulo Zanoni 	}
16009719fb98SPaulo Zanoni 
16019719fb98SPaulo Zanoni 	/* check event from PCH */
16029719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
16039719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
16049719fb98SPaulo Zanoni 
16059719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
16069719fb98SPaulo Zanoni 
16079719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
16089719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
16099719fb98SPaulo Zanoni 	}
16109719fb98SPaulo Zanoni }
16119719fb98SPaulo Zanoni 
1612f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1613b1f14ad0SJesse Barnes {
1614b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1615b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1616f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
16170e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1618b1f14ad0SJesse Barnes 
1619b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1620b1f14ad0SJesse Barnes 
16218664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
16228664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1623907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
16248664281bSPaulo Zanoni 
1625b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1626b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1627b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
162823a78516SPaulo Zanoni 	POSTING_READ(DEIER);
16290e43406bSChris Wilson 
163044498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
163144498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
163244498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
163344498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
163444498aeaSPaulo Zanoni 	 * due to its back queue). */
1635ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
163644498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
163744498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
163844498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1639ab5c608bSBen Widawsky 	}
164044498aeaSPaulo Zanoni 
16410e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
16420e43406bSChris Wilson 	if (gt_iir) {
1643d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
16440e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1645d8fc8a47SPaulo Zanoni 		else
1646d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
16470e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
16480e43406bSChris Wilson 		ret = IRQ_HANDLED;
16490e43406bSChris Wilson 	}
1650b1f14ad0SJesse Barnes 
1651b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
16520e43406bSChris Wilson 	if (de_iir) {
1653f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
16549719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1655f1af8fc1SPaulo Zanoni 		else
1656f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
16570e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
16580e43406bSChris Wilson 		ret = IRQ_HANDLED;
16590e43406bSChris Wilson 	}
16600e43406bSChris Wilson 
1661f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
1662f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
16630e43406bSChris Wilson 		if (pm_iir) {
1664d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
1665b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
16660e43406bSChris Wilson 			ret = IRQ_HANDLED;
16670e43406bSChris Wilson 		}
1668f1af8fc1SPaulo Zanoni 	}
1669b1f14ad0SJesse Barnes 
1670b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1671b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1672ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
167344498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
167444498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1675ab5c608bSBen Widawsky 	}
1676b1f14ad0SJesse Barnes 
1677b1f14ad0SJesse Barnes 	return ret;
1678b1f14ad0SJesse Barnes }
1679b1f14ad0SJesse Barnes 
168017e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
168117e1df07SDaniel Vetter 			       bool reset_completed)
168217e1df07SDaniel Vetter {
168317e1df07SDaniel Vetter 	struct intel_ring_buffer *ring;
168417e1df07SDaniel Vetter 	int i;
168517e1df07SDaniel Vetter 
168617e1df07SDaniel Vetter 	/*
168717e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
168817e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
168917e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
169017e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
169117e1df07SDaniel Vetter 	 */
169217e1df07SDaniel Vetter 
169317e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
169417e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
169517e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
169617e1df07SDaniel Vetter 
169717e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
169817e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
169917e1df07SDaniel Vetter 
170017e1df07SDaniel Vetter 	/*
170117e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
170217e1df07SDaniel Vetter 	 * reset state is cleared.
170317e1df07SDaniel Vetter 	 */
170417e1df07SDaniel Vetter 	if (reset_completed)
170517e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
170617e1df07SDaniel Vetter }
170717e1df07SDaniel Vetter 
17088a905236SJesse Barnes /**
17098a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
17108a905236SJesse Barnes  * @work: work struct
17118a905236SJesse Barnes  *
17128a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
17138a905236SJesse Barnes  * was detected.
17148a905236SJesse Barnes  */
17158a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
17168a905236SJesse Barnes {
17171f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
17181f83fee0SDaniel Vetter 						    work);
17191f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
17201f83fee0SDaniel Vetter 						    gpu_error);
17218a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1722cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1723cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1724cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
172517e1df07SDaniel Vetter 	int ret;
17268a905236SJesse Barnes 
1727f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
17288a905236SJesse Barnes 
17297db0ba24SDaniel Vetter 	/*
17307db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
17317db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
17327db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
17337db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
17347db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
17357db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
17367db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
17377db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
17387db0ba24SDaniel Vetter 	 */
17397db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
174044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
17417db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
17427db0ba24SDaniel Vetter 				   reset_event);
17431f83fee0SDaniel Vetter 
174417e1df07SDaniel Vetter 		/*
174517e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
174617e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
174717e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
174817e1df07SDaniel Vetter 		 * deadlocks with the reset work.
174917e1df07SDaniel Vetter 		 */
1750f69061beSDaniel Vetter 		ret = i915_reset(dev);
1751f69061beSDaniel Vetter 
175217e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
175317e1df07SDaniel Vetter 
1754f69061beSDaniel Vetter 		if (ret == 0) {
1755f69061beSDaniel Vetter 			/*
1756f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1757f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1758f69061beSDaniel Vetter 			 * complete.
1759f69061beSDaniel Vetter 			 *
1760f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1761f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1762f69061beSDaniel Vetter 			 * updates before
1763f69061beSDaniel Vetter 			 * the counter increment.
1764f69061beSDaniel Vetter 			 */
1765f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1766f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1767f69061beSDaniel Vetter 
1768f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1769f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
17701f83fee0SDaniel Vetter 		} else {
17711f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1772f316a42cSBen Gamari 		}
17731f83fee0SDaniel Vetter 
177417e1df07SDaniel Vetter 		/*
177517e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
177617e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
177717e1df07SDaniel Vetter 		 */
177817e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
1779f316a42cSBen Gamari 	}
17808a905236SJesse Barnes }
17818a905236SJesse Barnes 
178235aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1783c0e09200SDave Airlie {
17848a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1785bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
178663eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1787050ee91fSBen Widawsky 	int pipe, i;
178863eeaf38SJesse Barnes 
178935aed2e6SChris Wilson 	if (!eir)
179035aed2e6SChris Wilson 		return;
179163eeaf38SJesse Barnes 
1792a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
17938a905236SJesse Barnes 
1794bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1795bd9854f9SBen Widawsky 
17968a905236SJesse Barnes 	if (IS_G4X(dev)) {
17978a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
17988a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
17998a905236SJesse Barnes 
1800a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1801a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1802050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1803050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1804a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1805a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
18068a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
18073143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
18088a905236SJesse Barnes 		}
18098a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
18108a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1811a70491ccSJoe Perches 			pr_err("page table error\n");
1812a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
18138a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
18143143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
18158a905236SJesse Barnes 		}
18168a905236SJesse Barnes 	}
18178a905236SJesse Barnes 
1818a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
181963eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
182063eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1821a70491ccSJoe Perches 			pr_err("page table error\n");
1822a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
182363eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
18243143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
182563eeaf38SJesse Barnes 		}
18268a905236SJesse Barnes 	}
18278a905236SJesse Barnes 
182863eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1829a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
18309db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1831a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
18329db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
183363eeaf38SJesse Barnes 		/* pipestat has already been acked */
183463eeaf38SJesse Barnes 	}
183563eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1836a70491ccSJoe Perches 		pr_err("instruction error\n");
1837a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1838050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1839050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1840a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
184163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
184263eeaf38SJesse Barnes 
1843a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1844a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1845a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
184663eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
18473143a2bfSChris Wilson 			POSTING_READ(IPEIR);
184863eeaf38SJesse Barnes 		} else {
184963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
185063eeaf38SJesse Barnes 
1851a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1852a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1853a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1854a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
185563eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
18563143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
185763eeaf38SJesse Barnes 		}
185863eeaf38SJesse Barnes 	}
185963eeaf38SJesse Barnes 
186063eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
18613143a2bfSChris Wilson 	POSTING_READ(EIR);
186263eeaf38SJesse Barnes 	eir = I915_READ(EIR);
186363eeaf38SJesse Barnes 	if (eir) {
186463eeaf38SJesse Barnes 		/*
186563eeaf38SJesse Barnes 		 * some errors might have become stuck,
186663eeaf38SJesse Barnes 		 * mask them.
186763eeaf38SJesse Barnes 		 */
186863eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
186963eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
187063eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
187163eeaf38SJesse Barnes 	}
187235aed2e6SChris Wilson }
187335aed2e6SChris Wilson 
187435aed2e6SChris Wilson /**
187535aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
187635aed2e6SChris Wilson  * @dev: drm device
187735aed2e6SChris Wilson  *
187835aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
187935aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
188035aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
188135aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
188235aed2e6SChris Wilson  * of a ring dump etc.).
188335aed2e6SChris Wilson  */
1884527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
188535aed2e6SChris Wilson {
188635aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
188735aed2e6SChris Wilson 
188835aed2e6SChris Wilson 	i915_capture_error_state(dev);
188935aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
18908a905236SJesse Barnes 
1891ba1234d1SBen Gamari 	if (wedged) {
1892f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1893f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
1894ba1234d1SBen Gamari 
189511ed50ecSBen Gamari 		/*
189617e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
189717e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
189817e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
189917e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
190017e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
190117e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
190217e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
190317e1df07SDaniel Vetter 		 *
190417e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
190517e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
190617e1df07SDaniel Vetter 		 * counter atomic_t.
190711ed50ecSBen Gamari 		 */
190817e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
190911ed50ecSBen Gamari 	}
191011ed50ecSBen Gamari 
1911122f46baSDaniel Vetter 	/*
1912122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
1913122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
1914122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
1915122f46baSDaniel Vetter 	 * code will deadlock.
1916122f46baSDaniel Vetter 	 */
1917122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
19188a905236SJesse Barnes }
19198a905236SJesse Barnes 
192021ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
19214e5359cdSSimon Farnsworth {
19224e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
19234e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
19244e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
192505394f39SChris Wilson 	struct drm_i915_gem_object *obj;
19264e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
19274e5359cdSSimon Farnsworth 	unsigned long flags;
19284e5359cdSSimon Farnsworth 	bool stall_detected;
19294e5359cdSSimon Farnsworth 
19304e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
19314e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
19324e5359cdSSimon Farnsworth 		return;
19334e5359cdSSimon Farnsworth 
19344e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
19354e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
19364e5359cdSSimon Farnsworth 
1937e7d841caSChris Wilson 	if (work == NULL ||
1938e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1939e7d841caSChris Wilson 	    !work->enable_stall_check) {
19404e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
19414e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
19424e5359cdSSimon Farnsworth 		return;
19434e5359cdSSimon Farnsworth 	}
19444e5359cdSSimon Farnsworth 
19454e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
194605394f39SChris Wilson 	obj = work->pending_flip_obj;
1947a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
19489db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1949446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1950f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
19514e5359cdSSimon Farnsworth 	} else {
19529db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
1953f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
195401f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
19554e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
19564e5359cdSSimon Farnsworth 	}
19574e5359cdSSimon Farnsworth 
19584e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
19594e5359cdSSimon Farnsworth 
19604e5359cdSSimon Farnsworth 	if (stall_detected) {
19614e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
19624e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
19634e5359cdSSimon Farnsworth 	}
19644e5359cdSSimon Farnsworth }
19654e5359cdSSimon Farnsworth 
196642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
196742f52ef8SKeith Packard  * we use as a pipe index
196842f52ef8SKeith Packard  */
1969f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
19700a3e67a4SJesse Barnes {
19710a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1972e9d21d7fSKeith Packard 	unsigned long irqflags;
197371e0ffa5SJesse Barnes 
19745eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
197571e0ffa5SJesse Barnes 		return -EINVAL;
19760a3e67a4SJesse Barnes 
19771ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1978f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
19797c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
19807c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
19810a3e67a4SJesse Barnes 	else
19827c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
19837c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
19848692d00eSChris Wilson 
19858692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
19868692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
19876b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
19881ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
19898692d00eSChris Wilson 
19900a3e67a4SJesse Barnes 	return 0;
19910a3e67a4SJesse Barnes }
19920a3e67a4SJesse Barnes 
1993f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1994f796cf8fSJesse Barnes {
1995f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1996f796cf8fSJesse Barnes 	unsigned long irqflags;
1997b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
1998b518421fSPaulo Zanoni 						     DE_PIPE_VBLANK_ILK(pipe);
1999f796cf8fSJesse Barnes 
2000f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2001f796cf8fSJesse Barnes 		return -EINVAL;
2002f796cf8fSJesse Barnes 
2003f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2004b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2005b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2006b1f14ad0SJesse Barnes 
2007b1f14ad0SJesse Barnes 	return 0;
2008b1f14ad0SJesse Barnes }
2009b1f14ad0SJesse Barnes 
20107e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
20117e231dbeSJesse Barnes {
20127e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20137e231dbeSJesse Barnes 	unsigned long irqflags;
201431acc7f5SJesse Barnes 	u32 imr;
20157e231dbeSJesse Barnes 
20167e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
20177e231dbeSJesse Barnes 		return -EINVAL;
20187e231dbeSJesse Barnes 
20197e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
20207e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
202131acc7f5SJesse Barnes 	if (pipe == 0)
20227e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
202331acc7f5SJesse Barnes 	else
20247e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
20257e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
202631acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
202731acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
20287e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
20297e231dbeSJesse Barnes 
20307e231dbeSJesse Barnes 	return 0;
20317e231dbeSJesse Barnes }
20327e231dbeSJesse Barnes 
203342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
203442f52ef8SKeith Packard  * we use as a pipe index
203542f52ef8SKeith Packard  */
2036f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
20370a3e67a4SJesse Barnes {
20380a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2039e9d21d7fSKeith Packard 	unsigned long irqflags;
20400a3e67a4SJesse Barnes 
20411ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
20428692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
20436b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
20448692d00eSChris Wilson 
20457c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
20467c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
20477c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
20481ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
20490a3e67a4SJesse Barnes }
20500a3e67a4SJesse Barnes 
2051f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2052f796cf8fSJesse Barnes {
2053f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2054f796cf8fSJesse Barnes 	unsigned long irqflags;
2055b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
2056b518421fSPaulo Zanoni 						     DE_PIPE_VBLANK_ILK(pipe);
2057f796cf8fSJesse Barnes 
2058f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2059b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2060b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2061b1f14ad0SJesse Barnes }
2062b1f14ad0SJesse Barnes 
20637e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
20647e231dbeSJesse Barnes {
20657e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20667e231dbeSJesse Barnes 	unsigned long irqflags;
206731acc7f5SJesse Barnes 	u32 imr;
20687e231dbeSJesse Barnes 
20697e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
207031acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
207131acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
20727e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
207331acc7f5SJesse Barnes 	if (pipe == 0)
20747e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
207531acc7f5SJesse Barnes 	else
20767e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
20777e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
20787e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
20797e231dbeSJesse Barnes }
20807e231dbeSJesse Barnes 
2081893eead0SChris Wilson static u32
2082893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2083852835f3SZou Nan hai {
2084893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2085893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2086893eead0SChris Wilson }
2087893eead0SChris Wilson 
20889107e9d2SChris Wilson static bool
20899107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2090893eead0SChris Wilson {
20919107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
20929107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2093f65d9421SBen Gamari }
2094f65d9421SBen Gamari 
20956274f212SChris Wilson static struct intel_ring_buffer *
20966274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2097a24a11e6SChris Wilson {
2098a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
20996274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2100a24a11e6SChris Wilson 
2101a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2102a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2103a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
21046274f212SChris Wilson 		return NULL;
2105a24a11e6SChris Wilson 
2106a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2107a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2108a24a11e6SChris Wilson 	 */
21096274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2110a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2111a24a11e6SChris Wilson 	do {
2112a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2113a24a11e6SChris Wilson 		if (cmd == ipehr)
2114a24a11e6SChris Wilson 			break;
2115a24a11e6SChris Wilson 
2116a24a11e6SChris Wilson 		acthd -= 4;
2117a24a11e6SChris Wilson 		if (acthd < acthd_min)
21186274f212SChris Wilson 			return NULL;
2119a24a11e6SChris Wilson 	} while (1);
2120a24a11e6SChris Wilson 
21216274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
21226274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2123a24a11e6SChris Wilson }
2124a24a11e6SChris Wilson 
21256274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
21266274f212SChris Wilson {
21276274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
21286274f212SChris Wilson 	struct intel_ring_buffer *signaller;
21296274f212SChris Wilson 	u32 seqno, ctl;
21306274f212SChris Wilson 
21316274f212SChris Wilson 	ring->hangcheck.deadlock = true;
21326274f212SChris Wilson 
21336274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
21346274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
21356274f212SChris Wilson 		return -1;
21366274f212SChris Wilson 
21376274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
21386274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
21396274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
21406274f212SChris Wilson 		return -1;
21416274f212SChris Wilson 
21426274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
21436274f212SChris Wilson }
21446274f212SChris Wilson 
21456274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
21466274f212SChris Wilson {
21476274f212SChris Wilson 	struct intel_ring_buffer *ring;
21486274f212SChris Wilson 	int i;
21496274f212SChris Wilson 
21506274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
21516274f212SChris Wilson 		ring->hangcheck.deadlock = false;
21526274f212SChris Wilson }
21536274f212SChris Wilson 
2154ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2155ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
21561ec14ad3SChris Wilson {
21571ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
21581ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
21599107e9d2SChris Wilson 	u32 tmp;
21609107e9d2SChris Wilson 
21616274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2162f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
21636274f212SChris Wilson 
21649107e9d2SChris Wilson 	if (IS_GEN2(dev))
2165f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
21669107e9d2SChris Wilson 
21679107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
21689107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
21699107e9d2SChris Wilson 	 * and break the hang. This should work on
21709107e9d2SChris Wilson 	 * all but the second generation chipsets.
21719107e9d2SChris Wilson 	 */
21729107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
21731ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
21741ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
21751ec14ad3SChris Wilson 			  ring->name);
217609e14bf3SChris Wilson 		i915_handle_error(dev, false);
21771ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2178f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
21791ec14ad3SChris Wilson 	}
2180a24a11e6SChris Wilson 
21816274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
21826274f212SChris Wilson 		switch (semaphore_passed(ring)) {
21836274f212SChris Wilson 		default:
2184f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
21856274f212SChris Wilson 		case 1:
2186a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
2187a24a11e6SChris Wilson 				  ring->name);
218809e14bf3SChris Wilson 			i915_handle_error(dev, false);
2189a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2190f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
21916274f212SChris Wilson 		case 0:
2192f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
21936274f212SChris Wilson 		}
21949107e9d2SChris Wilson 	}
21959107e9d2SChris Wilson 
2196f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2197a24a11e6SChris Wilson }
2198d1e61e7fSChris Wilson 
2199f65d9421SBen Gamari /**
2200f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
220105407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
220205407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
220305407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
220405407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
220505407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2206f65d9421SBen Gamari  */
2207a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2208f65d9421SBen Gamari {
2209f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2210f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2211b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2212b4519513SChris Wilson 	int i;
221305407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
22149107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
22159107e9d2SChris Wilson #define BUSY 1
22169107e9d2SChris Wilson #define KICK 5
22179107e9d2SChris Wilson #define HUNG 20
22189107e9d2SChris Wilson #define FIRE 30
2219893eead0SChris Wilson 
22203e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
22213e0dc6b0SBen Widawsky 		return;
22223e0dc6b0SBen Widawsky 
2223b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
222405407ff8SMika Kuoppala 		u32 seqno, acthd;
22259107e9d2SChris Wilson 		bool busy = true;
2226b4519513SChris Wilson 
22276274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
22286274f212SChris Wilson 
222905407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
223005407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
223105407ff8SMika Kuoppala 
223205407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
22339107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2234da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2235da661464SMika Kuoppala 
22369107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
22379107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2238094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
22399107e9d2SChris Wilson 						DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
22409107e9d2SChris Wilson 							  ring->name);
22419107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2242094f9a54SChris Wilson 					}
2243094f9a54SChris Wilson 					/* Safeguard against driver failure */
2244094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
22459107e9d2SChris Wilson 				} else
22469107e9d2SChris Wilson 					busy = false;
224705407ff8SMika Kuoppala 			} else {
22486274f212SChris Wilson 				/* We always increment the hangcheck score
22496274f212SChris Wilson 				 * if the ring is busy and still processing
22506274f212SChris Wilson 				 * the same request, so that no single request
22516274f212SChris Wilson 				 * can run indefinitely (such as a chain of
22526274f212SChris Wilson 				 * batches). The only time we do not increment
22536274f212SChris Wilson 				 * the hangcheck score on this ring, if this
22546274f212SChris Wilson 				 * ring is in a legitimate wait for another
22556274f212SChris Wilson 				 * ring. In that case the waiting ring is a
22566274f212SChris Wilson 				 * victim and we want to be sure we catch the
22576274f212SChris Wilson 				 * right culprit. Then every time we do kick
22586274f212SChris Wilson 				 * the ring, add a small increment to the
22596274f212SChris Wilson 				 * score so that we can catch a batch that is
22606274f212SChris Wilson 				 * being repeatedly kicked and so responsible
22616274f212SChris Wilson 				 * for stalling the machine.
22629107e9d2SChris Wilson 				 */
2263ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2264ad8beaeaSMika Kuoppala 								    acthd);
2265ad8beaeaSMika Kuoppala 
2266ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2267da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2268f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
22696274f212SChris Wilson 					break;
2270f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2271ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
22726274f212SChris Wilson 					break;
2273f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2274ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
22756274f212SChris Wilson 					break;
2276f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2277ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
22786274f212SChris Wilson 					stuck[i] = true;
22796274f212SChris Wilson 					break;
22806274f212SChris Wilson 				}
228105407ff8SMika Kuoppala 			}
22829107e9d2SChris Wilson 		} else {
2283da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2284da661464SMika Kuoppala 
22859107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
22869107e9d2SChris Wilson 			 * attempts across multiple batches.
22879107e9d2SChris Wilson 			 */
22889107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
22899107e9d2SChris Wilson 				ring->hangcheck.score--;
2290cbb465e7SChris Wilson 		}
2291f65d9421SBen Gamari 
229205407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
229305407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
22949107e9d2SChris Wilson 		busy_count += busy;
229505407ff8SMika Kuoppala 	}
229605407ff8SMika Kuoppala 
229705407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
22989107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2299b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
230005407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2301a43adf07SChris Wilson 				 ring->name);
2302a43adf07SChris Wilson 			rings_hung++;
230305407ff8SMika Kuoppala 		}
230405407ff8SMika Kuoppala 	}
230505407ff8SMika Kuoppala 
230605407ff8SMika Kuoppala 	if (rings_hung)
230705407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
230805407ff8SMika Kuoppala 
230905407ff8SMika Kuoppala 	if (busy_count)
231005407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
231105407ff8SMika Kuoppala 		 * being added */
231210cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
231310cd45b6SMika Kuoppala }
231410cd45b6SMika Kuoppala 
231510cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
231610cd45b6SMika Kuoppala {
231710cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
231810cd45b6SMika Kuoppala 	if (!i915_enable_hangcheck)
231910cd45b6SMika Kuoppala 		return;
232010cd45b6SMika Kuoppala 
232199584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
232210cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2323f65d9421SBen Gamari }
2324f65d9421SBen Gamari 
232591738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
232691738a95SPaulo Zanoni {
232791738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
232891738a95SPaulo Zanoni 
232991738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
233091738a95SPaulo Zanoni 		return;
233191738a95SPaulo Zanoni 
233291738a95SPaulo Zanoni 	/* south display irq */
233391738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
233491738a95SPaulo Zanoni 	/*
233591738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
233691738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
233791738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
233891738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
233991738a95SPaulo Zanoni 	 */
234091738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
234191738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
234291738a95SPaulo Zanoni }
234391738a95SPaulo Zanoni 
2344d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2345d18ea1b5SDaniel Vetter {
2346d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2347d18ea1b5SDaniel Vetter 
2348d18ea1b5SDaniel Vetter 	/* and GT */
2349d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2350d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2351d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2352d18ea1b5SDaniel Vetter 
2353d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2354d18ea1b5SDaniel Vetter 		/* and PM */
2355d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2356d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2357d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2358d18ea1b5SDaniel Vetter 	}
2359d18ea1b5SDaniel Vetter }
2360d18ea1b5SDaniel Vetter 
2361c0e09200SDave Airlie /* drm_dma.h hooks
2362c0e09200SDave Airlie */
2363f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2364036a4a7dSZhenyu Wang {
2365036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2366036a4a7dSZhenyu Wang 
23674697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
23684697995bSJesse Barnes 
2369036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2370bdfcdb63SDaniel Vetter 
2371036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2372036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
23733143a2bfSChris Wilson 	POSTING_READ(DEIER);
2374036a4a7dSZhenyu Wang 
2375d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2376c650156aSZhenyu Wang 
237791738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
23787d99163dSBen Widawsky }
23797d99163dSBen Widawsky 
23807e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
23817e231dbeSJesse Barnes {
23827e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23837e231dbeSJesse Barnes 	int pipe;
23847e231dbeSJesse Barnes 
23857e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
23867e231dbeSJesse Barnes 
23877e231dbeSJesse Barnes 	/* VLV magic */
23887e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
23897e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
23907e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
23917e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
23927e231dbeSJesse Barnes 
23937e231dbeSJesse Barnes 	/* and GT */
23947e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
23957e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2396d18ea1b5SDaniel Vetter 
2397d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
23987e231dbeSJesse Barnes 
23997e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
24007e231dbeSJesse Barnes 
24017e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
24027e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
24037e231dbeSJesse Barnes 	for_each_pipe(pipe)
24047e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
24057e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
24067e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
24077e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
24087e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
24097e231dbeSJesse Barnes }
24107e231dbeSJesse Barnes 
241182a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
241282a28bcfSDaniel Vetter {
241382a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
241482a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
241582a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2416fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
241782a28bcfSDaniel Vetter 
241882a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2419fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
242082a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2421cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2422fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
242382a28bcfSDaniel Vetter 	} else {
2424fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
242582a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2426cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2427fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
242882a28bcfSDaniel Vetter 	}
242982a28bcfSDaniel Vetter 
2430fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
243182a28bcfSDaniel Vetter 
24327fe0b973SKeith Packard 	/*
24337fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
24347fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
24357fe0b973SKeith Packard 	 *
24367fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
24377fe0b973SKeith Packard 	 */
24387fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
24397fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
24407fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
24417fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
24427fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
24437fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
24447fe0b973SKeith Packard }
24457fe0b973SKeith Packard 
2446d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2447d46da437SPaulo Zanoni {
2448d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
244982a28bcfSDaniel Vetter 	u32 mask;
2450d46da437SPaulo Zanoni 
2451692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2452692a04cfSDaniel Vetter 		return;
2453692a04cfSDaniel Vetter 
24548664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
24558664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2456de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
24578664281bSPaulo Zanoni 	} else {
24588664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
24598664281bSPaulo Zanoni 
24608664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
24618664281bSPaulo Zanoni 	}
2462ab5c608bSBen Widawsky 
2463d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2464d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2465d46da437SPaulo Zanoni }
2466d46da437SPaulo Zanoni 
24670a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
24680a9a8c91SDaniel Vetter {
24690a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
24700a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
24710a9a8c91SDaniel Vetter 
24720a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
24730a9a8c91SDaniel Vetter 
24740a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
2475040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
24760a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
247735a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
247835a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
24790a9a8c91SDaniel Vetter 	}
24800a9a8c91SDaniel Vetter 
24810a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
24820a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
24830a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
24840a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
24850a9a8c91SDaniel Vetter 	} else {
24860a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
24870a9a8c91SDaniel Vetter 	}
24880a9a8c91SDaniel Vetter 
24890a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
24900a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
24910a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
24920a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
24930a9a8c91SDaniel Vetter 
24940a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
24950a9a8c91SDaniel Vetter 		pm_irqs |= GEN6_PM_RPS_EVENTS;
24960a9a8c91SDaniel Vetter 
24970a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
24980a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
24990a9a8c91SDaniel Vetter 
2500605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
25010a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2502605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
25030a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
25040a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
25050a9a8c91SDaniel Vetter 	}
25060a9a8c91SDaniel Vetter }
25070a9a8c91SDaniel Vetter 
2508f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2509036a4a7dSZhenyu Wang {
25104bc9d430SDaniel Vetter 	unsigned long irqflags;
2511036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25128e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
25138e76f8dcSPaulo Zanoni 
25148e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
25158e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
25168e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
25178e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
25188e76f8dcSPaulo Zanoni 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
25198e76f8dcSPaulo Zanoni 				DE_ERR_INT_IVB);
25208e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
25218e76f8dcSPaulo Zanoni 			      DE_PIPEA_VBLANK_IVB);
25228e76f8dcSPaulo Zanoni 
25238e76f8dcSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
25248e76f8dcSPaulo Zanoni 	} else {
25258e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2526ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
25275b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
25285b3a856bSDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
25295b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
25305b3a856bSDaniel Vetter 				DE_POISON);
25318e76f8dcSPaulo Zanoni 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
25328e76f8dcSPaulo Zanoni 	}
2533036a4a7dSZhenyu Wang 
25341ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2535036a4a7dSZhenyu Wang 
2536036a4a7dSZhenyu Wang 	/* should always can generate irq */
2537036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
25381ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
25398e76f8dcSPaulo Zanoni 	I915_WRITE(DEIER, display_mask | extra_mask);
25403143a2bfSChris Wilson 	POSTING_READ(DEIER);
2541036a4a7dSZhenyu Wang 
25420a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
2543036a4a7dSZhenyu Wang 
2544d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
25457fe0b973SKeith Packard 
2546f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
25476005ce42SDaniel Vetter 		/* Enable PCU event interrupts
25486005ce42SDaniel Vetter 		 *
25496005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
25504bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
25514bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
25524bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2553f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
25544bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2555f97108d1SJesse Barnes 	}
2556f97108d1SJesse Barnes 
2557036a4a7dSZhenyu Wang 	return 0;
2558036a4a7dSZhenyu Wang }
2559036a4a7dSZhenyu Wang 
25607e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
25617e231dbeSJesse Barnes {
25627e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25637e231dbeSJesse Barnes 	u32 enable_mask;
256431acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
2565b79480baSDaniel Vetter 	unsigned long irqflags;
25667e231dbeSJesse Barnes 
25677e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
256831acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
256931acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
257031acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
25717e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
25727e231dbeSJesse Barnes 
257331acc7f5SJesse Barnes 	/*
257431acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
257531acc7f5SJesse Barnes 	 * toggle them based on usage.
257631acc7f5SJesse Barnes 	 */
257731acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
257831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
257931acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
25807e231dbeSJesse Barnes 
258120afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
258220afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
258320afbda2SDaniel Vetter 
25847e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
25857e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
25867e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
25877e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
25887e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
25897e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
25907e231dbeSJesse Barnes 
2591b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2592b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2593b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
259431acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2595515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
259631acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
2597b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
259831acc7f5SJesse Barnes 
25997e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26007e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26017e231dbeSJesse Barnes 
26020a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
26037e231dbeSJesse Barnes 
26047e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
26057e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
26067e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
26077e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
26087e231dbeSJesse Barnes #endif
26097e231dbeSJesse Barnes 
26107e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
261120afbda2SDaniel Vetter 
261220afbda2SDaniel Vetter 	return 0;
261320afbda2SDaniel Vetter }
261420afbda2SDaniel Vetter 
26157e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
26167e231dbeSJesse Barnes {
26177e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26187e231dbeSJesse Barnes 	int pipe;
26197e231dbeSJesse Barnes 
26207e231dbeSJesse Barnes 	if (!dev_priv)
26217e231dbeSJesse Barnes 		return;
26227e231dbeSJesse Barnes 
2623ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2624ac4c16c5SEgbert Eich 
26257e231dbeSJesse Barnes 	for_each_pipe(pipe)
26267e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
26277e231dbeSJesse Barnes 
26287e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
26297e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
26307e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
26317e231dbeSJesse Barnes 	for_each_pipe(pipe)
26327e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
26337e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26347e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
26357e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
26367e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
26377e231dbeSJesse Barnes }
26387e231dbeSJesse Barnes 
2639f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2640036a4a7dSZhenyu Wang {
2641036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26424697995bSJesse Barnes 
26434697995bSJesse Barnes 	if (!dev_priv)
26444697995bSJesse Barnes 		return;
26454697995bSJesse Barnes 
2646ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2647ac4c16c5SEgbert Eich 
2648036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2649036a4a7dSZhenyu Wang 
2650036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2651036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2652036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
26538664281bSPaulo Zanoni 	if (IS_GEN7(dev))
26548664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2655036a4a7dSZhenyu Wang 
2656036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2657036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2658036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2659192aac1fSKeith Packard 
2660ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2661ab5c608bSBen Widawsky 		return;
2662ab5c608bSBen Widawsky 
2663192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2664192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2665192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
26668664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
26678664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2668036a4a7dSZhenyu Wang }
2669036a4a7dSZhenyu Wang 
2670c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2671c2798b19SChris Wilson {
2672c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2673c2798b19SChris Wilson 	int pipe;
2674c2798b19SChris Wilson 
2675c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2676c2798b19SChris Wilson 
2677c2798b19SChris Wilson 	for_each_pipe(pipe)
2678c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2679c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2680c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2681c2798b19SChris Wilson 	POSTING_READ16(IER);
2682c2798b19SChris Wilson }
2683c2798b19SChris Wilson 
2684c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2685c2798b19SChris Wilson {
2686c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2687c2798b19SChris Wilson 
2688c2798b19SChris Wilson 	I915_WRITE16(EMR,
2689c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2690c2798b19SChris Wilson 
2691c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2692c2798b19SChris Wilson 	dev_priv->irq_mask =
2693c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2694c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2695c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2696c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2697c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2698c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2699c2798b19SChris Wilson 
2700c2798b19SChris Wilson 	I915_WRITE16(IER,
2701c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2702c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2703c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2704c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2705c2798b19SChris Wilson 	POSTING_READ16(IER);
2706c2798b19SChris Wilson 
2707c2798b19SChris Wilson 	return 0;
2708c2798b19SChris Wilson }
2709c2798b19SChris Wilson 
271090a72f87SVille Syrjälä /*
271190a72f87SVille Syrjälä  * Returns true when a page flip has completed.
271290a72f87SVille Syrjälä  */
271390a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
271490a72f87SVille Syrjälä 			       int pipe, u16 iir)
271590a72f87SVille Syrjälä {
271690a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
271790a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
271890a72f87SVille Syrjälä 
271990a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
272090a72f87SVille Syrjälä 		return false;
272190a72f87SVille Syrjälä 
272290a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
272390a72f87SVille Syrjälä 		return false;
272490a72f87SVille Syrjälä 
272590a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
272690a72f87SVille Syrjälä 
272790a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
272890a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
272990a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
273090a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
273190a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
273290a72f87SVille Syrjälä 	 */
273390a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
273490a72f87SVille Syrjälä 		return false;
273590a72f87SVille Syrjälä 
273690a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
273790a72f87SVille Syrjälä 
273890a72f87SVille Syrjälä 	return true;
273990a72f87SVille Syrjälä }
274090a72f87SVille Syrjälä 
2741ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2742c2798b19SChris Wilson {
2743c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2744c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2745c2798b19SChris Wilson 	u16 iir, new_iir;
2746c2798b19SChris Wilson 	u32 pipe_stats[2];
2747c2798b19SChris Wilson 	unsigned long irqflags;
2748c2798b19SChris Wilson 	int pipe;
2749c2798b19SChris Wilson 	u16 flip_mask =
2750c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2751c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2752c2798b19SChris Wilson 
2753c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2754c2798b19SChris Wilson 
2755c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2756c2798b19SChris Wilson 	if (iir == 0)
2757c2798b19SChris Wilson 		return IRQ_NONE;
2758c2798b19SChris Wilson 
2759c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2760c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2761c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2762c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2763c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2764c2798b19SChris Wilson 		 */
2765c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2766c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2767c2798b19SChris Wilson 			i915_handle_error(dev, false);
2768c2798b19SChris Wilson 
2769c2798b19SChris Wilson 		for_each_pipe(pipe) {
2770c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2771c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2772c2798b19SChris Wilson 
2773c2798b19SChris Wilson 			/*
2774c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2775c2798b19SChris Wilson 			 */
2776c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2777c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2778c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2779c2798b19SChris Wilson 							 pipe_name(pipe));
2780c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2781c2798b19SChris Wilson 			}
2782c2798b19SChris Wilson 		}
2783c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2784c2798b19SChris Wilson 
2785c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2786c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2787c2798b19SChris Wilson 
2788d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2789c2798b19SChris Wilson 
2790c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2791c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2792c2798b19SChris Wilson 
2793c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
279490a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
279590a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2796c2798b19SChris Wilson 
2797c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
279890a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
279990a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2800c2798b19SChris Wilson 
2801c2798b19SChris Wilson 		iir = new_iir;
2802c2798b19SChris Wilson 	}
2803c2798b19SChris Wilson 
2804c2798b19SChris Wilson 	return IRQ_HANDLED;
2805c2798b19SChris Wilson }
2806c2798b19SChris Wilson 
2807c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2808c2798b19SChris Wilson {
2809c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2810c2798b19SChris Wilson 	int pipe;
2811c2798b19SChris Wilson 
2812c2798b19SChris Wilson 	for_each_pipe(pipe) {
2813c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2814c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2815c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2816c2798b19SChris Wilson 	}
2817c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2818c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2819c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2820c2798b19SChris Wilson }
2821c2798b19SChris Wilson 
2822a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2823a266c7d5SChris Wilson {
2824a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2825a266c7d5SChris Wilson 	int pipe;
2826a266c7d5SChris Wilson 
2827a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2828a266c7d5SChris Wilson 
2829a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2830a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2831a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2832a266c7d5SChris Wilson 	}
2833a266c7d5SChris Wilson 
283400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2835a266c7d5SChris Wilson 	for_each_pipe(pipe)
2836a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2837a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2838a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2839a266c7d5SChris Wilson 	POSTING_READ(IER);
2840a266c7d5SChris Wilson }
2841a266c7d5SChris Wilson 
2842a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2843a266c7d5SChris Wilson {
2844a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
284538bde180SChris Wilson 	u32 enable_mask;
2846a266c7d5SChris Wilson 
284738bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
284838bde180SChris Wilson 
284938bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
285038bde180SChris Wilson 	dev_priv->irq_mask =
285138bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
285238bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
285338bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
285438bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
285538bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
285638bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
285738bde180SChris Wilson 
285838bde180SChris Wilson 	enable_mask =
285938bde180SChris Wilson 		I915_ASLE_INTERRUPT |
286038bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
286138bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
286238bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
286338bde180SChris Wilson 		I915_USER_INTERRUPT;
286438bde180SChris Wilson 
2865a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
286620afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
286720afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
286820afbda2SDaniel Vetter 
2869a266c7d5SChris Wilson 		/* Enable in IER... */
2870a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2871a266c7d5SChris Wilson 		/* and unmask in IMR */
2872a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2873a266c7d5SChris Wilson 	}
2874a266c7d5SChris Wilson 
2875a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2876a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2877a266c7d5SChris Wilson 	POSTING_READ(IER);
2878a266c7d5SChris Wilson 
2879f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
288020afbda2SDaniel Vetter 
288120afbda2SDaniel Vetter 	return 0;
288220afbda2SDaniel Vetter }
288320afbda2SDaniel Vetter 
288490a72f87SVille Syrjälä /*
288590a72f87SVille Syrjälä  * Returns true when a page flip has completed.
288690a72f87SVille Syrjälä  */
288790a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
288890a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
288990a72f87SVille Syrjälä {
289090a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
289190a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
289290a72f87SVille Syrjälä 
289390a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
289490a72f87SVille Syrjälä 		return false;
289590a72f87SVille Syrjälä 
289690a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
289790a72f87SVille Syrjälä 		return false;
289890a72f87SVille Syrjälä 
289990a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
290090a72f87SVille Syrjälä 
290190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
290290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
290390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
290490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
290590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
290690a72f87SVille Syrjälä 	 */
290790a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
290890a72f87SVille Syrjälä 		return false;
290990a72f87SVille Syrjälä 
291090a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
291190a72f87SVille Syrjälä 
291290a72f87SVille Syrjälä 	return true;
291390a72f87SVille Syrjälä }
291490a72f87SVille Syrjälä 
2915ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2916a266c7d5SChris Wilson {
2917a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2918a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29198291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2920a266c7d5SChris Wilson 	unsigned long irqflags;
292138bde180SChris Wilson 	u32 flip_mask =
292238bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
292338bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
292438bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2925a266c7d5SChris Wilson 
2926a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2927a266c7d5SChris Wilson 
2928a266c7d5SChris Wilson 	iir = I915_READ(IIR);
292938bde180SChris Wilson 	do {
293038bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
29318291ee90SChris Wilson 		bool blc_event = false;
2932a266c7d5SChris Wilson 
2933a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2934a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2935a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2936a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2937a266c7d5SChris Wilson 		 */
2938a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2939a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2940a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2941a266c7d5SChris Wilson 
2942a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2943a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2944a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2945a266c7d5SChris Wilson 
294638bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2947a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2948a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2949a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2950a266c7d5SChris Wilson 							 pipe_name(pipe));
2951a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
295238bde180SChris Wilson 				irq_received = true;
2953a266c7d5SChris Wilson 			}
2954a266c7d5SChris Wilson 		}
2955a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2956a266c7d5SChris Wilson 
2957a266c7d5SChris Wilson 		if (!irq_received)
2958a266c7d5SChris Wilson 			break;
2959a266c7d5SChris Wilson 
2960a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2961a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2962a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2963a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2964b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
2965a266c7d5SChris Wilson 
2966a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2967a266c7d5SChris Wilson 				  hotplug_status);
296891d131d2SDaniel Vetter 
296910a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
297091d131d2SDaniel Vetter 
2971a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
297238bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2973a266c7d5SChris Wilson 		}
2974a266c7d5SChris Wilson 
297538bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2976a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2977a266c7d5SChris Wilson 
2978a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2979a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2980a266c7d5SChris Wilson 
2981a266c7d5SChris Wilson 		for_each_pipe(pipe) {
298238bde180SChris Wilson 			int plane = pipe;
298338bde180SChris Wilson 			if (IS_MOBILE(dev))
298438bde180SChris Wilson 				plane = !plane;
29855e2032d4SVille Syrjälä 
298690a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
298790a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
298890a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
2989a266c7d5SChris Wilson 
2990a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2991a266c7d5SChris Wilson 				blc_event = true;
2992a266c7d5SChris Wilson 		}
2993a266c7d5SChris Wilson 
2994a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2995a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2996a266c7d5SChris Wilson 
2997a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2998a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2999a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3000a266c7d5SChris Wilson 		 * we would never get another interrupt.
3001a266c7d5SChris Wilson 		 *
3002a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3003a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3004a266c7d5SChris Wilson 		 * another one.
3005a266c7d5SChris Wilson 		 *
3006a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3007a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3008a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3009a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3010a266c7d5SChris Wilson 		 * stray interrupts.
3011a266c7d5SChris Wilson 		 */
301238bde180SChris Wilson 		ret = IRQ_HANDLED;
3013a266c7d5SChris Wilson 		iir = new_iir;
301438bde180SChris Wilson 	} while (iir & ~flip_mask);
3015a266c7d5SChris Wilson 
3016d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
30178291ee90SChris Wilson 
3018a266c7d5SChris Wilson 	return ret;
3019a266c7d5SChris Wilson }
3020a266c7d5SChris Wilson 
3021a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3022a266c7d5SChris Wilson {
3023a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3024a266c7d5SChris Wilson 	int pipe;
3025a266c7d5SChris Wilson 
3026ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3027ac4c16c5SEgbert Eich 
3028a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3029a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3030a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3031a266c7d5SChris Wilson 	}
3032a266c7d5SChris Wilson 
303300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
303455b39755SChris Wilson 	for_each_pipe(pipe) {
303555b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3036a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
303755b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
303855b39755SChris Wilson 	}
3039a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3040a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3041a266c7d5SChris Wilson 
3042a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3043a266c7d5SChris Wilson }
3044a266c7d5SChris Wilson 
3045a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3046a266c7d5SChris Wilson {
3047a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3048a266c7d5SChris Wilson 	int pipe;
3049a266c7d5SChris Wilson 
3050a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3051a266c7d5SChris Wilson 
3052a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3053a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3054a266c7d5SChris Wilson 
3055a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3056a266c7d5SChris Wilson 	for_each_pipe(pipe)
3057a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3058a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3059a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3060a266c7d5SChris Wilson 	POSTING_READ(IER);
3061a266c7d5SChris Wilson }
3062a266c7d5SChris Wilson 
3063a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3064a266c7d5SChris Wilson {
3065a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3066bbba0a97SChris Wilson 	u32 enable_mask;
3067a266c7d5SChris Wilson 	u32 error_mask;
3068b79480baSDaniel Vetter 	unsigned long irqflags;
3069a266c7d5SChris Wilson 
3070a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3071bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3072adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3073bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3074bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3075bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3076bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3077bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3078bbba0a97SChris Wilson 
3079bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
308021ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
308121ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3082bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3083bbba0a97SChris Wilson 
3084bbba0a97SChris Wilson 	if (IS_G4X(dev))
3085bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3086a266c7d5SChris Wilson 
3087b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3088b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3089b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3090515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
3091b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3092a266c7d5SChris Wilson 
3093a266c7d5SChris Wilson 	/*
3094a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3095a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3096a266c7d5SChris Wilson 	 */
3097a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3098a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3099a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3100a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3101a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3102a266c7d5SChris Wilson 	} else {
3103a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3104a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3105a266c7d5SChris Wilson 	}
3106a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3107a266c7d5SChris Wilson 
3108a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3109a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3110a266c7d5SChris Wilson 	POSTING_READ(IER);
3111a266c7d5SChris Wilson 
311220afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
311320afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
311420afbda2SDaniel Vetter 
3115f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
311620afbda2SDaniel Vetter 
311720afbda2SDaniel Vetter 	return 0;
311820afbda2SDaniel Vetter }
311920afbda2SDaniel Vetter 
3120bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
312120afbda2SDaniel Vetter {
312220afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3123e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3124cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
312520afbda2SDaniel Vetter 	u32 hotplug_en;
312620afbda2SDaniel Vetter 
3127b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3128b5ea2d56SDaniel Vetter 
3129bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3130bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3131bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3132adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3133e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3134cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3135cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3136cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3137a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3138a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3139a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3140a266c7d5SChris Wilson 		*/
3141a266c7d5SChris Wilson 		if (IS_G4X(dev))
3142a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
314385fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3144a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3145a266c7d5SChris Wilson 
3146a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3147a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3148a266c7d5SChris Wilson 	}
3149bac56d5bSEgbert Eich }
3150a266c7d5SChris Wilson 
3151ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3152a266c7d5SChris Wilson {
3153a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3154a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3155a266c7d5SChris Wilson 	u32 iir, new_iir;
3156a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3157a266c7d5SChris Wilson 	unsigned long irqflags;
3158a266c7d5SChris Wilson 	int irq_received;
3159a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
316021ad8330SVille Syrjälä 	u32 flip_mask =
316121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
316221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3163a266c7d5SChris Wilson 
3164a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3165a266c7d5SChris Wilson 
3166a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3167a266c7d5SChris Wilson 
3168a266c7d5SChris Wilson 	for (;;) {
31692c8ba29fSChris Wilson 		bool blc_event = false;
31702c8ba29fSChris Wilson 
317121ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3172a266c7d5SChris Wilson 
3173a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3174a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3175a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3176a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3177a266c7d5SChris Wilson 		 */
3178a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3179a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3180a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3181a266c7d5SChris Wilson 
3182a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3183a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3184a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3185a266c7d5SChris Wilson 
3186a266c7d5SChris Wilson 			/*
3187a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3188a266c7d5SChris Wilson 			 */
3189a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3190a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3191a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3192a266c7d5SChris Wilson 							 pipe_name(pipe));
3193a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3194a266c7d5SChris Wilson 				irq_received = 1;
3195a266c7d5SChris Wilson 			}
3196a266c7d5SChris Wilson 		}
3197a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3198a266c7d5SChris Wilson 
3199a266c7d5SChris Wilson 		if (!irq_received)
3200a266c7d5SChris Wilson 			break;
3201a266c7d5SChris Wilson 
3202a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3203a266c7d5SChris Wilson 
3204a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3205adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3206a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3207b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3208b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
32094f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3210a266c7d5SChris Wilson 
3211a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3212a266c7d5SChris Wilson 				  hotplug_status);
321391d131d2SDaniel Vetter 
321410a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
321510a504deSDaniel Vetter 					      IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
321691d131d2SDaniel Vetter 
3217a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3218a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3219a266c7d5SChris Wilson 		}
3220a266c7d5SChris Wilson 
322121ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3222a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3223a266c7d5SChris Wilson 
3224a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3225a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3226a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3227a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3228a266c7d5SChris Wilson 
3229a266c7d5SChris Wilson 		for_each_pipe(pipe) {
32302c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
323190a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
323290a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3233a266c7d5SChris Wilson 
3234a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3235a266c7d5SChris Wilson 				blc_event = true;
3236a266c7d5SChris Wilson 		}
3237a266c7d5SChris Wilson 
3238a266c7d5SChris Wilson 
3239a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3240a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3241a266c7d5SChris Wilson 
3242515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3243515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3244515ac2bbSDaniel Vetter 
3245a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3246a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3247a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3248a266c7d5SChris Wilson 		 * we would never get another interrupt.
3249a266c7d5SChris Wilson 		 *
3250a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3251a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3252a266c7d5SChris Wilson 		 * another one.
3253a266c7d5SChris Wilson 		 *
3254a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3255a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3256a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3257a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3258a266c7d5SChris Wilson 		 * stray interrupts.
3259a266c7d5SChris Wilson 		 */
3260a266c7d5SChris Wilson 		iir = new_iir;
3261a266c7d5SChris Wilson 	}
3262a266c7d5SChris Wilson 
3263d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
32642c8ba29fSChris Wilson 
3265a266c7d5SChris Wilson 	return ret;
3266a266c7d5SChris Wilson }
3267a266c7d5SChris Wilson 
3268a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3269a266c7d5SChris Wilson {
3270a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3271a266c7d5SChris Wilson 	int pipe;
3272a266c7d5SChris Wilson 
3273a266c7d5SChris Wilson 	if (!dev_priv)
3274a266c7d5SChris Wilson 		return;
3275a266c7d5SChris Wilson 
3276ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3277ac4c16c5SEgbert Eich 
3278a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3279a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3280a266c7d5SChris Wilson 
3281a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3282a266c7d5SChris Wilson 	for_each_pipe(pipe)
3283a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3284a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3285a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3286a266c7d5SChris Wilson 
3287a266c7d5SChris Wilson 	for_each_pipe(pipe)
3288a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3289a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3290a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3291a266c7d5SChris Wilson }
3292a266c7d5SChris Wilson 
3293ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3294ac4c16c5SEgbert Eich {
3295ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3296ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3297ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3298ac4c16c5SEgbert Eich 	unsigned long irqflags;
3299ac4c16c5SEgbert Eich 	int i;
3300ac4c16c5SEgbert Eich 
3301ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3302ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3303ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3304ac4c16c5SEgbert Eich 
3305ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3306ac4c16c5SEgbert Eich 			continue;
3307ac4c16c5SEgbert Eich 
3308ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3309ac4c16c5SEgbert Eich 
3310ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3311ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3312ac4c16c5SEgbert Eich 
3313ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3314ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3315ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3316ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3317ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3318ac4c16c5SEgbert Eich 				if (!connector->polled)
3319ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3320ac4c16c5SEgbert Eich 			}
3321ac4c16c5SEgbert Eich 		}
3322ac4c16c5SEgbert Eich 	}
3323ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3324ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3325ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3326ac4c16c5SEgbert Eich }
3327ac4c16c5SEgbert Eich 
3328f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3329f71d4af4SJesse Barnes {
33308b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
33318b2e326dSChris Wilson 
33328b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
333399584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3334c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3335a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
33368b2e326dSChris Wilson 
333799584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
333899584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
333961bac78eSDaniel Vetter 		    (unsigned long) dev);
3340ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3341ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
334261bac78eSDaniel Vetter 
334397a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
33449ee32feaSDaniel Vetter 
33454cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
33464cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
33474cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
33484cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3349f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3350f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3351391f75e2SVille Syrjälä 	} else {
3352391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
3353391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
3354f71d4af4SJesse Barnes 	}
3355f71d4af4SJesse Barnes 
3356c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
3357f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3358f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3359c2baf4b7SVille Syrjälä 	}
3360f71d4af4SJesse Barnes 
33617e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
33627e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
33637e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
33647e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
33657e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
33667e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
33677e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3368fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3369f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3370f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3371f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3372f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3373f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3374f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3375f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
337682a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3377f71d4af4SJesse Barnes 	} else {
3378c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3379c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3380c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3381c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3382c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3383a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3384a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3385a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3386a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3387a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
338820afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3389c2798b19SChris Wilson 		} else {
3390a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3391a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3392a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3393a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3394bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3395c2798b19SChris Wilson 		}
3396f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3397f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3398f71d4af4SJesse Barnes 	}
3399f71d4af4SJesse Barnes }
340020afbda2SDaniel Vetter 
340120afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
340220afbda2SDaniel Vetter {
340320afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3404821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3405821450c6SEgbert Eich 	struct drm_connector *connector;
3406b5ea2d56SDaniel Vetter 	unsigned long irqflags;
3407821450c6SEgbert Eich 	int i;
340820afbda2SDaniel Vetter 
3409821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3410821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3411821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3412821450c6SEgbert Eich 	}
3413821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3414821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3415821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3416821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3417821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3418821450c6SEgbert Eich 	}
3419b5ea2d56SDaniel Vetter 
3420b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3421b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
3422b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
342320afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
342420afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
3425b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
342620afbda2SDaniel Vetter }
3427c67a470bSPaulo Zanoni 
3428c67a470bSPaulo Zanoni /* Disable interrupts so we can allow Package C8+. */
3429c67a470bSPaulo Zanoni void hsw_pc8_disable_interrupts(struct drm_device *dev)
3430c67a470bSPaulo Zanoni {
3431c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3432c67a470bSPaulo Zanoni 	unsigned long irqflags;
3433c67a470bSPaulo Zanoni 
3434c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3435c67a470bSPaulo Zanoni 
3436c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.deimr = I915_READ(DEIMR);
3437c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.sdeimr = I915_READ(SDEIMR);
3438c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtimr = I915_READ(GTIMR);
3439c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtier = I915_READ(GTIER);
3440c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
3441c67a470bSPaulo Zanoni 
3442c67a470bSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, ~DE_PCH_EVENT_IVB);
3443c67a470bSPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, ~SDE_HOTPLUG_MASK_CPT);
3444c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
3445c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
3446c67a470bSPaulo Zanoni 
3447c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = true;
3448c67a470bSPaulo Zanoni 
3449c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3450c67a470bSPaulo Zanoni }
3451c67a470bSPaulo Zanoni 
3452c67a470bSPaulo Zanoni /* Restore interrupts so we can recover from Package C8+. */
3453c67a470bSPaulo Zanoni void hsw_pc8_restore_interrupts(struct drm_device *dev)
3454c67a470bSPaulo Zanoni {
3455c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3456c67a470bSPaulo Zanoni 	unsigned long irqflags;
3457c67a470bSPaulo Zanoni 	uint32_t val, expected;
3458c67a470bSPaulo Zanoni 
3459c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3460c67a470bSPaulo Zanoni 
3461c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
3462c67a470bSPaulo Zanoni 	expected = ~DE_PCH_EVENT_IVB;
3463c67a470bSPaulo Zanoni 	WARN(val != expected, "DEIMR is 0x%08x, not 0x%08x\n", val, expected);
3464c67a470bSPaulo Zanoni 
3465c67a470bSPaulo Zanoni 	val = I915_READ(SDEIMR) & ~SDE_HOTPLUG_MASK_CPT;
3466c67a470bSPaulo Zanoni 	expected = ~SDE_HOTPLUG_MASK_CPT;
3467c67a470bSPaulo Zanoni 	WARN(val != expected, "SDEIMR non-HPD bits are 0x%08x, not 0x%08x\n",
3468c67a470bSPaulo Zanoni 	     val, expected);
3469c67a470bSPaulo Zanoni 
3470c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
3471c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3472c67a470bSPaulo Zanoni 	WARN(val != expected, "GTIMR is 0x%08x, not 0x%08x\n", val, expected);
3473c67a470bSPaulo Zanoni 
3474c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
3475c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3476c67a470bSPaulo Zanoni 	WARN(val != expected, "GEN6_PMIMR is 0x%08x, not 0x%08x\n", val,
3477c67a470bSPaulo Zanoni 	     expected);
3478c67a470bSPaulo Zanoni 
3479c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = false;
3480c67a470bSPaulo Zanoni 
3481c67a470bSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pc8.regsave.deimr);
3482c67a470bSPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv,
3483c67a470bSPaulo Zanoni 				     ~dev_priv->pc8.regsave.sdeimr &
3484c67a470bSPaulo Zanoni 				     ~SDE_HOTPLUG_MASK_CPT);
3485c67a470bSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pc8.regsave.gtimr);
3486c67a470bSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pc8.regsave.gen6_pmimr);
3487c67a470bSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pc8.regsave.gtier);
3488c67a470bSPaulo Zanoni 
3489c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3490c67a470bSPaulo Zanoni }
3491