xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 81a078092ed25b1017d1351c68837b750a09933a)
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 /**
35201c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
35301c66889SZhao Yakui  */
35401c66889SZhao Yakui void intel_enable_asle(struct drm_device *dev)
35501c66889SZhao Yakui {
3561ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
3571ec14ad3SChris Wilson 	unsigned long irqflags;
3581ec14ad3SChris Wilson 
3597e231dbeSJesse Barnes 	/* FIXME: opregion/asle for VLV */
3607e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev))
3617e231dbeSJesse Barnes 		return;
3627e231dbeSJesse Barnes 
3631ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
36401c66889SZhao Yakui 
365c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
366f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
367edcb49caSZhao Yakui 	else {
36801c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
369d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
370a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen >= 4)
371edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
372d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
373edcb49caSZhao Yakui 	}
3741ec14ad3SChris Wilson 
3751ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
37601c66889SZhao Yakui }
37701c66889SZhao Yakui 
37801c66889SZhao Yakui /**
3790a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
3800a3e67a4SJesse Barnes  * @dev: DRM device
3810a3e67a4SJesse Barnes  * @pipe: pipe to check
3820a3e67a4SJesse Barnes  *
3830a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
3840a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
3850a3e67a4SJesse Barnes  * before reading such registers if unsure.
3860a3e67a4SJesse Barnes  */
3870a3e67a4SJesse Barnes static int
3880a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
3890a3e67a4SJesse Barnes {
3900a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
391702e7a56SPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
392702e7a56SPaulo Zanoni 								      pipe);
393702e7a56SPaulo Zanoni 
394702e7a56SPaulo Zanoni 	return I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_ENABLE;
3950a3e67a4SJesse Barnes }
3960a3e67a4SJesse Barnes 
39742f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
39842f52ef8SKeith Packard  * we use as a pipe index
39942f52ef8SKeith Packard  */
400f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
4010a3e67a4SJesse Barnes {
4020a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4030a3e67a4SJesse Barnes 	unsigned long high_frame;
4040a3e67a4SJesse Barnes 	unsigned long low_frame;
4055eddb70bSChris Wilson 	u32 high1, high2, low;
4060a3e67a4SJesse Barnes 
4070a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
40844d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4099db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
4100a3e67a4SJesse Barnes 		return 0;
4110a3e67a4SJesse Barnes 	}
4120a3e67a4SJesse Barnes 
4139db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
4149db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
4155eddb70bSChris Wilson 
4160a3e67a4SJesse Barnes 	/*
4170a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
4180a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
4190a3e67a4SJesse Barnes 	 * register.
4200a3e67a4SJesse Barnes 	 */
4210a3e67a4SJesse Barnes 	do {
4225eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4235eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
4245eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4250a3e67a4SJesse Barnes 	} while (high1 != high2);
4260a3e67a4SJesse Barnes 
4275eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
4285eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
4295eddb70bSChris Wilson 	return (high1 << 8) | low;
4300a3e67a4SJesse Barnes }
4310a3e67a4SJesse Barnes 
432f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
4339880b7a5SJesse Barnes {
4349880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4359db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
4369880b7a5SJesse Barnes 
4379880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
43844d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4399db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4409880b7a5SJesse Barnes 		return 0;
4419880b7a5SJesse Barnes 	}
4429880b7a5SJesse Barnes 
4439880b7a5SJesse Barnes 	return I915_READ(reg);
4449880b7a5SJesse Barnes }
4459880b7a5SJesse Barnes 
446f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
4470af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
4480af7e4dfSMario Kleiner {
4490af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4500af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
4510af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
4520af7e4dfSMario Kleiner 	bool in_vbl = true;
4530af7e4dfSMario Kleiner 	int ret = 0;
454fe2b8f9dSPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
455fe2b8f9dSPaulo Zanoni 								      pipe);
4560af7e4dfSMario Kleiner 
4570af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
4580af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
4599db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4600af7e4dfSMario Kleiner 		return 0;
4610af7e4dfSMario Kleiner 	}
4620af7e4dfSMario Kleiner 
4630af7e4dfSMario Kleiner 	/* Get vtotal. */
464fe2b8f9dSPaulo Zanoni 	vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4650af7e4dfSMario Kleiner 
4660af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
4670af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
4680af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
4690af7e4dfSMario Kleiner 		 */
4700af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
4710af7e4dfSMario Kleiner 
4720af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
4730af7e4dfSMario Kleiner 		 * horizontal scanout position.
4740af7e4dfSMario Kleiner 		 */
4750af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
4760af7e4dfSMario Kleiner 		*hpos = 0;
4770af7e4dfSMario Kleiner 	} else {
4780af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
4790af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
4800af7e4dfSMario Kleiner 		 * scanout position.
4810af7e4dfSMario Kleiner 		 */
4820af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
4830af7e4dfSMario Kleiner 
484fe2b8f9dSPaulo Zanoni 		htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4850af7e4dfSMario Kleiner 		*vpos = position / htotal;
4860af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
4870af7e4dfSMario Kleiner 	}
4880af7e4dfSMario Kleiner 
4890af7e4dfSMario Kleiner 	/* Query vblank area. */
490fe2b8f9dSPaulo Zanoni 	vbl = I915_READ(VBLANK(cpu_transcoder));
4910af7e4dfSMario Kleiner 
4920af7e4dfSMario Kleiner 	/* Test position against vblank region. */
4930af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
4940af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
4950af7e4dfSMario Kleiner 
4960af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
4970af7e4dfSMario Kleiner 		in_vbl = false;
4980af7e4dfSMario Kleiner 
4990af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
5000af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
5010af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
5020af7e4dfSMario Kleiner 
5030af7e4dfSMario Kleiner 	/* Readouts valid? */
5040af7e4dfSMario Kleiner 	if (vbl > 0)
5050af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
5060af7e4dfSMario Kleiner 
5070af7e4dfSMario Kleiner 	/* In vblank? */
5080af7e4dfSMario Kleiner 	if (in_vbl)
5090af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
5100af7e4dfSMario Kleiner 
5110af7e4dfSMario Kleiner 	return ret;
5120af7e4dfSMario Kleiner }
5130af7e4dfSMario Kleiner 
514f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
5150af7e4dfSMario Kleiner 			      int *max_error,
5160af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
5170af7e4dfSMario Kleiner 			      unsigned flags)
5180af7e4dfSMario Kleiner {
5194041b853SChris Wilson 	struct drm_crtc *crtc;
5200af7e4dfSMario Kleiner 
5217eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
5224041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5230af7e4dfSMario Kleiner 		return -EINVAL;
5240af7e4dfSMario Kleiner 	}
5250af7e4dfSMario Kleiner 
5260af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
5274041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
5284041b853SChris Wilson 	if (crtc == NULL) {
5294041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5304041b853SChris Wilson 		return -EINVAL;
5314041b853SChris Wilson 	}
5324041b853SChris Wilson 
5334041b853SChris Wilson 	if (!crtc->enabled) {
5344041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
5354041b853SChris Wilson 		return -EBUSY;
5364041b853SChris Wilson 	}
5370af7e4dfSMario Kleiner 
5380af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
5394041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
5404041b853SChris Wilson 						     vblank_time, flags,
5414041b853SChris Wilson 						     crtc);
5420af7e4dfSMario Kleiner }
5430af7e4dfSMario Kleiner 
544321a1b30SEgbert Eich static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
545321a1b30SEgbert Eich {
546321a1b30SEgbert Eich 	enum drm_connector_status old_status;
547321a1b30SEgbert Eich 
548321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
549321a1b30SEgbert Eich 	old_status = connector->status;
550321a1b30SEgbert Eich 
551321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
552321a1b30SEgbert Eich 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
553321a1b30SEgbert Eich 		      connector->base.id,
554321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
555321a1b30SEgbert Eich 		      old_status, connector->status);
556321a1b30SEgbert Eich 	return (old_status != connector->status);
557321a1b30SEgbert Eich }
558321a1b30SEgbert Eich 
5595ca58282SJesse Barnes /*
5605ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
5615ca58282SJesse Barnes  */
562ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
563ac4c16c5SEgbert Eich 
5645ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
5655ca58282SJesse Barnes {
5665ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5675ca58282SJesse Barnes 						    hotplug_work);
5685ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
569c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
570cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
571cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
572cd569aedSEgbert Eich 	struct drm_connector *connector;
573cd569aedSEgbert Eich 	unsigned long irqflags;
574cd569aedSEgbert Eich 	bool hpd_disabled = false;
575321a1b30SEgbert Eich 	bool changed = false;
576142e2398SEgbert Eich 	u32 hpd_event_bits;
5775ca58282SJesse Barnes 
57852d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
57952d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
58052d7ecedSDaniel Vetter 		return;
58152d7ecedSDaniel Vetter 
582a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
583e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
584e67189abSJesse Barnes 
585cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
586142e2398SEgbert Eich 
587142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
588142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
589cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
590cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
591cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
592cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
593cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
594cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
595cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
596cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
597cd569aedSEgbert Eich 				drm_get_connector_name(connector));
598cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
599cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
600cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
601cd569aedSEgbert Eich 			hpd_disabled = true;
602cd569aedSEgbert Eich 		}
603142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
604142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
605142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
606142e2398SEgbert Eich 		}
607cd569aedSEgbert Eich 	}
608cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
609cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
610cd569aedSEgbert Eich 	  * some connectors */
611ac4c16c5SEgbert Eich 	if (hpd_disabled) {
612cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
613ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
614ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
615ac4c16c5SEgbert Eich 	}
616cd569aedSEgbert Eich 
617cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
618cd569aedSEgbert Eich 
619321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
620321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
621321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
622321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
623cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
624cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
625321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
626321a1b30SEgbert Eich 				changed = true;
627321a1b30SEgbert Eich 		}
628321a1b30SEgbert Eich 	}
62940ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
63040ee3381SKeith Packard 
631321a1b30SEgbert Eich 	if (changed)
632321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
6335ca58282SJesse Barnes }
6345ca58282SJesse Barnes 
63573edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
636f97108d1SJesse Barnes {
637f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
638b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
6399270388eSDaniel Vetter 	u8 new_delay;
6409270388eSDaniel Vetter 	unsigned long flags;
6419270388eSDaniel Vetter 
6429270388eSDaniel Vetter 	spin_lock_irqsave(&mchdev_lock, flags);
643f97108d1SJesse Barnes 
64473edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
64573edd18fSDaniel Vetter 
64620e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
6479270388eSDaniel Vetter 
6487648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
649b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
650b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
651f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
652f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
653f97108d1SJesse Barnes 
654f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
655b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
65620e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
65720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
65820e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
65920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
660b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
66120e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
66220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
66320e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
66420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
665f97108d1SJesse Barnes 	}
666f97108d1SJesse Barnes 
6677648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
66820e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
669f97108d1SJesse Barnes 
6709270388eSDaniel Vetter 	spin_unlock_irqrestore(&mchdev_lock, flags);
6719270388eSDaniel Vetter 
672f97108d1SJesse Barnes 	return;
673f97108d1SJesse Barnes }
674f97108d1SJesse Barnes 
675549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
676549f7365SChris Wilson 			struct intel_ring_buffer *ring)
677549f7365SChris Wilson {
678549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
6799862e600SChris Wilson 
680475553deSChris Wilson 	if (ring->obj == NULL)
681475553deSChris Wilson 		return;
682475553deSChris Wilson 
683b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
6849862e600SChris Wilson 
685549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
6863e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
68799584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
68899584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
689cecc21feSChris Wilson 			  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
6903e0dc6b0SBen Widawsky 	}
691549f7365SChris Wilson }
692549f7365SChris Wilson 
6934912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
6943b8d8d91SJesse Barnes {
6954912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
696c6a828d3SDaniel Vetter 						    rps.work);
6974912d041SBen Widawsky 	u32 pm_iir, pm_imr;
6987b9e0ae6SChris Wilson 	u8 new_delay;
6993b8d8d91SJesse Barnes 
700c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
701c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
702c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
7034912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
704a9e2641dSDaniel Vetter 	I915_WRITE(GEN6_PMIMR, 0);
705c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
7064912d041SBen Widawsky 
7077b9e0ae6SChris Wilson 	if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
7083b8d8d91SJesse Barnes 		return;
7093b8d8d91SJesse Barnes 
7104fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
7117b9e0ae6SChris Wilson 
7127b9e0ae6SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
713c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
7147b9e0ae6SChris Wilson 	else
715c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
7163b8d8d91SJesse Barnes 
71779249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
71879249636SBen Widawsky 	 * interrupt
71979249636SBen Widawsky 	 */
72079249636SBen Widawsky 	if (!(new_delay > dev_priv->rps.max_delay ||
72179249636SBen Widawsky 	      new_delay < dev_priv->rps.min_delay)) {
7220a073b84SJesse Barnes 		if (IS_VALLEYVIEW(dev_priv->dev))
7230a073b84SJesse Barnes 			valleyview_set_rps(dev_priv->dev, new_delay);
7240a073b84SJesse Barnes 		else
7254912d041SBen Widawsky 			gen6_set_rps(dev_priv->dev, new_delay);
72679249636SBen Widawsky 	}
7273b8d8d91SJesse Barnes 
72852ceb908SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev)) {
72952ceb908SJesse Barnes 		/*
73052ceb908SJesse Barnes 		 * On VLV, when we enter RC6 we may not be at the minimum
73152ceb908SJesse Barnes 		 * voltage level, so arm a timer to check.  It should only
73252ceb908SJesse Barnes 		 * fire when there's activity or once after we've entered
73352ceb908SJesse Barnes 		 * RC6, and then won't be re-armed until the next RPS interrupt.
73452ceb908SJesse Barnes 		 */
73552ceb908SJesse Barnes 		mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
73652ceb908SJesse Barnes 				 msecs_to_jiffies(100));
73752ceb908SJesse Barnes 	}
73852ceb908SJesse Barnes 
7394fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
7403b8d8d91SJesse Barnes }
7413b8d8d91SJesse Barnes 
742e3689190SBen Widawsky 
743e3689190SBen Widawsky /**
744e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
745e3689190SBen Widawsky  * occurred.
746e3689190SBen Widawsky  * @work: workqueue struct
747e3689190SBen Widawsky  *
748e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
749e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
750e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
751e3689190SBen Widawsky  */
752e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
753e3689190SBen Widawsky {
754e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
755a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
756e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
757e3689190SBen Widawsky 	char *parity_event[5];
758e3689190SBen Widawsky 	uint32_t misccpctl;
759e3689190SBen Widawsky 	unsigned long flags;
760e3689190SBen Widawsky 
761e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
762e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
763e3689190SBen Widawsky 	 * any time we access those registers.
764e3689190SBen Widawsky 	 */
765e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
766e3689190SBen Widawsky 
767e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
768e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
769e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
770e3689190SBen Widawsky 
771e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
772e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
773e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
774e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
775e3689190SBen Widawsky 
776e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
777e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
778e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
779e3689190SBen Widawsky 
780e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
781e3689190SBen Widawsky 
782e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
783e3689190SBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
784e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
785e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
786e3689190SBen Widawsky 
787e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
788e3689190SBen Widawsky 
789e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
790e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
791e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
792e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
793e3689190SBen Widawsky 	parity_event[4] = NULL;
794e3689190SBen Widawsky 
795e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
796e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
797e3689190SBen Widawsky 
798e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
799e3689190SBen Widawsky 		  row, bank, subbank);
800e3689190SBen Widawsky 
801e3689190SBen Widawsky 	kfree(parity_event[3]);
802e3689190SBen Widawsky 	kfree(parity_event[2]);
803e3689190SBen Widawsky 	kfree(parity_event[1]);
804e3689190SBen Widawsky }
805e3689190SBen Widawsky 
806d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
807e3689190SBen Widawsky {
808e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
809e3689190SBen Widawsky 	unsigned long flags;
810e3689190SBen Widawsky 
811e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
812e3689190SBen Widawsky 		return;
813e3689190SBen Widawsky 
814e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
815e3689190SBen Widawsky 	dev_priv->gt_irq_mask |= GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
816e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
817e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
818e3689190SBen Widawsky 
819a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
820e3689190SBen Widawsky }
821e3689190SBen Widawsky 
822e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
823e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
824e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
825e7b4c6b1SDaniel Vetter {
826e7b4c6b1SDaniel Vetter 
827e7b4c6b1SDaniel Vetter 	if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
828e7b4c6b1SDaniel Vetter 		      GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
829e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
830e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BSD_USER_INTERRUPT)
831e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
832e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
833e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
834e7b4c6b1SDaniel Vetter 
835e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
836e7b4c6b1SDaniel Vetter 		      GT_GEN6_BSD_CS_ERROR_INTERRUPT |
837e7b4c6b1SDaniel Vetter 		      GT_RENDER_CS_ERROR_INTERRUPT)) {
838e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
839e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
840e7b4c6b1SDaniel Vetter 	}
841e3689190SBen Widawsky 
842e3689190SBen Widawsky 	if (gt_iir & GT_GEN7_L3_PARITY_ERROR_INTERRUPT)
843e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
844e7b4c6b1SDaniel Vetter }
845e7b4c6b1SDaniel Vetter 
846fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
847fc6826d1SChris Wilson 				u32 pm_iir)
848fc6826d1SChris Wilson {
849fc6826d1SChris Wilson 	unsigned long flags;
850fc6826d1SChris Wilson 
851fc6826d1SChris Wilson 	/*
852fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
853fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
854fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
855c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
856fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
857fc6826d1SChris Wilson 	 *
858c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
859fc6826d1SChris Wilson 	 */
860fc6826d1SChris Wilson 
861c6a828d3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
862c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
863c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
864fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
865c6a828d3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
866fc6826d1SChris Wilson 
867c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
868fc6826d1SChris Wilson }
869fc6826d1SChris Wilson 
870b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
871b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
872b543fb04SEgbert Eich 
873cd569aedSEgbert Eich static inline bool hotplug_irq_storm_detect(struct drm_device *dev,
874b543fb04SEgbert Eich 					    u32 hotplug_trigger,
875b543fb04SEgbert Eich 					    const u32 *hpd)
876b543fb04SEgbert Eich {
877b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
878b543fb04SEgbert Eich 	unsigned long irqflags;
879b543fb04SEgbert Eich 	int i;
880cd569aedSEgbert Eich 	bool ret = false;
881b543fb04SEgbert Eich 
882b543fb04SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
883b543fb04SEgbert Eich 
884b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
885821450c6SEgbert Eich 
886b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
887b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
888142e2398SEgbert Eich 			dev_priv->hpd_event_bits |= (1 << i);
889b543fb04SEgbert Eich 			continue;
890b543fb04SEgbert Eich 
891b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
892b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
893b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
894b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
895b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
896b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
897b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
898142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
899b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
900cd569aedSEgbert Eich 			ret = true;
901b543fb04SEgbert Eich 		} else {
902b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
903b543fb04SEgbert Eich 		}
904b543fb04SEgbert Eich 	}
905b543fb04SEgbert Eich 
906b543fb04SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
907cd569aedSEgbert Eich 
908cd569aedSEgbert Eich 	return ret;
909b543fb04SEgbert Eich }
910b543fb04SEgbert Eich 
911515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
912515ac2bbSDaniel Vetter {
91328c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
91428c70f16SDaniel Vetter 
91528c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
916515ac2bbSDaniel Vetter }
917515ac2bbSDaniel Vetter 
918ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
919ce99c256SDaniel Vetter {
9209ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
9219ee32feaSDaniel Vetter 
9229ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
923ce99c256SDaniel Vetter }
924ce99c256SDaniel Vetter 
925ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
9267e231dbeSJesse Barnes {
9277e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
9287e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
9297e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
9307e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
9317e231dbeSJesse Barnes 	unsigned long irqflags;
9327e231dbeSJesse Barnes 	int pipe;
9337e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
9347e231dbeSJesse Barnes 
9357e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
9367e231dbeSJesse Barnes 
9377e231dbeSJesse Barnes 	while (true) {
9387e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
9397e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
9407e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
9417e231dbeSJesse Barnes 
9427e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
9437e231dbeSJesse Barnes 			goto out;
9447e231dbeSJesse Barnes 
9457e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
9467e231dbeSJesse Barnes 
947e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
9487e231dbeSJesse Barnes 
9497e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
9507e231dbeSJesse Barnes 		for_each_pipe(pipe) {
9517e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
9527e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
9537e231dbeSJesse Barnes 
9547e231dbeSJesse Barnes 			/*
9557e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
9567e231dbeSJesse Barnes 			 */
9577e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
9587e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
9597e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
9607e231dbeSJesse Barnes 							 pipe_name(pipe));
9617e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
9627e231dbeSJesse Barnes 			}
9637e231dbeSJesse Barnes 		}
9647e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
9657e231dbeSJesse Barnes 
96631acc7f5SJesse Barnes 		for_each_pipe(pipe) {
96731acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
96831acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
96931acc7f5SJesse Barnes 
97031acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
97131acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
97231acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
97331acc7f5SJesse Barnes 			}
97431acc7f5SJesse Barnes 		}
97531acc7f5SJesse Barnes 
9767e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
9777e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
9787e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
979b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
9807e231dbeSJesse Barnes 
9817e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
9827e231dbeSJesse Barnes 					 hotplug_status);
983b543fb04SEgbert Eich 			if (hotplug_trigger) {
984cd569aedSEgbert Eich 				if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_status_i915))
985cd569aedSEgbert Eich 					i915_hpd_irq_setup(dev);
9867e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
9877e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
988b543fb04SEgbert Eich 			}
9897e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
9907e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
9917e231dbeSJesse Barnes 		}
9927e231dbeSJesse Barnes 
993515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
994515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
9957e231dbeSJesse Barnes 
996fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
997fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
9987e231dbeSJesse Barnes 
9997e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
10007e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
10017e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
10027e231dbeSJesse Barnes 	}
10037e231dbeSJesse Barnes 
10047e231dbeSJesse Barnes out:
10057e231dbeSJesse Barnes 	return ret;
10067e231dbeSJesse Barnes }
10077e231dbeSJesse Barnes 
100823e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1009776ad806SJesse Barnes {
1010776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10119db4a9c7SJesse Barnes 	int pipe;
1012b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1013776ad806SJesse Barnes 
1014b543fb04SEgbert Eich 	if (hotplug_trigger) {
1015cd569aedSEgbert Eich 		if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_ibx))
1016cd569aedSEgbert Eich 			ibx_hpd_irq_setup(dev);
101776e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
1018b543fb04SEgbert Eich 	}
1019cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1020cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1021776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1022cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1023cfc33bf7SVille Syrjälä 				 port_name(port));
1024cfc33bf7SVille Syrjälä 	}
1025776ad806SJesse Barnes 
1026ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1027ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1028ce99c256SDaniel Vetter 
1029776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1030515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1031776ad806SJesse Barnes 
1032776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1033776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1034776ad806SJesse Barnes 
1035776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1036776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1037776ad806SJesse Barnes 
1038776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1039776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1040776ad806SJesse Barnes 
10419db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
10429db4a9c7SJesse Barnes 		for_each_pipe(pipe)
10439db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
10449db4a9c7SJesse Barnes 					 pipe_name(pipe),
10459db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1046776ad806SJesse Barnes 
1047776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1048776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1049776ad806SJesse Barnes 
1050776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1051776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1052776ad806SJesse Barnes 
1053776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
10548664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
10558664281bSPaulo Zanoni 							  false))
10568664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
10578664281bSPaulo Zanoni 
10588664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
10598664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
10608664281bSPaulo Zanoni 							  false))
10618664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
10628664281bSPaulo Zanoni }
10638664281bSPaulo Zanoni 
10648664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
10658664281bSPaulo Zanoni {
10668664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
10678664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
10688664281bSPaulo Zanoni 
1069de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1070de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1071de032bf4SPaulo Zanoni 
10728664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_A)
10738664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
10748664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
10758664281bSPaulo Zanoni 
10768664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_B)
10778664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
10788664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
10798664281bSPaulo Zanoni 
10808664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_C)
10818664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
10828664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
10838664281bSPaulo Zanoni 
10848664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
10858664281bSPaulo Zanoni }
10868664281bSPaulo Zanoni 
10878664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
10888664281bSPaulo Zanoni {
10898664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
10908664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
10918664281bSPaulo Zanoni 
1092de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1093de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1094de032bf4SPaulo Zanoni 
10958664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
10968664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
10978664281bSPaulo Zanoni 							  false))
10988664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
10998664281bSPaulo Zanoni 
11008664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
11018664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
11028664281bSPaulo Zanoni 							  false))
11038664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
11048664281bSPaulo Zanoni 
11058664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
11068664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
11078664281bSPaulo Zanoni 							  false))
11088664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
11098664281bSPaulo Zanoni 
11108664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1111776ad806SJesse Barnes }
1112776ad806SJesse Barnes 
111323e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
111423e81d69SAdam Jackson {
111523e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
111623e81d69SAdam Jackson 	int pipe;
1117b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
111823e81d69SAdam Jackson 
1119b543fb04SEgbert Eich 	if (hotplug_trigger) {
1120cd569aedSEgbert Eich 		if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_cpt))
1121cd569aedSEgbert Eich 			ibx_hpd_irq_setup(dev);
112276e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
1123b543fb04SEgbert Eich 	}
1124cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1125cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
112623e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1127cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1128cfc33bf7SVille Syrjälä 				 port_name(port));
1129cfc33bf7SVille Syrjälä 	}
113023e81d69SAdam Jackson 
113123e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1132ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
113323e81d69SAdam Jackson 
113423e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1135515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
113623e81d69SAdam Jackson 
113723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
113823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
113923e81d69SAdam Jackson 
114023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
114123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
114223e81d69SAdam Jackson 
114323e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
114423e81d69SAdam Jackson 		for_each_pipe(pipe)
114523e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
114623e81d69SAdam Jackson 					 pipe_name(pipe),
114723e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
11488664281bSPaulo Zanoni 
11498664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
11508664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
115123e81d69SAdam Jackson }
115223e81d69SAdam Jackson 
1153ff1f525eSDaniel Vetter static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
1154b1f14ad0SJesse Barnes {
1155b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1156b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1157ab5c608bSBen Widawsky 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
11580e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
11590e43406bSChris Wilson 	int i;
1160b1f14ad0SJesse Barnes 
1161b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1162b1f14ad0SJesse Barnes 
11638664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
11648664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
11658664281bSPaulo Zanoni 	if (IS_HASWELL(dev) &&
11668664281bSPaulo Zanoni 	    (I915_READ_NOTRACE(FPGA_DBG) & FPGA_DBG_RM_NOCLAIM)) {
11678664281bSPaulo Zanoni 		DRM_ERROR("Unclaimed register before interrupt\n");
11688664281bSPaulo Zanoni 		I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
11698664281bSPaulo Zanoni 	}
11708664281bSPaulo Zanoni 
1171b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1172b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1173b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
11740e43406bSChris Wilson 
117544498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
117644498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
117744498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
117844498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
117944498aeaSPaulo Zanoni 	 * due to its back queue). */
1180ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
118144498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
118244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
118344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1184ab5c608bSBen Widawsky 	}
118544498aeaSPaulo Zanoni 
11868664281bSPaulo Zanoni 	/* On Haswell, also mask ERR_INT because we don't want to risk
11878664281bSPaulo Zanoni 	 * generating "unclaimed register" interrupts from inside the interrupt
11888664281bSPaulo Zanoni 	 * handler. */
11898664281bSPaulo Zanoni 	if (IS_HASWELL(dev))
11908664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
11918664281bSPaulo Zanoni 
11920e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
11930e43406bSChris Wilson 	if (gt_iir) {
11940e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
11950e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
11960e43406bSChris Wilson 		ret = IRQ_HANDLED;
11970e43406bSChris Wilson 	}
1198b1f14ad0SJesse Barnes 
1199b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
12000e43406bSChris Wilson 	if (de_iir) {
12018664281bSPaulo Zanoni 		if (de_iir & DE_ERR_INT_IVB)
12028664281bSPaulo Zanoni 			ivb_err_int_handler(dev);
12038664281bSPaulo Zanoni 
1204ce99c256SDaniel Vetter 		if (de_iir & DE_AUX_CHANNEL_A_IVB)
1205ce99c256SDaniel Vetter 			dp_aux_irq_handler(dev);
1206ce99c256SDaniel Vetter 
1207b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
1208*81a07809SJani Nikula 			intel_opregion_asle_intr(dev);
1209b1f14ad0SJesse Barnes 
12100e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
121174d44445SDaniel Vetter 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
121274d44445SDaniel Vetter 				drm_handle_vblank(dev, i);
12130e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
12140e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
12150e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
1216b1f14ad0SJesse Barnes 			}
1217b1f14ad0SJesse Barnes 		}
1218b1f14ad0SJesse Barnes 
1219b1f14ad0SJesse Barnes 		/* check event from PCH */
1220ab5c608bSBen Widawsky 		if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
12210e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
12220e43406bSChris Wilson 
122323e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
12240e43406bSChris Wilson 
12250e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
12260e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
1227b1f14ad0SJesse Barnes 		}
1228b1f14ad0SJesse Barnes 
12290e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
12300e43406bSChris Wilson 		ret = IRQ_HANDLED;
12310e43406bSChris Wilson 	}
12320e43406bSChris Wilson 
12330e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
12340e43406bSChris Wilson 	if (pm_iir) {
1235fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
1236fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
1237b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
12380e43406bSChris Wilson 		ret = IRQ_HANDLED;
12390e43406bSChris Wilson 	}
1240b1f14ad0SJesse Barnes 
12418664281bSPaulo Zanoni 	if (IS_HASWELL(dev) && ivb_can_enable_err_int(dev))
12428664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
12438664281bSPaulo Zanoni 
1244b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1245b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1246ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
124744498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
124844498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1249ab5c608bSBen Widawsky 	}
1250b1f14ad0SJesse Barnes 
1251b1f14ad0SJesse Barnes 	return ret;
1252b1f14ad0SJesse Barnes }
1253b1f14ad0SJesse Barnes 
1254e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
1255e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1256e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1257e7b4c6b1SDaniel Vetter {
1258e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
1259e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1260e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1261e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1262e7b4c6b1SDaniel Vetter }
1263e7b4c6b1SDaniel Vetter 
1264ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1265036a4a7dSZhenyu Wang {
12664697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1267036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1268036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
126944498aeaSPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
1270881f47b6SXiang, Haihao 
12714697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
12724697995bSJesse Barnes 
12732d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
12742d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
12752d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
12763143a2bfSChris Wilson 	POSTING_READ(DEIER);
12772d109a84SZou, Nanhai 
127844498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
127944498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
128044498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
128144498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
128244498aeaSPaulo Zanoni 	 * due to its back queue). */
128344498aeaSPaulo Zanoni 	sde_ier = I915_READ(SDEIER);
128444498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, 0);
128544498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
128644498aeaSPaulo Zanoni 
1287036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
1288036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
12893b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
1290036a4a7dSZhenyu Wang 
1291acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
1292c7c85101SZou Nan hai 		goto done;
1293036a4a7dSZhenyu Wang 
1294036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
1295036a4a7dSZhenyu Wang 
1296e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
1297e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
1298e7b4c6b1SDaniel Vetter 	else
1299e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1300036a4a7dSZhenyu Wang 
1301ce99c256SDaniel Vetter 	if (de_iir & DE_AUX_CHANNEL_A)
1302ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1303ce99c256SDaniel Vetter 
130401c66889SZhao Yakui 	if (de_iir & DE_GSE)
1305*81a07809SJani Nikula 		intel_opregion_asle_intr(dev);
130601c66889SZhao Yakui 
130774d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
130874d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
130974d44445SDaniel Vetter 
131074d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
131174d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
131274d44445SDaniel Vetter 
1313de032bf4SPaulo Zanoni 	if (de_iir & DE_POISON)
1314de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1315de032bf4SPaulo Zanoni 
13168664281bSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
13178664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
13188664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
13198664281bSPaulo Zanoni 
13208664281bSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
13218664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
13228664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
13238664281bSPaulo Zanoni 
1324f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1325013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
13262bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
1327013d5aa2SJesse Barnes 	}
1328013d5aa2SJesse Barnes 
1329f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1330f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
13312bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
1332013d5aa2SJesse Barnes 	}
1333c062df61SLi Peng 
1334c650156aSZhenyu Wang 	/* check event from PCH */
1335776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
1336acd15b6cSDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
1337acd15b6cSDaniel Vetter 
133823e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
133923e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
134023e81d69SAdam Jackson 		else
134123e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
1342acd15b6cSDaniel Vetter 
1343acd15b6cSDaniel Vetter 		/* should clear PCH hotplug event before clear CPU irq */
1344acd15b6cSDaniel Vetter 		I915_WRITE(SDEIIR, pch_iir);
1345776ad806SJesse Barnes 	}
1346c650156aSZhenyu Wang 
134773edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
134873edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
1349f97108d1SJesse Barnes 
1350fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
1351fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
13523b8d8d91SJesse Barnes 
1353c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
1354c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
13554912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
1356036a4a7dSZhenyu Wang 
1357c7c85101SZou Nan hai done:
13582d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
13593143a2bfSChris Wilson 	POSTING_READ(DEIER);
136044498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, sde_ier);
136144498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
13622d109a84SZou, Nanhai 
1363036a4a7dSZhenyu Wang 	return ret;
1364036a4a7dSZhenyu Wang }
1365036a4a7dSZhenyu Wang 
13668a905236SJesse Barnes /**
13678a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
13688a905236SJesse Barnes  * @work: work struct
13698a905236SJesse Barnes  *
13708a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
13718a905236SJesse Barnes  * was detected.
13728a905236SJesse Barnes  */
13738a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
13748a905236SJesse Barnes {
13751f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
13761f83fee0SDaniel Vetter 						    work);
13771f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
13781f83fee0SDaniel Vetter 						    gpu_error);
13798a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1380f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
1381f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
1382f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
1383f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
1384f69061beSDaniel Vetter 	int i, ret;
13858a905236SJesse Barnes 
1386f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
13878a905236SJesse Barnes 
13887db0ba24SDaniel Vetter 	/*
13897db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
13907db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
13917db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
13927db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
13937db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
13947db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
13957db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
13967db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
13977db0ba24SDaniel Vetter 	 */
13987db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
139944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
14007db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
14017db0ba24SDaniel Vetter 				   reset_event);
14021f83fee0SDaniel Vetter 
1403f69061beSDaniel Vetter 		ret = i915_reset(dev);
1404f69061beSDaniel Vetter 
1405f69061beSDaniel Vetter 		if (ret == 0) {
1406f69061beSDaniel Vetter 			/*
1407f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1408f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1409f69061beSDaniel Vetter 			 * complete.
1410f69061beSDaniel Vetter 			 *
1411f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1412f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1413f69061beSDaniel Vetter 			 * updates before
1414f69061beSDaniel Vetter 			 * the counter increment.
1415f69061beSDaniel Vetter 			 */
1416f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1417f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1418f69061beSDaniel Vetter 
1419f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1420f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
14211f83fee0SDaniel Vetter 		} else {
14221f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1423f316a42cSBen Gamari 		}
14241f83fee0SDaniel Vetter 
1425f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
1426f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
1427f69061beSDaniel Vetter 
142896a02917SVille Syrjälä 		intel_display_handle_reset(dev);
142996a02917SVille Syrjälä 
14301f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
1431f316a42cSBen Gamari 	}
14328a905236SJesse Barnes }
14338a905236SJesse Barnes 
143485f9e50dSDaniel Vetter /* NB: please notice the memset */
143585f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
143685f9e50dSDaniel Vetter 				    uint32_t *instdone)
143785f9e50dSDaniel Vetter {
143885f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
143985f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
144085f9e50dSDaniel Vetter 
144185f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
144285f9e50dSDaniel Vetter 	case 2:
144385f9e50dSDaniel Vetter 	case 3:
144485f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
144585f9e50dSDaniel Vetter 		break;
144685f9e50dSDaniel Vetter 	case 4:
144785f9e50dSDaniel Vetter 	case 5:
144885f9e50dSDaniel Vetter 	case 6:
144985f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
145085f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
145185f9e50dSDaniel Vetter 		break;
145285f9e50dSDaniel Vetter 	default:
145385f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
145485f9e50dSDaniel Vetter 	case 7:
145585f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
145685f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
145785f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
145885f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
145985f9e50dSDaniel Vetter 		break;
146085f9e50dSDaniel Vetter 	}
146185f9e50dSDaniel Vetter }
146285f9e50dSDaniel Vetter 
14633bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
14649df30794SChris Wilson static struct drm_i915_error_object *
1465d0d045e8SBen Widawsky i915_error_object_create_sized(struct drm_i915_private *dev_priv,
1466d0d045e8SBen Widawsky 			       struct drm_i915_gem_object *src,
1467d0d045e8SBen Widawsky 			       const int num_pages)
14689df30794SChris Wilson {
14699df30794SChris Wilson 	struct drm_i915_error_object *dst;
1470d0d045e8SBen Widawsky 	int i;
1471e56660ddSChris Wilson 	u32 reloc_offset;
14729df30794SChris Wilson 
147305394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
14749df30794SChris Wilson 		return NULL;
14759df30794SChris Wilson 
1476d0d045e8SBen Widawsky 	dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
14779df30794SChris Wilson 	if (dst == NULL)
14789df30794SChris Wilson 		return NULL;
14799df30794SChris Wilson 
148005394f39SChris Wilson 	reloc_offset = src->gtt_offset;
1481d0d045e8SBen Widawsky 	for (i = 0; i < num_pages; i++) {
1482788885aeSAndrew Morton 		unsigned long flags;
1483e56660ddSChris Wilson 		void *d;
1484788885aeSAndrew Morton 
1485e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
14869df30794SChris Wilson 		if (d == NULL)
14879df30794SChris Wilson 			goto unwind;
1488e56660ddSChris Wilson 
1489788885aeSAndrew Morton 		local_irq_save(flags);
14905d4545aeSBen Widawsky 		if (reloc_offset < dev_priv->gtt.mappable_end &&
149174898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
1492172975aaSChris Wilson 			void __iomem *s;
1493172975aaSChris Wilson 
1494172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
1495172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
1496172975aaSChris Wilson 			 * captures what the GPU read.
1497172975aaSChris Wilson 			 */
1498172975aaSChris Wilson 
14995d4545aeSBen Widawsky 			s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
15003e4d3af5SPeter Zijlstra 						     reloc_offset);
1501e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
15023e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
1503960e3564SChris Wilson 		} else if (src->stolen) {
1504960e3564SChris Wilson 			unsigned long offset;
1505960e3564SChris Wilson 
1506960e3564SChris Wilson 			offset = dev_priv->mm.stolen_base;
1507960e3564SChris Wilson 			offset += src->stolen->start;
1508960e3564SChris Wilson 			offset += i << PAGE_SHIFT;
1509960e3564SChris Wilson 
15101a240d4dSDaniel Vetter 			memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
1511172975aaSChris Wilson 		} else {
15129da3da66SChris Wilson 			struct page *page;
1513172975aaSChris Wilson 			void *s;
1514172975aaSChris Wilson 
15159da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
1516172975aaSChris Wilson 
15179da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
15189da3da66SChris Wilson 
15199da3da66SChris Wilson 			s = kmap_atomic(page);
1520172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
1521172975aaSChris Wilson 			kunmap_atomic(s);
1522172975aaSChris Wilson 
15239da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
1524172975aaSChris Wilson 		}
1525788885aeSAndrew Morton 		local_irq_restore(flags);
1526e56660ddSChris Wilson 
15279da3da66SChris Wilson 		dst->pages[i] = d;
1528e56660ddSChris Wilson 
1529e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
15309df30794SChris Wilson 	}
1531d0d045e8SBen Widawsky 	dst->page_count = num_pages;
153205394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
15339df30794SChris Wilson 
15349df30794SChris Wilson 	return dst;
15359df30794SChris Wilson 
15369df30794SChris Wilson unwind:
15379da3da66SChris Wilson 	while (i--)
15389da3da66SChris Wilson 		kfree(dst->pages[i]);
15399df30794SChris Wilson 	kfree(dst);
15409df30794SChris Wilson 	return NULL;
15419df30794SChris Wilson }
1542d0d045e8SBen Widawsky #define i915_error_object_create(dev_priv, src) \
1543d0d045e8SBen Widawsky 	i915_error_object_create_sized((dev_priv), (src), \
1544d0d045e8SBen Widawsky 				       (src)->base.size>>PAGE_SHIFT)
15459df30794SChris Wilson 
15469df30794SChris Wilson static void
15479df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
15489df30794SChris Wilson {
15499df30794SChris Wilson 	int page;
15509df30794SChris Wilson 
15519df30794SChris Wilson 	if (obj == NULL)
15529df30794SChris Wilson 		return;
15539df30794SChris Wilson 
15549df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
15559df30794SChris Wilson 		kfree(obj->pages[page]);
15569df30794SChris Wilson 
15579df30794SChris Wilson 	kfree(obj);
15589df30794SChris Wilson }
15599df30794SChris Wilson 
1560742cbee8SDaniel Vetter void
1561742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
15629df30794SChris Wilson {
1563742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
1564742cbee8SDaniel Vetter 							  typeof(*error), ref);
1565e2f973d5SChris Wilson 	int i;
1566e2f973d5SChris Wilson 
156752d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
156852d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
156952d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
157052d39a21SChris Wilson 		kfree(error->ring[i].requests);
157152d39a21SChris Wilson 	}
1572e2f973d5SChris Wilson 
15739df30794SChris Wilson 	kfree(error->active_bo);
15746ef3d427SChris Wilson 	kfree(error->overlay);
15759df30794SChris Wilson 	kfree(error);
15769df30794SChris Wilson }
15771b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
15781b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
1579c724e8a9SChris Wilson {
1580c724e8a9SChris Wilson 	err->size = obj->base.size;
1581c724e8a9SChris Wilson 	err->name = obj->base.name;
15820201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
15830201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1584c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
1585c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1586c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1587c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1588c724e8a9SChris Wilson 	err->pinned = 0;
1589c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1590c724e8a9SChris Wilson 		err->pinned = 1;
1591c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1592c724e8a9SChris Wilson 		err->pinned = -1;
1593c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1594c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1595c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
159696154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
159793dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
15981b50247aSChris Wilson }
1599c724e8a9SChris Wilson 
16001b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
16011b50247aSChris Wilson 			     int count, struct list_head *head)
16021b50247aSChris Wilson {
16031b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
16041b50247aSChris Wilson 	int i = 0;
16051b50247aSChris Wilson 
16061b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
16071b50247aSChris Wilson 		capture_bo(err++, obj);
1608c724e8a9SChris Wilson 		if (++i == count)
1609c724e8a9SChris Wilson 			break;
16101b50247aSChris Wilson 	}
1611c724e8a9SChris Wilson 
16121b50247aSChris Wilson 	return i;
16131b50247aSChris Wilson }
16141b50247aSChris Wilson 
16151b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
16161b50247aSChris Wilson 			     int count, struct list_head *head)
16171b50247aSChris Wilson {
16181b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
16191b50247aSChris Wilson 	int i = 0;
16201b50247aSChris Wilson 
16211b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
16221b50247aSChris Wilson 		if (obj->pin_count == 0)
16231b50247aSChris Wilson 			continue;
16241b50247aSChris Wilson 
16251b50247aSChris Wilson 		capture_bo(err++, obj);
16261b50247aSChris Wilson 		if (++i == count)
16271b50247aSChris Wilson 			break;
1628c724e8a9SChris Wilson 	}
1629c724e8a9SChris Wilson 
1630c724e8a9SChris Wilson 	return i;
1631c724e8a9SChris Wilson }
1632c724e8a9SChris Wilson 
1633748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1634748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1635748ebc60SChris Wilson {
1636748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1637748ebc60SChris Wilson 	int i;
1638748ebc60SChris Wilson 
1639748ebc60SChris Wilson 	/* Fences */
1640748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1641775d17b6SDaniel Vetter 	case 7:
1642748ebc60SChris Wilson 	case 6:
164342b5aeabSVille Syrjälä 		for (i = 0; i < dev_priv->num_fence_regs; i++)
1644748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1645748ebc60SChris Wilson 		break;
1646748ebc60SChris Wilson 	case 5:
1647748ebc60SChris Wilson 	case 4:
1648748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1649748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1650748ebc60SChris Wilson 		break;
1651748ebc60SChris Wilson 	case 3:
1652748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1653748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1654748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1655748ebc60SChris Wilson 	case 2:
1656748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1657748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1658748ebc60SChris Wilson 		break;
1659748ebc60SChris Wilson 
16607dbf9d6eSBen Widawsky 	default:
16617dbf9d6eSBen Widawsky 		BUG();
1662748ebc60SChris Wilson 	}
1663748ebc60SChris Wilson }
1664748ebc60SChris Wilson 
1665bcfb2e28SChris Wilson static struct drm_i915_error_object *
1666bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1667bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1668bcfb2e28SChris Wilson {
1669bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1670bcfb2e28SChris Wilson 	u32 seqno;
1671bcfb2e28SChris Wilson 
1672bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1673bcfb2e28SChris Wilson 		return NULL;
1674bcfb2e28SChris Wilson 
1675b45305fcSDaniel Vetter 	if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1676b45305fcSDaniel Vetter 		u32 acthd = I915_READ(ACTHD);
1677b45305fcSDaniel Vetter 
1678b45305fcSDaniel Vetter 		if (WARN_ON(ring->id != RCS))
1679b45305fcSDaniel Vetter 			return NULL;
1680b45305fcSDaniel Vetter 
1681b45305fcSDaniel Vetter 		obj = ring->private;
1682b45305fcSDaniel Vetter 		if (acthd >= obj->gtt_offset &&
1683b45305fcSDaniel Vetter 		    acthd < obj->gtt_offset + obj->base.size)
1684b45305fcSDaniel Vetter 			return i915_error_object_create(dev_priv, obj);
1685b45305fcSDaniel Vetter 	}
1686b45305fcSDaniel Vetter 
1687b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1688bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1689bcfb2e28SChris Wilson 		if (obj->ring != ring)
1690bcfb2e28SChris Wilson 			continue;
1691bcfb2e28SChris Wilson 
16920201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1693bcfb2e28SChris Wilson 			continue;
1694bcfb2e28SChris Wilson 
1695bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1696bcfb2e28SChris Wilson 			continue;
1697bcfb2e28SChris Wilson 
1698bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1699bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1700bcfb2e28SChris Wilson 		 */
1701bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1702bcfb2e28SChris Wilson 	}
1703bcfb2e28SChris Wilson 
1704bcfb2e28SChris Wilson 	return NULL;
1705bcfb2e28SChris Wilson }
1706bcfb2e28SChris Wilson 
1707d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1708d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1709d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1710d27b1e0eSDaniel Vetter {
1711d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1712d27b1e0eSDaniel Vetter 
171333f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
171412f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
171533f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
17167e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
17177e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
17187e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
17197e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
1720df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1721df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
172233f3f518SDaniel Vetter 	}
1723c1cd90edSDaniel Vetter 
1724d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
17259d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1726d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1727d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1728d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1729c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1730050ee91fSBen Widawsky 		if (ring->id == RCS)
1731d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1732d27b1e0eSDaniel Vetter 	} else {
17339d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1734d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1735d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1736d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1737d27b1e0eSDaniel Vetter 	}
1738d27b1e0eSDaniel Vetter 
17399574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1740c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1741b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1742d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1743c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1744c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
17450f3b6849SChris Wilson 	error->ctl[ring->id] = I915_READ_CTL(ring);
17467e3b8737SDaniel Vetter 
17477e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
17487e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1749d27b1e0eSDaniel Vetter }
1750d27b1e0eSDaniel Vetter 
17518c123e54SBen Widawsky 
17528c123e54SBen Widawsky static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
17538c123e54SBen Widawsky 					   struct drm_i915_error_state *error,
17548c123e54SBen Widawsky 					   struct drm_i915_error_ring *ering)
17558c123e54SBen Widawsky {
17568c123e54SBen Widawsky 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
17578c123e54SBen Widawsky 	struct drm_i915_gem_object *obj;
17588c123e54SBen Widawsky 
17598c123e54SBen Widawsky 	/* Currently render ring is the only HW context user */
17608c123e54SBen Widawsky 	if (ring->id != RCS || !error->ccid)
17618c123e54SBen Widawsky 		return;
17628c123e54SBen Widawsky 
17638c123e54SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) {
17648c123e54SBen Widawsky 		if ((error->ccid & PAGE_MASK) == obj->gtt_offset) {
17658c123e54SBen Widawsky 			ering->ctx = i915_error_object_create_sized(dev_priv,
17668c123e54SBen Widawsky 								    obj, 1);
17678c123e54SBen Widawsky 		}
17688c123e54SBen Widawsky 	}
17698c123e54SBen Widawsky }
17708c123e54SBen Widawsky 
177152d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
177252d39a21SChris Wilson 				  struct drm_i915_error_state *error)
177352d39a21SChris Wilson {
177452d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1775b4519513SChris Wilson 	struct intel_ring_buffer *ring;
177652d39a21SChris Wilson 	struct drm_i915_gem_request *request;
177752d39a21SChris Wilson 	int i, count;
177852d39a21SChris Wilson 
1779b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
178052d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
178152d39a21SChris Wilson 
178252d39a21SChris Wilson 		error->ring[i].batchbuffer =
178352d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
178452d39a21SChris Wilson 
178552d39a21SChris Wilson 		error->ring[i].ringbuffer =
178652d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
178752d39a21SChris Wilson 
17888c123e54SBen Widawsky 
17898c123e54SBen Widawsky 		i915_gem_record_active_context(ring, error, &error->ring[i]);
17908c123e54SBen Widawsky 
179152d39a21SChris Wilson 		count = 0;
179252d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
179352d39a21SChris Wilson 			count++;
179452d39a21SChris Wilson 
179552d39a21SChris Wilson 		error->ring[i].num_requests = count;
179652d39a21SChris Wilson 		error->ring[i].requests =
179752d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
179852d39a21SChris Wilson 				GFP_ATOMIC);
179952d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
180052d39a21SChris Wilson 			error->ring[i].num_requests = 0;
180152d39a21SChris Wilson 			continue;
180252d39a21SChris Wilson 		}
180352d39a21SChris Wilson 
180452d39a21SChris Wilson 		count = 0;
180552d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
180652d39a21SChris Wilson 			struct drm_i915_error_request *erq;
180752d39a21SChris Wilson 
180852d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
180952d39a21SChris Wilson 			erq->seqno = request->seqno;
181052d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1811ee4f42b1SChris Wilson 			erq->tail = request->tail;
181252d39a21SChris Wilson 		}
181352d39a21SChris Wilson 	}
181452d39a21SChris Wilson }
181552d39a21SChris Wilson 
18168a905236SJesse Barnes /**
18178a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
18188a905236SJesse Barnes  * @dev: drm device
18198a905236SJesse Barnes  *
18208a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
18218a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
18228a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
18238a905236SJesse Barnes  * to pick up.
18248a905236SJesse Barnes  */
182563eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
182663eeaf38SJesse Barnes {
182763eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
182805394f39SChris Wilson 	struct drm_i915_gem_object *obj;
182963eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
183063eeaf38SJesse Barnes 	unsigned long flags;
18319db4a9c7SJesse Barnes 	int i, pipe;
183263eeaf38SJesse Barnes 
183399584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
183499584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
183599584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
18369df30794SChris Wilson 	if (error)
18379df30794SChris Wilson 		return;
183863eeaf38SJesse Barnes 
18399db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
184033f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
184163eeaf38SJesse Barnes 	if (!error) {
18429df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
18439df30794SChris Wilson 		return;
184463eeaf38SJesse Barnes 	}
184563eeaf38SJesse Barnes 
18462f86f191SBen Widawsky 	DRM_INFO("capturing error event; look for more information in "
18472f86f191SBen Widawsky 		 "/sys/kernel/debug/dri/%d/i915_error_state\n",
1848b6f7833bSChris Wilson 		 dev->primary->index);
18492fa772f3SChris Wilson 
1850742cbee8SDaniel Vetter 	kref_init(&error->ref);
185163eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
185263eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1853211816ecSBen Widawsky 	if (HAS_HW_CONTEXTS(dev))
1854b9a3906bSBen Widawsky 		error->ccid = I915_READ(CCID);
1855be998e2eSBen Widawsky 
1856be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1857be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1858be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1859be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1860be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1861be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1862be998e2eSBen Widawsky 	else
1863be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1864be998e2eSBen Widawsky 
18650f3b6849SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6)
18660f3b6849SChris Wilson 		error->derrmr = I915_READ(DERRMR);
18670f3b6849SChris Wilson 
18680f3b6849SChris Wilson 	if (IS_VALLEYVIEW(dev))
18690f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_VLV);
18700f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 7)
18710f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_MT);
18720f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen == 6)
18730f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE);
18740f3b6849SChris Wilson 
18754f3308b9SPaulo Zanoni 	if (!HAS_PCH_SPLIT(dev))
18769db4a9c7SJesse Barnes 		for_each_pipe(pipe)
18779db4a9c7SJesse Barnes 			error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1878d27b1e0eSDaniel Vetter 
187933f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1880f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
188133f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
188233f3f518SDaniel Vetter 	}
1883add354ddSChris Wilson 
188471e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
188571e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
188671e172e8SBen Widawsky 
1887050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1888050ee91fSBen Widawsky 
1889748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
189052d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
18919df30794SChris Wilson 
1892c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
18939df30794SChris Wilson 	error->active_bo = NULL;
1894c724e8a9SChris Wilson 	error->pinned_bo = NULL;
18959df30794SChris Wilson 
1896bcfb2e28SChris Wilson 	i = 0;
1897bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1898bcfb2e28SChris Wilson 		i++;
1899bcfb2e28SChris Wilson 	error->active_bo_count = i;
19006c085a72SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
19011b50247aSChris Wilson 		if (obj->pin_count)
1902bcfb2e28SChris Wilson 			i++;
1903bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1904c724e8a9SChris Wilson 
19058e934dbfSChris Wilson 	error->active_bo = NULL;
19068e934dbfSChris Wilson 	error->pinned_bo = NULL;
1907bcfb2e28SChris Wilson 	if (i) {
1908bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
19099df30794SChris Wilson 					   GFP_ATOMIC);
1910c724e8a9SChris Wilson 		if (error->active_bo)
1911c724e8a9SChris Wilson 			error->pinned_bo =
1912c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
19139df30794SChris Wilson 	}
1914c724e8a9SChris Wilson 
1915c724e8a9SChris Wilson 	if (error->active_bo)
1916c724e8a9SChris Wilson 		error->active_bo_count =
19171b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1918c724e8a9SChris Wilson 					  error->active_bo_count,
1919c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1920c724e8a9SChris Wilson 
1921c724e8a9SChris Wilson 	if (error->pinned_bo)
1922c724e8a9SChris Wilson 		error->pinned_bo_count =
19231b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1924c724e8a9SChris Wilson 					  error->pinned_bo_count,
19256c085a72SChris Wilson 					  &dev_priv->mm.bound_list);
192663eeaf38SJesse Barnes 
19278a905236SJesse Barnes 	do_gettimeofday(&error->time);
19288a905236SJesse Barnes 
19296ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1930c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
19316ef3d427SChris Wilson 
193299584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
193399584db3SDaniel Vetter 	if (dev_priv->gpu_error.first_error == NULL) {
193499584db3SDaniel Vetter 		dev_priv->gpu_error.first_error = error;
19359df30794SChris Wilson 		error = NULL;
19369df30794SChris Wilson 	}
193799584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19389df30794SChris Wilson 
19399df30794SChris Wilson 	if (error)
1940742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
19419df30794SChris Wilson }
19429df30794SChris Wilson 
19439df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
19449df30794SChris Wilson {
19459df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
19469df30794SChris Wilson 	struct drm_i915_error_state *error;
19476dc0e816SBen Widawsky 	unsigned long flags;
19489df30794SChris Wilson 
194999584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
195099584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
195199584db3SDaniel Vetter 	dev_priv->gpu_error.first_error = NULL;
195299584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19539df30794SChris Wilson 
19549df30794SChris Wilson 	if (error)
1955742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
195663eeaf38SJesse Barnes }
19573bd3c932SChris Wilson #else
19583bd3c932SChris Wilson #define i915_capture_error_state(x)
19593bd3c932SChris Wilson #endif
196063eeaf38SJesse Barnes 
196135aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1962c0e09200SDave Airlie {
19638a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1964bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
196563eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1966050ee91fSBen Widawsky 	int pipe, i;
196763eeaf38SJesse Barnes 
196835aed2e6SChris Wilson 	if (!eir)
196935aed2e6SChris Wilson 		return;
197063eeaf38SJesse Barnes 
1971a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
19728a905236SJesse Barnes 
1973bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1974bd9854f9SBen Widawsky 
19758a905236SJesse Barnes 	if (IS_G4X(dev)) {
19768a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
19778a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
19788a905236SJesse Barnes 
1979a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1980a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1981050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1982050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1983a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1984a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
19858a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
19863143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
19878a905236SJesse Barnes 		}
19888a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
19898a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1990a70491ccSJoe Perches 			pr_err("page table error\n");
1991a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
19928a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
19933143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
19948a905236SJesse Barnes 		}
19958a905236SJesse Barnes 	}
19968a905236SJesse Barnes 
1997a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
199863eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
199963eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2000a70491ccSJoe Perches 			pr_err("page table error\n");
2001a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
200263eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20033143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
200463eeaf38SJesse Barnes 		}
20058a905236SJesse Barnes 	}
20068a905236SJesse Barnes 
200763eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2008a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
20099db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2010a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
20119db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
201263eeaf38SJesse Barnes 		/* pipestat has already been acked */
201363eeaf38SJesse Barnes 	}
201463eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2015a70491ccSJoe Perches 		pr_err("instruction error\n");
2016a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2017050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2018050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2019a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
202063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
202163eeaf38SJesse Barnes 
2022a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2023a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2024a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
202563eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
20263143a2bfSChris Wilson 			POSTING_READ(IPEIR);
202763eeaf38SJesse Barnes 		} else {
202863eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
202963eeaf38SJesse Barnes 
2030a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2031a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2032a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2033a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
203463eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20353143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
203663eeaf38SJesse Barnes 		}
203763eeaf38SJesse Barnes 	}
203863eeaf38SJesse Barnes 
203963eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
20403143a2bfSChris Wilson 	POSTING_READ(EIR);
204163eeaf38SJesse Barnes 	eir = I915_READ(EIR);
204263eeaf38SJesse Barnes 	if (eir) {
204363eeaf38SJesse Barnes 		/*
204463eeaf38SJesse Barnes 		 * some errors might have become stuck,
204563eeaf38SJesse Barnes 		 * mask them.
204663eeaf38SJesse Barnes 		 */
204763eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
204863eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
204963eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
205063eeaf38SJesse Barnes 	}
205135aed2e6SChris Wilson }
205235aed2e6SChris Wilson 
205335aed2e6SChris Wilson /**
205435aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
205535aed2e6SChris Wilson  * @dev: drm device
205635aed2e6SChris Wilson  *
205735aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
205835aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
205935aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
206035aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
206135aed2e6SChris Wilson  * of a ring dump etc.).
206235aed2e6SChris Wilson  */
2063527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
206435aed2e6SChris Wilson {
206535aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2066b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2067b4519513SChris Wilson 	int i;
206835aed2e6SChris Wilson 
206935aed2e6SChris Wilson 	i915_capture_error_state(dev);
207035aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
20718a905236SJesse Barnes 
2072ba1234d1SBen Gamari 	if (wedged) {
2073f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2074f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2075ba1234d1SBen Gamari 
207611ed50ecSBen Gamari 		/*
20771f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
20781f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
207911ed50ecSBen Gamari 		 */
2080b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
2081b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
208211ed50ecSBen Gamari 	}
208311ed50ecSBen Gamari 
208499584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
20858a905236SJesse Barnes }
20868a905236SJesse Barnes 
208721ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
20884e5359cdSSimon Farnsworth {
20894e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
20904e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
20914e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
209205394f39SChris Wilson 	struct drm_i915_gem_object *obj;
20934e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
20944e5359cdSSimon Farnsworth 	unsigned long flags;
20954e5359cdSSimon Farnsworth 	bool stall_detected;
20964e5359cdSSimon Farnsworth 
20974e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
20984e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
20994e5359cdSSimon Farnsworth 		return;
21004e5359cdSSimon Farnsworth 
21014e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
21024e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
21034e5359cdSSimon Farnsworth 
2104e7d841caSChris Wilson 	if (work == NULL ||
2105e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2106e7d841caSChris Wilson 	    !work->enable_stall_check) {
21074e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
21084e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
21094e5359cdSSimon Farnsworth 		return;
21104e5359cdSSimon Farnsworth 	}
21114e5359cdSSimon Farnsworth 
21124e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
211305394f39SChris Wilson 	obj = work->pending_flip_obj;
2114a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
21159db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2116446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2117446f2545SArmin Reese 					obj->gtt_offset;
21184e5359cdSSimon Farnsworth 	} else {
21199db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
212005394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
212101f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
21224e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
21234e5359cdSSimon Farnsworth 	}
21244e5359cdSSimon Farnsworth 
21254e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
21264e5359cdSSimon Farnsworth 
21274e5359cdSSimon Farnsworth 	if (stall_detected) {
21284e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
21294e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
21304e5359cdSSimon Farnsworth 	}
21314e5359cdSSimon Farnsworth }
21324e5359cdSSimon Farnsworth 
213342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
213442f52ef8SKeith Packard  * we use as a pipe index
213542f52ef8SKeith Packard  */
2136f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
21370a3e67a4SJesse Barnes {
21380a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2139e9d21d7fSKeith Packard 	unsigned long irqflags;
214071e0ffa5SJesse Barnes 
21415eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
214271e0ffa5SJesse Barnes 		return -EINVAL;
21430a3e67a4SJesse Barnes 
21441ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2145f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
21467c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21477c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
21480a3e67a4SJesse Barnes 	else
21497c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21507c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
21518692d00eSChris Wilson 
21528692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
21538692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
21546b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
21551ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
21568692d00eSChris Wilson 
21570a3e67a4SJesse Barnes 	return 0;
21580a3e67a4SJesse Barnes }
21590a3e67a4SJesse Barnes 
2160f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2161f796cf8fSJesse Barnes {
2162f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2163f796cf8fSJesse Barnes 	unsigned long irqflags;
2164f796cf8fSJesse Barnes 
2165f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2166f796cf8fSJesse Barnes 		return -EINVAL;
2167f796cf8fSJesse Barnes 
2168f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2169f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
2170f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2171f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2172f796cf8fSJesse Barnes 
2173f796cf8fSJesse Barnes 	return 0;
2174f796cf8fSJesse Barnes }
2175f796cf8fSJesse Barnes 
2176f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
2177b1f14ad0SJesse Barnes {
2178b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2179b1f14ad0SJesse Barnes 	unsigned long irqflags;
2180b1f14ad0SJesse Barnes 
2181b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2182b1f14ad0SJesse Barnes 		return -EINVAL;
2183b1f14ad0SJesse Barnes 
2184b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2185b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
2186b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
2187b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2188b1f14ad0SJesse Barnes 
2189b1f14ad0SJesse Barnes 	return 0;
2190b1f14ad0SJesse Barnes }
2191b1f14ad0SJesse Barnes 
21927e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
21937e231dbeSJesse Barnes {
21947e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21957e231dbeSJesse Barnes 	unsigned long irqflags;
219631acc7f5SJesse Barnes 	u32 imr;
21977e231dbeSJesse Barnes 
21987e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
21997e231dbeSJesse Barnes 		return -EINVAL;
22007e231dbeSJesse Barnes 
22017e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22027e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
220331acc7f5SJesse Barnes 	if (pipe == 0)
22047e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
220531acc7f5SJesse Barnes 	else
22067e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22077e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
220831acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
220931acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
22107e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22117e231dbeSJesse Barnes 
22127e231dbeSJesse Barnes 	return 0;
22137e231dbeSJesse Barnes }
22147e231dbeSJesse Barnes 
221542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
221642f52ef8SKeith Packard  * we use as a pipe index
221742f52ef8SKeith Packard  */
2218f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
22190a3e67a4SJesse Barnes {
22200a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2221e9d21d7fSKeith Packard 	unsigned long irqflags;
22220a3e67a4SJesse Barnes 
22231ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22248692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
22256b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
22268692d00eSChris Wilson 
22277c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
22287c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
22297c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
22301ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22310a3e67a4SJesse Barnes }
22320a3e67a4SJesse Barnes 
2233f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2234f796cf8fSJesse Barnes {
2235f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2236f796cf8fSJesse Barnes 	unsigned long irqflags;
2237f796cf8fSJesse Barnes 
2238f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2239f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
2240f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2241f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2242f796cf8fSJesse Barnes }
2243f796cf8fSJesse Barnes 
2244f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
2245b1f14ad0SJesse Barnes {
2246b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2247b1f14ad0SJesse Barnes 	unsigned long irqflags;
2248b1f14ad0SJesse Barnes 
2249b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2250b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
2251b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
2252b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2253b1f14ad0SJesse Barnes }
2254b1f14ad0SJesse Barnes 
22557e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
22567e231dbeSJesse Barnes {
22577e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22587e231dbeSJesse Barnes 	unsigned long irqflags;
225931acc7f5SJesse Barnes 	u32 imr;
22607e231dbeSJesse Barnes 
22617e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
226231acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
226331acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
22647e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
226531acc7f5SJesse Barnes 	if (pipe == 0)
22667e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
226731acc7f5SJesse Barnes 	else
22687e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22697e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
22707e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22717e231dbeSJesse Barnes }
22727e231dbeSJesse Barnes 
2273893eead0SChris Wilson static u32
2274893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2275852835f3SZou Nan hai {
2276893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2277893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2278893eead0SChris Wilson }
2279893eead0SChris Wilson 
2280893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
2281893eead0SChris Wilson {
2282893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
2283b2eadbc8SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring, false),
2284b2eadbc8SChris Wilson 			      ring_last_seqno(ring))) {
2285893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
22869574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
22879574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
22889574b3feSBen Widawsky 				  ring->name);
2289893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
2290893eead0SChris Wilson 			*err = true;
2291893eead0SChris Wilson 		}
2292893eead0SChris Wilson 		return true;
2293893eead0SChris Wilson 	}
2294893eead0SChris Wilson 	return false;
2295f65d9421SBen Gamari }
2296f65d9421SBen Gamari 
2297a24a11e6SChris Wilson static bool semaphore_passed(struct intel_ring_buffer *ring)
2298a24a11e6SChris Wilson {
2299a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2300a24a11e6SChris Wilson 	u32 acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2301a24a11e6SChris Wilson 	struct intel_ring_buffer *signaller;
2302a24a11e6SChris Wilson 	u32 cmd, ipehr, acthd_min;
2303a24a11e6SChris Wilson 
2304a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2305a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2306a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
2307a24a11e6SChris Wilson 		return false;
2308a24a11e6SChris Wilson 
2309a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2310a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2311a24a11e6SChris Wilson 	 */
2312a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2313a24a11e6SChris Wilson 	do {
2314a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2315a24a11e6SChris Wilson 		if (cmd == ipehr)
2316a24a11e6SChris Wilson 			break;
2317a24a11e6SChris Wilson 
2318a24a11e6SChris Wilson 		acthd -= 4;
2319a24a11e6SChris Wilson 		if (acthd < acthd_min)
2320a24a11e6SChris Wilson 			return false;
2321a24a11e6SChris Wilson 	} while (1);
2322a24a11e6SChris Wilson 
2323a24a11e6SChris Wilson 	signaller = &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2324a24a11e6SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false),
2325a24a11e6SChris Wilson 				 ioread32(ring->virtual_start+acthd+4)+1);
2326a24a11e6SChris Wilson }
2327a24a11e6SChris Wilson 
23281ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
23291ec14ad3SChris Wilson {
23301ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
23311ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
23321ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
23331ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
23341ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
23351ec14ad3SChris Wilson 			  ring->name);
23361ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
23371ec14ad3SChris Wilson 		return true;
23381ec14ad3SChris Wilson 	}
2339a24a11e6SChris Wilson 
2340a24a11e6SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 &&
2341a24a11e6SChris Wilson 	    tmp & RING_WAIT_SEMAPHORE &&
2342a24a11e6SChris Wilson 	    semaphore_passed(ring)) {
2343a24a11e6SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
2344a24a11e6SChris Wilson 			  ring->name);
2345a24a11e6SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2346a24a11e6SChris Wilson 		return true;
2347a24a11e6SChris Wilson 	}
23481ec14ad3SChris Wilson 	return false;
23491ec14ad3SChris Wilson }
23501ec14ad3SChris Wilson 
2351d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
2352d1e61e7fSChris Wilson {
2353d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
2354d1e61e7fSChris Wilson 
235599584db3SDaniel Vetter 	if (dev_priv->gpu_error.hangcheck_count++ > 1) {
2356b4519513SChris Wilson 		bool hung = true;
2357b4519513SChris Wilson 
2358d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
2359d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
2360d1e61e7fSChris Wilson 
2361d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
2362b4519513SChris Wilson 			struct intel_ring_buffer *ring;
2363b4519513SChris Wilson 			int i;
2364b4519513SChris Wilson 
2365d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
2366d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
2367d1e61e7fSChris Wilson 			 * and break the hang. This should work on
2368d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
2369d1e61e7fSChris Wilson 			 */
2370b4519513SChris Wilson 			for_each_ring(ring, dev_priv, i)
2371b4519513SChris Wilson 				hung &= !kick_ring(ring);
2372d1e61e7fSChris Wilson 		}
2373d1e61e7fSChris Wilson 
2374b4519513SChris Wilson 		return hung;
2375d1e61e7fSChris Wilson 	}
2376d1e61e7fSChris Wilson 
2377d1e61e7fSChris Wilson 	return false;
2378d1e61e7fSChris Wilson }
2379d1e61e7fSChris Wilson 
2380f65d9421SBen Gamari /**
2381f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
2382f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
2383f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
2384f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
2385f65d9421SBen Gamari  */
2386f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
2387f65d9421SBen Gamari {
2388f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2389f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2390bd9854f9SBen Widawsky 	uint32_t acthd[I915_NUM_RINGS], instdone[I915_NUM_INSTDONE_REG];
2391b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2392b4519513SChris Wilson 	bool err = false, idle;
2393b4519513SChris Wilson 	int i;
2394893eead0SChris Wilson 
23953e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
23963e0dc6b0SBen Widawsky 		return;
23973e0dc6b0SBen Widawsky 
2398b4519513SChris Wilson 	memset(acthd, 0, sizeof(acthd));
2399b4519513SChris Wilson 	idle = true;
2400b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
2401b4519513SChris Wilson 	    idle &= i915_hangcheck_ring_idle(ring, &err);
2402b4519513SChris Wilson 	    acthd[i] = intel_ring_get_active_head(ring);
2403b4519513SChris Wilson 	}
2404b4519513SChris Wilson 
2405893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
2406b4519513SChris Wilson 	if (idle) {
2407d1e61e7fSChris Wilson 		if (err) {
2408d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
2409d1e61e7fSChris Wilson 				return;
2410d1e61e7fSChris Wilson 
2411893eead0SChris Wilson 			goto repeat;
2412d1e61e7fSChris Wilson 		}
2413d1e61e7fSChris Wilson 
241499584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
2415893eead0SChris Wilson 		return;
2416893eead0SChris Wilson 	}
2417f65d9421SBen Gamari 
2418bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
241999584db3SDaniel Vetter 	if (memcmp(dev_priv->gpu_error.last_acthd, acthd,
242099584db3SDaniel Vetter 		   sizeof(acthd)) == 0 &&
242199584db3SDaniel Vetter 	    memcmp(dev_priv->gpu_error.prev_instdone, instdone,
242299584db3SDaniel Vetter 		   sizeof(instdone)) == 0) {
2423d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
2424f65d9421SBen Gamari 			return;
2425cbb465e7SChris Wilson 	} else {
242699584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
2427cbb465e7SChris Wilson 
242899584db3SDaniel Vetter 		memcpy(dev_priv->gpu_error.last_acthd, acthd,
242999584db3SDaniel Vetter 		       sizeof(acthd));
243099584db3SDaniel Vetter 		memcpy(dev_priv->gpu_error.prev_instdone, instdone,
243199584db3SDaniel Vetter 		       sizeof(instdone));
2432cbb465e7SChris Wilson 	}
2433f65d9421SBen Gamari 
2434893eead0SChris Wilson repeat:
2435f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
243699584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
2437cecc21feSChris Wilson 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2438f65d9421SBen Gamari }
2439f65d9421SBen Gamari 
2440c0e09200SDave Airlie /* drm_dma.h hooks
2441c0e09200SDave Airlie */
2442f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2443036a4a7dSZhenyu Wang {
2444036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2445036a4a7dSZhenyu Wang 
24464697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
24474697995bSJesse Barnes 
2448036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2449bdfcdb63SDaniel Vetter 
2450036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
2451036a4a7dSZhenyu Wang 
2452036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2453036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
24543143a2bfSChris Wilson 	POSTING_READ(DEIER);
2455036a4a7dSZhenyu Wang 
2456036a4a7dSZhenyu Wang 	/* and GT */
2457036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2458036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
24593143a2bfSChris Wilson 	POSTING_READ(GTIER);
2460c650156aSZhenyu Wang 
2461ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2462ab5c608bSBen Widawsky 		return;
2463ab5c608bSBen Widawsky 
2464c650156aSZhenyu Wang 	/* south display irq */
2465c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
246682a28bcfSDaniel Vetter 	/*
246782a28bcfSDaniel Vetter 	 * SDEIER is also touched by the interrupt handler to work around missed
246882a28bcfSDaniel Vetter 	 * PCH interrupts. Hence we can't update it after the interrupt handler
246982a28bcfSDaniel Vetter 	 * is enabled - instead we unconditionally enable all PCH interrupt
247082a28bcfSDaniel Vetter 	 * sources here, but then only unmask them as needed with SDEIMR.
247182a28bcfSDaniel Vetter 	 */
247282a28bcfSDaniel Vetter 	I915_WRITE(SDEIER, 0xffffffff);
24733143a2bfSChris Wilson 	POSTING_READ(SDEIER);
2474036a4a7dSZhenyu Wang }
2475036a4a7dSZhenyu Wang 
24767e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
24777e231dbeSJesse Barnes {
24787e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24797e231dbeSJesse Barnes 	int pipe;
24807e231dbeSJesse Barnes 
24817e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
24827e231dbeSJesse Barnes 
24837e231dbeSJesse Barnes 	/* VLV magic */
24847e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
24857e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
24867e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
24877e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
24887e231dbeSJesse Barnes 
24897e231dbeSJesse Barnes 	/* and GT */
24907e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
24917e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
24927e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
24937e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
24947e231dbeSJesse Barnes 	POSTING_READ(GTIER);
24957e231dbeSJesse Barnes 
24967e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
24977e231dbeSJesse Barnes 
24987e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
24997e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
25007e231dbeSJesse Barnes 	for_each_pipe(pipe)
25017e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
25027e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
25037e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
25047e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
25057e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
25067e231dbeSJesse Barnes }
25077e231dbeSJesse Barnes 
250882a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
250982a28bcfSDaniel Vetter {
251082a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
251182a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
251282a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
251382a28bcfSDaniel Vetter 	u32 mask = ~I915_READ(SDEIMR);
251482a28bcfSDaniel Vetter 	u32 hotplug;
251582a28bcfSDaniel Vetter 
251682a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2517995e6b3dSEgbert Eich 		mask &= ~SDE_HOTPLUG_MASK;
251882a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2519cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
252082a28bcfSDaniel Vetter 				mask |= hpd_ibx[intel_encoder->hpd_pin];
252182a28bcfSDaniel Vetter 	} else {
2522995e6b3dSEgbert Eich 		mask &= ~SDE_HOTPLUG_MASK_CPT;
252382a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2524cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
252582a28bcfSDaniel Vetter 				mask |= hpd_cpt[intel_encoder->hpd_pin];
252682a28bcfSDaniel Vetter 	}
252782a28bcfSDaniel Vetter 
252882a28bcfSDaniel Vetter 	I915_WRITE(SDEIMR, ~mask);
252982a28bcfSDaniel Vetter 
25307fe0b973SKeith Packard 	/*
25317fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
25327fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
25337fe0b973SKeith Packard 	 *
25347fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
25357fe0b973SKeith Packard 	 */
25367fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
25377fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
25387fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
25397fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
25407fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
25417fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
25427fe0b973SKeith Packard }
25437fe0b973SKeith Packard 
2544d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2545d46da437SPaulo Zanoni {
2546d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
254782a28bcfSDaniel Vetter 	u32 mask;
2548d46da437SPaulo Zanoni 
25498664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
25508664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2551de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
25528664281bSPaulo Zanoni 	} else {
25538664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
25548664281bSPaulo Zanoni 
25558664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
25568664281bSPaulo Zanoni 	}
2557ab5c608bSBen Widawsky 
2558ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2559ab5c608bSBen Widawsky 		return;
2560ab5c608bSBen Widawsky 
2561d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2562d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2563d46da437SPaulo Zanoni }
2564d46da437SPaulo Zanoni 
2565f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2566036a4a7dSZhenyu Wang {
2567036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2568036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
2569013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2570ce99c256SDaniel Vetter 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
25718664281bSPaulo Zanoni 			   DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
2572de032bf4SPaulo Zanoni 			   DE_PIPEA_FIFO_UNDERRUN | DE_POISON;
25731ec14ad3SChris Wilson 	u32 render_irqs;
2574036a4a7dSZhenyu Wang 
25751ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2576036a4a7dSZhenyu Wang 
2577036a4a7dSZhenyu Wang 	/* should always can generate irq */
2578036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
25791ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
25801ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
25813143a2bfSChris Wilson 	POSTING_READ(DEIER);
2582036a4a7dSZhenyu Wang 
25831ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
2584036a4a7dSZhenyu Wang 
2585036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
25861ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2587881f47b6SXiang, Haihao 
25881ec14ad3SChris Wilson 	if (IS_GEN6(dev))
25891ec14ad3SChris Wilson 		render_irqs =
25901ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
2591e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
2592e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
25931ec14ad3SChris Wilson 	else
25941ec14ad3SChris Wilson 		render_irqs =
259588f23b8fSChris Wilson 			GT_USER_INTERRUPT |
2596c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
25971ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
25981ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
25993143a2bfSChris Wilson 	POSTING_READ(GTIER);
2600036a4a7dSZhenyu Wang 
2601d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
26027fe0b973SKeith Packard 
2603f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
2604f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
2605f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
2606f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
2607f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2608f97108d1SJesse Barnes 	}
2609f97108d1SJesse Barnes 
2610036a4a7dSZhenyu Wang 	return 0;
2611036a4a7dSZhenyu Wang }
2612036a4a7dSZhenyu Wang 
2613f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2614b1f14ad0SJesse Barnes {
2615b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2616b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2617b615b57aSChris Wilson 	u32 display_mask =
2618b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2619b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
2620b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
2621ce99c256SDaniel Vetter 		DE_PLANEA_FLIP_DONE_IVB |
26228664281bSPaulo Zanoni 		DE_AUX_CHANNEL_A_IVB |
26238664281bSPaulo Zanoni 		DE_ERR_INT_IVB;
2624b1f14ad0SJesse Barnes 	u32 render_irqs;
2625b1f14ad0SJesse Barnes 
2626b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2627b1f14ad0SJesse Barnes 
2628b1f14ad0SJesse Barnes 	/* should always can generate irq */
26298664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2630b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2631b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2632b615b57aSChris Wilson 	I915_WRITE(DEIER,
2633b615b57aSChris Wilson 		   display_mask |
2634b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
2635b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
2636b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
2637b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2638b1f14ad0SJesse Barnes 
263915b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2640b1f14ad0SJesse Barnes 
2641b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2642b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2643b1f14ad0SJesse Barnes 
2644e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
264515b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2646b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
2647b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2648b1f14ad0SJesse Barnes 
2649d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
26507fe0b973SKeith Packard 
2651b1f14ad0SJesse Barnes 	return 0;
2652b1f14ad0SJesse Barnes }
2653b1f14ad0SJesse Barnes 
26547e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
26557e231dbeSJesse Barnes {
26567e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26577e231dbeSJesse Barnes 	u32 enable_mask;
265831acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
26593bcedbe5SJesse Barnes 	u32 render_irqs;
26607e231dbeSJesse Barnes 	u16 msid;
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 
26767e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
26777e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
26787e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
26797e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
26807e231dbeSJesse Barnes 	msid |= (1<<14);
26817e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
26827e231dbeSJesse Barnes 
268320afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
268420afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
268520afbda2SDaniel Vetter 
26867e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
26877e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
26887e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26897e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
26907e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
26917e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
26927e231dbeSJesse Barnes 
269331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2694515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
269531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
269631acc7f5SJesse Barnes 
26977e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26987e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
26997e231dbeSJesse Barnes 
270031acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
270131acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
27023bcedbe5SJesse Barnes 
27033bcedbe5SJesse Barnes 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
27043bcedbe5SJesse Barnes 		GEN6_BLITTER_USER_INTERRUPT;
27053bcedbe5SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
27067e231dbeSJesse Barnes 	POSTING_READ(GTIER);
27077e231dbeSJesse Barnes 
27087e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
27097e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
27107e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
27117e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
27127e231dbeSJesse Barnes #endif
27137e231dbeSJesse Barnes 
27147e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
271520afbda2SDaniel Vetter 
271620afbda2SDaniel Vetter 	return 0;
271720afbda2SDaniel Vetter }
271820afbda2SDaniel Vetter 
27197e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
27207e231dbeSJesse Barnes {
27217e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
27227e231dbeSJesse Barnes 	int pipe;
27237e231dbeSJesse Barnes 
27247e231dbeSJesse Barnes 	if (!dev_priv)
27257e231dbeSJesse Barnes 		return;
27267e231dbeSJesse Barnes 
2727ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2728ac4c16c5SEgbert Eich 
27297e231dbeSJesse Barnes 	for_each_pipe(pipe)
27307e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
27317e231dbeSJesse Barnes 
27327e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
27337e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
27347e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
27357e231dbeSJesse Barnes 	for_each_pipe(pipe)
27367e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
27377e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
27387e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
27397e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
27407e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
27417e231dbeSJesse Barnes }
27427e231dbeSJesse Barnes 
2743f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2744036a4a7dSZhenyu Wang {
2745036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
27464697995bSJesse Barnes 
27474697995bSJesse Barnes 	if (!dev_priv)
27484697995bSJesse Barnes 		return;
27494697995bSJesse Barnes 
2750ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2751ac4c16c5SEgbert Eich 
2752036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2753036a4a7dSZhenyu Wang 
2754036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2755036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2756036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
27578664281bSPaulo Zanoni 	if (IS_GEN7(dev))
27588664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2759036a4a7dSZhenyu Wang 
2760036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2761036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2762036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2763192aac1fSKeith Packard 
2764ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2765ab5c608bSBen Widawsky 		return;
2766ab5c608bSBen Widawsky 
2767192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2768192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2769192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
27708664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
27718664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2772036a4a7dSZhenyu Wang }
2773036a4a7dSZhenyu Wang 
2774c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2775c2798b19SChris Wilson {
2776c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2777c2798b19SChris Wilson 	int pipe;
2778c2798b19SChris Wilson 
2779c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2780c2798b19SChris Wilson 
2781c2798b19SChris Wilson 	for_each_pipe(pipe)
2782c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2783c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2784c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2785c2798b19SChris Wilson 	POSTING_READ16(IER);
2786c2798b19SChris Wilson }
2787c2798b19SChris Wilson 
2788c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2789c2798b19SChris Wilson {
2790c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2791c2798b19SChris Wilson 
2792c2798b19SChris Wilson 	I915_WRITE16(EMR,
2793c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2794c2798b19SChris Wilson 
2795c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2796c2798b19SChris Wilson 	dev_priv->irq_mask =
2797c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2798c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2799c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2800c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2801c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2802c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2803c2798b19SChris Wilson 
2804c2798b19SChris Wilson 	I915_WRITE16(IER,
2805c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2806c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2807c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2808c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2809c2798b19SChris Wilson 	POSTING_READ16(IER);
2810c2798b19SChris Wilson 
2811c2798b19SChris Wilson 	return 0;
2812c2798b19SChris Wilson }
2813c2798b19SChris Wilson 
281490a72f87SVille Syrjälä /*
281590a72f87SVille Syrjälä  * Returns true when a page flip has completed.
281690a72f87SVille Syrjälä  */
281790a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
281890a72f87SVille Syrjälä 			       int pipe, u16 iir)
281990a72f87SVille Syrjälä {
282090a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
282190a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
282290a72f87SVille Syrjälä 
282390a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
282490a72f87SVille Syrjälä 		return false;
282590a72f87SVille Syrjälä 
282690a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
282790a72f87SVille Syrjälä 		return false;
282890a72f87SVille Syrjälä 
282990a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
283090a72f87SVille Syrjälä 
283190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
283290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
283390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
283490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
283590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
283690a72f87SVille Syrjälä 	 */
283790a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
283890a72f87SVille Syrjälä 		return false;
283990a72f87SVille Syrjälä 
284090a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
284190a72f87SVille Syrjälä 
284290a72f87SVille Syrjälä 	return true;
284390a72f87SVille Syrjälä }
284490a72f87SVille Syrjälä 
2845ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2846c2798b19SChris Wilson {
2847c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2848c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2849c2798b19SChris Wilson 	u16 iir, new_iir;
2850c2798b19SChris Wilson 	u32 pipe_stats[2];
2851c2798b19SChris Wilson 	unsigned long irqflags;
2852c2798b19SChris Wilson 	int irq_received;
2853c2798b19SChris Wilson 	int pipe;
2854c2798b19SChris Wilson 	u16 flip_mask =
2855c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2856c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2857c2798b19SChris Wilson 
2858c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2859c2798b19SChris Wilson 
2860c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2861c2798b19SChris Wilson 	if (iir == 0)
2862c2798b19SChris Wilson 		return IRQ_NONE;
2863c2798b19SChris Wilson 
2864c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2865c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2866c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2867c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2868c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2869c2798b19SChris Wilson 		 */
2870c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2871c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2872c2798b19SChris Wilson 			i915_handle_error(dev, false);
2873c2798b19SChris Wilson 
2874c2798b19SChris Wilson 		for_each_pipe(pipe) {
2875c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2876c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2877c2798b19SChris Wilson 
2878c2798b19SChris Wilson 			/*
2879c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2880c2798b19SChris Wilson 			 */
2881c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2882c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2883c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2884c2798b19SChris Wilson 							 pipe_name(pipe));
2885c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2886c2798b19SChris Wilson 				irq_received = 1;
2887c2798b19SChris Wilson 			}
2888c2798b19SChris Wilson 		}
2889c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2890c2798b19SChris Wilson 
2891c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2892c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2893c2798b19SChris Wilson 
2894d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2895c2798b19SChris Wilson 
2896c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2897c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2898c2798b19SChris Wilson 
2899c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
290090a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
290190a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2902c2798b19SChris Wilson 
2903c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
290490a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
290590a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2906c2798b19SChris Wilson 
2907c2798b19SChris Wilson 		iir = new_iir;
2908c2798b19SChris Wilson 	}
2909c2798b19SChris Wilson 
2910c2798b19SChris Wilson 	return IRQ_HANDLED;
2911c2798b19SChris Wilson }
2912c2798b19SChris Wilson 
2913c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2914c2798b19SChris Wilson {
2915c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2916c2798b19SChris Wilson 	int pipe;
2917c2798b19SChris Wilson 
2918c2798b19SChris Wilson 	for_each_pipe(pipe) {
2919c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2920c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2921c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2922c2798b19SChris Wilson 	}
2923c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2924c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2925c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2926c2798b19SChris Wilson }
2927c2798b19SChris Wilson 
2928a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2929a266c7d5SChris Wilson {
2930a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2931a266c7d5SChris Wilson 	int pipe;
2932a266c7d5SChris Wilson 
2933a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2934a266c7d5SChris Wilson 
2935a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2936a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2937a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2938a266c7d5SChris Wilson 	}
2939a266c7d5SChris Wilson 
294000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2941a266c7d5SChris Wilson 	for_each_pipe(pipe)
2942a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2943a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2944a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2945a266c7d5SChris Wilson 	POSTING_READ(IER);
2946a266c7d5SChris Wilson }
2947a266c7d5SChris Wilson 
2948a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2949a266c7d5SChris Wilson {
2950a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
295138bde180SChris Wilson 	u32 enable_mask;
2952a266c7d5SChris Wilson 
295338bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
295438bde180SChris Wilson 
295538bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
295638bde180SChris Wilson 	dev_priv->irq_mask =
295738bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
295838bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
295938bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
296038bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
296138bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
296238bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
296338bde180SChris Wilson 
296438bde180SChris Wilson 	enable_mask =
296538bde180SChris Wilson 		I915_ASLE_INTERRUPT |
296638bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
296738bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
296838bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
296938bde180SChris Wilson 		I915_USER_INTERRUPT;
297038bde180SChris Wilson 
2971a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
297220afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
297320afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
297420afbda2SDaniel Vetter 
2975a266c7d5SChris Wilson 		/* Enable in IER... */
2976a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2977a266c7d5SChris Wilson 		/* and unmask in IMR */
2978a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2979a266c7d5SChris Wilson 	}
2980a266c7d5SChris Wilson 
2981a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2982a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2983a266c7d5SChris Wilson 	POSTING_READ(IER);
2984a266c7d5SChris Wilson 
298520afbda2SDaniel Vetter 	intel_opregion_enable_asle(dev);
298620afbda2SDaniel Vetter 
298720afbda2SDaniel Vetter 	return 0;
298820afbda2SDaniel Vetter }
298920afbda2SDaniel Vetter 
299090a72f87SVille Syrjälä /*
299190a72f87SVille Syrjälä  * Returns true when a page flip has completed.
299290a72f87SVille Syrjälä  */
299390a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
299490a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
299590a72f87SVille Syrjälä {
299690a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
299790a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
299890a72f87SVille Syrjälä 
299990a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
300090a72f87SVille Syrjälä 		return false;
300190a72f87SVille Syrjälä 
300290a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
300390a72f87SVille Syrjälä 		return false;
300490a72f87SVille Syrjälä 
300590a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
300690a72f87SVille Syrjälä 
300790a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
300890a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
300990a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
301090a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
301190a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
301290a72f87SVille Syrjälä 	 */
301390a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
301490a72f87SVille Syrjälä 		return false;
301590a72f87SVille Syrjälä 
301690a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
301790a72f87SVille Syrjälä 
301890a72f87SVille Syrjälä 	return true;
301990a72f87SVille Syrjälä }
302090a72f87SVille Syrjälä 
3021ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3022a266c7d5SChris Wilson {
3023a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3024a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
30258291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3026a266c7d5SChris Wilson 	unsigned long irqflags;
302738bde180SChris Wilson 	u32 flip_mask =
302838bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
302938bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
303038bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3031a266c7d5SChris Wilson 
3032a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3033a266c7d5SChris Wilson 
3034a266c7d5SChris Wilson 	iir = I915_READ(IIR);
303538bde180SChris Wilson 	do {
303638bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
30378291ee90SChris Wilson 		bool blc_event = false;
3038a266c7d5SChris Wilson 
3039a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3040a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3041a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3042a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3043a266c7d5SChris Wilson 		 */
3044a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3045a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3046a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3047a266c7d5SChris Wilson 
3048a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3049a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3050a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3051a266c7d5SChris Wilson 
305238bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3053a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3054a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3055a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3056a266c7d5SChris Wilson 							 pipe_name(pipe));
3057a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
305838bde180SChris Wilson 				irq_received = true;
3059a266c7d5SChris Wilson 			}
3060a266c7d5SChris Wilson 		}
3061a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3062a266c7d5SChris Wilson 
3063a266c7d5SChris Wilson 		if (!irq_received)
3064a266c7d5SChris Wilson 			break;
3065a266c7d5SChris Wilson 
3066a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3067a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3068a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3069a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3070b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3071a266c7d5SChris Wilson 
3072a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3073a266c7d5SChris Wilson 				  hotplug_status);
3074b543fb04SEgbert Eich 			if (hotplug_trigger) {
3075cd569aedSEgbert Eich 				if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_status_i915))
3076cd569aedSEgbert Eich 					i915_hpd_irq_setup(dev);
3077a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
3078a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
3079b543fb04SEgbert Eich 			}
3080a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
308138bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3082a266c7d5SChris Wilson 		}
3083a266c7d5SChris Wilson 
308438bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3085a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3086a266c7d5SChris Wilson 
3087a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3088a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3089a266c7d5SChris Wilson 
3090a266c7d5SChris Wilson 		for_each_pipe(pipe) {
309138bde180SChris Wilson 			int plane = pipe;
309238bde180SChris Wilson 			if (IS_MOBILE(dev))
309338bde180SChris Wilson 				plane = !plane;
30945e2032d4SVille Syrjälä 
309590a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
309690a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
309790a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3098a266c7d5SChris Wilson 
3099a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3100a266c7d5SChris Wilson 				blc_event = true;
3101a266c7d5SChris Wilson 		}
3102a266c7d5SChris Wilson 
3103a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3104a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3105a266c7d5SChris Wilson 
3106a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3107a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3108a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3109a266c7d5SChris Wilson 		 * we would never get another interrupt.
3110a266c7d5SChris Wilson 		 *
3111a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3112a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3113a266c7d5SChris Wilson 		 * another one.
3114a266c7d5SChris Wilson 		 *
3115a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3116a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3117a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3118a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3119a266c7d5SChris Wilson 		 * stray interrupts.
3120a266c7d5SChris Wilson 		 */
312138bde180SChris Wilson 		ret = IRQ_HANDLED;
3122a266c7d5SChris Wilson 		iir = new_iir;
312338bde180SChris Wilson 	} while (iir & ~flip_mask);
3124a266c7d5SChris Wilson 
3125d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
31268291ee90SChris Wilson 
3127a266c7d5SChris Wilson 	return ret;
3128a266c7d5SChris Wilson }
3129a266c7d5SChris Wilson 
3130a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3131a266c7d5SChris Wilson {
3132a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3133a266c7d5SChris Wilson 	int pipe;
3134a266c7d5SChris Wilson 
3135ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3136ac4c16c5SEgbert Eich 
3137a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3138a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3139a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3140a266c7d5SChris Wilson 	}
3141a266c7d5SChris Wilson 
314200d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
314355b39755SChris Wilson 	for_each_pipe(pipe) {
314455b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3145a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
314655b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
314755b39755SChris Wilson 	}
3148a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3149a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3150a266c7d5SChris Wilson 
3151a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3152a266c7d5SChris Wilson }
3153a266c7d5SChris Wilson 
3154a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3155a266c7d5SChris Wilson {
3156a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3157a266c7d5SChris Wilson 	int pipe;
3158a266c7d5SChris Wilson 
3159a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3160a266c7d5SChris Wilson 
3161a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3162a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3163a266c7d5SChris Wilson 
3164a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3165a266c7d5SChris Wilson 	for_each_pipe(pipe)
3166a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3167a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3168a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3169a266c7d5SChris Wilson 	POSTING_READ(IER);
3170a266c7d5SChris Wilson }
3171a266c7d5SChris Wilson 
3172a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3173a266c7d5SChris Wilson {
3174a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3175bbba0a97SChris Wilson 	u32 enable_mask;
3176a266c7d5SChris Wilson 	u32 error_mask;
3177a266c7d5SChris Wilson 
3178a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3179bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3180adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3181bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3182bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3183bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3184bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3185bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3186bbba0a97SChris Wilson 
3187bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
318821ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
318921ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3190bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3191bbba0a97SChris Wilson 
3192bbba0a97SChris Wilson 	if (IS_G4X(dev))
3193bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3194a266c7d5SChris Wilson 
3195515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
3196a266c7d5SChris Wilson 
3197a266c7d5SChris Wilson 	/*
3198a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3199a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3200a266c7d5SChris Wilson 	 */
3201a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3202a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3203a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3204a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3205a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3206a266c7d5SChris Wilson 	} else {
3207a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3208a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3209a266c7d5SChris Wilson 	}
3210a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3211a266c7d5SChris Wilson 
3212a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3213a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3214a266c7d5SChris Wilson 	POSTING_READ(IER);
3215a266c7d5SChris Wilson 
321620afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
321720afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
321820afbda2SDaniel Vetter 
321920afbda2SDaniel Vetter 	intel_opregion_enable_asle(dev);
322020afbda2SDaniel Vetter 
322120afbda2SDaniel Vetter 	return 0;
322220afbda2SDaniel Vetter }
322320afbda2SDaniel Vetter 
3224bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
322520afbda2SDaniel Vetter {
322620afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3227e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3228cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
322920afbda2SDaniel Vetter 	u32 hotplug_en;
323020afbda2SDaniel Vetter 
3231bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3232bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3233bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3234adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3235e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3236cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3237cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3238cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3239a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3240a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3241a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3242a266c7d5SChris Wilson 		*/
3243a266c7d5SChris Wilson 		if (IS_G4X(dev))
3244a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
324585fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3246a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3247a266c7d5SChris Wilson 
3248a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3249a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3250a266c7d5SChris Wilson 	}
3251bac56d5bSEgbert Eich }
3252a266c7d5SChris Wilson 
3253ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3254a266c7d5SChris Wilson {
3255a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3256a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3257a266c7d5SChris Wilson 	u32 iir, new_iir;
3258a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3259a266c7d5SChris Wilson 	unsigned long irqflags;
3260a266c7d5SChris Wilson 	int irq_received;
3261a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
326221ad8330SVille Syrjälä 	u32 flip_mask =
326321ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
326421ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3265a266c7d5SChris Wilson 
3266a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3267a266c7d5SChris Wilson 
3268a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3269a266c7d5SChris Wilson 
3270a266c7d5SChris Wilson 	for (;;) {
32712c8ba29fSChris Wilson 		bool blc_event = false;
32722c8ba29fSChris Wilson 
327321ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3274a266c7d5SChris Wilson 
3275a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3276a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3277a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3278a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3279a266c7d5SChris Wilson 		 */
3280a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3281a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3282a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3283a266c7d5SChris Wilson 
3284a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3285a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3286a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3287a266c7d5SChris Wilson 
3288a266c7d5SChris Wilson 			/*
3289a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3290a266c7d5SChris Wilson 			 */
3291a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3292a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3293a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3294a266c7d5SChris Wilson 							 pipe_name(pipe));
3295a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3296a266c7d5SChris Wilson 				irq_received = 1;
3297a266c7d5SChris Wilson 			}
3298a266c7d5SChris Wilson 		}
3299a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3300a266c7d5SChris Wilson 
3301a266c7d5SChris Wilson 		if (!irq_received)
3302a266c7d5SChris Wilson 			break;
3303a266c7d5SChris Wilson 
3304a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3305a266c7d5SChris Wilson 
3306a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3307adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3308a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3309b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3310b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
3311b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_I965);
3312a266c7d5SChris Wilson 
3313a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3314a266c7d5SChris Wilson 				  hotplug_status);
3315b543fb04SEgbert Eich 			if (hotplug_trigger) {
3316cd569aedSEgbert Eich 				if (hotplug_irq_storm_detect(dev, hotplug_trigger,
3317cd569aedSEgbert Eich 							    IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i965))
3318cd569aedSEgbert Eich 					i915_hpd_irq_setup(dev);
3319a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
3320a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
3321b543fb04SEgbert Eich 			}
3322a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3323a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3324a266c7d5SChris Wilson 		}
3325a266c7d5SChris Wilson 
332621ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3327a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3328a266c7d5SChris Wilson 
3329a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3330a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3331a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3332a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3333a266c7d5SChris Wilson 
3334a266c7d5SChris Wilson 		for_each_pipe(pipe) {
33352c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
333690a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
333790a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3338a266c7d5SChris Wilson 
3339a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3340a266c7d5SChris Wilson 				blc_event = true;
3341a266c7d5SChris Wilson 		}
3342a266c7d5SChris Wilson 
3343a266c7d5SChris Wilson 
3344a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3345a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3346a266c7d5SChris Wilson 
3347515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3348515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3349515ac2bbSDaniel Vetter 
3350a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3351a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3352a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3353a266c7d5SChris Wilson 		 * we would never get another interrupt.
3354a266c7d5SChris Wilson 		 *
3355a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3356a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3357a266c7d5SChris Wilson 		 * another one.
3358a266c7d5SChris Wilson 		 *
3359a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3360a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3361a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3362a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3363a266c7d5SChris Wilson 		 * stray interrupts.
3364a266c7d5SChris Wilson 		 */
3365a266c7d5SChris Wilson 		iir = new_iir;
3366a266c7d5SChris Wilson 	}
3367a266c7d5SChris Wilson 
3368d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
33692c8ba29fSChris Wilson 
3370a266c7d5SChris Wilson 	return ret;
3371a266c7d5SChris Wilson }
3372a266c7d5SChris Wilson 
3373a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3374a266c7d5SChris Wilson {
3375a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3376a266c7d5SChris Wilson 	int pipe;
3377a266c7d5SChris Wilson 
3378a266c7d5SChris Wilson 	if (!dev_priv)
3379a266c7d5SChris Wilson 		return;
3380a266c7d5SChris Wilson 
3381ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3382ac4c16c5SEgbert Eich 
3383a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3384a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3385a266c7d5SChris Wilson 
3386a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3387a266c7d5SChris Wilson 	for_each_pipe(pipe)
3388a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3389a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3390a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3391a266c7d5SChris Wilson 
3392a266c7d5SChris Wilson 	for_each_pipe(pipe)
3393a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3394a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3395a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3396a266c7d5SChris Wilson }
3397a266c7d5SChris Wilson 
3398ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3399ac4c16c5SEgbert Eich {
3400ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3401ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3402ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3403ac4c16c5SEgbert Eich 	unsigned long irqflags;
3404ac4c16c5SEgbert Eich 	int i;
3405ac4c16c5SEgbert Eich 
3406ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3407ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3408ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3409ac4c16c5SEgbert Eich 
3410ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3411ac4c16c5SEgbert Eich 			continue;
3412ac4c16c5SEgbert Eich 
3413ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3414ac4c16c5SEgbert Eich 
3415ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3416ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3417ac4c16c5SEgbert Eich 
3418ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3419ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3420ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3421ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3422ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3423ac4c16c5SEgbert Eich 				if (!connector->polled)
3424ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3425ac4c16c5SEgbert Eich 			}
3426ac4c16c5SEgbert Eich 		}
3427ac4c16c5SEgbert Eich 	}
3428ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3429ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3430ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3431ac4c16c5SEgbert Eich }
3432ac4c16c5SEgbert Eich 
3433f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3434f71d4af4SJesse Barnes {
34358b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
34368b2e326dSChris Wilson 
34378b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
343899584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3439c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3440a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
34418b2e326dSChris Wilson 
344299584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
344399584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
344461bac78eSDaniel Vetter 		    (unsigned long) dev);
3445ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3446ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
344761bac78eSDaniel Vetter 
344897a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
34499ee32feaSDaniel Vetter 
3450f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
3451f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
34527d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3453f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3454f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3455f71d4af4SJesse Barnes 	}
3456f71d4af4SJesse Barnes 
3457c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3458f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3459c3613de9SKeith Packard 	else
3460c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
3461f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3462f71d4af4SJesse Barnes 
34637e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
34647e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
34657e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
34667e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
34677e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
34687e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
34697e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3470fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
34714a06e201SDaniel Vetter 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
3472f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
3473f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
3474f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3475f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
3476f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3477f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
3478f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
347982a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3480f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3481f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3482f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3483f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3484f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3485f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3486f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
348782a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3488f71d4af4SJesse Barnes 	} else {
3489c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3490c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3491c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3492c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3493c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3494a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3495a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3496a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3497a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3498a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
349920afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3500c2798b19SChris Wilson 		} else {
3501a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3502a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3503a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3504a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3505bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3506c2798b19SChris Wilson 		}
3507f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3508f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3509f71d4af4SJesse Barnes 	}
3510f71d4af4SJesse Barnes }
351120afbda2SDaniel Vetter 
351220afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
351320afbda2SDaniel Vetter {
351420afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3515821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3516821450c6SEgbert Eich 	struct drm_connector *connector;
3517821450c6SEgbert Eich 	int i;
351820afbda2SDaniel Vetter 
3519821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3520821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3521821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3522821450c6SEgbert Eich 	}
3523821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3524821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3525821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3526821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3527821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3528821450c6SEgbert Eich 	}
352920afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
353020afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
353120afbda2SDaniel Vetter }
3532