xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 41b578fb0e8b930f2470d3f673b0fa279e77a7b8)
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 
487c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
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 
567c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
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 
6426951cafSXiong Zhang static const u32 hpd_spt[HPD_NUM_PINS] = {
6526951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
6626951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
6726951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
6826951cafSXiong Zhang 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT
6926951cafSXiong Zhang };
7026951cafSXiong Zhang 
717c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
72e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
73e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
74e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
75e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
76e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
77e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
78e5868a31SEgbert Eich };
79e5868a31SEgbert Eich 
807c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
81e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
82e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
83e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
84e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
85e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
86e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
87e5868a31SEgbert Eich };
88e5868a31SEgbert Eich 
894bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
90e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
91e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
92e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
93e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
94e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
95e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
96e5868a31SEgbert Eich };
97e5868a31SEgbert Eich 
98e0a20ad7SShashank Sharma /* BXT hpd list */
99e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
100e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
101e0a20ad7SShashank Sharma 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
102e0a20ad7SShashank Sharma };
103e0a20ad7SShashank Sharma 
1045c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
105f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
1065c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
1075c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
1085c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
1095c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1105c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1115c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1125c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1135c502442SPaulo Zanoni } while (0)
1145c502442SPaulo Zanoni 
115f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
116a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1175c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
118a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1195c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1205c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1215c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1225c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
123a9d356a6SPaulo Zanoni } while (0)
124a9d356a6SPaulo Zanoni 
125337ba017SPaulo Zanoni /*
126337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
127337ba017SPaulo Zanoni  */
128337ba017SPaulo Zanoni #define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
129337ba017SPaulo Zanoni 	u32 val = I915_READ(reg); \
130337ba017SPaulo Zanoni 	if (val) { \
131337ba017SPaulo Zanoni 		WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
132337ba017SPaulo Zanoni 		     (reg), val); \
133337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
134337ba017SPaulo Zanoni 		POSTING_READ(reg); \
135337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
136337ba017SPaulo Zanoni 		POSTING_READ(reg); \
137337ba017SPaulo Zanoni 	} \
138337ba017SPaulo Zanoni } while (0)
139337ba017SPaulo Zanoni 
14035079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
141337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
14235079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1437d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1447d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
14535079899SPaulo Zanoni } while (0)
14635079899SPaulo Zanoni 
14735079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
148337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
14935079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1507d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1517d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
15235079899SPaulo Zanoni } while (0)
15335079899SPaulo Zanoni 
154c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
155c9a9a268SImre Deak 
156036a4a7dSZhenyu Wang /* For display hotplug interrupt */
15747339cd9SDaniel Vetter void
1582d1013ddSJani Nikula ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
159036a4a7dSZhenyu Wang {
1604bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1614bc9d430SDaniel Vetter 
1629df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
163c67a470bSPaulo Zanoni 		return;
164c67a470bSPaulo Zanoni 
1651ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
1661ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
1671ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1683143a2bfSChris Wilson 		POSTING_READ(DEIMR);
169036a4a7dSZhenyu Wang 	}
170036a4a7dSZhenyu Wang }
171036a4a7dSZhenyu Wang 
17247339cd9SDaniel Vetter void
1732d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
174036a4a7dSZhenyu Wang {
1754bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1764bc9d430SDaniel Vetter 
17706ffc778SPaulo Zanoni 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
178c67a470bSPaulo Zanoni 		return;
179c67a470bSPaulo Zanoni 
1801ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1811ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1821ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1833143a2bfSChris Wilson 		POSTING_READ(DEIMR);
184036a4a7dSZhenyu Wang 	}
185036a4a7dSZhenyu Wang }
186036a4a7dSZhenyu Wang 
18743eaea13SPaulo Zanoni /**
18843eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
18943eaea13SPaulo Zanoni  * @dev_priv: driver private
19043eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
19143eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
19243eaea13SPaulo Zanoni  */
19343eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
19443eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
19543eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
19643eaea13SPaulo Zanoni {
19743eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
19843eaea13SPaulo Zanoni 
19915a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
20015a17aaeSDaniel Vetter 
2019df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
202c67a470bSPaulo Zanoni 		return;
203c67a470bSPaulo Zanoni 
20443eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
20543eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
20643eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
20743eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
20843eaea13SPaulo Zanoni }
20943eaea13SPaulo Zanoni 
210480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
21143eaea13SPaulo Zanoni {
21243eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
21343eaea13SPaulo Zanoni }
21443eaea13SPaulo Zanoni 
215480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
21643eaea13SPaulo Zanoni {
21743eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
21843eaea13SPaulo Zanoni }
21943eaea13SPaulo Zanoni 
220b900b949SImre Deak static u32 gen6_pm_iir(struct drm_i915_private *dev_priv)
221b900b949SImre Deak {
222b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
223b900b949SImre Deak }
224b900b949SImre Deak 
225a72fbc3aSImre Deak static u32 gen6_pm_imr(struct drm_i915_private *dev_priv)
226a72fbc3aSImre Deak {
227a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
228a72fbc3aSImre Deak }
229a72fbc3aSImre Deak 
230b900b949SImre Deak static u32 gen6_pm_ier(struct drm_i915_private *dev_priv)
231b900b949SImre Deak {
232b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
233b900b949SImre Deak }
234b900b949SImre Deak 
235edbfdb45SPaulo Zanoni /**
236edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
237edbfdb45SPaulo Zanoni   * @dev_priv: driver private
238edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
239edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
240edbfdb45SPaulo Zanoni   */
241edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
242edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
243edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
244edbfdb45SPaulo Zanoni {
245605cd25bSPaulo Zanoni 	uint32_t new_val;
246edbfdb45SPaulo Zanoni 
24715a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
24815a17aaeSDaniel Vetter 
249edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
250edbfdb45SPaulo Zanoni 
251605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
252f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
253f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
254f52ecbcfSPaulo Zanoni 
255605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
256605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
257a72fbc3aSImre Deak 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
258a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
259edbfdb45SPaulo Zanoni 	}
260f52ecbcfSPaulo Zanoni }
261edbfdb45SPaulo Zanoni 
262480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
263edbfdb45SPaulo Zanoni {
2649939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
2659939fba2SImre Deak 		return;
2669939fba2SImre Deak 
267edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
268edbfdb45SPaulo Zanoni }
269edbfdb45SPaulo Zanoni 
2709939fba2SImre Deak static void __gen6_disable_pm_irq(struct drm_i915_private *dev_priv,
2719939fba2SImre Deak 				  uint32_t mask)
2729939fba2SImre Deak {
2739939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
2749939fba2SImre Deak }
2759939fba2SImre Deak 
276480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
277edbfdb45SPaulo Zanoni {
2789939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
2799939fba2SImre Deak 		return;
2809939fba2SImre Deak 
2819939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, mask);
282edbfdb45SPaulo Zanoni }
283edbfdb45SPaulo Zanoni 
2843cc134e3SImre Deak void gen6_reset_rps_interrupts(struct drm_device *dev)
2853cc134e3SImre Deak {
2863cc134e3SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
2873cc134e3SImre Deak 	uint32_t reg = gen6_pm_iir(dev_priv);
2883cc134e3SImre Deak 
2893cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
2903cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
2913cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
2923cc134e3SImre Deak 	POSTING_READ(reg);
293096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
2943cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
2953cc134e3SImre Deak }
2963cc134e3SImre Deak 
297b900b949SImre Deak void gen6_enable_rps_interrupts(struct drm_device *dev)
298b900b949SImre Deak {
299b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
300b900b949SImre Deak 
301b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
30278e68d36SImre Deak 
303b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
3043cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
305d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
30678e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
30778e68d36SImre Deak 				dev_priv->pm_rps_events);
308b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
30978e68d36SImre Deak 
310b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
311b900b949SImre Deak }
312b900b949SImre Deak 
31359d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
31459d02a1fSImre Deak {
31559d02a1fSImre Deak 	/*
316f24eeb19SImre Deak 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
31759d02a1fSImre Deak 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
318f24eeb19SImre Deak 	 *
319f24eeb19SImre Deak 	 * TODO: verify if this can be reproduced on VLV,CHV.
32059d02a1fSImre Deak 	 */
32159d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
32259d02a1fSImre Deak 		mask &= ~GEN6_PM_RP_UP_EI_EXPIRED;
32359d02a1fSImre Deak 
32459d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
32559d02a1fSImre Deak 		mask &= ~GEN8_PMINTR_REDIRECT_TO_NON_DISP;
32659d02a1fSImre Deak 
32759d02a1fSImre Deak 	return mask;
32859d02a1fSImre Deak }
32959d02a1fSImre Deak 
330b900b949SImre Deak void gen6_disable_rps_interrupts(struct drm_device *dev)
331b900b949SImre Deak {
332b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
333b900b949SImre Deak 
334d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
335d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
336d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
337d4d70aa5SImre Deak 
338d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
339d4d70aa5SImre Deak 
3409939fba2SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3419939fba2SImre Deak 
34259d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
3439939fba2SImre Deak 
3449939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
345b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
346b900b949SImre Deak 				~dev_priv->pm_rps_events);
34758072ccbSImre Deak 
34858072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
34958072ccbSImre Deak 
35058072ccbSImre Deak 	synchronize_irq(dev->irq);
351b900b949SImre Deak }
352b900b949SImre Deak 
3530961021aSBen Widawsky /**
354fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
355fee884edSDaniel Vetter  * @dev_priv: driver private
356fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
357fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
358fee884edSDaniel Vetter  */
35947339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
360fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
361fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
362fee884edSDaniel Vetter {
363fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
364fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
365fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
366fee884edSDaniel Vetter 
36715a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
36815a17aaeSDaniel Vetter 
369fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
370fee884edSDaniel Vetter 
3719df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
372c67a470bSPaulo Zanoni 		return;
373c67a470bSPaulo Zanoni 
374fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
375fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
376fee884edSDaniel Vetter }
3778664281bSPaulo Zanoni 
378b5ea642aSDaniel Vetter static void
379755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
380755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
3817c463586SKeith Packard {
3829db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
383755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
3847c463586SKeith Packard 
385b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
386d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
387b79480baSDaniel Vetter 
38804feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
38904feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
39004feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
39104feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
392755e9019SImre Deak 		return;
393755e9019SImre Deak 
394755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
39546c06a30SVille Syrjälä 		return;
39646c06a30SVille Syrjälä 
39791d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
39891d181ddSImre Deak 
3997c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
400755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
40146c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4023143a2bfSChris Wilson 	POSTING_READ(reg);
4037c463586SKeith Packard }
4047c463586SKeith Packard 
405b5ea642aSDaniel Vetter static void
406755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
407755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
4087c463586SKeith Packard {
4099db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
410755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
4117c463586SKeith Packard 
412b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
413d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
414b79480baSDaniel Vetter 
41504feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
41604feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
41704feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
41804feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
41946c06a30SVille Syrjälä 		return;
42046c06a30SVille Syrjälä 
421755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
422755e9019SImre Deak 		return;
423755e9019SImre Deak 
42491d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
42591d181ddSImre Deak 
426755e9019SImre Deak 	pipestat &= ~enable_mask;
42746c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4283143a2bfSChris Wilson 	POSTING_READ(reg);
4297c463586SKeith Packard }
4307c463586SKeith Packard 
43110c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
43210c59c51SImre Deak {
43310c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
43410c59c51SImre Deak 
43510c59c51SImre Deak 	/*
436724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
437724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
43810c59c51SImre Deak 	 */
43910c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
44010c59c51SImre Deak 		return 0;
441724a6905SVille Syrjälä 	/*
442724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
443724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
444724a6905SVille Syrjälä 	 */
445724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
446724a6905SVille Syrjälä 		return 0;
44710c59c51SImre Deak 
44810c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
44910c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
45010c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
45110c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
45210c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
45310c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
45410c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
45510c59c51SImre Deak 
45610c59c51SImre Deak 	return enable_mask;
45710c59c51SImre Deak }
45810c59c51SImre Deak 
459755e9019SImre Deak void
460755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
461755e9019SImre Deak 		     u32 status_mask)
462755e9019SImre Deak {
463755e9019SImre Deak 	u32 enable_mask;
464755e9019SImre Deak 
46510c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
46610c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
46710c59c51SImre Deak 							   status_mask);
46810c59c51SImre Deak 	else
469755e9019SImre Deak 		enable_mask = status_mask << 16;
470755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
471755e9019SImre Deak }
472755e9019SImre Deak 
473755e9019SImre Deak void
474755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
475755e9019SImre Deak 		      u32 status_mask)
476755e9019SImre Deak {
477755e9019SImre Deak 	u32 enable_mask;
478755e9019SImre Deak 
47910c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
48010c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
48110c59c51SImre Deak 							   status_mask);
48210c59c51SImre Deak 	else
483755e9019SImre Deak 		enable_mask = status_mask << 16;
484755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
485755e9019SImre Deak }
486755e9019SImre Deak 
487c0e09200SDave Airlie /**
488f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
48901c66889SZhao Yakui  */
490f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
49101c66889SZhao Yakui {
4922d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4931ec14ad3SChris Wilson 
494f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
495f49e38ddSJani Nikula 		return;
496f49e38ddSJani Nikula 
49713321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
49801c66889SZhao Yakui 
499755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
500a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
5013b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
502755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
5031ec14ad3SChris Wilson 
50413321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
50501c66889SZhao Yakui }
50601c66889SZhao Yakui 
507f75f3746SVille Syrjälä /*
508f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
509f75f3746SVille Syrjälä  * around the vertical blanking period.
510f75f3746SVille Syrjälä  *
511f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
512f75f3746SVille Syrjälä  *  vblank_start >= 3
513f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
514f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
515f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
516f75f3746SVille Syrjälä  *
517f75f3746SVille Syrjälä  *           start of vblank:
518f75f3746SVille Syrjälä  *           latch double buffered registers
519f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
520f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
521f75f3746SVille Syrjälä  *           |
522f75f3746SVille Syrjälä  *           |          frame start:
523f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
524f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
525f75f3746SVille Syrjälä  *           |          |
526f75f3746SVille Syrjälä  *           |          |  start of vsync:
527f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
528f75f3746SVille Syrjälä  *           |          |  |
529f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
530f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
531f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
532f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
533f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
534f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
535f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
536f75f3746SVille Syrjälä  *       |          |                                         |
537f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
538f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
539f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
540f75f3746SVille Syrjälä  *
541f75f3746SVille Syrjälä  * x  = horizontal active
542f75f3746SVille Syrjälä  * _  = horizontal blanking
543f75f3746SVille Syrjälä  * hs = horizontal sync
544f75f3746SVille Syrjälä  * va = vertical active
545f75f3746SVille Syrjälä  * vb = vertical blanking
546f75f3746SVille Syrjälä  * vs = vertical sync
547f75f3746SVille Syrjälä  * vbs = vblank_start (number)
548f75f3746SVille Syrjälä  *
549f75f3746SVille Syrjälä  * Summary:
550f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
551f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
552f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
553f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
554f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
555f75f3746SVille Syrjälä  */
556f75f3746SVille Syrjälä 
5574cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
5584cdb83ecSVille Syrjälä {
5594cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
5604cdb83ecSVille Syrjälä 	return 0;
5614cdb83ecSVille Syrjälä }
5624cdb83ecSVille Syrjälä 
56342f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
56442f52ef8SKeith Packard  * we use as a pipe index
56542f52ef8SKeith Packard  */
566f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5670a3e67a4SJesse Barnes {
5682d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
5690a3e67a4SJesse Barnes 	unsigned long high_frame;
5700a3e67a4SJesse Barnes 	unsigned long low_frame;
5710b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
572391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
573391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
574fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
575391f75e2SVille Syrjälä 
5760b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
5770b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
5780b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
5790b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
5800b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
581391f75e2SVille Syrjälä 
5820b2a8e09SVille Syrjälä 	/* Convert to pixel count */
5830b2a8e09SVille Syrjälä 	vbl_start *= htotal;
5840b2a8e09SVille Syrjälä 
5850b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
5860b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
5870b2a8e09SVille Syrjälä 
5889db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5899db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5905eddb70bSChris Wilson 
5910a3e67a4SJesse Barnes 	/*
5920a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5930a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
5940a3e67a4SJesse Barnes 	 * register.
5950a3e67a4SJesse Barnes 	 */
5960a3e67a4SJesse Barnes 	do {
5975eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
598391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
5995eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
6000a3e67a4SJesse Barnes 	} while (high1 != high2);
6010a3e67a4SJesse Barnes 
6025eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
603391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6045eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
605391f75e2SVille Syrjälä 
606391f75e2SVille Syrjälä 	/*
607391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
608391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
609391f75e2SVille Syrjälä 	 * counter against vblank start.
610391f75e2SVille Syrjälä 	 */
611edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
6120a3e67a4SJesse Barnes }
6130a3e67a4SJesse Barnes 
614f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
6159880b7a5SJesse Barnes {
6162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6179db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
6189880b7a5SJesse Barnes 
6199880b7a5SJesse Barnes 	return I915_READ(reg);
6209880b7a5SJesse Barnes }
6219880b7a5SJesse Barnes 
622ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
623ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
624ad3543edSMario Kleiner 
625a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
626a225f079SVille Syrjälä {
627a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
628a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
629fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
630a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
63180715b2fSVille Syrjälä 	int position, vtotal;
632a225f079SVille Syrjälä 
63380715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
634a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
635a225f079SVille Syrjälä 		vtotal /= 2;
636a225f079SVille Syrjälä 
637a225f079SVille Syrjälä 	if (IS_GEN2(dev))
638a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
639a225f079SVille Syrjälä 	else
640a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
641a225f079SVille Syrjälä 
642a225f079SVille Syrjälä 	/*
643*41b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
644*41b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
645*41b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
646*41b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
647*41b578fbSJesse Barnes 	 *
648*41b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
649*41b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
650*41b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
651*41b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
652*41b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
653*41b578fbSJesse Barnes 	 */
654*41b578fbSJesse Barnes 	if (IS_HASWELL(dev) && !position) {
655*41b578fbSJesse Barnes 		int i, temp;
656*41b578fbSJesse Barnes 
657*41b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
658*41b578fbSJesse Barnes 			udelay(1);
659*41b578fbSJesse Barnes 			temp = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) &
660*41b578fbSJesse Barnes 				DSL_LINEMASK_GEN3;
661*41b578fbSJesse Barnes 			if (temp != position) {
662*41b578fbSJesse Barnes 				position = temp;
663*41b578fbSJesse Barnes 				break;
664*41b578fbSJesse Barnes 			}
665*41b578fbSJesse Barnes 		}
666*41b578fbSJesse Barnes 	}
667*41b578fbSJesse Barnes 
668*41b578fbSJesse Barnes 	/*
66980715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
67080715b2fSVille Syrjälä 	 * scanline_offset adjustment.
671a225f079SVille Syrjälä 	 */
67280715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
673a225f079SVille Syrjälä }
674a225f079SVille Syrjälä 
675f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
676abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
677abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
6780af7e4dfSMario Kleiner {
679c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
680c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
681c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
682fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
6833aa18df8SVille Syrjälä 	int position;
68478e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
6850af7e4dfSMario Kleiner 	bool in_vbl = true;
6860af7e4dfSMario Kleiner 	int ret = 0;
687ad3543edSMario Kleiner 	unsigned long irqflags;
6880af7e4dfSMario Kleiner 
689fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
6900af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
6919db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6920af7e4dfSMario Kleiner 		return 0;
6930af7e4dfSMario Kleiner 	}
6940af7e4dfSMario Kleiner 
695c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
69678e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
697c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
698c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
699c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7000af7e4dfSMario Kleiner 
701d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
702d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
703d31faf65SVille Syrjälä 		vbl_end /= 2;
704d31faf65SVille Syrjälä 		vtotal /= 2;
705d31faf65SVille Syrjälä 	}
706d31faf65SVille Syrjälä 
707c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
708c2baf4b7SVille Syrjälä 
709ad3543edSMario Kleiner 	/*
710ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
711ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
712ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
713ad3543edSMario Kleiner 	 */
714ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
715ad3543edSMario Kleiner 
716ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
717ad3543edSMario Kleiner 
718ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
719ad3543edSMario Kleiner 	if (stime)
720ad3543edSMario Kleiner 		*stime = ktime_get();
721ad3543edSMario Kleiner 
7227c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7230af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
7240af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
7250af7e4dfSMario Kleiner 		 */
726a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
7270af7e4dfSMario Kleiner 	} else {
7280af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
7290af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
7300af7e4dfSMario Kleiner 		 * scanout position.
7310af7e4dfSMario Kleiner 		 */
732ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
7330af7e4dfSMario Kleiner 
7343aa18df8SVille Syrjälä 		/* convert to pixel counts */
7353aa18df8SVille Syrjälä 		vbl_start *= htotal;
7363aa18df8SVille Syrjälä 		vbl_end *= htotal;
7373aa18df8SVille Syrjälä 		vtotal *= htotal;
73878e8fc6bSVille Syrjälä 
73978e8fc6bSVille Syrjälä 		/*
7407e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
7417e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
7427e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
7437e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
7447e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
7457e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
7467e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
7477e78f1cbSVille Syrjälä 		 */
7487e78f1cbSVille Syrjälä 		if (position >= vtotal)
7497e78f1cbSVille Syrjälä 			position = vtotal - 1;
7507e78f1cbSVille Syrjälä 
7517e78f1cbSVille Syrjälä 		/*
75278e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
75378e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
75478e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
75578e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
75678e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
75778e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
75878e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
75978e8fc6bSVille Syrjälä 		 */
76078e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
7613aa18df8SVille Syrjälä 	}
7623aa18df8SVille Syrjälä 
763ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
764ad3543edSMario Kleiner 	if (etime)
765ad3543edSMario Kleiner 		*etime = ktime_get();
766ad3543edSMario Kleiner 
767ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
768ad3543edSMario Kleiner 
769ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
770ad3543edSMario Kleiner 
7713aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
7723aa18df8SVille Syrjälä 
7733aa18df8SVille Syrjälä 	/*
7743aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
7753aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
7763aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
7773aa18df8SVille Syrjälä 	 * up since vbl_end.
7783aa18df8SVille Syrjälä 	 */
7793aa18df8SVille Syrjälä 	if (position >= vbl_start)
7803aa18df8SVille Syrjälä 		position -= vbl_end;
7813aa18df8SVille Syrjälä 	else
7823aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
7833aa18df8SVille Syrjälä 
7847c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7853aa18df8SVille Syrjälä 		*vpos = position;
7863aa18df8SVille Syrjälä 		*hpos = 0;
7873aa18df8SVille Syrjälä 	} else {
7880af7e4dfSMario Kleiner 		*vpos = position / htotal;
7890af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
7900af7e4dfSMario Kleiner 	}
7910af7e4dfSMario Kleiner 
7920af7e4dfSMario Kleiner 	/* In vblank? */
7930af7e4dfSMario Kleiner 	if (in_vbl)
7943d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
7950af7e4dfSMario Kleiner 
7960af7e4dfSMario Kleiner 	return ret;
7970af7e4dfSMario Kleiner }
7980af7e4dfSMario Kleiner 
799a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
800a225f079SVille Syrjälä {
801a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
802a225f079SVille Syrjälä 	unsigned long irqflags;
803a225f079SVille Syrjälä 	int position;
804a225f079SVille Syrjälä 
805a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
806a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
807a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
808a225f079SVille Syrjälä 
809a225f079SVille Syrjälä 	return position;
810a225f079SVille Syrjälä }
811a225f079SVille Syrjälä 
812f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
8130af7e4dfSMario Kleiner 			      int *max_error,
8140af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
8150af7e4dfSMario Kleiner 			      unsigned flags)
8160af7e4dfSMario Kleiner {
8174041b853SChris Wilson 	struct drm_crtc *crtc;
8180af7e4dfSMario Kleiner 
8197eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
8204041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
8210af7e4dfSMario Kleiner 		return -EINVAL;
8220af7e4dfSMario Kleiner 	}
8230af7e4dfSMario Kleiner 
8240af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
8254041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
8264041b853SChris Wilson 	if (crtc == NULL) {
8274041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
8284041b853SChris Wilson 		return -EINVAL;
8294041b853SChris Wilson 	}
8304041b853SChris Wilson 
831fc467a22SMaarten Lankhorst 	if (!crtc->hwmode.crtc_clock) {
8324041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
8334041b853SChris Wilson 		return -EBUSY;
8344041b853SChris Wilson 	}
8350af7e4dfSMario Kleiner 
8360af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
8374041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
8384041b853SChris Wilson 						     vblank_time, flags,
8397da903efSVille Syrjälä 						     crtc,
840fc467a22SMaarten Lankhorst 						     &crtc->hwmode);
8415ca58282SJesse Barnes }
8425ca58282SJesse Barnes 
843d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
844f97108d1SJesse Barnes {
8452d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
846b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
8479270388eSDaniel Vetter 	u8 new_delay;
8489270388eSDaniel Vetter 
849d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
850f97108d1SJesse Barnes 
85173edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
85273edd18fSDaniel Vetter 
85320e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
8549270388eSDaniel Vetter 
8557648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
856b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
857b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
858f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
859f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
860f97108d1SJesse Barnes 
861f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
862b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
86320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
86420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
86520e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
86620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
867b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
86820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
86920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
87020e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
87120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
872f97108d1SJesse Barnes 	}
873f97108d1SJesse Barnes 
8747648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
87520e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
876f97108d1SJesse Barnes 
877d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
8789270388eSDaniel Vetter 
879f97108d1SJesse Barnes 	return;
880f97108d1SJesse Barnes }
881f97108d1SJesse Barnes 
88274cdb337SChris Wilson static void notify_ring(struct intel_engine_cs *ring)
883549f7365SChris Wilson {
88493b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
885475553deSChris Wilson 		return;
886475553deSChris Wilson 
887bcfcc8baSJohn Harrison 	trace_i915_gem_request_notify(ring);
8889862e600SChris Wilson 
889549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
890549f7365SChris Wilson }
891549f7365SChris Wilson 
89243cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
89343cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
89431685c25SDeepak S {
89543cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
89643cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
89743cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
89831685c25SDeepak S }
89931685c25SDeepak S 
90043cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
90143cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
90243cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
90343cf3bf0SChris Wilson 			 int threshold)
90431685c25SDeepak S {
90543cf3bf0SChris Wilson 	u64 time, c0;
90631685c25SDeepak S 
90743cf3bf0SChris Wilson 	if (old->cz_clock == 0)
90843cf3bf0SChris Wilson 		return false;
90931685c25SDeepak S 
91043cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
91143cf3bf0SChris Wilson 	time *= threshold * dev_priv->mem_freq;
91231685c25SDeepak S 
91343cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
91443cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
91543cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
91643cf3bf0SChris Wilson 	 */
91743cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
91843cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
91943cf3bf0SChris Wilson 	c0 *= 100 * VLV_CZ_CLOCK_TO_MILLI_SEC * 4 / 1000;
92031685c25SDeepak S 
92143cf3bf0SChris Wilson 	return c0 >= time;
92231685c25SDeepak S }
92331685c25SDeepak S 
92443cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
92543cf3bf0SChris Wilson {
92643cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
92743cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
92843cf3bf0SChris Wilson }
92943cf3bf0SChris Wilson 
93043cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
93143cf3bf0SChris Wilson {
93243cf3bf0SChris Wilson 	struct intel_rps_ei now;
93343cf3bf0SChris Wilson 	u32 events = 0;
93443cf3bf0SChris Wilson 
9356f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
93643cf3bf0SChris Wilson 		return 0;
93743cf3bf0SChris Wilson 
93843cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
93943cf3bf0SChris Wilson 	if (now.cz_clock == 0)
94043cf3bf0SChris Wilson 		return 0;
94131685c25SDeepak S 
94243cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
94343cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
94443cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
9458fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
94643cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
94743cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
94831685c25SDeepak S 	}
94931685c25SDeepak S 
95043cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
95143cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
95243cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
9538fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
95443cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
95543cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
95643cf3bf0SChris Wilson 	}
95743cf3bf0SChris Wilson 
95843cf3bf0SChris Wilson 	return events;
95931685c25SDeepak S }
96031685c25SDeepak S 
961f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
962f5a4c67dSChris Wilson {
963f5a4c67dSChris Wilson 	struct intel_engine_cs *ring;
964f5a4c67dSChris Wilson 	int i;
965f5a4c67dSChris Wilson 
966f5a4c67dSChris Wilson 	for_each_ring(ring, dev_priv, i)
967f5a4c67dSChris Wilson 		if (ring->irq_refcount)
968f5a4c67dSChris Wilson 			return true;
969f5a4c67dSChris Wilson 
970f5a4c67dSChris Wilson 	return false;
971f5a4c67dSChris Wilson }
972f5a4c67dSChris Wilson 
9734912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
9743b8d8d91SJesse Barnes {
9752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
9762d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
9778d3afd7dSChris Wilson 	bool client_boost;
9788d3afd7dSChris Wilson 	int new_delay, adj, min, max;
979edbfdb45SPaulo Zanoni 	u32 pm_iir;
9803b8d8d91SJesse Barnes 
98159cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
982d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
983d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
984d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
985d4d70aa5SImre Deak 		return;
986d4d70aa5SImre Deak 	}
987c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
988c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
989a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
990480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
9918d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
9928d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
99359cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
9944912d041SBen Widawsky 
99560611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
996a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
99760611c13SPaulo Zanoni 
9988d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
9993b8d8d91SJesse Barnes 		return;
10003b8d8d91SJesse Barnes 
10014fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
10027b9e0ae6SChris Wilson 
100343cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
100443cf3bf0SChris Wilson 
1005dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1006edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
10078d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
10088d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
10098d3afd7dSChris Wilson 
10108d3afd7dSChris Wilson 	if (client_boost) {
10118d3afd7dSChris Wilson 		new_delay = dev_priv->rps.max_freq_softlimit;
10128d3afd7dSChris Wilson 		adj = 0;
10138d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1014dd75fdc8SChris Wilson 		if (adj > 0)
1015dd75fdc8SChris Wilson 			adj *= 2;
1016edcf284bSChris Wilson 		else /* CHV needs even encode values */
1017edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
10187425034aSVille Syrjälä 		/*
10197425034aSVille Syrjälä 		 * For better performance, jump directly
10207425034aSVille Syrjälä 		 * to RPe if we're below it.
10217425034aSVille Syrjälä 		 */
1022edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1023b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1024edcf284bSChris Wilson 			adj = 0;
1025edcf284bSChris Wilson 		}
1026f5a4c67dSChris Wilson 	} else if (any_waiters(dev_priv)) {
1027f5a4c67dSChris Wilson 		adj = 0;
1028dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1029b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1030b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1031dd75fdc8SChris Wilson 		else
1032b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1033dd75fdc8SChris Wilson 		adj = 0;
1034dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1035dd75fdc8SChris Wilson 		if (adj < 0)
1036dd75fdc8SChris Wilson 			adj *= 2;
1037edcf284bSChris Wilson 		else /* CHV needs even encode values */
1038edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1039dd75fdc8SChris Wilson 	} else { /* unknown event */
1040edcf284bSChris Wilson 		adj = 0;
1041dd75fdc8SChris Wilson 	}
10423b8d8d91SJesse Barnes 
1043edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1044edcf284bSChris Wilson 
104579249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
104679249636SBen Widawsky 	 * interrupt
104779249636SBen Widawsky 	 */
1048edcf284bSChris Wilson 	new_delay += adj;
10498d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
105027544369SDeepak S 
1051ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
10523b8d8d91SJesse Barnes 
10534fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
10543b8d8d91SJesse Barnes }
10553b8d8d91SJesse Barnes 
1056e3689190SBen Widawsky 
1057e3689190SBen Widawsky /**
1058e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1059e3689190SBen Widawsky  * occurred.
1060e3689190SBen Widawsky  * @work: workqueue struct
1061e3689190SBen Widawsky  *
1062e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1063e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1064e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1065e3689190SBen Widawsky  */
1066e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1067e3689190SBen Widawsky {
10682d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10692d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1070e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
107135a85ac6SBen Widawsky 	char *parity_event[6];
1072e3689190SBen Widawsky 	uint32_t misccpctl;
107335a85ac6SBen Widawsky 	uint8_t slice = 0;
1074e3689190SBen Widawsky 
1075e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1076e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1077e3689190SBen Widawsky 	 * any time we access those registers.
1078e3689190SBen Widawsky 	 */
1079e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1080e3689190SBen Widawsky 
108135a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
108235a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
108335a85ac6SBen Widawsky 		goto out;
108435a85ac6SBen Widawsky 
1085e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1086e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1087e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1088e3689190SBen Widawsky 
108935a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
109035a85ac6SBen Widawsky 		u32 reg;
109135a85ac6SBen Widawsky 
109235a85ac6SBen Widawsky 		slice--;
109335a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
109435a85ac6SBen Widawsky 			break;
109535a85ac6SBen Widawsky 
109635a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
109735a85ac6SBen Widawsky 
109835a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
109935a85ac6SBen Widawsky 
110035a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1101e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1102e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1103e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1104e3689190SBen Widawsky 
110535a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
110635a85ac6SBen Widawsky 		POSTING_READ(reg);
1107e3689190SBen Widawsky 
1108cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1109e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1110e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1111e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
111235a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
111335a85ac6SBen Widawsky 		parity_event[5] = NULL;
1114e3689190SBen Widawsky 
11155bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1116e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1117e3689190SBen Widawsky 
111835a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
111935a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1120e3689190SBen Widawsky 
112135a85ac6SBen Widawsky 		kfree(parity_event[4]);
1122e3689190SBen Widawsky 		kfree(parity_event[3]);
1123e3689190SBen Widawsky 		kfree(parity_event[2]);
1124e3689190SBen Widawsky 		kfree(parity_event[1]);
1125e3689190SBen Widawsky 	}
1126e3689190SBen Widawsky 
112735a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
112835a85ac6SBen Widawsky 
112935a85ac6SBen Widawsky out:
113035a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
11314cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1132480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
11334cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
113435a85ac6SBen Widawsky 
113535a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
113635a85ac6SBen Widawsky }
113735a85ac6SBen Widawsky 
113835a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1139e3689190SBen Widawsky {
11402d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1141e3689190SBen Widawsky 
1142040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1143e3689190SBen Widawsky 		return;
1144e3689190SBen Widawsky 
1145d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1146480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1147d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1148e3689190SBen Widawsky 
114935a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
115035a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
115135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
115235a85ac6SBen Widawsky 
115335a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
115435a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
115535a85ac6SBen Widawsky 
1156a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1157e3689190SBen Widawsky }
1158e3689190SBen Widawsky 
1159f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1160f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1161f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1162f1af8fc1SPaulo Zanoni {
1163f1af8fc1SPaulo Zanoni 	if (gt_iir &
1164f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
116574cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1166f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
116774cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1168f1af8fc1SPaulo Zanoni }
1169f1af8fc1SPaulo Zanoni 
1170e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1171e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1172e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1173e7b4c6b1SDaniel Vetter {
1174e7b4c6b1SDaniel Vetter 
1175cc609d5dSBen Widawsky 	if (gt_iir &
1176cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
117774cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1178cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
117974cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1180cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
118174cdb337SChris Wilson 		notify_ring(&dev_priv->ring[BCS]);
1182e7b4c6b1SDaniel Vetter 
1183cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1184cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1185aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1186aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1187e3689190SBen Widawsky 
118835a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
118935a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1190e7b4c6b1SDaniel Vetter }
1191e7b4c6b1SDaniel Vetter 
119274cdb337SChris Wilson static irqreturn_t gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1193abd58f01SBen Widawsky 				       u32 master_ctl)
1194abd58f01SBen Widawsky {
1195abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1196abd58f01SBen Widawsky 
1197abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
119874cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(0));
1199abd58f01SBen Widawsky 		if (tmp) {
1200cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(0), tmp);
1201abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1202e981e7b1SThomas Daniel 
120374cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
120474cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[RCS]);
120574cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
120674cdb337SChris Wilson 				notify_ring(&dev_priv->ring[RCS]);
1207e981e7b1SThomas Daniel 
120874cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
120974cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[BCS]);
121074cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
121174cdb337SChris Wilson 				notify_ring(&dev_priv->ring[BCS]);
1212abd58f01SBen Widawsky 		} else
1213abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1214abd58f01SBen Widawsky 	}
1215abd58f01SBen Widawsky 
121685f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
121774cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(1));
1218abd58f01SBen Widawsky 		if (tmp) {
1219cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(1), tmp);
1220abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1221e981e7b1SThomas Daniel 
122274cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
122374cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS]);
122474cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
122574cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS]);
1226e981e7b1SThomas Daniel 
122774cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
122874cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS2]);
122974cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
123074cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS2]);
1231abd58f01SBen Widawsky 		} else
1232abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1233abd58f01SBen Widawsky 	}
1234abd58f01SBen Widawsky 
123574cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
123674cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(3));
123774cdb337SChris Wilson 		if (tmp) {
123874cdb337SChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(3), tmp);
123974cdb337SChris Wilson 			ret = IRQ_HANDLED;
124074cdb337SChris Wilson 
124174cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
124274cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VECS]);
124374cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
124474cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VECS]);
124574cdb337SChris Wilson 		} else
124674cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
124774cdb337SChris Wilson 	}
124874cdb337SChris Wilson 
12490961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
125074cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(2));
12510961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
1252cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
12530961021aSBen Widawsky 				      tmp & dev_priv->pm_rps_events);
125438cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
1255c9a9a268SImre Deak 			gen6_rps_irq_handler(dev_priv, tmp);
12560961021aSBen Widawsky 		} else
12570961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
12580961021aSBen Widawsky 	}
12590961021aSBen Widawsky 
1260abd58f01SBen Widawsky 	return ret;
1261abd58f01SBen Widawsky }
1262abd58f01SBen Widawsky 
126363c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
126413cf5504SDave Airlie {
126513cf5504SDave Airlie 	switch (port) {
126613cf5504SDave Airlie 	case PORT_A:
126763c88d22SImre Deak 		return val & BXT_PORTA_HOTPLUG_LONG_DETECT;
126813cf5504SDave Airlie 	case PORT_B:
126963c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
127013cf5504SDave Airlie 	case PORT_C:
127163c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
127213cf5504SDave Airlie 	case PORT_D:
127363c88d22SImre Deak 		return val & PORTD_HOTPLUG_LONG_DETECT;
127463c88d22SImre Deak 	default:
127563c88d22SImre Deak 		return false;
127613cf5504SDave Airlie 	}
127713cf5504SDave Airlie }
127813cf5504SDave Airlie 
1279676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
128013cf5504SDave Airlie {
128113cf5504SDave Airlie 	switch (port) {
128213cf5504SDave Airlie 	case PORT_B:
1283676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
128413cf5504SDave Airlie 	case PORT_C:
1285676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
128613cf5504SDave Airlie 	case PORT_D:
1287676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
128826951cafSXiong Zhang 	case PORT_E:
128926951cafSXiong Zhang 		return val & PORTE_HOTPLUG_LONG_DETECT;
129013cf5504SDave Airlie 	default:
1291676574dfSJani Nikula 		return false;
129213cf5504SDave Airlie 	}
129313cf5504SDave Airlie }
129413cf5504SDave Airlie 
1295676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
1296b543fb04SEgbert Eich {
1297fc6826d1SChris Wilson 	switch (port) {
1298fc6826d1SChris Wilson 	case PORT_B:
1299676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
1300fc6826d1SChris Wilson 	case PORT_C:
1301676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
1302fc6826d1SChris Wilson 	case PORT_D:
1303676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1304676574dfSJani Nikula 	default:
1305676574dfSJani Nikula 		return false;
1306c6a828d3SDaniel Vetter 	}
1307fc6826d1SChris Wilson }
1308fc6826d1SChris Wilson 
1309676574dfSJani Nikula /* Get a bit mask of pins that have triggered, and which ones may be long. */
1310fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
13118c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1312fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1313fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1314676574dfSJani Nikula {
131513cf5504SDave Airlie 	enum port port;
1316676574dfSJani Nikula 	int i;
1317b543fb04SEgbert Eich 
1318676574dfSJani Nikula 	*pin_mask = 0;
1319676574dfSJani Nikula 	*long_mask = 0;
132091d131d2SDaniel Vetter 
1321676574dfSJani Nikula 	for_each_hpd_pin(i) {
13228c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
132313cf5504SDave Airlie 			continue;
1324821450c6SEgbert Eich 
1325676574dfSJani Nikula 		*pin_mask |= BIT(i);
132613cf5504SDave Airlie 
1327cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1328b543fb04SEgbert Eich 			continue;
1329b543fb04SEgbert Eich 
1330fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1331676574dfSJani Nikula 			*long_mask |= BIT(i);
133213cf5504SDave Airlie 	}
133313cf5504SDave Airlie 
1334676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1335676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1336b543fb04SEgbert Eich 
1337b543fb04SEgbert Eich }
1338b543fb04SEgbert Eich 
1339515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1340515ac2bbSDaniel Vetter {
13412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
134228c70f16SDaniel Vetter 
134328c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1344515ac2bbSDaniel Vetter }
1345515ac2bbSDaniel Vetter 
1346ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1347ce99c256SDaniel Vetter {
13482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
13499ee32feaSDaniel Vetter 
13509ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1351ce99c256SDaniel Vetter }
1352ce99c256SDaniel Vetter 
13538bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1354277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1355eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1356eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
13578bc5e955SDaniel Vetter 					 uint32_t crc4)
13588bf1e9f1SShuang He {
13598bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
13608bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
13618bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1362ac2300d4SDamien Lespiau 	int head, tail;
1363b2c88f5bSDamien Lespiau 
1364d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1365d538bbdfSDamien Lespiau 
13660c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1367d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
136834273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
13690c912c79SDamien Lespiau 		return;
13700c912c79SDamien Lespiau 	}
13710c912c79SDamien Lespiau 
1372d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1373d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1374b2c88f5bSDamien Lespiau 
1375b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1376d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1377b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1378b2c88f5bSDamien Lespiau 		return;
1379b2c88f5bSDamien Lespiau 	}
1380b2c88f5bSDamien Lespiau 
1381b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
13828bf1e9f1SShuang He 
13838bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1384eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1385eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1386eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1387eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1388eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1389b2c88f5bSDamien Lespiau 
1390b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1391d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1392d538bbdfSDamien Lespiau 
1393d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
139407144428SDamien Lespiau 
139507144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
13968bf1e9f1SShuang He }
1397277de95eSDaniel Vetter #else
1398277de95eSDaniel Vetter static inline void
1399277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1400277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1401277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1402277de95eSDaniel Vetter 			     uint32_t crc4) {}
1403277de95eSDaniel Vetter #endif
1404eba94eb9SDaniel Vetter 
1405277de95eSDaniel Vetter 
1406277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
14075a69b89fSDaniel Vetter {
14085a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
14095a69b89fSDaniel Vetter 
1410277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
14115a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
14125a69b89fSDaniel Vetter 				     0, 0, 0, 0);
14135a69b89fSDaniel Vetter }
14145a69b89fSDaniel Vetter 
1415277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1416eba94eb9SDaniel Vetter {
1417eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1418eba94eb9SDaniel Vetter 
1419277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1420eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1421eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1422eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1423eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
14248bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1425eba94eb9SDaniel Vetter }
14265b3a856bSDaniel Vetter 
1427277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
14285b3a856bSDaniel Vetter {
14295b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
14300b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
14310b5c5ed0SDaniel Vetter 
14320b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
14330b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
14340b5c5ed0SDaniel Vetter 	else
14350b5c5ed0SDaniel Vetter 		res1 = 0;
14360b5c5ed0SDaniel Vetter 
14370b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14380b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
14390b5c5ed0SDaniel Vetter 	else
14400b5c5ed0SDaniel Vetter 		res2 = 0;
14415b3a856bSDaniel Vetter 
1442277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
14430b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
14440b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
14450b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
14460b5c5ed0SDaniel Vetter 				     res1, res2);
14475b3a856bSDaniel Vetter }
14488bf1e9f1SShuang He 
14491403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
14501403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
14511403c0d4SPaulo Zanoni  * the work queue. */
14521403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1453baf02a1fSBen Widawsky {
1454a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
145559cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1456480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1457d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1458d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
14592adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
146041a05a3aSDaniel Vetter 		}
1461d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1462d4d70aa5SImre Deak 	}
1463baf02a1fSBen Widawsky 
1464c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1465c9a9a268SImre Deak 		return;
1466c9a9a268SImre Deak 
14671403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
146812638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
146974cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VECS]);
147012638c57SBen Widawsky 
1471aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1472aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
147312638c57SBen Widawsky 	}
14741403c0d4SPaulo Zanoni }
1475baf02a1fSBen Widawsky 
14768d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
14778d7849dbSVille Syrjälä {
14788d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
14798d7849dbSVille Syrjälä 		return false;
14808d7849dbSVille Syrjälä 
14818d7849dbSVille Syrjälä 	return true;
14828d7849dbSVille Syrjälä }
14838d7849dbSVille Syrjälä 
1484c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
14857e231dbeSJesse Barnes {
1486c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
148791d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
14887e231dbeSJesse Barnes 	int pipe;
14897e231dbeSJesse Barnes 
149058ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1491055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
149291d181ddSImre Deak 		int reg;
1493bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
149491d181ddSImre Deak 
1495bbb5eebfSDaniel Vetter 		/*
1496bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1497bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1498bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1499bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1500bbb5eebfSDaniel Vetter 		 * handle.
1501bbb5eebfSDaniel Vetter 		 */
15020f239f4cSDaniel Vetter 
15030f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
15040f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1505bbb5eebfSDaniel Vetter 
1506bbb5eebfSDaniel Vetter 		switch (pipe) {
1507bbb5eebfSDaniel Vetter 		case PIPE_A:
1508bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1509bbb5eebfSDaniel Vetter 			break;
1510bbb5eebfSDaniel Vetter 		case PIPE_B:
1511bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1512bbb5eebfSDaniel Vetter 			break;
15133278f67fSVille Syrjälä 		case PIPE_C:
15143278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
15153278f67fSVille Syrjälä 			break;
1516bbb5eebfSDaniel Vetter 		}
1517bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1518bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1519bbb5eebfSDaniel Vetter 
1520bbb5eebfSDaniel Vetter 		if (!mask)
152191d181ddSImre Deak 			continue;
152291d181ddSImre Deak 
152391d181ddSImre Deak 		reg = PIPESTAT(pipe);
1524bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1525bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
15267e231dbeSJesse Barnes 
15277e231dbeSJesse Barnes 		/*
15287e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
15297e231dbeSJesse Barnes 		 */
153091d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
153191d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
15327e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
15337e231dbeSJesse Barnes 	}
153458ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
15357e231dbeSJesse Barnes 
1536055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1537d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1538d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1539d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
154031acc7f5SJesse Barnes 
1541579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
154231acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
154331acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
154431acc7f5SJesse Barnes 		}
15454356d586SDaniel Vetter 
15464356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1547277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
15482d9d2b0bSVille Syrjälä 
15491f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
15501f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
155131acc7f5SJesse Barnes 	}
155231acc7f5SJesse Barnes 
1553c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1554c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1555c1874ed7SImre Deak }
1556c1874ed7SImre Deak 
155716c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
155816c6c56bSVille Syrjälä {
155916c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
156016c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1561676574dfSJani Nikula 	u32 pin_mask, long_mask;
156216c6c56bSVille Syrjälä 
15630d2e4297SJani Nikula 	if (!hotplug_status)
15640d2e4297SJani Nikula 		return;
15650d2e4297SJani Nikula 
15663ff60f89SOscar Mateo 	I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
15673ff60f89SOscar Mateo 	/*
15683ff60f89SOscar Mateo 	 * Make sure hotplug status is cleared before we clear IIR, or else we
15693ff60f89SOscar Mateo 	 * may miss hotplug events.
15703ff60f89SOscar Mateo 	 */
15713ff60f89SOscar Mateo 	POSTING_READ(PORT_HOTPLUG_STAT);
15723ff60f89SOscar Mateo 
15734bca26d0SVille Syrjälä 	if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
157416c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
157516c6c56bSVille Syrjälä 
1576fd63e2a9SImre Deak 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1577fd63e2a9SImre Deak 				   hotplug_trigger, hpd_status_g4x,
1578fd63e2a9SImre Deak 				   i9xx_port_hotplug_long_detect);
1579676574dfSJani Nikula 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
1580369712e8SJani Nikula 
1581369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1582369712e8SJani Nikula 			dp_aux_irq_handler(dev);
158316c6c56bSVille Syrjälä 	} else {
158416c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
158516c6c56bSVille Syrjälä 
1586fd63e2a9SImre Deak 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
15874e3d1e26SVille Syrjälä 				   hotplug_trigger, hpd_status_i915,
1588fd63e2a9SImre Deak 				   i9xx_port_hotplug_long_detect);
1589676574dfSJani Nikula 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
15903ff60f89SOscar Mateo 	}
159116c6c56bSVille Syrjälä }
159216c6c56bSVille Syrjälä 
1593c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1594c1874ed7SImre Deak {
159545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
15962d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1597c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1598c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1599c1874ed7SImre Deak 
16002dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
16012dd2a883SImre Deak 		return IRQ_NONE;
16022dd2a883SImre Deak 
1603c1874ed7SImre Deak 	while (true) {
16043ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
16053ff60f89SOscar Mateo 
1606c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
16073ff60f89SOscar Mateo 		if (gt_iir)
16083ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
16093ff60f89SOscar Mateo 
1610c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
16113ff60f89SOscar Mateo 		if (pm_iir)
16123ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
16133ff60f89SOscar Mateo 
16143ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
16153ff60f89SOscar Mateo 		if (iir) {
16163ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
16173ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
16183ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
16193ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
16203ff60f89SOscar Mateo 		}
1621c1874ed7SImre Deak 
1622c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1623c1874ed7SImre Deak 			goto out;
1624c1874ed7SImre Deak 
1625c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1626c1874ed7SImre Deak 
16273ff60f89SOscar Mateo 		if (gt_iir)
1628c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
162960611c13SPaulo Zanoni 		if (pm_iir)
1630d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
16313ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
16323ff60f89SOscar Mateo 		 * signalled in iir */
16333ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
16347e231dbeSJesse Barnes 	}
16357e231dbeSJesse Barnes 
16367e231dbeSJesse Barnes out:
16377e231dbeSJesse Barnes 	return ret;
16387e231dbeSJesse Barnes }
16397e231dbeSJesse Barnes 
164043f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
164143f328d7SVille Syrjälä {
164245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
164343f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
164443f328d7SVille Syrjälä 	u32 master_ctl, iir;
164543f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
164643f328d7SVille Syrjälä 
16472dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
16482dd2a883SImre Deak 		return IRQ_NONE;
16492dd2a883SImre Deak 
16508e5fd599SVille Syrjälä 	for (;;) {
16518e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
16523278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
16533278f67fSVille Syrjälä 
16543278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
16558e5fd599SVille Syrjälä 			break;
165643f328d7SVille Syrjälä 
165727b6c122SOscar Mateo 		ret = IRQ_HANDLED;
165827b6c122SOscar Mateo 
165943f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
166043f328d7SVille Syrjälä 
166127b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
166227b6c122SOscar Mateo 
166327b6c122SOscar Mateo 		if (iir) {
166427b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
166527b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
166627b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
166727b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
166827b6c122SOscar Mateo 		}
166927b6c122SOscar Mateo 
167074cdb337SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl);
167143f328d7SVille Syrjälä 
167227b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
167327b6c122SOscar Mateo 		 * signalled in iir */
16743278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
167543f328d7SVille Syrjälä 
167643f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
167743f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
16788e5fd599SVille Syrjälä 	}
16793278f67fSVille Syrjälä 
168043f328d7SVille Syrjälä 	return ret;
168143f328d7SVille Syrjälä }
168243f328d7SVille Syrjälä 
168323e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1684776ad806SJesse Barnes {
16852d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
16869db4a9c7SJesse Barnes 	int pipe;
1687b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1688aaf5ec2eSSonika Jindal 
1689aaf5ec2eSSonika Jindal 	if (hotplug_trigger) {
1690aaf5ec2eSSonika Jindal 		u32 dig_hotplug_reg, pin_mask, long_mask;
1691776ad806SJesse Barnes 
169213cf5504SDave Airlie 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
169313cf5504SDave Airlie 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
169413cf5504SDave Airlie 
1695fd63e2a9SImre Deak 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1696fd63e2a9SImre Deak 				   dig_hotplug_reg, hpd_ibx,
1697fd63e2a9SImre Deak 				   pch_port_hotplug_long_detect);
1698676574dfSJani Nikula 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
1699aaf5ec2eSSonika Jindal 	}
170091d131d2SDaniel Vetter 
1701cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1702cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1703776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1704cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1705cfc33bf7SVille Syrjälä 				 port_name(port));
1706cfc33bf7SVille Syrjälä 	}
1707776ad806SJesse Barnes 
1708ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1709ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1710ce99c256SDaniel Vetter 
1711776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1712515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1713776ad806SJesse Barnes 
1714776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1715776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1716776ad806SJesse Barnes 
1717776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1718776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1719776ad806SJesse Barnes 
1720776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1721776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1722776ad806SJesse Barnes 
17239db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1724055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
17259db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
17269db4a9c7SJesse Barnes 					 pipe_name(pipe),
17279db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1728776ad806SJesse Barnes 
1729776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1730776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1731776ad806SJesse Barnes 
1732776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1733776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1734776ad806SJesse Barnes 
1735776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
17361f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
17378664281bSPaulo Zanoni 
17388664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
17391f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
17408664281bSPaulo Zanoni }
17418664281bSPaulo Zanoni 
17428664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
17438664281bSPaulo Zanoni {
17448664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
17458664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
17465a69b89fSDaniel Vetter 	enum pipe pipe;
17478664281bSPaulo Zanoni 
1748de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1749de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1750de032bf4SPaulo Zanoni 
1751055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
17521f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
17531f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
17548664281bSPaulo Zanoni 
17555a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
17565a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1757277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
17585a69b89fSDaniel Vetter 			else
1759277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
17605a69b89fSDaniel Vetter 		}
17615a69b89fSDaniel Vetter 	}
17628bf1e9f1SShuang He 
17638664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
17648664281bSPaulo Zanoni }
17658664281bSPaulo Zanoni 
17668664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
17678664281bSPaulo Zanoni {
17688664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
17698664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
17708664281bSPaulo Zanoni 
1771de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1772de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1773de032bf4SPaulo Zanoni 
17748664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
17751f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
17768664281bSPaulo Zanoni 
17778664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
17781f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
17798664281bSPaulo Zanoni 
17808664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
17811f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
17828664281bSPaulo Zanoni 
17838664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1784776ad806SJesse Barnes }
1785776ad806SJesse Barnes 
178623e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
178723e81d69SAdam Jackson {
17882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
178923e81d69SAdam Jackson 	int pipe;
179026951cafSXiong Zhang 	u32 hotplug_trigger;
179126951cafSXiong Zhang 
179226951cafSXiong Zhang 	if (HAS_PCH_SPT(dev))
179326951cafSXiong Zhang 		hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT;
179426951cafSXiong Zhang 	else
179526951cafSXiong Zhang 		hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1796aaf5ec2eSSonika Jindal 
1797aaf5ec2eSSonika Jindal 	if (hotplug_trigger) {
1798aaf5ec2eSSonika Jindal 		u32 dig_hotplug_reg, pin_mask, long_mask;
179923e81d69SAdam Jackson 
180013cf5504SDave Airlie 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
180113cf5504SDave Airlie 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
180213cf5504SDave Airlie 
180326951cafSXiong Zhang 		if (HAS_PCH_SPT(dev)) {
180426951cafSXiong Zhang 			intel_get_hpd_pins(&pin_mask, &long_mask,
180526951cafSXiong Zhang 					   hotplug_trigger,
180626951cafSXiong Zhang 					   dig_hotplug_reg, hpd_spt,
180726951cafSXiong Zhang 					   pch_port_hotplug_long_detect);
180826951cafSXiong Zhang 
180926951cafSXiong Zhang 			/* detect PORTE HP event */
181026951cafSXiong Zhang 			dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
181126951cafSXiong Zhang 			if (pch_port_hotplug_long_detect(PORT_E,
181226951cafSXiong Zhang 							 dig_hotplug_reg))
181326951cafSXiong Zhang 				long_mask |= 1 << HPD_PORT_E;
181426951cafSXiong Zhang 		} else
181526951cafSXiong Zhang 			intel_get_hpd_pins(&pin_mask, &long_mask,
181626951cafSXiong Zhang 					   hotplug_trigger,
1817fd63e2a9SImre Deak 					   dig_hotplug_reg, hpd_cpt,
1818fd63e2a9SImre Deak 					   pch_port_hotplug_long_detect);
181926951cafSXiong Zhang 
1820676574dfSJani Nikula 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
1821aaf5ec2eSSonika Jindal 	}
182291d131d2SDaniel Vetter 
1823cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1824cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
182523e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1826cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1827cfc33bf7SVille Syrjälä 				 port_name(port));
1828cfc33bf7SVille Syrjälä 	}
182923e81d69SAdam Jackson 
183023e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1831ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
183223e81d69SAdam Jackson 
183323e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1834515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
183523e81d69SAdam Jackson 
183623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
183723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
183823e81d69SAdam Jackson 
183923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
184023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
184123e81d69SAdam Jackson 
184223e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1843055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
184423e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
184523e81d69SAdam Jackson 					 pipe_name(pipe),
184623e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
18478664281bSPaulo Zanoni 
18488664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
18498664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
185023e81d69SAdam Jackson }
185123e81d69SAdam Jackson 
1852c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1853c008bc6eSPaulo Zanoni {
1854c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
185540da17c2SDaniel Vetter 	enum pipe pipe;
1856c008bc6eSPaulo Zanoni 
1857c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1858c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1859c008bc6eSPaulo Zanoni 
1860c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1861c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1862c008bc6eSPaulo Zanoni 
1863c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1864c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1865c008bc6eSPaulo Zanoni 
1866055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1867d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
1868d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1869d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
1870c008bc6eSPaulo Zanoni 
187140da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
18721f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1873c008bc6eSPaulo Zanoni 
187440da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
187540da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
18765b3a856bSDaniel Vetter 
187740da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
187840da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
187940da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
188040da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
1881c008bc6eSPaulo Zanoni 		}
1882c008bc6eSPaulo Zanoni 	}
1883c008bc6eSPaulo Zanoni 
1884c008bc6eSPaulo Zanoni 	/* check event from PCH */
1885c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1886c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1887c008bc6eSPaulo Zanoni 
1888c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1889c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1890c008bc6eSPaulo Zanoni 		else
1891c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1892c008bc6eSPaulo Zanoni 
1893c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1894c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1895c008bc6eSPaulo Zanoni 	}
1896c008bc6eSPaulo Zanoni 
1897c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1898c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1899c008bc6eSPaulo Zanoni }
1900c008bc6eSPaulo Zanoni 
19019719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
19029719fb98SPaulo Zanoni {
19039719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
190407d27e20SDamien Lespiau 	enum pipe pipe;
19059719fb98SPaulo Zanoni 
19069719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
19079719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
19089719fb98SPaulo Zanoni 
19099719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
19109719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
19119719fb98SPaulo Zanoni 
19129719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
19139719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
19149719fb98SPaulo Zanoni 
1915055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1916d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
1917d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1918d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
191940da17c2SDaniel Vetter 
192040da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
192107d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
192207d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
192307d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
19249719fb98SPaulo Zanoni 		}
19259719fb98SPaulo Zanoni 	}
19269719fb98SPaulo Zanoni 
19279719fb98SPaulo Zanoni 	/* check event from PCH */
19289719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
19299719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
19309719fb98SPaulo Zanoni 
19319719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
19329719fb98SPaulo Zanoni 
19339719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
19349719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
19359719fb98SPaulo Zanoni 	}
19369719fb98SPaulo Zanoni }
19379719fb98SPaulo Zanoni 
193872c90f62SOscar Mateo /*
193972c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
194072c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
194172c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
194272c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
194372c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
194472c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
194572c90f62SOscar Mateo  */
1946f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1947b1f14ad0SJesse Barnes {
194845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
19492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1950f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
19510e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1952b1f14ad0SJesse Barnes 
19532dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
19542dd2a883SImre Deak 		return IRQ_NONE;
19552dd2a883SImre Deak 
19568664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
19578664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1958907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
19598664281bSPaulo Zanoni 
1960b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1961b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1962b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
196323a78516SPaulo Zanoni 	POSTING_READ(DEIER);
19640e43406bSChris Wilson 
196544498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
196644498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
196744498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
196844498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
196944498aeaSPaulo Zanoni 	 * due to its back queue). */
1970ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
197144498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
197244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
197344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1974ab5c608bSBen Widawsky 	}
197544498aeaSPaulo Zanoni 
197672c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
197772c90f62SOscar Mateo 
19780e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
19790e43406bSChris Wilson 	if (gt_iir) {
198072c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
198172c90f62SOscar Mateo 		ret = IRQ_HANDLED;
1982d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
19830e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1984d8fc8a47SPaulo Zanoni 		else
1985d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
19860e43406bSChris Wilson 	}
1987b1f14ad0SJesse Barnes 
1988b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
19890e43406bSChris Wilson 	if (de_iir) {
199072c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
199172c90f62SOscar Mateo 		ret = IRQ_HANDLED;
1992f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
19939719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1994f1af8fc1SPaulo Zanoni 		else
1995f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
19960e43406bSChris Wilson 	}
19970e43406bSChris Wilson 
1998f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
1999f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
20000e43406bSChris Wilson 		if (pm_iir) {
2001b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
20020e43406bSChris Wilson 			ret = IRQ_HANDLED;
200372c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
20040e43406bSChris Wilson 		}
2005f1af8fc1SPaulo Zanoni 	}
2006b1f14ad0SJesse Barnes 
2007b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2008b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2009ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
201044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
201144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2012ab5c608bSBen Widawsky 	}
2013b1f14ad0SJesse Barnes 
2014b1f14ad0SJesse Barnes 	return ret;
2015b1f14ad0SJesse Barnes }
2016b1f14ad0SJesse Barnes 
2017d04a492dSShashank Sharma static void bxt_hpd_handler(struct drm_device *dev, uint32_t iir_status)
2018d04a492dSShashank Sharma {
2019d04a492dSShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
2020676574dfSJani Nikula 	u32 hp_control, hp_trigger;
2021676574dfSJani Nikula 	u32 pin_mask, long_mask;
2022d04a492dSShashank Sharma 
2023d04a492dSShashank Sharma 	/* Get the status */
2024d04a492dSShashank Sharma 	hp_trigger = iir_status & BXT_DE_PORT_HOTPLUG_MASK;
2025d04a492dSShashank Sharma 	hp_control = I915_READ(BXT_HOTPLUG_CTL);
2026d04a492dSShashank Sharma 
2027d04a492dSShashank Sharma 	/* Hotplug not enabled ? */
2028d04a492dSShashank Sharma 	if (!(hp_control & BXT_HOTPLUG_CTL_MASK)) {
2029d04a492dSShashank Sharma 		DRM_ERROR("Interrupt when HPD disabled\n");
2030d04a492dSShashank Sharma 		return;
2031d04a492dSShashank Sharma 	}
2032d04a492dSShashank Sharma 
2033d04a492dSShashank Sharma 	/* Clear sticky bits in hpd status */
2034d04a492dSShashank Sharma 	I915_WRITE(BXT_HOTPLUG_CTL, hp_control);
2035475c2e3bSJani Nikula 
2036fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hp_trigger, hp_control,
203763c88d22SImre Deak 			   hpd_bxt, bxt_port_hotplug_long_detect);
2038475c2e3bSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2039d04a492dSShashank Sharma }
2040d04a492dSShashank Sharma 
2041abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2042abd58f01SBen Widawsky {
2043abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2044abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2045abd58f01SBen Widawsky 	u32 master_ctl;
2046abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2047abd58f01SBen Widawsky 	uint32_t tmp = 0;
2048c42664ccSDaniel Vetter 	enum pipe pipe;
204988e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
205088e04703SJesse Barnes 
20512dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
20522dd2a883SImre Deak 		return IRQ_NONE;
20532dd2a883SImre Deak 
205488e04703SJesse Barnes 	if (IS_GEN9(dev))
205588e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
205688e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2057abd58f01SBen Widawsky 
2058cb0d205eSChris Wilson 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2059abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2060abd58f01SBen Widawsky 	if (!master_ctl)
2061abd58f01SBen Widawsky 		return IRQ_NONE;
2062abd58f01SBen Widawsky 
2063cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2064abd58f01SBen Widawsky 
206538cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
206638cc46d7SOscar Mateo 
206774cdb337SChris Wilson 	ret = gen8_gt_irq_handler(dev_priv, master_ctl);
2068abd58f01SBen Widawsky 
2069abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2070abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2071abd58f01SBen Widawsky 		if (tmp) {
2072abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2073abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
207438cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
207538cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
207638cc46d7SOscar Mateo 			else
207738cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2078abd58f01SBen Widawsky 		}
207938cc46d7SOscar Mateo 		else
208038cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2081abd58f01SBen Widawsky 	}
2082abd58f01SBen Widawsky 
20836d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
20846d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
20856d766f02SDaniel Vetter 		if (tmp) {
2086d04a492dSShashank Sharma 			bool found = false;
2087d04a492dSShashank Sharma 
20886d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
20896d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
209088e04703SJesse Barnes 
2091d04a492dSShashank Sharma 			if (tmp & aux_mask) {
209238cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
2093d04a492dSShashank Sharma 				found = true;
2094d04a492dSShashank Sharma 			}
2095d04a492dSShashank Sharma 
2096d04a492dSShashank Sharma 			if (IS_BROXTON(dev) && tmp & BXT_DE_PORT_HOTPLUG_MASK) {
2097d04a492dSShashank Sharma 				bxt_hpd_handler(dev, tmp);
2098d04a492dSShashank Sharma 				found = true;
2099d04a492dSShashank Sharma 			}
2100d04a492dSShashank Sharma 
21019e63743eSShashank Sharma 			if (IS_BROXTON(dev) && (tmp & BXT_DE_PORT_GMBUS)) {
21029e63743eSShashank Sharma 				gmbus_irq_handler(dev);
21039e63743eSShashank Sharma 				found = true;
21049e63743eSShashank Sharma 			}
21059e63743eSShashank Sharma 
2106d04a492dSShashank Sharma 			if (!found)
210738cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
21086d766f02SDaniel Vetter 		}
210938cc46d7SOscar Mateo 		else
211038cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
21116d766f02SDaniel Vetter 	}
21126d766f02SDaniel Vetter 
2113055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2114770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2115abd58f01SBen Widawsky 
2116c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2117c42664ccSDaniel Vetter 			continue;
2118c42664ccSDaniel Vetter 
2119abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
212038cc46d7SOscar Mateo 		if (pipe_iir) {
212138cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
212238cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2123770de83dSDamien Lespiau 
2124d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2125d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2126d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2127abd58f01SBen Widawsky 
2128770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2129770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2130770de83dSDamien Lespiau 			else
2131770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2132770de83dSDamien Lespiau 
2133770de83dSDamien Lespiau 			if (flip_done) {
2134abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2135abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2136abd58f01SBen Widawsky 			}
2137abd58f01SBen Widawsky 
21380fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
21390fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
21400fbe7870SDaniel Vetter 
21411f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
21421f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
21431f7247c0SDaniel Vetter 								    pipe);
214438d83c96SDaniel Vetter 
2145770de83dSDamien Lespiau 
2146770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2147770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2148770de83dSDamien Lespiau 			else
2149770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2150770de83dSDamien Lespiau 
2151770de83dSDamien Lespiau 			if (fault_errors)
215230100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
215330100f2bSDaniel Vetter 					  pipe_name(pipe),
215430100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2155c42664ccSDaniel Vetter 		} else
2156abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2157abd58f01SBen Widawsky 	}
2158abd58f01SBen Widawsky 
2159266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev) && !HAS_PCH_NOP(dev) &&
2160266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
216192d03a80SDaniel Vetter 		/*
216292d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
216392d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
216492d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
216592d03a80SDaniel Vetter 		 */
216692d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
216792d03a80SDaniel Vetter 		if (pch_iir) {
216892d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
216992d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
217038cc46d7SOscar Mateo 			cpt_irq_handler(dev, pch_iir);
217138cc46d7SOscar Mateo 		} else
217238cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
217338cc46d7SOscar Mateo 
217492d03a80SDaniel Vetter 	}
217592d03a80SDaniel Vetter 
2176cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2177cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2178abd58f01SBen Widawsky 
2179abd58f01SBen Widawsky 	return ret;
2180abd58f01SBen Widawsky }
2181abd58f01SBen Widawsky 
218217e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
218317e1df07SDaniel Vetter 			       bool reset_completed)
218417e1df07SDaniel Vetter {
2185a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
218617e1df07SDaniel Vetter 	int i;
218717e1df07SDaniel Vetter 
218817e1df07SDaniel Vetter 	/*
218917e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
219017e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
219117e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
219217e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
219317e1df07SDaniel Vetter 	 */
219417e1df07SDaniel Vetter 
219517e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
219617e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
219717e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
219817e1df07SDaniel Vetter 
219917e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
220017e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
220117e1df07SDaniel Vetter 
220217e1df07SDaniel Vetter 	/*
220317e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
220417e1df07SDaniel Vetter 	 * reset state is cleared.
220517e1df07SDaniel Vetter 	 */
220617e1df07SDaniel Vetter 	if (reset_completed)
220717e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
220817e1df07SDaniel Vetter }
220917e1df07SDaniel Vetter 
22108a905236SJesse Barnes /**
2211b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
22128a905236SJesse Barnes  *
22138a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
22148a905236SJesse Barnes  * was detected.
22158a905236SJesse Barnes  */
2216b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
22178a905236SJesse Barnes {
2218b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2219b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2220cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2221cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2222cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
222317e1df07SDaniel Vetter 	int ret;
22248a905236SJesse Barnes 
22255bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
22268a905236SJesse Barnes 
22277db0ba24SDaniel Vetter 	/*
22287db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
22297db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
22307db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
22317db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
22327db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
22337db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
22347db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
22357db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
22367db0ba24SDaniel Vetter 	 */
22377db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
223844d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
22395bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
22407db0ba24SDaniel Vetter 				   reset_event);
22411f83fee0SDaniel Vetter 
224217e1df07SDaniel Vetter 		/*
2243f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2244f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2245f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2246f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2247f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2248f454c694SImre Deak 		 */
2249f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
22507514747dSVille Syrjälä 
22517514747dSVille Syrjälä 		intel_prepare_reset(dev);
22527514747dSVille Syrjälä 
2253f454c694SImre Deak 		/*
225417e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
225517e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
225617e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
225717e1df07SDaniel Vetter 		 * deadlocks with the reset work.
225817e1df07SDaniel Vetter 		 */
2259f69061beSDaniel Vetter 		ret = i915_reset(dev);
2260f69061beSDaniel Vetter 
22617514747dSVille Syrjälä 		intel_finish_reset(dev);
226217e1df07SDaniel Vetter 
2263f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2264f454c694SImre Deak 
2265f69061beSDaniel Vetter 		if (ret == 0) {
2266f69061beSDaniel Vetter 			/*
2267f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2268f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2269f69061beSDaniel Vetter 			 * complete.
2270f69061beSDaniel Vetter 			 *
2271f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2272f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2273f69061beSDaniel Vetter 			 * updates before
2274f69061beSDaniel Vetter 			 * the counter increment.
2275f69061beSDaniel Vetter 			 */
22764e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2277f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2278f69061beSDaniel Vetter 
22795bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2280f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
22811f83fee0SDaniel Vetter 		} else {
2282805de8f4SPeter Zijlstra 			atomic_or(I915_WEDGED, &error->reset_counter);
2283f316a42cSBen Gamari 		}
22841f83fee0SDaniel Vetter 
228517e1df07SDaniel Vetter 		/*
228617e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
228717e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
228817e1df07SDaniel Vetter 		 */
228917e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2290f316a42cSBen Gamari 	}
22918a905236SJesse Barnes }
22928a905236SJesse Barnes 
229335aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2294c0e09200SDave Airlie {
22958a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2296bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
229763eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2298050ee91fSBen Widawsky 	int pipe, i;
229963eeaf38SJesse Barnes 
230035aed2e6SChris Wilson 	if (!eir)
230135aed2e6SChris Wilson 		return;
230263eeaf38SJesse Barnes 
2303a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
23048a905236SJesse Barnes 
2305bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2306bd9854f9SBen Widawsky 
23078a905236SJesse Barnes 	if (IS_G4X(dev)) {
23088a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
23098a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
23108a905236SJesse Barnes 
2311a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2312a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2313050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2314050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2315a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2316a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
23178a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
23183143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
23198a905236SJesse Barnes 		}
23208a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
23218a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2322a70491ccSJoe Perches 			pr_err("page table error\n");
2323a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
23248a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
23253143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
23268a905236SJesse Barnes 		}
23278a905236SJesse Barnes 	}
23288a905236SJesse Barnes 
2329a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
233063eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
233163eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2332a70491ccSJoe Perches 			pr_err("page table error\n");
2333a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
233463eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
23353143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
233663eeaf38SJesse Barnes 		}
23378a905236SJesse Barnes 	}
23388a905236SJesse Barnes 
233963eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2340a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2341055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2342a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
23439db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
234463eeaf38SJesse Barnes 		/* pipestat has already been acked */
234563eeaf38SJesse Barnes 	}
234663eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2347a70491ccSJoe Perches 		pr_err("instruction error\n");
2348a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2349050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2350050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2351a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
235263eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
235363eeaf38SJesse Barnes 
2354a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2355a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2356a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
235763eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
23583143a2bfSChris Wilson 			POSTING_READ(IPEIR);
235963eeaf38SJesse Barnes 		} else {
236063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
236163eeaf38SJesse Barnes 
2362a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2363a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2364a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2365a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
236663eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
23673143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
236863eeaf38SJesse Barnes 		}
236963eeaf38SJesse Barnes 	}
237063eeaf38SJesse Barnes 
237163eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
23723143a2bfSChris Wilson 	POSTING_READ(EIR);
237363eeaf38SJesse Barnes 	eir = I915_READ(EIR);
237463eeaf38SJesse Barnes 	if (eir) {
237563eeaf38SJesse Barnes 		/*
237663eeaf38SJesse Barnes 		 * some errors might have become stuck,
237763eeaf38SJesse Barnes 		 * mask them.
237863eeaf38SJesse Barnes 		 */
237963eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
238063eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
238163eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
238263eeaf38SJesse Barnes 	}
238335aed2e6SChris Wilson }
238435aed2e6SChris Wilson 
238535aed2e6SChris Wilson /**
2386b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
238735aed2e6SChris Wilson  * @dev: drm device
238835aed2e6SChris Wilson  *
2389b8d24a06SMika Kuoppala  * Do some basic checking of regsiter state at error time and
239035aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
239135aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
239235aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
239335aed2e6SChris Wilson  * of a ring dump etc.).
239435aed2e6SChris Wilson  */
239558174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
239658174462SMika Kuoppala 		       const char *fmt, ...)
239735aed2e6SChris Wilson {
239835aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
239958174462SMika Kuoppala 	va_list args;
240058174462SMika Kuoppala 	char error_msg[80];
240135aed2e6SChris Wilson 
240258174462SMika Kuoppala 	va_start(args, fmt);
240358174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
240458174462SMika Kuoppala 	va_end(args);
240558174462SMika Kuoppala 
240658174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
240735aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
24088a905236SJesse Barnes 
2409ba1234d1SBen Gamari 	if (wedged) {
2410805de8f4SPeter Zijlstra 		atomic_or(I915_RESET_IN_PROGRESS_FLAG,
2411f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2412ba1234d1SBen Gamari 
241311ed50ecSBen Gamari 		/*
2414b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2415b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2416b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
241717e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
241817e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
241917e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
242017e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
242117e1df07SDaniel Vetter 		 *
242217e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
242317e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
242417e1df07SDaniel Vetter 		 * counter atomic_t.
242511ed50ecSBen Gamari 		 */
242617e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
242711ed50ecSBen Gamari 	}
242811ed50ecSBen Gamari 
2429b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
24308a905236SJesse Barnes }
24318a905236SJesse Barnes 
243242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
243342f52ef8SKeith Packard  * we use as a pipe index
243442f52ef8SKeith Packard  */
2435f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
24360a3e67a4SJesse Barnes {
24372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2438e9d21d7fSKeith Packard 	unsigned long irqflags;
243971e0ffa5SJesse Barnes 
24401ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2441f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
24427c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2443755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
24440a3e67a4SJesse Barnes 	else
24457c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2446755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
24471ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
24488692d00eSChris Wilson 
24490a3e67a4SJesse Barnes 	return 0;
24500a3e67a4SJesse Barnes }
24510a3e67a4SJesse Barnes 
2452f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2453f796cf8fSJesse Barnes {
24542d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2455f796cf8fSJesse Barnes 	unsigned long irqflags;
2456b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
245740da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2458f796cf8fSJesse Barnes 
2459f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2460b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2461b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2462b1f14ad0SJesse Barnes 
2463b1f14ad0SJesse Barnes 	return 0;
2464b1f14ad0SJesse Barnes }
2465b1f14ad0SJesse Barnes 
24667e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
24677e231dbeSJesse Barnes {
24682d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
24697e231dbeSJesse Barnes 	unsigned long irqflags;
24707e231dbeSJesse Barnes 
24717e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
247231acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2473755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
24747e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
24757e231dbeSJesse Barnes 
24767e231dbeSJesse Barnes 	return 0;
24777e231dbeSJesse Barnes }
24787e231dbeSJesse Barnes 
2479abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2480abd58f01SBen Widawsky {
2481abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2482abd58f01SBen Widawsky 	unsigned long irqflags;
2483abd58f01SBen Widawsky 
2484abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
24857167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
24867167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2487abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2488abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2489abd58f01SBen Widawsky 	return 0;
2490abd58f01SBen Widawsky }
2491abd58f01SBen Widawsky 
249242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
249342f52ef8SKeith Packard  * we use as a pipe index
249442f52ef8SKeith Packard  */
2495f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
24960a3e67a4SJesse Barnes {
24972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2498e9d21d7fSKeith Packard 	unsigned long irqflags;
24990a3e67a4SJesse Barnes 
25001ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
25017c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2502755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2503755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
25041ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25050a3e67a4SJesse Barnes }
25060a3e67a4SJesse Barnes 
2507f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2508f796cf8fSJesse Barnes {
25092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2510f796cf8fSJesse Barnes 	unsigned long irqflags;
2511b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
251240da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2513f796cf8fSJesse Barnes 
2514f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2515b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2516b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2517b1f14ad0SJesse Barnes }
2518b1f14ad0SJesse Barnes 
25197e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
25207e231dbeSJesse Barnes {
25212d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
25227e231dbeSJesse Barnes 	unsigned long irqflags;
25237e231dbeSJesse Barnes 
25247e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
252531acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2526755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
25277e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25287e231dbeSJesse Barnes }
25297e231dbeSJesse Barnes 
2530abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2531abd58f01SBen Widawsky {
2532abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2533abd58f01SBen Widawsky 	unsigned long irqflags;
2534abd58f01SBen Widawsky 
2535abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
25367167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
25377167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2538abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2539abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2540abd58f01SBen Widawsky }
2541abd58f01SBen Widawsky 
25429107e9d2SChris Wilson static bool
254394f7bbe1STomas Elf ring_idle(struct intel_engine_cs *ring, u32 seqno)
2544893eead0SChris Wilson {
25459107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
254694f7bbe1STomas Elf 		i915_seqno_passed(seqno, ring->last_submitted_seqno));
2547f65d9421SBen Gamari }
2548f65d9421SBen Gamari 
2549a028c4b0SDaniel Vetter static bool
2550a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2551a028c4b0SDaniel Vetter {
2552a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2553a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2554a028c4b0SDaniel Vetter 	} else {
2555a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2556a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2557a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2558a028c4b0SDaniel Vetter 	}
2559a028c4b0SDaniel Vetter }
2560a028c4b0SDaniel Vetter 
2561a4872ba6SOscar Mateo static struct intel_engine_cs *
2562a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2563921d42eaSDaniel Vetter {
2564921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2565a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2566921d42eaSDaniel Vetter 	int i;
2567921d42eaSDaniel Vetter 
2568921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2569a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2570a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2571a6cdb93aSRodrigo Vivi 				continue;
2572a6cdb93aSRodrigo Vivi 
2573a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2574a6cdb93aSRodrigo Vivi 				return signaller;
2575a6cdb93aSRodrigo Vivi 		}
2576921d42eaSDaniel Vetter 	} else {
2577921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2578921d42eaSDaniel Vetter 
2579921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2580921d42eaSDaniel Vetter 			if(ring == signaller)
2581921d42eaSDaniel Vetter 				continue;
2582921d42eaSDaniel Vetter 
2583ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2584921d42eaSDaniel Vetter 				return signaller;
2585921d42eaSDaniel Vetter 		}
2586921d42eaSDaniel Vetter 	}
2587921d42eaSDaniel Vetter 
2588a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2589a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2590921d42eaSDaniel Vetter 
2591921d42eaSDaniel Vetter 	return NULL;
2592921d42eaSDaniel Vetter }
2593921d42eaSDaniel Vetter 
2594a4872ba6SOscar Mateo static struct intel_engine_cs *
2595a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2596a24a11e6SChris Wilson {
2597a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
259888fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2599a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2600a6cdb93aSRodrigo Vivi 	int i, backwards;
2601a24a11e6SChris Wilson 
2602a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2603a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
26046274f212SChris Wilson 		return NULL;
2605a24a11e6SChris Wilson 
260688fe429dSDaniel Vetter 	/*
260788fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
260888fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2609a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2610a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
261188fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
261288fe429dSDaniel Vetter 	 * ringbuffer itself.
2613a24a11e6SChris Wilson 	 */
261488fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2615a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
261688fe429dSDaniel Vetter 
2617a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
261888fe429dSDaniel Vetter 		/*
261988fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
262088fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
262188fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
262288fe429dSDaniel Vetter 		 */
2623ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
262488fe429dSDaniel Vetter 
262588fe429dSDaniel Vetter 		/* This here seems to blow up */
2626ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2627a24a11e6SChris Wilson 		if (cmd == ipehr)
2628a24a11e6SChris Wilson 			break;
2629a24a11e6SChris Wilson 
263088fe429dSDaniel Vetter 		head -= 4;
263188fe429dSDaniel Vetter 	}
2632a24a11e6SChris Wilson 
263388fe429dSDaniel Vetter 	if (!i)
263488fe429dSDaniel Vetter 		return NULL;
263588fe429dSDaniel Vetter 
2636ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2637a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2638a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2639a6cdb93aSRodrigo Vivi 		offset <<= 32;
2640a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2641a6cdb93aSRodrigo Vivi 	}
2642a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2643a24a11e6SChris Wilson }
2644a24a11e6SChris Wilson 
2645a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
26466274f212SChris Wilson {
26476274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2648a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2649a0d036b0SChris Wilson 	u32 seqno;
26506274f212SChris Wilson 
26514be17381SChris Wilson 	ring->hangcheck.deadlock++;
26526274f212SChris Wilson 
26536274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
26544be17381SChris Wilson 	if (signaller == NULL)
26554be17381SChris Wilson 		return -1;
26564be17381SChris Wilson 
26574be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
26584be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
26596274f212SChris Wilson 		return -1;
26606274f212SChris Wilson 
26614be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
26624be17381SChris Wilson 		return 1;
26634be17381SChris Wilson 
2664a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2665a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2666a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
26674be17381SChris Wilson 		return -1;
26684be17381SChris Wilson 
26694be17381SChris Wilson 	return 0;
26706274f212SChris Wilson }
26716274f212SChris Wilson 
26726274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
26736274f212SChris Wilson {
2674a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
26756274f212SChris Wilson 	int i;
26766274f212SChris Wilson 
26776274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
26784be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
26796274f212SChris Wilson }
26806274f212SChris Wilson 
2681ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2682a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
26831ec14ad3SChris Wilson {
26841ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
26851ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
26869107e9d2SChris Wilson 	u32 tmp;
26879107e9d2SChris Wilson 
2688f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2689f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2690f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2691f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2692f260fe7bSMika Kuoppala 		}
2693f260fe7bSMika Kuoppala 
2694f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2695f260fe7bSMika Kuoppala 	}
26966274f212SChris Wilson 
26979107e9d2SChris Wilson 	if (IS_GEN2(dev))
2698f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
26999107e9d2SChris Wilson 
27009107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
27019107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
27029107e9d2SChris Wilson 	 * and break the hang. This should work on
27039107e9d2SChris Wilson 	 * all but the second generation chipsets.
27049107e9d2SChris Wilson 	 */
27059107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
27061ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
270758174462SMika Kuoppala 		i915_handle_error(dev, false,
270858174462SMika Kuoppala 				  "Kicking stuck wait on %s",
27091ec14ad3SChris Wilson 				  ring->name);
27101ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2711f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
27121ec14ad3SChris Wilson 	}
2713a24a11e6SChris Wilson 
27146274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
27156274f212SChris Wilson 		switch (semaphore_passed(ring)) {
27166274f212SChris Wilson 		default:
2717f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
27186274f212SChris Wilson 		case 1:
271958174462SMika Kuoppala 			i915_handle_error(dev, false,
272058174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2721a24a11e6SChris Wilson 					  ring->name);
2722a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2723f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
27246274f212SChris Wilson 		case 0:
2725f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
27266274f212SChris Wilson 		}
27279107e9d2SChris Wilson 	}
27289107e9d2SChris Wilson 
2729f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2730a24a11e6SChris Wilson }
2731d1e61e7fSChris Wilson 
2732737b1506SChris Wilson /*
2733f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
273405407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
273505407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
273605407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
273705407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
273805407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2739f65d9421SBen Gamari  */
2740737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
2741f65d9421SBen Gamari {
2742737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
2743737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
2744737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
2745737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
2746a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2747b4519513SChris Wilson 	int i;
274805407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
27499107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
27509107e9d2SChris Wilson #define BUSY 1
27519107e9d2SChris Wilson #define KICK 5
27529107e9d2SChris Wilson #define HUNG 20
2753893eead0SChris Wilson 
2754d330a953SJani Nikula 	if (!i915.enable_hangcheck)
27553e0dc6b0SBen Widawsky 		return;
27563e0dc6b0SBen Widawsky 
2757b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
275850877445SChris Wilson 		u64 acthd;
275950877445SChris Wilson 		u32 seqno;
27609107e9d2SChris Wilson 		bool busy = true;
2761b4519513SChris Wilson 
27626274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
27636274f212SChris Wilson 
276405407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
276505407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
276605407ff8SMika Kuoppala 
276705407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
276894f7bbe1STomas Elf 			if (ring_idle(ring, seqno)) {
2769da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2770da661464SMika Kuoppala 
27719107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
27729107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2773094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2774f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
27759107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
27769107e9d2SChris Wilson 								  ring->name);
2777f4adcd24SDaniel Vetter 						else
2778f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2779f4adcd24SDaniel Vetter 								 ring->name);
27809107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2781094f9a54SChris Wilson 					}
2782094f9a54SChris Wilson 					/* Safeguard against driver failure */
2783094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
27849107e9d2SChris Wilson 				} else
27859107e9d2SChris Wilson 					busy = false;
278605407ff8SMika Kuoppala 			} else {
27876274f212SChris Wilson 				/* We always increment the hangcheck score
27886274f212SChris Wilson 				 * if the ring is busy and still processing
27896274f212SChris Wilson 				 * the same request, so that no single request
27906274f212SChris Wilson 				 * can run indefinitely (such as a chain of
27916274f212SChris Wilson 				 * batches). The only time we do not increment
27926274f212SChris Wilson 				 * the hangcheck score on this ring, if this
27936274f212SChris Wilson 				 * ring is in a legitimate wait for another
27946274f212SChris Wilson 				 * ring. In that case the waiting ring is a
27956274f212SChris Wilson 				 * victim and we want to be sure we catch the
27966274f212SChris Wilson 				 * right culprit. Then every time we do kick
27976274f212SChris Wilson 				 * the ring, add a small increment to the
27986274f212SChris Wilson 				 * score so that we can catch a batch that is
27996274f212SChris Wilson 				 * being repeatedly kicked and so responsible
28006274f212SChris Wilson 				 * for stalling the machine.
28019107e9d2SChris Wilson 				 */
2802ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2803ad8beaeaSMika Kuoppala 								    acthd);
2804ad8beaeaSMika Kuoppala 
2805ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2806da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2807f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
2808f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2809f260fe7bSMika Kuoppala 					break;
2810f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
2811ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
28126274f212SChris Wilson 					break;
2813f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2814ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
28156274f212SChris Wilson 					break;
2816f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2817ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
28186274f212SChris Wilson 					stuck[i] = true;
28196274f212SChris Wilson 					break;
28206274f212SChris Wilson 				}
282105407ff8SMika Kuoppala 			}
28229107e9d2SChris Wilson 		} else {
2823da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2824da661464SMika Kuoppala 
28259107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
28269107e9d2SChris Wilson 			 * attempts across multiple batches.
28279107e9d2SChris Wilson 			 */
28289107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
28299107e9d2SChris Wilson 				ring->hangcheck.score--;
2830f260fe7bSMika Kuoppala 
2831f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
2832cbb465e7SChris Wilson 		}
2833f65d9421SBen Gamari 
283405407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
283505407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
28369107e9d2SChris Wilson 		busy_count += busy;
283705407ff8SMika Kuoppala 	}
283805407ff8SMika Kuoppala 
283905407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2840b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2841b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
284205407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2843a43adf07SChris Wilson 				 ring->name);
2844a43adf07SChris Wilson 			rings_hung++;
284505407ff8SMika Kuoppala 		}
284605407ff8SMika Kuoppala 	}
284705407ff8SMika Kuoppala 
284805407ff8SMika Kuoppala 	if (rings_hung)
284958174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
285005407ff8SMika Kuoppala 
285105407ff8SMika Kuoppala 	if (busy_count)
285205407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
285305407ff8SMika Kuoppala 		 * being added */
285410cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
285510cd45b6SMika Kuoppala }
285610cd45b6SMika Kuoppala 
285710cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
285810cd45b6SMika Kuoppala {
2859737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
2860672e7b7cSChris Wilson 
2861d330a953SJani Nikula 	if (!i915.enable_hangcheck)
286210cd45b6SMika Kuoppala 		return;
286310cd45b6SMika Kuoppala 
2864737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
2865737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
2866737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
2867737b1506SChris Wilson 	 */
2868737b1506SChris Wilson 
2869737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
2870737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
2871f65d9421SBen Gamari }
2872f65d9421SBen Gamari 
28731c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
287491738a95SPaulo Zanoni {
287591738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
287691738a95SPaulo Zanoni 
287791738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
287891738a95SPaulo Zanoni 		return;
287991738a95SPaulo Zanoni 
2880f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
2881105b122eSPaulo Zanoni 
2882105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
2883105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
2884622364b6SPaulo Zanoni }
2885105b122eSPaulo Zanoni 
288691738a95SPaulo Zanoni /*
2887622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
2888622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
2889622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
2890622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
2891622364b6SPaulo Zanoni  *
2892622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
289391738a95SPaulo Zanoni  */
2894622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
2895622364b6SPaulo Zanoni {
2896622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2897622364b6SPaulo Zanoni 
2898622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
2899622364b6SPaulo Zanoni 		return;
2900622364b6SPaulo Zanoni 
2901622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
290291738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
290391738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
290491738a95SPaulo Zanoni }
290591738a95SPaulo Zanoni 
29067c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
2907d18ea1b5SDaniel Vetter {
2908d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2909d18ea1b5SDaniel Vetter 
2910f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
2911a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
2912f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
2913d18ea1b5SDaniel Vetter }
2914d18ea1b5SDaniel Vetter 
2915c0e09200SDave Airlie /* drm_dma.h hooks
2916c0e09200SDave Airlie */
2917be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
2918036a4a7dSZhenyu Wang {
29192d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2920036a4a7dSZhenyu Wang 
29210c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
2922bdfcdb63SDaniel Vetter 
2923f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
2924c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
2925c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
2926036a4a7dSZhenyu Wang 
29277c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
2928c650156aSZhenyu Wang 
29291c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
29307d99163dSBen Widawsky }
29317d99163dSBen Widawsky 
293270591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
293370591a41SVille Syrjälä {
293470591a41SVille Syrjälä 	enum pipe pipe;
293570591a41SVille Syrjälä 
293670591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
293770591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
293870591a41SVille Syrjälä 
293970591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
294070591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
294170591a41SVille Syrjälä 
294270591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
294370591a41SVille Syrjälä }
294470591a41SVille Syrjälä 
29457e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
29467e231dbeSJesse Barnes {
29472d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
29487e231dbeSJesse Barnes 
29497e231dbeSJesse Barnes 	/* VLV magic */
29507e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
29517e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
29527e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
29537e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
29547e231dbeSJesse Barnes 
29557c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
29567e231dbeSJesse Barnes 
29577c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
29587e231dbeSJesse Barnes 
295970591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
29607e231dbeSJesse Barnes }
29617e231dbeSJesse Barnes 
2962d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
2963d6e3cca3SDaniel Vetter {
2964d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
2965d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
2966d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
2967d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
2968d6e3cca3SDaniel Vetter }
2969d6e3cca3SDaniel Vetter 
2970823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
2971abd58f01SBen Widawsky {
2972abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2973abd58f01SBen Widawsky 	int pipe;
2974abd58f01SBen Widawsky 
2975abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2976abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2977abd58f01SBen Widawsky 
2978d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
2979abd58f01SBen Widawsky 
2980055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
2981f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
2982813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
2983f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
2984abd58f01SBen Widawsky 
2985f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
2986f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
2987f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
2988abd58f01SBen Widawsky 
2989266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
29901c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
2991abd58f01SBen Widawsky }
2992abd58f01SBen Widawsky 
29934c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
29944c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
2995d49bdb0eSPaulo Zanoni {
29961180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
2997d49bdb0eSPaulo Zanoni 
299813321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
2999d14c0343SDamien Lespiau 	if (pipe_mask & 1 << PIPE_A)
3000d14c0343SDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_A,
3001d14c0343SDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_A],
3002d14c0343SDamien Lespiau 				  ~dev_priv->de_irq_mask[PIPE_A] | extra_ier);
30034c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_B)
30044c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B,
30054c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_B],
30061180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
30074c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_C)
30084c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C,
30094c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_C],
30101180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
301113321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3012d49bdb0eSPaulo Zanoni }
3013d49bdb0eSPaulo Zanoni 
301443f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
301543f328d7SVille Syrjälä {
301643f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
301743f328d7SVille Syrjälä 
301843f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
301943f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
302043f328d7SVille Syrjälä 
3021d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
302243f328d7SVille Syrjälä 
302343f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
302443f328d7SVille Syrjälä 
302543f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
302643f328d7SVille Syrjälä 
302770591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
302843f328d7SVille Syrjälä }
302943f328d7SVille Syrjälä 
303082a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
303182a28bcfSDaniel Vetter {
30322d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
303382a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
3034fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
303582a28bcfSDaniel Vetter 
303682a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3037fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
3038b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
30395fcece80SJani Nikula 			if (dev_priv->hotplug.stats[intel_encoder->hpd_pin].state == HPD_ENABLED)
3040fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
304126951cafSXiong Zhang 	} else if (HAS_PCH_SPT(dev)) {
304226951cafSXiong Zhang 		hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
304326951cafSXiong Zhang 		for_each_intel_encoder(dev, intel_encoder)
304426951cafSXiong Zhang 			if (dev_priv->hotplug.stats[intel_encoder->hpd_pin].state == HPD_ENABLED)
304526951cafSXiong Zhang 				enabled_irqs |= hpd_spt[intel_encoder->hpd_pin];
304682a28bcfSDaniel Vetter 	} else {
3047fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
3048b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
30495fcece80SJani Nikula 			if (dev_priv->hotplug.stats[intel_encoder->hpd_pin].state == HPD_ENABLED)
3050fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
305182a28bcfSDaniel Vetter 	}
305282a28bcfSDaniel Vetter 
3053fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
305482a28bcfSDaniel Vetter 
30557fe0b973SKeith Packard 	/*
30567fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
30577fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
30587fe0b973SKeith Packard 	 *
30597fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
30607fe0b973SKeith Packard 	 */
30617fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
30627fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
30637fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
30647fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
30657fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
30667fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
306726951cafSXiong Zhang 
306826951cafSXiong Zhang 	/* enable SPT PORTE hot plug */
306926951cafSXiong Zhang 	if (HAS_PCH_SPT(dev)) {
307026951cafSXiong Zhang 		hotplug = I915_READ(PCH_PORT_HOTPLUG2);
307126951cafSXiong Zhang 		hotplug |= PORTE_HOTPLUG_ENABLE;
307226951cafSXiong Zhang 		I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
307326951cafSXiong Zhang 	}
30747fe0b973SKeith Packard }
30757fe0b973SKeith Packard 
3076e0a20ad7SShashank Sharma static void bxt_hpd_irq_setup(struct drm_device *dev)
3077e0a20ad7SShashank Sharma {
3078e0a20ad7SShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
3079e0a20ad7SShashank Sharma 	struct intel_encoder *intel_encoder;
3080e0a20ad7SShashank Sharma 	u32 hotplug_port = 0;
3081e0a20ad7SShashank Sharma 	u32 hotplug_ctrl;
3082e0a20ad7SShashank Sharma 
3083e0a20ad7SShashank Sharma 	/* Now, enable HPD */
3084e0a20ad7SShashank Sharma 	for_each_intel_encoder(dev, intel_encoder) {
30855fcece80SJani Nikula 		if (dev_priv->hotplug.stats[intel_encoder->hpd_pin].state
3086e0a20ad7SShashank Sharma 				== HPD_ENABLED)
3087e0a20ad7SShashank Sharma 			hotplug_port |= hpd_bxt[intel_encoder->hpd_pin];
3088e0a20ad7SShashank Sharma 	}
3089e0a20ad7SShashank Sharma 
3090e0a20ad7SShashank Sharma 	/* Mask all HPD control bits */
3091e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(BXT_HOTPLUG_CTL) & ~BXT_HOTPLUG_CTL_MASK;
3092e0a20ad7SShashank Sharma 
3093e0a20ad7SShashank Sharma 	/* Enable requested port in hotplug control */
3094e0a20ad7SShashank Sharma 	/* TODO: implement (short) HPD support on port A */
3095e0a20ad7SShashank Sharma 	WARN_ON_ONCE(hotplug_port & BXT_DE_PORT_HP_DDIA);
3096e0a20ad7SShashank Sharma 	if (hotplug_port & BXT_DE_PORT_HP_DDIB)
3097e0a20ad7SShashank Sharma 		hotplug_ctrl |= BXT_DDIB_HPD_ENABLE;
3098e0a20ad7SShashank Sharma 	if (hotplug_port & BXT_DE_PORT_HP_DDIC)
3099e0a20ad7SShashank Sharma 		hotplug_ctrl |= BXT_DDIC_HPD_ENABLE;
3100e0a20ad7SShashank Sharma 	I915_WRITE(BXT_HOTPLUG_CTL, hotplug_ctrl);
3101e0a20ad7SShashank Sharma 
3102e0a20ad7SShashank Sharma 	/* Unmask DDI hotplug in IMR */
3103e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(GEN8_DE_PORT_IMR) & ~hotplug_port;
3104e0a20ad7SShashank Sharma 	I915_WRITE(GEN8_DE_PORT_IMR, hotplug_ctrl);
3105e0a20ad7SShashank Sharma 
3106e0a20ad7SShashank Sharma 	/* Enable DDI hotplug in IER */
3107e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(GEN8_DE_PORT_IER) | hotplug_port;
3108e0a20ad7SShashank Sharma 	I915_WRITE(GEN8_DE_PORT_IER, hotplug_ctrl);
3109e0a20ad7SShashank Sharma 	POSTING_READ(GEN8_DE_PORT_IER);
3110e0a20ad7SShashank Sharma }
3111e0a20ad7SShashank Sharma 
3112d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3113d46da437SPaulo Zanoni {
31142d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
311582a28bcfSDaniel Vetter 	u32 mask;
3116d46da437SPaulo Zanoni 
3117692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3118692a04cfSDaniel Vetter 		return;
3119692a04cfSDaniel Vetter 
3120105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
31215c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3122105b122eSPaulo Zanoni 	else
31235c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
31248664281bSPaulo Zanoni 
3125337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3126d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3127d46da437SPaulo Zanoni }
3128d46da437SPaulo Zanoni 
31290a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
31300a9a8c91SDaniel Vetter {
31310a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
31320a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
31330a9a8c91SDaniel Vetter 
31340a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
31350a9a8c91SDaniel Vetter 
31360a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3137040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
31380a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
313935a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
314035a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
31410a9a8c91SDaniel Vetter 	}
31420a9a8c91SDaniel Vetter 
31430a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
31440a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
31450a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
31460a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
31470a9a8c91SDaniel Vetter 	} else {
31480a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
31490a9a8c91SDaniel Vetter 	}
31500a9a8c91SDaniel Vetter 
315135079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
31520a9a8c91SDaniel Vetter 
31530a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
315478e68d36SImre Deak 		/*
315578e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
315678e68d36SImre Deak 		 * itself is enabled/disabled.
315778e68d36SImre Deak 		 */
31580a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
31590a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
31600a9a8c91SDaniel Vetter 
3161605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
316235079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
31630a9a8c91SDaniel Vetter 	}
31640a9a8c91SDaniel Vetter }
31650a9a8c91SDaniel Vetter 
3166f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3167036a4a7dSZhenyu Wang {
31682d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
31698e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
31708e76f8dcSPaulo Zanoni 
31718e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
31728e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
31738e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
31748e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
31755c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
31768e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
31775c673b60SDaniel Vetter 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
31788e76f8dcSPaulo Zanoni 	} else {
31798e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3180ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
31815b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
31825b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
31835b3a856bSDaniel Vetter 				DE_POISON);
31845c673b60SDaniel Vetter 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
31855c673b60SDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
31868e76f8dcSPaulo Zanoni 	}
3187036a4a7dSZhenyu Wang 
31881ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3189036a4a7dSZhenyu Wang 
31900c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
31910c841212SPaulo Zanoni 
3192622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3193622364b6SPaulo Zanoni 
319435079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3195036a4a7dSZhenyu Wang 
31960a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3197036a4a7dSZhenyu Wang 
3198d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
31997fe0b973SKeith Packard 
3200f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
32016005ce42SDaniel Vetter 		/* Enable PCU event interrupts
32026005ce42SDaniel Vetter 		 *
32036005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
32044bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
32054bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3206d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3207f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3208d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3209f97108d1SJesse Barnes 	}
3210f97108d1SJesse Barnes 
3211036a4a7dSZhenyu Wang 	return 0;
3212036a4a7dSZhenyu Wang }
3213036a4a7dSZhenyu Wang 
3214f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3215f8b79e58SImre Deak {
3216f8b79e58SImre Deak 	u32 pipestat_mask;
3217f8b79e58SImre Deak 	u32 iir_mask;
3218120dda4fSVille Syrjälä 	enum pipe pipe;
3219f8b79e58SImre Deak 
3220f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3221f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3222f8b79e58SImre Deak 
3223120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3224120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3225f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3226f8b79e58SImre Deak 
3227f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3228f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3229f8b79e58SImre Deak 
3230120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3231120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3232120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3233f8b79e58SImre Deak 
3234f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3235f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3236f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3237120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3238120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3239f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3240f8b79e58SImre Deak 
3241f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3242f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3243f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
324476e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
324576e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3246f8b79e58SImre Deak }
3247f8b79e58SImre Deak 
3248f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3249f8b79e58SImre Deak {
3250f8b79e58SImre Deak 	u32 pipestat_mask;
3251f8b79e58SImre Deak 	u32 iir_mask;
3252120dda4fSVille Syrjälä 	enum pipe pipe;
3253f8b79e58SImre Deak 
3254f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3255f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
32566c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3257120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3258120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3259f8b79e58SImre Deak 
3260f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3261f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
326276e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3263f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3264f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3265f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3266f8b79e58SImre Deak 
3267f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3268f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3269f8b79e58SImre Deak 
3270120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3271120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3272120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3273f8b79e58SImre Deak 
3274f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3275f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3276120dda4fSVille Syrjälä 
3277120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3278120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3279f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3280f8b79e58SImre Deak }
3281f8b79e58SImre Deak 
3282f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3283f8b79e58SImre Deak {
3284f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3285f8b79e58SImre Deak 
3286f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3287f8b79e58SImre Deak 		return;
3288f8b79e58SImre Deak 
3289f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3290f8b79e58SImre Deak 
3291950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3292f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3293f8b79e58SImre Deak }
3294f8b79e58SImre Deak 
3295f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3296f8b79e58SImre Deak {
3297f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3298f8b79e58SImre Deak 
3299f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3300f8b79e58SImre Deak 		return;
3301f8b79e58SImre Deak 
3302f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3303f8b79e58SImre Deak 
3304950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3305f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3306f8b79e58SImre Deak }
3307f8b79e58SImre Deak 
33080e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
33097e231dbeSJesse Barnes {
3310f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
33117e231dbeSJesse Barnes 
331220afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
331320afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
331420afbda2SDaniel Vetter 
33157e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
331676e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
331776e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
331876e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
331976e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
33207e231dbeSJesse Barnes 
3321b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3322b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3323d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3324f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3325f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3326d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
33270e6c9a9eSVille Syrjälä }
33280e6c9a9eSVille Syrjälä 
33290e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
33300e6c9a9eSVille Syrjälä {
33310e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
33320e6c9a9eSVille Syrjälä 
33330e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
33347e231dbeSJesse Barnes 
33350a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
33367e231dbeSJesse Barnes 
33377e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
33387e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
33397e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
33407e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
33417e231dbeSJesse Barnes #endif
33427e231dbeSJesse Barnes 
33437e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
334420afbda2SDaniel Vetter 
334520afbda2SDaniel Vetter 	return 0;
334620afbda2SDaniel Vetter }
334720afbda2SDaniel Vetter 
3348abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3349abd58f01SBen Widawsky {
3350abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3351abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3352abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
335373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3354abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
335573d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
335673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3357abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
335873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
335973d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
336073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3361abd58f01SBen Widawsky 		0,
336273d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
336373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3364abd58f01SBen Widawsky 		};
3365abd58f01SBen Widawsky 
33660961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
33679a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
33689a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
336978e68d36SImre Deak 	/*
337078e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
337178e68d36SImre Deak 	 * is enabled/disabled.
337278e68d36SImre Deak 	 */
337378e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
33749a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3375abd58f01SBen Widawsky }
3376abd58f01SBen Widawsky 
3377abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3378abd58f01SBen Widawsky {
3379770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3380770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
3381abd58f01SBen Widawsky 	int pipe;
33829e63743eSShashank Sharma 	u32 de_port_en = GEN8_AUX_CHANNEL_A;
3383770de83dSDamien Lespiau 
338488e04703SJesse Barnes 	if (IS_GEN9(dev_priv)) {
3385770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3386770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
33879e63743eSShashank Sharma 		de_port_en |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
338888e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
33899e63743eSShashank Sharma 
33909e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
33919e63743eSShashank Sharma 			de_port_en |= BXT_DE_PORT_GMBUS;
339288e04703SJesse Barnes 	} else
3393770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3394770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
3395770de83dSDamien Lespiau 
3396770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3397770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3398770de83dSDamien Lespiau 
339913b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
340013b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
340113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3402abd58f01SBen Widawsky 
3403055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3404f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3405813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3406813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3407813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
340835079899SPaulo Zanoni 					  de_pipe_enables);
3409abd58f01SBen Widawsky 
34109e63743eSShashank Sharma 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_en, de_port_en);
3411abd58f01SBen Widawsky }
3412abd58f01SBen Widawsky 
3413abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3414abd58f01SBen Widawsky {
3415abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3416abd58f01SBen Widawsky 
3417266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3418622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3419622364b6SPaulo Zanoni 
3420abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3421abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3422abd58f01SBen Widawsky 
3423266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3424abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3425abd58f01SBen Widawsky 
3426abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3427abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3428abd58f01SBen Widawsky 
3429abd58f01SBen Widawsky 	return 0;
3430abd58f01SBen Widawsky }
3431abd58f01SBen Widawsky 
343243f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
343343f328d7SVille Syrjälä {
343443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
343543f328d7SVille Syrjälä 
3436c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
343743f328d7SVille Syrjälä 
343843f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
343943f328d7SVille Syrjälä 
344043f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
344143f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
344243f328d7SVille Syrjälä 
344343f328d7SVille Syrjälä 	return 0;
344443f328d7SVille Syrjälä }
344543f328d7SVille Syrjälä 
3446abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3447abd58f01SBen Widawsky {
3448abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3449abd58f01SBen Widawsky 
3450abd58f01SBen Widawsky 	if (!dev_priv)
3451abd58f01SBen Widawsky 		return;
3452abd58f01SBen Widawsky 
3453823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3454abd58f01SBen Widawsky }
3455abd58f01SBen Widawsky 
34568ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
34578ea0be4fSVille Syrjälä {
34588ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
34598ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
34608ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34618ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
34628ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
34638ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
34648ea0be4fSVille Syrjälä 
34658ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
34668ea0be4fSVille Syrjälä 
3467c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
34688ea0be4fSVille Syrjälä }
34698ea0be4fSVille Syrjälä 
34707e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
34717e231dbeSJesse Barnes {
34722d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
34737e231dbeSJesse Barnes 
34747e231dbeSJesse Barnes 	if (!dev_priv)
34757e231dbeSJesse Barnes 		return;
34767e231dbeSJesse Barnes 
3477843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3478843d0e7dSImre Deak 
3479893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3480893fce8eSVille Syrjälä 
34817e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3482f8b79e58SImre Deak 
34838ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
34847e231dbeSJesse Barnes }
34857e231dbeSJesse Barnes 
348643f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
348743f328d7SVille Syrjälä {
348843f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
348943f328d7SVille Syrjälä 
349043f328d7SVille Syrjälä 	if (!dev_priv)
349143f328d7SVille Syrjälä 		return;
349243f328d7SVille Syrjälä 
349343f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
349443f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
349543f328d7SVille Syrjälä 
3496a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
349743f328d7SVille Syrjälä 
3498a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
349943f328d7SVille Syrjälä 
3500c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
350143f328d7SVille Syrjälä }
350243f328d7SVille Syrjälä 
3503f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3504036a4a7dSZhenyu Wang {
35052d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
35064697995bSJesse Barnes 
35074697995bSJesse Barnes 	if (!dev_priv)
35084697995bSJesse Barnes 		return;
35094697995bSJesse Barnes 
3510be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3511036a4a7dSZhenyu Wang }
3512036a4a7dSZhenyu Wang 
3513c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3514c2798b19SChris Wilson {
35152d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3516c2798b19SChris Wilson 	int pipe;
3517c2798b19SChris Wilson 
3518055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3519c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3520c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3521c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3522c2798b19SChris Wilson 	POSTING_READ16(IER);
3523c2798b19SChris Wilson }
3524c2798b19SChris Wilson 
3525c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3526c2798b19SChris Wilson {
35272d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3528c2798b19SChris Wilson 
3529c2798b19SChris Wilson 	I915_WRITE16(EMR,
3530c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3531c2798b19SChris Wilson 
3532c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3533c2798b19SChris Wilson 	dev_priv->irq_mask =
3534c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3535c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3536c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
353737ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3538c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3539c2798b19SChris Wilson 
3540c2798b19SChris Wilson 	I915_WRITE16(IER,
3541c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3542c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3543c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3544c2798b19SChris Wilson 	POSTING_READ16(IER);
3545c2798b19SChris Wilson 
3546379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3547379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3548d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3549755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3550755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3551d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3552379ef82dSDaniel Vetter 
3553c2798b19SChris Wilson 	return 0;
3554c2798b19SChris Wilson }
3555c2798b19SChris Wilson 
355690a72f87SVille Syrjälä /*
355790a72f87SVille Syrjälä  * Returns true when a page flip has completed.
355890a72f87SVille Syrjälä  */
355990a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
35601f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
356190a72f87SVille Syrjälä {
35622d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
35631f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
356490a72f87SVille Syrjälä 
35658d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
356690a72f87SVille Syrjälä 		return false;
356790a72f87SVille Syrjälä 
356890a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3569d6bbafa1SChris Wilson 		goto check_page_flip;
357090a72f87SVille Syrjälä 
357190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
357290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
357390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
357490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
357590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
357690a72f87SVille Syrjälä 	 */
357790a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3578d6bbafa1SChris Wilson 		goto check_page_flip;
357990a72f87SVille Syrjälä 
35807d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
358190a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
358290a72f87SVille Syrjälä 	return true;
3583d6bbafa1SChris Wilson 
3584d6bbafa1SChris Wilson check_page_flip:
3585d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3586d6bbafa1SChris Wilson 	return false;
358790a72f87SVille Syrjälä }
358890a72f87SVille Syrjälä 
3589ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3590c2798b19SChris Wilson {
359145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
35922d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3593c2798b19SChris Wilson 	u16 iir, new_iir;
3594c2798b19SChris Wilson 	u32 pipe_stats[2];
3595c2798b19SChris Wilson 	int pipe;
3596c2798b19SChris Wilson 	u16 flip_mask =
3597c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3598c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3599c2798b19SChris Wilson 
36002dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
36012dd2a883SImre Deak 		return IRQ_NONE;
36022dd2a883SImre Deak 
3603c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3604c2798b19SChris Wilson 	if (iir == 0)
3605c2798b19SChris Wilson 		return IRQ_NONE;
3606c2798b19SChris Wilson 
3607c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3608c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3609c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3610c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3611c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3612c2798b19SChris Wilson 		 */
3613222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3614c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3615aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3616c2798b19SChris Wilson 
3617055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3618c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3619c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3620c2798b19SChris Wilson 
3621c2798b19SChris Wilson 			/*
3622c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3623c2798b19SChris Wilson 			 */
36242d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3625c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3626c2798b19SChris Wilson 		}
3627222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3628c2798b19SChris Wilson 
3629c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3630c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3631c2798b19SChris Wilson 
3632c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
363374cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3634c2798b19SChris Wilson 
3635055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
36361f1c2e24SVille Syrjälä 			int plane = pipe;
36373a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
36381f1c2e24SVille Syrjälä 				plane = !plane;
36391f1c2e24SVille Syrjälä 
36404356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
36411f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
36421f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3643c2798b19SChris Wilson 
36444356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3645277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
36462d9d2b0bSVille Syrjälä 
36471f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
36481f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
36491f7247c0SDaniel Vetter 								    pipe);
36504356d586SDaniel Vetter 		}
3651c2798b19SChris Wilson 
3652c2798b19SChris Wilson 		iir = new_iir;
3653c2798b19SChris Wilson 	}
3654c2798b19SChris Wilson 
3655c2798b19SChris Wilson 	return IRQ_HANDLED;
3656c2798b19SChris Wilson }
3657c2798b19SChris Wilson 
3658c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3659c2798b19SChris Wilson {
36602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3661c2798b19SChris Wilson 	int pipe;
3662c2798b19SChris Wilson 
3663055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3664c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3665c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3666c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3667c2798b19SChris Wilson 	}
3668c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3669c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3670c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3671c2798b19SChris Wilson }
3672c2798b19SChris Wilson 
3673a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3674a266c7d5SChris Wilson {
36752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3676a266c7d5SChris Wilson 	int pipe;
3677a266c7d5SChris Wilson 
3678a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3679a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3680a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3681a266c7d5SChris Wilson 	}
3682a266c7d5SChris Wilson 
368300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3684055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3685a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3686a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3687a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3688a266c7d5SChris Wilson 	POSTING_READ(IER);
3689a266c7d5SChris Wilson }
3690a266c7d5SChris Wilson 
3691a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3692a266c7d5SChris Wilson {
36932d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
369438bde180SChris Wilson 	u32 enable_mask;
3695a266c7d5SChris Wilson 
369638bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
369738bde180SChris Wilson 
369838bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
369938bde180SChris Wilson 	dev_priv->irq_mask =
370038bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
370138bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
370238bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
370338bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
370437ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
370538bde180SChris Wilson 
370638bde180SChris Wilson 	enable_mask =
370738bde180SChris Wilson 		I915_ASLE_INTERRUPT |
370838bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
370938bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
371038bde180SChris Wilson 		I915_USER_INTERRUPT;
371138bde180SChris Wilson 
3712a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
371320afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
371420afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
371520afbda2SDaniel Vetter 
3716a266c7d5SChris Wilson 		/* Enable in IER... */
3717a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3718a266c7d5SChris Wilson 		/* and unmask in IMR */
3719a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3720a266c7d5SChris Wilson 	}
3721a266c7d5SChris Wilson 
3722a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3723a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3724a266c7d5SChris Wilson 	POSTING_READ(IER);
3725a266c7d5SChris Wilson 
3726f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
372720afbda2SDaniel Vetter 
3728379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3729379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3730d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3731755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3732755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3733d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3734379ef82dSDaniel Vetter 
373520afbda2SDaniel Vetter 	return 0;
373620afbda2SDaniel Vetter }
373720afbda2SDaniel Vetter 
373890a72f87SVille Syrjälä /*
373990a72f87SVille Syrjälä  * Returns true when a page flip has completed.
374090a72f87SVille Syrjälä  */
374190a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
374290a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
374390a72f87SVille Syrjälä {
37442d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
374590a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
374690a72f87SVille Syrjälä 
37478d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
374890a72f87SVille Syrjälä 		return false;
374990a72f87SVille Syrjälä 
375090a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3751d6bbafa1SChris Wilson 		goto check_page_flip;
375290a72f87SVille Syrjälä 
375390a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
375490a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
375590a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
375690a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
375790a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
375890a72f87SVille Syrjälä 	 */
375990a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3760d6bbafa1SChris Wilson 		goto check_page_flip;
376190a72f87SVille Syrjälä 
37627d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
376390a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
376490a72f87SVille Syrjälä 	return true;
3765d6bbafa1SChris Wilson 
3766d6bbafa1SChris Wilson check_page_flip:
3767d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3768d6bbafa1SChris Wilson 	return false;
376990a72f87SVille Syrjälä }
377090a72f87SVille Syrjälä 
3771ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3772a266c7d5SChris Wilson {
377345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
37742d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37758291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
377638bde180SChris Wilson 	u32 flip_mask =
377738bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
377838bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
377938bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3780a266c7d5SChris Wilson 
37812dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
37822dd2a883SImre Deak 		return IRQ_NONE;
37832dd2a883SImre Deak 
3784a266c7d5SChris Wilson 	iir = I915_READ(IIR);
378538bde180SChris Wilson 	do {
378638bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
37878291ee90SChris Wilson 		bool blc_event = false;
3788a266c7d5SChris Wilson 
3789a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3790a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3791a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3792a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3793a266c7d5SChris Wilson 		 */
3794222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3795a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3796aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3797a266c7d5SChris Wilson 
3798055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3799a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3800a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3801a266c7d5SChris Wilson 
380238bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3803a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3804a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
380538bde180SChris Wilson 				irq_received = true;
3806a266c7d5SChris Wilson 			}
3807a266c7d5SChris Wilson 		}
3808222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3809a266c7d5SChris Wilson 
3810a266c7d5SChris Wilson 		if (!irq_received)
3811a266c7d5SChris Wilson 			break;
3812a266c7d5SChris Wilson 
3813a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
381416c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
381516c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
381616c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3817a266c7d5SChris Wilson 
381838bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3819a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3820a266c7d5SChris Wilson 
3821a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
382274cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3823a266c7d5SChris Wilson 
3824055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
382538bde180SChris Wilson 			int plane = pipe;
38263a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
382738bde180SChris Wilson 				plane = !plane;
38285e2032d4SVille Syrjälä 
382990a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
383090a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
383190a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3832a266c7d5SChris Wilson 
3833a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3834a266c7d5SChris Wilson 				blc_event = true;
38354356d586SDaniel Vetter 
38364356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3837277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
38382d9d2b0bSVille Syrjälä 
38391f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
38401f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
38411f7247c0SDaniel Vetter 								    pipe);
3842a266c7d5SChris Wilson 		}
3843a266c7d5SChris Wilson 
3844a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3845a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3846a266c7d5SChris Wilson 
3847a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3848a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3849a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3850a266c7d5SChris Wilson 		 * we would never get another interrupt.
3851a266c7d5SChris Wilson 		 *
3852a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3853a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3854a266c7d5SChris Wilson 		 * another one.
3855a266c7d5SChris Wilson 		 *
3856a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3857a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3858a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3859a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3860a266c7d5SChris Wilson 		 * stray interrupts.
3861a266c7d5SChris Wilson 		 */
386238bde180SChris Wilson 		ret = IRQ_HANDLED;
3863a266c7d5SChris Wilson 		iir = new_iir;
386438bde180SChris Wilson 	} while (iir & ~flip_mask);
3865a266c7d5SChris Wilson 
3866a266c7d5SChris Wilson 	return ret;
3867a266c7d5SChris Wilson }
3868a266c7d5SChris Wilson 
3869a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3870a266c7d5SChris Wilson {
38712d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3872a266c7d5SChris Wilson 	int pipe;
3873a266c7d5SChris Wilson 
3874a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3875a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3876a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3877a266c7d5SChris Wilson 	}
3878a266c7d5SChris Wilson 
387900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
3880055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
388155b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3882a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
388355b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
388455b39755SChris Wilson 	}
3885a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3886a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3887a266c7d5SChris Wilson 
3888a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3889a266c7d5SChris Wilson }
3890a266c7d5SChris Wilson 
3891a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3892a266c7d5SChris Wilson {
38932d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3894a266c7d5SChris Wilson 	int pipe;
3895a266c7d5SChris Wilson 
3896a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3897a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3898a266c7d5SChris Wilson 
3899a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3900055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3901a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3902a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3903a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3904a266c7d5SChris Wilson 	POSTING_READ(IER);
3905a266c7d5SChris Wilson }
3906a266c7d5SChris Wilson 
3907a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3908a266c7d5SChris Wilson {
39092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3910bbba0a97SChris Wilson 	u32 enable_mask;
3911a266c7d5SChris Wilson 	u32 error_mask;
3912a266c7d5SChris Wilson 
3913a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3914bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3915adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3916bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3917bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3918bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3919bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3920bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3921bbba0a97SChris Wilson 
3922bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
392321ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
392421ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3925bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3926bbba0a97SChris Wilson 
3927bbba0a97SChris Wilson 	if (IS_G4X(dev))
3928bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3929a266c7d5SChris Wilson 
3930b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3931b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3932d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3933755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3934755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3935755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3936d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3937a266c7d5SChris Wilson 
3938a266c7d5SChris Wilson 	/*
3939a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3940a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3941a266c7d5SChris Wilson 	 */
3942a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3943a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3944a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3945a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3946a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3947a266c7d5SChris Wilson 	} else {
3948a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3949a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3950a266c7d5SChris Wilson 	}
3951a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3952a266c7d5SChris Wilson 
3953a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3954a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3955a266c7d5SChris Wilson 	POSTING_READ(IER);
3956a266c7d5SChris Wilson 
395720afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
395820afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
395920afbda2SDaniel Vetter 
3960f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
396120afbda2SDaniel Vetter 
396220afbda2SDaniel Vetter 	return 0;
396320afbda2SDaniel Vetter }
396420afbda2SDaniel Vetter 
3965bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
396620afbda2SDaniel Vetter {
39672d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3968cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
396920afbda2SDaniel Vetter 	u32 hotplug_en;
397020afbda2SDaniel Vetter 
3971b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3972b5ea2d56SDaniel Vetter 
3973bac56d5bSEgbert Eich 	hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3974bac56d5bSEgbert Eich 	hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3975adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
3976e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
3977b2784e15SDamien Lespiau 	for_each_intel_encoder(dev, intel_encoder)
39785fcece80SJani Nikula 		if (dev_priv->hotplug.stats[intel_encoder->hpd_pin].state == HPD_ENABLED)
3979cd569aedSEgbert Eich 			hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3980a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
3981a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
3982a266c7d5SChris Wilson 	   seconds later.  So just do it once.
3983a266c7d5SChris Wilson 	*/
3984a266c7d5SChris Wilson 	if (IS_G4X(dev))
3985a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
398685fc95baSDaniel Vetter 	hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3987a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3988a266c7d5SChris Wilson 
3989a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
3990a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3991a266c7d5SChris Wilson }
3992a266c7d5SChris Wilson 
3993ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3994a266c7d5SChris Wilson {
399545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39962d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3997a266c7d5SChris Wilson 	u32 iir, new_iir;
3998a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3999a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
400021ad8330SVille Syrjälä 	u32 flip_mask =
400121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
400221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4003a266c7d5SChris Wilson 
40042dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40052dd2a883SImre Deak 		return IRQ_NONE;
40062dd2a883SImre Deak 
4007a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4008a266c7d5SChris Wilson 
4009a266c7d5SChris Wilson 	for (;;) {
4010501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
40112c8ba29fSChris Wilson 		bool blc_event = false;
40122c8ba29fSChris Wilson 
4013a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4014a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4015a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4016a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4017a266c7d5SChris Wilson 		 */
4018222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4019a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4020aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4021a266c7d5SChris Wilson 
4022055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4023a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4024a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4025a266c7d5SChris Wilson 
4026a266c7d5SChris Wilson 			/*
4027a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4028a266c7d5SChris Wilson 			 */
4029a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4030a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4031501e01d7SVille Syrjälä 				irq_received = true;
4032a266c7d5SChris Wilson 			}
4033a266c7d5SChris Wilson 		}
4034222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4035a266c7d5SChris Wilson 
4036a266c7d5SChris Wilson 		if (!irq_received)
4037a266c7d5SChris Wilson 			break;
4038a266c7d5SChris Wilson 
4039a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4040a266c7d5SChris Wilson 
4041a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
404216c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
404316c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4044a266c7d5SChris Wilson 
404521ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4046a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4047a266c7d5SChris Wilson 
4048a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
404974cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4050a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
405174cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VCS]);
4052a266c7d5SChris Wilson 
4053055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
40542c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
405590a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
405690a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4057a266c7d5SChris Wilson 
4058a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4059a266c7d5SChris Wilson 				blc_event = true;
40604356d586SDaniel Vetter 
40614356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4062277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4063a266c7d5SChris Wilson 
40641f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40651f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
40662d9d2b0bSVille Syrjälä 		}
4067a266c7d5SChris Wilson 
4068a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4069a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4070a266c7d5SChris Wilson 
4071515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4072515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4073515ac2bbSDaniel Vetter 
4074a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4075a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4076a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4077a266c7d5SChris Wilson 		 * we would never get another interrupt.
4078a266c7d5SChris Wilson 		 *
4079a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4080a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4081a266c7d5SChris Wilson 		 * another one.
4082a266c7d5SChris Wilson 		 *
4083a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4084a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4085a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4086a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4087a266c7d5SChris Wilson 		 * stray interrupts.
4088a266c7d5SChris Wilson 		 */
4089a266c7d5SChris Wilson 		iir = new_iir;
4090a266c7d5SChris Wilson 	}
4091a266c7d5SChris Wilson 
4092a266c7d5SChris Wilson 	return ret;
4093a266c7d5SChris Wilson }
4094a266c7d5SChris Wilson 
4095a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4096a266c7d5SChris Wilson {
40972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4098a266c7d5SChris Wilson 	int pipe;
4099a266c7d5SChris Wilson 
4100a266c7d5SChris Wilson 	if (!dev_priv)
4101a266c7d5SChris Wilson 		return;
4102a266c7d5SChris Wilson 
4103a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4104a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4105a266c7d5SChris Wilson 
4106a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4107055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4108a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4109a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4110a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4111a266c7d5SChris Wilson 
4112055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4113a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4114a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4115a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4116a266c7d5SChris Wilson }
4117a266c7d5SChris Wilson 
4118fca52a55SDaniel Vetter /**
4119fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4120fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4121fca52a55SDaniel Vetter  *
4122fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4123fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4124fca52a55SDaniel Vetter  */
4125b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4126f71d4af4SJesse Barnes {
4127b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
41288b2e326dSChris Wilson 
412977913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
413077913b39SJani Nikula 
4131c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4132a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
41338b2e326dSChris Wilson 
4134a6706b45SDeepak S 	/* Let's track the enabled rps events */
4135b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
41366c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
41376f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
413831685c25SDeepak S 	else
4139a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4140a6706b45SDeepak S 
4141737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4142737b1506SChris Wilson 			  i915_hangcheck_elapsed);
414361bac78eSDaniel Vetter 
414497a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
41459ee32feaSDaniel Vetter 
4146b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
41474cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
41484cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4149b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4150f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4151f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4152391f75e2SVille Syrjälä 	} else {
4153391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4154391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4155f71d4af4SJesse Barnes 	}
4156f71d4af4SJesse Barnes 
415721da2700SVille Syrjälä 	/*
415821da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
415921da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
416021da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
416121da2700SVille Syrjälä 	 */
4162b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
416321da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
416421da2700SVille Syrjälä 
4165f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4166f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4167f71d4af4SJesse Barnes 
4168b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
416943f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
417043f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
417143f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
417243f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
417343f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
417443f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
417543f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4176b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
41777e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
41787e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
41797e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
41807e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
41817e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
41827e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4183fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4184b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4185abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4186723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4187abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4188abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4189abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4190abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4191e0a20ad7SShashank Sharma 		if (HAS_PCH_SPLIT(dev))
4192abd58f01SBen Widawsky 			dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4193e0a20ad7SShashank Sharma 		else
4194e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
4195f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4196f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4197723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4198f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4199f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4200f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4201f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
420282a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4203f71d4af4SJesse Barnes 	} else {
4204b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4205c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4206c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4207c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4208c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4209b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4210a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4211a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4212a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4213a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4214c2798b19SChris Wilson 		} else {
4215a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4216a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4217a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4218a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4219c2798b19SChris Wilson 		}
4220778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4221778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4222f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4223f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4224f71d4af4SJesse Barnes 	}
4225f71d4af4SJesse Barnes }
422620afbda2SDaniel Vetter 
4227fca52a55SDaniel Vetter /**
4228fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4229fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4230fca52a55SDaniel Vetter  *
4231fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4232fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4233fca52a55SDaniel Vetter  *
4234fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4235fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4236fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4237fca52a55SDaniel Vetter  */
42382aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
42392aeb7d3aSDaniel Vetter {
42402aeb7d3aSDaniel Vetter 	/*
42412aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
42422aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
42432aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
42442aeb7d3aSDaniel Vetter 	 */
42452aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
42462aeb7d3aSDaniel Vetter 
42472aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
42482aeb7d3aSDaniel Vetter }
42492aeb7d3aSDaniel Vetter 
4250fca52a55SDaniel Vetter /**
4251fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4252fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4253fca52a55SDaniel Vetter  *
4254fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4255fca52a55SDaniel Vetter  * resources acquired in the init functions.
4256fca52a55SDaniel Vetter  */
42572aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
42582aeb7d3aSDaniel Vetter {
42592aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
42602aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
42612aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
42622aeb7d3aSDaniel Vetter }
42632aeb7d3aSDaniel Vetter 
4264fca52a55SDaniel Vetter /**
4265fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4266fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4267fca52a55SDaniel Vetter  *
4268fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4269fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4270fca52a55SDaniel Vetter  */
4271b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4272c67a470bSPaulo Zanoni {
4273b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
42742aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
42752dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4276c67a470bSPaulo Zanoni }
4277c67a470bSPaulo Zanoni 
4278fca52a55SDaniel Vetter /**
4279fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4280fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4281fca52a55SDaniel Vetter  *
4282fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4283fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4284fca52a55SDaniel Vetter  */
4285b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4286c67a470bSPaulo Zanoni {
42872aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4288b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4289b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4290c67a470bSPaulo Zanoni }
4291