xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 6d766f022a57446b3ccbaabfa2676bb979fb7c2f)
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
4453b6c42e8SDaniel Vetter i915_enable_pipestat(drm_i915_private_t *dev_priv, enum pipe 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
4623b6c42e8SDaniel Vetter i915_disable_pipestat(drm_i915_private_t *dev_priv, enum pipe 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 
4903b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_ENABLE);
491a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
4923b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
4933b6c42e8SDaniel Vetter 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
4941ec14ad3SChris Wilson 
4951ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
49601c66889SZhao Yakui }
49701c66889SZhao Yakui 
49801c66889SZhao Yakui /**
4990a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
5000a3e67a4SJesse Barnes  * @dev: DRM device
5010a3e67a4SJesse Barnes  * @pipe: pipe to check
5020a3e67a4SJesse Barnes  *
5030a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
5040a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
5050a3e67a4SJesse Barnes  * before reading such registers if unsure.
5060a3e67a4SJesse Barnes  */
5070a3e67a4SJesse Barnes static int
5080a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
5090a3e67a4SJesse Barnes {
5100a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
511702e7a56SPaulo Zanoni 
512a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
513a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
514a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
515a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51671f8ba6bSPaulo Zanoni 
517a01025afSDaniel Vetter 		return intel_crtc->active;
518a01025afSDaniel Vetter 	} else {
519a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
520a01025afSDaniel Vetter 	}
5210a3e67a4SJesse Barnes }
5220a3e67a4SJesse Barnes 
5234cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
5244cdb83ecSVille Syrjälä {
5254cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
5264cdb83ecSVille Syrjälä 	return 0;
5274cdb83ecSVille Syrjälä }
5284cdb83ecSVille Syrjälä 
52942f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
53042f52ef8SKeith Packard  * we use as a pipe index
53142f52ef8SKeith Packard  */
532f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5330a3e67a4SJesse Barnes {
5340a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5350a3e67a4SJesse Barnes 	unsigned long high_frame;
5360a3e67a4SJesse Barnes 	unsigned long low_frame;
537391f75e2SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start;
5380a3e67a4SJesse Barnes 
5390a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
54044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5419db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
5420a3e67a4SJesse Barnes 		return 0;
5430a3e67a4SJesse Barnes 	}
5440a3e67a4SJesse Barnes 
545391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
546391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
547391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
548391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
549391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
550391f75e2SVille Syrjälä 
551391f75e2SVille Syrjälä 		vbl_start = mode->crtc_vblank_start * mode->crtc_htotal;
552391f75e2SVille Syrjälä 	} else {
553391f75e2SVille Syrjälä 		enum transcoder cpu_transcoder =
554391f75e2SVille Syrjälä 			intel_pipe_to_cpu_transcoder(dev_priv, pipe);
555391f75e2SVille Syrjälä 		u32 htotal;
556391f75e2SVille Syrjälä 
557391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
558391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
559391f75e2SVille Syrjälä 
560391f75e2SVille Syrjälä 		vbl_start *= htotal;
561391f75e2SVille Syrjälä 	}
562391f75e2SVille Syrjälä 
5639db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5649db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5655eddb70bSChris Wilson 
5660a3e67a4SJesse Barnes 	/*
5670a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5680a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
5690a3e67a4SJesse Barnes 	 * register.
5700a3e67a4SJesse Barnes 	 */
5710a3e67a4SJesse Barnes 	do {
5725eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
573391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
5745eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
5750a3e67a4SJesse Barnes 	} while (high1 != high2);
5760a3e67a4SJesse Barnes 
5775eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
578391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
5795eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
580391f75e2SVille Syrjälä 
581391f75e2SVille Syrjälä 	/*
582391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
583391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
584391f75e2SVille Syrjälä 	 * counter against vblank start.
585391f75e2SVille Syrjälä 	 */
586391f75e2SVille Syrjälä 	return ((high1 << 8) | low) + (pixel >= vbl_start);
5870a3e67a4SJesse Barnes }
5880a3e67a4SJesse Barnes 
589f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
5909880b7a5SJesse Barnes {
5919880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5929db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
5939880b7a5SJesse Barnes 
5949880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
59544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5969db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
5979880b7a5SJesse Barnes 		return 0;
5989880b7a5SJesse Barnes 	}
5999880b7a5SJesse Barnes 
6009880b7a5SJesse Barnes 	return I915_READ(reg);
6019880b7a5SJesse Barnes }
6029880b7a5SJesse Barnes 
6037c06b08aSVille Syrjälä static bool intel_pipe_in_vblank(struct drm_device *dev, enum pipe pipe)
60454ddcbd2SVille Syrjälä {
60554ddcbd2SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
60654ddcbd2SVille Syrjälä 	uint32_t status;
60754ddcbd2SVille Syrjälä 
60854ddcbd2SVille Syrjälä 	if (IS_VALLEYVIEW(dev)) {
60954ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
61054ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
61154ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
61254ddcbd2SVille Syrjälä 
61354ddcbd2SVille Syrjälä 		return I915_READ(VLV_ISR) & status;
6147c06b08aSVille Syrjälä 	} else if (IS_GEN2(dev)) {
6157c06b08aSVille Syrjälä 		status = pipe == PIPE_A ?
6167c06b08aSVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
6177c06b08aSVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
6187c06b08aSVille Syrjälä 
6197c06b08aSVille Syrjälä 		return I915_READ16(ISR) & status;
6207c06b08aSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen < 5) {
62154ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
62254ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
62354ddcbd2SVille Syrjälä 			I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
62454ddcbd2SVille Syrjälä 
62554ddcbd2SVille Syrjälä 		return I915_READ(ISR) & status;
62654ddcbd2SVille Syrjälä 	} else if (INTEL_INFO(dev)->gen < 7) {
62754ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
62854ddcbd2SVille Syrjälä 			DE_PIPEA_VBLANK :
62954ddcbd2SVille Syrjälä 			DE_PIPEB_VBLANK;
63054ddcbd2SVille Syrjälä 
63154ddcbd2SVille Syrjälä 		return I915_READ(DEISR) & status;
63254ddcbd2SVille Syrjälä 	} else {
63354ddcbd2SVille Syrjälä 		switch (pipe) {
63454ddcbd2SVille Syrjälä 		default:
63554ddcbd2SVille Syrjälä 		case PIPE_A:
63654ddcbd2SVille Syrjälä 			status = DE_PIPEA_VBLANK_IVB;
63754ddcbd2SVille Syrjälä 			break;
63854ddcbd2SVille Syrjälä 		case PIPE_B:
63954ddcbd2SVille Syrjälä 			status = DE_PIPEB_VBLANK_IVB;
64054ddcbd2SVille Syrjälä 			break;
64154ddcbd2SVille Syrjälä 		case PIPE_C:
64254ddcbd2SVille Syrjälä 			status = DE_PIPEC_VBLANK_IVB;
64354ddcbd2SVille Syrjälä 			break;
64454ddcbd2SVille Syrjälä 		}
64554ddcbd2SVille Syrjälä 
64654ddcbd2SVille Syrjälä 		return I915_READ(DEISR) & status;
64754ddcbd2SVille Syrjälä 	}
64854ddcbd2SVille Syrjälä }
64954ddcbd2SVille Syrjälä 
650f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
6510af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
6520af7e4dfSMario Kleiner {
653c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
654c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
655c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
656c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
6573aa18df8SVille Syrjälä 	int position;
6580af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
6590af7e4dfSMario Kleiner 	bool in_vbl = true;
6600af7e4dfSMario Kleiner 	int ret = 0;
6610af7e4dfSMario Kleiner 
662c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
6630af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
6649db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6650af7e4dfSMario Kleiner 		return 0;
6660af7e4dfSMario Kleiner 	}
6670af7e4dfSMario Kleiner 
668c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
669c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
670c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
671c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
6720af7e4dfSMario Kleiner 
673c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
674c2baf4b7SVille Syrjälä 
6757c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
6760af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
6770af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
6780af7e4dfSMario Kleiner 		 */
6797c06b08aSVille Syrjälä 		if (IS_GEN2(dev))
6807c06b08aSVille Syrjälä 			position = I915_READ(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
6817c06b08aSVille Syrjälä 		else
6827c06b08aSVille Syrjälä 			position = I915_READ(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
68354ddcbd2SVille Syrjälä 
68454ddcbd2SVille Syrjälä 		/*
68554ddcbd2SVille Syrjälä 		 * The scanline counter increments at the leading edge
68654ddcbd2SVille Syrjälä 		 * of hsync, ie. it completely misses the active portion
68754ddcbd2SVille Syrjälä 		 * of the line. Fix up the counter at both edges of vblank
68854ddcbd2SVille Syrjälä 		 * to get a more accurate picture whether we're in vblank
68954ddcbd2SVille Syrjälä 		 * or not.
69054ddcbd2SVille Syrjälä 		 */
6917c06b08aSVille Syrjälä 		in_vbl = intel_pipe_in_vblank(dev, pipe);
69254ddcbd2SVille Syrjälä 		if ((in_vbl && position == vbl_start - 1) ||
69354ddcbd2SVille Syrjälä 		    (!in_vbl && position == vbl_end - 1))
69454ddcbd2SVille Syrjälä 			position = (position + 1) % vtotal;
6950af7e4dfSMario Kleiner 	} else {
6960af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
6970af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
6980af7e4dfSMario Kleiner 		 * scanout position.
6990af7e4dfSMario Kleiner 		 */
7000af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
7010af7e4dfSMario Kleiner 
7023aa18df8SVille Syrjälä 		/* convert to pixel counts */
7033aa18df8SVille Syrjälä 		vbl_start *= htotal;
7043aa18df8SVille Syrjälä 		vbl_end *= htotal;
7053aa18df8SVille Syrjälä 		vtotal *= htotal;
7063aa18df8SVille Syrjälä 	}
7073aa18df8SVille Syrjälä 
7083aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
7093aa18df8SVille Syrjälä 
7103aa18df8SVille Syrjälä 	/*
7113aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
7123aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
7133aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
7143aa18df8SVille Syrjälä 	 * up since vbl_end.
7153aa18df8SVille Syrjälä 	 */
7163aa18df8SVille Syrjälä 	if (position >= vbl_start)
7173aa18df8SVille Syrjälä 		position -= vbl_end;
7183aa18df8SVille Syrjälä 	else
7193aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
7203aa18df8SVille Syrjälä 
7217c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7223aa18df8SVille Syrjälä 		*vpos = position;
7233aa18df8SVille Syrjälä 		*hpos = 0;
7243aa18df8SVille Syrjälä 	} else {
7250af7e4dfSMario Kleiner 		*vpos = position / htotal;
7260af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
7270af7e4dfSMario Kleiner 	}
7280af7e4dfSMario Kleiner 
7290af7e4dfSMario Kleiner 	/* In vblank? */
7300af7e4dfSMario Kleiner 	if (in_vbl)
7310af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
7320af7e4dfSMario Kleiner 
7330af7e4dfSMario Kleiner 	return ret;
7340af7e4dfSMario Kleiner }
7350af7e4dfSMario Kleiner 
736f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
7370af7e4dfSMario Kleiner 			      int *max_error,
7380af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
7390af7e4dfSMario Kleiner 			      unsigned flags)
7400af7e4dfSMario Kleiner {
7414041b853SChris Wilson 	struct drm_crtc *crtc;
7420af7e4dfSMario Kleiner 
7437eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
7444041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7450af7e4dfSMario Kleiner 		return -EINVAL;
7460af7e4dfSMario Kleiner 	}
7470af7e4dfSMario Kleiner 
7480af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
7494041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
7504041b853SChris Wilson 	if (crtc == NULL) {
7514041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7524041b853SChris Wilson 		return -EINVAL;
7534041b853SChris Wilson 	}
7544041b853SChris Wilson 
7554041b853SChris Wilson 	if (!crtc->enabled) {
7564041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
7574041b853SChris Wilson 		return -EBUSY;
7584041b853SChris Wilson 	}
7590af7e4dfSMario Kleiner 
7600af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
7614041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
7624041b853SChris Wilson 						     vblank_time, flags,
7634041b853SChris Wilson 						     crtc);
7640af7e4dfSMario Kleiner }
7650af7e4dfSMario Kleiner 
76667c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
76767c347ffSJani Nikula 				struct drm_connector *connector)
768321a1b30SEgbert Eich {
769321a1b30SEgbert Eich 	enum drm_connector_status old_status;
770321a1b30SEgbert Eich 
771321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
772321a1b30SEgbert Eich 	old_status = connector->status;
773321a1b30SEgbert Eich 
774321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
77567c347ffSJani Nikula 	if (old_status == connector->status)
77667c347ffSJani Nikula 		return false;
77767c347ffSJani Nikula 
77867c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
779321a1b30SEgbert Eich 		      connector->base.id,
780321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
78167c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
78267c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
78367c347ffSJani Nikula 
78467c347ffSJani Nikula 	return true;
785321a1b30SEgbert Eich }
786321a1b30SEgbert Eich 
7875ca58282SJesse Barnes /*
7885ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
7895ca58282SJesse Barnes  */
790ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
791ac4c16c5SEgbert Eich 
7925ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
7935ca58282SJesse Barnes {
7945ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
7955ca58282SJesse Barnes 						    hotplug_work);
7965ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
797c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
798cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
799cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
800cd569aedSEgbert Eich 	struct drm_connector *connector;
801cd569aedSEgbert Eich 	unsigned long irqflags;
802cd569aedSEgbert Eich 	bool hpd_disabled = false;
803321a1b30SEgbert Eich 	bool changed = false;
804142e2398SEgbert Eich 	u32 hpd_event_bits;
8055ca58282SJesse Barnes 
80652d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
80752d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
80852d7ecedSDaniel Vetter 		return;
80952d7ecedSDaniel Vetter 
810a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
811e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
812e67189abSJesse Barnes 
813cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
814142e2398SEgbert Eich 
815142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
816142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
817cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
818cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
819cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
820cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
821cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
822cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
823cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
824cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
825cd569aedSEgbert Eich 				drm_get_connector_name(connector));
826cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
827cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
828cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
829cd569aedSEgbert Eich 			hpd_disabled = true;
830cd569aedSEgbert Eich 		}
831142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
832142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
833142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
834142e2398SEgbert Eich 		}
835cd569aedSEgbert Eich 	}
836cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
837cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
838cd569aedSEgbert Eich 	  * some connectors */
839ac4c16c5SEgbert Eich 	if (hpd_disabled) {
840cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
841ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
842ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
843ac4c16c5SEgbert Eich 	}
844cd569aedSEgbert Eich 
845cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
846cd569aedSEgbert Eich 
847321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
848321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
849321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
850321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
851cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
852cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
853321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
854321a1b30SEgbert Eich 				changed = true;
855321a1b30SEgbert Eich 		}
856321a1b30SEgbert Eich 	}
85740ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
85840ee3381SKeith Packard 
859321a1b30SEgbert Eich 	if (changed)
860321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
8615ca58282SJesse Barnes }
8625ca58282SJesse Barnes 
863d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
864f97108d1SJesse Barnes {
865f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
866b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
8679270388eSDaniel Vetter 	u8 new_delay;
8689270388eSDaniel Vetter 
869d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
870f97108d1SJesse Barnes 
87173edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
87273edd18fSDaniel Vetter 
87320e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
8749270388eSDaniel Vetter 
8757648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
876b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
877b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
878f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
879f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
880f97108d1SJesse Barnes 
881f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
882b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
88320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
88420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
88520e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
88620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
887b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
88820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
88920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
89020e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
89120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
892f97108d1SJesse Barnes 	}
893f97108d1SJesse Barnes 
8947648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
89520e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
896f97108d1SJesse Barnes 
897d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
8989270388eSDaniel Vetter 
899f97108d1SJesse Barnes 	return;
900f97108d1SJesse Barnes }
901f97108d1SJesse Barnes 
902549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
903549f7365SChris Wilson 			struct intel_ring_buffer *ring)
904549f7365SChris Wilson {
905475553deSChris Wilson 	if (ring->obj == NULL)
906475553deSChris Wilson 		return;
907475553deSChris Wilson 
908814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
9099862e600SChris Wilson 
910549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
91110cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
912549f7365SChris Wilson }
913549f7365SChris Wilson 
9144912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
9153b8d8d91SJesse Barnes {
9164912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
917c6a828d3SDaniel Vetter 						    rps.work);
918edbfdb45SPaulo Zanoni 	u32 pm_iir;
919dd75fdc8SChris Wilson 	int new_delay, adj;
9203b8d8d91SJesse Barnes 
92159cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
922c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
923c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
9244848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
925edbfdb45SPaulo Zanoni 	snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
92659cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
9274912d041SBen Widawsky 
92860611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
92960611c13SPaulo Zanoni 	WARN_ON(pm_iir & ~GEN6_PM_RPS_EVENTS);
93060611c13SPaulo Zanoni 
9314848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
9323b8d8d91SJesse Barnes 		return;
9333b8d8d91SJesse Barnes 
9344fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
9357b9e0ae6SChris Wilson 
936dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
9377425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
938dd75fdc8SChris Wilson 		if (adj > 0)
939dd75fdc8SChris Wilson 			adj *= 2;
940dd75fdc8SChris Wilson 		else
941dd75fdc8SChris Wilson 			adj = 1;
942dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
9437425034aSVille Syrjälä 
9447425034aSVille Syrjälä 		/*
9457425034aSVille Syrjälä 		 * For better performance, jump directly
9467425034aSVille Syrjälä 		 * to RPe if we're below it.
9477425034aSVille Syrjälä 		 */
948dd75fdc8SChris Wilson 		if (new_delay < dev_priv->rps.rpe_delay)
9497425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
950dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
951dd75fdc8SChris Wilson 		if (dev_priv->rps.cur_delay > dev_priv->rps.rpe_delay)
952dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.rpe_delay;
953dd75fdc8SChris Wilson 		else
954dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.min_delay;
955dd75fdc8SChris Wilson 		adj = 0;
956dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
957dd75fdc8SChris Wilson 		if (adj < 0)
958dd75fdc8SChris Wilson 			adj *= 2;
959dd75fdc8SChris Wilson 		else
960dd75fdc8SChris Wilson 			adj = -1;
961dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
962dd75fdc8SChris Wilson 	} else { /* unknown event */
963dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay;
964dd75fdc8SChris Wilson 	}
9653b8d8d91SJesse Barnes 
96679249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
96779249636SBen Widawsky 	 * interrupt
96879249636SBen Widawsky 	 */
969dd75fdc8SChris Wilson 	if (new_delay < (int)dev_priv->rps.min_delay)
970dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.min_delay;
971dd75fdc8SChris Wilson 	if (new_delay > (int)dev_priv->rps.max_delay)
972dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.max_delay;
973dd75fdc8SChris Wilson 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_delay;
974dd75fdc8SChris Wilson 
9750a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
9760a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
9770a073b84SJesse Barnes 	else
9784912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
9793b8d8d91SJesse Barnes 
9804fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
9813b8d8d91SJesse Barnes }
9823b8d8d91SJesse Barnes 
983e3689190SBen Widawsky 
984e3689190SBen Widawsky /**
985e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
986e3689190SBen Widawsky  * occurred.
987e3689190SBen Widawsky  * @work: workqueue struct
988e3689190SBen Widawsky  *
989e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
990e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
991e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
992e3689190SBen Widawsky  */
993e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
994e3689190SBen Widawsky {
995e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
996a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
997e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
99835a85ac6SBen Widawsky 	char *parity_event[6];
999e3689190SBen Widawsky 	uint32_t misccpctl;
1000e3689190SBen Widawsky 	unsigned long flags;
100135a85ac6SBen Widawsky 	uint8_t slice = 0;
1002e3689190SBen Widawsky 
1003e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1004e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1005e3689190SBen Widawsky 	 * any time we access those registers.
1006e3689190SBen Widawsky 	 */
1007e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1008e3689190SBen Widawsky 
100935a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
101035a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
101135a85ac6SBen Widawsky 		goto out;
101235a85ac6SBen Widawsky 
1013e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1014e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1015e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1016e3689190SBen Widawsky 
101735a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
101835a85ac6SBen Widawsky 		u32 reg;
101935a85ac6SBen Widawsky 
102035a85ac6SBen Widawsky 		slice--;
102135a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
102235a85ac6SBen Widawsky 			break;
102335a85ac6SBen Widawsky 
102435a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
102535a85ac6SBen Widawsky 
102635a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
102735a85ac6SBen Widawsky 
102835a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1029e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1030e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1031e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1032e3689190SBen Widawsky 
103335a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
103435a85ac6SBen Widawsky 		POSTING_READ(reg);
1035e3689190SBen Widawsky 
1036cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1037e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1038e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1039e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
104035a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
104135a85ac6SBen Widawsky 		parity_event[5] = NULL;
1042e3689190SBen Widawsky 
1043e3689190SBen Widawsky 		kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
1044e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1045e3689190SBen Widawsky 
104635a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
104735a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1048e3689190SBen Widawsky 
104935a85ac6SBen Widawsky 		kfree(parity_event[4]);
1050e3689190SBen Widawsky 		kfree(parity_event[3]);
1051e3689190SBen Widawsky 		kfree(parity_event[2]);
1052e3689190SBen Widawsky 		kfree(parity_event[1]);
1053e3689190SBen Widawsky 	}
1054e3689190SBen Widawsky 
105535a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
105635a85ac6SBen Widawsky 
105735a85ac6SBen Widawsky out:
105835a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
105935a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
106035a85ac6SBen Widawsky 	ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
106135a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
106235a85ac6SBen Widawsky 
106335a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
106435a85ac6SBen Widawsky }
106535a85ac6SBen Widawsky 
106635a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1067e3689190SBen Widawsky {
1068e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1069e3689190SBen Widawsky 
1070040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1071e3689190SBen Widawsky 		return;
1072e3689190SBen Widawsky 
1073d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
107435a85ac6SBen Widawsky 	ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1075d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1076e3689190SBen Widawsky 
107735a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
107835a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
107935a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
108035a85ac6SBen Widawsky 
108135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
108235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
108335a85ac6SBen Widawsky 
1084a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1085e3689190SBen Widawsky }
1086e3689190SBen Widawsky 
1087f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1088f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1089f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1090f1af8fc1SPaulo Zanoni {
1091f1af8fc1SPaulo Zanoni 	if (gt_iir &
1092f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1093f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1094f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1095f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1096f1af8fc1SPaulo Zanoni }
1097f1af8fc1SPaulo Zanoni 
1098e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1099e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1100e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1101e7b4c6b1SDaniel Vetter {
1102e7b4c6b1SDaniel Vetter 
1103cc609d5dSBen Widawsky 	if (gt_iir &
1104cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1105e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1106cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1107e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1108cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1109e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1110e7b4c6b1SDaniel Vetter 
1111cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1112cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1113cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
1114e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
1115e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
1116e7b4c6b1SDaniel Vetter 	}
1117e3689190SBen Widawsky 
111835a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
111935a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1120e7b4c6b1SDaniel Vetter }
1121e7b4c6b1SDaniel Vetter 
1122abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1123abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1124abd58f01SBen Widawsky 				       u32 master_ctl)
1125abd58f01SBen Widawsky {
1126abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1127abd58f01SBen Widawsky 	uint32_t tmp = 0;
1128abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1129abd58f01SBen Widawsky 
1130abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1131abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1132abd58f01SBen Widawsky 		if (tmp) {
1133abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1134abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1135abd58f01SBen Widawsky 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1136abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1137abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[RCS]);
1138abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1139abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[BCS]);
1140abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1141abd58f01SBen Widawsky 		} else
1142abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1143abd58f01SBen Widawsky 	}
1144abd58f01SBen Widawsky 
1145abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VCS1_IRQ) {
1146abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1147abd58f01SBen Widawsky 		if (tmp) {
1148abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1149abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1150abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1151abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VCS]);
1152abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1153abd58f01SBen Widawsky 		} else
1154abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1155abd58f01SBen Widawsky 	}
1156abd58f01SBen Widawsky 
1157abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1158abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1159abd58f01SBen Widawsky 		if (tmp) {
1160abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1161abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1162abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1163abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VECS]);
1164abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1165abd58f01SBen Widawsky 		} else
1166abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1167abd58f01SBen Widawsky 	}
1168abd58f01SBen Widawsky 
1169abd58f01SBen Widawsky 	return ret;
1170abd58f01SBen Widawsky }
1171abd58f01SBen Widawsky 
1172b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1173b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1174b543fb04SEgbert Eich 
117510a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1176b543fb04SEgbert Eich 					 u32 hotplug_trigger,
1177b543fb04SEgbert Eich 					 const u32 *hpd)
1178b543fb04SEgbert Eich {
1179b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
1180b543fb04SEgbert Eich 	int i;
118110a504deSDaniel Vetter 	bool storm_detected = false;
1182b543fb04SEgbert Eich 
118391d131d2SDaniel Vetter 	if (!hotplug_trigger)
118491d131d2SDaniel Vetter 		return;
118591d131d2SDaniel Vetter 
1186b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1187b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
1188821450c6SEgbert Eich 
1189b8f102e8SEgbert Eich 		WARN(((hpd[i] & hotplug_trigger) &&
1190b8f102e8SEgbert Eich 		      dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED),
1191b8f102e8SEgbert Eich 		     "Received HPD interrupt although disabled\n");
1192b8f102e8SEgbert Eich 
1193b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1194b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1195b543fb04SEgbert Eich 			continue;
1196b543fb04SEgbert Eich 
1197bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1198b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1199b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1200b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1201b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1202b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1203b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1204b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1205b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1206142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1207b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
120810a504deSDaniel Vetter 			storm_detected = true;
1209b543fb04SEgbert Eich 		} else {
1210b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1211b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1212b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1213b543fb04SEgbert Eich 		}
1214b543fb04SEgbert Eich 	}
1215b543fb04SEgbert Eich 
121610a504deSDaniel Vetter 	if (storm_detected)
121710a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1218b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
12195876fa0dSDaniel Vetter 
1220645416f5SDaniel Vetter 	/*
1221645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1222645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1223645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1224645416f5SDaniel Vetter 	 * deadlock.
1225645416f5SDaniel Vetter 	 */
1226645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1227b543fb04SEgbert Eich }
1228b543fb04SEgbert Eich 
1229515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1230515ac2bbSDaniel Vetter {
123128c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
123228c70f16SDaniel Vetter 
123328c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1234515ac2bbSDaniel Vetter }
1235515ac2bbSDaniel Vetter 
1236ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1237ce99c256SDaniel Vetter {
12389ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
12399ee32feaSDaniel Vetter 
12409ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1241ce99c256SDaniel Vetter }
1242ce99c256SDaniel Vetter 
12438bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1244277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1245eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1246eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
12478bc5e955SDaniel Vetter 					 uint32_t crc4)
12488bf1e9f1SShuang He {
12498bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
12508bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
12518bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1252ac2300d4SDamien Lespiau 	int head, tail;
1253b2c88f5bSDamien Lespiau 
1254d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1255d538bbdfSDamien Lespiau 
12560c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1257d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
12580c912c79SDamien Lespiau 		DRM_ERROR("spurious interrupt\n");
12590c912c79SDamien Lespiau 		return;
12600c912c79SDamien Lespiau 	}
12610c912c79SDamien Lespiau 
1262d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1263d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1264b2c88f5bSDamien Lespiau 
1265b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1266d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1267b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1268b2c88f5bSDamien Lespiau 		return;
1269b2c88f5bSDamien Lespiau 	}
1270b2c88f5bSDamien Lespiau 
1271b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
12728bf1e9f1SShuang He 
12738bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1274eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1275eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1276eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1277eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1278eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1279b2c88f5bSDamien Lespiau 
1280b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1281d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1282d538bbdfSDamien Lespiau 
1283d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
128407144428SDamien Lespiau 
128507144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
12868bf1e9f1SShuang He }
1287277de95eSDaniel Vetter #else
1288277de95eSDaniel Vetter static inline void
1289277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1290277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1291277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1292277de95eSDaniel Vetter 			     uint32_t crc4) {}
1293277de95eSDaniel Vetter #endif
1294eba94eb9SDaniel Vetter 
1295277de95eSDaniel Vetter 
1296277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
12975a69b89fSDaniel Vetter {
12985a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
12995a69b89fSDaniel Vetter 
1300277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
13015a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
13025a69b89fSDaniel Vetter 				     0, 0, 0, 0);
13035a69b89fSDaniel Vetter }
13045a69b89fSDaniel Vetter 
1305277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1306eba94eb9SDaniel Vetter {
1307eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1308eba94eb9SDaniel Vetter 
1309277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1310eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1311eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1312eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1313eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
13148bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1315eba94eb9SDaniel Vetter }
13165b3a856bSDaniel Vetter 
1317277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
13185b3a856bSDaniel Vetter {
13195b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
13200b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
13210b5c5ed0SDaniel Vetter 
13220b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
13230b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
13240b5c5ed0SDaniel Vetter 	else
13250b5c5ed0SDaniel Vetter 		res1 = 0;
13260b5c5ed0SDaniel Vetter 
13270b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
13280b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
13290b5c5ed0SDaniel Vetter 	else
13300b5c5ed0SDaniel Vetter 		res2 = 0;
13315b3a856bSDaniel Vetter 
1332277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
13330b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
13340b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
13350b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
13360b5c5ed0SDaniel Vetter 				     res1, res2);
13375b3a856bSDaniel Vetter }
13388bf1e9f1SShuang He 
13391403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
13401403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
13411403c0d4SPaulo Zanoni  * the work queue. */
13421403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1343baf02a1fSBen Widawsky {
134441a05a3aSDaniel Vetter 	if (pm_iir & GEN6_PM_RPS_EVENTS) {
134559cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
13464848405cSBen Widawsky 		dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
13474d3b3d5fSPaulo Zanoni 		snb_disable_pm_irq(dev_priv, pm_iir & GEN6_PM_RPS_EVENTS);
134859cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
13492adbee62SDaniel Vetter 
13502adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
135141a05a3aSDaniel Vetter 	}
1352baf02a1fSBen Widawsky 
13531403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
135412638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
135512638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
135612638c57SBen Widawsky 
135712638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
135812638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
135912638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
136012638c57SBen Widawsky 		}
136112638c57SBen Widawsky 	}
13621403c0d4SPaulo Zanoni }
1363baf02a1fSBen Widawsky 
1364ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
13657e231dbeSJesse Barnes {
13667e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
13677e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13687e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
13697e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
13707e231dbeSJesse Barnes 	unsigned long irqflags;
13717e231dbeSJesse Barnes 	int pipe;
13727e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
13737e231dbeSJesse Barnes 
13747e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
13757e231dbeSJesse Barnes 
13767e231dbeSJesse Barnes 	while (true) {
13777e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
13787e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
13797e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
13807e231dbeSJesse Barnes 
13817e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
13827e231dbeSJesse Barnes 			goto out;
13837e231dbeSJesse Barnes 
13847e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
13857e231dbeSJesse Barnes 
1386e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
13877e231dbeSJesse Barnes 
13887e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
13897e231dbeSJesse Barnes 		for_each_pipe(pipe) {
13907e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
13917e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
13927e231dbeSJesse Barnes 
13937e231dbeSJesse Barnes 			/*
13947e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
13957e231dbeSJesse Barnes 			 */
13967e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
13977e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
13987e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
13997e231dbeSJesse Barnes 							 pipe_name(pipe));
14007e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
14017e231dbeSJesse Barnes 			}
14027e231dbeSJesse Barnes 		}
14037e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14047e231dbeSJesse Barnes 
140531acc7f5SJesse Barnes 		for_each_pipe(pipe) {
140631acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
140731acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
140831acc7f5SJesse Barnes 
140931acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
141031acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
141131acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
141231acc7f5SJesse Barnes 			}
14134356d586SDaniel Vetter 
14144356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1415277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
141631acc7f5SJesse Barnes 		}
141731acc7f5SJesse Barnes 
14187e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
14197e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
14207e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1421b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
14227e231dbeSJesse Barnes 
14237e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
14247e231dbeSJesse Barnes 					 hotplug_status);
142591d131d2SDaniel Vetter 
142610a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
142791d131d2SDaniel Vetter 
14287e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
14297e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
14307e231dbeSJesse Barnes 		}
14317e231dbeSJesse Barnes 
1432515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1433515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
14347e231dbeSJesse Barnes 
143560611c13SPaulo Zanoni 		if (pm_iir)
1436d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
14377e231dbeSJesse Barnes 
14387e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
14397e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
14407e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
14417e231dbeSJesse Barnes 	}
14427e231dbeSJesse Barnes 
14437e231dbeSJesse Barnes out:
14447e231dbeSJesse Barnes 	return ret;
14457e231dbeSJesse Barnes }
14467e231dbeSJesse Barnes 
144723e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1448776ad806SJesse Barnes {
1449776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
14509db4a9c7SJesse Barnes 	int pipe;
1451b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1452776ad806SJesse Barnes 
145310a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
145491d131d2SDaniel Vetter 
1455cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1456cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1457776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1458cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1459cfc33bf7SVille Syrjälä 				 port_name(port));
1460cfc33bf7SVille Syrjälä 	}
1461776ad806SJesse Barnes 
1462ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1463ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1464ce99c256SDaniel Vetter 
1465776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1466515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1467776ad806SJesse Barnes 
1468776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1469776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1470776ad806SJesse Barnes 
1471776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1472776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1473776ad806SJesse Barnes 
1474776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1475776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1476776ad806SJesse Barnes 
14779db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
14789db4a9c7SJesse Barnes 		for_each_pipe(pipe)
14799db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
14809db4a9c7SJesse Barnes 					 pipe_name(pipe),
14819db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1482776ad806SJesse Barnes 
1483776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1484776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1485776ad806SJesse Barnes 
1486776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1487776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1488776ad806SJesse Barnes 
1489776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
14908664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
14918664281bSPaulo Zanoni 							  false))
14928664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
14938664281bSPaulo Zanoni 
14948664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
14958664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
14968664281bSPaulo Zanoni 							  false))
14978664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
14988664281bSPaulo Zanoni }
14998664281bSPaulo Zanoni 
15008664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
15018664281bSPaulo Zanoni {
15028664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
15038664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
15045a69b89fSDaniel Vetter 	enum pipe pipe;
15058664281bSPaulo Zanoni 
1506de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1507de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1508de032bf4SPaulo Zanoni 
15095a69b89fSDaniel Vetter 	for_each_pipe(pipe) {
15105a69b89fSDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
15115a69b89fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
15125a69b89fSDaniel Vetter 								  false))
15135a69b89fSDaniel Vetter 				DRM_DEBUG_DRIVER("Pipe %c FIFO underrun\n",
15145a69b89fSDaniel Vetter 						 pipe_name(pipe));
15155a69b89fSDaniel Vetter 		}
15168664281bSPaulo Zanoni 
15175a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
15185a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1519277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
15205a69b89fSDaniel Vetter 			else
1521277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
15225a69b89fSDaniel Vetter 		}
15235a69b89fSDaniel Vetter 	}
15248bf1e9f1SShuang He 
15258664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
15268664281bSPaulo Zanoni }
15278664281bSPaulo Zanoni 
15288664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
15298664281bSPaulo Zanoni {
15308664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
15318664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
15328664281bSPaulo Zanoni 
1533de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1534de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1535de032bf4SPaulo Zanoni 
15368664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
15378664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
15388664281bSPaulo Zanoni 							  false))
15398664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
15408664281bSPaulo Zanoni 
15418664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
15428664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
15438664281bSPaulo Zanoni 							  false))
15448664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
15458664281bSPaulo Zanoni 
15468664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
15478664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
15488664281bSPaulo Zanoni 							  false))
15498664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
15508664281bSPaulo Zanoni 
15518664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1552776ad806SJesse Barnes }
1553776ad806SJesse Barnes 
155423e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
155523e81d69SAdam Jackson {
155623e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
155723e81d69SAdam Jackson 	int pipe;
1558b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
155923e81d69SAdam Jackson 
156010a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
156191d131d2SDaniel Vetter 
1562cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1563cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
156423e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1565cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1566cfc33bf7SVille Syrjälä 				 port_name(port));
1567cfc33bf7SVille Syrjälä 	}
156823e81d69SAdam Jackson 
156923e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1570ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
157123e81d69SAdam Jackson 
157223e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1573515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
157423e81d69SAdam Jackson 
157523e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
157623e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
157723e81d69SAdam Jackson 
157823e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
157923e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
158023e81d69SAdam Jackson 
158123e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
158223e81d69SAdam Jackson 		for_each_pipe(pipe)
158323e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
158423e81d69SAdam Jackson 					 pipe_name(pipe),
158523e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
15868664281bSPaulo Zanoni 
15878664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
15888664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
158923e81d69SAdam Jackson }
159023e81d69SAdam Jackson 
1591c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1592c008bc6eSPaulo Zanoni {
1593c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
159440da17c2SDaniel Vetter 	enum pipe pipe;
1595c008bc6eSPaulo Zanoni 
1596c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1597c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1598c008bc6eSPaulo Zanoni 
1599c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1600c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1601c008bc6eSPaulo Zanoni 
1602c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1603c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1604c008bc6eSPaulo Zanoni 
160540da17c2SDaniel Vetter 	for_each_pipe(pipe) {
160640da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
160740da17c2SDaniel Vetter 			drm_handle_vblank(dev, pipe);
1608c008bc6eSPaulo Zanoni 
160940da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
161040da17c2SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
161140da17c2SDaniel Vetter 				DRM_DEBUG_DRIVER("Pipe %c FIFO underrun\n",
161240da17c2SDaniel Vetter 						 pipe_name(pipe));
1613c008bc6eSPaulo Zanoni 
161440da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
161540da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
16165b3a856bSDaniel Vetter 
161740da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
161840da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
161940da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
162040da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
1621c008bc6eSPaulo Zanoni 		}
1622c008bc6eSPaulo Zanoni 	}
1623c008bc6eSPaulo Zanoni 
1624c008bc6eSPaulo Zanoni 	/* check event from PCH */
1625c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1626c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1627c008bc6eSPaulo Zanoni 
1628c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1629c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1630c008bc6eSPaulo Zanoni 		else
1631c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1632c008bc6eSPaulo Zanoni 
1633c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1634c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1635c008bc6eSPaulo Zanoni 	}
1636c008bc6eSPaulo Zanoni 
1637c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1638c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1639c008bc6eSPaulo Zanoni }
1640c008bc6eSPaulo Zanoni 
16419719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
16429719fb98SPaulo Zanoni {
16439719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
16443b6c42e8SDaniel Vetter 	enum pipe i;
16459719fb98SPaulo Zanoni 
16469719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
16479719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
16489719fb98SPaulo Zanoni 
16499719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
16509719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
16519719fb98SPaulo Zanoni 
16529719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
16539719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
16549719fb98SPaulo Zanoni 
16553b6c42e8SDaniel Vetter 	for_each_pipe(i) {
165640da17c2SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(i)))
16579719fb98SPaulo Zanoni 			drm_handle_vblank(dev, i);
165840da17c2SDaniel Vetter 
165940da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
166040da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(i)) {
16619719fb98SPaulo Zanoni 			intel_prepare_page_flip(dev, i);
16629719fb98SPaulo Zanoni 			intel_finish_page_flip_plane(dev, i);
16639719fb98SPaulo Zanoni 		}
16649719fb98SPaulo Zanoni 	}
16659719fb98SPaulo Zanoni 
16669719fb98SPaulo Zanoni 	/* check event from PCH */
16679719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
16689719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
16699719fb98SPaulo Zanoni 
16709719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
16719719fb98SPaulo Zanoni 
16729719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
16739719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
16749719fb98SPaulo Zanoni 	}
16759719fb98SPaulo Zanoni }
16769719fb98SPaulo Zanoni 
1677f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1678b1f14ad0SJesse Barnes {
1679b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1680b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1681f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
16820e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1683b1f14ad0SJesse Barnes 
1684b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1685b1f14ad0SJesse Barnes 
16868664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
16878664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1688907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
16898664281bSPaulo Zanoni 
1690b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1691b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1692b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
169323a78516SPaulo Zanoni 	POSTING_READ(DEIER);
16940e43406bSChris Wilson 
169544498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
169644498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
169744498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
169844498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
169944498aeaSPaulo Zanoni 	 * due to its back queue). */
1700ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
170144498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
170244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
170344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1704ab5c608bSBen Widawsky 	}
170544498aeaSPaulo Zanoni 
17060e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
17070e43406bSChris Wilson 	if (gt_iir) {
1708d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
17090e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1710d8fc8a47SPaulo Zanoni 		else
1711d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
17120e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
17130e43406bSChris Wilson 		ret = IRQ_HANDLED;
17140e43406bSChris Wilson 	}
1715b1f14ad0SJesse Barnes 
1716b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
17170e43406bSChris Wilson 	if (de_iir) {
1718f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
17199719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1720f1af8fc1SPaulo Zanoni 		else
1721f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
17220e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
17230e43406bSChris Wilson 		ret = IRQ_HANDLED;
17240e43406bSChris Wilson 	}
17250e43406bSChris Wilson 
1726f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
1727f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
17280e43406bSChris Wilson 		if (pm_iir) {
1729d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
1730b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
17310e43406bSChris Wilson 			ret = IRQ_HANDLED;
17320e43406bSChris Wilson 		}
1733f1af8fc1SPaulo Zanoni 	}
1734b1f14ad0SJesse Barnes 
1735b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1736b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1737ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
173844498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
173944498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1740ab5c608bSBen Widawsky 	}
1741b1f14ad0SJesse Barnes 
1742b1f14ad0SJesse Barnes 	return ret;
1743b1f14ad0SJesse Barnes }
1744b1f14ad0SJesse Barnes 
1745abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
1746abd58f01SBen Widawsky {
1747abd58f01SBen Widawsky 	struct drm_device *dev = arg;
1748abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
1749abd58f01SBen Widawsky 	u32 master_ctl;
1750abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1751abd58f01SBen Widawsky 	uint32_t tmp = 0;
1752c42664ccSDaniel Vetter 	enum pipe pipe;
1753abd58f01SBen Widawsky 
1754abd58f01SBen Widawsky 	atomic_inc(&dev_priv->irq_received);
1755abd58f01SBen Widawsky 
1756abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
1757abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
1758abd58f01SBen Widawsky 	if (!master_ctl)
1759abd58f01SBen Widawsky 		return IRQ_NONE;
1760abd58f01SBen Widawsky 
1761abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
1762abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
1763abd58f01SBen Widawsky 
1764abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
1765abd58f01SBen Widawsky 
1766abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
1767abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
1768abd58f01SBen Widawsky 		if (tmp & GEN8_DE_MISC_GSE)
1769abd58f01SBen Widawsky 			intel_opregion_asle_intr(dev);
1770abd58f01SBen Widawsky 		else if (tmp)
1771abd58f01SBen Widawsky 			DRM_ERROR("Unexpected DE Misc interrupt\n");
1772abd58f01SBen Widawsky 		else
1773abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
1774abd58f01SBen Widawsky 
1775abd58f01SBen Widawsky 		if (tmp) {
1776abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
1777abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1778abd58f01SBen Widawsky 		}
1779abd58f01SBen Widawsky 	}
1780abd58f01SBen Widawsky 
1781*6d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
1782*6d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
1783*6d766f02SDaniel Vetter 		if (tmp & GEN8_AUX_CHANNEL_A)
1784*6d766f02SDaniel Vetter 			dp_aux_irq_handler(dev);
1785*6d766f02SDaniel Vetter 		else if (tmp)
1786*6d766f02SDaniel Vetter 			DRM_ERROR("Unexpected DE Port interrupt\n");
1787*6d766f02SDaniel Vetter 		else
1788*6d766f02SDaniel Vetter 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
1789*6d766f02SDaniel Vetter 
1790*6d766f02SDaniel Vetter 		if (tmp) {
1791*6d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
1792*6d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
1793*6d766f02SDaniel Vetter 		}
1794*6d766f02SDaniel Vetter 	}
1795*6d766f02SDaniel Vetter 
1796abd58f01SBen Widawsky 	for_each_pipe(pipe) {
1797abd58f01SBen Widawsky 		uint32_t pipe_iir;
1798abd58f01SBen Widawsky 
1799c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
1800c42664ccSDaniel Vetter 			continue;
1801c42664ccSDaniel Vetter 
1802abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
1803abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_VBLANK)
1804abd58f01SBen Widawsky 			drm_handle_vblank(dev, pipe);
1805abd58f01SBen Widawsky 
1806abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_FLIP_DONE) {
1807abd58f01SBen Widawsky 			intel_prepare_page_flip(dev, pipe);
1808abd58f01SBen Widawsky 			intel_finish_page_flip_plane(dev, pipe);
1809abd58f01SBen Widawsky 		}
1810abd58f01SBen Widawsky 
181130100f2bSDaniel Vetter 		if (pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS) {
181230100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
181330100f2bSDaniel Vetter 				  pipe_name(pipe),
181430100f2bSDaniel Vetter 				  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
181530100f2bSDaniel Vetter 		}
1816abd58f01SBen Widawsky 
1817abd58f01SBen Widawsky 		if (pipe_iir) {
1818abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1819abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
1820c42664ccSDaniel Vetter 		} else
1821abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
1822abd58f01SBen Widawsky 	}
1823abd58f01SBen Widawsky 
1824abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
1825abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
1826abd58f01SBen Widawsky 
1827abd58f01SBen Widawsky 	return ret;
1828abd58f01SBen Widawsky }
1829abd58f01SBen Widawsky 
183017e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
183117e1df07SDaniel Vetter 			       bool reset_completed)
183217e1df07SDaniel Vetter {
183317e1df07SDaniel Vetter 	struct intel_ring_buffer *ring;
183417e1df07SDaniel Vetter 	int i;
183517e1df07SDaniel Vetter 
183617e1df07SDaniel Vetter 	/*
183717e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
183817e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
183917e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
184017e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
184117e1df07SDaniel Vetter 	 */
184217e1df07SDaniel Vetter 
184317e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
184417e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
184517e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
184617e1df07SDaniel Vetter 
184717e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
184817e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
184917e1df07SDaniel Vetter 
185017e1df07SDaniel Vetter 	/*
185117e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
185217e1df07SDaniel Vetter 	 * reset state is cleared.
185317e1df07SDaniel Vetter 	 */
185417e1df07SDaniel Vetter 	if (reset_completed)
185517e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
185617e1df07SDaniel Vetter }
185717e1df07SDaniel Vetter 
18588a905236SJesse Barnes /**
18598a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
18608a905236SJesse Barnes  * @work: work struct
18618a905236SJesse Barnes  *
18628a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
18638a905236SJesse Barnes  * was detected.
18648a905236SJesse Barnes  */
18658a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
18668a905236SJesse Barnes {
18671f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
18681f83fee0SDaniel Vetter 						    work);
18691f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
18701f83fee0SDaniel Vetter 						    gpu_error);
18718a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1872cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1873cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1874cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
187517e1df07SDaniel Vetter 	int ret;
18768a905236SJesse Barnes 
1877f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
18788a905236SJesse Barnes 
18797db0ba24SDaniel Vetter 	/*
18807db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
18817db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
18827db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
18837db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
18847db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
18857db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
18867db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
18877db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
18887db0ba24SDaniel Vetter 	 */
18897db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
189044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
18917db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
18927db0ba24SDaniel Vetter 				   reset_event);
18931f83fee0SDaniel Vetter 
189417e1df07SDaniel Vetter 		/*
189517e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
189617e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
189717e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
189817e1df07SDaniel Vetter 		 * deadlocks with the reset work.
189917e1df07SDaniel Vetter 		 */
1900f69061beSDaniel Vetter 		ret = i915_reset(dev);
1901f69061beSDaniel Vetter 
190217e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
190317e1df07SDaniel Vetter 
1904f69061beSDaniel Vetter 		if (ret == 0) {
1905f69061beSDaniel Vetter 			/*
1906f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1907f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1908f69061beSDaniel Vetter 			 * complete.
1909f69061beSDaniel Vetter 			 *
1910f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1911f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1912f69061beSDaniel Vetter 			 * updates before
1913f69061beSDaniel Vetter 			 * the counter increment.
1914f69061beSDaniel Vetter 			 */
1915f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1916f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1917f69061beSDaniel Vetter 
1918f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1919f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
19201f83fee0SDaniel Vetter 		} else {
19211f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1922f316a42cSBen Gamari 		}
19231f83fee0SDaniel Vetter 
192417e1df07SDaniel Vetter 		/*
192517e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
192617e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
192717e1df07SDaniel Vetter 		 */
192817e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
1929f316a42cSBen Gamari 	}
19308a905236SJesse Barnes }
19318a905236SJesse Barnes 
193235aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1933c0e09200SDave Airlie {
19348a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1935bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
193663eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1937050ee91fSBen Widawsky 	int pipe, i;
193863eeaf38SJesse Barnes 
193935aed2e6SChris Wilson 	if (!eir)
194035aed2e6SChris Wilson 		return;
194163eeaf38SJesse Barnes 
1942a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
19438a905236SJesse Barnes 
1944bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1945bd9854f9SBen Widawsky 
19468a905236SJesse Barnes 	if (IS_G4X(dev)) {
19478a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
19488a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
19498a905236SJesse Barnes 
1950a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1951a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1952050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1953050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1954a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1955a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
19568a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
19573143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
19588a905236SJesse Barnes 		}
19598a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
19608a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1961a70491ccSJoe Perches 			pr_err("page table error\n");
1962a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
19638a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
19643143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
19658a905236SJesse Barnes 		}
19668a905236SJesse Barnes 	}
19678a905236SJesse Barnes 
1968a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
196963eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
197063eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1971a70491ccSJoe Perches 			pr_err("page table error\n");
1972a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
197363eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
19743143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
197563eeaf38SJesse Barnes 		}
19768a905236SJesse Barnes 	}
19778a905236SJesse Barnes 
197863eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1979a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
19809db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1981a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
19829db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
198363eeaf38SJesse Barnes 		/* pipestat has already been acked */
198463eeaf38SJesse Barnes 	}
198563eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1986a70491ccSJoe Perches 		pr_err("instruction error\n");
1987a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1988050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1989050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1990a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
199163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
199263eeaf38SJesse Barnes 
1993a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1994a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1995a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
199663eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
19973143a2bfSChris Wilson 			POSTING_READ(IPEIR);
199863eeaf38SJesse Barnes 		} else {
199963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
200063eeaf38SJesse Barnes 
2001a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2002a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2003a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2004a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
200563eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20063143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
200763eeaf38SJesse Barnes 		}
200863eeaf38SJesse Barnes 	}
200963eeaf38SJesse Barnes 
201063eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
20113143a2bfSChris Wilson 	POSTING_READ(EIR);
201263eeaf38SJesse Barnes 	eir = I915_READ(EIR);
201363eeaf38SJesse Barnes 	if (eir) {
201463eeaf38SJesse Barnes 		/*
201563eeaf38SJesse Barnes 		 * some errors might have become stuck,
201663eeaf38SJesse Barnes 		 * mask them.
201763eeaf38SJesse Barnes 		 */
201863eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
201963eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
202063eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
202163eeaf38SJesse Barnes 	}
202235aed2e6SChris Wilson }
202335aed2e6SChris Wilson 
202435aed2e6SChris Wilson /**
202535aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
202635aed2e6SChris Wilson  * @dev: drm device
202735aed2e6SChris Wilson  *
202835aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
202935aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
203035aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
203135aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
203235aed2e6SChris Wilson  * of a ring dump etc.).
203335aed2e6SChris Wilson  */
2034527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
203535aed2e6SChris Wilson {
203635aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
203735aed2e6SChris Wilson 
203835aed2e6SChris Wilson 	i915_capture_error_state(dev);
203935aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
20408a905236SJesse Barnes 
2041ba1234d1SBen Gamari 	if (wedged) {
2042f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2043f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2044ba1234d1SBen Gamari 
204511ed50ecSBen Gamari 		/*
204617e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
204717e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
204817e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
204917e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
205017e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
205117e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
205217e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
205317e1df07SDaniel Vetter 		 *
205417e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
205517e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
205617e1df07SDaniel Vetter 		 * counter atomic_t.
205711ed50ecSBen Gamari 		 */
205817e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
205911ed50ecSBen Gamari 	}
206011ed50ecSBen Gamari 
2061122f46baSDaniel Vetter 	/*
2062122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
2063122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
2064122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2065122f46baSDaniel Vetter 	 * code will deadlock.
2066122f46baSDaniel Vetter 	 */
2067122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
20688a905236SJesse Barnes }
20698a905236SJesse Barnes 
207021ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
20714e5359cdSSimon Farnsworth {
20724e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
20734e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
20744e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
207505394f39SChris Wilson 	struct drm_i915_gem_object *obj;
20764e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
20774e5359cdSSimon Farnsworth 	unsigned long flags;
20784e5359cdSSimon Farnsworth 	bool stall_detected;
20794e5359cdSSimon Farnsworth 
20804e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
20814e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
20824e5359cdSSimon Farnsworth 		return;
20834e5359cdSSimon Farnsworth 
20844e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
20854e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
20864e5359cdSSimon Farnsworth 
2087e7d841caSChris Wilson 	if (work == NULL ||
2088e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2089e7d841caSChris Wilson 	    !work->enable_stall_check) {
20904e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
20914e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
20924e5359cdSSimon Farnsworth 		return;
20934e5359cdSSimon Farnsworth 	}
20944e5359cdSSimon Farnsworth 
20954e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
209605394f39SChris Wilson 	obj = work->pending_flip_obj;
2097a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
20989db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2099446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2100f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
21014e5359cdSSimon Farnsworth 	} else {
21029db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
2103f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
210401f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
21054e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
21064e5359cdSSimon Farnsworth 	}
21074e5359cdSSimon Farnsworth 
21084e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
21094e5359cdSSimon Farnsworth 
21104e5359cdSSimon Farnsworth 	if (stall_detected) {
21114e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
21124e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
21134e5359cdSSimon Farnsworth 	}
21144e5359cdSSimon Farnsworth }
21154e5359cdSSimon Farnsworth 
211642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
211742f52ef8SKeith Packard  * we use as a pipe index
211842f52ef8SKeith Packard  */
2119f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
21200a3e67a4SJesse Barnes {
21210a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2122e9d21d7fSKeith Packard 	unsigned long irqflags;
212371e0ffa5SJesse Barnes 
21245eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
212571e0ffa5SJesse Barnes 		return -EINVAL;
21260a3e67a4SJesse Barnes 
21271ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2128f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
21297c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21307c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
21310a3e67a4SJesse Barnes 	else
21327c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21337c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
21348692d00eSChris Wilson 
21358692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
21368692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
21376b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
21381ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
21398692d00eSChris Wilson 
21400a3e67a4SJesse Barnes 	return 0;
21410a3e67a4SJesse Barnes }
21420a3e67a4SJesse Barnes 
2143f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2144f796cf8fSJesse Barnes {
2145f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2146f796cf8fSJesse Barnes 	unsigned long irqflags;
2147b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
214840da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2149f796cf8fSJesse Barnes 
2150f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2151f796cf8fSJesse Barnes 		return -EINVAL;
2152f796cf8fSJesse Barnes 
2153f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2154b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2155b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2156b1f14ad0SJesse Barnes 
2157b1f14ad0SJesse Barnes 	return 0;
2158b1f14ad0SJesse Barnes }
2159b1f14ad0SJesse Barnes 
21607e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
21617e231dbeSJesse Barnes {
21627e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21637e231dbeSJesse Barnes 	unsigned long irqflags;
216431acc7f5SJesse Barnes 	u32 imr;
21657e231dbeSJesse Barnes 
21667e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
21677e231dbeSJesse Barnes 		return -EINVAL;
21687e231dbeSJesse Barnes 
21697e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
21707e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
21713b6c42e8SDaniel Vetter 	if (pipe == PIPE_A)
21727e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
217331acc7f5SJesse Barnes 	else
21747e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
21757e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
217631acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
217731acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
21787e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
21797e231dbeSJesse Barnes 
21807e231dbeSJesse Barnes 	return 0;
21817e231dbeSJesse Barnes }
21827e231dbeSJesse Barnes 
2183abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2184abd58f01SBen Widawsky {
2185abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2186abd58f01SBen Widawsky 	unsigned long irqflags;
2187abd58f01SBen Widawsky 	uint32_t imr;
2188abd58f01SBen Widawsky 
2189abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2190abd58f01SBen Widawsky 		return -EINVAL;
2191abd58f01SBen Widawsky 
2192abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2193abd58f01SBen Widawsky 	imr = I915_READ(GEN8_DE_PIPE_IMR(pipe));
2194abd58f01SBen Widawsky 	if ((imr & GEN8_PIPE_VBLANK) == 1) {
2195abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), imr & ~GEN8_PIPE_VBLANK);
2196abd58f01SBen Widawsky 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2197abd58f01SBen Widawsky 	}
2198abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2199abd58f01SBen Widawsky 	return 0;
2200abd58f01SBen Widawsky }
2201abd58f01SBen Widawsky 
220242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
220342f52ef8SKeith Packard  * we use as a pipe index
220442f52ef8SKeith Packard  */
2205f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
22060a3e67a4SJesse Barnes {
22070a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2208e9d21d7fSKeith Packard 	unsigned long irqflags;
22090a3e67a4SJesse Barnes 
22101ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22118692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
22126b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
22138692d00eSChris Wilson 
22147c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
22157c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
22167c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
22171ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22180a3e67a4SJesse Barnes }
22190a3e67a4SJesse Barnes 
2220f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2221f796cf8fSJesse Barnes {
2222f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2223f796cf8fSJesse Barnes 	unsigned long irqflags;
2224b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
222540da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2226f796cf8fSJesse Barnes 
2227f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2228b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2229b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2230b1f14ad0SJesse Barnes }
2231b1f14ad0SJesse Barnes 
22327e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
22337e231dbeSJesse Barnes {
22347e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22357e231dbeSJesse Barnes 	unsigned long irqflags;
223631acc7f5SJesse Barnes 	u32 imr;
22377e231dbeSJesse Barnes 
22387e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
223931acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
224031acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
22417e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
22423b6c42e8SDaniel Vetter 	if (pipe == PIPE_A)
22437e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
224431acc7f5SJesse Barnes 	else
22457e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22467e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
22477e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22487e231dbeSJesse Barnes }
22497e231dbeSJesse Barnes 
2250abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2251abd58f01SBen Widawsky {
2252abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2253abd58f01SBen Widawsky 	unsigned long irqflags;
2254abd58f01SBen Widawsky 	uint32_t imr;
2255abd58f01SBen Widawsky 
2256abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2257abd58f01SBen Widawsky 		return;
2258abd58f01SBen Widawsky 
2259abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2260abd58f01SBen Widawsky 	imr = I915_READ(GEN8_DE_PIPE_IMR(pipe));
2261abd58f01SBen Widawsky 	if ((imr & GEN8_PIPE_VBLANK) == 0) {
2262abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), imr | GEN8_PIPE_VBLANK);
2263abd58f01SBen Widawsky 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2264abd58f01SBen Widawsky 	}
2265abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2266abd58f01SBen Widawsky }
2267abd58f01SBen Widawsky 
2268893eead0SChris Wilson static u32
2269893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2270852835f3SZou Nan hai {
2271893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2272893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2273893eead0SChris Wilson }
2274893eead0SChris Wilson 
22759107e9d2SChris Wilson static bool
22769107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2277893eead0SChris Wilson {
22789107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
22799107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2280f65d9421SBen Gamari }
2281f65d9421SBen Gamari 
22826274f212SChris Wilson static struct intel_ring_buffer *
22836274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2284a24a11e6SChris Wilson {
2285a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
22866274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2287a24a11e6SChris Wilson 
2288a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2289a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2290a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
22916274f212SChris Wilson 		return NULL;
2292a24a11e6SChris Wilson 
2293a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2294a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2295a24a11e6SChris Wilson 	 */
22966274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2297a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2298a24a11e6SChris Wilson 	do {
2299a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2300a24a11e6SChris Wilson 		if (cmd == ipehr)
2301a24a11e6SChris Wilson 			break;
2302a24a11e6SChris Wilson 
2303a24a11e6SChris Wilson 		acthd -= 4;
2304a24a11e6SChris Wilson 		if (acthd < acthd_min)
23056274f212SChris Wilson 			return NULL;
2306a24a11e6SChris Wilson 	} while (1);
2307a24a11e6SChris Wilson 
23086274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
23096274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2310a24a11e6SChris Wilson }
2311a24a11e6SChris Wilson 
23126274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
23136274f212SChris Wilson {
23146274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
23156274f212SChris Wilson 	struct intel_ring_buffer *signaller;
23166274f212SChris Wilson 	u32 seqno, ctl;
23176274f212SChris Wilson 
23186274f212SChris Wilson 	ring->hangcheck.deadlock = true;
23196274f212SChris Wilson 
23206274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
23216274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
23226274f212SChris Wilson 		return -1;
23236274f212SChris Wilson 
23246274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
23256274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
23266274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
23276274f212SChris Wilson 		return -1;
23286274f212SChris Wilson 
23296274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
23306274f212SChris Wilson }
23316274f212SChris Wilson 
23326274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
23336274f212SChris Wilson {
23346274f212SChris Wilson 	struct intel_ring_buffer *ring;
23356274f212SChris Wilson 	int i;
23366274f212SChris Wilson 
23376274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
23386274f212SChris Wilson 		ring->hangcheck.deadlock = false;
23396274f212SChris Wilson }
23406274f212SChris Wilson 
2341ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2342ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
23431ec14ad3SChris Wilson {
23441ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
23451ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
23469107e9d2SChris Wilson 	u32 tmp;
23479107e9d2SChris Wilson 
23486274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2349f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
23506274f212SChris Wilson 
23519107e9d2SChris Wilson 	if (IS_GEN2(dev))
2352f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
23539107e9d2SChris Wilson 
23549107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
23559107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
23569107e9d2SChris Wilson 	 * and break the hang. This should work on
23579107e9d2SChris Wilson 	 * all but the second generation chipsets.
23589107e9d2SChris Wilson 	 */
23599107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
23601ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
23611ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
23621ec14ad3SChris Wilson 			  ring->name);
236309e14bf3SChris Wilson 		i915_handle_error(dev, false);
23641ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2365f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
23661ec14ad3SChris Wilson 	}
2367a24a11e6SChris Wilson 
23686274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
23696274f212SChris Wilson 		switch (semaphore_passed(ring)) {
23706274f212SChris Wilson 		default:
2371f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
23726274f212SChris Wilson 		case 1:
2373a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
2374a24a11e6SChris Wilson 				  ring->name);
237509e14bf3SChris Wilson 			i915_handle_error(dev, false);
2376a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2377f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
23786274f212SChris Wilson 		case 0:
2379f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
23806274f212SChris Wilson 		}
23819107e9d2SChris Wilson 	}
23829107e9d2SChris Wilson 
2383f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2384a24a11e6SChris Wilson }
2385d1e61e7fSChris Wilson 
2386f65d9421SBen Gamari /**
2387f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
238805407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
238905407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
239005407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
239105407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
239205407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2393f65d9421SBen Gamari  */
2394a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2395f65d9421SBen Gamari {
2396f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2397f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2398b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2399b4519513SChris Wilson 	int i;
240005407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
24019107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
24029107e9d2SChris Wilson #define BUSY 1
24039107e9d2SChris Wilson #define KICK 5
24049107e9d2SChris Wilson #define HUNG 20
24059107e9d2SChris Wilson #define FIRE 30
2406893eead0SChris Wilson 
24073e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
24083e0dc6b0SBen Widawsky 		return;
24093e0dc6b0SBen Widawsky 
2410b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
241105407ff8SMika Kuoppala 		u32 seqno, acthd;
24129107e9d2SChris Wilson 		bool busy = true;
2413b4519513SChris Wilson 
24146274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
24156274f212SChris Wilson 
241605407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
241705407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
241805407ff8SMika Kuoppala 
241905407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
24209107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2421da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2422da661464SMika Kuoppala 
24239107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
24249107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2425094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2426f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
24279107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
24289107e9d2SChris Wilson 								  ring->name);
2429f4adcd24SDaniel Vetter 						else
2430f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2431f4adcd24SDaniel Vetter 								 ring->name);
24329107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2433094f9a54SChris Wilson 					}
2434094f9a54SChris Wilson 					/* Safeguard against driver failure */
2435094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
24369107e9d2SChris Wilson 				} else
24379107e9d2SChris Wilson 					busy = false;
243805407ff8SMika Kuoppala 			} else {
24396274f212SChris Wilson 				/* We always increment the hangcheck score
24406274f212SChris Wilson 				 * if the ring is busy and still processing
24416274f212SChris Wilson 				 * the same request, so that no single request
24426274f212SChris Wilson 				 * can run indefinitely (such as a chain of
24436274f212SChris Wilson 				 * batches). The only time we do not increment
24446274f212SChris Wilson 				 * the hangcheck score on this ring, if this
24456274f212SChris Wilson 				 * ring is in a legitimate wait for another
24466274f212SChris Wilson 				 * ring. In that case the waiting ring is a
24476274f212SChris Wilson 				 * victim and we want to be sure we catch the
24486274f212SChris Wilson 				 * right culprit. Then every time we do kick
24496274f212SChris Wilson 				 * the ring, add a small increment to the
24506274f212SChris Wilson 				 * score so that we can catch a batch that is
24516274f212SChris Wilson 				 * being repeatedly kicked and so responsible
24526274f212SChris Wilson 				 * for stalling the machine.
24539107e9d2SChris Wilson 				 */
2454ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2455ad8beaeaSMika Kuoppala 								    acthd);
2456ad8beaeaSMika Kuoppala 
2457ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2458da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2459f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
24606274f212SChris Wilson 					break;
2461f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2462ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
24636274f212SChris Wilson 					break;
2464f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2465ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
24666274f212SChris Wilson 					break;
2467f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2468ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
24696274f212SChris Wilson 					stuck[i] = true;
24706274f212SChris Wilson 					break;
24716274f212SChris Wilson 				}
247205407ff8SMika Kuoppala 			}
24739107e9d2SChris Wilson 		} else {
2474da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2475da661464SMika Kuoppala 
24769107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
24779107e9d2SChris Wilson 			 * attempts across multiple batches.
24789107e9d2SChris Wilson 			 */
24799107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
24809107e9d2SChris Wilson 				ring->hangcheck.score--;
2481cbb465e7SChris Wilson 		}
2482f65d9421SBen Gamari 
248305407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
248405407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
24859107e9d2SChris Wilson 		busy_count += busy;
248605407ff8SMika Kuoppala 	}
248705407ff8SMika Kuoppala 
248805407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
24899107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2490b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
249105407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2492a43adf07SChris Wilson 				 ring->name);
2493a43adf07SChris Wilson 			rings_hung++;
249405407ff8SMika Kuoppala 		}
249505407ff8SMika Kuoppala 	}
249605407ff8SMika Kuoppala 
249705407ff8SMika Kuoppala 	if (rings_hung)
249805407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
249905407ff8SMika Kuoppala 
250005407ff8SMika Kuoppala 	if (busy_count)
250105407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
250205407ff8SMika Kuoppala 		 * being added */
250310cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
250410cd45b6SMika Kuoppala }
250510cd45b6SMika Kuoppala 
250610cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
250710cd45b6SMika Kuoppala {
250810cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
250910cd45b6SMika Kuoppala 	if (!i915_enable_hangcheck)
251010cd45b6SMika Kuoppala 		return;
251110cd45b6SMika Kuoppala 
251299584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
251310cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2514f65d9421SBen Gamari }
2515f65d9421SBen Gamari 
251691738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
251791738a95SPaulo Zanoni {
251891738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
251991738a95SPaulo Zanoni 
252091738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
252191738a95SPaulo Zanoni 		return;
252291738a95SPaulo Zanoni 
252391738a95SPaulo Zanoni 	/* south display irq */
252491738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
252591738a95SPaulo Zanoni 	/*
252691738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
252791738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
252891738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
252991738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
253091738a95SPaulo Zanoni 	 */
253191738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
253291738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
253391738a95SPaulo Zanoni }
253491738a95SPaulo Zanoni 
2535d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2536d18ea1b5SDaniel Vetter {
2537d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2538d18ea1b5SDaniel Vetter 
2539d18ea1b5SDaniel Vetter 	/* and GT */
2540d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2541d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2542d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2543d18ea1b5SDaniel Vetter 
2544d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2545d18ea1b5SDaniel Vetter 		/* and PM */
2546d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2547d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2548d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2549d18ea1b5SDaniel Vetter 	}
2550d18ea1b5SDaniel Vetter }
2551d18ea1b5SDaniel Vetter 
2552c0e09200SDave Airlie /* drm_dma.h hooks
2553c0e09200SDave Airlie */
2554f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2555036a4a7dSZhenyu Wang {
2556036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2557036a4a7dSZhenyu Wang 
25584697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
25594697995bSJesse Barnes 
2560036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2561bdfcdb63SDaniel Vetter 
2562036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2563036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
25643143a2bfSChris Wilson 	POSTING_READ(DEIER);
2565036a4a7dSZhenyu Wang 
2566d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2567c650156aSZhenyu Wang 
256891738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
25697d99163dSBen Widawsky }
25707d99163dSBen Widawsky 
25717e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
25727e231dbeSJesse Barnes {
25737e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25747e231dbeSJesse Barnes 	int pipe;
25757e231dbeSJesse Barnes 
25767e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
25777e231dbeSJesse Barnes 
25787e231dbeSJesse Barnes 	/* VLV magic */
25797e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
25807e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
25817e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
25827e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
25837e231dbeSJesse Barnes 
25847e231dbeSJesse Barnes 	/* and GT */
25857e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
25867e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2587d18ea1b5SDaniel Vetter 
2588d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
25897e231dbeSJesse Barnes 
25907e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
25917e231dbeSJesse Barnes 
25927e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
25937e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
25947e231dbeSJesse Barnes 	for_each_pipe(pipe)
25957e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
25967e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
25977e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
25987e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
25997e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
26007e231dbeSJesse Barnes }
26017e231dbeSJesse Barnes 
2602abd58f01SBen Widawsky static void gen8_irq_preinstall(struct drm_device *dev)
2603abd58f01SBen Widawsky {
2604abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2605abd58f01SBen Widawsky 	int pipe;
2606abd58f01SBen Widawsky 
2607abd58f01SBen Widawsky 	atomic_set(&dev_priv->irq_received, 0);
2608abd58f01SBen Widawsky 
2609abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2610abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2611abd58f01SBen Widawsky 
2612abd58f01SBen Widawsky 	/* IIR can theoretically queue up two events. Be paranoid */
2613abd58f01SBen Widawsky #define GEN8_IRQ_INIT_NDX(type, which) do { \
2614abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
2615abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IMR(which)); \
2616abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER(which), 0); \
2617abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2618abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IIR(which)); \
2619abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2620abd58f01SBen Widawsky 	} while (0)
2621abd58f01SBen Widawsky 
2622abd58f01SBen Widawsky #define GEN8_IRQ_INIT(type) do { \
2623abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
2624abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IMR); \
2625abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER, 0); \
2626abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2627abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IIR); \
2628abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2629abd58f01SBen Widawsky 	} while (0)
2630abd58f01SBen Widawsky 
2631abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 0);
2632abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 1);
2633abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 2);
2634abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 3);
2635abd58f01SBen Widawsky 
2636abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2637abd58f01SBen Widawsky 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe);
2638abd58f01SBen Widawsky 	}
2639abd58f01SBen Widawsky 
2640abd58f01SBen Widawsky 	GEN8_IRQ_INIT(DE_PORT);
2641abd58f01SBen Widawsky 	GEN8_IRQ_INIT(DE_MISC);
2642abd58f01SBen Widawsky 	GEN8_IRQ_INIT(PCU);
2643abd58f01SBen Widawsky #undef GEN8_IRQ_INIT
2644abd58f01SBen Widawsky #undef GEN8_IRQ_INIT_NDX
2645abd58f01SBen Widawsky 
2646abd58f01SBen Widawsky 	POSTING_READ(GEN8_PCU_IIR);
2647abd58f01SBen Widawsky }
2648abd58f01SBen Widawsky 
264982a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
265082a28bcfSDaniel Vetter {
265182a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
265282a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
265382a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2654fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
265582a28bcfSDaniel Vetter 
265682a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2657fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
265882a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2659cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2660fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
266182a28bcfSDaniel Vetter 	} else {
2662fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
266382a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2664cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2665fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
266682a28bcfSDaniel Vetter 	}
266782a28bcfSDaniel Vetter 
2668fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
266982a28bcfSDaniel Vetter 
26707fe0b973SKeith Packard 	/*
26717fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
26727fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
26737fe0b973SKeith Packard 	 *
26747fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
26757fe0b973SKeith Packard 	 */
26767fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
26777fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
26787fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
26797fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
26807fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
26817fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
26827fe0b973SKeith Packard }
26837fe0b973SKeith Packard 
2684d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2685d46da437SPaulo Zanoni {
2686d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
268782a28bcfSDaniel Vetter 	u32 mask;
2688d46da437SPaulo Zanoni 
2689692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2690692a04cfSDaniel Vetter 		return;
2691692a04cfSDaniel Vetter 
26928664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
26938664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2694de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
26958664281bSPaulo Zanoni 	} else {
26968664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
26978664281bSPaulo Zanoni 
26988664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
26998664281bSPaulo Zanoni 	}
2700ab5c608bSBen Widawsky 
2701d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2702d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2703d46da437SPaulo Zanoni }
2704d46da437SPaulo Zanoni 
27050a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
27060a9a8c91SDaniel Vetter {
27070a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
27080a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
27090a9a8c91SDaniel Vetter 
27100a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
27110a9a8c91SDaniel Vetter 
27120a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
2713040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
27140a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
271535a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
271635a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
27170a9a8c91SDaniel Vetter 	}
27180a9a8c91SDaniel Vetter 
27190a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
27200a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
27210a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
27220a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
27230a9a8c91SDaniel Vetter 	} else {
27240a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
27250a9a8c91SDaniel Vetter 	}
27260a9a8c91SDaniel Vetter 
27270a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
27280a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
27290a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
27300a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
27310a9a8c91SDaniel Vetter 
27320a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
27330a9a8c91SDaniel Vetter 		pm_irqs |= GEN6_PM_RPS_EVENTS;
27340a9a8c91SDaniel Vetter 
27350a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
27360a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
27370a9a8c91SDaniel Vetter 
2738605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
27390a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2740605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
27410a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
27420a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
27430a9a8c91SDaniel Vetter 	}
27440a9a8c91SDaniel Vetter }
27450a9a8c91SDaniel Vetter 
2746f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2747036a4a7dSZhenyu Wang {
27484bc9d430SDaniel Vetter 	unsigned long irqflags;
2749036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
27508e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
27518e76f8dcSPaulo Zanoni 
27528e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
27538e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
27548e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
27558e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
27568e76f8dcSPaulo Zanoni 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
27578e76f8dcSPaulo Zanoni 				DE_ERR_INT_IVB);
27588e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
27598e76f8dcSPaulo Zanoni 			      DE_PIPEA_VBLANK_IVB);
27608e76f8dcSPaulo Zanoni 
27618e76f8dcSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
27628e76f8dcSPaulo Zanoni 	} else {
27638e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2764ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
27655b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
27665b3a856bSDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
27675b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
27685b3a856bSDaniel Vetter 				DE_POISON);
27698e76f8dcSPaulo Zanoni 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
27708e76f8dcSPaulo Zanoni 	}
2771036a4a7dSZhenyu Wang 
27721ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2773036a4a7dSZhenyu Wang 
2774036a4a7dSZhenyu Wang 	/* should always can generate irq */
2775036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
27761ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
27778e76f8dcSPaulo Zanoni 	I915_WRITE(DEIER, display_mask | extra_mask);
27783143a2bfSChris Wilson 	POSTING_READ(DEIER);
2779036a4a7dSZhenyu Wang 
27800a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
2781036a4a7dSZhenyu Wang 
2782d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
27837fe0b973SKeith Packard 
2784f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
27856005ce42SDaniel Vetter 		/* Enable PCU event interrupts
27866005ce42SDaniel Vetter 		 *
27876005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
27884bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
27894bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
27904bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2791f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
27924bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2793f97108d1SJesse Barnes 	}
2794f97108d1SJesse Barnes 
2795036a4a7dSZhenyu Wang 	return 0;
2796036a4a7dSZhenyu Wang }
2797036a4a7dSZhenyu Wang 
27987e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
27997e231dbeSJesse Barnes {
28007e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
28017e231dbeSJesse Barnes 	u32 enable_mask;
2802379ef82dSDaniel Vetter 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV |
2803379ef82dSDaniel Vetter 		PIPE_CRC_DONE_ENABLE;
2804b79480baSDaniel Vetter 	unsigned long irqflags;
28057e231dbeSJesse Barnes 
28067e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
280731acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
280831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
280931acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
28107e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
28117e231dbeSJesse Barnes 
281231acc7f5SJesse Barnes 	/*
281331acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
281431acc7f5SJesse Barnes 	 * toggle them based on usage.
281531acc7f5SJesse Barnes 	 */
281631acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
281731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
281831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
28197e231dbeSJesse Barnes 
282020afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
282120afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
282220afbda2SDaniel Vetter 
28237e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
28247e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
28257e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28267e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
28277e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
28287e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
28297e231dbeSJesse Barnes 
2830b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2831b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2832b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
28333b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_enable);
28343b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_EVENT_ENABLE);
28353b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_enable);
2836b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
283731acc7f5SJesse Barnes 
28387e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28397e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28407e231dbeSJesse Barnes 
28410a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
28427e231dbeSJesse Barnes 
28437e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
28447e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
28457e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
28467e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
28477e231dbeSJesse Barnes #endif
28487e231dbeSJesse Barnes 
28497e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
285020afbda2SDaniel Vetter 
285120afbda2SDaniel Vetter 	return 0;
285220afbda2SDaniel Vetter }
285320afbda2SDaniel Vetter 
2854abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
2855abd58f01SBen Widawsky {
2856abd58f01SBen Widawsky 	int i;
2857abd58f01SBen Widawsky 
2858abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
2859abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
2860abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
2861abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
2862abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
2863abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
2864abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
2865abd58f01SBen Widawsky 		0,
2866abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT
2867abd58f01SBen Widawsky 		};
2868abd58f01SBen Widawsky 
2869abd58f01SBen Widawsky 	for (i = 0; i < ARRAY_SIZE(gt_interrupts); i++) {
2870abd58f01SBen Widawsky 		u32 tmp = I915_READ(GEN8_GT_IIR(i));
2871abd58f01SBen Widawsky 		if (tmp)
2872abd58f01SBen Widawsky 			DRM_ERROR("Interrupt (%d) should have been masked in pre-install 0x%08x\n",
2873abd58f01SBen Widawsky 				  i, tmp);
2874abd58f01SBen Widawsky 		I915_WRITE(GEN8_GT_IMR(i), ~gt_interrupts[i]);
2875abd58f01SBen Widawsky 		I915_WRITE(GEN8_GT_IER(i), gt_interrupts[i]);
2876abd58f01SBen Widawsky 	}
2877abd58f01SBen Widawsky 	POSTING_READ(GEN8_GT_IER(0));
2878abd58f01SBen Widawsky }
2879abd58f01SBen Widawsky 
2880abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
2881abd58f01SBen Widawsky {
2882abd58f01SBen Widawsky 	struct drm_device *dev = dev_priv->dev;
2883abd58f01SBen Widawsky 	uint32_t de_pipe_enables = GEN8_PIPE_FLIP_DONE |
2884abd58f01SBen Widawsky 				   GEN8_PIPE_VBLANK |
288530100f2bSDaniel Vetter 				   GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2886abd58f01SBen Widawsky 	int pipe;
2887abd58f01SBen Widawsky 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_enables;
2888abd58f01SBen Widawsky 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_enables;
2889abd58f01SBen Widawsky 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_enables;
2890abd58f01SBen Widawsky 
2891abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2892abd58f01SBen Widawsky 		u32 tmp = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2893abd58f01SBen Widawsky 		if (tmp)
2894abd58f01SBen Widawsky 			DRM_ERROR("Interrupt (%d) should have been masked in pre-install 0x%08x\n",
2895abd58f01SBen Widawsky 				  pipe, tmp);
2896abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2897abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IER(pipe), de_pipe_enables);
2898abd58f01SBen Widawsky 	}
2899abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_ISR(0));
2900abd58f01SBen Widawsky 
2901*6d766f02SDaniel Vetter 	I915_WRITE(GEN8_DE_PORT_IMR, ~GEN8_AUX_CHANNEL_A);
2902*6d766f02SDaniel Vetter 	I915_WRITE(GEN8_DE_PORT_IER, GEN8_AUX_CHANNEL_A);
2903abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PORT_IER);
2904abd58f01SBen Widawsky }
2905abd58f01SBen Widawsky 
2906abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
2907abd58f01SBen Widawsky {
2908abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2909abd58f01SBen Widawsky 
2910abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
2911abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
2912abd58f01SBen Widawsky 
2913abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
2914abd58f01SBen Widawsky 
2915abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
2916abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2917abd58f01SBen Widawsky 
2918abd58f01SBen Widawsky 	return 0;
2919abd58f01SBen Widawsky }
2920abd58f01SBen Widawsky 
2921abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
2922abd58f01SBen Widawsky {
2923abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2924abd58f01SBen Widawsky 	int pipe;
2925abd58f01SBen Widawsky 
2926abd58f01SBen Widawsky 	if (!dev_priv)
2927abd58f01SBen Widawsky 		return;
2928abd58f01SBen Widawsky 
2929abd58f01SBen Widawsky 	atomic_set(&dev_priv->irq_received, 0);
2930abd58f01SBen Widawsky 
2931abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2932abd58f01SBen Widawsky 
2933abd58f01SBen Widawsky #define GEN8_IRQ_FINI_NDX(type, which) do { \
2934abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
2935abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER(which), 0); \
2936abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2937abd58f01SBen Widawsky 	} while (0)
2938abd58f01SBen Widawsky 
2939abd58f01SBen Widawsky #define GEN8_IRQ_FINI(type) do { \
2940abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
2941abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER, 0); \
2942abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2943abd58f01SBen Widawsky 	} while (0)
2944abd58f01SBen Widawsky 
2945abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 0);
2946abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 1);
2947abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 2);
2948abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 3);
2949abd58f01SBen Widawsky 
2950abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2951abd58f01SBen Widawsky 		GEN8_IRQ_FINI_NDX(DE_PIPE, pipe);
2952abd58f01SBen Widawsky 	}
2953abd58f01SBen Widawsky 
2954abd58f01SBen Widawsky 	GEN8_IRQ_FINI(DE_PORT);
2955abd58f01SBen Widawsky 	GEN8_IRQ_FINI(DE_MISC);
2956abd58f01SBen Widawsky 	GEN8_IRQ_FINI(PCU);
2957abd58f01SBen Widawsky #undef GEN8_IRQ_FINI
2958abd58f01SBen Widawsky #undef GEN8_IRQ_FINI_NDX
2959abd58f01SBen Widawsky 
2960abd58f01SBen Widawsky 	POSTING_READ(GEN8_PCU_IIR);
2961abd58f01SBen Widawsky }
2962abd58f01SBen Widawsky 
29637e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
29647e231dbeSJesse Barnes {
29657e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29667e231dbeSJesse Barnes 	int pipe;
29677e231dbeSJesse Barnes 
29687e231dbeSJesse Barnes 	if (!dev_priv)
29697e231dbeSJesse Barnes 		return;
29707e231dbeSJesse Barnes 
2971ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2972ac4c16c5SEgbert Eich 
29737e231dbeSJesse Barnes 	for_each_pipe(pipe)
29747e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
29757e231dbeSJesse Barnes 
29767e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
29777e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
29787e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
29797e231dbeSJesse Barnes 	for_each_pipe(pipe)
29807e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
29817e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
29827e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
29837e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
29847e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
29857e231dbeSJesse Barnes }
29867e231dbeSJesse Barnes 
2987f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2988036a4a7dSZhenyu Wang {
2989036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29904697995bSJesse Barnes 
29914697995bSJesse Barnes 	if (!dev_priv)
29924697995bSJesse Barnes 		return;
29934697995bSJesse Barnes 
2994ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2995ac4c16c5SEgbert Eich 
2996036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2997036a4a7dSZhenyu Wang 
2998036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2999036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
3000036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
30018664281bSPaulo Zanoni 	if (IS_GEN7(dev))
30028664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
3003036a4a7dSZhenyu Wang 
3004036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
3005036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
3006036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
3007192aac1fSKeith Packard 
3008ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
3009ab5c608bSBen Widawsky 		return;
3010ab5c608bSBen Widawsky 
3011192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
3012192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
3013192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
30148664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
30158664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
3016036a4a7dSZhenyu Wang }
3017036a4a7dSZhenyu Wang 
3018c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3019c2798b19SChris Wilson {
3020c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3021c2798b19SChris Wilson 	int pipe;
3022c2798b19SChris Wilson 
3023c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3024c2798b19SChris Wilson 
3025c2798b19SChris Wilson 	for_each_pipe(pipe)
3026c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3027c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3028c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3029c2798b19SChris Wilson 	POSTING_READ16(IER);
3030c2798b19SChris Wilson }
3031c2798b19SChris Wilson 
3032c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3033c2798b19SChris Wilson {
3034c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3035379ef82dSDaniel Vetter 	unsigned long irqflags;
3036c2798b19SChris Wilson 
3037c2798b19SChris Wilson 	I915_WRITE16(EMR,
3038c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3039c2798b19SChris Wilson 
3040c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3041c2798b19SChris Wilson 	dev_priv->irq_mask =
3042c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3043c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3044c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3045c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3046c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3047c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3048c2798b19SChris Wilson 
3049c2798b19SChris Wilson 	I915_WRITE16(IER,
3050c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3051c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3052c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3053c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3054c2798b19SChris Wilson 	POSTING_READ16(IER);
3055c2798b19SChris Wilson 
3056379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3057379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3058379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
30593b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_ENABLE);
30603b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_ENABLE);
3061379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3062379ef82dSDaniel Vetter 
3063c2798b19SChris Wilson 	return 0;
3064c2798b19SChris Wilson }
3065c2798b19SChris Wilson 
306690a72f87SVille Syrjälä /*
306790a72f87SVille Syrjälä  * Returns true when a page flip has completed.
306890a72f87SVille Syrjälä  */
306990a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
307090a72f87SVille Syrjälä 			       int pipe, u16 iir)
307190a72f87SVille Syrjälä {
307290a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
307390a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
307490a72f87SVille Syrjälä 
307590a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
307690a72f87SVille Syrjälä 		return false;
307790a72f87SVille Syrjälä 
307890a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
307990a72f87SVille Syrjälä 		return false;
308090a72f87SVille Syrjälä 
308190a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
308290a72f87SVille Syrjälä 
308390a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
308490a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
308590a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
308690a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
308790a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
308890a72f87SVille Syrjälä 	 */
308990a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
309090a72f87SVille Syrjälä 		return false;
309190a72f87SVille Syrjälä 
309290a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
309390a72f87SVille Syrjälä 
309490a72f87SVille Syrjälä 	return true;
309590a72f87SVille Syrjälä }
309690a72f87SVille Syrjälä 
3097ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3098c2798b19SChris Wilson {
3099c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3100c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3101c2798b19SChris Wilson 	u16 iir, new_iir;
3102c2798b19SChris Wilson 	u32 pipe_stats[2];
3103c2798b19SChris Wilson 	unsigned long irqflags;
3104c2798b19SChris Wilson 	int pipe;
3105c2798b19SChris Wilson 	u16 flip_mask =
3106c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3107c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3108c2798b19SChris Wilson 
3109c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3110c2798b19SChris Wilson 
3111c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3112c2798b19SChris Wilson 	if (iir == 0)
3113c2798b19SChris Wilson 		return IRQ_NONE;
3114c2798b19SChris Wilson 
3115c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3116c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3117c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3118c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3119c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3120c2798b19SChris Wilson 		 */
3121c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3122c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3123c2798b19SChris Wilson 			i915_handle_error(dev, false);
3124c2798b19SChris Wilson 
3125c2798b19SChris Wilson 		for_each_pipe(pipe) {
3126c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3127c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3128c2798b19SChris Wilson 
3129c2798b19SChris Wilson 			/*
3130c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3131c2798b19SChris Wilson 			 */
3132c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3133c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3134c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3135c2798b19SChris Wilson 							 pipe_name(pipe));
3136c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3137c2798b19SChris Wilson 			}
3138c2798b19SChris Wilson 		}
3139c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3140c2798b19SChris Wilson 
3141c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3142c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3143c2798b19SChris Wilson 
3144d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3145c2798b19SChris Wilson 
3146c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3147c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3148c2798b19SChris Wilson 
31494356d586SDaniel Vetter 		for_each_pipe(pipe) {
31504356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
31514356d586SDaniel Vetter 			    i8xx_handle_vblank(dev, pipe, iir))
31524356d586SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3153c2798b19SChris Wilson 
31544356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3155277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
31564356d586SDaniel Vetter 		}
3157c2798b19SChris Wilson 
3158c2798b19SChris Wilson 		iir = new_iir;
3159c2798b19SChris Wilson 	}
3160c2798b19SChris Wilson 
3161c2798b19SChris Wilson 	return IRQ_HANDLED;
3162c2798b19SChris Wilson }
3163c2798b19SChris Wilson 
3164c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3165c2798b19SChris Wilson {
3166c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3167c2798b19SChris Wilson 	int pipe;
3168c2798b19SChris Wilson 
3169c2798b19SChris Wilson 	for_each_pipe(pipe) {
3170c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3171c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3172c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3173c2798b19SChris Wilson 	}
3174c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3175c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3176c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3177c2798b19SChris Wilson }
3178c2798b19SChris Wilson 
3179a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3180a266c7d5SChris Wilson {
3181a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3182a266c7d5SChris Wilson 	int pipe;
3183a266c7d5SChris Wilson 
3184a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3185a266c7d5SChris Wilson 
3186a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3187a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3188a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3189a266c7d5SChris Wilson 	}
3190a266c7d5SChris Wilson 
319100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3192a266c7d5SChris Wilson 	for_each_pipe(pipe)
3193a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3194a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3195a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3196a266c7d5SChris Wilson 	POSTING_READ(IER);
3197a266c7d5SChris Wilson }
3198a266c7d5SChris Wilson 
3199a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3200a266c7d5SChris Wilson {
3201a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
320238bde180SChris Wilson 	u32 enable_mask;
3203379ef82dSDaniel Vetter 	unsigned long irqflags;
3204a266c7d5SChris Wilson 
320538bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
320638bde180SChris Wilson 
320738bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
320838bde180SChris Wilson 	dev_priv->irq_mask =
320938bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
321038bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
321138bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
321238bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
321338bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
321438bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
321538bde180SChris Wilson 
321638bde180SChris Wilson 	enable_mask =
321738bde180SChris Wilson 		I915_ASLE_INTERRUPT |
321838bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
321938bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
322038bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
322138bde180SChris Wilson 		I915_USER_INTERRUPT;
322238bde180SChris Wilson 
3223a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
322420afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
322520afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
322620afbda2SDaniel Vetter 
3227a266c7d5SChris Wilson 		/* Enable in IER... */
3228a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3229a266c7d5SChris Wilson 		/* and unmask in IMR */
3230a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3231a266c7d5SChris Wilson 	}
3232a266c7d5SChris Wilson 
3233a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3234a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3235a266c7d5SChris Wilson 	POSTING_READ(IER);
3236a266c7d5SChris Wilson 
3237f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
323820afbda2SDaniel Vetter 
3239379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3240379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3241379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
32423b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_ENABLE);
32433b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_ENABLE);
3244379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3245379ef82dSDaniel Vetter 
324620afbda2SDaniel Vetter 	return 0;
324720afbda2SDaniel Vetter }
324820afbda2SDaniel Vetter 
324990a72f87SVille Syrjälä /*
325090a72f87SVille Syrjälä  * Returns true when a page flip has completed.
325190a72f87SVille Syrjälä  */
325290a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
325390a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
325490a72f87SVille Syrjälä {
325590a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
325690a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
325790a72f87SVille Syrjälä 
325890a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
325990a72f87SVille Syrjälä 		return false;
326090a72f87SVille Syrjälä 
326190a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
326290a72f87SVille Syrjälä 		return false;
326390a72f87SVille Syrjälä 
326490a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
326590a72f87SVille Syrjälä 
326690a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
326790a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
326890a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
326990a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
327090a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
327190a72f87SVille Syrjälä 	 */
327290a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
327390a72f87SVille Syrjälä 		return false;
327490a72f87SVille Syrjälä 
327590a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
327690a72f87SVille Syrjälä 
327790a72f87SVille Syrjälä 	return true;
327890a72f87SVille Syrjälä }
327990a72f87SVille Syrjälä 
3280ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3281a266c7d5SChris Wilson {
3282a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3283a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
32848291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3285a266c7d5SChris Wilson 	unsigned long irqflags;
328638bde180SChris Wilson 	u32 flip_mask =
328738bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
328838bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
328938bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3290a266c7d5SChris Wilson 
3291a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3292a266c7d5SChris Wilson 
3293a266c7d5SChris Wilson 	iir = I915_READ(IIR);
329438bde180SChris Wilson 	do {
329538bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
32968291ee90SChris Wilson 		bool blc_event = false;
3297a266c7d5SChris Wilson 
3298a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3299a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3300a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3301a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3302a266c7d5SChris Wilson 		 */
3303a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3304a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3305a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3306a266c7d5SChris Wilson 
3307a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3308a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3309a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3310a266c7d5SChris Wilson 
331138bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3312a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3313a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3314a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3315a266c7d5SChris Wilson 							 pipe_name(pipe));
3316a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
331738bde180SChris Wilson 				irq_received = true;
3318a266c7d5SChris Wilson 			}
3319a266c7d5SChris Wilson 		}
3320a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3321a266c7d5SChris Wilson 
3322a266c7d5SChris Wilson 		if (!irq_received)
3323a266c7d5SChris Wilson 			break;
3324a266c7d5SChris Wilson 
3325a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3326a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3327a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3328a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3329b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3330a266c7d5SChris Wilson 
3331a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3332a266c7d5SChris Wilson 				  hotplug_status);
333391d131d2SDaniel Vetter 
333410a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
333591d131d2SDaniel Vetter 
3336a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
333738bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3338a266c7d5SChris Wilson 		}
3339a266c7d5SChris Wilson 
334038bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3341a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3342a266c7d5SChris Wilson 
3343a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3344a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3345a266c7d5SChris Wilson 
3346a266c7d5SChris Wilson 		for_each_pipe(pipe) {
334738bde180SChris Wilson 			int plane = pipe;
334838bde180SChris Wilson 			if (IS_MOBILE(dev))
334938bde180SChris Wilson 				plane = !plane;
33505e2032d4SVille Syrjälä 
335190a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
335290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
335390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3354a266c7d5SChris Wilson 
3355a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3356a266c7d5SChris Wilson 				blc_event = true;
33574356d586SDaniel Vetter 
33584356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3359277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
3360a266c7d5SChris Wilson 		}
3361a266c7d5SChris Wilson 
3362a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3363a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3364a266c7d5SChris Wilson 
3365a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3366a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3367a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3368a266c7d5SChris Wilson 		 * we would never get another interrupt.
3369a266c7d5SChris Wilson 		 *
3370a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3371a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3372a266c7d5SChris Wilson 		 * another one.
3373a266c7d5SChris Wilson 		 *
3374a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3375a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3376a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3377a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3378a266c7d5SChris Wilson 		 * stray interrupts.
3379a266c7d5SChris Wilson 		 */
338038bde180SChris Wilson 		ret = IRQ_HANDLED;
3381a266c7d5SChris Wilson 		iir = new_iir;
338238bde180SChris Wilson 	} while (iir & ~flip_mask);
3383a266c7d5SChris Wilson 
3384d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
33858291ee90SChris Wilson 
3386a266c7d5SChris Wilson 	return ret;
3387a266c7d5SChris Wilson }
3388a266c7d5SChris Wilson 
3389a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3390a266c7d5SChris Wilson {
3391a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3392a266c7d5SChris Wilson 	int pipe;
3393a266c7d5SChris Wilson 
3394ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3395ac4c16c5SEgbert Eich 
3396a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3397a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3398a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3399a266c7d5SChris Wilson 	}
3400a266c7d5SChris Wilson 
340100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
340255b39755SChris Wilson 	for_each_pipe(pipe) {
340355b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3404a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
340555b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
340655b39755SChris Wilson 	}
3407a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3408a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3409a266c7d5SChris Wilson 
3410a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3411a266c7d5SChris Wilson }
3412a266c7d5SChris Wilson 
3413a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3414a266c7d5SChris Wilson {
3415a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3416a266c7d5SChris Wilson 	int pipe;
3417a266c7d5SChris Wilson 
3418a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3419a266c7d5SChris Wilson 
3420a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3421a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3422a266c7d5SChris Wilson 
3423a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3424a266c7d5SChris Wilson 	for_each_pipe(pipe)
3425a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3426a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3427a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3428a266c7d5SChris Wilson 	POSTING_READ(IER);
3429a266c7d5SChris Wilson }
3430a266c7d5SChris Wilson 
3431a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3432a266c7d5SChris Wilson {
3433a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3434bbba0a97SChris Wilson 	u32 enable_mask;
3435a266c7d5SChris Wilson 	u32 error_mask;
3436b79480baSDaniel Vetter 	unsigned long irqflags;
3437a266c7d5SChris Wilson 
3438a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3439bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3440adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3441bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3442bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3443bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3444bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3445bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3446bbba0a97SChris Wilson 
3447bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
344821ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
344921ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3450bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3451bbba0a97SChris Wilson 
3452bbba0a97SChris Wilson 	if (IS_G4X(dev))
3453bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3454a266c7d5SChris Wilson 
3455b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3456b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3457b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
34583b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_EVENT_ENABLE);
34593b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_ENABLE);
34603b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_ENABLE);
3461b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3462a266c7d5SChris Wilson 
3463a266c7d5SChris Wilson 	/*
3464a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3465a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3466a266c7d5SChris Wilson 	 */
3467a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3468a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3469a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3470a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3471a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3472a266c7d5SChris Wilson 	} else {
3473a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3474a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3475a266c7d5SChris Wilson 	}
3476a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3477a266c7d5SChris Wilson 
3478a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3479a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3480a266c7d5SChris Wilson 	POSTING_READ(IER);
3481a266c7d5SChris Wilson 
348220afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
348320afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
348420afbda2SDaniel Vetter 
3485f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
348620afbda2SDaniel Vetter 
348720afbda2SDaniel Vetter 	return 0;
348820afbda2SDaniel Vetter }
348920afbda2SDaniel Vetter 
3490bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
349120afbda2SDaniel Vetter {
349220afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3493e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3494cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
349520afbda2SDaniel Vetter 	u32 hotplug_en;
349620afbda2SDaniel Vetter 
3497b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3498b5ea2d56SDaniel Vetter 
3499bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3500bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3501bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3502adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3503e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3504cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3505cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3506cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3507a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3508a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3509a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3510a266c7d5SChris Wilson 		*/
3511a266c7d5SChris Wilson 		if (IS_G4X(dev))
3512a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
351385fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3514a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3515a266c7d5SChris Wilson 
3516a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3517a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3518a266c7d5SChris Wilson 	}
3519bac56d5bSEgbert Eich }
3520a266c7d5SChris Wilson 
3521ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3522a266c7d5SChris Wilson {
3523a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3524a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3525a266c7d5SChris Wilson 	u32 iir, new_iir;
3526a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3527a266c7d5SChris Wilson 	unsigned long irqflags;
3528a266c7d5SChris Wilson 	int irq_received;
3529a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
353021ad8330SVille Syrjälä 	u32 flip_mask =
353121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
353221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3533a266c7d5SChris Wilson 
3534a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3535a266c7d5SChris Wilson 
3536a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3537a266c7d5SChris Wilson 
3538a266c7d5SChris Wilson 	for (;;) {
35392c8ba29fSChris Wilson 		bool blc_event = false;
35402c8ba29fSChris Wilson 
354121ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3542a266c7d5SChris Wilson 
3543a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3544a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3545a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3546a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3547a266c7d5SChris Wilson 		 */
3548a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3549a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3550a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3551a266c7d5SChris Wilson 
3552a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3553a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3554a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3555a266c7d5SChris Wilson 
3556a266c7d5SChris Wilson 			/*
3557a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3558a266c7d5SChris Wilson 			 */
3559a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3560a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3561a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3562a266c7d5SChris Wilson 							 pipe_name(pipe));
3563a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3564a266c7d5SChris Wilson 				irq_received = 1;
3565a266c7d5SChris Wilson 			}
3566a266c7d5SChris Wilson 		}
3567a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3568a266c7d5SChris Wilson 
3569a266c7d5SChris Wilson 		if (!irq_received)
3570a266c7d5SChris Wilson 			break;
3571a266c7d5SChris Wilson 
3572a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3573a266c7d5SChris Wilson 
3574a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3575adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3576a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3577b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3578b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
35794f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3580a266c7d5SChris Wilson 
3581a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3582a266c7d5SChris Wilson 				  hotplug_status);
358391d131d2SDaniel Vetter 
358410a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
358510a504deSDaniel Vetter 					      IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
358691d131d2SDaniel Vetter 
3587a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3588a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3589a266c7d5SChris Wilson 		}
3590a266c7d5SChris Wilson 
359121ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3592a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3593a266c7d5SChris Wilson 
3594a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3595a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3596a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3597a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3598a266c7d5SChris Wilson 
3599a266c7d5SChris Wilson 		for_each_pipe(pipe) {
36002c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
360190a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
360290a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3603a266c7d5SChris Wilson 
3604a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3605a266c7d5SChris Wilson 				blc_event = true;
36064356d586SDaniel Vetter 
36074356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3608277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
3609a266c7d5SChris Wilson 		}
3610a266c7d5SChris Wilson 
3611a266c7d5SChris Wilson 
3612a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3613a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3614a266c7d5SChris Wilson 
3615515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3616515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3617515ac2bbSDaniel Vetter 
3618a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3619a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3620a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3621a266c7d5SChris Wilson 		 * we would never get another interrupt.
3622a266c7d5SChris Wilson 		 *
3623a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3624a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3625a266c7d5SChris Wilson 		 * another one.
3626a266c7d5SChris Wilson 		 *
3627a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3628a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3629a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3630a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3631a266c7d5SChris Wilson 		 * stray interrupts.
3632a266c7d5SChris Wilson 		 */
3633a266c7d5SChris Wilson 		iir = new_iir;
3634a266c7d5SChris Wilson 	}
3635a266c7d5SChris Wilson 
3636d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
36372c8ba29fSChris Wilson 
3638a266c7d5SChris Wilson 	return ret;
3639a266c7d5SChris Wilson }
3640a266c7d5SChris Wilson 
3641a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3642a266c7d5SChris Wilson {
3643a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3644a266c7d5SChris Wilson 	int pipe;
3645a266c7d5SChris Wilson 
3646a266c7d5SChris Wilson 	if (!dev_priv)
3647a266c7d5SChris Wilson 		return;
3648a266c7d5SChris Wilson 
3649ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3650ac4c16c5SEgbert Eich 
3651a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3652a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3653a266c7d5SChris Wilson 
3654a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3655a266c7d5SChris Wilson 	for_each_pipe(pipe)
3656a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3657a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3658a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3659a266c7d5SChris Wilson 
3660a266c7d5SChris Wilson 	for_each_pipe(pipe)
3661a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3662a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3663a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3664a266c7d5SChris Wilson }
3665a266c7d5SChris Wilson 
3666ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3667ac4c16c5SEgbert Eich {
3668ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3669ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3670ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3671ac4c16c5SEgbert Eich 	unsigned long irqflags;
3672ac4c16c5SEgbert Eich 	int i;
3673ac4c16c5SEgbert Eich 
3674ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3675ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3676ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3677ac4c16c5SEgbert Eich 
3678ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3679ac4c16c5SEgbert Eich 			continue;
3680ac4c16c5SEgbert Eich 
3681ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3682ac4c16c5SEgbert Eich 
3683ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3684ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3685ac4c16c5SEgbert Eich 
3686ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3687ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3688ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3689ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3690ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3691ac4c16c5SEgbert Eich 				if (!connector->polled)
3692ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3693ac4c16c5SEgbert Eich 			}
3694ac4c16c5SEgbert Eich 		}
3695ac4c16c5SEgbert Eich 	}
3696ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3697ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3698ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3699ac4c16c5SEgbert Eich }
3700ac4c16c5SEgbert Eich 
3701f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3702f71d4af4SJesse Barnes {
37038b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
37048b2e326dSChris Wilson 
37058b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
370699584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3707c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3708a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
37098b2e326dSChris Wilson 
371099584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
371199584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
371261bac78eSDaniel Vetter 		    (unsigned long) dev);
3713ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3714ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
371561bac78eSDaniel Vetter 
371697a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
37179ee32feaSDaniel Vetter 
37184cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
37194cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
37204cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
37214cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3722f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3723f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3724391f75e2SVille Syrjälä 	} else {
3725391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
3726391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
3727f71d4af4SJesse Barnes 	}
3728f71d4af4SJesse Barnes 
3729c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
3730f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3731f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3732c2baf4b7SVille Syrjälä 	}
3733f71d4af4SJesse Barnes 
37347e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
37357e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
37367e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
37377e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
37387e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
37397e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
37407e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3741fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3742abd58f01SBen Widawsky 	} else if (IS_GEN8(dev)) {
3743abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
3744abd58f01SBen Widawsky 		dev->driver->irq_preinstall = gen8_irq_preinstall;
3745abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
3746abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
3747abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
3748abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
3749abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3750f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3751f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3752f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3753f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3754f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3755f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3756f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
375782a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3758f71d4af4SJesse Barnes 	} else {
3759c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3760c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3761c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3762c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3763c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3764a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3765a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3766a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3767a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3768a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
376920afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3770c2798b19SChris Wilson 		} else {
3771a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3772a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3773a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3774a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3775bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3776c2798b19SChris Wilson 		}
3777f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3778f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3779f71d4af4SJesse Barnes 	}
3780f71d4af4SJesse Barnes }
378120afbda2SDaniel Vetter 
378220afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
378320afbda2SDaniel Vetter {
378420afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3785821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3786821450c6SEgbert Eich 	struct drm_connector *connector;
3787b5ea2d56SDaniel Vetter 	unsigned long irqflags;
3788821450c6SEgbert Eich 	int i;
378920afbda2SDaniel Vetter 
3790821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3791821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3792821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3793821450c6SEgbert Eich 	}
3794821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3795821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3796821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3797821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3798821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3799821450c6SEgbert Eich 	}
3800b5ea2d56SDaniel Vetter 
3801b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3802b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
3803b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
380420afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
380520afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
3806b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
380720afbda2SDaniel Vetter }
3808c67a470bSPaulo Zanoni 
3809c67a470bSPaulo Zanoni /* Disable interrupts so we can allow Package C8+. */
3810c67a470bSPaulo Zanoni void hsw_pc8_disable_interrupts(struct drm_device *dev)
3811c67a470bSPaulo Zanoni {
3812c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3813c67a470bSPaulo Zanoni 	unsigned long irqflags;
3814c67a470bSPaulo Zanoni 
3815c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3816c67a470bSPaulo Zanoni 
3817c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.deimr = I915_READ(DEIMR);
3818c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.sdeimr = I915_READ(SDEIMR);
3819c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtimr = I915_READ(GTIMR);
3820c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtier = I915_READ(GTIER);
3821c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
3822c67a470bSPaulo Zanoni 
3823c67a470bSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, ~DE_PCH_EVENT_IVB);
3824c67a470bSPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, ~SDE_HOTPLUG_MASK_CPT);
3825c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
3826c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
3827c67a470bSPaulo Zanoni 
3828c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = true;
3829c67a470bSPaulo Zanoni 
3830c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3831c67a470bSPaulo Zanoni }
3832c67a470bSPaulo Zanoni 
3833c67a470bSPaulo Zanoni /* Restore interrupts so we can recover from Package C8+. */
3834c67a470bSPaulo Zanoni void hsw_pc8_restore_interrupts(struct drm_device *dev)
3835c67a470bSPaulo Zanoni {
3836c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3837c67a470bSPaulo Zanoni 	unsigned long irqflags;
3838c67a470bSPaulo Zanoni 	uint32_t val, expected;
3839c67a470bSPaulo Zanoni 
3840c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3841c67a470bSPaulo Zanoni 
3842c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
3843c67a470bSPaulo Zanoni 	expected = ~DE_PCH_EVENT_IVB;
3844c67a470bSPaulo Zanoni 	WARN(val != expected, "DEIMR is 0x%08x, not 0x%08x\n", val, expected);
3845c67a470bSPaulo Zanoni 
3846c67a470bSPaulo Zanoni 	val = I915_READ(SDEIMR) & ~SDE_HOTPLUG_MASK_CPT;
3847c67a470bSPaulo Zanoni 	expected = ~SDE_HOTPLUG_MASK_CPT;
3848c67a470bSPaulo Zanoni 	WARN(val != expected, "SDEIMR non-HPD bits are 0x%08x, not 0x%08x\n",
3849c67a470bSPaulo Zanoni 	     val, expected);
3850c67a470bSPaulo Zanoni 
3851c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
3852c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3853c67a470bSPaulo Zanoni 	WARN(val != expected, "GTIMR is 0x%08x, not 0x%08x\n", val, expected);
3854c67a470bSPaulo Zanoni 
3855c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
3856c67a470bSPaulo Zanoni 	expected = 0xffffffff;
3857c67a470bSPaulo Zanoni 	WARN(val != expected, "GEN6_PMIMR is 0x%08x, not 0x%08x\n", val,
3858c67a470bSPaulo Zanoni 	     expected);
3859c67a470bSPaulo Zanoni 
3860c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = false;
3861c67a470bSPaulo Zanoni 
3862c67a470bSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pc8.regsave.deimr);
3863c67a470bSPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv,
3864c67a470bSPaulo Zanoni 				     ~dev_priv->pc8.regsave.sdeimr &
3865c67a470bSPaulo Zanoni 				     ~SDE_HOTPLUG_MASK_CPT);
3866c67a470bSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pc8.regsave.gtimr);
3867c67a470bSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pc8.regsave.gen6_pmimr);
3868c67a470bSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pc8.regsave.gtier);
3869c67a470bSPaulo Zanoni 
3870c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3871c67a470bSPaulo Zanoni }
3872