xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 6005ce42433df3f69de99d7e730383a6adb852ef)
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 
82cd569aedSEgbert Eich static void ibx_hpd_irq_setup(struct drm_device *dev);
83cd569aedSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev);
84e5868a31SEgbert Eich 
85036a4a7dSZhenyu Wang /* For display hotplug interrupt */
86995b6762SChris Wilson static void
87f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
88036a4a7dSZhenyu Wang {
894bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
904bc9d430SDaniel Vetter 
911ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
921ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
931ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
943143a2bfSChris Wilson 		POSTING_READ(DEIMR);
95036a4a7dSZhenyu Wang 	}
96036a4a7dSZhenyu Wang }
97036a4a7dSZhenyu Wang 
980ff9800aSPaulo Zanoni static void
99f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
100036a4a7dSZhenyu Wang {
1014bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1024bc9d430SDaniel Vetter 
1031ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1041ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1051ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1063143a2bfSChris Wilson 		POSTING_READ(DEIMR);
107036a4a7dSZhenyu Wang 	}
108036a4a7dSZhenyu Wang }
109036a4a7dSZhenyu Wang 
1108664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
1118664281bSPaulo Zanoni {
1128664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1138664281bSPaulo Zanoni 	struct intel_crtc *crtc;
1148664281bSPaulo Zanoni 	enum pipe pipe;
1158664281bSPaulo Zanoni 
1164bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1174bc9d430SDaniel Vetter 
1188664281bSPaulo Zanoni 	for_each_pipe(pipe) {
1198664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
1208664281bSPaulo Zanoni 
1218664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
1228664281bSPaulo Zanoni 			return false;
1238664281bSPaulo Zanoni 	}
1248664281bSPaulo Zanoni 
1258664281bSPaulo Zanoni 	return true;
1268664281bSPaulo Zanoni }
1278664281bSPaulo Zanoni 
1288664281bSPaulo Zanoni static bool cpt_can_enable_serr_int(struct drm_device *dev)
1298664281bSPaulo Zanoni {
1308664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1318664281bSPaulo Zanoni 	enum pipe pipe;
1328664281bSPaulo Zanoni 	struct intel_crtc *crtc;
1338664281bSPaulo Zanoni 
1348664281bSPaulo Zanoni 	for_each_pipe(pipe) {
1358664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
1368664281bSPaulo Zanoni 
1378664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
1388664281bSPaulo Zanoni 			return false;
1398664281bSPaulo Zanoni 	}
1408664281bSPaulo Zanoni 
1418664281bSPaulo Zanoni 	return true;
1428664281bSPaulo Zanoni }
1438664281bSPaulo Zanoni 
1448664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
1458664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
1468664281bSPaulo Zanoni {
1478664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1488664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
1498664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
1508664281bSPaulo Zanoni 
1518664281bSPaulo Zanoni 	if (enable)
1528664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
1538664281bSPaulo Zanoni 	else
1548664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
1558664281bSPaulo Zanoni }
1568664281bSPaulo Zanoni 
1578664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
1588664281bSPaulo Zanoni 						  bool enable)
1598664281bSPaulo Zanoni {
1608664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1618664281bSPaulo Zanoni 
1628664281bSPaulo Zanoni 	if (enable) {
1638664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
1648664281bSPaulo Zanoni 			return;
1658664281bSPaulo Zanoni 
1668664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN_A |
1678664281bSPaulo Zanoni 					 ERR_INT_FIFO_UNDERRUN_B |
1688664281bSPaulo Zanoni 					 ERR_INT_FIFO_UNDERRUN_C);
1698664281bSPaulo Zanoni 
1708664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
1718664281bSPaulo Zanoni 	} else {
1728664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
1738664281bSPaulo Zanoni 	}
1748664281bSPaulo Zanoni }
1758664281bSPaulo Zanoni 
1768664281bSPaulo Zanoni static void ibx_set_fifo_underrun_reporting(struct intel_crtc *crtc,
1778664281bSPaulo Zanoni 					    bool enable)
1788664281bSPaulo Zanoni {
1798664281bSPaulo Zanoni 	struct drm_device *dev = crtc->base.dev;
1808664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1818664281bSPaulo Zanoni 	uint32_t bit = (crtc->pipe == PIPE_A) ? SDE_TRANSA_FIFO_UNDER :
1828664281bSPaulo Zanoni 						SDE_TRANSB_FIFO_UNDER;
1838664281bSPaulo Zanoni 
1848664281bSPaulo Zanoni 	if (enable)
1858664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) & ~bit);
1868664281bSPaulo Zanoni 	else
1878664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) | bit);
1888664281bSPaulo Zanoni 
1898664281bSPaulo Zanoni 	POSTING_READ(SDEIMR);
1908664281bSPaulo Zanoni }
1918664281bSPaulo Zanoni 
1928664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
1938664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
1948664281bSPaulo Zanoni 					    bool enable)
1958664281bSPaulo Zanoni {
1968664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1978664281bSPaulo Zanoni 
1988664281bSPaulo Zanoni 	if (enable) {
1998664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
2008664281bSPaulo Zanoni 			return;
2018664281bSPaulo Zanoni 
2028664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, SERR_INT_TRANS_A_FIFO_UNDERRUN |
2038664281bSPaulo Zanoni 				     SERR_INT_TRANS_B_FIFO_UNDERRUN |
2048664281bSPaulo Zanoni 				     SERR_INT_TRANS_C_FIFO_UNDERRUN);
2058664281bSPaulo Zanoni 
2068664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) & ~SDE_ERROR_CPT);
2078664281bSPaulo Zanoni 	} else {
2088664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) | SDE_ERROR_CPT);
2098664281bSPaulo Zanoni 	}
2108664281bSPaulo Zanoni 
2118664281bSPaulo Zanoni 	POSTING_READ(SDEIMR);
2128664281bSPaulo Zanoni }
2138664281bSPaulo Zanoni 
2148664281bSPaulo Zanoni /**
2158664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
2168664281bSPaulo Zanoni  * @dev: drm device
2178664281bSPaulo Zanoni  * @pipe: pipe
2188664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
2198664281bSPaulo Zanoni  *
2208664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
2218664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
2228664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
2238664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
2248664281bSPaulo Zanoni  * bit for all the pipes.
2258664281bSPaulo Zanoni  *
2268664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
2278664281bSPaulo Zanoni  */
2288664281bSPaulo Zanoni bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
2298664281bSPaulo Zanoni 					   enum pipe pipe, bool enable)
2308664281bSPaulo Zanoni {
2318664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2328664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
2338664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2348664281bSPaulo Zanoni 	unsigned long flags;
2358664281bSPaulo Zanoni 	bool ret;
2368664281bSPaulo Zanoni 
2378664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
2388664281bSPaulo Zanoni 
2398664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
2408664281bSPaulo Zanoni 
2418664281bSPaulo Zanoni 	if (enable == ret)
2428664281bSPaulo Zanoni 		goto done;
2438664281bSPaulo Zanoni 
2448664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
2458664281bSPaulo Zanoni 
2468664281bSPaulo Zanoni 	if (IS_GEN5(dev) || IS_GEN6(dev))
2478664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
2488664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
2498664281bSPaulo Zanoni 		ivybridge_set_fifo_underrun_reporting(dev, enable);
2508664281bSPaulo Zanoni 
2518664281bSPaulo Zanoni done:
2528664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
2538664281bSPaulo Zanoni 	return ret;
2548664281bSPaulo Zanoni }
2558664281bSPaulo Zanoni 
2568664281bSPaulo Zanoni /**
2578664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
2588664281bSPaulo Zanoni  * @dev: drm device
2598664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
2608664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
2618664281bSPaulo Zanoni  *
2628664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
2638664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
2648664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
2658664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
2668664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
2678664281bSPaulo Zanoni  *
2688664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
2698664281bSPaulo Zanoni  */
2708664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
2718664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
2728664281bSPaulo Zanoni 					   bool enable)
2738664281bSPaulo Zanoni {
2748664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2758664281bSPaulo Zanoni 	enum pipe p;
2768664281bSPaulo Zanoni 	struct drm_crtc *crtc;
2778664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc;
2788664281bSPaulo Zanoni 	unsigned long flags;
2798664281bSPaulo Zanoni 	bool ret;
2808664281bSPaulo Zanoni 
2818664281bSPaulo Zanoni 	if (HAS_PCH_LPT(dev)) {
2828664281bSPaulo Zanoni 		crtc = NULL;
2838664281bSPaulo Zanoni 		for_each_pipe(p) {
2848664281bSPaulo Zanoni 			struct drm_crtc *c = dev_priv->pipe_to_crtc_mapping[p];
2858664281bSPaulo Zanoni 			if (intel_pipe_has_type(c, INTEL_OUTPUT_ANALOG)) {
2868664281bSPaulo Zanoni 				crtc = c;
2878664281bSPaulo Zanoni 				break;
2888664281bSPaulo Zanoni 			}
2898664281bSPaulo Zanoni 		}
2908664281bSPaulo Zanoni 		if (!crtc) {
2918664281bSPaulo Zanoni 			DRM_ERROR("PCH FIFO underrun, but no CRTC using the PCH found\n");
2928664281bSPaulo Zanoni 			return false;
2938664281bSPaulo Zanoni 		}
2948664281bSPaulo Zanoni 	} else {
2958664281bSPaulo Zanoni 		crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
2968664281bSPaulo Zanoni 	}
2978664281bSPaulo Zanoni 	intel_crtc = to_intel_crtc(crtc);
2988664281bSPaulo Zanoni 
2998664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
3008664281bSPaulo Zanoni 
3018664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
3028664281bSPaulo Zanoni 
3038664281bSPaulo Zanoni 	if (enable == ret)
3048664281bSPaulo Zanoni 		goto done;
3058664281bSPaulo Zanoni 
3068664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
3078664281bSPaulo Zanoni 
3088664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
3098664281bSPaulo Zanoni 		ibx_set_fifo_underrun_reporting(intel_crtc, enable);
3108664281bSPaulo Zanoni 	else
3118664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
3128664281bSPaulo Zanoni 
3138664281bSPaulo Zanoni done:
3148664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
3158664281bSPaulo Zanoni 	return ret;
3168664281bSPaulo Zanoni }
3178664281bSPaulo Zanoni 
3188664281bSPaulo Zanoni 
3197c463586SKeith Packard void
3207c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3217c463586SKeith Packard {
3229db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
32346c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3247c463586SKeith Packard 
32546c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
32646c06a30SVille Syrjälä 		return;
32746c06a30SVille Syrjälä 
3287c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
32946c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
33046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3313143a2bfSChris Wilson 	POSTING_READ(reg);
3327c463586SKeith Packard }
3337c463586SKeith Packard 
3347c463586SKeith Packard void
3357c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3367c463586SKeith Packard {
3379db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
33846c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3397c463586SKeith Packard 
34046c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
34146c06a30SVille Syrjälä 		return;
34246c06a30SVille Syrjälä 
34346c06a30SVille Syrjälä 	pipestat &= ~mask;
34446c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3453143a2bfSChris Wilson 	POSTING_READ(reg);
3467c463586SKeith Packard }
3477c463586SKeith Packard 
348c0e09200SDave Airlie /**
349f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
35001c66889SZhao Yakui  */
351f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
35201c66889SZhao Yakui {
3531ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
3541ec14ad3SChris Wilson 	unsigned long irqflags;
3551ec14ad3SChris Wilson 
356f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
357f49e38ddSJani Nikula 		return;
358f49e38ddSJani Nikula 
3591ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
36001c66889SZhao Yakui 
361f898780bSJani Nikula 	i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
362a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
363f898780bSJani Nikula 		i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
3641ec14ad3SChris Wilson 
3651ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
36601c66889SZhao Yakui }
36701c66889SZhao Yakui 
36801c66889SZhao Yakui /**
3690a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
3700a3e67a4SJesse Barnes  * @dev: DRM device
3710a3e67a4SJesse Barnes  * @pipe: pipe to check
3720a3e67a4SJesse Barnes  *
3730a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
3740a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
3750a3e67a4SJesse Barnes  * before reading such registers if unsure.
3760a3e67a4SJesse Barnes  */
3770a3e67a4SJesse Barnes static int
3780a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
3790a3e67a4SJesse Barnes {
3800a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
381702e7a56SPaulo Zanoni 
382a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
383a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
384a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
385a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
38671f8ba6bSPaulo Zanoni 
387a01025afSDaniel Vetter 		return intel_crtc->active;
388a01025afSDaniel Vetter 	} else {
389a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
390a01025afSDaniel Vetter 	}
3910a3e67a4SJesse Barnes }
3920a3e67a4SJesse Barnes 
39342f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
39442f52ef8SKeith Packard  * we use as a pipe index
39542f52ef8SKeith Packard  */
396f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
3970a3e67a4SJesse Barnes {
3980a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3990a3e67a4SJesse Barnes 	unsigned long high_frame;
4000a3e67a4SJesse Barnes 	unsigned long low_frame;
4015eddb70bSChris Wilson 	u32 high1, high2, low;
4020a3e67a4SJesse Barnes 
4030a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
40444d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4059db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
4060a3e67a4SJesse Barnes 		return 0;
4070a3e67a4SJesse Barnes 	}
4080a3e67a4SJesse Barnes 
4099db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
4109db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
4115eddb70bSChris Wilson 
4120a3e67a4SJesse Barnes 	/*
4130a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
4140a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
4150a3e67a4SJesse Barnes 	 * register.
4160a3e67a4SJesse Barnes 	 */
4170a3e67a4SJesse Barnes 	do {
4185eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4195eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
4205eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4210a3e67a4SJesse Barnes 	} while (high1 != high2);
4220a3e67a4SJesse Barnes 
4235eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
4245eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
4255eddb70bSChris Wilson 	return (high1 << 8) | low;
4260a3e67a4SJesse Barnes }
4270a3e67a4SJesse Barnes 
428f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
4299880b7a5SJesse Barnes {
4309880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4319db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
4329880b7a5SJesse Barnes 
4339880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
43444d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4359db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4369880b7a5SJesse Barnes 		return 0;
4379880b7a5SJesse Barnes 	}
4389880b7a5SJesse Barnes 
4399880b7a5SJesse Barnes 	return I915_READ(reg);
4409880b7a5SJesse Barnes }
4419880b7a5SJesse Barnes 
442f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
4430af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
4440af7e4dfSMario Kleiner {
4450af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4460af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
4470af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
4480af7e4dfSMario Kleiner 	bool in_vbl = true;
4490af7e4dfSMario Kleiner 	int ret = 0;
450fe2b8f9dSPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
451fe2b8f9dSPaulo Zanoni 								      pipe);
4520af7e4dfSMario Kleiner 
4530af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
4540af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
4559db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4560af7e4dfSMario Kleiner 		return 0;
4570af7e4dfSMario Kleiner 	}
4580af7e4dfSMario Kleiner 
4590af7e4dfSMario Kleiner 	/* Get vtotal. */
460fe2b8f9dSPaulo Zanoni 	vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4610af7e4dfSMario Kleiner 
4620af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
4630af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
4640af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
4650af7e4dfSMario Kleiner 		 */
4660af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
4670af7e4dfSMario Kleiner 
4680af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
4690af7e4dfSMario Kleiner 		 * horizontal scanout position.
4700af7e4dfSMario Kleiner 		 */
4710af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
4720af7e4dfSMario Kleiner 		*hpos = 0;
4730af7e4dfSMario Kleiner 	} else {
4740af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
4750af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
4760af7e4dfSMario Kleiner 		 * scanout position.
4770af7e4dfSMario Kleiner 		 */
4780af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
4790af7e4dfSMario Kleiner 
480fe2b8f9dSPaulo Zanoni 		htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4810af7e4dfSMario Kleiner 		*vpos = position / htotal;
4820af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
4830af7e4dfSMario Kleiner 	}
4840af7e4dfSMario Kleiner 
4850af7e4dfSMario Kleiner 	/* Query vblank area. */
486fe2b8f9dSPaulo Zanoni 	vbl = I915_READ(VBLANK(cpu_transcoder));
4870af7e4dfSMario Kleiner 
4880af7e4dfSMario Kleiner 	/* Test position against vblank region. */
4890af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
4900af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
4910af7e4dfSMario Kleiner 
4920af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
4930af7e4dfSMario Kleiner 		in_vbl = false;
4940af7e4dfSMario Kleiner 
4950af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
4960af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
4970af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
4980af7e4dfSMario Kleiner 
4990af7e4dfSMario Kleiner 	/* Readouts valid? */
5000af7e4dfSMario Kleiner 	if (vbl > 0)
5010af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
5020af7e4dfSMario Kleiner 
5030af7e4dfSMario Kleiner 	/* In vblank? */
5040af7e4dfSMario Kleiner 	if (in_vbl)
5050af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
5060af7e4dfSMario Kleiner 
5070af7e4dfSMario Kleiner 	return ret;
5080af7e4dfSMario Kleiner }
5090af7e4dfSMario Kleiner 
510f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
5110af7e4dfSMario Kleiner 			      int *max_error,
5120af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
5130af7e4dfSMario Kleiner 			      unsigned flags)
5140af7e4dfSMario Kleiner {
5154041b853SChris Wilson 	struct drm_crtc *crtc;
5160af7e4dfSMario Kleiner 
5177eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
5184041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5190af7e4dfSMario Kleiner 		return -EINVAL;
5200af7e4dfSMario Kleiner 	}
5210af7e4dfSMario Kleiner 
5220af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
5234041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
5244041b853SChris Wilson 	if (crtc == NULL) {
5254041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5264041b853SChris Wilson 		return -EINVAL;
5274041b853SChris Wilson 	}
5284041b853SChris Wilson 
5294041b853SChris Wilson 	if (!crtc->enabled) {
5304041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
5314041b853SChris Wilson 		return -EBUSY;
5324041b853SChris Wilson 	}
5330af7e4dfSMario Kleiner 
5340af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
5354041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
5364041b853SChris Wilson 						     vblank_time, flags,
5374041b853SChris Wilson 						     crtc);
5380af7e4dfSMario Kleiner }
5390af7e4dfSMario Kleiner 
540321a1b30SEgbert Eich static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
541321a1b30SEgbert Eich {
542321a1b30SEgbert Eich 	enum drm_connector_status old_status;
543321a1b30SEgbert Eich 
544321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
545321a1b30SEgbert Eich 	old_status = connector->status;
546321a1b30SEgbert Eich 
547321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
548321a1b30SEgbert Eich 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
549321a1b30SEgbert Eich 		      connector->base.id,
550321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
551321a1b30SEgbert Eich 		      old_status, connector->status);
552321a1b30SEgbert Eich 	return (old_status != connector->status);
553321a1b30SEgbert Eich }
554321a1b30SEgbert Eich 
5555ca58282SJesse Barnes /*
5565ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
5575ca58282SJesse Barnes  */
558ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
559ac4c16c5SEgbert Eich 
5605ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
5615ca58282SJesse Barnes {
5625ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5635ca58282SJesse Barnes 						    hotplug_work);
5645ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
565c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
566cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
567cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
568cd569aedSEgbert Eich 	struct drm_connector *connector;
569cd569aedSEgbert Eich 	unsigned long irqflags;
570cd569aedSEgbert Eich 	bool hpd_disabled = false;
571321a1b30SEgbert Eich 	bool changed = false;
572142e2398SEgbert Eich 	u32 hpd_event_bits;
5735ca58282SJesse Barnes 
57452d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
57552d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
57652d7ecedSDaniel Vetter 		return;
57752d7ecedSDaniel Vetter 
578a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
579e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
580e67189abSJesse Barnes 
581cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
582142e2398SEgbert Eich 
583142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
584142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
585cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
586cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
587cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
588cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
589cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
590cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
591cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
592cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
593cd569aedSEgbert Eich 				drm_get_connector_name(connector));
594cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
595cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
596cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
597cd569aedSEgbert Eich 			hpd_disabled = true;
598cd569aedSEgbert Eich 		}
599142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
600142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
601142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
602142e2398SEgbert Eich 		}
603cd569aedSEgbert Eich 	}
604cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
605cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
606cd569aedSEgbert Eich 	  * some connectors */
607ac4c16c5SEgbert Eich 	if (hpd_disabled) {
608cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
609ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
610ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
611ac4c16c5SEgbert Eich 	}
612cd569aedSEgbert Eich 
613cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
614cd569aedSEgbert Eich 
615321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
616321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
617321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
618321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
619cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
620cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
621321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
622321a1b30SEgbert Eich 				changed = true;
623321a1b30SEgbert Eich 		}
624321a1b30SEgbert Eich 	}
62540ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
62640ee3381SKeith Packard 
627321a1b30SEgbert Eich 	if (changed)
628321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
6295ca58282SJesse Barnes }
6305ca58282SJesse Barnes 
63173edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
632f97108d1SJesse Barnes {
633f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
634b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
6359270388eSDaniel Vetter 	u8 new_delay;
6369270388eSDaniel Vetter 	unsigned long flags;
6379270388eSDaniel Vetter 
6389270388eSDaniel Vetter 	spin_lock_irqsave(&mchdev_lock, flags);
639f97108d1SJesse Barnes 
64073edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
64173edd18fSDaniel Vetter 
64220e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
6439270388eSDaniel Vetter 
6447648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
645b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
646b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
647f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
648f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
649f97108d1SJesse Barnes 
650f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
651b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
65220e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
65320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
65420e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
65520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
656b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
65720e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
65820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
65920e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
66020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
661f97108d1SJesse Barnes 	}
662f97108d1SJesse Barnes 
6637648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
66420e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
665f97108d1SJesse Barnes 
6669270388eSDaniel Vetter 	spin_unlock_irqrestore(&mchdev_lock, flags);
6679270388eSDaniel Vetter 
668f97108d1SJesse Barnes 	return;
669f97108d1SJesse Barnes }
670f97108d1SJesse Barnes 
671549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
672549f7365SChris Wilson 			struct intel_ring_buffer *ring)
673549f7365SChris Wilson {
674549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
6759862e600SChris Wilson 
676475553deSChris Wilson 	if (ring->obj == NULL)
677475553deSChris Wilson 		return;
678475553deSChris Wilson 
679b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
6809862e600SChris Wilson 
681549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
6823e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
68399584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
684cecc21feSChris Wilson 			  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
6853e0dc6b0SBen Widawsky 	}
686549f7365SChris Wilson }
687549f7365SChris Wilson 
6884912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
6893b8d8d91SJesse Barnes {
6904912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
691c6a828d3SDaniel Vetter 						    rps.work);
6924912d041SBen Widawsky 	u32 pm_iir, pm_imr;
6937b9e0ae6SChris Wilson 	u8 new_delay;
6943b8d8d91SJesse Barnes 
695c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
696c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
697c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
6984912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
6994848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
7004848405cSBen Widawsky 	I915_WRITE(GEN6_PMIMR, pm_imr & ~GEN6_PM_RPS_EVENTS);
701c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
7024912d041SBen Widawsky 
7034848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
7043b8d8d91SJesse Barnes 		return;
7053b8d8d91SJesse Barnes 
7064fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
7077b9e0ae6SChris Wilson 
7087425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
709c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
7107425034aSVille Syrjälä 
7117425034aSVille Syrjälä 		/*
7127425034aSVille Syrjälä 		 * For better performance, jump directly
7137425034aSVille Syrjälä 		 * to RPe if we're below it.
7147425034aSVille Syrjälä 		 */
7157425034aSVille Syrjälä 		if (IS_VALLEYVIEW(dev_priv->dev) &&
7167425034aSVille Syrjälä 		    dev_priv->rps.cur_delay < dev_priv->rps.rpe_delay)
7177425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
7187425034aSVille Syrjälä 	} else
719c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
7203b8d8d91SJesse Barnes 
72179249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
72279249636SBen Widawsky 	 * interrupt
72379249636SBen Widawsky 	 */
724d8289c9eSVille Syrjälä 	if (new_delay >= dev_priv->rps.min_delay &&
725d8289c9eSVille Syrjälä 	    new_delay <= dev_priv->rps.max_delay) {
7260a073b84SJesse Barnes 		if (IS_VALLEYVIEW(dev_priv->dev))
7270a073b84SJesse Barnes 			valleyview_set_rps(dev_priv->dev, new_delay);
7280a073b84SJesse Barnes 		else
7294912d041SBen Widawsky 			gen6_set_rps(dev_priv->dev, new_delay);
73079249636SBen Widawsky 	}
7313b8d8d91SJesse Barnes 
73252ceb908SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev)) {
73352ceb908SJesse Barnes 		/*
73452ceb908SJesse Barnes 		 * On VLV, when we enter RC6 we may not be at the minimum
73552ceb908SJesse Barnes 		 * voltage level, so arm a timer to check.  It should only
73652ceb908SJesse Barnes 		 * fire when there's activity or once after we've entered
73752ceb908SJesse Barnes 		 * RC6, and then won't be re-armed until the next RPS interrupt.
73852ceb908SJesse Barnes 		 */
73952ceb908SJesse Barnes 		mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
74052ceb908SJesse Barnes 				 msecs_to_jiffies(100));
74152ceb908SJesse Barnes 	}
74252ceb908SJesse Barnes 
7434fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
7443b8d8d91SJesse Barnes }
7453b8d8d91SJesse Barnes 
746e3689190SBen Widawsky 
747e3689190SBen Widawsky /**
748e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
749e3689190SBen Widawsky  * occurred.
750e3689190SBen Widawsky  * @work: workqueue struct
751e3689190SBen Widawsky  *
752e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
753e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
754e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
755e3689190SBen Widawsky  */
756e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
757e3689190SBen Widawsky {
758e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
759a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
760e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
761e3689190SBen Widawsky 	char *parity_event[5];
762e3689190SBen Widawsky 	uint32_t misccpctl;
763e3689190SBen Widawsky 	unsigned long flags;
764e3689190SBen Widawsky 
765e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
766e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
767e3689190SBen Widawsky 	 * any time we access those registers.
768e3689190SBen Widawsky 	 */
769e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
770e3689190SBen Widawsky 
771e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
772e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
773e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
774e3689190SBen Widawsky 
775e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
776e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
777e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
778e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
779e3689190SBen Widawsky 
780e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
781e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
782e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
783e3689190SBen Widawsky 
784e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
785e3689190SBen Widawsky 
786e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
787cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
788e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
789e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
790e3689190SBen Widawsky 
791e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
792e3689190SBen Widawsky 
793e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
794e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
795e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
796e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
797e3689190SBen Widawsky 	parity_event[4] = NULL;
798e3689190SBen Widawsky 
799e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
800e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
801e3689190SBen Widawsky 
802e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
803e3689190SBen Widawsky 		  row, bank, subbank);
804e3689190SBen Widawsky 
805e3689190SBen Widawsky 	kfree(parity_event[3]);
806e3689190SBen Widawsky 	kfree(parity_event[2]);
807e3689190SBen Widawsky 	kfree(parity_event[1]);
808e3689190SBen Widawsky }
809e3689190SBen Widawsky 
810d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
811e3689190SBen Widawsky {
812e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
813e3689190SBen Widawsky 	unsigned long flags;
814e3689190SBen Widawsky 
815e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
816e3689190SBen Widawsky 		return;
817e3689190SBen Widawsky 
818e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
819cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
820e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
821e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
822e3689190SBen Widawsky 
823a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
824e3689190SBen Widawsky }
825e3689190SBen Widawsky 
826e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
827e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
828e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
829e7b4c6b1SDaniel Vetter {
830e7b4c6b1SDaniel Vetter 
831cc609d5dSBen Widawsky 	if (gt_iir &
832cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
833e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
834cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
835e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
836cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
837e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
838e7b4c6b1SDaniel Vetter 
839cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
840cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
841cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
842e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
843e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
844e7b4c6b1SDaniel Vetter 	}
845e3689190SBen Widawsky 
846cc609d5dSBen Widawsky 	if (gt_iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
847e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
848e7b4c6b1SDaniel Vetter }
849e7b4c6b1SDaniel Vetter 
850baf02a1fSBen Widawsky /* Legacy way of handling PM interrupts */
851fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
852fc6826d1SChris Wilson 				u32 pm_iir)
853fc6826d1SChris Wilson {
854fc6826d1SChris Wilson 	unsigned long flags;
855fc6826d1SChris Wilson 
856fc6826d1SChris Wilson 	/*
857fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
858fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
859fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
860c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
861fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
862fc6826d1SChris Wilson 	 *
863c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
864fc6826d1SChris Wilson 	 */
865fc6826d1SChris Wilson 
866c6a828d3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
867c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
868c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
869fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
870c6a828d3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
871fc6826d1SChris Wilson 
872c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
873fc6826d1SChris Wilson }
874fc6826d1SChris Wilson 
875b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
876b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
877b543fb04SEgbert Eich 
878cd569aedSEgbert Eich static inline bool hotplug_irq_storm_detect(struct drm_device *dev,
879b543fb04SEgbert Eich 					    u32 hotplug_trigger,
880b543fb04SEgbert Eich 					    const u32 *hpd)
881b543fb04SEgbert Eich {
882b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
883b543fb04SEgbert Eich 	unsigned long irqflags;
884b543fb04SEgbert Eich 	int i;
885cd569aedSEgbert Eich 	bool ret = false;
886b543fb04SEgbert Eich 
887b543fb04SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
888b543fb04SEgbert Eich 
889b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
890821450c6SEgbert Eich 
891b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
892b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
893b543fb04SEgbert Eich 			continue;
894b543fb04SEgbert Eich 
895bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
896b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
897b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
898b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
899b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
900b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
901b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
902b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
903142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
904b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
905cd569aedSEgbert Eich 			ret = true;
906b543fb04SEgbert Eich 		} else {
907b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
908b543fb04SEgbert Eich 		}
909b543fb04SEgbert Eich 	}
910b543fb04SEgbert Eich 
911b543fb04SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
912cd569aedSEgbert Eich 
913cd569aedSEgbert Eich 	return ret;
914b543fb04SEgbert Eich }
915b543fb04SEgbert Eich 
916515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
917515ac2bbSDaniel Vetter {
91828c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
91928c70f16SDaniel Vetter 
92028c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
921515ac2bbSDaniel Vetter }
922515ac2bbSDaniel Vetter 
923ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
924ce99c256SDaniel Vetter {
9259ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
9269ee32feaSDaniel Vetter 
9279ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
928ce99c256SDaniel Vetter }
929ce99c256SDaniel Vetter 
930baf02a1fSBen Widawsky /* Unlike gen6_queue_rps_work() from which this function is originally derived,
931baf02a1fSBen Widawsky  * we must be able to deal with other PM interrupts. This is complicated because
932baf02a1fSBen Widawsky  * of the way in which we use the masks to defer the RPS work (which for
933baf02a1fSBen Widawsky  * posterity is necessary because of forcewake).
934baf02a1fSBen Widawsky  */
935baf02a1fSBen Widawsky static void hsw_pm_irq_handler(struct drm_i915_private *dev_priv,
936baf02a1fSBen Widawsky 			       u32 pm_iir)
937baf02a1fSBen Widawsky {
938baf02a1fSBen Widawsky 	unsigned long flags;
939baf02a1fSBen Widawsky 
940baf02a1fSBen Widawsky 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
9414848405cSBen Widawsky 	dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
942baf02a1fSBen Widawsky 	if (dev_priv->rps.pm_iir) {
943baf02a1fSBen Widawsky 		I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
944baf02a1fSBen Widawsky 		/* never want to mask useful interrupts. (also posting read) */
9454848405cSBen Widawsky 		WARN_ON(I915_READ_NOTRACE(GEN6_PMIMR) & ~GEN6_PM_RPS_EVENTS);
946baf02a1fSBen Widawsky 		/* TODO: if queue_work is slow, move it out of the spinlock */
947baf02a1fSBen Widawsky 		queue_work(dev_priv->wq, &dev_priv->rps.work);
948baf02a1fSBen Widawsky 	}
949baf02a1fSBen Widawsky 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
950baf02a1fSBen Widawsky 
95112638c57SBen Widawsky 	if (pm_iir & ~GEN6_PM_RPS_EVENTS) {
95212638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
95312638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
95412638c57SBen Widawsky 
95512638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
95612638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
95712638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
95812638c57SBen Widawsky 		}
95912638c57SBen Widawsky 	}
960baf02a1fSBen Widawsky }
961baf02a1fSBen Widawsky 
962ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
9637e231dbeSJesse Barnes {
9647e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
9657e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
9667e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
9677e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
9687e231dbeSJesse Barnes 	unsigned long irqflags;
9697e231dbeSJesse Barnes 	int pipe;
9707e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
9717e231dbeSJesse Barnes 
9727e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
9737e231dbeSJesse Barnes 
9747e231dbeSJesse Barnes 	while (true) {
9757e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
9767e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
9777e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
9787e231dbeSJesse Barnes 
9797e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
9807e231dbeSJesse Barnes 			goto out;
9817e231dbeSJesse Barnes 
9827e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
9837e231dbeSJesse Barnes 
984e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
9857e231dbeSJesse Barnes 
9867e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
9877e231dbeSJesse Barnes 		for_each_pipe(pipe) {
9887e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
9897e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
9907e231dbeSJesse Barnes 
9917e231dbeSJesse Barnes 			/*
9927e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
9937e231dbeSJesse Barnes 			 */
9947e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
9957e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
9967e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
9977e231dbeSJesse Barnes 							 pipe_name(pipe));
9987e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
9997e231dbeSJesse Barnes 			}
10007e231dbeSJesse Barnes 		}
10017e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
10027e231dbeSJesse Barnes 
100331acc7f5SJesse Barnes 		for_each_pipe(pipe) {
100431acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
100531acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
100631acc7f5SJesse Barnes 
100731acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
100831acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
100931acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
101031acc7f5SJesse Barnes 			}
101131acc7f5SJesse Barnes 		}
101231acc7f5SJesse Barnes 
10137e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
10147e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
10157e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1016b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
10177e231dbeSJesse Barnes 
10187e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
10197e231dbeSJesse Barnes 					 hotplug_status);
1020b543fb04SEgbert Eich 			if (hotplug_trigger) {
1021cd569aedSEgbert Eich 				if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_status_i915))
1022cd569aedSEgbert Eich 					i915_hpd_irq_setup(dev);
10237e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
10247e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
1025b543fb04SEgbert Eich 			}
10267e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
10277e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
10287e231dbeSJesse Barnes 		}
10297e231dbeSJesse Barnes 
1030515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1031515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
10327e231dbeSJesse Barnes 
10334848405cSBen Widawsky 		if (pm_iir & GEN6_PM_RPS_EVENTS)
1034fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
10357e231dbeSJesse Barnes 
10367e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
10377e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
10387e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
10397e231dbeSJesse Barnes 	}
10407e231dbeSJesse Barnes 
10417e231dbeSJesse Barnes out:
10427e231dbeSJesse Barnes 	return ret;
10437e231dbeSJesse Barnes }
10447e231dbeSJesse Barnes 
104523e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1046776ad806SJesse Barnes {
1047776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10489db4a9c7SJesse Barnes 	int pipe;
1049b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1050776ad806SJesse Barnes 
1051b543fb04SEgbert Eich 	if (hotplug_trigger) {
1052cd569aedSEgbert Eich 		if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_ibx))
1053cd569aedSEgbert Eich 			ibx_hpd_irq_setup(dev);
105476e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
1055b543fb04SEgbert Eich 	}
1056cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1057cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1058776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1059cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1060cfc33bf7SVille Syrjälä 				 port_name(port));
1061cfc33bf7SVille Syrjälä 	}
1062776ad806SJesse Barnes 
1063ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1064ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1065ce99c256SDaniel Vetter 
1066776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1067515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1068776ad806SJesse Barnes 
1069776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1070776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1071776ad806SJesse Barnes 
1072776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1073776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1074776ad806SJesse Barnes 
1075776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1076776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1077776ad806SJesse Barnes 
10789db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
10799db4a9c7SJesse Barnes 		for_each_pipe(pipe)
10809db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
10819db4a9c7SJesse Barnes 					 pipe_name(pipe),
10829db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1083776ad806SJesse Barnes 
1084776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1085776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1086776ad806SJesse Barnes 
1087776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1088776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1089776ad806SJesse Barnes 
1090776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
10918664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
10928664281bSPaulo Zanoni 							  false))
10938664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
10948664281bSPaulo Zanoni 
10958664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
10968664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
10978664281bSPaulo Zanoni 							  false))
10988664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
10998664281bSPaulo Zanoni }
11008664281bSPaulo Zanoni 
11018664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
11028664281bSPaulo Zanoni {
11038664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
11048664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
11058664281bSPaulo Zanoni 
1106de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1107de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1108de032bf4SPaulo Zanoni 
11098664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_A)
11108664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
11118664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
11128664281bSPaulo Zanoni 
11138664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_B)
11148664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
11158664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
11168664281bSPaulo Zanoni 
11178664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_C)
11188664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
11198664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
11208664281bSPaulo Zanoni 
11218664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
11228664281bSPaulo Zanoni }
11238664281bSPaulo Zanoni 
11248664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
11258664281bSPaulo Zanoni {
11268664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
11278664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
11288664281bSPaulo Zanoni 
1129de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1130de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1131de032bf4SPaulo Zanoni 
11328664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
11338664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
11348664281bSPaulo Zanoni 							  false))
11358664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
11368664281bSPaulo Zanoni 
11378664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
11388664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
11398664281bSPaulo Zanoni 							  false))
11408664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
11418664281bSPaulo Zanoni 
11428664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
11438664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
11448664281bSPaulo Zanoni 							  false))
11458664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
11468664281bSPaulo Zanoni 
11478664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1148776ad806SJesse Barnes }
1149776ad806SJesse Barnes 
115023e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
115123e81d69SAdam Jackson {
115223e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
115323e81d69SAdam Jackson 	int pipe;
1154b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
115523e81d69SAdam Jackson 
1156b543fb04SEgbert Eich 	if (hotplug_trigger) {
1157cd569aedSEgbert Eich 		if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_cpt))
1158cd569aedSEgbert Eich 			ibx_hpd_irq_setup(dev);
115976e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
1160b543fb04SEgbert Eich 	}
1161cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1162cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
116323e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1164cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1165cfc33bf7SVille Syrjälä 				 port_name(port));
1166cfc33bf7SVille Syrjälä 	}
116723e81d69SAdam Jackson 
116823e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1169ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
117023e81d69SAdam Jackson 
117123e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1172515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
117323e81d69SAdam Jackson 
117423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
117523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
117623e81d69SAdam Jackson 
117723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
117823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
117923e81d69SAdam Jackson 
118023e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
118123e81d69SAdam Jackson 		for_each_pipe(pipe)
118223e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
118323e81d69SAdam Jackson 					 pipe_name(pipe),
118423e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
11858664281bSPaulo Zanoni 
11868664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
11878664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
118823e81d69SAdam Jackson }
118923e81d69SAdam Jackson 
1190ff1f525eSDaniel Vetter static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
1191b1f14ad0SJesse Barnes {
1192b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1193b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1194ab5c608bSBen Widawsky 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
11950e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
11960e43406bSChris Wilson 	int i;
1197b1f14ad0SJesse Barnes 
1198b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1199b1f14ad0SJesse Barnes 
12008664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
12018664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
12028664281bSPaulo Zanoni 	if (IS_HASWELL(dev) &&
12038664281bSPaulo Zanoni 	    (I915_READ_NOTRACE(FPGA_DBG) & FPGA_DBG_RM_NOCLAIM)) {
12048664281bSPaulo Zanoni 		DRM_ERROR("Unclaimed register before interrupt\n");
12058664281bSPaulo Zanoni 		I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
12068664281bSPaulo Zanoni 	}
12078664281bSPaulo Zanoni 
1208b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1209b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1210b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
12110e43406bSChris Wilson 
121244498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
121344498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
121444498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
121544498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
121644498aeaSPaulo Zanoni 	 * due to its back queue). */
1217ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
121844498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
121944498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
122044498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1221ab5c608bSBen Widawsky 	}
122244498aeaSPaulo Zanoni 
12238664281bSPaulo Zanoni 	/* On Haswell, also mask ERR_INT because we don't want to risk
12248664281bSPaulo Zanoni 	 * generating "unclaimed register" interrupts from inside the interrupt
12258664281bSPaulo Zanoni 	 * handler. */
12264bc9d430SDaniel Vetter 	if (IS_HASWELL(dev)) {
12274bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
12288664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
12294bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
12304bc9d430SDaniel Vetter 	}
12318664281bSPaulo Zanoni 
12320e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
12330e43406bSChris Wilson 	if (gt_iir) {
12340e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
12350e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
12360e43406bSChris Wilson 		ret = IRQ_HANDLED;
12370e43406bSChris Wilson 	}
1238b1f14ad0SJesse Barnes 
1239b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
12400e43406bSChris Wilson 	if (de_iir) {
12418664281bSPaulo Zanoni 		if (de_iir & DE_ERR_INT_IVB)
12428664281bSPaulo Zanoni 			ivb_err_int_handler(dev);
12438664281bSPaulo Zanoni 
1244ce99c256SDaniel Vetter 		if (de_iir & DE_AUX_CHANNEL_A_IVB)
1245ce99c256SDaniel Vetter 			dp_aux_irq_handler(dev);
1246ce99c256SDaniel Vetter 
1247b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
124881a07809SJani Nikula 			intel_opregion_asle_intr(dev);
1249b1f14ad0SJesse Barnes 
12500e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
125174d44445SDaniel Vetter 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
125274d44445SDaniel Vetter 				drm_handle_vblank(dev, i);
12530e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
12540e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
12550e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
1256b1f14ad0SJesse Barnes 			}
1257b1f14ad0SJesse Barnes 		}
1258b1f14ad0SJesse Barnes 
1259b1f14ad0SJesse Barnes 		/* check event from PCH */
1260ab5c608bSBen Widawsky 		if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
12610e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
12620e43406bSChris Wilson 
126323e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
12640e43406bSChris Wilson 
12650e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
12660e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
1267b1f14ad0SJesse Barnes 		}
1268b1f14ad0SJesse Barnes 
12690e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
12700e43406bSChris Wilson 		ret = IRQ_HANDLED;
12710e43406bSChris Wilson 	}
12720e43406bSChris Wilson 
12730e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
12740e43406bSChris Wilson 	if (pm_iir) {
1275baf02a1fSBen Widawsky 		if (IS_HASWELL(dev))
1276baf02a1fSBen Widawsky 			hsw_pm_irq_handler(dev_priv, pm_iir);
12774848405cSBen Widawsky 		else if (pm_iir & GEN6_PM_RPS_EVENTS)
1278fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
1279b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
12800e43406bSChris Wilson 		ret = IRQ_HANDLED;
12810e43406bSChris Wilson 	}
1282b1f14ad0SJesse Barnes 
12834bc9d430SDaniel Vetter 	if (IS_HASWELL(dev)) {
12844bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
12854bc9d430SDaniel Vetter 		if (ivb_can_enable_err_int(dev))
12868664281bSPaulo Zanoni 			ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
12874bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
12884bc9d430SDaniel Vetter 	}
12898664281bSPaulo Zanoni 
1290b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1291b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1292ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
129344498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
129444498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1295ab5c608bSBen Widawsky 	}
1296b1f14ad0SJesse Barnes 
1297b1f14ad0SJesse Barnes 	return ret;
1298b1f14ad0SJesse Barnes }
1299b1f14ad0SJesse Barnes 
1300e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
1301e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1302e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1303e7b4c6b1SDaniel Vetter {
1304cc609d5dSBen Widawsky 	if (gt_iir &
1305cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1306e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1307cc609d5dSBen Widawsky 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1308e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1309e7b4c6b1SDaniel Vetter }
1310e7b4c6b1SDaniel Vetter 
1311ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1312036a4a7dSZhenyu Wang {
13134697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1314036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1315036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
131644498aeaSPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
1317881f47b6SXiang, Haihao 
13184697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
13194697995bSJesse Barnes 
13202d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
13212d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
13222d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
13233143a2bfSChris Wilson 	POSTING_READ(DEIER);
13242d109a84SZou, Nanhai 
132544498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
132644498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
132744498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
132844498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
132944498aeaSPaulo Zanoni 	 * due to its back queue). */
133044498aeaSPaulo Zanoni 	sde_ier = I915_READ(SDEIER);
133144498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, 0);
133244498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
133344498aeaSPaulo Zanoni 
1334036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
1335036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
13363b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
1337036a4a7dSZhenyu Wang 
1338acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
1339c7c85101SZou Nan hai 		goto done;
1340036a4a7dSZhenyu Wang 
1341036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
1342036a4a7dSZhenyu Wang 
1343e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
1344e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
1345e7b4c6b1SDaniel Vetter 	else
1346e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1347036a4a7dSZhenyu Wang 
1348ce99c256SDaniel Vetter 	if (de_iir & DE_AUX_CHANNEL_A)
1349ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1350ce99c256SDaniel Vetter 
135101c66889SZhao Yakui 	if (de_iir & DE_GSE)
135281a07809SJani Nikula 		intel_opregion_asle_intr(dev);
135301c66889SZhao Yakui 
135474d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
135574d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
135674d44445SDaniel Vetter 
135774d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
135874d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
135974d44445SDaniel Vetter 
1360de032bf4SPaulo Zanoni 	if (de_iir & DE_POISON)
1361de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1362de032bf4SPaulo Zanoni 
13638664281bSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
13648664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
13658664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
13668664281bSPaulo Zanoni 
13678664281bSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
13688664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
13698664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
13708664281bSPaulo Zanoni 
1371f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1372013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
13732bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
1374013d5aa2SJesse Barnes 	}
1375013d5aa2SJesse Barnes 
1376f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1377f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
13782bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
1379013d5aa2SJesse Barnes 	}
1380c062df61SLi Peng 
1381c650156aSZhenyu Wang 	/* check event from PCH */
1382776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
1383acd15b6cSDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
1384acd15b6cSDaniel Vetter 
138523e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
138623e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
138723e81d69SAdam Jackson 		else
138823e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
1389acd15b6cSDaniel Vetter 
1390acd15b6cSDaniel Vetter 		/* should clear PCH hotplug event before clear CPU irq */
1391acd15b6cSDaniel Vetter 		I915_WRITE(SDEIIR, pch_iir);
1392776ad806SJesse Barnes 	}
1393c650156aSZhenyu Wang 
139473edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
139573edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
1396f97108d1SJesse Barnes 
13974848405cSBen Widawsky 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_RPS_EVENTS)
1398fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
13993b8d8d91SJesse Barnes 
1400c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
1401c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
14024912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
1403036a4a7dSZhenyu Wang 
1404c7c85101SZou Nan hai done:
14052d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
14063143a2bfSChris Wilson 	POSTING_READ(DEIER);
140744498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, sde_ier);
140844498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
14092d109a84SZou, Nanhai 
1410036a4a7dSZhenyu Wang 	return ret;
1411036a4a7dSZhenyu Wang }
1412036a4a7dSZhenyu Wang 
14138a905236SJesse Barnes /**
14148a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
14158a905236SJesse Barnes  * @work: work struct
14168a905236SJesse Barnes  *
14178a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
14188a905236SJesse Barnes  * was detected.
14198a905236SJesse Barnes  */
14208a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
14218a905236SJesse Barnes {
14221f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
14231f83fee0SDaniel Vetter 						    work);
14241f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
14251f83fee0SDaniel Vetter 						    gpu_error);
14268a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1427f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
1428f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
1429f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
1430f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
1431f69061beSDaniel Vetter 	int i, ret;
14328a905236SJesse Barnes 
1433f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
14348a905236SJesse Barnes 
14357db0ba24SDaniel Vetter 	/*
14367db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
14377db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
14387db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
14397db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
14407db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
14417db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
14427db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
14437db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
14447db0ba24SDaniel Vetter 	 */
14457db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
144644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
14477db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
14487db0ba24SDaniel Vetter 				   reset_event);
14491f83fee0SDaniel Vetter 
1450f69061beSDaniel Vetter 		ret = i915_reset(dev);
1451f69061beSDaniel Vetter 
1452f69061beSDaniel Vetter 		if (ret == 0) {
1453f69061beSDaniel Vetter 			/*
1454f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1455f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1456f69061beSDaniel Vetter 			 * complete.
1457f69061beSDaniel Vetter 			 *
1458f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1459f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1460f69061beSDaniel Vetter 			 * updates before
1461f69061beSDaniel Vetter 			 * the counter increment.
1462f69061beSDaniel Vetter 			 */
1463f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1464f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1465f69061beSDaniel Vetter 
1466f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1467f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
14681f83fee0SDaniel Vetter 		} else {
14691f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1470f316a42cSBen Gamari 		}
14711f83fee0SDaniel Vetter 
1472f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
1473f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
1474f69061beSDaniel Vetter 
147596a02917SVille Syrjälä 		intel_display_handle_reset(dev);
147696a02917SVille Syrjälä 
14771f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
1478f316a42cSBen Gamari 	}
14798a905236SJesse Barnes }
14808a905236SJesse Barnes 
148185f9e50dSDaniel Vetter /* NB: please notice the memset */
148285f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
148385f9e50dSDaniel Vetter 				    uint32_t *instdone)
148485f9e50dSDaniel Vetter {
148585f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
148685f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
148785f9e50dSDaniel Vetter 
148885f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
148985f9e50dSDaniel Vetter 	case 2:
149085f9e50dSDaniel Vetter 	case 3:
149185f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
149285f9e50dSDaniel Vetter 		break;
149385f9e50dSDaniel Vetter 	case 4:
149485f9e50dSDaniel Vetter 	case 5:
149585f9e50dSDaniel Vetter 	case 6:
149685f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
149785f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
149885f9e50dSDaniel Vetter 		break;
149985f9e50dSDaniel Vetter 	default:
150085f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
150185f9e50dSDaniel Vetter 	case 7:
150285f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
150385f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
150485f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
150585f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
150685f9e50dSDaniel Vetter 		break;
150785f9e50dSDaniel Vetter 	}
150885f9e50dSDaniel Vetter }
150985f9e50dSDaniel Vetter 
15103bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
15119df30794SChris Wilson static struct drm_i915_error_object *
1512d0d045e8SBen Widawsky i915_error_object_create_sized(struct drm_i915_private *dev_priv,
1513d0d045e8SBen Widawsky 			       struct drm_i915_gem_object *src,
1514d0d045e8SBen Widawsky 			       const int num_pages)
15159df30794SChris Wilson {
15169df30794SChris Wilson 	struct drm_i915_error_object *dst;
1517d0d045e8SBen Widawsky 	int i;
1518e56660ddSChris Wilson 	u32 reloc_offset;
15199df30794SChris Wilson 
152005394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
15219df30794SChris Wilson 		return NULL;
15229df30794SChris Wilson 
1523d0d045e8SBen Widawsky 	dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
15249df30794SChris Wilson 	if (dst == NULL)
15259df30794SChris Wilson 		return NULL;
15269df30794SChris Wilson 
152705394f39SChris Wilson 	reloc_offset = src->gtt_offset;
1528d0d045e8SBen Widawsky 	for (i = 0; i < num_pages; i++) {
1529788885aeSAndrew Morton 		unsigned long flags;
1530e56660ddSChris Wilson 		void *d;
1531788885aeSAndrew Morton 
1532e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
15339df30794SChris Wilson 		if (d == NULL)
15349df30794SChris Wilson 			goto unwind;
1535e56660ddSChris Wilson 
1536788885aeSAndrew Morton 		local_irq_save(flags);
15375d4545aeSBen Widawsky 		if (reloc_offset < dev_priv->gtt.mappable_end &&
153874898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
1539172975aaSChris Wilson 			void __iomem *s;
1540172975aaSChris Wilson 
1541172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
1542172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
1543172975aaSChris Wilson 			 * captures what the GPU read.
1544172975aaSChris Wilson 			 */
1545172975aaSChris Wilson 
15465d4545aeSBen Widawsky 			s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
15473e4d3af5SPeter Zijlstra 						     reloc_offset);
1548e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
15493e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
1550960e3564SChris Wilson 		} else if (src->stolen) {
1551960e3564SChris Wilson 			unsigned long offset;
1552960e3564SChris Wilson 
1553960e3564SChris Wilson 			offset = dev_priv->mm.stolen_base;
1554960e3564SChris Wilson 			offset += src->stolen->start;
1555960e3564SChris Wilson 			offset += i << PAGE_SHIFT;
1556960e3564SChris Wilson 
15571a240d4dSDaniel Vetter 			memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
1558172975aaSChris Wilson 		} else {
15599da3da66SChris Wilson 			struct page *page;
1560172975aaSChris Wilson 			void *s;
1561172975aaSChris Wilson 
15629da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
1563172975aaSChris Wilson 
15649da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
15659da3da66SChris Wilson 
15669da3da66SChris Wilson 			s = kmap_atomic(page);
1567172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
1568172975aaSChris Wilson 			kunmap_atomic(s);
1569172975aaSChris Wilson 
15709da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
1571172975aaSChris Wilson 		}
1572788885aeSAndrew Morton 		local_irq_restore(flags);
1573e56660ddSChris Wilson 
15749da3da66SChris Wilson 		dst->pages[i] = d;
1575e56660ddSChris Wilson 
1576e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
15779df30794SChris Wilson 	}
1578d0d045e8SBen Widawsky 	dst->page_count = num_pages;
157905394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
15809df30794SChris Wilson 
15819df30794SChris Wilson 	return dst;
15829df30794SChris Wilson 
15839df30794SChris Wilson unwind:
15849da3da66SChris Wilson 	while (i--)
15859da3da66SChris Wilson 		kfree(dst->pages[i]);
15869df30794SChris Wilson 	kfree(dst);
15879df30794SChris Wilson 	return NULL;
15889df30794SChris Wilson }
1589d0d045e8SBen Widawsky #define i915_error_object_create(dev_priv, src) \
1590d0d045e8SBen Widawsky 	i915_error_object_create_sized((dev_priv), (src), \
1591d0d045e8SBen Widawsky 				       (src)->base.size>>PAGE_SHIFT)
15929df30794SChris Wilson 
15939df30794SChris Wilson static void
15949df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
15959df30794SChris Wilson {
15969df30794SChris Wilson 	int page;
15979df30794SChris Wilson 
15989df30794SChris Wilson 	if (obj == NULL)
15999df30794SChris Wilson 		return;
16009df30794SChris Wilson 
16019df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
16029df30794SChris Wilson 		kfree(obj->pages[page]);
16039df30794SChris Wilson 
16049df30794SChris Wilson 	kfree(obj);
16059df30794SChris Wilson }
16069df30794SChris Wilson 
1607742cbee8SDaniel Vetter void
1608742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
16099df30794SChris Wilson {
1610742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
1611742cbee8SDaniel Vetter 							  typeof(*error), ref);
1612e2f973d5SChris Wilson 	int i;
1613e2f973d5SChris Wilson 
161452d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
161552d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
161652d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
16177ed73da0SBen Widawsky 		i915_error_object_free(error->ring[i].ctx);
161852d39a21SChris Wilson 		kfree(error->ring[i].requests);
161952d39a21SChris Wilson 	}
1620e2f973d5SChris Wilson 
16219df30794SChris Wilson 	kfree(error->active_bo);
16226ef3d427SChris Wilson 	kfree(error->overlay);
16237ed73da0SBen Widawsky 	kfree(error->display);
16249df30794SChris Wilson 	kfree(error);
16259df30794SChris Wilson }
16261b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
16271b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
1628c724e8a9SChris Wilson {
1629c724e8a9SChris Wilson 	err->size = obj->base.size;
1630c724e8a9SChris Wilson 	err->name = obj->base.name;
16310201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
16320201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1633c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
1634c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1635c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1636c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1637c724e8a9SChris Wilson 	err->pinned = 0;
1638c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1639c724e8a9SChris Wilson 		err->pinned = 1;
1640c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1641c724e8a9SChris Wilson 		err->pinned = -1;
1642c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1643c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1644c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
164596154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
164693dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
16471b50247aSChris Wilson }
1648c724e8a9SChris Wilson 
16491b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
16501b50247aSChris Wilson 			     int count, struct list_head *head)
16511b50247aSChris Wilson {
16521b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
16531b50247aSChris Wilson 	int i = 0;
16541b50247aSChris Wilson 
16551b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
16561b50247aSChris Wilson 		capture_bo(err++, obj);
1657c724e8a9SChris Wilson 		if (++i == count)
1658c724e8a9SChris Wilson 			break;
16591b50247aSChris Wilson 	}
1660c724e8a9SChris Wilson 
16611b50247aSChris Wilson 	return i;
16621b50247aSChris Wilson }
16631b50247aSChris Wilson 
16641b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
16651b50247aSChris Wilson 			     int count, struct list_head *head)
16661b50247aSChris Wilson {
16671b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
16681b50247aSChris Wilson 	int i = 0;
16691b50247aSChris Wilson 
167035c20a60SBen Widawsky 	list_for_each_entry(obj, head, global_list) {
16711b50247aSChris Wilson 		if (obj->pin_count == 0)
16721b50247aSChris Wilson 			continue;
16731b50247aSChris Wilson 
16741b50247aSChris Wilson 		capture_bo(err++, obj);
16751b50247aSChris Wilson 		if (++i == count)
16761b50247aSChris Wilson 			break;
1677c724e8a9SChris Wilson 	}
1678c724e8a9SChris Wilson 
1679c724e8a9SChris Wilson 	return i;
1680c724e8a9SChris Wilson }
1681c724e8a9SChris Wilson 
1682748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1683748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1684748ebc60SChris Wilson {
1685748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1686748ebc60SChris Wilson 	int i;
1687748ebc60SChris Wilson 
1688748ebc60SChris Wilson 	/* Fences */
1689748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1690775d17b6SDaniel Vetter 	case 7:
1691748ebc60SChris Wilson 	case 6:
169242b5aeabSVille Syrjälä 		for (i = 0; i < dev_priv->num_fence_regs; i++)
1693748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1694748ebc60SChris Wilson 		break;
1695748ebc60SChris Wilson 	case 5:
1696748ebc60SChris Wilson 	case 4:
1697748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1698748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1699748ebc60SChris Wilson 		break;
1700748ebc60SChris Wilson 	case 3:
1701748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1702748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1703748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1704748ebc60SChris Wilson 	case 2:
1705748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1706748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1707748ebc60SChris Wilson 		break;
1708748ebc60SChris Wilson 
17097dbf9d6eSBen Widawsky 	default:
17107dbf9d6eSBen Widawsky 		BUG();
1711748ebc60SChris Wilson 	}
1712748ebc60SChris Wilson }
1713748ebc60SChris Wilson 
1714bcfb2e28SChris Wilson static struct drm_i915_error_object *
1715bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1716bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1717bcfb2e28SChris Wilson {
1718bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1719bcfb2e28SChris Wilson 	u32 seqno;
1720bcfb2e28SChris Wilson 
1721bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1722bcfb2e28SChris Wilson 		return NULL;
1723bcfb2e28SChris Wilson 
1724b45305fcSDaniel Vetter 	if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1725b45305fcSDaniel Vetter 		u32 acthd = I915_READ(ACTHD);
1726b45305fcSDaniel Vetter 
1727b45305fcSDaniel Vetter 		if (WARN_ON(ring->id != RCS))
1728b45305fcSDaniel Vetter 			return NULL;
1729b45305fcSDaniel Vetter 
1730b45305fcSDaniel Vetter 		obj = ring->private;
1731b45305fcSDaniel Vetter 		if (acthd >= obj->gtt_offset &&
1732b45305fcSDaniel Vetter 		    acthd < obj->gtt_offset + obj->base.size)
1733b45305fcSDaniel Vetter 			return i915_error_object_create(dev_priv, obj);
1734b45305fcSDaniel Vetter 	}
1735b45305fcSDaniel Vetter 
1736b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1737bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1738bcfb2e28SChris Wilson 		if (obj->ring != ring)
1739bcfb2e28SChris Wilson 			continue;
1740bcfb2e28SChris Wilson 
17410201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1742bcfb2e28SChris Wilson 			continue;
1743bcfb2e28SChris Wilson 
1744bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1745bcfb2e28SChris Wilson 			continue;
1746bcfb2e28SChris Wilson 
1747bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1748bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1749bcfb2e28SChris Wilson 		 */
1750bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1751bcfb2e28SChris Wilson 	}
1752bcfb2e28SChris Wilson 
1753bcfb2e28SChris Wilson 	return NULL;
1754bcfb2e28SChris Wilson }
1755bcfb2e28SChris Wilson 
1756d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1757d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1758d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1759d27b1e0eSDaniel Vetter {
1760d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1761d27b1e0eSDaniel Vetter 
176233f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
176312f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
176433f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
17657e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
17667e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
17677e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
17687e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
1769df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1770df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
177133f3f518SDaniel Vetter 	}
1772c1cd90edSDaniel Vetter 
1773d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
17749d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1775d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1776d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1777d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1778c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1779050ee91fSBen Widawsky 		if (ring->id == RCS)
1780d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1781d27b1e0eSDaniel Vetter 	} else {
17829d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1783d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1784d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1785d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1786d27b1e0eSDaniel Vetter 	}
1787d27b1e0eSDaniel Vetter 
17889574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1789c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1790b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1791d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1792c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1793c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
17940f3b6849SChris Wilson 	error->ctl[ring->id] = I915_READ_CTL(ring);
17957e3b8737SDaniel Vetter 
17967e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
17977e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1798d27b1e0eSDaniel Vetter }
1799d27b1e0eSDaniel Vetter 
18008c123e54SBen Widawsky 
18018c123e54SBen Widawsky static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
18028c123e54SBen Widawsky 					   struct drm_i915_error_state *error,
18038c123e54SBen Widawsky 					   struct drm_i915_error_ring *ering)
18048c123e54SBen Widawsky {
18058c123e54SBen Widawsky 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
18068c123e54SBen Widawsky 	struct drm_i915_gem_object *obj;
18078c123e54SBen Widawsky 
18088c123e54SBen Widawsky 	/* Currently render ring is the only HW context user */
18098c123e54SBen Widawsky 	if (ring->id != RCS || !error->ccid)
18108c123e54SBen Widawsky 		return;
18118c123e54SBen Widawsky 
181235c20a60SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
18138c123e54SBen Widawsky 		if ((error->ccid & PAGE_MASK) == obj->gtt_offset) {
18148c123e54SBen Widawsky 			ering->ctx = i915_error_object_create_sized(dev_priv,
18158c123e54SBen Widawsky 								    obj, 1);
18168c123e54SBen Widawsky 		}
18178c123e54SBen Widawsky 	}
18188c123e54SBen Widawsky }
18198c123e54SBen Widawsky 
182052d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
182152d39a21SChris Wilson 				  struct drm_i915_error_state *error)
182252d39a21SChris Wilson {
182352d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1824b4519513SChris Wilson 	struct intel_ring_buffer *ring;
182552d39a21SChris Wilson 	struct drm_i915_gem_request *request;
182652d39a21SChris Wilson 	int i, count;
182752d39a21SChris Wilson 
1828b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
182952d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
183052d39a21SChris Wilson 
183152d39a21SChris Wilson 		error->ring[i].batchbuffer =
183252d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
183352d39a21SChris Wilson 
183452d39a21SChris Wilson 		error->ring[i].ringbuffer =
183552d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
183652d39a21SChris Wilson 
18378c123e54SBen Widawsky 
18388c123e54SBen Widawsky 		i915_gem_record_active_context(ring, error, &error->ring[i]);
18398c123e54SBen Widawsky 
184052d39a21SChris Wilson 		count = 0;
184152d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
184252d39a21SChris Wilson 			count++;
184352d39a21SChris Wilson 
184452d39a21SChris Wilson 		error->ring[i].num_requests = count;
184552d39a21SChris Wilson 		error->ring[i].requests =
184652d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
184752d39a21SChris Wilson 				GFP_ATOMIC);
184852d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
184952d39a21SChris Wilson 			error->ring[i].num_requests = 0;
185052d39a21SChris Wilson 			continue;
185152d39a21SChris Wilson 		}
185252d39a21SChris Wilson 
185352d39a21SChris Wilson 		count = 0;
185452d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
185552d39a21SChris Wilson 			struct drm_i915_error_request *erq;
185652d39a21SChris Wilson 
185752d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
185852d39a21SChris Wilson 			erq->seqno = request->seqno;
185952d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1860ee4f42b1SChris Wilson 			erq->tail = request->tail;
186152d39a21SChris Wilson 		}
186252d39a21SChris Wilson 	}
186352d39a21SChris Wilson }
186452d39a21SChris Wilson 
18658a905236SJesse Barnes /**
18668a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
18678a905236SJesse Barnes  * @dev: drm device
18688a905236SJesse Barnes  *
18698a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
18708a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
18718a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
18728a905236SJesse Barnes  * to pick up.
18738a905236SJesse Barnes  */
187463eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
187563eeaf38SJesse Barnes {
187663eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
187705394f39SChris Wilson 	struct drm_i915_gem_object *obj;
187863eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
187963eeaf38SJesse Barnes 	unsigned long flags;
18809db4a9c7SJesse Barnes 	int i, pipe;
188163eeaf38SJesse Barnes 
188299584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
188399584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
188499584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
18859df30794SChris Wilson 	if (error)
18869df30794SChris Wilson 		return;
188763eeaf38SJesse Barnes 
18889db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
188933f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
189063eeaf38SJesse Barnes 	if (!error) {
18919df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
18929df30794SChris Wilson 		return;
189363eeaf38SJesse Barnes 	}
189463eeaf38SJesse Barnes 
18952f86f191SBen Widawsky 	DRM_INFO("capturing error event; look for more information in "
18962f86f191SBen Widawsky 		 "/sys/kernel/debug/dri/%d/i915_error_state\n",
1897b6f7833bSChris Wilson 		 dev->primary->index);
18982fa772f3SChris Wilson 
1899742cbee8SDaniel Vetter 	kref_init(&error->ref);
190063eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
190163eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1902211816ecSBen Widawsky 	if (HAS_HW_CONTEXTS(dev))
1903b9a3906bSBen Widawsky 		error->ccid = I915_READ(CCID);
1904be998e2eSBen Widawsky 
1905be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1906be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1907be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1908be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1909be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1910be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1911be998e2eSBen Widawsky 	else
1912be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1913be998e2eSBen Widawsky 
19140f3b6849SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6)
19150f3b6849SChris Wilson 		error->derrmr = I915_READ(DERRMR);
19160f3b6849SChris Wilson 
19170f3b6849SChris Wilson 	if (IS_VALLEYVIEW(dev))
19180f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_VLV);
19190f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 7)
19200f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_MT);
19210f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen == 6)
19220f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE);
19230f3b6849SChris Wilson 
19244f3308b9SPaulo Zanoni 	if (!HAS_PCH_SPLIT(dev))
19259db4a9c7SJesse Barnes 		for_each_pipe(pipe)
19269db4a9c7SJesse Barnes 			error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1927d27b1e0eSDaniel Vetter 
192833f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1929f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
193033f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
193133f3f518SDaniel Vetter 	}
1932add354ddSChris Wilson 
193371e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
193471e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
193571e172e8SBen Widawsky 
1936050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1937050ee91fSBen Widawsky 
1938748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
193952d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
19409df30794SChris Wilson 
1941c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
19429df30794SChris Wilson 	error->active_bo = NULL;
1943c724e8a9SChris Wilson 	error->pinned_bo = NULL;
19449df30794SChris Wilson 
1945bcfb2e28SChris Wilson 	i = 0;
1946bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1947bcfb2e28SChris Wilson 		i++;
1948bcfb2e28SChris Wilson 	error->active_bo_count = i;
194935c20a60SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
19501b50247aSChris Wilson 		if (obj->pin_count)
1951bcfb2e28SChris Wilson 			i++;
1952bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1953c724e8a9SChris Wilson 
19548e934dbfSChris Wilson 	error->active_bo = NULL;
19558e934dbfSChris Wilson 	error->pinned_bo = NULL;
1956bcfb2e28SChris Wilson 	if (i) {
1957bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
19589df30794SChris Wilson 					   GFP_ATOMIC);
1959c724e8a9SChris Wilson 		if (error->active_bo)
1960c724e8a9SChris Wilson 			error->pinned_bo =
1961c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
19629df30794SChris Wilson 	}
1963c724e8a9SChris Wilson 
1964c724e8a9SChris Wilson 	if (error->active_bo)
1965c724e8a9SChris Wilson 		error->active_bo_count =
19661b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1967c724e8a9SChris Wilson 					  error->active_bo_count,
1968c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1969c724e8a9SChris Wilson 
1970c724e8a9SChris Wilson 	if (error->pinned_bo)
1971c724e8a9SChris Wilson 		error->pinned_bo_count =
19721b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1973c724e8a9SChris Wilson 					  error->pinned_bo_count,
19746c085a72SChris Wilson 					  &dev_priv->mm.bound_list);
197563eeaf38SJesse Barnes 
19768a905236SJesse Barnes 	do_gettimeofday(&error->time);
19778a905236SJesse Barnes 
19786ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1979c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
19806ef3d427SChris Wilson 
198199584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
198299584db3SDaniel Vetter 	if (dev_priv->gpu_error.first_error == NULL) {
198399584db3SDaniel Vetter 		dev_priv->gpu_error.first_error = error;
19849df30794SChris Wilson 		error = NULL;
19859df30794SChris Wilson 	}
198699584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19879df30794SChris Wilson 
19889df30794SChris Wilson 	if (error)
1989742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
19909df30794SChris Wilson }
19919df30794SChris Wilson 
19929df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
19939df30794SChris Wilson {
19949df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
19959df30794SChris Wilson 	struct drm_i915_error_state *error;
19966dc0e816SBen Widawsky 	unsigned long flags;
19979df30794SChris Wilson 
199899584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
199999584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
200099584db3SDaniel Vetter 	dev_priv->gpu_error.first_error = NULL;
200199584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
20029df30794SChris Wilson 
20039df30794SChris Wilson 	if (error)
2004742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
200563eeaf38SJesse Barnes }
20063bd3c932SChris Wilson #else
20073bd3c932SChris Wilson #define i915_capture_error_state(x)
20083bd3c932SChris Wilson #endif
200963eeaf38SJesse Barnes 
201035aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2011c0e09200SDave Airlie {
20128a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2013bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
201463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2015050ee91fSBen Widawsky 	int pipe, i;
201663eeaf38SJesse Barnes 
201735aed2e6SChris Wilson 	if (!eir)
201835aed2e6SChris Wilson 		return;
201963eeaf38SJesse Barnes 
2020a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
20218a905236SJesse Barnes 
2022bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2023bd9854f9SBen Widawsky 
20248a905236SJesse Barnes 	if (IS_G4X(dev)) {
20258a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
20268a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
20278a905236SJesse Barnes 
2028a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2029a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2030050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2031050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2032a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2033a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
20348a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20353143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
20368a905236SJesse Barnes 		}
20378a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
20388a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2039a70491ccSJoe Perches 			pr_err("page table error\n");
2040a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
20418a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20423143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
20438a905236SJesse Barnes 		}
20448a905236SJesse Barnes 	}
20458a905236SJesse Barnes 
2046a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
204763eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
204863eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2049a70491ccSJoe Perches 			pr_err("page table error\n");
2050a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
205163eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20523143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
205363eeaf38SJesse Barnes 		}
20548a905236SJesse Barnes 	}
20558a905236SJesse Barnes 
205663eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2057a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
20589db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2059a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
20609db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
206163eeaf38SJesse Barnes 		/* pipestat has already been acked */
206263eeaf38SJesse Barnes 	}
206363eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2064a70491ccSJoe Perches 		pr_err("instruction error\n");
2065a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2066050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2067050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2068a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
206963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
207063eeaf38SJesse Barnes 
2071a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2072a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2073a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
207463eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
20753143a2bfSChris Wilson 			POSTING_READ(IPEIR);
207663eeaf38SJesse Barnes 		} else {
207763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
207863eeaf38SJesse Barnes 
2079a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2080a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2081a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2082a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
208363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20843143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
208563eeaf38SJesse Barnes 		}
208663eeaf38SJesse Barnes 	}
208763eeaf38SJesse Barnes 
208863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
20893143a2bfSChris Wilson 	POSTING_READ(EIR);
209063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
209163eeaf38SJesse Barnes 	if (eir) {
209263eeaf38SJesse Barnes 		/*
209363eeaf38SJesse Barnes 		 * some errors might have become stuck,
209463eeaf38SJesse Barnes 		 * mask them.
209563eeaf38SJesse Barnes 		 */
209663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
209763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
209863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
209963eeaf38SJesse Barnes 	}
210035aed2e6SChris Wilson }
210135aed2e6SChris Wilson 
210235aed2e6SChris Wilson /**
210335aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
210435aed2e6SChris Wilson  * @dev: drm device
210535aed2e6SChris Wilson  *
210635aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
210735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
210835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
210935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
211035aed2e6SChris Wilson  * of a ring dump etc.).
211135aed2e6SChris Wilson  */
2112527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
211335aed2e6SChris Wilson {
211435aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2115b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2116b4519513SChris Wilson 	int i;
211735aed2e6SChris Wilson 
211835aed2e6SChris Wilson 	i915_capture_error_state(dev);
211935aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
21208a905236SJesse Barnes 
2121ba1234d1SBen Gamari 	if (wedged) {
2122f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2123f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2124ba1234d1SBen Gamari 
212511ed50ecSBen Gamari 		/*
21261f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
21271f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
212811ed50ecSBen Gamari 		 */
2129b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
2130b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
213111ed50ecSBen Gamari 	}
213211ed50ecSBen Gamari 
213399584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
21348a905236SJesse Barnes }
21358a905236SJesse Barnes 
213621ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
21374e5359cdSSimon Farnsworth {
21384e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
21394e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
21404e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
214105394f39SChris Wilson 	struct drm_i915_gem_object *obj;
21424e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
21434e5359cdSSimon Farnsworth 	unsigned long flags;
21444e5359cdSSimon Farnsworth 	bool stall_detected;
21454e5359cdSSimon Farnsworth 
21464e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
21474e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
21484e5359cdSSimon Farnsworth 		return;
21494e5359cdSSimon Farnsworth 
21504e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
21514e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
21524e5359cdSSimon Farnsworth 
2153e7d841caSChris Wilson 	if (work == NULL ||
2154e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2155e7d841caSChris Wilson 	    !work->enable_stall_check) {
21564e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
21574e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
21584e5359cdSSimon Farnsworth 		return;
21594e5359cdSSimon Farnsworth 	}
21604e5359cdSSimon Farnsworth 
21614e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
216205394f39SChris Wilson 	obj = work->pending_flip_obj;
2163a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
21649db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2165446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2166446f2545SArmin Reese 					obj->gtt_offset;
21674e5359cdSSimon Farnsworth 	} else {
21689db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
216905394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
217001f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
21714e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
21724e5359cdSSimon Farnsworth 	}
21734e5359cdSSimon Farnsworth 
21744e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
21754e5359cdSSimon Farnsworth 
21764e5359cdSSimon Farnsworth 	if (stall_detected) {
21774e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
21784e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
21794e5359cdSSimon Farnsworth 	}
21804e5359cdSSimon Farnsworth }
21814e5359cdSSimon Farnsworth 
218242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
218342f52ef8SKeith Packard  * we use as a pipe index
218442f52ef8SKeith Packard  */
2185f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
21860a3e67a4SJesse Barnes {
21870a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2188e9d21d7fSKeith Packard 	unsigned long irqflags;
218971e0ffa5SJesse Barnes 
21905eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
219171e0ffa5SJesse Barnes 		return -EINVAL;
21920a3e67a4SJesse Barnes 
21931ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2194f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
21957c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21967c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
21970a3e67a4SJesse Barnes 	else
21987c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21997c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
22008692d00eSChris Wilson 
22018692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
22028692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
22036b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
22041ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22058692d00eSChris Wilson 
22060a3e67a4SJesse Barnes 	return 0;
22070a3e67a4SJesse Barnes }
22080a3e67a4SJesse Barnes 
2209f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2210f796cf8fSJesse Barnes {
2211f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2212f796cf8fSJesse Barnes 	unsigned long irqflags;
2213f796cf8fSJesse Barnes 
2214f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2215f796cf8fSJesse Barnes 		return -EINVAL;
2216f796cf8fSJesse Barnes 
2217f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2218f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
2219f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2220f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2221f796cf8fSJesse Barnes 
2222f796cf8fSJesse Barnes 	return 0;
2223f796cf8fSJesse Barnes }
2224f796cf8fSJesse Barnes 
2225f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
2226b1f14ad0SJesse Barnes {
2227b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2228b1f14ad0SJesse Barnes 	unsigned long irqflags;
2229b1f14ad0SJesse Barnes 
2230b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2231b1f14ad0SJesse Barnes 		return -EINVAL;
2232b1f14ad0SJesse Barnes 
2233b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2234b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
2235b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
2236b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2237b1f14ad0SJesse Barnes 
2238b1f14ad0SJesse Barnes 	return 0;
2239b1f14ad0SJesse Barnes }
2240b1f14ad0SJesse Barnes 
22417e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
22427e231dbeSJesse Barnes {
22437e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22447e231dbeSJesse Barnes 	unsigned long irqflags;
224531acc7f5SJesse Barnes 	u32 imr;
22467e231dbeSJesse Barnes 
22477e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
22487e231dbeSJesse Barnes 		return -EINVAL;
22497e231dbeSJesse Barnes 
22507e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22517e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
225231acc7f5SJesse Barnes 	if (pipe == 0)
22537e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
225431acc7f5SJesse Barnes 	else
22557e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22567e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
225731acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
225831acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
22597e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22607e231dbeSJesse Barnes 
22617e231dbeSJesse Barnes 	return 0;
22627e231dbeSJesse Barnes }
22637e231dbeSJesse Barnes 
226442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
226542f52ef8SKeith Packard  * we use as a pipe index
226642f52ef8SKeith Packard  */
2267f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
22680a3e67a4SJesse Barnes {
22690a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2270e9d21d7fSKeith Packard 	unsigned long irqflags;
22710a3e67a4SJesse Barnes 
22721ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22738692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
22746b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
22758692d00eSChris Wilson 
22767c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
22777c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
22787c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
22791ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22800a3e67a4SJesse Barnes }
22810a3e67a4SJesse Barnes 
2282f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2283f796cf8fSJesse Barnes {
2284f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2285f796cf8fSJesse Barnes 	unsigned long irqflags;
2286f796cf8fSJesse Barnes 
2287f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2288f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
2289f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2290f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2291f796cf8fSJesse Barnes }
2292f796cf8fSJesse Barnes 
2293f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
2294b1f14ad0SJesse Barnes {
2295b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2296b1f14ad0SJesse Barnes 	unsigned long irqflags;
2297b1f14ad0SJesse Barnes 
2298b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2299b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
2300b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
2301b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2302b1f14ad0SJesse Barnes }
2303b1f14ad0SJesse Barnes 
23047e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
23057e231dbeSJesse Barnes {
23067e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23077e231dbeSJesse Barnes 	unsigned long irqflags;
230831acc7f5SJesse Barnes 	u32 imr;
23097e231dbeSJesse Barnes 
23107e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
231131acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
231231acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
23137e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
231431acc7f5SJesse Barnes 	if (pipe == 0)
23157e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
231631acc7f5SJesse Barnes 	else
23177e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23187e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
23197e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
23207e231dbeSJesse Barnes }
23217e231dbeSJesse Barnes 
2322893eead0SChris Wilson static u32
2323893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2324852835f3SZou Nan hai {
2325893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2326893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2327893eead0SChris Wilson }
2328893eead0SChris Wilson 
23299107e9d2SChris Wilson static bool
23309107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2331893eead0SChris Wilson {
23329107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
23339107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2334f65d9421SBen Gamari }
2335f65d9421SBen Gamari 
23366274f212SChris Wilson static struct intel_ring_buffer *
23376274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2338a24a11e6SChris Wilson {
2339a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
23406274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2341a24a11e6SChris Wilson 
2342a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2343a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2344a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
23456274f212SChris Wilson 		return NULL;
2346a24a11e6SChris Wilson 
2347a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2348a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2349a24a11e6SChris Wilson 	 */
23506274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2351a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2352a24a11e6SChris Wilson 	do {
2353a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2354a24a11e6SChris Wilson 		if (cmd == ipehr)
2355a24a11e6SChris Wilson 			break;
2356a24a11e6SChris Wilson 
2357a24a11e6SChris Wilson 		acthd -= 4;
2358a24a11e6SChris Wilson 		if (acthd < acthd_min)
23596274f212SChris Wilson 			return NULL;
2360a24a11e6SChris Wilson 	} while (1);
2361a24a11e6SChris Wilson 
23626274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
23636274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2364a24a11e6SChris Wilson }
2365a24a11e6SChris Wilson 
23666274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
23676274f212SChris Wilson {
23686274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
23696274f212SChris Wilson 	struct intel_ring_buffer *signaller;
23706274f212SChris Wilson 	u32 seqno, ctl;
23716274f212SChris Wilson 
23726274f212SChris Wilson 	ring->hangcheck.deadlock = true;
23736274f212SChris Wilson 
23746274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
23756274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
23766274f212SChris Wilson 		return -1;
23776274f212SChris Wilson 
23786274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
23796274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
23806274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
23816274f212SChris Wilson 		return -1;
23826274f212SChris Wilson 
23836274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
23846274f212SChris Wilson }
23856274f212SChris Wilson 
23866274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
23876274f212SChris Wilson {
23886274f212SChris Wilson 	struct intel_ring_buffer *ring;
23896274f212SChris Wilson 	int i;
23906274f212SChris Wilson 
23916274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
23926274f212SChris Wilson 		ring->hangcheck.deadlock = false;
23936274f212SChris Wilson }
23946274f212SChris Wilson 
2395ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2396ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
23971ec14ad3SChris Wilson {
23981ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
23991ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
24009107e9d2SChris Wilson 	u32 tmp;
24019107e9d2SChris Wilson 
24026274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
24036274f212SChris Wilson 		return active;
24046274f212SChris Wilson 
24059107e9d2SChris Wilson 	if (IS_GEN2(dev))
24066274f212SChris Wilson 		return hung;
24079107e9d2SChris Wilson 
24089107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
24099107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
24109107e9d2SChris Wilson 	 * and break the hang. This should work on
24119107e9d2SChris Wilson 	 * all but the second generation chipsets.
24129107e9d2SChris Wilson 	 */
24139107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
24141ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
24151ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
24161ec14ad3SChris Wilson 			  ring->name);
24171ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
24186274f212SChris Wilson 		return kick;
24191ec14ad3SChris Wilson 	}
2420a24a11e6SChris Wilson 
24216274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
24226274f212SChris Wilson 		switch (semaphore_passed(ring)) {
24236274f212SChris Wilson 		default:
24246274f212SChris Wilson 			return hung;
24256274f212SChris Wilson 		case 1:
2426a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
2427a24a11e6SChris Wilson 				  ring->name);
2428a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
24296274f212SChris Wilson 			return kick;
24306274f212SChris Wilson 		case 0:
24316274f212SChris Wilson 			return wait;
24326274f212SChris Wilson 		}
24339107e9d2SChris Wilson 	}
24349107e9d2SChris Wilson 
24356274f212SChris Wilson 	return hung;
2436a24a11e6SChris Wilson }
2437d1e61e7fSChris Wilson 
2438f65d9421SBen Gamari /**
2439f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
244005407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
244105407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
244205407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
244305407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
244405407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2445f65d9421SBen Gamari  */
2446f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
2447f65d9421SBen Gamari {
2448f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2449f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2450b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2451b4519513SChris Wilson 	int i;
245205407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
24539107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
24549107e9d2SChris Wilson #define BUSY 1
24559107e9d2SChris Wilson #define KICK 5
24569107e9d2SChris Wilson #define HUNG 20
24579107e9d2SChris Wilson #define FIRE 30
2458893eead0SChris Wilson 
24593e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
24603e0dc6b0SBen Widawsky 		return;
24613e0dc6b0SBen Widawsky 
2462b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
246305407ff8SMika Kuoppala 		u32 seqno, acthd;
24649107e9d2SChris Wilson 		bool busy = true;
2465b4519513SChris Wilson 
24666274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
24676274f212SChris Wilson 
246805407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
246905407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
247005407ff8SMika Kuoppala 
247105407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
24729107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
24739107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
24749107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
24759107e9d2SChris Wilson 					DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
24769107e9d2SChris Wilson 						  ring->name);
24779107e9d2SChris Wilson 					wake_up_all(&ring->irq_queue);
24789107e9d2SChris Wilson 					ring->hangcheck.score += HUNG;
24799107e9d2SChris Wilson 				} else
24809107e9d2SChris Wilson 					busy = false;
248105407ff8SMika Kuoppala 			} else {
24829107e9d2SChris Wilson 				int score;
24839107e9d2SChris Wilson 
24846274f212SChris Wilson 				/* We always increment the hangcheck score
24856274f212SChris Wilson 				 * if the ring is busy and still processing
24866274f212SChris Wilson 				 * the same request, so that no single request
24876274f212SChris Wilson 				 * can run indefinitely (such as a chain of
24886274f212SChris Wilson 				 * batches). The only time we do not increment
24896274f212SChris Wilson 				 * the hangcheck score on this ring, if this
24906274f212SChris Wilson 				 * ring is in a legitimate wait for another
24916274f212SChris Wilson 				 * ring. In that case the waiting ring is a
24926274f212SChris Wilson 				 * victim and we want to be sure we catch the
24936274f212SChris Wilson 				 * right culprit. Then every time we do kick
24946274f212SChris Wilson 				 * the ring, add a small increment to the
24956274f212SChris Wilson 				 * score so that we can catch a batch that is
24966274f212SChris Wilson 				 * being repeatedly kicked and so responsible
24976274f212SChris Wilson 				 * for stalling the machine.
24989107e9d2SChris Wilson 				 */
2499ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2500ad8beaeaSMika Kuoppala 								    acthd);
2501ad8beaeaSMika Kuoppala 
2502ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
25036274f212SChris Wilson 				case wait:
25046274f212SChris Wilson 					score = 0;
25056274f212SChris Wilson 					break;
25066274f212SChris Wilson 				case active:
25079107e9d2SChris Wilson 					score = BUSY;
25086274f212SChris Wilson 					break;
25096274f212SChris Wilson 				case kick:
25106274f212SChris Wilson 					score = KICK;
25116274f212SChris Wilson 					break;
25126274f212SChris Wilson 				case hung:
25136274f212SChris Wilson 					score = HUNG;
25146274f212SChris Wilson 					stuck[i] = true;
25156274f212SChris Wilson 					break;
25166274f212SChris Wilson 				}
25179107e9d2SChris Wilson 				ring->hangcheck.score += score;
251805407ff8SMika Kuoppala 			}
25199107e9d2SChris Wilson 		} else {
25209107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
25219107e9d2SChris Wilson 			 * attempts across multiple batches.
25229107e9d2SChris Wilson 			 */
25239107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
25249107e9d2SChris Wilson 				ring->hangcheck.score--;
2525cbb465e7SChris Wilson 		}
2526f65d9421SBen Gamari 
252705407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
252805407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
25299107e9d2SChris Wilson 		busy_count += busy;
253005407ff8SMika Kuoppala 	}
253105407ff8SMika Kuoppala 
253205407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
25339107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2534acd78c11SBen Widawsky 			DRM_ERROR("%s on %s\n",
253505407ff8SMika Kuoppala 				  stuck[i] ? "stuck" : "no progress",
2536a43adf07SChris Wilson 				  ring->name);
2537a43adf07SChris Wilson 			rings_hung++;
253805407ff8SMika Kuoppala 		}
253905407ff8SMika Kuoppala 	}
254005407ff8SMika Kuoppala 
254105407ff8SMika Kuoppala 	if (rings_hung)
254205407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
254305407ff8SMika Kuoppala 
254405407ff8SMika Kuoppala 	if (busy_count)
254505407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
254605407ff8SMika Kuoppala 		 * being added */
254799584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
254805407ff8SMika Kuoppala 			  round_jiffies_up(jiffies +
254905407ff8SMika Kuoppala 					   DRM_I915_HANGCHECK_JIFFIES));
2550f65d9421SBen Gamari }
2551f65d9421SBen Gamari 
255291738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
255391738a95SPaulo Zanoni {
255491738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
255591738a95SPaulo Zanoni 
255691738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
255791738a95SPaulo Zanoni 		return;
255891738a95SPaulo Zanoni 
255991738a95SPaulo Zanoni 	/* south display irq */
256091738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
256191738a95SPaulo Zanoni 	/*
256291738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
256391738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
256491738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
256591738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
256691738a95SPaulo Zanoni 	 */
256791738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
256891738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
256991738a95SPaulo Zanoni }
257091738a95SPaulo Zanoni 
2571c0e09200SDave Airlie /* drm_dma.h hooks
2572c0e09200SDave Airlie */
2573f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2574036a4a7dSZhenyu Wang {
2575036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2576036a4a7dSZhenyu Wang 
25774697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
25784697995bSJesse Barnes 
2579036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2580bdfcdb63SDaniel Vetter 
2581036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
2582036a4a7dSZhenyu Wang 
2583036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2584036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
25853143a2bfSChris Wilson 	POSTING_READ(DEIER);
2586036a4a7dSZhenyu Wang 
2587036a4a7dSZhenyu Wang 	/* and GT */
2588036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2589036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
25903143a2bfSChris Wilson 	POSTING_READ(GTIER);
2591c650156aSZhenyu Wang 
259291738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
25937d99163dSBen Widawsky }
25947d99163dSBen Widawsky 
25957d99163dSBen Widawsky static void ivybridge_irq_preinstall(struct drm_device *dev)
25967d99163dSBen Widawsky {
25977d99163dSBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25987d99163dSBen Widawsky 
25997d99163dSBen Widawsky 	atomic_set(&dev_priv->irq_received, 0);
26007d99163dSBen Widawsky 
26017d99163dSBen Widawsky 	I915_WRITE(HWSTAM, 0xeffe);
26027d99163dSBen Widawsky 
26037d99163dSBen Widawsky 	/* XXX hotplug from PCH */
26047d99163dSBen Widawsky 
26057d99163dSBen Widawsky 	I915_WRITE(DEIMR, 0xffffffff);
26067d99163dSBen Widawsky 	I915_WRITE(DEIER, 0x0);
26077d99163dSBen Widawsky 	POSTING_READ(DEIER);
26087d99163dSBen Widawsky 
26097d99163dSBen Widawsky 	/* and GT */
26107d99163dSBen Widawsky 	I915_WRITE(GTIMR, 0xffffffff);
26117d99163dSBen Widawsky 	I915_WRITE(GTIER, 0x0);
26127d99163dSBen Widawsky 	POSTING_READ(GTIER);
26137d99163dSBen Widawsky 
2614eda63ffbSBen Widawsky 	/* Power management */
2615eda63ffbSBen Widawsky 	I915_WRITE(GEN6_PMIMR, 0xffffffff);
2616eda63ffbSBen Widawsky 	I915_WRITE(GEN6_PMIER, 0x0);
2617eda63ffbSBen Widawsky 	POSTING_READ(GEN6_PMIER);
2618eda63ffbSBen Widawsky 
261991738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
2620036a4a7dSZhenyu Wang }
2621036a4a7dSZhenyu Wang 
26227e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
26237e231dbeSJesse Barnes {
26247e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26257e231dbeSJesse Barnes 	int pipe;
26267e231dbeSJesse Barnes 
26277e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
26287e231dbeSJesse Barnes 
26297e231dbeSJesse Barnes 	/* VLV magic */
26307e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
26317e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
26327e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
26337e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
26347e231dbeSJesse Barnes 
26357e231dbeSJesse Barnes 	/* and GT */
26367e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
26377e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
26387e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
26397e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
26407e231dbeSJesse Barnes 	POSTING_READ(GTIER);
26417e231dbeSJesse Barnes 
26427e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
26437e231dbeSJesse Barnes 
26447e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
26457e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
26467e231dbeSJesse Barnes 	for_each_pipe(pipe)
26477e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
26487e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26497e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
26507e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
26517e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
26527e231dbeSJesse Barnes }
26537e231dbeSJesse Barnes 
265482a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
265582a28bcfSDaniel Vetter {
265682a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
265782a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
265882a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
265982a28bcfSDaniel Vetter 	u32 mask = ~I915_READ(SDEIMR);
266082a28bcfSDaniel Vetter 	u32 hotplug;
266182a28bcfSDaniel Vetter 
266282a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2663995e6b3dSEgbert Eich 		mask &= ~SDE_HOTPLUG_MASK;
266482a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2665cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
266682a28bcfSDaniel Vetter 				mask |= hpd_ibx[intel_encoder->hpd_pin];
266782a28bcfSDaniel Vetter 	} else {
2668995e6b3dSEgbert Eich 		mask &= ~SDE_HOTPLUG_MASK_CPT;
266982a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2670cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
267182a28bcfSDaniel Vetter 				mask |= hpd_cpt[intel_encoder->hpd_pin];
267282a28bcfSDaniel Vetter 	}
267382a28bcfSDaniel Vetter 
267482a28bcfSDaniel Vetter 	I915_WRITE(SDEIMR, ~mask);
267582a28bcfSDaniel Vetter 
26767fe0b973SKeith Packard 	/*
26777fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
26787fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
26797fe0b973SKeith Packard 	 *
26807fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
26817fe0b973SKeith Packard 	 */
26827fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
26837fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
26847fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
26857fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
26867fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
26877fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
26887fe0b973SKeith Packard }
26897fe0b973SKeith Packard 
2690d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2691d46da437SPaulo Zanoni {
2692d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
269382a28bcfSDaniel Vetter 	u32 mask;
2694d46da437SPaulo Zanoni 
2695692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2696692a04cfSDaniel Vetter 		return;
2697692a04cfSDaniel Vetter 
26988664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
26998664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2700de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
27018664281bSPaulo Zanoni 	} else {
27028664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
27038664281bSPaulo Zanoni 
27048664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
27058664281bSPaulo Zanoni 	}
2706ab5c608bSBen Widawsky 
2707d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2708d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2709d46da437SPaulo Zanoni }
2710d46da437SPaulo Zanoni 
2711f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2712036a4a7dSZhenyu Wang {
27134bc9d430SDaniel Vetter 	unsigned long irqflags;
27144bc9d430SDaniel Vetter 
2715036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2716036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
2717013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2718ce99c256SDaniel Vetter 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
27198664281bSPaulo Zanoni 			   DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
2720de032bf4SPaulo Zanoni 			   DE_PIPEA_FIFO_UNDERRUN | DE_POISON;
2721cc609d5dSBen Widawsky 	u32 gt_irqs;
2722036a4a7dSZhenyu Wang 
27231ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2724036a4a7dSZhenyu Wang 
2725036a4a7dSZhenyu Wang 	/* should always can generate irq */
2726036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
27271ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2728*6005ce42SDaniel Vetter 	I915_WRITE(DEIER, display_mask |
2729*6005ce42SDaniel Vetter 			  DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT);
27303143a2bfSChris Wilson 	POSTING_READ(DEIER);
2731036a4a7dSZhenyu Wang 
27321ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
2733036a4a7dSZhenyu Wang 
2734036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
27351ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2736881f47b6SXiang, Haihao 
2737cc609d5dSBen Widawsky 	gt_irqs = GT_RENDER_USER_INTERRUPT;
2738cc609d5dSBen Widawsky 
27391ec14ad3SChris Wilson 	if (IS_GEN6(dev))
2740cc609d5dSBen Widawsky 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
27411ec14ad3SChris Wilson 	else
2742cc609d5dSBen Widawsky 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
2743cc609d5dSBen Widawsky 			   ILK_BSD_USER_INTERRUPT;
2744cc609d5dSBen Widawsky 
2745cc609d5dSBen Widawsky 	I915_WRITE(GTIER, gt_irqs);
27463143a2bfSChris Wilson 	POSTING_READ(GTIER);
2747036a4a7dSZhenyu Wang 
2748d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
27497fe0b973SKeith Packard 
2750f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
2751*6005ce42SDaniel Vetter 		/* Enable PCU event interrupts
2752*6005ce42SDaniel Vetter 		 *
2753*6005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
27544bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
27554bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
27564bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2757f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
27584bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2759f97108d1SJesse Barnes 	}
2760f97108d1SJesse Barnes 
2761036a4a7dSZhenyu Wang 	return 0;
2762036a4a7dSZhenyu Wang }
2763036a4a7dSZhenyu Wang 
2764f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2765b1f14ad0SJesse Barnes {
2766b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2767b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2768b615b57aSChris Wilson 	u32 display_mask =
2769b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2770b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
2771b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
2772ce99c256SDaniel Vetter 		DE_PLANEA_FLIP_DONE_IVB |
27738664281bSPaulo Zanoni 		DE_AUX_CHANNEL_A_IVB |
27748664281bSPaulo Zanoni 		DE_ERR_INT_IVB;
277512638c57SBen Widawsky 	u32 pm_irqs = GEN6_PM_RPS_EVENTS;
2776cc609d5dSBen Widawsky 	u32 gt_irqs;
2777b1f14ad0SJesse Barnes 
2778b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2779b1f14ad0SJesse Barnes 
2780b1f14ad0SJesse Barnes 	/* should always can generate irq */
27818664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2782b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2783b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2784b615b57aSChris Wilson 	I915_WRITE(DEIER,
2785b615b57aSChris Wilson 		   display_mask |
2786b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
2787b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
2788b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
2789b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2790b1f14ad0SJesse Barnes 
2791cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2792b1f14ad0SJesse Barnes 
2793b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2794b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2795b1f14ad0SJesse Barnes 
2796cc609d5dSBen Widawsky 	gt_irqs = GT_RENDER_USER_INTERRUPT | GT_BSD_USER_INTERRUPT |
2797cc609d5dSBen Widawsky 		  GT_BLT_USER_INTERRUPT | GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2798cc609d5dSBen Widawsky 	I915_WRITE(GTIER, gt_irqs);
2799b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2800b1f14ad0SJesse Barnes 
280112638c57SBen Widawsky 	I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
280212638c57SBen Widawsky 	if (HAS_VEBOX(dev))
280312638c57SBen Widawsky 		pm_irqs |= PM_VEBOX_USER_INTERRUPT |
280412638c57SBen Widawsky 			PM_VEBOX_CS_ERROR_INTERRUPT;
280512638c57SBen Widawsky 
280612638c57SBen Widawsky 	/* Our enable/disable rps functions may touch these registers so
280712638c57SBen Widawsky 	 * make sure to set a known state for only the non-RPS bits.
280812638c57SBen Widawsky 	 * The RMW is extra paranoia since this should be called after being set
280912638c57SBen Widawsky 	 * to a known state in preinstall.
281012638c57SBen Widawsky 	 * */
281112638c57SBen Widawsky 	I915_WRITE(GEN6_PMIMR,
281212638c57SBen Widawsky 		   (I915_READ(GEN6_PMIMR) | ~GEN6_PM_RPS_EVENTS) & ~pm_irqs);
281312638c57SBen Widawsky 	I915_WRITE(GEN6_PMIER,
281412638c57SBen Widawsky 		   (I915_READ(GEN6_PMIER) & GEN6_PM_RPS_EVENTS) | pm_irqs);
281512638c57SBen Widawsky 	POSTING_READ(GEN6_PMIER);
2816eda63ffbSBen Widawsky 
2817d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
28187fe0b973SKeith Packard 
2819b1f14ad0SJesse Barnes 	return 0;
2820b1f14ad0SJesse Barnes }
2821b1f14ad0SJesse Barnes 
28227e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
28237e231dbeSJesse Barnes {
28247e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2825cc609d5dSBen Widawsky 	u32 gt_irqs;
28267e231dbeSJesse Barnes 	u32 enable_mask;
282731acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
28287e231dbeSJesse Barnes 
28297e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
283031acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
283131acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
283231acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
28337e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
28347e231dbeSJesse Barnes 
283531acc7f5SJesse Barnes 	/*
283631acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
283731acc7f5SJesse Barnes 	 * toggle them based on usage.
283831acc7f5SJesse Barnes 	 */
283931acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
284031acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
284131acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
28427e231dbeSJesse Barnes 
284320afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
284420afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
284520afbda2SDaniel Vetter 
28467e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
28477e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
28487e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28497e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
28507e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
28517e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
28527e231dbeSJesse Barnes 
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);
285631acc7f5SJesse Barnes 
28577e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28587e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28597e231dbeSJesse Barnes 
286031acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
286131acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
28623bcedbe5SJesse Barnes 
2863cc609d5dSBen Widawsky 	gt_irqs = GT_RENDER_USER_INTERRUPT | GT_BSD_USER_INTERRUPT |
2864cc609d5dSBen Widawsky 		GT_BLT_USER_INTERRUPT;
2865cc609d5dSBen Widawsky 	I915_WRITE(GTIER, gt_irqs);
28667e231dbeSJesse Barnes 	POSTING_READ(GTIER);
28677e231dbeSJesse Barnes 
28687e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
28697e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
28707e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
28717e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
28727e231dbeSJesse Barnes #endif
28737e231dbeSJesse Barnes 
28747e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
287520afbda2SDaniel Vetter 
287620afbda2SDaniel Vetter 	return 0;
287720afbda2SDaniel Vetter }
287820afbda2SDaniel Vetter 
28797e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
28807e231dbeSJesse Barnes {
28817e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
28827e231dbeSJesse Barnes 	int pipe;
28837e231dbeSJesse Barnes 
28847e231dbeSJesse Barnes 	if (!dev_priv)
28857e231dbeSJesse Barnes 		return;
28867e231dbeSJesse Barnes 
2887ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2888ac4c16c5SEgbert Eich 
28897e231dbeSJesse Barnes 	for_each_pipe(pipe)
28907e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
28917e231dbeSJesse Barnes 
28927e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
28937e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
28947e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
28957e231dbeSJesse Barnes 	for_each_pipe(pipe)
28967e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
28977e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28987e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
28997e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
29007e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
29017e231dbeSJesse Barnes }
29027e231dbeSJesse Barnes 
2903f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2904036a4a7dSZhenyu Wang {
2905036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29064697995bSJesse Barnes 
29074697995bSJesse Barnes 	if (!dev_priv)
29084697995bSJesse Barnes 		return;
29094697995bSJesse Barnes 
2910ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2911ac4c16c5SEgbert Eich 
2912036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2913036a4a7dSZhenyu Wang 
2914036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2915036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2916036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
29178664281bSPaulo Zanoni 	if (IS_GEN7(dev))
29188664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2919036a4a7dSZhenyu Wang 
2920036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2921036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2922036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2923192aac1fSKeith Packard 
2924ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2925ab5c608bSBen Widawsky 		return;
2926ab5c608bSBen Widawsky 
2927192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2928192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2929192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
29308664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
29318664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2932036a4a7dSZhenyu Wang }
2933036a4a7dSZhenyu Wang 
2934c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2935c2798b19SChris Wilson {
2936c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2937c2798b19SChris Wilson 	int pipe;
2938c2798b19SChris Wilson 
2939c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2940c2798b19SChris Wilson 
2941c2798b19SChris Wilson 	for_each_pipe(pipe)
2942c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2943c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2944c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2945c2798b19SChris Wilson 	POSTING_READ16(IER);
2946c2798b19SChris Wilson }
2947c2798b19SChris Wilson 
2948c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2949c2798b19SChris Wilson {
2950c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2951c2798b19SChris Wilson 
2952c2798b19SChris Wilson 	I915_WRITE16(EMR,
2953c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2954c2798b19SChris Wilson 
2955c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2956c2798b19SChris Wilson 	dev_priv->irq_mask =
2957c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2958c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2959c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2960c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2961c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2962c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2963c2798b19SChris Wilson 
2964c2798b19SChris Wilson 	I915_WRITE16(IER,
2965c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2966c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2967c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2968c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2969c2798b19SChris Wilson 	POSTING_READ16(IER);
2970c2798b19SChris Wilson 
2971c2798b19SChris Wilson 	return 0;
2972c2798b19SChris Wilson }
2973c2798b19SChris Wilson 
297490a72f87SVille Syrjälä /*
297590a72f87SVille Syrjälä  * Returns true when a page flip has completed.
297690a72f87SVille Syrjälä  */
297790a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
297890a72f87SVille Syrjälä 			       int pipe, u16 iir)
297990a72f87SVille Syrjälä {
298090a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
298190a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
298290a72f87SVille Syrjälä 
298390a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
298490a72f87SVille Syrjälä 		return false;
298590a72f87SVille Syrjälä 
298690a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
298790a72f87SVille Syrjälä 		return false;
298890a72f87SVille Syrjälä 
298990a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
299090a72f87SVille Syrjälä 
299190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
299290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
299390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
299490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
299590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
299690a72f87SVille Syrjälä 	 */
299790a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
299890a72f87SVille Syrjälä 		return false;
299990a72f87SVille Syrjälä 
300090a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
300190a72f87SVille Syrjälä 
300290a72f87SVille Syrjälä 	return true;
300390a72f87SVille Syrjälä }
300490a72f87SVille Syrjälä 
3005ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3006c2798b19SChris Wilson {
3007c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3008c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3009c2798b19SChris Wilson 	u16 iir, new_iir;
3010c2798b19SChris Wilson 	u32 pipe_stats[2];
3011c2798b19SChris Wilson 	unsigned long irqflags;
3012c2798b19SChris Wilson 	int irq_received;
3013c2798b19SChris Wilson 	int pipe;
3014c2798b19SChris Wilson 	u16 flip_mask =
3015c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3016c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3017c2798b19SChris Wilson 
3018c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3019c2798b19SChris Wilson 
3020c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3021c2798b19SChris Wilson 	if (iir == 0)
3022c2798b19SChris Wilson 		return IRQ_NONE;
3023c2798b19SChris Wilson 
3024c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3025c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3026c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3027c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3028c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3029c2798b19SChris Wilson 		 */
3030c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3031c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3032c2798b19SChris Wilson 			i915_handle_error(dev, false);
3033c2798b19SChris Wilson 
3034c2798b19SChris Wilson 		for_each_pipe(pipe) {
3035c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3036c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3037c2798b19SChris Wilson 
3038c2798b19SChris Wilson 			/*
3039c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3040c2798b19SChris Wilson 			 */
3041c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3042c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3043c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3044c2798b19SChris Wilson 							 pipe_name(pipe));
3045c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3046c2798b19SChris Wilson 				irq_received = 1;
3047c2798b19SChris Wilson 			}
3048c2798b19SChris Wilson 		}
3049c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3050c2798b19SChris Wilson 
3051c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3052c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3053c2798b19SChris Wilson 
3054d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3055c2798b19SChris Wilson 
3056c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3057c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3058c2798b19SChris Wilson 
3059c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
306090a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
306190a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
3062c2798b19SChris Wilson 
3063c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
306490a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
306590a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
3066c2798b19SChris Wilson 
3067c2798b19SChris Wilson 		iir = new_iir;
3068c2798b19SChris Wilson 	}
3069c2798b19SChris Wilson 
3070c2798b19SChris Wilson 	return IRQ_HANDLED;
3071c2798b19SChris Wilson }
3072c2798b19SChris Wilson 
3073c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3074c2798b19SChris Wilson {
3075c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3076c2798b19SChris Wilson 	int pipe;
3077c2798b19SChris Wilson 
3078c2798b19SChris Wilson 	for_each_pipe(pipe) {
3079c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3080c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3081c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3082c2798b19SChris Wilson 	}
3083c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3084c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3085c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3086c2798b19SChris Wilson }
3087c2798b19SChris Wilson 
3088a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3089a266c7d5SChris Wilson {
3090a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3091a266c7d5SChris Wilson 	int pipe;
3092a266c7d5SChris Wilson 
3093a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3094a266c7d5SChris Wilson 
3095a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3096a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3097a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3098a266c7d5SChris Wilson 	}
3099a266c7d5SChris Wilson 
310000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3101a266c7d5SChris Wilson 	for_each_pipe(pipe)
3102a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3103a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3104a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3105a266c7d5SChris Wilson 	POSTING_READ(IER);
3106a266c7d5SChris Wilson }
3107a266c7d5SChris Wilson 
3108a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3109a266c7d5SChris Wilson {
3110a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
311138bde180SChris Wilson 	u32 enable_mask;
3112a266c7d5SChris Wilson 
311338bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
311438bde180SChris Wilson 
311538bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
311638bde180SChris Wilson 	dev_priv->irq_mask =
311738bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
311838bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
311938bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
312038bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
312138bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
312238bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
312338bde180SChris Wilson 
312438bde180SChris Wilson 	enable_mask =
312538bde180SChris Wilson 		I915_ASLE_INTERRUPT |
312638bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
312738bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
312838bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
312938bde180SChris Wilson 		I915_USER_INTERRUPT;
313038bde180SChris Wilson 
3131a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
313220afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
313320afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
313420afbda2SDaniel Vetter 
3135a266c7d5SChris Wilson 		/* Enable in IER... */
3136a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3137a266c7d5SChris Wilson 		/* and unmask in IMR */
3138a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3139a266c7d5SChris Wilson 	}
3140a266c7d5SChris Wilson 
3141a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3142a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3143a266c7d5SChris Wilson 	POSTING_READ(IER);
3144a266c7d5SChris Wilson 
3145f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
314620afbda2SDaniel Vetter 
314720afbda2SDaniel Vetter 	return 0;
314820afbda2SDaniel Vetter }
314920afbda2SDaniel Vetter 
315090a72f87SVille Syrjälä /*
315190a72f87SVille Syrjälä  * Returns true when a page flip has completed.
315290a72f87SVille Syrjälä  */
315390a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
315490a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
315590a72f87SVille Syrjälä {
315690a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
315790a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
315890a72f87SVille Syrjälä 
315990a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
316090a72f87SVille Syrjälä 		return false;
316190a72f87SVille Syrjälä 
316290a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
316390a72f87SVille Syrjälä 		return false;
316490a72f87SVille Syrjälä 
316590a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
316690a72f87SVille Syrjälä 
316790a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
316890a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
316990a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
317090a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
317190a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
317290a72f87SVille Syrjälä 	 */
317390a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
317490a72f87SVille Syrjälä 		return false;
317590a72f87SVille Syrjälä 
317690a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
317790a72f87SVille Syrjälä 
317890a72f87SVille Syrjälä 	return true;
317990a72f87SVille Syrjälä }
318090a72f87SVille Syrjälä 
3181ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3182a266c7d5SChris Wilson {
3183a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3184a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
31858291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3186a266c7d5SChris Wilson 	unsigned long irqflags;
318738bde180SChris Wilson 	u32 flip_mask =
318838bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
318938bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
319038bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3191a266c7d5SChris Wilson 
3192a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3193a266c7d5SChris Wilson 
3194a266c7d5SChris Wilson 	iir = I915_READ(IIR);
319538bde180SChris Wilson 	do {
319638bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
31978291ee90SChris Wilson 		bool blc_event = false;
3198a266c7d5SChris Wilson 
3199a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3200a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3201a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3202a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3203a266c7d5SChris Wilson 		 */
3204a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3205a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3206a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3207a266c7d5SChris Wilson 
3208a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3209a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3210a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3211a266c7d5SChris Wilson 
321238bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3213a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3214a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3215a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3216a266c7d5SChris Wilson 							 pipe_name(pipe));
3217a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
321838bde180SChris Wilson 				irq_received = true;
3219a266c7d5SChris Wilson 			}
3220a266c7d5SChris Wilson 		}
3221a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3222a266c7d5SChris Wilson 
3223a266c7d5SChris Wilson 		if (!irq_received)
3224a266c7d5SChris Wilson 			break;
3225a266c7d5SChris Wilson 
3226a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3227a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3228a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3229a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3230b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3231a266c7d5SChris Wilson 
3232a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3233a266c7d5SChris Wilson 				  hotplug_status);
3234b543fb04SEgbert Eich 			if (hotplug_trigger) {
3235cd569aedSEgbert Eich 				if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_status_i915))
3236cd569aedSEgbert Eich 					i915_hpd_irq_setup(dev);
3237a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
3238a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
3239b543fb04SEgbert Eich 			}
3240a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
324138bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3242a266c7d5SChris Wilson 		}
3243a266c7d5SChris Wilson 
324438bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3245a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3246a266c7d5SChris Wilson 
3247a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3248a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3249a266c7d5SChris Wilson 
3250a266c7d5SChris Wilson 		for_each_pipe(pipe) {
325138bde180SChris Wilson 			int plane = pipe;
325238bde180SChris Wilson 			if (IS_MOBILE(dev))
325338bde180SChris Wilson 				plane = !plane;
32545e2032d4SVille Syrjälä 
325590a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
325690a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
325790a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3258a266c7d5SChris Wilson 
3259a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3260a266c7d5SChris Wilson 				blc_event = true;
3261a266c7d5SChris Wilson 		}
3262a266c7d5SChris Wilson 
3263a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3264a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3265a266c7d5SChris Wilson 
3266a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3267a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3268a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3269a266c7d5SChris Wilson 		 * we would never get another interrupt.
3270a266c7d5SChris Wilson 		 *
3271a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3272a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3273a266c7d5SChris Wilson 		 * another one.
3274a266c7d5SChris Wilson 		 *
3275a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3276a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3277a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3278a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3279a266c7d5SChris Wilson 		 * stray interrupts.
3280a266c7d5SChris Wilson 		 */
328138bde180SChris Wilson 		ret = IRQ_HANDLED;
3282a266c7d5SChris Wilson 		iir = new_iir;
328338bde180SChris Wilson 	} while (iir & ~flip_mask);
3284a266c7d5SChris Wilson 
3285d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
32868291ee90SChris Wilson 
3287a266c7d5SChris Wilson 	return ret;
3288a266c7d5SChris Wilson }
3289a266c7d5SChris Wilson 
3290a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3291a266c7d5SChris Wilson {
3292a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3293a266c7d5SChris Wilson 	int pipe;
3294a266c7d5SChris Wilson 
3295ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3296ac4c16c5SEgbert Eich 
3297a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3298a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3299a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3300a266c7d5SChris Wilson 	}
3301a266c7d5SChris Wilson 
330200d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
330355b39755SChris Wilson 	for_each_pipe(pipe) {
330455b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3305a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
330655b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
330755b39755SChris Wilson 	}
3308a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3309a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3310a266c7d5SChris Wilson 
3311a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3312a266c7d5SChris Wilson }
3313a266c7d5SChris Wilson 
3314a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3315a266c7d5SChris Wilson {
3316a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3317a266c7d5SChris Wilson 	int pipe;
3318a266c7d5SChris Wilson 
3319a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3320a266c7d5SChris Wilson 
3321a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3322a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3323a266c7d5SChris Wilson 
3324a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3325a266c7d5SChris Wilson 	for_each_pipe(pipe)
3326a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3327a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3328a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3329a266c7d5SChris Wilson 	POSTING_READ(IER);
3330a266c7d5SChris Wilson }
3331a266c7d5SChris Wilson 
3332a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3333a266c7d5SChris Wilson {
3334a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3335bbba0a97SChris Wilson 	u32 enable_mask;
3336a266c7d5SChris Wilson 	u32 error_mask;
3337a266c7d5SChris Wilson 
3338a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3339bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3340adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3341bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3342bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3343bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3344bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3345bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3346bbba0a97SChris Wilson 
3347bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
334821ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
334921ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3350bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3351bbba0a97SChris Wilson 
3352bbba0a97SChris Wilson 	if (IS_G4X(dev))
3353bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3354a266c7d5SChris Wilson 
3355515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
3356a266c7d5SChris Wilson 
3357a266c7d5SChris Wilson 	/*
3358a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3359a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3360a266c7d5SChris Wilson 	 */
3361a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3362a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3363a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3364a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3365a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3366a266c7d5SChris Wilson 	} else {
3367a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3368a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3369a266c7d5SChris Wilson 	}
3370a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3371a266c7d5SChris Wilson 
3372a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3373a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3374a266c7d5SChris Wilson 	POSTING_READ(IER);
3375a266c7d5SChris Wilson 
337620afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
337720afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
337820afbda2SDaniel Vetter 
3379f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
338020afbda2SDaniel Vetter 
338120afbda2SDaniel Vetter 	return 0;
338220afbda2SDaniel Vetter }
338320afbda2SDaniel Vetter 
3384bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
338520afbda2SDaniel Vetter {
338620afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3387e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3388cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
338920afbda2SDaniel Vetter 	u32 hotplug_en;
339020afbda2SDaniel Vetter 
3391bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3392bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3393bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3394adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3395e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3396cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3397cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3398cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3399a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3400a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3401a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3402a266c7d5SChris Wilson 		*/
3403a266c7d5SChris Wilson 		if (IS_G4X(dev))
3404a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
340585fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3406a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3407a266c7d5SChris Wilson 
3408a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3409a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3410a266c7d5SChris Wilson 	}
3411bac56d5bSEgbert Eich }
3412a266c7d5SChris Wilson 
3413ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3414a266c7d5SChris Wilson {
3415a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3416a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3417a266c7d5SChris Wilson 	u32 iir, new_iir;
3418a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3419a266c7d5SChris Wilson 	unsigned long irqflags;
3420a266c7d5SChris Wilson 	int irq_received;
3421a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
342221ad8330SVille Syrjälä 	u32 flip_mask =
342321ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
342421ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3425a266c7d5SChris Wilson 
3426a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3427a266c7d5SChris Wilson 
3428a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3429a266c7d5SChris Wilson 
3430a266c7d5SChris Wilson 	for (;;) {
34312c8ba29fSChris Wilson 		bool blc_event = false;
34322c8ba29fSChris Wilson 
343321ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3434a266c7d5SChris Wilson 
3435a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3436a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3437a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3438a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3439a266c7d5SChris Wilson 		 */
3440a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3441a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3442a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3443a266c7d5SChris Wilson 
3444a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3445a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3446a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3447a266c7d5SChris Wilson 
3448a266c7d5SChris Wilson 			/*
3449a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3450a266c7d5SChris Wilson 			 */
3451a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3452a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3453a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3454a266c7d5SChris Wilson 							 pipe_name(pipe));
3455a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3456a266c7d5SChris Wilson 				irq_received = 1;
3457a266c7d5SChris Wilson 			}
3458a266c7d5SChris Wilson 		}
3459a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3460a266c7d5SChris Wilson 
3461a266c7d5SChris Wilson 		if (!irq_received)
3462a266c7d5SChris Wilson 			break;
3463a266c7d5SChris Wilson 
3464a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3465a266c7d5SChris Wilson 
3466a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3467adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3468a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3469b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3470b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
34714f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3472a266c7d5SChris Wilson 
3473a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3474a266c7d5SChris Wilson 				  hotplug_status);
3475b543fb04SEgbert Eich 			if (hotplug_trigger) {
3476cd569aedSEgbert Eich 				if (hotplug_irq_storm_detect(dev, hotplug_trigger,
34774f7fd709SDaniel Vetter 							    IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915))
3478cd569aedSEgbert Eich 					i915_hpd_irq_setup(dev);
3479a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
3480a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
3481b543fb04SEgbert Eich 			}
3482a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3483a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3484a266c7d5SChris Wilson 		}
3485a266c7d5SChris Wilson 
348621ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3487a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3488a266c7d5SChris Wilson 
3489a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3490a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3491a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3492a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3493a266c7d5SChris Wilson 
3494a266c7d5SChris Wilson 		for_each_pipe(pipe) {
34952c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
349690a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
349790a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3498a266c7d5SChris Wilson 
3499a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3500a266c7d5SChris Wilson 				blc_event = true;
3501a266c7d5SChris Wilson 		}
3502a266c7d5SChris Wilson 
3503a266c7d5SChris Wilson 
3504a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3505a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3506a266c7d5SChris Wilson 
3507515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3508515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3509515ac2bbSDaniel Vetter 
3510a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3511a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3512a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3513a266c7d5SChris Wilson 		 * we would never get another interrupt.
3514a266c7d5SChris Wilson 		 *
3515a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3516a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3517a266c7d5SChris Wilson 		 * another one.
3518a266c7d5SChris Wilson 		 *
3519a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3520a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3521a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3522a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3523a266c7d5SChris Wilson 		 * stray interrupts.
3524a266c7d5SChris Wilson 		 */
3525a266c7d5SChris Wilson 		iir = new_iir;
3526a266c7d5SChris Wilson 	}
3527a266c7d5SChris Wilson 
3528d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
35292c8ba29fSChris Wilson 
3530a266c7d5SChris Wilson 	return ret;
3531a266c7d5SChris Wilson }
3532a266c7d5SChris Wilson 
3533a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3534a266c7d5SChris Wilson {
3535a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3536a266c7d5SChris Wilson 	int pipe;
3537a266c7d5SChris Wilson 
3538a266c7d5SChris Wilson 	if (!dev_priv)
3539a266c7d5SChris Wilson 		return;
3540a266c7d5SChris Wilson 
3541ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3542ac4c16c5SEgbert Eich 
3543a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3544a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3545a266c7d5SChris Wilson 
3546a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3547a266c7d5SChris Wilson 	for_each_pipe(pipe)
3548a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3549a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3550a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3551a266c7d5SChris Wilson 
3552a266c7d5SChris Wilson 	for_each_pipe(pipe)
3553a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3554a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3555a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3556a266c7d5SChris Wilson }
3557a266c7d5SChris Wilson 
3558ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3559ac4c16c5SEgbert Eich {
3560ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3561ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3562ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3563ac4c16c5SEgbert Eich 	unsigned long irqflags;
3564ac4c16c5SEgbert Eich 	int i;
3565ac4c16c5SEgbert Eich 
3566ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3567ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3568ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3569ac4c16c5SEgbert Eich 
3570ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3571ac4c16c5SEgbert Eich 			continue;
3572ac4c16c5SEgbert Eich 
3573ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3574ac4c16c5SEgbert Eich 
3575ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3576ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3577ac4c16c5SEgbert Eich 
3578ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3579ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3580ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3581ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3582ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3583ac4c16c5SEgbert Eich 				if (!connector->polled)
3584ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3585ac4c16c5SEgbert Eich 			}
3586ac4c16c5SEgbert Eich 		}
3587ac4c16c5SEgbert Eich 	}
3588ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3589ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3590ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3591ac4c16c5SEgbert Eich }
3592ac4c16c5SEgbert Eich 
3593f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3594f71d4af4SJesse Barnes {
35958b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
35968b2e326dSChris Wilson 
35978b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
359899584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3599c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3600a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
36018b2e326dSChris Wilson 
360299584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
360399584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
360461bac78eSDaniel Vetter 		    (unsigned long) dev);
3605ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3606ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
360761bac78eSDaniel Vetter 
360897a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
36099ee32feaSDaniel Vetter 
3610f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
3611f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
36127d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3613f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3614f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3615f71d4af4SJesse Barnes 	}
3616f71d4af4SJesse Barnes 
3617c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3618f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3619c3613de9SKeith Packard 	else
3620c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
3621f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3622f71d4af4SJesse Barnes 
36237e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
36247e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
36257e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
36267e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
36277e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
36287e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
36297e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3630fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
36314a06e201SDaniel Vetter 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
36327d99163dSBen Widawsky 		/* Share uninstall handlers with ILK/SNB */
3633f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
36347d99163dSBen Widawsky 		dev->driver->irq_preinstall = ivybridge_irq_preinstall;
3635f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
3636f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3637f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
3638f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
363982a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3640f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3641f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3642f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3643f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3644f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3645f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3646f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
364782a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3648f71d4af4SJesse Barnes 	} else {
3649c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3650c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3651c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3652c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3653c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3654a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3655a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3656a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3657a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3658a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
365920afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3660c2798b19SChris Wilson 		} else {
3661a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3662a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3663a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3664a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3665bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3666c2798b19SChris Wilson 		}
3667f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3668f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3669f71d4af4SJesse Barnes 	}
3670f71d4af4SJesse Barnes }
367120afbda2SDaniel Vetter 
367220afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
367320afbda2SDaniel Vetter {
367420afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3675821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3676821450c6SEgbert Eich 	struct drm_connector *connector;
3677821450c6SEgbert Eich 	int i;
367820afbda2SDaniel Vetter 
3679821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3680821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3681821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3682821450c6SEgbert Eich 	}
3683821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3684821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3685821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3686821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3687821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3688821450c6SEgbert Eich 	}
368920afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
369020afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
369120afbda2SDaniel Vetter }
3692