xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 91d131d21e4916f84e5957cc25bea6dd355dfe77)
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 
1318664281bSPaulo Zanoni 	for_each_pipe(pipe) {
1328664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
1338664281bSPaulo Zanoni 
1348664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
1358664281bSPaulo Zanoni 			return false;
1368664281bSPaulo Zanoni 	}
1378664281bSPaulo Zanoni 
1388664281bSPaulo Zanoni 	return true;
1398664281bSPaulo Zanoni }
1408664281bSPaulo Zanoni 
1418664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
1428664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
1438664281bSPaulo Zanoni {
1448664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1458664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
1468664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
1478664281bSPaulo Zanoni 
1488664281bSPaulo Zanoni 	if (enable)
1498664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
1508664281bSPaulo Zanoni 	else
1518664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
1528664281bSPaulo Zanoni }
1538664281bSPaulo Zanoni 
1548664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
1558664281bSPaulo Zanoni 						  bool enable)
1568664281bSPaulo Zanoni {
1578664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1588664281bSPaulo Zanoni 
1598664281bSPaulo Zanoni 	if (enable) {
1608664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
1618664281bSPaulo Zanoni 			return;
1628664281bSPaulo Zanoni 
1638664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN_A |
1648664281bSPaulo Zanoni 					 ERR_INT_FIFO_UNDERRUN_B |
1658664281bSPaulo Zanoni 					 ERR_INT_FIFO_UNDERRUN_C);
1668664281bSPaulo Zanoni 
1678664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
1688664281bSPaulo Zanoni 	} else {
1698664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
1708664281bSPaulo Zanoni 	}
1718664281bSPaulo Zanoni }
1728664281bSPaulo Zanoni 
1738664281bSPaulo Zanoni static void ibx_set_fifo_underrun_reporting(struct intel_crtc *crtc,
1748664281bSPaulo Zanoni 					    bool enable)
1758664281bSPaulo Zanoni {
1768664281bSPaulo Zanoni 	struct drm_device *dev = crtc->base.dev;
1778664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1788664281bSPaulo Zanoni 	uint32_t bit = (crtc->pipe == PIPE_A) ? SDE_TRANSA_FIFO_UNDER :
1798664281bSPaulo Zanoni 						SDE_TRANSB_FIFO_UNDER;
1808664281bSPaulo Zanoni 
1818664281bSPaulo Zanoni 	if (enable)
1828664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) & ~bit);
1838664281bSPaulo Zanoni 	else
1848664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) | bit);
1858664281bSPaulo Zanoni 
1868664281bSPaulo Zanoni 	POSTING_READ(SDEIMR);
1878664281bSPaulo Zanoni }
1888664281bSPaulo Zanoni 
1898664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
1908664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
1918664281bSPaulo Zanoni 					    bool enable)
1928664281bSPaulo Zanoni {
1938664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1948664281bSPaulo Zanoni 
1958664281bSPaulo Zanoni 	if (enable) {
1968664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
1978664281bSPaulo Zanoni 			return;
1988664281bSPaulo Zanoni 
1998664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, SERR_INT_TRANS_A_FIFO_UNDERRUN |
2008664281bSPaulo Zanoni 				     SERR_INT_TRANS_B_FIFO_UNDERRUN |
2018664281bSPaulo Zanoni 				     SERR_INT_TRANS_C_FIFO_UNDERRUN);
2028664281bSPaulo Zanoni 
2038664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) & ~SDE_ERROR_CPT);
2048664281bSPaulo Zanoni 	} else {
2058664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) | SDE_ERROR_CPT);
2068664281bSPaulo Zanoni 	}
2078664281bSPaulo Zanoni 
2088664281bSPaulo Zanoni 	POSTING_READ(SDEIMR);
2098664281bSPaulo Zanoni }
2108664281bSPaulo Zanoni 
2118664281bSPaulo Zanoni /**
2128664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
2138664281bSPaulo Zanoni  * @dev: drm device
2148664281bSPaulo Zanoni  * @pipe: pipe
2158664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
2168664281bSPaulo Zanoni  *
2178664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
2188664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
2198664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
2208664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
2218664281bSPaulo Zanoni  * bit for all the pipes.
2228664281bSPaulo Zanoni  *
2238664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
2248664281bSPaulo Zanoni  */
2258664281bSPaulo Zanoni bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
2268664281bSPaulo Zanoni 					   enum pipe pipe, bool enable)
2278664281bSPaulo Zanoni {
2288664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2298664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
2308664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2318664281bSPaulo Zanoni 	unsigned long flags;
2328664281bSPaulo Zanoni 	bool ret;
2338664281bSPaulo Zanoni 
2348664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
2358664281bSPaulo Zanoni 
2368664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
2378664281bSPaulo Zanoni 
2388664281bSPaulo Zanoni 	if (enable == ret)
2398664281bSPaulo Zanoni 		goto done;
2408664281bSPaulo Zanoni 
2418664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
2428664281bSPaulo Zanoni 
2438664281bSPaulo Zanoni 	if (IS_GEN5(dev) || IS_GEN6(dev))
2448664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
2458664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
2468664281bSPaulo Zanoni 		ivybridge_set_fifo_underrun_reporting(dev, enable);
2478664281bSPaulo Zanoni 
2488664281bSPaulo Zanoni done:
2498664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
2508664281bSPaulo Zanoni 	return ret;
2518664281bSPaulo Zanoni }
2528664281bSPaulo Zanoni 
2538664281bSPaulo Zanoni /**
2548664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
2558664281bSPaulo Zanoni  * @dev: drm device
2568664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
2578664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
2588664281bSPaulo Zanoni  *
2598664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
2608664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
2618664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
2628664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
2638664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
2648664281bSPaulo Zanoni  *
2658664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
2668664281bSPaulo Zanoni  */
2678664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
2688664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
2698664281bSPaulo Zanoni 					   bool enable)
2708664281bSPaulo Zanoni {
2718664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2728664281bSPaulo Zanoni 	enum pipe p;
2738664281bSPaulo Zanoni 	struct drm_crtc *crtc;
2748664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc;
2758664281bSPaulo Zanoni 	unsigned long flags;
2768664281bSPaulo Zanoni 	bool ret;
2778664281bSPaulo Zanoni 
2788664281bSPaulo Zanoni 	if (HAS_PCH_LPT(dev)) {
2798664281bSPaulo Zanoni 		crtc = NULL;
2808664281bSPaulo Zanoni 		for_each_pipe(p) {
2818664281bSPaulo Zanoni 			struct drm_crtc *c = dev_priv->pipe_to_crtc_mapping[p];
2828664281bSPaulo Zanoni 			if (intel_pipe_has_type(c, INTEL_OUTPUT_ANALOG)) {
2838664281bSPaulo Zanoni 				crtc = c;
2848664281bSPaulo Zanoni 				break;
2858664281bSPaulo Zanoni 			}
2868664281bSPaulo Zanoni 		}
2878664281bSPaulo Zanoni 		if (!crtc) {
2888664281bSPaulo Zanoni 			DRM_ERROR("PCH FIFO underrun, but no CRTC using the PCH found\n");
2898664281bSPaulo Zanoni 			return false;
2908664281bSPaulo Zanoni 		}
2918664281bSPaulo Zanoni 	} else {
2928664281bSPaulo Zanoni 		crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
2938664281bSPaulo Zanoni 	}
2948664281bSPaulo Zanoni 	intel_crtc = to_intel_crtc(crtc);
2958664281bSPaulo Zanoni 
2968664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
2978664281bSPaulo Zanoni 
2988664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
2998664281bSPaulo Zanoni 
3008664281bSPaulo Zanoni 	if (enable == ret)
3018664281bSPaulo Zanoni 		goto done;
3028664281bSPaulo Zanoni 
3038664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
3048664281bSPaulo Zanoni 
3058664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
3068664281bSPaulo Zanoni 		ibx_set_fifo_underrun_reporting(intel_crtc, enable);
3078664281bSPaulo Zanoni 	else
3088664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
3098664281bSPaulo Zanoni 
3108664281bSPaulo Zanoni done:
3118664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
3128664281bSPaulo Zanoni 	return ret;
3138664281bSPaulo Zanoni }
3148664281bSPaulo Zanoni 
3158664281bSPaulo Zanoni 
3167c463586SKeith Packard void
3177c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3187c463586SKeith Packard {
3199db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
32046c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3217c463586SKeith Packard 
32246c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
32346c06a30SVille Syrjälä 		return;
32446c06a30SVille Syrjälä 
3257c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
32646c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
32746c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3283143a2bfSChris Wilson 	POSTING_READ(reg);
3297c463586SKeith Packard }
3307c463586SKeith Packard 
3317c463586SKeith Packard void
3327c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3337c463586SKeith Packard {
3349db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
33546c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3367c463586SKeith Packard 
33746c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
33846c06a30SVille Syrjälä 		return;
33946c06a30SVille Syrjälä 
34046c06a30SVille Syrjälä 	pipestat &= ~mask;
34146c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3423143a2bfSChris Wilson 	POSTING_READ(reg);
3437c463586SKeith Packard }
3447c463586SKeith Packard 
345c0e09200SDave Airlie /**
346f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
34701c66889SZhao Yakui  */
348f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
34901c66889SZhao Yakui {
3501ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
3511ec14ad3SChris Wilson 	unsigned long irqflags;
3521ec14ad3SChris Wilson 
353f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
354f49e38ddSJani Nikula 		return;
355f49e38ddSJani Nikula 
3561ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
35701c66889SZhao Yakui 
358f898780bSJani Nikula 	i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
359a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
360f898780bSJani Nikula 		i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
3611ec14ad3SChris Wilson 
3621ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
36301c66889SZhao Yakui }
36401c66889SZhao Yakui 
36501c66889SZhao Yakui /**
3660a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
3670a3e67a4SJesse Barnes  * @dev: DRM device
3680a3e67a4SJesse Barnes  * @pipe: pipe to check
3690a3e67a4SJesse Barnes  *
3700a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
3710a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
3720a3e67a4SJesse Barnes  * before reading such registers if unsure.
3730a3e67a4SJesse Barnes  */
3740a3e67a4SJesse Barnes static int
3750a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
3760a3e67a4SJesse Barnes {
3770a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
378702e7a56SPaulo Zanoni 
379a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
380a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
381a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
382a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
38371f8ba6bSPaulo Zanoni 
384a01025afSDaniel Vetter 		return intel_crtc->active;
385a01025afSDaniel Vetter 	} else {
386a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
387a01025afSDaniel Vetter 	}
3880a3e67a4SJesse Barnes }
3890a3e67a4SJesse Barnes 
39042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
39142f52ef8SKeith Packard  * we use as a pipe index
39242f52ef8SKeith Packard  */
393f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
3940a3e67a4SJesse Barnes {
3950a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3960a3e67a4SJesse Barnes 	unsigned long high_frame;
3970a3e67a4SJesse Barnes 	unsigned long low_frame;
3985eddb70bSChris Wilson 	u32 high1, high2, low;
3990a3e67a4SJesse Barnes 
4000a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
40144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4029db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
4030a3e67a4SJesse Barnes 		return 0;
4040a3e67a4SJesse Barnes 	}
4050a3e67a4SJesse Barnes 
4069db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
4079db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
4085eddb70bSChris Wilson 
4090a3e67a4SJesse Barnes 	/*
4100a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
4110a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
4120a3e67a4SJesse Barnes 	 * register.
4130a3e67a4SJesse Barnes 	 */
4140a3e67a4SJesse Barnes 	do {
4155eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4165eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
4175eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4180a3e67a4SJesse Barnes 	} while (high1 != high2);
4190a3e67a4SJesse Barnes 
4205eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
4215eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
4225eddb70bSChris Wilson 	return (high1 << 8) | low;
4230a3e67a4SJesse Barnes }
4240a3e67a4SJesse Barnes 
425f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
4269880b7a5SJesse Barnes {
4279880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4289db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
4299880b7a5SJesse Barnes 
4309880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
43144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4329db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4339880b7a5SJesse Barnes 		return 0;
4349880b7a5SJesse Barnes 	}
4359880b7a5SJesse Barnes 
4369880b7a5SJesse Barnes 	return I915_READ(reg);
4379880b7a5SJesse Barnes }
4389880b7a5SJesse Barnes 
439f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
4400af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
4410af7e4dfSMario Kleiner {
4420af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4430af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
4440af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
4450af7e4dfSMario Kleiner 	bool in_vbl = true;
4460af7e4dfSMario Kleiner 	int ret = 0;
447fe2b8f9dSPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
448fe2b8f9dSPaulo Zanoni 								      pipe);
4490af7e4dfSMario Kleiner 
4500af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
4510af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
4529db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4530af7e4dfSMario Kleiner 		return 0;
4540af7e4dfSMario Kleiner 	}
4550af7e4dfSMario Kleiner 
4560af7e4dfSMario Kleiner 	/* Get vtotal. */
457fe2b8f9dSPaulo Zanoni 	vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4580af7e4dfSMario Kleiner 
4590af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
4600af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
4610af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
4620af7e4dfSMario Kleiner 		 */
4630af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
4640af7e4dfSMario Kleiner 
4650af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
4660af7e4dfSMario Kleiner 		 * horizontal scanout position.
4670af7e4dfSMario Kleiner 		 */
4680af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
4690af7e4dfSMario Kleiner 		*hpos = 0;
4700af7e4dfSMario Kleiner 	} else {
4710af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
4720af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
4730af7e4dfSMario Kleiner 		 * scanout position.
4740af7e4dfSMario Kleiner 		 */
4750af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
4760af7e4dfSMario Kleiner 
477fe2b8f9dSPaulo Zanoni 		htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4780af7e4dfSMario Kleiner 		*vpos = position / htotal;
4790af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
4800af7e4dfSMario Kleiner 	}
4810af7e4dfSMario Kleiner 
4820af7e4dfSMario Kleiner 	/* Query vblank area. */
483fe2b8f9dSPaulo Zanoni 	vbl = I915_READ(VBLANK(cpu_transcoder));
4840af7e4dfSMario Kleiner 
4850af7e4dfSMario Kleiner 	/* Test position against vblank region. */
4860af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
4870af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
4880af7e4dfSMario Kleiner 
4890af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
4900af7e4dfSMario Kleiner 		in_vbl = false;
4910af7e4dfSMario Kleiner 
4920af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
4930af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
4940af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
4950af7e4dfSMario Kleiner 
4960af7e4dfSMario Kleiner 	/* Readouts valid? */
4970af7e4dfSMario Kleiner 	if (vbl > 0)
4980af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
4990af7e4dfSMario Kleiner 
5000af7e4dfSMario Kleiner 	/* In vblank? */
5010af7e4dfSMario Kleiner 	if (in_vbl)
5020af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
5030af7e4dfSMario Kleiner 
5040af7e4dfSMario Kleiner 	return ret;
5050af7e4dfSMario Kleiner }
5060af7e4dfSMario Kleiner 
507f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
5080af7e4dfSMario Kleiner 			      int *max_error,
5090af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
5100af7e4dfSMario Kleiner 			      unsigned flags)
5110af7e4dfSMario Kleiner {
5124041b853SChris Wilson 	struct drm_crtc *crtc;
5130af7e4dfSMario Kleiner 
5147eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
5154041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5160af7e4dfSMario Kleiner 		return -EINVAL;
5170af7e4dfSMario Kleiner 	}
5180af7e4dfSMario Kleiner 
5190af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
5204041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
5214041b853SChris Wilson 	if (crtc == NULL) {
5224041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5234041b853SChris Wilson 		return -EINVAL;
5244041b853SChris Wilson 	}
5254041b853SChris Wilson 
5264041b853SChris Wilson 	if (!crtc->enabled) {
5274041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
5284041b853SChris Wilson 		return -EBUSY;
5294041b853SChris Wilson 	}
5300af7e4dfSMario Kleiner 
5310af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
5324041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
5334041b853SChris Wilson 						     vblank_time, flags,
5344041b853SChris Wilson 						     crtc);
5350af7e4dfSMario Kleiner }
5360af7e4dfSMario Kleiner 
537321a1b30SEgbert Eich static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
538321a1b30SEgbert Eich {
539321a1b30SEgbert Eich 	enum drm_connector_status old_status;
540321a1b30SEgbert Eich 
541321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
542321a1b30SEgbert Eich 	old_status = connector->status;
543321a1b30SEgbert Eich 
544321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
545321a1b30SEgbert Eich 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
546321a1b30SEgbert Eich 		      connector->base.id,
547321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
548321a1b30SEgbert Eich 		      old_status, connector->status);
549321a1b30SEgbert Eich 	return (old_status != connector->status);
550321a1b30SEgbert Eich }
551321a1b30SEgbert Eich 
5525ca58282SJesse Barnes /*
5535ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
5545ca58282SJesse Barnes  */
555ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
556ac4c16c5SEgbert Eich 
5575ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
5585ca58282SJesse Barnes {
5595ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5605ca58282SJesse Barnes 						    hotplug_work);
5615ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
562c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
563cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
564cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
565cd569aedSEgbert Eich 	struct drm_connector *connector;
566cd569aedSEgbert Eich 	unsigned long irqflags;
567cd569aedSEgbert Eich 	bool hpd_disabled = false;
568321a1b30SEgbert Eich 	bool changed = false;
569142e2398SEgbert Eich 	u32 hpd_event_bits;
5705ca58282SJesse Barnes 
57152d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
57252d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
57352d7ecedSDaniel Vetter 		return;
57452d7ecedSDaniel Vetter 
575a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
576e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
577e67189abSJesse Barnes 
578cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
579142e2398SEgbert Eich 
580142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
581142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
582cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
583cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
584cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
585cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
586cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
587cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
588cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
589cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
590cd569aedSEgbert Eich 				drm_get_connector_name(connector));
591cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
592cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
593cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
594cd569aedSEgbert Eich 			hpd_disabled = true;
595cd569aedSEgbert Eich 		}
596142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
597142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
598142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
599142e2398SEgbert Eich 		}
600cd569aedSEgbert Eich 	}
601cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
602cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
603cd569aedSEgbert Eich 	  * some connectors */
604ac4c16c5SEgbert Eich 	if (hpd_disabled) {
605cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
606ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
607ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
608ac4c16c5SEgbert Eich 	}
609cd569aedSEgbert Eich 
610cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
611cd569aedSEgbert Eich 
612321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
613321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
614321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
615321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
616cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
617cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
618321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
619321a1b30SEgbert Eich 				changed = true;
620321a1b30SEgbert Eich 		}
621321a1b30SEgbert Eich 	}
62240ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
62340ee3381SKeith Packard 
624321a1b30SEgbert Eich 	if (changed)
625321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
6265ca58282SJesse Barnes }
6275ca58282SJesse Barnes 
62873edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
629f97108d1SJesse Barnes {
630f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
631b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
6329270388eSDaniel Vetter 	u8 new_delay;
6339270388eSDaniel Vetter 	unsigned long flags;
6349270388eSDaniel Vetter 
6359270388eSDaniel Vetter 	spin_lock_irqsave(&mchdev_lock, flags);
636f97108d1SJesse Barnes 
63773edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
63873edd18fSDaniel Vetter 
63920e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
6409270388eSDaniel Vetter 
6417648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
642b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
643b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
644f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
645f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
646f97108d1SJesse Barnes 
647f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
648b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
64920e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
65020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
65120e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
65220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
653b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
65420e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
65520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
65620e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
65720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
658f97108d1SJesse Barnes 	}
659f97108d1SJesse Barnes 
6607648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
66120e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
662f97108d1SJesse Barnes 
6639270388eSDaniel Vetter 	spin_unlock_irqrestore(&mchdev_lock, flags);
6649270388eSDaniel Vetter 
665f97108d1SJesse Barnes 	return;
666f97108d1SJesse Barnes }
667f97108d1SJesse Barnes 
668549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
669549f7365SChris Wilson 			struct intel_ring_buffer *ring)
670549f7365SChris Wilson {
671549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
6729862e600SChris Wilson 
673475553deSChris Wilson 	if (ring->obj == NULL)
674475553deSChris Wilson 		return;
675475553deSChris Wilson 
676b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
6779862e600SChris Wilson 
678549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
6793e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
68099584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
681cecc21feSChris Wilson 			  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
6823e0dc6b0SBen Widawsky 	}
683549f7365SChris Wilson }
684549f7365SChris Wilson 
6854912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
6863b8d8d91SJesse Barnes {
6874912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
688c6a828d3SDaniel Vetter 						    rps.work);
6894912d041SBen Widawsky 	u32 pm_iir, pm_imr;
6907b9e0ae6SChris Wilson 	u8 new_delay;
6913b8d8d91SJesse Barnes 
692c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
693c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
694c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
6954912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
6964848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
6974848405cSBen Widawsky 	I915_WRITE(GEN6_PMIMR, pm_imr & ~GEN6_PM_RPS_EVENTS);
698c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
6994912d041SBen Widawsky 
7004848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
7013b8d8d91SJesse Barnes 		return;
7023b8d8d91SJesse Barnes 
7034fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
7047b9e0ae6SChris Wilson 
7057425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
706c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
7077425034aSVille Syrjälä 
7087425034aSVille Syrjälä 		/*
7097425034aSVille Syrjälä 		 * For better performance, jump directly
7107425034aSVille Syrjälä 		 * to RPe if we're below it.
7117425034aSVille Syrjälä 		 */
7127425034aSVille Syrjälä 		if (IS_VALLEYVIEW(dev_priv->dev) &&
7137425034aSVille Syrjälä 		    dev_priv->rps.cur_delay < dev_priv->rps.rpe_delay)
7147425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
7157425034aSVille Syrjälä 	} else
716c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
7173b8d8d91SJesse Barnes 
71879249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
71979249636SBen Widawsky 	 * interrupt
72079249636SBen Widawsky 	 */
721d8289c9eSVille Syrjälä 	if (new_delay >= dev_priv->rps.min_delay &&
722d8289c9eSVille Syrjälä 	    new_delay <= dev_priv->rps.max_delay) {
7230a073b84SJesse Barnes 		if (IS_VALLEYVIEW(dev_priv->dev))
7240a073b84SJesse Barnes 			valleyview_set_rps(dev_priv->dev, new_delay);
7250a073b84SJesse Barnes 		else
7264912d041SBen Widawsky 			gen6_set_rps(dev_priv->dev, new_delay);
72779249636SBen Widawsky 	}
7283b8d8d91SJesse Barnes 
72952ceb908SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev)) {
73052ceb908SJesse Barnes 		/*
73152ceb908SJesse Barnes 		 * On VLV, when we enter RC6 we may not be at the minimum
73252ceb908SJesse Barnes 		 * voltage level, so arm a timer to check.  It should only
73352ceb908SJesse Barnes 		 * fire when there's activity or once after we've entered
73452ceb908SJesse Barnes 		 * RC6, and then won't be re-armed until the next RPS interrupt.
73552ceb908SJesse Barnes 		 */
73652ceb908SJesse Barnes 		mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
73752ceb908SJesse Barnes 				 msecs_to_jiffies(100));
73852ceb908SJesse Barnes 	}
73952ceb908SJesse Barnes 
7404fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
7413b8d8d91SJesse Barnes }
7423b8d8d91SJesse Barnes 
743e3689190SBen Widawsky 
744e3689190SBen Widawsky /**
745e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
746e3689190SBen Widawsky  * occurred.
747e3689190SBen Widawsky  * @work: workqueue struct
748e3689190SBen Widawsky  *
749e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
750e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
751e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
752e3689190SBen Widawsky  */
753e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
754e3689190SBen Widawsky {
755e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
756a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
757e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
758e3689190SBen Widawsky 	char *parity_event[5];
759e3689190SBen Widawsky 	uint32_t misccpctl;
760e3689190SBen Widawsky 	unsigned long flags;
761e3689190SBen Widawsky 
762e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
763e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
764e3689190SBen Widawsky 	 * any time we access those registers.
765e3689190SBen Widawsky 	 */
766e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
767e3689190SBen Widawsky 
768e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
769e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
770e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
771e3689190SBen Widawsky 
772e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
773e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
774e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
775e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
776e3689190SBen Widawsky 
777e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
778e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
779e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
780e3689190SBen Widawsky 
781e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
782e3689190SBen Widawsky 
783e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
784cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
785e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
786e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
787e3689190SBen Widawsky 
788e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
789e3689190SBen Widawsky 
790e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
791e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
792e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
793e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
794e3689190SBen Widawsky 	parity_event[4] = NULL;
795e3689190SBen Widawsky 
796e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
797e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
798e3689190SBen Widawsky 
799e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
800e3689190SBen Widawsky 		  row, bank, subbank);
801e3689190SBen Widawsky 
802e3689190SBen Widawsky 	kfree(parity_event[3]);
803e3689190SBen Widawsky 	kfree(parity_event[2]);
804e3689190SBen Widawsky 	kfree(parity_event[1]);
805e3689190SBen Widawsky }
806e3689190SBen Widawsky 
807d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
808e3689190SBen Widawsky {
809e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
810e3689190SBen Widawsky 	unsigned long flags;
811e3689190SBen Widawsky 
812e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
813e3689190SBen Widawsky 		return;
814e3689190SBen Widawsky 
815e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
816cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
817e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
818e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
819e3689190SBen Widawsky 
820a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
821e3689190SBen Widawsky }
822e3689190SBen Widawsky 
823e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
824e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
825e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
826e7b4c6b1SDaniel Vetter {
827e7b4c6b1SDaniel Vetter 
828cc609d5dSBen Widawsky 	if (gt_iir &
829cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
830e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
831cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
832e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
833cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
834e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
835e7b4c6b1SDaniel Vetter 
836cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
837cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
838cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
839e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
840e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
841e7b4c6b1SDaniel Vetter 	}
842e3689190SBen Widawsky 
843cc609d5dSBen Widawsky 	if (gt_iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
844e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
845e7b4c6b1SDaniel Vetter }
846e7b4c6b1SDaniel Vetter 
847baf02a1fSBen Widawsky /* Legacy way of handling PM interrupts */
848fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
849fc6826d1SChris Wilson 				u32 pm_iir)
850fc6826d1SChris Wilson {
851fc6826d1SChris Wilson 	unsigned long flags;
852fc6826d1SChris Wilson 
853fc6826d1SChris Wilson 	/*
854fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
855fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
856fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
857c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
858fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
859fc6826d1SChris Wilson 	 *
860c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
861fc6826d1SChris Wilson 	 */
862fc6826d1SChris Wilson 
863c6a828d3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
864c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
865c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
866fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
867c6a828d3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
868fc6826d1SChris Wilson 
869c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
870fc6826d1SChris Wilson }
871fc6826d1SChris Wilson 
872b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
873b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
874b543fb04SEgbert Eich 
87510a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
876b543fb04SEgbert Eich 					 u32 hotplug_trigger,
877b543fb04SEgbert Eich 					 const u32 *hpd)
878b543fb04SEgbert Eich {
879b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
880b543fb04SEgbert Eich 	unsigned long irqflags;
881b543fb04SEgbert Eich 	int i;
88210a504deSDaniel Vetter 	bool storm_detected = false;
883b543fb04SEgbert Eich 
884*91d131d2SDaniel Vetter 	if (!hotplug_trigger)
885*91d131d2SDaniel Vetter 		return;
886*91d131d2SDaniel Vetter 
887b543fb04SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
888b543fb04SEgbert Eich 
889b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
890821450c6SEgbert Eich 
891b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
892b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
893b543fb04SEgbert Eich 			continue;
894b543fb04SEgbert Eich 
895bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
896b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
897b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
898b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
899b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
900b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
901b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
902b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
903142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
904b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
90510a504deSDaniel Vetter 			storm_detected = true;
906b543fb04SEgbert Eich 		} else {
907b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
908b543fb04SEgbert Eich 		}
909b543fb04SEgbert Eich 	}
910b543fb04SEgbert Eich 
911b543fb04SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
912cd569aedSEgbert Eich 
91310a504deSDaniel Vetter 	if (storm_detected)
91410a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
9155876fa0dSDaniel Vetter 
9165876fa0dSDaniel Vetter 	queue_work(dev_priv->wq,
9175876fa0dSDaniel Vetter 		   &dev_priv->hotplug_work);
918b543fb04SEgbert Eich }
919b543fb04SEgbert Eich 
920515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
921515ac2bbSDaniel Vetter {
92228c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
92328c70f16SDaniel Vetter 
92428c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
925515ac2bbSDaniel Vetter }
926515ac2bbSDaniel Vetter 
927ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
928ce99c256SDaniel Vetter {
9299ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
9309ee32feaSDaniel Vetter 
9319ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
932ce99c256SDaniel Vetter }
933ce99c256SDaniel Vetter 
934baf02a1fSBen Widawsky /* Unlike gen6_queue_rps_work() from which this function is originally derived,
935baf02a1fSBen Widawsky  * we must be able to deal with other PM interrupts. This is complicated because
936baf02a1fSBen Widawsky  * of the way in which we use the masks to defer the RPS work (which for
937baf02a1fSBen Widawsky  * posterity is necessary because of forcewake).
938baf02a1fSBen Widawsky  */
939baf02a1fSBen Widawsky static void hsw_pm_irq_handler(struct drm_i915_private *dev_priv,
940baf02a1fSBen Widawsky 			       u32 pm_iir)
941baf02a1fSBen Widawsky {
942baf02a1fSBen Widawsky 	unsigned long flags;
943baf02a1fSBen Widawsky 
944baf02a1fSBen Widawsky 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
9454848405cSBen Widawsky 	dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
946baf02a1fSBen Widawsky 	if (dev_priv->rps.pm_iir) {
947baf02a1fSBen Widawsky 		I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
948baf02a1fSBen Widawsky 		/* never want to mask useful interrupts. (also posting read) */
9494848405cSBen Widawsky 		WARN_ON(I915_READ_NOTRACE(GEN6_PMIMR) & ~GEN6_PM_RPS_EVENTS);
950baf02a1fSBen Widawsky 		/* TODO: if queue_work is slow, move it out of the spinlock */
951baf02a1fSBen Widawsky 		queue_work(dev_priv->wq, &dev_priv->rps.work);
952baf02a1fSBen Widawsky 	}
953baf02a1fSBen Widawsky 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
954baf02a1fSBen Widawsky 
95512638c57SBen Widawsky 	if (pm_iir & ~GEN6_PM_RPS_EVENTS) {
95612638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
95712638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
95812638c57SBen Widawsky 
95912638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
96012638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
96112638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
96212638c57SBen Widawsky 		}
96312638c57SBen Widawsky 	}
964baf02a1fSBen Widawsky }
965baf02a1fSBen Widawsky 
966ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
9677e231dbeSJesse Barnes {
9687e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
9697e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
9707e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
9717e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
9727e231dbeSJesse Barnes 	unsigned long irqflags;
9737e231dbeSJesse Barnes 	int pipe;
9747e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
9757e231dbeSJesse Barnes 
9767e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
9777e231dbeSJesse Barnes 
9787e231dbeSJesse Barnes 	while (true) {
9797e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
9807e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
9817e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
9827e231dbeSJesse Barnes 
9837e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
9847e231dbeSJesse Barnes 			goto out;
9857e231dbeSJesse Barnes 
9867e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
9877e231dbeSJesse Barnes 
988e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
9897e231dbeSJesse Barnes 
9907e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
9917e231dbeSJesse Barnes 		for_each_pipe(pipe) {
9927e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
9937e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
9947e231dbeSJesse Barnes 
9957e231dbeSJesse Barnes 			/*
9967e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
9977e231dbeSJesse Barnes 			 */
9987e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
9997e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
10007e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
10017e231dbeSJesse Barnes 							 pipe_name(pipe));
10027e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
10037e231dbeSJesse Barnes 			}
10047e231dbeSJesse Barnes 		}
10057e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
10067e231dbeSJesse Barnes 
100731acc7f5SJesse Barnes 		for_each_pipe(pipe) {
100831acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
100931acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
101031acc7f5SJesse Barnes 
101131acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
101231acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
101331acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
101431acc7f5SJesse Barnes 			}
101531acc7f5SJesse Barnes 		}
101631acc7f5SJesse Barnes 
10177e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
10187e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
10197e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1020b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
10217e231dbeSJesse Barnes 
10227e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
10237e231dbeSJesse Barnes 					 hotplug_status);
1024*91d131d2SDaniel Vetter 
102510a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
1026*91d131d2SDaniel Vetter 
10277e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
10287e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
10297e231dbeSJesse Barnes 		}
10307e231dbeSJesse Barnes 
1031515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1032515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
10337e231dbeSJesse Barnes 
10344848405cSBen Widawsky 		if (pm_iir & GEN6_PM_RPS_EVENTS)
1035fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
10367e231dbeSJesse Barnes 
10377e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
10387e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
10397e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
10407e231dbeSJesse Barnes 	}
10417e231dbeSJesse Barnes 
10427e231dbeSJesse Barnes out:
10437e231dbeSJesse Barnes 	return ret;
10447e231dbeSJesse Barnes }
10457e231dbeSJesse Barnes 
104623e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1047776ad806SJesse Barnes {
1048776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10499db4a9c7SJesse Barnes 	int pipe;
1050b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1051776ad806SJesse Barnes 
105210a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
1053*91d131d2SDaniel Vetter 
1054cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1055cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1056776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1057cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1058cfc33bf7SVille Syrjälä 				 port_name(port));
1059cfc33bf7SVille Syrjälä 	}
1060776ad806SJesse Barnes 
1061ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1062ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1063ce99c256SDaniel Vetter 
1064776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1065515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1066776ad806SJesse Barnes 
1067776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1068776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1069776ad806SJesse Barnes 
1070776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1071776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1072776ad806SJesse Barnes 
1073776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1074776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1075776ad806SJesse Barnes 
10769db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
10779db4a9c7SJesse Barnes 		for_each_pipe(pipe)
10789db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
10799db4a9c7SJesse Barnes 					 pipe_name(pipe),
10809db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1081776ad806SJesse Barnes 
1082776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1083776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1084776ad806SJesse Barnes 
1085776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1086776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1087776ad806SJesse Barnes 
1088776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
10898664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
10908664281bSPaulo Zanoni 							  false))
10918664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
10928664281bSPaulo Zanoni 
10938664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
10948664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
10958664281bSPaulo Zanoni 							  false))
10968664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
10978664281bSPaulo Zanoni }
10988664281bSPaulo Zanoni 
10998664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
11008664281bSPaulo Zanoni {
11018664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
11028664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
11038664281bSPaulo Zanoni 
1104de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1105de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1106de032bf4SPaulo Zanoni 
11078664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_A)
11088664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
11098664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
11108664281bSPaulo Zanoni 
11118664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_B)
11128664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
11138664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
11148664281bSPaulo Zanoni 
11158664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_C)
11168664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
11178664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
11188664281bSPaulo Zanoni 
11198664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
11208664281bSPaulo Zanoni }
11218664281bSPaulo Zanoni 
11228664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
11238664281bSPaulo Zanoni {
11248664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
11258664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
11268664281bSPaulo Zanoni 
1127de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1128de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1129de032bf4SPaulo Zanoni 
11308664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
11318664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
11328664281bSPaulo Zanoni 							  false))
11338664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
11348664281bSPaulo Zanoni 
11358664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
11368664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
11378664281bSPaulo Zanoni 							  false))
11388664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
11398664281bSPaulo Zanoni 
11408664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
11418664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
11428664281bSPaulo Zanoni 							  false))
11438664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
11448664281bSPaulo Zanoni 
11458664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1146776ad806SJesse Barnes }
1147776ad806SJesse Barnes 
114823e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
114923e81d69SAdam Jackson {
115023e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
115123e81d69SAdam Jackson 	int pipe;
1152b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
115323e81d69SAdam Jackson 
115410a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
1155*91d131d2SDaniel Vetter 
1156cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1157cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
115823e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1159cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1160cfc33bf7SVille Syrjälä 				 port_name(port));
1161cfc33bf7SVille Syrjälä 	}
116223e81d69SAdam Jackson 
116323e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1164ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
116523e81d69SAdam Jackson 
116623e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1167515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
116823e81d69SAdam Jackson 
116923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
117023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
117123e81d69SAdam Jackson 
117223e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
117323e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
117423e81d69SAdam Jackson 
117523e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
117623e81d69SAdam Jackson 		for_each_pipe(pipe)
117723e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
117823e81d69SAdam Jackson 					 pipe_name(pipe),
117923e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
11808664281bSPaulo Zanoni 
11818664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
11828664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
118323e81d69SAdam Jackson }
118423e81d69SAdam Jackson 
1185ff1f525eSDaniel Vetter static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
1186b1f14ad0SJesse Barnes {
1187b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1188b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1189ab5c608bSBen Widawsky 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
11900e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
11910e43406bSChris Wilson 	int i;
1192b1f14ad0SJesse Barnes 
1193b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1194b1f14ad0SJesse Barnes 
11958664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
11968664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
11978664281bSPaulo Zanoni 	if (IS_HASWELL(dev) &&
11988664281bSPaulo Zanoni 	    (I915_READ_NOTRACE(FPGA_DBG) & FPGA_DBG_RM_NOCLAIM)) {
11998664281bSPaulo Zanoni 		DRM_ERROR("Unclaimed register before interrupt\n");
12008664281bSPaulo Zanoni 		I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
12018664281bSPaulo Zanoni 	}
12028664281bSPaulo Zanoni 
1203b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1204b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1205b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
12060e43406bSChris Wilson 
120744498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
120844498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
120944498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
121044498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
121144498aeaSPaulo Zanoni 	 * due to its back queue). */
1212ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
121344498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
121444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
121544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1216ab5c608bSBen Widawsky 	}
121744498aeaSPaulo Zanoni 
12188664281bSPaulo Zanoni 	/* On Haswell, also mask ERR_INT because we don't want to risk
12198664281bSPaulo Zanoni 	 * generating "unclaimed register" interrupts from inside the interrupt
12208664281bSPaulo Zanoni 	 * handler. */
12214bc9d430SDaniel Vetter 	if (IS_HASWELL(dev)) {
12224bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
12238664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
12244bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
12254bc9d430SDaniel Vetter 	}
12268664281bSPaulo Zanoni 
12270e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
12280e43406bSChris Wilson 	if (gt_iir) {
12290e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
12300e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
12310e43406bSChris Wilson 		ret = IRQ_HANDLED;
12320e43406bSChris Wilson 	}
1233b1f14ad0SJesse Barnes 
1234b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
12350e43406bSChris Wilson 	if (de_iir) {
12368664281bSPaulo Zanoni 		if (de_iir & DE_ERR_INT_IVB)
12378664281bSPaulo Zanoni 			ivb_err_int_handler(dev);
12388664281bSPaulo Zanoni 
1239ce99c256SDaniel Vetter 		if (de_iir & DE_AUX_CHANNEL_A_IVB)
1240ce99c256SDaniel Vetter 			dp_aux_irq_handler(dev);
1241ce99c256SDaniel Vetter 
1242b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
124381a07809SJani Nikula 			intel_opregion_asle_intr(dev);
1244b1f14ad0SJesse Barnes 
12450e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
124674d44445SDaniel Vetter 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
124774d44445SDaniel Vetter 				drm_handle_vblank(dev, i);
12480e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
12490e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
12500e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
1251b1f14ad0SJesse Barnes 			}
1252b1f14ad0SJesse Barnes 		}
1253b1f14ad0SJesse Barnes 
1254b1f14ad0SJesse Barnes 		/* check event from PCH */
1255ab5c608bSBen Widawsky 		if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
12560e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
12570e43406bSChris Wilson 
125823e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
12590e43406bSChris Wilson 
12600e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
12610e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
1262b1f14ad0SJesse Barnes 		}
1263b1f14ad0SJesse Barnes 
12640e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
12650e43406bSChris Wilson 		ret = IRQ_HANDLED;
12660e43406bSChris Wilson 	}
12670e43406bSChris Wilson 
12680e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
12690e43406bSChris Wilson 	if (pm_iir) {
1270baf02a1fSBen Widawsky 		if (IS_HASWELL(dev))
1271baf02a1fSBen Widawsky 			hsw_pm_irq_handler(dev_priv, pm_iir);
12724848405cSBen Widawsky 		else if (pm_iir & GEN6_PM_RPS_EVENTS)
1273fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
1274b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
12750e43406bSChris Wilson 		ret = IRQ_HANDLED;
12760e43406bSChris Wilson 	}
1277b1f14ad0SJesse Barnes 
12784bc9d430SDaniel Vetter 	if (IS_HASWELL(dev)) {
12794bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
12804bc9d430SDaniel Vetter 		if (ivb_can_enable_err_int(dev))
12818664281bSPaulo Zanoni 			ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
12824bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
12834bc9d430SDaniel Vetter 	}
12848664281bSPaulo Zanoni 
1285b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1286b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1287ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
128844498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
128944498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1290ab5c608bSBen Widawsky 	}
1291b1f14ad0SJesse Barnes 
1292b1f14ad0SJesse Barnes 	return ret;
1293b1f14ad0SJesse Barnes }
1294b1f14ad0SJesse Barnes 
1295e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
1296e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1297e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1298e7b4c6b1SDaniel Vetter {
1299cc609d5dSBen Widawsky 	if (gt_iir &
1300cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1301e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1302cc609d5dSBen Widawsky 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1303e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1304e7b4c6b1SDaniel Vetter }
1305e7b4c6b1SDaniel Vetter 
1306ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1307036a4a7dSZhenyu Wang {
13084697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1309036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1310036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
131144498aeaSPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
1312881f47b6SXiang, Haihao 
13134697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
13144697995bSJesse Barnes 
13152d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
13162d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
13172d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
13183143a2bfSChris Wilson 	POSTING_READ(DEIER);
13192d109a84SZou, Nanhai 
132044498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
132144498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
132244498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
132344498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
132444498aeaSPaulo Zanoni 	 * due to its back queue). */
132544498aeaSPaulo Zanoni 	sde_ier = I915_READ(SDEIER);
132644498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, 0);
132744498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
132844498aeaSPaulo Zanoni 
1329036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
1330036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
13313b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
1332036a4a7dSZhenyu Wang 
1333acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
1334c7c85101SZou Nan hai 		goto done;
1335036a4a7dSZhenyu Wang 
1336036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
1337036a4a7dSZhenyu Wang 
1338e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
1339e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
1340e7b4c6b1SDaniel Vetter 	else
1341e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1342036a4a7dSZhenyu Wang 
1343ce99c256SDaniel Vetter 	if (de_iir & DE_AUX_CHANNEL_A)
1344ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1345ce99c256SDaniel Vetter 
134601c66889SZhao Yakui 	if (de_iir & DE_GSE)
134781a07809SJani Nikula 		intel_opregion_asle_intr(dev);
134801c66889SZhao Yakui 
134974d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
135074d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
135174d44445SDaniel Vetter 
135274d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
135374d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
135474d44445SDaniel Vetter 
1355de032bf4SPaulo Zanoni 	if (de_iir & DE_POISON)
1356de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1357de032bf4SPaulo Zanoni 
13588664281bSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
13598664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
13608664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
13618664281bSPaulo Zanoni 
13628664281bSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
13638664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
13648664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
13658664281bSPaulo Zanoni 
1366f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1367013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
13682bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
1369013d5aa2SJesse Barnes 	}
1370013d5aa2SJesse Barnes 
1371f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1372f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
13732bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
1374013d5aa2SJesse Barnes 	}
1375c062df61SLi Peng 
1376c650156aSZhenyu Wang 	/* check event from PCH */
1377776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
1378acd15b6cSDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
1379acd15b6cSDaniel Vetter 
138023e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
138123e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
138223e81d69SAdam Jackson 		else
138323e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
1384acd15b6cSDaniel Vetter 
1385acd15b6cSDaniel Vetter 		/* should clear PCH hotplug event before clear CPU irq */
1386acd15b6cSDaniel Vetter 		I915_WRITE(SDEIIR, pch_iir);
1387776ad806SJesse Barnes 	}
1388c650156aSZhenyu Wang 
138973edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
139073edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
1391f97108d1SJesse Barnes 
13924848405cSBen Widawsky 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_RPS_EVENTS)
1393fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
13943b8d8d91SJesse Barnes 
1395c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
1396c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
13974912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
1398036a4a7dSZhenyu Wang 
1399c7c85101SZou Nan hai done:
14002d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
14013143a2bfSChris Wilson 	POSTING_READ(DEIER);
140244498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, sde_ier);
140344498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
14042d109a84SZou, Nanhai 
1405036a4a7dSZhenyu Wang 	return ret;
1406036a4a7dSZhenyu Wang }
1407036a4a7dSZhenyu Wang 
14088a905236SJesse Barnes /**
14098a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
14108a905236SJesse Barnes  * @work: work struct
14118a905236SJesse Barnes  *
14128a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
14138a905236SJesse Barnes  * was detected.
14148a905236SJesse Barnes  */
14158a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
14168a905236SJesse Barnes {
14171f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
14181f83fee0SDaniel Vetter 						    work);
14191f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
14201f83fee0SDaniel Vetter 						    gpu_error);
14218a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1422f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
1423f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
1424f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
1425f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
1426f69061beSDaniel Vetter 	int i, ret;
14278a905236SJesse Barnes 
1428f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
14298a905236SJesse Barnes 
14307db0ba24SDaniel Vetter 	/*
14317db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
14327db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
14337db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
14347db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
14357db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
14367db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
14377db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
14387db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
14397db0ba24SDaniel Vetter 	 */
14407db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
144144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
14427db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
14437db0ba24SDaniel Vetter 				   reset_event);
14441f83fee0SDaniel Vetter 
1445f69061beSDaniel Vetter 		ret = i915_reset(dev);
1446f69061beSDaniel Vetter 
1447f69061beSDaniel Vetter 		if (ret == 0) {
1448f69061beSDaniel Vetter 			/*
1449f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1450f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1451f69061beSDaniel Vetter 			 * complete.
1452f69061beSDaniel Vetter 			 *
1453f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1454f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1455f69061beSDaniel Vetter 			 * updates before
1456f69061beSDaniel Vetter 			 * the counter increment.
1457f69061beSDaniel Vetter 			 */
1458f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1459f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1460f69061beSDaniel Vetter 
1461f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1462f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
14631f83fee0SDaniel Vetter 		} else {
14641f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1465f316a42cSBen Gamari 		}
14661f83fee0SDaniel Vetter 
1467f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
1468f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
1469f69061beSDaniel Vetter 
147096a02917SVille Syrjälä 		intel_display_handle_reset(dev);
147196a02917SVille Syrjälä 
14721f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
1473f316a42cSBen Gamari 	}
14748a905236SJesse Barnes }
14758a905236SJesse Barnes 
147685f9e50dSDaniel Vetter /* NB: please notice the memset */
147785f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
147885f9e50dSDaniel Vetter 				    uint32_t *instdone)
147985f9e50dSDaniel Vetter {
148085f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
148185f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
148285f9e50dSDaniel Vetter 
148385f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
148485f9e50dSDaniel Vetter 	case 2:
148585f9e50dSDaniel Vetter 	case 3:
148685f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
148785f9e50dSDaniel Vetter 		break;
148885f9e50dSDaniel Vetter 	case 4:
148985f9e50dSDaniel Vetter 	case 5:
149085f9e50dSDaniel Vetter 	case 6:
149185f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
149285f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
149385f9e50dSDaniel Vetter 		break;
149485f9e50dSDaniel Vetter 	default:
149585f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
149685f9e50dSDaniel Vetter 	case 7:
149785f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
149885f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
149985f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
150085f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
150185f9e50dSDaniel Vetter 		break;
150285f9e50dSDaniel Vetter 	}
150385f9e50dSDaniel Vetter }
150485f9e50dSDaniel Vetter 
15053bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
15069df30794SChris Wilson static struct drm_i915_error_object *
1507d0d045e8SBen Widawsky i915_error_object_create_sized(struct drm_i915_private *dev_priv,
1508d0d045e8SBen Widawsky 			       struct drm_i915_gem_object *src,
1509d0d045e8SBen Widawsky 			       const int num_pages)
15109df30794SChris Wilson {
15119df30794SChris Wilson 	struct drm_i915_error_object *dst;
1512d0d045e8SBen Widawsky 	int i;
1513e56660ddSChris Wilson 	u32 reloc_offset;
15149df30794SChris Wilson 
151505394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
15169df30794SChris Wilson 		return NULL;
15179df30794SChris Wilson 
1518d0d045e8SBen Widawsky 	dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
15199df30794SChris Wilson 	if (dst == NULL)
15209df30794SChris Wilson 		return NULL;
15219df30794SChris Wilson 
152205394f39SChris Wilson 	reloc_offset = src->gtt_offset;
1523d0d045e8SBen Widawsky 	for (i = 0; i < num_pages; i++) {
1524788885aeSAndrew Morton 		unsigned long flags;
1525e56660ddSChris Wilson 		void *d;
1526788885aeSAndrew Morton 
1527e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
15289df30794SChris Wilson 		if (d == NULL)
15299df30794SChris Wilson 			goto unwind;
1530e56660ddSChris Wilson 
1531788885aeSAndrew Morton 		local_irq_save(flags);
15325d4545aeSBen Widawsky 		if (reloc_offset < dev_priv->gtt.mappable_end &&
153374898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
1534172975aaSChris Wilson 			void __iomem *s;
1535172975aaSChris Wilson 
1536172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
1537172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
1538172975aaSChris Wilson 			 * captures what the GPU read.
1539172975aaSChris Wilson 			 */
1540172975aaSChris Wilson 
15415d4545aeSBen Widawsky 			s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
15423e4d3af5SPeter Zijlstra 						     reloc_offset);
1543e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
15443e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
1545960e3564SChris Wilson 		} else if (src->stolen) {
1546960e3564SChris Wilson 			unsigned long offset;
1547960e3564SChris Wilson 
1548960e3564SChris Wilson 			offset = dev_priv->mm.stolen_base;
1549960e3564SChris Wilson 			offset += src->stolen->start;
1550960e3564SChris Wilson 			offset += i << PAGE_SHIFT;
1551960e3564SChris Wilson 
15521a240d4dSDaniel Vetter 			memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
1553172975aaSChris Wilson 		} else {
15549da3da66SChris Wilson 			struct page *page;
1555172975aaSChris Wilson 			void *s;
1556172975aaSChris Wilson 
15579da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
1558172975aaSChris Wilson 
15599da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
15609da3da66SChris Wilson 
15619da3da66SChris Wilson 			s = kmap_atomic(page);
1562172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
1563172975aaSChris Wilson 			kunmap_atomic(s);
1564172975aaSChris Wilson 
15659da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
1566172975aaSChris Wilson 		}
1567788885aeSAndrew Morton 		local_irq_restore(flags);
1568e56660ddSChris Wilson 
15699da3da66SChris Wilson 		dst->pages[i] = d;
1570e56660ddSChris Wilson 
1571e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
15729df30794SChris Wilson 	}
1573d0d045e8SBen Widawsky 	dst->page_count = num_pages;
157405394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
15759df30794SChris Wilson 
15769df30794SChris Wilson 	return dst;
15779df30794SChris Wilson 
15789df30794SChris Wilson unwind:
15799da3da66SChris Wilson 	while (i--)
15809da3da66SChris Wilson 		kfree(dst->pages[i]);
15819df30794SChris Wilson 	kfree(dst);
15829df30794SChris Wilson 	return NULL;
15839df30794SChris Wilson }
1584d0d045e8SBen Widawsky #define i915_error_object_create(dev_priv, src) \
1585d0d045e8SBen Widawsky 	i915_error_object_create_sized((dev_priv), (src), \
1586d0d045e8SBen Widawsky 				       (src)->base.size>>PAGE_SHIFT)
15879df30794SChris Wilson 
15889df30794SChris Wilson static void
15899df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
15909df30794SChris Wilson {
15919df30794SChris Wilson 	int page;
15929df30794SChris Wilson 
15939df30794SChris Wilson 	if (obj == NULL)
15949df30794SChris Wilson 		return;
15959df30794SChris Wilson 
15969df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
15979df30794SChris Wilson 		kfree(obj->pages[page]);
15989df30794SChris Wilson 
15999df30794SChris Wilson 	kfree(obj);
16009df30794SChris Wilson }
16019df30794SChris Wilson 
1602742cbee8SDaniel Vetter void
1603742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
16049df30794SChris Wilson {
1605742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
1606742cbee8SDaniel Vetter 							  typeof(*error), ref);
1607e2f973d5SChris Wilson 	int i;
1608e2f973d5SChris Wilson 
160952d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
161052d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
161152d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
16127ed73da0SBen Widawsky 		i915_error_object_free(error->ring[i].ctx);
161352d39a21SChris Wilson 		kfree(error->ring[i].requests);
161452d39a21SChris Wilson 	}
1615e2f973d5SChris Wilson 
16169df30794SChris Wilson 	kfree(error->active_bo);
16176ef3d427SChris Wilson 	kfree(error->overlay);
16187ed73da0SBen Widawsky 	kfree(error->display);
16199df30794SChris Wilson 	kfree(error);
16209df30794SChris Wilson }
16211b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
16221b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
1623c724e8a9SChris Wilson {
1624c724e8a9SChris Wilson 	err->size = obj->base.size;
1625c724e8a9SChris Wilson 	err->name = obj->base.name;
16260201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
16270201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1628c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
1629c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1630c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1631c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1632c724e8a9SChris Wilson 	err->pinned = 0;
1633c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1634c724e8a9SChris Wilson 		err->pinned = 1;
1635c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1636c724e8a9SChris Wilson 		err->pinned = -1;
1637c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1638c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1639c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
164096154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
164193dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
16421b50247aSChris Wilson }
1643c724e8a9SChris Wilson 
16441b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
16451b50247aSChris Wilson 			     int count, struct list_head *head)
16461b50247aSChris Wilson {
16471b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
16481b50247aSChris Wilson 	int i = 0;
16491b50247aSChris Wilson 
16501b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
16511b50247aSChris Wilson 		capture_bo(err++, obj);
1652c724e8a9SChris Wilson 		if (++i == count)
1653c724e8a9SChris Wilson 			break;
16541b50247aSChris Wilson 	}
1655c724e8a9SChris Wilson 
16561b50247aSChris Wilson 	return i;
16571b50247aSChris Wilson }
16581b50247aSChris Wilson 
16591b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
16601b50247aSChris Wilson 			     int count, struct list_head *head)
16611b50247aSChris Wilson {
16621b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
16631b50247aSChris Wilson 	int i = 0;
16641b50247aSChris Wilson 
166535c20a60SBen Widawsky 	list_for_each_entry(obj, head, global_list) {
16661b50247aSChris Wilson 		if (obj->pin_count == 0)
16671b50247aSChris Wilson 			continue;
16681b50247aSChris Wilson 
16691b50247aSChris Wilson 		capture_bo(err++, obj);
16701b50247aSChris Wilson 		if (++i == count)
16711b50247aSChris Wilson 			break;
1672c724e8a9SChris Wilson 	}
1673c724e8a9SChris Wilson 
1674c724e8a9SChris Wilson 	return i;
1675c724e8a9SChris Wilson }
1676c724e8a9SChris Wilson 
1677748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1678748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1679748ebc60SChris Wilson {
1680748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1681748ebc60SChris Wilson 	int i;
1682748ebc60SChris Wilson 
1683748ebc60SChris Wilson 	/* Fences */
1684748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1685775d17b6SDaniel Vetter 	case 7:
1686748ebc60SChris Wilson 	case 6:
168742b5aeabSVille Syrjälä 		for (i = 0; i < dev_priv->num_fence_regs; i++)
1688748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1689748ebc60SChris Wilson 		break;
1690748ebc60SChris Wilson 	case 5:
1691748ebc60SChris Wilson 	case 4:
1692748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1693748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1694748ebc60SChris Wilson 		break;
1695748ebc60SChris Wilson 	case 3:
1696748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1697748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1698748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1699748ebc60SChris Wilson 	case 2:
1700748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1701748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1702748ebc60SChris Wilson 		break;
1703748ebc60SChris Wilson 
17047dbf9d6eSBen Widawsky 	default:
17057dbf9d6eSBen Widawsky 		BUG();
1706748ebc60SChris Wilson 	}
1707748ebc60SChris Wilson }
1708748ebc60SChris Wilson 
1709bcfb2e28SChris Wilson static struct drm_i915_error_object *
1710bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1711bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1712bcfb2e28SChris Wilson {
1713bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1714bcfb2e28SChris Wilson 	u32 seqno;
1715bcfb2e28SChris Wilson 
1716bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1717bcfb2e28SChris Wilson 		return NULL;
1718bcfb2e28SChris Wilson 
1719b45305fcSDaniel Vetter 	if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1720b45305fcSDaniel Vetter 		u32 acthd = I915_READ(ACTHD);
1721b45305fcSDaniel Vetter 
1722b45305fcSDaniel Vetter 		if (WARN_ON(ring->id != RCS))
1723b45305fcSDaniel Vetter 			return NULL;
1724b45305fcSDaniel Vetter 
1725b45305fcSDaniel Vetter 		obj = ring->private;
1726b45305fcSDaniel Vetter 		if (acthd >= obj->gtt_offset &&
1727b45305fcSDaniel Vetter 		    acthd < obj->gtt_offset + obj->base.size)
1728b45305fcSDaniel Vetter 			return i915_error_object_create(dev_priv, obj);
1729b45305fcSDaniel Vetter 	}
1730b45305fcSDaniel Vetter 
1731b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1732bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1733bcfb2e28SChris Wilson 		if (obj->ring != ring)
1734bcfb2e28SChris Wilson 			continue;
1735bcfb2e28SChris Wilson 
17360201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1737bcfb2e28SChris Wilson 			continue;
1738bcfb2e28SChris Wilson 
1739bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1740bcfb2e28SChris Wilson 			continue;
1741bcfb2e28SChris Wilson 
1742bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1743bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1744bcfb2e28SChris Wilson 		 */
1745bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1746bcfb2e28SChris Wilson 	}
1747bcfb2e28SChris Wilson 
1748bcfb2e28SChris Wilson 	return NULL;
1749bcfb2e28SChris Wilson }
1750bcfb2e28SChris Wilson 
1751d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1752d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1753d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1754d27b1e0eSDaniel Vetter {
1755d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1756d27b1e0eSDaniel Vetter 
175733f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
175812f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
175933f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
17607e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
17617e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
17627e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
17637e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
1764df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1765df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
176633f3f518SDaniel Vetter 	}
1767c1cd90edSDaniel Vetter 
1768d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
17699d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1770d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1771d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1772d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1773c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1774050ee91fSBen Widawsky 		if (ring->id == RCS)
1775d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1776d27b1e0eSDaniel Vetter 	} else {
17779d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1778d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1779d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1780d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1781d27b1e0eSDaniel Vetter 	}
1782d27b1e0eSDaniel Vetter 
17839574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1784c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1785b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1786d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1787c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1788c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
17890f3b6849SChris Wilson 	error->ctl[ring->id] = I915_READ_CTL(ring);
17907e3b8737SDaniel Vetter 
17917e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
17927e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1793d27b1e0eSDaniel Vetter }
1794d27b1e0eSDaniel Vetter 
17958c123e54SBen Widawsky 
17968c123e54SBen Widawsky static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
17978c123e54SBen Widawsky 					   struct drm_i915_error_state *error,
17988c123e54SBen Widawsky 					   struct drm_i915_error_ring *ering)
17998c123e54SBen Widawsky {
18008c123e54SBen Widawsky 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
18018c123e54SBen Widawsky 	struct drm_i915_gem_object *obj;
18028c123e54SBen Widawsky 
18038c123e54SBen Widawsky 	/* Currently render ring is the only HW context user */
18048c123e54SBen Widawsky 	if (ring->id != RCS || !error->ccid)
18058c123e54SBen Widawsky 		return;
18068c123e54SBen Widawsky 
180735c20a60SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
18088c123e54SBen Widawsky 		if ((error->ccid & PAGE_MASK) == obj->gtt_offset) {
18098c123e54SBen Widawsky 			ering->ctx = i915_error_object_create_sized(dev_priv,
18108c123e54SBen Widawsky 								    obj, 1);
18118c123e54SBen Widawsky 		}
18128c123e54SBen Widawsky 	}
18138c123e54SBen Widawsky }
18148c123e54SBen Widawsky 
181552d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
181652d39a21SChris Wilson 				  struct drm_i915_error_state *error)
181752d39a21SChris Wilson {
181852d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1819b4519513SChris Wilson 	struct intel_ring_buffer *ring;
182052d39a21SChris Wilson 	struct drm_i915_gem_request *request;
182152d39a21SChris Wilson 	int i, count;
182252d39a21SChris Wilson 
1823b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
182452d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
182552d39a21SChris Wilson 
182652d39a21SChris Wilson 		error->ring[i].batchbuffer =
182752d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
182852d39a21SChris Wilson 
182952d39a21SChris Wilson 		error->ring[i].ringbuffer =
183052d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
183152d39a21SChris Wilson 
18328c123e54SBen Widawsky 
18338c123e54SBen Widawsky 		i915_gem_record_active_context(ring, error, &error->ring[i]);
18348c123e54SBen Widawsky 
183552d39a21SChris Wilson 		count = 0;
183652d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
183752d39a21SChris Wilson 			count++;
183852d39a21SChris Wilson 
183952d39a21SChris Wilson 		error->ring[i].num_requests = count;
184052d39a21SChris Wilson 		error->ring[i].requests =
184152d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
184252d39a21SChris Wilson 				GFP_ATOMIC);
184352d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
184452d39a21SChris Wilson 			error->ring[i].num_requests = 0;
184552d39a21SChris Wilson 			continue;
184652d39a21SChris Wilson 		}
184752d39a21SChris Wilson 
184852d39a21SChris Wilson 		count = 0;
184952d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
185052d39a21SChris Wilson 			struct drm_i915_error_request *erq;
185152d39a21SChris Wilson 
185252d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
185352d39a21SChris Wilson 			erq->seqno = request->seqno;
185452d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1855ee4f42b1SChris Wilson 			erq->tail = request->tail;
185652d39a21SChris Wilson 		}
185752d39a21SChris Wilson 	}
185852d39a21SChris Wilson }
185952d39a21SChris Wilson 
18608a905236SJesse Barnes /**
18618a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
18628a905236SJesse Barnes  * @dev: drm device
18638a905236SJesse Barnes  *
18648a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
18658a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
18668a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
18678a905236SJesse Barnes  * to pick up.
18688a905236SJesse Barnes  */
186963eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
187063eeaf38SJesse Barnes {
187163eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
187205394f39SChris Wilson 	struct drm_i915_gem_object *obj;
187363eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
187463eeaf38SJesse Barnes 	unsigned long flags;
18759db4a9c7SJesse Barnes 	int i, pipe;
187663eeaf38SJesse Barnes 
187799584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
187899584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
187999584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
18809df30794SChris Wilson 	if (error)
18819df30794SChris Wilson 		return;
188263eeaf38SJesse Barnes 
18839db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
188433f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
188563eeaf38SJesse Barnes 	if (!error) {
18869df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
18879df30794SChris Wilson 		return;
188863eeaf38SJesse Barnes 	}
188963eeaf38SJesse Barnes 
18902f86f191SBen Widawsky 	DRM_INFO("capturing error event; look for more information in "
18912f86f191SBen Widawsky 		 "/sys/kernel/debug/dri/%d/i915_error_state\n",
1892b6f7833bSChris Wilson 		 dev->primary->index);
18932fa772f3SChris Wilson 
1894742cbee8SDaniel Vetter 	kref_init(&error->ref);
189563eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
189663eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1897211816ecSBen Widawsky 	if (HAS_HW_CONTEXTS(dev))
1898b9a3906bSBen Widawsky 		error->ccid = I915_READ(CCID);
1899be998e2eSBen Widawsky 
1900be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1901be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1902be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1903be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1904be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1905be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1906be998e2eSBen Widawsky 	else
1907be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1908be998e2eSBen Widawsky 
19090f3b6849SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6)
19100f3b6849SChris Wilson 		error->derrmr = I915_READ(DERRMR);
19110f3b6849SChris Wilson 
19120f3b6849SChris Wilson 	if (IS_VALLEYVIEW(dev))
19130f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_VLV);
19140f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 7)
19150f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_MT);
19160f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen == 6)
19170f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE);
19180f3b6849SChris Wilson 
19194f3308b9SPaulo Zanoni 	if (!HAS_PCH_SPLIT(dev))
19209db4a9c7SJesse Barnes 		for_each_pipe(pipe)
19219db4a9c7SJesse Barnes 			error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1922d27b1e0eSDaniel Vetter 
192333f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1924f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
192533f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
192633f3f518SDaniel Vetter 	}
1927add354ddSChris Wilson 
192871e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
192971e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
193071e172e8SBen Widawsky 
1931050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1932050ee91fSBen Widawsky 
1933748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
193452d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
19359df30794SChris Wilson 
1936c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
19379df30794SChris Wilson 	error->active_bo = NULL;
1938c724e8a9SChris Wilson 	error->pinned_bo = NULL;
19399df30794SChris Wilson 
1940bcfb2e28SChris Wilson 	i = 0;
1941bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1942bcfb2e28SChris Wilson 		i++;
1943bcfb2e28SChris Wilson 	error->active_bo_count = i;
194435c20a60SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
19451b50247aSChris Wilson 		if (obj->pin_count)
1946bcfb2e28SChris Wilson 			i++;
1947bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1948c724e8a9SChris Wilson 
19498e934dbfSChris Wilson 	error->active_bo = NULL;
19508e934dbfSChris Wilson 	error->pinned_bo = NULL;
1951bcfb2e28SChris Wilson 	if (i) {
1952bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
19539df30794SChris Wilson 					   GFP_ATOMIC);
1954c724e8a9SChris Wilson 		if (error->active_bo)
1955c724e8a9SChris Wilson 			error->pinned_bo =
1956c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
19579df30794SChris Wilson 	}
1958c724e8a9SChris Wilson 
1959c724e8a9SChris Wilson 	if (error->active_bo)
1960c724e8a9SChris Wilson 		error->active_bo_count =
19611b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1962c724e8a9SChris Wilson 					  error->active_bo_count,
1963c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1964c724e8a9SChris Wilson 
1965c724e8a9SChris Wilson 	if (error->pinned_bo)
1966c724e8a9SChris Wilson 		error->pinned_bo_count =
19671b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1968c724e8a9SChris Wilson 					  error->pinned_bo_count,
19696c085a72SChris Wilson 					  &dev_priv->mm.bound_list);
197063eeaf38SJesse Barnes 
19718a905236SJesse Barnes 	do_gettimeofday(&error->time);
19728a905236SJesse Barnes 
19736ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1974c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
19756ef3d427SChris Wilson 
197699584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
197799584db3SDaniel Vetter 	if (dev_priv->gpu_error.first_error == NULL) {
197899584db3SDaniel Vetter 		dev_priv->gpu_error.first_error = error;
19799df30794SChris Wilson 		error = NULL;
19809df30794SChris Wilson 	}
198199584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19829df30794SChris Wilson 
19839df30794SChris Wilson 	if (error)
1984742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
19859df30794SChris Wilson }
19869df30794SChris Wilson 
19879df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
19889df30794SChris Wilson {
19899df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
19909df30794SChris Wilson 	struct drm_i915_error_state *error;
19916dc0e816SBen Widawsky 	unsigned long flags;
19929df30794SChris Wilson 
199399584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
199499584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
199599584db3SDaniel Vetter 	dev_priv->gpu_error.first_error = NULL;
199699584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19979df30794SChris Wilson 
19989df30794SChris Wilson 	if (error)
1999742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
200063eeaf38SJesse Barnes }
20013bd3c932SChris Wilson #else
20023bd3c932SChris Wilson #define i915_capture_error_state(x)
20033bd3c932SChris Wilson #endif
200463eeaf38SJesse Barnes 
200535aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2006c0e09200SDave Airlie {
20078a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2008bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
200963eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2010050ee91fSBen Widawsky 	int pipe, i;
201163eeaf38SJesse Barnes 
201235aed2e6SChris Wilson 	if (!eir)
201335aed2e6SChris Wilson 		return;
201463eeaf38SJesse Barnes 
2015a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
20168a905236SJesse Barnes 
2017bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2018bd9854f9SBen Widawsky 
20198a905236SJesse Barnes 	if (IS_G4X(dev)) {
20208a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
20218a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
20228a905236SJesse Barnes 
2023a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2024a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2025050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2026050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2027a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2028a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
20298a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20303143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
20318a905236SJesse Barnes 		}
20328a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
20338a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2034a70491ccSJoe Perches 			pr_err("page table error\n");
2035a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
20368a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20373143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
20388a905236SJesse Barnes 		}
20398a905236SJesse Barnes 	}
20408a905236SJesse Barnes 
2041a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
204263eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
204363eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2044a70491ccSJoe Perches 			pr_err("page table error\n");
2045a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
204663eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20473143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
204863eeaf38SJesse Barnes 		}
20498a905236SJesse Barnes 	}
20508a905236SJesse Barnes 
205163eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2052a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
20539db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2054a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
20559db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
205663eeaf38SJesse Barnes 		/* pipestat has already been acked */
205763eeaf38SJesse Barnes 	}
205863eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2059a70491ccSJoe Perches 		pr_err("instruction error\n");
2060a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2061050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2062050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2063a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
206463eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
206563eeaf38SJesse Barnes 
2066a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2067a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2068a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
206963eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
20703143a2bfSChris Wilson 			POSTING_READ(IPEIR);
207163eeaf38SJesse Barnes 		} else {
207263eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
207363eeaf38SJesse Barnes 
2074a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2075a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2076a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2077a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
207863eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20793143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
208063eeaf38SJesse Barnes 		}
208163eeaf38SJesse Barnes 	}
208263eeaf38SJesse Barnes 
208363eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
20843143a2bfSChris Wilson 	POSTING_READ(EIR);
208563eeaf38SJesse Barnes 	eir = I915_READ(EIR);
208663eeaf38SJesse Barnes 	if (eir) {
208763eeaf38SJesse Barnes 		/*
208863eeaf38SJesse Barnes 		 * some errors might have become stuck,
208963eeaf38SJesse Barnes 		 * mask them.
209063eeaf38SJesse Barnes 		 */
209163eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
209263eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
209363eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
209463eeaf38SJesse Barnes 	}
209535aed2e6SChris Wilson }
209635aed2e6SChris Wilson 
209735aed2e6SChris Wilson /**
209835aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
209935aed2e6SChris Wilson  * @dev: drm device
210035aed2e6SChris Wilson  *
210135aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
210235aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
210335aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
210435aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
210535aed2e6SChris Wilson  * of a ring dump etc.).
210635aed2e6SChris Wilson  */
2107527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
210835aed2e6SChris Wilson {
210935aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2110b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2111b4519513SChris Wilson 	int i;
211235aed2e6SChris Wilson 
211335aed2e6SChris Wilson 	i915_capture_error_state(dev);
211435aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
21158a905236SJesse Barnes 
2116ba1234d1SBen Gamari 	if (wedged) {
2117f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2118f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2119ba1234d1SBen Gamari 
212011ed50ecSBen Gamari 		/*
21211f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
21221f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
212311ed50ecSBen Gamari 		 */
2124b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
2125b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
212611ed50ecSBen Gamari 	}
212711ed50ecSBen Gamari 
212899584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
21298a905236SJesse Barnes }
21308a905236SJesse Barnes 
213121ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
21324e5359cdSSimon Farnsworth {
21334e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
21344e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
21354e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
213605394f39SChris Wilson 	struct drm_i915_gem_object *obj;
21374e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
21384e5359cdSSimon Farnsworth 	unsigned long flags;
21394e5359cdSSimon Farnsworth 	bool stall_detected;
21404e5359cdSSimon Farnsworth 
21414e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
21424e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
21434e5359cdSSimon Farnsworth 		return;
21444e5359cdSSimon Farnsworth 
21454e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
21464e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
21474e5359cdSSimon Farnsworth 
2148e7d841caSChris Wilson 	if (work == NULL ||
2149e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2150e7d841caSChris Wilson 	    !work->enable_stall_check) {
21514e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
21524e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
21534e5359cdSSimon Farnsworth 		return;
21544e5359cdSSimon Farnsworth 	}
21554e5359cdSSimon Farnsworth 
21564e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
215705394f39SChris Wilson 	obj = work->pending_flip_obj;
2158a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
21599db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2160446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2161446f2545SArmin Reese 					obj->gtt_offset;
21624e5359cdSSimon Farnsworth 	} else {
21639db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
216405394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
216501f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
21664e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
21674e5359cdSSimon Farnsworth 	}
21684e5359cdSSimon Farnsworth 
21694e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
21704e5359cdSSimon Farnsworth 
21714e5359cdSSimon Farnsworth 	if (stall_detected) {
21724e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
21734e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
21744e5359cdSSimon Farnsworth 	}
21754e5359cdSSimon Farnsworth }
21764e5359cdSSimon Farnsworth 
217742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
217842f52ef8SKeith Packard  * we use as a pipe index
217942f52ef8SKeith Packard  */
2180f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
21810a3e67a4SJesse Barnes {
21820a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2183e9d21d7fSKeith Packard 	unsigned long irqflags;
218471e0ffa5SJesse Barnes 
21855eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
218671e0ffa5SJesse Barnes 		return -EINVAL;
21870a3e67a4SJesse Barnes 
21881ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2189f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
21907c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21917c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
21920a3e67a4SJesse Barnes 	else
21937c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21947c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
21958692d00eSChris Wilson 
21968692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
21978692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
21986b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
21991ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22008692d00eSChris Wilson 
22010a3e67a4SJesse Barnes 	return 0;
22020a3e67a4SJesse Barnes }
22030a3e67a4SJesse Barnes 
2204f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2205f796cf8fSJesse Barnes {
2206f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2207f796cf8fSJesse Barnes 	unsigned long irqflags;
2208f796cf8fSJesse Barnes 
2209f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2210f796cf8fSJesse Barnes 		return -EINVAL;
2211f796cf8fSJesse Barnes 
2212f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2213f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
2214f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2215f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2216f796cf8fSJesse Barnes 
2217f796cf8fSJesse Barnes 	return 0;
2218f796cf8fSJesse Barnes }
2219f796cf8fSJesse Barnes 
2220f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
2221b1f14ad0SJesse Barnes {
2222b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2223b1f14ad0SJesse Barnes 	unsigned long irqflags;
2224b1f14ad0SJesse Barnes 
2225b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2226b1f14ad0SJesse Barnes 		return -EINVAL;
2227b1f14ad0SJesse Barnes 
2228b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2229b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
2230b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
2231b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2232b1f14ad0SJesse Barnes 
2233b1f14ad0SJesse Barnes 	return 0;
2234b1f14ad0SJesse Barnes }
2235b1f14ad0SJesse Barnes 
22367e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
22377e231dbeSJesse Barnes {
22387e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22397e231dbeSJesse Barnes 	unsigned long irqflags;
224031acc7f5SJesse Barnes 	u32 imr;
22417e231dbeSJesse Barnes 
22427e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
22437e231dbeSJesse Barnes 		return -EINVAL;
22447e231dbeSJesse Barnes 
22457e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22467e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
224731acc7f5SJesse Barnes 	if (pipe == 0)
22487e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
224931acc7f5SJesse Barnes 	else
22507e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22517e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
225231acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
225331acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
22547e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22557e231dbeSJesse Barnes 
22567e231dbeSJesse Barnes 	return 0;
22577e231dbeSJesse Barnes }
22587e231dbeSJesse Barnes 
225942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
226042f52ef8SKeith Packard  * we use as a pipe index
226142f52ef8SKeith Packard  */
2262f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
22630a3e67a4SJesse Barnes {
22640a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2265e9d21d7fSKeith Packard 	unsigned long irqflags;
22660a3e67a4SJesse Barnes 
22671ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22688692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
22696b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
22708692d00eSChris Wilson 
22717c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
22727c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
22737c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
22741ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22750a3e67a4SJesse Barnes }
22760a3e67a4SJesse Barnes 
2277f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2278f796cf8fSJesse Barnes {
2279f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2280f796cf8fSJesse Barnes 	unsigned long irqflags;
2281f796cf8fSJesse Barnes 
2282f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2283f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
2284f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2285f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2286f796cf8fSJesse Barnes }
2287f796cf8fSJesse Barnes 
2288f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
2289b1f14ad0SJesse Barnes {
2290b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2291b1f14ad0SJesse Barnes 	unsigned long irqflags;
2292b1f14ad0SJesse Barnes 
2293b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2294b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
2295b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
2296b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2297b1f14ad0SJesse Barnes }
2298b1f14ad0SJesse Barnes 
22997e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
23007e231dbeSJesse Barnes {
23017e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23027e231dbeSJesse Barnes 	unsigned long irqflags;
230331acc7f5SJesse Barnes 	u32 imr;
23047e231dbeSJesse Barnes 
23057e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
230631acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
230731acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
23087e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
230931acc7f5SJesse Barnes 	if (pipe == 0)
23107e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
231131acc7f5SJesse Barnes 	else
23127e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23137e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
23147e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
23157e231dbeSJesse Barnes }
23167e231dbeSJesse Barnes 
2317893eead0SChris Wilson static u32
2318893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2319852835f3SZou Nan hai {
2320893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2321893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2322893eead0SChris Wilson }
2323893eead0SChris Wilson 
23249107e9d2SChris Wilson static bool
23259107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2326893eead0SChris Wilson {
23279107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
23289107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2329f65d9421SBen Gamari }
2330f65d9421SBen Gamari 
23316274f212SChris Wilson static struct intel_ring_buffer *
23326274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2333a24a11e6SChris Wilson {
2334a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
23356274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2336a24a11e6SChris Wilson 
2337a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2338a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2339a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
23406274f212SChris Wilson 		return NULL;
2341a24a11e6SChris Wilson 
2342a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2343a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2344a24a11e6SChris Wilson 	 */
23456274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2346a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2347a24a11e6SChris Wilson 	do {
2348a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2349a24a11e6SChris Wilson 		if (cmd == ipehr)
2350a24a11e6SChris Wilson 			break;
2351a24a11e6SChris Wilson 
2352a24a11e6SChris Wilson 		acthd -= 4;
2353a24a11e6SChris Wilson 		if (acthd < acthd_min)
23546274f212SChris Wilson 			return NULL;
2355a24a11e6SChris Wilson 	} while (1);
2356a24a11e6SChris Wilson 
23576274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
23586274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2359a24a11e6SChris Wilson }
2360a24a11e6SChris Wilson 
23616274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
23626274f212SChris Wilson {
23636274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
23646274f212SChris Wilson 	struct intel_ring_buffer *signaller;
23656274f212SChris Wilson 	u32 seqno, ctl;
23666274f212SChris Wilson 
23676274f212SChris Wilson 	ring->hangcheck.deadlock = true;
23686274f212SChris Wilson 
23696274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
23706274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
23716274f212SChris Wilson 		return -1;
23726274f212SChris Wilson 
23736274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
23746274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
23756274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
23766274f212SChris Wilson 		return -1;
23776274f212SChris Wilson 
23786274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
23796274f212SChris Wilson }
23806274f212SChris Wilson 
23816274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
23826274f212SChris Wilson {
23836274f212SChris Wilson 	struct intel_ring_buffer *ring;
23846274f212SChris Wilson 	int i;
23856274f212SChris Wilson 
23866274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
23876274f212SChris Wilson 		ring->hangcheck.deadlock = false;
23886274f212SChris Wilson }
23896274f212SChris Wilson 
2390ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2391ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
23921ec14ad3SChris Wilson {
23931ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
23941ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
23959107e9d2SChris Wilson 	u32 tmp;
23969107e9d2SChris Wilson 
23976274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
23986274f212SChris Wilson 		return active;
23996274f212SChris Wilson 
24009107e9d2SChris Wilson 	if (IS_GEN2(dev))
24016274f212SChris Wilson 		return hung;
24029107e9d2SChris Wilson 
24039107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
24049107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
24059107e9d2SChris Wilson 	 * and break the hang. This should work on
24069107e9d2SChris Wilson 	 * all but the second generation chipsets.
24079107e9d2SChris Wilson 	 */
24089107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
24091ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
24101ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
24111ec14ad3SChris Wilson 			  ring->name);
24121ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
24136274f212SChris Wilson 		return kick;
24141ec14ad3SChris Wilson 	}
2415a24a11e6SChris Wilson 
24166274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
24176274f212SChris Wilson 		switch (semaphore_passed(ring)) {
24186274f212SChris Wilson 		default:
24196274f212SChris Wilson 			return hung;
24206274f212SChris Wilson 		case 1:
2421a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
2422a24a11e6SChris Wilson 				  ring->name);
2423a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
24246274f212SChris Wilson 			return kick;
24256274f212SChris Wilson 		case 0:
24266274f212SChris Wilson 			return wait;
24276274f212SChris Wilson 		}
24289107e9d2SChris Wilson 	}
24299107e9d2SChris Wilson 
24306274f212SChris Wilson 	return hung;
2431a24a11e6SChris Wilson }
2432d1e61e7fSChris Wilson 
2433f65d9421SBen Gamari /**
2434f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
243505407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
243605407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
243705407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
243805407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
243905407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2440f65d9421SBen Gamari  */
2441f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
2442f65d9421SBen Gamari {
2443f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2444f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2445b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2446b4519513SChris Wilson 	int i;
244705407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
24489107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
24499107e9d2SChris Wilson #define BUSY 1
24509107e9d2SChris Wilson #define KICK 5
24519107e9d2SChris Wilson #define HUNG 20
24529107e9d2SChris Wilson #define FIRE 30
2453893eead0SChris Wilson 
24543e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
24553e0dc6b0SBen Widawsky 		return;
24563e0dc6b0SBen Widawsky 
2457b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
245805407ff8SMika Kuoppala 		u32 seqno, acthd;
24599107e9d2SChris Wilson 		bool busy = true;
2460b4519513SChris Wilson 
24616274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
24626274f212SChris Wilson 
246305407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
246405407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
246505407ff8SMika Kuoppala 
246605407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
24679107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
24689107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
24699107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
24709107e9d2SChris Wilson 					DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
24719107e9d2SChris Wilson 						  ring->name);
24729107e9d2SChris Wilson 					wake_up_all(&ring->irq_queue);
24739107e9d2SChris Wilson 					ring->hangcheck.score += HUNG;
24749107e9d2SChris Wilson 				} else
24759107e9d2SChris Wilson 					busy = false;
247605407ff8SMika Kuoppala 			} else {
24779107e9d2SChris Wilson 				int score;
24789107e9d2SChris Wilson 
24796274f212SChris Wilson 				/* We always increment the hangcheck score
24806274f212SChris Wilson 				 * if the ring is busy and still processing
24816274f212SChris Wilson 				 * the same request, so that no single request
24826274f212SChris Wilson 				 * can run indefinitely (such as a chain of
24836274f212SChris Wilson 				 * batches). The only time we do not increment
24846274f212SChris Wilson 				 * the hangcheck score on this ring, if this
24856274f212SChris Wilson 				 * ring is in a legitimate wait for another
24866274f212SChris Wilson 				 * ring. In that case the waiting ring is a
24876274f212SChris Wilson 				 * victim and we want to be sure we catch the
24886274f212SChris Wilson 				 * right culprit. Then every time we do kick
24896274f212SChris Wilson 				 * the ring, add a small increment to the
24906274f212SChris Wilson 				 * score so that we can catch a batch that is
24916274f212SChris Wilson 				 * being repeatedly kicked and so responsible
24926274f212SChris Wilson 				 * for stalling the machine.
24939107e9d2SChris Wilson 				 */
2494ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2495ad8beaeaSMika Kuoppala 								    acthd);
2496ad8beaeaSMika Kuoppala 
2497ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
24986274f212SChris Wilson 				case wait:
24996274f212SChris Wilson 					score = 0;
25006274f212SChris Wilson 					break;
25016274f212SChris Wilson 				case active:
25029107e9d2SChris Wilson 					score = BUSY;
25036274f212SChris Wilson 					break;
25046274f212SChris Wilson 				case kick:
25056274f212SChris Wilson 					score = KICK;
25066274f212SChris Wilson 					break;
25076274f212SChris Wilson 				case hung:
25086274f212SChris Wilson 					score = HUNG;
25096274f212SChris Wilson 					stuck[i] = true;
25106274f212SChris Wilson 					break;
25116274f212SChris Wilson 				}
25129107e9d2SChris Wilson 				ring->hangcheck.score += score;
251305407ff8SMika Kuoppala 			}
25149107e9d2SChris Wilson 		} else {
25159107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
25169107e9d2SChris Wilson 			 * attempts across multiple batches.
25179107e9d2SChris Wilson 			 */
25189107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
25199107e9d2SChris Wilson 				ring->hangcheck.score--;
2520cbb465e7SChris Wilson 		}
2521f65d9421SBen Gamari 
252205407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
252305407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
25249107e9d2SChris Wilson 		busy_count += busy;
252505407ff8SMika Kuoppala 	}
252605407ff8SMika Kuoppala 
252705407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
25289107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2529acd78c11SBen Widawsky 			DRM_ERROR("%s on %s\n",
253005407ff8SMika Kuoppala 				  stuck[i] ? "stuck" : "no progress",
2531a43adf07SChris Wilson 				  ring->name);
2532a43adf07SChris Wilson 			rings_hung++;
253305407ff8SMika Kuoppala 		}
253405407ff8SMika Kuoppala 	}
253505407ff8SMika Kuoppala 
253605407ff8SMika Kuoppala 	if (rings_hung)
253705407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
253805407ff8SMika Kuoppala 
253905407ff8SMika Kuoppala 	if (busy_count)
254005407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
254105407ff8SMika Kuoppala 		 * being added */
254299584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
254305407ff8SMika Kuoppala 			  round_jiffies_up(jiffies +
254405407ff8SMika Kuoppala 					   DRM_I915_HANGCHECK_JIFFIES));
2545f65d9421SBen Gamari }
2546f65d9421SBen Gamari 
254791738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
254891738a95SPaulo Zanoni {
254991738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
255091738a95SPaulo Zanoni 
255191738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
255291738a95SPaulo Zanoni 		return;
255391738a95SPaulo Zanoni 
255491738a95SPaulo Zanoni 	/* south display irq */
255591738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
255691738a95SPaulo Zanoni 	/*
255791738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
255891738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
255991738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
256091738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
256191738a95SPaulo Zanoni 	 */
256291738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
256391738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
256491738a95SPaulo Zanoni }
256591738a95SPaulo Zanoni 
2566c0e09200SDave Airlie /* drm_dma.h hooks
2567c0e09200SDave Airlie */
2568f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2569036a4a7dSZhenyu Wang {
2570036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2571036a4a7dSZhenyu Wang 
25724697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
25734697995bSJesse Barnes 
2574036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2575bdfcdb63SDaniel Vetter 
2576036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
2577036a4a7dSZhenyu Wang 
2578036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2579036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
25803143a2bfSChris Wilson 	POSTING_READ(DEIER);
2581036a4a7dSZhenyu Wang 
2582036a4a7dSZhenyu Wang 	/* and GT */
2583036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2584036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
25853143a2bfSChris Wilson 	POSTING_READ(GTIER);
2586c650156aSZhenyu Wang 
258791738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
25887d99163dSBen Widawsky }
25897d99163dSBen Widawsky 
25907d99163dSBen Widawsky static void ivybridge_irq_preinstall(struct drm_device *dev)
25917d99163dSBen Widawsky {
25927d99163dSBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25937d99163dSBen Widawsky 
25947d99163dSBen Widawsky 	atomic_set(&dev_priv->irq_received, 0);
25957d99163dSBen Widawsky 
25967d99163dSBen Widawsky 	I915_WRITE(HWSTAM, 0xeffe);
25977d99163dSBen Widawsky 
25987d99163dSBen Widawsky 	/* XXX hotplug from PCH */
25997d99163dSBen Widawsky 
26007d99163dSBen Widawsky 	I915_WRITE(DEIMR, 0xffffffff);
26017d99163dSBen Widawsky 	I915_WRITE(DEIER, 0x0);
26027d99163dSBen Widawsky 	POSTING_READ(DEIER);
26037d99163dSBen Widawsky 
26047d99163dSBen Widawsky 	/* and GT */
26057d99163dSBen Widawsky 	I915_WRITE(GTIMR, 0xffffffff);
26067d99163dSBen Widawsky 	I915_WRITE(GTIER, 0x0);
26077d99163dSBen Widawsky 	POSTING_READ(GTIER);
26087d99163dSBen Widawsky 
2609eda63ffbSBen Widawsky 	/* Power management */
2610eda63ffbSBen Widawsky 	I915_WRITE(GEN6_PMIMR, 0xffffffff);
2611eda63ffbSBen Widawsky 	I915_WRITE(GEN6_PMIER, 0x0);
2612eda63ffbSBen Widawsky 	POSTING_READ(GEN6_PMIER);
2613eda63ffbSBen Widawsky 
261491738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
2615036a4a7dSZhenyu Wang }
2616036a4a7dSZhenyu Wang 
26177e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
26187e231dbeSJesse Barnes {
26197e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26207e231dbeSJesse Barnes 	int pipe;
26217e231dbeSJesse Barnes 
26227e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
26237e231dbeSJesse Barnes 
26247e231dbeSJesse Barnes 	/* VLV magic */
26257e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
26267e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
26277e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
26287e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
26297e231dbeSJesse Barnes 
26307e231dbeSJesse Barnes 	/* and GT */
26317e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
26327e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
26337e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
26347e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
26357e231dbeSJesse Barnes 	POSTING_READ(GTIER);
26367e231dbeSJesse Barnes 
26377e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
26387e231dbeSJesse Barnes 
26397e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
26407e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
26417e231dbeSJesse Barnes 	for_each_pipe(pipe)
26427e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
26437e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26447e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
26457e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
26467e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
26477e231dbeSJesse Barnes }
26487e231dbeSJesse Barnes 
264982a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
265082a28bcfSDaniel Vetter {
265182a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
265282a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
265382a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
265482a28bcfSDaniel Vetter 	u32 mask = ~I915_READ(SDEIMR);
265582a28bcfSDaniel Vetter 	u32 hotplug;
265682a28bcfSDaniel Vetter 
265782a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2658995e6b3dSEgbert Eich 		mask &= ~SDE_HOTPLUG_MASK;
265982a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2660cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
266182a28bcfSDaniel Vetter 				mask |= hpd_ibx[intel_encoder->hpd_pin];
266282a28bcfSDaniel Vetter 	} else {
2663995e6b3dSEgbert Eich 		mask &= ~SDE_HOTPLUG_MASK_CPT;
266482a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2665cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
266682a28bcfSDaniel Vetter 				mask |= hpd_cpt[intel_encoder->hpd_pin];
266782a28bcfSDaniel Vetter 	}
266882a28bcfSDaniel Vetter 
266982a28bcfSDaniel Vetter 	I915_WRITE(SDEIMR, ~mask);
267082a28bcfSDaniel Vetter 
26717fe0b973SKeith Packard 	/*
26727fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
26737fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
26747fe0b973SKeith Packard 	 *
26757fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
26767fe0b973SKeith Packard 	 */
26777fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
26787fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
26797fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
26807fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
26817fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
26827fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
26837fe0b973SKeith Packard }
26847fe0b973SKeith Packard 
2685d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2686d46da437SPaulo Zanoni {
2687d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
268882a28bcfSDaniel Vetter 	u32 mask;
2689d46da437SPaulo Zanoni 
2690692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2691692a04cfSDaniel Vetter 		return;
2692692a04cfSDaniel Vetter 
26938664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
26948664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2695de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
26968664281bSPaulo Zanoni 	} else {
26978664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
26988664281bSPaulo Zanoni 
26998664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
27008664281bSPaulo Zanoni 	}
2701ab5c608bSBen Widawsky 
2702d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2703d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2704d46da437SPaulo Zanoni }
2705d46da437SPaulo Zanoni 
2706f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2707036a4a7dSZhenyu Wang {
27084bc9d430SDaniel Vetter 	unsigned long irqflags;
27094bc9d430SDaniel Vetter 
2710036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2711036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
2712013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2713ce99c256SDaniel Vetter 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
27148664281bSPaulo Zanoni 			   DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
2715de032bf4SPaulo Zanoni 			   DE_PIPEA_FIFO_UNDERRUN | DE_POISON;
2716cc609d5dSBen Widawsky 	u32 gt_irqs;
2717036a4a7dSZhenyu Wang 
27181ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2719036a4a7dSZhenyu Wang 
2720036a4a7dSZhenyu Wang 	/* should always can generate irq */
2721036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
27221ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
27236005ce42SDaniel Vetter 	I915_WRITE(DEIER, display_mask |
27246005ce42SDaniel Vetter 			  DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT);
27253143a2bfSChris Wilson 	POSTING_READ(DEIER);
2726036a4a7dSZhenyu Wang 
27271ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
2728036a4a7dSZhenyu Wang 
2729036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
27301ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2731881f47b6SXiang, Haihao 
2732cc609d5dSBen Widawsky 	gt_irqs = GT_RENDER_USER_INTERRUPT;
2733cc609d5dSBen Widawsky 
27341ec14ad3SChris Wilson 	if (IS_GEN6(dev))
2735cc609d5dSBen Widawsky 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
27361ec14ad3SChris Wilson 	else
2737cc609d5dSBen Widawsky 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
2738cc609d5dSBen Widawsky 			   ILK_BSD_USER_INTERRUPT;
2739cc609d5dSBen Widawsky 
2740cc609d5dSBen Widawsky 	I915_WRITE(GTIER, gt_irqs);
27413143a2bfSChris Wilson 	POSTING_READ(GTIER);
2742036a4a7dSZhenyu Wang 
2743d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
27447fe0b973SKeith Packard 
2745f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
27466005ce42SDaniel Vetter 		/* Enable PCU event interrupts
27476005ce42SDaniel Vetter 		 *
27486005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
27494bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
27504bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
27514bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2752f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
27534bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2754f97108d1SJesse Barnes 	}
2755f97108d1SJesse Barnes 
2756036a4a7dSZhenyu Wang 	return 0;
2757036a4a7dSZhenyu Wang }
2758036a4a7dSZhenyu Wang 
2759f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2760b1f14ad0SJesse Barnes {
2761b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2762b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2763b615b57aSChris Wilson 	u32 display_mask =
2764b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2765b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
2766b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
2767ce99c256SDaniel Vetter 		DE_PLANEA_FLIP_DONE_IVB |
27688664281bSPaulo Zanoni 		DE_AUX_CHANNEL_A_IVB |
27698664281bSPaulo Zanoni 		DE_ERR_INT_IVB;
277012638c57SBen Widawsky 	u32 pm_irqs = GEN6_PM_RPS_EVENTS;
2771cc609d5dSBen Widawsky 	u32 gt_irqs;
2772b1f14ad0SJesse Barnes 
2773b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2774b1f14ad0SJesse Barnes 
2775b1f14ad0SJesse Barnes 	/* should always can generate irq */
27768664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2777b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2778b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2779b615b57aSChris Wilson 	I915_WRITE(DEIER,
2780b615b57aSChris Wilson 		   display_mask |
2781b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
2782b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
2783b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
2784b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2785b1f14ad0SJesse Barnes 
2786cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2787b1f14ad0SJesse Barnes 
2788b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2789b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2790b1f14ad0SJesse Barnes 
2791cc609d5dSBen Widawsky 	gt_irqs = GT_RENDER_USER_INTERRUPT | GT_BSD_USER_INTERRUPT |
2792cc609d5dSBen Widawsky 		  GT_BLT_USER_INTERRUPT | GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2793cc609d5dSBen Widawsky 	I915_WRITE(GTIER, gt_irqs);
2794b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2795b1f14ad0SJesse Barnes 
279612638c57SBen Widawsky 	I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
279712638c57SBen Widawsky 	if (HAS_VEBOX(dev))
279812638c57SBen Widawsky 		pm_irqs |= PM_VEBOX_USER_INTERRUPT |
279912638c57SBen Widawsky 			PM_VEBOX_CS_ERROR_INTERRUPT;
280012638c57SBen Widawsky 
280112638c57SBen Widawsky 	/* Our enable/disable rps functions may touch these registers so
280212638c57SBen Widawsky 	 * make sure to set a known state for only the non-RPS bits.
280312638c57SBen Widawsky 	 * The RMW is extra paranoia since this should be called after being set
280412638c57SBen Widawsky 	 * to a known state in preinstall.
280512638c57SBen Widawsky 	 * */
280612638c57SBen Widawsky 	I915_WRITE(GEN6_PMIMR,
280712638c57SBen Widawsky 		   (I915_READ(GEN6_PMIMR) | ~GEN6_PM_RPS_EVENTS) & ~pm_irqs);
280812638c57SBen Widawsky 	I915_WRITE(GEN6_PMIER,
280912638c57SBen Widawsky 		   (I915_READ(GEN6_PMIER) & GEN6_PM_RPS_EVENTS) | pm_irqs);
281012638c57SBen Widawsky 	POSTING_READ(GEN6_PMIER);
2811eda63ffbSBen Widawsky 
2812d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
28137fe0b973SKeith Packard 
2814b1f14ad0SJesse Barnes 	return 0;
2815b1f14ad0SJesse Barnes }
2816b1f14ad0SJesse Barnes 
28177e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
28187e231dbeSJesse Barnes {
28197e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2820cc609d5dSBen Widawsky 	u32 gt_irqs;
28217e231dbeSJesse Barnes 	u32 enable_mask;
282231acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
28237e231dbeSJesse Barnes 
28247e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
282531acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
282631acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
282731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
28287e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
28297e231dbeSJesse Barnes 
283031acc7f5SJesse Barnes 	/*
283131acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
283231acc7f5SJesse Barnes 	 * toggle them based on usage.
283331acc7f5SJesse Barnes 	 */
283431acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
283531acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
283631acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
28377e231dbeSJesse Barnes 
283820afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
283920afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
284020afbda2SDaniel Vetter 
28417e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
28427e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
28437e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28447e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
28457e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
28467e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
28477e231dbeSJesse Barnes 
284831acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2849515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
285031acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
285131acc7f5SJesse Barnes 
28527e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28537e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28547e231dbeSJesse Barnes 
285531acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
285631acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
28573bcedbe5SJesse Barnes 
2858cc609d5dSBen Widawsky 	gt_irqs = GT_RENDER_USER_INTERRUPT | GT_BSD_USER_INTERRUPT |
2859cc609d5dSBen Widawsky 		GT_BLT_USER_INTERRUPT;
2860cc609d5dSBen Widawsky 	I915_WRITE(GTIER, gt_irqs);
28617e231dbeSJesse Barnes 	POSTING_READ(GTIER);
28627e231dbeSJesse Barnes 
28637e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
28647e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
28657e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
28667e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
28677e231dbeSJesse Barnes #endif
28687e231dbeSJesse Barnes 
28697e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
287020afbda2SDaniel Vetter 
287120afbda2SDaniel Vetter 	return 0;
287220afbda2SDaniel Vetter }
287320afbda2SDaniel Vetter 
28747e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
28757e231dbeSJesse Barnes {
28767e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
28777e231dbeSJesse Barnes 	int pipe;
28787e231dbeSJesse Barnes 
28797e231dbeSJesse Barnes 	if (!dev_priv)
28807e231dbeSJesse Barnes 		return;
28817e231dbeSJesse Barnes 
2882ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2883ac4c16c5SEgbert Eich 
28847e231dbeSJesse Barnes 	for_each_pipe(pipe)
28857e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
28867e231dbeSJesse Barnes 
28877e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
28887e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
28897e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
28907e231dbeSJesse Barnes 	for_each_pipe(pipe)
28917e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
28927e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28937e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
28947e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
28957e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
28967e231dbeSJesse Barnes }
28977e231dbeSJesse Barnes 
2898f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2899036a4a7dSZhenyu Wang {
2900036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29014697995bSJesse Barnes 
29024697995bSJesse Barnes 	if (!dev_priv)
29034697995bSJesse Barnes 		return;
29044697995bSJesse Barnes 
2905ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2906ac4c16c5SEgbert Eich 
2907036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2908036a4a7dSZhenyu Wang 
2909036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2910036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2911036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
29128664281bSPaulo Zanoni 	if (IS_GEN7(dev))
29138664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2914036a4a7dSZhenyu Wang 
2915036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2916036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2917036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2918192aac1fSKeith Packard 
2919ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2920ab5c608bSBen Widawsky 		return;
2921ab5c608bSBen Widawsky 
2922192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2923192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2924192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
29258664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
29268664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2927036a4a7dSZhenyu Wang }
2928036a4a7dSZhenyu Wang 
2929c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2930c2798b19SChris Wilson {
2931c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2932c2798b19SChris Wilson 	int pipe;
2933c2798b19SChris Wilson 
2934c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2935c2798b19SChris Wilson 
2936c2798b19SChris Wilson 	for_each_pipe(pipe)
2937c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2938c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2939c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2940c2798b19SChris Wilson 	POSTING_READ16(IER);
2941c2798b19SChris Wilson }
2942c2798b19SChris Wilson 
2943c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2944c2798b19SChris Wilson {
2945c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2946c2798b19SChris Wilson 
2947c2798b19SChris Wilson 	I915_WRITE16(EMR,
2948c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2949c2798b19SChris Wilson 
2950c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2951c2798b19SChris Wilson 	dev_priv->irq_mask =
2952c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2953c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2954c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2955c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2956c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2957c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2958c2798b19SChris Wilson 
2959c2798b19SChris Wilson 	I915_WRITE16(IER,
2960c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2961c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2962c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2963c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2964c2798b19SChris Wilson 	POSTING_READ16(IER);
2965c2798b19SChris Wilson 
2966c2798b19SChris Wilson 	return 0;
2967c2798b19SChris Wilson }
2968c2798b19SChris Wilson 
296990a72f87SVille Syrjälä /*
297090a72f87SVille Syrjälä  * Returns true when a page flip has completed.
297190a72f87SVille Syrjälä  */
297290a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
297390a72f87SVille Syrjälä 			       int pipe, u16 iir)
297490a72f87SVille Syrjälä {
297590a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
297690a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
297790a72f87SVille Syrjälä 
297890a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
297990a72f87SVille Syrjälä 		return false;
298090a72f87SVille Syrjälä 
298190a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
298290a72f87SVille Syrjälä 		return false;
298390a72f87SVille Syrjälä 
298490a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
298590a72f87SVille Syrjälä 
298690a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
298790a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
298890a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
298990a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
299090a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
299190a72f87SVille Syrjälä 	 */
299290a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
299390a72f87SVille Syrjälä 		return false;
299490a72f87SVille Syrjälä 
299590a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
299690a72f87SVille Syrjälä 
299790a72f87SVille Syrjälä 	return true;
299890a72f87SVille Syrjälä }
299990a72f87SVille Syrjälä 
3000ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3001c2798b19SChris Wilson {
3002c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3003c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3004c2798b19SChris Wilson 	u16 iir, new_iir;
3005c2798b19SChris Wilson 	u32 pipe_stats[2];
3006c2798b19SChris Wilson 	unsigned long irqflags;
3007c2798b19SChris Wilson 	int irq_received;
3008c2798b19SChris Wilson 	int pipe;
3009c2798b19SChris Wilson 	u16 flip_mask =
3010c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3011c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3012c2798b19SChris Wilson 
3013c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3014c2798b19SChris Wilson 
3015c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3016c2798b19SChris Wilson 	if (iir == 0)
3017c2798b19SChris Wilson 		return IRQ_NONE;
3018c2798b19SChris Wilson 
3019c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3020c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3021c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3022c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3023c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3024c2798b19SChris Wilson 		 */
3025c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3026c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3027c2798b19SChris Wilson 			i915_handle_error(dev, false);
3028c2798b19SChris Wilson 
3029c2798b19SChris Wilson 		for_each_pipe(pipe) {
3030c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3031c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3032c2798b19SChris Wilson 
3033c2798b19SChris Wilson 			/*
3034c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3035c2798b19SChris Wilson 			 */
3036c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3037c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3038c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3039c2798b19SChris Wilson 							 pipe_name(pipe));
3040c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3041c2798b19SChris Wilson 				irq_received = 1;
3042c2798b19SChris Wilson 			}
3043c2798b19SChris Wilson 		}
3044c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3045c2798b19SChris Wilson 
3046c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3047c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3048c2798b19SChris Wilson 
3049d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3050c2798b19SChris Wilson 
3051c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3052c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3053c2798b19SChris Wilson 
3054c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
305590a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
305690a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
3057c2798b19SChris Wilson 
3058c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
305990a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
306090a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
3061c2798b19SChris Wilson 
3062c2798b19SChris Wilson 		iir = new_iir;
3063c2798b19SChris Wilson 	}
3064c2798b19SChris Wilson 
3065c2798b19SChris Wilson 	return IRQ_HANDLED;
3066c2798b19SChris Wilson }
3067c2798b19SChris Wilson 
3068c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3069c2798b19SChris Wilson {
3070c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3071c2798b19SChris Wilson 	int pipe;
3072c2798b19SChris Wilson 
3073c2798b19SChris Wilson 	for_each_pipe(pipe) {
3074c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3075c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3076c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3077c2798b19SChris Wilson 	}
3078c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3079c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3080c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3081c2798b19SChris Wilson }
3082c2798b19SChris Wilson 
3083a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3084a266c7d5SChris Wilson {
3085a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3086a266c7d5SChris Wilson 	int pipe;
3087a266c7d5SChris Wilson 
3088a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3089a266c7d5SChris Wilson 
3090a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3091a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3092a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3093a266c7d5SChris Wilson 	}
3094a266c7d5SChris Wilson 
309500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3096a266c7d5SChris Wilson 	for_each_pipe(pipe)
3097a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3098a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3099a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3100a266c7d5SChris Wilson 	POSTING_READ(IER);
3101a266c7d5SChris Wilson }
3102a266c7d5SChris Wilson 
3103a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3104a266c7d5SChris Wilson {
3105a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
310638bde180SChris Wilson 	u32 enable_mask;
3107a266c7d5SChris Wilson 
310838bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
310938bde180SChris Wilson 
311038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
311138bde180SChris Wilson 	dev_priv->irq_mask =
311238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
311338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
311438bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
311538bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
311638bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
311738bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
311838bde180SChris Wilson 
311938bde180SChris Wilson 	enable_mask =
312038bde180SChris Wilson 		I915_ASLE_INTERRUPT |
312138bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
312238bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
312338bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
312438bde180SChris Wilson 		I915_USER_INTERRUPT;
312538bde180SChris Wilson 
3126a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
312720afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
312820afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
312920afbda2SDaniel Vetter 
3130a266c7d5SChris Wilson 		/* Enable in IER... */
3131a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3132a266c7d5SChris Wilson 		/* and unmask in IMR */
3133a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3134a266c7d5SChris Wilson 	}
3135a266c7d5SChris Wilson 
3136a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3137a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3138a266c7d5SChris Wilson 	POSTING_READ(IER);
3139a266c7d5SChris Wilson 
3140f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
314120afbda2SDaniel Vetter 
314220afbda2SDaniel Vetter 	return 0;
314320afbda2SDaniel Vetter }
314420afbda2SDaniel Vetter 
314590a72f87SVille Syrjälä /*
314690a72f87SVille Syrjälä  * Returns true when a page flip has completed.
314790a72f87SVille Syrjälä  */
314890a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
314990a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
315090a72f87SVille Syrjälä {
315190a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
315290a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
315390a72f87SVille Syrjälä 
315490a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
315590a72f87SVille Syrjälä 		return false;
315690a72f87SVille Syrjälä 
315790a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
315890a72f87SVille Syrjälä 		return false;
315990a72f87SVille Syrjälä 
316090a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
316190a72f87SVille Syrjälä 
316290a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
316390a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
316490a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
316590a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
316690a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
316790a72f87SVille Syrjälä 	 */
316890a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
316990a72f87SVille Syrjälä 		return false;
317090a72f87SVille Syrjälä 
317190a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
317290a72f87SVille Syrjälä 
317390a72f87SVille Syrjälä 	return true;
317490a72f87SVille Syrjälä }
317590a72f87SVille Syrjälä 
3176ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3177a266c7d5SChris Wilson {
3178a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3179a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
31808291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3181a266c7d5SChris Wilson 	unsigned long irqflags;
318238bde180SChris Wilson 	u32 flip_mask =
318338bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
318438bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
318538bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3186a266c7d5SChris Wilson 
3187a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3188a266c7d5SChris Wilson 
3189a266c7d5SChris Wilson 	iir = I915_READ(IIR);
319038bde180SChris Wilson 	do {
319138bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
31928291ee90SChris Wilson 		bool blc_event = false;
3193a266c7d5SChris Wilson 
3194a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3195a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3196a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3197a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3198a266c7d5SChris Wilson 		 */
3199a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3200a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3201a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3202a266c7d5SChris Wilson 
3203a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3204a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3205a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3206a266c7d5SChris Wilson 
320738bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3208a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3209a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3210a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3211a266c7d5SChris Wilson 							 pipe_name(pipe));
3212a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
321338bde180SChris Wilson 				irq_received = true;
3214a266c7d5SChris Wilson 			}
3215a266c7d5SChris Wilson 		}
3216a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3217a266c7d5SChris Wilson 
3218a266c7d5SChris Wilson 		if (!irq_received)
3219a266c7d5SChris Wilson 			break;
3220a266c7d5SChris Wilson 
3221a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3222a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3223a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3224a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3225b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3226a266c7d5SChris Wilson 
3227a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3228a266c7d5SChris Wilson 				  hotplug_status);
3229*91d131d2SDaniel Vetter 
323010a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
3231*91d131d2SDaniel Vetter 
3232a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
323338bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3234a266c7d5SChris Wilson 		}
3235a266c7d5SChris Wilson 
323638bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3237a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3238a266c7d5SChris Wilson 
3239a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3240a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3241a266c7d5SChris Wilson 
3242a266c7d5SChris Wilson 		for_each_pipe(pipe) {
324338bde180SChris Wilson 			int plane = pipe;
324438bde180SChris Wilson 			if (IS_MOBILE(dev))
324538bde180SChris Wilson 				plane = !plane;
32465e2032d4SVille Syrjälä 
324790a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
324890a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
324990a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3250a266c7d5SChris Wilson 
3251a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3252a266c7d5SChris Wilson 				blc_event = true;
3253a266c7d5SChris Wilson 		}
3254a266c7d5SChris Wilson 
3255a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3256a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3257a266c7d5SChris Wilson 
3258a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3259a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3260a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3261a266c7d5SChris Wilson 		 * we would never get another interrupt.
3262a266c7d5SChris Wilson 		 *
3263a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3264a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3265a266c7d5SChris Wilson 		 * another one.
3266a266c7d5SChris Wilson 		 *
3267a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3268a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3269a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3270a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3271a266c7d5SChris Wilson 		 * stray interrupts.
3272a266c7d5SChris Wilson 		 */
327338bde180SChris Wilson 		ret = IRQ_HANDLED;
3274a266c7d5SChris Wilson 		iir = new_iir;
327538bde180SChris Wilson 	} while (iir & ~flip_mask);
3276a266c7d5SChris Wilson 
3277d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
32788291ee90SChris Wilson 
3279a266c7d5SChris Wilson 	return ret;
3280a266c7d5SChris Wilson }
3281a266c7d5SChris Wilson 
3282a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3283a266c7d5SChris Wilson {
3284a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3285a266c7d5SChris Wilson 	int pipe;
3286a266c7d5SChris Wilson 
3287ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3288ac4c16c5SEgbert Eich 
3289a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3290a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3291a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3292a266c7d5SChris Wilson 	}
3293a266c7d5SChris Wilson 
329400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
329555b39755SChris Wilson 	for_each_pipe(pipe) {
329655b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3297a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
329855b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
329955b39755SChris Wilson 	}
3300a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3301a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3302a266c7d5SChris Wilson 
3303a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3304a266c7d5SChris Wilson }
3305a266c7d5SChris Wilson 
3306a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3307a266c7d5SChris Wilson {
3308a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3309a266c7d5SChris Wilson 	int pipe;
3310a266c7d5SChris Wilson 
3311a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3312a266c7d5SChris Wilson 
3313a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3314a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3315a266c7d5SChris Wilson 
3316a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3317a266c7d5SChris Wilson 	for_each_pipe(pipe)
3318a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3319a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3320a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3321a266c7d5SChris Wilson 	POSTING_READ(IER);
3322a266c7d5SChris Wilson }
3323a266c7d5SChris Wilson 
3324a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3325a266c7d5SChris Wilson {
3326a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3327bbba0a97SChris Wilson 	u32 enable_mask;
3328a266c7d5SChris Wilson 	u32 error_mask;
3329a266c7d5SChris Wilson 
3330a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3331bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3332adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3333bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3334bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3335bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3336bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3337bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3338bbba0a97SChris Wilson 
3339bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
334021ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
334121ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3342bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3343bbba0a97SChris Wilson 
3344bbba0a97SChris Wilson 	if (IS_G4X(dev))
3345bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3346a266c7d5SChris Wilson 
3347515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
3348a266c7d5SChris Wilson 
3349a266c7d5SChris Wilson 	/*
3350a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3351a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3352a266c7d5SChris Wilson 	 */
3353a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3354a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3355a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3356a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3357a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3358a266c7d5SChris Wilson 	} else {
3359a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3360a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3361a266c7d5SChris Wilson 	}
3362a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3363a266c7d5SChris Wilson 
3364a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3365a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3366a266c7d5SChris Wilson 	POSTING_READ(IER);
3367a266c7d5SChris Wilson 
336820afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
336920afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
337020afbda2SDaniel Vetter 
3371f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
337220afbda2SDaniel Vetter 
337320afbda2SDaniel Vetter 	return 0;
337420afbda2SDaniel Vetter }
337520afbda2SDaniel Vetter 
3376bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
337720afbda2SDaniel Vetter {
337820afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3379e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3380cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
338120afbda2SDaniel Vetter 	u32 hotplug_en;
338220afbda2SDaniel Vetter 
3383bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3384bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3385bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3386adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3387e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3388cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3389cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3390cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3391a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3392a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3393a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3394a266c7d5SChris Wilson 		*/
3395a266c7d5SChris Wilson 		if (IS_G4X(dev))
3396a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
339785fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3398a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3399a266c7d5SChris Wilson 
3400a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3401a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3402a266c7d5SChris Wilson 	}
3403bac56d5bSEgbert Eich }
3404a266c7d5SChris Wilson 
3405ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3406a266c7d5SChris Wilson {
3407a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3408a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3409a266c7d5SChris Wilson 	u32 iir, new_iir;
3410a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3411a266c7d5SChris Wilson 	unsigned long irqflags;
3412a266c7d5SChris Wilson 	int irq_received;
3413a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
341421ad8330SVille Syrjälä 	u32 flip_mask =
341521ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
341621ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3417a266c7d5SChris Wilson 
3418a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3419a266c7d5SChris Wilson 
3420a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3421a266c7d5SChris Wilson 
3422a266c7d5SChris Wilson 	for (;;) {
34232c8ba29fSChris Wilson 		bool blc_event = false;
34242c8ba29fSChris Wilson 
342521ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3426a266c7d5SChris Wilson 
3427a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3428a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3429a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3430a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3431a266c7d5SChris Wilson 		 */
3432a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3433a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3434a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3435a266c7d5SChris Wilson 
3436a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3437a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3438a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3439a266c7d5SChris Wilson 
3440a266c7d5SChris Wilson 			/*
3441a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3442a266c7d5SChris Wilson 			 */
3443a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3444a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3445a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3446a266c7d5SChris Wilson 							 pipe_name(pipe));
3447a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3448a266c7d5SChris Wilson 				irq_received = 1;
3449a266c7d5SChris Wilson 			}
3450a266c7d5SChris Wilson 		}
3451a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3452a266c7d5SChris Wilson 
3453a266c7d5SChris Wilson 		if (!irq_received)
3454a266c7d5SChris Wilson 			break;
3455a266c7d5SChris Wilson 
3456a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3457a266c7d5SChris Wilson 
3458a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3459adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3460a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3461b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3462b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
34634f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3464a266c7d5SChris Wilson 
3465a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3466a266c7d5SChris Wilson 				  hotplug_status);
3467*91d131d2SDaniel Vetter 
346810a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
346910a504deSDaniel Vetter 					      IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
3470*91d131d2SDaniel Vetter 
3471a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3472a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3473a266c7d5SChris Wilson 		}
3474a266c7d5SChris Wilson 
347521ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3476a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3477a266c7d5SChris Wilson 
3478a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3479a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3480a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3481a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3482a266c7d5SChris Wilson 
3483a266c7d5SChris Wilson 		for_each_pipe(pipe) {
34842c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
348590a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
348690a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3487a266c7d5SChris Wilson 
3488a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3489a266c7d5SChris Wilson 				blc_event = true;
3490a266c7d5SChris Wilson 		}
3491a266c7d5SChris Wilson 
3492a266c7d5SChris Wilson 
3493a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3494a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3495a266c7d5SChris Wilson 
3496515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3497515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3498515ac2bbSDaniel Vetter 
3499a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3500a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3501a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3502a266c7d5SChris Wilson 		 * we would never get another interrupt.
3503a266c7d5SChris Wilson 		 *
3504a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3505a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3506a266c7d5SChris Wilson 		 * another one.
3507a266c7d5SChris Wilson 		 *
3508a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3509a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3510a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3511a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3512a266c7d5SChris Wilson 		 * stray interrupts.
3513a266c7d5SChris Wilson 		 */
3514a266c7d5SChris Wilson 		iir = new_iir;
3515a266c7d5SChris Wilson 	}
3516a266c7d5SChris Wilson 
3517d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
35182c8ba29fSChris Wilson 
3519a266c7d5SChris Wilson 	return ret;
3520a266c7d5SChris Wilson }
3521a266c7d5SChris Wilson 
3522a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3523a266c7d5SChris Wilson {
3524a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3525a266c7d5SChris Wilson 	int pipe;
3526a266c7d5SChris Wilson 
3527a266c7d5SChris Wilson 	if (!dev_priv)
3528a266c7d5SChris Wilson 		return;
3529a266c7d5SChris Wilson 
3530ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3531ac4c16c5SEgbert Eich 
3532a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3533a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3534a266c7d5SChris Wilson 
3535a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3536a266c7d5SChris Wilson 	for_each_pipe(pipe)
3537a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3538a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3539a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3540a266c7d5SChris Wilson 
3541a266c7d5SChris Wilson 	for_each_pipe(pipe)
3542a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3543a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3544a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3545a266c7d5SChris Wilson }
3546a266c7d5SChris Wilson 
3547ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3548ac4c16c5SEgbert Eich {
3549ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3550ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3551ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3552ac4c16c5SEgbert Eich 	unsigned long irqflags;
3553ac4c16c5SEgbert Eich 	int i;
3554ac4c16c5SEgbert Eich 
3555ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3556ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3557ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3558ac4c16c5SEgbert Eich 
3559ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3560ac4c16c5SEgbert Eich 			continue;
3561ac4c16c5SEgbert Eich 
3562ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3563ac4c16c5SEgbert Eich 
3564ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3565ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3566ac4c16c5SEgbert Eich 
3567ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3568ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3569ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3570ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3571ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3572ac4c16c5SEgbert Eich 				if (!connector->polled)
3573ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3574ac4c16c5SEgbert Eich 			}
3575ac4c16c5SEgbert Eich 		}
3576ac4c16c5SEgbert Eich 	}
3577ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3578ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3579ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3580ac4c16c5SEgbert Eich }
3581ac4c16c5SEgbert Eich 
3582f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3583f71d4af4SJesse Barnes {
35848b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
35858b2e326dSChris Wilson 
35868b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
358799584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3588c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3589a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
35908b2e326dSChris Wilson 
359199584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
359299584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
359361bac78eSDaniel Vetter 		    (unsigned long) dev);
3594ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3595ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
359661bac78eSDaniel Vetter 
359797a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
35989ee32feaSDaniel Vetter 
3599f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
3600f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
36017d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3602f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3603f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3604f71d4af4SJesse Barnes 	}
3605f71d4af4SJesse Barnes 
3606c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3607f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3608c3613de9SKeith Packard 	else
3609c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
3610f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3611f71d4af4SJesse Barnes 
36127e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
36137e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
36147e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
36157e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
36167e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
36177e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
36187e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3619fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
36204a06e201SDaniel Vetter 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
36217d99163dSBen Widawsky 		/* Share uninstall handlers with ILK/SNB */
3622f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
36237d99163dSBen Widawsky 		dev->driver->irq_preinstall = ivybridge_irq_preinstall;
3624f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
3625f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3626f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
3627f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
362882a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3629f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3630f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3631f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3632f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3633f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3634f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3635f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
363682a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3637f71d4af4SJesse Barnes 	} else {
3638c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3639c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3640c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3641c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3642c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3643a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3644a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3645a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3646a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3647a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
364820afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3649c2798b19SChris Wilson 		} else {
3650a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3651a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3652a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3653a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3654bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3655c2798b19SChris Wilson 		}
3656f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3657f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3658f71d4af4SJesse Barnes 	}
3659f71d4af4SJesse Barnes }
366020afbda2SDaniel Vetter 
366120afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
366220afbda2SDaniel Vetter {
366320afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3664821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3665821450c6SEgbert Eich 	struct drm_connector *connector;
3666821450c6SEgbert Eich 	int i;
366720afbda2SDaniel Vetter 
3668821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3669821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3670821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3671821450c6SEgbert Eich 	}
3672821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3673821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3674821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3675821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3676821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3677821450c6SEgbert Eich 	}
367820afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
367920afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
368020afbda2SDaniel Vetter }
3681