xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision da66146425c3136943452988afd3d64cd551da58)
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>
33760285e7SDavid Howells #include <drm/drmP.h>
34760285e7SDavid Howells #include <drm/i915_drm.h>
35c0e09200SDave Airlie #include "i915_drv.h"
361c5d22f7SChris Wilson #include "i915_trace.h"
3779e53945SJesse Barnes #include "intel_drv.h"
38c0e09200SDave Airlie 
39e5868a31SEgbert Eich static const u32 hpd_ibx[] = {
40e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
41e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
42e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
43e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
44e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
45e5868a31SEgbert Eich };
46e5868a31SEgbert Eich 
47e5868a31SEgbert Eich static const u32 hpd_cpt[] = {
48e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
4973c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
50e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
51e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
52e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
53e5868a31SEgbert Eich };
54e5868a31SEgbert Eich 
55e5868a31SEgbert Eich static const u32 hpd_mask_i915[] = {
56e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
57e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
58e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
59e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
60e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
61e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
62e5868a31SEgbert Eich };
63e5868a31SEgbert Eich 
64e5868a31SEgbert Eich static const u32 hpd_status_gen4[] = {
65e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
66e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
67e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
68e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
69e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
70e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
71e5868a31SEgbert Eich };
72e5868a31SEgbert Eich 
73e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
74e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
75e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
76e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
77e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
78e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
79e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
80e5868a31SEgbert Eich };
81e5868a31SEgbert Eich 
82036a4a7dSZhenyu Wang /* For display hotplug interrupt */
83995b6762SChris Wilson static void
84f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
85036a4a7dSZhenyu Wang {
864bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
874bc9d430SDaniel Vetter 
88c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
89c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
90c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.deimr &= ~mask;
91c67a470bSPaulo Zanoni 		return;
92c67a470bSPaulo Zanoni 	}
93c67a470bSPaulo Zanoni 
941ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
951ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
961ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
973143a2bfSChris Wilson 		POSTING_READ(DEIMR);
98036a4a7dSZhenyu Wang 	}
99036a4a7dSZhenyu Wang }
100036a4a7dSZhenyu Wang 
1010ff9800aSPaulo Zanoni static void
102f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
103036a4a7dSZhenyu Wang {
1044bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1054bc9d430SDaniel Vetter 
106c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
107c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
108c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.deimr |= mask;
109c67a470bSPaulo Zanoni 		return;
110c67a470bSPaulo Zanoni 	}
111c67a470bSPaulo Zanoni 
1121ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1131ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1141ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1153143a2bfSChris Wilson 		POSTING_READ(DEIMR);
116036a4a7dSZhenyu Wang 	}
117036a4a7dSZhenyu Wang }
118036a4a7dSZhenyu Wang 
11943eaea13SPaulo Zanoni /**
12043eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
12143eaea13SPaulo Zanoni  * @dev_priv: driver private
12243eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
12343eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
12443eaea13SPaulo Zanoni  */
12543eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
12643eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
12743eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
12843eaea13SPaulo Zanoni {
12943eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
13043eaea13SPaulo Zanoni 
131c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
132c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
133c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gtimr &= ~interrupt_mask;
134c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gtimr |= (~enabled_irq_mask &
135c67a470bSPaulo Zanoni 						interrupt_mask);
136c67a470bSPaulo Zanoni 		return;
137c67a470bSPaulo Zanoni 	}
138c67a470bSPaulo Zanoni 
13943eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
14043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
14143eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
14243eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
14343eaea13SPaulo Zanoni }
14443eaea13SPaulo Zanoni 
14543eaea13SPaulo Zanoni void ilk_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
14643eaea13SPaulo Zanoni {
14743eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
14843eaea13SPaulo Zanoni }
14943eaea13SPaulo Zanoni 
15043eaea13SPaulo Zanoni void ilk_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
15143eaea13SPaulo Zanoni {
15243eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
15343eaea13SPaulo Zanoni }
15443eaea13SPaulo Zanoni 
155edbfdb45SPaulo Zanoni /**
156edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
157edbfdb45SPaulo Zanoni   * @dev_priv: driver private
158edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
159edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
160edbfdb45SPaulo Zanoni   */
161edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
162edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
163edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
164edbfdb45SPaulo Zanoni {
165605cd25bSPaulo Zanoni 	uint32_t new_val;
166edbfdb45SPaulo Zanoni 
167edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
168edbfdb45SPaulo Zanoni 
169c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
170c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
171c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gen6_pmimr &= ~interrupt_mask;
172c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gen6_pmimr |= (~enabled_irq_mask &
173c67a470bSPaulo Zanoni 						     interrupt_mask);
174c67a470bSPaulo Zanoni 		return;
175c67a470bSPaulo Zanoni 	}
176c67a470bSPaulo Zanoni 
177605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
178f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
179f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
180f52ecbcfSPaulo Zanoni 
181605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
182605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
183605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
184edbfdb45SPaulo Zanoni 		POSTING_READ(GEN6_PMIMR);
185edbfdb45SPaulo Zanoni 	}
186f52ecbcfSPaulo Zanoni }
187edbfdb45SPaulo Zanoni 
188edbfdb45SPaulo Zanoni void snb_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
189edbfdb45SPaulo Zanoni {
190edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
191edbfdb45SPaulo Zanoni }
192edbfdb45SPaulo Zanoni 
193edbfdb45SPaulo Zanoni void snb_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
194edbfdb45SPaulo Zanoni {
195edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, 0);
196edbfdb45SPaulo Zanoni }
197edbfdb45SPaulo Zanoni 
1988664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
1998664281bSPaulo Zanoni {
2008664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2018664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2028664281bSPaulo Zanoni 	enum pipe pipe;
2038664281bSPaulo Zanoni 
2044bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2054bc9d430SDaniel Vetter 
2068664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2078664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2088664281bSPaulo Zanoni 
2098664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
2108664281bSPaulo Zanoni 			return false;
2118664281bSPaulo Zanoni 	}
2128664281bSPaulo Zanoni 
2138664281bSPaulo Zanoni 	return true;
2148664281bSPaulo Zanoni }
2158664281bSPaulo Zanoni 
2168664281bSPaulo Zanoni static bool cpt_can_enable_serr_int(struct drm_device *dev)
2178664281bSPaulo Zanoni {
2188664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2198664281bSPaulo Zanoni 	enum pipe pipe;
2208664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2218664281bSPaulo Zanoni 
222fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
223fee884edSDaniel Vetter 
2248664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2258664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2268664281bSPaulo Zanoni 
2278664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
2288664281bSPaulo Zanoni 			return false;
2298664281bSPaulo Zanoni 	}
2308664281bSPaulo Zanoni 
2318664281bSPaulo Zanoni 	return true;
2328664281bSPaulo Zanoni }
2338664281bSPaulo Zanoni 
2348664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
2358664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
2368664281bSPaulo Zanoni {
2378664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2388664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
2398664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
2408664281bSPaulo Zanoni 
2418664281bSPaulo Zanoni 	if (enable)
2428664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
2438664281bSPaulo Zanoni 	else
2448664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
2458664281bSPaulo Zanoni }
2468664281bSPaulo Zanoni 
2478664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
2487336df65SDaniel Vetter 						  enum pipe pipe, bool enable)
2498664281bSPaulo Zanoni {
2508664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2518664281bSPaulo Zanoni 	if (enable) {
2527336df65SDaniel Vetter 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
2537336df65SDaniel Vetter 
2548664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
2558664281bSPaulo Zanoni 			return;
2568664281bSPaulo Zanoni 
2578664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
2588664281bSPaulo Zanoni 	} else {
2597336df65SDaniel Vetter 		bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
2607336df65SDaniel Vetter 
2617336df65SDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
2628664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
2637336df65SDaniel Vetter 
2647336df65SDaniel Vetter 		if (!was_enabled &&
2657336df65SDaniel Vetter 		    (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
2667336df65SDaniel Vetter 			DRM_DEBUG_KMS("uncleared fifo underrun on pipe %c\n",
2677336df65SDaniel Vetter 				      pipe_name(pipe));
2687336df65SDaniel Vetter 		}
2698664281bSPaulo Zanoni 	}
2708664281bSPaulo Zanoni }
2718664281bSPaulo Zanoni 
272fee884edSDaniel Vetter /**
273fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
274fee884edSDaniel Vetter  * @dev_priv: driver private
275fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
276fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
277fee884edSDaniel Vetter  */
278fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
279fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
280fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
281fee884edSDaniel Vetter {
282fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
283fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
284fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
285fee884edSDaniel Vetter 
286fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
287fee884edSDaniel Vetter 
288c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled &&
289c67a470bSPaulo Zanoni 	    (interrupt_mask & SDE_HOTPLUG_MASK_CPT)) {
290c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
291c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr &= ~interrupt_mask;
292c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr |= (~enabled_irq_mask &
293c67a470bSPaulo Zanoni 						 interrupt_mask);
294c67a470bSPaulo Zanoni 		return;
295c67a470bSPaulo Zanoni 	}
296c67a470bSPaulo Zanoni 
297fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
298fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
299fee884edSDaniel Vetter }
300fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
301fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
302fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
303fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
304fee884edSDaniel Vetter 
305de28075dSDaniel Vetter static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
306de28075dSDaniel Vetter 					    enum transcoder pch_transcoder,
3078664281bSPaulo Zanoni 					    bool enable)
3088664281bSPaulo Zanoni {
3098664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
310de28075dSDaniel Vetter 	uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
311de28075dSDaniel Vetter 		       SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
3128664281bSPaulo Zanoni 
3138664281bSPaulo Zanoni 	if (enable)
314fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
3158664281bSPaulo Zanoni 	else
316fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
3178664281bSPaulo Zanoni }
3188664281bSPaulo Zanoni 
3198664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
3208664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
3218664281bSPaulo Zanoni 					    bool enable)
3228664281bSPaulo Zanoni {
3238664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3248664281bSPaulo Zanoni 
3258664281bSPaulo Zanoni 	if (enable) {
3261dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
3271dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
3281dd246fbSDaniel Vetter 
3298664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
3308664281bSPaulo Zanoni 			return;
3318664281bSPaulo Zanoni 
332fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3338664281bSPaulo Zanoni 	} else {
3341dd246fbSDaniel Vetter 		uint32_t tmp = I915_READ(SERR_INT);
3351dd246fbSDaniel Vetter 		bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
3361dd246fbSDaniel Vetter 
3371dd246fbSDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
338fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3391dd246fbSDaniel Vetter 
3401dd246fbSDaniel Vetter 		if (!was_enabled &&
3411dd246fbSDaniel Vetter 		    (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
3421dd246fbSDaniel Vetter 			DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
3431dd246fbSDaniel Vetter 				      transcoder_name(pch_transcoder));
3441dd246fbSDaniel Vetter 		}
3458664281bSPaulo Zanoni 	}
3468664281bSPaulo Zanoni }
3478664281bSPaulo Zanoni 
3488664281bSPaulo Zanoni /**
3498664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
3508664281bSPaulo Zanoni  * @dev: drm device
3518664281bSPaulo Zanoni  * @pipe: pipe
3528664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
3538664281bSPaulo Zanoni  *
3548664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
3558664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
3568664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
3578664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
3588664281bSPaulo Zanoni  * bit for all the pipes.
3598664281bSPaulo Zanoni  *
3608664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
3618664281bSPaulo Zanoni  */
3628664281bSPaulo Zanoni bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
3638664281bSPaulo Zanoni 					   enum pipe pipe, bool enable)
3648664281bSPaulo Zanoni {
3658664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3668664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
3678664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3688664281bSPaulo Zanoni 	unsigned long flags;
3698664281bSPaulo Zanoni 	bool ret;
3708664281bSPaulo Zanoni 
3718664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
3728664281bSPaulo Zanoni 
3738664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
3748664281bSPaulo Zanoni 
3758664281bSPaulo Zanoni 	if (enable == ret)
3768664281bSPaulo Zanoni 		goto done;
3778664281bSPaulo Zanoni 
3788664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
3798664281bSPaulo Zanoni 
3808664281bSPaulo Zanoni 	if (IS_GEN5(dev) || IS_GEN6(dev))
3818664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
3828664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
3837336df65SDaniel Vetter 		ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
3848664281bSPaulo Zanoni 
3858664281bSPaulo Zanoni done:
3868664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
3878664281bSPaulo Zanoni 	return ret;
3888664281bSPaulo Zanoni }
3898664281bSPaulo Zanoni 
3908664281bSPaulo Zanoni /**
3918664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
3928664281bSPaulo Zanoni  * @dev: drm device
3938664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
3948664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
3958664281bSPaulo Zanoni  *
3968664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
3978664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
3988664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
3998664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
4008664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
4018664281bSPaulo Zanoni  *
4028664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
4038664281bSPaulo Zanoni  */
4048664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
4058664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
4068664281bSPaulo Zanoni 					   bool enable)
4078664281bSPaulo Zanoni {
4088664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
409de28075dSDaniel Vetter 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
410de28075dSDaniel Vetter 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4118664281bSPaulo Zanoni 	unsigned long flags;
4128664281bSPaulo Zanoni 	bool ret;
4138664281bSPaulo Zanoni 
414de28075dSDaniel Vetter 	/*
415de28075dSDaniel Vetter 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
416de28075dSDaniel Vetter 	 * has only one pch transcoder A that all pipes can use. To avoid racy
417de28075dSDaniel Vetter 	 * pch transcoder -> pipe lookups from interrupt code simply store the
418de28075dSDaniel Vetter 	 * underrun statistics in crtc A. Since we never expose this anywhere
419de28075dSDaniel Vetter 	 * nor use it outside of the fifo underrun code here using the "wrong"
420de28075dSDaniel Vetter 	 * crtc on LPT won't cause issues.
421de28075dSDaniel Vetter 	 */
4228664281bSPaulo Zanoni 
4238664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
4248664281bSPaulo Zanoni 
4258664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
4268664281bSPaulo Zanoni 
4278664281bSPaulo Zanoni 	if (enable == ret)
4288664281bSPaulo Zanoni 		goto done;
4298664281bSPaulo Zanoni 
4308664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
4318664281bSPaulo Zanoni 
4328664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
433de28075dSDaniel Vetter 		ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4348664281bSPaulo Zanoni 	else
4358664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4368664281bSPaulo Zanoni 
4378664281bSPaulo Zanoni done:
4388664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
4398664281bSPaulo Zanoni 	return ret;
4408664281bSPaulo Zanoni }
4418664281bSPaulo Zanoni 
4428664281bSPaulo Zanoni 
4437c463586SKeith Packard void
4447c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
4457c463586SKeith Packard {
4469db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
44746c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
4487c463586SKeith Packard 
449b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
450b79480baSDaniel Vetter 
45146c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
45246c06a30SVille Syrjälä 		return;
45346c06a30SVille Syrjälä 
4547c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
45546c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
45646c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4573143a2bfSChris Wilson 	POSTING_READ(reg);
4587c463586SKeith Packard }
4597c463586SKeith Packard 
4607c463586SKeith Packard void
4617c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
4627c463586SKeith Packard {
4639db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
46446c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
4657c463586SKeith Packard 
466b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
467b79480baSDaniel Vetter 
46846c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
46946c06a30SVille Syrjälä 		return;
47046c06a30SVille Syrjälä 
47146c06a30SVille Syrjälä 	pipestat &= ~mask;
47246c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4733143a2bfSChris Wilson 	POSTING_READ(reg);
4747c463586SKeith Packard }
4757c463586SKeith Packard 
476c0e09200SDave Airlie /**
477f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
47801c66889SZhao Yakui  */
479f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
48001c66889SZhao Yakui {
4811ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
4821ec14ad3SChris Wilson 	unsigned long irqflags;
4831ec14ad3SChris Wilson 
484f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
485f49e38ddSJani Nikula 		return;
486f49e38ddSJani Nikula 
4871ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
48801c66889SZhao Yakui 
489f898780bSJani Nikula 	i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
490a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
491f898780bSJani Nikula 		i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
4921ec14ad3SChris Wilson 
4931ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
49401c66889SZhao Yakui }
49501c66889SZhao Yakui 
49601c66889SZhao Yakui /**
4970a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
4980a3e67a4SJesse Barnes  * @dev: DRM device
4990a3e67a4SJesse Barnes  * @pipe: pipe to check
5000a3e67a4SJesse Barnes  *
5010a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
5020a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
5030a3e67a4SJesse Barnes  * before reading such registers if unsure.
5040a3e67a4SJesse Barnes  */
5050a3e67a4SJesse Barnes static int
5060a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
5070a3e67a4SJesse Barnes {
5080a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
509702e7a56SPaulo Zanoni 
510a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
511a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
512a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
513a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51471f8ba6bSPaulo Zanoni 
515a01025afSDaniel Vetter 		return intel_crtc->active;
516a01025afSDaniel Vetter 	} else {
517a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
518a01025afSDaniel Vetter 	}
5190a3e67a4SJesse Barnes }
5200a3e67a4SJesse Barnes 
52142f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
52242f52ef8SKeith Packard  * we use as a pipe index
52342f52ef8SKeith Packard  */
524f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5250a3e67a4SJesse Barnes {
5260a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5270a3e67a4SJesse Barnes 	unsigned long high_frame;
5280a3e67a4SJesse Barnes 	unsigned long low_frame;
5295eddb70bSChris Wilson 	u32 high1, high2, low;
5300a3e67a4SJesse Barnes 
5310a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
53244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5339db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
5340a3e67a4SJesse Barnes 		return 0;
5350a3e67a4SJesse Barnes 	}
5360a3e67a4SJesse Barnes 
5379db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5389db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5395eddb70bSChris Wilson 
5400a3e67a4SJesse Barnes 	/*
5410a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5420a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
5430a3e67a4SJesse Barnes 	 * register.
5440a3e67a4SJesse Barnes 	 */
5450a3e67a4SJesse Barnes 	do {
5465eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
5475eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
5485eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
5490a3e67a4SJesse Barnes 	} while (high1 != high2);
5500a3e67a4SJesse Barnes 
5515eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
5525eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
5535eddb70bSChris Wilson 	return (high1 << 8) | low;
5540a3e67a4SJesse Barnes }
5550a3e67a4SJesse Barnes 
556f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
5579880b7a5SJesse Barnes {
5589880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5599db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
5609880b7a5SJesse Barnes 
5619880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
56244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5639db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
5649880b7a5SJesse Barnes 		return 0;
5659880b7a5SJesse Barnes 	}
5669880b7a5SJesse Barnes 
5679880b7a5SJesse Barnes 	return I915_READ(reg);
5689880b7a5SJesse Barnes }
5699880b7a5SJesse Barnes 
570f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
5710af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
5720af7e4dfSMario Kleiner {
5730af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5740af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
5750af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
5760af7e4dfSMario Kleiner 	bool in_vbl = true;
5770af7e4dfSMario Kleiner 	int ret = 0;
578fe2b8f9dSPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
579fe2b8f9dSPaulo Zanoni 								      pipe);
5800af7e4dfSMario Kleiner 
5810af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
5820af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
5839db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
5840af7e4dfSMario Kleiner 		return 0;
5850af7e4dfSMario Kleiner 	}
5860af7e4dfSMario Kleiner 
5870af7e4dfSMario Kleiner 	/* Get vtotal. */
588fe2b8f9dSPaulo Zanoni 	vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
5890af7e4dfSMario Kleiner 
5900af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
5910af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
5920af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
5930af7e4dfSMario Kleiner 		 */
5940af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
5950af7e4dfSMario Kleiner 
5960af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
5970af7e4dfSMario Kleiner 		 * horizontal scanout position.
5980af7e4dfSMario Kleiner 		 */
5990af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
6000af7e4dfSMario Kleiner 		*hpos = 0;
6010af7e4dfSMario Kleiner 	} else {
6020af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
6030af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
6040af7e4dfSMario Kleiner 		 * scanout position.
6050af7e4dfSMario Kleiner 		 */
6060af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
6070af7e4dfSMario Kleiner 
608fe2b8f9dSPaulo Zanoni 		htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
6090af7e4dfSMario Kleiner 		*vpos = position / htotal;
6100af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
6110af7e4dfSMario Kleiner 	}
6120af7e4dfSMario Kleiner 
6130af7e4dfSMario Kleiner 	/* Query vblank area. */
614fe2b8f9dSPaulo Zanoni 	vbl = I915_READ(VBLANK(cpu_transcoder));
6150af7e4dfSMario Kleiner 
6160af7e4dfSMario Kleiner 	/* Test position against vblank region. */
6170af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
6180af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
6190af7e4dfSMario Kleiner 
6200af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
6210af7e4dfSMario Kleiner 		in_vbl = false;
6220af7e4dfSMario Kleiner 
6230af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
6240af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
6250af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
6260af7e4dfSMario Kleiner 
6270af7e4dfSMario Kleiner 	/* Readouts valid? */
6280af7e4dfSMario Kleiner 	if (vbl > 0)
6290af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
6300af7e4dfSMario Kleiner 
6310af7e4dfSMario Kleiner 	/* In vblank? */
6320af7e4dfSMario Kleiner 	if (in_vbl)
6330af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
6340af7e4dfSMario Kleiner 
6350af7e4dfSMario Kleiner 	return ret;
6360af7e4dfSMario Kleiner }
6370af7e4dfSMario Kleiner 
638f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
6390af7e4dfSMario Kleiner 			      int *max_error,
6400af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
6410af7e4dfSMario Kleiner 			      unsigned flags)
6420af7e4dfSMario Kleiner {
6434041b853SChris Wilson 	struct drm_crtc *crtc;
6440af7e4dfSMario Kleiner 
6457eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
6464041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
6470af7e4dfSMario Kleiner 		return -EINVAL;
6480af7e4dfSMario Kleiner 	}
6490af7e4dfSMario Kleiner 
6500af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
6514041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
6524041b853SChris Wilson 	if (crtc == NULL) {
6534041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
6544041b853SChris Wilson 		return -EINVAL;
6554041b853SChris Wilson 	}
6564041b853SChris Wilson 
6574041b853SChris Wilson 	if (!crtc->enabled) {
6584041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
6594041b853SChris Wilson 		return -EBUSY;
6604041b853SChris Wilson 	}
6610af7e4dfSMario Kleiner 
6620af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
6634041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
6644041b853SChris Wilson 						     vblank_time, flags,
6654041b853SChris Wilson 						     crtc);
6660af7e4dfSMario Kleiner }
6670af7e4dfSMario Kleiner 
668321a1b30SEgbert Eich static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
669321a1b30SEgbert Eich {
670321a1b30SEgbert Eich 	enum drm_connector_status old_status;
671321a1b30SEgbert Eich 
672321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
673321a1b30SEgbert Eich 	old_status = connector->status;
674321a1b30SEgbert Eich 
675321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
676321a1b30SEgbert Eich 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
677321a1b30SEgbert Eich 		      connector->base.id,
678321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
679321a1b30SEgbert Eich 		      old_status, connector->status);
680321a1b30SEgbert Eich 	return (old_status != connector->status);
681321a1b30SEgbert Eich }
682321a1b30SEgbert Eich 
6835ca58282SJesse Barnes /*
6845ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
6855ca58282SJesse Barnes  */
686ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
687ac4c16c5SEgbert Eich 
6885ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
6895ca58282SJesse Barnes {
6905ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
6915ca58282SJesse Barnes 						    hotplug_work);
6925ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
693c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
694cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
695cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
696cd569aedSEgbert Eich 	struct drm_connector *connector;
697cd569aedSEgbert Eich 	unsigned long irqflags;
698cd569aedSEgbert Eich 	bool hpd_disabled = false;
699321a1b30SEgbert Eich 	bool changed = false;
700142e2398SEgbert Eich 	u32 hpd_event_bits;
7015ca58282SJesse Barnes 
70252d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
70352d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
70452d7ecedSDaniel Vetter 		return;
70552d7ecedSDaniel Vetter 
706a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
707e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
708e67189abSJesse Barnes 
709cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
710142e2398SEgbert Eich 
711142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
712142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
713cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
714cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
715cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
716cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
717cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
718cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
719cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
720cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
721cd569aedSEgbert Eich 				drm_get_connector_name(connector));
722cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
723cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
724cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
725cd569aedSEgbert Eich 			hpd_disabled = true;
726cd569aedSEgbert Eich 		}
727142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
728142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
729142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
730142e2398SEgbert Eich 		}
731cd569aedSEgbert Eich 	}
732cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
733cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
734cd569aedSEgbert Eich 	  * some connectors */
735ac4c16c5SEgbert Eich 	if (hpd_disabled) {
736cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
737ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
738ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
739ac4c16c5SEgbert Eich 	}
740cd569aedSEgbert Eich 
741cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
742cd569aedSEgbert Eich 
743321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
744321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
745321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
746321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
747cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
748cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
749321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
750321a1b30SEgbert Eich 				changed = true;
751321a1b30SEgbert Eich 		}
752321a1b30SEgbert Eich 	}
75340ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
75440ee3381SKeith Packard 
755321a1b30SEgbert Eich 	if (changed)
756321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
7575ca58282SJesse Barnes }
7585ca58282SJesse Barnes 
759d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
760f97108d1SJesse Barnes {
761f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
762b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
7639270388eSDaniel Vetter 	u8 new_delay;
7649270388eSDaniel Vetter 
765d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
766f97108d1SJesse Barnes 
76773edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
76873edd18fSDaniel Vetter 
76920e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
7709270388eSDaniel Vetter 
7717648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
772b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
773b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
774f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
775f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
776f97108d1SJesse Barnes 
777f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
778b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
77920e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
78020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
78120e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
78220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
783b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
78420e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
78520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
78620e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
78720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
788f97108d1SJesse Barnes 	}
789f97108d1SJesse Barnes 
7907648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
79120e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
792f97108d1SJesse Barnes 
793d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
7949270388eSDaniel Vetter 
795f97108d1SJesse Barnes 	return;
796f97108d1SJesse Barnes }
797f97108d1SJesse Barnes 
798549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
799549f7365SChris Wilson 			struct intel_ring_buffer *ring)
800549f7365SChris Wilson {
801475553deSChris Wilson 	if (ring->obj == NULL)
802475553deSChris Wilson 		return;
803475553deSChris Wilson 
804b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
8059862e600SChris Wilson 
806549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
80710cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
808549f7365SChris Wilson }
809549f7365SChris Wilson 
8104912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
8113b8d8d91SJesse Barnes {
8124912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
813c6a828d3SDaniel Vetter 						    rps.work);
814edbfdb45SPaulo Zanoni 	u32 pm_iir;
8157b9e0ae6SChris Wilson 	u8 new_delay;
8163b8d8d91SJesse Barnes 
81759cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
818c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
819c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
8204848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
821edbfdb45SPaulo Zanoni 	snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
82259cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
8234912d041SBen Widawsky 
82460611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
82560611c13SPaulo Zanoni 	WARN_ON(pm_iir & ~GEN6_PM_RPS_EVENTS);
82660611c13SPaulo Zanoni 
8274848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
8283b8d8d91SJesse Barnes 		return;
8293b8d8d91SJesse Barnes 
8304fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
8317b9e0ae6SChris Wilson 
8327425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
833c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
8347425034aSVille Syrjälä 
8357425034aSVille Syrjälä 		/*
8367425034aSVille Syrjälä 		 * For better performance, jump directly
8377425034aSVille Syrjälä 		 * to RPe if we're below it.
8387425034aSVille Syrjälä 		 */
8397425034aSVille Syrjälä 		if (IS_VALLEYVIEW(dev_priv->dev) &&
8407425034aSVille Syrjälä 		    dev_priv->rps.cur_delay < dev_priv->rps.rpe_delay)
8417425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
8427425034aSVille Syrjälä 	} else
843c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
8443b8d8d91SJesse Barnes 
84579249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
84679249636SBen Widawsky 	 * interrupt
84779249636SBen Widawsky 	 */
848d8289c9eSVille Syrjälä 	if (new_delay >= dev_priv->rps.min_delay &&
849d8289c9eSVille Syrjälä 	    new_delay <= dev_priv->rps.max_delay) {
8500a073b84SJesse Barnes 		if (IS_VALLEYVIEW(dev_priv->dev))
8510a073b84SJesse Barnes 			valleyview_set_rps(dev_priv->dev, new_delay);
8520a073b84SJesse Barnes 		else
8534912d041SBen Widawsky 			gen6_set_rps(dev_priv->dev, new_delay);
85479249636SBen Widawsky 	}
8553b8d8d91SJesse Barnes 
85652ceb908SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev)) {
85752ceb908SJesse Barnes 		/*
85852ceb908SJesse Barnes 		 * On VLV, when we enter RC6 we may not be at the minimum
85952ceb908SJesse Barnes 		 * voltage level, so arm a timer to check.  It should only
86052ceb908SJesse Barnes 		 * fire when there's activity or once after we've entered
86152ceb908SJesse Barnes 		 * RC6, and then won't be re-armed until the next RPS interrupt.
86252ceb908SJesse Barnes 		 */
86352ceb908SJesse Barnes 		mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
86452ceb908SJesse Barnes 				 msecs_to_jiffies(100));
86552ceb908SJesse Barnes 	}
86652ceb908SJesse Barnes 
8674fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
8683b8d8d91SJesse Barnes }
8693b8d8d91SJesse Barnes 
870e3689190SBen Widawsky 
871e3689190SBen Widawsky /**
872e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
873e3689190SBen Widawsky  * occurred.
874e3689190SBen Widawsky  * @work: workqueue struct
875e3689190SBen Widawsky  *
876e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
877e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
878e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
879e3689190SBen Widawsky  */
880e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
881e3689190SBen Widawsky {
882e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
883a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
884e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
885e3689190SBen Widawsky 	char *parity_event[5];
886e3689190SBen Widawsky 	uint32_t misccpctl;
887e3689190SBen Widawsky 	unsigned long flags;
888e3689190SBen Widawsky 
889e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
890e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
891e3689190SBen Widawsky 	 * any time we access those registers.
892e3689190SBen Widawsky 	 */
893e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
894e3689190SBen Widawsky 
895e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
896e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
897e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
898e3689190SBen Widawsky 
899e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
900e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
901e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
902e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
903e3689190SBen Widawsky 
904e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
905e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
906e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
907e3689190SBen Widawsky 
908e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
909e3689190SBen Widawsky 
910e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
91143eaea13SPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
912e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
913e3689190SBen Widawsky 
914e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
915e3689190SBen Widawsky 
916cce723edSBen Widawsky 	parity_event[0] = I915_L3_PARITY_UEVENT "=1";
917e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
918e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
919e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
920e3689190SBen Widawsky 	parity_event[4] = NULL;
921e3689190SBen Widawsky 
922e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
923e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
924e3689190SBen Widawsky 
925e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
926e3689190SBen Widawsky 		  row, bank, subbank);
927e3689190SBen Widawsky 
928e3689190SBen Widawsky 	kfree(parity_event[3]);
929e3689190SBen Widawsky 	kfree(parity_event[2]);
930e3689190SBen Widawsky 	kfree(parity_event[1]);
931e3689190SBen Widawsky }
932e3689190SBen Widawsky 
933d0ecd7e2SDaniel Vetter static void ivybridge_parity_error_irq_handler(struct drm_device *dev)
934e3689190SBen Widawsky {
935e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
936e3689190SBen Widawsky 
937e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
938e3689190SBen Widawsky 		return;
939e3689190SBen Widawsky 
940d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
94143eaea13SPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
942d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
943e3689190SBen Widawsky 
944a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
945e3689190SBen Widawsky }
946e3689190SBen Widawsky 
947f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
948f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
949f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
950f1af8fc1SPaulo Zanoni {
951f1af8fc1SPaulo Zanoni 	if (gt_iir &
952f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
953f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
954f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
955f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
956f1af8fc1SPaulo Zanoni }
957f1af8fc1SPaulo Zanoni 
958e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
959e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
960e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
961e7b4c6b1SDaniel Vetter {
962e7b4c6b1SDaniel Vetter 
963cc609d5dSBen Widawsky 	if (gt_iir &
964cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
965e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
966cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
967e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
968cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
969e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
970e7b4c6b1SDaniel Vetter 
971cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
972cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
973cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
974e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
975e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
976e7b4c6b1SDaniel Vetter 	}
977e3689190SBen Widawsky 
978cc609d5dSBen Widawsky 	if (gt_iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
979d0ecd7e2SDaniel Vetter 		ivybridge_parity_error_irq_handler(dev);
980e7b4c6b1SDaniel Vetter }
981e7b4c6b1SDaniel Vetter 
982b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
983b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
984b543fb04SEgbert Eich 
98510a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
986b543fb04SEgbert Eich 					 u32 hotplug_trigger,
987b543fb04SEgbert Eich 					 const u32 *hpd)
988b543fb04SEgbert Eich {
989b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
990b543fb04SEgbert Eich 	int i;
99110a504deSDaniel Vetter 	bool storm_detected = false;
992b543fb04SEgbert Eich 
99391d131d2SDaniel Vetter 	if (!hotplug_trigger)
99491d131d2SDaniel Vetter 		return;
99591d131d2SDaniel Vetter 
996b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
997b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
998821450c6SEgbert Eich 
999b8f102e8SEgbert Eich 		WARN(((hpd[i] & hotplug_trigger) &&
1000b8f102e8SEgbert Eich 		      dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED),
1001b8f102e8SEgbert Eich 		     "Received HPD interrupt although disabled\n");
1002b8f102e8SEgbert Eich 
1003b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1004b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1005b543fb04SEgbert Eich 			continue;
1006b543fb04SEgbert Eich 
1007bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1008b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1009b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1010b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1011b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1012b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1013b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1014b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1015b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1016142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1017b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
101810a504deSDaniel Vetter 			storm_detected = true;
1019b543fb04SEgbert Eich 		} else {
1020b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1021b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1022b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1023b543fb04SEgbert Eich 		}
1024b543fb04SEgbert Eich 	}
1025b543fb04SEgbert Eich 
102610a504deSDaniel Vetter 	if (storm_detected)
102710a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1028b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
10295876fa0dSDaniel Vetter 
1030645416f5SDaniel Vetter 	/*
1031645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1032645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1033645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1034645416f5SDaniel Vetter 	 * deadlock.
1035645416f5SDaniel Vetter 	 */
1036645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1037b543fb04SEgbert Eich }
1038b543fb04SEgbert Eich 
1039515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1040515ac2bbSDaniel Vetter {
104128c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
104228c70f16SDaniel Vetter 
104328c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1044515ac2bbSDaniel Vetter }
1045515ac2bbSDaniel Vetter 
1046ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1047ce99c256SDaniel Vetter {
10489ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
10499ee32feaSDaniel Vetter 
10509ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1051ce99c256SDaniel Vetter }
1052ce99c256SDaniel Vetter 
10531403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
10541403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
10551403c0d4SPaulo Zanoni  * the work queue. */
10561403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1057baf02a1fSBen Widawsky {
105841a05a3aSDaniel Vetter 	if (pm_iir & GEN6_PM_RPS_EVENTS) {
105959cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
10604848405cSBen Widawsky 		dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
10614d3b3d5fSPaulo Zanoni 		snb_disable_pm_irq(dev_priv, pm_iir & GEN6_PM_RPS_EVENTS);
106259cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
10632adbee62SDaniel Vetter 
10642adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
106541a05a3aSDaniel Vetter 	}
1066baf02a1fSBen Widawsky 
10671403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
106812638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
106912638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
107012638c57SBen Widawsky 
107112638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
107212638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
107312638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
107412638c57SBen Widawsky 		}
107512638c57SBen Widawsky 	}
10761403c0d4SPaulo Zanoni }
1077baf02a1fSBen Widawsky 
1078ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
10797e231dbeSJesse Barnes {
10807e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
10817e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10827e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
10837e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
10847e231dbeSJesse Barnes 	unsigned long irqflags;
10857e231dbeSJesse Barnes 	int pipe;
10867e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
10877e231dbeSJesse Barnes 
10887e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
10897e231dbeSJesse Barnes 
10907e231dbeSJesse Barnes 	while (true) {
10917e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
10927e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
10937e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
10947e231dbeSJesse Barnes 
10957e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
10967e231dbeSJesse Barnes 			goto out;
10977e231dbeSJesse Barnes 
10987e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
10997e231dbeSJesse Barnes 
1100e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
11017e231dbeSJesse Barnes 
11027e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
11037e231dbeSJesse Barnes 		for_each_pipe(pipe) {
11047e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
11057e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
11067e231dbeSJesse Barnes 
11077e231dbeSJesse Barnes 			/*
11087e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
11097e231dbeSJesse Barnes 			 */
11107e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
11117e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
11127e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
11137e231dbeSJesse Barnes 							 pipe_name(pipe));
11147e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
11157e231dbeSJesse Barnes 			}
11167e231dbeSJesse Barnes 		}
11177e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
11187e231dbeSJesse Barnes 
111931acc7f5SJesse Barnes 		for_each_pipe(pipe) {
112031acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
112131acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
112231acc7f5SJesse Barnes 
112331acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
112431acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
112531acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
112631acc7f5SJesse Barnes 			}
112731acc7f5SJesse Barnes 		}
112831acc7f5SJesse Barnes 
11297e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
11307e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
11317e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1132b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
11337e231dbeSJesse Barnes 
11347e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
11357e231dbeSJesse Barnes 					 hotplug_status);
113691d131d2SDaniel Vetter 
113710a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
113891d131d2SDaniel Vetter 
11397e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
11407e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
11417e231dbeSJesse Barnes 		}
11427e231dbeSJesse Barnes 
1143515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1144515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
11457e231dbeSJesse Barnes 
114660611c13SPaulo Zanoni 		if (pm_iir)
1147d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
11487e231dbeSJesse Barnes 
11497e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
11507e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
11517e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
11527e231dbeSJesse Barnes 	}
11537e231dbeSJesse Barnes 
11547e231dbeSJesse Barnes out:
11557e231dbeSJesse Barnes 	return ret;
11567e231dbeSJesse Barnes }
11577e231dbeSJesse Barnes 
115823e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1159776ad806SJesse Barnes {
1160776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11619db4a9c7SJesse Barnes 	int pipe;
1162b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1163776ad806SJesse Barnes 
116410a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
116591d131d2SDaniel Vetter 
1166cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1167cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1168776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1169cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1170cfc33bf7SVille Syrjälä 				 port_name(port));
1171cfc33bf7SVille Syrjälä 	}
1172776ad806SJesse Barnes 
1173ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1174ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1175ce99c256SDaniel Vetter 
1176776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1177515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1178776ad806SJesse Barnes 
1179776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1180776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1181776ad806SJesse Barnes 
1182776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1183776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1184776ad806SJesse Barnes 
1185776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1186776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1187776ad806SJesse Barnes 
11889db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
11899db4a9c7SJesse Barnes 		for_each_pipe(pipe)
11909db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
11919db4a9c7SJesse Barnes 					 pipe_name(pipe),
11929db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1193776ad806SJesse Barnes 
1194776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1195776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1196776ad806SJesse Barnes 
1197776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1198776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1199776ad806SJesse Barnes 
1200776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
12018664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
12028664281bSPaulo Zanoni 							  false))
12038664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
12048664281bSPaulo Zanoni 
12058664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
12068664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
12078664281bSPaulo Zanoni 							  false))
12088664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
12098664281bSPaulo Zanoni }
12108664281bSPaulo Zanoni 
12118664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
12128664281bSPaulo Zanoni {
12138664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
12148664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
12158664281bSPaulo Zanoni 
1216de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1217de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1218de032bf4SPaulo Zanoni 
12198664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_A)
12208664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
12218664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
12228664281bSPaulo Zanoni 
12238664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_B)
12248664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
12258664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
12268664281bSPaulo Zanoni 
12278664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_C)
12288664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
12298664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
12308664281bSPaulo Zanoni 
12318664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
12328664281bSPaulo Zanoni }
12338664281bSPaulo Zanoni 
12348664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
12358664281bSPaulo Zanoni {
12368664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
12378664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
12388664281bSPaulo Zanoni 
1239de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1240de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1241de032bf4SPaulo Zanoni 
12428664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
12438664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
12448664281bSPaulo Zanoni 							  false))
12458664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
12468664281bSPaulo Zanoni 
12478664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
12488664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
12498664281bSPaulo Zanoni 							  false))
12508664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
12518664281bSPaulo Zanoni 
12528664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
12538664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
12548664281bSPaulo Zanoni 							  false))
12558664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
12568664281bSPaulo Zanoni 
12578664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1258776ad806SJesse Barnes }
1259776ad806SJesse Barnes 
126023e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
126123e81d69SAdam Jackson {
126223e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
126323e81d69SAdam Jackson 	int pipe;
1264b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
126523e81d69SAdam Jackson 
126610a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
126791d131d2SDaniel Vetter 
1268cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1269cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
127023e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1271cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1272cfc33bf7SVille Syrjälä 				 port_name(port));
1273cfc33bf7SVille Syrjälä 	}
127423e81d69SAdam Jackson 
127523e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1276ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
127723e81d69SAdam Jackson 
127823e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1279515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
128023e81d69SAdam Jackson 
128123e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
128223e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
128323e81d69SAdam Jackson 
128423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
128523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
128623e81d69SAdam Jackson 
128723e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
128823e81d69SAdam Jackson 		for_each_pipe(pipe)
128923e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
129023e81d69SAdam Jackson 					 pipe_name(pipe),
129123e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
12928664281bSPaulo Zanoni 
12938664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
12948664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
129523e81d69SAdam Jackson }
129623e81d69SAdam Jackson 
1297c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1298c008bc6eSPaulo Zanoni {
1299c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1300c008bc6eSPaulo Zanoni 
1301c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1302c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1303c008bc6eSPaulo Zanoni 
1304c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1305c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1306c008bc6eSPaulo Zanoni 
1307c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEA_VBLANK)
1308c008bc6eSPaulo Zanoni 		drm_handle_vblank(dev, 0);
1309c008bc6eSPaulo Zanoni 
1310c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEB_VBLANK)
1311c008bc6eSPaulo Zanoni 		drm_handle_vblank(dev, 1);
1312c008bc6eSPaulo Zanoni 
1313c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1314c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1315c008bc6eSPaulo Zanoni 
1316c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
1317c008bc6eSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1318c008bc6eSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1319c008bc6eSPaulo Zanoni 
1320c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
1321c008bc6eSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1322c008bc6eSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1323c008bc6eSPaulo Zanoni 
1324c008bc6eSPaulo Zanoni 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1325c008bc6eSPaulo Zanoni 		intel_prepare_page_flip(dev, 0);
1326c008bc6eSPaulo Zanoni 		intel_finish_page_flip_plane(dev, 0);
1327c008bc6eSPaulo Zanoni 	}
1328c008bc6eSPaulo Zanoni 
1329c008bc6eSPaulo Zanoni 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1330c008bc6eSPaulo Zanoni 		intel_prepare_page_flip(dev, 1);
1331c008bc6eSPaulo Zanoni 		intel_finish_page_flip_plane(dev, 1);
1332c008bc6eSPaulo Zanoni 	}
1333c008bc6eSPaulo Zanoni 
1334c008bc6eSPaulo Zanoni 	/* check event from PCH */
1335c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1336c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1337c008bc6eSPaulo Zanoni 
1338c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1339c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1340c008bc6eSPaulo Zanoni 		else
1341c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1342c008bc6eSPaulo Zanoni 
1343c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1344c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1345c008bc6eSPaulo Zanoni 	}
1346c008bc6eSPaulo Zanoni 
1347c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1348c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1349c008bc6eSPaulo Zanoni }
1350c008bc6eSPaulo Zanoni 
13519719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
13529719fb98SPaulo Zanoni {
13539719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
13549719fb98SPaulo Zanoni 	int i;
13559719fb98SPaulo Zanoni 
13569719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
13579719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
13589719fb98SPaulo Zanoni 
13599719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
13609719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
13619719fb98SPaulo Zanoni 
13629719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
13639719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
13649719fb98SPaulo Zanoni 
13659719fb98SPaulo Zanoni 	for (i = 0; i < 3; i++) {
13669719fb98SPaulo Zanoni 		if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
13679719fb98SPaulo Zanoni 			drm_handle_vblank(dev, i);
13689719fb98SPaulo Zanoni 		if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
13699719fb98SPaulo Zanoni 			intel_prepare_page_flip(dev, i);
13709719fb98SPaulo Zanoni 			intel_finish_page_flip_plane(dev, i);
13719719fb98SPaulo Zanoni 		}
13729719fb98SPaulo Zanoni 	}
13739719fb98SPaulo Zanoni 
13749719fb98SPaulo Zanoni 	/* check event from PCH */
13759719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
13769719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
13779719fb98SPaulo Zanoni 
13789719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
13799719fb98SPaulo Zanoni 
13809719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
13819719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
13829719fb98SPaulo Zanoni 	}
13839719fb98SPaulo Zanoni }
13849719fb98SPaulo Zanoni 
1385f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1386b1f14ad0SJesse Barnes {
1387b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1388b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1389f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
13900e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1391333a8204SPaulo Zanoni 	bool err_int_reenable = false;
1392b1f14ad0SJesse Barnes 
1393b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1394b1f14ad0SJesse Barnes 
13958664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
13968664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1397907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
13988664281bSPaulo Zanoni 
1399b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1400b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1401b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
140223a78516SPaulo Zanoni 	POSTING_READ(DEIER);
14030e43406bSChris Wilson 
140444498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
140544498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
140644498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
140744498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
140844498aeaSPaulo Zanoni 	 * due to its back queue). */
1409ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
141044498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
141144498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
141244498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1413ab5c608bSBen Widawsky 	}
141444498aeaSPaulo Zanoni 
14158664281bSPaulo Zanoni 	/* On Haswell, also mask ERR_INT because we don't want to risk
14168664281bSPaulo Zanoni 	 * generating "unclaimed register" interrupts from inside the interrupt
14178664281bSPaulo Zanoni 	 * handler. */
14184bc9d430SDaniel Vetter 	if (IS_HASWELL(dev)) {
14194bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1420333a8204SPaulo Zanoni 		err_int_reenable = ~dev_priv->irq_mask & DE_ERR_INT_IVB;
1421333a8204SPaulo Zanoni 		if (err_int_reenable)
14228664281bSPaulo Zanoni 			ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
14234bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
14244bc9d430SDaniel Vetter 	}
14258664281bSPaulo Zanoni 
14260e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
14270e43406bSChris Wilson 	if (gt_iir) {
1428d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
14290e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1430d8fc8a47SPaulo Zanoni 		else
1431d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
14320e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
14330e43406bSChris Wilson 		ret = IRQ_HANDLED;
14340e43406bSChris Wilson 	}
1435b1f14ad0SJesse Barnes 
1436b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
14370e43406bSChris Wilson 	if (de_iir) {
1438f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
14399719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1440f1af8fc1SPaulo Zanoni 		else
1441f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
14420e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
14430e43406bSChris Wilson 		ret = IRQ_HANDLED;
14440e43406bSChris Wilson 	}
14450e43406bSChris Wilson 
1446f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
1447f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
14480e43406bSChris Wilson 		if (pm_iir) {
1449d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
1450b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
14510e43406bSChris Wilson 			ret = IRQ_HANDLED;
14520e43406bSChris Wilson 		}
1453f1af8fc1SPaulo Zanoni 	}
1454b1f14ad0SJesse Barnes 
1455333a8204SPaulo Zanoni 	if (err_int_reenable) {
14564bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
14574bc9d430SDaniel Vetter 		if (ivb_can_enable_err_int(dev))
14588664281bSPaulo Zanoni 			ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
14594bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
14604bc9d430SDaniel Vetter 	}
14618664281bSPaulo Zanoni 
1462b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1463b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1464ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
146544498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
146644498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1467ab5c608bSBen Widawsky 	}
1468b1f14ad0SJesse Barnes 
1469b1f14ad0SJesse Barnes 	return ret;
1470b1f14ad0SJesse Barnes }
1471b1f14ad0SJesse Barnes 
14728a905236SJesse Barnes /**
14738a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
14748a905236SJesse Barnes  * @work: work struct
14758a905236SJesse Barnes  *
14768a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
14778a905236SJesse Barnes  * was detected.
14788a905236SJesse Barnes  */
14798a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
14808a905236SJesse Barnes {
14811f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
14821f83fee0SDaniel Vetter 						    work);
14831f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
14841f83fee0SDaniel Vetter 						    gpu_error);
14858a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1486f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
1487cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1488cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1489cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
1490f69061beSDaniel Vetter 	int i, ret;
14918a905236SJesse Barnes 
1492f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
14938a905236SJesse Barnes 
14947db0ba24SDaniel Vetter 	/*
14957db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
14967db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
14977db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
14987db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
14997db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
15007db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
15017db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
15027db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
15037db0ba24SDaniel Vetter 	 */
15047db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
150544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
15067db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
15077db0ba24SDaniel Vetter 				   reset_event);
15081f83fee0SDaniel Vetter 
1509f69061beSDaniel Vetter 		ret = i915_reset(dev);
1510f69061beSDaniel Vetter 
1511f69061beSDaniel Vetter 		if (ret == 0) {
1512f69061beSDaniel Vetter 			/*
1513f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1514f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1515f69061beSDaniel Vetter 			 * complete.
1516f69061beSDaniel Vetter 			 *
1517f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1518f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1519f69061beSDaniel Vetter 			 * updates before
1520f69061beSDaniel Vetter 			 * the counter increment.
1521f69061beSDaniel Vetter 			 */
1522f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1523f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1524f69061beSDaniel Vetter 
1525f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1526f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
15271f83fee0SDaniel Vetter 		} else {
15281f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1529f316a42cSBen Gamari 		}
15301f83fee0SDaniel Vetter 
1531f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
1532f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
1533f69061beSDaniel Vetter 
153496a02917SVille Syrjälä 		intel_display_handle_reset(dev);
153596a02917SVille Syrjälä 
15361f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
1537f316a42cSBen Gamari 	}
15388a905236SJesse Barnes }
15398a905236SJesse Barnes 
154035aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1541c0e09200SDave Airlie {
15428a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1543bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
154463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1545050ee91fSBen Widawsky 	int pipe, i;
154663eeaf38SJesse Barnes 
154735aed2e6SChris Wilson 	if (!eir)
154835aed2e6SChris Wilson 		return;
154963eeaf38SJesse Barnes 
1550a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
15518a905236SJesse Barnes 
1552bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1553bd9854f9SBen Widawsky 
15548a905236SJesse Barnes 	if (IS_G4X(dev)) {
15558a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
15568a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
15578a905236SJesse Barnes 
1558a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1559a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1560050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1561050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1562a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1563a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
15648a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
15653143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
15668a905236SJesse Barnes 		}
15678a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
15688a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1569a70491ccSJoe Perches 			pr_err("page table error\n");
1570a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
15718a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
15723143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
15738a905236SJesse Barnes 		}
15748a905236SJesse Barnes 	}
15758a905236SJesse Barnes 
1576a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
157763eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
157863eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1579a70491ccSJoe Perches 			pr_err("page table error\n");
1580a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
158163eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
15823143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
158363eeaf38SJesse Barnes 		}
15848a905236SJesse Barnes 	}
15858a905236SJesse Barnes 
158663eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1587a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
15889db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1589a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
15909db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
159163eeaf38SJesse Barnes 		/* pipestat has already been acked */
159263eeaf38SJesse Barnes 	}
159363eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1594a70491ccSJoe Perches 		pr_err("instruction error\n");
1595a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1596050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1597050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1598a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
159963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
160063eeaf38SJesse Barnes 
1601a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1602a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1603a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
160463eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
16053143a2bfSChris Wilson 			POSTING_READ(IPEIR);
160663eeaf38SJesse Barnes 		} else {
160763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
160863eeaf38SJesse Barnes 
1609a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1610a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1611a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1612a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
161363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
16143143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
161563eeaf38SJesse Barnes 		}
161663eeaf38SJesse Barnes 	}
161763eeaf38SJesse Barnes 
161863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
16193143a2bfSChris Wilson 	POSTING_READ(EIR);
162063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
162163eeaf38SJesse Barnes 	if (eir) {
162263eeaf38SJesse Barnes 		/*
162363eeaf38SJesse Barnes 		 * some errors might have become stuck,
162463eeaf38SJesse Barnes 		 * mask them.
162563eeaf38SJesse Barnes 		 */
162663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
162763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
162863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
162963eeaf38SJesse Barnes 	}
163035aed2e6SChris Wilson }
163135aed2e6SChris Wilson 
163235aed2e6SChris Wilson /**
163335aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
163435aed2e6SChris Wilson  * @dev: drm device
163535aed2e6SChris Wilson  *
163635aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
163735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
163835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
163935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
164035aed2e6SChris Wilson  * of a ring dump etc.).
164135aed2e6SChris Wilson  */
1642527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
164335aed2e6SChris Wilson {
164435aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1645b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1646b4519513SChris Wilson 	int i;
164735aed2e6SChris Wilson 
164835aed2e6SChris Wilson 	i915_capture_error_state(dev);
164935aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
16508a905236SJesse Barnes 
1651ba1234d1SBen Gamari 	if (wedged) {
1652f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1653f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
1654ba1234d1SBen Gamari 
165511ed50ecSBen Gamari 		/*
16561f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
16571f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
165811ed50ecSBen Gamari 		 */
1659b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1660b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
166111ed50ecSBen Gamari 	}
166211ed50ecSBen Gamari 
166399584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
16648a905236SJesse Barnes }
16658a905236SJesse Barnes 
166621ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
16674e5359cdSSimon Farnsworth {
16684e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
16694e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
16704e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
167105394f39SChris Wilson 	struct drm_i915_gem_object *obj;
16724e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
16734e5359cdSSimon Farnsworth 	unsigned long flags;
16744e5359cdSSimon Farnsworth 	bool stall_detected;
16754e5359cdSSimon Farnsworth 
16764e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
16774e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
16784e5359cdSSimon Farnsworth 		return;
16794e5359cdSSimon Farnsworth 
16804e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
16814e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
16824e5359cdSSimon Farnsworth 
1683e7d841caSChris Wilson 	if (work == NULL ||
1684e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1685e7d841caSChris Wilson 	    !work->enable_stall_check) {
16864e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
16874e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
16884e5359cdSSimon Farnsworth 		return;
16894e5359cdSSimon Farnsworth 	}
16904e5359cdSSimon Farnsworth 
16914e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
169205394f39SChris Wilson 	obj = work->pending_flip_obj;
1693a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
16949db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1695446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1696f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
16974e5359cdSSimon Farnsworth 	} else {
16989db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
1699f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
170001f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
17014e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
17024e5359cdSSimon Farnsworth 	}
17034e5359cdSSimon Farnsworth 
17044e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
17054e5359cdSSimon Farnsworth 
17064e5359cdSSimon Farnsworth 	if (stall_detected) {
17074e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
17084e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
17094e5359cdSSimon Farnsworth 	}
17104e5359cdSSimon Farnsworth }
17114e5359cdSSimon Farnsworth 
171242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
171342f52ef8SKeith Packard  * we use as a pipe index
171442f52ef8SKeith Packard  */
1715f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
17160a3e67a4SJesse Barnes {
17170a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1718e9d21d7fSKeith Packard 	unsigned long irqflags;
171971e0ffa5SJesse Barnes 
17205eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
172171e0ffa5SJesse Barnes 		return -EINVAL;
17220a3e67a4SJesse Barnes 
17231ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1724f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
17257c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
17267c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
17270a3e67a4SJesse Barnes 	else
17287c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
17297c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
17308692d00eSChris Wilson 
17318692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
17328692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
17336b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
17341ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17358692d00eSChris Wilson 
17360a3e67a4SJesse Barnes 	return 0;
17370a3e67a4SJesse Barnes }
17380a3e67a4SJesse Barnes 
1739f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1740f796cf8fSJesse Barnes {
1741f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1742f796cf8fSJesse Barnes 	unsigned long irqflags;
1743b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
1744b518421fSPaulo Zanoni 						     DE_PIPE_VBLANK_ILK(pipe);
1745f796cf8fSJesse Barnes 
1746f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1747f796cf8fSJesse Barnes 		return -EINVAL;
1748f796cf8fSJesse Barnes 
1749f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1750b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
1751b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1752b1f14ad0SJesse Barnes 
1753b1f14ad0SJesse Barnes 	return 0;
1754b1f14ad0SJesse Barnes }
1755b1f14ad0SJesse Barnes 
17567e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
17577e231dbeSJesse Barnes {
17587e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17597e231dbeSJesse Barnes 	unsigned long irqflags;
176031acc7f5SJesse Barnes 	u32 imr;
17617e231dbeSJesse Barnes 
17627e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
17637e231dbeSJesse Barnes 		return -EINVAL;
17647e231dbeSJesse Barnes 
17657e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17667e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
176731acc7f5SJesse Barnes 	if (pipe == 0)
17687e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
176931acc7f5SJesse Barnes 	else
17707e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
17717e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
177231acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
177331acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
17747e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17757e231dbeSJesse Barnes 
17767e231dbeSJesse Barnes 	return 0;
17777e231dbeSJesse Barnes }
17787e231dbeSJesse Barnes 
177942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
178042f52ef8SKeith Packard  * we use as a pipe index
178142f52ef8SKeith Packard  */
1782f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
17830a3e67a4SJesse Barnes {
17840a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1785e9d21d7fSKeith Packard 	unsigned long irqflags;
17860a3e67a4SJesse Barnes 
17871ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17888692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
17896b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
17908692d00eSChris Wilson 
17917c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
17927c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
17937c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
17941ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17950a3e67a4SJesse Barnes }
17960a3e67a4SJesse Barnes 
1797f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1798f796cf8fSJesse Barnes {
1799f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1800f796cf8fSJesse Barnes 	unsigned long irqflags;
1801b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
1802b518421fSPaulo Zanoni 						     DE_PIPE_VBLANK_ILK(pipe);
1803f796cf8fSJesse Barnes 
1804f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1805b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
1806b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1807b1f14ad0SJesse Barnes }
1808b1f14ad0SJesse Barnes 
18097e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
18107e231dbeSJesse Barnes {
18117e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18127e231dbeSJesse Barnes 	unsigned long irqflags;
181331acc7f5SJesse Barnes 	u32 imr;
18147e231dbeSJesse Barnes 
18157e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
181631acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
181731acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
18187e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
181931acc7f5SJesse Barnes 	if (pipe == 0)
18207e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
182131acc7f5SJesse Barnes 	else
18227e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
18237e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
18247e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
18257e231dbeSJesse Barnes }
18267e231dbeSJesse Barnes 
1827893eead0SChris Wilson static u32
1828893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1829852835f3SZou Nan hai {
1830893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1831893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1832893eead0SChris Wilson }
1833893eead0SChris Wilson 
18349107e9d2SChris Wilson static bool
18359107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
1836893eead0SChris Wilson {
18379107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
18389107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
1839f65d9421SBen Gamari }
1840f65d9421SBen Gamari 
18416274f212SChris Wilson static struct intel_ring_buffer *
18426274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
1843a24a11e6SChris Wilson {
1844a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
18456274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
1846a24a11e6SChris Wilson 
1847a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
1848a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
1849a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
18506274f212SChris Wilson 		return NULL;
1851a24a11e6SChris Wilson 
1852a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
1853a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
1854a24a11e6SChris Wilson 	 */
18556274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
1856a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
1857a24a11e6SChris Wilson 	do {
1858a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
1859a24a11e6SChris Wilson 		if (cmd == ipehr)
1860a24a11e6SChris Wilson 			break;
1861a24a11e6SChris Wilson 
1862a24a11e6SChris Wilson 		acthd -= 4;
1863a24a11e6SChris Wilson 		if (acthd < acthd_min)
18646274f212SChris Wilson 			return NULL;
1865a24a11e6SChris Wilson 	} while (1);
1866a24a11e6SChris Wilson 
18676274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
18686274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
1869a24a11e6SChris Wilson }
1870a24a11e6SChris Wilson 
18716274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
18726274f212SChris Wilson {
18736274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
18746274f212SChris Wilson 	struct intel_ring_buffer *signaller;
18756274f212SChris Wilson 	u32 seqno, ctl;
18766274f212SChris Wilson 
18776274f212SChris Wilson 	ring->hangcheck.deadlock = true;
18786274f212SChris Wilson 
18796274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
18806274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
18816274f212SChris Wilson 		return -1;
18826274f212SChris Wilson 
18836274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
18846274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
18856274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
18866274f212SChris Wilson 		return -1;
18876274f212SChris Wilson 
18886274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
18896274f212SChris Wilson }
18906274f212SChris Wilson 
18916274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
18926274f212SChris Wilson {
18936274f212SChris Wilson 	struct intel_ring_buffer *ring;
18946274f212SChris Wilson 	int i;
18956274f212SChris Wilson 
18966274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
18976274f212SChris Wilson 		ring->hangcheck.deadlock = false;
18986274f212SChris Wilson }
18996274f212SChris Wilson 
1900ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
1901ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
19021ec14ad3SChris Wilson {
19031ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
19041ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
19059107e9d2SChris Wilson 	u32 tmp;
19069107e9d2SChris Wilson 
19076274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
1908f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
19096274f212SChris Wilson 
19109107e9d2SChris Wilson 	if (IS_GEN2(dev))
1911f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
19129107e9d2SChris Wilson 
19139107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
19149107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
19159107e9d2SChris Wilson 	 * and break the hang. This should work on
19169107e9d2SChris Wilson 	 * all but the second generation chipsets.
19179107e9d2SChris Wilson 	 */
19189107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
19191ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
19201ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
19211ec14ad3SChris Wilson 			  ring->name);
19221ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
1923f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
19241ec14ad3SChris Wilson 	}
1925a24a11e6SChris Wilson 
19266274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
19276274f212SChris Wilson 		switch (semaphore_passed(ring)) {
19286274f212SChris Wilson 		default:
1929f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
19306274f212SChris Wilson 		case 1:
1931a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
1932a24a11e6SChris Wilson 				  ring->name);
1933a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
1934f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
19356274f212SChris Wilson 		case 0:
1936f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
19376274f212SChris Wilson 		}
19389107e9d2SChris Wilson 	}
19399107e9d2SChris Wilson 
1940f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
1941a24a11e6SChris Wilson }
1942d1e61e7fSChris Wilson 
1943f65d9421SBen Gamari /**
1944f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
194505407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
194605407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
194705407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
194805407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
194905407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
1950f65d9421SBen Gamari  */
1951a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
1952f65d9421SBen Gamari {
1953f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1954f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1955b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1956b4519513SChris Wilson 	int i;
195705407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
19589107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
19599107e9d2SChris Wilson #define BUSY 1
19609107e9d2SChris Wilson #define KICK 5
19619107e9d2SChris Wilson #define HUNG 20
19629107e9d2SChris Wilson #define FIRE 30
1963893eead0SChris Wilson 
19643e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
19653e0dc6b0SBen Widawsky 		return;
19663e0dc6b0SBen Widawsky 
1967b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
196805407ff8SMika Kuoppala 		u32 seqno, acthd;
19699107e9d2SChris Wilson 		bool busy = true;
1970b4519513SChris Wilson 
19716274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
19726274f212SChris Wilson 
197305407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
197405407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
197505407ff8SMika Kuoppala 
197605407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
19779107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
1978*da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
1979*da661464SMika Kuoppala 
19809107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
19819107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
19829107e9d2SChris Wilson 					DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
19839107e9d2SChris Wilson 						  ring->name);
19849107e9d2SChris Wilson 					wake_up_all(&ring->irq_queue);
19859107e9d2SChris Wilson 					ring->hangcheck.score += HUNG;
19869107e9d2SChris Wilson 				} else
19879107e9d2SChris Wilson 					busy = false;
198805407ff8SMika Kuoppala 			} else {
19896274f212SChris Wilson 				/* We always increment the hangcheck score
19906274f212SChris Wilson 				 * if the ring is busy and still processing
19916274f212SChris Wilson 				 * the same request, so that no single request
19926274f212SChris Wilson 				 * can run indefinitely (such as a chain of
19936274f212SChris Wilson 				 * batches). The only time we do not increment
19946274f212SChris Wilson 				 * the hangcheck score on this ring, if this
19956274f212SChris Wilson 				 * ring is in a legitimate wait for another
19966274f212SChris Wilson 				 * ring. In that case the waiting ring is a
19976274f212SChris Wilson 				 * victim and we want to be sure we catch the
19986274f212SChris Wilson 				 * right culprit. Then every time we do kick
19996274f212SChris Wilson 				 * the ring, add a small increment to the
20006274f212SChris Wilson 				 * score so that we can catch a batch that is
20016274f212SChris Wilson 				 * being repeatedly kicked and so responsible
20026274f212SChris Wilson 				 * for stalling the machine.
20039107e9d2SChris Wilson 				 */
2004ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2005ad8beaeaSMika Kuoppala 								    acthd);
2006ad8beaeaSMika Kuoppala 
2007ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2008*da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2009f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
20106274f212SChris Wilson 					break;
2011f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2012ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
20136274f212SChris Wilson 					break;
2014f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2015ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
20166274f212SChris Wilson 					break;
2017f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2018ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
20196274f212SChris Wilson 					stuck[i] = true;
20206274f212SChris Wilson 					break;
20216274f212SChris Wilson 				}
202205407ff8SMika Kuoppala 			}
20239107e9d2SChris Wilson 		} else {
2024*da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2025*da661464SMika Kuoppala 
20269107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
20279107e9d2SChris Wilson 			 * attempts across multiple batches.
20289107e9d2SChris Wilson 			 */
20299107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
20309107e9d2SChris Wilson 				ring->hangcheck.score--;
2031cbb465e7SChris Wilson 		}
2032f65d9421SBen Gamari 
203305407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
203405407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
20359107e9d2SChris Wilson 		busy_count += busy;
203605407ff8SMika Kuoppala 	}
203705407ff8SMika Kuoppala 
203805407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
20399107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2040b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
204105407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2042a43adf07SChris Wilson 				 ring->name);
2043a43adf07SChris Wilson 			rings_hung++;
204405407ff8SMika Kuoppala 		}
204505407ff8SMika Kuoppala 	}
204605407ff8SMika Kuoppala 
204705407ff8SMika Kuoppala 	if (rings_hung)
204805407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
204905407ff8SMika Kuoppala 
205005407ff8SMika Kuoppala 	if (busy_count)
205105407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
205205407ff8SMika Kuoppala 		 * being added */
205310cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
205410cd45b6SMika Kuoppala }
205510cd45b6SMika Kuoppala 
205610cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
205710cd45b6SMika Kuoppala {
205810cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
205910cd45b6SMika Kuoppala 	if (!i915_enable_hangcheck)
206010cd45b6SMika Kuoppala 		return;
206110cd45b6SMika Kuoppala 
206299584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
206310cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2064f65d9421SBen Gamari }
2065f65d9421SBen Gamari 
206691738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
206791738a95SPaulo Zanoni {
206891738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
206991738a95SPaulo Zanoni 
207091738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
207191738a95SPaulo Zanoni 		return;
207291738a95SPaulo Zanoni 
207391738a95SPaulo Zanoni 	/* south display irq */
207491738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
207591738a95SPaulo Zanoni 	/*
207691738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
207791738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
207891738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
207991738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
208091738a95SPaulo Zanoni 	 */
208191738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
208291738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
208391738a95SPaulo Zanoni }
208491738a95SPaulo Zanoni 
2085d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2086d18ea1b5SDaniel Vetter {
2087d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2088d18ea1b5SDaniel Vetter 
2089d18ea1b5SDaniel Vetter 	/* and GT */
2090d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2091d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2092d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2093d18ea1b5SDaniel Vetter 
2094d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2095d18ea1b5SDaniel Vetter 		/* and PM */
2096d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2097d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2098d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2099d18ea1b5SDaniel Vetter 	}
2100d18ea1b5SDaniel Vetter }
2101d18ea1b5SDaniel Vetter 
2102c0e09200SDave Airlie /* drm_dma.h hooks
2103c0e09200SDave Airlie */
2104f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2105036a4a7dSZhenyu Wang {
2106036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2107036a4a7dSZhenyu Wang 
21084697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
21094697995bSJesse Barnes 
2110036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2111bdfcdb63SDaniel Vetter 
2112036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2113036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
21143143a2bfSChris Wilson 	POSTING_READ(DEIER);
2115036a4a7dSZhenyu Wang 
2116d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2117c650156aSZhenyu Wang 
211891738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
21197d99163dSBen Widawsky }
21207d99163dSBen Widawsky 
21217e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
21227e231dbeSJesse Barnes {
21237e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21247e231dbeSJesse Barnes 	int pipe;
21257e231dbeSJesse Barnes 
21267e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
21277e231dbeSJesse Barnes 
21287e231dbeSJesse Barnes 	/* VLV magic */
21297e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
21307e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
21317e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
21327e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
21337e231dbeSJesse Barnes 
21347e231dbeSJesse Barnes 	/* and GT */
21357e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
21367e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2137d18ea1b5SDaniel Vetter 
2138d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
21397e231dbeSJesse Barnes 
21407e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
21417e231dbeSJesse Barnes 
21427e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
21437e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
21447e231dbeSJesse Barnes 	for_each_pipe(pipe)
21457e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
21467e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
21477e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
21487e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
21497e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
21507e231dbeSJesse Barnes }
21517e231dbeSJesse Barnes 
215282a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
215382a28bcfSDaniel Vetter {
215482a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
215582a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
215682a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2157fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
215882a28bcfSDaniel Vetter 
215982a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2160fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
216182a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2162cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2163fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
216482a28bcfSDaniel Vetter 	} else {
2165fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
216682a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2167cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2168fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
216982a28bcfSDaniel Vetter 	}
217082a28bcfSDaniel Vetter 
2171fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
217282a28bcfSDaniel Vetter 
21737fe0b973SKeith Packard 	/*
21747fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
21757fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
21767fe0b973SKeith Packard 	 *
21777fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
21787fe0b973SKeith Packard 	 */
21797fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
21807fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
21817fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
21827fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
21837fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
21847fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
21857fe0b973SKeith Packard }
21867fe0b973SKeith Packard 
2187d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2188d46da437SPaulo Zanoni {
2189d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
219082a28bcfSDaniel Vetter 	u32 mask;
2191d46da437SPaulo Zanoni 
2192692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2193692a04cfSDaniel Vetter 		return;
2194692a04cfSDaniel Vetter 
21958664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
21968664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2197de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
21988664281bSPaulo Zanoni 	} else {
21998664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
22008664281bSPaulo Zanoni 
22018664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
22028664281bSPaulo Zanoni 	}
2203ab5c608bSBen Widawsky 
2204d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2205d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2206d46da437SPaulo Zanoni }
2207d46da437SPaulo Zanoni 
22080a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
22090a9a8c91SDaniel Vetter {
22100a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
22110a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
22120a9a8c91SDaniel Vetter 
22130a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
22140a9a8c91SDaniel Vetter 
22150a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
22160a9a8c91SDaniel Vetter 	if (HAS_L3_GPU_CACHE(dev)) {
22170a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
22180a9a8c91SDaniel Vetter 		dev_priv->gt_irq_mask = ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
22190a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
22200a9a8c91SDaniel Vetter 	}
22210a9a8c91SDaniel Vetter 
22220a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
22230a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
22240a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
22250a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
22260a9a8c91SDaniel Vetter 	} else {
22270a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
22280a9a8c91SDaniel Vetter 	}
22290a9a8c91SDaniel Vetter 
22300a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
22310a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
22320a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
22330a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
22340a9a8c91SDaniel Vetter 
22350a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
22360a9a8c91SDaniel Vetter 		pm_irqs |= GEN6_PM_RPS_EVENTS;
22370a9a8c91SDaniel Vetter 
22380a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
22390a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
22400a9a8c91SDaniel Vetter 
2241605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
22420a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2243605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
22440a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
22450a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
22460a9a8c91SDaniel Vetter 	}
22470a9a8c91SDaniel Vetter }
22480a9a8c91SDaniel Vetter 
2249f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2250036a4a7dSZhenyu Wang {
22514bc9d430SDaniel Vetter 	unsigned long irqflags;
2252036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22538e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
22548e76f8dcSPaulo Zanoni 
22558e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
22568e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
22578e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
22588e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
22598e76f8dcSPaulo Zanoni 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
22608e76f8dcSPaulo Zanoni 				DE_ERR_INT_IVB);
22618e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
22628e76f8dcSPaulo Zanoni 			      DE_PIPEA_VBLANK_IVB);
22638e76f8dcSPaulo Zanoni 
22648e76f8dcSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
22658e76f8dcSPaulo Zanoni 	} else {
22668e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2267ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
22688664281bSPaulo Zanoni 				DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
22698e76f8dcSPaulo Zanoni 				DE_PIPEA_FIFO_UNDERRUN | DE_POISON);
22708e76f8dcSPaulo Zanoni 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
22718e76f8dcSPaulo Zanoni 	}
2272036a4a7dSZhenyu Wang 
22731ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2274036a4a7dSZhenyu Wang 
2275036a4a7dSZhenyu Wang 	/* should always can generate irq */
2276036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
22771ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
22788e76f8dcSPaulo Zanoni 	I915_WRITE(DEIER, display_mask | extra_mask);
22793143a2bfSChris Wilson 	POSTING_READ(DEIER);
2280036a4a7dSZhenyu Wang 
22810a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
2282036a4a7dSZhenyu Wang 
2283d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
22847fe0b973SKeith Packard 
2285f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
22866005ce42SDaniel Vetter 		/* Enable PCU event interrupts
22876005ce42SDaniel Vetter 		 *
22886005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
22894bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
22904bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
22914bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2292f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
22934bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2294f97108d1SJesse Barnes 	}
2295f97108d1SJesse Barnes 
2296036a4a7dSZhenyu Wang 	return 0;
2297036a4a7dSZhenyu Wang }
2298036a4a7dSZhenyu Wang 
22997e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
23007e231dbeSJesse Barnes {
23017e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23027e231dbeSJesse Barnes 	u32 enable_mask;
230331acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
2304b79480baSDaniel Vetter 	unsigned long irqflags;
23057e231dbeSJesse Barnes 
23067e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
230731acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
230831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
230931acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
23107e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23117e231dbeSJesse Barnes 
231231acc7f5SJesse Barnes 	/*
231331acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
231431acc7f5SJesse Barnes 	 * toggle them based on usage.
231531acc7f5SJesse Barnes 	 */
231631acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
231731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
231831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23197e231dbeSJesse Barnes 
232020afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
232120afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
232220afbda2SDaniel Vetter 
23237e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
23247e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
23257e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
23267e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
23277e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
23287e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
23297e231dbeSJesse Barnes 
2330b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2331b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2332b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
233331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2334515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
233531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
2336b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
233731acc7f5SJesse Barnes 
23387e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
23397e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
23407e231dbeSJesse Barnes 
23410a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
23427e231dbeSJesse Barnes 
23437e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
23447e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
23457e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
23467e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
23477e231dbeSJesse Barnes #endif
23487e231dbeSJesse Barnes 
23497e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
235020afbda2SDaniel Vetter 
235120afbda2SDaniel Vetter 	return 0;
235220afbda2SDaniel Vetter }
235320afbda2SDaniel Vetter 
23547e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
23557e231dbeSJesse Barnes {
23567e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23577e231dbeSJesse Barnes 	int pipe;
23587e231dbeSJesse Barnes 
23597e231dbeSJesse Barnes 	if (!dev_priv)
23607e231dbeSJesse Barnes 		return;
23617e231dbeSJesse Barnes 
2362ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2363ac4c16c5SEgbert Eich 
23647e231dbeSJesse Barnes 	for_each_pipe(pipe)
23657e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
23667e231dbeSJesse Barnes 
23677e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
23687e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
23697e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
23707e231dbeSJesse Barnes 	for_each_pipe(pipe)
23717e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
23727e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
23737e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
23747e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
23757e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
23767e231dbeSJesse Barnes }
23777e231dbeSJesse Barnes 
2378f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2379036a4a7dSZhenyu Wang {
2380036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23814697995bSJesse Barnes 
23824697995bSJesse Barnes 	if (!dev_priv)
23834697995bSJesse Barnes 		return;
23844697995bSJesse Barnes 
2385ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2386ac4c16c5SEgbert Eich 
2387036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2388036a4a7dSZhenyu Wang 
2389036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2390036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2391036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
23928664281bSPaulo Zanoni 	if (IS_GEN7(dev))
23938664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2394036a4a7dSZhenyu Wang 
2395036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2396036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2397036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2398192aac1fSKeith Packard 
2399ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2400ab5c608bSBen Widawsky 		return;
2401ab5c608bSBen Widawsky 
2402192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2403192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2404192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
24058664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
24068664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2407036a4a7dSZhenyu Wang }
2408036a4a7dSZhenyu Wang 
2409c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2410c2798b19SChris Wilson {
2411c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2412c2798b19SChris Wilson 	int pipe;
2413c2798b19SChris Wilson 
2414c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2415c2798b19SChris Wilson 
2416c2798b19SChris Wilson 	for_each_pipe(pipe)
2417c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2418c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2419c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2420c2798b19SChris Wilson 	POSTING_READ16(IER);
2421c2798b19SChris Wilson }
2422c2798b19SChris Wilson 
2423c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2424c2798b19SChris Wilson {
2425c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2426c2798b19SChris Wilson 
2427c2798b19SChris Wilson 	I915_WRITE16(EMR,
2428c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2429c2798b19SChris Wilson 
2430c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2431c2798b19SChris Wilson 	dev_priv->irq_mask =
2432c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2433c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2434c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2435c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2436c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2437c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2438c2798b19SChris Wilson 
2439c2798b19SChris Wilson 	I915_WRITE16(IER,
2440c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2441c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2442c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2443c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2444c2798b19SChris Wilson 	POSTING_READ16(IER);
2445c2798b19SChris Wilson 
2446c2798b19SChris Wilson 	return 0;
2447c2798b19SChris Wilson }
2448c2798b19SChris Wilson 
244990a72f87SVille Syrjälä /*
245090a72f87SVille Syrjälä  * Returns true when a page flip has completed.
245190a72f87SVille Syrjälä  */
245290a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
245390a72f87SVille Syrjälä 			       int pipe, u16 iir)
245490a72f87SVille Syrjälä {
245590a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
245690a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
245790a72f87SVille Syrjälä 
245890a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
245990a72f87SVille Syrjälä 		return false;
246090a72f87SVille Syrjälä 
246190a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
246290a72f87SVille Syrjälä 		return false;
246390a72f87SVille Syrjälä 
246490a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
246590a72f87SVille Syrjälä 
246690a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
246790a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
246890a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
246990a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
247090a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
247190a72f87SVille Syrjälä 	 */
247290a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
247390a72f87SVille Syrjälä 		return false;
247490a72f87SVille Syrjälä 
247590a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
247690a72f87SVille Syrjälä 
247790a72f87SVille Syrjälä 	return true;
247890a72f87SVille Syrjälä }
247990a72f87SVille Syrjälä 
2480ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2481c2798b19SChris Wilson {
2482c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2483c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2484c2798b19SChris Wilson 	u16 iir, new_iir;
2485c2798b19SChris Wilson 	u32 pipe_stats[2];
2486c2798b19SChris Wilson 	unsigned long irqflags;
2487c2798b19SChris Wilson 	int pipe;
2488c2798b19SChris Wilson 	u16 flip_mask =
2489c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2490c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2491c2798b19SChris Wilson 
2492c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2493c2798b19SChris Wilson 
2494c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2495c2798b19SChris Wilson 	if (iir == 0)
2496c2798b19SChris Wilson 		return IRQ_NONE;
2497c2798b19SChris Wilson 
2498c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2499c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2500c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2501c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2502c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2503c2798b19SChris Wilson 		 */
2504c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2505c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2506c2798b19SChris Wilson 			i915_handle_error(dev, false);
2507c2798b19SChris Wilson 
2508c2798b19SChris Wilson 		for_each_pipe(pipe) {
2509c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2510c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2511c2798b19SChris Wilson 
2512c2798b19SChris Wilson 			/*
2513c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2514c2798b19SChris Wilson 			 */
2515c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2516c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2517c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2518c2798b19SChris Wilson 							 pipe_name(pipe));
2519c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2520c2798b19SChris Wilson 			}
2521c2798b19SChris Wilson 		}
2522c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2523c2798b19SChris Wilson 
2524c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2525c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2526c2798b19SChris Wilson 
2527d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2528c2798b19SChris Wilson 
2529c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2530c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2531c2798b19SChris Wilson 
2532c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
253390a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
253490a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2535c2798b19SChris Wilson 
2536c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
253790a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
253890a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2539c2798b19SChris Wilson 
2540c2798b19SChris Wilson 		iir = new_iir;
2541c2798b19SChris Wilson 	}
2542c2798b19SChris Wilson 
2543c2798b19SChris Wilson 	return IRQ_HANDLED;
2544c2798b19SChris Wilson }
2545c2798b19SChris Wilson 
2546c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2547c2798b19SChris Wilson {
2548c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2549c2798b19SChris Wilson 	int pipe;
2550c2798b19SChris Wilson 
2551c2798b19SChris Wilson 	for_each_pipe(pipe) {
2552c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2553c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2554c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2555c2798b19SChris Wilson 	}
2556c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2557c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2558c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2559c2798b19SChris Wilson }
2560c2798b19SChris Wilson 
2561a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2562a266c7d5SChris Wilson {
2563a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2564a266c7d5SChris Wilson 	int pipe;
2565a266c7d5SChris Wilson 
2566a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2567a266c7d5SChris Wilson 
2568a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2569a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2570a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2571a266c7d5SChris Wilson 	}
2572a266c7d5SChris Wilson 
257300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2574a266c7d5SChris Wilson 	for_each_pipe(pipe)
2575a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2576a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2577a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2578a266c7d5SChris Wilson 	POSTING_READ(IER);
2579a266c7d5SChris Wilson }
2580a266c7d5SChris Wilson 
2581a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2582a266c7d5SChris Wilson {
2583a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
258438bde180SChris Wilson 	u32 enable_mask;
2585a266c7d5SChris Wilson 
258638bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
258738bde180SChris Wilson 
258838bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
258938bde180SChris Wilson 	dev_priv->irq_mask =
259038bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
259138bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
259238bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
259338bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
259438bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
259538bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
259638bde180SChris Wilson 
259738bde180SChris Wilson 	enable_mask =
259838bde180SChris Wilson 		I915_ASLE_INTERRUPT |
259938bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
260038bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
260138bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
260238bde180SChris Wilson 		I915_USER_INTERRUPT;
260338bde180SChris Wilson 
2604a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
260520afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
260620afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
260720afbda2SDaniel Vetter 
2608a266c7d5SChris Wilson 		/* Enable in IER... */
2609a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2610a266c7d5SChris Wilson 		/* and unmask in IMR */
2611a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2612a266c7d5SChris Wilson 	}
2613a266c7d5SChris Wilson 
2614a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2615a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2616a266c7d5SChris Wilson 	POSTING_READ(IER);
2617a266c7d5SChris Wilson 
2618f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
261920afbda2SDaniel Vetter 
262020afbda2SDaniel Vetter 	return 0;
262120afbda2SDaniel Vetter }
262220afbda2SDaniel Vetter 
262390a72f87SVille Syrjälä /*
262490a72f87SVille Syrjälä  * Returns true when a page flip has completed.
262590a72f87SVille Syrjälä  */
262690a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
262790a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
262890a72f87SVille Syrjälä {
262990a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
263090a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
263190a72f87SVille Syrjälä 
263290a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
263390a72f87SVille Syrjälä 		return false;
263490a72f87SVille Syrjälä 
263590a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
263690a72f87SVille Syrjälä 		return false;
263790a72f87SVille Syrjälä 
263890a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
263990a72f87SVille Syrjälä 
264090a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
264190a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
264290a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
264390a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
264490a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
264590a72f87SVille Syrjälä 	 */
264690a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
264790a72f87SVille Syrjälä 		return false;
264890a72f87SVille Syrjälä 
264990a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
265090a72f87SVille Syrjälä 
265190a72f87SVille Syrjälä 	return true;
265290a72f87SVille Syrjälä }
265390a72f87SVille Syrjälä 
2654ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2655a266c7d5SChris Wilson {
2656a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2657a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26588291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2659a266c7d5SChris Wilson 	unsigned long irqflags;
266038bde180SChris Wilson 	u32 flip_mask =
266138bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
266238bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
266338bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2664a266c7d5SChris Wilson 
2665a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2666a266c7d5SChris Wilson 
2667a266c7d5SChris Wilson 	iir = I915_READ(IIR);
266838bde180SChris Wilson 	do {
266938bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
26708291ee90SChris Wilson 		bool blc_event = false;
2671a266c7d5SChris Wilson 
2672a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2673a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2674a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2675a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2676a266c7d5SChris Wilson 		 */
2677a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2678a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2679a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2680a266c7d5SChris Wilson 
2681a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2682a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2683a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2684a266c7d5SChris Wilson 
268538bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2686a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2687a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2688a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2689a266c7d5SChris Wilson 							 pipe_name(pipe));
2690a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
269138bde180SChris Wilson 				irq_received = true;
2692a266c7d5SChris Wilson 			}
2693a266c7d5SChris Wilson 		}
2694a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2695a266c7d5SChris Wilson 
2696a266c7d5SChris Wilson 		if (!irq_received)
2697a266c7d5SChris Wilson 			break;
2698a266c7d5SChris Wilson 
2699a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2700a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2701a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2702a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2703b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
2704a266c7d5SChris Wilson 
2705a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2706a266c7d5SChris Wilson 				  hotplug_status);
270791d131d2SDaniel Vetter 
270810a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
270991d131d2SDaniel Vetter 
2710a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
271138bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2712a266c7d5SChris Wilson 		}
2713a266c7d5SChris Wilson 
271438bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2715a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2716a266c7d5SChris Wilson 
2717a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2718a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2719a266c7d5SChris Wilson 
2720a266c7d5SChris Wilson 		for_each_pipe(pipe) {
272138bde180SChris Wilson 			int plane = pipe;
272238bde180SChris Wilson 			if (IS_MOBILE(dev))
272338bde180SChris Wilson 				plane = !plane;
27245e2032d4SVille Syrjälä 
272590a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
272690a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
272790a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
2728a266c7d5SChris Wilson 
2729a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2730a266c7d5SChris Wilson 				blc_event = true;
2731a266c7d5SChris Wilson 		}
2732a266c7d5SChris Wilson 
2733a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2734a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2735a266c7d5SChris Wilson 
2736a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2737a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2738a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2739a266c7d5SChris Wilson 		 * we would never get another interrupt.
2740a266c7d5SChris Wilson 		 *
2741a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2742a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2743a266c7d5SChris Wilson 		 * another one.
2744a266c7d5SChris Wilson 		 *
2745a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2746a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2747a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2748a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2749a266c7d5SChris Wilson 		 * stray interrupts.
2750a266c7d5SChris Wilson 		 */
275138bde180SChris Wilson 		ret = IRQ_HANDLED;
2752a266c7d5SChris Wilson 		iir = new_iir;
275338bde180SChris Wilson 	} while (iir & ~flip_mask);
2754a266c7d5SChris Wilson 
2755d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
27568291ee90SChris Wilson 
2757a266c7d5SChris Wilson 	return ret;
2758a266c7d5SChris Wilson }
2759a266c7d5SChris Wilson 
2760a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2761a266c7d5SChris Wilson {
2762a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2763a266c7d5SChris Wilson 	int pipe;
2764a266c7d5SChris Wilson 
2765ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2766ac4c16c5SEgbert Eich 
2767a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2768a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2769a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2770a266c7d5SChris Wilson 	}
2771a266c7d5SChris Wilson 
277200d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
277355b39755SChris Wilson 	for_each_pipe(pipe) {
277455b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2775a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
277655b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
277755b39755SChris Wilson 	}
2778a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2779a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2780a266c7d5SChris Wilson 
2781a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2782a266c7d5SChris Wilson }
2783a266c7d5SChris Wilson 
2784a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2785a266c7d5SChris Wilson {
2786a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2787a266c7d5SChris Wilson 	int pipe;
2788a266c7d5SChris Wilson 
2789a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2790a266c7d5SChris Wilson 
2791a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2792a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2793a266c7d5SChris Wilson 
2794a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2795a266c7d5SChris Wilson 	for_each_pipe(pipe)
2796a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2797a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2798a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2799a266c7d5SChris Wilson 	POSTING_READ(IER);
2800a266c7d5SChris Wilson }
2801a266c7d5SChris Wilson 
2802a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2803a266c7d5SChris Wilson {
2804a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2805bbba0a97SChris Wilson 	u32 enable_mask;
2806a266c7d5SChris Wilson 	u32 error_mask;
2807b79480baSDaniel Vetter 	unsigned long irqflags;
2808a266c7d5SChris Wilson 
2809a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2810bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2811adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2812bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2813bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2814bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2815bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2816bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2817bbba0a97SChris Wilson 
2818bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
281921ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
282021ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
2821bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2822bbba0a97SChris Wilson 
2823bbba0a97SChris Wilson 	if (IS_G4X(dev))
2824bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2825a266c7d5SChris Wilson 
2826b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2827b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2828b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2829515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
2830b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2831a266c7d5SChris Wilson 
2832a266c7d5SChris Wilson 	/*
2833a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2834a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2835a266c7d5SChris Wilson 	 */
2836a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2837a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2838a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2839a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2840a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2841a266c7d5SChris Wilson 	} else {
2842a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2843a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2844a266c7d5SChris Wilson 	}
2845a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2846a266c7d5SChris Wilson 
2847a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2848a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2849a266c7d5SChris Wilson 	POSTING_READ(IER);
2850a266c7d5SChris Wilson 
285120afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
285220afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
285320afbda2SDaniel Vetter 
2854f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
285520afbda2SDaniel Vetter 
285620afbda2SDaniel Vetter 	return 0;
285720afbda2SDaniel Vetter }
285820afbda2SDaniel Vetter 
2859bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
286020afbda2SDaniel Vetter {
286120afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2862e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
2863cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
286420afbda2SDaniel Vetter 	u32 hotplug_en;
286520afbda2SDaniel Vetter 
2866b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2867b5ea2d56SDaniel Vetter 
2868bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
2869bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2870bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
2871adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
2872e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
2873cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2874cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2875cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
2876a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2877a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2878a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2879a266c7d5SChris Wilson 		*/
2880a266c7d5SChris Wilson 		if (IS_G4X(dev))
2881a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
288285fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
2883a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2884a266c7d5SChris Wilson 
2885a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2886a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2887a266c7d5SChris Wilson 	}
2888bac56d5bSEgbert Eich }
2889a266c7d5SChris Wilson 
2890ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
2891a266c7d5SChris Wilson {
2892a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2893a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2894a266c7d5SChris Wilson 	u32 iir, new_iir;
2895a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2896a266c7d5SChris Wilson 	unsigned long irqflags;
2897a266c7d5SChris Wilson 	int irq_received;
2898a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
289921ad8330SVille Syrjälä 	u32 flip_mask =
290021ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
290121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2902a266c7d5SChris Wilson 
2903a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2904a266c7d5SChris Wilson 
2905a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2906a266c7d5SChris Wilson 
2907a266c7d5SChris Wilson 	for (;;) {
29082c8ba29fSChris Wilson 		bool blc_event = false;
29092c8ba29fSChris Wilson 
291021ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
2911a266c7d5SChris Wilson 
2912a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2913a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2914a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2915a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2916a266c7d5SChris Wilson 		 */
2917a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2918a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2919a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2920a266c7d5SChris Wilson 
2921a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2922a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2923a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2924a266c7d5SChris Wilson 
2925a266c7d5SChris Wilson 			/*
2926a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2927a266c7d5SChris Wilson 			 */
2928a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2929a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2930a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2931a266c7d5SChris Wilson 							 pipe_name(pipe));
2932a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2933a266c7d5SChris Wilson 				irq_received = 1;
2934a266c7d5SChris Wilson 			}
2935a266c7d5SChris Wilson 		}
2936a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2937a266c7d5SChris Wilson 
2938a266c7d5SChris Wilson 		if (!irq_received)
2939a266c7d5SChris Wilson 			break;
2940a266c7d5SChris Wilson 
2941a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2942a266c7d5SChris Wilson 
2943a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2944adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2945a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2946b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
2947b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
29484f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
2949a266c7d5SChris Wilson 
2950a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2951a266c7d5SChris Wilson 				  hotplug_status);
295291d131d2SDaniel Vetter 
295310a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
295410a504deSDaniel Vetter 					      IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
295591d131d2SDaniel Vetter 
2956a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2957a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2958a266c7d5SChris Wilson 		}
2959a266c7d5SChris Wilson 
296021ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
2961a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2962a266c7d5SChris Wilson 
2963a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2964a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2965a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2966a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2967a266c7d5SChris Wilson 
2968a266c7d5SChris Wilson 		for_each_pipe(pipe) {
29692c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
297090a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
297190a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
2972a266c7d5SChris Wilson 
2973a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2974a266c7d5SChris Wilson 				blc_event = true;
2975a266c7d5SChris Wilson 		}
2976a266c7d5SChris Wilson 
2977a266c7d5SChris Wilson 
2978a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2979a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2980a266c7d5SChris Wilson 
2981515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
2982515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
2983515ac2bbSDaniel Vetter 
2984a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2985a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2986a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2987a266c7d5SChris Wilson 		 * we would never get another interrupt.
2988a266c7d5SChris Wilson 		 *
2989a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2990a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2991a266c7d5SChris Wilson 		 * another one.
2992a266c7d5SChris Wilson 		 *
2993a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2994a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2995a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2996a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2997a266c7d5SChris Wilson 		 * stray interrupts.
2998a266c7d5SChris Wilson 		 */
2999a266c7d5SChris Wilson 		iir = new_iir;
3000a266c7d5SChris Wilson 	}
3001a266c7d5SChris Wilson 
3002d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
30032c8ba29fSChris Wilson 
3004a266c7d5SChris Wilson 	return ret;
3005a266c7d5SChris Wilson }
3006a266c7d5SChris Wilson 
3007a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3008a266c7d5SChris Wilson {
3009a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3010a266c7d5SChris Wilson 	int pipe;
3011a266c7d5SChris Wilson 
3012a266c7d5SChris Wilson 	if (!dev_priv)
3013a266c7d5SChris Wilson 		return;
3014a266c7d5SChris Wilson 
3015ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3016ac4c16c5SEgbert Eich 
3017a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3018a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3019a266c7d5SChris Wilson 
3020a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3021a266c7d5SChris Wilson 	for_each_pipe(pipe)
3022a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3023a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3024a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3025a266c7d5SChris Wilson 
3026a266c7d5SChris Wilson 	for_each_pipe(pipe)
3027a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3028a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3029a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3030a266c7d5SChris Wilson }
3031a266c7d5SChris Wilson 
3032ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3033ac4c16c5SEgbert Eich {
3034ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3035ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3036ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3037ac4c16c5SEgbert Eich 	unsigned long irqflags;
3038ac4c16c5SEgbert Eich 	int i;
3039ac4c16c5SEgbert Eich 
3040ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3041ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3042ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3043ac4c16c5SEgbert Eich 
3044ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3045ac4c16c5SEgbert Eich 			continue;
3046ac4c16c5SEgbert Eich 
3047ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3048ac4c16c5SEgbert Eich 
3049ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3050ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3051ac4c16c5SEgbert Eich 
3052ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3053ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3054ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3055ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3056ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3057ac4c16c5SEgbert Eich 				if (!connector->polled)
3058ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3059ac4c16c5SEgbert Eich 			}
3060ac4c16c5SEgbert Eich 		}
3061ac4c16c5SEgbert Eich 	}
3062ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3063ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3064ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3065ac4c16c5SEgbert Eich }
3066ac4c16c5SEgbert Eich 
3067f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3068f71d4af4SJesse Barnes {
30698b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
30708b2e326dSChris Wilson 
30718b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
307299584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3073c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3074a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
30758b2e326dSChris Wilson 
307699584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
307799584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
307861bac78eSDaniel Vetter 		    (unsigned long) dev);
3079ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3080ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
308161bac78eSDaniel Vetter 
308297a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
30839ee32feaSDaniel Vetter 
3084f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
3085f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
30867d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3087f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3088f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3089f71d4af4SJesse Barnes 	}
3090f71d4af4SJesse Barnes 
3091c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3092f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3093c3613de9SKeith Packard 	else
3094c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
3095f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3096f71d4af4SJesse Barnes 
30977e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
30987e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
30997e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
31007e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
31017e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
31027e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
31037e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3104fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3105f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3106f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3107f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3108f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3109f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3110f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3111f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
311282a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3113f71d4af4SJesse Barnes 	} else {
3114c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3115c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3116c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3117c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3118c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3119a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3120a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3121a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3122a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3123a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
312420afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3125c2798b19SChris Wilson 		} else {
3126a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3127a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3128a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3129a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3130bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3131c2798b19SChris Wilson 		}
3132f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3133f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3134f71d4af4SJesse Barnes 	}
3135f71d4af4SJesse Barnes }
313620afbda2SDaniel Vetter 
313720afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
313820afbda2SDaniel Vetter {
313920afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3140821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3141821450c6SEgbert Eich 	struct drm_connector *connector;
3142b5ea2d56SDaniel Vetter 	unsigned long irqflags;
3143821450c6SEgbert Eich 	int i;
314420afbda2SDaniel Vetter 
3145821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3146821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3147821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3148821450c6SEgbert Eich 	}
3149821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3150821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3151821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3152821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3153821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3154821450c6SEgbert Eich 	}
3155b5ea2d56SDaniel Vetter 
3156b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3157b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
3158b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
315920afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
316020afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
3161b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
316220afbda2SDaniel Vetter }
3163c67a470bSPaulo Zanoni 
3164c67a470bSPaulo Zanoni /* Disable interrupts so we can allow Package C8+. */
3165c67a470bSPaulo Zanoni void hsw_pc8_disable_interrupts(struct drm_device *dev)
3166c67a470bSPaulo Zanoni {
3167c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3168c67a470bSPaulo Zanoni 	unsigned long irqflags;
3169c67a470bSPaulo Zanoni 
3170c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3171c67a470bSPaulo Zanoni 
3172c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.deimr = I915_READ(DEIMR);
3173c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.sdeimr = I915_READ(SDEIMR);
3174c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtimr = I915_READ(GTIMR);
3175c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtier = I915_READ(GTIER);
3176c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
3177c67a470bSPaulo Zanoni 
3178c67a470bSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, ~DE_PCH_EVENT_IVB);
3179c67a470bSPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, ~SDE_HOTPLUG_MASK_CPT);
3180c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
3181c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
3182c67a470bSPaulo Zanoni 
3183c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = true;
3184c67a470bSPaulo Zanoni 
3185c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3186c67a470bSPaulo Zanoni }
3187c67a470bSPaulo Zanoni 
3188c67a470bSPaulo Zanoni /* Restore interrupts so we can recover from Package C8+. */
3189c67a470bSPaulo Zanoni void hsw_pc8_restore_interrupts(struct drm_device *dev)
3190c67a470bSPaulo Zanoni {
3191c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3192c67a470bSPaulo Zanoni 	unsigned long irqflags;
3193c67a470bSPaulo Zanoni 	uint32_t val, expected;
3194c67a470bSPaulo Zanoni 
3195c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3196c67a470bSPaulo Zanoni 
3197c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
3198c67a470bSPaulo Zanoni 	expected = ~DE_PCH_EVENT_IVB;
3199c67a470bSPaulo Zanoni 	WARN(val != expected, "DEIMR is 0x%08x, not 0x%08x\n", val, expected);
3200c67a470bSPaulo Zanoni 
3201c67a470bSPaulo Zanoni 	val = I915_READ(SDEIMR) & ~SDE_HOTPLUG_MASK_CPT;
3202c67a470bSPaulo Zanoni 	expected = ~SDE_HOTPLUG_MASK_CPT;
3203c67a470bSPaulo Zanoni 	WARN(val != expected, "SDEIMR non-HPD bits are 0x%08x, not 0x%08x\n",
3204c67a470bSPaulo Zanoni 	     val, expected);
3205c67a470bSPaulo Zanoni 
3206c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
3207c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3208c67a470bSPaulo Zanoni 	WARN(val != expected, "GTIMR is 0x%08x, not 0x%08x\n", val, expected);
3209c67a470bSPaulo Zanoni 
3210c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
3211c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3212c67a470bSPaulo Zanoni 	WARN(val != expected, "GEN6_PMIMR is 0x%08x, not 0x%08x\n", val,
3213c67a470bSPaulo Zanoni 	     expected);
3214c67a470bSPaulo Zanoni 
3215c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = false;
3216c67a470bSPaulo Zanoni 
3217c67a470bSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pc8.regsave.deimr);
3218c67a470bSPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv,
3219c67a470bSPaulo Zanoni 				     ~dev_priv->pc8.regsave.sdeimr &
3220c67a470bSPaulo Zanoni 				     ~SDE_HOTPLUG_MASK_CPT);
3221c67a470bSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pc8.regsave.gtimr);
3222c67a470bSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pc8.regsave.gen6_pmimr);
3223c67a470bSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pc8.regsave.gtier);
3224c67a470bSPaulo Zanoni 
3225c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3226c67a470bSPaulo Zanoni }
3227