xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision ac2300d4d5c2e9e4d82361a582cf1df3ec22fec7)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33b2c88f5bSDamien Lespiau #include <linux/circ_buf.h>
34760285e7SDavid Howells #include <drm/drmP.h>
35760285e7SDavid Howells #include <drm/i915_drm.h>
36c0e09200SDave Airlie #include "i915_drv.h"
371c5d22f7SChris Wilson #include "i915_trace.h"
3879e53945SJesse Barnes #include "intel_drv.h"
39c0e09200SDave Airlie 
40e5868a31SEgbert Eich static const u32 hpd_ibx[] = {
41e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
42e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
43e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
44e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
45e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
46e5868a31SEgbert Eich };
47e5868a31SEgbert Eich 
48e5868a31SEgbert Eich static const u32 hpd_cpt[] = {
49e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
5073c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
51e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
52e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
53e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
54e5868a31SEgbert Eich };
55e5868a31SEgbert Eich 
56e5868a31SEgbert Eich static const u32 hpd_mask_i915[] = {
57e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
58e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
59e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
60e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
61e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
62e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
63e5868a31SEgbert Eich };
64e5868a31SEgbert Eich 
65e5868a31SEgbert Eich static const u32 hpd_status_gen4[] = {
66e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
67e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
68e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
69e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
70e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
71e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
72e5868a31SEgbert Eich };
73e5868a31SEgbert Eich 
74e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
75e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
76e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
77e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
78e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
79e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
80e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
81e5868a31SEgbert Eich };
82e5868a31SEgbert Eich 
83036a4a7dSZhenyu Wang /* For display hotplug interrupt */
84995b6762SChris Wilson static void
85f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
86036a4a7dSZhenyu Wang {
874bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
884bc9d430SDaniel Vetter 
89c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
90c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
91c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.deimr &= ~mask;
92c67a470bSPaulo Zanoni 		return;
93c67a470bSPaulo Zanoni 	}
94c67a470bSPaulo Zanoni 
951ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
961ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
971ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
983143a2bfSChris Wilson 		POSTING_READ(DEIMR);
99036a4a7dSZhenyu Wang 	}
100036a4a7dSZhenyu Wang }
101036a4a7dSZhenyu Wang 
1020ff9800aSPaulo Zanoni static void
103f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
104036a4a7dSZhenyu Wang {
1054bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1064bc9d430SDaniel Vetter 
107c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
108c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
109c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.deimr |= mask;
110c67a470bSPaulo Zanoni 		return;
111c67a470bSPaulo Zanoni 	}
112c67a470bSPaulo Zanoni 
1131ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1141ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1151ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1163143a2bfSChris Wilson 		POSTING_READ(DEIMR);
117036a4a7dSZhenyu Wang 	}
118036a4a7dSZhenyu Wang }
119036a4a7dSZhenyu Wang 
12043eaea13SPaulo Zanoni /**
12143eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
12243eaea13SPaulo Zanoni  * @dev_priv: driver private
12343eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
12443eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
12543eaea13SPaulo Zanoni  */
12643eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
12743eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
12843eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
12943eaea13SPaulo Zanoni {
13043eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
13143eaea13SPaulo Zanoni 
132c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
133c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
134c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gtimr &= ~interrupt_mask;
135c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gtimr |= (~enabled_irq_mask &
136c67a470bSPaulo Zanoni 						interrupt_mask);
137c67a470bSPaulo Zanoni 		return;
138c67a470bSPaulo Zanoni 	}
139c67a470bSPaulo Zanoni 
14043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
14143eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
14243eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
14343eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
14443eaea13SPaulo Zanoni }
14543eaea13SPaulo Zanoni 
14643eaea13SPaulo Zanoni void ilk_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
14743eaea13SPaulo Zanoni {
14843eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
14943eaea13SPaulo Zanoni }
15043eaea13SPaulo Zanoni 
15143eaea13SPaulo Zanoni void ilk_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
15243eaea13SPaulo Zanoni {
15343eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
15443eaea13SPaulo Zanoni }
15543eaea13SPaulo Zanoni 
156edbfdb45SPaulo Zanoni /**
157edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
158edbfdb45SPaulo Zanoni   * @dev_priv: driver private
159edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
160edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
161edbfdb45SPaulo Zanoni   */
162edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
163edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
164edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
165edbfdb45SPaulo Zanoni {
166605cd25bSPaulo Zanoni 	uint32_t new_val;
167edbfdb45SPaulo Zanoni 
168edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
169edbfdb45SPaulo Zanoni 
170c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
171c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
172c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gen6_pmimr &= ~interrupt_mask;
173c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gen6_pmimr |= (~enabled_irq_mask &
174c67a470bSPaulo Zanoni 						     interrupt_mask);
175c67a470bSPaulo Zanoni 		return;
176c67a470bSPaulo Zanoni 	}
177c67a470bSPaulo Zanoni 
178605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
179f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
180f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
181f52ecbcfSPaulo Zanoni 
182605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
183605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
184605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
185edbfdb45SPaulo Zanoni 		POSTING_READ(GEN6_PMIMR);
186edbfdb45SPaulo Zanoni 	}
187f52ecbcfSPaulo Zanoni }
188edbfdb45SPaulo Zanoni 
189edbfdb45SPaulo Zanoni void snb_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
190edbfdb45SPaulo Zanoni {
191edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
192edbfdb45SPaulo Zanoni }
193edbfdb45SPaulo Zanoni 
194edbfdb45SPaulo Zanoni void snb_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
195edbfdb45SPaulo Zanoni {
196edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, 0);
197edbfdb45SPaulo Zanoni }
198edbfdb45SPaulo Zanoni 
1998664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
2008664281bSPaulo Zanoni {
2018664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2028664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2038664281bSPaulo Zanoni 	enum pipe pipe;
2048664281bSPaulo Zanoni 
2054bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2064bc9d430SDaniel Vetter 
2078664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2088664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2098664281bSPaulo Zanoni 
2108664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
2118664281bSPaulo Zanoni 			return false;
2128664281bSPaulo Zanoni 	}
2138664281bSPaulo Zanoni 
2148664281bSPaulo Zanoni 	return true;
2158664281bSPaulo Zanoni }
2168664281bSPaulo Zanoni 
2178664281bSPaulo Zanoni static bool cpt_can_enable_serr_int(struct drm_device *dev)
2188664281bSPaulo Zanoni {
2198664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2208664281bSPaulo Zanoni 	enum pipe pipe;
2218664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2228664281bSPaulo Zanoni 
223fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
224fee884edSDaniel Vetter 
2258664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2268664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2278664281bSPaulo Zanoni 
2288664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
2298664281bSPaulo Zanoni 			return false;
2308664281bSPaulo Zanoni 	}
2318664281bSPaulo Zanoni 
2328664281bSPaulo Zanoni 	return true;
2338664281bSPaulo Zanoni }
2348664281bSPaulo Zanoni 
2358664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
2368664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
2378664281bSPaulo Zanoni {
2388664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2398664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
2408664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
2418664281bSPaulo Zanoni 
2428664281bSPaulo Zanoni 	if (enable)
2438664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
2448664281bSPaulo Zanoni 	else
2458664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
2468664281bSPaulo Zanoni }
2478664281bSPaulo Zanoni 
2488664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
2497336df65SDaniel Vetter 						  enum pipe pipe, bool enable)
2508664281bSPaulo Zanoni {
2518664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2528664281bSPaulo Zanoni 	if (enable) {
2537336df65SDaniel Vetter 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
2547336df65SDaniel Vetter 
2558664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
2568664281bSPaulo Zanoni 			return;
2578664281bSPaulo Zanoni 
2588664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
2598664281bSPaulo Zanoni 	} else {
2607336df65SDaniel Vetter 		bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
2617336df65SDaniel Vetter 
2627336df65SDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
2638664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
2647336df65SDaniel Vetter 
2657336df65SDaniel Vetter 		if (!was_enabled &&
2667336df65SDaniel Vetter 		    (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
2677336df65SDaniel Vetter 			DRM_DEBUG_KMS("uncleared fifo underrun on pipe %c\n",
2687336df65SDaniel Vetter 				      pipe_name(pipe));
2697336df65SDaniel Vetter 		}
2708664281bSPaulo Zanoni 	}
2718664281bSPaulo Zanoni }
2728664281bSPaulo Zanoni 
273fee884edSDaniel Vetter /**
274fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
275fee884edSDaniel Vetter  * @dev_priv: driver private
276fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
277fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
278fee884edSDaniel Vetter  */
279fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
280fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
281fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
282fee884edSDaniel Vetter {
283fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
284fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
285fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
286fee884edSDaniel Vetter 
287fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
288fee884edSDaniel Vetter 
289c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled &&
290c67a470bSPaulo Zanoni 	    (interrupt_mask & SDE_HOTPLUG_MASK_CPT)) {
291c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
292c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr &= ~interrupt_mask;
293c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr |= (~enabled_irq_mask &
294c67a470bSPaulo Zanoni 						 interrupt_mask);
295c67a470bSPaulo Zanoni 		return;
296c67a470bSPaulo Zanoni 	}
297c67a470bSPaulo Zanoni 
298fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
299fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
300fee884edSDaniel Vetter }
301fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
302fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
303fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
304fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
305fee884edSDaniel Vetter 
306de28075dSDaniel Vetter static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
307de28075dSDaniel Vetter 					    enum transcoder pch_transcoder,
3088664281bSPaulo Zanoni 					    bool enable)
3098664281bSPaulo Zanoni {
3108664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
311de28075dSDaniel Vetter 	uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
312de28075dSDaniel Vetter 		       SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
3138664281bSPaulo Zanoni 
3148664281bSPaulo Zanoni 	if (enable)
315fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
3168664281bSPaulo Zanoni 	else
317fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
3188664281bSPaulo Zanoni }
3198664281bSPaulo Zanoni 
3208664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
3218664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
3228664281bSPaulo Zanoni 					    bool enable)
3238664281bSPaulo Zanoni {
3248664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3258664281bSPaulo Zanoni 
3268664281bSPaulo Zanoni 	if (enable) {
3271dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
3281dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
3291dd246fbSDaniel Vetter 
3308664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
3318664281bSPaulo Zanoni 			return;
3328664281bSPaulo Zanoni 
333fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3348664281bSPaulo Zanoni 	} else {
3351dd246fbSDaniel Vetter 		uint32_t tmp = I915_READ(SERR_INT);
3361dd246fbSDaniel Vetter 		bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
3371dd246fbSDaniel Vetter 
3381dd246fbSDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
339fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3401dd246fbSDaniel Vetter 
3411dd246fbSDaniel Vetter 		if (!was_enabled &&
3421dd246fbSDaniel Vetter 		    (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
3431dd246fbSDaniel Vetter 			DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
3441dd246fbSDaniel Vetter 				      transcoder_name(pch_transcoder));
3451dd246fbSDaniel Vetter 		}
3468664281bSPaulo Zanoni 	}
3478664281bSPaulo Zanoni }
3488664281bSPaulo Zanoni 
3498664281bSPaulo Zanoni /**
3508664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
3518664281bSPaulo Zanoni  * @dev: drm device
3528664281bSPaulo Zanoni  * @pipe: pipe
3538664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
3548664281bSPaulo Zanoni  *
3558664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
3568664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
3578664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
3588664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
3598664281bSPaulo Zanoni  * bit for all the pipes.
3608664281bSPaulo Zanoni  *
3618664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
3628664281bSPaulo Zanoni  */
3638664281bSPaulo Zanoni bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
3648664281bSPaulo Zanoni 					   enum pipe pipe, bool enable)
3658664281bSPaulo Zanoni {
3668664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3678664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
3688664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3698664281bSPaulo Zanoni 	unsigned long flags;
3708664281bSPaulo Zanoni 	bool ret;
3718664281bSPaulo Zanoni 
3728664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
3738664281bSPaulo Zanoni 
3748664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
3758664281bSPaulo Zanoni 
3768664281bSPaulo Zanoni 	if (enable == ret)
3778664281bSPaulo Zanoni 		goto done;
3788664281bSPaulo Zanoni 
3798664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
3808664281bSPaulo Zanoni 
3818664281bSPaulo Zanoni 	if (IS_GEN5(dev) || IS_GEN6(dev))
3828664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
3838664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
3847336df65SDaniel Vetter 		ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
3858664281bSPaulo Zanoni 
3868664281bSPaulo Zanoni done:
3878664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
3888664281bSPaulo Zanoni 	return ret;
3898664281bSPaulo Zanoni }
3908664281bSPaulo Zanoni 
3918664281bSPaulo Zanoni /**
3928664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
3938664281bSPaulo Zanoni  * @dev: drm device
3948664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
3958664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
3968664281bSPaulo Zanoni  *
3978664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
3988664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
3998664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
4008664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
4018664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
4028664281bSPaulo Zanoni  *
4038664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
4048664281bSPaulo Zanoni  */
4058664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
4068664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
4078664281bSPaulo Zanoni 					   bool enable)
4088664281bSPaulo Zanoni {
4098664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
410de28075dSDaniel Vetter 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
411de28075dSDaniel Vetter 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4128664281bSPaulo Zanoni 	unsigned long flags;
4138664281bSPaulo Zanoni 	bool ret;
4148664281bSPaulo Zanoni 
415de28075dSDaniel Vetter 	/*
416de28075dSDaniel Vetter 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
417de28075dSDaniel Vetter 	 * has only one pch transcoder A that all pipes can use. To avoid racy
418de28075dSDaniel Vetter 	 * pch transcoder -> pipe lookups from interrupt code simply store the
419de28075dSDaniel Vetter 	 * underrun statistics in crtc A. Since we never expose this anywhere
420de28075dSDaniel Vetter 	 * nor use it outside of the fifo underrun code here using the "wrong"
421de28075dSDaniel Vetter 	 * crtc on LPT won't cause issues.
422de28075dSDaniel Vetter 	 */
4238664281bSPaulo Zanoni 
4248664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
4258664281bSPaulo Zanoni 
4268664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
4278664281bSPaulo Zanoni 
4288664281bSPaulo Zanoni 	if (enable == ret)
4298664281bSPaulo Zanoni 		goto done;
4308664281bSPaulo Zanoni 
4318664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
4328664281bSPaulo Zanoni 
4338664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
434de28075dSDaniel Vetter 		ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4358664281bSPaulo Zanoni 	else
4368664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4378664281bSPaulo Zanoni 
4388664281bSPaulo Zanoni done:
4398664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
4408664281bSPaulo Zanoni 	return ret;
4418664281bSPaulo Zanoni }
4428664281bSPaulo Zanoni 
4438664281bSPaulo Zanoni 
4447c463586SKeith Packard void
4457c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
4467c463586SKeith Packard {
4479db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
44846c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
4497c463586SKeith Packard 
450b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
451b79480baSDaniel Vetter 
45246c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
45346c06a30SVille Syrjälä 		return;
45446c06a30SVille Syrjälä 
4557c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
45646c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
45746c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4583143a2bfSChris Wilson 	POSTING_READ(reg);
4597c463586SKeith Packard }
4607c463586SKeith Packard 
4617c463586SKeith Packard void
4627c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
4637c463586SKeith Packard {
4649db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
46546c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
4667c463586SKeith Packard 
467b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
468b79480baSDaniel Vetter 
46946c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
47046c06a30SVille Syrjälä 		return;
47146c06a30SVille Syrjälä 
47246c06a30SVille Syrjälä 	pipestat &= ~mask;
47346c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4743143a2bfSChris Wilson 	POSTING_READ(reg);
4757c463586SKeith Packard }
4767c463586SKeith Packard 
477c0e09200SDave Airlie /**
478f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
47901c66889SZhao Yakui  */
480f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
48101c66889SZhao Yakui {
4821ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
4831ec14ad3SChris Wilson 	unsigned long irqflags;
4841ec14ad3SChris Wilson 
485f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
486f49e38ddSJani Nikula 		return;
487f49e38ddSJani Nikula 
4881ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
48901c66889SZhao Yakui 
490f898780bSJani Nikula 	i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
491a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
492f898780bSJani Nikula 		i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
4931ec14ad3SChris Wilson 
4941ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
49501c66889SZhao Yakui }
49601c66889SZhao Yakui 
49701c66889SZhao Yakui /**
4980a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
4990a3e67a4SJesse Barnes  * @dev: DRM device
5000a3e67a4SJesse Barnes  * @pipe: pipe to check
5010a3e67a4SJesse Barnes  *
5020a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
5030a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
5040a3e67a4SJesse Barnes  * before reading such registers if unsure.
5050a3e67a4SJesse Barnes  */
5060a3e67a4SJesse Barnes static int
5070a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
5080a3e67a4SJesse Barnes {
5090a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
510702e7a56SPaulo Zanoni 
511a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
512a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
513a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
514a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51571f8ba6bSPaulo Zanoni 
516a01025afSDaniel Vetter 		return intel_crtc->active;
517a01025afSDaniel Vetter 	} else {
518a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
519a01025afSDaniel Vetter 	}
5200a3e67a4SJesse Barnes }
5210a3e67a4SJesse Barnes 
5224cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
5234cdb83ecSVille Syrjälä {
5244cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
5254cdb83ecSVille Syrjälä 	return 0;
5264cdb83ecSVille Syrjälä }
5274cdb83ecSVille Syrjälä 
52842f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
52942f52ef8SKeith Packard  * we use as a pipe index
53042f52ef8SKeith Packard  */
531f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5320a3e67a4SJesse Barnes {
5330a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5340a3e67a4SJesse Barnes 	unsigned long high_frame;
5350a3e67a4SJesse Barnes 	unsigned long low_frame;
536391f75e2SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start;
5370a3e67a4SJesse Barnes 
5380a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
53944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5409db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
5410a3e67a4SJesse Barnes 		return 0;
5420a3e67a4SJesse Barnes 	}
5430a3e67a4SJesse Barnes 
544391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
545391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
546391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
547391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
548391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
549391f75e2SVille Syrjälä 
550391f75e2SVille Syrjälä 		vbl_start = mode->crtc_vblank_start * mode->crtc_htotal;
551391f75e2SVille Syrjälä 	} else {
552391f75e2SVille Syrjälä 		enum transcoder cpu_transcoder =
553391f75e2SVille Syrjälä 			intel_pipe_to_cpu_transcoder(dev_priv, pipe);
554391f75e2SVille Syrjälä 		u32 htotal;
555391f75e2SVille Syrjälä 
556391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
557391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
558391f75e2SVille Syrjälä 
559391f75e2SVille Syrjälä 		vbl_start *= htotal;
560391f75e2SVille Syrjälä 	}
561391f75e2SVille Syrjälä 
5629db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5639db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5645eddb70bSChris Wilson 
5650a3e67a4SJesse Barnes 	/*
5660a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5670a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
5680a3e67a4SJesse Barnes 	 * register.
5690a3e67a4SJesse Barnes 	 */
5700a3e67a4SJesse Barnes 	do {
5715eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
572391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
5735eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
5740a3e67a4SJesse Barnes 	} while (high1 != high2);
5750a3e67a4SJesse Barnes 
5765eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
577391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
5785eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
579391f75e2SVille Syrjälä 
580391f75e2SVille Syrjälä 	/*
581391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
582391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
583391f75e2SVille Syrjälä 	 * counter against vblank start.
584391f75e2SVille Syrjälä 	 */
585391f75e2SVille Syrjälä 	return ((high1 << 8) | low) + (pixel >= vbl_start);
5860a3e67a4SJesse Barnes }
5870a3e67a4SJesse Barnes 
588f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
5899880b7a5SJesse Barnes {
5909880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5919db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
5929880b7a5SJesse Barnes 
5939880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
59444d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5959db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
5969880b7a5SJesse Barnes 		return 0;
5979880b7a5SJesse Barnes 	}
5989880b7a5SJesse Barnes 
5999880b7a5SJesse Barnes 	return I915_READ(reg);
6009880b7a5SJesse Barnes }
6019880b7a5SJesse Barnes 
6027c06b08aSVille Syrjälä static bool intel_pipe_in_vblank(struct drm_device *dev, enum pipe pipe)
60354ddcbd2SVille Syrjälä {
60454ddcbd2SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
60554ddcbd2SVille Syrjälä 	uint32_t status;
60654ddcbd2SVille Syrjälä 
60754ddcbd2SVille Syrjälä 	if (IS_VALLEYVIEW(dev)) {
60854ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
60954ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
61054ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
61154ddcbd2SVille Syrjälä 
61254ddcbd2SVille Syrjälä 		return I915_READ(VLV_ISR) & status;
6137c06b08aSVille Syrjälä 	} else if (IS_GEN2(dev)) {
6147c06b08aSVille Syrjälä 		status = pipe == PIPE_A ?
6157c06b08aSVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
6167c06b08aSVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
6177c06b08aSVille Syrjälä 
6187c06b08aSVille Syrjälä 		return I915_READ16(ISR) & status;
6197c06b08aSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen < 5) {
62054ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
62154ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
62254ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
62354ddcbd2SVille Syrjälä 
62454ddcbd2SVille Syrjälä 		return I915_READ(ISR) & status;
62554ddcbd2SVille Syrjälä 	} else if (INTEL_INFO(dev)->gen < 7) {
62654ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
62754ddcbd2SVille Syrjälä 			DE_PIPEA_VBLANK :
62854ddcbd2SVille Syrjälä 			DE_PIPEB_VBLANK;
62954ddcbd2SVille Syrjälä 
63054ddcbd2SVille Syrjälä 		return I915_READ(DEISR) & status;
63154ddcbd2SVille Syrjälä 	} else {
63254ddcbd2SVille Syrjälä 		switch (pipe) {
63354ddcbd2SVille Syrjälä 		default:
63454ddcbd2SVille Syrjälä 		case PIPE_A:
63554ddcbd2SVille Syrjälä 			status = DE_PIPEA_VBLANK_IVB;
63654ddcbd2SVille Syrjälä 			break;
63754ddcbd2SVille Syrjälä 		case PIPE_B:
63854ddcbd2SVille Syrjälä 			status = DE_PIPEB_VBLANK_IVB;
63954ddcbd2SVille Syrjälä 			break;
64054ddcbd2SVille Syrjälä 		case PIPE_C:
64154ddcbd2SVille Syrjälä 			status = DE_PIPEC_VBLANK_IVB;
64254ddcbd2SVille Syrjälä 			break;
64354ddcbd2SVille Syrjälä 		}
64454ddcbd2SVille Syrjälä 
64554ddcbd2SVille Syrjälä 		return I915_READ(DEISR) & status;
64654ddcbd2SVille Syrjälä 	}
64754ddcbd2SVille Syrjälä }
64854ddcbd2SVille Syrjälä 
649f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
6500af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
6510af7e4dfSMario Kleiner {
652c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
653c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
654c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
655c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
6563aa18df8SVille Syrjälä 	int position;
6570af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
6580af7e4dfSMario Kleiner 	bool in_vbl = true;
6590af7e4dfSMario Kleiner 	int ret = 0;
6600af7e4dfSMario Kleiner 
661c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
6620af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
6639db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6640af7e4dfSMario Kleiner 		return 0;
6650af7e4dfSMario Kleiner 	}
6660af7e4dfSMario Kleiner 
667c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
668c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
669c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
670c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
6710af7e4dfSMario Kleiner 
672c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
673c2baf4b7SVille Syrjälä 
6747c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
6750af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
6760af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
6770af7e4dfSMario Kleiner 		 */
6787c06b08aSVille Syrjälä 		if (IS_GEN2(dev))
6797c06b08aSVille Syrjälä 			position = I915_READ(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
6807c06b08aSVille Syrjälä 		else
6817c06b08aSVille Syrjälä 			position = I915_READ(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
68254ddcbd2SVille Syrjälä 
68354ddcbd2SVille Syrjälä 		/*
68454ddcbd2SVille Syrjälä 		 * The scanline counter increments at the leading edge
68554ddcbd2SVille Syrjälä 		 * of hsync, ie. it completely misses the active portion
68654ddcbd2SVille Syrjälä 		 * of the line. Fix up the counter at both edges of vblank
68754ddcbd2SVille Syrjälä 		 * to get a more accurate picture whether we're in vblank
68854ddcbd2SVille Syrjälä 		 * or not.
68954ddcbd2SVille Syrjälä 		 */
6907c06b08aSVille Syrjälä 		in_vbl = intel_pipe_in_vblank(dev, pipe);
69154ddcbd2SVille Syrjälä 		if ((in_vbl && position == vbl_start - 1) ||
69254ddcbd2SVille Syrjälä 		    (!in_vbl && position == vbl_end - 1))
69354ddcbd2SVille Syrjälä 			position = (position + 1) % vtotal;
6940af7e4dfSMario Kleiner 	} else {
6950af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
6960af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
6970af7e4dfSMario Kleiner 		 * scanout position.
6980af7e4dfSMario Kleiner 		 */
6990af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
7000af7e4dfSMario Kleiner 
7013aa18df8SVille Syrjälä 		/* convert to pixel counts */
7023aa18df8SVille Syrjälä 		vbl_start *= htotal;
7033aa18df8SVille Syrjälä 		vbl_end *= htotal;
7043aa18df8SVille Syrjälä 		vtotal *= htotal;
7053aa18df8SVille Syrjälä 	}
7063aa18df8SVille Syrjälä 
7073aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
7083aa18df8SVille Syrjälä 
7093aa18df8SVille Syrjälä 	/*
7103aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
7113aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
7123aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
7133aa18df8SVille Syrjälä 	 * up since vbl_end.
7143aa18df8SVille Syrjälä 	 */
7153aa18df8SVille Syrjälä 	if (position >= vbl_start)
7163aa18df8SVille Syrjälä 		position -= vbl_end;
7173aa18df8SVille Syrjälä 	else
7183aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
7193aa18df8SVille Syrjälä 
7207c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7213aa18df8SVille Syrjälä 		*vpos = position;
7223aa18df8SVille Syrjälä 		*hpos = 0;
7233aa18df8SVille Syrjälä 	} else {
7240af7e4dfSMario Kleiner 		*vpos = position / htotal;
7250af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
7260af7e4dfSMario Kleiner 	}
7270af7e4dfSMario Kleiner 
7280af7e4dfSMario Kleiner 	/* In vblank? */
7290af7e4dfSMario Kleiner 	if (in_vbl)
7300af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
7310af7e4dfSMario Kleiner 
7320af7e4dfSMario Kleiner 	return ret;
7330af7e4dfSMario Kleiner }
7340af7e4dfSMario Kleiner 
735f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
7360af7e4dfSMario Kleiner 			      int *max_error,
7370af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
7380af7e4dfSMario Kleiner 			      unsigned flags)
7390af7e4dfSMario Kleiner {
7404041b853SChris Wilson 	struct drm_crtc *crtc;
7410af7e4dfSMario Kleiner 
7427eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
7434041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7440af7e4dfSMario Kleiner 		return -EINVAL;
7450af7e4dfSMario Kleiner 	}
7460af7e4dfSMario Kleiner 
7470af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
7484041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
7494041b853SChris Wilson 	if (crtc == NULL) {
7504041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7514041b853SChris Wilson 		return -EINVAL;
7524041b853SChris Wilson 	}
7534041b853SChris Wilson 
7544041b853SChris Wilson 	if (!crtc->enabled) {
7554041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
7564041b853SChris Wilson 		return -EBUSY;
7574041b853SChris Wilson 	}
7580af7e4dfSMario Kleiner 
7590af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
7604041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
7614041b853SChris Wilson 						     vblank_time, flags,
7624041b853SChris Wilson 						     crtc);
7630af7e4dfSMario Kleiner }
7640af7e4dfSMario Kleiner 
76567c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
76667c347ffSJani Nikula 				struct drm_connector *connector)
767321a1b30SEgbert Eich {
768321a1b30SEgbert Eich 	enum drm_connector_status old_status;
769321a1b30SEgbert Eich 
770321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
771321a1b30SEgbert Eich 	old_status = connector->status;
772321a1b30SEgbert Eich 
773321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
77467c347ffSJani Nikula 	if (old_status == connector->status)
77567c347ffSJani Nikula 		return false;
77667c347ffSJani Nikula 
77767c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
778321a1b30SEgbert Eich 		      connector->base.id,
779321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
78067c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
78167c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
78267c347ffSJani Nikula 
78367c347ffSJani Nikula 	return true;
784321a1b30SEgbert Eich }
785321a1b30SEgbert Eich 
7865ca58282SJesse Barnes /*
7875ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
7885ca58282SJesse Barnes  */
789ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
790ac4c16c5SEgbert Eich 
7915ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
7925ca58282SJesse Barnes {
7935ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
7945ca58282SJesse Barnes 						    hotplug_work);
7955ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
796c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
797cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
798cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
799cd569aedSEgbert Eich 	struct drm_connector *connector;
800cd569aedSEgbert Eich 	unsigned long irqflags;
801cd569aedSEgbert Eich 	bool hpd_disabled = false;
802321a1b30SEgbert Eich 	bool changed = false;
803142e2398SEgbert Eich 	u32 hpd_event_bits;
8045ca58282SJesse Barnes 
80552d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
80652d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
80752d7ecedSDaniel Vetter 		return;
80852d7ecedSDaniel Vetter 
809a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
810e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
811e67189abSJesse Barnes 
812cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
813142e2398SEgbert Eich 
814142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
815142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
816cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
817cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
818cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
819cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
820cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
821cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
822cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
823cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
824cd569aedSEgbert Eich 				drm_get_connector_name(connector));
825cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
826cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
827cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
828cd569aedSEgbert Eich 			hpd_disabled = true;
829cd569aedSEgbert Eich 		}
830142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
831142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
832142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
833142e2398SEgbert Eich 		}
834cd569aedSEgbert Eich 	}
835cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
836cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
837cd569aedSEgbert Eich 	  * some connectors */
838ac4c16c5SEgbert Eich 	if (hpd_disabled) {
839cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
840ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
841ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
842ac4c16c5SEgbert Eich 	}
843cd569aedSEgbert Eich 
844cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
845cd569aedSEgbert Eich 
846321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
847321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
848321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
849321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
850cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
851cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
852321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
853321a1b30SEgbert Eich 				changed = true;
854321a1b30SEgbert Eich 		}
855321a1b30SEgbert Eich 	}
85640ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
85740ee3381SKeith Packard 
858321a1b30SEgbert Eich 	if (changed)
859321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
8605ca58282SJesse Barnes }
8615ca58282SJesse Barnes 
862d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
863f97108d1SJesse Barnes {
864f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
865b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
8669270388eSDaniel Vetter 	u8 new_delay;
8679270388eSDaniel Vetter 
868d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
869f97108d1SJesse Barnes 
87073edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
87173edd18fSDaniel Vetter 
87220e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
8739270388eSDaniel Vetter 
8747648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
875b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
876b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
877f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
878f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
879f97108d1SJesse Barnes 
880f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
881b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
88220e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
88320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
88420e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
88520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
886b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
88720e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
88820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
88920e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
89020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
891f97108d1SJesse Barnes 	}
892f97108d1SJesse Barnes 
8937648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
89420e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
895f97108d1SJesse Barnes 
896d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
8979270388eSDaniel Vetter 
898f97108d1SJesse Barnes 	return;
899f97108d1SJesse Barnes }
900f97108d1SJesse Barnes 
901549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
902549f7365SChris Wilson 			struct intel_ring_buffer *ring)
903549f7365SChris Wilson {
904475553deSChris Wilson 	if (ring->obj == NULL)
905475553deSChris Wilson 		return;
906475553deSChris Wilson 
907814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
9089862e600SChris Wilson 
909549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
91010cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
911549f7365SChris Wilson }
912549f7365SChris Wilson 
9134912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
9143b8d8d91SJesse Barnes {
9154912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
916c6a828d3SDaniel Vetter 						    rps.work);
917edbfdb45SPaulo Zanoni 	u32 pm_iir;
918dd75fdc8SChris Wilson 	int new_delay, adj;
9193b8d8d91SJesse Barnes 
92059cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
921c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
922c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
9234848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
924edbfdb45SPaulo Zanoni 	snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
92559cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
9264912d041SBen Widawsky 
92760611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
92860611c13SPaulo Zanoni 	WARN_ON(pm_iir & ~GEN6_PM_RPS_EVENTS);
92960611c13SPaulo Zanoni 
9304848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
9313b8d8d91SJesse Barnes 		return;
9323b8d8d91SJesse Barnes 
9334fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
9347b9e0ae6SChris Wilson 
935dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
9367425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
937dd75fdc8SChris Wilson 		if (adj > 0)
938dd75fdc8SChris Wilson 			adj *= 2;
939dd75fdc8SChris Wilson 		else
940dd75fdc8SChris Wilson 			adj = 1;
941dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
9427425034aSVille Syrjälä 
9437425034aSVille Syrjälä 		/*
9447425034aSVille Syrjälä 		 * For better performance, jump directly
9457425034aSVille Syrjälä 		 * to RPe if we're below it.
9467425034aSVille Syrjälä 		 */
947dd75fdc8SChris Wilson 		if (new_delay < dev_priv->rps.rpe_delay)
9487425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
949dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
950dd75fdc8SChris Wilson 		if (dev_priv->rps.cur_delay > dev_priv->rps.rpe_delay)
951dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.rpe_delay;
952dd75fdc8SChris Wilson 		else
953dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.min_delay;
954dd75fdc8SChris Wilson 		adj = 0;
955dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
956dd75fdc8SChris Wilson 		if (adj < 0)
957dd75fdc8SChris Wilson 			adj *= 2;
958dd75fdc8SChris Wilson 		else
959dd75fdc8SChris Wilson 			adj = -1;
960dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
961dd75fdc8SChris Wilson 	} else { /* unknown event */
962dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay;
963dd75fdc8SChris Wilson 	}
9643b8d8d91SJesse Barnes 
96579249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
96679249636SBen Widawsky 	 * interrupt
96779249636SBen Widawsky 	 */
968dd75fdc8SChris Wilson 	if (new_delay < (int)dev_priv->rps.min_delay)
969dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.min_delay;
970dd75fdc8SChris Wilson 	if (new_delay > (int)dev_priv->rps.max_delay)
971dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.max_delay;
972dd75fdc8SChris Wilson 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_delay;
973dd75fdc8SChris Wilson 
9740a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
9750a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
9760a073b84SJesse Barnes 	else
9774912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
9783b8d8d91SJesse Barnes 
9794fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
9803b8d8d91SJesse Barnes }
9813b8d8d91SJesse Barnes 
982e3689190SBen Widawsky 
983e3689190SBen Widawsky /**
984e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
985e3689190SBen Widawsky  * occurred.
986e3689190SBen Widawsky  * @work: workqueue struct
987e3689190SBen Widawsky  *
988e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
989e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
990e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
991e3689190SBen Widawsky  */
992e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
993e3689190SBen Widawsky {
994e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
995a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
996e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
99735a85ac6SBen Widawsky 	char *parity_event[6];
998e3689190SBen Widawsky 	uint32_t misccpctl;
999e3689190SBen Widawsky 	unsigned long flags;
100035a85ac6SBen Widawsky 	uint8_t slice = 0;
1001e3689190SBen Widawsky 
1002e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1003e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1004e3689190SBen Widawsky 	 * any time we access those registers.
1005e3689190SBen Widawsky 	 */
1006e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1007e3689190SBen Widawsky 
100835a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
100935a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
101035a85ac6SBen Widawsky 		goto out;
101135a85ac6SBen Widawsky 
1012e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1013e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1014e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1015e3689190SBen Widawsky 
101635a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
101735a85ac6SBen Widawsky 		u32 reg;
101835a85ac6SBen Widawsky 
101935a85ac6SBen Widawsky 		slice--;
102035a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
102135a85ac6SBen Widawsky 			break;
102235a85ac6SBen Widawsky 
102335a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
102435a85ac6SBen Widawsky 
102535a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
102635a85ac6SBen Widawsky 
102735a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1028e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1029e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1030e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1031e3689190SBen Widawsky 
103235a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
103335a85ac6SBen Widawsky 		POSTING_READ(reg);
1034e3689190SBen Widawsky 
1035cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1036e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1037e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1038e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
103935a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
104035a85ac6SBen Widawsky 		parity_event[5] = NULL;
1041e3689190SBen Widawsky 
1042e3689190SBen Widawsky 		kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
1043e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1044e3689190SBen Widawsky 
104535a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
104635a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1047e3689190SBen Widawsky 
104835a85ac6SBen Widawsky 		kfree(parity_event[4]);
1049e3689190SBen Widawsky 		kfree(parity_event[3]);
1050e3689190SBen Widawsky 		kfree(parity_event[2]);
1051e3689190SBen Widawsky 		kfree(parity_event[1]);
1052e3689190SBen Widawsky 	}
1053e3689190SBen Widawsky 
105435a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
105535a85ac6SBen Widawsky 
105635a85ac6SBen Widawsky out:
105735a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
105835a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
105935a85ac6SBen Widawsky 	ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
106035a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
106135a85ac6SBen Widawsky 
106235a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
106335a85ac6SBen Widawsky }
106435a85ac6SBen Widawsky 
106535a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1066e3689190SBen Widawsky {
1067e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1068e3689190SBen Widawsky 
1069040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1070e3689190SBen Widawsky 		return;
1071e3689190SBen Widawsky 
1072d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
107335a85ac6SBen Widawsky 	ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1074d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1075e3689190SBen Widawsky 
107635a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
107735a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
107835a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
107935a85ac6SBen Widawsky 
108035a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
108135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
108235a85ac6SBen Widawsky 
1083a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1084e3689190SBen Widawsky }
1085e3689190SBen Widawsky 
1086f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1087f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1088f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1089f1af8fc1SPaulo Zanoni {
1090f1af8fc1SPaulo Zanoni 	if (gt_iir &
1091f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1092f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1093f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1094f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1095f1af8fc1SPaulo Zanoni }
1096f1af8fc1SPaulo Zanoni 
1097e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1098e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1099e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1100e7b4c6b1SDaniel Vetter {
1101e7b4c6b1SDaniel Vetter 
1102cc609d5dSBen Widawsky 	if (gt_iir &
1103cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1104e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1105cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1106e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1107cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1108e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1109e7b4c6b1SDaniel Vetter 
1110cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1111cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1112cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
1113e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
1114e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
1115e7b4c6b1SDaniel Vetter 	}
1116e3689190SBen Widawsky 
111735a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
111835a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1119e7b4c6b1SDaniel Vetter }
1120e7b4c6b1SDaniel Vetter 
1121b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1122b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1123b543fb04SEgbert Eich 
112410a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1125b543fb04SEgbert Eich 					 u32 hotplug_trigger,
1126b543fb04SEgbert Eich 					 const u32 *hpd)
1127b543fb04SEgbert Eich {
1128b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
1129b543fb04SEgbert Eich 	int i;
113010a504deSDaniel Vetter 	bool storm_detected = false;
1131b543fb04SEgbert Eich 
113291d131d2SDaniel Vetter 	if (!hotplug_trigger)
113391d131d2SDaniel Vetter 		return;
113491d131d2SDaniel Vetter 
1135b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1136b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
1137821450c6SEgbert Eich 
1138b8f102e8SEgbert Eich 		WARN(((hpd[i] & hotplug_trigger) &&
1139b8f102e8SEgbert Eich 		      dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED),
1140b8f102e8SEgbert Eich 		     "Received HPD interrupt although disabled\n");
1141b8f102e8SEgbert Eich 
1142b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1143b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1144b543fb04SEgbert Eich 			continue;
1145b543fb04SEgbert Eich 
1146bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1147b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1148b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1149b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1150b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1151b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1152b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1153b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1154b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1155142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1156b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
115710a504deSDaniel Vetter 			storm_detected = true;
1158b543fb04SEgbert Eich 		} else {
1159b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1160b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1161b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1162b543fb04SEgbert Eich 		}
1163b543fb04SEgbert Eich 	}
1164b543fb04SEgbert Eich 
116510a504deSDaniel Vetter 	if (storm_detected)
116610a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1167b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
11685876fa0dSDaniel Vetter 
1169645416f5SDaniel Vetter 	/*
1170645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1171645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1172645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1173645416f5SDaniel Vetter 	 * deadlock.
1174645416f5SDaniel Vetter 	 */
1175645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1176b543fb04SEgbert Eich }
1177b543fb04SEgbert Eich 
1178515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1179515ac2bbSDaniel Vetter {
118028c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
118128c70f16SDaniel Vetter 
118228c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1183515ac2bbSDaniel Vetter }
1184515ac2bbSDaniel Vetter 
1185ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1186ce99c256SDaniel Vetter {
11879ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
11889ee32feaSDaniel Vetter 
11899ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1190ce99c256SDaniel Vetter }
1191ce99c256SDaniel Vetter 
11928bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
11938bf1e9f1SShuang He static void ivb_pipe_crc_update(struct drm_device *dev, enum pipe pipe)
11948bf1e9f1SShuang He {
11958bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
11968bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
11978bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1198*ac2300d4SDamien Lespiau 	int head, tail;
1199b2c88f5bSDamien Lespiau 
1200b2c88f5bSDamien Lespiau 	head = atomic_read(&pipe_crc->head);
1201b2c88f5bSDamien Lespiau 	tail = atomic_read(&pipe_crc->tail);
1202b2c88f5bSDamien Lespiau 
1203b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1204b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1205b2c88f5bSDamien Lespiau 		return;
1206b2c88f5bSDamien Lespiau 	}
1207b2c88f5bSDamien Lespiau 
1208b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
12098bf1e9f1SShuang He 
1210*ac2300d4SDamien Lespiau 	entry->frame = I915_READ(PIPEFRAME(pipe));
12118bf1e9f1SShuang He 	entry->crc[0] = I915_READ(PIPE_CRC_RES_1_IVB(pipe));
12128bf1e9f1SShuang He 	entry->crc[1] = I915_READ(PIPE_CRC_RES_2_IVB(pipe));
12138bf1e9f1SShuang He 	entry->crc[2] = I915_READ(PIPE_CRC_RES_3_IVB(pipe));
12148bf1e9f1SShuang He 	entry->crc[3] = I915_READ(PIPE_CRC_RES_4_IVB(pipe));
12158bf1e9f1SShuang He 	entry->crc[4] = I915_READ(PIPE_CRC_RES_5_IVB(pipe));
1216b2c88f5bSDamien Lespiau 
1217b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1218b2c88f5bSDamien Lespiau 	atomic_set(&pipe_crc->head, head);
12198bf1e9f1SShuang He }
12208bf1e9f1SShuang He #else
12218bf1e9f1SShuang He static void ivb_pipe_crc_update(struct drm_device *dev, int pipe) {}
12228bf1e9f1SShuang He #endif
12238bf1e9f1SShuang He 
12241403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
12251403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
12261403c0d4SPaulo Zanoni  * the work queue. */
12271403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1228baf02a1fSBen Widawsky {
122941a05a3aSDaniel Vetter 	if (pm_iir & GEN6_PM_RPS_EVENTS) {
123059cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
12314848405cSBen Widawsky 		dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
12324d3b3d5fSPaulo Zanoni 		snb_disable_pm_irq(dev_priv, pm_iir & GEN6_PM_RPS_EVENTS);
123359cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
12342adbee62SDaniel Vetter 
12352adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
123641a05a3aSDaniel Vetter 	}
1237baf02a1fSBen Widawsky 
12381403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
123912638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
124012638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
124112638c57SBen Widawsky 
124212638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
124312638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
124412638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
124512638c57SBen Widawsky 		}
124612638c57SBen Widawsky 	}
12471403c0d4SPaulo Zanoni }
1248baf02a1fSBen Widawsky 
1249ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
12507e231dbeSJesse Barnes {
12517e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
12527e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
12537e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
12547e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
12557e231dbeSJesse Barnes 	unsigned long irqflags;
12567e231dbeSJesse Barnes 	int pipe;
12577e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
12587e231dbeSJesse Barnes 
12597e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
12607e231dbeSJesse Barnes 
12617e231dbeSJesse Barnes 	while (true) {
12627e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
12637e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
12647e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
12657e231dbeSJesse Barnes 
12667e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
12677e231dbeSJesse Barnes 			goto out;
12687e231dbeSJesse Barnes 
12697e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
12707e231dbeSJesse Barnes 
1271e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
12727e231dbeSJesse Barnes 
12737e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
12747e231dbeSJesse Barnes 		for_each_pipe(pipe) {
12757e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
12767e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
12777e231dbeSJesse Barnes 
12787e231dbeSJesse Barnes 			/*
12797e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
12807e231dbeSJesse Barnes 			 */
12817e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
12827e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
12837e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
12847e231dbeSJesse Barnes 							 pipe_name(pipe));
12857e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
12867e231dbeSJesse Barnes 			}
12877e231dbeSJesse Barnes 		}
12887e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
12897e231dbeSJesse Barnes 
129031acc7f5SJesse Barnes 		for_each_pipe(pipe) {
129131acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
129231acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
129331acc7f5SJesse Barnes 
129431acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
129531acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
129631acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
129731acc7f5SJesse Barnes 			}
129831acc7f5SJesse Barnes 		}
129931acc7f5SJesse Barnes 
13007e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
13017e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
13027e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1303b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
13047e231dbeSJesse Barnes 
13057e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
13067e231dbeSJesse Barnes 					 hotplug_status);
130791d131d2SDaniel Vetter 
130810a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
130991d131d2SDaniel Vetter 
13107e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
13117e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
13127e231dbeSJesse Barnes 		}
13137e231dbeSJesse Barnes 
1314515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1315515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
13167e231dbeSJesse Barnes 
131760611c13SPaulo Zanoni 		if (pm_iir)
1318d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
13197e231dbeSJesse Barnes 
13207e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
13217e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
13227e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
13237e231dbeSJesse Barnes 	}
13247e231dbeSJesse Barnes 
13257e231dbeSJesse Barnes out:
13267e231dbeSJesse Barnes 	return ret;
13277e231dbeSJesse Barnes }
13287e231dbeSJesse Barnes 
132923e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1330776ad806SJesse Barnes {
1331776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13329db4a9c7SJesse Barnes 	int pipe;
1333b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1334776ad806SJesse Barnes 
133510a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
133691d131d2SDaniel Vetter 
1337cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1338cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1339776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1340cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1341cfc33bf7SVille Syrjälä 				 port_name(port));
1342cfc33bf7SVille Syrjälä 	}
1343776ad806SJesse Barnes 
1344ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1345ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1346ce99c256SDaniel Vetter 
1347776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1348515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1349776ad806SJesse Barnes 
1350776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1351776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1352776ad806SJesse Barnes 
1353776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1354776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1355776ad806SJesse Barnes 
1356776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1357776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1358776ad806SJesse Barnes 
13599db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
13609db4a9c7SJesse Barnes 		for_each_pipe(pipe)
13619db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
13629db4a9c7SJesse Barnes 					 pipe_name(pipe),
13639db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1364776ad806SJesse Barnes 
1365776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1366776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1367776ad806SJesse Barnes 
1368776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1369776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1370776ad806SJesse Barnes 
1371776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
13728664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
13738664281bSPaulo Zanoni 							  false))
13748664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
13758664281bSPaulo Zanoni 
13768664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
13778664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
13788664281bSPaulo Zanoni 							  false))
13798664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
13808664281bSPaulo Zanoni }
13818664281bSPaulo Zanoni 
13828664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
13838664281bSPaulo Zanoni {
13848664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
13858664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
13868664281bSPaulo Zanoni 
1387de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1388de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1389de032bf4SPaulo Zanoni 
13908664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_A)
13918664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
13928664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
13938664281bSPaulo Zanoni 
13948664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_B)
13958664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
13968664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
13978664281bSPaulo Zanoni 
13988664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_C)
13998664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
14008664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
14018664281bSPaulo Zanoni 
14028bf1e9f1SShuang He 	if (err_int & ERR_INT_PIPE_CRC_DONE_A)
14038bf1e9f1SShuang He 		ivb_pipe_crc_update(dev, PIPE_A);
14048bf1e9f1SShuang He 
14058bf1e9f1SShuang He 	if (err_int & ERR_INT_PIPE_CRC_DONE_B)
14068bf1e9f1SShuang He 		ivb_pipe_crc_update(dev, PIPE_B);
14078bf1e9f1SShuang He 
14088bf1e9f1SShuang He 	if (err_int & ERR_INT_PIPE_CRC_DONE_C)
14098bf1e9f1SShuang He 		ivb_pipe_crc_update(dev, PIPE_C);
14108bf1e9f1SShuang He 
14118664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
14128664281bSPaulo Zanoni }
14138664281bSPaulo Zanoni 
14148664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
14158664281bSPaulo Zanoni {
14168664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
14178664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
14188664281bSPaulo Zanoni 
1419de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1420de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1421de032bf4SPaulo Zanoni 
14228664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
14238664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
14248664281bSPaulo Zanoni 							  false))
14258664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
14268664281bSPaulo Zanoni 
14278664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
14288664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
14298664281bSPaulo Zanoni 							  false))
14308664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
14318664281bSPaulo Zanoni 
14328664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
14338664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
14348664281bSPaulo Zanoni 							  false))
14358664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
14368664281bSPaulo Zanoni 
14378664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1438776ad806SJesse Barnes }
1439776ad806SJesse Barnes 
144023e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
144123e81d69SAdam Jackson {
144223e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
144323e81d69SAdam Jackson 	int pipe;
1444b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
144523e81d69SAdam Jackson 
144610a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
144791d131d2SDaniel Vetter 
1448cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1449cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
145023e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1451cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1452cfc33bf7SVille Syrjälä 				 port_name(port));
1453cfc33bf7SVille Syrjälä 	}
145423e81d69SAdam Jackson 
145523e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1456ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
145723e81d69SAdam Jackson 
145823e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1459515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
146023e81d69SAdam Jackson 
146123e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
146223e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
146323e81d69SAdam Jackson 
146423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
146523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
146623e81d69SAdam Jackson 
146723e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
146823e81d69SAdam Jackson 		for_each_pipe(pipe)
146923e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
147023e81d69SAdam Jackson 					 pipe_name(pipe),
147123e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
14728664281bSPaulo Zanoni 
14738664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
14748664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
147523e81d69SAdam Jackson }
147623e81d69SAdam Jackson 
1477c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1478c008bc6eSPaulo Zanoni {
1479c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1480c008bc6eSPaulo Zanoni 
1481c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1482c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1483c008bc6eSPaulo Zanoni 
1484c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1485c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1486c008bc6eSPaulo Zanoni 
1487c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEA_VBLANK)
1488c008bc6eSPaulo Zanoni 		drm_handle_vblank(dev, 0);
1489c008bc6eSPaulo Zanoni 
1490c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEB_VBLANK)
1491c008bc6eSPaulo Zanoni 		drm_handle_vblank(dev, 1);
1492c008bc6eSPaulo Zanoni 
1493c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1494c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1495c008bc6eSPaulo Zanoni 
1496c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
1497c008bc6eSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1498c008bc6eSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1499c008bc6eSPaulo Zanoni 
1500c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
1501c008bc6eSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1502c008bc6eSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1503c008bc6eSPaulo Zanoni 
1504c008bc6eSPaulo Zanoni 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1505c008bc6eSPaulo Zanoni 		intel_prepare_page_flip(dev, 0);
1506c008bc6eSPaulo Zanoni 		intel_finish_page_flip_plane(dev, 0);
1507c008bc6eSPaulo Zanoni 	}
1508c008bc6eSPaulo Zanoni 
1509c008bc6eSPaulo Zanoni 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1510c008bc6eSPaulo Zanoni 		intel_prepare_page_flip(dev, 1);
1511c008bc6eSPaulo Zanoni 		intel_finish_page_flip_plane(dev, 1);
1512c008bc6eSPaulo Zanoni 	}
1513c008bc6eSPaulo Zanoni 
1514c008bc6eSPaulo Zanoni 	/* check event from PCH */
1515c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1516c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1517c008bc6eSPaulo Zanoni 
1518c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1519c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1520c008bc6eSPaulo Zanoni 		else
1521c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1522c008bc6eSPaulo Zanoni 
1523c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1524c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1525c008bc6eSPaulo Zanoni 	}
1526c008bc6eSPaulo Zanoni 
1527c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1528c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1529c008bc6eSPaulo Zanoni }
1530c008bc6eSPaulo Zanoni 
15319719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
15329719fb98SPaulo Zanoni {
15339719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
15349719fb98SPaulo Zanoni 	int i;
15359719fb98SPaulo Zanoni 
15369719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
15379719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
15389719fb98SPaulo Zanoni 
15399719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
15409719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
15419719fb98SPaulo Zanoni 
15429719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
15439719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
15449719fb98SPaulo Zanoni 
15459719fb98SPaulo Zanoni 	for (i = 0; i < 3; i++) {
15469719fb98SPaulo Zanoni 		if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
15479719fb98SPaulo Zanoni 			drm_handle_vblank(dev, i);
15489719fb98SPaulo Zanoni 		if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
15499719fb98SPaulo Zanoni 			intel_prepare_page_flip(dev, i);
15509719fb98SPaulo Zanoni 			intel_finish_page_flip_plane(dev, i);
15519719fb98SPaulo Zanoni 		}
15529719fb98SPaulo Zanoni 	}
15539719fb98SPaulo Zanoni 
15549719fb98SPaulo Zanoni 	/* check event from PCH */
15559719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
15569719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
15579719fb98SPaulo Zanoni 
15589719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
15599719fb98SPaulo Zanoni 
15609719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
15619719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
15629719fb98SPaulo Zanoni 	}
15639719fb98SPaulo Zanoni }
15649719fb98SPaulo Zanoni 
1565f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1566b1f14ad0SJesse Barnes {
1567b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1568b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1569f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
15700e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1571b1f14ad0SJesse Barnes 
1572b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1573b1f14ad0SJesse Barnes 
15748664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
15758664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1576907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
15778664281bSPaulo Zanoni 
1578b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1579b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1580b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
158123a78516SPaulo Zanoni 	POSTING_READ(DEIER);
15820e43406bSChris Wilson 
158344498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
158444498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
158544498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
158644498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
158744498aeaSPaulo Zanoni 	 * due to its back queue). */
1588ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
158944498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
159044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
159144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1592ab5c608bSBen Widawsky 	}
159344498aeaSPaulo Zanoni 
15940e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
15950e43406bSChris Wilson 	if (gt_iir) {
1596d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
15970e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1598d8fc8a47SPaulo Zanoni 		else
1599d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
16000e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
16010e43406bSChris Wilson 		ret = IRQ_HANDLED;
16020e43406bSChris Wilson 	}
1603b1f14ad0SJesse Barnes 
1604b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
16050e43406bSChris Wilson 	if (de_iir) {
1606f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
16079719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1608f1af8fc1SPaulo Zanoni 		else
1609f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
16100e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
16110e43406bSChris Wilson 		ret = IRQ_HANDLED;
16120e43406bSChris Wilson 	}
16130e43406bSChris Wilson 
1614f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
1615f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
16160e43406bSChris Wilson 		if (pm_iir) {
1617d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
1618b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
16190e43406bSChris Wilson 			ret = IRQ_HANDLED;
16200e43406bSChris Wilson 		}
1621f1af8fc1SPaulo Zanoni 	}
1622b1f14ad0SJesse Barnes 
1623b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1624b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1625ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
162644498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
162744498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1628ab5c608bSBen Widawsky 	}
1629b1f14ad0SJesse Barnes 
1630b1f14ad0SJesse Barnes 	return ret;
1631b1f14ad0SJesse Barnes }
1632b1f14ad0SJesse Barnes 
163317e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
163417e1df07SDaniel Vetter 			       bool reset_completed)
163517e1df07SDaniel Vetter {
163617e1df07SDaniel Vetter 	struct intel_ring_buffer *ring;
163717e1df07SDaniel Vetter 	int i;
163817e1df07SDaniel Vetter 
163917e1df07SDaniel Vetter 	/*
164017e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
164117e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
164217e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
164317e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
164417e1df07SDaniel Vetter 	 */
164517e1df07SDaniel Vetter 
164617e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
164717e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
164817e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
164917e1df07SDaniel Vetter 
165017e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
165117e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
165217e1df07SDaniel Vetter 
165317e1df07SDaniel Vetter 	/*
165417e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
165517e1df07SDaniel Vetter 	 * reset state is cleared.
165617e1df07SDaniel Vetter 	 */
165717e1df07SDaniel Vetter 	if (reset_completed)
165817e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
165917e1df07SDaniel Vetter }
166017e1df07SDaniel Vetter 
16618a905236SJesse Barnes /**
16628a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
16638a905236SJesse Barnes  * @work: work struct
16648a905236SJesse Barnes  *
16658a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
16668a905236SJesse Barnes  * was detected.
16678a905236SJesse Barnes  */
16688a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
16698a905236SJesse Barnes {
16701f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
16711f83fee0SDaniel Vetter 						    work);
16721f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
16731f83fee0SDaniel Vetter 						    gpu_error);
16748a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1675cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1676cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1677cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
167817e1df07SDaniel Vetter 	int ret;
16798a905236SJesse Barnes 
1680f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
16818a905236SJesse Barnes 
16827db0ba24SDaniel Vetter 	/*
16837db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
16847db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
16857db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
16867db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
16877db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
16887db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
16897db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
16907db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
16917db0ba24SDaniel Vetter 	 */
16927db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
169344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
16947db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
16957db0ba24SDaniel Vetter 				   reset_event);
16961f83fee0SDaniel Vetter 
169717e1df07SDaniel Vetter 		/*
169817e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
169917e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
170017e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
170117e1df07SDaniel Vetter 		 * deadlocks with the reset work.
170217e1df07SDaniel Vetter 		 */
1703f69061beSDaniel Vetter 		ret = i915_reset(dev);
1704f69061beSDaniel Vetter 
170517e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
170617e1df07SDaniel Vetter 
1707f69061beSDaniel Vetter 		if (ret == 0) {
1708f69061beSDaniel Vetter 			/*
1709f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1710f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1711f69061beSDaniel Vetter 			 * complete.
1712f69061beSDaniel Vetter 			 *
1713f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1714f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1715f69061beSDaniel Vetter 			 * updates before
1716f69061beSDaniel Vetter 			 * the counter increment.
1717f69061beSDaniel Vetter 			 */
1718f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1719f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1720f69061beSDaniel Vetter 
1721f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1722f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
17231f83fee0SDaniel Vetter 		} else {
17241f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1725f316a42cSBen Gamari 		}
17261f83fee0SDaniel Vetter 
172717e1df07SDaniel Vetter 		/*
172817e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
172917e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
173017e1df07SDaniel Vetter 		 */
173117e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
1732f316a42cSBen Gamari 	}
17338a905236SJesse Barnes }
17348a905236SJesse Barnes 
173535aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1736c0e09200SDave Airlie {
17378a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1738bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
173963eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1740050ee91fSBen Widawsky 	int pipe, i;
174163eeaf38SJesse Barnes 
174235aed2e6SChris Wilson 	if (!eir)
174335aed2e6SChris Wilson 		return;
174463eeaf38SJesse Barnes 
1745a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
17468a905236SJesse Barnes 
1747bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1748bd9854f9SBen Widawsky 
17498a905236SJesse Barnes 	if (IS_G4X(dev)) {
17508a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
17518a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
17528a905236SJesse Barnes 
1753a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1754a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1755050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1756050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1757a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1758a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
17598a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
17603143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
17618a905236SJesse Barnes 		}
17628a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
17638a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1764a70491ccSJoe Perches 			pr_err("page table error\n");
1765a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
17668a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
17673143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
17688a905236SJesse Barnes 		}
17698a905236SJesse Barnes 	}
17708a905236SJesse Barnes 
1771a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
177263eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
177363eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1774a70491ccSJoe Perches 			pr_err("page table error\n");
1775a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
177663eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
17773143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
177863eeaf38SJesse Barnes 		}
17798a905236SJesse Barnes 	}
17808a905236SJesse Barnes 
178163eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1782a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
17839db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1784a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
17859db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
178663eeaf38SJesse Barnes 		/* pipestat has already been acked */
178763eeaf38SJesse Barnes 	}
178863eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1789a70491ccSJoe Perches 		pr_err("instruction error\n");
1790a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1791050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1792050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1793a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
179463eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
179563eeaf38SJesse Barnes 
1796a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1797a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1798a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
179963eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
18003143a2bfSChris Wilson 			POSTING_READ(IPEIR);
180163eeaf38SJesse Barnes 		} else {
180263eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
180363eeaf38SJesse Barnes 
1804a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1805a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1806a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1807a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
180863eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
18093143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
181063eeaf38SJesse Barnes 		}
181163eeaf38SJesse Barnes 	}
181263eeaf38SJesse Barnes 
181363eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
18143143a2bfSChris Wilson 	POSTING_READ(EIR);
181563eeaf38SJesse Barnes 	eir = I915_READ(EIR);
181663eeaf38SJesse Barnes 	if (eir) {
181763eeaf38SJesse Barnes 		/*
181863eeaf38SJesse Barnes 		 * some errors might have become stuck,
181963eeaf38SJesse Barnes 		 * mask them.
182063eeaf38SJesse Barnes 		 */
182163eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
182263eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
182363eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
182463eeaf38SJesse Barnes 	}
182535aed2e6SChris Wilson }
182635aed2e6SChris Wilson 
182735aed2e6SChris Wilson /**
182835aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
182935aed2e6SChris Wilson  * @dev: drm device
183035aed2e6SChris Wilson  *
183135aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
183235aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
183335aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
183435aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
183535aed2e6SChris Wilson  * of a ring dump etc.).
183635aed2e6SChris Wilson  */
1837527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
183835aed2e6SChris Wilson {
183935aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
184035aed2e6SChris Wilson 
184135aed2e6SChris Wilson 	i915_capture_error_state(dev);
184235aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
18438a905236SJesse Barnes 
1844ba1234d1SBen Gamari 	if (wedged) {
1845f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1846f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
1847ba1234d1SBen Gamari 
184811ed50ecSBen Gamari 		/*
184917e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
185017e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
185117e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
185217e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
185317e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
185417e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
185517e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
185617e1df07SDaniel Vetter 		 *
185717e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
185817e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
185917e1df07SDaniel Vetter 		 * counter atomic_t.
186011ed50ecSBen Gamari 		 */
186117e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
186211ed50ecSBen Gamari 	}
186311ed50ecSBen Gamari 
1864122f46baSDaniel Vetter 	/*
1865122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
1866122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
1867122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
1868122f46baSDaniel Vetter 	 * code will deadlock.
1869122f46baSDaniel Vetter 	 */
1870122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
18718a905236SJesse Barnes }
18728a905236SJesse Barnes 
187321ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
18744e5359cdSSimon Farnsworth {
18754e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
18764e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
18774e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
187805394f39SChris Wilson 	struct drm_i915_gem_object *obj;
18794e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
18804e5359cdSSimon Farnsworth 	unsigned long flags;
18814e5359cdSSimon Farnsworth 	bool stall_detected;
18824e5359cdSSimon Farnsworth 
18834e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
18844e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
18854e5359cdSSimon Farnsworth 		return;
18864e5359cdSSimon Farnsworth 
18874e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
18884e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
18894e5359cdSSimon Farnsworth 
1890e7d841caSChris Wilson 	if (work == NULL ||
1891e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1892e7d841caSChris Wilson 	    !work->enable_stall_check) {
18934e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
18944e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
18954e5359cdSSimon Farnsworth 		return;
18964e5359cdSSimon Farnsworth 	}
18974e5359cdSSimon Farnsworth 
18984e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
189905394f39SChris Wilson 	obj = work->pending_flip_obj;
1900a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
19019db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1902446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1903f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
19044e5359cdSSimon Farnsworth 	} else {
19059db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
1906f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
190701f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
19084e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
19094e5359cdSSimon Farnsworth 	}
19104e5359cdSSimon Farnsworth 
19114e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
19124e5359cdSSimon Farnsworth 
19134e5359cdSSimon Farnsworth 	if (stall_detected) {
19144e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
19154e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
19164e5359cdSSimon Farnsworth 	}
19174e5359cdSSimon Farnsworth }
19184e5359cdSSimon Farnsworth 
191942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
192042f52ef8SKeith Packard  * we use as a pipe index
192142f52ef8SKeith Packard  */
1922f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
19230a3e67a4SJesse Barnes {
19240a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1925e9d21d7fSKeith Packard 	unsigned long irqflags;
192671e0ffa5SJesse Barnes 
19275eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
192871e0ffa5SJesse Barnes 		return -EINVAL;
19290a3e67a4SJesse Barnes 
19301ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1931f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
19327c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
19337c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
19340a3e67a4SJesse Barnes 	else
19357c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
19367c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
19378692d00eSChris Wilson 
19388692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
19398692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
19406b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
19411ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
19428692d00eSChris Wilson 
19430a3e67a4SJesse Barnes 	return 0;
19440a3e67a4SJesse Barnes }
19450a3e67a4SJesse Barnes 
1946f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1947f796cf8fSJesse Barnes {
1948f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1949f796cf8fSJesse Barnes 	unsigned long irqflags;
1950b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
1951b518421fSPaulo Zanoni 						     DE_PIPE_VBLANK_ILK(pipe);
1952f796cf8fSJesse Barnes 
1953f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1954f796cf8fSJesse Barnes 		return -EINVAL;
1955f796cf8fSJesse Barnes 
1956f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1957b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
1958b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1959b1f14ad0SJesse Barnes 
1960b1f14ad0SJesse Barnes 	return 0;
1961b1f14ad0SJesse Barnes }
1962b1f14ad0SJesse Barnes 
19637e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
19647e231dbeSJesse Barnes {
19657e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19667e231dbeSJesse Barnes 	unsigned long irqflags;
196731acc7f5SJesse Barnes 	u32 imr;
19687e231dbeSJesse Barnes 
19697e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
19707e231dbeSJesse Barnes 		return -EINVAL;
19717e231dbeSJesse Barnes 
19727e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
19737e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
197431acc7f5SJesse Barnes 	if (pipe == 0)
19757e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
197631acc7f5SJesse Barnes 	else
19777e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
19787e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
197931acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
198031acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
19817e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
19827e231dbeSJesse Barnes 
19837e231dbeSJesse Barnes 	return 0;
19847e231dbeSJesse Barnes }
19857e231dbeSJesse Barnes 
198642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
198742f52ef8SKeith Packard  * we use as a pipe index
198842f52ef8SKeith Packard  */
1989f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
19900a3e67a4SJesse Barnes {
19910a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1992e9d21d7fSKeith Packard 	unsigned long irqflags;
19930a3e67a4SJesse Barnes 
19941ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
19958692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
19966b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
19978692d00eSChris Wilson 
19987c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
19997c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
20007c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
20011ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
20020a3e67a4SJesse Barnes }
20030a3e67a4SJesse Barnes 
2004f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2005f796cf8fSJesse Barnes {
2006f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2007f796cf8fSJesse Barnes 	unsigned long irqflags;
2008b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
2009b518421fSPaulo Zanoni 						     DE_PIPE_VBLANK_ILK(pipe);
2010f796cf8fSJesse Barnes 
2011f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2012b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2013b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2014b1f14ad0SJesse Barnes }
2015b1f14ad0SJesse Barnes 
20167e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
20177e231dbeSJesse Barnes {
20187e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20197e231dbeSJesse Barnes 	unsigned long irqflags;
202031acc7f5SJesse Barnes 	u32 imr;
20217e231dbeSJesse Barnes 
20227e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
202331acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
202431acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
20257e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
202631acc7f5SJesse Barnes 	if (pipe == 0)
20277e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
202831acc7f5SJesse Barnes 	else
20297e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
20307e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
20317e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
20327e231dbeSJesse Barnes }
20337e231dbeSJesse Barnes 
2034893eead0SChris Wilson static u32
2035893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2036852835f3SZou Nan hai {
2037893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2038893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2039893eead0SChris Wilson }
2040893eead0SChris Wilson 
20419107e9d2SChris Wilson static bool
20429107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2043893eead0SChris Wilson {
20449107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
20459107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2046f65d9421SBen Gamari }
2047f65d9421SBen Gamari 
20486274f212SChris Wilson static struct intel_ring_buffer *
20496274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2050a24a11e6SChris Wilson {
2051a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
20526274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2053a24a11e6SChris Wilson 
2054a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2055a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2056a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
20576274f212SChris Wilson 		return NULL;
2058a24a11e6SChris Wilson 
2059a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2060a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2061a24a11e6SChris Wilson 	 */
20626274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2063a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2064a24a11e6SChris Wilson 	do {
2065a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2066a24a11e6SChris Wilson 		if (cmd == ipehr)
2067a24a11e6SChris Wilson 			break;
2068a24a11e6SChris Wilson 
2069a24a11e6SChris Wilson 		acthd -= 4;
2070a24a11e6SChris Wilson 		if (acthd < acthd_min)
20716274f212SChris Wilson 			return NULL;
2072a24a11e6SChris Wilson 	} while (1);
2073a24a11e6SChris Wilson 
20746274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
20756274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2076a24a11e6SChris Wilson }
2077a24a11e6SChris Wilson 
20786274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
20796274f212SChris Wilson {
20806274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
20816274f212SChris Wilson 	struct intel_ring_buffer *signaller;
20826274f212SChris Wilson 	u32 seqno, ctl;
20836274f212SChris Wilson 
20846274f212SChris Wilson 	ring->hangcheck.deadlock = true;
20856274f212SChris Wilson 
20866274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
20876274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
20886274f212SChris Wilson 		return -1;
20896274f212SChris Wilson 
20906274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
20916274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
20926274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
20936274f212SChris Wilson 		return -1;
20946274f212SChris Wilson 
20956274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
20966274f212SChris Wilson }
20976274f212SChris Wilson 
20986274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
20996274f212SChris Wilson {
21006274f212SChris Wilson 	struct intel_ring_buffer *ring;
21016274f212SChris Wilson 	int i;
21026274f212SChris Wilson 
21036274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
21046274f212SChris Wilson 		ring->hangcheck.deadlock = false;
21056274f212SChris Wilson }
21066274f212SChris Wilson 
2107ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2108ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
21091ec14ad3SChris Wilson {
21101ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
21111ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
21129107e9d2SChris Wilson 	u32 tmp;
21139107e9d2SChris Wilson 
21146274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2115f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
21166274f212SChris Wilson 
21179107e9d2SChris Wilson 	if (IS_GEN2(dev))
2118f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
21199107e9d2SChris Wilson 
21209107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
21219107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
21229107e9d2SChris Wilson 	 * and break the hang. This should work on
21239107e9d2SChris Wilson 	 * all but the second generation chipsets.
21249107e9d2SChris Wilson 	 */
21259107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
21261ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
21271ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
21281ec14ad3SChris Wilson 			  ring->name);
212909e14bf3SChris Wilson 		i915_handle_error(dev, false);
21301ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2131f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
21321ec14ad3SChris Wilson 	}
2133a24a11e6SChris Wilson 
21346274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
21356274f212SChris Wilson 		switch (semaphore_passed(ring)) {
21366274f212SChris Wilson 		default:
2137f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
21386274f212SChris Wilson 		case 1:
2139a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
2140a24a11e6SChris Wilson 				  ring->name);
214109e14bf3SChris Wilson 			i915_handle_error(dev, false);
2142a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2143f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
21446274f212SChris Wilson 		case 0:
2145f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
21466274f212SChris Wilson 		}
21479107e9d2SChris Wilson 	}
21489107e9d2SChris Wilson 
2149f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2150a24a11e6SChris Wilson }
2151d1e61e7fSChris Wilson 
2152f65d9421SBen Gamari /**
2153f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
215405407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
215505407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
215605407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
215705407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
215805407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2159f65d9421SBen Gamari  */
2160a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2161f65d9421SBen Gamari {
2162f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2163f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2164b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2165b4519513SChris Wilson 	int i;
216605407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
21679107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
21689107e9d2SChris Wilson #define BUSY 1
21699107e9d2SChris Wilson #define KICK 5
21709107e9d2SChris Wilson #define HUNG 20
21719107e9d2SChris Wilson #define FIRE 30
2172893eead0SChris Wilson 
21733e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
21743e0dc6b0SBen Widawsky 		return;
21753e0dc6b0SBen Widawsky 
2176b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
217705407ff8SMika Kuoppala 		u32 seqno, acthd;
21789107e9d2SChris Wilson 		bool busy = true;
2179b4519513SChris Wilson 
21806274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
21816274f212SChris Wilson 
218205407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
218305407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
218405407ff8SMika Kuoppala 
218505407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
21869107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2187da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2188da661464SMika Kuoppala 
21899107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
21909107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2191094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
21929107e9d2SChris Wilson 						DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
21939107e9d2SChris Wilson 							  ring->name);
21949107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2195094f9a54SChris Wilson 					}
2196094f9a54SChris Wilson 					/* Safeguard against driver failure */
2197094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
21989107e9d2SChris Wilson 				} else
21999107e9d2SChris Wilson 					busy = false;
220005407ff8SMika Kuoppala 			} else {
22016274f212SChris Wilson 				/* We always increment the hangcheck score
22026274f212SChris Wilson 				 * if the ring is busy and still processing
22036274f212SChris Wilson 				 * the same request, so that no single request
22046274f212SChris Wilson 				 * can run indefinitely (such as a chain of
22056274f212SChris Wilson 				 * batches). The only time we do not increment
22066274f212SChris Wilson 				 * the hangcheck score on this ring, if this
22076274f212SChris Wilson 				 * ring is in a legitimate wait for another
22086274f212SChris Wilson 				 * ring. In that case the waiting ring is a
22096274f212SChris Wilson 				 * victim and we want to be sure we catch the
22106274f212SChris Wilson 				 * right culprit. Then every time we do kick
22116274f212SChris Wilson 				 * the ring, add a small increment to the
22126274f212SChris Wilson 				 * score so that we can catch a batch that is
22136274f212SChris Wilson 				 * being repeatedly kicked and so responsible
22146274f212SChris Wilson 				 * for stalling the machine.
22159107e9d2SChris Wilson 				 */
2216ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2217ad8beaeaSMika Kuoppala 								    acthd);
2218ad8beaeaSMika Kuoppala 
2219ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2220da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2221f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
22226274f212SChris Wilson 					break;
2223f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2224ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
22256274f212SChris Wilson 					break;
2226f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2227ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
22286274f212SChris Wilson 					break;
2229f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2230ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
22316274f212SChris Wilson 					stuck[i] = true;
22326274f212SChris Wilson 					break;
22336274f212SChris Wilson 				}
223405407ff8SMika Kuoppala 			}
22359107e9d2SChris Wilson 		} else {
2236da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2237da661464SMika Kuoppala 
22389107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
22399107e9d2SChris Wilson 			 * attempts across multiple batches.
22409107e9d2SChris Wilson 			 */
22419107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
22429107e9d2SChris Wilson 				ring->hangcheck.score--;
2243cbb465e7SChris Wilson 		}
2244f65d9421SBen Gamari 
224505407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
224605407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
22479107e9d2SChris Wilson 		busy_count += busy;
224805407ff8SMika Kuoppala 	}
224905407ff8SMika Kuoppala 
225005407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
22519107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2252b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
225305407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2254a43adf07SChris Wilson 				 ring->name);
2255a43adf07SChris Wilson 			rings_hung++;
225605407ff8SMika Kuoppala 		}
225705407ff8SMika Kuoppala 	}
225805407ff8SMika Kuoppala 
225905407ff8SMika Kuoppala 	if (rings_hung)
226005407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
226105407ff8SMika Kuoppala 
226205407ff8SMika Kuoppala 	if (busy_count)
226305407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
226405407ff8SMika Kuoppala 		 * being added */
226510cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
226610cd45b6SMika Kuoppala }
226710cd45b6SMika Kuoppala 
226810cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
226910cd45b6SMika Kuoppala {
227010cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
227110cd45b6SMika Kuoppala 	if (!i915_enable_hangcheck)
227210cd45b6SMika Kuoppala 		return;
227310cd45b6SMika Kuoppala 
227499584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
227510cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2276f65d9421SBen Gamari }
2277f65d9421SBen Gamari 
227891738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
227991738a95SPaulo Zanoni {
228091738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
228191738a95SPaulo Zanoni 
228291738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
228391738a95SPaulo Zanoni 		return;
228491738a95SPaulo Zanoni 
228591738a95SPaulo Zanoni 	/* south display irq */
228691738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
228791738a95SPaulo Zanoni 	/*
228891738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
228991738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
229091738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
229191738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
229291738a95SPaulo Zanoni 	 */
229391738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
229491738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
229591738a95SPaulo Zanoni }
229691738a95SPaulo Zanoni 
2297d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2298d18ea1b5SDaniel Vetter {
2299d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2300d18ea1b5SDaniel Vetter 
2301d18ea1b5SDaniel Vetter 	/* and GT */
2302d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2303d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2304d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2305d18ea1b5SDaniel Vetter 
2306d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2307d18ea1b5SDaniel Vetter 		/* and PM */
2308d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2309d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2310d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2311d18ea1b5SDaniel Vetter 	}
2312d18ea1b5SDaniel Vetter }
2313d18ea1b5SDaniel Vetter 
2314c0e09200SDave Airlie /* drm_dma.h hooks
2315c0e09200SDave Airlie */
2316f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2317036a4a7dSZhenyu Wang {
2318036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2319036a4a7dSZhenyu Wang 
23204697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
23214697995bSJesse Barnes 
2322036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2323bdfcdb63SDaniel Vetter 
2324036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2325036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
23263143a2bfSChris Wilson 	POSTING_READ(DEIER);
2327036a4a7dSZhenyu Wang 
2328d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2329c650156aSZhenyu Wang 
233091738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
23317d99163dSBen Widawsky }
23327d99163dSBen Widawsky 
23337e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
23347e231dbeSJesse Barnes {
23357e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23367e231dbeSJesse Barnes 	int pipe;
23377e231dbeSJesse Barnes 
23387e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
23397e231dbeSJesse Barnes 
23407e231dbeSJesse Barnes 	/* VLV magic */
23417e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
23427e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
23437e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
23447e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
23457e231dbeSJesse Barnes 
23467e231dbeSJesse Barnes 	/* and GT */
23477e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
23487e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2349d18ea1b5SDaniel Vetter 
2350d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
23517e231dbeSJesse Barnes 
23527e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
23537e231dbeSJesse Barnes 
23547e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
23557e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
23567e231dbeSJesse Barnes 	for_each_pipe(pipe)
23577e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
23587e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
23597e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
23607e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
23617e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
23627e231dbeSJesse Barnes }
23637e231dbeSJesse Barnes 
236482a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
236582a28bcfSDaniel Vetter {
236682a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
236782a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
236882a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2369fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
237082a28bcfSDaniel Vetter 
237182a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2372fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
237382a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2374cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2375fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
237682a28bcfSDaniel Vetter 	} else {
2377fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
237882a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2379cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2380fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
238182a28bcfSDaniel Vetter 	}
238282a28bcfSDaniel Vetter 
2383fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
238482a28bcfSDaniel Vetter 
23857fe0b973SKeith Packard 	/*
23867fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
23877fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
23887fe0b973SKeith Packard 	 *
23897fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
23907fe0b973SKeith Packard 	 */
23917fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
23927fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
23937fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
23947fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
23957fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
23967fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
23977fe0b973SKeith Packard }
23987fe0b973SKeith Packard 
2399d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2400d46da437SPaulo Zanoni {
2401d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
240282a28bcfSDaniel Vetter 	u32 mask;
2403d46da437SPaulo Zanoni 
2404692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2405692a04cfSDaniel Vetter 		return;
2406692a04cfSDaniel Vetter 
24078664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
24088664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2409de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
24108664281bSPaulo Zanoni 	} else {
24118664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
24128664281bSPaulo Zanoni 
24138664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
24148664281bSPaulo Zanoni 	}
2415ab5c608bSBen Widawsky 
2416d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2417d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2418d46da437SPaulo Zanoni }
2419d46da437SPaulo Zanoni 
24200a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
24210a9a8c91SDaniel Vetter {
24220a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
24230a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
24240a9a8c91SDaniel Vetter 
24250a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
24260a9a8c91SDaniel Vetter 
24270a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
2428040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
24290a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
243035a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
243135a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
24320a9a8c91SDaniel Vetter 	}
24330a9a8c91SDaniel Vetter 
24340a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
24350a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
24360a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
24370a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
24380a9a8c91SDaniel Vetter 	} else {
24390a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
24400a9a8c91SDaniel Vetter 	}
24410a9a8c91SDaniel Vetter 
24420a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
24430a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
24440a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
24450a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
24460a9a8c91SDaniel Vetter 
24470a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
24480a9a8c91SDaniel Vetter 		pm_irqs |= GEN6_PM_RPS_EVENTS;
24490a9a8c91SDaniel Vetter 
24500a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
24510a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
24520a9a8c91SDaniel Vetter 
2453605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
24540a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2455605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
24560a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
24570a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
24580a9a8c91SDaniel Vetter 	}
24590a9a8c91SDaniel Vetter }
24600a9a8c91SDaniel Vetter 
2461f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2462036a4a7dSZhenyu Wang {
24634bc9d430SDaniel Vetter 	unsigned long irqflags;
2464036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24658e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
24668e76f8dcSPaulo Zanoni 
24678e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
24688e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
24698e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
24708e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
24718e76f8dcSPaulo Zanoni 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
24728e76f8dcSPaulo Zanoni 				DE_ERR_INT_IVB);
24738e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
24748e76f8dcSPaulo Zanoni 			      DE_PIPEA_VBLANK_IVB);
24758e76f8dcSPaulo Zanoni 
24768e76f8dcSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
24778e76f8dcSPaulo Zanoni 	} else {
24788e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2479ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
24808664281bSPaulo Zanoni 				DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
24818e76f8dcSPaulo Zanoni 				DE_PIPEA_FIFO_UNDERRUN | DE_POISON);
24828e76f8dcSPaulo Zanoni 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
24838e76f8dcSPaulo Zanoni 	}
2484036a4a7dSZhenyu Wang 
24851ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2486036a4a7dSZhenyu Wang 
2487036a4a7dSZhenyu Wang 	/* should always can generate irq */
2488036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
24891ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
24908e76f8dcSPaulo Zanoni 	I915_WRITE(DEIER, display_mask | extra_mask);
24913143a2bfSChris Wilson 	POSTING_READ(DEIER);
2492036a4a7dSZhenyu Wang 
24930a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
2494036a4a7dSZhenyu Wang 
2495d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
24967fe0b973SKeith Packard 
2497f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
24986005ce42SDaniel Vetter 		/* Enable PCU event interrupts
24996005ce42SDaniel Vetter 		 *
25006005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
25014bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
25024bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
25034bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2504f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
25054bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2506f97108d1SJesse Barnes 	}
2507f97108d1SJesse Barnes 
2508036a4a7dSZhenyu Wang 	return 0;
2509036a4a7dSZhenyu Wang }
2510036a4a7dSZhenyu Wang 
25117e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
25127e231dbeSJesse Barnes {
25137e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25147e231dbeSJesse Barnes 	u32 enable_mask;
251531acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
2516b79480baSDaniel Vetter 	unsigned long irqflags;
25177e231dbeSJesse Barnes 
25187e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
251931acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
252031acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
252131acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
25227e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
25237e231dbeSJesse Barnes 
252431acc7f5SJesse Barnes 	/*
252531acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
252631acc7f5SJesse Barnes 	 * toggle them based on usage.
252731acc7f5SJesse Barnes 	 */
252831acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
252931acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
253031acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
25317e231dbeSJesse Barnes 
253220afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
253320afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
253420afbda2SDaniel Vetter 
25357e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
25367e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
25377e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
25387e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
25397e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
25407e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
25417e231dbeSJesse Barnes 
2542b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2543b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2544b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
254531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2546515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
254731acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
2548b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
254931acc7f5SJesse Barnes 
25507e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
25517e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
25527e231dbeSJesse Barnes 
25530a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
25547e231dbeSJesse Barnes 
25557e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
25567e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
25577e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
25587e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
25597e231dbeSJesse Barnes #endif
25607e231dbeSJesse Barnes 
25617e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
256220afbda2SDaniel Vetter 
256320afbda2SDaniel Vetter 	return 0;
256420afbda2SDaniel Vetter }
256520afbda2SDaniel Vetter 
25667e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
25677e231dbeSJesse Barnes {
25687e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25697e231dbeSJesse Barnes 	int pipe;
25707e231dbeSJesse Barnes 
25717e231dbeSJesse Barnes 	if (!dev_priv)
25727e231dbeSJesse Barnes 		return;
25737e231dbeSJesse Barnes 
2574ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2575ac4c16c5SEgbert Eich 
25767e231dbeSJesse Barnes 	for_each_pipe(pipe)
25777e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
25787e231dbeSJesse Barnes 
25797e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
25807e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
25817e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
25827e231dbeSJesse Barnes 	for_each_pipe(pipe)
25837e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
25847e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
25857e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
25867e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
25877e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
25887e231dbeSJesse Barnes }
25897e231dbeSJesse Barnes 
2590f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2591036a4a7dSZhenyu Wang {
2592036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25934697995bSJesse Barnes 
25944697995bSJesse Barnes 	if (!dev_priv)
25954697995bSJesse Barnes 		return;
25964697995bSJesse Barnes 
2597ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2598ac4c16c5SEgbert Eich 
2599036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2600036a4a7dSZhenyu Wang 
2601036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2602036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2603036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
26048664281bSPaulo Zanoni 	if (IS_GEN7(dev))
26058664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2606036a4a7dSZhenyu Wang 
2607036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2608036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2609036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2610192aac1fSKeith Packard 
2611ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2612ab5c608bSBen Widawsky 		return;
2613ab5c608bSBen Widawsky 
2614192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2615192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2616192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
26178664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
26188664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2619036a4a7dSZhenyu Wang }
2620036a4a7dSZhenyu Wang 
2621c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2622c2798b19SChris Wilson {
2623c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2624c2798b19SChris Wilson 	int pipe;
2625c2798b19SChris Wilson 
2626c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2627c2798b19SChris Wilson 
2628c2798b19SChris Wilson 	for_each_pipe(pipe)
2629c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2630c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2631c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2632c2798b19SChris Wilson 	POSTING_READ16(IER);
2633c2798b19SChris Wilson }
2634c2798b19SChris Wilson 
2635c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2636c2798b19SChris Wilson {
2637c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2638c2798b19SChris Wilson 
2639c2798b19SChris Wilson 	I915_WRITE16(EMR,
2640c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2641c2798b19SChris Wilson 
2642c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2643c2798b19SChris Wilson 	dev_priv->irq_mask =
2644c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2645c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2646c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2647c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2648c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2649c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2650c2798b19SChris Wilson 
2651c2798b19SChris Wilson 	I915_WRITE16(IER,
2652c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2653c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2654c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2655c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2656c2798b19SChris Wilson 	POSTING_READ16(IER);
2657c2798b19SChris Wilson 
2658c2798b19SChris Wilson 	return 0;
2659c2798b19SChris Wilson }
2660c2798b19SChris Wilson 
266190a72f87SVille Syrjälä /*
266290a72f87SVille Syrjälä  * Returns true when a page flip has completed.
266390a72f87SVille Syrjälä  */
266490a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
266590a72f87SVille Syrjälä 			       int pipe, u16 iir)
266690a72f87SVille Syrjälä {
266790a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
266890a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
266990a72f87SVille Syrjälä 
267090a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
267190a72f87SVille Syrjälä 		return false;
267290a72f87SVille Syrjälä 
267390a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
267490a72f87SVille Syrjälä 		return false;
267590a72f87SVille Syrjälä 
267690a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
267790a72f87SVille Syrjälä 
267890a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
267990a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
268090a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
268190a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
268290a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
268390a72f87SVille Syrjälä 	 */
268490a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
268590a72f87SVille Syrjälä 		return false;
268690a72f87SVille Syrjälä 
268790a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
268890a72f87SVille Syrjälä 
268990a72f87SVille Syrjälä 	return true;
269090a72f87SVille Syrjälä }
269190a72f87SVille Syrjälä 
2692ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2693c2798b19SChris Wilson {
2694c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2695c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2696c2798b19SChris Wilson 	u16 iir, new_iir;
2697c2798b19SChris Wilson 	u32 pipe_stats[2];
2698c2798b19SChris Wilson 	unsigned long irqflags;
2699c2798b19SChris Wilson 	int pipe;
2700c2798b19SChris Wilson 	u16 flip_mask =
2701c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2702c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2703c2798b19SChris Wilson 
2704c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2705c2798b19SChris Wilson 
2706c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2707c2798b19SChris Wilson 	if (iir == 0)
2708c2798b19SChris Wilson 		return IRQ_NONE;
2709c2798b19SChris Wilson 
2710c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2711c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2712c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2713c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2714c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2715c2798b19SChris Wilson 		 */
2716c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2717c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2718c2798b19SChris Wilson 			i915_handle_error(dev, false);
2719c2798b19SChris Wilson 
2720c2798b19SChris Wilson 		for_each_pipe(pipe) {
2721c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2722c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2723c2798b19SChris Wilson 
2724c2798b19SChris Wilson 			/*
2725c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2726c2798b19SChris Wilson 			 */
2727c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2728c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2729c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2730c2798b19SChris Wilson 							 pipe_name(pipe));
2731c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2732c2798b19SChris Wilson 			}
2733c2798b19SChris Wilson 		}
2734c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2735c2798b19SChris Wilson 
2736c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2737c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2738c2798b19SChris Wilson 
2739d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2740c2798b19SChris Wilson 
2741c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2742c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2743c2798b19SChris Wilson 
2744c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
274590a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
274690a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2747c2798b19SChris Wilson 
2748c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
274990a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
275090a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2751c2798b19SChris Wilson 
2752c2798b19SChris Wilson 		iir = new_iir;
2753c2798b19SChris Wilson 	}
2754c2798b19SChris Wilson 
2755c2798b19SChris Wilson 	return IRQ_HANDLED;
2756c2798b19SChris Wilson }
2757c2798b19SChris Wilson 
2758c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2759c2798b19SChris Wilson {
2760c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2761c2798b19SChris Wilson 	int pipe;
2762c2798b19SChris Wilson 
2763c2798b19SChris Wilson 	for_each_pipe(pipe) {
2764c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2765c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2766c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2767c2798b19SChris Wilson 	}
2768c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2769c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2770c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2771c2798b19SChris Wilson }
2772c2798b19SChris Wilson 
2773a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2774a266c7d5SChris Wilson {
2775a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2776a266c7d5SChris Wilson 	int pipe;
2777a266c7d5SChris Wilson 
2778a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2779a266c7d5SChris Wilson 
2780a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2781a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2782a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2783a266c7d5SChris Wilson 	}
2784a266c7d5SChris Wilson 
278500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2786a266c7d5SChris Wilson 	for_each_pipe(pipe)
2787a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2788a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2789a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2790a266c7d5SChris Wilson 	POSTING_READ(IER);
2791a266c7d5SChris Wilson }
2792a266c7d5SChris Wilson 
2793a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2794a266c7d5SChris Wilson {
2795a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
279638bde180SChris Wilson 	u32 enable_mask;
2797a266c7d5SChris Wilson 
279838bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
279938bde180SChris Wilson 
280038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
280138bde180SChris Wilson 	dev_priv->irq_mask =
280238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
280338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
280438bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
280538bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
280638bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
280738bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
280838bde180SChris Wilson 
280938bde180SChris Wilson 	enable_mask =
281038bde180SChris Wilson 		I915_ASLE_INTERRUPT |
281138bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
281238bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
281338bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
281438bde180SChris Wilson 		I915_USER_INTERRUPT;
281538bde180SChris Wilson 
2816a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
281720afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
281820afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
281920afbda2SDaniel Vetter 
2820a266c7d5SChris Wilson 		/* Enable in IER... */
2821a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2822a266c7d5SChris Wilson 		/* and unmask in IMR */
2823a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2824a266c7d5SChris Wilson 	}
2825a266c7d5SChris Wilson 
2826a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2827a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2828a266c7d5SChris Wilson 	POSTING_READ(IER);
2829a266c7d5SChris Wilson 
2830f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
283120afbda2SDaniel Vetter 
283220afbda2SDaniel Vetter 	return 0;
283320afbda2SDaniel Vetter }
283420afbda2SDaniel Vetter 
283590a72f87SVille Syrjälä /*
283690a72f87SVille Syrjälä  * Returns true when a page flip has completed.
283790a72f87SVille Syrjälä  */
283890a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
283990a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
284090a72f87SVille Syrjälä {
284190a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
284290a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
284390a72f87SVille Syrjälä 
284490a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
284590a72f87SVille Syrjälä 		return false;
284690a72f87SVille Syrjälä 
284790a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
284890a72f87SVille Syrjälä 		return false;
284990a72f87SVille Syrjälä 
285090a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
285190a72f87SVille Syrjälä 
285290a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
285390a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
285490a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
285590a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
285690a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
285790a72f87SVille Syrjälä 	 */
285890a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
285990a72f87SVille Syrjälä 		return false;
286090a72f87SVille Syrjälä 
286190a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
286290a72f87SVille Syrjälä 
286390a72f87SVille Syrjälä 	return true;
286490a72f87SVille Syrjälä }
286590a72f87SVille Syrjälä 
2866ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2867a266c7d5SChris Wilson {
2868a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2869a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
28708291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2871a266c7d5SChris Wilson 	unsigned long irqflags;
287238bde180SChris Wilson 	u32 flip_mask =
287338bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
287438bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
287538bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2876a266c7d5SChris Wilson 
2877a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2878a266c7d5SChris Wilson 
2879a266c7d5SChris Wilson 	iir = I915_READ(IIR);
288038bde180SChris Wilson 	do {
288138bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
28828291ee90SChris Wilson 		bool blc_event = false;
2883a266c7d5SChris Wilson 
2884a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2885a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2886a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2887a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2888a266c7d5SChris Wilson 		 */
2889a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2890a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2891a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2892a266c7d5SChris Wilson 
2893a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2894a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2895a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2896a266c7d5SChris Wilson 
289738bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2898a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2899a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2900a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2901a266c7d5SChris Wilson 							 pipe_name(pipe));
2902a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
290338bde180SChris Wilson 				irq_received = true;
2904a266c7d5SChris Wilson 			}
2905a266c7d5SChris Wilson 		}
2906a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2907a266c7d5SChris Wilson 
2908a266c7d5SChris Wilson 		if (!irq_received)
2909a266c7d5SChris Wilson 			break;
2910a266c7d5SChris Wilson 
2911a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2912a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2913a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2914a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2915b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
2916a266c7d5SChris Wilson 
2917a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2918a266c7d5SChris Wilson 				  hotplug_status);
291991d131d2SDaniel Vetter 
292010a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
292191d131d2SDaniel Vetter 
2922a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
292338bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2924a266c7d5SChris Wilson 		}
2925a266c7d5SChris Wilson 
292638bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2927a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2928a266c7d5SChris Wilson 
2929a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2930a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2931a266c7d5SChris Wilson 
2932a266c7d5SChris Wilson 		for_each_pipe(pipe) {
293338bde180SChris Wilson 			int plane = pipe;
293438bde180SChris Wilson 			if (IS_MOBILE(dev))
293538bde180SChris Wilson 				plane = !plane;
29365e2032d4SVille Syrjälä 
293790a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
293890a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
293990a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
2940a266c7d5SChris Wilson 
2941a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2942a266c7d5SChris Wilson 				blc_event = true;
2943a266c7d5SChris Wilson 		}
2944a266c7d5SChris Wilson 
2945a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2946a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2947a266c7d5SChris Wilson 
2948a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2949a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2950a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2951a266c7d5SChris Wilson 		 * we would never get another interrupt.
2952a266c7d5SChris Wilson 		 *
2953a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2954a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2955a266c7d5SChris Wilson 		 * another one.
2956a266c7d5SChris Wilson 		 *
2957a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2958a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2959a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2960a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2961a266c7d5SChris Wilson 		 * stray interrupts.
2962a266c7d5SChris Wilson 		 */
296338bde180SChris Wilson 		ret = IRQ_HANDLED;
2964a266c7d5SChris Wilson 		iir = new_iir;
296538bde180SChris Wilson 	} while (iir & ~flip_mask);
2966a266c7d5SChris Wilson 
2967d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
29688291ee90SChris Wilson 
2969a266c7d5SChris Wilson 	return ret;
2970a266c7d5SChris Wilson }
2971a266c7d5SChris Wilson 
2972a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2973a266c7d5SChris Wilson {
2974a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2975a266c7d5SChris Wilson 	int pipe;
2976a266c7d5SChris Wilson 
2977ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2978ac4c16c5SEgbert Eich 
2979a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2980a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2981a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2982a266c7d5SChris Wilson 	}
2983a266c7d5SChris Wilson 
298400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
298555b39755SChris Wilson 	for_each_pipe(pipe) {
298655b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2987a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
298855b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
298955b39755SChris Wilson 	}
2990a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2991a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2992a266c7d5SChris Wilson 
2993a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2994a266c7d5SChris Wilson }
2995a266c7d5SChris Wilson 
2996a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2997a266c7d5SChris Wilson {
2998a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2999a266c7d5SChris Wilson 	int pipe;
3000a266c7d5SChris Wilson 
3001a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3002a266c7d5SChris Wilson 
3003a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3004a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3005a266c7d5SChris Wilson 
3006a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3007a266c7d5SChris Wilson 	for_each_pipe(pipe)
3008a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3009a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3010a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3011a266c7d5SChris Wilson 	POSTING_READ(IER);
3012a266c7d5SChris Wilson }
3013a266c7d5SChris Wilson 
3014a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3015a266c7d5SChris Wilson {
3016a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3017bbba0a97SChris Wilson 	u32 enable_mask;
3018a266c7d5SChris Wilson 	u32 error_mask;
3019b79480baSDaniel Vetter 	unsigned long irqflags;
3020a266c7d5SChris Wilson 
3021a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3022bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3023adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3024bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3025bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3026bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3027bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3028bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3029bbba0a97SChris Wilson 
3030bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
303121ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
303221ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3033bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3034bbba0a97SChris Wilson 
3035bbba0a97SChris Wilson 	if (IS_G4X(dev))
3036bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3037a266c7d5SChris Wilson 
3038b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3039b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3040b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3041515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
3042b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3043a266c7d5SChris Wilson 
3044a266c7d5SChris Wilson 	/*
3045a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3046a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3047a266c7d5SChris Wilson 	 */
3048a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3049a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3050a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3051a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3052a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3053a266c7d5SChris Wilson 	} else {
3054a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3055a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3056a266c7d5SChris Wilson 	}
3057a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3058a266c7d5SChris Wilson 
3059a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3060a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3061a266c7d5SChris Wilson 	POSTING_READ(IER);
3062a266c7d5SChris Wilson 
306320afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
306420afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
306520afbda2SDaniel Vetter 
3066f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
306720afbda2SDaniel Vetter 
306820afbda2SDaniel Vetter 	return 0;
306920afbda2SDaniel Vetter }
307020afbda2SDaniel Vetter 
3071bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
307220afbda2SDaniel Vetter {
307320afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3074e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3075cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
307620afbda2SDaniel Vetter 	u32 hotplug_en;
307720afbda2SDaniel Vetter 
3078b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3079b5ea2d56SDaniel Vetter 
3080bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3081bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3082bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3083adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3084e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3085cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3086cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3087cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3088a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3089a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3090a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3091a266c7d5SChris Wilson 		*/
3092a266c7d5SChris Wilson 		if (IS_G4X(dev))
3093a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
309485fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3095a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3096a266c7d5SChris Wilson 
3097a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3098a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3099a266c7d5SChris Wilson 	}
3100bac56d5bSEgbert Eich }
3101a266c7d5SChris Wilson 
3102ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3103a266c7d5SChris Wilson {
3104a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3105a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3106a266c7d5SChris Wilson 	u32 iir, new_iir;
3107a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3108a266c7d5SChris Wilson 	unsigned long irqflags;
3109a266c7d5SChris Wilson 	int irq_received;
3110a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
311121ad8330SVille Syrjälä 	u32 flip_mask =
311221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
311321ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3114a266c7d5SChris Wilson 
3115a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3116a266c7d5SChris Wilson 
3117a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3118a266c7d5SChris Wilson 
3119a266c7d5SChris Wilson 	for (;;) {
31202c8ba29fSChris Wilson 		bool blc_event = false;
31212c8ba29fSChris Wilson 
312221ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3123a266c7d5SChris Wilson 
3124a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3125a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3126a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3127a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3128a266c7d5SChris Wilson 		 */
3129a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3130a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3131a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3132a266c7d5SChris Wilson 
3133a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3134a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3135a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3136a266c7d5SChris Wilson 
3137a266c7d5SChris Wilson 			/*
3138a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3139a266c7d5SChris Wilson 			 */
3140a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3141a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3142a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3143a266c7d5SChris Wilson 							 pipe_name(pipe));
3144a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3145a266c7d5SChris Wilson 				irq_received = 1;
3146a266c7d5SChris Wilson 			}
3147a266c7d5SChris Wilson 		}
3148a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3149a266c7d5SChris Wilson 
3150a266c7d5SChris Wilson 		if (!irq_received)
3151a266c7d5SChris Wilson 			break;
3152a266c7d5SChris Wilson 
3153a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3154a266c7d5SChris Wilson 
3155a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3156adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3157a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3158b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3159b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
31604f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3161a266c7d5SChris Wilson 
3162a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3163a266c7d5SChris Wilson 				  hotplug_status);
316491d131d2SDaniel Vetter 
316510a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
316610a504deSDaniel Vetter 					      IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
316791d131d2SDaniel Vetter 
3168a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3169a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3170a266c7d5SChris Wilson 		}
3171a266c7d5SChris Wilson 
317221ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3173a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3174a266c7d5SChris Wilson 
3175a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3176a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3177a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3178a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3179a266c7d5SChris Wilson 
3180a266c7d5SChris Wilson 		for_each_pipe(pipe) {
31812c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
318290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
318390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3184a266c7d5SChris Wilson 
3185a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3186a266c7d5SChris Wilson 				blc_event = true;
3187a266c7d5SChris Wilson 		}
3188a266c7d5SChris Wilson 
3189a266c7d5SChris Wilson 
3190a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3191a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3192a266c7d5SChris Wilson 
3193515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3194515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3195515ac2bbSDaniel Vetter 
3196a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3197a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3198a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3199a266c7d5SChris Wilson 		 * we would never get another interrupt.
3200a266c7d5SChris Wilson 		 *
3201a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3202a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3203a266c7d5SChris Wilson 		 * another one.
3204a266c7d5SChris Wilson 		 *
3205a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3206a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3207a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3208a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3209a266c7d5SChris Wilson 		 * stray interrupts.
3210a266c7d5SChris Wilson 		 */
3211a266c7d5SChris Wilson 		iir = new_iir;
3212a266c7d5SChris Wilson 	}
3213a266c7d5SChris Wilson 
3214d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
32152c8ba29fSChris Wilson 
3216a266c7d5SChris Wilson 	return ret;
3217a266c7d5SChris Wilson }
3218a266c7d5SChris Wilson 
3219a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3220a266c7d5SChris Wilson {
3221a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3222a266c7d5SChris Wilson 	int pipe;
3223a266c7d5SChris Wilson 
3224a266c7d5SChris Wilson 	if (!dev_priv)
3225a266c7d5SChris Wilson 		return;
3226a266c7d5SChris Wilson 
3227ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3228ac4c16c5SEgbert Eich 
3229a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3230a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3231a266c7d5SChris Wilson 
3232a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3233a266c7d5SChris Wilson 	for_each_pipe(pipe)
3234a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3235a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3236a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3237a266c7d5SChris Wilson 
3238a266c7d5SChris Wilson 	for_each_pipe(pipe)
3239a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3240a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3241a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3242a266c7d5SChris Wilson }
3243a266c7d5SChris Wilson 
3244ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3245ac4c16c5SEgbert Eich {
3246ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3247ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3248ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3249ac4c16c5SEgbert Eich 	unsigned long irqflags;
3250ac4c16c5SEgbert Eich 	int i;
3251ac4c16c5SEgbert Eich 
3252ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3253ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3254ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3255ac4c16c5SEgbert Eich 
3256ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3257ac4c16c5SEgbert Eich 			continue;
3258ac4c16c5SEgbert Eich 
3259ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3260ac4c16c5SEgbert Eich 
3261ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3262ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3263ac4c16c5SEgbert Eich 
3264ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3265ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3266ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3267ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3268ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3269ac4c16c5SEgbert Eich 				if (!connector->polled)
3270ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3271ac4c16c5SEgbert Eich 			}
3272ac4c16c5SEgbert Eich 		}
3273ac4c16c5SEgbert Eich 	}
3274ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3275ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3276ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3277ac4c16c5SEgbert Eich }
3278ac4c16c5SEgbert Eich 
3279f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3280f71d4af4SJesse Barnes {
32818b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
32828b2e326dSChris Wilson 
32838b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
328499584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3285c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3286a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
32878b2e326dSChris Wilson 
328899584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
328999584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
329061bac78eSDaniel Vetter 		    (unsigned long) dev);
3291ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3292ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
329361bac78eSDaniel Vetter 
329497a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
32959ee32feaSDaniel Vetter 
32964cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
32974cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
32984cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
32994cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3300f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3301f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3302391f75e2SVille Syrjälä 	} else {
3303391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
3304391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
3305f71d4af4SJesse Barnes 	}
3306f71d4af4SJesse Barnes 
3307c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
3308f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3309f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3310c2baf4b7SVille Syrjälä 	}
3311f71d4af4SJesse Barnes 
33127e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
33137e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
33147e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
33157e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
33167e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
33177e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
33187e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3319fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3320f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3321f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3322f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3323f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3324f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3325f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3326f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
332782a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3328f71d4af4SJesse Barnes 	} else {
3329c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3330c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3331c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3332c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3333c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3334a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3335a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3336a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3337a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3338a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
333920afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3340c2798b19SChris Wilson 		} else {
3341a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3342a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3343a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3344a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3345bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3346c2798b19SChris Wilson 		}
3347f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3348f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3349f71d4af4SJesse Barnes 	}
3350f71d4af4SJesse Barnes }
335120afbda2SDaniel Vetter 
335220afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
335320afbda2SDaniel Vetter {
335420afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3355821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3356821450c6SEgbert Eich 	struct drm_connector *connector;
3357b5ea2d56SDaniel Vetter 	unsigned long irqflags;
3358821450c6SEgbert Eich 	int i;
335920afbda2SDaniel Vetter 
3360821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3361821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3362821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3363821450c6SEgbert Eich 	}
3364821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3365821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3366821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3367821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3368821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3369821450c6SEgbert Eich 	}
3370b5ea2d56SDaniel Vetter 
3371b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3372b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
3373b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
337420afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
337520afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
3376b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
337720afbda2SDaniel Vetter }
3378c67a470bSPaulo Zanoni 
3379c67a470bSPaulo Zanoni /* Disable interrupts so we can allow Package C8+. */
3380c67a470bSPaulo Zanoni void hsw_pc8_disable_interrupts(struct drm_device *dev)
3381c67a470bSPaulo Zanoni {
3382c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3383c67a470bSPaulo Zanoni 	unsigned long irqflags;
3384c67a470bSPaulo Zanoni 
3385c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3386c67a470bSPaulo Zanoni 
3387c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.deimr = I915_READ(DEIMR);
3388c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.sdeimr = I915_READ(SDEIMR);
3389c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtimr = I915_READ(GTIMR);
3390c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtier = I915_READ(GTIER);
3391c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
3392c67a470bSPaulo Zanoni 
3393c67a470bSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, ~DE_PCH_EVENT_IVB);
3394c67a470bSPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, ~SDE_HOTPLUG_MASK_CPT);
3395c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
3396c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
3397c67a470bSPaulo Zanoni 
3398c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = true;
3399c67a470bSPaulo Zanoni 
3400c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3401c67a470bSPaulo Zanoni }
3402c67a470bSPaulo Zanoni 
3403c67a470bSPaulo Zanoni /* Restore interrupts so we can recover from Package C8+. */
3404c67a470bSPaulo Zanoni void hsw_pc8_restore_interrupts(struct drm_device *dev)
3405c67a470bSPaulo Zanoni {
3406c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3407c67a470bSPaulo Zanoni 	unsigned long irqflags;
3408c67a470bSPaulo Zanoni 	uint32_t val, expected;
3409c67a470bSPaulo Zanoni 
3410c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3411c67a470bSPaulo Zanoni 
3412c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
3413c67a470bSPaulo Zanoni 	expected = ~DE_PCH_EVENT_IVB;
3414c67a470bSPaulo Zanoni 	WARN(val != expected, "DEIMR is 0x%08x, not 0x%08x\n", val, expected);
3415c67a470bSPaulo Zanoni 
3416c67a470bSPaulo Zanoni 	val = I915_READ(SDEIMR) & ~SDE_HOTPLUG_MASK_CPT;
3417c67a470bSPaulo Zanoni 	expected = ~SDE_HOTPLUG_MASK_CPT;
3418c67a470bSPaulo Zanoni 	WARN(val != expected, "SDEIMR non-HPD bits are 0x%08x, not 0x%08x\n",
3419c67a470bSPaulo Zanoni 	     val, expected);
3420c67a470bSPaulo Zanoni 
3421c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
3422c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3423c67a470bSPaulo Zanoni 	WARN(val != expected, "GTIMR is 0x%08x, not 0x%08x\n", val, expected);
3424c67a470bSPaulo Zanoni 
3425c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
3426c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3427c67a470bSPaulo Zanoni 	WARN(val != expected, "GEN6_PMIMR is 0x%08x, not 0x%08x\n", val,
3428c67a470bSPaulo Zanoni 	     expected);
3429c67a470bSPaulo Zanoni 
3430c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = false;
3431c67a470bSPaulo Zanoni 
3432c67a470bSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pc8.regsave.deimr);
3433c67a470bSPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv,
3434c67a470bSPaulo Zanoni 				     ~dev_priv->pc8.regsave.sdeimr &
3435c67a470bSPaulo Zanoni 				     ~SDE_HOTPLUG_MASK_CPT);
3436c67a470bSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pc8.regsave.gtimr);
3437c67a470bSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pc8.regsave.gen6_pmimr);
3438c67a470bSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pc8.regsave.gtier);
3439c67a470bSPaulo Zanoni 
3440c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3441c67a470bSPaulo Zanoni }
3442