xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision ed5cbb0355779dc5516b2f7b62cedce185b00439)
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_i965[] = {
74e5868a31SEgbert Eich 	 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
75e5868a31SEgbert Eich 	 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I965,
76e5868a31SEgbert Eich 	 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I965,
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 
82e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
83e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
84e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
85e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
86e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
87e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
88e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
89e5868a31SEgbert Eich };
90e5868a31SEgbert Eich 
91cd569aedSEgbert Eich static void ibx_hpd_irq_setup(struct drm_device *dev);
92cd569aedSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev);
93e5868a31SEgbert Eich 
94036a4a7dSZhenyu Wang /* For display hotplug interrupt */
95995b6762SChris Wilson static void
96f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
97036a4a7dSZhenyu Wang {
981ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
991ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
1001ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1013143a2bfSChris Wilson 		POSTING_READ(DEIMR);
102036a4a7dSZhenyu Wang 	}
103036a4a7dSZhenyu Wang }
104036a4a7dSZhenyu Wang 
1050ff9800aSPaulo Zanoni static void
106f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
107036a4a7dSZhenyu Wang {
1081ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1091ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1101ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1113143a2bfSChris Wilson 		POSTING_READ(DEIMR);
112036a4a7dSZhenyu Wang 	}
113036a4a7dSZhenyu Wang }
114036a4a7dSZhenyu Wang 
1158664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
1168664281bSPaulo Zanoni {
1178664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1188664281bSPaulo Zanoni 	struct intel_crtc *crtc;
1198664281bSPaulo Zanoni 	enum pipe pipe;
1208664281bSPaulo Zanoni 
1218664281bSPaulo Zanoni 	for_each_pipe(pipe) {
1228664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
1238664281bSPaulo Zanoni 
1248664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
1258664281bSPaulo Zanoni 			return false;
1268664281bSPaulo Zanoni 	}
1278664281bSPaulo Zanoni 
1288664281bSPaulo Zanoni 	return true;
1298664281bSPaulo Zanoni }
1308664281bSPaulo Zanoni 
1318664281bSPaulo Zanoni static bool cpt_can_enable_serr_int(struct drm_device *dev)
1328664281bSPaulo Zanoni {
1338664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1348664281bSPaulo Zanoni 	enum pipe pipe;
1358664281bSPaulo Zanoni 	struct intel_crtc *crtc;
1368664281bSPaulo Zanoni 
1378664281bSPaulo Zanoni 	for_each_pipe(pipe) {
1388664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
1398664281bSPaulo Zanoni 
1408664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
1418664281bSPaulo Zanoni 			return false;
1428664281bSPaulo Zanoni 	}
1438664281bSPaulo Zanoni 
1448664281bSPaulo Zanoni 	return true;
1458664281bSPaulo Zanoni }
1468664281bSPaulo Zanoni 
1478664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
1488664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
1498664281bSPaulo Zanoni {
1508664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1518664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
1528664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
1538664281bSPaulo Zanoni 
1548664281bSPaulo Zanoni 	if (enable)
1558664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
1568664281bSPaulo Zanoni 	else
1578664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
1588664281bSPaulo Zanoni }
1598664281bSPaulo Zanoni 
1608664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
1618664281bSPaulo Zanoni 						  bool enable)
1628664281bSPaulo Zanoni {
1638664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1648664281bSPaulo Zanoni 
1658664281bSPaulo Zanoni 	if (enable) {
1668664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
1678664281bSPaulo Zanoni 			return;
1688664281bSPaulo Zanoni 
1698664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN_A |
1708664281bSPaulo Zanoni 					 ERR_INT_FIFO_UNDERRUN_B |
1718664281bSPaulo Zanoni 					 ERR_INT_FIFO_UNDERRUN_C);
1728664281bSPaulo Zanoni 
1738664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
1748664281bSPaulo Zanoni 	} else {
1758664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
1768664281bSPaulo Zanoni 	}
1778664281bSPaulo Zanoni }
1788664281bSPaulo Zanoni 
1798664281bSPaulo Zanoni static void ibx_set_fifo_underrun_reporting(struct intel_crtc *crtc,
1808664281bSPaulo Zanoni 					    bool enable)
1818664281bSPaulo Zanoni {
1828664281bSPaulo Zanoni 	struct drm_device *dev = crtc->base.dev;
1838664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1848664281bSPaulo Zanoni 	uint32_t bit = (crtc->pipe == PIPE_A) ? SDE_TRANSA_FIFO_UNDER :
1858664281bSPaulo Zanoni 						SDE_TRANSB_FIFO_UNDER;
1868664281bSPaulo Zanoni 
1878664281bSPaulo Zanoni 	if (enable)
1888664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) & ~bit);
1898664281bSPaulo Zanoni 	else
1908664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) | bit);
1918664281bSPaulo Zanoni 
1928664281bSPaulo Zanoni 	POSTING_READ(SDEIMR);
1938664281bSPaulo Zanoni }
1948664281bSPaulo Zanoni 
1958664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
1968664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
1978664281bSPaulo Zanoni 					    bool enable)
1988664281bSPaulo Zanoni {
1998664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2008664281bSPaulo Zanoni 
2018664281bSPaulo Zanoni 	if (enable) {
2028664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
2038664281bSPaulo Zanoni 			return;
2048664281bSPaulo Zanoni 
2058664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, SERR_INT_TRANS_A_FIFO_UNDERRUN |
2068664281bSPaulo Zanoni 				     SERR_INT_TRANS_B_FIFO_UNDERRUN |
2078664281bSPaulo Zanoni 				     SERR_INT_TRANS_C_FIFO_UNDERRUN);
2088664281bSPaulo Zanoni 
2098664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) & ~SDE_ERROR_CPT);
2108664281bSPaulo Zanoni 	} else {
2118664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) | SDE_ERROR_CPT);
2128664281bSPaulo Zanoni 	}
2138664281bSPaulo Zanoni 
2148664281bSPaulo Zanoni 	POSTING_READ(SDEIMR);
2158664281bSPaulo Zanoni }
2168664281bSPaulo Zanoni 
2178664281bSPaulo Zanoni /**
2188664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
2198664281bSPaulo Zanoni  * @dev: drm device
2208664281bSPaulo Zanoni  * @pipe: pipe
2218664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
2228664281bSPaulo Zanoni  *
2238664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
2248664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
2258664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
2268664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
2278664281bSPaulo Zanoni  * bit for all the pipes.
2288664281bSPaulo Zanoni  *
2298664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
2308664281bSPaulo Zanoni  */
2318664281bSPaulo Zanoni bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
2328664281bSPaulo Zanoni 					   enum pipe pipe, bool enable)
2338664281bSPaulo Zanoni {
2348664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2358664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
2368664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2378664281bSPaulo Zanoni 	unsigned long flags;
2388664281bSPaulo Zanoni 	bool ret;
2398664281bSPaulo Zanoni 
2408664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
2418664281bSPaulo Zanoni 
2428664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
2438664281bSPaulo Zanoni 
2448664281bSPaulo Zanoni 	if (enable == ret)
2458664281bSPaulo Zanoni 		goto done;
2468664281bSPaulo Zanoni 
2478664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
2488664281bSPaulo Zanoni 
2498664281bSPaulo Zanoni 	if (IS_GEN5(dev) || IS_GEN6(dev))
2508664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
2518664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
2528664281bSPaulo Zanoni 		ivybridge_set_fifo_underrun_reporting(dev, enable);
2538664281bSPaulo Zanoni 
2548664281bSPaulo Zanoni done:
2558664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
2568664281bSPaulo Zanoni 	return ret;
2578664281bSPaulo Zanoni }
2588664281bSPaulo Zanoni 
2598664281bSPaulo Zanoni /**
2608664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
2618664281bSPaulo Zanoni  * @dev: drm device
2628664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
2638664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
2648664281bSPaulo Zanoni  *
2658664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
2668664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
2678664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
2688664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
2698664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
2708664281bSPaulo Zanoni  *
2718664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
2728664281bSPaulo Zanoni  */
2738664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
2748664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
2758664281bSPaulo Zanoni 					   bool enable)
2768664281bSPaulo Zanoni {
2778664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2788664281bSPaulo Zanoni 	enum pipe p;
2798664281bSPaulo Zanoni 	struct drm_crtc *crtc;
2808664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc;
2818664281bSPaulo Zanoni 	unsigned long flags;
2828664281bSPaulo Zanoni 	bool ret;
2838664281bSPaulo Zanoni 
2848664281bSPaulo Zanoni 	if (HAS_PCH_LPT(dev)) {
2858664281bSPaulo Zanoni 		crtc = NULL;
2868664281bSPaulo Zanoni 		for_each_pipe(p) {
2878664281bSPaulo Zanoni 			struct drm_crtc *c = dev_priv->pipe_to_crtc_mapping[p];
2888664281bSPaulo Zanoni 			if (intel_pipe_has_type(c, INTEL_OUTPUT_ANALOG)) {
2898664281bSPaulo Zanoni 				crtc = c;
2908664281bSPaulo Zanoni 				break;
2918664281bSPaulo Zanoni 			}
2928664281bSPaulo Zanoni 		}
2938664281bSPaulo Zanoni 		if (!crtc) {
2948664281bSPaulo Zanoni 			DRM_ERROR("PCH FIFO underrun, but no CRTC using the PCH found\n");
2958664281bSPaulo Zanoni 			return false;
2968664281bSPaulo Zanoni 		}
2978664281bSPaulo Zanoni 	} else {
2988664281bSPaulo Zanoni 		crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
2998664281bSPaulo Zanoni 	}
3008664281bSPaulo Zanoni 	intel_crtc = to_intel_crtc(crtc);
3018664281bSPaulo Zanoni 
3028664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
3038664281bSPaulo Zanoni 
3048664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
3058664281bSPaulo Zanoni 
3068664281bSPaulo Zanoni 	if (enable == ret)
3078664281bSPaulo Zanoni 		goto done;
3088664281bSPaulo Zanoni 
3098664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
3108664281bSPaulo Zanoni 
3118664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
3128664281bSPaulo Zanoni 		ibx_set_fifo_underrun_reporting(intel_crtc, enable);
3138664281bSPaulo Zanoni 	else
3148664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
3158664281bSPaulo Zanoni 
3168664281bSPaulo Zanoni done:
3178664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
3188664281bSPaulo Zanoni 	return ret;
3198664281bSPaulo Zanoni }
3208664281bSPaulo Zanoni 
3218664281bSPaulo Zanoni 
3227c463586SKeith Packard void
3237c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3247c463586SKeith Packard {
3259db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
32646c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3277c463586SKeith Packard 
32846c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
32946c06a30SVille Syrjälä 		return;
33046c06a30SVille Syrjälä 
3317c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
33246c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
33346c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3343143a2bfSChris Wilson 	POSTING_READ(reg);
3357c463586SKeith Packard }
3367c463586SKeith Packard 
3377c463586SKeith Packard void
3387c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3397c463586SKeith Packard {
3409db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
34146c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3427c463586SKeith Packard 
34346c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
34446c06a30SVille Syrjälä 		return;
34546c06a30SVille Syrjälä 
34646c06a30SVille Syrjälä 	pipestat &= ~mask;
34746c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3483143a2bfSChris Wilson 	POSTING_READ(reg);
3497c463586SKeith Packard }
3507c463586SKeith Packard 
351c0e09200SDave Airlie /**
352f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
35301c66889SZhao Yakui  */
354f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
35501c66889SZhao Yakui {
3561ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
3571ec14ad3SChris Wilson 	unsigned long irqflags;
3581ec14ad3SChris Wilson 
359f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
360f49e38ddSJani Nikula 		return;
361f49e38ddSJani Nikula 
3621ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
36301c66889SZhao Yakui 
364f898780bSJani Nikula 	i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
365a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
366f898780bSJani Nikula 		i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
3671ec14ad3SChris Wilson 
3681ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
36901c66889SZhao Yakui }
37001c66889SZhao Yakui 
37101c66889SZhao Yakui /**
3720a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
3730a3e67a4SJesse Barnes  * @dev: DRM device
3740a3e67a4SJesse Barnes  * @pipe: pipe to check
3750a3e67a4SJesse Barnes  *
3760a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
3770a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
3780a3e67a4SJesse Barnes  * before reading such registers if unsure.
3790a3e67a4SJesse Barnes  */
3800a3e67a4SJesse Barnes static int
3810a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
3820a3e67a4SJesse Barnes {
3830a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
384702e7a56SPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
385702e7a56SPaulo Zanoni 								      pipe);
386702e7a56SPaulo Zanoni 
38771f8ba6bSPaulo Zanoni 	if (!intel_display_power_enabled(dev,
38871f8ba6bSPaulo Zanoni 		POWER_DOMAIN_TRANSCODER(cpu_transcoder)))
38971f8ba6bSPaulo Zanoni 		return false;
39071f8ba6bSPaulo Zanoni 
391702e7a56SPaulo Zanoni 	return I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_ENABLE;
3920a3e67a4SJesse Barnes }
3930a3e67a4SJesse Barnes 
39442f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
39542f52ef8SKeith Packard  * we use as a pipe index
39642f52ef8SKeith Packard  */
397f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
3980a3e67a4SJesse Barnes {
3990a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4000a3e67a4SJesse Barnes 	unsigned long high_frame;
4010a3e67a4SJesse Barnes 	unsigned long low_frame;
4025eddb70bSChris Wilson 	u32 high1, high2, low;
4030a3e67a4SJesse Barnes 
4040a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
40544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4069db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
4070a3e67a4SJesse Barnes 		return 0;
4080a3e67a4SJesse Barnes 	}
4090a3e67a4SJesse Barnes 
4109db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
4119db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
4125eddb70bSChris Wilson 
4130a3e67a4SJesse Barnes 	/*
4140a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
4150a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
4160a3e67a4SJesse Barnes 	 * register.
4170a3e67a4SJesse Barnes 	 */
4180a3e67a4SJesse Barnes 	do {
4195eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4205eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
4215eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4220a3e67a4SJesse Barnes 	} while (high1 != high2);
4230a3e67a4SJesse Barnes 
4245eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
4255eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
4265eddb70bSChris Wilson 	return (high1 << 8) | low;
4270a3e67a4SJesse Barnes }
4280a3e67a4SJesse Barnes 
429f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
4309880b7a5SJesse Barnes {
4319880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4329db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
4339880b7a5SJesse Barnes 
4349880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
43544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4369db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4379880b7a5SJesse Barnes 		return 0;
4389880b7a5SJesse Barnes 	}
4399880b7a5SJesse Barnes 
4409880b7a5SJesse Barnes 	return I915_READ(reg);
4419880b7a5SJesse Barnes }
4429880b7a5SJesse Barnes 
443f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
4440af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
4450af7e4dfSMario Kleiner {
4460af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4470af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
4480af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
4490af7e4dfSMario Kleiner 	bool in_vbl = true;
4500af7e4dfSMario Kleiner 	int ret = 0;
451fe2b8f9dSPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
452fe2b8f9dSPaulo Zanoni 								      pipe);
4530af7e4dfSMario Kleiner 
4540af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
4550af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
4569db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4570af7e4dfSMario Kleiner 		return 0;
4580af7e4dfSMario Kleiner 	}
4590af7e4dfSMario Kleiner 
4600af7e4dfSMario Kleiner 	/* Get vtotal. */
461fe2b8f9dSPaulo Zanoni 	vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4620af7e4dfSMario Kleiner 
4630af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
4640af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
4650af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
4660af7e4dfSMario Kleiner 		 */
4670af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
4680af7e4dfSMario Kleiner 
4690af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
4700af7e4dfSMario Kleiner 		 * horizontal scanout position.
4710af7e4dfSMario Kleiner 		 */
4720af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
4730af7e4dfSMario Kleiner 		*hpos = 0;
4740af7e4dfSMario Kleiner 	} else {
4750af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
4760af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
4770af7e4dfSMario Kleiner 		 * scanout position.
4780af7e4dfSMario Kleiner 		 */
4790af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
4800af7e4dfSMario Kleiner 
481fe2b8f9dSPaulo Zanoni 		htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4820af7e4dfSMario Kleiner 		*vpos = position / htotal;
4830af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
4840af7e4dfSMario Kleiner 	}
4850af7e4dfSMario Kleiner 
4860af7e4dfSMario Kleiner 	/* Query vblank area. */
487fe2b8f9dSPaulo Zanoni 	vbl = I915_READ(VBLANK(cpu_transcoder));
4880af7e4dfSMario Kleiner 
4890af7e4dfSMario Kleiner 	/* Test position against vblank region. */
4900af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
4910af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
4920af7e4dfSMario Kleiner 
4930af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
4940af7e4dfSMario Kleiner 		in_vbl = false;
4950af7e4dfSMario Kleiner 
4960af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
4970af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
4980af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
4990af7e4dfSMario Kleiner 
5000af7e4dfSMario Kleiner 	/* Readouts valid? */
5010af7e4dfSMario Kleiner 	if (vbl > 0)
5020af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
5030af7e4dfSMario Kleiner 
5040af7e4dfSMario Kleiner 	/* In vblank? */
5050af7e4dfSMario Kleiner 	if (in_vbl)
5060af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
5070af7e4dfSMario Kleiner 
5080af7e4dfSMario Kleiner 	return ret;
5090af7e4dfSMario Kleiner }
5100af7e4dfSMario Kleiner 
511f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
5120af7e4dfSMario Kleiner 			      int *max_error,
5130af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
5140af7e4dfSMario Kleiner 			      unsigned flags)
5150af7e4dfSMario Kleiner {
5164041b853SChris Wilson 	struct drm_crtc *crtc;
5170af7e4dfSMario Kleiner 
5187eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
5194041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5200af7e4dfSMario Kleiner 		return -EINVAL;
5210af7e4dfSMario Kleiner 	}
5220af7e4dfSMario Kleiner 
5230af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
5244041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
5254041b853SChris Wilson 	if (crtc == NULL) {
5264041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5274041b853SChris Wilson 		return -EINVAL;
5284041b853SChris Wilson 	}
5294041b853SChris Wilson 
5304041b853SChris Wilson 	if (!crtc->enabled) {
5314041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
5324041b853SChris Wilson 		return -EBUSY;
5334041b853SChris Wilson 	}
5340af7e4dfSMario Kleiner 
5350af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
5364041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
5374041b853SChris Wilson 						     vblank_time, flags,
5384041b853SChris Wilson 						     crtc);
5390af7e4dfSMario Kleiner }
5400af7e4dfSMario Kleiner 
541321a1b30SEgbert Eich static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
542321a1b30SEgbert Eich {
543321a1b30SEgbert Eich 	enum drm_connector_status old_status;
544321a1b30SEgbert Eich 
545321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
546321a1b30SEgbert Eich 	old_status = connector->status;
547321a1b30SEgbert Eich 
548321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
549321a1b30SEgbert Eich 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
550321a1b30SEgbert Eich 		      connector->base.id,
551321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
552321a1b30SEgbert Eich 		      old_status, connector->status);
553321a1b30SEgbert Eich 	return (old_status != connector->status);
554321a1b30SEgbert Eich }
555321a1b30SEgbert Eich 
5565ca58282SJesse Barnes /*
5575ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
5585ca58282SJesse Barnes  */
559ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
560ac4c16c5SEgbert Eich 
5615ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
5625ca58282SJesse Barnes {
5635ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5645ca58282SJesse Barnes 						    hotplug_work);
5655ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
566c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
567cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
568cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
569cd569aedSEgbert Eich 	struct drm_connector *connector;
570cd569aedSEgbert Eich 	unsigned long irqflags;
571cd569aedSEgbert Eich 	bool hpd_disabled = false;
572321a1b30SEgbert Eich 	bool changed = false;
573142e2398SEgbert Eich 	u32 hpd_event_bits;
5745ca58282SJesse Barnes 
57552d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
57652d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
57752d7ecedSDaniel Vetter 		return;
57852d7ecedSDaniel Vetter 
579a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
580e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
581e67189abSJesse Barnes 
582cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
583142e2398SEgbert Eich 
584142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
585142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
586cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
587cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
588cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
589cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
590cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
591cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
592cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
593cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
594cd569aedSEgbert Eich 				drm_get_connector_name(connector));
595cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
596cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
597cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
598cd569aedSEgbert Eich 			hpd_disabled = true;
599cd569aedSEgbert Eich 		}
600142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
601142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
602142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
603142e2398SEgbert Eich 		}
604cd569aedSEgbert Eich 	}
605cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
606cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
607cd569aedSEgbert Eich 	  * some connectors */
608ac4c16c5SEgbert Eich 	if (hpd_disabled) {
609cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
610ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
611ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
612ac4c16c5SEgbert Eich 	}
613cd569aedSEgbert Eich 
614cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
615cd569aedSEgbert Eich 
616321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
617321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
618321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
619321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
620cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
621cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
622321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
623321a1b30SEgbert Eich 				changed = true;
624321a1b30SEgbert Eich 		}
625321a1b30SEgbert Eich 	}
62640ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
62740ee3381SKeith Packard 
628321a1b30SEgbert Eich 	if (changed)
629321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
6305ca58282SJesse Barnes }
6315ca58282SJesse Barnes 
63273edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
633f97108d1SJesse Barnes {
634f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
635b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
6369270388eSDaniel Vetter 	u8 new_delay;
6379270388eSDaniel Vetter 	unsigned long flags;
6389270388eSDaniel Vetter 
6399270388eSDaniel Vetter 	spin_lock_irqsave(&mchdev_lock, flags);
640f97108d1SJesse Barnes 
64173edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
64273edd18fSDaniel Vetter 
64320e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
6449270388eSDaniel Vetter 
6457648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
646b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
647b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
648f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
649f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
650f97108d1SJesse Barnes 
651f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
652b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
65320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
65420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
65520e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
65620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
657b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
65820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
65920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
66020e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
66120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
662f97108d1SJesse Barnes 	}
663f97108d1SJesse Barnes 
6647648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
66520e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
666f97108d1SJesse Barnes 
6679270388eSDaniel Vetter 	spin_unlock_irqrestore(&mchdev_lock, flags);
6689270388eSDaniel Vetter 
669f97108d1SJesse Barnes 	return;
670f97108d1SJesse Barnes }
671f97108d1SJesse Barnes 
672549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
673549f7365SChris Wilson 			struct intel_ring_buffer *ring)
674549f7365SChris Wilson {
675549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
6769862e600SChris Wilson 
677475553deSChris Wilson 	if (ring->obj == NULL)
678475553deSChris Wilson 		return;
679475553deSChris Wilson 
680b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
6819862e600SChris Wilson 
682549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
6833e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
68499584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
68599584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
686cecc21feSChris Wilson 			  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
6873e0dc6b0SBen Widawsky 	}
688549f7365SChris Wilson }
689549f7365SChris Wilson 
6904912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
6913b8d8d91SJesse Barnes {
6924912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
693c6a828d3SDaniel Vetter 						    rps.work);
6944912d041SBen Widawsky 	u32 pm_iir, pm_imr;
6957b9e0ae6SChris Wilson 	u8 new_delay;
6963b8d8d91SJesse Barnes 
697c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
698c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
699c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
7004912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
701a9e2641dSDaniel Vetter 	I915_WRITE(GEN6_PMIMR, 0);
702c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
7034912d041SBen Widawsky 
7047b9e0ae6SChris Wilson 	if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
7053b8d8d91SJesse Barnes 		return;
7063b8d8d91SJesse Barnes 
7074fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
7087b9e0ae6SChris Wilson 
7097b9e0ae6SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
710c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
7117b9e0ae6SChris Wilson 	else
712c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
7133b8d8d91SJesse Barnes 
71479249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
71579249636SBen Widawsky 	 * interrupt
71679249636SBen Widawsky 	 */
71779249636SBen Widawsky 	if (!(new_delay > dev_priv->rps.max_delay ||
71879249636SBen Widawsky 	      new_delay < dev_priv->rps.min_delay)) {
7190a073b84SJesse Barnes 		if (IS_VALLEYVIEW(dev_priv->dev))
7200a073b84SJesse Barnes 			valleyview_set_rps(dev_priv->dev, new_delay);
7210a073b84SJesse Barnes 		else
7224912d041SBen Widawsky 			gen6_set_rps(dev_priv->dev, new_delay);
72379249636SBen Widawsky 	}
7243b8d8d91SJesse Barnes 
72552ceb908SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev)) {
72652ceb908SJesse Barnes 		/*
72752ceb908SJesse Barnes 		 * On VLV, when we enter RC6 we may not be at the minimum
72852ceb908SJesse Barnes 		 * voltage level, so arm a timer to check.  It should only
72952ceb908SJesse Barnes 		 * fire when there's activity or once after we've entered
73052ceb908SJesse Barnes 		 * RC6, and then won't be re-armed until the next RPS interrupt.
73152ceb908SJesse Barnes 		 */
73252ceb908SJesse Barnes 		mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
73352ceb908SJesse Barnes 				 msecs_to_jiffies(100));
73452ceb908SJesse Barnes 	}
73552ceb908SJesse Barnes 
7364fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
7373b8d8d91SJesse Barnes }
7383b8d8d91SJesse Barnes 
739e3689190SBen Widawsky 
740e3689190SBen Widawsky /**
741e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
742e3689190SBen Widawsky  * occurred.
743e3689190SBen Widawsky  * @work: workqueue struct
744e3689190SBen Widawsky  *
745e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
746e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
747e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
748e3689190SBen Widawsky  */
749e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
750e3689190SBen Widawsky {
751e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
752a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
753e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
754e3689190SBen Widawsky 	char *parity_event[5];
755e3689190SBen Widawsky 	uint32_t misccpctl;
756e3689190SBen Widawsky 	unsigned long flags;
757e3689190SBen Widawsky 
758e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
759e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
760e3689190SBen Widawsky 	 * any time we access those registers.
761e3689190SBen Widawsky 	 */
762e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
763e3689190SBen Widawsky 
764e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
765e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
766e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
767e3689190SBen Widawsky 
768e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
769e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
770e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
771e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
772e3689190SBen Widawsky 
773e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
774e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
775e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
776e3689190SBen Widawsky 
777e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
778e3689190SBen Widawsky 
779e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
780e3689190SBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
781e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
782e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
783e3689190SBen Widawsky 
784e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
785e3689190SBen Widawsky 
786e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
787e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
788e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
789e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
790e3689190SBen Widawsky 	parity_event[4] = NULL;
791e3689190SBen Widawsky 
792e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
793e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
794e3689190SBen Widawsky 
795e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
796e3689190SBen Widawsky 		  row, bank, subbank);
797e3689190SBen Widawsky 
798e3689190SBen Widawsky 	kfree(parity_event[3]);
799e3689190SBen Widawsky 	kfree(parity_event[2]);
800e3689190SBen Widawsky 	kfree(parity_event[1]);
801e3689190SBen Widawsky }
802e3689190SBen Widawsky 
803d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
804e3689190SBen Widawsky {
805e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
806e3689190SBen Widawsky 	unsigned long flags;
807e3689190SBen Widawsky 
808e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
809e3689190SBen Widawsky 		return;
810e3689190SBen Widawsky 
811e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
812e3689190SBen Widawsky 	dev_priv->gt_irq_mask |= GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
813e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
814e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
815e3689190SBen Widawsky 
816a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
817e3689190SBen Widawsky }
818e3689190SBen Widawsky 
819e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
820e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
821e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
822e7b4c6b1SDaniel Vetter {
823e7b4c6b1SDaniel Vetter 
824e7b4c6b1SDaniel Vetter 	if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
825e7b4c6b1SDaniel Vetter 		      GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
826e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
827e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BSD_USER_INTERRUPT)
828e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
829e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
830e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
831e7b4c6b1SDaniel Vetter 
832e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
833e7b4c6b1SDaniel Vetter 		      GT_GEN6_BSD_CS_ERROR_INTERRUPT |
834e7b4c6b1SDaniel Vetter 		      GT_RENDER_CS_ERROR_INTERRUPT)) {
835e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
836e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
837e7b4c6b1SDaniel Vetter 	}
838e3689190SBen Widawsky 
839e3689190SBen Widawsky 	if (gt_iir & GT_GEN7_L3_PARITY_ERROR_INTERRUPT)
840e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
841e7b4c6b1SDaniel Vetter }
842e7b4c6b1SDaniel Vetter 
843fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
844fc6826d1SChris Wilson 				u32 pm_iir)
845fc6826d1SChris Wilson {
846fc6826d1SChris Wilson 	unsigned long flags;
847fc6826d1SChris Wilson 
848fc6826d1SChris Wilson 	/*
849fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
850fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
851fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
852c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
853fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
854fc6826d1SChris Wilson 	 *
855c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
856fc6826d1SChris Wilson 	 */
857fc6826d1SChris Wilson 
858c6a828d3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
859c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
860c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
861fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
862c6a828d3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
863fc6826d1SChris Wilson 
864c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
865fc6826d1SChris Wilson }
866fc6826d1SChris Wilson 
867b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
868b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
869b543fb04SEgbert Eich 
870cd569aedSEgbert Eich static inline bool hotplug_irq_storm_detect(struct drm_device *dev,
871b543fb04SEgbert Eich 					    u32 hotplug_trigger,
872b543fb04SEgbert Eich 					    const u32 *hpd)
873b543fb04SEgbert Eich {
874b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
875b543fb04SEgbert Eich 	unsigned long irqflags;
876b543fb04SEgbert Eich 	int i;
877cd569aedSEgbert Eich 	bool ret = false;
878b543fb04SEgbert Eich 
879b543fb04SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
880b543fb04SEgbert Eich 
881b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
882821450c6SEgbert Eich 
883b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
884b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
885b543fb04SEgbert Eich 			continue;
886b543fb04SEgbert Eich 
887bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
888b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
889b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
890b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
891b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
892b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
893b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
894b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
895142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
896b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
897cd569aedSEgbert Eich 			ret = true;
898b543fb04SEgbert Eich 		} else {
899b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
900b543fb04SEgbert Eich 		}
901b543fb04SEgbert Eich 	}
902b543fb04SEgbert Eich 
903b543fb04SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
904cd569aedSEgbert Eich 
905cd569aedSEgbert Eich 	return ret;
906b543fb04SEgbert Eich }
907b543fb04SEgbert Eich 
908515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
909515ac2bbSDaniel Vetter {
91028c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
91128c70f16SDaniel Vetter 
91228c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
913515ac2bbSDaniel Vetter }
914515ac2bbSDaniel Vetter 
915ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
916ce99c256SDaniel Vetter {
9179ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
9189ee32feaSDaniel Vetter 
9199ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
920ce99c256SDaniel Vetter }
921ce99c256SDaniel Vetter 
922ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
9237e231dbeSJesse Barnes {
9247e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
9257e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
9267e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
9277e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
9287e231dbeSJesse Barnes 	unsigned long irqflags;
9297e231dbeSJesse Barnes 	int pipe;
9307e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
9317e231dbeSJesse Barnes 
9327e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
9337e231dbeSJesse Barnes 
9347e231dbeSJesse Barnes 	while (true) {
9357e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
9367e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
9377e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
9387e231dbeSJesse Barnes 
9397e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
9407e231dbeSJesse Barnes 			goto out;
9417e231dbeSJesse Barnes 
9427e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
9437e231dbeSJesse Barnes 
944e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
9457e231dbeSJesse Barnes 
9467e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
9477e231dbeSJesse Barnes 		for_each_pipe(pipe) {
9487e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
9497e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
9507e231dbeSJesse Barnes 
9517e231dbeSJesse Barnes 			/*
9527e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
9537e231dbeSJesse Barnes 			 */
9547e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
9557e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
9567e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
9577e231dbeSJesse Barnes 							 pipe_name(pipe));
9587e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
9597e231dbeSJesse Barnes 			}
9607e231dbeSJesse Barnes 		}
9617e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
9627e231dbeSJesse Barnes 
96331acc7f5SJesse Barnes 		for_each_pipe(pipe) {
96431acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
96531acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
96631acc7f5SJesse Barnes 
96731acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
96831acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
96931acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
97031acc7f5SJesse Barnes 			}
97131acc7f5SJesse Barnes 		}
97231acc7f5SJesse Barnes 
9737e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
9747e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
9757e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
976b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
9777e231dbeSJesse Barnes 
9787e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
9797e231dbeSJesse Barnes 					 hotplug_status);
980b543fb04SEgbert Eich 			if (hotplug_trigger) {
981cd569aedSEgbert Eich 				if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_status_i915))
982cd569aedSEgbert Eich 					i915_hpd_irq_setup(dev);
9837e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
9847e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
985b543fb04SEgbert Eich 			}
9867e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
9877e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
9887e231dbeSJesse Barnes 		}
9897e231dbeSJesse Barnes 
990515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
991515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
9927e231dbeSJesse Barnes 
993fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
994fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
9957e231dbeSJesse Barnes 
9967e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
9977e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
9987e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
9997e231dbeSJesse Barnes 	}
10007e231dbeSJesse Barnes 
10017e231dbeSJesse Barnes out:
10027e231dbeSJesse Barnes 	return ret;
10037e231dbeSJesse Barnes }
10047e231dbeSJesse Barnes 
100523e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1006776ad806SJesse Barnes {
1007776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10089db4a9c7SJesse Barnes 	int pipe;
1009b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1010776ad806SJesse Barnes 
1011b543fb04SEgbert Eich 	if (hotplug_trigger) {
1012cd569aedSEgbert Eich 		if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_ibx))
1013cd569aedSEgbert Eich 			ibx_hpd_irq_setup(dev);
101476e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
1015b543fb04SEgbert Eich 	}
1016cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1017cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1018776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1019cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1020cfc33bf7SVille Syrjälä 				 port_name(port));
1021cfc33bf7SVille Syrjälä 	}
1022776ad806SJesse Barnes 
1023ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1024ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1025ce99c256SDaniel Vetter 
1026776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1027515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1028776ad806SJesse Barnes 
1029776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1030776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1031776ad806SJesse Barnes 
1032776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1033776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1034776ad806SJesse Barnes 
1035776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1036776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1037776ad806SJesse Barnes 
10389db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
10399db4a9c7SJesse Barnes 		for_each_pipe(pipe)
10409db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
10419db4a9c7SJesse Barnes 					 pipe_name(pipe),
10429db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1043776ad806SJesse Barnes 
1044776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1045776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1046776ad806SJesse Barnes 
1047776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1048776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1049776ad806SJesse Barnes 
1050776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
10518664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
10528664281bSPaulo Zanoni 							  false))
10538664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
10548664281bSPaulo Zanoni 
10558664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
10568664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
10578664281bSPaulo Zanoni 							  false))
10588664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
10598664281bSPaulo Zanoni }
10608664281bSPaulo Zanoni 
10618664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
10628664281bSPaulo Zanoni {
10638664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
10648664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
10658664281bSPaulo Zanoni 
1066de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1067de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1068de032bf4SPaulo Zanoni 
10698664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_A)
10708664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
10718664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
10728664281bSPaulo Zanoni 
10738664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_B)
10748664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
10758664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
10768664281bSPaulo Zanoni 
10778664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_C)
10788664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
10798664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
10808664281bSPaulo Zanoni 
10818664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
10828664281bSPaulo Zanoni }
10838664281bSPaulo Zanoni 
10848664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
10858664281bSPaulo Zanoni {
10868664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
10878664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
10888664281bSPaulo Zanoni 
1089de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1090de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1091de032bf4SPaulo Zanoni 
10928664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
10938664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
10948664281bSPaulo Zanoni 							  false))
10958664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
10968664281bSPaulo Zanoni 
10978664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
10988664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
10998664281bSPaulo Zanoni 							  false))
11008664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
11018664281bSPaulo Zanoni 
11028664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
11038664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
11048664281bSPaulo Zanoni 							  false))
11058664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
11068664281bSPaulo Zanoni 
11078664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1108776ad806SJesse Barnes }
1109776ad806SJesse Barnes 
111023e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
111123e81d69SAdam Jackson {
111223e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
111323e81d69SAdam Jackson 	int pipe;
1114b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
111523e81d69SAdam Jackson 
1116b543fb04SEgbert Eich 	if (hotplug_trigger) {
1117cd569aedSEgbert Eich 		if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_cpt))
1118cd569aedSEgbert Eich 			ibx_hpd_irq_setup(dev);
111976e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
1120b543fb04SEgbert Eich 	}
1121cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1122cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
112323e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1124cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1125cfc33bf7SVille Syrjälä 				 port_name(port));
1126cfc33bf7SVille Syrjälä 	}
112723e81d69SAdam Jackson 
112823e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1129ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
113023e81d69SAdam Jackson 
113123e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1132515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
113323e81d69SAdam Jackson 
113423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
113523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
113623e81d69SAdam Jackson 
113723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
113823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
113923e81d69SAdam Jackson 
114023e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
114123e81d69SAdam Jackson 		for_each_pipe(pipe)
114223e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
114323e81d69SAdam Jackson 					 pipe_name(pipe),
114423e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
11458664281bSPaulo Zanoni 
11468664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
11478664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
114823e81d69SAdam Jackson }
114923e81d69SAdam Jackson 
1150ff1f525eSDaniel Vetter static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
1151b1f14ad0SJesse Barnes {
1152b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1153b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1154ab5c608bSBen Widawsky 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
11550e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
11560e43406bSChris Wilson 	int i;
1157b1f14ad0SJesse Barnes 
1158b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1159b1f14ad0SJesse Barnes 
11608664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
11618664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
11628664281bSPaulo Zanoni 	if (IS_HASWELL(dev) &&
11638664281bSPaulo Zanoni 	    (I915_READ_NOTRACE(FPGA_DBG) & FPGA_DBG_RM_NOCLAIM)) {
11648664281bSPaulo Zanoni 		DRM_ERROR("Unclaimed register before interrupt\n");
11658664281bSPaulo Zanoni 		I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
11668664281bSPaulo Zanoni 	}
11678664281bSPaulo Zanoni 
1168b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1169b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1170b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
11710e43406bSChris Wilson 
117244498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
117344498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
117444498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
117544498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
117644498aeaSPaulo Zanoni 	 * due to its back queue). */
1177ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
117844498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
117944498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
118044498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1181ab5c608bSBen Widawsky 	}
118244498aeaSPaulo Zanoni 
11838664281bSPaulo Zanoni 	/* On Haswell, also mask ERR_INT because we don't want to risk
11848664281bSPaulo Zanoni 	 * generating "unclaimed register" interrupts from inside the interrupt
11858664281bSPaulo Zanoni 	 * handler. */
11868664281bSPaulo Zanoni 	if (IS_HASWELL(dev))
11878664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
11888664281bSPaulo Zanoni 
11890e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
11900e43406bSChris Wilson 	if (gt_iir) {
11910e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
11920e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
11930e43406bSChris Wilson 		ret = IRQ_HANDLED;
11940e43406bSChris Wilson 	}
1195b1f14ad0SJesse Barnes 
1196b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
11970e43406bSChris Wilson 	if (de_iir) {
11988664281bSPaulo Zanoni 		if (de_iir & DE_ERR_INT_IVB)
11998664281bSPaulo Zanoni 			ivb_err_int_handler(dev);
12008664281bSPaulo Zanoni 
1201ce99c256SDaniel Vetter 		if (de_iir & DE_AUX_CHANNEL_A_IVB)
1202ce99c256SDaniel Vetter 			dp_aux_irq_handler(dev);
1203ce99c256SDaniel Vetter 
1204b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
120581a07809SJani Nikula 			intel_opregion_asle_intr(dev);
1206b1f14ad0SJesse Barnes 
12070e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
120874d44445SDaniel Vetter 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
120974d44445SDaniel Vetter 				drm_handle_vblank(dev, i);
12100e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
12110e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
12120e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
1213b1f14ad0SJesse Barnes 			}
1214b1f14ad0SJesse Barnes 		}
1215b1f14ad0SJesse Barnes 
1216b1f14ad0SJesse Barnes 		/* check event from PCH */
1217ab5c608bSBen Widawsky 		if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
12180e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
12190e43406bSChris Wilson 
122023e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
12210e43406bSChris Wilson 
12220e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
12230e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
1224b1f14ad0SJesse Barnes 		}
1225b1f14ad0SJesse Barnes 
12260e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
12270e43406bSChris Wilson 		ret = IRQ_HANDLED;
12280e43406bSChris Wilson 	}
12290e43406bSChris Wilson 
12300e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
12310e43406bSChris Wilson 	if (pm_iir) {
1232fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
1233fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
1234b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
12350e43406bSChris Wilson 		ret = IRQ_HANDLED;
12360e43406bSChris Wilson 	}
1237b1f14ad0SJesse Barnes 
12388664281bSPaulo Zanoni 	if (IS_HASWELL(dev) && ivb_can_enable_err_int(dev))
12398664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
12408664281bSPaulo Zanoni 
1241b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1242b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1243ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
124444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
124544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1246ab5c608bSBen Widawsky 	}
1247b1f14ad0SJesse Barnes 
1248b1f14ad0SJesse Barnes 	return ret;
1249b1f14ad0SJesse Barnes }
1250b1f14ad0SJesse Barnes 
1251e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
1252e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1253e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1254e7b4c6b1SDaniel Vetter {
1255e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
1256e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1257e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1258e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1259e7b4c6b1SDaniel Vetter }
1260e7b4c6b1SDaniel Vetter 
1261ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1262036a4a7dSZhenyu Wang {
12634697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1264036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1265036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
126644498aeaSPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
1267881f47b6SXiang, Haihao 
12684697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
12694697995bSJesse Barnes 
12702d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
12712d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
12722d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
12733143a2bfSChris Wilson 	POSTING_READ(DEIER);
12742d109a84SZou, Nanhai 
127544498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
127644498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
127744498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
127844498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
127944498aeaSPaulo Zanoni 	 * due to its back queue). */
128044498aeaSPaulo Zanoni 	sde_ier = I915_READ(SDEIER);
128144498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, 0);
128244498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
128344498aeaSPaulo Zanoni 
1284036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
1285036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
12863b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
1287036a4a7dSZhenyu Wang 
1288acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
1289c7c85101SZou Nan hai 		goto done;
1290036a4a7dSZhenyu Wang 
1291036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
1292036a4a7dSZhenyu Wang 
1293e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
1294e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
1295e7b4c6b1SDaniel Vetter 	else
1296e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1297036a4a7dSZhenyu Wang 
1298ce99c256SDaniel Vetter 	if (de_iir & DE_AUX_CHANNEL_A)
1299ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1300ce99c256SDaniel Vetter 
130101c66889SZhao Yakui 	if (de_iir & DE_GSE)
130281a07809SJani Nikula 		intel_opregion_asle_intr(dev);
130301c66889SZhao Yakui 
130474d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
130574d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
130674d44445SDaniel Vetter 
130774d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
130874d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
130974d44445SDaniel Vetter 
1310de032bf4SPaulo Zanoni 	if (de_iir & DE_POISON)
1311de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1312de032bf4SPaulo Zanoni 
13138664281bSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
13148664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
13158664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
13168664281bSPaulo Zanoni 
13178664281bSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
13188664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
13198664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
13208664281bSPaulo Zanoni 
1321f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1322013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
13232bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
1324013d5aa2SJesse Barnes 	}
1325013d5aa2SJesse Barnes 
1326f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1327f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
13282bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
1329013d5aa2SJesse Barnes 	}
1330c062df61SLi Peng 
1331c650156aSZhenyu Wang 	/* check event from PCH */
1332776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
1333acd15b6cSDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
1334acd15b6cSDaniel Vetter 
133523e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
133623e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
133723e81d69SAdam Jackson 		else
133823e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
1339acd15b6cSDaniel Vetter 
1340acd15b6cSDaniel Vetter 		/* should clear PCH hotplug event before clear CPU irq */
1341acd15b6cSDaniel Vetter 		I915_WRITE(SDEIIR, pch_iir);
1342776ad806SJesse Barnes 	}
1343c650156aSZhenyu Wang 
134473edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
134573edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
1346f97108d1SJesse Barnes 
1347fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
1348fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
13493b8d8d91SJesse Barnes 
1350c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
1351c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
13524912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
1353036a4a7dSZhenyu Wang 
1354c7c85101SZou Nan hai done:
13552d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
13563143a2bfSChris Wilson 	POSTING_READ(DEIER);
135744498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, sde_ier);
135844498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
13592d109a84SZou, Nanhai 
1360036a4a7dSZhenyu Wang 	return ret;
1361036a4a7dSZhenyu Wang }
1362036a4a7dSZhenyu Wang 
13638a905236SJesse Barnes /**
13648a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
13658a905236SJesse Barnes  * @work: work struct
13668a905236SJesse Barnes  *
13678a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
13688a905236SJesse Barnes  * was detected.
13698a905236SJesse Barnes  */
13708a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
13718a905236SJesse Barnes {
13721f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
13731f83fee0SDaniel Vetter 						    work);
13741f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
13751f83fee0SDaniel Vetter 						    gpu_error);
13768a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1377f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
1378f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
1379f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
1380f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
1381f69061beSDaniel Vetter 	int i, ret;
13828a905236SJesse Barnes 
1383f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
13848a905236SJesse Barnes 
13857db0ba24SDaniel Vetter 	/*
13867db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
13877db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
13887db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
13897db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
13907db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
13917db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
13927db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
13937db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
13947db0ba24SDaniel Vetter 	 */
13957db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
139644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
13977db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
13987db0ba24SDaniel Vetter 				   reset_event);
13991f83fee0SDaniel Vetter 
1400f69061beSDaniel Vetter 		ret = i915_reset(dev);
1401f69061beSDaniel Vetter 
1402f69061beSDaniel Vetter 		if (ret == 0) {
1403f69061beSDaniel Vetter 			/*
1404f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1405f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1406f69061beSDaniel Vetter 			 * complete.
1407f69061beSDaniel Vetter 			 *
1408f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1409f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1410f69061beSDaniel Vetter 			 * updates before
1411f69061beSDaniel Vetter 			 * the counter increment.
1412f69061beSDaniel Vetter 			 */
1413f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1414f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1415f69061beSDaniel Vetter 
1416f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1417f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
14181f83fee0SDaniel Vetter 		} else {
14191f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1420f316a42cSBen Gamari 		}
14211f83fee0SDaniel Vetter 
1422f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
1423f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
1424f69061beSDaniel Vetter 
142596a02917SVille Syrjälä 		intel_display_handle_reset(dev);
142696a02917SVille Syrjälä 
14271f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
1428f316a42cSBen Gamari 	}
14298a905236SJesse Barnes }
14308a905236SJesse Barnes 
143185f9e50dSDaniel Vetter /* NB: please notice the memset */
143285f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
143385f9e50dSDaniel Vetter 				    uint32_t *instdone)
143485f9e50dSDaniel Vetter {
143585f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
143685f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
143785f9e50dSDaniel Vetter 
143885f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
143985f9e50dSDaniel Vetter 	case 2:
144085f9e50dSDaniel Vetter 	case 3:
144185f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
144285f9e50dSDaniel Vetter 		break;
144385f9e50dSDaniel Vetter 	case 4:
144485f9e50dSDaniel Vetter 	case 5:
144585f9e50dSDaniel Vetter 	case 6:
144685f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
144785f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
144885f9e50dSDaniel Vetter 		break;
144985f9e50dSDaniel Vetter 	default:
145085f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
145185f9e50dSDaniel Vetter 	case 7:
145285f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
145385f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
145485f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
145585f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
145685f9e50dSDaniel Vetter 		break;
145785f9e50dSDaniel Vetter 	}
145885f9e50dSDaniel Vetter }
145985f9e50dSDaniel Vetter 
14603bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
14619df30794SChris Wilson static struct drm_i915_error_object *
1462d0d045e8SBen Widawsky i915_error_object_create_sized(struct drm_i915_private *dev_priv,
1463d0d045e8SBen Widawsky 			       struct drm_i915_gem_object *src,
1464d0d045e8SBen Widawsky 			       const int num_pages)
14659df30794SChris Wilson {
14669df30794SChris Wilson 	struct drm_i915_error_object *dst;
1467d0d045e8SBen Widawsky 	int i;
1468e56660ddSChris Wilson 	u32 reloc_offset;
14699df30794SChris Wilson 
147005394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
14719df30794SChris Wilson 		return NULL;
14729df30794SChris Wilson 
1473d0d045e8SBen Widawsky 	dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
14749df30794SChris Wilson 	if (dst == NULL)
14759df30794SChris Wilson 		return NULL;
14769df30794SChris Wilson 
147705394f39SChris Wilson 	reloc_offset = src->gtt_offset;
1478d0d045e8SBen Widawsky 	for (i = 0; i < num_pages; i++) {
1479788885aeSAndrew Morton 		unsigned long flags;
1480e56660ddSChris Wilson 		void *d;
1481788885aeSAndrew Morton 
1482e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
14839df30794SChris Wilson 		if (d == NULL)
14849df30794SChris Wilson 			goto unwind;
1485e56660ddSChris Wilson 
1486788885aeSAndrew Morton 		local_irq_save(flags);
14875d4545aeSBen Widawsky 		if (reloc_offset < dev_priv->gtt.mappable_end &&
148874898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
1489172975aaSChris Wilson 			void __iomem *s;
1490172975aaSChris Wilson 
1491172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
1492172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
1493172975aaSChris Wilson 			 * captures what the GPU read.
1494172975aaSChris Wilson 			 */
1495172975aaSChris Wilson 
14965d4545aeSBen Widawsky 			s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
14973e4d3af5SPeter Zijlstra 						     reloc_offset);
1498e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
14993e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
1500960e3564SChris Wilson 		} else if (src->stolen) {
1501960e3564SChris Wilson 			unsigned long offset;
1502960e3564SChris Wilson 
1503960e3564SChris Wilson 			offset = dev_priv->mm.stolen_base;
1504960e3564SChris Wilson 			offset += src->stolen->start;
1505960e3564SChris Wilson 			offset += i << PAGE_SHIFT;
1506960e3564SChris Wilson 
15071a240d4dSDaniel Vetter 			memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
1508172975aaSChris Wilson 		} else {
15099da3da66SChris Wilson 			struct page *page;
1510172975aaSChris Wilson 			void *s;
1511172975aaSChris Wilson 
15129da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
1513172975aaSChris Wilson 
15149da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
15159da3da66SChris Wilson 
15169da3da66SChris Wilson 			s = kmap_atomic(page);
1517172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
1518172975aaSChris Wilson 			kunmap_atomic(s);
1519172975aaSChris Wilson 
15209da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
1521172975aaSChris Wilson 		}
1522788885aeSAndrew Morton 		local_irq_restore(flags);
1523e56660ddSChris Wilson 
15249da3da66SChris Wilson 		dst->pages[i] = d;
1525e56660ddSChris Wilson 
1526e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
15279df30794SChris Wilson 	}
1528d0d045e8SBen Widawsky 	dst->page_count = num_pages;
152905394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
15309df30794SChris Wilson 
15319df30794SChris Wilson 	return dst;
15329df30794SChris Wilson 
15339df30794SChris Wilson unwind:
15349da3da66SChris Wilson 	while (i--)
15359da3da66SChris Wilson 		kfree(dst->pages[i]);
15369df30794SChris Wilson 	kfree(dst);
15379df30794SChris Wilson 	return NULL;
15389df30794SChris Wilson }
1539d0d045e8SBen Widawsky #define i915_error_object_create(dev_priv, src) \
1540d0d045e8SBen Widawsky 	i915_error_object_create_sized((dev_priv), (src), \
1541d0d045e8SBen Widawsky 				       (src)->base.size>>PAGE_SHIFT)
15429df30794SChris Wilson 
15439df30794SChris Wilson static void
15449df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
15459df30794SChris Wilson {
15469df30794SChris Wilson 	int page;
15479df30794SChris Wilson 
15489df30794SChris Wilson 	if (obj == NULL)
15499df30794SChris Wilson 		return;
15509df30794SChris Wilson 
15519df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
15529df30794SChris Wilson 		kfree(obj->pages[page]);
15539df30794SChris Wilson 
15549df30794SChris Wilson 	kfree(obj);
15559df30794SChris Wilson }
15569df30794SChris Wilson 
1557742cbee8SDaniel Vetter void
1558742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
15599df30794SChris Wilson {
1560742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
1561742cbee8SDaniel Vetter 							  typeof(*error), ref);
1562e2f973d5SChris Wilson 	int i;
1563e2f973d5SChris Wilson 
156452d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
156552d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
156652d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
156752d39a21SChris Wilson 		kfree(error->ring[i].requests);
156852d39a21SChris Wilson 	}
1569e2f973d5SChris Wilson 
15709df30794SChris Wilson 	kfree(error->active_bo);
15716ef3d427SChris Wilson 	kfree(error->overlay);
15729df30794SChris Wilson 	kfree(error);
15739df30794SChris Wilson }
15741b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
15751b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
1576c724e8a9SChris Wilson {
1577c724e8a9SChris Wilson 	err->size = obj->base.size;
1578c724e8a9SChris Wilson 	err->name = obj->base.name;
15790201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
15800201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1581c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
1582c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1583c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1584c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1585c724e8a9SChris Wilson 	err->pinned = 0;
1586c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1587c724e8a9SChris Wilson 		err->pinned = 1;
1588c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1589c724e8a9SChris Wilson 		err->pinned = -1;
1590c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1591c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1592c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
159396154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
159493dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
15951b50247aSChris Wilson }
1596c724e8a9SChris Wilson 
15971b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
15981b50247aSChris Wilson 			     int count, struct list_head *head)
15991b50247aSChris Wilson {
16001b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
16011b50247aSChris Wilson 	int i = 0;
16021b50247aSChris Wilson 
16031b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
16041b50247aSChris Wilson 		capture_bo(err++, obj);
1605c724e8a9SChris Wilson 		if (++i == count)
1606c724e8a9SChris Wilson 			break;
16071b50247aSChris Wilson 	}
1608c724e8a9SChris Wilson 
16091b50247aSChris Wilson 	return i;
16101b50247aSChris Wilson }
16111b50247aSChris Wilson 
16121b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
16131b50247aSChris Wilson 			     int count, struct list_head *head)
16141b50247aSChris Wilson {
16151b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
16161b50247aSChris Wilson 	int i = 0;
16171b50247aSChris Wilson 
16181b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
16191b50247aSChris Wilson 		if (obj->pin_count == 0)
16201b50247aSChris Wilson 			continue;
16211b50247aSChris Wilson 
16221b50247aSChris Wilson 		capture_bo(err++, obj);
16231b50247aSChris Wilson 		if (++i == count)
16241b50247aSChris Wilson 			break;
1625c724e8a9SChris Wilson 	}
1626c724e8a9SChris Wilson 
1627c724e8a9SChris Wilson 	return i;
1628c724e8a9SChris Wilson }
1629c724e8a9SChris Wilson 
1630748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1631748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1632748ebc60SChris Wilson {
1633748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1634748ebc60SChris Wilson 	int i;
1635748ebc60SChris Wilson 
1636748ebc60SChris Wilson 	/* Fences */
1637748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1638775d17b6SDaniel Vetter 	case 7:
1639748ebc60SChris Wilson 	case 6:
164042b5aeabSVille Syrjälä 		for (i = 0; i < dev_priv->num_fence_regs; i++)
1641748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1642748ebc60SChris Wilson 		break;
1643748ebc60SChris Wilson 	case 5:
1644748ebc60SChris Wilson 	case 4:
1645748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1646748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1647748ebc60SChris Wilson 		break;
1648748ebc60SChris Wilson 	case 3:
1649748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1650748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1651748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1652748ebc60SChris Wilson 	case 2:
1653748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1654748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1655748ebc60SChris Wilson 		break;
1656748ebc60SChris Wilson 
16577dbf9d6eSBen Widawsky 	default:
16587dbf9d6eSBen Widawsky 		BUG();
1659748ebc60SChris Wilson 	}
1660748ebc60SChris Wilson }
1661748ebc60SChris Wilson 
1662bcfb2e28SChris Wilson static struct drm_i915_error_object *
1663bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1664bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1665bcfb2e28SChris Wilson {
1666bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1667bcfb2e28SChris Wilson 	u32 seqno;
1668bcfb2e28SChris Wilson 
1669bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1670bcfb2e28SChris Wilson 		return NULL;
1671bcfb2e28SChris Wilson 
1672b45305fcSDaniel Vetter 	if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1673b45305fcSDaniel Vetter 		u32 acthd = I915_READ(ACTHD);
1674b45305fcSDaniel Vetter 
1675b45305fcSDaniel Vetter 		if (WARN_ON(ring->id != RCS))
1676b45305fcSDaniel Vetter 			return NULL;
1677b45305fcSDaniel Vetter 
1678b45305fcSDaniel Vetter 		obj = ring->private;
1679b45305fcSDaniel Vetter 		if (acthd >= obj->gtt_offset &&
1680b45305fcSDaniel Vetter 		    acthd < obj->gtt_offset + obj->base.size)
1681b45305fcSDaniel Vetter 			return i915_error_object_create(dev_priv, obj);
1682b45305fcSDaniel Vetter 	}
1683b45305fcSDaniel Vetter 
1684b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1685bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1686bcfb2e28SChris Wilson 		if (obj->ring != ring)
1687bcfb2e28SChris Wilson 			continue;
1688bcfb2e28SChris Wilson 
16890201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1690bcfb2e28SChris Wilson 			continue;
1691bcfb2e28SChris Wilson 
1692bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1693bcfb2e28SChris Wilson 			continue;
1694bcfb2e28SChris Wilson 
1695bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1696bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1697bcfb2e28SChris Wilson 		 */
1698bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1699bcfb2e28SChris Wilson 	}
1700bcfb2e28SChris Wilson 
1701bcfb2e28SChris Wilson 	return NULL;
1702bcfb2e28SChris Wilson }
1703bcfb2e28SChris Wilson 
1704d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1705d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1706d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1707d27b1e0eSDaniel Vetter {
1708d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1709d27b1e0eSDaniel Vetter 
171033f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
171112f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
171233f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
17137e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
17147e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
17157e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
17167e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
1717df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1718df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
171933f3f518SDaniel Vetter 	}
1720c1cd90edSDaniel Vetter 
1721d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
17229d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1723d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1724d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1725d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1726c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1727050ee91fSBen Widawsky 		if (ring->id == RCS)
1728d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1729d27b1e0eSDaniel Vetter 	} else {
17309d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1731d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1732d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1733d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1734d27b1e0eSDaniel Vetter 	}
1735d27b1e0eSDaniel Vetter 
17369574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1737c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1738b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1739d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1740c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1741c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
17420f3b6849SChris Wilson 	error->ctl[ring->id] = I915_READ_CTL(ring);
17437e3b8737SDaniel Vetter 
17447e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
17457e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1746d27b1e0eSDaniel Vetter }
1747d27b1e0eSDaniel Vetter 
17488c123e54SBen Widawsky 
17498c123e54SBen Widawsky static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
17508c123e54SBen Widawsky 					   struct drm_i915_error_state *error,
17518c123e54SBen Widawsky 					   struct drm_i915_error_ring *ering)
17528c123e54SBen Widawsky {
17538c123e54SBen Widawsky 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
17548c123e54SBen Widawsky 	struct drm_i915_gem_object *obj;
17558c123e54SBen Widawsky 
17568c123e54SBen Widawsky 	/* Currently render ring is the only HW context user */
17578c123e54SBen Widawsky 	if (ring->id != RCS || !error->ccid)
17588c123e54SBen Widawsky 		return;
17598c123e54SBen Widawsky 
17608c123e54SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) {
17618c123e54SBen Widawsky 		if ((error->ccid & PAGE_MASK) == obj->gtt_offset) {
17628c123e54SBen Widawsky 			ering->ctx = i915_error_object_create_sized(dev_priv,
17638c123e54SBen Widawsky 								    obj, 1);
17648c123e54SBen Widawsky 		}
17658c123e54SBen Widawsky 	}
17668c123e54SBen Widawsky }
17678c123e54SBen Widawsky 
176852d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
176952d39a21SChris Wilson 				  struct drm_i915_error_state *error)
177052d39a21SChris Wilson {
177152d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1772b4519513SChris Wilson 	struct intel_ring_buffer *ring;
177352d39a21SChris Wilson 	struct drm_i915_gem_request *request;
177452d39a21SChris Wilson 	int i, count;
177552d39a21SChris Wilson 
1776b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
177752d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
177852d39a21SChris Wilson 
177952d39a21SChris Wilson 		error->ring[i].batchbuffer =
178052d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
178152d39a21SChris Wilson 
178252d39a21SChris Wilson 		error->ring[i].ringbuffer =
178352d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
178452d39a21SChris Wilson 
17858c123e54SBen Widawsky 
17868c123e54SBen Widawsky 		i915_gem_record_active_context(ring, error, &error->ring[i]);
17878c123e54SBen Widawsky 
178852d39a21SChris Wilson 		count = 0;
178952d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
179052d39a21SChris Wilson 			count++;
179152d39a21SChris Wilson 
179252d39a21SChris Wilson 		error->ring[i].num_requests = count;
179352d39a21SChris Wilson 		error->ring[i].requests =
179452d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
179552d39a21SChris Wilson 				GFP_ATOMIC);
179652d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
179752d39a21SChris Wilson 			error->ring[i].num_requests = 0;
179852d39a21SChris Wilson 			continue;
179952d39a21SChris Wilson 		}
180052d39a21SChris Wilson 
180152d39a21SChris Wilson 		count = 0;
180252d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
180352d39a21SChris Wilson 			struct drm_i915_error_request *erq;
180452d39a21SChris Wilson 
180552d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
180652d39a21SChris Wilson 			erq->seqno = request->seqno;
180752d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1808ee4f42b1SChris Wilson 			erq->tail = request->tail;
180952d39a21SChris Wilson 		}
181052d39a21SChris Wilson 	}
181152d39a21SChris Wilson }
181252d39a21SChris Wilson 
18138a905236SJesse Barnes /**
18148a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
18158a905236SJesse Barnes  * @dev: drm device
18168a905236SJesse Barnes  *
18178a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
18188a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
18198a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
18208a905236SJesse Barnes  * to pick up.
18218a905236SJesse Barnes  */
182263eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
182363eeaf38SJesse Barnes {
182463eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
182505394f39SChris Wilson 	struct drm_i915_gem_object *obj;
182663eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
182763eeaf38SJesse Barnes 	unsigned long flags;
18289db4a9c7SJesse Barnes 	int i, pipe;
182963eeaf38SJesse Barnes 
183099584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
183199584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
183299584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
18339df30794SChris Wilson 	if (error)
18349df30794SChris Wilson 		return;
183563eeaf38SJesse Barnes 
18369db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
183733f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
183863eeaf38SJesse Barnes 	if (!error) {
18399df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
18409df30794SChris Wilson 		return;
184163eeaf38SJesse Barnes 	}
184263eeaf38SJesse Barnes 
18432f86f191SBen Widawsky 	DRM_INFO("capturing error event; look for more information in "
18442f86f191SBen Widawsky 		 "/sys/kernel/debug/dri/%d/i915_error_state\n",
1845b6f7833bSChris Wilson 		 dev->primary->index);
18462fa772f3SChris Wilson 
1847742cbee8SDaniel Vetter 	kref_init(&error->ref);
184863eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
184963eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1850211816ecSBen Widawsky 	if (HAS_HW_CONTEXTS(dev))
1851b9a3906bSBen Widawsky 		error->ccid = I915_READ(CCID);
1852be998e2eSBen Widawsky 
1853be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1854be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1855be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1856be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1857be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1858be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1859be998e2eSBen Widawsky 	else
1860be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1861be998e2eSBen Widawsky 
18620f3b6849SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6)
18630f3b6849SChris Wilson 		error->derrmr = I915_READ(DERRMR);
18640f3b6849SChris Wilson 
18650f3b6849SChris Wilson 	if (IS_VALLEYVIEW(dev))
18660f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_VLV);
18670f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 7)
18680f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_MT);
18690f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen == 6)
18700f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE);
18710f3b6849SChris Wilson 
18724f3308b9SPaulo Zanoni 	if (!HAS_PCH_SPLIT(dev))
18739db4a9c7SJesse Barnes 		for_each_pipe(pipe)
18749db4a9c7SJesse Barnes 			error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1875d27b1e0eSDaniel Vetter 
187633f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1877f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
187833f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
187933f3f518SDaniel Vetter 	}
1880add354ddSChris Wilson 
188171e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
188271e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
188371e172e8SBen Widawsky 
1884050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1885050ee91fSBen Widawsky 
1886748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
188752d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
18889df30794SChris Wilson 
1889c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
18909df30794SChris Wilson 	error->active_bo = NULL;
1891c724e8a9SChris Wilson 	error->pinned_bo = NULL;
18929df30794SChris Wilson 
1893bcfb2e28SChris Wilson 	i = 0;
1894bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1895bcfb2e28SChris Wilson 		i++;
1896bcfb2e28SChris Wilson 	error->active_bo_count = i;
18976c085a72SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
18981b50247aSChris Wilson 		if (obj->pin_count)
1899bcfb2e28SChris Wilson 			i++;
1900bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1901c724e8a9SChris Wilson 
19028e934dbfSChris Wilson 	error->active_bo = NULL;
19038e934dbfSChris Wilson 	error->pinned_bo = NULL;
1904bcfb2e28SChris Wilson 	if (i) {
1905bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
19069df30794SChris Wilson 					   GFP_ATOMIC);
1907c724e8a9SChris Wilson 		if (error->active_bo)
1908c724e8a9SChris Wilson 			error->pinned_bo =
1909c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
19109df30794SChris Wilson 	}
1911c724e8a9SChris Wilson 
1912c724e8a9SChris Wilson 	if (error->active_bo)
1913c724e8a9SChris Wilson 		error->active_bo_count =
19141b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1915c724e8a9SChris Wilson 					  error->active_bo_count,
1916c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1917c724e8a9SChris Wilson 
1918c724e8a9SChris Wilson 	if (error->pinned_bo)
1919c724e8a9SChris Wilson 		error->pinned_bo_count =
19201b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1921c724e8a9SChris Wilson 					  error->pinned_bo_count,
19226c085a72SChris Wilson 					  &dev_priv->mm.bound_list);
192363eeaf38SJesse Barnes 
19248a905236SJesse Barnes 	do_gettimeofday(&error->time);
19258a905236SJesse Barnes 
19266ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1927c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
19286ef3d427SChris Wilson 
192999584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
193099584db3SDaniel Vetter 	if (dev_priv->gpu_error.first_error == NULL) {
193199584db3SDaniel Vetter 		dev_priv->gpu_error.first_error = error;
19329df30794SChris Wilson 		error = NULL;
19339df30794SChris Wilson 	}
193499584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19359df30794SChris Wilson 
19369df30794SChris Wilson 	if (error)
1937742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
19389df30794SChris Wilson }
19399df30794SChris Wilson 
19409df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
19419df30794SChris Wilson {
19429df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
19439df30794SChris Wilson 	struct drm_i915_error_state *error;
19446dc0e816SBen Widawsky 	unsigned long flags;
19459df30794SChris Wilson 
194699584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
194799584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
194899584db3SDaniel Vetter 	dev_priv->gpu_error.first_error = NULL;
194999584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19509df30794SChris Wilson 
19519df30794SChris Wilson 	if (error)
1952742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
195363eeaf38SJesse Barnes }
19543bd3c932SChris Wilson #else
19553bd3c932SChris Wilson #define i915_capture_error_state(x)
19563bd3c932SChris Wilson #endif
195763eeaf38SJesse Barnes 
195835aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1959c0e09200SDave Airlie {
19608a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1961bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
196263eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1963050ee91fSBen Widawsky 	int pipe, i;
196463eeaf38SJesse Barnes 
196535aed2e6SChris Wilson 	if (!eir)
196635aed2e6SChris Wilson 		return;
196763eeaf38SJesse Barnes 
1968a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
19698a905236SJesse Barnes 
1970bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1971bd9854f9SBen Widawsky 
19728a905236SJesse Barnes 	if (IS_G4X(dev)) {
19738a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
19748a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
19758a905236SJesse Barnes 
1976a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1977a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1978050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1979050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1980a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1981a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
19828a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
19833143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
19848a905236SJesse Barnes 		}
19858a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
19868a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1987a70491ccSJoe Perches 			pr_err("page table error\n");
1988a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
19898a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
19903143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
19918a905236SJesse Barnes 		}
19928a905236SJesse Barnes 	}
19938a905236SJesse Barnes 
1994a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
199563eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
199663eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1997a70491ccSJoe Perches 			pr_err("page table error\n");
1998a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
199963eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20003143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
200163eeaf38SJesse Barnes 		}
20028a905236SJesse Barnes 	}
20038a905236SJesse Barnes 
200463eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2005a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
20069db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2007a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
20089db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
200963eeaf38SJesse Barnes 		/* pipestat has already been acked */
201063eeaf38SJesse Barnes 	}
201163eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2012a70491ccSJoe Perches 		pr_err("instruction error\n");
2013a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2014050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2015050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2016a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
201763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
201863eeaf38SJesse Barnes 
2019a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2020a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2021a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
202263eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
20233143a2bfSChris Wilson 			POSTING_READ(IPEIR);
202463eeaf38SJesse Barnes 		} else {
202563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
202663eeaf38SJesse Barnes 
2027a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2028a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2029a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2030a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
203163eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20323143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
203363eeaf38SJesse Barnes 		}
203463eeaf38SJesse Barnes 	}
203563eeaf38SJesse Barnes 
203663eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
20373143a2bfSChris Wilson 	POSTING_READ(EIR);
203863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
203963eeaf38SJesse Barnes 	if (eir) {
204063eeaf38SJesse Barnes 		/*
204163eeaf38SJesse Barnes 		 * some errors might have become stuck,
204263eeaf38SJesse Barnes 		 * mask them.
204363eeaf38SJesse Barnes 		 */
204463eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
204563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
204663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
204763eeaf38SJesse Barnes 	}
204835aed2e6SChris Wilson }
204935aed2e6SChris Wilson 
205035aed2e6SChris Wilson /**
205135aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
205235aed2e6SChris Wilson  * @dev: drm device
205335aed2e6SChris Wilson  *
205435aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
205535aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
205635aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
205735aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
205835aed2e6SChris Wilson  * of a ring dump etc.).
205935aed2e6SChris Wilson  */
2060527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
206135aed2e6SChris Wilson {
206235aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2063b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2064b4519513SChris Wilson 	int i;
206535aed2e6SChris Wilson 
206635aed2e6SChris Wilson 	i915_capture_error_state(dev);
206735aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
20688a905236SJesse Barnes 
2069ba1234d1SBen Gamari 	if (wedged) {
2070f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2071f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2072ba1234d1SBen Gamari 
207311ed50ecSBen Gamari 		/*
20741f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
20751f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
207611ed50ecSBen Gamari 		 */
2077b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
2078b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
207911ed50ecSBen Gamari 	}
208011ed50ecSBen Gamari 
208199584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
20828a905236SJesse Barnes }
20838a905236SJesse Barnes 
208421ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
20854e5359cdSSimon Farnsworth {
20864e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
20874e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
20884e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
208905394f39SChris Wilson 	struct drm_i915_gem_object *obj;
20904e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
20914e5359cdSSimon Farnsworth 	unsigned long flags;
20924e5359cdSSimon Farnsworth 	bool stall_detected;
20934e5359cdSSimon Farnsworth 
20944e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
20954e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
20964e5359cdSSimon Farnsworth 		return;
20974e5359cdSSimon Farnsworth 
20984e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
20994e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
21004e5359cdSSimon Farnsworth 
2101e7d841caSChris Wilson 	if (work == NULL ||
2102e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2103e7d841caSChris Wilson 	    !work->enable_stall_check) {
21044e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
21054e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
21064e5359cdSSimon Farnsworth 		return;
21074e5359cdSSimon Farnsworth 	}
21084e5359cdSSimon Farnsworth 
21094e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
211005394f39SChris Wilson 	obj = work->pending_flip_obj;
2111a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
21129db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2113446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2114446f2545SArmin Reese 					obj->gtt_offset;
21154e5359cdSSimon Farnsworth 	} else {
21169db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
211705394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
211801f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
21194e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
21204e5359cdSSimon Farnsworth 	}
21214e5359cdSSimon Farnsworth 
21224e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
21234e5359cdSSimon Farnsworth 
21244e5359cdSSimon Farnsworth 	if (stall_detected) {
21254e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
21264e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
21274e5359cdSSimon Farnsworth 	}
21284e5359cdSSimon Farnsworth }
21294e5359cdSSimon Farnsworth 
213042f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
213142f52ef8SKeith Packard  * we use as a pipe index
213242f52ef8SKeith Packard  */
2133f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
21340a3e67a4SJesse Barnes {
21350a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2136e9d21d7fSKeith Packard 	unsigned long irqflags;
213771e0ffa5SJesse Barnes 
21385eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
213971e0ffa5SJesse Barnes 		return -EINVAL;
21400a3e67a4SJesse Barnes 
21411ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2142f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
21437c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21447c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
21450a3e67a4SJesse Barnes 	else
21467c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21477c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
21488692d00eSChris Wilson 
21498692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
21508692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
21516b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
21521ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
21538692d00eSChris Wilson 
21540a3e67a4SJesse Barnes 	return 0;
21550a3e67a4SJesse Barnes }
21560a3e67a4SJesse Barnes 
2157f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2158f796cf8fSJesse Barnes {
2159f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2160f796cf8fSJesse Barnes 	unsigned long irqflags;
2161f796cf8fSJesse Barnes 
2162f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2163f796cf8fSJesse Barnes 		return -EINVAL;
2164f796cf8fSJesse Barnes 
2165f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2166f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
2167f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2168f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2169f796cf8fSJesse Barnes 
2170f796cf8fSJesse Barnes 	return 0;
2171f796cf8fSJesse Barnes }
2172f796cf8fSJesse Barnes 
2173f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
2174b1f14ad0SJesse Barnes {
2175b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2176b1f14ad0SJesse Barnes 	unsigned long irqflags;
2177b1f14ad0SJesse Barnes 
2178b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2179b1f14ad0SJesse Barnes 		return -EINVAL;
2180b1f14ad0SJesse Barnes 
2181b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2182b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
2183b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
2184b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2185b1f14ad0SJesse Barnes 
2186b1f14ad0SJesse Barnes 	return 0;
2187b1f14ad0SJesse Barnes }
2188b1f14ad0SJesse Barnes 
21897e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
21907e231dbeSJesse Barnes {
21917e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21927e231dbeSJesse Barnes 	unsigned long irqflags;
219331acc7f5SJesse Barnes 	u32 imr;
21947e231dbeSJesse Barnes 
21957e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
21967e231dbeSJesse Barnes 		return -EINVAL;
21977e231dbeSJesse Barnes 
21987e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
21997e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
220031acc7f5SJesse Barnes 	if (pipe == 0)
22017e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
220231acc7f5SJesse Barnes 	else
22037e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22047e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
220531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
220631acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
22077e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22087e231dbeSJesse Barnes 
22097e231dbeSJesse Barnes 	return 0;
22107e231dbeSJesse Barnes }
22117e231dbeSJesse Barnes 
221242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
221342f52ef8SKeith Packard  * we use as a pipe index
221442f52ef8SKeith Packard  */
2215f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
22160a3e67a4SJesse Barnes {
22170a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2218e9d21d7fSKeith Packard 	unsigned long irqflags;
22190a3e67a4SJesse Barnes 
22201ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22218692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
22226b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
22238692d00eSChris Wilson 
22247c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
22257c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
22267c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
22271ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22280a3e67a4SJesse Barnes }
22290a3e67a4SJesse Barnes 
2230f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2231f796cf8fSJesse Barnes {
2232f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2233f796cf8fSJesse Barnes 	unsigned long irqflags;
2234f796cf8fSJesse Barnes 
2235f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2236f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
2237f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2238f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2239f796cf8fSJesse Barnes }
2240f796cf8fSJesse Barnes 
2241f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
2242b1f14ad0SJesse Barnes {
2243b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2244b1f14ad0SJesse Barnes 	unsigned long irqflags;
2245b1f14ad0SJesse Barnes 
2246b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2247b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
2248b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
2249b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2250b1f14ad0SJesse Barnes }
2251b1f14ad0SJesse Barnes 
22527e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
22537e231dbeSJesse Barnes {
22547e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22557e231dbeSJesse Barnes 	unsigned long irqflags;
225631acc7f5SJesse Barnes 	u32 imr;
22577e231dbeSJesse Barnes 
22587e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
225931acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
226031acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
22617e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
226231acc7f5SJesse Barnes 	if (pipe == 0)
22637e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
226431acc7f5SJesse Barnes 	else
22657e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22667e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
22677e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22687e231dbeSJesse Barnes }
22697e231dbeSJesse Barnes 
2270893eead0SChris Wilson static u32
2271893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2272852835f3SZou Nan hai {
2273893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2274893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2275893eead0SChris Wilson }
2276893eead0SChris Wilson 
227779ee20dcSMika Kuoppala static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring,
227879ee20dcSMika Kuoppala 				     u32 ring_seqno, bool *err)
2279893eead0SChris Wilson {
2280893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
228179ee20dcSMika Kuoppala 	    i915_seqno_passed(ring_seqno, ring_last_seqno(ring))) {
2282893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
22839574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
22849574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
22859574b3feSBen Widawsky 				  ring->name);
2286893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
2287893eead0SChris Wilson 			*err = true;
2288893eead0SChris Wilson 		}
2289893eead0SChris Wilson 		return true;
2290893eead0SChris Wilson 	}
2291893eead0SChris Wilson 	return false;
2292f65d9421SBen Gamari }
2293f65d9421SBen Gamari 
2294a24a11e6SChris Wilson static bool semaphore_passed(struct intel_ring_buffer *ring)
2295a24a11e6SChris Wilson {
2296a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2297a24a11e6SChris Wilson 	u32 acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2298a24a11e6SChris Wilson 	struct intel_ring_buffer *signaller;
2299a24a11e6SChris Wilson 	u32 cmd, ipehr, acthd_min;
2300a24a11e6SChris Wilson 
2301a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2302a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2303a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
2304a24a11e6SChris Wilson 		return false;
2305a24a11e6SChris Wilson 
2306a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2307a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2308a24a11e6SChris Wilson 	 */
2309a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2310a24a11e6SChris Wilson 	do {
2311a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2312a24a11e6SChris Wilson 		if (cmd == ipehr)
2313a24a11e6SChris Wilson 			break;
2314a24a11e6SChris Wilson 
2315a24a11e6SChris Wilson 		acthd -= 4;
2316a24a11e6SChris Wilson 		if (acthd < acthd_min)
2317a24a11e6SChris Wilson 			return false;
2318a24a11e6SChris Wilson 	} while (1);
2319a24a11e6SChris Wilson 
2320a24a11e6SChris Wilson 	signaller = &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2321a24a11e6SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false),
2322a24a11e6SChris Wilson 				 ioread32(ring->virtual_start+acthd+4)+1);
2323a24a11e6SChris Wilson }
2324a24a11e6SChris Wilson 
23251ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
23261ec14ad3SChris Wilson {
23271ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
23281ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
23291ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
23301ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
23311ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
23321ec14ad3SChris Wilson 			  ring->name);
23331ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
23341ec14ad3SChris Wilson 		return true;
23351ec14ad3SChris Wilson 	}
2336a24a11e6SChris Wilson 
2337a24a11e6SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 &&
2338a24a11e6SChris Wilson 	    tmp & RING_WAIT_SEMAPHORE &&
2339a24a11e6SChris Wilson 	    semaphore_passed(ring)) {
2340a24a11e6SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
2341a24a11e6SChris Wilson 			  ring->name);
2342a24a11e6SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2343a24a11e6SChris Wilson 		return true;
2344a24a11e6SChris Wilson 	}
23451ec14ad3SChris Wilson 	return false;
23461ec14ad3SChris Wilson }
23471ec14ad3SChris Wilson 
2348*ed5cbb03SMika Kuoppala static bool i915_hangcheck_ring_hung(struct intel_ring_buffer *ring)
2349d1e61e7fSChris Wilson {
2350*ed5cbb03SMika Kuoppala 	if (IS_GEN2(ring->dev))
2351*ed5cbb03SMika Kuoppala 		return false;
2352b4519513SChris Wilson 
2353d1e61e7fSChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
2354d1e61e7fSChris Wilson 	 * If so we can simply poke the RB_WAIT bit
2355d1e61e7fSChris Wilson 	 * and break the hang. This should work on
2356d1e61e7fSChris Wilson 	 * all but the second generation chipsets.
2357d1e61e7fSChris Wilson 	 */
2358*ed5cbb03SMika Kuoppala 	return !kick_ring(ring);
2359d1e61e7fSChris Wilson }
2360d1e61e7fSChris Wilson 
2361*ed5cbb03SMika Kuoppala static bool i915_hangcheck_hung(struct drm_device *dev)
2362*ed5cbb03SMika Kuoppala {
2363*ed5cbb03SMika Kuoppala 	drm_i915_private_t *dev_priv = dev->dev_private;
2364*ed5cbb03SMika Kuoppala 
2365*ed5cbb03SMika Kuoppala 	if (dev_priv->gpu_error.hangcheck_count++ > 1) {
2366*ed5cbb03SMika Kuoppala 		bool hung = true;
2367*ed5cbb03SMika Kuoppala 		struct intel_ring_buffer *ring;
2368*ed5cbb03SMika Kuoppala 		int i;
2369*ed5cbb03SMika Kuoppala 
2370*ed5cbb03SMika Kuoppala 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
2371*ed5cbb03SMika Kuoppala 		i915_handle_error(dev, true);
2372*ed5cbb03SMika Kuoppala 
2373*ed5cbb03SMika Kuoppala 		for_each_ring(ring, dev_priv, i)
2374*ed5cbb03SMika Kuoppala 			hung &= i915_hangcheck_ring_hung(ring);
2375*ed5cbb03SMika Kuoppala 
2376b4519513SChris Wilson 		return hung;
2377d1e61e7fSChris Wilson 	}
2378d1e61e7fSChris Wilson 
2379d1e61e7fSChris Wilson 	return false;
2380d1e61e7fSChris Wilson }
2381d1e61e7fSChris Wilson 
2382f65d9421SBen Gamari /**
2383f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
2384f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
2385f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
2386f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
2387f65d9421SBen Gamari  */
2388f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
2389f65d9421SBen Gamari {
2390f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2391f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2392b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2393b4519513SChris Wilson 	bool err = false, idle;
2394b4519513SChris Wilson 	int i;
239592cab734SMika Kuoppala 	u32 seqno[I915_NUM_RINGS];
239692cab734SMika Kuoppala 	bool work_done;
2397893eead0SChris Wilson 
23983e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
23993e0dc6b0SBen Widawsky 		return;
24003e0dc6b0SBen Widawsky 
2401b4519513SChris Wilson 	idle = true;
2402b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
240392cab734SMika Kuoppala 		seqno[i] = ring->get_seqno(ring, false);
240492cab734SMika Kuoppala 		idle &= i915_hangcheck_ring_idle(ring, seqno[i], &err);
2405b4519513SChris Wilson 	}
2406b4519513SChris Wilson 
2407893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
2408b4519513SChris Wilson 	if (idle) {
2409d1e61e7fSChris Wilson 		if (err) {
2410d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
2411d1e61e7fSChris Wilson 				return;
2412d1e61e7fSChris Wilson 
2413893eead0SChris Wilson 			goto repeat;
2414d1e61e7fSChris Wilson 		}
2415d1e61e7fSChris Wilson 
241699584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
2417893eead0SChris Wilson 		return;
2418893eead0SChris Wilson 	}
2419f65d9421SBen Gamari 
242092cab734SMika Kuoppala 	work_done = false;
242192cab734SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
242292cab734SMika Kuoppala 		if (ring->hangcheck.seqno != seqno[i]) {
242392cab734SMika Kuoppala 			work_done = true;
242492cab734SMika Kuoppala 			ring->hangcheck.seqno = seqno[i];
242592cab734SMika Kuoppala 		}
242692cab734SMika Kuoppala 	}
242792cab734SMika Kuoppala 
242892cab734SMika Kuoppala 	if (!work_done) {
2429d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
2430f65d9421SBen Gamari 			return;
2431cbb465e7SChris Wilson 	} else {
243299584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
2433cbb465e7SChris Wilson 	}
2434f65d9421SBen Gamari 
2435893eead0SChris Wilson repeat:
2436f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
243799584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
2438cecc21feSChris Wilson 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2439f65d9421SBen Gamari }
2440f65d9421SBen Gamari 
2441c0e09200SDave Airlie /* drm_dma.h hooks
2442c0e09200SDave Airlie */
2443f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2444036a4a7dSZhenyu Wang {
2445036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2446036a4a7dSZhenyu Wang 
24474697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
24484697995bSJesse Barnes 
2449036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2450bdfcdb63SDaniel Vetter 
2451036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
2452036a4a7dSZhenyu Wang 
2453036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2454036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
24553143a2bfSChris Wilson 	POSTING_READ(DEIER);
2456036a4a7dSZhenyu Wang 
2457036a4a7dSZhenyu Wang 	/* and GT */
2458036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2459036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
24603143a2bfSChris Wilson 	POSTING_READ(GTIER);
2461c650156aSZhenyu Wang 
2462ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2463ab5c608bSBen Widawsky 		return;
2464ab5c608bSBen Widawsky 
2465c650156aSZhenyu Wang 	/* south display irq */
2466c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
246782a28bcfSDaniel Vetter 	/*
246882a28bcfSDaniel Vetter 	 * SDEIER is also touched by the interrupt handler to work around missed
246982a28bcfSDaniel Vetter 	 * PCH interrupts. Hence we can't update it after the interrupt handler
247082a28bcfSDaniel Vetter 	 * is enabled - instead we unconditionally enable all PCH interrupt
247182a28bcfSDaniel Vetter 	 * sources here, but then only unmask them as needed with SDEIMR.
247282a28bcfSDaniel Vetter 	 */
247382a28bcfSDaniel Vetter 	I915_WRITE(SDEIER, 0xffffffff);
24743143a2bfSChris Wilson 	POSTING_READ(SDEIER);
2475036a4a7dSZhenyu Wang }
2476036a4a7dSZhenyu Wang 
24777e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
24787e231dbeSJesse Barnes {
24797e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24807e231dbeSJesse Barnes 	int pipe;
24817e231dbeSJesse Barnes 
24827e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
24837e231dbeSJesse Barnes 
24847e231dbeSJesse Barnes 	/* VLV magic */
24857e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
24867e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
24877e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
24887e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
24897e231dbeSJesse Barnes 
24907e231dbeSJesse Barnes 	/* and GT */
24917e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
24927e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
24937e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
24947e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
24957e231dbeSJesse Barnes 	POSTING_READ(GTIER);
24967e231dbeSJesse Barnes 
24977e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
24987e231dbeSJesse Barnes 
24997e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
25007e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
25017e231dbeSJesse Barnes 	for_each_pipe(pipe)
25027e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
25037e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
25047e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
25057e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
25067e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
25077e231dbeSJesse Barnes }
25087e231dbeSJesse Barnes 
250982a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
251082a28bcfSDaniel Vetter {
251182a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
251282a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
251382a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
251482a28bcfSDaniel Vetter 	u32 mask = ~I915_READ(SDEIMR);
251582a28bcfSDaniel Vetter 	u32 hotplug;
251682a28bcfSDaniel Vetter 
251782a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2518995e6b3dSEgbert Eich 		mask &= ~SDE_HOTPLUG_MASK;
251982a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2520cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
252182a28bcfSDaniel Vetter 				mask |= hpd_ibx[intel_encoder->hpd_pin];
252282a28bcfSDaniel Vetter 	} else {
2523995e6b3dSEgbert Eich 		mask &= ~SDE_HOTPLUG_MASK_CPT;
252482a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2525cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
252682a28bcfSDaniel Vetter 				mask |= hpd_cpt[intel_encoder->hpd_pin];
252782a28bcfSDaniel Vetter 	}
252882a28bcfSDaniel Vetter 
252982a28bcfSDaniel Vetter 	I915_WRITE(SDEIMR, ~mask);
253082a28bcfSDaniel Vetter 
25317fe0b973SKeith Packard 	/*
25327fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
25337fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
25347fe0b973SKeith Packard 	 *
25357fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
25367fe0b973SKeith Packard 	 */
25377fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
25387fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
25397fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
25407fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
25417fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
25427fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
25437fe0b973SKeith Packard }
25447fe0b973SKeith Packard 
2545d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2546d46da437SPaulo Zanoni {
2547d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
254882a28bcfSDaniel Vetter 	u32 mask;
2549d46da437SPaulo Zanoni 
25508664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
25518664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2552de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
25538664281bSPaulo Zanoni 	} else {
25548664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
25558664281bSPaulo Zanoni 
25568664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
25578664281bSPaulo Zanoni 	}
2558ab5c608bSBen Widawsky 
2559ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2560ab5c608bSBen Widawsky 		return;
2561ab5c608bSBen Widawsky 
2562d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2563d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2564d46da437SPaulo Zanoni }
2565d46da437SPaulo Zanoni 
2566f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2567036a4a7dSZhenyu Wang {
2568036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2569036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
2570013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2571ce99c256SDaniel Vetter 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
25728664281bSPaulo Zanoni 			   DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
2573de032bf4SPaulo Zanoni 			   DE_PIPEA_FIFO_UNDERRUN | DE_POISON;
25741ec14ad3SChris Wilson 	u32 render_irqs;
2575036a4a7dSZhenyu Wang 
25761ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2577036a4a7dSZhenyu Wang 
2578036a4a7dSZhenyu Wang 	/* should always can generate irq */
2579036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
25801ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
25811ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
25823143a2bfSChris Wilson 	POSTING_READ(DEIER);
2583036a4a7dSZhenyu Wang 
25841ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
2585036a4a7dSZhenyu Wang 
2586036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
25871ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2588881f47b6SXiang, Haihao 
25891ec14ad3SChris Wilson 	if (IS_GEN6(dev))
25901ec14ad3SChris Wilson 		render_irqs =
25911ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
2592e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
2593e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
25941ec14ad3SChris Wilson 	else
25951ec14ad3SChris Wilson 		render_irqs =
259688f23b8fSChris Wilson 			GT_USER_INTERRUPT |
2597c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
25981ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
25991ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
26003143a2bfSChris Wilson 	POSTING_READ(GTIER);
2601036a4a7dSZhenyu Wang 
2602d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
26037fe0b973SKeith Packard 
2604f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
2605f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
2606f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
2607f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
2608f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2609f97108d1SJesse Barnes 	}
2610f97108d1SJesse Barnes 
2611036a4a7dSZhenyu Wang 	return 0;
2612036a4a7dSZhenyu Wang }
2613036a4a7dSZhenyu Wang 
2614f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2615b1f14ad0SJesse Barnes {
2616b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2617b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2618b615b57aSChris Wilson 	u32 display_mask =
2619b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2620b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
2621b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
2622ce99c256SDaniel Vetter 		DE_PLANEA_FLIP_DONE_IVB |
26238664281bSPaulo Zanoni 		DE_AUX_CHANNEL_A_IVB |
26248664281bSPaulo Zanoni 		DE_ERR_INT_IVB;
2625b1f14ad0SJesse Barnes 	u32 render_irqs;
2626b1f14ad0SJesse Barnes 
2627b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2628b1f14ad0SJesse Barnes 
2629b1f14ad0SJesse Barnes 	/* should always can generate irq */
26308664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2631b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2632b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2633b615b57aSChris Wilson 	I915_WRITE(DEIER,
2634b615b57aSChris Wilson 		   display_mask |
2635b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
2636b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
2637b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
2638b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2639b1f14ad0SJesse Barnes 
264015b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2641b1f14ad0SJesse Barnes 
2642b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2643b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2644b1f14ad0SJesse Barnes 
2645e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
264615b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2647b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
2648b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2649b1f14ad0SJesse Barnes 
2650d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
26517fe0b973SKeith Packard 
2652b1f14ad0SJesse Barnes 	return 0;
2653b1f14ad0SJesse Barnes }
2654b1f14ad0SJesse Barnes 
26557e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
26567e231dbeSJesse Barnes {
26577e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26587e231dbeSJesse Barnes 	u32 enable_mask;
265931acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
26603bcedbe5SJesse Barnes 	u32 render_irqs;
26617e231dbeSJesse Barnes 
26627e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
266331acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
266431acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
266531acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
26667e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
26677e231dbeSJesse Barnes 
266831acc7f5SJesse Barnes 	/*
266931acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
267031acc7f5SJesse Barnes 	 * toggle them based on usage.
267131acc7f5SJesse Barnes 	 */
267231acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
267331acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
267431acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
26757e231dbeSJesse Barnes 
267620afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
267720afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
267820afbda2SDaniel Vetter 
26797e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
26807e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
26817e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26827e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
26837e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
26847e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
26857e231dbeSJesse Barnes 
268631acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2687515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
268831acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
268931acc7f5SJesse Barnes 
26907e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26917e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26927e231dbeSJesse Barnes 
269331acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
269431acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
26953bcedbe5SJesse Barnes 
26963bcedbe5SJesse Barnes 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
26973bcedbe5SJesse Barnes 		GEN6_BLITTER_USER_INTERRUPT;
26983bcedbe5SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
26997e231dbeSJesse Barnes 	POSTING_READ(GTIER);
27007e231dbeSJesse Barnes 
27017e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
27027e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
27037e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
27047e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
27057e231dbeSJesse Barnes #endif
27067e231dbeSJesse Barnes 
27077e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
270820afbda2SDaniel Vetter 
270920afbda2SDaniel Vetter 	return 0;
271020afbda2SDaniel Vetter }
271120afbda2SDaniel Vetter 
27127e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
27137e231dbeSJesse Barnes {
27147e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
27157e231dbeSJesse Barnes 	int pipe;
27167e231dbeSJesse Barnes 
27177e231dbeSJesse Barnes 	if (!dev_priv)
27187e231dbeSJesse Barnes 		return;
27197e231dbeSJesse Barnes 
2720ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2721ac4c16c5SEgbert Eich 
27227e231dbeSJesse Barnes 	for_each_pipe(pipe)
27237e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
27247e231dbeSJesse Barnes 
27257e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
27267e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
27277e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
27287e231dbeSJesse Barnes 	for_each_pipe(pipe)
27297e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
27307e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
27317e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
27327e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
27337e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
27347e231dbeSJesse Barnes }
27357e231dbeSJesse Barnes 
2736f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2737036a4a7dSZhenyu Wang {
2738036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
27394697995bSJesse Barnes 
27404697995bSJesse Barnes 	if (!dev_priv)
27414697995bSJesse Barnes 		return;
27424697995bSJesse Barnes 
2743ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2744ac4c16c5SEgbert Eich 
2745036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2746036a4a7dSZhenyu Wang 
2747036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2748036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2749036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
27508664281bSPaulo Zanoni 	if (IS_GEN7(dev))
27518664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2752036a4a7dSZhenyu Wang 
2753036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2754036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2755036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2756192aac1fSKeith Packard 
2757ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2758ab5c608bSBen Widawsky 		return;
2759ab5c608bSBen Widawsky 
2760192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2761192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2762192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
27638664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
27648664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2765036a4a7dSZhenyu Wang }
2766036a4a7dSZhenyu Wang 
2767c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2768c2798b19SChris Wilson {
2769c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2770c2798b19SChris Wilson 	int pipe;
2771c2798b19SChris Wilson 
2772c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2773c2798b19SChris Wilson 
2774c2798b19SChris Wilson 	for_each_pipe(pipe)
2775c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2776c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2777c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2778c2798b19SChris Wilson 	POSTING_READ16(IER);
2779c2798b19SChris Wilson }
2780c2798b19SChris Wilson 
2781c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2782c2798b19SChris Wilson {
2783c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2784c2798b19SChris Wilson 
2785c2798b19SChris Wilson 	I915_WRITE16(EMR,
2786c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2787c2798b19SChris Wilson 
2788c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2789c2798b19SChris Wilson 	dev_priv->irq_mask =
2790c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2791c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2792c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2793c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2794c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2795c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2796c2798b19SChris Wilson 
2797c2798b19SChris Wilson 	I915_WRITE16(IER,
2798c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2799c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2800c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2801c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2802c2798b19SChris Wilson 	POSTING_READ16(IER);
2803c2798b19SChris Wilson 
2804c2798b19SChris Wilson 	return 0;
2805c2798b19SChris Wilson }
2806c2798b19SChris Wilson 
280790a72f87SVille Syrjälä /*
280890a72f87SVille Syrjälä  * Returns true when a page flip has completed.
280990a72f87SVille Syrjälä  */
281090a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
281190a72f87SVille Syrjälä 			       int pipe, u16 iir)
281290a72f87SVille Syrjälä {
281390a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
281490a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
281590a72f87SVille Syrjälä 
281690a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
281790a72f87SVille Syrjälä 		return false;
281890a72f87SVille Syrjälä 
281990a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
282090a72f87SVille Syrjälä 		return false;
282190a72f87SVille Syrjälä 
282290a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
282390a72f87SVille Syrjälä 
282490a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
282590a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
282690a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
282790a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
282890a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
282990a72f87SVille Syrjälä 	 */
283090a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
283190a72f87SVille Syrjälä 		return false;
283290a72f87SVille Syrjälä 
283390a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
283490a72f87SVille Syrjälä 
283590a72f87SVille Syrjälä 	return true;
283690a72f87SVille Syrjälä }
283790a72f87SVille Syrjälä 
2838ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2839c2798b19SChris Wilson {
2840c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2841c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2842c2798b19SChris Wilson 	u16 iir, new_iir;
2843c2798b19SChris Wilson 	u32 pipe_stats[2];
2844c2798b19SChris Wilson 	unsigned long irqflags;
2845c2798b19SChris Wilson 	int irq_received;
2846c2798b19SChris Wilson 	int pipe;
2847c2798b19SChris Wilson 	u16 flip_mask =
2848c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2849c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2850c2798b19SChris Wilson 
2851c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2852c2798b19SChris Wilson 
2853c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2854c2798b19SChris Wilson 	if (iir == 0)
2855c2798b19SChris Wilson 		return IRQ_NONE;
2856c2798b19SChris Wilson 
2857c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2858c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2859c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2860c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2861c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2862c2798b19SChris Wilson 		 */
2863c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2864c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2865c2798b19SChris Wilson 			i915_handle_error(dev, false);
2866c2798b19SChris Wilson 
2867c2798b19SChris Wilson 		for_each_pipe(pipe) {
2868c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2869c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2870c2798b19SChris Wilson 
2871c2798b19SChris Wilson 			/*
2872c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2873c2798b19SChris Wilson 			 */
2874c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2875c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2876c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2877c2798b19SChris Wilson 							 pipe_name(pipe));
2878c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2879c2798b19SChris Wilson 				irq_received = 1;
2880c2798b19SChris Wilson 			}
2881c2798b19SChris Wilson 		}
2882c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2883c2798b19SChris Wilson 
2884c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2885c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2886c2798b19SChris Wilson 
2887d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2888c2798b19SChris Wilson 
2889c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2890c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2891c2798b19SChris Wilson 
2892c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
289390a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
289490a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2895c2798b19SChris Wilson 
2896c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
289790a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
289890a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2899c2798b19SChris Wilson 
2900c2798b19SChris Wilson 		iir = new_iir;
2901c2798b19SChris Wilson 	}
2902c2798b19SChris Wilson 
2903c2798b19SChris Wilson 	return IRQ_HANDLED;
2904c2798b19SChris Wilson }
2905c2798b19SChris Wilson 
2906c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2907c2798b19SChris Wilson {
2908c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2909c2798b19SChris Wilson 	int pipe;
2910c2798b19SChris Wilson 
2911c2798b19SChris Wilson 	for_each_pipe(pipe) {
2912c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2913c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2914c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2915c2798b19SChris Wilson 	}
2916c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2917c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2918c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2919c2798b19SChris Wilson }
2920c2798b19SChris Wilson 
2921a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2922a266c7d5SChris Wilson {
2923a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2924a266c7d5SChris Wilson 	int pipe;
2925a266c7d5SChris Wilson 
2926a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2927a266c7d5SChris Wilson 
2928a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2929a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2930a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2931a266c7d5SChris Wilson 	}
2932a266c7d5SChris Wilson 
293300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2934a266c7d5SChris Wilson 	for_each_pipe(pipe)
2935a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2936a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2937a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2938a266c7d5SChris Wilson 	POSTING_READ(IER);
2939a266c7d5SChris Wilson }
2940a266c7d5SChris Wilson 
2941a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2942a266c7d5SChris Wilson {
2943a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
294438bde180SChris Wilson 	u32 enable_mask;
2945a266c7d5SChris Wilson 
294638bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
294738bde180SChris Wilson 
294838bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
294938bde180SChris Wilson 	dev_priv->irq_mask =
295038bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
295138bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
295238bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
295338bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
295438bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
295538bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
295638bde180SChris Wilson 
295738bde180SChris Wilson 	enable_mask =
295838bde180SChris Wilson 		I915_ASLE_INTERRUPT |
295938bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
296038bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
296138bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
296238bde180SChris Wilson 		I915_USER_INTERRUPT;
296338bde180SChris Wilson 
2964a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
296520afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
296620afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
296720afbda2SDaniel Vetter 
2968a266c7d5SChris Wilson 		/* Enable in IER... */
2969a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2970a266c7d5SChris Wilson 		/* and unmask in IMR */
2971a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2972a266c7d5SChris Wilson 	}
2973a266c7d5SChris Wilson 
2974a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2975a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2976a266c7d5SChris Wilson 	POSTING_READ(IER);
2977a266c7d5SChris Wilson 
2978f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
297920afbda2SDaniel Vetter 
298020afbda2SDaniel Vetter 	return 0;
298120afbda2SDaniel Vetter }
298220afbda2SDaniel Vetter 
298390a72f87SVille Syrjälä /*
298490a72f87SVille Syrjälä  * Returns true when a page flip has completed.
298590a72f87SVille Syrjälä  */
298690a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
298790a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
298890a72f87SVille Syrjälä {
298990a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
299090a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
299190a72f87SVille Syrjälä 
299290a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
299390a72f87SVille Syrjälä 		return false;
299490a72f87SVille Syrjälä 
299590a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
299690a72f87SVille Syrjälä 		return false;
299790a72f87SVille Syrjälä 
299890a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
299990a72f87SVille Syrjälä 
300090a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
300190a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
300290a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
300390a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
300490a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
300590a72f87SVille Syrjälä 	 */
300690a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
300790a72f87SVille Syrjälä 		return false;
300890a72f87SVille Syrjälä 
300990a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
301090a72f87SVille Syrjälä 
301190a72f87SVille Syrjälä 	return true;
301290a72f87SVille Syrjälä }
301390a72f87SVille Syrjälä 
3014ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3015a266c7d5SChris Wilson {
3016a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3017a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
30188291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3019a266c7d5SChris Wilson 	unsigned long irqflags;
302038bde180SChris Wilson 	u32 flip_mask =
302138bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
302238bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
302338bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3024a266c7d5SChris Wilson 
3025a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3026a266c7d5SChris Wilson 
3027a266c7d5SChris Wilson 	iir = I915_READ(IIR);
302838bde180SChris Wilson 	do {
302938bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
30308291ee90SChris Wilson 		bool blc_event = false;
3031a266c7d5SChris Wilson 
3032a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3033a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3034a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3035a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3036a266c7d5SChris Wilson 		 */
3037a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3038a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3039a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3040a266c7d5SChris Wilson 
3041a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3042a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3043a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3044a266c7d5SChris Wilson 
304538bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3046a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3047a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3048a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3049a266c7d5SChris Wilson 							 pipe_name(pipe));
3050a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
305138bde180SChris Wilson 				irq_received = true;
3052a266c7d5SChris Wilson 			}
3053a266c7d5SChris Wilson 		}
3054a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3055a266c7d5SChris Wilson 
3056a266c7d5SChris Wilson 		if (!irq_received)
3057a266c7d5SChris Wilson 			break;
3058a266c7d5SChris Wilson 
3059a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3060a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3061a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3062a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3063b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3064a266c7d5SChris Wilson 
3065a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3066a266c7d5SChris Wilson 				  hotplug_status);
3067b543fb04SEgbert Eich 			if (hotplug_trigger) {
3068cd569aedSEgbert Eich 				if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_status_i915))
3069cd569aedSEgbert Eich 					i915_hpd_irq_setup(dev);
3070a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
3071a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
3072b543fb04SEgbert Eich 			}
3073a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
307438bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3075a266c7d5SChris Wilson 		}
3076a266c7d5SChris Wilson 
307738bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3078a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3079a266c7d5SChris Wilson 
3080a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3081a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3082a266c7d5SChris Wilson 
3083a266c7d5SChris Wilson 		for_each_pipe(pipe) {
308438bde180SChris Wilson 			int plane = pipe;
308538bde180SChris Wilson 			if (IS_MOBILE(dev))
308638bde180SChris Wilson 				plane = !plane;
30875e2032d4SVille Syrjälä 
308890a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
308990a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
309090a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3091a266c7d5SChris Wilson 
3092a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3093a266c7d5SChris Wilson 				blc_event = true;
3094a266c7d5SChris Wilson 		}
3095a266c7d5SChris Wilson 
3096a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3097a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3098a266c7d5SChris Wilson 
3099a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3100a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3101a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3102a266c7d5SChris Wilson 		 * we would never get another interrupt.
3103a266c7d5SChris Wilson 		 *
3104a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3105a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3106a266c7d5SChris Wilson 		 * another one.
3107a266c7d5SChris Wilson 		 *
3108a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3109a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3110a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3111a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3112a266c7d5SChris Wilson 		 * stray interrupts.
3113a266c7d5SChris Wilson 		 */
311438bde180SChris Wilson 		ret = IRQ_HANDLED;
3115a266c7d5SChris Wilson 		iir = new_iir;
311638bde180SChris Wilson 	} while (iir & ~flip_mask);
3117a266c7d5SChris Wilson 
3118d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
31198291ee90SChris Wilson 
3120a266c7d5SChris Wilson 	return ret;
3121a266c7d5SChris Wilson }
3122a266c7d5SChris Wilson 
3123a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3124a266c7d5SChris Wilson {
3125a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3126a266c7d5SChris Wilson 	int pipe;
3127a266c7d5SChris Wilson 
3128ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3129ac4c16c5SEgbert Eich 
3130a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3131a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3132a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3133a266c7d5SChris Wilson 	}
3134a266c7d5SChris Wilson 
313500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
313655b39755SChris Wilson 	for_each_pipe(pipe) {
313755b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3138a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
313955b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
314055b39755SChris Wilson 	}
3141a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3142a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3143a266c7d5SChris Wilson 
3144a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3145a266c7d5SChris Wilson }
3146a266c7d5SChris Wilson 
3147a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3148a266c7d5SChris Wilson {
3149a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3150a266c7d5SChris Wilson 	int pipe;
3151a266c7d5SChris Wilson 
3152a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3153a266c7d5SChris Wilson 
3154a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3155a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3156a266c7d5SChris Wilson 
3157a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3158a266c7d5SChris Wilson 	for_each_pipe(pipe)
3159a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3160a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3161a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3162a266c7d5SChris Wilson 	POSTING_READ(IER);
3163a266c7d5SChris Wilson }
3164a266c7d5SChris Wilson 
3165a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3166a266c7d5SChris Wilson {
3167a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3168bbba0a97SChris Wilson 	u32 enable_mask;
3169a266c7d5SChris Wilson 	u32 error_mask;
3170a266c7d5SChris Wilson 
3171a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3172bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3173adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3174bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3175bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3176bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3177bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3178bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3179bbba0a97SChris Wilson 
3180bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
318121ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
318221ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3183bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3184bbba0a97SChris Wilson 
3185bbba0a97SChris Wilson 	if (IS_G4X(dev))
3186bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3187a266c7d5SChris Wilson 
3188515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
3189a266c7d5SChris Wilson 
3190a266c7d5SChris Wilson 	/*
3191a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3192a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3193a266c7d5SChris Wilson 	 */
3194a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3195a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3196a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3197a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3198a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3199a266c7d5SChris Wilson 	} else {
3200a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3201a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3202a266c7d5SChris Wilson 	}
3203a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3204a266c7d5SChris Wilson 
3205a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3206a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3207a266c7d5SChris Wilson 	POSTING_READ(IER);
3208a266c7d5SChris Wilson 
320920afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
321020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
321120afbda2SDaniel Vetter 
3212f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
321320afbda2SDaniel Vetter 
321420afbda2SDaniel Vetter 	return 0;
321520afbda2SDaniel Vetter }
321620afbda2SDaniel Vetter 
3217bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
321820afbda2SDaniel Vetter {
321920afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3220e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3221cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
322220afbda2SDaniel Vetter 	u32 hotplug_en;
322320afbda2SDaniel Vetter 
3224bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3225bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3226bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3227adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3228e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3229cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3230cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3231cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3232a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3233a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3234a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3235a266c7d5SChris Wilson 		*/
3236a266c7d5SChris Wilson 		if (IS_G4X(dev))
3237a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
323885fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3239a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3240a266c7d5SChris Wilson 
3241a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3242a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3243a266c7d5SChris Wilson 	}
3244bac56d5bSEgbert Eich }
3245a266c7d5SChris Wilson 
3246ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3247a266c7d5SChris Wilson {
3248a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3249a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3250a266c7d5SChris Wilson 	u32 iir, new_iir;
3251a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3252a266c7d5SChris Wilson 	unsigned long irqflags;
3253a266c7d5SChris Wilson 	int irq_received;
3254a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
325521ad8330SVille Syrjälä 	u32 flip_mask =
325621ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
325721ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3258a266c7d5SChris Wilson 
3259a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3260a266c7d5SChris Wilson 
3261a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3262a266c7d5SChris Wilson 
3263a266c7d5SChris Wilson 	for (;;) {
32642c8ba29fSChris Wilson 		bool blc_event = false;
32652c8ba29fSChris Wilson 
326621ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3267a266c7d5SChris Wilson 
3268a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3269a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3270a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3271a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3272a266c7d5SChris Wilson 		 */
3273a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3274a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3275a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3276a266c7d5SChris Wilson 
3277a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3278a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3279a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3280a266c7d5SChris Wilson 
3281a266c7d5SChris Wilson 			/*
3282a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3283a266c7d5SChris Wilson 			 */
3284a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3285a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3286a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3287a266c7d5SChris Wilson 							 pipe_name(pipe));
3288a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3289a266c7d5SChris Wilson 				irq_received = 1;
3290a266c7d5SChris Wilson 			}
3291a266c7d5SChris Wilson 		}
3292a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3293a266c7d5SChris Wilson 
3294a266c7d5SChris Wilson 		if (!irq_received)
3295a266c7d5SChris Wilson 			break;
3296a266c7d5SChris Wilson 
3297a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3298a266c7d5SChris Wilson 
3299a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3300adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3301a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3302b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3303b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
3304b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_I965);
3305a266c7d5SChris Wilson 
3306a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3307a266c7d5SChris Wilson 				  hotplug_status);
3308b543fb04SEgbert Eich 			if (hotplug_trigger) {
3309cd569aedSEgbert Eich 				if (hotplug_irq_storm_detect(dev, hotplug_trigger,
3310cd569aedSEgbert Eich 							    IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i965))
3311cd569aedSEgbert Eich 					i915_hpd_irq_setup(dev);
3312a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
3313a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
3314b543fb04SEgbert Eich 			}
3315a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3316a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3317a266c7d5SChris Wilson 		}
3318a266c7d5SChris Wilson 
331921ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3320a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3321a266c7d5SChris Wilson 
3322a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3323a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3324a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3325a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3326a266c7d5SChris Wilson 
3327a266c7d5SChris Wilson 		for_each_pipe(pipe) {
33282c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
332990a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
333090a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3331a266c7d5SChris Wilson 
3332a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3333a266c7d5SChris Wilson 				blc_event = true;
3334a266c7d5SChris Wilson 		}
3335a266c7d5SChris Wilson 
3336a266c7d5SChris Wilson 
3337a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3338a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3339a266c7d5SChris Wilson 
3340515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3341515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3342515ac2bbSDaniel Vetter 
3343a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3344a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3345a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3346a266c7d5SChris Wilson 		 * we would never get another interrupt.
3347a266c7d5SChris Wilson 		 *
3348a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3349a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3350a266c7d5SChris Wilson 		 * another one.
3351a266c7d5SChris Wilson 		 *
3352a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3353a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3354a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3355a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3356a266c7d5SChris Wilson 		 * stray interrupts.
3357a266c7d5SChris Wilson 		 */
3358a266c7d5SChris Wilson 		iir = new_iir;
3359a266c7d5SChris Wilson 	}
3360a266c7d5SChris Wilson 
3361d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
33622c8ba29fSChris Wilson 
3363a266c7d5SChris Wilson 	return ret;
3364a266c7d5SChris Wilson }
3365a266c7d5SChris Wilson 
3366a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3367a266c7d5SChris Wilson {
3368a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3369a266c7d5SChris Wilson 	int pipe;
3370a266c7d5SChris Wilson 
3371a266c7d5SChris Wilson 	if (!dev_priv)
3372a266c7d5SChris Wilson 		return;
3373a266c7d5SChris Wilson 
3374ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3375ac4c16c5SEgbert Eich 
3376a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3377a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3378a266c7d5SChris Wilson 
3379a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3380a266c7d5SChris Wilson 	for_each_pipe(pipe)
3381a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3382a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3383a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3384a266c7d5SChris Wilson 
3385a266c7d5SChris Wilson 	for_each_pipe(pipe)
3386a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3387a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3388a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3389a266c7d5SChris Wilson }
3390a266c7d5SChris Wilson 
3391ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3392ac4c16c5SEgbert Eich {
3393ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3394ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3395ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3396ac4c16c5SEgbert Eich 	unsigned long irqflags;
3397ac4c16c5SEgbert Eich 	int i;
3398ac4c16c5SEgbert Eich 
3399ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3400ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3401ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3402ac4c16c5SEgbert Eich 
3403ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3404ac4c16c5SEgbert Eich 			continue;
3405ac4c16c5SEgbert Eich 
3406ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3407ac4c16c5SEgbert Eich 
3408ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3409ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3410ac4c16c5SEgbert Eich 
3411ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3412ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3413ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3414ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3415ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3416ac4c16c5SEgbert Eich 				if (!connector->polled)
3417ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3418ac4c16c5SEgbert Eich 			}
3419ac4c16c5SEgbert Eich 		}
3420ac4c16c5SEgbert Eich 	}
3421ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3422ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3423ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3424ac4c16c5SEgbert Eich }
3425ac4c16c5SEgbert Eich 
3426f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3427f71d4af4SJesse Barnes {
34288b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
34298b2e326dSChris Wilson 
34308b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
343199584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3432c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3433a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
34348b2e326dSChris Wilson 
343599584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
343699584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
343761bac78eSDaniel Vetter 		    (unsigned long) dev);
3438ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3439ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
344061bac78eSDaniel Vetter 
344197a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
34429ee32feaSDaniel Vetter 
3443f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
3444f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
34457d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3446f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3447f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3448f71d4af4SJesse Barnes 	}
3449f71d4af4SJesse Barnes 
3450c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3451f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3452c3613de9SKeith Packard 	else
3453c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
3454f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3455f71d4af4SJesse Barnes 
34567e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
34577e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
34587e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
34597e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
34607e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
34617e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
34627e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3463fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
34644a06e201SDaniel Vetter 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
3465f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
3466f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
3467f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3468f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
3469f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3470f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
3471f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
347282a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3473f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3474f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3475f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3476f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3477f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3478f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3479f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
348082a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3481f71d4af4SJesse Barnes 	} else {
3482c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3483c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3484c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3485c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3486c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3487a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3488a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3489a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3490a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3491a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
349220afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3493c2798b19SChris Wilson 		} else {
3494a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3495a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3496a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3497a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3498bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3499c2798b19SChris Wilson 		}
3500f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3501f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3502f71d4af4SJesse Barnes 	}
3503f71d4af4SJesse Barnes }
350420afbda2SDaniel Vetter 
350520afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
350620afbda2SDaniel Vetter {
350720afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3508821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3509821450c6SEgbert Eich 	struct drm_connector *connector;
3510821450c6SEgbert Eich 	int i;
351120afbda2SDaniel Vetter 
3512821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3513821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3514821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3515821450c6SEgbert Eich 	}
3516821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3517821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3518821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3519821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3520821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3521821450c6SEgbert Eich 	}
352220afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
352320afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
352420afbda2SDaniel Vetter }
3525