xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 040d2baa6229d50c406340035766c4e99725bf3d)
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 
66867c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
66967c347ffSJani Nikula 				struct drm_connector *connector)
670321a1b30SEgbert Eich {
671321a1b30SEgbert Eich 	enum drm_connector_status old_status;
672321a1b30SEgbert Eich 
673321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
674321a1b30SEgbert Eich 	old_status = connector->status;
675321a1b30SEgbert Eich 
676321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
67767c347ffSJani Nikula 	if (old_status == connector->status)
67867c347ffSJani Nikula 		return false;
67967c347ffSJani Nikula 
68067c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
681321a1b30SEgbert Eich 		      connector->base.id,
682321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
68367c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
68467c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
68567c347ffSJani Nikula 
68667c347ffSJani Nikula 	return true;
687321a1b30SEgbert Eich }
688321a1b30SEgbert Eich 
6895ca58282SJesse Barnes /*
6905ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
6915ca58282SJesse Barnes  */
692ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
693ac4c16c5SEgbert Eich 
6945ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
6955ca58282SJesse Barnes {
6965ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
6975ca58282SJesse Barnes 						    hotplug_work);
6985ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
699c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
700cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
701cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
702cd569aedSEgbert Eich 	struct drm_connector *connector;
703cd569aedSEgbert Eich 	unsigned long irqflags;
704cd569aedSEgbert Eich 	bool hpd_disabled = false;
705321a1b30SEgbert Eich 	bool changed = false;
706142e2398SEgbert Eich 	u32 hpd_event_bits;
7075ca58282SJesse Barnes 
70852d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
70952d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
71052d7ecedSDaniel Vetter 		return;
71152d7ecedSDaniel Vetter 
712a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
713e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
714e67189abSJesse Barnes 
715cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
716142e2398SEgbert Eich 
717142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
718142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
719cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
720cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
721cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
722cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
723cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
724cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
725cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
726cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
727cd569aedSEgbert Eich 				drm_get_connector_name(connector));
728cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
729cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
730cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
731cd569aedSEgbert Eich 			hpd_disabled = true;
732cd569aedSEgbert Eich 		}
733142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
734142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
735142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
736142e2398SEgbert Eich 		}
737cd569aedSEgbert Eich 	}
738cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
739cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
740cd569aedSEgbert Eich 	  * some connectors */
741ac4c16c5SEgbert Eich 	if (hpd_disabled) {
742cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
743ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
744ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
745ac4c16c5SEgbert Eich 	}
746cd569aedSEgbert Eich 
747cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
748cd569aedSEgbert Eich 
749321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
750321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
751321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
752321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
753cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
754cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
755321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
756321a1b30SEgbert Eich 				changed = true;
757321a1b30SEgbert Eich 		}
758321a1b30SEgbert Eich 	}
75940ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
76040ee3381SKeith Packard 
761321a1b30SEgbert Eich 	if (changed)
762321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
7635ca58282SJesse Barnes }
7645ca58282SJesse Barnes 
765d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
766f97108d1SJesse Barnes {
767f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
768b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
7699270388eSDaniel Vetter 	u8 new_delay;
7709270388eSDaniel Vetter 
771d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
772f97108d1SJesse Barnes 
77373edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
77473edd18fSDaniel Vetter 
77520e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
7769270388eSDaniel Vetter 
7777648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
778b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
779b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
780f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
781f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
782f97108d1SJesse Barnes 
783f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
784b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
78520e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
78620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
78720e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
78820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
789b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
79020e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
79120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
79220e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
79320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
794f97108d1SJesse Barnes 	}
795f97108d1SJesse Barnes 
7967648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
79720e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
798f97108d1SJesse Barnes 
799d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
8009270388eSDaniel Vetter 
801f97108d1SJesse Barnes 	return;
802f97108d1SJesse Barnes }
803f97108d1SJesse Barnes 
804549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
805549f7365SChris Wilson 			struct intel_ring_buffer *ring)
806549f7365SChris Wilson {
807475553deSChris Wilson 	if (ring->obj == NULL)
808475553deSChris Wilson 		return;
809475553deSChris Wilson 
810b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
8119862e600SChris Wilson 
812549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
81310cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
814549f7365SChris Wilson }
815549f7365SChris Wilson 
8164912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
8173b8d8d91SJesse Barnes {
8184912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
819c6a828d3SDaniel Vetter 						    rps.work);
820edbfdb45SPaulo Zanoni 	u32 pm_iir;
8217b9e0ae6SChris Wilson 	u8 new_delay;
8223b8d8d91SJesse Barnes 
82359cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
824c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
825c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
8264848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
827edbfdb45SPaulo Zanoni 	snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
82859cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
8294912d041SBen Widawsky 
83060611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
83160611c13SPaulo Zanoni 	WARN_ON(pm_iir & ~GEN6_PM_RPS_EVENTS);
83260611c13SPaulo Zanoni 
8334848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
8343b8d8d91SJesse Barnes 		return;
8353b8d8d91SJesse Barnes 
8364fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
8377b9e0ae6SChris Wilson 
8387425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
839c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
8407425034aSVille Syrjälä 
8417425034aSVille Syrjälä 		/*
8427425034aSVille Syrjälä 		 * For better performance, jump directly
8437425034aSVille Syrjälä 		 * to RPe if we're below it.
8447425034aSVille Syrjälä 		 */
8457425034aSVille Syrjälä 		if (IS_VALLEYVIEW(dev_priv->dev) &&
8467425034aSVille Syrjälä 		    dev_priv->rps.cur_delay < dev_priv->rps.rpe_delay)
8477425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
8487425034aSVille Syrjälä 	} else
849c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
8503b8d8d91SJesse Barnes 
85179249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
85279249636SBen Widawsky 	 * interrupt
85379249636SBen Widawsky 	 */
854d8289c9eSVille Syrjälä 	if (new_delay >= dev_priv->rps.min_delay &&
855d8289c9eSVille Syrjälä 	    new_delay <= dev_priv->rps.max_delay) {
8560a073b84SJesse Barnes 		if (IS_VALLEYVIEW(dev_priv->dev))
8570a073b84SJesse Barnes 			valleyview_set_rps(dev_priv->dev, new_delay);
8580a073b84SJesse Barnes 		else
8594912d041SBen Widawsky 			gen6_set_rps(dev_priv->dev, new_delay);
86079249636SBen Widawsky 	}
8613b8d8d91SJesse Barnes 
86252ceb908SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev)) {
86352ceb908SJesse Barnes 		/*
86452ceb908SJesse Barnes 		 * On VLV, when we enter RC6 we may not be at the minimum
86552ceb908SJesse Barnes 		 * voltage level, so arm a timer to check.  It should only
86652ceb908SJesse Barnes 		 * fire when there's activity or once after we've entered
86752ceb908SJesse Barnes 		 * RC6, and then won't be re-armed until the next RPS interrupt.
86852ceb908SJesse Barnes 		 */
86952ceb908SJesse Barnes 		mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
87052ceb908SJesse Barnes 				 msecs_to_jiffies(100));
87152ceb908SJesse Barnes 	}
87252ceb908SJesse Barnes 
8734fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
8743b8d8d91SJesse Barnes }
8753b8d8d91SJesse Barnes 
876e3689190SBen Widawsky 
877e3689190SBen Widawsky /**
878e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
879e3689190SBen Widawsky  * occurred.
880e3689190SBen Widawsky  * @work: workqueue struct
881e3689190SBen Widawsky  *
882e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
883e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
884e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
885e3689190SBen Widawsky  */
886e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
887e3689190SBen Widawsky {
888e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
889a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
890e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
89135a85ac6SBen Widawsky 	char *parity_event[6];
892e3689190SBen Widawsky 	uint32_t misccpctl;
893e3689190SBen Widawsky 	unsigned long flags;
89435a85ac6SBen Widawsky 	uint8_t slice = 0;
895e3689190SBen Widawsky 
896e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
897e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
898e3689190SBen Widawsky 	 * any time we access those registers.
899e3689190SBen Widawsky 	 */
900e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
901e3689190SBen Widawsky 
90235a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
90335a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
90435a85ac6SBen Widawsky 		goto out;
90535a85ac6SBen Widawsky 
906e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
907e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
908e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
909e3689190SBen Widawsky 
91035a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
91135a85ac6SBen Widawsky 		u32 reg;
91235a85ac6SBen Widawsky 
91335a85ac6SBen Widawsky 		slice--;
91435a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
91535a85ac6SBen Widawsky 			break;
91635a85ac6SBen Widawsky 
91735a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
91835a85ac6SBen Widawsky 
91935a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
92035a85ac6SBen Widawsky 
92135a85ac6SBen Widawsky 		error_status = I915_READ(reg);
922e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
923e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
924e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
925e3689190SBen Widawsky 
92635a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
92735a85ac6SBen Widawsky 		POSTING_READ(reg);
928e3689190SBen Widawsky 
929cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
930e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
931e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
932e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
93335a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
93435a85ac6SBen Widawsky 		parity_event[5] = NULL;
935e3689190SBen Widawsky 
936e3689190SBen Widawsky 		kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
937e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
938e3689190SBen Widawsky 
93935a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
94035a85ac6SBen Widawsky 			  slice, row, bank, subbank);
941e3689190SBen Widawsky 
94235a85ac6SBen Widawsky 		kfree(parity_event[4]);
943e3689190SBen Widawsky 		kfree(parity_event[3]);
944e3689190SBen Widawsky 		kfree(parity_event[2]);
945e3689190SBen Widawsky 		kfree(parity_event[1]);
946e3689190SBen Widawsky 	}
947e3689190SBen Widawsky 
94835a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
94935a85ac6SBen Widawsky 
95035a85ac6SBen Widawsky out:
95135a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
95235a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
95335a85ac6SBen Widawsky 	ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
95435a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
95535a85ac6SBen Widawsky 
95635a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
95735a85ac6SBen Widawsky }
95835a85ac6SBen Widawsky 
95935a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
960e3689190SBen Widawsky {
961e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
962e3689190SBen Widawsky 
963*040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
964e3689190SBen Widawsky 		return;
965e3689190SBen Widawsky 
966d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
96735a85ac6SBen Widawsky 	ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
968d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
969e3689190SBen Widawsky 
97035a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
97135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
97235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
97335a85ac6SBen Widawsky 
97435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
97535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
97635a85ac6SBen Widawsky 
977a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
978e3689190SBen Widawsky }
979e3689190SBen Widawsky 
980f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
981f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
982f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
983f1af8fc1SPaulo Zanoni {
984f1af8fc1SPaulo Zanoni 	if (gt_iir &
985f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
986f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
987f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
988f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
989f1af8fc1SPaulo Zanoni }
990f1af8fc1SPaulo Zanoni 
991e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
992e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
993e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
994e7b4c6b1SDaniel Vetter {
995e7b4c6b1SDaniel Vetter 
996cc609d5dSBen Widawsky 	if (gt_iir &
997cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
998e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
999cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1000e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1001cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1002e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1003e7b4c6b1SDaniel Vetter 
1004cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1005cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1006cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
1007e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
1008e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
1009e7b4c6b1SDaniel Vetter 	}
1010e3689190SBen Widawsky 
101135a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
101235a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1013e7b4c6b1SDaniel Vetter }
1014e7b4c6b1SDaniel Vetter 
1015b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1016b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1017b543fb04SEgbert Eich 
101810a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1019b543fb04SEgbert Eich 					 u32 hotplug_trigger,
1020b543fb04SEgbert Eich 					 const u32 *hpd)
1021b543fb04SEgbert Eich {
1022b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
1023b543fb04SEgbert Eich 	int i;
102410a504deSDaniel Vetter 	bool storm_detected = false;
1025b543fb04SEgbert Eich 
102691d131d2SDaniel Vetter 	if (!hotplug_trigger)
102791d131d2SDaniel Vetter 		return;
102891d131d2SDaniel Vetter 
1029b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1030b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
1031821450c6SEgbert Eich 
1032b8f102e8SEgbert Eich 		WARN(((hpd[i] & hotplug_trigger) &&
1033b8f102e8SEgbert Eich 		      dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED),
1034b8f102e8SEgbert Eich 		     "Received HPD interrupt although disabled\n");
1035b8f102e8SEgbert Eich 
1036b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1037b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1038b543fb04SEgbert Eich 			continue;
1039b543fb04SEgbert Eich 
1040bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1041b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1042b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1043b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1044b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1045b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1046b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1047b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1048b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1049142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1050b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
105110a504deSDaniel Vetter 			storm_detected = true;
1052b543fb04SEgbert Eich 		} else {
1053b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1054b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1055b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1056b543fb04SEgbert Eich 		}
1057b543fb04SEgbert Eich 	}
1058b543fb04SEgbert Eich 
105910a504deSDaniel Vetter 	if (storm_detected)
106010a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1061b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
10625876fa0dSDaniel Vetter 
1063645416f5SDaniel Vetter 	/*
1064645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1065645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1066645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1067645416f5SDaniel Vetter 	 * deadlock.
1068645416f5SDaniel Vetter 	 */
1069645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1070b543fb04SEgbert Eich }
1071b543fb04SEgbert Eich 
1072515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1073515ac2bbSDaniel Vetter {
107428c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
107528c70f16SDaniel Vetter 
107628c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1077515ac2bbSDaniel Vetter }
1078515ac2bbSDaniel Vetter 
1079ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1080ce99c256SDaniel Vetter {
10819ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
10829ee32feaSDaniel Vetter 
10839ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1084ce99c256SDaniel Vetter }
1085ce99c256SDaniel Vetter 
10861403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
10871403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
10881403c0d4SPaulo Zanoni  * the work queue. */
10891403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1090baf02a1fSBen Widawsky {
109141a05a3aSDaniel Vetter 	if (pm_iir & GEN6_PM_RPS_EVENTS) {
109259cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
10934848405cSBen Widawsky 		dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
10944d3b3d5fSPaulo Zanoni 		snb_disable_pm_irq(dev_priv, pm_iir & GEN6_PM_RPS_EVENTS);
109559cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
10962adbee62SDaniel Vetter 
10972adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
109841a05a3aSDaniel Vetter 	}
1099baf02a1fSBen Widawsky 
11001403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
110112638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
110212638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
110312638c57SBen Widawsky 
110412638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
110512638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
110612638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
110712638c57SBen Widawsky 		}
110812638c57SBen Widawsky 	}
11091403c0d4SPaulo Zanoni }
1110baf02a1fSBen Widawsky 
1111ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
11127e231dbeSJesse Barnes {
11137e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
11147e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11157e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
11167e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
11177e231dbeSJesse Barnes 	unsigned long irqflags;
11187e231dbeSJesse Barnes 	int pipe;
11197e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
11207e231dbeSJesse Barnes 
11217e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
11227e231dbeSJesse Barnes 
11237e231dbeSJesse Barnes 	while (true) {
11247e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
11257e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
11267e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
11277e231dbeSJesse Barnes 
11287e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
11297e231dbeSJesse Barnes 			goto out;
11307e231dbeSJesse Barnes 
11317e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
11327e231dbeSJesse Barnes 
1133e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
11347e231dbeSJesse Barnes 
11357e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
11367e231dbeSJesse Barnes 		for_each_pipe(pipe) {
11377e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
11387e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
11397e231dbeSJesse Barnes 
11407e231dbeSJesse Barnes 			/*
11417e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
11427e231dbeSJesse Barnes 			 */
11437e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
11447e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
11457e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
11467e231dbeSJesse Barnes 							 pipe_name(pipe));
11477e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
11487e231dbeSJesse Barnes 			}
11497e231dbeSJesse Barnes 		}
11507e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
11517e231dbeSJesse Barnes 
115231acc7f5SJesse Barnes 		for_each_pipe(pipe) {
115331acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
115431acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
115531acc7f5SJesse Barnes 
115631acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
115731acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
115831acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
115931acc7f5SJesse Barnes 			}
116031acc7f5SJesse Barnes 		}
116131acc7f5SJesse Barnes 
11627e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
11637e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
11647e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1165b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
11667e231dbeSJesse Barnes 
11677e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
11687e231dbeSJesse Barnes 					 hotplug_status);
116991d131d2SDaniel Vetter 
117010a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
117191d131d2SDaniel Vetter 
11727e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
11737e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
11747e231dbeSJesse Barnes 		}
11757e231dbeSJesse Barnes 
1176515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1177515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
11787e231dbeSJesse Barnes 
117960611c13SPaulo Zanoni 		if (pm_iir)
1180d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
11817e231dbeSJesse Barnes 
11827e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
11837e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
11847e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
11857e231dbeSJesse Barnes 	}
11867e231dbeSJesse Barnes 
11877e231dbeSJesse Barnes out:
11887e231dbeSJesse Barnes 	return ret;
11897e231dbeSJesse Barnes }
11907e231dbeSJesse Barnes 
119123e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1192776ad806SJesse Barnes {
1193776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11949db4a9c7SJesse Barnes 	int pipe;
1195b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1196776ad806SJesse Barnes 
119710a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
119891d131d2SDaniel Vetter 
1199cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1200cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1201776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1202cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1203cfc33bf7SVille Syrjälä 				 port_name(port));
1204cfc33bf7SVille Syrjälä 	}
1205776ad806SJesse Barnes 
1206ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1207ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1208ce99c256SDaniel Vetter 
1209776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1210515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1211776ad806SJesse Barnes 
1212776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1213776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1214776ad806SJesse Barnes 
1215776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1216776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1217776ad806SJesse Barnes 
1218776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1219776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1220776ad806SJesse Barnes 
12219db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
12229db4a9c7SJesse Barnes 		for_each_pipe(pipe)
12239db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
12249db4a9c7SJesse Barnes 					 pipe_name(pipe),
12259db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1226776ad806SJesse Barnes 
1227776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1228776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1229776ad806SJesse Barnes 
1230776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1231776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1232776ad806SJesse Barnes 
1233776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
12348664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
12358664281bSPaulo Zanoni 							  false))
12368664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
12378664281bSPaulo Zanoni 
12388664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
12398664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
12408664281bSPaulo Zanoni 							  false))
12418664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
12428664281bSPaulo Zanoni }
12438664281bSPaulo Zanoni 
12448664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
12458664281bSPaulo Zanoni {
12468664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
12478664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
12488664281bSPaulo Zanoni 
1249de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1250de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1251de032bf4SPaulo Zanoni 
12528664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_A)
12538664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
12548664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
12558664281bSPaulo Zanoni 
12568664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_B)
12578664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
12588664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
12598664281bSPaulo Zanoni 
12608664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_C)
12618664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
12628664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
12638664281bSPaulo Zanoni 
12648664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
12658664281bSPaulo Zanoni }
12668664281bSPaulo Zanoni 
12678664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
12688664281bSPaulo Zanoni {
12698664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
12708664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
12718664281bSPaulo Zanoni 
1272de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1273de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1274de032bf4SPaulo Zanoni 
12758664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
12768664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
12778664281bSPaulo Zanoni 							  false))
12788664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
12798664281bSPaulo Zanoni 
12808664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
12818664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
12828664281bSPaulo Zanoni 							  false))
12838664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
12848664281bSPaulo Zanoni 
12858664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
12868664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
12878664281bSPaulo Zanoni 							  false))
12888664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
12898664281bSPaulo Zanoni 
12908664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1291776ad806SJesse Barnes }
1292776ad806SJesse Barnes 
129323e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
129423e81d69SAdam Jackson {
129523e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
129623e81d69SAdam Jackson 	int pipe;
1297b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
129823e81d69SAdam Jackson 
129910a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
130091d131d2SDaniel Vetter 
1301cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1302cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
130323e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1304cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1305cfc33bf7SVille Syrjälä 				 port_name(port));
1306cfc33bf7SVille Syrjälä 	}
130723e81d69SAdam Jackson 
130823e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1309ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
131023e81d69SAdam Jackson 
131123e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1312515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
131323e81d69SAdam Jackson 
131423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
131523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
131623e81d69SAdam Jackson 
131723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
131823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
131923e81d69SAdam Jackson 
132023e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
132123e81d69SAdam Jackson 		for_each_pipe(pipe)
132223e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
132323e81d69SAdam Jackson 					 pipe_name(pipe),
132423e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
13258664281bSPaulo Zanoni 
13268664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
13278664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
132823e81d69SAdam Jackson }
132923e81d69SAdam Jackson 
1330c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1331c008bc6eSPaulo Zanoni {
1332c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1333c008bc6eSPaulo Zanoni 
1334c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1335c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1336c008bc6eSPaulo Zanoni 
1337c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1338c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1339c008bc6eSPaulo Zanoni 
1340c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEA_VBLANK)
1341c008bc6eSPaulo Zanoni 		drm_handle_vblank(dev, 0);
1342c008bc6eSPaulo Zanoni 
1343c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEB_VBLANK)
1344c008bc6eSPaulo Zanoni 		drm_handle_vblank(dev, 1);
1345c008bc6eSPaulo Zanoni 
1346c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1347c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1348c008bc6eSPaulo Zanoni 
1349c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
1350c008bc6eSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1351c008bc6eSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1352c008bc6eSPaulo Zanoni 
1353c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
1354c008bc6eSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1355c008bc6eSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1356c008bc6eSPaulo Zanoni 
1357c008bc6eSPaulo Zanoni 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1358c008bc6eSPaulo Zanoni 		intel_prepare_page_flip(dev, 0);
1359c008bc6eSPaulo Zanoni 		intel_finish_page_flip_plane(dev, 0);
1360c008bc6eSPaulo Zanoni 	}
1361c008bc6eSPaulo Zanoni 
1362c008bc6eSPaulo Zanoni 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1363c008bc6eSPaulo Zanoni 		intel_prepare_page_flip(dev, 1);
1364c008bc6eSPaulo Zanoni 		intel_finish_page_flip_plane(dev, 1);
1365c008bc6eSPaulo Zanoni 	}
1366c008bc6eSPaulo Zanoni 
1367c008bc6eSPaulo Zanoni 	/* check event from PCH */
1368c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1369c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1370c008bc6eSPaulo Zanoni 
1371c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1372c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1373c008bc6eSPaulo Zanoni 		else
1374c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1375c008bc6eSPaulo Zanoni 
1376c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1377c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1378c008bc6eSPaulo Zanoni 	}
1379c008bc6eSPaulo Zanoni 
1380c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1381c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1382c008bc6eSPaulo Zanoni }
1383c008bc6eSPaulo Zanoni 
13849719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
13859719fb98SPaulo Zanoni {
13869719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
13879719fb98SPaulo Zanoni 	int i;
13889719fb98SPaulo Zanoni 
13899719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
13909719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
13919719fb98SPaulo Zanoni 
13929719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
13939719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
13949719fb98SPaulo Zanoni 
13959719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
13969719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
13979719fb98SPaulo Zanoni 
13989719fb98SPaulo Zanoni 	for (i = 0; i < 3; i++) {
13999719fb98SPaulo Zanoni 		if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
14009719fb98SPaulo Zanoni 			drm_handle_vblank(dev, i);
14019719fb98SPaulo Zanoni 		if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
14029719fb98SPaulo Zanoni 			intel_prepare_page_flip(dev, i);
14039719fb98SPaulo Zanoni 			intel_finish_page_flip_plane(dev, i);
14049719fb98SPaulo Zanoni 		}
14059719fb98SPaulo Zanoni 	}
14069719fb98SPaulo Zanoni 
14079719fb98SPaulo Zanoni 	/* check event from PCH */
14089719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
14099719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
14109719fb98SPaulo Zanoni 
14119719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
14129719fb98SPaulo Zanoni 
14139719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
14149719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
14159719fb98SPaulo Zanoni 	}
14169719fb98SPaulo Zanoni }
14179719fb98SPaulo Zanoni 
1418f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1419b1f14ad0SJesse Barnes {
1420b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1421b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1422f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
14230e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1424333a8204SPaulo Zanoni 	bool err_int_reenable = false;
1425b1f14ad0SJesse Barnes 
1426b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1427b1f14ad0SJesse Barnes 
14288664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
14298664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1430907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
14318664281bSPaulo Zanoni 
1432b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1433b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1434b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
143523a78516SPaulo Zanoni 	POSTING_READ(DEIER);
14360e43406bSChris Wilson 
143744498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
143844498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
143944498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
144044498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
144144498aeaSPaulo Zanoni 	 * due to its back queue). */
1442ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
144344498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
144444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
144544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1446ab5c608bSBen Widawsky 	}
144744498aeaSPaulo Zanoni 
14488664281bSPaulo Zanoni 	/* On Haswell, also mask ERR_INT because we don't want to risk
14498664281bSPaulo Zanoni 	 * generating "unclaimed register" interrupts from inside the interrupt
14508664281bSPaulo Zanoni 	 * handler. */
14514bc9d430SDaniel Vetter 	if (IS_HASWELL(dev)) {
14524bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1453333a8204SPaulo Zanoni 		err_int_reenable = ~dev_priv->irq_mask & DE_ERR_INT_IVB;
1454333a8204SPaulo Zanoni 		if (err_int_reenable)
14558664281bSPaulo Zanoni 			ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
14564bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
14574bc9d430SDaniel Vetter 	}
14588664281bSPaulo Zanoni 
14590e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
14600e43406bSChris Wilson 	if (gt_iir) {
1461d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
14620e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1463d8fc8a47SPaulo Zanoni 		else
1464d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
14650e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
14660e43406bSChris Wilson 		ret = IRQ_HANDLED;
14670e43406bSChris Wilson 	}
1468b1f14ad0SJesse Barnes 
1469b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
14700e43406bSChris Wilson 	if (de_iir) {
1471f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
14729719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1473f1af8fc1SPaulo Zanoni 		else
1474f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
14750e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
14760e43406bSChris Wilson 		ret = IRQ_HANDLED;
14770e43406bSChris Wilson 	}
14780e43406bSChris Wilson 
1479f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
1480f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
14810e43406bSChris Wilson 		if (pm_iir) {
1482d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
1483b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
14840e43406bSChris Wilson 			ret = IRQ_HANDLED;
14850e43406bSChris Wilson 		}
1486f1af8fc1SPaulo Zanoni 	}
1487b1f14ad0SJesse Barnes 
1488333a8204SPaulo Zanoni 	if (err_int_reenable) {
14894bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
14904bc9d430SDaniel Vetter 		if (ivb_can_enable_err_int(dev))
14918664281bSPaulo Zanoni 			ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
14924bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
14934bc9d430SDaniel Vetter 	}
14948664281bSPaulo Zanoni 
1495b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1496b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1497ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
149844498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
149944498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1500ab5c608bSBen Widawsky 	}
1501b1f14ad0SJesse Barnes 
1502b1f14ad0SJesse Barnes 	return ret;
1503b1f14ad0SJesse Barnes }
1504b1f14ad0SJesse Barnes 
15058a905236SJesse Barnes /**
15068a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
15078a905236SJesse Barnes  * @work: work struct
15088a905236SJesse Barnes  *
15098a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
15108a905236SJesse Barnes  * was detected.
15118a905236SJesse Barnes  */
15128a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
15138a905236SJesse Barnes {
15141f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
15151f83fee0SDaniel Vetter 						    work);
15161f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
15171f83fee0SDaniel Vetter 						    gpu_error);
15188a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1519f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
1520cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1521cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1522cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
1523f69061beSDaniel Vetter 	int i, ret;
15248a905236SJesse Barnes 
1525f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
15268a905236SJesse Barnes 
15277db0ba24SDaniel Vetter 	/*
15287db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
15297db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
15307db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
15317db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
15327db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
15337db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
15347db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
15357db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
15367db0ba24SDaniel Vetter 	 */
15377db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
153844d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
15397db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
15407db0ba24SDaniel Vetter 				   reset_event);
15411f83fee0SDaniel Vetter 
1542f69061beSDaniel Vetter 		ret = i915_reset(dev);
1543f69061beSDaniel Vetter 
1544f69061beSDaniel Vetter 		if (ret == 0) {
1545f69061beSDaniel Vetter 			/*
1546f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1547f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1548f69061beSDaniel Vetter 			 * complete.
1549f69061beSDaniel Vetter 			 *
1550f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1551f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1552f69061beSDaniel Vetter 			 * updates before
1553f69061beSDaniel Vetter 			 * the counter increment.
1554f69061beSDaniel Vetter 			 */
1555f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1556f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1557f69061beSDaniel Vetter 
1558f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1559f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
15601f83fee0SDaniel Vetter 		} else {
15611f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1562f316a42cSBen Gamari 		}
15631f83fee0SDaniel Vetter 
1564f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
1565f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
1566f69061beSDaniel Vetter 
156796a02917SVille Syrjälä 		intel_display_handle_reset(dev);
156896a02917SVille Syrjälä 
15691f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
1570f316a42cSBen Gamari 	}
15718a905236SJesse Barnes }
15728a905236SJesse Barnes 
157335aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1574c0e09200SDave Airlie {
15758a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1576bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
157763eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1578050ee91fSBen Widawsky 	int pipe, i;
157963eeaf38SJesse Barnes 
158035aed2e6SChris Wilson 	if (!eir)
158135aed2e6SChris Wilson 		return;
158263eeaf38SJesse Barnes 
1583a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
15848a905236SJesse Barnes 
1585bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1586bd9854f9SBen Widawsky 
15878a905236SJesse Barnes 	if (IS_G4X(dev)) {
15888a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
15898a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
15908a905236SJesse Barnes 
1591a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1592a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1593050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1594050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1595a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1596a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
15978a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
15983143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
15998a905236SJesse Barnes 		}
16008a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
16018a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1602a70491ccSJoe Perches 			pr_err("page table error\n");
1603a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
16048a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
16053143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
16068a905236SJesse Barnes 		}
16078a905236SJesse Barnes 	}
16088a905236SJesse Barnes 
1609a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
161063eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
161163eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1612a70491ccSJoe Perches 			pr_err("page table error\n");
1613a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
161463eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
16153143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
161663eeaf38SJesse Barnes 		}
16178a905236SJesse Barnes 	}
16188a905236SJesse Barnes 
161963eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1620a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
16219db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1622a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
16239db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
162463eeaf38SJesse Barnes 		/* pipestat has already been acked */
162563eeaf38SJesse Barnes 	}
162663eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1627a70491ccSJoe Perches 		pr_err("instruction error\n");
1628a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1629050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1630050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1631a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
163263eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
163363eeaf38SJesse Barnes 
1634a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1635a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1636a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
163763eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
16383143a2bfSChris Wilson 			POSTING_READ(IPEIR);
163963eeaf38SJesse Barnes 		} else {
164063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
164163eeaf38SJesse Barnes 
1642a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1643a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1644a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1645a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
164663eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
16473143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
164863eeaf38SJesse Barnes 		}
164963eeaf38SJesse Barnes 	}
165063eeaf38SJesse Barnes 
165163eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
16523143a2bfSChris Wilson 	POSTING_READ(EIR);
165363eeaf38SJesse Barnes 	eir = I915_READ(EIR);
165463eeaf38SJesse Barnes 	if (eir) {
165563eeaf38SJesse Barnes 		/*
165663eeaf38SJesse Barnes 		 * some errors might have become stuck,
165763eeaf38SJesse Barnes 		 * mask them.
165863eeaf38SJesse Barnes 		 */
165963eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
166063eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
166163eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
166263eeaf38SJesse Barnes 	}
166335aed2e6SChris Wilson }
166435aed2e6SChris Wilson 
166535aed2e6SChris Wilson /**
166635aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
166735aed2e6SChris Wilson  * @dev: drm device
166835aed2e6SChris Wilson  *
166935aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
167035aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
167135aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
167235aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
167335aed2e6SChris Wilson  * of a ring dump etc.).
167435aed2e6SChris Wilson  */
1675527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
167635aed2e6SChris Wilson {
167735aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1678b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1679b4519513SChris Wilson 	int i;
168035aed2e6SChris Wilson 
168135aed2e6SChris Wilson 	i915_capture_error_state(dev);
168235aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
16838a905236SJesse Barnes 
1684ba1234d1SBen Gamari 	if (wedged) {
1685f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1686f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
1687ba1234d1SBen Gamari 
168811ed50ecSBen Gamari 		/*
16891f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
16901f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
169111ed50ecSBen Gamari 		 */
1692b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1693b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
169411ed50ecSBen Gamari 	}
169511ed50ecSBen Gamari 
169699584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
16978a905236SJesse Barnes }
16988a905236SJesse Barnes 
169921ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
17004e5359cdSSimon Farnsworth {
17014e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
17024e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
17034e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
170405394f39SChris Wilson 	struct drm_i915_gem_object *obj;
17054e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
17064e5359cdSSimon Farnsworth 	unsigned long flags;
17074e5359cdSSimon Farnsworth 	bool stall_detected;
17084e5359cdSSimon Farnsworth 
17094e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
17104e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
17114e5359cdSSimon Farnsworth 		return;
17124e5359cdSSimon Farnsworth 
17134e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
17144e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
17154e5359cdSSimon Farnsworth 
1716e7d841caSChris Wilson 	if (work == NULL ||
1717e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1718e7d841caSChris Wilson 	    !work->enable_stall_check) {
17194e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
17204e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
17214e5359cdSSimon Farnsworth 		return;
17224e5359cdSSimon Farnsworth 	}
17234e5359cdSSimon Farnsworth 
17244e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
172505394f39SChris Wilson 	obj = work->pending_flip_obj;
1726a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
17279db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1728446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1729f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
17304e5359cdSSimon Farnsworth 	} else {
17319db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
1732f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
173301f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
17344e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
17354e5359cdSSimon Farnsworth 	}
17364e5359cdSSimon Farnsworth 
17374e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
17384e5359cdSSimon Farnsworth 
17394e5359cdSSimon Farnsworth 	if (stall_detected) {
17404e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
17414e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
17424e5359cdSSimon Farnsworth 	}
17434e5359cdSSimon Farnsworth }
17444e5359cdSSimon Farnsworth 
174542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
174642f52ef8SKeith Packard  * we use as a pipe index
174742f52ef8SKeith Packard  */
1748f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
17490a3e67a4SJesse Barnes {
17500a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1751e9d21d7fSKeith Packard 	unsigned long irqflags;
175271e0ffa5SJesse Barnes 
17535eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
175471e0ffa5SJesse Barnes 		return -EINVAL;
17550a3e67a4SJesse Barnes 
17561ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1757f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
17587c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
17597c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
17600a3e67a4SJesse Barnes 	else
17617c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
17627c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
17638692d00eSChris Wilson 
17648692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
17658692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
17666b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
17671ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17688692d00eSChris Wilson 
17690a3e67a4SJesse Barnes 	return 0;
17700a3e67a4SJesse Barnes }
17710a3e67a4SJesse Barnes 
1772f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1773f796cf8fSJesse Barnes {
1774f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1775f796cf8fSJesse Barnes 	unsigned long irqflags;
1776b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
1777b518421fSPaulo Zanoni 						     DE_PIPE_VBLANK_ILK(pipe);
1778f796cf8fSJesse Barnes 
1779f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1780f796cf8fSJesse Barnes 		return -EINVAL;
1781f796cf8fSJesse Barnes 
1782f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1783b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
1784b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1785b1f14ad0SJesse Barnes 
1786b1f14ad0SJesse Barnes 	return 0;
1787b1f14ad0SJesse Barnes }
1788b1f14ad0SJesse Barnes 
17897e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
17907e231dbeSJesse Barnes {
17917e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17927e231dbeSJesse Barnes 	unsigned long irqflags;
179331acc7f5SJesse Barnes 	u32 imr;
17947e231dbeSJesse Barnes 
17957e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
17967e231dbeSJesse Barnes 		return -EINVAL;
17977e231dbeSJesse Barnes 
17987e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17997e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
180031acc7f5SJesse Barnes 	if (pipe == 0)
18017e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
180231acc7f5SJesse Barnes 	else
18037e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
18047e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
180531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
180631acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
18077e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
18087e231dbeSJesse Barnes 
18097e231dbeSJesse Barnes 	return 0;
18107e231dbeSJesse Barnes }
18117e231dbeSJesse Barnes 
181242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
181342f52ef8SKeith Packard  * we use as a pipe index
181442f52ef8SKeith Packard  */
1815f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
18160a3e67a4SJesse Barnes {
18170a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1818e9d21d7fSKeith Packard 	unsigned long irqflags;
18190a3e67a4SJesse Barnes 
18201ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
18218692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
18226b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
18238692d00eSChris Wilson 
18247c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
18257c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
18267c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
18271ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
18280a3e67a4SJesse Barnes }
18290a3e67a4SJesse Barnes 
1830f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1831f796cf8fSJesse Barnes {
1832f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1833f796cf8fSJesse Barnes 	unsigned long irqflags;
1834b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
1835b518421fSPaulo Zanoni 						     DE_PIPE_VBLANK_ILK(pipe);
1836f796cf8fSJesse Barnes 
1837f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1838b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
1839b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1840b1f14ad0SJesse Barnes }
1841b1f14ad0SJesse Barnes 
18427e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
18437e231dbeSJesse Barnes {
18447e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18457e231dbeSJesse Barnes 	unsigned long irqflags;
184631acc7f5SJesse Barnes 	u32 imr;
18477e231dbeSJesse Barnes 
18487e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
184931acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
185031acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
18517e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
185231acc7f5SJesse Barnes 	if (pipe == 0)
18537e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
185431acc7f5SJesse Barnes 	else
18557e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
18567e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
18577e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
18587e231dbeSJesse Barnes }
18597e231dbeSJesse Barnes 
1860893eead0SChris Wilson static u32
1861893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1862852835f3SZou Nan hai {
1863893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1864893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1865893eead0SChris Wilson }
1866893eead0SChris Wilson 
18679107e9d2SChris Wilson static bool
18689107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
1869893eead0SChris Wilson {
18709107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
18719107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
1872f65d9421SBen Gamari }
1873f65d9421SBen Gamari 
18746274f212SChris Wilson static struct intel_ring_buffer *
18756274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
1876a24a11e6SChris Wilson {
1877a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
18786274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
1879a24a11e6SChris Wilson 
1880a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
1881a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
1882a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
18836274f212SChris Wilson 		return NULL;
1884a24a11e6SChris Wilson 
1885a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
1886a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
1887a24a11e6SChris Wilson 	 */
18886274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
1889a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
1890a24a11e6SChris Wilson 	do {
1891a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
1892a24a11e6SChris Wilson 		if (cmd == ipehr)
1893a24a11e6SChris Wilson 			break;
1894a24a11e6SChris Wilson 
1895a24a11e6SChris Wilson 		acthd -= 4;
1896a24a11e6SChris Wilson 		if (acthd < acthd_min)
18976274f212SChris Wilson 			return NULL;
1898a24a11e6SChris Wilson 	} while (1);
1899a24a11e6SChris Wilson 
19006274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
19016274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
1902a24a11e6SChris Wilson }
1903a24a11e6SChris Wilson 
19046274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
19056274f212SChris Wilson {
19066274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
19076274f212SChris Wilson 	struct intel_ring_buffer *signaller;
19086274f212SChris Wilson 	u32 seqno, ctl;
19096274f212SChris Wilson 
19106274f212SChris Wilson 	ring->hangcheck.deadlock = true;
19116274f212SChris Wilson 
19126274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
19136274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
19146274f212SChris Wilson 		return -1;
19156274f212SChris Wilson 
19166274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
19176274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
19186274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
19196274f212SChris Wilson 		return -1;
19206274f212SChris Wilson 
19216274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
19226274f212SChris Wilson }
19236274f212SChris Wilson 
19246274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
19256274f212SChris Wilson {
19266274f212SChris Wilson 	struct intel_ring_buffer *ring;
19276274f212SChris Wilson 	int i;
19286274f212SChris Wilson 
19296274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
19306274f212SChris Wilson 		ring->hangcheck.deadlock = false;
19316274f212SChris Wilson }
19326274f212SChris Wilson 
1933ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
1934ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
19351ec14ad3SChris Wilson {
19361ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
19371ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
19389107e9d2SChris Wilson 	u32 tmp;
19399107e9d2SChris Wilson 
19406274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
1941f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
19426274f212SChris Wilson 
19439107e9d2SChris Wilson 	if (IS_GEN2(dev))
1944f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
19459107e9d2SChris Wilson 
19469107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
19479107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
19489107e9d2SChris Wilson 	 * and break the hang. This should work on
19499107e9d2SChris Wilson 	 * all but the second generation chipsets.
19509107e9d2SChris Wilson 	 */
19519107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
19521ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
19531ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
19541ec14ad3SChris Wilson 			  ring->name);
19551ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
1956f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
19571ec14ad3SChris Wilson 	}
1958a24a11e6SChris Wilson 
19596274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
19606274f212SChris Wilson 		switch (semaphore_passed(ring)) {
19616274f212SChris Wilson 		default:
1962f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
19636274f212SChris Wilson 		case 1:
1964a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
1965a24a11e6SChris Wilson 				  ring->name);
1966a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
1967f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
19686274f212SChris Wilson 		case 0:
1969f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
19706274f212SChris Wilson 		}
19719107e9d2SChris Wilson 	}
19729107e9d2SChris Wilson 
1973f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
1974a24a11e6SChris Wilson }
1975d1e61e7fSChris Wilson 
1976f65d9421SBen Gamari /**
1977f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
197805407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
197905407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
198005407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
198105407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
198205407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
1983f65d9421SBen Gamari  */
1984a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
1985f65d9421SBen Gamari {
1986f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1987f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1988b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1989b4519513SChris Wilson 	int i;
199005407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
19919107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
19929107e9d2SChris Wilson #define BUSY 1
19939107e9d2SChris Wilson #define KICK 5
19949107e9d2SChris Wilson #define HUNG 20
19959107e9d2SChris Wilson #define FIRE 30
1996893eead0SChris Wilson 
19973e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
19983e0dc6b0SBen Widawsky 		return;
19993e0dc6b0SBen Widawsky 
2000b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
200105407ff8SMika Kuoppala 		u32 seqno, acthd;
20029107e9d2SChris Wilson 		bool busy = true;
2003b4519513SChris Wilson 
20046274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
20056274f212SChris Wilson 
200605407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
200705407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
200805407ff8SMika Kuoppala 
200905407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
20109107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2011da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2012da661464SMika Kuoppala 
20139107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
20149107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
20159107e9d2SChris Wilson 					DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
20169107e9d2SChris Wilson 						  ring->name);
20179107e9d2SChris Wilson 					wake_up_all(&ring->irq_queue);
20189107e9d2SChris Wilson 					ring->hangcheck.score += HUNG;
20199107e9d2SChris Wilson 				} else
20209107e9d2SChris Wilson 					busy = false;
202105407ff8SMika Kuoppala 			} else {
20226274f212SChris Wilson 				/* We always increment the hangcheck score
20236274f212SChris Wilson 				 * if the ring is busy and still processing
20246274f212SChris Wilson 				 * the same request, so that no single request
20256274f212SChris Wilson 				 * can run indefinitely (such as a chain of
20266274f212SChris Wilson 				 * batches). The only time we do not increment
20276274f212SChris Wilson 				 * the hangcheck score on this ring, if this
20286274f212SChris Wilson 				 * ring is in a legitimate wait for another
20296274f212SChris Wilson 				 * ring. In that case the waiting ring is a
20306274f212SChris Wilson 				 * victim and we want to be sure we catch the
20316274f212SChris Wilson 				 * right culprit. Then every time we do kick
20326274f212SChris Wilson 				 * the ring, add a small increment to the
20336274f212SChris Wilson 				 * score so that we can catch a batch that is
20346274f212SChris Wilson 				 * being repeatedly kicked and so responsible
20356274f212SChris Wilson 				 * for stalling the machine.
20369107e9d2SChris Wilson 				 */
2037ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2038ad8beaeaSMika Kuoppala 								    acthd);
2039ad8beaeaSMika Kuoppala 
2040ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2041da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2042f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
20436274f212SChris Wilson 					break;
2044f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2045ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
20466274f212SChris Wilson 					break;
2047f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2048ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
20496274f212SChris Wilson 					break;
2050f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2051ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
20526274f212SChris Wilson 					stuck[i] = true;
20536274f212SChris Wilson 					break;
20546274f212SChris Wilson 				}
205505407ff8SMika Kuoppala 			}
20569107e9d2SChris Wilson 		} else {
2057da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2058da661464SMika Kuoppala 
20599107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
20609107e9d2SChris Wilson 			 * attempts across multiple batches.
20619107e9d2SChris Wilson 			 */
20629107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
20639107e9d2SChris Wilson 				ring->hangcheck.score--;
2064cbb465e7SChris Wilson 		}
2065f65d9421SBen Gamari 
206605407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
206705407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
20689107e9d2SChris Wilson 		busy_count += busy;
206905407ff8SMika Kuoppala 	}
207005407ff8SMika Kuoppala 
207105407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
20729107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2073b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
207405407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2075a43adf07SChris Wilson 				 ring->name);
2076a43adf07SChris Wilson 			rings_hung++;
207705407ff8SMika Kuoppala 		}
207805407ff8SMika Kuoppala 	}
207905407ff8SMika Kuoppala 
208005407ff8SMika Kuoppala 	if (rings_hung)
208105407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
208205407ff8SMika Kuoppala 
208305407ff8SMika Kuoppala 	if (busy_count)
208405407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
208505407ff8SMika Kuoppala 		 * being added */
208610cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
208710cd45b6SMika Kuoppala }
208810cd45b6SMika Kuoppala 
208910cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
209010cd45b6SMika Kuoppala {
209110cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
209210cd45b6SMika Kuoppala 	if (!i915_enable_hangcheck)
209310cd45b6SMika Kuoppala 		return;
209410cd45b6SMika Kuoppala 
209599584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
209610cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2097f65d9421SBen Gamari }
2098f65d9421SBen Gamari 
209991738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
210091738a95SPaulo Zanoni {
210191738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
210291738a95SPaulo Zanoni 
210391738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
210491738a95SPaulo Zanoni 		return;
210591738a95SPaulo Zanoni 
210691738a95SPaulo Zanoni 	/* south display irq */
210791738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
210891738a95SPaulo Zanoni 	/*
210991738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
211091738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
211191738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
211291738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
211391738a95SPaulo Zanoni 	 */
211491738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
211591738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
211691738a95SPaulo Zanoni }
211791738a95SPaulo Zanoni 
2118d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2119d18ea1b5SDaniel Vetter {
2120d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2121d18ea1b5SDaniel Vetter 
2122d18ea1b5SDaniel Vetter 	/* and GT */
2123d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2124d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2125d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2126d18ea1b5SDaniel Vetter 
2127d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2128d18ea1b5SDaniel Vetter 		/* and PM */
2129d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2130d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2131d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2132d18ea1b5SDaniel Vetter 	}
2133d18ea1b5SDaniel Vetter }
2134d18ea1b5SDaniel Vetter 
2135c0e09200SDave Airlie /* drm_dma.h hooks
2136c0e09200SDave Airlie */
2137f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2138036a4a7dSZhenyu Wang {
2139036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2140036a4a7dSZhenyu Wang 
21414697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
21424697995bSJesse Barnes 
2143036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2144bdfcdb63SDaniel Vetter 
2145036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2146036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
21473143a2bfSChris Wilson 	POSTING_READ(DEIER);
2148036a4a7dSZhenyu Wang 
2149d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2150c650156aSZhenyu Wang 
215191738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
21527d99163dSBen Widawsky }
21537d99163dSBen Widawsky 
21547e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
21557e231dbeSJesse Barnes {
21567e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21577e231dbeSJesse Barnes 	int pipe;
21587e231dbeSJesse Barnes 
21597e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
21607e231dbeSJesse Barnes 
21617e231dbeSJesse Barnes 	/* VLV magic */
21627e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
21637e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
21647e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
21657e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
21667e231dbeSJesse Barnes 
21677e231dbeSJesse Barnes 	/* and GT */
21687e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
21697e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2170d18ea1b5SDaniel Vetter 
2171d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
21727e231dbeSJesse Barnes 
21737e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
21747e231dbeSJesse Barnes 
21757e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
21767e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
21777e231dbeSJesse Barnes 	for_each_pipe(pipe)
21787e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
21797e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
21807e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
21817e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
21827e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
21837e231dbeSJesse Barnes }
21847e231dbeSJesse Barnes 
218582a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
218682a28bcfSDaniel Vetter {
218782a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
218882a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
218982a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2190fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
219182a28bcfSDaniel Vetter 
219282a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2193fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
219482a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2195cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2196fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
219782a28bcfSDaniel Vetter 	} else {
2198fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
219982a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2200cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2201fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
220282a28bcfSDaniel Vetter 	}
220382a28bcfSDaniel Vetter 
2204fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
220582a28bcfSDaniel Vetter 
22067fe0b973SKeith Packard 	/*
22077fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
22087fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
22097fe0b973SKeith Packard 	 *
22107fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
22117fe0b973SKeith Packard 	 */
22127fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
22137fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
22147fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
22157fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
22167fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
22177fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
22187fe0b973SKeith Packard }
22197fe0b973SKeith Packard 
2220d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2221d46da437SPaulo Zanoni {
2222d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
222382a28bcfSDaniel Vetter 	u32 mask;
2224d46da437SPaulo Zanoni 
2225692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2226692a04cfSDaniel Vetter 		return;
2227692a04cfSDaniel Vetter 
22288664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
22298664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2230de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
22318664281bSPaulo Zanoni 	} else {
22328664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
22338664281bSPaulo Zanoni 
22348664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
22358664281bSPaulo Zanoni 	}
2236ab5c608bSBen Widawsky 
2237d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2238d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2239d46da437SPaulo Zanoni }
2240d46da437SPaulo Zanoni 
22410a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
22420a9a8c91SDaniel Vetter {
22430a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
22440a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
22450a9a8c91SDaniel Vetter 
22460a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
22470a9a8c91SDaniel Vetter 
22480a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
2249*040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
22500a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
225135a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
225235a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
22530a9a8c91SDaniel Vetter 	}
22540a9a8c91SDaniel Vetter 
22550a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
22560a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
22570a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
22580a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
22590a9a8c91SDaniel Vetter 	} else {
22600a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
22610a9a8c91SDaniel Vetter 	}
22620a9a8c91SDaniel Vetter 
22630a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
22640a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
22650a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
22660a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
22670a9a8c91SDaniel Vetter 
22680a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
22690a9a8c91SDaniel Vetter 		pm_irqs |= GEN6_PM_RPS_EVENTS;
22700a9a8c91SDaniel Vetter 
22710a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
22720a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
22730a9a8c91SDaniel Vetter 
2274605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
22750a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2276605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
22770a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
22780a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
22790a9a8c91SDaniel Vetter 	}
22800a9a8c91SDaniel Vetter }
22810a9a8c91SDaniel Vetter 
2282f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2283036a4a7dSZhenyu Wang {
22844bc9d430SDaniel Vetter 	unsigned long irqflags;
2285036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22868e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
22878e76f8dcSPaulo Zanoni 
22888e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
22898e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
22908e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
22918e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
22928e76f8dcSPaulo Zanoni 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
22938e76f8dcSPaulo Zanoni 				DE_ERR_INT_IVB);
22948e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
22958e76f8dcSPaulo Zanoni 			      DE_PIPEA_VBLANK_IVB);
22968e76f8dcSPaulo Zanoni 
22978e76f8dcSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
22988e76f8dcSPaulo Zanoni 	} else {
22998e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2300ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
23018664281bSPaulo Zanoni 				DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
23028e76f8dcSPaulo Zanoni 				DE_PIPEA_FIFO_UNDERRUN | DE_POISON);
23038e76f8dcSPaulo Zanoni 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
23048e76f8dcSPaulo Zanoni 	}
2305036a4a7dSZhenyu Wang 
23061ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2307036a4a7dSZhenyu Wang 
2308036a4a7dSZhenyu Wang 	/* should always can generate irq */
2309036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
23101ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
23118e76f8dcSPaulo Zanoni 	I915_WRITE(DEIER, display_mask | extra_mask);
23123143a2bfSChris Wilson 	POSTING_READ(DEIER);
2313036a4a7dSZhenyu Wang 
23140a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
2315036a4a7dSZhenyu Wang 
2316d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
23177fe0b973SKeith Packard 
2318f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
23196005ce42SDaniel Vetter 		/* Enable PCU event interrupts
23206005ce42SDaniel Vetter 		 *
23216005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
23224bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
23234bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
23244bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2325f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
23264bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2327f97108d1SJesse Barnes 	}
2328f97108d1SJesse Barnes 
2329036a4a7dSZhenyu Wang 	return 0;
2330036a4a7dSZhenyu Wang }
2331036a4a7dSZhenyu Wang 
23327e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
23337e231dbeSJesse Barnes {
23347e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23357e231dbeSJesse Barnes 	u32 enable_mask;
233631acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
2337b79480baSDaniel Vetter 	unsigned long irqflags;
23387e231dbeSJesse Barnes 
23397e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
234031acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
234131acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
234231acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
23437e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23447e231dbeSJesse Barnes 
234531acc7f5SJesse Barnes 	/*
234631acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
234731acc7f5SJesse Barnes 	 * toggle them based on usage.
234831acc7f5SJesse Barnes 	 */
234931acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
235031acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
235131acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23527e231dbeSJesse Barnes 
235320afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
235420afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
235520afbda2SDaniel Vetter 
23567e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
23577e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
23587e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
23597e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
23607e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
23617e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
23627e231dbeSJesse Barnes 
2363b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2364b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2365b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
236631acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2367515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
236831acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
2369b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
237031acc7f5SJesse Barnes 
23717e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
23727e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
23737e231dbeSJesse Barnes 
23740a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
23757e231dbeSJesse Barnes 
23767e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
23777e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
23787e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
23797e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
23807e231dbeSJesse Barnes #endif
23817e231dbeSJesse Barnes 
23827e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
238320afbda2SDaniel Vetter 
238420afbda2SDaniel Vetter 	return 0;
238520afbda2SDaniel Vetter }
238620afbda2SDaniel Vetter 
23877e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
23887e231dbeSJesse Barnes {
23897e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23907e231dbeSJesse Barnes 	int pipe;
23917e231dbeSJesse Barnes 
23927e231dbeSJesse Barnes 	if (!dev_priv)
23937e231dbeSJesse Barnes 		return;
23947e231dbeSJesse Barnes 
2395ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2396ac4c16c5SEgbert Eich 
23977e231dbeSJesse Barnes 	for_each_pipe(pipe)
23987e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
23997e231dbeSJesse Barnes 
24007e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
24017e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
24027e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
24037e231dbeSJesse Barnes 	for_each_pipe(pipe)
24047e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
24057e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
24067e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
24077e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
24087e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
24097e231dbeSJesse Barnes }
24107e231dbeSJesse Barnes 
2411f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2412036a4a7dSZhenyu Wang {
2413036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24144697995bSJesse Barnes 
24154697995bSJesse Barnes 	if (!dev_priv)
24164697995bSJesse Barnes 		return;
24174697995bSJesse Barnes 
2418ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2419ac4c16c5SEgbert Eich 
2420036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2421036a4a7dSZhenyu Wang 
2422036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2423036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2424036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
24258664281bSPaulo Zanoni 	if (IS_GEN7(dev))
24268664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2427036a4a7dSZhenyu Wang 
2428036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2429036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2430036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2431192aac1fSKeith Packard 
2432ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2433ab5c608bSBen Widawsky 		return;
2434ab5c608bSBen Widawsky 
2435192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2436192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2437192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
24388664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
24398664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2440036a4a7dSZhenyu Wang }
2441036a4a7dSZhenyu Wang 
2442c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2443c2798b19SChris Wilson {
2444c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2445c2798b19SChris Wilson 	int pipe;
2446c2798b19SChris Wilson 
2447c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2448c2798b19SChris Wilson 
2449c2798b19SChris Wilson 	for_each_pipe(pipe)
2450c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2451c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2452c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2453c2798b19SChris Wilson 	POSTING_READ16(IER);
2454c2798b19SChris Wilson }
2455c2798b19SChris Wilson 
2456c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2457c2798b19SChris Wilson {
2458c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2459c2798b19SChris Wilson 
2460c2798b19SChris Wilson 	I915_WRITE16(EMR,
2461c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2462c2798b19SChris Wilson 
2463c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2464c2798b19SChris Wilson 	dev_priv->irq_mask =
2465c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2466c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2467c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2468c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2469c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2470c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2471c2798b19SChris Wilson 
2472c2798b19SChris Wilson 	I915_WRITE16(IER,
2473c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2474c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2475c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2476c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2477c2798b19SChris Wilson 	POSTING_READ16(IER);
2478c2798b19SChris Wilson 
2479c2798b19SChris Wilson 	return 0;
2480c2798b19SChris Wilson }
2481c2798b19SChris Wilson 
248290a72f87SVille Syrjälä /*
248390a72f87SVille Syrjälä  * Returns true when a page flip has completed.
248490a72f87SVille Syrjälä  */
248590a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
248690a72f87SVille Syrjälä 			       int pipe, u16 iir)
248790a72f87SVille Syrjälä {
248890a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
248990a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
249090a72f87SVille Syrjälä 
249190a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
249290a72f87SVille Syrjälä 		return false;
249390a72f87SVille Syrjälä 
249490a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
249590a72f87SVille Syrjälä 		return false;
249690a72f87SVille Syrjälä 
249790a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
249890a72f87SVille Syrjälä 
249990a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
250090a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
250190a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
250290a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
250390a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
250490a72f87SVille Syrjälä 	 */
250590a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
250690a72f87SVille Syrjälä 		return false;
250790a72f87SVille Syrjälä 
250890a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
250990a72f87SVille Syrjälä 
251090a72f87SVille Syrjälä 	return true;
251190a72f87SVille Syrjälä }
251290a72f87SVille Syrjälä 
2513ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2514c2798b19SChris Wilson {
2515c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2516c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2517c2798b19SChris Wilson 	u16 iir, new_iir;
2518c2798b19SChris Wilson 	u32 pipe_stats[2];
2519c2798b19SChris Wilson 	unsigned long irqflags;
2520c2798b19SChris Wilson 	int pipe;
2521c2798b19SChris Wilson 	u16 flip_mask =
2522c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2523c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2524c2798b19SChris Wilson 
2525c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2526c2798b19SChris Wilson 
2527c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2528c2798b19SChris Wilson 	if (iir == 0)
2529c2798b19SChris Wilson 		return IRQ_NONE;
2530c2798b19SChris Wilson 
2531c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2532c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2533c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2534c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2535c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2536c2798b19SChris Wilson 		 */
2537c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2538c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2539c2798b19SChris Wilson 			i915_handle_error(dev, false);
2540c2798b19SChris Wilson 
2541c2798b19SChris Wilson 		for_each_pipe(pipe) {
2542c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2543c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2544c2798b19SChris Wilson 
2545c2798b19SChris Wilson 			/*
2546c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2547c2798b19SChris Wilson 			 */
2548c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2549c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2550c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2551c2798b19SChris Wilson 							 pipe_name(pipe));
2552c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2553c2798b19SChris Wilson 			}
2554c2798b19SChris Wilson 		}
2555c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2556c2798b19SChris Wilson 
2557c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2558c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2559c2798b19SChris Wilson 
2560d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2561c2798b19SChris Wilson 
2562c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2563c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2564c2798b19SChris Wilson 
2565c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
256690a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
256790a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2568c2798b19SChris Wilson 
2569c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
257090a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
257190a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2572c2798b19SChris Wilson 
2573c2798b19SChris Wilson 		iir = new_iir;
2574c2798b19SChris Wilson 	}
2575c2798b19SChris Wilson 
2576c2798b19SChris Wilson 	return IRQ_HANDLED;
2577c2798b19SChris Wilson }
2578c2798b19SChris Wilson 
2579c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2580c2798b19SChris Wilson {
2581c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2582c2798b19SChris Wilson 	int pipe;
2583c2798b19SChris Wilson 
2584c2798b19SChris Wilson 	for_each_pipe(pipe) {
2585c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2586c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2587c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2588c2798b19SChris Wilson 	}
2589c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2590c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2591c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2592c2798b19SChris Wilson }
2593c2798b19SChris Wilson 
2594a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2595a266c7d5SChris Wilson {
2596a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2597a266c7d5SChris Wilson 	int pipe;
2598a266c7d5SChris Wilson 
2599a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2600a266c7d5SChris Wilson 
2601a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2602a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2603a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2604a266c7d5SChris Wilson 	}
2605a266c7d5SChris Wilson 
260600d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2607a266c7d5SChris Wilson 	for_each_pipe(pipe)
2608a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2609a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2610a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2611a266c7d5SChris Wilson 	POSTING_READ(IER);
2612a266c7d5SChris Wilson }
2613a266c7d5SChris Wilson 
2614a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2615a266c7d5SChris Wilson {
2616a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
261738bde180SChris Wilson 	u32 enable_mask;
2618a266c7d5SChris Wilson 
261938bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
262038bde180SChris Wilson 
262138bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
262238bde180SChris Wilson 	dev_priv->irq_mask =
262338bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
262438bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
262538bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
262638bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
262738bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
262838bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
262938bde180SChris Wilson 
263038bde180SChris Wilson 	enable_mask =
263138bde180SChris Wilson 		I915_ASLE_INTERRUPT |
263238bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
263338bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
263438bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
263538bde180SChris Wilson 		I915_USER_INTERRUPT;
263638bde180SChris Wilson 
2637a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
263820afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
263920afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
264020afbda2SDaniel Vetter 
2641a266c7d5SChris Wilson 		/* Enable in IER... */
2642a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2643a266c7d5SChris Wilson 		/* and unmask in IMR */
2644a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2645a266c7d5SChris Wilson 	}
2646a266c7d5SChris Wilson 
2647a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2648a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2649a266c7d5SChris Wilson 	POSTING_READ(IER);
2650a266c7d5SChris Wilson 
2651f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
265220afbda2SDaniel Vetter 
265320afbda2SDaniel Vetter 	return 0;
265420afbda2SDaniel Vetter }
265520afbda2SDaniel Vetter 
265690a72f87SVille Syrjälä /*
265790a72f87SVille Syrjälä  * Returns true when a page flip has completed.
265890a72f87SVille Syrjälä  */
265990a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
266090a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
266190a72f87SVille Syrjälä {
266290a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
266390a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
266490a72f87SVille Syrjälä 
266590a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
266690a72f87SVille Syrjälä 		return false;
266790a72f87SVille Syrjälä 
266890a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
266990a72f87SVille Syrjälä 		return false;
267090a72f87SVille Syrjälä 
267190a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
267290a72f87SVille Syrjälä 
267390a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
267490a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
267590a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
267690a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
267790a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
267890a72f87SVille Syrjälä 	 */
267990a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
268090a72f87SVille Syrjälä 		return false;
268190a72f87SVille Syrjälä 
268290a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
268390a72f87SVille Syrjälä 
268490a72f87SVille Syrjälä 	return true;
268590a72f87SVille Syrjälä }
268690a72f87SVille Syrjälä 
2687ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2688a266c7d5SChris Wilson {
2689a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2690a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26918291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2692a266c7d5SChris Wilson 	unsigned long irqflags;
269338bde180SChris Wilson 	u32 flip_mask =
269438bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
269538bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
269638bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2697a266c7d5SChris Wilson 
2698a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2699a266c7d5SChris Wilson 
2700a266c7d5SChris Wilson 	iir = I915_READ(IIR);
270138bde180SChris Wilson 	do {
270238bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
27038291ee90SChris Wilson 		bool blc_event = false;
2704a266c7d5SChris Wilson 
2705a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2706a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2707a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2708a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2709a266c7d5SChris Wilson 		 */
2710a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2711a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2712a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2713a266c7d5SChris Wilson 
2714a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2715a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2716a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2717a266c7d5SChris Wilson 
271838bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2719a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2720a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2721a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2722a266c7d5SChris Wilson 							 pipe_name(pipe));
2723a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
272438bde180SChris Wilson 				irq_received = true;
2725a266c7d5SChris Wilson 			}
2726a266c7d5SChris Wilson 		}
2727a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2728a266c7d5SChris Wilson 
2729a266c7d5SChris Wilson 		if (!irq_received)
2730a266c7d5SChris Wilson 			break;
2731a266c7d5SChris Wilson 
2732a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2733a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2734a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2735a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2736b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
2737a266c7d5SChris Wilson 
2738a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2739a266c7d5SChris Wilson 				  hotplug_status);
274091d131d2SDaniel Vetter 
274110a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
274291d131d2SDaniel Vetter 
2743a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
274438bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2745a266c7d5SChris Wilson 		}
2746a266c7d5SChris Wilson 
274738bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2748a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2749a266c7d5SChris Wilson 
2750a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2751a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2752a266c7d5SChris Wilson 
2753a266c7d5SChris Wilson 		for_each_pipe(pipe) {
275438bde180SChris Wilson 			int plane = pipe;
275538bde180SChris Wilson 			if (IS_MOBILE(dev))
275638bde180SChris Wilson 				plane = !plane;
27575e2032d4SVille Syrjälä 
275890a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
275990a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
276090a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
2761a266c7d5SChris Wilson 
2762a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2763a266c7d5SChris Wilson 				blc_event = true;
2764a266c7d5SChris Wilson 		}
2765a266c7d5SChris Wilson 
2766a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2767a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2768a266c7d5SChris Wilson 
2769a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2770a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2771a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2772a266c7d5SChris Wilson 		 * we would never get another interrupt.
2773a266c7d5SChris Wilson 		 *
2774a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2775a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2776a266c7d5SChris Wilson 		 * another one.
2777a266c7d5SChris Wilson 		 *
2778a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2779a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2780a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2781a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2782a266c7d5SChris Wilson 		 * stray interrupts.
2783a266c7d5SChris Wilson 		 */
278438bde180SChris Wilson 		ret = IRQ_HANDLED;
2785a266c7d5SChris Wilson 		iir = new_iir;
278638bde180SChris Wilson 	} while (iir & ~flip_mask);
2787a266c7d5SChris Wilson 
2788d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
27898291ee90SChris Wilson 
2790a266c7d5SChris Wilson 	return ret;
2791a266c7d5SChris Wilson }
2792a266c7d5SChris Wilson 
2793a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2794a266c7d5SChris Wilson {
2795a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2796a266c7d5SChris Wilson 	int pipe;
2797a266c7d5SChris Wilson 
2798ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2799ac4c16c5SEgbert Eich 
2800a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2801a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2802a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2803a266c7d5SChris Wilson 	}
2804a266c7d5SChris Wilson 
280500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
280655b39755SChris Wilson 	for_each_pipe(pipe) {
280755b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2808a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
280955b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
281055b39755SChris Wilson 	}
2811a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2812a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2813a266c7d5SChris Wilson 
2814a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2815a266c7d5SChris Wilson }
2816a266c7d5SChris Wilson 
2817a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2818a266c7d5SChris Wilson {
2819a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2820a266c7d5SChris Wilson 	int pipe;
2821a266c7d5SChris Wilson 
2822a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2823a266c7d5SChris Wilson 
2824a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2825a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2826a266c7d5SChris Wilson 
2827a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2828a266c7d5SChris Wilson 	for_each_pipe(pipe)
2829a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2830a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2831a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2832a266c7d5SChris Wilson 	POSTING_READ(IER);
2833a266c7d5SChris Wilson }
2834a266c7d5SChris Wilson 
2835a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2836a266c7d5SChris Wilson {
2837a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2838bbba0a97SChris Wilson 	u32 enable_mask;
2839a266c7d5SChris Wilson 	u32 error_mask;
2840b79480baSDaniel Vetter 	unsigned long irqflags;
2841a266c7d5SChris Wilson 
2842a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2843bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2844adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2845bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2846bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2847bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2848bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2849bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2850bbba0a97SChris Wilson 
2851bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
285221ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
285321ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
2854bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2855bbba0a97SChris Wilson 
2856bbba0a97SChris Wilson 	if (IS_G4X(dev))
2857bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2858a266c7d5SChris Wilson 
2859b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2860b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2861b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2862515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
2863b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2864a266c7d5SChris Wilson 
2865a266c7d5SChris Wilson 	/*
2866a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2867a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2868a266c7d5SChris Wilson 	 */
2869a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2870a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2871a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2872a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2873a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2874a266c7d5SChris Wilson 	} else {
2875a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2876a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2877a266c7d5SChris Wilson 	}
2878a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2879a266c7d5SChris Wilson 
2880a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2881a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2882a266c7d5SChris Wilson 	POSTING_READ(IER);
2883a266c7d5SChris Wilson 
288420afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
288520afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
288620afbda2SDaniel Vetter 
2887f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
288820afbda2SDaniel Vetter 
288920afbda2SDaniel Vetter 	return 0;
289020afbda2SDaniel Vetter }
289120afbda2SDaniel Vetter 
2892bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
289320afbda2SDaniel Vetter {
289420afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2895e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
2896cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
289720afbda2SDaniel Vetter 	u32 hotplug_en;
289820afbda2SDaniel Vetter 
2899b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2900b5ea2d56SDaniel Vetter 
2901bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
2902bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2903bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
2904adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
2905e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
2906cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2907cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2908cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
2909a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2910a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2911a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2912a266c7d5SChris Wilson 		*/
2913a266c7d5SChris Wilson 		if (IS_G4X(dev))
2914a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
291585fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
2916a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2917a266c7d5SChris Wilson 
2918a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2919a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2920a266c7d5SChris Wilson 	}
2921bac56d5bSEgbert Eich }
2922a266c7d5SChris Wilson 
2923ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
2924a266c7d5SChris Wilson {
2925a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2926a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2927a266c7d5SChris Wilson 	u32 iir, new_iir;
2928a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2929a266c7d5SChris Wilson 	unsigned long irqflags;
2930a266c7d5SChris Wilson 	int irq_received;
2931a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
293221ad8330SVille Syrjälä 	u32 flip_mask =
293321ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
293421ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2935a266c7d5SChris Wilson 
2936a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2937a266c7d5SChris Wilson 
2938a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2939a266c7d5SChris Wilson 
2940a266c7d5SChris Wilson 	for (;;) {
29412c8ba29fSChris Wilson 		bool blc_event = false;
29422c8ba29fSChris Wilson 
294321ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
2944a266c7d5SChris Wilson 
2945a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2946a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2947a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2948a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2949a266c7d5SChris Wilson 		 */
2950a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2951a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2952a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2953a266c7d5SChris Wilson 
2954a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2955a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2956a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2957a266c7d5SChris Wilson 
2958a266c7d5SChris Wilson 			/*
2959a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2960a266c7d5SChris Wilson 			 */
2961a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2962a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2963a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2964a266c7d5SChris Wilson 							 pipe_name(pipe));
2965a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2966a266c7d5SChris Wilson 				irq_received = 1;
2967a266c7d5SChris Wilson 			}
2968a266c7d5SChris Wilson 		}
2969a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2970a266c7d5SChris Wilson 
2971a266c7d5SChris Wilson 		if (!irq_received)
2972a266c7d5SChris Wilson 			break;
2973a266c7d5SChris Wilson 
2974a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2975a266c7d5SChris Wilson 
2976a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2977adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2978a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2979b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
2980b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
29814f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
2982a266c7d5SChris Wilson 
2983a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2984a266c7d5SChris Wilson 				  hotplug_status);
298591d131d2SDaniel Vetter 
298610a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
298710a504deSDaniel Vetter 					      IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
298891d131d2SDaniel Vetter 
2989a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2990a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2991a266c7d5SChris Wilson 		}
2992a266c7d5SChris Wilson 
299321ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
2994a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2995a266c7d5SChris Wilson 
2996a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2997a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2998a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2999a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3000a266c7d5SChris Wilson 
3001a266c7d5SChris Wilson 		for_each_pipe(pipe) {
30022c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
300390a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
300490a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3005a266c7d5SChris Wilson 
3006a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3007a266c7d5SChris Wilson 				blc_event = true;
3008a266c7d5SChris Wilson 		}
3009a266c7d5SChris Wilson 
3010a266c7d5SChris Wilson 
3011a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3012a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3013a266c7d5SChris Wilson 
3014515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3015515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3016515ac2bbSDaniel Vetter 
3017a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3018a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3019a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3020a266c7d5SChris Wilson 		 * we would never get another interrupt.
3021a266c7d5SChris Wilson 		 *
3022a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3023a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3024a266c7d5SChris Wilson 		 * another one.
3025a266c7d5SChris Wilson 		 *
3026a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3027a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3028a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3029a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3030a266c7d5SChris Wilson 		 * stray interrupts.
3031a266c7d5SChris Wilson 		 */
3032a266c7d5SChris Wilson 		iir = new_iir;
3033a266c7d5SChris Wilson 	}
3034a266c7d5SChris Wilson 
3035d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
30362c8ba29fSChris Wilson 
3037a266c7d5SChris Wilson 	return ret;
3038a266c7d5SChris Wilson }
3039a266c7d5SChris Wilson 
3040a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3041a266c7d5SChris Wilson {
3042a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3043a266c7d5SChris Wilson 	int pipe;
3044a266c7d5SChris Wilson 
3045a266c7d5SChris Wilson 	if (!dev_priv)
3046a266c7d5SChris Wilson 		return;
3047a266c7d5SChris Wilson 
3048ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3049ac4c16c5SEgbert Eich 
3050a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3051a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3052a266c7d5SChris Wilson 
3053a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3054a266c7d5SChris Wilson 	for_each_pipe(pipe)
3055a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3056a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3057a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3058a266c7d5SChris Wilson 
3059a266c7d5SChris Wilson 	for_each_pipe(pipe)
3060a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3061a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3062a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3063a266c7d5SChris Wilson }
3064a266c7d5SChris Wilson 
3065ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3066ac4c16c5SEgbert Eich {
3067ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3068ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3069ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3070ac4c16c5SEgbert Eich 	unsigned long irqflags;
3071ac4c16c5SEgbert Eich 	int i;
3072ac4c16c5SEgbert Eich 
3073ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3074ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3075ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3076ac4c16c5SEgbert Eich 
3077ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3078ac4c16c5SEgbert Eich 			continue;
3079ac4c16c5SEgbert Eich 
3080ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3081ac4c16c5SEgbert Eich 
3082ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3083ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3084ac4c16c5SEgbert Eich 
3085ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3086ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3087ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3088ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3089ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3090ac4c16c5SEgbert Eich 				if (!connector->polled)
3091ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3092ac4c16c5SEgbert Eich 			}
3093ac4c16c5SEgbert Eich 		}
3094ac4c16c5SEgbert Eich 	}
3095ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3096ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3097ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3098ac4c16c5SEgbert Eich }
3099ac4c16c5SEgbert Eich 
3100f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3101f71d4af4SJesse Barnes {
31028b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
31038b2e326dSChris Wilson 
31048b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
310599584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3106c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3107a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
31088b2e326dSChris Wilson 
310999584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
311099584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
311161bac78eSDaniel Vetter 		    (unsigned long) dev);
3112ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3113ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
311461bac78eSDaniel Vetter 
311597a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
31169ee32feaSDaniel Vetter 
3117f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
3118f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
31197d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3120f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3121f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3122f71d4af4SJesse Barnes 	}
3123f71d4af4SJesse Barnes 
3124c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3125f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3126c3613de9SKeith Packard 	else
3127c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
3128f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3129f71d4af4SJesse Barnes 
31307e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
31317e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
31327e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
31337e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
31347e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
31357e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
31367e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3137fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3138f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3139f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3140f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3141f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3142f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3143f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3144f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
314582a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3146f71d4af4SJesse Barnes 	} else {
3147c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3148c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3149c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3150c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3151c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3152a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3153a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3154a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3155a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3156a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
315720afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3158c2798b19SChris Wilson 		} else {
3159a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3160a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3161a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3162a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3163bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3164c2798b19SChris Wilson 		}
3165f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3166f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3167f71d4af4SJesse Barnes 	}
3168f71d4af4SJesse Barnes }
316920afbda2SDaniel Vetter 
317020afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
317120afbda2SDaniel Vetter {
317220afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3173821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3174821450c6SEgbert Eich 	struct drm_connector *connector;
3175b5ea2d56SDaniel Vetter 	unsigned long irqflags;
3176821450c6SEgbert Eich 	int i;
317720afbda2SDaniel Vetter 
3178821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3179821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3180821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3181821450c6SEgbert Eich 	}
3182821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3183821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3184821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3185821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3186821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3187821450c6SEgbert Eich 	}
3188b5ea2d56SDaniel Vetter 
3189b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3190b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
3191b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
319220afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
319320afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
3194b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
319520afbda2SDaniel Vetter }
3196c67a470bSPaulo Zanoni 
3197c67a470bSPaulo Zanoni /* Disable interrupts so we can allow Package C8+. */
3198c67a470bSPaulo Zanoni void hsw_pc8_disable_interrupts(struct drm_device *dev)
3199c67a470bSPaulo Zanoni {
3200c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3201c67a470bSPaulo Zanoni 	unsigned long irqflags;
3202c67a470bSPaulo Zanoni 
3203c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3204c67a470bSPaulo Zanoni 
3205c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.deimr = I915_READ(DEIMR);
3206c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.sdeimr = I915_READ(SDEIMR);
3207c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtimr = I915_READ(GTIMR);
3208c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtier = I915_READ(GTIER);
3209c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
3210c67a470bSPaulo Zanoni 
3211c67a470bSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, ~DE_PCH_EVENT_IVB);
3212c67a470bSPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, ~SDE_HOTPLUG_MASK_CPT);
3213c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
3214c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
3215c67a470bSPaulo Zanoni 
3216c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = true;
3217c67a470bSPaulo Zanoni 
3218c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3219c67a470bSPaulo Zanoni }
3220c67a470bSPaulo Zanoni 
3221c67a470bSPaulo Zanoni /* Restore interrupts so we can recover from Package C8+. */
3222c67a470bSPaulo Zanoni void hsw_pc8_restore_interrupts(struct drm_device *dev)
3223c67a470bSPaulo Zanoni {
3224c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3225c67a470bSPaulo Zanoni 	unsigned long irqflags;
3226c67a470bSPaulo Zanoni 	uint32_t val, expected;
3227c67a470bSPaulo Zanoni 
3228c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3229c67a470bSPaulo Zanoni 
3230c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
3231c67a470bSPaulo Zanoni 	expected = ~DE_PCH_EVENT_IVB;
3232c67a470bSPaulo Zanoni 	WARN(val != expected, "DEIMR is 0x%08x, not 0x%08x\n", val, expected);
3233c67a470bSPaulo Zanoni 
3234c67a470bSPaulo Zanoni 	val = I915_READ(SDEIMR) & ~SDE_HOTPLUG_MASK_CPT;
3235c67a470bSPaulo Zanoni 	expected = ~SDE_HOTPLUG_MASK_CPT;
3236c67a470bSPaulo Zanoni 	WARN(val != expected, "SDEIMR non-HPD bits are 0x%08x, not 0x%08x\n",
3237c67a470bSPaulo Zanoni 	     val, expected);
3238c67a470bSPaulo Zanoni 
3239c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
3240c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3241c67a470bSPaulo Zanoni 	WARN(val != expected, "GTIMR is 0x%08x, not 0x%08x\n", val, expected);
3242c67a470bSPaulo Zanoni 
3243c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
3244c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3245c67a470bSPaulo Zanoni 	WARN(val != expected, "GEN6_PMIMR is 0x%08x, not 0x%08x\n", val,
3246c67a470bSPaulo Zanoni 	     expected);
3247c67a470bSPaulo Zanoni 
3248c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = false;
3249c67a470bSPaulo Zanoni 
3250c67a470bSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pc8.regsave.deimr);
3251c67a470bSPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv,
3252c67a470bSPaulo Zanoni 				     ~dev_priv->pc8.regsave.sdeimr &
3253c67a470bSPaulo Zanoni 				     ~SDE_HOTPLUG_MASK_CPT);
3254c67a470bSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pc8.regsave.gtimr);
3255c67a470bSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pc8.regsave.gen6_pmimr);
3256c67a470bSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pc8.regsave.gtier);
3257c67a470bSPaulo Zanoni 
3258c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3259c67a470bSPaulo Zanoni }
3260