xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision f343c5f6477354967ee1e331a68a56b9fece2f36)
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 
322b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
323b79480baSDaniel Vetter 
32446c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
32546c06a30SVille Syrjälä 		return;
32646c06a30SVille Syrjälä 
3277c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
32846c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
32946c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3303143a2bfSChris Wilson 	POSTING_READ(reg);
3317c463586SKeith Packard }
3327c463586SKeith Packard 
3337c463586SKeith Packard void
3347c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3357c463586SKeith Packard {
3369db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
33746c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3387c463586SKeith Packard 
339b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
340b79480baSDaniel Vetter 
34146c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
34246c06a30SVille Syrjälä 		return;
34346c06a30SVille Syrjälä 
34446c06a30SVille Syrjälä 	pipestat &= ~mask;
34546c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3463143a2bfSChris Wilson 	POSTING_READ(reg);
3477c463586SKeith Packard }
3487c463586SKeith Packard 
349c0e09200SDave Airlie /**
350f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
35101c66889SZhao Yakui  */
352f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
35301c66889SZhao Yakui {
3541ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
3551ec14ad3SChris Wilson 	unsigned long irqflags;
3561ec14ad3SChris Wilson 
357f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
358f49e38ddSJani Nikula 		return;
359f49e38ddSJani Nikula 
3601ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
36101c66889SZhao Yakui 
362f898780bSJani Nikula 	i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
363a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
364f898780bSJani Nikula 		i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
3651ec14ad3SChris Wilson 
3661ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
36701c66889SZhao Yakui }
36801c66889SZhao Yakui 
36901c66889SZhao Yakui /**
3700a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
3710a3e67a4SJesse Barnes  * @dev: DRM device
3720a3e67a4SJesse Barnes  * @pipe: pipe to check
3730a3e67a4SJesse Barnes  *
3740a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
3750a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
3760a3e67a4SJesse Barnes  * before reading such registers if unsure.
3770a3e67a4SJesse Barnes  */
3780a3e67a4SJesse Barnes static int
3790a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
3800a3e67a4SJesse Barnes {
3810a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
382702e7a56SPaulo Zanoni 
383a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
384a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
385a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
386a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
38771f8ba6bSPaulo Zanoni 
388a01025afSDaniel Vetter 		return intel_crtc->active;
389a01025afSDaniel Vetter 	} else {
390a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
391a01025afSDaniel Vetter 	}
3920a3e67a4SJesse Barnes }
3930a3e67a4SJesse Barnes 
39442f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
39542f52ef8SKeith Packard  * we use as a pipe index
39642f52ef8SKeith Packard  */
397f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
3980a3e67a4SJesse Barnes {
3990a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4000a3e67a4SJesse Barnes 	unsigned long high_frame;
4010a3e67a4SJesse Barnes 	unsigned long low_frame;
4025eddb70bSChris Wilson 	u32 high1, high2, low;
4030a3e67a4SJesse Barnes 
4040a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
40544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4069db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
4070a3e67a4SJesse Barnes 		return 0;
4080a3e67a4SJesse Barnes 	}
4090a3e67a4SJesse Barnes 
4109db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
4119db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
4125eddb70bSChris Wilson 
4130a3e67a4SJesse Barnes 	/*
4140a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
4150a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
4160a3e67a4SJesse Barnes 	 * register.
4170a3e67a4SJesse Barnes 	 */
4180a3e67a4SJesse Barnes 	do {
4195eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4205eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
4215eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4220a3e67a4SJesse Barnes 	} while (high1 != high2);
4230a3e67a4SJesse Barnes 
4245eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
4255eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
4265eddb70bSChris Wilson 	return (high1 << 8) | low;
4270a3e67a4SJesse Barnes }
4280a3e67a4SJesse Barnes 
429f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
4309880b7a5SJesse Barnes {
4319880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4329db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
4339880b7a5SJesse Barnes 
4349880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
43544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4369db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4379880b7a5SJesse Barnes 		return 0;
4389880b7a5SJesse Barnes 	}
4399880b7a5SJesse Barnes 
4409880b7a5SJesse Barnes 	return I915_READ(reg);
4419880b7a5SJesse Barnes }
4429880b7a5SJesse Barnes 
443f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
4440af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
4450af7e4dfSMario Kleiner {
4460af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4470af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
4480af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
4490af7e4dfSMario Kleiner 	bool in_vbl = true;
4500af7e4dfSMario Kleiner 	int ret = 0;
451fe2b8f9dSPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
452fe2b8f9dSPaulo Zanoni 								      pipe);
4530af7e4dfSMario Kleiner 
4540af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
4550af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
4569db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4570af7e4dfSMario Kleiner 		return 0;
4580af7e4dfSMario Kleiner 	}
4590af7e4dfSMario Kleiner 
4600af7e4dfSMario Kleiner 	/* Get vtotal. */
461fe2b8f9dSPaulo Zanoni 	vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4620af7e4dfSMario Kleiner 
4630af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
4640af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
4650af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
4660af7e4dfSMario Kleiner 		 */
4670af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
4680af7e4dfSMario Kleiner 
4690af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
4700af7e4dfSMario Kleiner 		 * horizontal scanout position.
4710af7e4dfSMario Kleiner 		 */
4720af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
4730af7e4dfSMario Kleiner 		*hpos = 0;
4740af7e4dfSMario Kleiner 	} else {
4750af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
4760af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
4770af7e4dfSMario Kleiner 		 * scanout position.
4780af7e4dfSMario Kleiner 		 */
4790af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
4800af7e4dfSMario Kleiner 
481fe2b8f9dSPaulo Zanoni 		htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4820af7e4dfSMario Kleiner 		*vpos = position / htotal;
4830af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
4840af7e4dfSMario Kleiner 	}
4850af7e4dfSMario Kleiner 
4860af7e4dfSMario Kleiner 	/* Query vblank area. */
487fe2b8f9dSPaulo Zanoni 	vbl = I915_READ(VBLANK(cpu_transcoder));
4880af7e4dfSMario Kleiner 
4890af7e4dfSMario Kleiner 	/* Test position against vblank region. */
4900af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
4910af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
4920af7e4dfSMario Kleiner 
4930af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
4940af7e4dfSMario Kleiner 		in_vbl = false;
4950af7e4dfSMario Kleiner 
4960af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
4970af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
4980af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
4990af7e4dfSMario Kleiner 
5000af7e4dfSMario Kleiner 	/* Readouts valid? */
5010af7e4dfSMario Kleiner 	if (vbl > 0)
5020af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
5030af7e4dfSMario Kleiner 
5040af7e4dfSMario Kleiner 	/* In vblank? */
5050af7e4dfSMario Kleiner 	if (in_vbl)
5060af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
5070af7e4dfSMario Kleiner 
5080af7e4dfSMario Kleiner 	return ret;
5090af7e4dfSMario Kleiner }
5100af7e4dfSMario Kleiner 
511f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
5120af7e4dfSMario Kleiner 			      int *max_error,
5130af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
5140af7e4dfSMario Kleiner 			      unsigned flags)
5150af7e4dfSMario Kleiner {
5164041b853SChris Wilson 	struct drm_crtc *crtc;
5170af7e4dfSMario Kleiner 
5187eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
5194041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5200af7e4dfSMario Kleiner 		return -EINVAL;
5210af7e4dfSMario Kleiner 	}
5220af7e4dfSMario Kleiner 
5230af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
5244041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
5254041b853SChris Wilson 	if (crtc == NULL) {
5264041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5274041b853SChris Wilson 		return -EINVAL;
5284041b853SChris Wilson 	}
5294041b853SChris Wilson 
5304041b853SChris Wilson 	if (!crtc->enabled) {
5314041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
5324041b853SChris Wilson 		return -EBUSY;
5334041b853SChris Wilson 	}
5340af7e4dfSMario Kleiner 
5350af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
5364041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
5374041b853SChris Wilson 						     vblank_time, flags,
5384041b853SChris Wilson 						     crtc);
5390af7e4dfSMario Kleiner }
5400af7e4dfSMario Kleiner 
541321a1b30SEgbert Eich static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
542321a1b30SEgbert Eich {
543321a1b30SEgbert Eich 	enum drm_connector_status old_status;
544321a1b30SEgbert Eich 
545321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
546321a1b30SEgbert Eich 	old_status = connector->status;
547321a1b30SEgbert Eich 
548321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
549321a1b30SEgbert Eich 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
550321a1b30SEgbert Eich 		      connector->base.id,
551321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
552321a1b30SEgbert Eich 		      old_status, connector->status);
553321a1b30SEgbert Eich 	return (old_status != connector->status);
554321a1b30SEgbert Eich }
555321a1b30SEgbert Eich 
5565ca58282SJesse Barnes /*
5575ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
5585ca58282SJesse Barnes  */
559ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
560ac4c16c5SEgbert Eich 
5615ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
5625ca58282SJesse Barnes {
5635ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5645ca58282SJesse Barnes 						    hotplug_work);
5655ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
566c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
567cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
568cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
569cd569aedSEgbert Eich 	struct drm_connector *connector;
570cd569aedSEgbert Eich 	unsigned long irqflags;
571cd569aedSEgbert Eich 	bool hpd_disabled = false;
572321a1b30SEgbert Eich 	bool changed = false;
573142e2398SEgbert Eich 	u32 hpd_event_bits;
5745ca58282SJesse Barnes 
57552d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
57652d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
57752d7ecedSDaniel Vetter 		return;
57852d7ecedSDaniel Vetter 
579a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
580e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
581e67189abSJesse Barnes 
582cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
583142e2398SEgbert Eich 
584142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
585142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
586cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
587cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
588cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
589cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
590cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
591cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
592cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
593cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
594cd569aedSEgbert Eich 				drm_get_connector_name(connector));
595cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
596cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
597cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
598cd569aedSEgbert Eich 			hpd_disabled = true;
599cd569aedSEgbert Eich 		}
600142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
601142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
602142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
603142e2398SEgbert Eich 		}
604cd569aedSEgbert Eich 	}
605cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
606cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
607cd569aedSEgbert Eich 	  * some connectors */
608ac4c16c5SEgbert Eich 	if (hpd_disabled) {
609cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
610ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
611ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
612ac4c16c5SEgbert Eich 	}
613cd569aedSEgbert Eich 
614cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
615cd569aedSEgbert Eich 
616321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
617321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
618321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
619321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
620cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
621cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
622321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
623321a1b30SEgbert Eich 				changed = true;
624321a1b30SEgbert Eich 		}
625321a1b30SEgbert Eich 	}
62640ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
62740ee3381SKeith Packard 
628321a1b30SEgbert Eich 	if (changed)
629321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
6305ca58282SJesse Barnes }
6315ca58282SJesse Barnes 
63273edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
633f97108d1SJesse Barnes {
634f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
635b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
6369270388eSDaniel Vetter 	u8 new_delay;
6379270388eSDaniel Vetter 	unsigned long flags;
6389270388eSDaniel Vetter 
6399270388eSDaniel Vetter 	spin_lock_irqsave(&mchdev_lock, flags);
640f97108d1SJesse Barnes 
64173edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
64273edd18fSDaniel Vetter 
64320e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
6449270388eSDaniel Vetter 
6457648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
646b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
647b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
648f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
649f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
650f97108d1SJesse Barnes 
651f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
652b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
65320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
65420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
65520e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
65620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
657b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
65820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
65920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
66020e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
66120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
662f97108d1SJesse Barnes 	}
663f97108d1SJesse Barnes 
6647648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
66520e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
666f97108d1SJesse Barnes 
6679270388eSDaniel Vetter 	spin_unlock_irqrestore(&mchdev_lock, flags);
6689270388eSDaniel Vetter 
669f97108d1SJesse Barnes 	return;
670f97108d1SJesse Barnes }
671f97108d1SJesse Barnes 
672549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
673549f7365SChris Wilson 			struct intel_ring_buffer *ring)
674549f7365SChris Wilson {
675549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
6769862e600SChris Wilson 
677475553deSChris Wilson 	if (ring->obj == NULL)
678475553deSChris Wilson 		return;
679475553deSChris Wilson 
680b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
6819862e600SChris Wilson 
682549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
6833e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
68499584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
685cecc21feSChris Wilson 			  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
6863e0dc6b0SBen Widawsky 	}
687549f7365SChris Wilson }
688549f7365SChris Wilson 
6894912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
6903b8d8d91SJesse Barnes {
6914912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
692c6a828d3SDaniel Vetter 						    rps.work);
6934912d041SBen Widawsky 	u32 pm_iir, pm_imr;
6947b9e0ae6SChris Wilson 	u8 new_delay;
6953b8d8d91SJesse Barnes 
696c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
697c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
698c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
6994912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
7004848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
7014848405cSBen Widawsky 	I915_WRITE(GEN6_PMIMR, pm_imr & ~GEN6_PM_RPS_EVENTS);
702c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
7034912d041SBen Widawsky 
7044848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
7053b8d8d91SJesse Barnes 		return;
7063b8d8d91SJesse Barnes 
7074fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
7087b9e0ae6SChris Wilson 
7097425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
710c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
7117425034aSVille Syrjälä 
7127425034aSVille Syrjälä 		/*
7137425034aSVille Syrjälä 		 * For better performance, jump directly
7147425034aSVille Syrjälä 		 * to RPe if we're below it.
7157425034aSVille Syrjälä 		 */
7167425034aSVille Syrjälä 		if (IS_VALLEYVIEW(dev_priv->dev) &&
7177425034aSVille Syrjälä 		    dev_priv->rps.cur_delay < dev_priv->rps.rpe_delay)
7187425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
7197425034aSVille Syrjälä 	} else
720c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
7213b8d8d91SJesse Barnes 
72279249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
72379249636SBen Widawsky 	 * interrupt
72479249636SBen Widawsky 	 */
725d8289c9eSVille Syrjälä 	if (new_delay >= dev_priv->rps.min_delay &&
726d8289c9eSVille Syrjälä 	    new_delay <= dev_priv->rps.max_delay) {
7270a073b84SJesse Barnes 		if (IS_VALLEYVIEW(dev_priv->dev))
7280a073b84SJesse Barnes 			valleyview_set_rps(dev_priv->dev, new_delay);
7290a073b84SJesse Barnes 		else
7304912d041SBen Widawsky 			gen6_set_rps(dev_priv->dev, new_delay);
73179249636SBen Widawsky 	}
7323b8d8d91SJesse Barnes 
73352ceb908SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev)) {
73452ceb908SJesse Barnes 		/*
73552ceb908SJesse Barnes 		 * On VLV, when we enter RC6 we may not be at the minimum
73652ceb908SJesse Barnes 		 * voltage level, so arm a timer to check.  It should only
73752ceb908SJesse Barnes 		 * fire when there's activity or once after we've entered
73852ceb908SJesse Barnes 		 * RC6, and then won't be re-armed until the next RPS interrupt.
73952ceb908SJesse Barnes 		 */
74052ceb908SJesse Barnes 		mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
74152ceb908SJesse Barnes 				 msecs_to_jiffies(100));
74252ceb908SJesse Barnes 	}
74352ceb908SJesse Barnes 
7444fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
7453b8d8d91SJesse Barnes }
7463b8d8d91SJesse Barnes 
747e3689190SBen Widawsky 
748e3689190SBen Widawsky /**
749e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
750e3689190SBen Widawsky  * occurred.
751e3689190SBen Widawsky  * @work: workqueue struct
752e3689190SBen Widawsky  *
753e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
754e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
755e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
756e3689190SBen Widawsky  */
757e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
758e3689190SBen Widawsky {
759e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
760a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
761e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
762e3689190SBen Widawsky 	char *parity_event[5];
763e3689190SBen Widawsky 	uint32_t misccpctl;
764e3689190SBen Widawsky 	unsigned long flags;
765e3689190SBen Widawsky 
766e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
767e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
768e3689190SBen Widawsky 	 * any time we access those registers.
769e3689190SBen Widawsky 	 */
770e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
771e3689190SBen Widawsky 
772e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
773e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
774e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
775e3689190SBen Widawsky 
776e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
777e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
778e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
779e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
780e3689190SBen Widawsky 
781e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
782e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
783e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
784e3689190SBen Widawsky 
785e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
786e3689190SBen Widawsky 
787e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
788cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
789e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
790e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
791e3689190SBen Widawsky 
792e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
793e3689190SBen Widawsky 
794e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
795e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
796e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
797e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
798e3689190SBen Widawsky 	parity_event[4] = NULL;
799e3689190SBen Widawsky 
800e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
801e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
802e3689190SBen Widawsky 
803e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
804e3689190SBen Widawsky 		  row, bank, subbank);
805e3689190SBen Widawsky 
806e3689190SBen Widawsky 	kfree(parity_event[3]);
807e3689190SBen Widawsky 	kfree(parity_event[2]);
808e3689190SBen Widawsky 	kfree(parity_event[1]);
809e3689190SBen Widawsky }
810e3689190SBen Widawsky 
811d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
812e3689190SBen Widawsky {
813e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
814e3689190SBen Widawsky 	unsigned long flags;
815e3689190SBen Widawsky 
816e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
817e3689190SBen Widawsky 		return;
818e3689190SBen Widawsky 
819e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
820cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
821e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
822e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
823e3689190SBen Widawsky 
824a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
825e3689190SBen Widawsky }
826e3689190SBen Widawsky 
827e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
828e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
829e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
830e7b4c6b1SDaniel Vetter {
831e7b4c6b1SDaniel Vetter 
832cc609d5dSBen Widawsky 	if (gt_iir &
833cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
834e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
835cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
836e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
837cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
838e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
839e7b4c6b1SDaniel Vetter 
840cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
841cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
842cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
843e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
844e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
845e7b4c6b1SDaniel Vetter 	}
846e3689190SBen Widawsky 
847cc609d5dSBen Widawsky 	if (gt_iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
848e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
849e7b4c6b1SDaniel Vetter }
850e7b4c6b1SDaniel Vetter 
851baf02a1fSBen Widawsky /* Legacy way of handling PM interrupts */
852fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
853fc6826d1SChris Wilson 				u32 pm_iir)
854fc6826d1SChris Wilson {
855fc6826d1SChris Wilson 	unsigned long flags;
856fc6826d1SChris Wilson 
857fc6826d1SChris Wilson 	/*
858fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
859fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
860fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
861c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
862fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
863fc6826d1SChris Wilson 	 *
864c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
865fc6826d1SChris Wilson 	 */
866fc6826d1SChris Wilson 
867c6a828d3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
868c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
869c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
870fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
871c6a828d3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
872fc6826d1SChris Wilson 
873c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
874fc6826d1SChris Wilson }
875fc6826d1SChris Wilson 
876b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
877b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
878b543fb04SEgbert Eich 
87910a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
880b543fb04SEgbert Eich 					 u32 hotplug_trigger,
881b543fb04SEgbert Eich 					 const u32 *hpd)
882b543fb04SEgbert Eich {
883b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
884b543fb04SEgbert Eich 	int i;
88510a504deSDaniel Vetter 	bool storm_detected = false;
886b543fb04SEgbert Eich 
88791d131d2SDaniel Vetter 	if (!hotplug_trigger)
88891d131d2SDaniel Vetter 		return;
88991d131d2SDaniel Vetter 
890b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
891b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
892821450c6SEgbert Eich 
893b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
894b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
895b543fb04SEgbert Eich 			continue;
896b543fb04SEgbert Eich 
897bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
898b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
899b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
900b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
901b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
902b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
903b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
904b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
905142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
906b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
90710a504deSDaniel Vetter 			storm_detected = true;
908b543fb04SEgbert Eich 		} else {
909b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
910b543fb04SEgbert Eich 		}
911b543fb04SEgbert Eich 	}
912b543fb04SEgbert Eich 
91310a504deSDaniel Vetter 	if (storm_detected)
91410a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
915b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
9165876fa0dSDaniel Vetter 
9175876fa0dSDaniel Vetter 	queue_work(dev_priv->wq,
9185876fa0dSDaniel Vetter 		   &dev_priv->hotplug_work);
919b543fb04SEgbert Eich }
920b543fb04SEgbert Eich 
921515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
922515ac2bbSDaniel Vetter {
92328c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
92428c70f16SDaniel Vetter 
92528c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
926515ac2bbSDaniel Vetter }
927515ac2bbSDaniel Vetter 
928ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
929ce99c256SDaniel Vetter {
9309ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
9319ee32feaSDaniel Vetter 
9329ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
933ce99c256SDaniel Vetter }
934ce99c256SDaniel Vetter 
935baf02a1fSBen Widawsky /* Unlike gen6_queue_rps_work() from which this function is originally derived,
936baf02a1fSBen Widawsky  * we must be able to deal with other PM interrupts. This is complicated because
937baf02a1fSBen Widawsky  * of the way in which we use the masks to defer the RPS work (which for
938baf02a1fSBen Widawsky  * posterity is necessary because of forcewake).
939baf02a1fSBen Widawsky  */
940baf02a1fSBen Widawsky static void hsw_pm_irq_handler(struct drm_i915_private *dev_priv,
941baf02a1fSBen Widawsky 			       u32 pm_iir)
942baf02a1fSBen Widawsky {
943baf02a1fSBen Widawsky 	unsigned long flags;
944baf02a1fSBen Widawsky 
945baf02a1fSBen Widawsky 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
9464848405cSBen Widawsky 	dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
947baf02a1fSBen Widawsky 	if (dev_priv->rps.pm_iir) {
948baf02a1fSBen Widawsky 		I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
949baf02a1fSBen Widawsky 		/* never want to mask useful interrupts. (also posting read) */
9504848405cSBen Widawsky 		WARN_ON(I915_READ_NOTRACE(GEN6_PMIMR) & ~GEN6_PM_RPS_EVENTS);
951baf02a1fSBen Widawsky 		/* TODO: if queue_work is slow, move it out of the spinlock */
952baf02a1fSBen Widawsky 		queue_work(dev_priv->wq, &dev_priv->rps.work);
953baf02a1fSBen Widawsky 	}
954baf02a1fSBen Widawsky 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
955baf02a1fSBen Widawsky 
95612638c57SBen Widawsky 	if (pm_iir & ~GEN6_PM_RPS_EVENTS) {
95712638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
95812638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
95912638c57SBen Widawsky 
96012638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
96112638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
96212638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
96312638c57SBen Widawsky 		}
96412638c57SBen Widawsky 	}
965baf02a1fSBen Widawsky }
966baf02a1fSBen Widawsky 
967ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
9687e231dbeSJesse Barnes {
9697e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
9707e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
9717e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
9727e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
9737e231dbeSJesse Barnes 	unsigned long irqflags;
9747e231dbeSJesse Barnes 	int pipe;
9757e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
9767e231dbeSJesse Barnes 
9777e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
9787e231dbeSJesse Barnes 
9797e231dbeSJesse Barnes 	while (true) {
9807e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
9817e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
9827e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
9837e231dbeSJesse Barnes 
9847e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
9857e231dbeSJesse Barnes 			goto out;
9867e231dbeSJesse Barnes 
9877e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
9887e231dbeSJesse Barnes 
989e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
9907e231dbeSJesse Barnes 
9917e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
9927e231dbeSJesse Barnes 		for_each_pipe(pipe) {
9937e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
9947e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
9957e231dbeSJesse Barnes 
9967e231dbeSJesse Barnes 			/*
9977e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
9987e231dbeSJesse Barnes 			 */
9997e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
10007e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
10017e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
10027e231dbeSJesse Barnes 							 pipe_name(pipe));
10037e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
10047e231dbeSJesse Barnes 			}
10057e231dbeSJesse Barnes 		}
10067e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
10077e231dbeSJesse Barnes 
100831acc7f5SJesse Barnes 		for_each_pipe(pipe) {
100931acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
101031acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
101131acc7f5SJesse Barnes 
101231acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
101331acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
101431acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
101531acc7f5SJesse Barnes 			}
101631acc7f5SJesse Barnes 		}
101731acc7f5SJesse Barnes 
10187e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
10197e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
10207e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1021b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
10227e231dbeSJesse Barnes 
10237e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
10247e231dbeSJesse Barnes 					 hotplug_status);
102591d131d2SDaniel Vetter 
102610a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
102791d131d2SDaniel Vetter 
10287e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
10297e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
10307e231dbeSJesse Barnes 		}
10317e231dbeSJesse Barnes 
1032515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1033515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
10347e231dbeSJesse Barnes 
10354848405cSBen Widawsky 		if (pm_iir & GEN6_PM_RPS_EVENTS)
1036fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
10377e231dbeSJesse Barnes 
10387e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
10397e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
10407e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
10417e231dbeSJesse Barnes 	}
10427e231dbeSJesse Barnes 
10437e231dbeSJesse Barnes out:
10447e231dbeSJesse Barnes 	return ret;
10457e231dbeSJesse Barnes }
10467e231dbeSJesse Barnes 
104723e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1048776ad806SJesse Barnes {
1049776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10509db4a9c7SJesse Barnes 	int pipe;
1051b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1052776ad806SJesse Barnes 
105310a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
105491d131d2SDaniel Vetter 
1055cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1056cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1057776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1058cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1059cfc33bf7SVille Syrjälä 				 port_name(port));
1060cfc33bf7SVille Syrjälä 	}
1061776ad806SJesse Barnes 
1062ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1063ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1064ce99c256SDaniel Vetter 
1065776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1066515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1067776ad806SJesse Barnes 
1068776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1069776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1070776ad806SJesse Barnes 
1071776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1072776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1073776ad806SJesse Barnes 
1074776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1075776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1076776ad806SJesse Barnes 
10779db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
10789db4a9c7SJesse Barnes 		for_each_pipe(pipe)
10799db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
10809db4a9c7SJesse Barnes 					 pipe_name(pipe),
10819db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1082776ad806SJesse Barnes 
1083776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1084776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1085776ad806SJesse Barnes 
1086776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1087776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1088776ad806SJesse Barnes 
1089776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
10908664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
10918664281bSPaulo Zanoni 							  false))
10928664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
10938664281bSPaulo Zanoni 
10948664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
10958664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
10968664281bSPaulo Zanoni 							  false))
10978664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
10988664281bSPaulo Zanoni }
10998664281bSPaulo Zanoni 
11008664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
11018664281bSPaulo Zanoni {
11028664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
11038664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
11048664281bSPaulo Zanoni 
1105de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1106de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1107de032bf4SPaulo Zanoni 
11088664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_A)
11098664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
11108664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
11118664281bSPaulo Zanoni 
11128664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_B)
11138664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
11148664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
11158664281bSPaulo Zanoni 
11168664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_C)
11178664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
11188664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
11198664281bSPaulo Zanoni 
11208664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
11218664281bSPaulo Zanoni }
11228664281bSPaulo Zanoni 
11238664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
11248664281bSPaulo Zanoni {
11258664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
11268664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
11278664281bSPaulo Zanoni 
1128de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1129de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1130de032bf4SPaulo Zanoni 
11318664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
11328664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
11338664281bSPaulo Zanoni 							  false))
11348664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
11358664281bSPaulo Zanoni 
11368664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
11378664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
11388664281bSPaulo Zanoni 							  false))
11398664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
11408664281bSPaulo Zanoni 
11418664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
11428664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
11438664281bSPaulo Zanoni 							  false))
11448664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
11458664281bSPaulo Zanoni 
11468664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1147776ad806SJesse Barnes }
1148776ad806SJesse Barnes 
114923e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
115023e81d69SAdam Jackson {
115123e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
115223e81d69SAdam Jackson 	int pipe;
1153b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
115423e81d69SAdam Jackson 
115510a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
115691d131d2SDaniel Vetter 
1157cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1158cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
115923e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1160cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1161cfc33bf7SVille Syrjälä 				 port_name(port));
1162cfc33bf7SVille Syrjälä 	}
116323e81d69SAdam Jackson 
116423e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1165ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
116623e81d69SAdam Jackson 
116723e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1168515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
116923e81d69SAdam Jackson 
117023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
117123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
117223e81d69SAdam Jackson 
117323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
117423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
117523e81d69SAdam Jackson 
117623e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
117723e81d69SAdam Jackson 		for_each_pipe(pipe)
117823e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
117923e81d69SAdam Jackson 					 pipe_name(pipe),
118023e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
11818664281bSPaulo Zanoni 
11828664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
11838664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
118423e81d69SAdam Jackson }
118523e81d69SAdam Jackson 
1186ff1f525eSDaniel Vetter static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
1187b1f14ad0SJesse Barnes {
1188b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1189b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1190ab5c608bSBen Widawsky 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
11910e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
11920e43406bSChris Wilson 	int i;
1193b1f14ad0SJesse Barnes 
1194b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1195b1f14ad0SJesse Barnes 
11968664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
11978664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
11988664281bSPaulo Zanoni 	if (IS_HASWELL(dev) &&
11998664281bSPaulo Zanoni 	    (I915_READ_NOTRACE(FPGA_DBG) & FPGA_DBG_RM_NOCLAIM)) {
12008664281bSPaulo Zanoni 		DRM_ERROR("Unclaimed register before interrupt\n");
12018664281bSPaulo Zanoni 		I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
12028664281bSPaulo Zanoni 	}
12038664281bSPaulo Zanoni 
1204b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1205b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1206b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
12070e43406bSChris Wilson 
120844498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
120944498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
121044498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
121144498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
121244498aeaSPaulo Zanoni 	 * due to its back queue). */
1213ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
121444498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
121544498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
121644498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1217ab5c608bSBen Widawsky 	}
121844498aeaSPaulo Zanoni 
12198664281bSPaulo Zanoni 	/* On Haswell, also mask ERR_INT because we don't want to risk
12208664281bSPaulo Zanoni 	 * generating "unclaimed register" interrupts from inside the interrupt
12218664281bSPaulo Zanoni 	 * handler. */
12224bc9d430SDaniel Vetter 	if (IS_HASWELL(dev)) {
12234bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
12248664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
12254bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
12264bc9d430SDaniel Vetter 	}
12278664281bSPaulo Zanoni 
12280e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
12290e43406bSChris Wilson 	if (gt_iir) {
12300e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
12310e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
12320e43406bSChris Wilson 		ret = IRQ_HANDLED;
12330e43406bSChris Wilson 	}
1234b1f14ad0SJesse Barnes 
1235b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
12360e43406bSChris Wilson 	if (de_iir) {
12378664281bSPaulo Zanoni 		if (de_iir & DE_ERR_INT_IVB)
12388664281bSPaulo Zanoni 			ivb_err_int_handler(dev);
12398664281bSPaulo Zanoni 
1240ce99c256SDaniel Vetter 		if (de_iir & DE_AUX_CHANNEL_A_IVB)
1241ce99c256SDaniel Vetter 			dp_aux_irq_handler(dev);
1242ce99c256SDaniel Vetter 
1243b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
124481a07809SJani Nikula 			intel_opregion_asle_intr(dev);
1245b1f14ad0SJesse Barnes 
12460e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
124774d44445SDaniel Vetter 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
124874d44445SDaniel Vetter 				drm_handle_vblank(dev, i);
12490e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
12500e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
12510e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
1252b1f14ad0SJesse Barnes 			}
1253b1f14ad0SJesse Barnes 		}
1254b1f14ad0SJesse Barnes 
1255b1f14ad0SJesse Barnes 		/* check event from PCH */
1256ab5c608bSBen Widawsky 		if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
12570e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
12580e43406bSChris Wilson 
125923e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
12600e43406bSChris Wilson 
12610e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
12620e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
1263b1f14ad0SJesse Barnes 		}
1264b1f14ad0SJesse Barnes 
12650e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
12660e43406bSChris Wilson 		ret = IRQ_HANDLED;
12670e43406bSChris Wilson 	}
12680e43406bSChris Wilson 
12690e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
12700e43406bSChris Wilson 	if (pm_iir) {
1271baf02a1fSBen Widawsky 		if (IS_HASWELL(dev))
1272baf02a1fSBen Widawsky 			hsw_pm_irq_handler(dev_priv, pm_iir);
12734848405cSBen Widawsky 		else if (pm_iir & GEN6_PM_RPS_EVENTS)
1274fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
1275b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
12760e43406bSChris Wilson 		ret = IRQ_HANDLED;
12770e43406bSChris Wilson 	}
1278b1f14ad0SJesse Barnes 
12794bc9d430SDaniel Vetter 	if (IS_HASWELL(dev)) {
12804bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
12814bc9d430SDaniel Vetter 		if (ivb_can_enable_err_int(dev))
12828664281bSPaulo Zanoni 			ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
12834bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
12844bc9d430SDaniel Vetter 	}
12858664281bSPaulo Zanoni 
1286b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1287b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1288ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
128944498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
129044498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1291ab5c608bSBen Widawsky 	}
1292b1f14ad0SJesse Barnes 
1293b1f14ad0SJesse Barnes 	return ret;
1294b1f14ad0SJesse Barnes }
1295b1f14ad0SJesse Barnes 
1296e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
1297e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1298e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1299e7b4c6b1SDaniel Vetter {
1300cc609d5dSBen Widawsky 	if (gt_iir &
1301cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1302e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1303cc609d5dSBen Widawsky 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1304e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1305e7b4c6b1SDaniel Vetter }
1306e7b4c6b1SDaniel Vetter 
1307ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1308036a4a7dSZhenyu Wang {
13094697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1310036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1311036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
131244498aeaSPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
1313881f47b6SXiang, Haihao 
13144697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
13154697995bSJesse Barnes 
13162d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
13172d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
13182d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
13193143a2bfSChris Wilson 	POSTING_READ(DEIER);
13202d109a84SZou, Nanhai 
132144498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
132244498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
132344498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
132444498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
132544498aeaSPaulo Zanoni 	 * due to its back queue). */
132644498aeaSPaulo Zanoni 	sde_ier = I915_READ(SDEIER);
132744498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, 0);
132844498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
132944498aeaSPaulo Zanoni 
1330036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
1331036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
13323b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
1333036a4a7dSZhenyu Wang 
1334acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
1335c7c85101SZou Nan hai 		goto done;
1336036a4a7dSZhenyu Wang 
1337036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
1338036a4a7dSZhenyu Wang 
1339e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
1340e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
1341e7b4c6b1SDaniel Vetter 	else
1342e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1343036a4a7dSZhenyu Wang 
1344ce99c256SDaniel Vetter 	if (de_iir & DE_AUX_CHANNEL_A)
1345ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1346ce99c256SDaniel Vetter 
134701c66889SZhao Yakui 	if (de_iir & DE_GSE)
134881a07809SJani Nikula 		intel_opregion_asle_intr(dev);
134901c66889SZhao Yakui 
135074d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
135174d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
135274d44445SDaniel Vetter 
135374d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
135474d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
135574d44445SDaniel Vetter 
1356de032bf4SPaulo Zanoni 	if (de_iir & DE_POISON)
1357de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1358de032bf4SPaulo Zanoni 
13598664281bSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
13608664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
13618664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
13628664281bSPaulo Zanoni 
13638664281bSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
13648664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
13658664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
13668664281bSPaulo Zanoni 
1367f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1368013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
13692bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
1370013d5aa2SJesse Barnes 	}
1371013d5aa2SJesse Barnes 
1372f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1373f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
13742bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
1375013d5aa2SJesse Barnes 	}
1376c062df61SLi Peng 
1377c650156aSZhenyu Wang 	/* check event from PCH */
1378776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
1379acd15b6cSDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
1380acd15b6cSDaniel Vetter 
138123e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
138223e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
138323e81d69SAdam Jackson 		else
138423e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
1385acd15b6cSDaniel Vetter 
1386acd15b6cSDaniel Vetter 		/* should clear PCH hotplug event before clear CPU irq */
1387acd15b6cSDaniel Vetter 		I915_WRITE(SDEIIR, pch_iir);
1388776ad806SJesse Barnes 	}
1389c650156aSZhenyu Wang 
139073edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
139173edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
1392f97108d1SJesse Barnes 
13934848405cSBen Widawsky 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_RPS_EVENTS)
1394fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
13953b8d8d91SJesse Barnes 
1396c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
1397c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
13984912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
1399036a4a7dSZhenyu Wang 
1400c7c85101SZou Nan hai done:
14012d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
14023143a2bfSChris Wilson 	POSTING_READ(DEIER);
140344498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, sde_ier);
140444498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
14052d109a84SZou, Nanhai 
1406036a4a7dSZhenyu Wang 	return ret;
1407036a4a7dSZhenyu Wang }
1408036a4a7dSZhenyu Wang 
14098a905236SJesse Barnes /**
14108a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
14118a905236SJesse Barnes  * @work: work struct
14128a905236SJesse Barnes  *
14138a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
14148a905236SJesse Barnes  * was detected.
14158a905236SJesse Barnes  */
14168a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
14178a905236SJesse Barnes {
14181f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
14191f83fee0SDaniel Vetter 						    work);
14201f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
14211f83fee0SDaniel Vetter 						    gpu_error);
14228a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1423f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
1424f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
1425f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
1426f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
1427f69061beSDaniel Vetter 	int i, ret;
14288a905236SJesse Barnes 
1429f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
14308a905236SJesse Barnes 
14317db0ba24SDaniel Vetter 	/*
14327db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
14337db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
14347db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
14357db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
14367db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
14377db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
14387db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
14397db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
14407db0ba24SDaniel Vetter 	 */
14417db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
144244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
14437db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
14447db0ba24SDaniel Vetter 				   reset_event);
14451f83fee0SDaniel Vetter 
1446f69061beSDaniel Vetter 		ret = i915_reset(dev);
1447f69061beSDaniel Vetter 
1448f69061beSDaniel Vetter 		if (ret == 0) {
1449f69061beSDaniel Vetter 			/*
1450f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1451f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1452f69061beSDaniel Vetter 			 * complete.
1453f69061beSDaniel Vetter 			 *
1454f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1455f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1456f69061beSDaniel Vetter 			 * updates before
1457f69061beSDaniel Vetter 			 * the counter increment.
1458f69061beSDaniel Vetter 			 */
1459f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1460f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1461f69061beSDaniel Vetter 
1462f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1463f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
14641f83fee0SDaniel Vetter 		} else {
14651f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1466f316a42cSBen Gamari 		}
14671f83fee0SDaniel Vetter 
1468f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
1469f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
1470f69061beSDaniel Vetter 
147196a02917SVille Syrjälä 		intel_display_handle_reset(dev);
147296a02917SVille Syrjälä 
14731f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
1474f316a42cSBen Gamari 	}
14758a905236SJesse Barnes }
14768a905236SJesse Barnes 
147785f9e50dSDaniel Vetter /* NB: please notice the memset */
147885f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
147985f9e50dSDaniel Vetter 				    uint32_t *instdone)
148085f9e50dSDaniel Vetter {
148185f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
148285f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
148385f9e50dSDaniel Vetter 
148485f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
148585f9e50dSDaniel Vetter 	case 2:
148685f9e50dSDaniel Vetter 	case 3:
148785f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
148885f9e50dSDaniel Vetter 		break;
148985f9e50dSDaniel Vetter 	case 4:
149085f9e50dSDaniel Vetter 	case 5:
149185f9e50dSDaniel Vetter 	case 6:
149285f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
149385f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
149485f9e50dSDaniel Vetter 		break;
149585f9e50dSDaniel Vetter 	default:
149685f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
149785f9e50dSDaniel Vetter 	case 7:
149885f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
149985f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
150085f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
150185f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
150285f9e50dSDaniel Vetter 		break;
150385f9e50dSDaniel Vetter 	}
150485f9e50dSDaniel Vetter }
150585f9e50dSDaniel Vetter 
15063bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
15079df30794SChris Wilson static struct drm_i915_error_object *
1508d0d045e8SBen Widawsky i915_error_object_create_sized(struct drm_i915_private *dev_priv,
1509d0d045e8SBen Widawsky 			       struct drm_i915_gem_object *src,
1510d0d045e8SBen Widawsky 			       const int num_pages)
15119df30794SChris Wilson {
15129df30794SChris Wilson 	struct drm_i915_error_object *dst;
1513d0d045e8SBen Widawsky 	int i;
1514e56660ddSChris Wilson 	u32 reloc_offset;
15159df30794SChris Wilson 
151605394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
15179df30794SChris Wilson 		return NULL;
15189df30794SChris Wilson 
1519d0d045e8SBen Widawsky 	dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
15209df30794SChris Wilson 	if (dst == NULL)
15219df30794SChris Wilson 		return NULL;
15229df30794SChris Wilson 
1523*f343c5f6SBen Widawsky 	reloc_offset = dst->gtt_offset = i915_gem_obj_ggtt_offset(src);
1524d0d045e8SBen Widawsky 	for (i = 0; i < num_pages; i++) {
1525788885aeSAndrew Morton 		unsigned long flags;
1526e56660ddSChris Wilson 		void *d;
1527788885aeSAndrew Morton 
1528e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
15299df30794SChris Wilson 		if (d == NULL)
15309df30794SChris Wilson 			goto unwind;
1531e56660ddSChris Wilson 
1532788885aeSAndrew Morton 		local_irq_save(flags);
15335d4545aeSBen Widawsky 		if (reloc_offset < dev_priv->gtt.mappable_end &&
153474898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
1535172975aaSChris Wilson 			void __iomem *s;
1536172975aaSChris Wilson 
1537172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
1538172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
1539172975aaSChris Wilson 			 * captures what the GPU read.
1540172975aaSChris Wilson 			 */
1541172975aaSChris Wilson 
15425d4545aeSBen Widawsky 			s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
15433e4d3af5SPeter Zijlstra 						     reloc_offset);
1544e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
15453e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
1546960e3564SChris Wilson 		} else if (src->stolen) {
1547960e3564SChris Wilson 			unsigned long offset;
1548960e3564SChris Wilson 
1549960e3564SChris Wilson 			offset = dev_priv->mm.stolen_base;
1550960e3564SChris Wilson 			offset += src->stolen->start;
1551960e3564SChris Wilson 			offset += i << PAGE_SHIFT;
1552960e3564SChris Wilson 
15531a240d4dSDaniel Vetter 			memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
1554172975aaSChris Wilson 		} else {
15559da3da66SChris Wilson 			struct page *page;
1556172975aaSChris Wilson 			void *s;
1557172975aaSChris Wilson 
15589da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
1559172975aaSChris Wilson 
15609da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
15619da3da66SChris Wilson 
15629da3da66SChris Wilson 			s = kmap_atomic(page);
1563172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
1564172975aaSChris Wilson 			kunmap_atomic(s);
1565172975aaSChris Wilson 
15669da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
1567172975aaSChris Wilson 		}
1568788885aeSAndrew Morton 		local_irq_restore(flags);
1569e56660ddSChris Wilson 
15709da3da66SChris Wilson 		dst->pages[i] = d;
1571e56660ddSChris Wilson 
1572e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
15739df30794SChris Wilson 	}
1574d0d045e8SBen Widawsky 	dst->page_count = num_pages;
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;
1628*f343c5f6SBen Widawsky 	err->gtt_offset = i915_gem_obj_ggtt_offset(obj);
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;
1726*f343c5f6SBen Widawsky 		if (acthd >= i915_gem_obj_ggtt_offset(obj) &&
1727*f343c5f6SBen Widawsky 		    acthd < i915_gem_obj_ggtt_offset(obj) + 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) {
1808*f343c5f6SBen Widawsky 		if ((error->ccid & PAGE_MASK) == i915_gem_obj_ggtt_offset(obj)) {
18098c123e54SBen Widawsky 			ering->ctx = i915_error_object_create_sized(dev_priv,
18108c123e54SBen Widawsky 								    obj, 1);
18113ef8fb5aSDamien Lespiau 			break;
18128c123e54SBen Widawsky 		}
18138c123e54SBen Widawsky 	}
18148c123e54SBen Widawsky }
18158c123e54SBen Widawsky 
181652d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
181752d39a21SChris Wilson 				  struct drm_i915_error_state *error)
181852d39a21SChris Wilson {
181952d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1820b4519513SChris Wilson 	struct intel_ring_buffer *ring;
182152d39a21SChris Wilson 	struct drm_i915_gem_request *request;
182252d39a21SChris Wilson 	int i, count;
182352d39a21SChris Wilson 
1824b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
182552d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
182652d39a21SChris Wilson 
182752d39a21SChris Wilson 		error->ring[i].batchbuffer =
182852d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
182952d39a21SChris Wilson 
183052d39a21SChris Wilson 		error->ring[i].ringbuffer =
183152d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
183252d39a21SChris Wilson 
18338c123e54SBen Widawsky 
18348c123e54SBen Widawsky 		i915_gem_record_active_context(ring, error, &error->ring[i]);
18358c123e54SBen Widawsky 
183652d39a21SChris Wilson 		count = 0;
183752d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
183852d39a21SChris Wilson 			count++;
183952d39a21SChris Wilson 
184052d39a21SChris Wilson 		error->ring[i].num_requests = count;
184152d39a21SChris Wilson 		error->ring[i].requests =
184252d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
184352d39a21SChris Wilson 				GFP_ATOMIC);
184452d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
184552d39a21SChris Wilson 			error->ring[i].num_requests = 0;
184652d39a21SChris Wilson 			continue;
184752d39a21SChris Wilson 		}
184852d39a21SChris Wilson 
184952d39a21SChris Wilson 		count = 0;
185052d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
185152d39a21SChris Wilson 			struct drm_i915_error_request *erq;
185252d39a21SChris Wilson 
185352d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
185452d39a21SChris Wilson 			erq->seqno = request->seqno;
185552d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1856ee4f42b1SChris Wilson 			erq->tail = request->tail;
185752d39a21SChris Wilson 		}
185852d39a21SChris Wilson 	}
185952d39a21SChris Wilson }
186052d39a21SChris Wilson 
186126b7c224SBen Widawsky static void i915_gem_capture_buffers(struct drm_i915_private *dev_priv,
186226b7c224SBen Widawsky 				     struct drm_i915_error_state *error)
186326b7c224SBen Widawsky {
186426b7c224SBen Widawsky 	struct drm_i915_gem_object *obj;
186526b7c224SBen Widawsky 	int i;
186626b7c224SBen Widawsky 
186726b7c224SBen Widawsky 	i = 0;
186826b7c224SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
186926b7c224SBen Widawsky 		i++;
187026b7c224SBen Widawsky 	error->active_bo_count = i;
187126b7c224SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
187226b7c224SBen Widawsky 		if (obj->pin_count)
187326b7c224SBen Widawsky 			i++;
187426b7c224SBen Widawsky 	error->pinned_bo_count = i - error->active_bo_count;
187526b7c224SBen Widawsky 
187626b7c224SBen Widawsky 	if (i) {
187726b7c224SBen Widawsky 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
187826b7c224SBen Widawsky 					   GFP_ATOMIC);
187926b7c224SBen Widawsky 		if (error->active_bo)
188026b7c224SBen Widawsky 			error->pinned_bo =
188126b7c224SBen Widawsky 				error->active_bo + error->active_bo_count;
188226b7c224SBen Widawsky 	}
188326b7c224SBen Widawsky 
188426b7c224SBen Widawsky 	if (error->active_bo)
188526b7c224SBen Widawsky 		error->active_bo_count =
188626b7c224SBen Widawsky 			capture_active_bo(error->active_bo,
188726b7c224SBen Widawsky 					  error->active_bo_count,
188826b7c224SBen Widawsky 					  &dev_priv->mm.active_list);
188926b7c224SBen Widawsky 
189026b7c224SBen Widawsky 	if (error->pinned_bo)
189126b7c224SBen Widawsky 		error->pinned_bo_count =
189226b7c224SBen Widawsky 			capture_pinned_bo(error->pinned_bo,
189326b7c224SBen Widawsky 					  error->pinned_bo_count,
189426b7c224SBen Widawsky 					  &dev_priv->mm.bound_list);
189526b7c224SBen Widawsky }
189626b7c224SBen Widawsky 
18978a905236SJesse Barnes /**
18988a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
18998a905236SJesse Barnes  * @dev: drm device
19008a905236SJesse Barnes  *
19018a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
19028a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
19038a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
19048a905236SJesse Barnes  * to pick up.
19058a905236SJesse Barnes  */
190663eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
190763eeaf38SJesse Barnes {
190863eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
190963eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
191063eeaf38SJesse Barnes 	unsigned long flags;
191126b7c224SBen Widawsky 	int pipe;
191263eeaf38SJesse Barnes 
191399584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
191499584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
191599584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19169df30794SChris Wilson 	if (error)
19179df30794SChris Wilson 		return;
191863eeaf38SJesse Barnes 
19199db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
192033f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
192163eeaf38SJesse Barnes 	if (!error) {
19229df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
19239df30794SChris Wilson 		return;
192463eeaf38SJesse Barnes 	}
192563eeaf38SJesse Barnes 
19262f86f191SBen Widawsky 	DRM_INFO("capturing error event; look for more information in "
1927ef86ddceSMika Kuoppala 		 "/sys/class/drm/card%d/error\n", dev->primary->index);
19282fa772f3SChris Wilson 
1929742cbee8SDaniel Vetter 	kref_init(&error->ref);
193063eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
193163eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1932211816ecSBen Widawsky 	if (HAS_HW_CONTEXTS(dev))
1933b9a3906bSBen Widawsky 		error->ccid = I915_READ(CCID);
1934be998e2eSBen Widawsky 
1935be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1936be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1937be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1938be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1939be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1940be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1941be998e2eSBen Widawsky 	else
1942be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1943be998e2eSBen Widawsky 
19440f3b6849SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6)
19450f3b6849SChris Wilson 		error->derrmr = I915_READ(DERRMR);
19460f3b6849SChris Wilson 
19470f3b6849SChris Wilson 	if (IS_VALLEYVIEW(dev))
19480f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_VLV);
19490f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 7)
19500f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_MT);
19510f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen == 6)
19520f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE);
19530f3b6849SChris Wilson 
19544f3308b9SPaulo Zanoni 	if (!HAS_PCH_SPLIT(dev))
19559db4a9c7SJesse Barnes 		for_each_pipe(pipe)
19569db4a9c7SJesse Barnes 			error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1957d27b1e0eSDaniel Vetter 
195833f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1959f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
196033f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
196133f3f518SDaniel Vetter 	}
1962add354ddSChris Wilson 
196371e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
196471e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
196571e172e8SBen Widawsky 
1966050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1967050ee91fSBen Widawsky 
196826b7c224SBen Widawsky 	i915_gem_capture_buffers(dev_priv, error);
1969748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
197052d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
19719df30794SChris Wilson 
19728a905236SJesse Barnes 	do_gettimeofday(&error->time);
19738a905236SJesse Barnes 
19746ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1975c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
19766ef3d427SChris Wilson 
197799584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
197899584db3SDaniel Vetter 	if (dev_priv->gpu_error.first_error == NULL) {
197999584db3SDaniel Vetter 		dev_priv->gpu_error.first_error = error;
19809df30794SChris Wilson 		error = NULL;
19819df30794SChris Wilson 	}
198299584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19839df30794SChris Wilson 
19849df30794SChris Wilson 	if (error)
1985742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
19869df30794SChris Wilson }
19879df30794SChris Wilson 
19889df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
19899df30794SChris Wilson {
19909df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
19919df30794SChris Wilson 	struct drm_i915_error_state *error;
19926dc0e816SBen Widawsky 	unsigned long flags;
19939df30794SChris Wilson 
199499584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
199599584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
199699584db3SDaniel Vetter 	dev_priv->gpu_error.first_error = NULL;
199799584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19989df30794SChris Wilson 
19999df30794SChris Wilson 	if (error)
2000742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
200163eeaf38SJesse Barnes }
20023bd3c932SChris Wilson #else
20033bd3c932SChris Wilson #define i915_capture_error_state(x)
20043bd3c932SChris Wilson #endif
200563eeaf38SJesse Barnes 
200635aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2007c0e09200SDave Airlie {
20088a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2009bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
201063eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2011050ee91fSBen Widawsky 	int pipe, i;
201263eeaf38SJesse Barnes 
201335aed2e6SChris Wilson 	if (!eir)
201435aed2e6SChris Wilson 		return;
201563eeaf38SJesse Barnes 
2016a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
20178a905236SJesse Barnes 
2018bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2019bd9854f9SBen Widawsky 
20208a905236SJesse Barnes 	if (IS_G4X(dev)) {
20218a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
20228a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
20238a905236SJesse Barnes 
2024a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2025a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2026050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2027050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2028a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2029a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
20308a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20313143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
20328a905236SJesse Barnes 		}
20338a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
20348a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2035a70491ccSJoe Perches 			pr_err("page table error\n");
2036a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
20378a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20383143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
20398a905236SJesse Barnes 		}
20408a905236SJesse Barnes 	}
20418a905236SJesse Barnes 
2042a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
204363eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
204463eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2045a70491ccSJoe Perches 			pr_err("page table error\n");
2046a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
204763eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20483143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
204963eeaf38SJesse Barnes 		}
20508a905236SJesse Barnes 	}
20518a905236SJesse Barnes 
205263eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2053a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
20549db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2055a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
20569db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
205763eeaf38SJesse Barnes 		/* pipestat has already been acked */
205863eeaf38SJesse Barnes 	}
205963eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2060a70491ccSJoe Perches 		pr_err("instruction error\n");
2061a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2062050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2063050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2064a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
206563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
206663eeaf38SJesse Barnes 
2067a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2068a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2069a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
207063eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
20713143a2bfSChris Wilson 			POSTING_READ(IPEIR);
207263eeaf38SJesse Barnes 		} else {
207363eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
207463eeaf38SJesse Barnes 
2075a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2076a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2077a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2078a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
207963eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20803143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
208163eeaf38SJesse Barnes 		}
208263eeaf38SJesse Barnes 	}
208363eeaf38SJesse Barnes 
208463eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
20853143a2bfSChris Wilson 	POSTING_READ(EIR);
208663eeaf38SJesse Barnes 	eir = I915_READ(EIR);
208763eeaf38SJesse Barnes 	if (eir) {
208863eeaf38SJesse Barnes 		/*
208963eeaf38SJesse Barnes 		 * some errors might have become stuck,
209063eeaf38SJesse Barnes 		 * mask them.
209163eeaf38SJesse Barnes 		 */
209263eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
209363eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
209463eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
209563eeaf38SJesse Barnes 	}
209635aed2e6SChris Wilson }
209735aed2e6SChris Wilson 
209835aed2e6SChris Wilson /**
209935aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
210035aed2e6SChris Wilson  * @dev: drm device
210135aed2e6SChris Wilson  *
210235aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
210335aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
210435aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
210535aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
210635aed2e6SChris Wilson  * of a ring dump etc.).
210735aed2e6SChris Wilson  */
2108527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
210935aed2e6SChris Wilson {
211035aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2111b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2112b4519513SChris Wilson 	int i;
211335aed2e6SChris Wilson 
211435aed2e6SChris Wilson 	i915_capture_error_state(dev);
211535aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
21168a905236SJesse Barnes 
2117ba1234d1SBen Gamari 	if (wedged) {
2118f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2119f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2120ba1234d1SBen Gamari 
212111ed50ecSBen Gamari 		/*
21221f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
21231f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
212411ed50ecSBen Gamari 		 */
2125b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
2126b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
212711ed50ecSBen Gamari 	}
212811ed50ecSBen Gamari 
212999584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
21308a905236SJesse Barnes }
21318a905236SJesse Barnes 
213221ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
21334e5359cdSSimon Farnsworth {
21344e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
21354e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
21364e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
213705394f39SChris Wilson 	struct drm_i915_gem_object *obj;
21384e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
21394e5359cdSSimon Farnsworth 	unsigned long flags;
21404e5359cdSSimon Farnsworth 	bool stall_detected;
21414e5359cdSSimon Farnsworth 
21424e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
21434e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
21444e5359cdSSimon Farnsworth 		return;
21454e5359cdSSimon Farnsworth 
21464e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
21474e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
21484e5359cdSSimon Farnsworth 
2149e7d841caSChris Wilson 	if (work == NULL ||
2150e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2151e7d841caSChris Wilson 	    !work->enable_stall_check) {
21524e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
21534e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
21544e5359cdSSimon Farnsworth 		return;
21554e5359cdSSimon Farnsworth 	}
21564e5359cdSSimon Farnsworth 
21574e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
215805394f39SChris Wilson 	obj = work->pending_flip_obj;
2159a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
21609db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2161446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2162*f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
21634e5359cdSSimon Farnsworth 	} else {
21649db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
2165*f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
216601f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
21674e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
21684e5359cdSSimon Farnsworth 	}
21694e5359cdSSimon Farnsworth 
21704e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
21714e5359cdSSimon Farnsworth 
21724e5359cdSSimon Farnsworth 	if (stall_detected) {
21734e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
21744e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
21754e5359cdSSimon Farnsworth 	}
21764e5359cdSSimon Farnsworth }
21774e5359cdSSimon Farnsworth 
217842f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
217942f52ef8SKeith Packard  * we use as a pipe index
218042f52ef8SKeith Packard  */
2181f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
21820a3e67a4SJesse Barnes {
21830a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2184e9d21d7fSKeith Packard 	unsigned long irqflags;
218571e0ffa5SJesse Barnes 
21865eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
218771e0ffa5SJesse Barnes 		return -EINVAL;
21880a3e67a4SJesse Barnes 
21891ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2190f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
21917c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21927c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
21930a3e67a4SJesse Barnes 	else
21947c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21957c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
21968692d00eSChris Wilson 
21978692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
21988692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
21996b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
22001ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22018692d00eSChris Wilson 
22020a3e67a4SJesse Barnes 	return 0;
22030a3e67a4SJesse Barnes }
22040a3e67a4SJesse Barnes 
2205f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2206f796cf8fSJesse Barnes {
2207f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2208f796cf8fSJesse Barnes 	unsigned long irqflags;
2209f796cf8fSJesse Barnes 
2210f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2211f796cf8fSJesse Barnes 		return -EINVAL;
2212f796cf8fSJesse Barnes 
2213f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2214f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
2215f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2216f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2217f796cf8fSJesse Barnes 
2218f796cf8fSJesse Barnes 	return 0;
2219f796cf8fSJesse Barnes }
2220f796cf8fSJesse Barnes 
2221f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
2222b1f14ad0SJesse Barnes {
2223b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2224b1f14ad0SJesse Barnes 	unsigned long irqflags;
2225b1f14ad0SJesse Barnes 
2226b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2227b1f14ad0SJesse Barnes 		return -EINVAL;
2228b1f14ad0SJesse Barnes 
2229b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2230b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
2231b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
2232b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2233b1f14ad0SJesse Barnes 
2234b1f14ad0SJesse Barnes 	return 0;
2235b1f14ad0SJesse Barnes }
2236b1f14ad0SJesse Barnes 
22377e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
22387e231dbeSJesse Barnes {
22397e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22407e231dbeSJesse Barnes 	unsigned long irqflags;
224131acc7f5SJesse Barnes 	u32 imr;
22427e231dbeSJesse Barnes 
22437e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
22447e231dbeSJesse Barnes 		return -EINVAL;
22457e231dbeSJesse Barnes 
22467e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22477e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
224831acc7f5SJesse Barnes 	if (pipe == 0)
22497e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
225031acc7f5SJesse Barnes 	else
22517e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22527e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
225331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
225431acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
22557e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22567e231dbeSJesse Barnes 
22577e231dbeSJesse Barnes 	return 0;
22587e231dbeSJesse Barnes }
22597e231dbeSJesse Barnes 
226042f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
226142f52ef8SKeith Packard  * we use as a pipe index
226242f52ef8SKeith Packard  */
2263f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
22640a3e67a4SJesse Barnes {
22650a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2266e9d21d7fSKeith Packard 	unsigned long irqflags;
22670a3e67a4SJesse Barnes 
22681ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22698692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
22706b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
22718692d00eSChris Wilson 
22727c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
22737c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
22747c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
22751ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22760a3e67a4SJesse Barnes }
22770a3e67a4SJesse Barnes 
2278f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2279f796cf8fSJesse Barnes {
2280f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2281f796cf8fSJesse Barnes 	unsigned long irqflags;
2282f796cf8fSJesse Barnes 
2283f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2284f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
2285f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2286f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2287f796cf8fSJesse Barnes }
2288f796cf8fSJesse Barnes 
2289f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
2290b1f14ad0SJesse Barnes {
2291b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2292b1f14ad0SJesse Barnes 	unsigned long irqflags;
2293b1f14ad0SJesse Barnes 
2294b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2295b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
2296b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
2297b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2298b1f14ad0SJesse Barnes }
2299b1f14ad0SJesse Barnes 
23007e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
23017e231dbeSJesse Barnes {
23027e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23037e231dbeSJesse Barnes 	unsigned long irqflags;
230431acc7f5SJesse Barnes 	u32 imr;
23057e231dbeSJesse Barnes 
23067e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
230731acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
230831acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
23097e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
231031acc7f5SJesse Barnes 	if (pipe == 0)
23117e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
231231acc7f5SJesse Barnes 	else
23137e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23147e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
23157e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
23167e231dbeSJesse Barnes }
23177e231dbeSJesse Barnes 
2318893eead0SChris Wilson static u32
2319893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2320852835f3SZou Nan hai {
2321893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2322893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2323893eead0SChris Wilson }
2324893eead0SChris Wilson 
23259107e9d2SChris Wilson static bool
23269107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2327893eead0SChris Wilson {
23289107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
23299107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2330f65d9421SBen Gamari }
2331f65d9421SBen Gamari 
23326274f212SChris Wilson static struct intel_ring_buffer *
23336274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2334a24a11e6SChris Wilson {
2335a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
23366274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2337a24a11e6SChris Wilson 
2338a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2339a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2340a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
23416274f212SChris Wilson 		return NULL;
2342a24a11e6SChris Wilson 
2343a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2344a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2345a24a11e6SChris Wilson 	 */
23466274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2347a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2348a24a11e6SChris Wilson 	do {
2349a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2350a24a11e6SChris Wilson 		if (cmd == ipehr)
2351a24a11e6SChris Wilson 			break;
2352a24a11e6SChris Wilson 
2353a24a11e6SChris Wilson 		acthd -= 4;
2354a24a11e6SChris Wilson 		if (acthd < acthd_min)
23556274f212SChris Wilson 			return NULL;
2356a24a11e6SChris Wilson 	} while (1);
2357a24a11e6SChris Wilson 
23586274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
23596274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2360a24a11e6SChris Wilson }
2361a24a11e6SChris Wilson 
23626274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
23636274f212SChris Wilson {
23646274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
23656274f212SChris Wilson 	struct intel_ring_buffer *signaller;
23666274f212SChris Wilson 	u32 seqno, ctl;
23676274f212SChris Wilson 
23686274f212SChris Wilson 	ring->hangcheck.deadlock = true;
23696274f212SChris Wilson 
23706274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
23716274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
23726274f212SChris Wilson 		return -1;
23736274f212SChris Wilson 
23746274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
23756274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
23766274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
23776274f212SChris Wilson 		return -1;
23786274f212SChris Wilson 
23796274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
23806274f212SChris Wilson }
23816274f212SChris Wilson 
23826274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
23836274f212SChris Wilson {
23846274f212SChris Wilson 	struct intel_ring_buffer *ring;
23856274f212SChris Wilson 	int i;
23866274f212SChris Wilson 
23876274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
23886274f212SChris Wilson 		ring->hangcheck.deadlock = false;
23896274f212SChris Wilson }
23906274f212SChris Wilson 
2391ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2392ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
23931ec14ad3SChris Wilson {
23941ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
23951ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
23969107e9d2SChris Wilson 	u32 tmp;
23979107e9d2SChris Wilson 
23986274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
23996274f212SChris Wilson 		return active;
24006274f212SChris Wilson 
24019107e9d2SChris Wilson 	if (IS_GEN2(dev))
24026274f212SChris Wilson 		return hung;
24039107e9d2SChris Wilson 
24049107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
24059107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
24069107e9d2SChris Wilson 	 * and break the hang. This should work on
24079107e9d2SChris Wilson 	 * all but the second generation chipsets.
24089107e9d2SChris Wilson 	 */
24099107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
24101ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
24111ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
24121ec14ad3SChris Wilson 			  ring->name);
24131ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
24146274f212SChris Wilson 		return kick;
24151ec14ad3SChris Wilson 	}
2416a24a11e6SChris Wilson 
24176274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
24186274f212SChris Wilson 		switch (semaphore_passed(ring)) {
24196274f212SChris Wilson 		default:
24206274f212SChris Wilson 			return hung;
24216274f212SChris Wilson 		case 1:
2422a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
2423a24a11e6SChris Wilson 				  ring->name);
2424a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
24256274f212SChris Wilson 			return kick;
24266274f212SChris Wilson 		case 0:
24276274f212SChris Wilson 			return wait;
24286274f212SChris Wilson 		}
24299107e9d2SChris Wilson 	}
24309107e9d2SChris Wilson 
24316274f212SChris Wilson 	return hung;
2432a24a11e6SChris Wilson }
2433d1e61e7fSChris Wilson 
2434f65d9421SBen Gamari /**
2435f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
243605407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
243705407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
243805407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
243905407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
244005407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2441f65d9421SBen Gamari  */
2442f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
2443f65d9421SBen Gamari {
2444f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2445f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2446b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2447b4519513SChris Wilson 	int i;
244805407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
24499107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
24509107e9d2SChris Wilson #define BUSY 1
24519107e9d2SChris Wilson #define KICK 5
24529107e9d2SChris Wilson #define HUNG 20
24539107e9d2SChris Wilson #define FIRE 30
2454893eead0SChris Wilson 
24553e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
24563e0dc6b0SBen Widawsky 		return;
24573e0dc6b0SBen Widawsky 
2458b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
245905407ff8SMika Kuoppala 		u32 seqno, acthd;
24609107e9d2SChris Wilson 		bool busy = true;
2461b4519513SChris Wilson 
24626274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
24636274f212SChris Wilson 
246405407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
246505407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
246605407ff8SMika Kuoppala 
246705407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
24689107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
24699107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
24709107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
24719107e9d2SChris Wilson 					DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
24729107e9d2SChris Wilson 						  ring->name);
24739107e9d2SChris Wilson 					wake_up_all(&ring->irq_queue);
24749107e9d2SChris Wilson 					ring->hangcheck.score += HUNG;
24759107e9d2SChris Wilson 				} else
24769107e9d2SChris Wilson 					busy = false;
247705407ff8SMika Kuoppala 			} else {
24789107e9d2SChris Wilson 				int score;
24799107e9d2SChris Wilson 
24806274f212SChris Wilson 				/* We always increment the hangcheck score
24816274f212SChris Wilson 				 * if the ring is busy and still processing
24826274f212SChris Wilson 				 * the same request, so that no single request
24836274f212SChris Wilson 				 * can run indefinitely (such as a chain of
24846274f212SChris Wilson 				 * batches). The only time we do not increment
24856274f212SChris Wilson 				 * the hangcheck score on this ring, if this
24866274f212SChris Wilson 				 * ring is in a legitimate wait for another
24876274f212SChris Wilson 				 * ring. In that case the waiting ring is a
24886274f212SChris Wilson 				 * victim and we want to be sure we catch the
24896274f212SChris Wilson 				 * right culprit. Then every time we do kick
24906274f212SChris Wilson 				 * the ring, add a small increment to the
24916274f212SChris Wilson 				 * score so that we can catch a batch that is
24926274f212SChris Wilson 				 * being repeatedly kicked and so responsible
24936274f212SChris Wilson 				 * for stalling the machine.
24949107e9d2SChris Wilson 				 */
2495ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2496ad8beaeaSMika Kuoppala 								    acthd);
2497ad8beaeaSMika Kuoppala 
2498ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
24996274f212SChris Wilson 				case wait:
25006274f212SChris Wilson 					score = 0;
25016274f212SChris Wilson 					break;
25026274f212SChris Wilson 				case active:
25039107e9d2SChris Wilson 					score = BUSY;
25046274f212SChris Wilson 					break;
25056274f212SChris Wilson 				case kick:
25066274f212SChris Wilson 					score = KICK;
25076274f212SChris Wilson 					break;
25086274f212SChris Wilson 				case hung:
25096274f212SChris Wilson 					score = HUNG;
25106274f212SChris Wilson 					stuck[i] = true;
25116274f212SChris Wilson 					break;
25126274f212SChris Wilson 				}
25139107e9d2SChris Wilson 				ring->hangcheck.score += score;
251405407ff8SMika Kuoppala 			}
25159107e9d2SChris Wilson 		} else {
25169107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
25179107e9d2SChris Wilson 			 * attempts across multiple batches.
25189107e9d2SChris Wilson 			 */
25199107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
25209107e9d2SChris Wilson 				ring->hangcheck.score--;
2521cbb465e7SChris Wilson 		}
2522f65d9421SBen Gamari 
252305407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
252405407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
25259107e9d2SChris Wilson 		busy_count += busy;
252605407ff8SMika Kuoppala 	}
252705407ff8SMika Kuoppala 
252805407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
25299107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2530acd78c11SBen Widawsky 			DRM_ERROR("%s on %s\n",
253105407ff8SMika Kuoppala 				  stuck[i] ? "stuck" : "no progress",
2532a43adf07SChris Wilson 				  ring->name);
2533a43adf07SChris Wilson 			rings_hung++;
253405407ff8SMika Kuoppala 		}
253505407ff8SMika Kuoppala 	}
253605407ff8SMika Kuoppala 
253705407ff8SMika Kuoppala 	if (rings_hung)
253805407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
253905407ff8SMika Kuoppala 
254005407ff8SMika Kuoppala 	if (busy_count)
254105407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
254205407ff8SMika Kuoppala 		 * being added */
254399584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
254405407ff8SMika Kuoppala 			  round_jiffies_up(jiffies +
254505407ff8SMika Kuoppala 					   DRM_I915_HANGCHECK_JIFFIES));
2546f65d9421SBen Gamari }
2547f65d9421SBen Gamari 
254891738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
254991738a95SPaulo Zanoni {
255091738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
255191738a95SPaulo Zanoni 
255291738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
255391738a95SPaulo Zanoni 		return;
255491738a95SPaulo Zanoni 
255591738a95SPaulo Zanoni 	/* south display irq */
255691738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
255791738a95SPaulo Zanoni 	/*
255891738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
255991738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
256091738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
256191738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
256291738a95SPaulo Zanoni 	 */
256391738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
256491738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
256591738a95SPaulo Zanoni }
256691738a95SPaulo Zanoni 
2567c0e09200SDave Airlie /* drm_dma.h hooks
2568c0e09200SDave Airlie */
2569f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2570036a4a7dSZhenyu Wang {
2571036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2572036a4a7dSZhenyu Wang 
25734697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
25744697995bSJesse Barnes 
2575036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2576bdfcdb63SDaniel Vetter 
2577036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
2578036a4a7dSZhenyu Wang 
2579036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2580036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
25813143a2bfSChris Wilson 	POSTING_READ(DEIER);
2582036a4a7dSZhenyu Wang 
2583036a4a7dSZhenyu Wang 	/* and GT */
2584036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2585036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
25863143a2bfSChris Wilson 	POSTING_READ(GTIER);
2587c650156aSZhenyu Wang 
258891738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
25897d99163dSBen Widawsky }
25907d99163dSBen Widawsky 
25917d99163dSBen Widawsky static void ivybridge_irq_preinstall(struct drm_device *dev)
25927d99163dSBen Widawsky {
25937d99163dSBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25947d99163dSBen Widawsky 
25957d99163dSBen Widawsky 	atomic_set(&dev_priv->irq_received, 0);
25967d99163dSBen Widawsky 
25977d99163dSBen Widawsky 	I915_WRITE(HWSTAM, 0xeffe);
25987d99163dSBen Widawsky 
25997d99163dSBen Widawsky 	/* XXX hotplug from PCH */
26007d99163dSBen Widawsky 
26017d99163dSBen Widawsky 	I915_WRITE(DEIMR, 0xffffffff);
26027d99163dSBen Widawsky 	I915_WRITE(DEIER, 0x0);
26037d99163dSBen Widawsky 	POSTING_READ(DEIER);
26047d99163dSBen Widawsky 
26057d99163dSBen Widawsky 	/* and GT */
26067d99163dSBen Widawsky 	I915_WRITE(GTIMR, 0xffffffff);
26077d99163dSBen Widawsky 	I915_WRITE(GTIER, 0x0);
26087d99163dSBen Widawsky 	POSTING_READ(GTIER);
26097d99163dSBen Widawsky 
2610eda63ffbSBen Widawsky 	/* Power management */
2611eda63ffbSBen Widawsky 	I915_WRITE(GEN6_PMIMR, 0xffffffff);
2612eda63ffbSBen Widawsky 	I915_WRITE(GEN6_PMIER, 0x0);
2613eda63ffbSBen Widawsky 	POSTING_READ(GEN6_PMIER);
2614eda63ffbSBen Widawsky 
261591738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
2616036a4a7dSZhenyu Wang }
2617036a4a7dSZhenyu Wang 
26187e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
26197e231dbeSJesse Barnes {
26207e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26217e231dbeSJesse Barnes 	int pipe;
26227e231dbeSJesse Barnes 
26237e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
26247e231dbeSJesse Barnes 
26257e231dbeSJesse Barnes 	/* VLV magic */
26267e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
26277e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
26287e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
26297e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
26307e231dbeSJesse Barnes 
26317e231dbeSJesse Barnes 	/* and GT */
26327e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
26337e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
26347e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
26357e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
26367e231dbeSJesse Barnes 	POSTING_READ(GTIER);
26377e231dbeSJesse Barnes 
26387e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
26397e231dbeSJesse Barnes 
26407e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
26417e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
26427e231dbeSJesse Barnes 	for_each_pipe(pipe)
26437e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
26447e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26457e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
26467e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
26477e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
26487e231dbeSJesse Barnes }
26497e231dbeSJesse Barnes 
265082a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
265182a28bcfSDaniel Vetter {
265282a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
265382a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
265482a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
265582a28bcfSDaniel Vetter 	u32 mask = ~I915_READ(SDEIMR);
265682a28bcfSDaniel Vetter 	u32 hotplug;
265782a28bcfSDaniel Vetter 
265882a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2659995e6b3dSEgbert Eich 		mask &= ~SDE_HOTPLUG_MASK;
266082a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2661cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
266282a28bcfSDaniel Vetter 				mask |= hpd_ibx[intel_encoder->hpd_pin];
266382a28bcfSDaniel Vetter 	} else {
2664995e6b3dSEgbert Eich 		mask &= ~SDE_HOTPLUG_MASK_CPT;
266582a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2666cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
266782a28bcfSDaniel Vetter 				mask |= hpd_cpt[intel_encoder->hpd_pin];
266882a28bcfSDaniel Vetter 	}
266982a28bcfSDaniel Vetter 
267082a28bcfSDaniel Vetter 	I915_WRITE(SDEIMR, ~mask);
267182a28bcfSDaniel Vetter 
26727fe0b973SKeith Packard 	/*
26737fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
26747fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
26757fe0b973SKeith Packard 	 *
26767fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
26777fe0b973SKeith Packard 	 */
26787fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
26797fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
26807fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
26817fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
26827fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
26837fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
26847fe0b973SKeith Packard }
26857fe0b973SKeith Packard 
2686d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2687d46da437SPaulo Zanoni {
2688d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
268982a28bcfSDaniel Vetter 	u32 mask;
2690d46da437SPaulo Zanoni 
2691692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2692692a04cfSDaniel Vetter 		return;
2693692a04cfSDaniel Vetter 
26948664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
26958664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2696de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
26978664281bSPaulo Zanoni 	} else {
26988664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
26998664281bSPaulo Zanoni 
27008664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
27018664281bSPaulo Zanoni 	}
2702ab5c608bSBen Widawsky 
2703d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2704d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2705d46da437SPaulo Zanoni }
2706d46da437SPaulo Zanoni 
2707f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2708036a4a7dSZhenyu Wang {
27094bc9d430SDaniel Vetter 	unsigned long irqflags;
27104bc9d430SDaniel Vetter 
2711036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2712036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
2713013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2714ce99c256SDaniel Vetter 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
27158664281bSPaulo Zanoni 			   DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
2716de032bf4SPaulo Zanoni 			   DE_PIPEA_FIFO_UNDERRUN | DE_POISON;
2717cc609d5dSBen Widawsky 	u32 gt_irqs;
2718036a4a7dSZhenyu Wang 
27191ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2720036a4a7dSZhenyu Wang 
2721036a4a7dSZhenyu Wang 	/* should always can generate irq */
2722036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
27231ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
27246005ce42SDaniel Vetter 	I915_WRITE(DEIER, display_mask |
27256005ce42SDaniel Vetter 			  DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT);
27263143a2bfSChris Wilson 	POSTING_READ(DEIER);
2727036a4a7dSZhenyu Wang 
27281ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
2729036a4a7dSZhenyu Wang 
2730036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
27311ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2732881f47b6SXiang, Haihao 
2733cc609d5dSBen Widawsky 	gt_irqs = GT_RENDER_USER_INTERRUPT;
2734cc609d5dSBen Widawsky 
27351ec14ad3SChris Wilson 	if (IS_GEN6(dev))
2736cc609d5dSBen Widawsky 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
27371ec14ad3SChris Wilson 	else
2738cc609d5dSBen Widawsky 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
2739cc609d5dSBen Widawsky 			   ILK_BSD_USER_INTERRUPT;
2740cc609d5dSBen Widawsky 
2741cc609d5dSBen Widawsky 	I915_WRITE(GTIER, gt_irqs);
27423143a2bfSChris Wilson 	POSTING_READ(GTIER);
2743036a4a7dSZhenyu Wang 
2744d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
27457fe0b973SKeith Packard 
2746f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
27476005ce42SDaniel Vetter 		/* Enable PCU event interrupts
27486005ce42SDaniel Vetter 		 *
27496005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
27504bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
27514bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
27524bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2753f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
27544bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2755f97108d1SJesse Barnes 	}
2756f97108d1SJesse Barnes 
2757036a4a7dSZhenyu Wang 	return 0;
2758036a4a7dSZhenyu Wang }
2759036a4a7dSZhenyu Wang 
2760f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2761b1f14ad0SJesse Barnes {
2762b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2763b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2764b615b57aSChris Wilson 	u32 display_mask =
2765b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2766b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
2767b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
2768ce99c256SDaniel Vetter 		DE_PLANEA_FLIP_DONE_IVB |
27698664281bSPaulo Zanoni 		DE_AUX_CHANNEL_A_IVB |
27708664281bSPaulo Zanoni 		DE_ERR_INT_IVB;
277112638c57SBen Widawsky 	u32 pm_irqs = GEN6_PM_RPS_EVENTS;
2772cc609d5dSBen Widawsky 	u32 gt_irqs;
2773b1f14ad0SJesse Barnes 
2774b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2775b1f14ad0SJesse Barnes 
2776b1f14ad0SJesse Barnes 	/* should always can generate irq */
27778664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2778b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2779b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2780b615b57aSChris Wilson 	I915_WRITE(DEIER,
2781b615b57aSChris Wilson 		   display_mask |
2782b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
2783b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
2784b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
2785b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2786b1f14ad0SJesse Barnes 
2787cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2788b1f14ad0SJesse Barnes 
2789b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2790b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2791b1f14ad0SJesse Barnes 
2792cc609d5dSBen Widawsky 	gt_irqs = GT_RENDER_USER_INTERRUPT | GT_BSD_USER_INTERRUPT |
2793cc609d5dSBen Widawsky 		  GT_BLT_USER_INTERRUPT | GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2794cc609d5dSBen Widawsky 	I915_WRITE(GTIER, gt_irqs);
2795b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2796b1f14ad0SJesse Barnes 
279712638c57SBen Widawsky 	I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
279812638c57SBen Widawsky 	if (HAS_VEBOX(dev))
279912638c57SBen Widawsky 		pm_irqs |= PM_VEBOX_USER_INTERRUPT |
280012638c57SBen Widawsky 			PM_VEBOX_CS_ERROR_INTERRUPT;
280112638c57SBen Widawsky 
280212638c57SBen Widawsky 	/* Our enable/disable rps functions may touch these registers so
280312638c57SBen Widawsky 	 * make sure to set a known state for only the non-RPS bits.
280412638c57SBen Widawsky 	 * The RMW is extra paranoia since this should be called after being set
280512638c57SBen Widawsky 	 * to a known state in preinstall.
280612638c57SBen Widawsky 	 * */
280712638c57SBen Widawsky 	I915_WRITE(GEN6_PMIMR,
280812638c57SBen Widawsky 		   (I915_READ(GEN6_PMIMR) | ~GEN6_PM_RPS_EVENTS) & ~pm_irqs);
280912638c57SBen Widawsky 	I915_WRITE(GEN6_PMIER,
281012638c57SBen Widawsky 		   (I915_READ(GEN6_PMIER) & GEN6_PM_RPS_EVENTS) | pm_irqs);
281112638c57SBen Widawsky 	POSTING_READ(GEN6_PMIER);
2812eda63ffbSBen Widawsky 
2813d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
28147fe0b973SKeith Packard 
2815b1f14ad0SJesse Barnes 	return 0;
2816b1f14ad0SJesse Barnes }
2817b1f14ad0SJesse Barnes 
28187e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
28197e231dbeSJesse Barnes {
28207e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2821cc609d5dSBen Widawsky 	u32 gt_irqs;
28227e231dbeSJesse Barnes 	u32 enable_mask;
282331acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
2824b79480baSDaniel Vetter 	unsigned long irqflags;
28257e231dbeSJesse Barnes 
28267e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
282731acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
282831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
282931acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
28307e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
28317e231dbeSJesse Barnes 
283231acc7f5SJesse Barnes 	/*
283331acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
283431acc7f5SJesse Barnes 	 * toggle them based on usage.
283531acc7f5SJesse Barnes 	 */
283631acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
283731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
283831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
28397e231dbeSJesse Barnes 
284020afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
284120afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
284220afbda2SDaniel Vetter 
28437e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
28447e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
28457e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28467e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
28477e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
28487e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
28497e231dbeSJesse Barnes 
2850b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2851b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2852b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
285331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2854515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
285531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
2856b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
285731acc7f5SJesse Barnes 
28587e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28597e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28607e231dbeSJesse Barnes 
286131acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
286231acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
28633bcedbe5SJesse Barnes 
2864cc609d5dSBen Widawsky 	gt_irqs = GT_RENDER_USER_INTERRUPT | GT_BSD_USER_INTERRUPT |
2865cc609d5dSBen Widawsky 		GT_BLT_USER_INTERRUPT;
2866cc609d5dSBen Widawsky 	I915_WRITE(GTIER, gt_irqs);
28677e231dbeSJesse Barnes 	POSTING_READ(GTIER);
28687e231dbeSJesse Barnes 
28697e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
28707e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
28717e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
28727e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
28737e231dbeSJesse Barnes #endif
28747e231dbeSJesse Barnes 
28757e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
287620afbda2SDaniel Vetter 
287720afbda2SDaniel Vetter 	return 0;
287820afbda2SDaniel Vetter }
287920afbda2SDaniel Vetter 
28807e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
28817e231dbeSJesse Barnes {
28827e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
28837e231dbeSJesse Barnes 	int pipe;
28847e231dbeSJesse Barnes 
28857e231dbeSJesse Barnes 	if (!dev_priv)
28867e231dbeSJesse Barnes 		return;
28877e231dbeSJesse Barnes 
2888ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2889ac4c16c5SEgbert Eich 
28907e231dbeSJesse Barnes 	for_each_pipe(pipe)
28917e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
28927e231dbeSJesse Barnes 
28937e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
28947e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
28957e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
28967e231dbeSJesse Barnes 	for_each_pipe(pipe)
28977e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
28987e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28997e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
29007e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
29017e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
29027e231dbeSJesse Barnes }
29037e231dbeSJesse Barnes 
2904f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2905036a4a7dSZhenyu Wang {
2906036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29074697995bSJesse Barnes 
29084697995bSJesse Barnes 	if (!dev_priv)
29094697995bSJesse Barnes 		return;
29104697995bSJesse Barnes 
2911ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2912ac4c16c5SEgbert Eich 
2913036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2914036a4a7dSZhenyu Wang 
2915036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2916036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2917036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
29188664281bSPaulo Zanoni 	if (IS_GEN7(dev))
29198664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2920036a4a7dSZhenyu Wang 
2921036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2922036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2923036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2924192aac1fSKeith Packard 
2925ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2926ab5c608bSBen Widawsky 		return;
2927ab5c608bSBen Widawsky 
2928192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2929192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2930192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
29318664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
29328664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2933036a4a7dSZhenyu Wang }
2934036a4a7dSZhenyu Wang 
2935c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2936c2798b19SChris Wilson {
2937c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2938c2798b19SChris Wilson 	int pipe;
2939c2798b19SChris Wilson 
2940c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2941c2798b19SChris Wilson 
2942c2798b19SChris Wilson 	for_each_pipe(pipe)
2943c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2944c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2945c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2946c2798b19SChris Wilson 	POSTING_READ16(IER);
2947c2798b19SChris Wilson }
2948c2798b19SChris Wilson 
2949c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2950c2798b19SChris Wilson {
2951c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2952c2798b19SChris Wilson 
2953c2798b19SChris Wilson 	I915_WRITE16(EMR,
2954c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2955c2798b19SChris Wilson 
2956c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2957c2798b19SChris Wilson 	dev_priv->irq_mask =
2958c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2959c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2960c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2961c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2962c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2963c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2964c2798b19SChris Wilson 
2965c2798b19SChris Wilson 	I915_WRITE16(IER,
2966c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2967c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2968c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2969c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2970c2798b19SChris Wilson 	POSTING_READ16(IER);
2971c2798b19SChris Wilson 
2972c2798b19SChris Wilson 	return 0;
2973c2798b19SChris Wilson }
2974c2798b19SChris Wilson 
297590a72f87SVille Syrjälä /*
297690a72f87SVille Syrjälä  * Returns true when a page flip has completed.
297790a72f87SVille Syrjälä  */
297890a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
297990a72f87SVille Syrjälä 			       int pipe, u16 iir)
298090a72f87SVille Syrjälä {
298190a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
298290a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
298390a72f87SVille Syrjälä 
298490a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
298590a72f87SVille Syrjälä 		return false;
298690a72f87SVille Syrjälä 
298790a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
298890a72f87SVille Syrjälä 		return false;
298990a72f87SVille Syrjälä 
299090a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
299190a72f87SVille Syrjälä 
299290a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
299390a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
299490a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
299590a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
299690a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
299790a72f87SVille Syrjälä 	 */
299890a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
299990a72f87SVille Syrjälä 		return false;
300090a72f87SVille Syrjälä 
300190a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
300290a72f87SVille Syrjälä 
300390a72f87SVille Syrjälä 	return true;
300490a72f87SVille Syrjälä }
300590a72f87SVille Syrjälä 
3006ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3007c2798b19SChris Wilson {
3008c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3009c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3010c2798b19SChris Wilson 	u16 iir, new_iir;
3011c2798b19SChris Wilson 	u32 pipe_stats[2];
3012c2798b19SChris Wilson 	unsigned long irqflags;
3013c2798b19SChris Wilson 	int irq_received;
3014c2798b19SChris Wilson 	int pipe;
3015c2798b19SChris Wilson 	u16 flip_mask =
3016c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3017c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3018c2798b19SChris Wilson 
3019c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3020c2798b19SChris Wilson 
3021c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3022c2798b19SChris Wilson 	if (iir == 0)
3023c2798b19SChris Wilson 		return IRQ_NONE;
3024c2798b19SChris Wilson 
3025c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3026c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3027c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3028c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3029c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3030c2798b19SChris Wilson 		 */
3031c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3032c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3033c2798b19SChris Wilson 			i915_handle_error(dev, false);
3034c2798b19SChris Wilson 
3035c2798b19SChris Wilson 		for_each_pipe(pipe) {
3036c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3037c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3038c2798b19SChris Wilson 
3039c2798b19SChris Wilson 			/*
3040c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3041c2798b19SChris Wilson 			 */
3042c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3043c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3044c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3045c2798b19SChris Wilson 							 pipe_name(pipe));
3046c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3047c2798b19SChris Wilson 				irq_received = 1;
3048c2798b19SChris Wilson 			}
3049c2798b19SChris Wilson 		}
3050c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3051c2798b19SChris Wilson 
3052c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3053c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3054c2798b19SChris Wilson 
3055d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3056c2798b19SChris Wilson 
3057c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3058c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3059c2798b19SChris Wilson 
3060c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
306190a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
306290a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
3063c2798b19SChris Wilson 
3064c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
306590a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
306690a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
3067c2798b19SChris Wilson 
3068c2798b19SChris Wilson 		iir = new_iir;
3069c2798b19SChris Wilson 	}
3070c2798b19SChris Wilson 
3071c2798b19SChris Wilson 	return IRQ_HANDLED;
3072c2798b19SChris Wilson }
3073c2798b19SChris Wilson 
3074c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3075c2798b19SChris Wilson {
3076c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3077c2798b19SChris Wilson 	int pipe;
3078c2798b19SChris Wilson 
3079c2798b19SChris Wilson 	for_each_pipe(pipe) {
3080c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3081c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3082c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3083c2798b19SChris Wilson 	}
3084c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3085c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3086c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3087c2798b19SChris Wilson }
3088c2798b19SChris Wilson 
3089a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3090a266c7d5SChris Wilson {
3091a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3092a266c7d5SChris Wilson 	int pipe;
3093a266c7d5SChris Wilson 
3094a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3095a266c7d5SChris Wilson 
3096a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3097a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3098a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3099a266c7d5SChris Wilson 	}
3100a266c7d5SChris Wilson 
310100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3102a266c7d5SChris Wilson 	for_each_pipe(pipe)
3103a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3104a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3105a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3106a266c7d5SChris Wilson 	POSTING_READ(IER);
3107a266c7d5SChris Wilson }
3108a266c7d5SChris Wilson 
3109a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3110a266c7d5SChris Wilson {
3111a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
311238bde180SChris Wilson 	u32 enable_mask;
3113a266c7d5SChris Wilson 
311438bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
311538bde180SChris Wilson 
311638bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
311738bde180SChris Wilson 	dev_priv->irq_mask =
311838bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
311938bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
312038bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
312138bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
312238bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
312338bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
312438bde180SChris Wilson 
312538bde180SChris Wilson 	enable_mask =
312638bde180SChris Wilson 		I915_ASLE_INTERRUPT |
312738bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
312838bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
312938bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
313038bde180SChris Wilson 		I915_USER_INTERRUPT;
313138bde180SChris Wilson 
3132a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
313320afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
313420afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
313520afbda2SDaniel Vetter 
3136a266c7d5SChris Wilson 		/* Enable in IER... */
3137a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3138a266c7d5SChris Wilson 		/* and unmask in IMR */
3139a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3140a266c7d5SChris Wilson 	}
3141a266c7d5SChris Wilson 
3142a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3143a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3144a266c7d5SChris Wilson 	POSTING_READ(IER);
3145a266c7d5SChris Wilson 
3146f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
314720afbda2SDaniel Vetter 
314820afbda2SDaniel Vetter 	return 0;
314920afbda2SDaniel Vetter }
315020afbda2SDaniel Vetter 
315190a72f87SVille Syrjälä /*
315290a72f87SVille Syrjälä  * Returns true when a page flip has completed.
315390a72f87SVille Syrjälä  */
315490a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
315590a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
315690a72f87SVille Syrjälä {
315790a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
315890a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
315990a72f87SVille Syrjälä 
316090a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
316190a72f87SVille Syrjälä 		return false;
316290a72f87SVille Syrjälä 
316390a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
316490a72f87SVille Syrjälä 		return false;
316590a72f87SVille Syrjälä 
316690a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
316790a72f87SVille Syrjälä 
316890a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
316990a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
317090a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
317190a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
317290a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
317390a72f87SVille Syrjälä 	 */
317490a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
317590a72f87SVille Syrjälä 		return false;
317690a72f87SVille Syrjälä 
317790a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
317890a72f87SVille Syrjälä 
317990a72f87SVille Syrjälä 	return true;
318090a72f87SVille Syrjälä }
318190a72f87SVille Syrjälä 
3182ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3183a266c7d5SChris Wilson {
3184a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3185a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
31868291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3187a266c7d5SChris Wilson 	unsigned long irqflags;
318838bde180SChris Wilson 	u32 flip_mask =
318938bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
319038bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
319138bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3192a266c7d5SChris Wilson 
3193a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3194a266c7d5SChris Wilson 
3195a266c7d5SChris Wilson 	iir = I915_READ(IIR);
319638bde180SChris Wilson 	do {
319738bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
31988291ee90SChris Wilson 		bool blc_event = false;
3199a266c7d5SChris Wilson 
3200a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3201a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3202a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3203a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3204a266c7d5SChris Wilson 		 */
3205a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3206a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3207a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3208a266c7d5SChris Wilson 
3209a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3210a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3211a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3212a266c7d5SChris Wilson 
321338bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3214a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3215a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3216a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3217a266c7d5SChris Wilson 							 pipe_name(pipe));
3218a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
321938bde180SChris Wilson 				irq_received = true;
3220a266c7d5SChris Wilson 			}
3221a266c7d5SChris Wilson 		}
3222a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3223a266c7d5SChris Wilson 
3224a266c7d5SChris Wilson 		if (!irq_received)
3225a266c7d5SChris Wilson 			break;
3226a266c7d5SChris Wilson 
3227a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3228a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3229a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3230a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3231b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3232a266c7d5SChris Wilson 
3233a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3234a266c7d5SChris Wilson 				  hotplug_status);
323591d131d2SDaniel Vetter 
323610a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
323791d131d2SDaniel Vetter 
3238a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
323938bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3240a266c7d5SChris Wilson 		}
3241a266c7d5SChris Wilson 
324238bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3243a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3244a266c7d5SChris Wilson 
3245a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3246a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3247a266c7d5SChris Wilson 
3248a266c7d5SChris Wilson 		for_each_pipe(pipe) {
324938bde180SChris Wilson 			int plane = pipe;
325038bde180SChris Wilson 			if (IS_MOBILE(dev))
325138bde180SChris Wilson 				plane = !plane;
32525e2032d4SVille Syrjälä 
325390a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
325490a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
325590a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3256a266c7d5SChris Wilson 
3257a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3258a266c7d5SChris Wilson 				blc_event = true;
3259a266c7d5SChris Wilson 		}
3260a266c7d5SChris Wilson 
3261a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3262a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3263a266c7d5SChris Wilson 
3264a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3265a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3266a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3267a266c7d5SChris Wilson 		 * we would never get another interrupt.
3268a266c7d5SChris Wilson 		 *
3269a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3270a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3271a266c7d5SChris Wilson 		 * another one.
3272a266c7d5SChris Wilson 		 *
3273a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3274a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3275a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3276a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3277a266c7d5SChris Wilson 		 * stray interrupts.
3278a266c7d5SChris Wilson 		 */
327938bde180SChris Wilson 		ret = IRQ_HANDLED;
3280a266c7d5SChris Wilson 		iir = new_iir;
328138bde180SChris Wilson 	} while (iir & ~flip_mask);
3282a266c7d5SChris Wilson 
3283d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
32848291ee90SChris Wilson 
3285a266c7d5SChris Wilson 	return ret;
3286a266c7d5SChris Wilson }
3287a266c7d5SChris Wilson 
3288a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3289a266c7d5SChris Wilson {
3290a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3291a266c7d5SChris Wilson 	int pipe;
3292a266c7d5SChris Wilson 
3293ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3294ac4c16c5SEgbert Eich 
3295a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3296a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3297a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3298a266c7d5SChris Wilson 	}
3299a266c7d5SChris Wilson 
330000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
330155b39755SChris Wilson 	for_each_pipe(pipe) {
330255b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3303a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
330455b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
330555b39755SChris Wilson 	}
3306a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3307a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3308a266c7d5SChris Wilson 
3309a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3310a266c7d5SChris Wilson }
3311a266c7d5SChris Wilson 
3312a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3313a266c7d5SChris Wilson {
3314a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3315a266c7d5SChris Wilson 	int pipe;
3316a266c7d5SChris Wilson 
3317a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3318a266c7d5SChris Wilson 
3319a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3320a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3321a266c7d5SChris Wilson 
3322a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3323a266c7d5SChris Wilson 	for_each_pipe(pipe)
3324a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3325a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3326a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3327a266c7d5SChris Wilson 	POSTING_READ(IER);
3328a266c7d5SChris Wilson }
3329a266c7d5SChris Wilson 
3330a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3331a266c7d5SChris Wilson {
3332a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3333bbba0a97SChris Wilson 	u32 enable_mask;
3334a266c7d5SChris Wilson 	u32 error_mask;
3335b79480baSDaniel Vetter 	unsigned long irqflags;
3336a266c7d5SChris Wilson 
3337a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3338bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3339adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3340bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3341bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3342bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3343bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3344bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3345bbba0a97SChris Wilson 
3346bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
334721ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
334821ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3349bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3350bbba0a97SChris Wilson 
3351bbba0a97SChris Wilson 	if (IS_G4X(dev))
3352bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3353a266c7d5SChris Wilson 
3354b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3355b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3356b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3357515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
3358b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3359a266c7d5SChris Wilson 
3360a266c7d5SChris Wilson 	/*
3361a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3362a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3363a266c7d5SChris Wilson 	 */
3364a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3365a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3366a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3367a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3368a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3369a266c7d5SChris Wilson 	} else {
3370a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3371a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3372a266c7d5SChris Wilson 	}
3373a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3374a266c7d5SChris Wilson 
3375a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3376a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3377a266c7d5SChris Wilson 	POSTING_READ(IER);
3378a266c7d5SChris Wilson 
337920afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
338020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
338120afbda2SDaniel Vetter 
3382f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
338320afbda2SDaniel Vetter 
338420afbda2SDaniel Vetter 	return 0;
338520afbda2SDaniel Vetter }
338620afbda2SDaniel Vetter 
3387bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
338820afbda2SDaniel Vetter {
338920afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3390e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3391cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
339220afbda2SDaniel Vetter 	u32 hotplug_en;
339320afbda2SDaniel Vetter 
3394b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3395b5ea2d56SDaniel Vetter 
3396bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3397bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3398bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3399adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3400e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3401cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3402cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3403cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3404a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3405a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3406a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3407a266c7d5SChris Wilson 		*/
3408a266c7d5SChris Wilson 		if (IS_G4X(dev))
3409a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
341085fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3411a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3412a266c7d5SChris Wilson 
3413a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3414a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3415a266c7d5SChris Wilson 	}
3416bac56d5bSEgbert Eich }
3417a266c7d5SChris Wilson 
3418ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3419a266c7d5SChris Wilson {
3420a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3421a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3422a266c7d5SChris Wilson 	u32 iir, new_iir;
3423a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3424a266c7d5SChris Wilson 	unsigned long irqflags;
3425a266c7d5SChris Wilson 	int irq_received;
3426a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
342721ad8330SVille Syrjälä 	u32 flip_mask =
342821ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
342921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3430a266c7d5SChris Wilson 
3431a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3432a266c7d5SChris Wilson 
3433a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3434a266c7d5SChris Wilson 
3435a266c7d5SChris Wilson 	for (;;) {
34362c8ba29fSChris Wilson 		bool blc_event = false;
34372c8ba29fSChris Wilson 
343821ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3439a266c7d5SChris Wilson 
3440a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3441a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3442a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3443a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3444a266c7d5SChris Wilson 		 */
3445a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3446a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3447a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3448a266c7d5SChris Wilson 
3449a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3450a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3451a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3452a266c7d5SChris Wilson 
3453a266c7d5SChris Wilson 			/*
3454a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3455a266c7d5SChris Wilson 			 */
3456a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3457a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3458a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3459a266c7d5SChris Wilson 							 pipe_name(pipe));
3460a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3461a266c7d5SChris Wilson 				irq_received = 1;
3462a266c7d5SChris Wilson 			}
3463a266c7d5SChris Wilson 		}
3464a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3465a266c7d5SChris Wilson 
3466a266c7d5SChris Wilson 		if (!irq_received)
3467a266c7d5SChris Wilson 			break;
3468a266c7d5SChris Wilson 
3469a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3470a266c7d5SChris Wilson 
3471a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3472adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3473a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3474b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3475b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
34764f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3477a266c7d5SChris Wilson 
3478a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3479a266c7d5SChris Wilson 				  hotplug_status);
348091d131d2SDaniel Vetter 
348110a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
348210a504deSDaniel Vetter 					      IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
348391d131d2SDaniel Vetter 
3484a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3485a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3486a266c7d5SChris Wilson 		}
3487a266c7d5SChris Wilson 
348821ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3489a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3490a266c7d5SChris Wilson 
3491a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3492a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3493a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3494a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3495a266c7d5SChris Wilson 
3496a266c7d5SChris Wilson 		for_each_pipe(pipe) {
34972c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
349890a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
349990a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3500a266c7d5SChris Wilson 
3501a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3502a266c7d5SChris Wilson 				blc_event = true;
3503a266c7d5SChris Wilson 		}
3504a266c7d5SChris Wilson 
3505a266c7d5SChris Wilson 
3506a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3507a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3508a266c7d5SChris Wilson 
3509515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3510515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3511515ac2bbSDaniel Vetter 
3512a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3513a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3514a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3515a266c7d5SChris Wilson 		 * we would never get another interrupt.
3516a266c7d5SChris Wilson 		 *
3517a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3518a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3519a266c7d5SChris Wilson 		 * another one.
3520a266c7d5SChris Wilson 		 *
3521a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3522a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3523a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3524a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3525a266c7d5SChris Wilson 		 * stray interrupts.
3526a266c7d5SChris Wilson 		 */
3527a266c7d5SChris Wilson 		iir = new_iir;
3528a266c7d5SChris Wilson 	}
3529a266c7d5SChris Wilson 
3530d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
35312c8ba29fSChris Wilson 
3532a266c7d5SChris Wilson 	return ret;
3533a266c7d5SChris Wilson }
3534a266c7d5SChris Wilson 
3535a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3536a266c7d5SChris Wilson {
3537a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3538a266c7d5SChris Wilson 	int pipe;
3539a266c7d5SChris Wilson 
3540a266c7d5SChris Wilson 	if (!dev_priv)
3541a266c7d5SChris Wilson 		return;
3542a266c7d5SChris Wilson 
3543ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3544ac4c16c5SEgbert Eich 
3545a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3546a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3547a266c7d5SChris Wilson 
3548a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3549a266c7d5SChris Wilson 	for_each_pipe(pipe)
3550a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3551a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3552a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3553a266c7d5SChris Wilson 
3554a266c7d5SChris Wilson 	for_each_pipe(pipe)
3555a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3556a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3557a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3558a266c7d5SChris Wilson }
3559a266c7d5SChris Wilson 
3560ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3561ac4c16c5SEgbert Eich {
3562ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3563ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3564ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3565ac4c16c5SEgbert Eich 	unsigned long irqflags;
3566ac4c16c5SEgbert Eich 	int i;
3567ac4c16c5SEgbert Eich 
3568ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3569ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3570ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3571ac4c16c5SEgbert Eich 
3572ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3573ac4c16c5SEgbert Eich 			continue;
3574ac4c16c5SEgbert Eich 
3575ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3576ac4c16c5SEgbert Eich 
3577ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3578ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3579ac4c16c5SEgbert Eich 
3580ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3581ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3582ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3583ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3584ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3585ac4c16c5SEgbert Eich 				if (!connector->polled)
3586ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3587ac4c16c5SEgbert Eich 			}
3588ac4c16c5SEgbert Eich 		}
3589ac4c16c5SEgbert Eich 	}
3590ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3591ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3592ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3593ac4c16c5SEgbert Eich }
3594ac4c16c5SEgbert Eich 
3595f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3596f71d4af4SJesse Barnes {
35978b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
35988b2e326dSChris Wilson 
35998b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
360099584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3601c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3602a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
36038b2e326dSChris Wilson 
360499584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
360599584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
360661bac78eSDaniel Vetter 		    (unsigned long) dev);
3607ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3608ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
360961bac78eSDaniel Vetter 
361097a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
36119ee32feaSDaniel Vetter 
3612f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
3613f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
36147d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3615f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3616f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3617f71d4af4SJesse Barnes 	}
3618f71d4af4SJesse Barnes 
3619c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3620f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3621c3613de9SKeith Packard 	else
3622c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
3623f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3624f71d4af4SJesse Barnes 
36257e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
36267e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
36277e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
36287e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
36297e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
36307e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
36317e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3632fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
36334a06e201SDaniel Vetter 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
36347d99163dSBen Widawsky 		/* Share uninstall handlers with ILK/SNB */
3635f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
36367d99163dSBen Widawsky 		dev->driver->irq_preinstall = ivybridge_irq_preinstall;
3637f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
3638f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3639f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
3640f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
364182a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3642f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3643f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3644f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3645f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3646f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3647f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3648f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
364982a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3650f71d4af4SJesse Barnes 	} else {
3651c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3652c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3653c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3654c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3655c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3656a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3657a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3658a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3659a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3660a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
366120afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3662c2798b19SChris Wilson 		} else {
3663a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3664a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3665a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3666a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3667bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3668c2798b19SChris Wilson 		}
3669f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3670f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3671f71d4af4SJesse Barnes 	}
3672f71d4af4SJesse Barnes }
367320afbda2SDaniel Vetter 
367420afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
367520afbda2SDaniel Vetter {
367620afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3677821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3678821450c6SEgbert Eich 	struct drm_connector *connector;
3679b5ea2d56SDaniel Vetter 	unsigned long irqflags;
3680821450c6SEgbert Eich 	int i;
368120afbda2SDaniel Vetter 
3682821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3683821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3684821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3685821450c6SEgbert Eich 	}
3686821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3687821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3688821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3689821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3690821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3691821450c6SEgbert Eich 	}
3692b5ea2d56SDaniel Vetter 
3693b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3694b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
3695b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
369620afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
369720afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
3698b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
369920afbda2SDaniel Vetter }
3700