xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 1dd246fb165819d31119e988c2887934c255fadc)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33760285e7SDavid Howells #include <drm/drmP.h>
34760285e7SDavid Howells #include <drm/i915_drm.h>
35c0e09200SDave Airlie #include "i915_drv.h"
361c5d22f7SChris Wilson #include "i915_trace.h"
3779e53945SJesse Barnes #include "intel_drv.h"
38c0e09200SDave Airlie 
39e5868a31SEgbert Eich static const u32 hpd_ibx[] = {
40e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
41e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
42e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
43e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
44e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
45e5868a31SEgbert Eich };
46e5868a31SEgbert Eich 
47e5868a31SEgbert Eich static const u32 hpd_cpt[] = {
48e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
4973c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
50e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
51e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
52e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
53e5868a31SEgbert Eich };
54e5868a31SEgbert Eich 
55e5868a31SEgbert Eich static const u32 hpd_mask_i915[] = {
56e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
57e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
58e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
59e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
60e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
61e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
62e5868a31SEgbert Eich };
63e5868a31SEgbert Eich 
64e5868a31SEgbert Eich static const u32 hpd_status_gen4[] = {
65e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
66e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
67e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
68e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
69e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
70e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
71e5868a31SEgbert Eich };
72e5868a31SEgbert Eich 
73e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
74e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
75e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
76e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
77e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
78e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
79e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
80e5868a31SEgbert Eich };
81e5868a31SEgbert Eich 
82036a4a7dSZhenyu Wang /* For display hotplug interrupt */
83995b6762SChris Wilson static void
84f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
85036a4a7dSZhenyu Wang {
864bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
874bc9d430SDaniel Vetter 
881ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
891ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
901ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
913143a2bfSChris Wilson 		POSTING_READ(DEIMR);
92036a4a7dSZhenyu Wang 	}
93036a4a7dSZhenyu Wang }
94036a4a7dSZhenyu Wang 
950ff9800aSPaulo Zanoni static void
96f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
97036a4a7dSZhenyu Wang {
984bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
994bc9d430SDaniel Vetter 
1001ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1011ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1021ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1033143a2bfSChris Wilson 		POSTING_READ(DEIMR);
104036a4a7dSZhenyu Wang 	}
105036a4a7dSZhenyu Wang }
106036a4a7dSZhenyu Wang 
1078664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
1088664281bSPaulo Zanoni {
1098664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1108664281bSPaulo Zanoni 	struct intel_crtc *crtc;
1118664281bSPaulo Zanoni 	enum pipe pipe;
1128664281bSPaulo Zanoni 
1134bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1144bc9d430SDaniel Vetter 
1158664281bSPaulo Zanoni 	for_each_pipe(pipe) {
1168664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
1178664281bSPaulo Zanoni 
1188664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
1198664281bSPaulo Zanoni 			return false;
1208664281bSPaulo Zanoni 	}
1218664281bSPaulo Zanoni 
1228664281bSPaulo Zanoni 	return true;
1238664281bSPaulo Zanoni }
1248664281bSPaulo Zanoni 
1258664281bSPaulo Zanoni static bool cpt_can_enable_serr_int(struct drm_device *dev)
1268664281bSPaulo Zanoni {
1278664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1288664281bSPaulo Zanoni 	enum pipe pipe;
1298664281bSPaulo Zanoni 	struct intel_crtc *crtc;
1308664281bSPaulo Zanoni 
131fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
132fee884edSDaniel Vetter 
1338664281bSPaulo Zanoni 	for_each_pipe(pipe) {
1348664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
1358664281bSPaulo Zanoni 
1368664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
1378664281bSPaulo Zanoni 			return false;
1388664281bSPaulo Zanoni 	}
1398664281bSPaulo Zanoni 
1408664281bSPaulo Zanoni 	return true;
1418664281bSPaulo Zanoni }
1428664281bSPaulo Zanoni 
1438664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
1448664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
1458664281bSPaulo Zanoni {
1468664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1478664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
1488664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
1498664281bSPaulo Zanoni 
1508664281bSPaulo Zanoni 	if (enable)
1518664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
1528664281bSPaulo Zanoni 	else
1538664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
1548664281bSPaulo Zanoni }
1558664281bSPaulo Zanoni 
1568664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
1578664281bSPaulo Zanoni 						  bool enable)
1588664281bSPaulo Zanoni {
1598664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1608664281bSPaulo Zanoni 
1618664281bSPaulo Zanoni 	if (enable) {
1628664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
1638664281bSPaulo Zanoni 			return;
1648664281bSPaulo Zanoni 
1658664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN_A |
1668664281bSPaulo Zanoni 					 ERR_INT_FIFO_UNDERRUN_B |
1678664281bSPaulo Zanoni 					 ERR_INT_FIFO_UNDERRUN_C);
1688664281bSPaulo Zanoni 
1698664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
1708664281bSPaulo Zanoni 	} else {
1718664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
1728664281bSPaulo Zanoni 	}
1738664281bSPaulo Zanoni }
1748664281bSPaulo Zanoni 
175fee884edSDaniel Vetter /**
176fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
177fee884edSDaniel Vetter  * @dev_priv: driver private
178fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
179fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
180fee884edSDaniel Vetter  */
181fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
182fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
183fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
184fee884edSDaniel Vetter {
185fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
186fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
187fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
188fee884edSDaniel Vetter 
189fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
190fee884edSDaniel Vetter 
191fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
192fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
193fee884edSDaniel Vetter }
194fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
195fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
196fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
197fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
198fee884edSDaniel Vetter 
1998664281bSPaulo Zanoni static void ibx_set_fifo_underrun_reporting(struct intel_crtc *crtc,
2008664281bSPaulo Zanoni 					    bool enable)
2018664281bSPaulo Zanoni {
2028664281bSPaulo Zanoni 	struct drm_device *dev = crtc->base.dev;
2038664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2048664281bSPaulo Zanoni 	uint32_t bit = (crtc->pipe == PIPE_A) ? SDE_TRANSA_FIFO_UNDER :
2058664281bSPaulo Zanoni 						SDE_TRANSB_FIFO_UNDER;
2068664281bSPaulo Zanoni 
2078664281bSPaulo Zanoni 	if (enable)
208fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
2098664281bSPaulo Zanoni 	else
210fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
2118664281bSPaulo Zanoni }
2128664281bSPaulo Zanoni 
2138664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
2148664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
2158664281bSPaulo Zanoni 					    bool enable)
2168664281bSPaulo Zanoni {
2178664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2188664281bSPaulo Zanoni 
2198664281bSPaulo Zanoni 	if (enable) {
220*1dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
221*1dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
222*1dd246fbSDaniel Vetter 
2238664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
2248664281bSPaulo Zanoni 			return;
2258664281bSPaulo Zanoni 
226fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
2278664281bSPaulo Zanoni 	} else {
228*1dd246fbSDaniel Vetter 		uint32_t tmp = I915_READ(SERR_INT);
229*1dd246fbSDaniel Vetter 		bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
230*1dd246fbSDaniel Vetter 
231*1dd246fbSDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
232fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
233*1dd246fbSDaniel Vetter 
234*1dd246fbSDaniel Vetter 		if (!was_enabled &&
235*1dd246fbSDaniel Vetter 		    (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
236*1dd246fbSDaniel Vetter 			DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
237*1dd246fbSDaniel Vetter 				      transcoder_name(pch_transcoder));
238*1dd246fbSDaniel Vetter 		}
2398664281bSPaulo Zanoni 	}
2408664281bSPaulo Zanoni }
2418664281bSPaulo Zanoni 
2428664281bSPaulo Zanoni /**
2438664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
2448664281bSPaulo Zanoni  * @dev: drm device
2458664281bSPaulo Zanoni  * @pipe: pipe
2468664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
2478664281bSPaulo Zanoni  *
2488664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
2498664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
2508664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
2518664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
2528664281bSPaulo Zanoni  * bit for all the pipes.
2538664281bSPaulo Zanoni  *
2548664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
2558664281bSPaulo Zanoni  */
2568664281bSPaulo Zanoni bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
2578664281bSPaulo Zanoni 					   enum pipe pipe, bool enable)
2588664281bSPaulo Zanoni {
2598664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2608664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
2618664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2628664281bSPaulo Zanoni 	unsigned long flags;
2638664281bSPaulo Zanoni 	bool ret;
2648664281bSPaulo Zanoni 
2658664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
2668664281bSPaulo Zanoni 
2678664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
2688664281bSPaulo Zanoni 
2698664281bSPaulo Zanoni 	if (enable == ret)
2708664281bSPaulo Zanoni 		goto done;
2718664281bSPaulo Zanoni 
2728664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
2738664281bSPaulo Zanoni 
2748664281bSPaulo Zanoni 	if (IS_GEN5(dev) || IS_GEN6(dev))
2758664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
2768664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
2778664281bSPaulo Zanoni 		ivybridge_set_fifo_underrun_reporting(dev, enable);
2788664281bSPaulo Zanoni 
2798664281bSPaulo Zanoni done:
2808664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
2818664281bSPaulo Zanoni 	return ret;
2828664281bSPaulo Zanoni }
2838664281bSPaulo Zanoni 
2848664281bSPaulo Zanoni /**
2858664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
2868664281bSPaulo Zanoni  * @dev: drm device
2878664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
2888664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
2898664281bSPaulo Zanoni  *
2908664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
2918664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
2928664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
2938664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
2948664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
2958664281bSPaulo Zanoni  *
2968664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
2978664281bSPaulo Zanoni  */
2988664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
2998664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
3008664281bSPaulo Zanoni 					   bool enable)
3018664281bSPaulo Zanoni {
3028664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3038664281bSPaulo Zanoni 	enum pipe p;
3048664281bSPaulo Zanoni 	struct drm_crtc *crtc;
3058664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc;
3068664281bSPaulo Zanoni 	unsigned long flags;
3078664281bSPaulo Zanoni 	bool ret;
3088664281bSPaulo Zanoni 
3098664281bSPaulo Zanoni 	if (HAS_PCH_LPT(dev)) {
3108664281bSPaulo Zanoni 		crtc = NULL;
3118664281bSPaulo Zanoni 		for_each_pipe(p) {
3128664281bSPaulo Zanoni 			struct drm_crtc *c = dev_priv->pipe_to_crtc_mapping[p];
3138664281bSPaulo Zanoni 			if (intel_pipe_has_type(c, INTEL_OUTPUT_ANALOG)) {
3148664281bSPaulo Zanoni 				crtc = c;
3158664281bSPaulo Zanoni 				break;
3168664281bSPaulo Zanoni 			}
3178664281bSPaulo Zanoni 		}
3188664281bSPaulo Zanoni 		if (!crtc) {
3198664281bSPaulo Zanoni 			DRM_ERROR("PCH FIFO underrun, but no CRTC using the PCH found\n");
3208664281bSPaulo Zanoni 			return false;
3218664281bSPaulo Zanoni 		}
3228664281bSPaulo Zanoni 	} else {
3238664281bSPaulo Zanoni 		crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
3248664281bSPaulo Zanoni 	}
3258664281bSPaulo Zanoni 	intel_crtc = to_intel_crtc(crtc);
3268664281bSPaulo Zanoni 
3278664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
3288664281bSPaulo Zanoni 
3298664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
3308664281bSPaulo Zanoni 
3318664281bSPaulo Zanoni 	if (enable == ret)
3328664281bSPaulo Zanoni 		goto done;
3338664281bSPaulo Zanoni 
3348664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
3358664281bSPaulo Zanoni 
3368664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
3378664281bSPaulo Zanoni 		ibx_set_fifo_underrun_reporting(intel_crtc, enable);
3388664281bSPaulo Zanoni 	else
3398664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
3408664281bSPaulo Zanoni 
3418664281bSPaulo Zanoni done:
3428664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
3438664281bSPaulo Zanoni 	return ret;
3448664281bSPaulo Zanoni }
3458664281bSPaulo Zanoni 
3468664281bSPaulo Zanoni 
3477c463586SKeith Packard void
3487c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3497c463586SKeith Packard {
3509db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
35146c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3527c463586SKeith Packard 
353b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
354b79480baSDaniel Vetter 
35546c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
35646c06a30SVille Syrjälä 		return;
35746c06a30SVille Syrjälä 
3587c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
35946c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
36046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3613143a2bfSChris Wilson 	POSTING_READ(reg);
3627c463586SKeith Packard }
3637c463586SKeith Packard 
3647c463586SKeith Packard void
3657c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3667c463586SKeith Packard {
3679db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
36846c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3697c463586SKeith Packard 
370b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
371b79480baSDaniel Vetter 
37246c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
37346c06a30SVille Syrjälä 		return;
37446c06a30SVille Syrjälä 
37546c06a30SVille Syrjälä 	pipestat &= ~mask;
37646c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3773143a2bfSChris Wilson 	POSTING_READ(reg);
3787c463586SKeith Packard }
3797c463586SKeith Packard 
380c0e09200SDave Airlie /**
381f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
38201c66889SZhao Yakui  */
383f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
38401c66889SZhao Yakui {
3851ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
3861ec14ad3SChris Wilson 	unsigned long irqflags;
3871ec14ad3SChris Wilson 
388f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
389f49e38ddSJani Nikula 		return;
390f49e38ddSJani Nikula 
3911ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
39201c66889SZhao Yakui 
393f898780bSJani Nikula 	i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
394a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
395f898780bSJani Nikula 		i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
3961ec14ad3SChris Wilson 
3971ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
39801c66889SZhao Yakui }
39901c66889SZhao Yakui 
40001c66889SZhao Yakui /**
4010a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
4020a3e67a4SJesse Barnes  * @dev: DRM device
4030a3e67a4SJesse Barnes  * @pipe: pipe to check
4040a3e67a4SJesse Barnes  *
4050a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
4060a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
4070a3e67a4SJesse Barnes  * before reading such registers if unsure.
4080a3e67a4SJesse Barnes  */
4090a3e67a4SJesse Barnes static int
4100a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
4110a3e67a4SJesse Barnes {
4120a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
413702e7a56SPaulo Zanoni 
414a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
415a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
416a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
417a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
41871f8ba6bSPaulo Zanoni 
419a01025afSDaniel Vetter 		return intel_crtc->active;
420a01025afSDaniel Vetter 	} else {
421a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
422a01025afSDaniel Vetter 	}
4230a3e67a4SJesse Barnes }
4240a3e67a4SJesse Barnes 
42542f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
42642f52ef8SKeith Packard  * we use as a pipe index
42742f52ef8SKeith Packard  */
428f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
4290a3e67a4SJesse Barnes {
4300a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4310a3e67a4SJesse Barnes 	unsigned long high_frame;
4320a3e67a4SJesse Barnes 	unsigned long low_frame;
4335eddb70bSChris Wilson 	u32 high1, high2, low;
4340a3e67a4SJesse Barnes 
4350a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
43644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4379db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
4380a3e67a4SJesse Barnes 		return 0;
4390a3e67a4SJesse Barnes 	}
4400a3e67a4SJesse Barnes 
4419db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
4429db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
4435eddb70bSChris Wilson 
4440a3e67a4SJesse Barnes 	/*
4450a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
4460a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
4470a3e67a4SJesse Barnes 	 * register.
4480a3e67a4SJesse Barnes 	 */
4490a3e67a4SJesse Barnes 	do {
4505eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4515eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
4525eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4530a3e67a4SJesse Barnes 	} while (high1 != high2);
4540a3e67a4SJesse Barnes 
4555eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
4565eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
4575eddb70bSChris Wilson 	return (high1 << 8) | low;
4580a3e67a4SJesse Barnes }
4590a3e67a4SJesse Barnes 
460f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
4619880b7a5SJesse Barnes {
4629880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4639db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
4649880b7a5SJesse Barnes 
4659880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
46644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4679db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4689880b7a5SJesse Barnes 		return 0;
4699880b7a5SJesse Barnes 	}
4709880b7a5SJesse Barnes 
4719880b7a5SJesse Barnes 	return I915_READ(reg);
4729880b7a5SJesse Barnes }
4739880b7a5SJesse Barnes 
474f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
4750af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
4760af7e4dfSMario Kleiner {
4770af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4780af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
4790af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
4800af7e4dfSMario Kleiner 	bool in_vbl = true;
4810af7e4dfSMario Kleiner 	int ret = 0;
482fe2b8f9dSPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
483fe2b8f9dSPaulo Zanoni 								      pipe);
4840af7e4dfSMario Kleiner 
4850af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
4860af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
4879db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4880af7e4dfSMario Kleiner 		return 0;
4890af7e4dfSMario Kleiner 	}
4900af7e4dfSMario Kleiner 
4910af7e4dfSMario Kleiner 	/* Get vtotal. */
492fe2b8f9dSPaulo Zanoni 	vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4930af7e4dfSMario Kleiner 
4940af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
4950af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
4960af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
4970af7e4dfSMario Kleiner 		 */
4980af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
4990af7e4dfSMario Kleiner 
5000af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
5010af7e4dfSMario Kleiner 		 * horizontal scanout position.
5020af7e4dfSMario Kleiner 		 */
5030af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
5040af7e4dfSMario Kleiner 		*hpos = 0;
5050af7e4dfSMario Kleiner 	} else {
5060af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
5070af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
5080af7e4dfSMario Kleiner 		 * scanout position.
5090af7e4dfSMario Kleiner 		 */
5100af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
5110af7e4dfSMario Kleiner 
512fe2b8f9dSPaulo Zanoni 		htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
5130af7e4dfSMario Kleiner 		*vpos = position / htotal;
5140af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
5150af7e4dfSMario Kleiner 	}
5160af7e4dfSMario Kleiner 
5170af7e4dfSMario Kleiner 	/* Query vblank area. */
518fe2b8f9dSPaulo Zanoni 	vbl = I915_READ(VBLANK(cpu_transcoder));
5190af7e4dfSMario Kleiner 
5200af7e4dfSMario Kleiner 	/* Test position against vblank region. */
5210af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
5220af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
5230af7e4dfSMario Kleiner 
5240af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
5250af7e4dfSMario Kleiner 		in_vbl = false;
5260af7e4dfSMario Kleiner 
5270af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
5280af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
5290af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
5300af7e4dfSMario Kleiner 
5310af7e4dfSMario Kleiner 	/* Readouts valid? */
5320af7e4dfSMario Kleiner 	if (vbl > 0)
5330af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
5340af7e4dfSMario Kleiner 
5350af7e4dfSMario Kleiner 	/* In vblank? */
5360af7e4dfSMario Kleiner 	if (in_vbl)
5370af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
5380af7e4dfSMario Kleiner 
5390af7e4dfSMario Kleiner 	return ret;
5400af7e4dfSMario Kleiner }
5410af7e4dfSMario Kleiner 
542f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
5430af7e4dfSMario Kleiner 			      int *max_error,
5440af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
5450af7e4dfSMario Kleiner 			      unsigned flags)
5460af7e4dfSMario Kleiner {
5474041b853SChris Wilson 	struct drm_crtc *crtc;
5480af7e4dfSMario Kleiner 
5497eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
5504041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5510af7e4dfSMario Kleiner 		return -EINVAL;
5520af7e4dfSMario Kleiner 	}
5530af7e4dfSMario Kleiner 
5540af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
5554041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
5564041b853SChris Wilson 	if (crtc == NULL) {
5574041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5584041b853SChris Wilson 		return -EINVAL;
5594041b853SChris Wilson 	}
5604041b853SChris Wilson 
5614041b853SChris Wilson 	if (!crtc->enabled) {
5624041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
5634041b853SChris Wilson 		return -EBUSY;
5644041b853SChris Wilson 	}
5650af7e4dfSMario Kleiner 
5660af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
5674041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
5684041b853SChris Wilson 						     vblank_time, flags,
5694041b853SChris Wilson 						     crtc);
5700af7e4dfSMario Kleiner }
5710af7e4dfSMario Kleiner 
572321a1b30SEgbert Eich static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
573321a1b30SEgbert Eich {
574321a1b30SEgbert Eich 	enum drm_connector_status old_status;
575321a1b30SEgbert Eich 
576321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
577321a1b30SEgbert Eich 	old_status = connector->status;
578321a1b30SEgbert Eich 
579321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
580321a1b30SEgbert Eich 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
581321a1b30SEgbert Eich 		      connector->base.id,
582321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
583321a1b30SEgbert Eich 		      old_status, connector->status);
584321a1b30SEgbert Eich 	return (old_status != connector->status);
585321a1b30SEgbert Eich }
586321a1b30SEgbert Eich 
5875ca58282SJesse Barnes /*
5885ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
5895ca58282SJesse Barnes  */
590ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
591ac4c16c5SEgbert Eich 
5925ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
5935ca58282SJesse Barnes {
5945ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5955ca58282SJesse Barnes 						    hotplug_work);
5965ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
597c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
598cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
599cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
600cd569aedSEgbert Eich 	struct drm_connector *connector;
601cd569aedSEgbert Eich 	unsigned long irqflags;
602cd569aedSEgbert Eich 	bool hpd_disabled = false;
603321a1b30SEgbert Eich 	bool changed = false;
604142e2398SEgbert Eich 	u32 hpd_event_bits;
6055ca58282SJesse Barnes 
60652d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
60752d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
60852d7ecedSDaniel Vetter 		return;
60952d7ecedSDaniel Vetter 
610a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
611e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
612e67189abSJesse Barnes 
613cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
614142e2398SEgbert Eich 
615142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
616142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
617cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
618cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
619cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
620cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
621cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
622cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
623cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
624cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
625cd569aedSEgbert Eich 				drm_get_connector_name(connector));
626cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
627cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
628cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
629cd569aedSEgbert Eich 			hpd_disabled = true;
630cd569aedSEgbert Eich 		}
631142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
632142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
633142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
634142e2398SEgbert Eich 		}
635cd569aedSEgbert Eich 	}
636cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
637cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
638cd569aedSEgbert Eich 	  * some connectors */
639ac4c16c5SEgbert Eich 	if (hpd_disabled) {
640cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
641ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
642ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
643ac4c16c5SEgbert Eich 	}
644cd569aedSEgbert Eich 
645cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
646cd569aedSEgbert Eich 
647321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
648321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
649321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
650321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
651cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
652cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
653321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
654321a1b30SEgbert Eich 				changed = true;
655321a1b30SEgbert Eich 		}
656321a1b30SEgbert Eich 	}
65740ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
65840ee3381SKeith Packard 
659321a1b30SEgbert Eich 	if (changed)
660321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
6615ca58282SJesse Barnes }
6625ca58282SJesse Barnes 
66373edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
664f97108d1SJesse Barnes {
665f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
666b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
6679270388eSDaniel Vetter 	u8 new_delay;
6689270388eSDaniel Vetter 	unsigned long flags;
6699270388eSDaniel Vetter 
6709270388eSDaniel Vetter 	spin_lock_irqsave(&mchdev_lock, flags);
671f97108d1SJesse Barnes 
67273edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
67373edd18fSDaniel Vetter 
67420e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
6759270388eSDaniel Vetter 
6767648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
677b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
678b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
679f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
680f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
681f97108d1SJesse Barnes 
682f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
683b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
68420e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
68520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
68620e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
68720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
688b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
68920e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
69020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
69120e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
69220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
693f97108d1SJesse Barnes 	}
694f97108d1SJesse Barnes 
6957648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
69620e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
697f97108d1SJesse Barnes 
6989270388eSDaniel Vetter 	spin_unlock_irqrestore(&mchdev_lock, flags);
6999270388eSDaniel Vetter 
700f97108d1SJesse Barnes 	return;
701f97108d1SJesse Barnes }
702f97108d1SJesse Barnes 
703549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
704549f7365SChris Wilson 			struct intel_ring_buffer *ring)
705549f7365SChris Wilson {
706549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
7079862e600SChris Wilson 
708475553deSChris Wilson 	if (ring->obj == NULL)
709475553deSChris Wilson 		return;
710475553deSChris Wilson 
711b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
7129862e600SChris Wilson 
713549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
7143e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
71599584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
716cecc21feSChris Wilson 			  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
7173e0dc6b0SBen Widawsky 	}
718549f7365SChris Wilson }
719549f7365SChris Wilson 
7204912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
7213b8d8d91SJesse Barnes {
7224912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
723c6a828d3SDaniel Vetter 						    rps.work);
7244912d041SBen Widawsky 	u32 pm_iir, pm_imr;
7257b9e0ae6SChris Wilson 	u8 new_delay;
7263b8d8d91SJesse Barnes 
727c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
728c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
729c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
7304912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
7314848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
7324848405cSBen Widawsky 	I915_WRITE(GEN6_PMIMR, pm_imr & ~GEN6_PM_RPS_EVENTS);
733c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
7344912d041SBen Widawsky 
7354848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
7363b8d8d91SJesse Barnes 		return;
7373b8d8d91SJesse Barnes 
7384fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
7397b9e0ae6SChris Wilson 
7407425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
741c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
7427425034aSVille Syrjälä 
7437425034aSVille Syrjälä 		/*
7447425034aSVille Syrjälä 		 * For better performance, jump directly
7457425034aSVille Syrjälä 		 * to RPe if we're below it.
7467425034aSVille Syrjälä 		 */
7477425034aSVille Syrjälä 		if (IS_VALLEYVIEW(dev_priv->dev) &&
7487425034aSVille Syrjälä 		    dev_priv->rps.cur_delay < dev_priv->rps.rpe_delay)
7497425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
7507425034aSVille Syrjälä 	} else
751c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
7523b8d8d91SJesse Barnes 
75379249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
75479249636SBen Widawsky 	 * interrupt
75579249636SBen Widawsky 	 */
756d8289c9eSVille Syrjälä 	if (new_delay >= dev_priv->rps.min_delay &&
757d8289c9eSVille Syrjälä 	    new_delay <= dev_priv->rps.max_delay) {
7580a073b84SJesse Barnes 		if (IS_VALLEYVIEW(dev_priv->dev))
7590a073b84SJesse Barnes 			valleyview_set_rps(dev_priv->dev, new_delay);
7600a073b84SJesse Barnes 		else
7614912d041SBen Widawsky 			gen6_set_rps(dev_priv->dev, new_delay);
76279249636SBen Widawsky 	}
7633b8d8d91SJesse Barnes 
76452ceb908SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev)) {
76552ceb908SJesse Barnes 		/*
76652ceb908SJesse Barnes 		 * On VLV, when we enter RC6 we may not be at the minimum
76752ceb908SJesse Barnes 		 * voltage level, so arm a timer to check.  It should only
76852ceb908SJesse Barnes 		 * fire when there's activity or once after we've entered
76952ceb908SJesse Barnes 		 * RC6, and then won't be re-armed until the next RPS interrupt.
77052ceb908SJesse Barnes 		 */
77152ceb908SJesse Barnes 		mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
77252ceb908SJesse Barnes 				 msecs_to_jiffies(100));
77352ceb908SJesse Barnes 	}
77452ceb908SJesse Barnes 
7754fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
7763b8d8d91SJesse Barnes }
7773b8d8d91SJesse Barnes 
778e3689190SBen Widawsky 
779e3689190SBen Widawsky /**
780e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
781e3689190SBen Widawsky  * occurred.
782e3689190SBen Widawsky  * @work: workqueue struct
783e3689190SBen Widawsky  *
784e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
785e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
786e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
787e3689190SBen Widawsky  */
788e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
789e3689190SBen Widawsky {
790e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
791a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
792e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
793e3689190SBen Widawsky 	char *parity_event[5];
794e3689190SBen Widawsky 	uint32_t misccpctl;
795e3689190SBen Widawsky 	unsigned long flags;
796e3689190SBen Widawsky 
797e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
798e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
799e3689190SBen Widawsky 	 * any time we access those registers.
800e3689190SBen Widawsky 	 */
801e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
802e3689190SBen Widawsky 
803e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
804e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
805e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
806e3689190SBen Widawsky 
807e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
808e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
809e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
810e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
811e3689190SBen Widawsky 
812e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
813e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
814e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
815e3689190SBen Widawsky 
816e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
817e3689190SBen Widawsky 
818e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
819cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
820e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
821e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
822e3689190SBen Widawsky 
823e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
824e3689190SBen Widawsky 
825e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
826e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
827e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
828e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
829e3689190SBen Widawsky 	parity_event[4] = NULL;
830e3689190SBen Widawsky 
831e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
832e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
833e3689190SBen Widawsky 
834e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
835e3689190SBen Widawsky 		  row, bank, subbank);
836e3689190SBen Widawsky 
837e3689190SBen Widawsky 	kfree(parity_event[3]);
838e3689190SBen Widawsky 	kfree(parity_event[2]);
839e3689190SBen Widawsky 	kfree(parity_event[1]);
840e3689190SBen Widawsky }
841e3689190SBen Widawsky 
842d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
843e3689190SBen Widawsky {
844e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
845e3689190SBen Widawsky 	unsigned long flags;
846e3689190SBen Widawsky 
847e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
848e3689190SBen Widawsky 		return;
849e3689190SBen Widawsky 
850e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
851cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
852e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
853e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
854e3689190SBen Widawsky 
855a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
856e3689190SBen Widawsky }
857e3689190SBen Widawsky 
858e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
859e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
860e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
861e7b4c6b1SDaniel Vetter {
862e7b4c6b1SDaniel Vetter 
863cc609d5dSBen Widawsky 	if (gt_iir &
864cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
865e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
866cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
867e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
868cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
869e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
870e7b4c6b1SDaniel Vetter 
871cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
872cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
873cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
874e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
875e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
876e7b4c6b1SDaniel Vetter 	}
877e3689190SBen Widawsky 
878cc609d5dSBen Widawsky 	if (gt_iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
879e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
880e7b4c6b1SDaniel Vetter }
881e7b4c6b1SDaniel Vetter 
882baf02a1fSBen Widawsky /* Legacy way of handling PM interrupts */
883fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
884fc6826d1SChris Wilson 				u32 pm_iir)
885fc6826d1SChris Wilson {
886fc6826d1SChris Wilson 	unsigned long flags;
887fc6826d1SChris Wilson 
888fc6826d1SChris Wilson 	/*
889fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
890fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
891fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
892c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
893fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
894fc6826d1SChris Wilson 	 *
895c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
896fc6826d1SChris Wilson 	 */
897fc6826d1SChris Wilson 
898c6a828d3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
899c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
900c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
901fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
902c6a828d3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
903fc6826d1SChris Wilson 
904c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
905fc6826d1SChris Wilson }
906fc6826d1SChris Wilson 
907b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
908b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
909b543fb04SEgbert Eich 
91010a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
911b543fb04SEgbert Eich 					 u32 hotplug_trigger,
912b543fb04SEgbert Eich 					 const u32 *hpd)
913b543fb04SEgbert Eich {
914b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
915b543fb04SEgbert Eich 	int i;
91610a504deSDaniel Vetter 	bool storm_detected = false;
917b543fb04SEgbert Eich 
91891d131d2SDaniel Vetter 	if (!hotplug_trigger)
91991d131d2SDaniel Vetter 		return;
92091d131d2SDaniel Vetter 
921b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
922b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
923821450c6SEgbert Eich 
924b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
925b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
926b543fb04SEgbert Eich 			continue;
927b543fb04SEgbert Eich 
928bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
929b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
930b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
931b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
932b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
933b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
934b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
935b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
936142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
937b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
93810a504deSDaniel Vetter 			storm_detected = true;
939b543fb04SEgbert Eich 		} else {
940b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
941b543fb04SEgbert Eich 		}
942b543fb04SEgbert Eich 	}
943b543fb04SEgbert Eich 
94410a504deSDaniel Vetter 	if (storm_detected)
94510a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
946b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
9475876fa0dSDaniel Vetter 
9485876fa0dSDaniel Vetter 	queue_work(dev_priv->wq,
9495876fa0dSDaniel Vetter 		   &dev_priv->hotplug_work);
950b543fb04SEgbert Eich }
951b543fb04SEgbert Eich 
952515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
953515ac2bbSDaniel Vetter {
95428c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
95528c70f16SDaniel Vetter 
95628c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
957515ac2bbSDaniel Vetter }
958515ac2bbSDaniel Vetter 
959ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
960ce99c256SDaniel Vetter {
9619ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
9629ee32feaSDaniel Vetter 
9639ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
964ce99c256SDaniel Vetter }
965ce99c256SDaniel Vetter 
966baf02a1fSBen Widawsky /* Unlike gen6_queue_rps_work() from which this function is originally derived,
967baf02a1fSBen Widawsky  * we must be able to deal with other PM interrupts. This is complicated because
968baf02a1fSBen Widawsky  * of the way in which we use the masks to defer the RPS work (which for
969baf02a1fSBen Widawsky  * posterity is necessary because of forcewake).
970baf02a1fSBen Widawsky  */
971baf02a1fSBen Widawsky static void hsw_pm_irq_handler(struct drm_i915_private *dev_priv,
972baf02a1fSBen Widawsky 			       u32 pm_iir)
973baf02a1fSBen Widawsky {
974baf02a1fSBen Widawsky 	unsigned long flags;
975baf02a1fSBen Widawsky 
976baf02a1fSBen Widawsky 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
9774848405cSBen Widawsky 	dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
978baf02a1fSBen Widawsky 	if (dev_priv->rps.pm_iir) {
979baf02a1fSBen Widawsky 		I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
980baf02a1fSBen Widawsky 		/* never want to mask useful interrupts. (also posting read) */
9814848405cSBen Widawsky 		WARN_ON(I915_READ_NOTRACE(GEN6_PMIMR) & ~GEN6_PM_RPS_EVENTS);
982baf02a1fSBen Widawsky 		/* TODO: if queue_work is slow, move it out of the spinlock */
983baf02a1fSBen Widawsky 		queue_work(dev_priv->wq, &dev_priv->rps.work);
984baf02a1fSBen Widawsky 	}
985baf02a1fSBen Widawsky 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
986baf02a1fSBen Widawsky 
98712638c57SBen Widawsky 	if (pm_iir & ~GEN6_PM_RPS_EVENTS) {
98812638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
98912638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
99012638c57SBen Widawsky 
99112638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
99212638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
99312638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
99412638c57SBen Widawsky 		}
99512638c57SBen Widawsky 	}
996baf02a1fSBen Widawsky }
997baf02a1fSBen Widawsky 
998ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
9997e231dbeSJesse Barnes {
10007e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
10017e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10027e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
10037e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
10047e231dbeSJesse Barnes 	unsigned long irqflags;
10057e231dbeSJesse Barnes 	int pipe;
10067e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
10077e231dbeSJesse Barnes 
10087e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
10097e231dbeSJesse Barnes 
10107e231dbeSJesse Barnes 	while (true) {
10117e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
10127e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
10137e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
10147e231dbeSJesse Barnes 
10157e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
10167e231dbeSJesse Barnes 			goto out;
10177e231dbeSJesse Barnes 
10187e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
10197e231dbeSJesse Barnes 
1020e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
10217e231dbeSJesse Barnes 
10227e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
10237e231dbeSJesse Barnes 		for_each_pipe(pipe) {
10247e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
10257e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
10267e231dbeSJesse Barnes 
10277e231dbeSJesse Barnes 			/*
10287e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
10297e231dbeSJesse Barnes 			 */
10307e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
10317e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
10327e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
10337e231dbeSJesse Barnes 							 pipe_name(pipe));
10347e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
10357e231dbeSJesse Barnes 			}
10367e231dbeSJesse Barnes 		}
10377e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
10387e231dbeSJesse Barnes 
103931acc7f5SJesse Barnes 		for_each_pipe(pipe) {
104031acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
104131acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
104231acc7f5SJesse Barnes 
104331acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
104431acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
104531acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
104631acc7f5SJesse Barnes 			}
104731acc7f5SJesse Barnes 		}
104831acc7f5SJesse Barnes 
10497e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
10507e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
10517e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1052b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
10537e231dbeSJesse Barnes 
10547e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
10557e231dbeSJesse Barnes 					 hotplug_status);
105691d131d2SDaniel Vetter 
105710a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
105891d131d2SDaniel Vetter 
10597e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
10607e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
10617e231dbeSJesse Barnes 		}
10627e231dbeSJesse Barnes 
1063515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1064515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
10657e231dbeSJesse Barnes 
10664848405cSBen Widawsky 		if (pm_iir & GEN6_PM_RPS_EVENTS)
1067fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
10687e231dbeSJesse Barnes 
10697e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
10707e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
10717e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
10727e231dbeSJesse Barnes 	}
10737e231dbeSJesse Barnes 
10747e231dbeSJesse Barnes out:
10757e231dbeSJesse Barnes 	return ret;
10767e231dbeSJesse Barnes }
10777e231dbeSJesse Barnes 
107823e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1079776ad806SJesse Barnes {
1080776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10819db4a9c7SJesse Barnes 	int pipe;
1082b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1083776ad806SJesse Barnes 
108410a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
108591d131d2SDaniel Vetter 
1086cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1087cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1088776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1089cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1090cfc33bf7SVille Syrjälä 				 port_name(port));
1091cfc33bf7SVille Syrjälä 	}
1092776ad806SJesse Barnes 
1093ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1094ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1095ce99c256SDaniel Vetter 
1096776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1097515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1098776ad806SJesse Barnes 
1099776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1100776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1101776ad806SJesse Barnes 
1102776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1103776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1104776ad806SJesse Barnes 
1105776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1106776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1107776ad806SJesse Barnes 
11089db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
11099db4a9c7SJesse Barnes 		for_each_pipe(pipe)
11109db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
11119db4a9c7SJesse Barnes 					 pipe_name(pipe),
11129db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1113776ad806SJesse Barnes 
1114776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1115776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1116776ad806SJesse Barnes 
1117776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1118776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1119776ad806SJesse Barnes 
1120776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
11218664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
11228664281bSPaulo Zanoni 							  false))
11238664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
11248664281bSPaulo Zanoni 
11258664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
11268664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
11278664281bSPaulo Zanoni 							  false))
11288664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
11298664281bSPaulo Zanoni }
11308664281bSPaulo Zanoni 
11318664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
11328664281bSPaulo Zanoni {
11338664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
11348664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
11358664281bSPaulo Zanoni 
1136de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1137de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1138de032bf4SPaulo Zanoni 
11398664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_A)
11408664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
11418664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
11428664281bSPaulo Zanoni 
11438664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_B)
11448664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
11458664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
11468664281bSPaulo Zanoni 
11478664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_C)
11488664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
11498664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
11508664281bSPaulo Zanoni 
11518664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
11528664281bSPaulo Zanoni }
11538664281bSPaulo Zanoni 
11548664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
11558664281bSPaulo Zanoni {
11568664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
11578664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
11588664281bSPaulo Zanoni 
1159de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1160de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1161de032bf4SPaulo Zanoni 
11628664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
11638664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
11648664281bSPaulo Zanoni 							  false))
11658664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
11668664281bSPaulo Zanoni 
11678664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
11688664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
11698664281bSPaulo Zanoni 							  false))
11708664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
11718664281bSPaulo Zanoni 
11728664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
11738664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
11748664281bSPaulo Zanoni 							  false))
11758664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
11768664281bSPaulo Zanoni 
11778664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1178776ad806SJesse Barnes }
1179776ad806SJesse Barnes 
118023e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
118123e81d69SAdam Jackson {
118223e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
118323e81d69SAdam Jackson 	int pipe;
1184b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
118523e81d69SAdam Jackson 
118610a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
118791d131d2SDaniel Vetter 
1188cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1189cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
119023e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1191cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1192cfc33bf7SVille Syrjälä 				 port_name(port));
1193cfc33bf7SVille Syrjälä 	}
119423e81d69SAdam Jackson 
119523e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1196ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
119723e81d69SAdam Jackson 
119823e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1199515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
120023e81d69SAdam Jackson 
120123e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
120223e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
120323e81d69SAdam Jackson 
120423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
120523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
120623e81d69SAdam Jackson 
120723e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
120823e81d69SAdam Jackson 		for_each_pipe(pipe)
120923e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
121023e81d69SAdam Jackson 					 pipe_name(pipe),
121123e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
12128664281bSPaulo Zanoni 
12138664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
12148664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
121523e81d69SAdam Jackson }
121623e81d69SAdam Jackson 
1217ff1f525eSDaniel Vetter static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
1218b1f14ad0SJesse Barnes {
1219b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1220b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1221ab5c608bSBen Widawsky 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
12220e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
12230e43406bSChris Wilson 	int i;
1224b1f14ad0SJesse Barnes 
1225b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1226b1f14ad0SJesse Barnes 
12278664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
12288664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
12298664281bSPaulo Zanoni 	if (IS_HASWELL(dev) &&
12308664281bSPaulo Zanoni 	    (I915_READ_NOTRACE(FPGA_DBG) & FPGA_DBG_RM_NOCLAIM)) {
12318664281bSPaulo Zanoni 		DRM_ERROR("Unclaimed register before interrupt\n");
12328664281bSPaulo Zanoni 		I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
12338664281bSPaulo Zanoni 	}
12348664281bSPaulo Zanoni 
1235b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1236b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1237b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
12380e43406bSChris Wilson 
123944498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
124044498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
124144498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
124244498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
124344498aeaSPaulo Zanoni 	 * due to its back queue). */
1244ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
124544498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
124644498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
124744498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1248ab5c608bSBen Widawsky 	}
124944498aeaSPaulo Zanoni 
12508664281bSPaulo Zanoni 	/* On Haswell, also mask ERR_INT because we don't want to risk
12518664281bSPaulo Zanoni 	 * generating "unclaimed register" interrupts from inside the interrupt
12528664281bSPaulo Zanoni 	 * handler. */
12534bc9d430SDaniel Vetter 	if (IS_HASWELL(dev)) {
12544bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
12558664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
12564bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
12574bc9d430SDaniel Vetter 	}
12588664281bSPaulo Zanoni 
12590e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
12600e43406bSChris Wilson 	if (gt_iir) {
12610e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
12620e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
12630e43406bSChris Wilson 		ret = IRQ_HANDLED;
12640e43406bSChris Wilson 	}
1265b1f14ad0SJesse Barnes 
1266b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
12670e43406bSChris Wilson 	if (de_iir) {
12688664281bSPaulo Zanoni 		if (de_iir & DE_ERR_INT_IVB)
12698664281bSPaulo Zanoni 			ivb_err_int_handler(dev);
12708664281bSPaulo Zanoni 
1271ce99c256SDaniel Vetter 		if (de_iir & DE_AUX_CHANNEL_A_IVB)
1272ce99c256SDaniel Vetter 			dp_aux_irq_handler(dev);
1273ce99c256SDaniel Vetter 
1274b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
127581a07809SJani Nikula 			intel_opregion_asle_intr(dev);
1276b1f14ad0SJesse Barnes 
12770e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
127874d44445SDaniel Vetter 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
127974d44445SDaniel Vetter 				drm_handle_vblank(dev, i);
12800e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
12810e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
12820e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
1283b1f14ad0SJesse Barnes 			}
1284b1f14ad0SJesse Barnes 		}
1285b1f14ad0SJesse Barnes 
1286b1f14ad0SJesse Barnes 		/* check event from PCH */
1287ab5c608bSBen Widawsky 		if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
12880e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
12890e43406bSChris Wilson 
129023e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
12910e43406bSChris Wilson 
12920e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
12930e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
1294b1f14ad0SJesse Barnes 		}
1295b1f14ad0SJesse Barnes 
12960e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
12970e43406bSChris Wilson 		ret = IRQ_HANDLED;
12980e43406bSChris Wilson 	}
12990e43406bSChris Wilson 
13000e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
13010e43406bSChris Wilson 	if (pm_iir) {
1302baf02a1fSBen Widawsky 		if (IS_HASWELL(dev))
1303baf02a1fSBen Widawsky 			hsw_pm_irq_handler(dev_priv, pm_iir);
13044848405cSBen Widawsky 		else if (pm_iir & GEN6_PM_RPS_EVENTS)
1305fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
1306b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
13070e43406bSChris Wilson 		ret = IRQ_HANDLED;
13080e43406bSChris Wilson 	}
1309b1f14ad0SJesse Barnes 
13104bc9d430SDaniel Vetter 	if (IS_HASWELL(dev)) {
13114bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
13124bc9d430SDaniel Vetter 		if (ivb_can_enable_err_int(dev))
13138664281bSPaulo Zanoni 			ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
13144bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
13154bc9d430SDaniel Vetter 	}
13168664281bSPaulo Zanoni 
1317b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1318b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1319ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
132044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
132144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1322ab5c608bSBen Widawsky 	}
1323b1f14ad0SJesse Barnes 
1324b1f14ad0SJesse Barnes 	return ret;
1325b1f14ad0SJesse Barnes }
1326b1f14ad0SJesse Barnes 
1327e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
1328e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1329e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1330e7b4c6b1SDaniel Vetter {
1331cc609d5dSBen Widawsky 	if (gt_iir &
1332cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1333e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1334cc609d5dSBen Widawsky 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1335e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1336e7b4c6b1SDaniel Vetter }
1337e7b4c6b1SDaniel Vetter 
1338ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1339036a4a7dSZhenyu Wang {
13404697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1341036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1342036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
134344498aeaSPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
1344881f47b6SXiang, Haihao 
13454697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
13464697995bSJesse Barnes 
13472d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
13482d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
13492d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
13503143a2bfSChris Wilson 	POSTING_READ(DEIER);
13512d109a84SZou, Nanhai 
135244498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
135344498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
135444498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
135544498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
135644498aeaSPaulo Zanoni 	 * due to its back queue). */
135744498aeaSPaulo Zanoni 	sde_ier = I915_READ(SDEIER);
135844498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, 0);
135944498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
136044498aeaSPaulo Zanoni 
1361036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
1362036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
13633b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
1364036a4a7dSZhenyu Wang 
1365acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
1366c7c85101SZou Nan hai 		goto done;
1367036a4a7dSZhenyu Wang 
1368036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
1369036a4a7dSZhenyu Wang 
1370e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
1371e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
1372e7b4c6b1SDaniel Vetter 	else
1373e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1374036a4a7dSZhenyu Wang 
1375ce99c256SDaniel Vetter 	if (de_iir & DE_AUX_CHANNEL_A)
1376ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1377ce99c256SDaniel Vetter 
137801c66889SZhao Yakui 	if (de_iir & DE_GSE)
137981a07809SJani Nikula 		intel_opregion_asle_intr(dev);
138001c66889SZhao Yakui 
138174d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
138274d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
138374d44445SDaniel Vetter 
138474d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
138574d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
138674d44445SDaniel Vetter 
1387de032bf4SPaulo Zanoni 	if (de_iir & DE_POISON)
1388de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1389de032bf4SPaulo Zanoni 
13908664281bSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
13918664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
13928664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
13938664281bSPaulo Zanoni 
13948664281bSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
13958664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
13968664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
13978664281bSPaulo Zanoni 
1398f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1399013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
14002bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
1401013d5aa2SJesse Barnes 	}
1402013d5aa2SJesse Barnes 
1403f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1404f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
14052bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
1406013d5aa2SJesse Barnes 	}
1407c062df61SLi Peng 
1408c650156aSZhenyu Wang 	/* check event from PCH */
1409776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
1410acd15b6cSDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
1411acd15b6cSDaniel Vetter 
141223e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
141323e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
141423e81d69SAdam Jackson 		else
141523e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
1416acd15b6cSDaniel Vetter 
1417acd15b6cSDaniel Vetter 		/* should clear PCH hotplug event before clear CPU irq */
1418acd15b6cSDaniel Vetter 		I915_WRITE(SDEIIR, pch_iir);
1419776ad806SJesse Barnes 	}
1420c650156aSZhenyu Wang 
142173edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
142273edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
1423f97108d1SJesse Barnes 
14244848405cSBen Widawsky 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_RPS_EVENTS)
1425fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
14263b8d8d91SJesse Barnes 
1427c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
1428c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
14294912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
1430036a4a7dSZhenyu Wang 
1431c7c85101SZou Nan hai done:
14322d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
14333143a2bfSChris Wilson 	POSTING_READ(DEIER);
143444498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, sde_ier);
143544498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
14362d109a84SZou, Nanhai 
1437036a4a7dSZhenyu Wang 	return ret;
1438036a4a7dSZhenyu Wang }
1439036a4a7dSZhenyu Wang 
14408a905236SJesse Barnes /**
14418a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
14428a905236SJesse Barnes  * @work: work struct
14438a905236SJesse Barnes  *
14448a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
14458a905236SJesse Barnes  * was detected.
14468a905236SJesse Barnes  */
14478a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
14488a905236SJesse Barnes {
14491f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
14501f83fee0SDaniel Vetter 						    work);
14511f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
14521f83fee0SDaniel Vetter 						    gpu_error);
14538a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1454f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
1455f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
1456f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
1457f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
1458f69061beSDaniel Vetter 	int i, ret;
14598a905236SJesse Barnes 
1460f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
14618a905236SJesse Barnes 
14627db0ba24SDaniel Vetter 	/*
14637db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
14647db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
14657db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
14667db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
14677db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
14687db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
14697db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
14707db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
14717db0ba24SDaniel Vetter 	 */
14727db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
147344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
14747db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
14757db0ba24SDaniel Vetter 				   reset_event);
14761f83fee0SDaniel Vetter 
1477f69061beSDaniel Vetter 		ret = i915_reset(dev);
1478f69061beSDaniel Vetter 
1479f69061beSDaniel Vetter 		if (ret == 0) {
1480f69061beSDaniel Vetter 			/*
1481f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1482f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1483f69061beSDaniel Vetter 			 * complete.
1484f69061beSDaniel Vetter 			 *
1485f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1486f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1487f69061beSDaniel Vetter 			 * updates before
1488f69061beSDaniel Vetter 			 * the counter increment.
1489f69061beSDaniel Vetter 			 */
1490f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1491f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1492f69061beSDaniel Vetter 
1493f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1494f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
14951f83fee0SDaniel Vetter 		} else {
14961f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1497f316a42cSBen Gamari 		}
14981f83fee0SDaniel Vetter 
1499f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
1500f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
1501f69061beSDaniel Vetter 
150296a02917SVille Syrjälä 		intel_display_handle_reset(dev);
150396a02917SVille Syrjälä 
15041f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
1505f316a42cSBen Gamari 	}
15068a905236SJesse Barnes }
15078a905236SJesse Barnes 
150885f9e50dSDaniel Vetter /* NB: please notice the memset */
150985f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
151085f9e50dSDaniel Vetter 				    uint32_t *instdone)
151185f9e50dSDaniel Vetter {
151285f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
151385f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
151485f9e50dSDaniel Vetter 
151585f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
151685f9e50dSDaniel Vetter 	case 2:
151785f9e50dSDaniel Vetter 	case 3:
151885f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
151985f9e50dSDaniel Vetter 		break;
152085f9e50dSDaniel Vetter 	case 4:
152185f9e50dSDaniel Vetter 	case 5:
152285f9e50dSDaniel Vetter 	case 6:
152385f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
152485f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
152585f9e50dSDaniel Vetter 		break;
152685f9e50dSDaniel Vetter 	default:
152785f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
152885f9e50dSDaniel Vetter 	case 7:
152985f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
153085f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
153185f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
153285f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
153385f9e50dSDaniel Vetter 		break;
153485f9e50dSDaniel Vetter 	}
153585f9e50dSDaniel Vetter }
153685f9e50dSDaniel Vetter 
15373bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
15389df30794SChris Wilson static struct drm_i915_error_object *
1539d0d045e8SBen Widawsky i915_error_object_create_sized(struct drm_i915_private *dev_priv,
1540d0d045e8SBen Widawsky 			       struct drm_i915_gem_object *src,
1541d0d045e8SBen Widawsky 			       const int num_pages)
15429df30794SChris Wilson {
15439df30794SChris Wilson 	struct drm_i915_error_object *dst;
1544d0d045e8SBen Widawsky 	int i;
1545e56660ddSChris Wilson 	u32 reloc_offset;
15469df30794SChris Wilson 
154705394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
15489df30794SChris Wilson 		return NULL;
15499df30794SChris Wilson 
1550d0d045e8SBen Widawsky 	dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
15519df30794SChris Wilson 	if (dst == NULL)
15529df30794SChris Wilson 		return NULL;
15539df30794SChris Wilson 
1554f343c5f6SBen Widawsky 	reloc_offset = dst->gtt_offset = i915_gem_obj_ggtt_offset(src);
1555d0d045e8SBen Widawsky 	for (i = 0; i < num_pages; i++) {
1556788885aeSAndrew Morton 		unsigned long flags;
1557e56660ddSChris Wilson 		void *d;
1558788885aeSAndrew Morton 
1559e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
15609df30794SChris Wilson 		if (d == NULL)
15619df30794SChris Wilson 			goto unwind;
1562e56660ddSChris Wilson 
1563788885aeSAndrew Morton 		local_irq_save(flags);
15645d4545aeSBen Widawsky 		if (reloc_offset < dev_priv->gtt.mappable_end &&
156574898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
1566172975aaSChris Wilson 			void __iomem *s;
1567172975aaSChris Wilson 
1568172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
1569172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
1570172975aaSChris Wilson 			 * captures what the GPU read.
1571172975aaSChris Wilson 			 */
1572172975aaSChris Wilson 
15735d4545aeSBen Widawsky 			s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
15743e4d3af5SPeter Zijlstra 						     reloc_offset);
1575e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
15763e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
1577960e3564SChris Wilson 		} else if (src->stolen) {
1578960e3564SChris Wilson 			unsigned long offset;
1579960e3564SChris Wilson 
1580960e3564SChris Wilson 			offset = dev_priv->mm.stolen_base;
1581960e3564SChris Wilson 			offset += src->stolen->start;
1582960e3564SChris Wilson 			offset += i << PAGE_SHIFT;
1583960e3564SChris Wilson 
15841a240d4dSDaniel Vetter 			memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
1585172975aaSChris Wilson 		} else {
15869da3da66SChris Wilson 			struct page *page;
1587172975aaSChris Wilson 			void *s;
1588172975aaSChris Wilson 
15899da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
1590172975aaSChris Wilson 
15919da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
15929da3da66SChris Wilson 
15939da3da66SChris Wilson 			s = kmap_atomic(page);
1594172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
1595172975aaSChris Wilson 			kunmap_atomic(s);
1596172975aaSChris Wilson 
15979da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
1598172975aaSChris Wilson 		}
1599788885aeSAndrew Morton 		local_irq_restore(flags);
1600e56660ddSChris Wilson 
16019da3da66SChris Wilson 		dst->pages[i] = d;
1602e56660ddSChris Wilson 
1603e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
16049df30794SChris Wilson 	}
1605d0d045e8SBen Widawsky 	dst->page_count = num_pages;
16069df30794SChris Wilson 
16079df30794SChris Wilson 	return dst;
16089df30794SChris Wilson 
16099df30794SChris Wilson unwind:
16109da3da66SChris Wilson 	while (i--)
16119da3da66SChris Wilson 		kfree(dst->pages[i]);
16129df30794SChris Wilson 	kfree(dst);
16139df30794SChris Wilson 	return NULL;
16149df30794SChris Wilson }
1615d0d045e8SBen Widawsky #define i915_error_object_create(dev_priv, src) \
1616d0d045e8SBen Widawsky 	i915_error_object_create_sized((dev_priv), (src), \
1617d0d045e8SBen Widawsky 				       (src)->base.size>>PAGE_SHIFT)
16189df30794SChris Wilson 
16199df30794SChris Wilson static void
16209df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
16219df30794SChris Wilson {
16229df30794SChris Wilson 	int page;
16239df30794SChris Wilson 
16249df30794SChris Wilson 	if (obj == NULL)
16259df30794SChris Wilson 		return;
16269df30794SChris Wilson 
16279df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
16289df30794SChris Wilson 		kfree(obj->pages[page]);
16299df30794SChris Wilson 
16309df30794SChris Wilson 	kfree(obj);
16319df30794SChris Wilson }
16329df30794SChris Wilson 
1633742cbee8SDaniel Vetter void
1634742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
16359df30794SChris Wilson {
1636742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
1637742cbee8SDaniel Vetter 							  typeof(*error), ref);
1638e2f973d5SChris Wilson 	int i;
1639e2f973d5SChris Wilson 
164052d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
164152d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
164252d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
16437ed73da0SBen Widawsky 		i915_error_object_free(error->ring[i].ctx);
164452d39a21SChris Wilson 		kfree(error->ring[i].requests);
164552d39a21SChris Wilson 	}
1646e2f973d5SChris Wilson 
16479df30794SChris Wilson 	kfree(error->active_bo);
16486ef3d427SChris Wilson 	kfree(error->overlay);
16497ed73da0SBen Widawsky 	kfree(error->display);
16509df30794SChris Wilson 	kfree(error);
16519df30794SChris Wilson }
16521b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
16531b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
1654c724e8a9SChris Wilson {
1655c724e8a9SChris Wilson 	err->size = obj->base.size;
1656c724e8a9SChris Wilson 	err->name = obj->base.name;
16570201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
16580201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1659f343c5f6SBen Widawsky 	err->gtt_offset = i915_gem_obj_ggtt_offset(obj);
1660c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1661c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1662c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1663c724e8a9SChris Wilson 	err->pinned = 0;
1664c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1665c724e8a9SChris Wilson 		err->pinned = 1;
1666c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1667c724e8a9SChris Wilson 		err->pinned = -1;
1668c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1669c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1670c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
167196154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
167293dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
16731b50247aSChris Wilson }
1674c724e8a9SChris Wilson 
16751b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
16761b50247aSChris Wilson 			     int count, struct list_head *head)
16771b50247aSChris Wilson {
16781b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
16791b50247aSChris Wilson 	int i = 0;
16801b50247aSChris Wilson 
16811b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
16821b50247aSChris Wilson 		capture_bo(err++, obj);
1683c724e8a9SChris Wilson 		if (++i == count)
1684c724e8a9SChris Wilson 			break;
16851b50247aSChris Wilson 	}
1686c724e8a9SChris Wilson 
16871b50247aSChris Wilson 	return i;
16881b50247aSChris Wilson }
16891b50247aSChris Wilson 
16901b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
16911b50247aSChris Wilson 			     int count, struct list_head *head)
16921b50247aSChris Wilson {
16931b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
16941b50247aSChris Wilson 	int i = 0;
16951b50247aSChris Wilson 
169635c20a60SBen Widawsky 	list_for_each_entry(obj, head, global_list) {
16971b50247aSChris Wilson 		if (obj->pin_count == 0)
16981b50247aSChris Wilson 			continue;
16991b50247aSChris Wilson 
17001b50247aSChris Wilson 		capture_bo(err++, obj);
17011b50247aSChris Wilson 		if (++i == count)
17021b50247aSChris Wilson 			break;
1703c724e8a9SChris Wilson 	}
1704c724e8a9SChris Wilson 
1705c724e8a9SChris Wilson 	return i;
1706c724e8a9SChris Wilson }
1707c724e8a9SChris Wilson 
1708748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1709748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1710748ebc60SChris Wilson {
1711748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1712748ebc60SChris Wilson 	int i;
1713748ebc60SChris Wilson 
1714748ebc60SChris Wilson 	/* Fences */
1715748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1716775d17b6SDaniel Vetter 	case 7:
1717748ebc60SChris Wilson 	case 6:
171842b5aeabSVille Syrjälä 		for (i = 0; i < dev_priv->num_fence_regs; i++)
1719748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1720748ebc60SChris Wilson 		break;
1721748ebc60SChris Wilson 	case 5:
1722748ebc60SChris Wilson 	case 4:
1723748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1724748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1725748ebc60SChris Wilson 		break;
1726748ebc60SChris Wilson 	case 3:
1727748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1728748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1729748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1730748ebc60SChris Wilson 	case 2:
1731748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1732748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1733748ebc60SChris Wilson 		break;
1734748ebc60SChris Wilson 
17357dbf9d6eSBen Widawsky 	default:
17367dbf9d6eSBen Widawsky 		BUG();
1737748ebc60SChris Wilson 	}
1738748ebc60SChris Wilson }
1739748ebc60SChris Wilson 
1740bcfb2e28SChris Wilson static struct drm_i915_error_object *
1741bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1742bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1743bcfb2e28SChris Wilson {
1744bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1745bcfb2e28SChris Wilson 	u32 seqno;
1746bcfb2e28SChris Wilson 
1747bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1748bcfb2e28SChris Wilson 		return NULL;
1749bcfb2e28SChris Wilson 
1750b45305fcSDaniel Vetter 	if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1751b45305fcSDaniel Vetter 		u32 acthd = I915_READ(ACTHD);
1752b45305fcSDaniel Vetter 
1753b45305fcSDaniel Vetter 		if (WARN_ON(ring->id != RCS))
1754b45305fcSDaniel Vetter 			return NULL;
1755b45305fcSDaniel Vetter 
1756b45305fcSDaniel Vetter 		obj = ring->private;
1757f343c5f6SBen Widawsky 		if (acthd >= i915_gem_obj_ggtt_offset(obj) &&
1758f343c5f6SBen Widawsky 		    acthd < i915_gem_obj_ggtt_offset(obj) + obj->base.size)
1759b45305fcSDaniel Vetter 			return i915_error_object_create(dev_priv, obj);
1760b45305fcSDaniel Vetter 	}
1761b45305fcSDaniel Vetter 
1762b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1763bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1764bcfb2e28SChris Wilson 		if (obj->ring != ring)
1765bcfb2e28SChris Wilson 			continue;
1766bcfb2e28SChris Wilson 
17670201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1768bcfb2e28SChris Wilson 			continue;
1769bcfb2e28SChris Wilson 
1770bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1771bcfb2e28SChris Wilson 			continue;
1772bcfb2e28SChris Wilson 
1773bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1774bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1775bcfb2e28SChris Wilson 		 */
1776bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1777bcfb2e28SChris Wilson 	}
1778bcfb2e28SChris Wilson 
1779bcfb2e28SChris Wilson 	return NULL;
1780bcfb2e28SChris Wilson }
1781bcfb2e28SChris Wilson 
1782d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1783d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1784d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1785d27b1e0eSDaniel Vetter {
1786d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1787d27b1e0eSDaniel Vetter 
178833f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
178912f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
179033f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
17917e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
17927e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
17937e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
17947e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
1795df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1796df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
179733f3f518SDaniel Vetter 	}
1798c1cd90edSDaniel Vetter 
1799d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
18009d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1801d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1802d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1803d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1804c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1805050ee91fSBen Widawsky 		if (ring->id == RCS)
1806d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1807d27b1e0eSDaniel Vetter 	} else {
18089d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1809d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1810d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1811d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1812d27b1e0eSDaniel Vetter 	}
1813d27b1e0eSDaniel Vetter 
18149574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1815c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1816b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1817d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1818c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1819c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
18200f3b6849SChris Wilson 	error->ctl[ring->id] = I915_READ_CTL(ring);
18217e3b8737SDaniel Vetter 
18227e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
18237e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1824d27b1e0eSDaniel Vetter }
1825d27b1e0eSDaniel Vetter 
18268c123e54SBen Widawsky 
18278c123e54SBen Widawsky static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
18288c123e54SBen Widawsky 					   struct drm_i915_error_state *error,
18298c123e54SBen Widawsky 					   struct drm_i915_error_ring *ering)
18308c123e54SBen Widawsky {
18318c123e54SBen Widawsky 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
18328c123e54SBen Widawsky 	struct drm_i915_gem_object *obj;
18338c123e54SBen Widawsky 
18348c123e54SBen Widawsky 	/* Currently render ring is the only HW context user */
18358c123e54SBen Widawsky 	if (ring->id != RCS || !error->ccid)
18368c123e54SBen Widawsky 		return;
18378c123e54SBen Widawsky 
183835c20a60SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
1839f343c5f6SBen Widawsky 		if ((error->ccid & PAGE_MASK) == i915_gem_obj_ggtt_offset(obj)) {
18408c123e54SBen Widawsky 			ering->ctx = i915_error_object_create_sized(dev_priv,
18418c123e54SBen Widawsky 								    obj, 1);
18423ef8fb5aSDamien Lespiau 			break;
18438c123e54SBen Widawsky 		}
18448c123e54SBen Widawsky 	}
18458c123e54SBen Widawsky }
18468c123e54SBen Widawsky 
184752d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
184852d39a21SChris Wilson 				  struct drm_i915_error_state *error)
184952d39a21SChris Wilson {
185052d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1851b4519513SChris Wilson 	struct intel_ring_buffer *ring;
185252d39a21SChris Wilson 	struct drm_i915_gem_request *request;
185352d39a21SChris Wilson 	int i, count;
185452d39a21SChris Wilson 
1855b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
185652d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
185752d39a21SChris Wilson 
185852d39a21SChris Wilson 		error->ring[i].batchbuffer =
185952d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
186052d39a21SChris Wilson 
186152d39a21SChris Wilson 		error->ring[i].ringbuffer =
186252d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
186352d39a21SChris Wilson 
18648c123e54SBen Widawsky 
18658c123e54SBen Widawsky 		i915_gem_record_active_context(ring, error, &error->ring[i]);
18668c123e54SBen Widawsky 
186752d39a21SChris Wilson 		count = 0;
186852d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
186952d39a21SChris Wilson 			count++;
187052d39a21SChris Wilson 
187152d39a21SChris Wilson 		error->ring[i].num_requests = count;
187252d39a21SChris Wilson 		error->ring[i].requests =
187352d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
187452d39a21SChris Wilson 				GFP_ATOMIC);
187552d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
187652d39a21SChris Wilson 			error->ring[i].num_requests = 0;
187752d39a21SChris Wilson 			continue;
187852d39a21SChris Wilson 		}
187952d39a21SChris Wilson 
188052d39a21SChris Wilson 		count = 0;
188152d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
188252d39a21SChris Wilson 			struct drm_i915_error_request *erq;
188352d39a21SChris Wilson 
188452d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
188552d39a21SChris Wilson 			erq->seqno = request->seqno;
188652d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1887ee4f42b1SChris Wilson 			erq->tail = request->tail;
188852d39a21SChris Wilson 		}
188952d39a21SChris Wilson 	}
189052d39a21SChris Wilson }
189152d39a21SChris Wilson 
189226b7c224SBen Widawsky static void i915_gem_capture_buffers(struct drm_i915_private *dev_priv,
189326b7c224SBen Widawsky 				     struct drm_i915_error_state *error)
189426b7c224SBen Widawsky {
189526b7c224SBen Widawsky 	struct drm_i915_gem_object *obj;
189626b7c224SBen Widawsky 	int i;
189726b7c224SBen Widawsky 
189826b7c224SBen Widawsky 	i = 0;
189926b7c224SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
190026b7c224SBen Widawsky 		i++;
190126b7c224SBen Widawsky 	error->active_bo_count = i;
190226b7c224SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
190326b7c224SBen Widawsky 		if (obj->pin_count)
190426b7c224SBen Widawsky 			i++;
190526b7c224SBen Widawsky 	error->pinned_bo_count = i - error->active_bo_count;
190626b7c224SBen Widawsky 
190726b7c224SBen Widawsky 	if (i) {
190826b7c224SBen Widawsky 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
190926b7c224SBen Widawsky 					   GFP_ATOMIC);
191026b7c224SBen Widawsky 		if (error->active_bo)
191126b7c224SBen Widawsky 			error->pinned_bo =
191226b7c224SBen Widawsky 				error->active_bo + error->active_bo_count;
191326b7c224SBen Widawsky 	}
191426b7c224SBen Widawsky 
191526b7c224SBen Widawsky 	if (error->active_bo)
191626b7c224SBen Widawsky 		error->active_bo_count =
191726b7c224SBen Widawsky 			capture_active_bo(error->active_bo,
191826b7c224SBen Widawsky 					  error->active_bo_count,
191926b7c224SBen Widawsky 					  &dev_priv->mm.active_list);
192026b7c224SBen Widawsky 
192126b7c224SBen Widawsky 	if (error->pinned_bo)
192226b7c224SBen Widawsky 		error->pinned_bo_count =
192326b7c224SBen Widawsky 			capture_pinned_bo(error->pinned_bo,
192426b7c224SBen Widawsky 					  error->pinned_bo_count,
192526b7c224SBen Widawsky 					  &dev_priv->mm.bound_list);
192626b7c224SBen Widawsky }
192726b7c224SBen Widawsky 
19288a905236SJesse Barnes /**
19298a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
19308a905236SJesse Barnes  * @dev: drm device
19318a905236SJesse Barnes  *
19328a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
19338a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
19348a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
19358a905236SJesse Barnes  * to pick up.
19368a905236SJesse Barnes  */
193763eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
193863eeaf38SJesse Barnes {
193963eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
194063eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
194163eeaf38SJesse Barnes 	unsigned long flags;
194226b7c224SBen Widawsky 	int pipe;
194363eeaf38SJesse Barnes 
194499584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
194599584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
194699584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19479df30794SChris Wilson 	if (error)
19489df30794SChris Wilson 		return;
194963eeaf38SJesse Barnes 
19509db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
195133f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
195263eeaf38SJesse Barnes 	if (!error) {
19539df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
19549df30794SChris Wilson 		return;
195563eeaf38SJesse Barnes 	}
195663eeaf38SJesse Barnes 
19572f86f191SBen Widawsky 	DRM_INFO("capturing error event; look for more information in "
1958ef86ddceSMika Kuoppala 		 "/sys/class/drm/card%d/error\n", dev->primary->index);
19592fa772f3SChris Wilson 
1960742cbee8SDaniel Vetter 	kref_init(&error->ref);
196163eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
196263eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1963211816ecSBen Widawsky 	if (HAS_HW_CONTEXTS(dev))
1964b9a3906bSBen Widawsky 		error->ccid = I915_READ(CCID);
1965be998e2eSBen Widawsky 
1966be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1967be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1968be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1969be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1970be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1971be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1972be998e2eSBen Widawsky 	else
1973be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1974be998e2eSBen Widawsky 
19750f3b6849SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6)
19760f3b6849SChris Wilson 		error->derrmr = I915_READ(DERRMR);
19770f3b6849SChris Wilson 
19780f3b6849SChris Wilson 	if (IS_VALLEYVIEW(dev))
19790f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_VLV);
19800f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 7)
19810f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_MT);
19820f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen == 6)
19830f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE);
19840f3b6849SChris Wilson 
19854f3308b9SPaulo Zanoni 	if (!HAS_PCH_SPLIT(dev))
19869db4a9c7SJesse Barnes 		for_each_pipe(pipe)
19879db4a9c7SJesse Barnes 			error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1988d27b1e0eSDaniel Vetter 
198933f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1990f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
199133f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
199233f3f518SDaniel Vetter 	}
1993add354ddSChris Wilson 
199471e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
199571e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
199671e172e8SBen Widawsky 
1997050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1998050ee91fSBen Widawsky 
199926b7c224SBen Widawsky 	i915_gem_capture_buffers(dev_priv, error);
2000748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
200152d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
20029df30794SChris Wilson 
20038a905236SJesse Barnes 	do_gettimeofday(&error->time);
20048a905236SJesse Barnes 
20056ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
2006c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
20076ef3d427SChris Wilson 
200899584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
200999584db3SDaniel Vetter 	if (dev_priv->gpu_error.first_error == NULL) {
201099584db3SDaniel Vetter 		dev_priv->gpu_error.first_error = error;
20119df30794SChris Wilson 		error = NULL;
20129df30794SChris Wilson 	}
201399584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
20149df30794SChris Wilson 
20159df30794SChris Wilson 	if (error)
2016742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
20179df30794SChris Wilson }
20189df30794SChris Wilson 
20199df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
20209df30794SChris Wilson {
20219df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
20229df30794SChris Wilson 	struct drm_i915_error_state *error;
20236dc0e816SBen Widawsky 	unsigned long flags;
20249df30794SChris Wilson 
202599584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
202699584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
202799584db3SDaniel Vetter 	dev_priv->gpu_error.first_error = NULL;
202899584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
20299df30794SChris Wilson 
20309df30794SChris Wilson 	if (error)
2031742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
203263eeaf38SJesse Barnes }
20333bd3c932SChris Wilson #else
20343bd3c932SChris Wilson #define i915_capture_error_state(x)
20353bd3c932SChris Wilson #endif
203663eeaf38SJesse Barnes 
203735aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2038c0e09200SDave Airlie {
20398a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2040bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
204163eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2042050ee91fSBen Widawsky 	int pipe, i;
204363eeaf38SJesse Barnes 
204435aed2e6SChris Wilson 	if (!eir)
204535aed2e6SChris Wilson 		return;
204663eeaf38SJesse Barnes 
2047a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
20488a905236SJesse Barnes 
2049bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2050bd9854f9SBen Widawsky 
20518a905236SJesse Barnes 	if (IS_G4X(dev)) {
20528a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
20538a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
20548a905236SJesse Barnes 
2055a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2056a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2057050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2058050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2059a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2060a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
20618a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20623143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
20638a905236SJesse Barnes 		}
20648a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
20658a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2066a70491ccSJoe Perches 			pr_err("page table error\n");
2067a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
20688a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20693143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
20708a905236SJesse Barnes 		}
20718a905236SJesse Barnes 	}
20728a905236SJesse Barnes 
2073a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
207463eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
207563eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2076a70491ccSJoe Perches 			pr_err("page table error\n");
2077a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
207863eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20793143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
208063eeaf38SJesse Barnes 		}
20818a905236SJesse Barnes 	}
20828a905236SJesse Barnes 
208363eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2084a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
20859db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2086a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
20879db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
208863eeaf38SJesse Barnes 		/* pipestat has already been acked */
208963eeaf38SJesse Barnes 	}
209063eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2091a70491ccSJoe Perches 		pr_err("instruction error\n");
2092a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2093050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2094050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2095a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
209663eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
209763eeaf38SJesse Barnes 
2098a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2099a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2100a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
210163eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
21023143a2bfSChris Wilson 			POSTING_READ(IPEIR);
210363eeaf38SJesse Barnes 		} else {
210463eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
210563eeaf38SJesse Barnes 
2106a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2107a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2108a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2109a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
211063eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
21113143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
211263eeaf38SJesse Barnes 		}
211363eeaf38SJesse Barnes 	}
211463eeaf38SJesse Barnes 
211563eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
21163143a2bfSChris Wilson 	POSTING_READ(EIR);
211763eeaf38SJesse Barnes 	eir = I915_READ(EIR);
211863eeaf38SJesse Barnes 	if (eir) {
211963eeaf38SJesse Barnes 		/*
212063eeaf38SJesse Barnes 		 * some errors might have become stuck,
212163eeaf38SJesse Barnes 		 * mask them.
212263eeaf38SJesse Barnes 		 */
212363eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
212463eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
212563eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
212663eeaf38SJesse Barnes 	}
212735aed2e6SChris Wilson }
212835aed2e6SChris Wilson 
212935aed2e6SChris Wilson /**
213035aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
213135aed2e6SChris Wilson  * @dev: drm device
213235aed2e6SChris Wilson  *
213335aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
213435aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
213535aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
213635aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
213735aed2e6SChris Wilson  * of a ring dump etc.).
213835aed2e6SChris Wilson  */
2139527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
214035aed2e6SChris Wilson {
214135aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2142b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2143b4519513SChris Wilson 	int i;
214435aed2e6SChris Wilson 
214535aed2e6SChris Wilson 	i915_capture_error_state(dev);
214635aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
21478a905236SJesse Barnes 
2148ba1234d1SBen Gamari 	if (wedged) {
2149f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2150f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2151ba1234d1SBen Gamari 
215211ed50ecSBen Gamari 		/*
21531f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
21541f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
215511ed50ecSBen Gamari 		 */
2156b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
2157b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
215811ed50ecSBen Gamari 	}
215911ed50ecSBen Gamari 
216099584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
21618a905236SJesse Barnes }
21628a905236SJesse Barnes 
216321ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
21644e5359cdSSimon Farnsworth {
21654e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
21664e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
21674e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
216805394f39SChris Wilson 	struct drm_i915_gem_object *obj;
21694e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
21704e5359cdSSimon Farnsworth 	unsigned long flags;
21714e5359cdSSimon Farnsworth 	bool stall_detected;
21724e5359cdSSimon Farnsworth 
21734e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
21744e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
21754e5359cdSSimon Farnsworth 		return;
21764e5359cdSSimon Farnsworth 
21774e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
21784e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
21794e5359cdSSimon Farnsworth 
2180e7d841caSChris Wilson 	if (work == NULL ||
2181e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2182e7d841caSChris Wilson 	    !work->enable_stall_check) {
21834e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
21844e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
21854e5359cdSSimon Farnsworth 		return;
21864e5359cdSSimon Farnsworth 	}
21874e5359cdSSimon Farnsworth 
21884e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
218905394f39SChris Wilson 	obj = work->pending_flip_obj;
2190a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
21919db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2192446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2193f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
21944e5359cdSSimon Farnsworth 	} else {
21959db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
2196f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
219701f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
21984e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
21994e5359cdSSimon Farnsworth 	}
22004e5359cdSSimon Farnsworth 
22014e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
22024e5359cdSSimon Farnsworth 
22034e5359cdSSimon Farnsworth 	if (stall_detected) {
22044e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
22054e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
22064e5359cdSSimon Farnsworth 	}
22074e5359cdSSimon Farnsworth }
22084e5359cdSSimon Farnsworth 
220942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
221042f52ef8SKeith Packard  * we use as a pipe index
221142f52ef8SKeith Packard  */
2212f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
22130a3e67a4SJesse Barnes {
22140a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2215e9d21d7fSKeith Packard 	unsigned long irqflags;
221671e0ffa5SJesse Barnes 
22175eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
221871e0ffa5SJesse Barnes 		return -EINVAL;
22190a3e67a4SJesse Barnes 
22201ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2221f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
22227c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
22237c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
22240a3e67a4SJesse Barnes 	else
22257c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
22267c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
22278692d00eSChris Wilson 
22288692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
22298692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
22306b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
22311ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22328692d00eSChris Wilson 
22330a3e67a4SJesse Barnes 	return 0;
22340a3e67a4SJesse Barnes }
22350a3e67a4SJesse Barnes 
2236f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2237f796cf8fSJesse Barnes {
2238f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2239f796cf8fSJesse Barnes 	unsigned long irqflags;
2240f796cf8fSJesse Barnes 
2241f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2242f796cf8fSJesse Barnes 		return -EINVAL;
2243f796cf8fSJesse Barnes 
2244f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2245f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
2246f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2247f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2248f796cf8fSJesse Barnes 
2249f796cf8fSJesse Barnes 	return 0;
2250f796cf8fSJesse Barnes }
2251f796cf8fSJesse Barnes 
2252f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
2253b1f14ad0SJesse Barnes {
2254b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2255b1f14ad0SJesse Barnes 	unsigned long irqflags;
2256b1f14ad0SJesse Barnes 
2257b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2258b1f14ad0SJesse Barnes 		return -EINVAL;
2259b1f14ad0SJesse Barnes 
2260b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2261b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
2262b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
2263b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2264b1f14ad0SJesse Barnes 
2265b1f14ad0SJesse Barnes 	return 0;
2266b1f14ad0SJesse Barnes }
2267b1f14ad0SJesse Barnes 
22687e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
22697e231dbeSJesse Barnes {
22707e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22717e231dbeSJesse Barnes 	unsigned long irqflags;
227231acc7f5SJesse Barnes 	u32 imr;
22737e231dbeSJesse Barnes 
22747e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
22757e231dbeSJesse Barnes 		return -EINVAL;
22767e231dbeSJesse Barnes 
22777e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22787e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
227931acc7f5SJesse Barnes 	if (pipe == 0)
22807e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
228131acc7f5SJesse Barnes 	else
22827e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22837e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
228431acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
228531acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
22867e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22877e231dbeSJesse Barnes 
22887e231dbeSJesse Barnes 	return 0;
22897e231dbeSJesse Barnes }
22907e231dbeSJesse Barnes 
229142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
229242f52ef8SKeith Packard  * we use as a pipe index
229342f52ef8SKeith Packard  */
2294f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
22950a3e67a4SJesse Barnes {
22960a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2297e9d21d7fSKeith Packard 	unsigned long irqflags;
22980a3e67a4SJesse Barnes 
22991ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
23008692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
23016b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
23028692d00eSChris Wilson 
23037c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
23047c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
23057c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
23061ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
23070a3e67a4SJesse Barnes }
23080a3e67a4SJesse Barnes 
2309f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2310f796cf8fSJesse Barnes {
2311f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2312f796cf8fSJesse Barnes 	unsigned long irqflags;
2313f796cf8fSJesse Barnes 
2314f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2315f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
2316f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2317f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2318f796cf8fSJesse Barnes }
2319f796cf8fSJesse Barnes 
2320f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
2321b1f14ad0SJesse Barnes {
2322b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2323b1f14ad0SJesse Barnes 	unsigned long irqflags;
2324b1f14ad0SJesse Barnes 
2325b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2326b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
2327b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
2328b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2329b1f14ad0SJesse Barnes }
2330b1f14ad0SJesse Barnes 
23317e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
23327e231dbeSJesse Barnes {
23337e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23347e231dbeSJesse Barnes 	unsigned long irqflags;
233531acc7f5SJesse Barnes 	u32 imr;
23367e231dbeSJesse Barnes 
23377e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
233831acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
233931acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
23407e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
234131acc7f5SJesse Barnes 	if (pipe == 0)
23427e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
234331acc7f5SJesse Barnes 	else
23447e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23457e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
23467e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
23477e231dbeSJesse Barnes }
23487e231dbeSJesse Barnes 
2349893eead0SChris Wilson static u32
2350893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2351852835f3SZou Nan hai {
2352893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2353893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2354893eead0SChris Wilson }
2355893eead0SChris Wilson 
23569107e9d2SChris Wilson static bool
23579107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2358893eead0SChris Wilson {
23599107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
23609107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2361f65d9421SBen Gamari }
2362f65d9421SBen Gamari 
23636274f212SChris Wilson static struct intel_ring_buffer *
23646274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2365a24a11e6SChris Wilson {
2366a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
23676274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2368a24a11e6SChris Wilson 
2369a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2370a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2371a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
23726274f212SChris Wilson 		return NULL;
2373a24a11e6SChris Wilson 
2374a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2375a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2376a24a11e6SChris Wilson 	 */
23776274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2378a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2379a24a11e6SChris Wilson 	do {
2380a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2381a24a11e6SChris Wilson 		if (cmd == ipehr)
2382a24a11e6SChris Wilson 			break;
2383a24a11e6SChris Wilson 
2384a24a11e6SChris Wilson 		acthd -= 4;
2385a24a11e6SChris Wilson 		if (acthd < acthd_min)
23866274f212SChris Wilson 			return NULL;
2387a24a11e6SChris Wilson 	} while (1);
2388a24a11e6SChris Wilson 
23896274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
23906274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2391a24a11e6SChris Wilson }
2392a24a11e6SChris Wilson 
23936274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
23946274f212SChris Wilson {
23956274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
23966274f212SChris Wilson 	struct intel_ring_buffer *signaller;
23976274f212SChris Wilson 	u32 seqno, ctl;
23986274f212SChris Wilson 
23996274f212SChris Wilson 	ring->hangcheck.deadlock = true;
24006274f212SChris Wilson 
24016274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
24026274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
24036274f212SChris Wilson 		return -1;
24046274f212SChris Wilson 
24056274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
24066274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
24076274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
24086274f212SChris Wilson 		return -1;
24096274f212SChris Wilson 
24106274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
24116274f212SChris Wilson }
24126274f212SChris Wilson 
24136274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
24146274f212SChris Wilson {
24156274f212SChris Wilson 	struct intel_ring_buffer *ring;
24166274f212SChris Wilson 	int i;
24176274f212SChris Wilson 
24186274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
24196274f212SChris Wilson 		ring->hangcheck.deadlock = false;
24206274f212SChris Wilson }
24216274f212SChris Wilson 
2422ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2423ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
24241ec14ad3SChris Wilson {
24251ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
24261ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
24279107e9d2SChris Wilson 	u32 tmp;
24289107e9d2SChris Wilson 
24296274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
24306274f212SChris Wilson 		return active;
24316274f212SChris Wilson 
24329107e9d2SChris Wilson 	if (IS_GEN2(dev))
24336274f212SChris Wilson 		return hung;
24349107e9d2SChris Wilson 
24359107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
24369107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
24379107e9d2SChris Wilson 	 * and break the hang. This should work on
24389107e9d2SChris Wilson 	 * all but the second generation chipsets.
24399107e9d2SChris Wilson 	 */
24409107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
24411ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
24421ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
24431ec14ad3SChris Wilson 			  ring->name);
24441ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
24456274f212SChris Wilson 		return kick;
24461ec14ad3SChris Wilson 	}
2447a24a11e6SChris Wilson 
24486274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
24496274f212SChris Wilson 		switch (semaphore_passed(ring)) {
24506274f212SChris Wilson 		default:
24516274f212SChris Wilson 			return hung;
24526274f212SChris Wilson 		case 1:
2453a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
2454a24a11e6SChris Wilson 				  ring->name);
2455a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
24566274f212SChris Wilson 			return kick;
24576274f212SChris Wilson 		case 0:
24586274f212SChris Wilson 			return wait;
24596274f212SChris Wilson 		}
24609107e9d2SChris Wilson 	}
24619107e9d2SChris Wilson 
24626274f212SChris Wilson 	return hung;
2463a24a11e6SChris Wilson }
2464d1e61e7fSChris Wilson 
2465f65d9421SBen Gamari /**
2466f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
246705407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
246805407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
246905407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
247005407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
247105407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2472f65d9421SBen Gamari  */
2473f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
2474f65d9421SBen Gamari {
2475f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2476f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2477b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2478b4519513SChris Wilson 	int i;
247905407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
24809107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
24819107e9d2SChris Wilson #define BUSY 1
24829107e9d2SChris Wilson #define KICK 5
24839107e9d2SChris Wilson #define HUNG 20
24849107e9d2SChris Wilson #define FIRE 30
2485893eead0SChris Wilson 
24863e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
24873e0dc6b0SBen Widawsky 		return;
24883e0dc6b0SBen Widawsky 
2489b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
249005407ff8SMika Kuoppala 		u32 seqno, acthd;
24919107e9d2SChris Wilson 		bool busy = true;
2492b4519513SChris Wilson 
24936274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
24946274f212SChris Wilson 
249505407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
249605407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
249705407ff8SMika Kuoppala 
249805407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
24999107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
25009107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
25019107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
25029107e9d2SChris Wilson 					DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
25039107e9d2SChris Wilson 						  ring->name);
25049107e9d2SChris Wilson 					wake_up_all(&ring->irq_queue);
25059107e9d2SChris Wilson 					ring->hangcheck.score += HUNG;
25069107e9d2SChris Wilson 				} else
25079107e9d2SChris Wilson 					busy = false;
250805407ff8SMika Kuoppala 			} else {
25099107e9d2SChris Wilson 				int score;
25109107e9d2SChris Wilson 
25116274f212SChris Wilson 				/* We always increment the hangcheck score
25126274f212SChris Wilson 				 * if the ring is busy and still processing
25136274f212SChris Wilson 				 * the same request, so that no single request
25146274f212SChris Wilson 				 * can run indefinitely (such as a chain of
25156274f212SChris Wilson 				 * batches). The only time we do not increment
25166274f212SChris Wilson 				 * the hangcheck score on this ring, if this
25176274f212SChris Wilson 				 * ring is in a legitimate wait for another
25186274f212SChris Wilson 				 * ring. In that case the waiting ring is a
25196274f212SChris Wilson 				 * victim and we want to be sure we catch the
25206274f212SChris Wilson 				 * right culprit. Then every time we do kick
25216274f212SChris Wilson 				 * the ring, add a small increment to the
25226274f212SChris Wilson 				 * score so that we can catch a batch that is
25236274f212SChris Wilson 				 * being repeatedly kicked and so responsible
25246274f212SChris Wilson 				 * for stalling the machine.
25259107e9d2SChris Wilson 				 */
2526ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2527ad8beaeaSMika Kuoppala 								    acthd);
2528ad8beaeaSMika Kuoppala 
2529ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
25306274f212SChris Wilson 				case wait:
25316274f212SChris Wilson 					score = 0;
25326274f212SChris Wilson 					break;
25336274f212SChris Wilson 				case active:
25349107e9d2SChris Wilson 					score = BUSY;
25356274f212SChris Wilson 					break;
25366274f212SChris Wilson 				case kick:
25376274f212SChris Wilson 					score = KICK;
25386274f212SChris Wilson 					break;
25396274f212SChris Wilson 				case hung:
25406274f212SChris Wilson 					score = HUNG;
25416274f212SChris Wilson 					stuck[i] = true;
25426274f212SChris Wilson 					break;
25436274f212SChris Wilson 				}
25449107e9d2SChris Wilson 				ring->hangcheck.score += score;
254505407ff8SMika Kuoppala 			}
25469107e9d2SChris Wilson 		} else {
25479107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
25489107e9d2SChris Wilson 			 * attempts across multiple batches.
25499107e9d2SChris Wilson 			 */
25509107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
25519107e9d2SChris Wilson 				ring->hangcheck.score--;
2552cbb465e7SChris Wilson 		}
2553f65d9421SBen Gamari 
255405407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
255505407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
25569107e9d2SChris Wilson 		busy_count += busy;
255705407ff8SMika Kuoppala 	}
255805407ff8SMika Kuoppala 
255905407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
25609107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2561acd78c11SBen Widawsky 			DRM_ERROR("%s on %s\n",
256205407ff8SMika Kuoppala 				  stuck[i] ? "stuck" : "no progress",
2563a43adf07SChris Wilson 				  ring->name);
2564a43adf07SChris Wilson 			rings_hung++;
256505407ff8SMika Kuoppala 		}
256605407ff8SMika Kuoppala 	}
256705407ff8SMika Kuoppala 
256805407ff8SMika Kuoppala 	if (rings_hung)
256905407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
257005407ff8SMika Kuoppala 
257105407ff8SMika Kuoppala 	if (busy_count)
257205407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
257305407ff8SMika Kuoppala 		 * being added */
257499584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
257505407ff8SMika Kuoppala 			  round_jiffies_up(jiffies +
257605407ff8SMika Kuoppala 					   DRM_I915_HANGCHECK_JIFFIES));
2577f65d9421SBen Gamari }
2578f65d9421SBen Gamari 
257991738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
258091738a95SPaulo Zanoni {
258191738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
258291738a95SPaulo Zanoni 
258391738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
258491738a95SPaulo Zanoni 		return;
258591738a95SPaulo Zanoni 
258691738a95SPaulo Zanoni 	/* south display irq */
258791738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
258891738a95SPaulo Zanoni 	/*
258991738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
259091738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
259191738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
259291738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
259391738a95SPaulo Zanoni 	 */
259491738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
259591738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
259691738a95SPaulo Zanoni }
259791738a95SPaulo Zanoni 
2598c0e09200SDave Airlie /* drm_dma.h hooks
2599c0e09200SDave Airlie */
2600f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2601036a4a7dSZhenyu Wang {
2602036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2603036a4a7dSZhenyu Wang 
26044697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
26054697995bSJesse Barnes 
2606036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2607bdfcdb63SDaniel Vetter 
2608036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
2609036a4a7dSZhenyu Wang 
2610036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2611036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
26123143a2bfSChris Wilson 	POSTING_READ(DEIER);
2613036a4a7dSZhenyu Wang 
2614036a4a7dSZhenyu Wang 	/* and GT */
2615036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2616036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
26173143a2bfSChris Wilson 	POSTING_READ(GTIER);
2618c650156aSZhenyu Wang 
261991738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
26207d99163dSBen Widawsky }
26217d99163dSBen Widawsky 
26227d99163dSBen Widawsky static void ivybridge_irq_preinstall(struct drm_device *dev)
26237d99163dSBen Widawsky {
26247d99163dSBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26257d99163dSBen Widawsky 
26267d99163dSBen Widawsky 	atomic_set(&dev_priv->irq_received, 0);
26277d99163dSBen Widawsky 
26287d99163dSBen Widawsky 	I915_WRITE(HWSTAM, 0xeffe);
26297d99163dSBen Widawsky 
26307d99163dSBen Widawsky 	/* XXX hotplug from PCH */
26317d99163dSBen Widawsky 
26327d99163dSBen Widawsky 	I915_WRITE(DEIMR, 0xffffffff);
26337d99163dSBen Widawsky 	I915_WRITE(DEIER, 0x0);
26347d99163dSBen Widawsky 	POSTING_READ(DEIER);
26357d99163dSBen Widawsky 
26367d99163dSBen Widawsky 	/* and GT */
26377d99163dSBen Widawsky 	I915_WRITE(GTIMR, 0xffffffff);
26387d99163dSBen Widawsky 	I915_WRITE(GTIER, 0x0);
26397d99163dSBen Widawsky 	POSTING_READ(GTIER);
26407d99163dSBen Widawsky 
2641eda63ffbSBen Widawsky 	/* Power management */
2642eda63ffbSBen Widawsky 	I915_WRITE(GEN6_PMIMR, 0xffffffff);
2643eda63ffbSBen Widawsky 	I915_WRITE(GEN6_PMIER, 0x0);
2644eda63ffbSBen Widawsky 	POSTING_READ(GEN6_PMIER);
2645eda63ffbSBen Widawsky 
264691738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
2647036a4a7dSZhenyu Wang }
2648036a4a7dSZhenyu Wang 
26497e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
26507e231dbeSJesse Barnes {
26517e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26527e231dbeSJesse Barnes 	int pipe;
26537e231dbeSJesse Barnes 
26547e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
26557e231dbeSJesse Barnes 
26567e231dbeSJesse Barnes 	/* VLV magic */
26577e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
26587e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
26597e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
26607e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
26617e231dbeSJesse Barnes 
26627e231dbeSJesse Barnes 	/* and GT */
26637e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
26647e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
26657e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
26667e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
26677e231dbeSJesse Barnes 	POSTING_READ(GTIER);
26687e231dbeSJesse Barnes 
26697e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
26707e231dbeSJesse Barnes 
26717e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
26727e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
26737e231dbeSJesse Barnes 	for_each_pipe(pipe)
26747e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
26757e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26767e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
26777e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
26787e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
26797e231dbeSJesse Barnes }
26807e231dbeSJesse Barnes 
268182a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
268282a28bcfSDaniel Vetter {
268382a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
268482a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
268582a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2686fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
268782a28bcfSDaniel Vetter 
268882a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2689fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
269082a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2691cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2692fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
269382a28bcfSDaniel Vetter 	} else {
2694fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
269582a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2696cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2697fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
269882a28bcfSDaniel Vetter 	}
269982a28bcfSDaniel Vetter 
2700fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
270182a28bcfSDaniel Vetter 
27027fe0b973SKeith Packard 	/*
27037fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
27047fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
27057fe0b973SKeith Packard 	 *
27067fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
27077fe0b973SKeith Packard 	 */
27087fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
27097fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
27107fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
27117fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
27127fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
27137fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
27147fe0b973SKeith Packard }
27157fe0b973SKeith Packard 
2716d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2717d46da437SPaulo Zanoni {
2718d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
271982a28bcfSDaniel Vetter 	u32 mask;
2720d46da437SPaulo Zanoni 
2721692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2722692a04cfSDaniel Vetter 		return;
2723692a04cfSDaniel Vetter 
27248664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
27258664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2726de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
27278664281bSPaulo Zanoni 	} else {
27288664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
27298664281bSPaulo Zanoni 
27308664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
27318664281bSPaulo Zanoni 	}
2732ab5c608bSBen Widawsky 
2733d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2734d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2735d46da437SPaulo Zanoni }
2736d46da437SPaulo Zanoni 
2737f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2738036a4a7dSZhenyu Wang {
27394bc9d430SDaniel Vetter 	unsigned long irqflags;
27404bc9d430SDaniel Vetter 
2741036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2742036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
2743013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2744ce99c256SDaniel Vetter 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
27458664281bSPaulo Zanoni 			   DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
2746de032bf4SPaulo Zanoni 			   DE_PIPEA_FIFO_UNDERRUN | DE_POISON;
2747cc609d5dSBen Widawsky 	u32 gt_irqs;
2748036a4a7dSZhenyu Wang 
27491ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2750036a4a7dSZhenyu Wang 
2751036a4a7dSZhenyu Wang 	/* should always can generate irq */
2752036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
27531ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
27546005ce42SDaniel Vetter 	I915_WRITE(DEIER, display_mask |
27556005ce42SDaniel Vetter 			  DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT);
27563143a2bfSChris Wilson 	POSTING_READ(DEIER);
2757036a4a7dSZhenyu Wang 
27581ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
2759036a4a7dSZhenyu Wang 
2760036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
27611ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2762881f47b6SXiang, Haihao 
2763cc609d5dSBen Widawsky 	gt_irqs = GT_RENDER_USER_INTERRUPT;
2764cc609d5dSBen Widawsky 
27651ec14ad3SChris Wilson 	if (IS_GEN6(dev))
2766cc609d5dSBen Widawsky 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
27671ec14ad3SChris Wilson 	else
2768cc609d5dSBen Widawsky 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
2769cc609d5dSBen Widawsky 			   ILK_BSD_USER_INTERRUPT;
2770cc609d5dSBen Widawsky 
2771cc609d5dSBen Widawsky 	I915_WRITE(GTIER, gt_irqs);
27723143a2bfSChris Wilson 	POSTING_READ(GTIER);
2773036a4a7dSZhenyu Wang 
2774d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
27757fe0b973SKeith Packard 
2776f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
27776005ce42SDaniel Vetter 		/* Enable PCU event interrupts
27786005ce42SDaniel Vetter 		 *
27796005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
27804bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
27814bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
27824bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2783f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
27844bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2785f97108d1SJesse Barnes 	}
2786f97108d1SJesse Barnes 
2787036a4a7dSZhenyu Wang 	return 0;
2788036a4a7dSZhenyu Wang }
2789036a4a7dSZhenyu Wang 
2790f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2791b1f14ad0SJesse Barnes {
2792b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2793b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2794b615b57aSChris Wilson 	u32 display_mask =
2795b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2796b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
2797b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
2798ce99c256SDaniel Vetter 		DE_PLANEA_FLIP_DONE_IVB |
27998664281bSPaulo Zanoni 		DE_AUX_CHANNEL_A_IVB |
28008664281bSPaulo Zanoni 		DE_ERR_INT_IVB;
280112638c57SBen Widawsky 	u32 pm_irqs = GEN6_PM_RPS_EVENTS;
2802cc609d5dSBen Widawsky 	u32 gt_irqs;
2803b1f14ad0SJesse Barnes 
2804b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2805b1f14ad0SJesse Barnes 
2806b1f14ad0SJesse Barnes 	/* should always can generate irq */
28078664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2808b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2809b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2810b615b57aSChris Wilson 	I915_WRITE(DEIER,
2811b615b57aSChris Wilson 		   display_mask |
2812b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
2813b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
2814b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
2815b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2816b1f14ad0SJesse Barnes 
2817cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2818b1f14ad0SJesse Barnes 
2819b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2820b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2821b1f14ad0SJesse Barnes 
2822cc609d5dSBen Widawsky 	gt_irqs = GT_RENDER_USER_INTERRUPT | GT_BSD_USER_INTERRUPT |
2823cc609d5dSBen Widawsky 		  GT_BLT_USER_INTERRUPT | GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2824cc609d5dSBen Widawsky 	I915_WRITE(GTIER, gt_irqs);
2825b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2826b1f14ad0SJesse Barnes 
282712638c57SBen Widawsky 	I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
282812638c57SBen Widawsky 	if (HAS_VEBOX(dev))
282912638c57SBen Widawsky 		pm_irqs |= PM_VEBOX_USER_INTERRUPT |
283012638c57SBen Widawsky 			PM_VEBOX_CS_ERROR_INTERRUPT;
283112638c57SBen Widawsky 
283212638c57SBen Widawsky 	/* Our enable/disable rps functions may touch these registers so
283312638c57SBen Widawsky 	 * make sure to set a known state for only the non-RPS bits.
283412638c57SBen Widawsky 	 * The RMW is extra paranoia since this should be called after being set
283512638c57SBen Widawsky 	 * to a known state in preinstall.
283612638c57SBen Widawsky 	 * */
283712638c57SBen Widawsky 	I915_WRITE(GEN6_PMIMR,
283812638c57SBen Widawsky 		   (I915_READ(GEN6_PMIMR) | ~GEN6_PM_RPS_EVENTS) & ~pm_irqs);
283912638c57SBen Widawsky 	I915_WRITE(GEN6_PMIER,
284012638c57SBen Widawsky 		   (I915_READ(GEN6_PMIER) & GEN6_PM_RPS_EVENTS) | pm_irqs);
284112638c57SBen Widawsky 	POSTING_READ(GEN6_PMIER);
2842eda63ffbSBen Widawsky 
2843d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
28447fe0b973SKeith Packard 
2845b1f14ad0SJesse Barnes 	return 0;
2846b1f14ad0SJesse Barnes }
2847b1f14ad0SJesse Barnes 
28487e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
28497e231dbeSJesse Barnes {
28507e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2851cc609d5dSBen Widawsky 	u32 gt_irqs;
28527e231dbeSJesse Barnes 	u32 enable_mask;
285331acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
2854b79480baSDaniel Vetter 	unsigned long irqflags;
28557e231dbeSJesse Barnes 
28567e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
285731acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
285831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
285931acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
28607e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
28617e231dbeSJesse Barnes 
286231acc7f5SJesse Barnes 	/*
286331acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
286431acc7f5SJesse Barnes 	 * toggle them based on usage.
286531acc7f5SJesse Barnes 	 */
286631acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
286731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
286831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
28697e231dbeSJesse Barnes 
287020afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
287120afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
287220afbda2SDaniel Vetter 
28737e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
28747e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
28757e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28767e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
28777e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
28787e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
28797e231dbeSJesse Barnes 
2880b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2881b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2882b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
288331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2884515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
288531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
2886b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
288731acc7f5SJesse Barnes 
28887e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28897e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28907e231dbeSJesse Barnes 
289131acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
289231acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
28933bcedbe5SJesse Barnes 
2894cc609d5dSBen Widawsky 	gt_irqs = GT_RENDER_USER_INTERRUPT | GT_BSD_USER_INTERRUPT |
2895cc609d5dSBen Widawsky 		GT_BLT_USER_INTERRUPT;
2896cc609d5dSBen Widawsky 	I915_WRITE(GTIER, gt_irqs);
28977e231dbeSJesse Barnes 	POSTING_READ(GTIER);
28987e231dbeSJesse Barnes 
28997e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
29007e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
29017e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
29027e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
29037e231dbeSJesse Barnes #endif
29047e231dbeSJesse Barnes 
29057e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
290620afbda2SDaniel Vetter 
290720afbda2SDaniel Vetter 	return 0;
290820afbda2SDaniel Vetter }
290920afbda2SDaniel Vetter 
29107e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
29117e231dbeSJesse Barnes {
29127e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29137e231dbeSJesse Barnes 	int pipe;
29147e231dbeSJesse Barnes 
29157e231dbeSJesse Barnes 	if (!dev_priv)
29167e231dbeSJesse Barnes 		return;
29177e231dbeSJesse Barnes 
2918ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2919ac4c16c5SEgbert Eich 
29207e231dbeSJesse Barnes 	for_each_pipe(pipe)
29217e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
29227e231dbeSJesse Barnes 
29237e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
29247e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
29257e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
29267e231dbeSJesse Barnes 	for_each_pipe(pipe)
29277e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
29287e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
29297e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
29307e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
29317e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
29327e231dbeSJesse Barnes }
29337e231dbeSJesse Barnes 
2934f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2935036a4a7dSZhenyu Wang {
2936036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29374697995bSJesse Barnes 
29384697995bSJesse Barnes 	if (!dev_priv)
29394697995bSJesse Barnes 		return;
29404697995bSJesse Barnes 
2941ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2942ac4c16c5SEgbert Eich 
2943036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2944036a4a7dSZhenyu Wang 
2945036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2946036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2947036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
29488664281bSPaulo Zanoni 	if (IS_GEN7(dev))
29498664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2950036a4a7dSZhenyu Wang 
2951036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2952036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2953036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2954192aac1fSKeith Packard 
2955ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2956ab5c608bSBen Widawsky 		return;
2957ab5c608bSBen Widawsky 
2958192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2959192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2960192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
29618664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
29628664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2963036a4a7dSZhenyu Wang }
2964036a4a7dSZhenyu Wang 
2965c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2966c2798b19SChris Wilson {
2967c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2968c2798b19SChris Wilson 	int pipe;
2969c2798b19SChris Wilson 
2970c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2971c2798b19SChris Wilson 
2972c2798b19SChris Wilson 	for_each_pipe(pipe)
2973c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2974c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2975c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2976c2798b19SChris Wilson 	POSTING_READ16(IER);
2977c2798b19SChris Wilson }
2978c2798b19SChris Wilson 
2979c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2980c2798b19SChris Wilson {
2981c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2982c2798b19SChris Wilson 
2983c2798b19SChris Wilson 	I915_WRITE16(EMR,
2984c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2985c2798b19SChris Wilson 
2986c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2987c2798b19SChris Wilson 	dev_priv->irq_mask =
2988c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2989c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2990c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2991c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2992c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2993c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2994c2798b19SChris Wilson 
2995c2798b19SChris Wilson 	I915_WRITE16(IER,
2996c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2997c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2998c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2999c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3000c2798b19SChris Wilson 	POSTING_READ16(IER);
3001c2798b19SChris Wilson 
3002c2798b19SChris Wilson 	return 0;
3003c2798b19SChris Wilson }
3004c2798b19SChris Wilson 
300590a72f87SVille Syrjälä /*
300690a72f87SVille Syrjälä  * Returns true when a page flip has completed.
300790a72f87SVille Syrjälä  */
300890a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
300990a72f87SVille Syrjälä 			       int pipe, u16 iir)
301090a72f87SVille Syrjälä {
301190a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
301290a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
301390a72f87SVille Syrjälä 
301490a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
301590a72f87SVille Syrjälä 		return false;
301690a72f87SVille Syrjälä 
301790a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
301890a72f87SVille Syrjälä 		return false;
301990a72f87SVille Syrjälä 
302090a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
302190a72f87SVille Syrjälä 
302290a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
302390a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
302490a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
302590a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
302690a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
302790a72f87SVille Syrjälä 	 */
302890a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
302990a72f87SVille Syrjälä 		return false;
303090a72f87SVille Syrjälä 
303190a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
303290a72f87SVille Syrjälä 
303390a72f87SVille Syrjälä 	return true;
303490a72f87SVille Syrjälä }
303590a72f87SVille Syrjälä 
3036ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3037c2798b19SChris Wilson {
3038c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3039c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3040c2798b19SChris Wilson 	u16 iir, new_iir;
3041c2798b19SChris Wilson 	u32 pipe_stats[2];
3042c2798b19SChris Wilson 	unsigned long irqflags;
3043c2798b19SChris Wilson 	int irq_received;
3044c2798b19SChris Wilson 	int pipe;
3045c2798b19SChris Wilson 	u16 flip_mask =
3046c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3047c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3048c2798b19SChris Wilson 
3049c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3050c2798b19SChris Wilson 
3051c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3052c2798b19SChris Wilson 	if (iir == 0)
3053c2798b19SChris Wilson 		return IRQ_NONE;
3054c2798b19SChris Wilson 
3055c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3056c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3057c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3058c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3059c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3060c2798b19SChris Wilson 		 */
3061c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3062c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3063c2798b19SChris Wilson 			i915_handle_error(dev, false);
3064c2798b19SChris Wilson 
3065c2798b19SChris Wilson 		for_each_pipe(pipe) {
3066c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3067c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3068c2798b19SChris Wilson 
3069c2798b19SChris Wilson 			/*
3070c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3071c2798b19SChris Wilson 			 */
3072c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3073c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3074c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3075c2798b19SChris Wilson 							 pipe_name(pipe));
3076c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3077c2798b19SChris Wilson 				irq_received = 1;
3078c2798b19SChris Wilson 			}
3079c2798b19SChris Wilson 		}
3080c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3081c2798b19SChris Wilson 
3082c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3083c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3084c2798b19SChris Wilson 
3085d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3086c2798b19SChris Wilson 
3087c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3088c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3089c2798b19SChris Wilson 
3090c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
309190a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
309290a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
3093c2798b19SChris Wilson 
3094c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
309590a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
309690a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
3097c2798b19SChris Wilson 
3098c2798b19SChris Wilson 		iir = new_iir;
3099c2798b19SChris Wilson 	}
3100c2798b19SChris Wilson 
3101c2798b19SChris Wilson 	return IRQ_HANDLED;
3102c2798b19SChris Wilson }
3103c2798b19SChris Wilson 
3104c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3105c2798b19SChris Wilson {
3106c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3107c2798b19SChris Wilson 	int pipe;
3108c2798b19SChris Wilson 
3109c2798b19SChris Wilson 	for_each_pipe(pipe) {
3110c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3111c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3112c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3113c2798b19SChris Wilson 	}
3114c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3115c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3116c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3117c2798b19SChris Wilson }
3118c2798b19SChris Wilson 
3119a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3120a266c7d5SChris Wilson {
3121a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3122a266c7d5SChris Wilson 	int pipe;
3123a266c7d5SChris Wilson 
3124a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3125a266c7d5SChris Wilson 
3126a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3127a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3128a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3129a266c7d5SChris Wilson 	}
3130a266c7d5SChris Wilson 
313100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3132a266c7d5SChris Wilson 	for_each_pipe(pipe)
3133a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3134a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3135a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3136a266c7d5SChris Wilson 	POSTING_READ(IER);
3137a266c7d5SChris Wilson }
3138a266c7d5SChris Wilson 
3139a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3140a266c7d5SChris Wilson {
3141a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
314238bde180SChris Wilson 	u32 enable_mask;
3143a266c7d5SChris Wilson 
314438bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
314538bde180SChris Wilson 
314638bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
314738bde180SChris Wilson 	dev_priv->irq_mask =
314838bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
314938bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
315038bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
315138bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
315238bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
315338bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
315438bde180SChris Wilson 
315538bde180SChris Wilson 	enable_mask =
315638bde180SChris Wilson 		I915_ASLE_INTERRUPT |
315738bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
315838bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
315938bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
316038bde180SChris Wilson 		I915_USER_INTERRUPT;
316138bde180SChris Wilson 
3162a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
316320afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
316420afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
316520afbda2SDaniel Vetter 
3166a266c7d5SChris Wilson 		/* Enable in IER... */
3167a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3168a266c7d5SChris Wilson 		/* and unmask in IMR */
3169a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3170a266c7d5SChris Wilson 	}
3171a266c7d5SChris Wilson 
3172a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3173a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3174a266c7d5SChris Wilson 	POSTING_READ(IER);
3175a266c7d5SChris Wilson 
3176f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
317720afbda2SDaniel Vetter 
317820afbda2SDaniel Vetter 	return 0;
317920afbda2SDaniel Vetter }
318020afbda2SDaniel Vetter 
318190a72f87SVille Syrjälä /*
318290a72f87SVille Syrjälä  * Returns true when a page flip has completed.
318390a72f87SVille Syrjälä  */
318490a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
318590a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
318690a72f87SVille Syrjälä {
318790a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
318890a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
318990a72f87SVille Syrjälä 
319090a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
319190a72f87SVille Syrjälä 		return false;
319290a72f87SVille Syrjälä 
319390a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
319490a72f87SVille Syrjälä 		return false;
319590a72f87SVille Syrjälä 
319690a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
319790a72f87SVille Syrjälä 
319890a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
319990a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
320090a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
320190a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
320290a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
320390a72f87SVille Syrjälä 	 */
320490a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
320590a72f87SVille Syrjälä 		return false;
320690a72f87SVille Syrjälä 
320790a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
320890a72f87SVille Syrjälä 
320990a72f87SVille Syrjälä 	return true;
321090a72f87SVille Syrjälä }
321190a72f87SVille Syrjälä 
3212ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3213a266c7d5SChris Wilson {
3214a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3215a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
32168291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3217a266c7d5SChris Wilson 	unsigned long irqflags;
321838bde180SChris Wilson 	u32 flip_mask =
321938bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
322038bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
322138bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3222a266c7d5SChris Wilson 
3223a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3224a266c7d5SChris Wilson 
3225a266c7d5SChris Wilson 	iir = I915_READ(IIR);
322638bde180SChris Wilson 	do {
322738bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
32288291ee90SChris Wilson 		bool blc_event = false;
3229a266c7d5SChris Wilson 
3230a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3231a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3232a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3233a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3234a266c7d5SChris Wilson 		 */
3235a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3236a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3237a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3238a266c7d5SChris Wilson 
3239a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3240a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3241a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3242a266c7d5SChris Wilson 
324338bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3244a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3245a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3246a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3247a266c7d5SChris Wilson 							 pipe_name(pipe));
3248a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
324938bde180SChris Wilson 				irq_received = true;
3250a266c7d5SChris Wilson 			}
3251a266c7d5SChris Wilson 		}
3252a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3253a266c7d5SChris Wilson 
3254a266c7d5SChris Wilson 		if (!irq_received)
3255a266c7d5SChris Wilson 			break;
3256a266c7d5SChris Wilson 
3257a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3258a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3259a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3260a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3261b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3262a266c7d5SChris Wilson 
3263a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3264a266c7d5SChris Wilson 				  hotplug_status);
326591d131d2SDaniel Vetter 
326610a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
326791d131d2SDaniel Vetter 
3268a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
326938bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3270a266c7d5SChris Wilson 		}
3271a266c7d5SChris Wilson 
327238bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3273a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3274a266c7d5SChris Wilson 
3275a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3276a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3277a266c7d5SChris Wilson 
3278a266c7d5SChris Wilson 		for_each_pipe(pipe) {
327938bde180SChris Wilson 			int plane = pipe;
328038bde180SChris Wilson 			if (IS_MOBILE(dev))
328138bde180SChris Wilson 				plane = !plane;
32825e2032d4SVille Syrjälä 
328390a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
328490a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
328590a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3286a266c7d5SChris Wilson 
3287a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3288a266c7d5SChris Wilson 				blc_event = true;
3289a266c7d5SChris Wilson 		}
3290a266c7d5SChris Wilson 
3291a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3292a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3293a266c7d5SChris Wilson 
3294a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3295a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3296a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3297a266c7d5SChris Wilson 		 * we would never get another interrupt.
3298a266c7d5SChris Wilson 		 *
3299a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3300a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3301a266c7d5SChris Wilson 		 * another one.
3302a266c7d5SChris Wilson 		 *
3303a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3304a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3305a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3306a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3307a266c7d5SChris Wilson 		 * stray interrupts.
3308a266c7d5SChris Wilson 		 */
330938bde180SChris Wilson 		ret = IRQ_HANDLED;
3310a266c7d5SChris Wilson 		iir = new_iir;
331138bde180SChris Wilson 	} while (iir & ~flip_mask);
3312a266c7d5SChris Wilson 
3313d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
33148291ee90SChris Wilson 
3315a266c7d5SChris Wilson 	return ret;
3316a266c7d5SChris Wilson }
3317a266c7d5SChris Wilson 
3318a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3319a266c7d5SChris Wilson {
3320a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3321a266c7d5SChris Wilson 	int pipe;
3322a266c7d5SChris Wilson 
3323ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3324ac4c16c5SEgbert Eich 
3325a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3326a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3327a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3328a266c7d5SChris Wilson 	}
3329a266c7d5SChris Wilson 
333000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
333155b39755SChris Wilson 	for_each_pipe(pipe) {
333255b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3333a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
333455b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
333555b39755SChris Wilson 	}
3336a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3337a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3338a266c7d5SChris Wilson 
3339a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3340a266c7d5SChris Wilson }
3341a266c7d5SChris Wilson 
3342a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3343a266c7d5SChris Wilson {
3344a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3345a266c7d5SChris Wilson 	int pipe;
3346a266c7d5SChris Wilson 
3347a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3348a266c7d5SChris Wilson 
3349a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3350a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3351a266c7d5SChris Wilson 
3352a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3353a266c7d5SChris Wilson 	for_each_pipe(pipe)
3354a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3355a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3356a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3357a266c7d5SChris Wilson 	POSTING_READ(IER);
3358a266c7d5SChris Wilson }
3359a266c7d5SChris Wilson 
3360a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3361a266c7d5SChris Wilson {
3362a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3363bbba0a97SChris Wilson 	u32 enable_mask;
3364a266c7d5SChris Wilson 	u32 error_mask;
3365b79480baSDaniel Vetter 	unsigned long irqflags;
3366a266c7d5SChris Wilson 
3367a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3368bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3369adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3370bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3371bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3372bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3373bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3374bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3375bbba0a97SChris Wilson 
3376bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
337721ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
337821ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3379bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3380bbba0a97SChris Wilson 
3381bbba0a97SChris Wilson 	if (IS_G4X(dev))
3382bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3383a266c7d5SChris Wilson 
3384b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3385b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3386b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3387515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
3388b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3389a266c7d5SChris Wilson 
3390a266c7d5SChris Wilson 	/*
3391a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3392a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3393a266c7d5SChris Wilson 	 */
3394a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3395a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3396a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3397a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3398a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3399a266c7d5SChris Wilson 	} else {
3400a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3401a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3402a266c7d5SChris Wilson 	}
3403a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3404a266c7d5SChris Wilson 
3405a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3406a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3407a266c7d5SChris Wilson 	POSTING_READ(IER);
3408a266c7d5SChris Wilson 
340920afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
341020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
341120afbda2SDaniel Vetter 
3412f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
341320afbda2SDaniel Vetter 
341420afbda2SDaniel Vetter 	return 0;
341520afbda2SDaniel Vetter }
341620afbda2SDaniel Vetter 
3417bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
341820afbda2SDaniel Vetter {
341920afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3420e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3421cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
342220afbda2SDaniel Vetter 	u32 hotplug_en;
342320afbda2SDaniel Vetter 
3424b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3425b5ea2d56SDaniel Vetter 
3426bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3427bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3428bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3429adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3430e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3431cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3432cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3433cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3434a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3435a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3436a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3437a266c7d5SChris Wilson 		*/
3438a266c7d5SChris Wilson 		if (IS_G4X(dev))
3439a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
344085fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3441a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3442a266c7d5SChris Wilson 
3443a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3444a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3445a266c7d5SChris Wilson 	}
3446bac56d5bSEgbert Eich }
3447a266c7d5SChris Wilson 
3448ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3449a266c7d5SChris Wilson {
3450a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3451a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3452a266c7d5SChris Wilson 	u32 iir, new_iir;
3453a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3454a266c7d5SChris Wilson 	unsigned long irqflags;
3455a266c7d5SChris Wilson 	int irq_received;
3456a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
345721ad8330SVille Syrjälä 	u32 flip_mask =
345821ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
345921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3460a266c7d5SChris Wilson 
3461a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3462a266c7d5SChris Wilson 
3463a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3464a266c7d5SChris Wilson 
3465a266c7d5SChris Wilson 	for (;;) {
34662c8ba29fSChris Wilson 		bool blc_event = false;
34672c8ba29fSChris Wilson 
346821ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3469a266c7d5SChris Wilson 
3470a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3471a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3472a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3473a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3474a266c7d5SChris Wilson 		 */
3475a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3476a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3477a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3478a266c7d5SChris Wilson 
3479a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3480a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3481a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3482a266c7d5SChris Wilson 
3483a266c7d5SChris Wilson 			/*
3484a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3485a266c7d5SChris Wilson 			 */
3486a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3487a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3488a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3489a266c7d5SChris Wilson 							 pipe_name(pipe));
3490a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3491a266c7d5SChris Wilson 				irq_received = 1;
3492a266c7d5SChris Wilson 			}
3493a266c7d5SChris Wilson 		}
3494a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3495a266c7d5SChris Wilson 
3496a266c7d5SChris Wilson 		if (!irq_received)
3497a266c7d5SChris Wilson 			break;
3498a266c7d5SChris Wilson 
3499a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3500a266c7d5SChris Wilson 
3501a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3502adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3503a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3504b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3505b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
35064f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3507a266c7d5SChris Wilson 
3508a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3509a266c7d5SChris Wilson 				  hotplug_status);
351091d131d2SDaniel Vetter 
351110a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
351210a504deSDaniel Vetter 					      IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
351391d131d2SDaniel Vetter 
3514a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3515a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3516a266c7d5SChris Wilson 		}
3517a266c7d5SChris Wilson 
351821ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3519a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3520a266c7d5SChris Wilson 
3521a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3522a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3523a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3524a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3525a266c7d5SChris Wilson 
3526a266c7d5SChris Wilson 		for_each_pipe(pipe) {
35272c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
352890a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
352990a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3530a266c7d5SChris Wilson 
3531a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3532a266c7d5SChris Wilson 				blc_event = true;
3533a266c7d5SChris Wilson 		}
3534a266c7d5SChris Wilson 
3535a266c7d5SChris Wilson 
3536a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3537a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3538a266c7d5SChris Wilson 
3539515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3540515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3541515ac2bbSDaniel Vetter 
3542a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3543a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3544a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3545a266c7d5SChris Wilson 		 * we would never get another interrupt.
3546a266c7d5SChris Wilson 		 *
3547a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3548a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3549a266c7d5SChris Wilson 		 * another one.
3550a266c7d5SChris Wilson 		 *
3551a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3552a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3553a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3554a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3555a266c7d5SChris Wilson 		 * stray interrupts.
3556a266c7d5SChris Wilson 		 */
3557a266c7d5SChris Wilson 		iir = new_iir;
3558a266c7d5SChris Wilson 	}
3559a266c7d5SChris Wilson 
3560d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
35612c8ba29fSChris Wilson 
3562a266c7d5SChris Wilson 	return ret;
3563a266c7d5SChris Wilson }
3564a266c7d5SChris Wilson 
3565a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3566a266c7d5SChris Wilson {
3567a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3568a266c7d5SChris Wilson 	int pipe;
3569a266c7d5SChris Wilson 
3570a266c7d5SChris Wilson 	if (!dev_priv)
3571a266c7d5SChris Wilson 		return;
3572a266c7d5SChris Wilson 
3573ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3574ac4c16c5SEgbert Eich 
3575a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3576a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3577a266c7d5SChris Wilson 
3578a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3579a266c7d5SChris Wilson 	for_each_pipe(pipe)
3580a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3581a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3582a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3583a266c7d5SChris Wilson 
3584a266c7d5SChris Wilson 	for_each_pipe(pipe)
3585a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3586a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3587a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3588a266c7d5SChris Wilson }
3589a266c7d5SChris Wilson 
3590ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3591ac4c16c5SEgbert Eich {
3592ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3593ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3594ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3595ac4c16c5SEgbert Eich 	unsigned long irqflags;
3596ac4c16c5SEgbert Eich 	int i;
3597ac4c16c5SEgbert Eich 
3598ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3599ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3600ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3601ac4c16c5SEgbert Eich 
3602ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3603ac4c16c5SEgbert Eich 			continue;
3604ac4c16c5SEgbert Eich 
3605ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3606ac4c16c5SEgbert Eich 
3607ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3608ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3609ac4c16c5SEgbert Eich 
3610ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3611ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3612ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3613ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3614ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3615ac4c16c5SEgbert Eich 				if (!connector->polled)
3616ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3617ac4c16c5SEgbert Eich 			}
3618ac4c16c5SEgbert Eich 		}
3619ac4c16c5SEgbert Eich 	}
3620ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3621ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3622ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3623ac4c16c5SEgbert Eich }
3624ac4c16c5SEgbert Eich 
3625f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3626f71d4af4SJesse Barnes {
36278b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
36288b2e326dSChris Wilson 
36298b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
363099584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3631c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3632a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
36338b2e326dSChris Wilson 
363499584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
363599584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
363661bac78eSDaniel Vetter 		    (unsigned long) dev);
3637ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3638ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
363961bac78eSDaniel Vetter 
364097a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
36419ee32feaSDaniel Vetter 
3642f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
3643f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
36447d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3645f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3646f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3647f71d4af4SJesse Barnes 	}
3648f71d4af4SJesse Barnes 
3649c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3650f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3651c3613de9SKeith Packard 	else
3652c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
3653f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3654f71d4af4SJesse Barnes 
36557e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
36567e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
36577e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
36587e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
36597e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
36607e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
36617e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3662fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
36634a06e201SDaniel Vetter 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
36647d99163dSBen Widawsky 		/* Share uninstall handlers with ILK/SNB */
3665f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
36667d99163dSBen Widawsky 		dev->driver->irq_preinstall = ivybridge_irq_preinstall;
3667f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
3668f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3669f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
3670f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
367182a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3672f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3673f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3674f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3675f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3676f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3677f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3678f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
367982a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3680f71d4af4SJesse Barnes 	} else {
3681c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3682c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3683c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3684c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3685c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3686a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3687a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3688a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3689a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3690a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
369120afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3692c2798b19SChris Wilson 		} else {
3693a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3694a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3695a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3696a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3697bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3698c2798b19SChris Wilson 		}
3699f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3700f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3701f71d4af4SJesse Barnes 	}
3702f71d4af4SJesse Barnes }
370320afbda2SDaniel Vetter 
370420afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
370520afbda2SDaniel Vetter {
370620afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3707821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3708821450c6SEgbert Eich 	struct drm_connector *connector;
3709b5ea2d56SDaniel Vetter 	unsigned long irqflags;
3710821450c6SEgbert Eich 	int i;
371120afbda2SDaniel Vetter 
3712821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3713821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3714821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3715821450c6SEgbert Eich 	}
3716821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3717821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3718821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3719821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3720821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3721821450c6SEgbert Eich 	}
3722b5ea2d56SDaniel Vetter 
3723b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3724b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
3725b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
372620afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
372720afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
3728b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
372920afbda2SDaniel Vetter }
3730