xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 34273620d9227b61b82257e56d2d190abb9de2d8)
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 
40fca52a55SDaniel Vetter /**
41fca52a55SDaniel Vetter  * DOC: interrupt handling
42fca52a55SDaniel Vetter  *
43fca52a55SDaniel Vetter  * These functions provide the basic support for enabling and disabling the
44fca52a55SDaniel Vetter  * interrupt handling support. There's a lot more functionality in i915_irq.c
45fca52a55SDaniel Vetter  * and related files, but that will be described in separate chapters.
46fca52a55SDaniel Vetter  */
47fca52a55SDaniel Vetter 
48e5868a31SEgbert Eich static const u32 hpd_ibx[] = {
49e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
50e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
51e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
52e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
53e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
54e5868a31SEgbert Eich };
55e5868a31SEgbert Eich 
56e5868a31SEgbert Eich static const u32 hpd_cpt[] = {
57e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
5873c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
59e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
60e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
61e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
62e5868a31SEgbert Eich };
63e5868a31SEgbert Eich 
64e5868a31SEgbert Eich static const u32 hpd_mask_i915[] = {
65e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
66e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
67e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
68e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
69e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
70e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
71e5868a31SEgbert Eich };
72e5868a31SEgbert Eich 
73704cfb87SDaniel Vetter static const u32 hpd_status_g4x[] = {
74e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
75e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
76e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
77e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
78e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
79e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
80e5868a31SEgbert Eich };
81e5868a31SEgbert Eich 
82e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
83e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
84e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
85e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
86e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
87e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
88e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
89e5868a31SEgbert Eich };
90e5868a31SEgbert Eich 
915c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
92f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
935c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
945c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
955c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
965c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
975c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
985c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
995c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1005c502442SPaulo Zanoni } while (0)
1015c502442SPaulo Zanoni 
102f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
103a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1045c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
105a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1065c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1075c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1085c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1095c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
110a9d356a6SPaulo Zanoni } while (0)
111a9d356a6SPaulo Zanoni 
112337ba017SPaulo Zanoni /*
113337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
114337ba017SPaulo Zanoni  */
115337ba017SPaulo Zanoni #define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
116337ba017SPaulo Zanoni 	u32 val = I915_READ(reg); \
117337ba017SPaulo Zanoni 	if (val) { \
118337ba017SPaulo Zanoni 		WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
119337ba017SPaulo Zanoni 		     (reg), val); \
120337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
121337ba017SPaulo Zanoni 		POSTING_READ(reg); \
122337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
123337ba017SPaulo Zanoni 		POSTING_READ(reg); \
124337ba017SPaulo Zanoni 	} \
125337ba017SPaulo Zanoni } while (0)
126337ba017SPaulo Zanoni 
12735079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
128337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
12935079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1307d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1317d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
13235079899SPaulo Zanoni } while (0)
13335079899SPaulo Zanoni 
13435079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
135337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
13635079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1377d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1387d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
13935079899SPaulo Zanoni } while (0)
14035079899SPaulo Zanoni 
141c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
142c9a9a268SImre Deak 
143036a4a7dSZhenyu Wang /* For display hotplug interrupt */
14447339cd9SDaniel Vetter void
1452d1013ddSJani Nikula ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
146036a4a7dSZhenyu Wang {
1474bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1484bc9d430SDaniel Vetter 
1499df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
150c67a470bSPaulo Zanoni 		return;
151c67a470bSPaulo Zanoni 
1521ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
1531ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
1541ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1553143a2bfSChris Wilson 		POSTING_READ(DEIMR);
156036a4a7dSZhenyu Wang 	}
157036a4a7dSZhenyu Wang }
158036a4a7dSZhenyu Wang 
15947339cd9SDaniel Vetter void
1602d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
161036a4a7dSZhenyu Wang {
1624bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1634bc9d430SDaniel Vetter 
16406ffc778SPaulo Zanoni 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
165c67a470bSPaulo Zanoni 		return;
166c67a470bSPaulo Zanoni 
1671ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1681ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1691ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1703143a2bfSChris Wilson 		POSTING_READ(DEIMR);
171036a4a7dSZhenyu Wang 	}
172036a4a7dSZhenyu Wang }
173036a4a7dSZhenyu Wang 
17443eaea13SPaulo Zanoni /**
17543eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
17643eaea13SPaulo Zanoni  * @dev_priv: driver private
17743eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
17843eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
17943eaea13SPaulo Zanoni  */
18043eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
18143eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
18243eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
18343eaea13SPaulo Zanoni {
18443eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
18543eaea13SPaulo Zanoni 
1869df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
187c67a470bSPaulo Zanoni 		return;
188c67a470bSPaulo Zanoni 
18943eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
19043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
19143eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
19243eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
19343eaea13SPaulo Zanoni }
19443eaea13SPaulo Zanoni 
195480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
19643eaea13SPaulo Zanoni {
19743eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
19843eaea13SPaulo Zanoni }
19943eaea13SPaulo Zanoni 
200480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
20143eaea13SPaulo Zanoni {
20243eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
20343eaea13SPaulo Zanoni }
20443eaea13SPaulo Zanoni 
205b900b949SImre Deak static u32 gen6_pm_iir(struct drm_i915_private *dev_priv)
206b900b949SImre Deak {
207b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
208b900b949SImre Deak }
209b900b949SImre Deak 
210a72fbc3aSImre Deak static u32 gen6_pm_imr(struct drm_i915_private *dev_priv)
211a72fbc3aSImre Deak {
212a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
213a72fbc3aSImre Deak }
214a72fbc3aSImre Deak 
215b900b949SImre Deak static u32 gen6_pm_ier(struct drm_i915_private *dev_priv)
216b900b949SImre Deak {
217b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
218b900b949SImre Deak }
219b900b949SImre Deak 
220edbfdb45SPaulo Zanoni /**
221edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
222edbfdb45SPaulo Zanoni   * @dev_priv: driver private
223edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
224edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
225edbfdb45SPaulo Zanoni   */
226edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
227edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
228edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
229edbfdb45SPaulo Zanoni {
230605cd25bSPaulo Zanoni 	uint32_t new_val;
231edbfdb45SPaulo Zanoni 
232edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
233edbfdb45SPaulo Zanoni 
2349df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
235c67a470bSPaulo Zanoni 		return;
236c67a470bSPaulo Zanoni 
237605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
238f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
239f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
240f52ecbcfSPaulo Zanoni 
241605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
242605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
243a72fbc3aSImre Deak 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
244a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
245edbfdb45SPaulo Zanoni 	}
246f52ecbcfSPaulo Zanoni }
247edbfdb45SPaulo Zanoni 
248480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
249edbfdb45SPaulo Zanoni {
250edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
251edbfdb45SPaulo Zanoni }
252edbfdb45SPaulo Zanoni 
253480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
254edbfdb45SPaulo Zanoni {
255edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, 0);
256edbfdb45SPaulo Zanoni }
257edbfdb45SPaulo Zanoni 
2583cc134e3SImre Deak void gen6_reset_rps_interrupts(struct drm_device *dev)
2593cc134e3SImre Deak {
2603cc134e3SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
2613cc134e3SImre Deak 	uint32_t reg = gen6_pm_iir(dev_priv);
2623cc134e3SImre Deak 
2633cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
2643cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
2653cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
2663cc134e3SImre Deak 	POSTING_READ(reg);
2673cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
2683cc134e3SImre Deak }
2693cc134e3SImre Deak 
270b900b949SImre Deak void gen6_enable_rps_interrupts(struct drm_device *dev)
271b900b949SImre Deak {
272b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
273b900b949SImre Deak 
274b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
275b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
2763cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
277d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
278b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
279b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
280b900b949SImre Deak }
281b900b949SImre Deak 
282b900b949SImre Deak void gen6_disable_rps_interrupts(struct drm_device *dev)
283b900b949SImre Deak {
284b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
285b900b949SImre Deak 
286d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
287d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
288d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
289d4d70aa5SImre Deak 
290d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
291d4d70aa5SImre Deak 
292b900b949SImre Deak 	I915_WRITE(GEN6_PMINTRMSK, INTEL_INFO(dev_priv)->gen >= 8 ?
293b900b949SImre Deak 		   ~GEN8_PMINTR_REDIRECT_TO_NON_DISP : ~0);
294b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
295b900b949SImre Deak 				~dev_priv->pm_rps_events);
296b900b949SImre Deak 
297b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
298b900b949SImre Deak 	dev_priv->rps.pm_iir = 0;
299b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
300b900b949SImre Deak 
301b900b949SImre Deak 	I915_WRITE(gen6_pm_iir(dev_priv), dev_priv->pm_rps_events);
302b900b949SImre Deak }
303b900b949SImre Deak 
3040961021aSBen Widawsky /**
305fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
306fee884edSDaniel Vetter  * @dev_priv: driver private
307fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
308fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
309fee884edSDaniel Vetter  */
31047339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
311fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
312fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
313fee884edSDaniel Vetter {
314fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
315fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
316fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
317fee884edSDaniel Vetter 
318fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
319fee884edSDaniel Vetter 
3209df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
321c67a470bSPaulo Zanoni 		return;
322c67a470bSPaulo Zanoni 
323fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
324fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
325fee884edSDaniel Vetter }
3268664281bSPaulo Zanoni 
327b5ea642aSDaniel Vetter static void
328755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
329755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
3307c463586SKeith Packard {
3319db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
332755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
3337c463586SKeith Packard 
334b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
335d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
336b79480baSDaniel Vetter 
33704feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
33804feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
33904feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
34004feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
341755e9019SImre Deak 		return;
342755e9019SImre Deak 
343755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
34446c06a30SVille Syrjälä 		return;
34546c06a30SVille Syrjälä 
34691d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
34791d181ddSImre Deak 
3487c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
349755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
35046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3513143a2bfSChris Wilson 	POSTING_READ(reg);
3527c463586SKeith Packard }
3537c463586SKeith Packard 
354b5ea642aSDaniel Vetter static void
355755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
356755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
3577c463586SKeith Packard {
3589db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
359755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
3607c463586SKeith Packard 
361b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
362d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
363b79480baSDaniel Vetter 
36404feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
36504feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
36604feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
36704feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
36846c06a30SVille Syrjälä 		return;
36946c06a30SVille Syrjälä 
370755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
371755e9019SImre Deak 		return;
372755e9019SImre Deak 
37391d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
37491d181ddSImre Deak 
375755e9019SImre Deak 	pipestat &= ~enable_mask;
37646c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3773143a2bfSChris Wilson 	POSTING_READ(reg);
3787c463586SKeith Packard }
3797c463586SKeith Packard 
38010c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
38110c59c51SImre Deak {
38210c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
38310c59c51SImre Deak 
38410c59c51SImre Deak 	/*
385724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
386724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
38710c59c51SImre Deak 	 */
38810c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
38910c59c51SImre Deak 		return 0;
390724a6905SVille Syrjälä 	/*
391724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
392724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
393724a6905SVille Syrjälä 	 */
394724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
395724a6905SVille Syrjälä 		return 0;
39610c59c51SImre Deak 
39710c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
39810c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
39910c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
40010c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
40110c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
40210c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
40310c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
40410c59c51SImre Deak 
40510c59c51SImre Deak 	return enable_mask;
40610c59c51SImre Deak }
40710c59c51SImre Deak 
408755e9019SImre Deak void
409755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
410755e9019SImre Deak 		     u32 status_mask)
411755e9019SImre Deak {
412755e9019SImre Deak 	u32 enable_mask;
413755e9019SImre Deak 
41410c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
41510c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
41610c59c51SImre Deak 							   status_mask);
41710c59c51SImre Deak 	else
418755e9019SImre Deak 		enable_mask = status_mask << 16;
419755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
420755e9019SImre Deak }
421755e9019SImre Deak 
422755e9019SImre Deak void
423755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
424755e9019SImre Deak 		      u32 status_mask)
425755e9019SImre Deak {
426755e9019SImre Deak 	u32 enable_mask;
427755e9019SImre Deak 
42810c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
42910c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
43010c59c51SImre Deak 							   status_mask);
43110c59c51SImre Deak 	else
432755e9019SImre Deak 		enable_mask = status_mask << 16;
433755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
434755e9019SImre Deak }
435755e9019SImre Deak 
436c0e09200SDave Airlie /**
437f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
43801c66889SZhao Yakui  */
439f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
44001c66889SZhao Yakui {
4412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4421ec14ad3SChris Wilson 
443f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
444f49e38ddSJani Nikula 		return;
445f49e38ddSJani Nikula 
44613321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
44701c66889SZhao Yakui 
448755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
449a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
4503b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
451755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
4521ec14ad3SChris Wilson 
45313321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
45401c66889SZhao Yakui }
45501c66889SZhao Yakui 
45601c66889SZhao Yakui /**
4570a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
4580a3e67a4SJesse Barnes  * @dev: DRM device
4590a3e67a4SJesse Barnes  * @pipe: pipe to check
4600a3e67a4SJesse Barnes  *
4610a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
4620a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
4630a3e67a4SJesse Barnes  * before reading such registers if unsure.
4640a3e67a4SJesse Barnes  */
4650a3e67a4SJesse Barnes static int
4660a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
4670a3e67a4SJesse Barnes {
4682d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
469702e7a56SPaulo Zanoni 
470a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
471a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
472a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
473a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
47471f8ba6bSPaulo Zanoni 
475a01025afSDaniel Vetter 		return intel_crtc->active;
476a01025afSDaniel Vetter 	} else {
477a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
478a01025afSDaniel Vetter 	}
4790a3e67a4SJesse Barnes }
4800a3e67a4SJesse Barnes 
481f75f3746SVille Syrjälä /*
482f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
483f75f3746SVille Syrjälä  * around the vertical blanking period.
484f75f3746SVille Syrjälä  *
485f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
486f75f3746SVille Syrjälä  *  vblank_start >= 3
487f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
488f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
489f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
490f75f3746SVille Syrjälä  *
491f75f3746SVille Syrjälä  *           start of vblank:
492f75f3746SVille Syrjälä  *           latch double buffered registers
493f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
494f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
495f75f3746SVille Syrjälä  *           |
496f75f3746SVille Syrjälä  *           |          frame start:
497f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
498f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
499f75f3746SVille Syrjälä  *           |          |
500f75f3746SVille Syrjälä  *           |          |  start of vsync:
501f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
502f75f3746SVille Syrjälä  *           |          |  |
503f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
504f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
505f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
506f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
507f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
508f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
509f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
510f75f3746SVille Syrjälä  *       |          |                                         |
511f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
512f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
513f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
514f75f3746SVille Syrjälä  *
515f75f3746SVille Syrjälä  * x  = horizontal active
516f75f3746SVille Syrjälä  * _  = horizontal blanking
517f75f3746SVille Syrjälä  * hs = horizontal sync
518f75f3746SVille Syrjälä  * va = vertical active
519f75f3746SVille Syrjälä  * vb = vertical blanking
520f75f3746SVille Syrjälä  * vs = vertical sync
521f75f3746SVille Syrjälä  * vbs = vblank_start (number)
522f75f3746SVille Syrjälä  *
523f75f3746SVille Syrjälä  * Summary:
524f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
525f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
526f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
527f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
528f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
529f75f3746SVille Syrjälä  */
530f75f3746SVille Syrjälä 
5314cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
5324cdb83ecSVille Syrjälä {
5334cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
5344cdb83ecSVille Syrjälä 	return 0;
5354cdb83ecSVille Syrjälä }
5364cdb83ecSVille Syrjälä 
53742f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
53842f52ef8SKeith Packard  * we use as a pipe index
53942f52ef8SKeith Packard  */
540f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5410a3e67a4SJesse Barnes {
5422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
5430a3e67a4SJesse Barnes 	unsigned long high_frame;
5440a3e67a4SJesse Barnes 	unsigned long low_frame;
5450b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
5460a3e67a4SJesse Barnes 
5470a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
54844d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5499db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
5500a3e67a4SJesse Barnes 		return 0;
5510a3e67a4SJesse Barnes 	}
5520a3e67a4SJesse Barnes 
553391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
554391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
555391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
556391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
557391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
558391f75e2SVille Syrjälä 
5590b2a8e09SVille Syrjälä 		htotal = mode->crtc_htotal;
5600b2a8e09SVille Syrjälä 		hsync_start = mode->crtc_hsync_start;
5610b2a8e09SVille Syrjälä 		vbl_start = mode->crtc_vblank_start;
5620b2a8e09SVille Syrjälä 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
5630b2a8e09SVille Syrjälä 			vbl_start = DIV_ROUND_UP(vbl_start, 2);
564391f75e2SVille Syrjälä 	} else {
565a2d213ddSDaniel Vetter 		enum transcoder cpu_transcoder = (enum transcoder) pipe;
566391f75e2SVille Syrjälä 
567391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
5680b2a8e09SVille Syrjälä 		hsync_start = (I915_READ(HSYNC(cpu_transcoder))  & 0x1fff) + 1;
569391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
5700b2a8e09SVille Syrjälä 		if ((I915_READ(PIPECONF(cpu_transcoder)) &
5710b2a8e09SVille Syrjälä 		     PIPECONF_INTERLACE_MASK) != PIPECONF_PROGRESSIVE)
5720b2a8e09SVille Syrjälä 			vbl_start = DIV_ROUND_UP(vbl_start, 2);
573391f75e2SVille Syrjälä 	}
574391f75e2SVille Syrjälä 
5750b2a8e09SVille Syrjälä 	/* Convert to pixel count */
5760b2a8e09SVille Syrjälä 	vbl_start *= htotal;
5770b2a8e09SVille Syrjälä 
5780b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
5790b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
5800b2a8e09SVille Syrjälä 
5819db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5829db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5835eddb70bSChris Wilson 
5840a3e67a4SJesse Barnes 	/*
5850a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5860a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
5870a3e67a4SJesse Barnes 	 * register.
5880a3e67a4SJesse Barnes 	 */
5890a3e67a4SJesse Barnes 	do {
5905eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
591391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
5925eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
5930a3e67a4SJesse Barnes 	} while (high1 != high2);
5940a3e67a4SJesse Barnes 
5955eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
596391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
5975eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
598391f75e2SVille Syrjälä 
599391f75e2SVille Syrjälä 	/*
600391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
601391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
602391f75e2SVille Syrjälä 	 * counter against vblank start.
603391f75e2SVille Syrjälä 	 */
604edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
6050a3e67a4SJesse Barnes }
6060a3e67a4SJesse Barnes 
607f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
6089880b7a5SJesse Barnes {
6092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6109db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
6119880b7a5SJesse Barnes 
6129880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
61344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
6149db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6159880b7a5SJesse Barnes 		return 0;
6169880b7a5SJesse Barnes 	}
6179880b7a5SJesse Barnes 
6189880b7a5SJesse Barnes 	return I915_READ(reg);
6199880b7a5SJesse Barnes }
6209880b7a5SJesse Barnes 
621ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
622ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
623ad3543edSMario Kleiner 
624a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
625a225f079SVille Syrjälä {
626a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
627a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
628a225f079SVille Syrjälä 	const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
629a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
63080715b2fSVille Syrjälä 	int position, vtotal;
631a225f079SVille Syrjälä 
63280715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
633a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
634a225f079SVille Syrjälä 		vtotal /= 2;
635a225f079SVille Syrjälä 
636a225f079SVille Syrjälä 	if (IS_GEN2(dev))
637a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
638a225f079SVille Syrjälä 	else
639a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
640a225f079SVille Syrjälä 
641a225f079SVille Syrjälä 	/*
64280715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
64380715b2fSVille Syrjälä 	 * scanline_offset adjustment.
644a225f079SVille Syrjälä 	 */
64580715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
646a225f079SVille Syrjälä }
647a225f079SVille Syrjälä 
648f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
649abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
650abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
6510af7e4dfSMario Kleiner {
652c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
653c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
654c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
655c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
6563aa18df8SVille Syrjälä 	int position;
65778e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
6580af7e4dfSMario Kleiner 	bool in_vbl = true;
6590af7e4dfSMario Kleiner 	int ret = 0;
660ad3543edSMario Kleiner 	unsigned long irqflags;
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;
66978e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
670c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
671c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
672c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
6730af7e4dfSMario Kleiner 
674d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
675d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
676d31faf65SVille Syrjälä 		vbl_end /= 2;
677d31faf65SVille Syrjälä 		vtotal /= 2;
678d31faf65SVille Syrjälä 	}
679d31faf65SVille Syrjälä 
680c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
681c2baf4b7SVille Syrjälä 
682ad3543edSMario Kleiner 	/*
683ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
684ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
685ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
686ad3543edSMario Kleiner 	 */
687ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
688ad3543edSMario Kleiner 
689ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
690ad3543edSMario Kleiner 
691ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
692ad3543edSMario Kleiner 	if (stime)
693ad3543edSMario Kleiner 		*stime = ktime_get();
694ad3543edSMario Kleiner 
6957c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
6960af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
6970af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
6980af7e4dfSMario Kleiner 		 */
699a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
7000af7e4dfSMario Kleiner 	} else {
7010af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
7020af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
7030af7e4dfSMario Kleiner 		 * scanout position.
7040af7e4dfSMario Kleiner 		 */
705ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
7060af7e4dfSMario Kleiner 
7073aa18df8SVille Syrjälä 		/* convert to pixel counts */
7083aa18df8SVille Syrjälä 		vbl_start *= htotal;
7093aa18df8SVille Syrjälä 		vbl_end *= htotal;
7103aa18df8SVille Syrjälä 		vtotal *= htotal;
71178e8fc6bSVille Syrjälä 
71278e8fc6bSVille Syrjälä 		/*
7137e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
7147e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
7157e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
7167e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
7177e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
7187e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
7197e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
7207e78f1cbSVille Syrjälä 		 */
7217e78f1cbSVille Syrjälä 		if (position >= vtotal)
7227e78f1cbSVille Syrjälä 			position = vtotal - 1;
7237e78f1cbSVille Syrjälä 
7247e78f1cbSVille Syrjälä 		/*
72578e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
72678e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
72778e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
72878e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
72978e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
73078e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
73178e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
73278e8fc6bSVille Syrjälä 		 */
73378e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
7343aa18df8SVille Syrjälä 	}
7353aa18df8SVille Syrjälä 
736ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
737ad3543edSMario Kleiner 	if (etime)
738ad3543edSMario Kleiner 		*etime = ktime_get();
739ad3543edSMario Kleiner 
740ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
741ad3543edSMario Kleiner 
742ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
743ad3543edSMario Kleiner 
7443aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
7453aa18df8SVille Syrjälä 
7463aa18df8SVille Syrjälä 	/*
7473aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
7483aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
7493aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
7503aa18df8SVille Syrjälä 	 * up since vbl_end.
7513aa18df8SVille Syrjälä 	 */
7523aa18df8SVille Syrjälä 	if (position >= vbl_start)
7533aa18df8SVille Syrjälä 		position -= vbl_end;
7543aa18df8SVille Syrjälä 	else
7553aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
7563aa18df8SVille Syrjälä 
7577c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7583aa18df8SVille Syrjälä 		*vpos = position;
7593aa18df8SVille Syrjälä 		*hpos = 0;
7603aa18df8SVille Syrjälä 	} else {
7610af7e4dfSMario Kleiner 		*vpos = position / htotal;
7620af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
7630af7e4dfSMario Kleiner 	}
7640af7e4dfSMario Kleiner 
7650af7e4dfSMario Kleiner 	/* In vblank? */
7660af7e4dfSMario Kleiner 	if (in_vbl)
7673d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
7680af7e4dfSMario Kleiner 
7690af7e4dfSMario Kleiner 	return ret;
7700af7e4dfSMario Kleiner }
7710af7e4dfSMario Kleiner 
772a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
773a225f079SVille Syrjälä {
774a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
775a225f079SVille Syrjälä 	unsigned long irqflags;
776a225f079SVille Syrjälä 	int position;
777a225f079SVille Syrjälä 
778a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
779a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
780a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
781a225f079SVille Syrjälä 
782a225f079SVille Syrjälä 	return position;
783a225f079SVille Syrjälä }
784a225f079SVille Syrjälä 
785f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
7860af7e4dfSMario Kleiner 			      int *max_error,
7870af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
7880af7e4dfSMario Kleiner 			      unsigned flags)
7890af7e4dfSMario Kleiner {
7904041b853SChris Wilson 	struct drm_crtc *crtc;
7910af7e4dfSMario Kleiner 
7927eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
7934041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7940af7e4dfSMario Kleiner 		return -EINVAL;
7950af7e4dfSMario Kleiner 	}
7960af7e4dfSMario Kleiner 
7970af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
7984041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
7994041b853SChris Wilson 	if (crtc == NULL) {
8004041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
8014041b853SChris Wilson 		return -EINVAL;
8024041b853SChris Wilson 	}
8034041b853SChris Wilson 
8044041b853SChris Wilson 	if (!crtc->enabled) {
8054041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
8064041b853SChris Wilson 		return -EBUSY;
8074041b853SChris Wilson 	}
8080af7e4dfSMario Kleiner 
8090af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
8104041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
8114041b853SChris Wilson 						     vblank_time, flags,
8127da903efSVille Syrjälä 						     crtc,
8137da903efSVille Syrjälä 						     &to_intel_crtc(crtc)->config.adjusted_mode);
8140af7e4dfSMario Kleiner }
8150af7e4dfSMario Kleiner 
81667c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
81767c347ffSJani Nikula 				struct drm_connector *connector)
818321a1b30SEgbert Eich {
819321a1b30SEgbert Eich 	enum drm_connector_status old_status;
820321a1b30SEgbert Eich 
821321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
822321a1b30SEgbert Eich 	old_status = connector->status;
823321a1b30SEgbert Eich 
824321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
82567c347ffSJani Nikula 	if (old_status == connector->status)
82667c347ffSJani Nikula 		return false;
82767c347ffSJani Nikula 
82867c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
829321a1b30SEgbert Eich 		      connector->base.id,
830c23cc417SJani Nikula 		      connector->name,
83167c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
83267c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
83367c347ffSJani Nikula 
83467c347ffSJani Nikula 	return true;
835321a1b30SEgbert Eich }
836321a1b30SEgbert Eich 
83713cf5504SDave Airlie static void i915_digport_work_func(struct work_struct *work)
83813cf5504SDave Airlie {
83913cf5504SDave Airlie 	struct drm_i915_private *dev_priv =
84013cf5504SDave Airlie 		container_of(work, struct drm_i915_private, dig_port_work);
84113cf5504SDave Airlie 	u32 long_port_mask, short_port_mask;
84213cf5504SDave Airlie 	struct intel_digital_port *intel_dig_port;
84313cf5504SDave Airlie 	int i, ret;
84413cf5504SDave Airlie 	u32 old_bits = 0;
84513cf5504SDave Airlie 
8464cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
84713cf5504SDave Airlie 	long_port_mask = dev_priv->long_hpd_port_mask;
84813cf5504SDave Airlie 	dev_priv->long_hpd_port_mask = 0;
84913cf5504SDave Airlie 	short_port_mask = dev_priv->short_hpd_port_mask;
85013cf5504SDave Airlie 	dev_priv->short_hpd_port_mask = 0;
8514cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
85213cf5504SDave Airlie 
85313cf5504SDave Airlie 	for (i = 0; i < I915_MAX_PORTS; i++) {
85413cf5504SDave Airlie 		bool valid = false;
85513cf5504SDave Airlie 		bool long_hpd = false;
85613cf5504SDave Airlie 		intel_dig_port = dev_priv->hpd_irq_port[i];
85713cf5504SDave Airlie 		if (!intel_dig_port || !intel_dig_port->hpd_pulse)
85813cf5504SDave Airlie 			continue;
85913cf5504SDave Airlie 
86013cf5504SDave Airlie 		if (long_port_mask & (1 << i))  {
86113cf5504SDave Airlie 			valid = true;
86213cf5504SDave Airlie 			long_hpd = true;
86313cf5504SDave Airlie 		} else if (short_port_mask & (1 << i))
86413cf5504SDave Airlie 			valid = true;
86513cf5504SDave Airlie 
86613cf5504SDave Airlie 		if (valid) {
86713cf5504SDave Airlie 			ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
86813cf5504SDave Airlie 			if (ret == true) {
86913cf5504SDave Airlie 				/* if we get true fallback to old school hpd */
87013cf5504SDave Airlie 				old_bits |= (1 << intel_dig_port->base.hpd_pin);
87113cf5504SDave Airlie 			}
87213cf5504SDave Airlie 		}
87313cf5504SDave Airlie 	}
87413cf5504SDave Airlie 
87513cf5504SDave Airlie 	if (old_bits) {
8764cb21832SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
87713cf5504SDave Airlie 		dev_priv->hpd_event_bits |= old_bits;
8784cb21832SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
87913cf5504SDave Airlie 		schedule_work(&dev_priv->hotplug_work);
88013cf5504SDave Airlie 	}
88113cf5504SDave Airlie }
88213cf5504SDave Airlie 
8835ca58282SJesse Barnes /*
8845ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
8855ca58282SJesse Barnes  */
886ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
887ac4c16c5SEgbert Eich 
8885ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
8895ca58282SJesse Barnes {
8902d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
8912d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, hotplug_work);
8925ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
893c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
894cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
895cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
896cd569aedSEgbert Eich 	struct drm_connector *connector;
897cd569aedSEgbert Eich 	bool hpd_disabled = false;
898321a1b30SEgbert Eich 	bool changed = false;
899142e2398SEgbert Eich 	u32 hpd_event_bits;
9005ca58282SJesse Barnes 
901a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
902e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
903e67189abSJesse Barnes 
9044cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
905142e2398SEgbert Eich 
906142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
907142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
908cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
909cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
91036cd7444SDave Airlie 		if (!intel_connector->encoder)
91136cd7444SDave Airlie 			continue;
912cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
913cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
914cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
915cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
916cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
917cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
918c23cc417SJani Nikula 				connector->name);
919cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
920cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
921cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
922cd569aedSEgbert Eich 			hpd_disabled = true;
923cd569aedSEgbert Eich 		}
924142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
925142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
926c23cc417SJani Nikula 				      connector->name, intel_encoder->hpd_pin);
927142e2398SEgbert Eich 		}
928cd569aedSEgbert Eich 	}
929cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
930cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
931cd569aedSEgbert Eich 	  * some connectors */
932ac4c16c5SEgbert Eich 	if (hpd_disabled) {
933cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
9346323751dSImre Deak 		mod_delayed_work(system_wq, &dev_priv->hotplug_reenable_work,
9356323751dSImre Deak 				 msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
936ac4c16c5SEgbert Eich 	}
937cd569aedSEgbert Eich 
9384cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
939cd569aedSEgbert Eich 
940321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
941321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
94236cd7444SDave Airlie 		if (!intel_connector->encoder)
94336cd7444SDave Airlie 			continue;
944321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
945321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
946cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
947cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
948321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
949321a1b30SEgbert Eich 				changed = true;
950321a1b30SEgbert Eich 		}
951321a1b30SEgbert Eich 	}
95240ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
95340ee3381SKeith Packard 
954321a1b30SEgbert Eich 	if (changed)
955321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
9565ca58282SJesse Barnes }
9575ca58282SJesse Barnes 
958d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
959f97108d1SJesse Barnes {
9602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
961b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9629270388eSDaniel Vetter 	u8 new_delay;
9639270388eSDaniel Vetter 
964d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
965f97108d1SJesse Barnes 
96673edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
96773edd18fSDaniel Vetter 
96820e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9699270388eSDaniel Vetter 
9707648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
971b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
972b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
973f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
974f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
975f97108d1SJesse Barnes 
976f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
977b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
97820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
97920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
98020e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
98120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
982b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
98320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
98420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
98520e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
98620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
987f97108d1SJesse Barnes 	}
988f97108d1SJesse Barnes 
9897648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
99020e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
991f97108d1SJesse Barnes 
992d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9939270388eSDaniel Vetter 
994f97108d1SJesse Barnes 	return;
995f97108d1SJesse Barnes }
996f97108d1SJesse Barnes 
997549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
998a4872ba6SOscar Mateo 			struct intel_engine_cs *ring)
999549f7365SChris Wilson {
100093b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
1001475553deSChris Wilson 		return;
1002475553deSChris Wilson 
1003814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
10049862e600SChris Wilson 
1005549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
1006549f7365SChris Wilson }
1007549f7365SChris Wilson 
100831685c25SDeepak S static u32 vlv_c0_residency(struct drm_i915_private *dev_priv,
1009bf225f20SChris Wilson 			    struct intel_rps_ei *rps_ei)
101031685c25SDeepak S {
101131685c25SDeepak S 	u32 cz_ts, cz_freq_khz;
101231685c25SDeepak S 	u32 render_count, media_count;
101331685c25SDeepak S 	u32 elapsed_render, elapsed_media, elapsed_time;
101431685c25SDeepak S 	u32 residency = 0;
101531685c25SDeepak S 
101631685c25SDeepak S 	cz_ts = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
101731685c25SDeepak S 	cz_freq_khz = DIV_ROUND_CLOSEST(dev_priv->mem_freq * 1000, 4);
101831685c25SDeepak S 
101931685c25SDeepak S 	render_count = I915_READ(VLV_RENDER_C0_COUNT_REG);
102031685c25SDeepak S 	media_count = I915_READ(VLV_MEDIA_C0_COUNT_REG);
102131685c25SDeepak S 
1022bf225f20SChris Wilson 	if (rps_ei->cz_clock == 0) {
1023bf225f20SChris Wilson 		rps_ei->cz_clock = cz_ts;
1024bf225f20SChris Wilson 		rps_ei->render_c0 = render_count;
1025bf225f20SChris Wilson 		rps_ei->media_c0 = media_count;
102631685c25SDeepak S 
102731685c25SDeepak S 		return dev_priv->rps.cur_freq;
102831685c25SDeepak S 	}
102931685c25SDeepak S 
1030bf225f20SChris Wilson 	elapsed_time = cz_ts - rps_ei->cz_clock;
1031bf225f20SChris Wilson 	rps_ei->cz_clock = cz_ts;
103231685c25SDeepak S 
1033bf225f20SChris Wilson 	elapsed_render = render_count - rps_ei->render_c0;
1034bf225f20SChris Wilson 	rps_ei->render_c0 = render_count;
103531685c25SDeepak S 
1036bf225f20SChris Wilson 	elapsed_media = media_count - rps_ei->media_c0;
1037bf225f20SChris Wilson 	rps_ei->media_c0 = media_count;
103831685c25SDeepak S 
103931685c25SDeepak S 	/* Convert all the counters into common unit of milli sec */
104031685c25SDeepak S 	elapsed_time /= VLV_CZ_CLOCK_TO_MILLI_SEC;
104131685c25SDeepak S 	elapsed_render /=  cz_freq_khz;
104231685c25SDeepak S 	elapsed_media /= cz_freq_khz;
104331685c25SDeepak S 
104431685c25SDeepak S 	/*
104531685c25SDeepak S 	 * Calculate overall C0 residency percentage
104631685c25SDeepak S 	 * only if elapsed time is non zero
104731685c25SDeepak S 	 */
104831685c25SDeepak S 	if (elapsed_time) {
104931685c25SDeepak S 		residency =
105031685c25SDeepak S 			((max(elapsed_render, elapsed_media) * 100)
105131685c25SDeepak S 				/ elapsed_time);
105231685c25SDeepak S 	}
105331685c25SDeepak S 
105431685c25SDeepak S 	return residency;
105531685c25SDeepak S }
105631685c25SDeepak S 
105731685c25SDeepak S /**
105831685c25SDeepak S  * vlv_calc_delay_from_C0_counters - Increase/Decrease freq based on GPU
105931685c25SDeepak S  * busy-ness calculated from C0 counters of render & media power wells
106031685c25SDeepak S  * @dev_priv: DRM device private
106131685c25SDeepak S  *
106231685c25SDeepak S  */
10634fa79042SDamien Lespiau static int vlv_calc_delay_from_C0_counters(struct drm_i915_private *dev_priv)
106431685c25SDeepak S {
106531685c25SDeepak S 	u32 residency_C0_up = 0, residency_C0_down = 0;
10664fa79042SDamien Lespiau 	int new_delay, adj;
106731685c25SDeepak S 
106831685c25SDeepak S 	dev_priv->rps.ei_interrupt_count++;
106931685c25SDeepak S 
107031685c25SDeepak S 	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
107131685c25SDeepak S 
107231685c25SDeepak S 
1073bf225f20SChris Wilson 	if (dev_priv->rps.up_ei.cz_clock == 0) {
1074bf225f20SChris Wilson 		vlv_c0_residency(dev_priv, &dev_priv->rps.up_ei);
1075bf225f20SChris Wilson 		vlv_c0_residency(dev_priv, &dev_priv->rps.down_ei);
107631685c25SDeepak S 		return dev_priv->rps.cur_freq;
107731685c25SDeepak S 	}
107831685c25SDeepak S 
107931685c25SDeepak S 
108031685c25SDeepak S 	/*
108131685c25SDeepak S 	 * To down throttle, C0 residency should be less than down threshold
108231685c25SDeepak S 	 * for continous EI intervals. So calculate down EI counters
108331685c25SDeepak S 	 * once in VLV_INT_COUNT_FOR_DOWN_EI
108431685c25SDeepak S 	 */
108531685c25SDeepak S 	if (dev_priv->rps.ei_interrupt_count == VLV_INT_COUNT_FOR_DOWN_EI) {
108631685c25SDeepak S 
108731685c25SDeepak S 		dev_priv->rps.ei_interrupt_count = 0;
108831685c25SDeepak S 
108931685c25SDeepak S 		residency_C0_down = vlv_c0_residency(dev_priv,
1090bf225f20SChris Wilson 						     &dev_priv->rps.down_ei);
109131685c25SDeepak S 	} else {
109231685c25SDeepak S 		residency_C0_up = vlv_c0_residency(dev_priv,
1093bf225f20SChris Wilson 						   &dev_priv->rps.up_ei);
109431685c25SDeepak S 	}
109531685c25SDeepak S 
109631685c25SDeepak S 	new_delay = dev_priv->rps.cur_freq;
109731685c25SDeepak S 
109831685c25SDeepak S 	adj = dev_priv->rps.last_adj;
109931685c25SDeepak S 	/* C0 residency is greater than UP threshold. Increase Frequency */
110031685c25SDeepak S 	if (residency_C0_up >= VLV_RP_UP_EI_THRESHOLD) {
110131685c25SDeepak S 		if (adj > 0)
110231685c25SDeepak S 			adj *= 2;
110331685c25SDeepak S 		else
110431685c25SDeepak S 			adj = 1;
110531685c25SDeepak S 
110631685c25SDeepak S 		if (dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit)
110731685c25SDeepak S 			new_delay = dev_priv->rps.cur_freq + adj;
110831685c25SDeepak S 
110931685c25SDeepak S 		/*
111031685c25SDeepak S 		 * For better performance, jump directly
111131685c25SDeepak S 		 * to RPe if we're below it.
111231685c25SDeepak S 		 */
111331685c25SDeepak S 		if (new_delay < dev_priv->rps.efficient_freq)
111431685c25SDeepak S 			new_delay = dev_priv->rps.efficient_freq;
111531685c25SDeepak S 
111631685c25SDeepak S 	} else if (!dev_priv->rps.ei_interrupt_count &&
111731685c25SDeepak S 			(residency_C0_down < VLV_RP_DOWN_EI_THRESHOLD)) {
111831685c25SDeepak S 		if (adj < 0)
111931685c25SDeepak S 			adj *= 2;
112031685c25SDeepak S 		else
112131685c25SDeepak S 			adj = -1;
112231685c25SDeepak S 		/*
112331685c25SDeepak S 		 * This means, C0 residency is less than down threshold over
112431685c25SDeepak S 		 * a period of VLV_INT_COUNT_FOR_DOWN_EI. So, reduce the freq
112531685c25SDeepak S 		 */
112631685c25SDeepak S 		if (dev_priv->rps.cur_freq > dev_priv->rps.min_freq_softlimit)
112731685c25SDeepak S 			new_delay = dev_priv->rps.cur_freq + adj;
112831685c25SDeepak S 	}
112931685c25SDeepak S 
113031685c25SDeepak S 	return new_delay;
113131685c25SDeepak S }
113231685c25SDeepak S 
11334912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11343b8d8d91SJesse Barnes {
11352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11362d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
1137edbfdb45SPaulo Zanoni 	u32 pm_iir;
1138dd75fdc8SChris Wilson 	int new_delay, adj;
11393b8d8d91SJesse Barnes 
114059cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1141d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1142d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1143d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1144d4d70aa5SImre Deak 		return;
1145d4d70aa5SImre Deak 	}
1146c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1147c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1148a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1149480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
115059cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11514912d041SBen Widawsky 
115260611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1153a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
115460611c13SPaulo Zanoni 
1155a6706b45SDeepak S 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
11563b8d8d91SJesse Barnes 		return;
11573b8d8d91SJesse Barnes 
11584fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11597b9e0ae6SChris Wilson 
1160dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
11617425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1162dd75fdc8SChris Wilson 		if (adj > 0)
1163dd75fdc8SChris Wilson 			adj *= 2;
116413a5660cSDeepak S 		else {
116513a5660cSDeepak S 			/* CHV needs even encode values */
116613a5660cSDeepak S 			adj = IS_CHERRYVIEW(dev_priv->dev) ? 2 : 1;
116713a5660cSDeepak S 		}
1168b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
11697425034aSVille Syrjälä 
11707425034aSVille Syrjälä 		/*
11717425034aSVille Syrjälä 		 * For better performance, jump directly
11727425034aSVille Syrjälä 		 * to RPe if we're below it.
11737425034aSVille Syrjälä 		 */
1174b39fb297SBen Widawsky 		if (new_delay < dev_priv->rps.efficient_freq)
1175b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1176dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1177b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1178b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1179dd75fdc8SChris Wilson 		else
1180b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1181dd75fdc8SChris Wilson 		adj = 0;
118231685c25SDeepak S 	} else if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
118331685c25SDeepak S 		new_delay = vlv_calc_delay_from_C0_counters(dev_priv);
1184dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1185dd75fdc8SChris Wilson 		if (adj < 0)
1186dd75fdc8SChris Wilson 			adj *= 2;
118713a5660cSDeepak S 		else {
118813a5660cSDeepak S 			/* CHV needs even encode values */
118913a5660cSDeepak S 			adj = IS_CHERRYVIEW(dev_priv->dev) ? -2 : -1;
119013a5660cSDeepak S 		}
1191b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
1192dd75fdc8SChris Wilson 	} else { /* unknown event */
1193b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq;
1194dd75fdc8SChris Wilson 	}
11953b8d8d91SJesse Barnes 
119679249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
119779249636SBen Widawsky 	 * interrupt
119879249636SBen Widawsky 	 */
11991272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
1200b39fb297SBen Widawsky 			    dev_priv->rps.min_freq_softlimit,
1201b39fb297SBen Widawsky 			    dev_priv->rps.max_freq_softlimit);
120227544369SDeepak S 
1203b39fb297SBen Widawsky 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
1204dd75fdc8SChris Wilson 
12050a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
12060a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
12070a073b84SJesse Barnes 	else
12084912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
12093b8d8d91SJesse Barnes 
12104fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
12113b8d8d91SJesse Barnes }
12123b8d8d91SJesse Barnes 
1213e3689190SBen Widawsky 
1214e3689190SBen Widawsky /**
1215e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1216e3689190SBen Widawsky  * occurred.
1217e3689190SBen Widawsky  * @work: workqueue struct
1218e3689190SBen Widawsky  *
1219e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1220e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1221e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1222e3689190SBen Widawsky  */
1223e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1224e3689190SBen Widawsky {
12252d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
12262d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1227e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
122835a85ac6SBen Widawsky 	char *parity_event[6];
1229e3689190SBen Widawsky 	uint32_t misccpctl;
123035a85ac6SBen Widawsky 	uint8_t slice = 0;
1231e3689190SBen Widawsky 
1232e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1233e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1234e3689190SBen Widawsky 	 * any time we access those registers.
1235e3689190SBen Widawsky 	 */
1236e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1237e3689190SBen Widawsky 
123835a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
123935a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
124035a85ac6SBen Widawsky 		goto out;
124135a85ac6SBen Widawsky 
1242e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1243e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1244e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1245e3689190SBen Widawsky 
124635a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
124735a85ac6SBen Widawsky 		u32 reg;
124835a85ac6SBen Widawsky 
124935a85ac6SBen Widawsky 		slice--;
125035a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
125135a85ac6SBen Widawsky 			break;
125235a85ac6SBen Widawsky 
125335a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
125435a85ac6SBen Widawsky 
125535a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
125635a85ac6SBen Widawsky 
125735a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1258e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1259e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1260e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1261e3689190SBen Widawsky 
126235a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
126335a85ac6SBen Widawsky 		POSTING_READ(reg);
1264e3689190SBen Widawsky 
1265cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1266e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1267e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1268e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
126935a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
127035a85ac6SBen Widawsky 		parity_event[5] = NULL;
1271e3689190SBen Widawsky 
12725bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1273e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1274e3689190SBen Widawsky 
127535a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
127635a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1277e3689190SBen Widawsky 
127835a85ac6SBen Widawsky 		kfree(parity_event[4]);
1279e3689190SBen Widawsky 		kfree(parity_event[3]);
1280e3689190SBen Widawsky 		kfree(parity_event[2]);
1281e3689190SBen Widawsky 		kfree(parity_event[1]);
1282e3689190SBen Widawsky 	}
1283e3689190SBen Widawsky 
128435a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
128535a85ac6SBen Widawsky 
128635a85ac6SBen Widawsky out:
128735a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12884cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1289480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
12904cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
129135a85ac6SBen Widawsky 
129235a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
129335a85ac6SBen Widawsky }
129435a85ac6SBen Widawsky 
129535a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1296e3689190SBen Widawsky {
12972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1298e3689190SBen Widawsky 
1299040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1300e3689190SBen Widawsky 		return;
1301e3689190SBen Widawsky 
1302d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1303480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1304d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1305e3689190SBen Widawsky 
130635a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
130735a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
130835a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
130935a85ac6SBen Widawsky 
131035a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
131135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
131235a85ac6SBen Widawsky 
1313a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1314e3689190SBen Widawsky }
1315e3689190SBen Widawsky 
1316f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1317f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1318f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1319f1af8fc1SPaulo Zanoni {
1320f1af8fc1SPaulo Zanoni 	if (gt_iir &
1321f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1322f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1323f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1324f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1325f1af8fc1SPaulo Zanoni }
1326f1af8fc1SPaulo Zanoni 
1327e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1328e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1329e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1330e7b4c6b1SDaniel Vetter {
1331e7b4c6b1SDaniel Vetter 
1332cc609d5dSBen Widawsky 	if (gt_iir &
1333cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1334e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1335cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1336e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1337cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1338e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1339e7b4c6b1SDaniel Vetter 
1340cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1341cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1342aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1343aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1344e3689190SBen Widawsky 
134535a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
134635a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1347e7b4c6b1SDaniel Vetter }
1348e7b4c6b1SDaniel Vetter 
1349abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1350abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1351abd58f01SBen Widawsky 				       u32 master_ctl)
1352abd58f01SBen Widawsky {
1353e981e7b1SThomas Daniel 	struct intel_engine_cs *ring;
1354abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1355abd58f01SBen Widawsky 	uint32_t tmp = 0;
1356abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1357abd58f01SBen Widawsky 
1358abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1359abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1360abd58f01SBen Widawsky 		if (tmp) {
136138cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1362abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1363e981e7b1SThomas Daniel 
1364abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1365e981e7b1SThomas Daniel 			ring = &dev_priv->ring[RCS];
1366abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1367e981e7b1SThomas Daniel 				notify_ring(dev, ring);
1368e981e7b1SThomas Daniel 			if (rcs & GT_CONTEXT_SWITCH_INTERRUPT)
1369e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1370e981e7b1SThomas Daniel 
1371e981e7b1SThomas Daniel 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1372e981e7b1SThomas Daniel 			ring = &dev_priv->ring[BCS];
1373abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1374e981e7b1SThomas Daniel 				notify_ring(dev, ring);
1375e981e7b1SThomas Daniel 			if (bcs & GT_CONTEXT_SWITCH_INTERRUPT)
1376e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1377abd58f01SBen Widawsky 		} else
1378abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1379abd58f01SBen Widawsky 	}
1380abd58f01SBen Widawsky 
138185f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1382abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1383abd58f01SBen Widawsky 		if (tmp) {
138438cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1385abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1386e981e7b1SThomas Daniel 
1387abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1388e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VCS];
1389abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1390e981e7b1SThomas Daniel 				notify_ring(dev, ring);
139173d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
1392e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1393e981e7b1SThomas Daniel 
139485f9b5f9SZhao Yakui 			vcs = tmp >> GEN8_VCS2_IRQ_SHIFT;
1395e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VCS2];
139685f9b5f9SZhao Yakui 			if (vcs & GT_RENDER_USER_INTERRUPT)
1397e981e7b1SThomas Daniel 				notify_ring(dev, ring);
139873d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
1399e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1400abd58f01SBen Widawsky 		} else
1401abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1402abd58f01SBen Widawsky 	}
1403abd58f01SBen Widawsky 
14040961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
14050961021aSBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(2));
14060961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
14070961021aSBen Widawsky 			I915_WRITE(GEN8_GT_IIR(2),
14080961021aSBen Widawsky 				   tmp & dev_priv->pm_rps_events);
140938cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
1410c9a9a268SImre Deak 			gen6_rps_irq_handler(dev_priv, tmp);
14110961021aSBen Widawsky 		} else
14120961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
14130961021aSBen Widawsky 	}
14140961021aSBen Widawsky 
1415abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1416abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1417abd58f01SBen Widawsky 		if (tmp) {
141838cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1419abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1420e981e7b1SThomas Daniel 
1421abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1422e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VECS];
1423abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1424e981e7b1SThomas Daniel 				notify_ring(dev, ring);
142573d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
1426e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1427abd58f01SBen Widawsky 		} else
1428abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1429abd58f01SBen Widawsky 	}
1430abd58f01SBen Widawsky 
1431abd58f01SBen Widawsky 	return ret;
1432abd58f01SBen Widawsky }
1433abd58f01SBen Widawsky 
1434b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1435b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1436b543fb04SEgbert Eich 
143707c338ceSJani Nikula static int pch_port_to_hotplug_shift(enum port port)
143813cf5504SDave Airlie {
143913cf5504SDave Airlie 	switch (port) {
144013cf5504SDave Airlie 	case PORT_A:
144113cf5504SDave Airlie 	case PORT_E:
144213cf5504SDave Airlie 	default:
144313cf5504SDave Airlie 		return -1;
144413cf5504SDave Airlie 	case PORT_B:
144513cf5504SDave Airlie 		return 0;
144613cf5504SDave Airlie 	case PORT_C:
144713cf5504SDave Airlie 		return 8;
144813cf5504SDave Airlie 	case PORT_D:
144913cf5504SDave Airlie 		return 16;
145013cf5504SDave Airlie 	}
145113cf5504SDave Airlie }
145213cf5504SDave Airlie 
145307c338ceSJani Nikula static int i915_port_to_hotplug_shift(enum port port)
145413cf5504SDave Airlie {
145513cf5504SDave Airlie 	switch (port) {
145613cf5504SDave Airlie 	case PORT_A:
145713cf5504SDave Airlie 	case PORT_E:
145813cf5504SDave Airlie 	default:
145913cf5504SDave Airlie 		return -1;
146013cf5504SDave Airlie 	case PORT_B:
146113cf5504SDave Airlie 		return 17;
146213cf5504SDave Airlie 	case PORT_C:
146313cf5504SDave Airlie 		return 19;
146413cf5504SDave Airlie 	case PORT_D:
146513cf5504SDave Airlie 		return 21;
146613cf5504SDave Airlie 	}
146713cf5504SDave Airlie }
146813cf5504SDave Airlie 
146913cf5504SDave Airlie static inline enum port get_port_from_pin(enum hpd_pin pin)
147013cf5504SDave Airlie {
147113cf5504SDave Airlie 	switch (pin) {
147213cf5504SDave Airlie 	case HPD_PORT_B:
147313cf5504SDave Airlie 		return PORT_B;
147413cf5504SDave Airlie 	case HPD_PORT_C:
147513cf5504SDave Airlie 		return PORT_C;
147613cf5504SDave Airlie 	case HPD_PORT_D:
147713cf5504SDave Airlie 		return PORT_D;
147813cf5504SDave Airlie 	default:
147913cf5504SDave Airlie 		return PORT_A; /* no hpd */
148013cf5504SDave Airlie 	}
148113cf5504SDave Airlie }
148213cf5504SDave Airlie 
148310a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1484b543fb04SEgbert Eich 					 u32 hotplug_trigger,
148513cf5504SDave Airlie 					 u32 dig_hotplug_reg,
1486b543fb04SEgbert Eich 					 const u32 *hpd)
1487b543fb04SEgbert Eich {
14882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1489b543fb04SEgbert Eich 	int i;
149013cf5504SDave Airlie 	enum port port;
149110a504deSDaniel Vetter 	bool storm_detected = false;
149213cf5504SDave Airlie 	bool queue_dig = false, queue_hp = false;
149313cf5504SDave Airlie 	u32 dig_shift;
149413cf5504SDave Airlie 	u32 dig_port_mask = 0;
1495b543fb04SEgbert Eich 
149691d131d2SDaniel Vetter 	if (!hotplug_trigger)
149791d131d2SDaniel Vetter 		return;
149891d131d2SDaniel Vetter 
149913cf5504SDave Airlie 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x\n",
150013cf5504SDave Airlie 			 hotplug_trigger, dig_hotplug_reg);
1501cc9bd499SImre Deak 
1502b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1503b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
150413cf5504SDave Airlie 		if (!(hpd[i] & hotplug_trigger))
150513cf5504SDave Airlie 			continue;
1506821450c6SEgbert Eich 
150713cf5504SDave Airlie 		port = get_port_from_pin(i);
150813cf5504SDave Airlie 		if (port && dev_priv->hpd_irq_port[port]) {
150913cf5504SDave Airlie 			bool long_hpd;
151013cf5504SDave Airlie 
151107c338ceSJani Nikula 			if (HAS_PCH_SPLIT(dev)) {
151207c338ceSJani Nikula 				dig_shift = pch_port_to_hotplug_shift(port);
151313cf5504SDave Airlie 				long_hpd = (dig_hotplug_reg >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
151407c338ceSJani Nikula 			} else {
151507c338ceSJani Nikula 				dig_shift = i915_port_to_hotplug_shift(port);
151607c338ceSJani Nikula 				long_hpd = (hotplug_trigger >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
151713cf5504SDave Airlie 			}
151813cf5504SDave Airlie 
151926fbb774SVille Syrjälä 			DRM_DEBUG_DRIVER("digital hpd port %c - %s\n",
152026fbb774SVille Syrjälä 					 port_name(port),
152126fbb774SVille Syrjälä 					 long_hpd ? "long" : "short");
152213cf5504SDave Airlie 			/* for long HPD pulses we want to have the digital queue happen,
152313cf5504SDave Airlie 			   but we still want HPD storm detection to function. */
152413cf5504SDave Airlie 			if (long_hpd) {
152513cf5504SDave Airlie 				dev_priv->long_hpd_port_mask |= (1 << port);
152613cf5504SDave Airlie 				dig_port_mask |= hpd[i];
152713cf5504SDave Airlie 			} else {
152813cf5504SDave Airlie 				/* for short HPD just trigger the digital queue */
152913cf5504SDave Airlie 				dev_priv->short_hpd_port_mask |= (1 << port);
153013cf5504SDave Airlie 				hotplug_trigger &= ~hpd[i];
153113cf5504SDave Airlie 			}
153213cf5504SDave Airlie 			queue_dig = true;
153313cf5504SDave Airlie 		}
153413cf5504SDave Airlie 	}
153513cf5504SDave Airlie 
153613cf5504SDave Airlie 	for (i = 1; i < HPD_NUM_PINS; i++) {
15373ff04a16SDaniel Vetter 		if (hpd[i] & hotplug_trigger &&
15383ff04a16SDaniel Vetter 		    dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
15393ff04a16SDaniel Vetter 			/*
15403ff04a16SDaniel Vetter 			 * On GMCH platforms the interrupt mask bits only
15413ff04a16SDaniel Vetter 			 * prevent irq generation, not the setting of the
15423ff04a16SDaniel Vetter 			 * hotplug bits itself. So only WARN about unexpected
15433ff04a16SDaniel Vetter 			 * interrupts on saner platforms.
15443ff04a16SDaniel Vetter 			 */
15453ff04a16SDaniel Vetter 			WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
1546cba1c073SChris Wilson 				  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1547cba1c073SChris Wilson 				  hotplug_trigger, i, hpd[i]);
1548b8f102e8SEgbert Eich 
15493ff04a16SDaniel Vetter 			continue;
15503ff04a16SDaniel Vetter 		}
15513ff04a16SDaniel Vetter 
1552b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1553b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1554b543fb04SEgbert Eich 			continue;
1555b543fb04SEgbert Eich 
155613cf5504SDave Airlie 		if (!(dig_port_mask & hpd[i])) {
1557bc5ead8cSJani Nikula 			dev_priv->hpd_event_bits |= (1 << i);
155813cf5504SDave Airlie 			queue_hp = true;
155913cf5504SDave Airlie 		}
156013cf5504SDave Airlie 
1561b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1562b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1563b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1564b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1565b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1566b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1567b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1568b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1569142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1570b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
157110a504deSDaniel Vetter 			storm_detected = true;
1572b543fb04SEgbert Eich 		} else {
1573b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1574b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1575b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1576b543fb04SEgbert Eich 		}
1577b543fb04SEgbert Eich 	}
1578b543fb04SEgbert Eich 
157910a504deSDaniel Vetter 	if (storm_detected)
158010a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1581b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
15825876fa0dSDaniel Vetter 
1583645416f5SDaniel Vetter 	/*
1584645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1585645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1586645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1587645416f5SDaniel Vetter 	 * deadlock.
1588645416f5SDaniel Vetter 	 */
158913cf5504SDave Airlie 	if (queue_dig)
15900e32b39cSDave Airlie 		queue_work(dev_priv->dp_wq, &dev_priv->dig_port_work);
159113cf5504SDave Airlie 	if (queue_hp)
1592645416f5SDaniel Vetter 		schedule_work(&dev_priv->hotplug_work);
1593b543fb04SEgbert Eich }
1594b543fb04SEgbert Eich 
1595515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1596515ac2bbSDaniel Vetter {
15972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
159828c70f16SDaniel Vetter 
159928c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1600515ac2bbSDaniel Vetter }
1601515ac2bbSDaniel Vetter 
1602ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1603ce99c256SDaniel Vetter {
16042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
16059ee32feaSDaniel Vetter 
16069ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1607ce99c256SDaniel Vetter }
1608ce99c256SDaniel Vetter 
16098bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1610277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1611eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1612eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
16138bc5e955SDaniel Vetter 					 uint32_t crc4)
16148bf1e9f1SShuang He {
16158bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
16168bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
16178bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1618ac2300d4SDamien Lespiau 	int head, tail;
1619b2c88f5bSDamien Lespiau 
1620d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1621d538bbdfSDamien Lespiau 
16220c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1623d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1624*34273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
16250c912c79SDamien Lespiau 		return;
16260c912c79SDamien Lespiau 	}
16270c912c79SDamien Lespiau 
1628d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1629d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1630b2c88f5bSDamien Lespiau 
1631b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1632d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1633b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1634b2c88f5bSDamien Lespiau 		return;
1635b2c88f5bSDamien Lespiau 	}
1636b2c88f5bSDamien Lespiau 
1637b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
16388bf1e9f1SShuang He 
16398bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1640eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1641eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1642eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1643eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1644eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1645b2c88f5bSDamien Lespiau 
1646b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1647d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1648d538bbdfSDamien Lespiau 
1649d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
165007144428SDamien Lespiau 
165107144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
16528bf1e9f1SShuang He }
1653277de95eSDaniel Vetter #else
1654277de95eSDaniel Vetter static inline void
1655277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1656277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1657277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1658277de95eSDaniel Vetter 			     uint32_t crc4) {}
1659277de95eSDaniel Vetter #endif
1660eba94eb9SDaniel Vetter 
1661277de95eSDaniel Vetter 
1662277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
16635a69b89fSDaniel Vetter {
16645a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
16655a69b89fSDaniel Vetter 
1666277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16675a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
16685a69b89fSDaniel Vetter 				     0, 0, 0, 0);
16695a69b89fSDaniel Vetter }
16705a69b89fSDaniel Vetter 
1671277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1672eba94eb9SDaniel Vetter {
1673eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1674eba94eb9SDaniel Vetter 
1675277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1676eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1677eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1678eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1679eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16808bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1681eba94eb9SDaniel Vetter }
16825b3a856bSDaniel Vetter 
1683277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
16845b3a856bSDaniel Vetter {
16855b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
16860b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16870b5c5ed0SDaniel Vetter 
16880b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
16890b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16900b5c5ed0SDaniel Vetter 	else
16910b5c5ed0SDaniel Vetter 		res1 = 0;
16920b5c5ed0SDaniel Vetter 
16930b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
16940b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16950b5c5ed0SDaniel Vetter 	else
16960b5c5ed0SDaniel Vetter 		res2 = 0;
16975b3a856bSDaniel Vetter 
1698277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16990b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
17000b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
17010b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
17020b5c5ed0SDaniel Vetter 				     res1, res2);
17035b3a856bSDaniel Vetter }
17048bf1e9f1SShuang He 
17051403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
17061403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
17071403c0d4SPaulo Zanoni  * the work queue. */
17081403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1709baf02a1fSBen Widawsky {
17104a74de82SImre Deak 	/* TODO: RPS on GEN9+ is not supported yet. */
17114a74de82SImre Deak 	if (WARN_ONCE(INTEL_INFO(dev_priv)->gen >= 9,
17124a74de82SImre Deak 		      "GEN9+: unexpected RPS IRQ\n"))
1713132f3f17SImre Deak 		return;
1714132f3f17SImre Deak 
1715a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
171659cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1717480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1718d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1719d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
17202adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
172141a05a3aSDaniel Vetter 		}
1722d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1723d4d70aa5SImre Deak 	}
1724baf02a1fSBen Widawsky 
1725c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1726c9a9a268SImre Deak 		return;
1727c9a9a268SImre Deak 
17281403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
172912638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
173012638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
173112638c57SBen Widawsky 
1732aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1733aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
173412638c57SBen Widawsky 	}
17351403c0d4SPaulo Zanoni }
1736baf02a1fSBen Widawsky 
17378d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
17388d7849dbSVille Syrjälä {
17398d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
17408d7849dbSVille Syrjälä 		return false;
17418d7849dbSVille Syrjälä 
17428d7849dbSVille Syrjälä 	return true;
17438d7849dbSVille Syrjälä }
17448d7849dbSVille Syrjälä 
1745c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
17467e231dbeSJesse Barnes {
1747c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
174891d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
17497e231dbeSJesse Barnes 	int pipe;
17507e231dbeSJesse Barnes 
175158ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1752055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
175391d181ddSImre Deak 		int reg;
1754bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
175591d181ddSImre Deak 
1756bbb5eebfSDaniel Vetter 		/*
1757bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1758bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1759bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1760bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1761bbb5eebfSDaniel Vetter 		 * handle.
1762bbb5eebfSDaniel Vetter 		 */
17630f239f4cSDaniel Vetter 
17640f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
17650f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1766bbb5eebfSDaniel Vetter 
1767bbb5eebfSDaniel Vetter 		switch (pipe) {
1768bbb5eebfSDaniel Vetter 		case PIPE_A:
1769bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1770bbb5eebfSDaniel Vetter 			break;
1771bbb5eebfSDaniel Vetter 		case PIPE_B:
1772bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1773bbb5eebfSDaniel Vetter 			break;
17743278f67fSVille Syrjälä 		case PIPE_C:
17753278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
17763278f67fSVille Syrjälä 			break;
1777bbb5eebfSDaniel Vetter 		}
1778bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1779bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1780bbb5eebfSDaniel Vetter 
1781bbb5eebfSDaniel Vetter 		if (!mask)
178291d181ddSImre Deak 			continue;
178391d181ddSImre Deak 
178491d181ddSImre Deak 		reg = PIPESTAT(pipe);
1785bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1786bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
17877e231dbeSJesse Barnes 
17887e231dbeSJesse Barnes 		/*
17897e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
17907e231dbeSJesse Barnes 		 */
179191d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
179291d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
17937e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
17947e231dbeSJesse Barnes 	}
179558ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
17967e231dbeSJesse Barnes 
1797055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1798d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1799d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1800d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
180131acc7f5SJesse Barnes 
1802579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
180331acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
180431acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
180531acc7f5SJesse Barnes 		}
18064356d586SDaniel Vetter 
18074356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1808277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
18092d9d2b0bSVille Syrjälä 
18101f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
18111f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
181231acc7f5SJesse Barnes 	}
181331acc7f5SJesse Barnes 
1814c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1815c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1816c1874ed7SImre Deak }
1817c1874ed7SImre Deak 
181816c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
181916c6c56bSVille Syrjälä {
182016c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
182116c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
182216c6c56bSVille Syrjälä 
18233ff60f89SOscar Mateo 	if (hotplug_status) {
18243ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
18253ff60f89SOscar Mateo 		/*
18263ff60f89SOscar Mateo 		 * Make sure hotplug status is cleared before we clear IIR, or else we
18273ff60f89SOscar Mateo 		 * may miss hotplug events.
18283ff60f89SOscar Mateo 		 */
18293ff60f89SOscar Mateo 		POSTING_READ(PORT_HOTPLUG_STAT);
18303ff60f89SOscar Mateo 
183116c6c56bSVille Syrjälä 		if (IS_G4X(dev)) {
183216c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
183316c6c56bSVille Syrjälä 
183413cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_g4x);
183516c6c56bSVille Syrjälä 		} else {
183616c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
183716c6c56bSVille Syrjälä 
183813cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_i915);
183916c6c56bSVille Syrjälä 		}
184016c6c56bSVille Syrjälä 
184116c6c56bSVille Syrjälä 		if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
184216c6c56bSVille Syrjälä 		    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
184316c6c56bSVille Syrjälä 			dp_aux_irq_handler(dev);
18443ff60f89SOscar Mateo 	}
184516c6c56bSVille Syrjälä }
184616c6c56bSVille Syrjälä 
1847c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1848c1874ed7SImre Deak {
184945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
18502d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1851c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1852c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1853c1874ed7SImre Deak 
1854c1874ed7SImre Deak 	while (true) {
18553ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
18563ff60f89SOscar Mateo 
1857c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
18583ff60f89SOscar Mateo 		if (gt_iir)
18593ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
18603ff60f89SOscar Mateo 
1861c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
18623ff60f89SOscar Mateo 		if (pm_iir)
18633ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
18643ff60f89SOscar Mateo 
18653ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
18663ff60f89SOscar Mateo 		if (iir) {
18673ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
18683ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
18693ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
18703ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
18713ff60f89SOscar Mateo 		}
1872c1874ed7SImre Deak 
1873c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1874c1874ed7SImre Deak 			goto out;
1875c1874ed7SImre Deak 
1876c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1877c1874ed7SImre Deak 
18783ff60f89SOscar Mateo 		if (gt_iir)
1879c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
188060611c13SPaulo Zanoni 		if (pm_iir)
1881d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
18823ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
18833ff60f89SOscar Mateo 		 * signalled in iir */
18843ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
18857e231dbeSJesse Barnes 	}
18867e231dbeSJesse Barnes 
18877e231dbeSJesse Barnes out:
18887e231dbeSJesse Barnes 	return ret;
18897e231dbeSJesse Barnes }
18907e231dbeSJesse Barnes 
189143f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
189243f328d7SVille Syrjälä {
189345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
189443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
189543f328d7SVille Syrjälä 	u32 master_ctl, iir;
189643f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
189743f328d7SVille Syrjälä 
18988e5fd599SVille Syrjälä 	for (;;) {
18998e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
19003278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
19013278f67fSVille Syrjälä 
19023278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
19038e5fd599SVille Syrjälä 			break;
190443f328d7SVille Syrjälä 
190527b6c122SOscar Mateo 		ret = IRQ_HANDLED;
190627b6c122SOscar Mateo 
190743f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
190843f328d7SVille Syrjälä 
190927b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
191027b6c122SOscar Mateo 
191127b6c122SOscar Mateo 		if (iir) {
191227b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
191327b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
191427b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
191527b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
191627b6c122SOscar Mateo 		}
191727b6c122SOscar Mateo 
19183278f67fSVille Syrjälä 		gen8_gt_irq_handler(dev, dev_priv, master_ctl);
191943f328d7SVille Syrjälä 
192027b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
192127b6c122SOscar Mateo 		 * signalled in iir */
19223278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
192343f328d7SVille Syrjälä 
192443f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
192543f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
19268e5fd599SVille Syrjälä 	}
19273278f67fSVille Syrjälä 
192843f328d7SVille Syrjälä 	return ret;
192943f328d7SVille Syrjälä }
193043f328d7SVille Syrjälä 
193123e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1932776ad806SJesse Barnes {
19332d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
19349db4a9c7SJesse Barnes 	int pipe;
1935b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
193613cf5504SDave Airlie 	u32 dig_hotplug_reg;
1937776ad806SJesse Barnes 
193813cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
193913cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
194013cf5504SDave Airlie 
194113cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_ibx);
194291d131d2SDaniel Vetter 
1943cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1944cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1945776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1946cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1947cfc33bf7SVille Syrjälä 				 port_name(port));
1948cfc33bf7SVille Syrjälä 	}
1949776ad806SJesse Barnes 
1950ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1951ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1952ce99c256SDaniel Vetter 
1953776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1954515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1955776ad806SJesse Barnes 
1956776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1957776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1958776ad806SJesse Barnes 
1959776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1960776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1961776ad806SJesse Barnes 
1962776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1963776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1964776ad806SJesse Barnes 
19659db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1966055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
19679db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
19689db4a9c7SJesse Barnes 					 pipe_name(pipe),
19699db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1970776ad806SJesse Barnes 
1971776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1972776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1973776ad806SJesse Barnes 
1974776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1975776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1976776ad806SJesse Barnes 
1977776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
19781f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19798664281bSPaulo Zanoni 
19808664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19811f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19828664281bSPaulo Zanoni }
19838664281bSPaulo Zanoni 
19848664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
19858664281bSPaulo Zanoni {
19868664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19878664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19885a69b89fSDaniel Vetter 	enum pipe pipe;
19898664281bSPaulo Zanoni 
1990de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1991de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1992de032bf4SPaulo Zanoni 
1993055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19941f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19951f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19968664281bSPaulo Zanoni 
19975a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19985a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1999277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
20005a69b89fSDaniel Vetter 			else
2001277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
20025a69b89fSDaniel Vetter 		}
20035a69b89fSDaniel Vetter 	}
20048bf1e9f1SShuang He 
20058664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
20068664281bSPaulo Zanoni }
20078664281bSPaulo Zanoni 
20088664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
20098664281bSPaulo Zanoni {
20108664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
20118664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
20128664281bSPaulo Zanoni 
2013de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2014de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2015de032bf4SPaulo Zanoni 
20168664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
20171f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
20188664281bSPaulo Zanoni 
20198664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
20201f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
20218664281bSPaulo Zanoni 
20228664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
20231f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
20248664281bSPaulo Zanoni 
20258664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2026776ad806SJesse Barnes }
2027776ad806SJesse Barnes 
202823e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
202923e81d69SAdam Jackson {
20302d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
203123e81d69SAdam Jackson 	int pipe;
2032b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
203313cf5504SDave Airlie 	u32 dig_hotplug_reg;
203423e81d69SAdam Jackson 
203513cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
203613cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
203713cf5504SDave Airlie 
203813cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_cpt);
203991d131d2SDaniel Vetter 
2040cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2041cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
204223e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2043cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2044cfc33bf7SVille Syrjälä 				 port_name(port));
2045cfc33bf7SVille Syrjälä 	}
204623e81d69SAdam Jackson 
204723e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
2048ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
204923e81d69SAdam Jackson 
205023e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
2051515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
205223e81d69SAdam Jackson 
205323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
205423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
205523e81d69SAdam Jackson 
205623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
205723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
205823e81d69SAdam Jackson 
205923e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2060055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
206123e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
206223e81d69SAdam Jackson 					 pipe_name(pipe),
206323e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
20648664281bSPaulo Zanoni 
20658664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
20668664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
206723e81d69SAdam Jackson }
206823e81d69SAdam Jackson 
2069c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
2070c008bc6eSPaulo Zanoni {
2071c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
207240da17c2SDaniel Vetter 	enum pipe pipe;
2073c008bc6eSPaulo Zanoni 
2074c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2075c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2076c008bc6eSPaulo Zanoni 
2077c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2078c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2079c008bc6eSPaulo Zanoni 
2080c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2081c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2082c008bc6eSPaulo Zanoni 
2083055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2084d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2085d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2086d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2087c008bc6eSPaulo Zanoni 
208840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20891f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2090c008bc6eSPaulo Zanoni 
209140da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
209240da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20935b3a856bSDaniel Vetter 
209440da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
209540da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
209640da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
209740da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2098c008bc6eSPaulo Zanoni 		}
2099c008bc6eSPaulo Zanoni 	}
2100c008bc6eSPaulo Zanoni 
2101c008bc6eSPaulo Zanoni 	/* check event from PCH */
2102c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2103c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2104c008bc6eSPaulo Zanoni 
2105c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2106c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2107c008bc6eSPaulo Zanoni 		else
2108c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2109c008bc6eSPaulo Zanoni 
2110c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2111c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2112c008bc6eSPaulo Zanoni 	}
2113c008bc6eSPaulo Zanoni 
2114c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2115c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2116c008bc6eSPaulo Zanoni }
2117c008bc6eSPaulo Zanoni 
21189719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
21199719fb98SPaulo Zanoni {
21209719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
212107d27e20SDamien Lespiau 	enum pipe pipe;
21229719fb98SPaulo Zanoni 
21239719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
21249719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
21259719fb98SPaulo Zanoni 
21269719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
21279719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
21289719fb98SPaulo Zanoni 
21299719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
21309719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
21319719fb98SPaulo Zanoni 
2132055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2133d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2134d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2135d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
213640da17c2SDaniel Vetter 
213740da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
213807d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
213907d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
214007d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
21419719fb98SPaulo Zanoni 		}
21429719fb98SPaulo Zanoni 	}
21439719fb98SPaulo Zanoni 
21449719fb98SPaulo Zanoni 	/* check event from PCH */
21459719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
21469719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
21479719fb98SPaulo Zanoni 
21489719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
21499719fb98SPaulo Zanoni 
21509719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21519719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
21529719fb98SPaulo Zanoni 	}
21539719fb98SPaulo Zanoni }
21549719fb98SPaulo Zanoni 
215572c90f62SOscar Mateo /*
215672c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
215772c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
215872c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
215972c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
216072c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
216172c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
216272c90f62SOscar Mateo  */
2163f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2164b1f14ad0SJesse Barnes {
216545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
21662d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2167f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21680e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2169b1f14ad0SJesse Barnes 
21708664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
21718664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2172907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
21738664281bSPaulo Zanoni 
2174b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2175b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2176b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
217723a78516SPaulo Zanoni 	POSTING_READ(DEIER);
21780e43406bSChris Wilson 
217944498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
218044498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
218144498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
218244498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
218344498aeaSPaulo Zanoni 	 * due to its back queue). */
2184ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
218544498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
218644498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
218744498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2188ab5c608bSBen Widawsky 	}
218944498aeaSPaulo Zanoni 
219072c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
219172c90f62SOscar Mateo 
21920e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
21930e43406bSChris Wilson 	if (gt_iir) {
219472c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
219572c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2196d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
21970e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2198d8fc8a47SPaulo Zanoni 		else
2199d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
22000e43406bSChris Wilson 	}
2201b1f14ad0SJesse Barnes 
2202b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
22030e43406bSChris Wilson 	if (de_iir) {
220472c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
220572c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2206f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
22079719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2208f1af8fc1SPaulo Zanoni 		else
2209f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
22100e43406bSChris Wilson 	}
22110e43406bSChris Wilson 
2212f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2213f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
22140e43406bSChris Wilson 		if (pm_iir) {
2215b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
22160e43406bSChris Wilson 			ret = IRQ_HANDLED;
221772c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
22180e43406bSChris Wilson 		}
2219f1af8fc1SPaulo Zanoni 	}
2220b1f14ad0SJesse Barnes 
2221b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2222b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2223ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
222444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
222544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2226ab5c608bSBen Widawsky 	}
2227b1f14ad0SJesse Barnes 
2228b1f14ad0SJesse Barnes 	return ret;
2229b1f14ad0SJesse Barnes }
2230b1f14ad0SJesse Barnes 
2231abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2232abd58f01SBen Widawsky {
2233abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2234abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2235abd58f01SBen Widawsky 	u32 master_ctl;
2236abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2237abd58f01SBen Widawsky 	uint32_t tmp = 0;
2238c42664ccSDaniel Vetter 	enum pipe pipe;
223988e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
224088e04703SJesse Barnes 
224188e04703SJesse Barnes 	if (IS_GEN9(dev))
224288e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
224388e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2244abd58f01SBen Widawsky 
2245abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
2246abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2247abd58f01SBen Widawsky 	if (!master_ctl)
2248abd58f01SBen Widawsky 		return IRQ_NONE;
2249abd58f01SBen Widawsky 
2250abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2251abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2252abd58f01SBen Widawsky 
225338cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
225438cc46d7SOscar Mateo 
2255abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
2256abd58f01SBen Widawsky 
2257abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2258abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2259abd58f01SBen Widawsky 		if (tmp) {
2260abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2261abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
226238cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
226338cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
226438cc46d7SOscar Mateo 			else
226538cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2266abd58f01SBen Widawsky 		}
226738cc46d7SOscar Mateo 		else
226838cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2269abd58f01SBen Widawsky 	}
2270abd58f01SBen Widawsky 
22716d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
22726d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
22736d766f02SDaniel Vetter 		if (tmp) {
22746d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
22756d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
227688e04703SJesse Barnes 
227788e04703SJesse Barnes 			if (tmp & aux_mask)
227838cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
227938cc46d7SOscar Mateo 			else
228038cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22816d766f02SDaniel Vetter 		}
228238cc46d7SOscar Mateo 		else
228338cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22846d766f02SDaniel Vetter 	}
22856d766f02SDaniel Vetter 
2286055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2287770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2288abd58f01SBen Widawsky 
2289c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2290c42664ccSDaniel Vetter 			continue;
2291c42664ccSDaniel Vetter 
2292abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
229338cc46d7SOscar Mateo 		if (pipe_iir) {
229438cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
229538cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2296770de83dSDamien Lespiau 
2297d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2298d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2299d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2300abd58f01SBen Widawsky 
2301770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2302770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2303770de83dSDamien Lespiau 			else
2304770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2305770de83dSDamien Lespiau 
2306770de83dSDamien Lespiau 			if (flip_done) {
2307abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2308abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2309abd58f01SBen Widawsky 			}
2310abd58f01SBen Widawsky 
23110fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
23120fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
23130fbe7870SDaniel Vetter 
23141f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
23151f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
23161f7247c0SDaniel Vetter 								    pipe);
231738d83c96SDaniel Vetter 
2318770de83dSDamien Lespiau 
2319770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2320770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2321770de83dSDamien Lespiau 			else
2322770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2323770de83dSDamien Lespiau 
2324770de83dSDamien Lespiau 			if (fault_errors)
232530100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
232630100f2bSDaniel Vetter 					  pipe_name(pipe),
232730100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2328c42664ccSDaniel Vetter 		} else
2329abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2330abd58f01SBen Widawsky 	}
2331abd58f01SBen Widawsky 
233292d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
233392d03a80SDaniel Vetter 		/*
233492d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
233592d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
233692d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
233792d03a80SDaniel Vetter 		 */
233892d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
233992d03a80SDaniel Vetter 		if (pch_iir) {
234092d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
234192d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
234238cc46d7SOscar Mateo 			cpt_irq_handler(dev, pch_iir);
234338cc46d7SOscar Mateo 		} else
234438cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
234538cc46d7SOscar Mateo 
234692d03a80SDaniel Vetter 	}
234792d03a80SDaniel Vetter 
2348abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2349abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2350abd58f01SBen Widawsky 
2351abd58f01SBen Widawsky 	return ret;
2352abd58f01SBen Widawsky }
2353abd58f01SBen Widawsky 
235417e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
235517e1df07SDaniel Vetter 			       bool reset_completed)
235617e1df07SDaniel Vetter {
2357a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
235817e1df07SDaniel Vetter 	int i;
235917e1df07SDaniel Vetter 
236017e1df07SDaniel Vetter 	/*
236117e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
236217e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
236317e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
236417e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
236517e1df07SDaniel Vetter 	 */
236617e1df07SDaniel Vetter 
236717e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
236817e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
236917e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
237017e1df07SDaniel Vetter 
237117e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
237217e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
237317e1df07SDaniel Vetter 
237417e1df07SDaniel Vetter 	/*
237517e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
237617e1df07SDaniel Vetter 	 * reset state is cleared.
237717e1df07SDaniel Vetter 	 */
237817e1df07SDaniel Vetter 	if (reset_completed)
237917e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
238017e1df07SDaniel Vetter }
238117e1df07SDaniel Vetter 
23828a905236SJesse Barnes /**
23838a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
23848a905236SJesse Barnes  * @work: work struct
23858a905236SJesse Barnes  *
23868a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23878a905236SJesse Barnes  * was detected.
23888a905236SJesse Barnes  */
23898a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
23908a905236SJesse Barnes {
23911f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
23921f83fee0SDaniel Vetter 						    work);
23932d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
23942d1013ddSJani Nikula 		container_of(error, struct drm_i915_private, gpu_error);
23958a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
2396cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2397cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2398cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
239917e1df07SDaniel Vetter 	int ret;
24008a905236SJesse Barnes 
24015bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
24028a905236SJesse Barnes 
24037db0ba24SDaniel Vetter 	/*
24047db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
24057db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
24067db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
24077db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
24087db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
24097db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
24107db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
24117db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
24127db0ba24SDaniel Vetter 	 */
24137db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
241444d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
24155bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
24167db0ba24SDaniel Vetter 				   reset_event);
24171f83fee0SDaniel Vetter 
241817e1df07SDaniel Vetter 		/*
2419f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2420f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2421f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2422f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2423f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2424f454c694SImre Deak 		 */
2425f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
24267514747dSVille Syrjälä 
24277514747dSVille Syrjälä 		intel_prepare_reset(dev);
24287514747dSVille Syrjälä 
2429f454c694SImre Deak 		/*
243017e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
243117e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
243217e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
243317e1df07SDaniel Vetter 		 * deadlocks with the reset work.
243417e1df07SDaniel Vetter 		 */
2435f69061beSDaniel Vetter 		ret = i915_reset(dev);
2436f69061beSDaniel Vetter 
24377514747dSVille Syrjälä 		intel_finish_reset(dev);
243817e1df07SDaniel Vetter 
2439f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2440f454c694SImre Deak 
2441f69061beSDaniel Vetter 		if (ret == 0) {
2442f69061beSDaniel Vetter 			/*
2443f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2444f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2445f69061beSDaniel Vetter 			 * complete.
2446f69061beSDaniel Vetter 			 *
2447f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2448f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2449f69061beSDaniel Vetter 			 * updates before
2450f69061beSDaniel Vetter 			 * the counter increment.
2451f69061beSDaniel Vetter 			 */
24524e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2453f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2454f69061beSDaniel Vetter 
24555bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2456f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
24571f83fee0SDaniel Vetter 		} else {
24582ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2459f316a42cSBen Gamari 		}
24601f83fee0SDaniel Vetter 
246117e1df07SDaniel Vetter 		/*
246217e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
246317e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
246417e1df07SDaniel Vetter 		 */
246517e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2466f316a42cSBen Gamari 	}
24678a905236SJesse Barnes }
24688a905236SJesse Barnes 
246935aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2470c0e09200SDave Airlie {
24718a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2472bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
247363eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2474050ee91fSBen Widawsky 	int pipe, i;
247563eeaf38SJesse Barnes 
247635aed2e6SChris Wilson 	if (!eir)
247735aed2e6SChris Wilson 		return;
247863eeaf38SJesse Barnes 
2479a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24808a905236SJesse Barnes 
2481bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2482bd9854f9SBen Widawsky 
24838a905236SJesse Barnes 	if (IS_G4X(dev)) {
24848a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24858a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24868a905236SJesse Barnes 
2487a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2488a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2489050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2490050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2491a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2492a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
24938a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24943143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
24958a905236SJesse Barnes 		}
24968a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
24978a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2498a70491ccSJoe Perches 			pr_err("page table error\n");
2499a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
25008a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25013143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
25028a905236SJesse Barnes 		}
25038a905236SJesse Barnes 	}
25048a905236SJesse Barnes 
2505a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
250663eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
250763eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2508a70491ccSJoe Perches 			pr_err("page table error\n");
2509a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
251063eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25113143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
251263eeaf38SJesse Barnes 		}
25138a905236SJesse Barnes 	}
25148a905236SJesse Barnes 
251563eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2516a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2517055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2518a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
25199db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
252063eeaf38SJesse Barnes 		/* pipestat has already been acked */
252163eeaf38SJesse Barnes 	}
252263eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2523a70491ccSJoe Perches 		pr_err("instruction error\n");
2524a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2525050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2526050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2527a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
252863eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
252963eeaf38SJesse Barnes 
2530a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2531a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2532a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
253363eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
25343143a2bfSChris Wilson 			POSTING_READ(IPEIR);
253563eeaf38SJesse Barnes 		} else {
253663eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
253763eeaf38SJesse Barnes 
2538a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2539a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2540a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2541a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
254263eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25433143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
254463eeaf38SJesse Barnes 		}
254563eeaf38SJesse Barnes 	}
254663eeaf38SJesse Barnes 
254763eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
25483143a2bfSChris Wilson 	POSTING_READ(EIR);
254963eeaf38SJesse Barnes 	eir = I915_READ(EIR);
255063eeaf38SJesse Barnes 	if (eir) {
255163eeaf38SJesse Barnes 		/*
255263eeaf38SJesse Barnes 		 * some errors might have become stuck,
255363eeaf38SJesse Barnes 		 * mask them.
255463eeaf38SJesse Barnes 		 */
255563eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
255663eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
255763eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
255863eeaf38SJesse Barnes 	}
255935aed2e6SChris Wilson }
256035aed2e6SChris Wilson 
256135aed2e6SChris Wilson /**
256235aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
256335aed2e6SChris Wilson  * @dev: drm device
256435aed2e6SChris Wilson  *
256535aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
256635aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
256735aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
256835aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
256935aed2e6SChris Wilson  * of a ring dump etc.).
257035aed2e6SChris Wilson  */
257158174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
257258174462SMika Kuoppala 		       const char *fmt, ...)
257335aed2e6SChris Wilson {
257435aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
257558174462SMika Kuoppala 	va_list args;
257658174462SMika Kuoppala 	char error_msg[80];
257735aed2e6SChris Wilson 
257858174462SMika Kuoppala 	va_start(args, fmt);
257958174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
258058174462SMika Kuoppala 	va_end(args);
258158174462SMika Kuoppala 
258258174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
258335aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25848a905236SJesse Barnes 
2585ba1234d1SBen Gamari 	if (wedged) {
2586f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2587f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2588ba1234d1SBen Gamari 
258911ed50ecSBen Gamari 		/*
259017e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
259117e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
259217e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
259317e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
259417e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
259517e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
259617e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
259717e1df07SDaniel Vetter 		 *
259817e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
259917e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
260017e1df07SDaniel Vetter 		 * counter atomic_t.
260111ed50ecSBen Gamari 		 */
260217e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
260311ed50ecSBen Gamari 	}
260411ed50ecSBen Gamari 
2605122f46baSDaniel Vetter 	/*
2606122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
2607122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
2608122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2609122f46baSDaniel Vetter 	 * code will deadlock.
2610122f46baSDaniel Vetter 	 */
2611122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
26128a905236SJesse Barnes }
26138a905236SJesse Barnes 
261442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
261542f52ef8SKeith Packard  * we use as a pipe index
261642f52ef8SKeith Packard  */
2617f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
26180a3e67a4SJesse Barnes {
26192d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2620e9d21d7fSKeith Packard 	unsigned long irqflags;
262171e0ffa5SJesse Barnes 
26225eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
262371e0ffa5SJesse Barnes 		return -EINVAL;
26240a3e67a4SJesse Barnes 
26251ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2626f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
26277c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2628755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
26290a3e67a4SJesse Barnes 	else
26307c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2631755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
26321ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26338692d00eSChris Wilson 
26340a3e67a4SJesse Barnes 	return 0;
26350a3e67a4SJesse Barnes }
26360a3e67a4SJesse Barnes 
2637f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2638f796cf8fSJesse Barnes {
26392d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2640f796cf8fSJesse Barnes 	unsigned long irqflags;
2641b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
264240da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2643f796cf8fSJesse Barnes 
2644f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2645f796cf8fSJesse Barnes 		return -EINVAL;
2646f796cf8fSJesse Barnes 
2647f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2648b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2649b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2650b1f14ad0SJesse Barnes 
2651b1f14ad0SJesse Barnes 	return 0;
2652b1f14ad0SJesse Barnes }
2653b1f14ad0SJesse Barnes 
26547e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
26557e231dbeSJesse Barnes {
26562d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26577e231dbeSJesse Barnes 	unsigned long irqflags;
26587e231dbeSJesse Barnes 
26597e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
26607e231dbeSJesse Barnes 		return -EINVAL;
26617e231dbeSJesse Barnes 
26627e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
266331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2664755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
26657e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26667e231dbeSJesse Barnes 
26677e231dbeSJesse Barnes 	return 0;
26687e231dbeSJesse Barnes }
26697e231dbeSJesse Barnes 
2670abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2671abd58f01SBen Widawsky {
2672abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2673abd58f01SBen Widawsky 	unsigned long irqflags;
2674abd58f01SBen Widawsky 
2675abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2676abd58f01SBen Widawsky 		return -EINVAL;
2677abd58f01SBen Widawsky 
2678abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26797167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
26807167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2681abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2682abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2683abd58f01SBen Widawsky 	return 0;
2684abd58f01SBen Widawsky }
2685abd58f01SBen Widawsky 
268642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
268742f52ef8SKeith Packard  * we use as a pipe index
268842f52ef8SKeith Packard  */
2689f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26900a3e67a4SJesse Barnes {
26912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2692e9d21d7fSKeith Packard 	unsigned long irqflags;
26930a3e67a4SJesse Barnes 
26941ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26957c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2696755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2697755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26981ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26990a3e67a4SJesse Barnes }
27000a3e67a4SJesse Barnes 
2701f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2702f796cf8fSJesse Barnes {
27032d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2704f796cf8fSJesse Barnes 	unsigned long irqflags;
2705b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
270640da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2707f796cf8fSJesse Barnes 
2708f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2709b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2710b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2711b1f14ad0SJesse Barnes }
2712b1f14ad0SJesse Barnes 
27137e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
27147e231dbeSJesse Barnes {
27152d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27167e231dbeSJesse Barnes 	unsigned long irqflags;
27177e231dbeSJesse Barnes 
27187e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
271931acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2720755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27217e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27227e231dbeSJesse Barnes }
27237e231dbeSJesse Barnes 
2724abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2725abd58f01SBen Widawsky {
2726abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2727abd58f01SBen Widawsky 	unsigned long irqflags;
2728abd58f01SBen Widawsky 
2729abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2730abd58f01SBen Widawsky 		return;
2731abd58f01SBen Widawsky 
2732abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27337167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
27347167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2735abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2736abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2737abd58f01SBen Widawsky }
2738abd58f01SBen Widawsky 
2739893eead0SChris Wilson static u32
2740a4872ba6SOscar Mateo ring_last_seqno(struct intel_engine_cs *ring)
2741852835f3SZou Nan hai {
2742893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2743893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2744893eead0SChris Wilson }
2745893eead0SChris Wilson 
27469107e9d2SChris Wilson static bool
2747a4872ba6SOscar Mateo ring_idle(struct intel_engine_cs *ring, u32 seqno)
2748893eead0SChris Wilson {
27499107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
27509107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2751f65d9421SBen Gamari }
2752f65d9421SBen Gamari 
2753a028c4b0SDaniel Vetter static bool
2754a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2755a028c4b0SDaniel Vetter {
2756a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2757a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2758a028c4b0SDaniel Vetter 	} else {
2759a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2760a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2761a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2762a028c4b0SDaniel Vetter 	}
2763a028c4b0SDaniel Vetter }
2764a028c4b0SDaniel Vetter 
2765a4872ba6SOscar Mateo static struct intel_engine_cs *
2766a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2767921d42eaSDaniel Vetter {
2768921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2769a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2770921d42eaSDaniel Vetter 	int i;
2771921d42eaSDaniel Vetter 
2772921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2773a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2774a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2775a6cdb93aSRodrigo Vivi 				continue;
2776a6cdb93aSRodrigo Vivi 
2777a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2778a6cdb93aSRodrigo Vivi 				return signaller;
2779a6cdb93aSRodrigo Vivi 		}
2780921d42eaSDaniel Vetter 	} else {
2781921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2782921d42eaSDaniel Vetter 
2783921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2784921d42eaSDaniel Vetter 			if(ring == signaller)
2785921d42eaSDaniel Vetter 				continue;
2786921d42eaSDaniel Vetter 
2787ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2788921d42eaSDaniel Vetter 				return signaller;
2789921d42eaSDaniel Vetter 		}
2790921d42eaSDaniel Vetter 	}
2791921d42eaSDaniel Vetter 
2792a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2793a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2794921d42eaSDaniel Vetter 
2795921d42eaSDaniel Vetter 	return NULL;
2796921d42eaSDaniel Vetter }
2797921d42eaSDaniel Vetter 
2798a4872ba6SOscar Mateo static struct intel_engine_cs *
2799a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2800a24a11e6SChris Wilson {
2801a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
280288fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2803a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2804a6cdb93aSRodrigo Vivi 	int i, backwards;
2805a24a11e6SChris Wilson 
2806a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2807a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
28086274f212SChris Wilson 		return NULL;
2809a24a11e6SChris Wilson 
281088fe429dSDaniel Vetter 	/*
281188fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
281288fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2813a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2814a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
281588fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
281688fe429dSDaniel Vetter 	 * ringbuffer itself.
2817a24a11e6SChris Wilson 	 */
281888fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2819a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
282088fe429dSDaniel Vetter 
2821a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
282288fe429dSDaniel Vetter 		/*
282388fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
282488fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
282588fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
282688fe429dSDaniel Vetter 		 */
2827ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
282888fe429dSDaniel Vetter 
282988fe429dSDaniel Vetter 		/* This here seems to blow up */
2830ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2831a24a11e6SChris Wilson 		if (cmd == ipehr)
2832a24a11e6SChris Wilson 			break;
2833a24a11e6SChris Wilson 
283488fe429dSDaniel Vetter 		head -= 4;
283588fe429dSDaniel Vetter 	}
2836a24a11e6SChris Wilson 
283788fe429dSDaniel Vetter 	if (!i)
283888fe429dSDaniel Vetter 		return NULL;
283988fe429dSDaniel Vetter 
2840ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2841a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2842a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2843a6cdb93aSRodrigo Vivi 		offset <<= 32;
2844a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2845a6cdb93aSRodrigo Vivi 	}
2846a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2847a24a11e6SChris Wilson }
2848a24a11e6SChris Wilson 
2849a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
28506274f212SChris Wilson {
28516274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2852a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2853a0d036b0SChris Wilson 	u32 seqno;
28546274f212SChris Wilson 
28554be17381SChris Wilson 	ring->hangcheck.deadlock++;
28566274f212SChris Wilson 
28576274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
28584be17381SChris Wilson 	if (signaller == NULL)
28594be17381SChris Wilson 		return -1;
28604be17381SChris Wilson 
28614be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
28624be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
28636274f212SChris Wilson 		return -1;
28646274f212SChris Wilson 
28654be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
28664be17381SChris Wilson 		return 1;
28674be17381SChris Wilson 
2868a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2869a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2870a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
28714be17381SChris Wilson 		return -1;
28724be17381SChris Wilson 
28734be17381SChris Wilson 	return 0;
28746274f212SChris Wilson }
28756274f212SChris Wilson 
28766274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
28776274f212SChris Wilson {
2878a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
28796274f212SChris Wilson 	int i;
28806274f212SChris Wilson 
28816274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
28824be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
28836274f212SChris Wilson }
28846274f212SChris Wilson 
2885ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2886a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
28871ec14ad3SChris Wilson {
28881ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28891ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28909107e9d2SChris Wilson 	u32 tmp;
28919107e9d2SChris Wilson 
2892f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2893f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2894f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2895f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2896f260fe7bSMika Kuoppala 		}
2897f260fe7bSMika Kuoppala 
2898f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2899f260fe7bSMika Kuoppala 	}
29006274f212SChris Wilson 
29019107e9d2SChris Wilson 	if (IS_GEN2(dev))
2902f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
29039107e9d2SChris Wilson 
29049107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
29059107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
29069107e9d2SChris Wilson 	 * and break the hang. This should work on
29079107e9d2SChris Wilson 	 * all but the second generation chipsets.
29089107e9d2SChris Wilson 	 */
29099107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
29101ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
291158174462SMika Kuoppala 		i915_handle_error(dev, false,
291258174462SMika Kuoppala 				  "Kicking stuck wait on %s",
29131ec14ad3SChris Wilson 				  ring->name);
29141ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2915f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
29161ec14ad3SChris Wilson 	}
2917a24a11e6SChris Wilson 
29186274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
29196274f212SChris Wilson 		switch (semaphore_passed(ring)) {
29206274f212SChris Wilson 		default:
2921f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
29226274f212SChris Wilson 		case 1:
292358174462SMika Kuoppala 			i915_handle_error(dev, false,
292458174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2925a24a11e6SChris Wilson 					  ring->name);
2926a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2927f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
29286274f212SChris Wilson 		case 0:
2929f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
29306274f212SChris Wilson 		}
29319107e9d2SChris Wilson 	}
29329107e9d2SChris Wilson 
2933f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2934a24a11e6SChris Wilson }
2935d1e61e7fSChris Wilson 
2936f65d9421SBen Gamari /**
2937f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
293805407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
293905407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
294005407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
294105407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
294205407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2943f65d9421SBen Gamari  */
2944a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2945f65d9421SBen Gamari {
2946f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
29472d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2948a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2949b4519513SChris Wilson 	int i;
295005407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
29519107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
29529107e9d2SChris Wilson #define BUSY 1
29539107e9d2SChris Wilson #define KICK 5
29549107e9d2SChris Wilson #define HUNG 20
2955893eead0SChris Wilson 
2956d330a953SJani Nikula 	if (!i915.enable_hangcheck)
29573e0dc6b0SBen Widawsky 		return;
29583e0dc6b0SBen Widawsky 
2959b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
296050877445SChris Wilson 		u64 acthd;
296150877445SChris Wilson 		u32 seqno;
29629107e9d2SChris Wilson 		bool busy = true;
2963b4519513SChris Wilson 
29646274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
29656274f212SChris Wilson 
296605407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
296705407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
296805407ff8SMika Kuoppala 
296905407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
29709107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2971da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2972da661464SMika Kuoppala 
29739107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
29749107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2975094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2976f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
29779107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
29789107e9d2SChris Wilson 								  ring->name);
2979f4adcd24SDaniel Vetter 						else
2980f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2981f4adcd24SDaniel Vetter 								 ring->name);
29829107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2983094f9a54SChris Wilson 					}
2984094f9a54SChris Wilson 					/* Safeguard against driver failure */
2985094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
29869107e9d2SChris Wilson 				} else
29879107e9d2SChris Wilson 					busy = false;
298805407ff8SMika Kuoppala 			} else {
29896274f212SChris Wilson 				/* We always increment the hangcheck score
29906274f212SChris Wilson 				 * if the ring is busy and still processing
29916274f212SChris Wilson 				 * the same request, so that no single request
29926274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29936274f212SChris Wilson 				 * batches). The only time we do not increment
29946274f212SChris Wilson 				 * the hangcheck score on this ring, if this
29956274f212SChris Wilson 				 * ring is in a legitimate wait for another
29966274f212SChris Wilson 				 * ring. In that case the waiting ring is a
29976274f212SChris Wilson 				 * victim and we want to be sure we catch the
29986274f212SChris Wilson 				 * right culprit. Then every time we do kick
29996274f212SChris Wilson 				 * the ring, add a small increment to the
30006274f212SChris Wilson 				 * score so that we can catch a batch that is
30016274f212SChris Wilson 				 * being repeatedly kicked and so responsible
30026274f212SChris Wilson 				 * for stalling the machine.
30039107e9d2SChris Wilson 				 */
3004ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
3005ad8beaeaSMika Kuoppala 								    acthd);
3006ad8beaeaSMika Kuoppala 
3007ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
3008da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3009f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3010f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
3011f260fe7bSMika Kuoppala 					break;
3012f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
3013ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
30146274f212SChris Wilson 					break;
3015f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3016ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
30176274f212SChris Wilson 					break;
3018f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3019ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
30206274f212SChris Wilson 					stuck[i] = true;
30216274f212SChris Wilson 					break;
30226274f212SChris Wilson 				}
302305407ff8SMika Kuoppala 			}
30249107e9d2SChris Wilson 		} else {
3025da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
3026da661464SMika Kuoppala 
30279107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
30289107e9d2SChris Wilson 			 * attempts across multiple batches.
30299107e9d2SChris Wilson 			 */
30309107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
30319107e9d2SChris Wilson 				ring->hangcheck.score--;
3032f260fe7bSMika Kuoppala 
3033f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
3034cbb465e7SChris Wilson 		}
3035f65d9421SBen Gamari 
303605407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
303705407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
30389107e9d2SChris Wilson 		busy_count += busy;
303905407ff8SMika Kuoppala 	}
304005407ff8SMika Kuoppala 
304105407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
3042b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3043b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
304405407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
3045a43adf07SChris Wilson 				 ring->name);
3046a43adf07SChris Wilson 			rings_hung++;
304705407ff8SMika Kuoppala 		}
304805407ff8SMika Kuoppala 	}
304905407ff8SMika Kuoppala 
305005407ff8SMika Kuoppala 	if (rings_hung)
305158174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
305205407ff8SMika Kuoppala 
305305407ff8SMika Kuoppala 	if (busy_count)
305405407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
305505407ff8SMika Kuoppala 		 * being added */
305610cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
305710cd45b6SMika Kuoppala }
305810cd45b6SMika Kuoppala 
305910cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
306010cd45b6SMika Kuoppala {
306110cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
3062672e7b7cSChris Wilson 	struct timer_list *timer = &dev_priv->gpu_error.hangcheck_timer;
3063672e7b7cSChris Wilson 
3064d330a953SJani Nikula 	if (!i915.enable_hangcheck)
306510cd45b6SMika Kuoppala 		return;
306610cd45b6SMika Kuoppala 
3067672e7b7cSChris Wilson 	/* Don't continually defer the hangcheck, but make sure it is active */
3068d9e600b2SChris Wilson 	if (timer_pending(timer))
3069d9e600b2SChris Wilson 		return;
3070d9e600b2SChris Wilson 	mod_timer(timer,
3071d9e600b2SChris Wilson 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
3072f65d9421SBen Gamari }
3073f65d9421SBen Gamari 
30741c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
307591738a95SPaulo Zanoni {
307691738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
307791738a95SPaulo Zanoni 
307891738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
307991738a95SPaulo Zanoni 		return;
308091738a95SPaulo Zanoni 
3081f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3082105b122eSPaulo Zanoni 
3083105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3084105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3085622364b6SPaulo Zanoni }
3086105b122eSPaulo Zanoni 
308791738a95SPaulo Zanoni /*
3088622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3089622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3090622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3091622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3092622364b6SPaulo Zanoni  *
3093622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
309491738a95SPaulo Zanoni  */
3095622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3096622364b6SPaulo Zanoni {
3097622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3098622364b6SPaulo Zanoni 
3099622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3100622364b6SPaulo Zanoni 		return;
3101622364b6SPaulo Zanoni 
3102622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
310391738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
310491738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
310591738a95SPaulo Zanoni }
310691738a95SPaulo Zanoni 
31077c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3108d18ea1b5SDaniel Vetter {
3109d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3110d18ea1b5SDaniel Vetter 
3111f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3112a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3113f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3114d18ea1b5SDaniel Vetter }
3115d18ea1b5SDaniel Vetter 
3116c0e09200SDave Airlie /* drm_dma.h hooks
3117c0e09200SDave Airlie */
3118be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3119036a4a7dSZhenyu Wang {
31202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3121036a4a7dSZhenyu Wang 
31220c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3123bdfcdb63SDaniel Vetter 
3124f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3125c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3126c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3127036a4a7dSZhenyu Wang 
31287c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3129c650156aSZhenyu Wang 
31301c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
31317d99163dSBen Widawsky }
31327d99163dSBen Widawsky 
313370591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
313470591a41SVille Syrjälä {
313570591a41SVille Syrjälä 	enum pipe pipe;
313670591a41SVille Syrjälä 
313770591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
313870591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
313970591a41SVille Syrjälä 
314070591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
314170591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
314270591a41SVille Syrjälä 
314370591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
314470591a41SVille Syrjälä }
314570591a41SVille Syrjälä 
31467e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
31477e231dbeSJesse Barnes {
31482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
31497e231dbeSJesse Barnes 
31507e231dbeSJesse Barnes 	/* VLV magic */
31517e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
31527e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
31537e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
31547e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
31557e231dbeSJesse Barnes 
31567c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
31577e231dbeSJesse Barnes 
31587c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
31597e231dbeSJesse Barnes 
316070591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
31617e231dbeSJesse Barnes }
31627e231dbeSJesse Barnes 
3163d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3164d6e3cca3SDaniel Vetter {
3165d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3166d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3167d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3168d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3169d6e3cca3SDaniel Vetter }
3170d6e3cca3SDaniel Vetter 
3171823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3172abd58f01SBen Widawsky {
3173abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3174abd58f01SBen Widawsky 	int pipe;
3175abd58f01SBen Widawsky 
3176abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3177abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3178abd58f01SBen Widawsky 
3179d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3180abd58f01SBen Widawsky 
3181055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3182f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3183813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3184f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3185abd58f01SBen Widawsky 
3186f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3187f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3188f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3189abd58f01SBen Widawsky 
31901c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
3191abd58f01SBen Widawsky }
3192abd58f01SBen Widawsky 
3193d49bdb0eSPaulo Zanoni void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv)
3194d49bdb0eSPaulo Zanoni {
31951180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3196d49bdb0eSPaulo Zanoni 
319713321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3198d49bdb0eSPaulo Zanoni 	GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B, dev_priv->de_irq_mask[PIPE_B],
31991180e206SPaulo Zanoni 			  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
3200d49bdb0eSPaulo Zanoni 	GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C, dev_priv->de_irq_mask[PIPE_C],
32011180e206SPaulo Zanoni 			  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
320213321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3203d49bdb0eSPaulo Zanoni }
3204d49bdb0eSPaulo Zanoni 
320543f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
320643f328d7SVille Syrjälä {
320743f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
320843f328d7SVille Syrjälä 
320943f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
321043f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
321143f328d7SVille Syrjälä 
3212d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
321343f328d7SVille Syrjälä 
321443f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
321543f328d7SVille Syrjälä 
321643f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
321743f328d7SVille Syrjälä 
321870591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
321943f328d7SVille Syrjälä }
322043f328d7SVille Syrjälä 
322182a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
322282a28bcfSDaniel Vetter {
32232d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
322482a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
3225fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
322682a28bcfSDaniel Vetter 
322782a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3228fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
3229b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3230cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3231fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
323282a28bcfSDaniel Vetter 	} else {
3233fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
3234b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3235cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3236fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
323782a28bcfSDaniel Vetter 	}
323882a28bcfSDaniel Vetter 
3239fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
324082a28bcfSDaniel Vetter 
32417fe0b973SKeith Packard 	/*
32427fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
32437fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
32447fe0b973SKeith Packard 	 *
32457fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
32467fe0b973SKeith Packard 	 */
32477fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32487fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
32497fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
32507fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
32517fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
32527fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32537fe0b973SKeith Packard }
32547fe0b973SKeith Packard 
3255d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3256d46da437SPaulo Zanoni {
32572d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
325882a28bcfSDaniel Vetter 	u32 mask;
3259d46da437SPaulo Zanoni 
3260692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3261692a04cfSDaniel Vetter 		return;
3262692a04cfSDaniel Vetter 
3263105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
32645c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3265105b122eSPaulo Zanoni 	else
32665c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32678664281bSPaulo Zanoni 
3268337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3269d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3270d46da437SPaulo Zanoni }
3271d46da437SPaulo Zanoni 
32720a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
32730a9a8c91SDaniel Vetter {
32740a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
32750a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
32760a9a8c91SDaniel Vetter 
32770a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
32780a9a8c91SDaniel Vetter 
32790a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3280040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
32810a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
328235a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
328335a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
32840a9a8c91SDaniel Vetter 	}
32850a9a8c91SDaniel Vetter 
32860a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
32870a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
32880a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
32890a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
32900a9a8c91SDaniel Vetter 	} else {
32910a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
32920a9a8c91SDaniel Vetter 	}
32930a9a8c91SDaniel Vetter 
329435079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
32950a9a8c91SDaniel Vetter 
32960a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
3297a6706b45SDeepak S 		pm_irqs |= dev_priv->pm_rps_events;
32980a9a8c91SDaniel Vetter 
32990a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
33000a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
33010a9a8c91SDaniel Vetter 
3302605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
330335079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
33040a9a8c91SDaniel Vetter 	}
33050a9a8c91SDaniel Vetter }
33060a9a8c91SDaniel Vetter 
3307f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3308036a4a7dSZhenyu Wang {
33092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33108e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33118e76f8dcSPaulo Zanoni 
33128e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
33138e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
33148e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
33158e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33165c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33178e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
33185c673b60SDaniel Vetter 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
33198e76f8dcSPaulo Zanoni 	} else {
33208e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3321ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
33225b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
33235b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
33245b3a856bSDaniel Vetter 				DE_POISON);
33255c673b60SDaniel Vetter 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
33265c673b60SDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
33278e76f8dcSPaulo Zanoni 	}
3328036a4a7dSZhenyu Wang 
33291ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3330036a4a7dSZhenyu Wang 
33310c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
33320c841212SPaulo Zanoni 
3333622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3334622364b6SPaulo Zanoni 
333535079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3336036a4a7dSZhenyu Wang 
33370a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3338036a4a7dSZhenyu Wang 
3339d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
33407fe0b973SKeith Packard 
3341f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
33426005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33436005ce42SDaniel Vetter 		 *
33446005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33454bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33464bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3347d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3348f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3349d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3350f97108d1SJesse Barnes 	}
3351f97108d1SJesse Barnes 
3352036a4a7dSZhenyu Wang 	return 0;
3353036a4a7dSZhenyu Wang }
3354036a4a7dSZhenyu Wang 
3355f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3356f8b79e58SImre Deak {
3357f8b79e58SImre Deak 	u32 pipestat_mask;
3358f8b79e58SImre Deak 	u32 iir_mask;
3359120dda4fSVille Syrjälä 	enum pipe pipe;
3360f8b79e58SImre Deak 
3361f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3362f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3363f8b79e58SImre Deak 
3364120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3365120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3366f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3367f8b79e58SImre Deak 
3368f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3369f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3370f8b79e58SImre Deak 
3371120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3372120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3373120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3374f8b79e58SImre Deak 
3375f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3376f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3377f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3378120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3379120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3380f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3381f8b79e58SImre Deak 
3382f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3383f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3384f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
338576e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
338676e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3387f8b79e58SImre Deak }
3388f8b79e58SImre Deak 
3389f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3390f8b79e58SImre Deak {
3391f8b79e58SImre Deak 	u32 pipestat_mask;
3392f8b79e58SImre Deak 	u32 iir_mask;
3393120dda4fSVille Syrjälä 	enum pipe pipe;
3394f8b79e58SImre Deak 
3395f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3396f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33976c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3398120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3399120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3400f8b79e58SImre Deak 
3401f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3402f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
340376e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3404f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3405f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3406f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3407f8b79e58SImre Deak 
3408f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3409f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3410f8b79e58SImre Deak 
3411120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3412120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3413120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3414f8b79e58SImre Deak 
3415f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3416f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3417120dda4fSVille Syrjälä 
3418120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3419120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3420f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3421f8b79e58SImre Deak }
3422f8b79e58SImre Deak 
3423f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3424f8b79e58SImre Deak {
3425f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3426f8b79e58SImre Deak 
3427f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3428f8b79e58SImre Deak 		return;
3429f8b79e58SImre Deak 
3430f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3431f8b79e58SImre Deak 
3432950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3433f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3434f8b79e58SImre Deak }
3435f8b79e58SImre Deak 
3436f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3437f8b79e58SImre Deak {
3438f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3439f8b79e58SImre Deak 
3440f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3441f8b79e58SImre Deak 		return;
3442f8b79e58SImre Deak 
3443f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3444f8b79e58SImre Deak 
3445950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3446f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3447f8b79e58SImre Deak }
3448f8b79e58SImre Deak 
34490e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
34507e231dbeSJesse Barnes {
3451f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
34527e231dbeSJesse Barnes 
345320afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
345420afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
345520afbda2SDaniel Vetter 
34567e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
345776e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
345876e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
345976e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
346076e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
34617e231dbeSJesse Barnes 
3462b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3463b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3464d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3465f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3466f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3467d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
34680e6c9a9eSVille Syrjälä }
34690e6c9a9eSVille Syrjälä 
34700e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
34710e6c9a9eSVille Syrjälä {
34720e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
34730e6c9a9eSVille Syrjälä 
34740e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
34757e231dbeSJesse Barnes 
34760a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
34777e231dbeSJesse Barnes 
34787e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
34797e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
34807e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
34817e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
34827e231dbeSJesse Barnes #endif
34837e231dbeSJesse Barnes 
34847e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
348520afbda2SDaniel Vetter 
348620afbda2SDaniel Vetter 	return 0;
348720afbda2SDaniel Vetter }
348820afbda2SDaniel Vetter 
3489abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3490abd58f01SBen Widawsky {
3491abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3492abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3493abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
349473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3495abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
349673d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
349773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3498abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
349973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
350073d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
350173d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3502abd58f01SBen Widawsky 		0,
350373d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
350473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3505abd58f01SBen Widawsky 		};
3506abd58f01SBen Widawsky 
35070961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
35089a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
35099a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
35109a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, dev_priv->pm_rps_events);
35119a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3512abd58f01SBen Widawsky }
3513abd58f01SBen Widawsky 
3514abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3515abd58f01SBen Widawsky {
3516770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3517770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
3518abd58f01SBen Widawsky 	int pipe;
351988e04703SJesse Barnes 	u32 aux_en = GEN8_AUX_CHANNEL_A;
3520770de83dSDamien Lespiau 
352188e04703SJesse Barnes 	if (IS_GEN9(dev_priv)) {
3522770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3523770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
352488e04703SJesse Barnes 		aux_en |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
352588e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
352688e04703SJesse Barnes 	} else
3527770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3528770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
3529770de83dSDamien Lespiau 
3530770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3531770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3532770de83dSDamien Lespiau 
353313b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
353413b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
353513b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3536abd58f01SBen Widawsky 
3537055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3538f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3539813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3540813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3541813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
354235079899SPaulo Zanoni 					  de_pipe_enables);
3543abd58f01SBen Widawsky 
354488e04703SJesse Barnes 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~aux_en, aux_en);
3545abd58f01SBen Widawsky }
3546abd58f01SBen Widawsky 
3547abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3548abd58f01SBen Widawsky {
3549abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3550abd58f01SBen Widawsky 
3551622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3552622364b6SPaulo Zanoni 
3553abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3554abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3555abd58f01SBen Widawsky 
3556abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3557abd58f01SBen Widawsky 
3558abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3559abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3560abd58f01SBen Widawsky 
3561abd58f01SBen Widawsky 	return 0;
3562abd58f01SBen Widawsky }
3563abd58f01SBen Widawsky 
356443f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
356543f328d7SVille Syrjälä {
356643f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
356743f328d7SVille Syrjälä 
3568c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
356943f328d7SVille Syrjälä 
357043f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
357143f328d7SVille Syrjälä 
357243f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
357343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
357443f328d7SVille Syrjälä 
357543f328d7SVille Syrjälä 	return 0;
357643f328d7SVille Syrjälä }
357743f328d7SVille Syrjälä 
3578abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3579abd58f01SBen Widawsky {
3580abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3581abd58f01SBen Widawsky 
3582abd58f01SBen Widawsky 	if (!dev_priv)
3583abd58f01SBen Widawsky 		return;
3584abd58f01SBen Widawsky 
3585823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3586abd58f01SBen Widawsky }
3587abd58f01SBen Widawsky 
35888ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
35898ea0be4fSVille Syrjälä {
35908ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
35918ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
35928ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35938ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
35948ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
35958ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
35968ea0be4fSVille Syrjälä 
35978ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
35988ea0be4fSVille Syrjälä 
35998ea0be4fSVille Syrjälä 	dev_priv->irq_mask = 0;
36008ea0be4fSVille Syrjälä }
36018ea0be4fSVille Syrjälä 
36027e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
36037e231dbeSJesse Barnes {
36042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36057e231dbeSJesse Barnes 
36067e231dbeSJesse Barnes 	if (!dev_priv)
36077e231dbeSJesse Barnes 		return;
36087e231dbeSJesse Barnes 
3609843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3610843d0e7dSImre Deak 
3611893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3612893fce8eSVille Syrjälä 
36137e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3614f8b79e58SImre Deak 
36158ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
36167e231dbeSJesse Barnes }
36177e231dbeSJesse Barnes 
361843f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
361943f328d7SVille Syrjälä {
362043f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
362143f328d7SVille Syrjälä 
362243f328d7SVille Syrjälä 	if (!dev_priv)
362343f328d7SVille Syrjälä 		return;
362443f328d7SVille Syrjälä 
362543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
362643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
362743f328d7SVille Syrjälä 
3628a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
362943f328d7SVille Syrjälä 
3630a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
363143f328d7SVille Syrjälä 
3632c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
363343f328d7SVille Syrjälä }
363443f328d7SVille Syrjälä 
3635f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3636036a4a7dSZhenyu Wang {
36372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36384697995bSJesse Barnes 
36394697995bSJesse Barnes 	if (!dev_priv)
36404697995bSJesse Barnes 		return;
36414697995bSJesse Barnes 
3642be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3643036a4a7dSZhenyu Wang }
3644036a4a7dSZhenyu Wang 
3645c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3646c2798b19SChris Wilson {
36472d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3648c2798b19SChris Wilson 	int pipe;
3649c2798b19SChris Wilson 
3650055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3651c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3652c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3653c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3654c2798b19SChris Wilson 	POSTING_READ16(IER);
3655c2798b19SChris Wilson }
3656c2798b19SChris Wilson 
3657c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3658c2798b19SChris Wilson {
36592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3660c2798b19SChris Wilson 
3661c2798b19SChris Wilson 	I915_WRITE16(EMR,
3662c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3663c2798b19SChris Wilson 
3664c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3665c2798b19SChris Wilson 	dev_priv->irq_mask =
3666c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3667c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3668c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3669c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3670c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3671c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3672c2798b19SChris Wilson 
3673c2798b19SChris Wilson 	I915_WRITE16(IER,
3674c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3675c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3676c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3677c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3678c2798b19SChris Wilson 	POSTING_READ16(IER);
3679c2798b19SChris Wilson 
3680379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3681379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3682d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3683755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3684755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3685d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3686379ef82dSDaniel Vetter 
3687c2798b19SChris Wilson 	return 0;
3688c2798b19SChris Wilson }
3689c2798b19SChris Wilson 
369090a72f87SVille Syrjälä /*
369190a72f87SVille Syrjälä  * Returns true when a page flip has completed.
369290a72f87SVille Syrjälä  */
369390a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
36941f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
369590a72f87SVille Syrjälä {
36962d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36971f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
369890a72f87SVille Syrjälä 
36998d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
370090a72f87SVille Syrjälä 		return false;
370190a72f87SVille Syrjälä 
370290a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3703d6bbafa1SChris Wilson 		goto check_page_flip;
370490a72f87SVille Syrjälä 
37051f1c2e24SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
370690a72f87SVille Syrjälä 
370790a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
370890a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
370990a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
371090a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
371190a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
371290a72f87SVille Syrjälä 	 */
371390a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3714d6bbafa1SChris Wilson 		goto check_page_flip;
371590a72f87SVille Syrjälä 
371690a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
371790a72f87SVille Syrjälä 	return true;
3718d6bbafa1SChris Wilson 
3719d6bbafa1SChris Wilson check_page_flip:
3720d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3721d6bbafa1SChris Wilson 	return false;
372290a72f87SVille Syrjälä }
372390a72f87SVille Syrjälä 
3724ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3725c2798b19SChris Wilson {
372645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
37272d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3728c2798b19SChris Wilson 	u16 iir, new_iir;
3729c2798b19SChris Wilson 	u32 pipe_stats[2];
3730c2798b19SChris Wilson 	int pipe;
3731c2798b19SChris Wilson 	u16 flip_mask =
3732c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3733c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3734c2798b19SChris Wilson 
3735c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3736c2798b19SChris Wilson 	if (iir == 0)
3737c2798b19SChris Wilson 		return IRQ_NONE;
3738c2798b19SChris Wilson 
3739c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3740c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3741c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3742c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3743c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3744c2798b19SChris Wilson 		 */
3745222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3746c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3747aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3748c2798b19SChris Wilson 
3749055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3750c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3751c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3752c2798b19SChris Wilson 
3753c2798b19SChris Wilson 			/*
3754c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3755c2798b19SChris Wilson 			 */
37562d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3757c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3758c2798b19SChris Wilson 		}
3759222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3760c2798b19SChris Wilson 
3761c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3762c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3763c2798b19SChris Wilson 
3764c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3765c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3766c2798b19SChris Wilson 
3767055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
37681f1c2e24SVille Syrjälä 			int plane = pipe;
37693a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
37701f1c2e24SVille Syrjälä 				plane = !plane;
37711f1c2e24SVille Syrjälä 
37724356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
37731f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
37741f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3775c2798b19SChris Wilson 
37764356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3777277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
37782d9d2b0bSVille Syrjälä 
37791f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
37801f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
37811f7247c0SDaniel Vetter 								    pipe);
37824356d586SDaniel Vetter 		}
3783c2798b19SChris Wilson 
3784c2798b19SChris Wilson 		iir = new_iir;
3785c2798b19SChris Wilson 	}
3786c2798b19SChris Wilson 
3787c2798b19SChris Wilson 	return IRQ_HANDLED;
3788c2798b19SChris Wilson }
3789c2798b19SChris Wilson 
3790c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3791c2798b19SChris Wilson {
37922d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3793c2798b19SChris Wilson 	int pipe;
3794c2798b19SChris Wilson 
3795055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3796c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3797c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3798c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3799c2798b19SChris Wilson 	}
3800c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3801c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3802c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3803c2798b19SChris Wilson }
3804c2798b19SChris Wilson 
3805a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3806a266c7d5SChris Wilson {
38072d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3808a266c7d5SChris Wilson 	int pipe;
3809a266c7d5SChris Wilson 
3810a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3811a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3812a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3813a266c7d5SChris Wilson 	}
3814a266c7d5SChris Wilson 
381500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3816055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3817a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3818a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3819a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3820a266c7d5SChris Wilson 	POSTING_READ(IER);
3821a266c7d5SChris Wilson }
3822a266c7d5SChris Wilson 
3823a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3824a266c7d5SChris Wilson {
38252d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
382638bde180SChris Wilson 	u32 enable_mask;
3827a266c7d5SChris Wilson 
382838bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
382938bde180SChris Wilson 
383038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
383138bde180SChris Wilson 	dev_priv->irq_mask =
383238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
383338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
383438bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
383538bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
383638bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
383738bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
383838bde180SChris Wilson 
383938bde180SChris Wilson 	enable_mask =
384038bde180SChris Wilson 		I915_ASLE_INTERRUPT |
384138bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
384238bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
384338bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
384438bde180SChris Wilson 		I915_USER_INTERRUPT;
384538bde180SChris Wilson 
3846a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
384720afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
384820afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
384920afbda2SDaniel Vetter 
3850a266c7d5SChris Wilson 		/* Enable in IER... */
3851a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3852a266c7d5SChris Wilson 		/* and unmask in IMR */
3853a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3854a266c7d5SChris Wilson 	}
3855a266c7d5SChris Wilson 
3856a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3857a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3858a266c7d5SChris Wilson 	POSTING_READ(IER);
3859a266c7d5SChris Wilson 
3860f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
386120afbda2SDaniel Vetter 
3862379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3863379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3864d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3865755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3866755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3867d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3868379ef82dSDaniel Vetter 
386920afbda2SDaniel Vetter 	return 0;
387020afbda2SDaniel Vetter }
387120afbda2SDaniel Vetter 
387290a72f87SVille Syrjälä /*
387390a72f87SVille Syrjälä  * Returns true when a page flip has completed.
387490a72f87SVille Syrjälä  */
387590a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
387690a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
387790a72f87SVille Syrjälä {
38782d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
387990a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
388090a72f87SVille Syrjälä 
38818d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
388290a72f87SVille Syrjälä 		return false;
388390a72f87SVille Syrjälä 
388490a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3885d6bbafa1SChris Wilson 		goto check_page_flip;
388690a72f87SVille Syrjälä 
388790a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
388890a72f87SVille Syrjälä 
388990a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
389090a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
389190a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
389290a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
389390a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
389490a72f87SVille Syrjälä 	 */
389590a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3896d6bbafa1SChris Wilson 		goto check_page_flip;
389790a72f87SVille Syrjälä 
389890a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
389990a72f87SVille Syrjälä 	return true;
3900d6bbafa1SChris Wilson 
3901d6bbafa1SChris Wilson check_page_flip:
3902d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3903d6bbafa1SChris Wilson 	return false;
390490a72f87SVille Syrjälä }
390590a72f87SVille Syrjälä 
3906ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3907a266c7d5SChris Wilson {
390845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39108291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
391138bde180SChris Wilson 	u32 flip_mask =
391238bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
391338bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
391438bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3915a266c7d5SChris Wilson 
3916a266c7d5SChris Wilson 	iir = I915_READ(IIR);
391738bde180SChris Wilson 	do {
391838bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
39198291ee90SChris Wilson 		bool blc_event = false;
3920a266c7d5SChris Wilson 
3921a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3922a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3923a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3924a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3925a266c7d5SChris Wilson 		 */
3926222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3927a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3928aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3929a266c7d5SChris Wilson 
3930055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3931a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3932a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3933a266c7d5SChris Wilson 
393438bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3935a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3936a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
393738bde180SChris Wilson 				irq_received = true;
3938a266c7d5SChris Wilson 			}
3939a266c7d5SChris Wilson 		}
3940222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3941a266c7d5SChris Wilson 
3942a266c7d5SChris Wilson 		if (!irq_received)
3943a266c7d5SChris Wilson 			break;
3944a266c7d5SChris Wilson 
3945a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
394616c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
394716c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
394816c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3949a266c7d5SChris Wilson 
395038bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3951a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3952a266c7d5SChris Wilson 
3953a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3954a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3955a266c7d5SChris Wilson 
3956055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
395738bde180SChris Wilson 			int plane = pipe;
39583a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
395938bde180SChris Wilson 				plane = !plane;
39605e2032d4SVille Syrjälä 
396190a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
396290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
396390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3964a266c7d5SChris Wilson 
3965a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3966a266c7d5SChris Wilson 				blc_event = true;
39674356d586SDaniel Vetter 
39684356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3969277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
39702d9d2b0bSVille Syrjälä 
39711f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
39721f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
39731f7247c0SDaniel Vetter 								    pipe);
3974a266c7d5SChris Wilson 		}
3975a266c7d5SChris Wilson 
3976a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3977a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3978a266c7d5SChris Wilson 
3979a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3980a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3981a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3982a266c7d5SChris Wilson 		 * we would never get another interrupt.
3983a266c7d5SChris Wilson 		 *
3984a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3985a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3986a266c7d5SChris Wilson 		 * another one.
3987a266c7d5SChris Wilson 		 *
3988a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3989a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3990a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3991a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3992a266c7d5SChris Wilson 		 * stray interrupts.
3993a266c7d5SChris Wilson 		 */
399438bde180SChris Wilson 		ret = IRQ_HANDLED;
3995a266c7d5SChris Wilson 		iir = new_iir;
399638bde180SChris Wilson 	} while (iir & ~flip_mask);
3997a266c7d5SChris Wilson 
3998a266c7d5SChris Wilson 	return ret;
3999a266c7d5SChris Wilson }
4000a266c7d5SChris Wilson 
4001a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4002a266c7d5SChris Wilson {
40032d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4004a266c7d5SChris Wilson 	int pipe;
4005a266c7d5SChris Wilson 
4006a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
4007a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
4008a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4009a266c7d5SChris Wilson 	}
4010a266c7d5SChris Wilson 
401100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4012055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
401355b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4014a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
401555b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
401655b39755SChris Wilson 	}
4017a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4018a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4019a266c7d5SChris Wilson 
4020a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4021a266c7d5SChris Wilson }
4022a266c7d5SChris Wilson 
4023a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4024a266c7d5SChris Wilson {
40252d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4026a266c7d5SChris Wilson 	int pipe;
4027a266c7d5SChris Wilson 
4028a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4029a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4030a266c7d5SChris Wilson 
4031a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4032055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4033a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4034a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4035a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4036a266c7d5SChris Wilson 	POSTING_READ(IER);
4037a266c7d5SChris Wilson }
4038a266c7d5SChris Wilson 
4039a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4040a266c7d5SChris Wilson {
40412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4042bbba0a97SChris Wilson 	u32 enable_mask;
4043a266c7d5SChris Wilson 	u32 error_mask;
4044a266c7d5SChris Wilson 
4045a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4046bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4047adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4048bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4049bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4050bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4051bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4052bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4053bbba0a97SChris Wilson 
4054bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
405521ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
405621ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4057bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4058bbba0a97SChris Wilson 
4059bbba0a97SChris Wilson 	if (IS_G4X(dev))
4060bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4061a266c7d5SChris Wilson 
4062b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4063b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4064d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4065755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4066755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4067755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4068d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4069a266c7d5SChris Wilson 
4070a266c7d5SChris Wilson 	/*
4071a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4072a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4073a266c7d5SChris Wilson 	 */
4074a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4075a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4076a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4077a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4078a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4079a266c7d5SChris Wilson 	} else {
4080a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4081a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4082a266c7d5SChris Wilson 	}
4083a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4084a266c7d5SChris Wilson 
4085a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4086a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4087a266c7d5SChris Wilson 	POSTING_READ(IER);
4088a266c7d5SChris Wilson 
408920afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
409020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
409120afbda2SDaniel Vetter 
4092f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
409320afbda2SDaniel Vetter 
409420afbda2SDaniel Vetter 	return 0;
409520afbda2SDaniel Vetter }
409620afbda2SDaniel Vetter 
4097bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
409820afbda2SDaniel Vetter {
40992d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4100cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
410120afbda2SDaniel Vetter 	u32 hotplug_en;
410220afbda2SDaniel Vetter 
4103b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4104b5ea2d56SDaniel Vetter 
4105bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
4106bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4107bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4108adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
4109e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
4110b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
4111cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4112cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
4113a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
4114a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
4115a266c7d5SChris Wilson 		   seconds later.  So just do it once.
4116a266c7d5SChris Wilson 		*/
4117a266c7d5SChris Wilson 		if (IS_G4X(dev))
4118a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
411985fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4120a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4121a266c7d5SChris Wilson 
4122a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
4123a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4124a266c7d5SChris Wilson 	}
4125bac56d5bSEgbert Eich }
4126a266c7d5SChris Wilson 
4127ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4128a266c7d5SChris Wilson {
412945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41302d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4131a266c7d5SChris Wilson 	u32 iir, new_iir;
4132a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4133a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
413421ad8330SVille Syrjälä 	u32 flip_mask =
413521ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
413621ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4137a266c7d5SChris Wilson 
4138a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4139a266c7d5SChris Wilson 
4140a266c7d5SChris Wilson 	for (;;) {
4141501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
41422c8ba29fSChris Wilson 		bool blc_event = false;
41432c8ba29fSChris Wilson 
4144a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4145a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4146a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4147a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4148a266c7d5SChris Wilson 		 */
4149222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4150a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4151aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4152a266c7d5SChris Wilson 
4153055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4154a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4155a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4156a266c7d5SChris Wilson 
4157a266c7d5SChris Wilson 			/*
4158a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4159a266c7d5SChris Wilson 			 */
4160a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4161a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4162501e01d7SVille Syrjälä 				irq_received = true;
4163a266c7d5SChris Wilson 			}
4164a266c7d5SChris Wilson 		}
4165222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4166a266c7d5SChris Wilson 
4167a266c7d5SChris Wilson 		if (!irq_received)
4168a266c7d5SChris Wilson 			break;
4169a266c7d5SChris Wilson 
4170a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4171a266c7d5SChris Wilson 
4172a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
417316c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
417416c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4175a266c7d5SChris Wilson 
417621ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4177a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4178a266c7d5SChris Wilson 
4179a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4180a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
4181a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
4182a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
4183a266c7d5SChris Wilson 
4184055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
41852c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
418690a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
418790a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4188a266c7d5SChris Wilson 
4189a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4190a266c7d5SChris Wilson 				blc_event = true;
41914356d586SDaniel Vetter 
41924356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4193277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4194a266c7d5SChris Wilson 
41951f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
41961f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
41972d9d2b0bSVille Syrjälä 		}
4198a266c7d5SChris Wilson 
4199a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4200a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4201a266c7d5SChris Wilson 
4202515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4203515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4204515ac2bbSDaniel Vetter 
4205a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4206a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4207a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4208a266c7d5SChris Wilson 		 * we would never get another interrupt.
4209a266c7d5SChris Wilson 		 *
4210a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4211a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4212a266c7d5SChris Wilson 		 * another one.
4213a266c7d5SChris Wilson 		 *
4214a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4215a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4216a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4217a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4218a266c7d5SChris Wilson 		 * stray interrupts.
4219a266c7d5SChris Wilson 		 */
4220a266c7d5SChris Wilson 		iir = new_iir;
4221a266c7d5SChris Wilson 	}
4222a266c7d5SChris Wilson 
4223a266c7d5SChris Wilson 	return ret;
4224a266c7d5SChris Wilson }
4225a266c7d5SChris Wilson 
4226a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4227a266c7d5SChris Wilson {
42282d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4229a266c7d5SChris Wilson 	int pipe;
4230a266c7d5SChris Wilson 
4231a266c7d5SChris Wilson 	if (!dev_priv)
4232a266c7d5SChris Wilson 		return;
4233a266c7d5SChris Wilson 
4234a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4235a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4236a266c7d5SChris Wilson 
4237a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4238055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4239a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4240a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4241a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4242a266c7d5SChris Wilson 
4243055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4244a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4245a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4246a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4247a266c7d5SChris Wilson }
4248a266c7d5SChris Wilson 
42494cb21832SDaniel Vetter static void intel_hpd_irq_reenable_work(struct work_struct *work)
4250ac4c16c5SEgbert Eich {
42516323751dSImre Deak 	struct drm_i915_private *dev_priv =
42526323751dSImre Deak 		container_of(work, typeof(*dev_priv),
42536323751dSImre Deak 			     hotplug_reenable_work.work);
4254ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
4255ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4256ac4c16c5SEgbert Eich 	int i;
4257ac4c16c5SEgbert Eich 
42586323751dSImre Deak 	intel_runtime_pm_get(dev_priv);
42596323751dSImre Deak 
42604cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4261ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4262ac4c16c5SEgbert Eich 		struct drm_connector *connector;
4263ac4c16c5SEgbert Eich 
4264ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4265ac4c16c5SEgbert Eich 			continue;
4266ac4c16c5SEgbert Eich 
4267ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4268ac4c16c5SEgbert Eich 
4269ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
4270ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
4271ac4c16c5SEgbert Eich 
4272ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4273ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4274ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4275c23cc417SJani Nikula 							 connector->name);
4276ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4277ac4c16c5SEgbert Eich 				if (!connector->polled)
4278ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4279ac4c16c5SEgbert Eich 			}
4280ac4c16c5SEgbert Eich 		}
4281ac4c16c5SEgbert Eich 	}
4282ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4283ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
42844cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
42856323751dSImre Deak 
42866323751dSImre Deak 	intel_runtime_pm_put(dev_priv);
4287ac4c16c5SEgbert Eich }
4288ac4c16c5SEgbert Eich 
4289fca52a55SDaniel Vetter /**
4290fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4291fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4292fca52a55SDaniel Vetter  *
4293fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4294fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4295fca52a55SDaniel Vetter  */
4296b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4297f71d4af4SJesse Barnes {
4298b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
42998b2e326dSChris Wilson 
43008b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
430113cf5504SDave Airlie 	INIT_WORK(&dev_priv->dig_port_work, i915_digport_work_func);
430299584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
4303c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4304a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
43058b2e326dSChris Wilson 
4306a6706b45SDeepak S 	/* Let's track the enabled rps events */
4307b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
43086c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
430931685c25SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
431031685c25SDeepak S 	else
4311a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4312a6706b45SDeepak S 
431399584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
431499584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
431561bac78eSDaniel Vetter 		    (unsigned long) dev);
43166323751dSImre Deak 	INIT_DELAYED_WORK(&dev_priv->hotplug_reenable_work,
43174cb21832SDaniel Vetter 			  intel_hpd_irq_reenable_work);
431861bac78eSDaniel Vetter 
431997a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
43209ee32feaSDaniel Vetter 
4321b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
43224cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
43234cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4324b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4325f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4326f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4327391f75e2SVille Syrjälä 	} else {
4328391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4329391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4330f71d4af4SJesse Barnes 	}
4331f71d4af4SJesse Barnes 
433221da2700SVille Syrjälä 	/*
433321da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
433421da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
433521da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
433621da2700SVille Syrjälä 	 */
4337b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
433821da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
433921da2700SVille Syrjälä 
4340c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
4341f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4342f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4343c2baf4b7SVille Syrjälä 	}
4344f71d4af4SJesse Barnes 
4345b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
434643f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
434743f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
434843f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
434943f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
435043f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
435143f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
435243f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4353b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
43547e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
43557e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
43567e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
43577e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
43587e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
43597e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4360fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4361b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4362abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4363723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4364abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4365abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4366abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4367abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4368abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4369f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4370f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4371723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4372f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4373f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4374f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4375f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
437682a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4377f71d4af4SJesse Barnes 	} else {
4378b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4379c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4380c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4381c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4382c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4383b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4384a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4385a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4386a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4387a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
438820afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4389c2798b19SChris Wilson 		} else {
4390a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4391a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4392a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4393a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4394bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4395c2798b19SChris Wilson 		}
4396f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4397f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4398f71d4af4SJesse Barnes 	}
4399f71d4af4SJesse Barnes }
440020afbda2SDaniel Vetter 
4401fca52a55SDaniel Vetter /**
4402fca52a55SDaniel Vetter  * intel_hpd_init - initializes and enables hpd support
4403fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4404fca52a55SDaniel Vetter  *
4405fca52a55SDaniel Vetter  * This function enables the hotplug support. It requires that interrupts have
4406fca52a55SDaniel Vetter  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
4407fca52a55SDaniel Vetter  * poll request can run concurrently to other code, so locking rules must be
4408fca52a55SDaniel Vetter  * obeyed.
4409fca52a55SDaniel Vetter  *
4410fca52a55SDaniel Vetter  * This is a separate step from interrupt enabling to simplify the locking rules
4411fca52a55SDaniel Vetter  * in the driver load and resume code.
4412fca52a55SDaniel Vetter  */
4413b963291cSDaniel Vetter void intel_hpd_init(struct drm_i915_private *dev_priv)
441420afbda2SDaniel Vetter {
4415b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
4416821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4417821450c6SEgbert Eich 	struct drm_connector *connector;
4418821450c6SEgbert Eich 	int i;
441920afbda2SDaniel Vetter 
4420821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4421821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4422821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4423821450c6SEgbert Eich 	}
4424821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4425821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4426821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
44270e32b39cSDave Airlie 		if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
44280e32b39cSDave Airlie 			connector->polled = DRM_CONNECTOR_POLL_HPD;
44290e32b39cSDave Airlie 		if (intel_connector->mst_port)
4430821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4431821450c6SEgbert Eich 	}
4432b5ea2d56SDaniel Vetter 
4433b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4434b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4435d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
443620afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
443720afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4438d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
443920afbda2SDaniel Vetter }
4440c67a470bSPaulo Zanoni 
4441fca52a55SDaniel Vetter /**
4442fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4443fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4444fca52a55SDaniel Vetter  *
4445fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4446fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4447fca52a55SDaniel Vetter  *
4448fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4449fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4450fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4451fca52a55SDaniel Vetter  */
44522aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
44532aeb7d3aSDaniel Vetter {
44542aeb7d3aSDaniel Vetter 	/*
44552aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
44562aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
44572aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
44582aeb7d3aSDaniel Vetter 	 */
44592aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
44602aeb7d3aSDaniel Vetter 
44612aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
44622aeb7d3aSDaniel Vetter }
44632aeb7d3aSDaniel Vetter 
4464fca52a55SDaniel Vetter /**
4465fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4466fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4467fca52a55SDaniel Vetter  *
4468fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4469fca52a55SDaniel Vetter  * resources acquired in the init functions.
4470fca52a55SDaniel Vetter  */
44712aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44722aeb7d3aSDaniel Vetter {
44732aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
44742aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
44752aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44762aeb7d3aSDaniel Vetter }
44772aeb7d3aSDaniel Vetter 
4478fca52a55SDaniel Vetter /**
4479fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4480fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4481fca52a55SDaniel Vetter  *
4482fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4483fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4484fca52a55SDaniel Vetter  */
4485b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4486c67a470bSPaulo Zanoni {
4487b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
44882aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
4489c67a470bSPaulo Zanoni }
4490c67a470bSPaulo Zanoni 
4491fca52a55SDaniel Vetter /**
4492fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4493fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4494fca52a55SDaniel Vetter  *
4495fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4496fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4497fca52a55SDaniel Vetter  */
4498b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4499c67a470bSPaulo Zanoni {
45002aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4501b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4502b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4503c67a470bSPaulo Zanoni }
4504