xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 7d99163da69e04ccae0b52093f716167ed71d66d)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33760285e7SDavid Howells #include <drm/drmP.h>
34760285e7SDavid Howells #include <drm/i915_drm.h>
35c0e09200SDave Airlie #include "i915_drv.h"
361c5d22f7SChris Wilson #include "i915_trace.h"
3779e53945SJesse Barnes #include "intel_drv.h"
38c0e09200SDave Airlie 
39e5868a31SEgbert Eich static const u32 hpd_ibx[] = {
40e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
41e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
42e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
43e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
44e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
45e5868a31SEgbert Eich };
46e5868a31SEgbert Eich 
47e5868a31SEgbert Eich static const u32 hpd_cpt[] = {
48e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
4973c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
50e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
51e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
52e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
53e5868a31SEgbert Eich };
54e5868a31SEgbert Eich 
55e5868a31SEgbert Eich static const u32 hpd_mask_i915[] = {
56e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
57e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
58e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
59e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
60e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
61e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
62e5868a31SEgbert Eich };
63e5868a31SEgbert Eich 
64e5868a31SEgbert Eich static const u32 hpd_status_gen4[] = {
65e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
66e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
67e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
68e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
69e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
70e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
71e5868a31SEgbert Eich };
72e5868a31SEgbert Eich 
73e5868a31SEgbert Eich static const u32 hpd_status_i965[] = {
74e5868a31SEgbert Eich 	 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
75e5868a31SEgbert Eich 	 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I965,
76e5868a31SEgbert Eich 	 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I965,
77e5868a31SEgbert Eich 	 [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
78e5868a31SEgbert Eich 	 [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
79e5868a31SEgbert Eich 	 [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
80e5868a31SEgbert Eich };
81e5868a31SEgbert Eich 
82e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
83e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
84e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
85e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
86e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
87e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
88e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
89e5868a31SEgbert Eich };
90e5868a31SEgbert Eich 
91cd569aedSEgbert Eich static void ibx_hpd_irq_setup(struct drm_device *dev);
92cd569aedSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev);
93e5868a31SEgbert Eich 
94036a4a7dSZhenyu Wang /* For display hotplug interrupt */
95995b6762SChris Wilson static void
96f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
97036a4a7dSZhenyu Wang {
981ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
991ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
1001ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1013143a2bfSChris Wilson 		POSTING_READ(DEIMR);
102036a4a7dSZhenyu Wang 	}
103036a4a7dSZhenyu Wang }
104036a4a7dSZhenyu Wang 
1050ff9800aSPaulo Zanoni static void
106f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
107036a4a7dSZhenyu Wang {
1081ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1091ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1101ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1113143a2bfSChris Wilson 		POSTING_READ(DEIMR);
112036a4a7dSZhenyu Wang 	}
113036a4a7dSZhenyu Wang }
114036a4a7dSZhenyu Wang 
1158664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
1168664281bSPaulo Zanoni {
1178664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1188664281bSPaulo Zanoni 	struct intel_crtc *crtc;
1198664281bSPaulo Zanoni 	enum pipe pipe;
1208664281bSPaulo Zanoni 
1218664281bSPaulo Zanoni 	for_each_pipe(pipe) {
1228664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
1238664281bSPaulo Zanoni 
1248664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
1258664281bSPaulo Zanoni 			return false;
1268664281bSPaulo Zanoni 	}
1278664281bSPaulo Zanoni 
1288664281bSPaulo Zanoni 	return true;
1298664281bSPaulo Zanoni }
1308664281bSPaulo Zanoni 
1318664281bSPaulo Zanoni static bool cpt_can_enable_serr_int(struct drm_device *dev)
1328664281bSPaulo Zanoni {
1338664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1348664281bSPaulo Zanoni 	enum pipe pipe;
1358664281bSPaulo Zanoni 	struct intel_crtc *crtc;
1368664281bSPaulo Zanoni 
1378664281bSPaulo Zanoni 	for_each_pipe(pipe) {
1388664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
1398664281bSPaulo Zanoni 
1408664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
1418664281bSPaulo Zanoni 			return false;
1428664281bSPaulo Zanoni 	}
1438664281bSPaulo Zanoni 
1448664281bSPaulo Zanoni 	return true;
1458664281bSPaulo Zanoni }
1468664281bSPaulo Zanoni 
1478664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
1488664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
1498664281bSPaulo Zanoni {
1508664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1518664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
1528664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
1538664281bSPaulo Zanoni 
1548664281bSPaulo Zanoni 	if (enable)
1558664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
1568664281bSPaulo Zanoni 	else
1578664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
1588664281bSPaulo Zanoni }
1598664281bSPaulo Zanoni 
1608664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
1618664281bSPaulo Zanoni 						  bool enable)
1628664281bSPaulo Zanoni {
1638664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1648664281bSPaulo Zanoni 
1658664281bSPaulo Zanoni 	if (enable) {
1668664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
1678664281bSPaulo Zanoni 			return;
1688664281bSPaulo Zanoni 
1698664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN_A |
1708664281bSPaulo Zanoni 					 ERR_INT_FIFO_UNDERRUN_B |
1718664281bSPaulo Zanoni 					 ERR_INT_FIFO_UNDERRUN_C);
1728664281bSPaulo Zanoni 
1738664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
1748664281bSPaulo Zanoni 	} else {
1758664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
1768664281bSPaulo Zanoni 	}
1778664281bSPaulo Zanoni }
1788664281bSPaulo Zanoni 
1798664281bSPaulo Zanoni static void ibx_set_fifo_underrun_reporting(struct intel_crtc *crtc,
1808664281bSPaulo Zanoni 					    bool enable)
1818664281bSPaulo Zanoni {
1828664281bSPaulo Zanoni 	struct drm_device *dev = crtc->base.dev;
1838664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1848664281bSPaulo Zanoni 	uint32_t bit = (crtc->pipe == PIPE_A) ? SDE_TRANSA_FIFO_UNDER :
1858664281bSPaulo Zanoni 						SDE_TRANSB_FIFO_UNDER;
1868664281bSPaulo Zanoni 
1878664281bSPaulo Zanoni 	if (enable)
1888664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) & ~bit);
1898664281bSPaulo Zanoni 	else
1908664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) | bit);
1918664281bSPaulo Zanoni 
1928664281bSPaulo Zanoni 	POSTING_READ(SDEIMR);
1938664281bSPaulo Zanoni }
1948664281bSPaulo Zanoni 
1958664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
1968664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
1978664281bSPaulo Zanoni 					    bool enable)
1988664281bSPaulo Zanoni {
1998664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2008664281bSPaulo Zanoni 
2018664281bSPaulo Zanoni 	if (enable) {
2028664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
2038664281bSPaulo Zanoni 			return;
2048664281bSPaulo Zanoni 
2058664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, SERR_INT_TRANS_A_FIFO_UNDERRUN |
2068664281bSPaulo Zanoni 				     SERR_INT_TRANS_B_FIFO_UNDERRUN |
2078664281bSPaulo Zanoni 				     SERR_INT_TRANS_C_FIFO_UNDERRUN);
2088664281bSPaulo Zanoni 
2098664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) & ~SDE_ERROR_CPT);
2108664281bSPaulo Zanoni 	} else {
2118664281bSPaulo Zanoni 		I915_WRITE(SDEIMR, I915_READ(SDEIMR) | SDE_ERROR_CPT);
2128664281bSPaulo Zanoni 	}
2138664281bSPaulo Zanoni 
2148664281bSPaulo Zanoni 	POSTING_READ(SDEIMR);
2158664281bSPaulo Zanoni }
2168664281bSPaulo Zanoni 
2178664281bSPaulo Zanoni /**
2188664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
2198664281bSPaulo Zanoni  * @dev: drm device
2208664281bSPaulo Zanoni  * @pipe: pipe
2218664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
2228664281bSPaulo Zanoni  *
2238664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
2248664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
2258664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
2268664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
2278664281bSPaulo Zanoni  * bit for all the pipes.
2288664281bSPaulo Zanoni  *
2298664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
2308664281bSPaulo Zanoni  */
2318664281bSPaulo Zanoni bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
2328664281bSPaulo Zanoni 					   enum pipe pipe, bool enable)
2338664281bSPaulo Zanoni {
2348664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2358664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
2368664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2378664281bSPaulo Zanoni 	unsigned long flags;
2388664281bSPaulo Zanoni 	bool ret;
2398664281bSPaulo Zanoni 
2408664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
2418664281bSPaulo Zanoni 
2428664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
2438664281bSPaulo Zanoni 
2448664281bSPaulo Zanoni 	if (enable == ret)
2458664281bSPaulo Zanoni 		goto done;
2468664281bSPaulo Zanoni 
2478664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
2488664281bSPaulo Zanoni 
2498664281bSPaulo Zanoni 	if (IS_GEN5(dev) || IS_GEN6(dev))
2508664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
2518664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
2528664281bSPaulo Zanoni 		ivybridge_set_fifo_underrun_reporting(dev, enable);
2538664281bSPaulo Zanoni 
2548664281bSPaulo Zanoni done:
2558664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
2568664281bSPaulo Zanoni 	return ret;
2578664281bSPaulo Zanoni }
2588664281bSPaulo Zanoni 
2598664281bSPaulo Zanoni /**
2608664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
2618664281bSPaulo Zanoni  * @dev: drm device
2628664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
2638664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
2648664281bSPaulo Zanoni  *
2658664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
2668664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
2678664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
2688664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
2698664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
2708664281bSPaulo Zanoni  *
2718664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
2728664281bSPaulo Zanoni  */
2738664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
2748664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
2758664281bSPaulo Zanoni 					   bool enable)
2768664281bSPaulo Zanoni {
2778664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2788664281bSPaulo Zanoni 	enum pipe p;
2798664281bSPaulo Zanoni 	struct drm_crtc *crtc;
2808664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc;
2818664281bSPaulo Zanoni 	unsigned long flags;
2828664281bSPaulo Zanoni 	bool ret;
2838664281bSPaulo Zanoni 
2848664281bSPaulo Zanoni 	if (HAS_PCH_LPT(dev)) {
2858664281bSPaulo Zanoni 		crtc = NULL;
2868664281bSPaulo Zanoni 		for_each_pipe(p) {
2878664281bSPaulo Zanoni 			struct drm_crtc *c = dev_priv->pipe_to_crtc_mapping[p];
2888664281bSPaulo Zanoni 			if (intel_pipe_has_type(c, INTEL_OUTPUT_ANALOG)) {
2898664281bSPaulo Zanoni 				crtc = c;
2908664281bSPaulo Zanoni 				break;
2918664281bSPaulo Zanoni 			}
2928664281bSPaulo Zanoni 		}
2938664281bSPaulo Zanoni 		if (!crtc) {
2948664281bSPaulo Zanoni 			DRM_ERROR("PCH FIFO underrun, but no CRTC using the PCH found\n");
2958664281bSPaulo Zanoni 			return false;
2968664281bSPaulo Zanoni 		}
2978664281bSPaulo Zanoni 	} else {
2988664281bSPaulo Zanoni 		crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
2998664281bSPaulo Zanoni 	}
3008664281bSPaulo Zanoni 	intel_crtc = to_intel_crtc(crtc);
3018664281bSPaulo Zanoni 
3028664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
3038664281bSPaulo Zanoni 
3048664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
3058664281bSPaulo Zanoni 
3068664281bSPaulo Zanoni 	if (enable == ret)
3078664281bSPaulo Zanoni 		goto done;
3088664281bSPaulo Zanoni 
3098664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
3108664281bSPaulo Zanoni 
3118664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
3128664281bSPaulo Zanoni 		ibx_set_fifo_underrun_reporting(intel_crtc, enable);
3138664281bSPaulo Zanoni 	else
3148664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
3158664281bSPaulo Zanoni 
3168664281bSPaulo Zanoni done:
3178664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
3188664281bSPaulo Zanoni 	return ret;
3198664281bSPaulo Zanoni }
3208664281bSPaulo Zanoni 
3218664281bSPaulo Zanoni 
3227c463586SKeith Packard void
3237c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3247c463586SKeith Packard {
3259db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
32646c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3277c463586SKeith Packard 
32846c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
32946c06a30SVille Syrjälä 		return;
33046c06a30SVille Syrjälä 
3317c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
33246c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
33346c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3343143a2bfSChris Wilson 	POSTING_READ(reg);
3357c463586SKeith Packard }
3367c463586SKeith Packard 
3377c463586SKeith Packard void
3387c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3397c463586SKeith Packard {
3409db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
34146c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3427c463586SKeith Packard 
34346c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
34446c06a30SVille Syrjälä 		return;
34546c06a30SVille Syrjälä 
34646c06a30SVille Syrjälä 	pipestat &= ~mask;
34746c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3483143a2bfSChris Wilson 	POSTING_READ(reg);
3497c463586SKeith Packard }
3507c463586SKeith Packard 
351c0e09200SDave Airlie /**
352f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
35301c66889SZhao Yakui  */
354f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
35501c66889SZhao Yakui {
3561ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
3571ec14ad3SChris Wilson 	unsigned long irqflags;
3581ec14ad3SChris Wilson 
359f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
360f49e38ddSJani Nikula 		return;
361f49e38ddSJani Nikula 
3621ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
36301c66889SZhao Yakui 
364f898780bSJani Nikula 	i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
365a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
366f898780bSJani Nikula 		i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
3671ec14ad3SChris Wilson 
3681ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
36901c66889SZhao Yakui }
37001c66889SZhao Yakui 
37101c66889SZhao Yakui /**
3720a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
3730a3e67a4SJesse Barnes  * @dev: DRM device
3740a3e67a4SJesse Barnes  * @pipe: pipe to check
3750a3e67a4SJesse Barnes  *
3760a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
3770a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
3780a3e67a4SJesse Barnes  * before reading such registers if unsure.
3790a3e67a4SJesse Barnes  */
3800a3e67a4SJesse Barnes static int
3810a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
3820a3e67a4SJesse Barnes {
3830a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
384702e7a56SPaulo Zanoni 
385a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
386a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
387a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
388a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
38971f8ba6bSPaulo Zanoni 
390a01025afSDaniel Vetter 		return intel_crtc->active;
391a01025afSDaniel Vetter 	} else {
392a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
393a01025afSDaniel Vetter 	}
3940a3e67a4SJesse Barnes }
3950a3e67a4SJesse Barnes 
39642f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
39742f52ef8SKeith Packard  * we use as a pipe index
39842f52ef8SKeith Packard  */
399f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
4000a3e67a4SJesse Barnes {
4010a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4020a3e67a4SJesse Barnes 	unsigned long high_frame;
4030a3e67a4SJesse Barnes 	unsigned long low_frame;
4045eddb70bSChris Wilson 	u32 high1, high2, low;
4050a3e67a4SJesse Barnes 
4060a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
40744d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4089db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
4090a3e67a4SJesse Barnes 		return 0;
4100a3e67a4SJesse Barnes 	}
4110a3e67a4SJesse Barnes 
4129db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
4139db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
4145eddb70bSChris Wilson 
4150a3e67a4SJesse Barnes 	/*
4160a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
4170a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
4180a3e67a4SJesse Barnes 	 * register.
4190a3e67a4SJesse Barnes 	 */
4200a3e67a4SJesse Barnes 	do {
4215eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4225eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
4235eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4240a3e67a4SJesse Barnes 	} while (high1 != high2);
4250a3e67a4SJesse Barnes 
4265eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
4275eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
4285eddb70bSChris Wilson 	return (high1 << 8) | low;
4290a3e67a4SJesse Barnes }
4300a3e67a4SJesse Barnes 
431f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
4329880b7a5SJesse Barnes {
4339880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4349db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
4359880b7a5SJesse Barnes 
4369880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
43744d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4389db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4399880b7a5SJesse Barnes 		return 0;
4409880b7a5SJesse Barnes 	}
4419880b7a5SJesse Barnes 
4429880b7a5SJesse Barnes 	return I915_READ(reg);
4439880b7a5SJesse Barnes }
4449880b7a5SJesse Barnes 
445f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
4460af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
4470af7e4dfSMario Kleiner {
4480af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4490af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
4500af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
4510af7e4dfSMario Kleiner 	bool in_vbl = true;
4520af7e4dfSMario Kleiner 	int ret = 0;
453fe2b8f9dSPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
454fe2b8f9dSPaulo Zanoni 								      pipe);
4550af7e4dfSMario Kleiner 
4560af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
4570af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
4589db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4590af7e4dfSMario Kleiner 		return 0;
4600af7e4dfSMario Kleiner 	}
4610af7e4dfSMario Kleiner 
4620af7e4dfSMario Kleiner 	/* Get vtotal. */
463fe2b8f9dSPaulo Zanoni 	vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4640af7e4dfSMario Kleiner 
4650af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
4660af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
4670af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
4680af7e4dfSMario Kleiner 		 */
4690af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
4700af7e4dfSMario Kleiner 
4710af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
4720af7e4dfSMario Kleiner 		 * horizontal scanout position.
4730af7e4dfSMario Kleiner 		 */
4740af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
4750af7e4dfSMario Kleiner 		*hpos = 0;
4760af7e4dfSMario Kleiner 	} else {
4770af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
4780af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
4790af7e4dfSMario Kleiner 		 * scanout position.
4800af7e4dfSMario Kleiner 		 */
4810af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
4820af7e4dfSMario Kleiner 
483fe2b8f9dSPaulo Zanoni 		htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4840af7e4dfSMario Kleiner 		*vpos = position / htotal;
4850af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
4860af7e4dfSMario Kleiner 	}
4870af7e4dfSMario Kleiner 
4880af7e4dfSMario Kleiner 	/* Query vblank area. */
489fe2b8f9dSPaulo Zanoni 	vbl = I915_READ(VBLANK(cpu_transcoder));
4900af7e4dfSMario Kleiner 
4910af7e4dfSMario Kleiner 	/* Test position against vblank region. */
4920af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
4930af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
4940af7e4dfSMario Kleiner 
4950af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
4960af7e4dfSMario Kleiner 		in_vbl = false;
4970af7e4dfSMario Kleiner 
4980af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
4990af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
5000af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
5010af7e4dfSMario Kleiner 
5020af7e4dfSMario Kleiner 	/* Readouts valid? */
5030af7e4dfSMario Kleiner 	if (vbl > 0)
5040af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
5050af7e4dfSMario Kleiner 
5060af7e4dfSMario Kleiner 	/* In vblank? */
5070af7e4dfSMario Kleiner 	if (in_vbl)
5080af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
5090af7e4dfSMario Kleiner 
5100af7e4dfSMario Kleiner 	return ret;
5110af7e4dfSMario Kleiner }
5120af7e4dfSMario Kleiner 
513f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
5140af7e4dfSMario Kleiner 			      int *max_error,
5150af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
5160af7e4dfSMario Kleiner 			      unsigned flags)
5170af7e4dfSMario Kleiner {
5184041b853SChris Wilson 	struct drm_crtc *crtc;
5190af7e4dfSMario Kleiner 
5207eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
5214041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5220af7e4dfSMario Kleiner 		return -EINVAL;
5230af7e4dfSMario Kleiner 	}
5240af7e4dfSMario Kleiner 
5250af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
5264041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
5274041b853SChris Wilson 	if (crtc == NULL) {
5284041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5294041b853SChris Wilson 		return -EINVAL;
5304041b853SChris Wilson 	}
5314041b853SChris Wilson 
5324041b853SChris Wilson 	if (!crtc->enabled) {
5334041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
5344041b853SChris Wilson 		return -EBUSY;
5354041b853SChris Wilson 	}
5360af7e4dfSMario Kleiner 
5370af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
5384041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
5394041b853SChris Wilson 						     vblank_time, flags,
5404041b853SChris Wilson 						     crtc);
5410af7e4dfSMario Kleiner }
5420af7e4dfSMario Kleiner 
543321a1b30SEgbert Eich static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
544321a1b30SEgbert Eich {
545321a1b30SEgbert Eich 	enum drm_connector_status old_status;
546321a1b30SEgbert Eich 
547321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
548321a1b30SEgbert Eich 	old_status = connector->status;
549321a1b30SEgbert Eich 
550321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
551321a1b30SEgbert Eich 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
552321a1b30SEgbert Eich 		      connector->base.id,
553321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
554321a1b30SEgbert Eich 		      old_status, connector->status);
555321a1b30SEgbert Eich 	return (old_status != connector->status);
556321a1b30SEgbert Eich }
557321a1b30SEgbert Eich 
5585ca58282SJesse Barnes /*
5595ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
5605ca58282SJesse Barnes  */
561ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
562ac4c16c5SEgbert Eich 
5635ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
5645ca58282SJesse Barnes {
5655ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5665ca58282SJesse Barnes 						    hotplug_work);
5675ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
568c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
569cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
570cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
571cd569aedSEgbert Eich 	struct drm_connector *connector;
572cd569aedSEgbert Eich 	unsigned long irqflags;
573cd569aedSEgbert Eich 	bool hpd_disabled = false;
574321a1b30SEgbert Eich 	bool changed = false;
575142e2398SEgbert Eich 	u32 hpd_event_bits;
5765ca58282SJesse Barnes 
57752d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
57852d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
57952d7ecedSDaniel Vetter 		return;
58052d7ecedSDaniel Vetter 
581a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
582e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
583e67189abSJesse Barnes 
584cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
585142e2398SEgbert Eich 
586142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
587142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
588cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
589cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
590cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
591cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
592cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
593cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
594cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
595cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
596cd569aedSEgbert Eich 				drm_get_connector_name(connector));
597cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
598cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
599cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
600cd569aedSEgbert Eich 			hpd_disabled = true;
601cd569aedSEgbert Eich 		}
602142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
603142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
604142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
605142e2398SEgbert Eich 		}
606cd569aedSEgbert Eich 	}
607cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
608cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
609cd569aedSEgbert Eich 	  * some connectors */
610ac4c16c5SEgbert Eich 	if (hpd_disabled) {
611cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
612ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
613ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
614ac4c16c5SEgbert Eich 	}
615cd569aedSEgbert Eich 
616cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
617cd569aedSEgbert Eich 
618321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
619321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
620321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
621321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
622cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
623cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
624321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
625321a1b30SEgbert Eich 				changed = true;
626321a1b30SEgbert Eich 		}
627321a1b30SEgbert Eich 	}
62840ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
62940ee3381SKeith Packard 
630321a1b30SEgbert Eich 	if (changed)
631321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
6325ca58282SJesse Barnes }
6335ca58282SJesse Barnes 
63473edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
635f97108d1SJesse Barnes {
636f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
637b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
6389270388eSDaniel Vetter 	u8 new_delay;
6399270388eSDaniel Vetter 	unsigned long flags;
6409270388eSDaniel Vetter 
6419270388eSDaniel Vetter 	spin_lock_irqsave(&mchdev_lock, flags);
642f97108d1SJesse Barnes 
64373edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
64473edd18fSDaniel Vetter 
64520e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
6469270388eSDaniel Vetter 
6477648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
648b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
649b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
650f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
651f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
652f97108d1SJesse Barnes 
653f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
654b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
65520e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
65620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
65720e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
65820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
659b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
66020e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
66120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
66220e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
66320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
664f97108d1SJesse Barnes 	}
665f97108d1SJesse Barnes 
6667648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
66720e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
668f97108d1SJesse Barnes 
6699270388eSDaniel Vetter 	spin_unlock_irqrestore(&mchdev_lock, flags);
6709270388eSDaniel Vetter 
671f97108d1SJesse Barnes 	return;
672f97108d1SJesse Barnes }
673f97108d1SJesse Barnes 
674549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
675549f7365SChris Wilson 			struct intel_ring_buffer *ring)
676549f7365SChris Wilson {
677549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
6789862e600SChris Wilson 
679475553deSChris Wilson 	if (ring->obj == NULL)
680475553deSChris Wilson 		return;
681475553deSChris Wilson 
682b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
6839862e600SChris Wilson 
684549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
6853e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
68699584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
68799584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
688cecc21feSChris Wilson 			  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
6893e0dc6b0SBen Widawsky 	}
690549f7365SChris Wilson }
691549f7365SChris Wilson 
6924912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
6933b8d8d91SJesse Barnes {
6944912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
695c6a828d3SDaniel Vetter 						    rps.work);
6964912d041SBen Widawsky 	u32 pm_iir, pm_imr;
6977b9e0ae6SChris Wilson 	u8 new_delay;
6983b8d8d91SJesse Barnes 
699c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
700c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
701c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
7024912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
703a9e2641dSDaniel Vetter 	I915_WRITE(GEN6_PMIMR, 0);
704c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
7054912d041SBen Widawsky 
7067b9e0ae6SChris Wilson 	if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
7073b8d8d91SJesse Barnes 		return;
7083b8d8d91SJesse Barnes 
7094fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
7107b9e0ae6SChris Wilson 
7117b9e0ae6SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
712c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
7137b9e0ae6SChris Wilson 	else
714c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
7153b8d8d91SJesse Barnes 
71679249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
71779249636SBen Widawsky 	 * interrupt
71879249636SBen Widawsky 	 */
71979249636SBen Widawsky 	if (!(new_delay > dev_priv->rps.max_delay ||
72079249636SBen Widawsky 	      new_delay < dev_priv->rps.min_delay)) {
7210a073b84SJesse Barnes 		if (IS_VALLEYVIEW(dev_priv->dev))
7220a073b84SJesse Barnes 			valleyview_set_rps(dev_priv->dev, new_delay);
7230a073b84SJesse Barnes 		else
7244912d041SBen Widawsky 			gen6_set_rps(dev_priv->dev, new_delay);
72579249636SBen Widawsky 	}
7263b8d8d91SJesse Barnes 
72752ceb908SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev)) {
72852ceb908SJesse Barnes 		/*
72952ceb908SJesse Barnes 		 * On VLV, when we enter RC6 we may not be at the minimum
73052ceb908SJesse Barnes 		 * voltage level, so arm a timer to check.  It should only
73152ceb908SJesse Barnes 		 * fire when there's activity or once after we've entered
73252ceb908SJesse Barnes 		 * RC6, and then won't be re-armed until the next RPS interrupt.
73352ceb908SJesse Barnes 		 */
73452ceb908SJesse Barnes 		mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
73552ceb908SJesse Barnes 				 msecs_to_jiffies(100));
73652ceb908SJesse Barnes 	}
73752ceb908SJesse Barnes 
7384fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
7393b8d8d91SJesse Barnes }
7403b8d8d91SJesse Barnes 
741e3689190SBen Widawsky 
742e3689190SBen Widawsky /**
743e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
744e3689190SBen Widawsky  * occurred.
745e3689190SBen Widawsky  * @work: workqueue struct
746e3689190SBen Widawsky  *
747e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
748e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
749e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
750e3689190SBen Widawsky  */
751e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
752e3689190SBen Widawsky {
753e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
754a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
755e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
756e3689190SBen Widawsky 	char *parity_event[5];
757e3689190SBen Widawsky 	uint32_t misccpctl;
758e3689190SBen Widawsky 	unsigned long flags;
759e3689190SBen Widawsky 
760e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
761e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
762e3689190SBen Widawsky 	 * any time we access those registers.
763e3689190SBen Widawsky 	 */
764e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
765e3689190SBen Widawsky 
766e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
767e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
768e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
769e3689190SBen Widawsky 
770e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
771e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
772e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
773e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
774e3689190SBen Widawsky 
775e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
776e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
777e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
778e3689190SBen Widawsky 
779e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
780e3689190SBen Widawsky 
781e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
782e3689190SBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
783e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
784e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
785e3689190SBen Widawsky 
786e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
787e3689190SBen Widawsky 
788e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
789e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
790e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
791e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
792e3689190SBen Widawsky 	parity_event[4] = NULL;
793e3689190SBen Widawsky 
794e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
795e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
796e3689190SBen Widawsky 
797e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
798e3689190SBen Widawsky 		  row, bank, subbank);
799e3689190SBen Widawsky 
800e3689190SBen Widawsky 	kfree(parity_event[3]);
801e3689190SBen Widawsky 	kfree(parity_event[2]);
802e3689190SBen Widawsky 	kfree(parity_event[1]);
803e3689190SBen Widawsky }
804e3689190SBen Widawsky 
805d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
806e3689190SBen Widawsky {
807e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
808e3689190SBen Widawsky 	unsigned long flags;
809e3689190SBen Widawsky 
810e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
811e3689190SBen Widawsky 		return;
812e3689190SBen Widawsky 
813e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
814e3689190SBen Widawsky 	dev_priv->gt_irq_mask |= GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
815e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
816e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
817e3689190SBen Widawsky 
818a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
819e3689190SBen Widawsky }
820e3689190SBen Widawsky 
821e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
822e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
823e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
824e7b4c6b1SDaniel Vetter {
825e7b4c6b1SDaniel Vetter 
826e7b4c6b1SDaniel Vetter 	if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
827e7b4c6b1SDaniel Vetter 		      GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
828e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
829e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BSD_USER_INTERRUPT)
830e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
831e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
832e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
833e7b4c6b1SDaniel Vetter 
834e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
835e7b4c6b1SDaniel Vetter 		      GT_GEN6_BSD_CS_ERROR_INTERRUPT |
836e7b4c6b1SDaniel Vetter 		      GT_RENDER_CS_ERROR_INTERRUPT)) {
837e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
838e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
839e7b4c6b1SDaniel Vetter 	}
840e3689190SBen Widawsky 
841e3689190SBen Widawsky 	if (gt_iir & GT_GEN7_L3_PARITY_ERROR_INTERRUPT)
842e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
843e7b4c6b1SDaniel Vetter }
844e7b4c6b1SDaniel Vetter 
845baf02a1fSBen Widawsky /* Legacy way of handling PM interrupts */
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)
888b543fb04SEgbert Eich 			continue;
889b543fb04SEgbert Eich 
890bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
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 
925baf02a1fSBen Widawsky /* Unlike gen6_queue_rps_work() from which this function is originally derived,
926baf02a1fSBen Widawsky  * we must be able to deal with other PM interrupts. This is complicated because
927baf02a1fSBen Widawsky  * of the way in which we use the masks to defer the RPS work (which for
928baf02a1fSBen Widawsky  * posterity is necessary because of forcewake).
929baf02a1fSBen Widawsky  */
930baf02a1fSBen Widawsky static void hsw_pm_irq_handler(struct drm_i915_private *dev_priv,
931baf02a1fSBen Widawsky 			       u32 pm_iir)
932baf02a1fSBen Widawsky {
933baf02a1fSBen Widawsky 	unsigned long flags;
934baf02a1fSBen Widawsky 
935baf02a1fSBen Widawsky 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
936baf02a1fSBen Widawsky 	dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_DEFERRED_EVENTS;
937baf02a1fSBen Widawsky 	if (dev_priv->rps.pm_iir) {
938baf02a1fSBen Widawsky 		I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
939baf02a1fSBen Widawsky 		/* never want to mask useful interrupts. (also posting read) */
940baf02a1fSBen Widawsky 		WARN_ON(I915_READ_NOTRACE(GEN6_PMIMR) & ~GEN6_PM_DEFERRED_EVENTS);
941baf02a1fSBen Widawsky 		/* TODO: if queue_work is slow, move it out of the spinlock */
942baf02a1fSBen Widawsky 		queue_work(dev_priv->wq, &dev_priv->rps.work);
943baf02a1fSBen Widawsky 	}
944baf02a1fSBen Widawsky 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
945baf02a1fSBen Widawsky 
946baf02a1fSBen Widawsky 	if (pm_iir & ~GEN6_PM_DEFERRED_EVENTS)
947baf02a1fSBen Widawsky 		DRM_ERROR("Unexpected PM interrupted\n");
948baf02a1fSBen Widawsky }
949baf02a1fSBen Widawsky 
950ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
9517e231dbeSJesse Barnes {
9527e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
9537e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
9547e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
9557e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
9567e231dbeSJesse Barnes 	unsigned long irqflags;
9577e231dbeSJesse Barnes 	int pipe;
9587e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
9597e231dbeSJesse Barnes 
9607e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
9617e231dbeSJesse Barnes 
9627e231dbeSJesse Barnes 	while (true) {
9637e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
9647e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
9657e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
9667e231dbeSJesse Barnes 
9677e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
9687e231dbeSJesse Barnes 			goto out;
9697e231dbeSJesse Barnes 
9707e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
9717e231dbeSJesse Barnes 
972e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
9737e231dbeSJesse Barnes 
9747e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
9757e231dbeSJesse Barnes 		for_each_pipe(pipe) {
9767e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
9777e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
9787e231dbeSJesse Barnes 
9797e231dbeSJesse Barnes 			/*
9807e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
9817e231dbeSJesse Barnes 			 */
9827e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
9837e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
9847e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
9857e231dbeSJesse Barnes 							 pipe_name(pipe));
9867e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
9877e231dbeSJesse Barnes 			}
9887e231dbeSJesse Barnes 		}
9897e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
9907e231dbeSJesse Barnes 
99131acc7f5SJesse Barnes 		for_each_pipe(pipe) {
99231acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
99331acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
99431acc7f5SJesse Barnes 
99531acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
99631acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
99731acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
99831acc7f5SJesse Barnes 			}
99931acc7f5SJesse Barnes 		}
100031acc7f5SJesse Barnes 
10017e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
10027e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
10037e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1004b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
10057e231dbeSJesse Barnes 
10067e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
10077e231dbeSJesse Barnes 					 hotplug_status);
1008b543fb04SEgbert Eich 			if (hotplug_trigger) {
1009cd569aedSEgbert Eich 				if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_status_i915))
1010cd569aedSEgbert Eich 					i915_hpd_irq_setup(dev);
10117e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
10127e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
1013b543fb04SEgbert Eich 			}
10147e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
10157e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
10167e231dbeSJesse Barnes 		}
10177e231dbeSJesse Barnes 
1018515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1019515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
10207e231dbeSJesse Barnes 
1021fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
1022fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
10237e231dbeSJesse Barnes 
10247e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
10257e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
10267e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
10277e231dbeSJesse Barnes 	}
10287e231dbeSJesse Barnes 
10297e231dbeSJesse Barnes out:
10307e231dbeSJesse Barnes 	return ret;
10317e231dbeSJesse Barnes }
10327e231dbeSJesse Barnes 
103323e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1034776ad806SJesse Barnes {
1035776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10369db4a9c7SJesse Barnes 	int pipe;
1037b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1038776ad806SJesse Barnes 
1039b543fb04SEgbert Eich 	if (hotplug_trigger) {
1040cd569aedSEgbert Eich 		if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_ibx))
1041cd569aedSEgbert Eich 			ibx_hpd_irq_setup(dev);
104276e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
1043b543fb04SEgbert Eich 	}
1044cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1045cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1046776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1047cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1048cfc33bf7SVille Syrjälä 				 port_name(port));
1049cfc33bf7SVille Syrjälä 	}
1050776ad806SJesse Barnes 
1051ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1052ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1053ce99c256SDaniel Vetter 
1054776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1055515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1056776ad806SJesse Barnes 
1057776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1058776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1059776ad806SJesse Barnes 
1060776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1061776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1062776ad806SJesse Barnes 
1063776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1064776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1065776ad806SJesse Barnes 
10669db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
10679db4a9c7SJesse Barnes 		for_each_pipe(pipe)
10689db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
10699db4a9c7SJesse Barnes 					 pipe_name(pipe),
10709db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1071776ad806SJesse Barnes 
1072776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1073776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1074776ad806SJesse Barnes 
1075776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1076776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1077776ad806SJesse Barnes 
1078776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
10798664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
10808664281bSPaulo Zanoni 							  false))
10818664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
10828664281bSPaulo Zanoni 
10838664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
10848664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
10858664281bSPaulo Zanoni 							  false))
10868664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
10878664281bSPaulo Zanoni }
10888664281bSPaulo Zanoni 
10898664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
10908664281bSPaulo Zanoni {
10918664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
10928664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
10938664281bSPaulo Zanoni 
1094de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1095de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1096de032bf4SPaulo Zanoni 
10978664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_A)
10988664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
10998664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
11008664281bSPaulo Zanoni 
11018664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_B)
11028664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
11038664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
11048664281bSPaulo Zanoni 
11058664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_C)
11068664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
11078664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
11088664281bSPaulo Zanoni 
11098664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
11108664281bSPaulo Zanoni }
11118664281bSPaulo Zanoni 
11128664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
11138664281bSPaulo Zanoni {
11148664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
11158664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
11168664281bSPaulo Zanoni 
1117de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1118de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1119de032bf4SPaulo Zanoni 
11208664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
11218664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
11228664281bSPaulo Zanoni 							  false))
11238664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
11248664281bSPaulo Zanoni 
11258664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
11268664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
11278664281bSPaulo Zanoni 							  false))
11288664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
11298664281bSPaulo Zanoni 
11308664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
11318664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
11328664281bSPaulo Zanoni 							  false))
11338664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
11348664281bSPaulo Zanoni 
11358664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1136776ad806SJesse Barnes }
1137776ad806SJesse Barnes 
113823e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
113923e81d69SAdam Jackson {
114023e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
114123e81d69SAdam Jackson 	int pipe;
1142b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
114323e81d69SAdam Jackson 
1144b543fb04SEgbert Eich 	if (hotplug_trigger) {
1145cd569aedSEgbert Eich 		if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_cpt))
1146cd569aedSEgbert Eich 			ibx_hpd_irq_setup(dev);
114776e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
1148b543fb04SEgbert Eich 	}
1149cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1150cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
115123e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1152cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1153cfc33bf7SVille Syrjälä 				 port_name(port));
1154cfc33bf7SVille Syrjälä 	}
115523e81d69SAdam Jackson 
115623e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1157ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
115823e81d69SAdam Jackson 
115923e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1160515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
116123e81d69SAdam Jackson 
116223e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
116323e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
116423e81d69SAdam Jackson 
116523e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
116623e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
116723e81d69SAdam Jackson 
116823e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
116923e81d69SAdam Jackson 		for_each_pipe(pipe)
117023e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
117123e81d69SAdam Jackson 					 pipe_name(pipe),
117223e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
11738664281bSPaulo Zanoni 
11748664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
11758664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
117623e81d69SAdam Jackson }
117723e81d69SAdam Jackson 
1178ff1f525eSDaniel Vetter static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
1179b1f14ad0SJesse Barnes {
1180b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1181b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1182ab5c608bSBen Widawsky 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
11830e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
11840e43406bSChris Wilson 	int i;
1185b1f14ad0SJesse Barnes 
1186b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1187b1f14ad0SJesse Barnes 
11888664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
11898664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
11908664281bSPaulo Zanoni 	if (IS_HASWELL(dev) &&
11918664281bSPaulo Zanoni 	    (I915_READ_NOTRACE(FPGA_DBG) & FPGA_DBG_RM_NOCLAIM)) {
11928664281bSPaulo Zanoni 		DRM_ERROR("Unclaimed register before interrupt\n");
11938664281bSPaulo Zanoni 		I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
11948664281bSPaulo Zanoni 	}
11958664281bSPaulo Zanoni 
1196b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1197b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1198b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
11990e43406bSChris Wilson 
120044498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
120144498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
120244498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
120344498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
120444498aeaSPaulo Zanoni 	 * due to its back queue). */
1205ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
120644498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
120744498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
120844498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1209ab5c608bSBen Widawsky 	}
121044498aeaSPaulo Zanoni 
12118664281bSPaulo Zanoni 	/* On Haswell, also mask ERR_INT because we don't want to risk
12128664281bSPaulo Zanoni 	 * generating "unclaimed register" interrupts from inside the interrupt
12138664281bSPaulo Zanoni 	 * handler. */
12148664281bSPaulo Zanoni 	if (IS_HASWELL(dev))
12158664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
12168664281bSPaulo Zanoni 
12170e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
12180e43406bSChris Wilson 	if (gt_iir) {
12190e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
12200e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
12210e43406bSChris Wilson 		ret = IRQ_HANDLED;
12220e43406bSChris Wilson 	}
1223b1f14ad0SJesse Barnes 
1224b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
12250e43406bSChris Wilson 	if (de_iir) {
12268664281bSPaulo Zanoni 		if (de_iir & DE_ERR_INT_IVB)
12278664281bSPaulo Zanoni 			ivb_err_int_handler(dev);
12288664281bSPaulo Zanoni 
1229ce99c256SDaniel Vetter 		if (de_iir & DE_AUX_CHANNEL_A_IVB)
1230ce99c256SDaniel Vetter 			dp_aux_irq_handler(dev);
1231ce99c256SDaniel Vetter 
1232b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
123381a07809SJani Nikula 			intel_opregion_asle_intr(dev);
1234b1f14ad0SJesse Barnes 
12350e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
123674d44445SDaniel Vetter 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
123774d44445SDaniel Vetter 				drm_handle_vblank(dev, i);
12380e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
12390e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
12400e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
1241b1f14ad0SJesse Barnes 			}
1242b1f14ad0SJesse Barnes 		}
1243b1f14ad0SJesse Barnes 
1244b1f14ad0SJesse Barnes 		/* check event from PCH */
1245ab5c608bSBen Widawsky 		if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
12460e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
12470e43406bSChris Wilson 
124823e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
12490e43406bSChris Wilson 
12500e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
12510e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
1252b1f14ad0SJesse Barnes 		}
1253b1f14ad0SJesse Barnes 
12540e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
12550e43406bSChris Wilson 		ret = IRQ_HANDLED;
12560e43406bSChris Wilson 	}
12570e43406bSChris Wilson 
12580e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
12590e43406bSChris Wilson 	if (pm_iir) {
1260baf02a1fSBen Widawsky 		if (IS_HASWELL(dev))
1261baf02a1fSBen Widawsky 			hsw_pm_irq_handler(dev_priv, pm_iir);
1262baf02a1fSBen Widawsky 		else if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
1263fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
1264b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
12650e43406bSChris Wilson 		ret = IRQ_HANDLED;
12660e43406bSChris Wilson 	}
1267b1f14ad0SJesse Barnes 
12688664281bSPaulo Zanoni 	if (IS_HASWELL(dev) && ivb_can_enable_err_int(dev))
12698664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
12708664281bSPaulo Zanoni 
1271b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1272b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1273ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
127444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
127544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1276ab5c608bSBen Widawsky 	}
1277b1f14ad0SJesse Barnes 
1278b1f14ad0SJesse Barnes 	return ret;
1279b1f14ad0SJesse Barnes }
1280b1f14ad0SJesse Barnes 
1281e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
1282e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1283e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1284e7b4c6b1SDaniel Vetter {
1285e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
1286e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1287e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1288e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1289e7b4c6b1SDaniel Vetter }
1290e7b4c6b1SDaniel Vetter 
1291ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1292036a4a7dSZhenyu Wang {
12934697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1294036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1295036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
129644498aeaSPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
1297881f47b6SXiang, Haihao 
12984697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
12994697995bSJesse Barnes 
13002d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
13012d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
13022d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
13033143a2bfSChris Wilson 	POSTING_READ(DEIER);
13042d109a84SZou, Nanhai 
130544498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
130644498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
130744498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
130844498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
130944498aeaSPaulo Zanoni 	 * due to its back queue). */
131044498aeaSPaulo Zanoni 	sde_ier = I915_READ(SDEIER);
131144498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, 0);
131244498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
131344498aeaSPaulo Zanoni 
1314036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
1315036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
13163b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
1317036a4a7dSZhenyu Wang 
1318acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
1319c7c85101SZou Nan hai 		goto done;
1320036a4a7dSZhenyu Wang 
1321036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
1322036a4a7dSZhenyu Wang 
1323e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
1324e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
1325e7b4c6b1SDaniel Vetter 	else
1326e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1327036a4a7dSZhenyu Wang 
1328ce99c256SDaniel Vetter 	if (de_iir & DE_AUX_CHANNEL_A)
1329ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1330ce99c256SDaniel Vetter 
133101c66889SZhao Yakui 	if (de_iir & DE_GSE)
133281a07809SJani Nikula 		intel_opregion_asle_intr(dev);
133301c66889SZhao Yakui 
133474d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
133574d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
133674d44445SDaniel Vetter 
133774d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
133874d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
133974d44445SDaniel Vetter 
1340de032bf4SPaulo Zanoni 	if (de_iir & DE_POISON)
1341de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1342de032bf4SPaulo Zanoni 
13438664281bSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
13448664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
13458664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
13468664281bSPaulo Zanoni 
13478664281bSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
13488664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
13498664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
13508664281bSPaulo Zanoni 
1351f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1352013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
13532bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
1354013d5aa2SJesse Barnes 	}
1355013d5aa2SJesse Barnes 
1356f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1357f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
13582bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
1359013d5aa2SJesse Barnes 	}
1360c062df61SLi Peng 
1361c650156aSZhenyu Wang 	/* check event from PCH */
1362776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
1363acd15b6cSDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
1364acd15b6cSDaniel Vetter 
136523e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
136623e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
136723e81d69SAdam Jackson 		else
136823e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
1369acd15b6cSDaniel Vetter 
1370acd15b6cSDaniel Vetter 		/* should clear PCH hotplug event before clear CPU irq */
1371acd15b6cSDaniel Vetter 		I915_WRITE(SDEIIR, pch_iir);
1372776ad806SJesse Barnes 	}
1373c650156aSZhenyu Wang 
137473edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
137573edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
1376f97108d1SJesse Barnes 
1377fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
1378fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
13793b8d8d91SJesse Barnes 
1380c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
1381c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
13824912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
1383036a4a7dSZhenyu Wang 
1384c7c85101SZou Nan hai done:
13852d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
13863143a2bfSChris Wilson 	POSTING_READ(DEIER);
138744498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, sde_ier);
138844498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
13892d109a84SZou, Nanhai 
1390036a4a7dSZhenyu Wang 	return ret;
1391036a4a7dSZhenyu Wang }
1392036a4a7dSZhenyu Wang 
13938a905236SJesse Barnes /**
13948a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
13958a905236SJesse Barnes  * @work: work struct
13968a905236SJesse Barnes  *
13978a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
13988a905236SJesse Barnes  * was detected.
13998a905236SJesse Barnes  */
14008a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
14018a905236SJesse Barnes {
14021f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
14031f83fee0SDaniel Vetter 						    work);
14041f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
14051f83fee0SDaniel Vetter 						    gpu_error);
14068a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1407f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
1408f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
1409f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
1410f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
1411f69061beSDaniel Vetter 	int i, ret;
14128a905236SJesse Barnes 
1413f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
14148a905236SJesse Barnes 
14157db0ba24SDaniel Vetter 	/*
14167db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
14177db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
14187db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
14197db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
14207db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
14217db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
14227db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
14237db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
14247db0ba24SDaniel Vetter 	 */
14257db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
142644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
14277db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
14287db0ba24SDaniel Vetter 				   reset_event);
14291f83fee0SDaniel Vetter 
1430f69061beSDaniel Vetter 		ret = i915_reset(dev);
1431f69061beSDaniel Vetter 
1432f69061beSDaniel Vetter 		if (ret == 0) {
1433f69061beSDaniel Vetter 			/*
1434f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1435f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1436f69061beSDaniel Vetter 			 * complete.
1437f69061beSDaniel Vetter 			 *
1438f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1439f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1440f69061beSDaniel Vetter 			 * updates before
1441f69061beSDaniel Vetter 			 * the counter increment.
1442f69061beSDaniel Vetter 			 */
1443f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1444f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1445f69061beSDaniel Vetter 
1446f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1447f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
14481f83fee0SDaniel Vetter 		} else {
14491f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1450f316a42cSBen Gamari 		}
14511f83fee0SDaniel Vetter 
1452f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
1453f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
1454f69061beSDaniel Vetter 
145596a02917SVille Syrjälä 		intel_display_handle_reset(dev);
145696a02917SVille Syrjälä 
14571f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
1458f316a42cSBen Gamari 	}
14598a905236SJesse Barnes }
14608a905236SJesse Barnes 
146185f9e50dSDaniel Vetter /* NB: please notice the memset */
146285f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
146385f9e50dSDaniel Vetter 				    uint32_t *instdone)
146485f9e50dSDaniel Vetter {
146585f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
146685f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
146785f9e50dSDaniel Vetter 
146885f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
146985f9e50dSDaniel Vetter 	case 2:
147085f9e50dSDaniel Vetter 	case 3:
147185f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
147285f9e50dSDaniel Vetter 		break;
147385f9e50dSDaniel Vetter 	case 4:
147485f9e50dSDaniel Vetter 	case 5:
147585f9e50dSDaniel Vetter 	case 6:
147685f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
147785f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
147885f9e50dSDaniel Vetter 		break;
147985f9e50dSDaniel Vetter 	default:
148085f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
148185f9e50dSDaniel Vetter 	case 7:
148285f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
148385f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
148485f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
148585f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
148685f9e50dSDaniel Vetter 		break;
148785f9e50dSDaniel Vetter 	}
148885f9e50dSDaniel Vetter }
148985f9e50dSDaniel Vetter 
14903bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
14919df30794SChris Wilson static struct drm_i915_error_object *
1492d0d045e8SBen Widawsky i915_error_object_create_sized(struct drm_i915_private *dev_priv,
1493d0d045e8SBen Widawsky 			       struct drm_i915_gem_object *src,
1494d0d045e8SBen Widawsky 			       const int num_pages)
14959df30794SChris Wilson {
14969df30794SChris Wilson 	struct drm_i915_error_object *dst;
1497d0d045e8SBen Widawsky 	int i;
1498e56660ddSChris Wilson 	u32 reloc_offset;
14999df30794SChris Wilson 
150005394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
15019df30794SChris Wilson 		return NULL;
15029df30794SChris Wilson 
1503d0d045e8SBen Widawsky 	dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
15049df30794SChris Wilson 	if (dst == NULL)
15059df30794SChris Wilson 		return NULL;
15069df30794SChris Wilson 
150705394f39SChris Wilson 	reloc_offset = src->gtt_offset;
1508d0d045e8SBen Widawsky 	for (i = 0; i < num_pages; i++) {
1509788885aeSAndrew Morton 		unsigned long flags;
1510e56660ddSChris Wilson 		void *d;
1511788885aeSAndrew Morton 
1512e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
15139df30794SChris Wilson 		if (d == NULL)
15149df30794SChris Wilson 			goto unwind;
1515e56660ddSChris Wilson 
1516788885aeSAndrew Morton 		local_irq_save(flags);
15175d4545aeSBen Widawsky 		if (reloc_offset < dev_priv->gtt.mappable_end &&
151874898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
1519172975aaSChris Wilson 			void __iomem *s;
1520172975aaSChris Wilson 
1521172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
1522172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
1523172975aaSChris Wilson 			 * captures what the GPU read.
1524172975aaSChris Wilson 			 */
1525172975aaSChris Wilson 
15265d4545aeSBen Widawsky 			s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
15273e4d3af5SPeter Zijlstra 						     reloc_offset);
1528e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
15293e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
1530960e3564SChris Wilson 		} else if (src->stolen) {
1531960e3564SChris Wilson 			unsigned long offset;
1532960e3564SChris Wilson 
1533960e3564SChris Wilson 			offset = dev_priv->mm.stolen_base;
1534960e3564SChris Wilson 			offset += src->stolen->start;
1535960e3564SChris Wilson 			offset += i << PAGE_SHIFT;
1536960e3564SChris Wilson 
15371a240d4dSDaniel Vetter 			memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
1538172975aaSChris Wilson 		} else {
15399da3da66SChris Wilson 			struct page *page;
1540172975aaSChris Wilson 			void *s;
1541172975aaSChris Wilson 
15429da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
1543172975aaSChris Wilson 
15449da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
15459da3da66SChris Wilson 
15469da3da66SChris Wilson 			s = kmap_atomic(page);
1547172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
1548172975aaSChris Wilson 			kunmap_atomic(s);
1549172975aaSChris Wilson 
15509da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
1551172975aaSChris Wilson 		}
1552788885aeSAndrew Morton 		local_irq_restore(flags);
1553e56660ddSChris Wilson 
15549da3da66SChris Wilson 		dst->pages[i] = d;
1555e56660ddSChris Wilson 
1556e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
15579df30794SChris Wilson 	}
1558d0d045e8SBen Widawsky 	dst->page_count = num_pages;
155905394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
15609df30794SChris Wilson 
15619df30794SChris Wilson 	return dst;
15629df30794SChris Wilson 
15639df30794SChris Wilson unwind:
15649da3da66SChris Wilson 	while (i--)
15659da3da66SChris Wilson 		kfree(dst->pages[i]);
15669df30794SChris Wilson 	kfree(dst);
15679df30794SChris Wilson 	return NULL;
15689df30794SChris Wilson }
1569d0d045e8SBen Widawsky #define i915_error_object_create(dev_priv, src) \
1570d0d045e8SBen Widawsky 	i915_error_object_create_sized((dev_priv), (src), \
1571d0d045e8SBen Widawsky 				       (src)->base.size>>PAGE_SHIFT)
15729df30794SChris Wilson 
15739df30794SChris Wilson static void
15749df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
15759df30794SChris Wilson {
15769df30794SChris Wilson 	int page;
15779df30794SChris Wilson 
15789df30794SChris Wilson 	if (obj == NULL)
15799df30794SChris Wilson 		return;
15809df30794SChris Wilson 
15819df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
15829df30794SChris Wilson 		kfree(obj->pages[page]);
15839df30794SChris Wilson 
15849df30794SChris Wilson 	kfree(obj);
15859df30794SChris Wilson }
15869df30794SChris Wilson 
1587742cbee8SDaniel Vetter void
1588742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
15899df30794SChris Wilson {
1590742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
1591742cbee8SDaniel Vetter 							  typeof(*error), ref);
1592e2f973d5SChris Wilson 	int i;
1593e2f973d5SChris Wilson 
159452d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
159552d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
159652d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
15977ed73da0SBen Widawsky 		i915_error_object_free(error->ring[i].ctx);
159852d39a21SChris Wilson 		kfree(error->ring[i].requests);
159952d39a21SChris Wilson 	}
1600e2f973d5SChris Wilson 
16019df30794SChris Wilson 	kfree(error->active_bo);
16026ef3d427SChris Wilson 	kfree(error->overlay);
16037ed73da0SBen Widawsky 	kfree(error->display);
16049df30794SChris Wilson 	kfree(error);
16059df30794SChris Wilson }
16061b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
16071b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
1608c724e8a9SChris Wilson {
1609c724e8a9SChris Wilson 	err->size = obj->base.size;
1610c724e8a9SChris Wilson 	err->name = obj->base.name;
16110201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
16120201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1613c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
1614c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1615c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1616c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1617c724e8a9SChris Wilson 	err->pinned = 0;
1618c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1619c724e8a9SChris Wilson 		err->pinned = 1;
1620c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1621c724e8a9SChris Wilson 		err->pinned = -1;
1622c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1623c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1624c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
162596154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
162693dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
16271b50247aSChris Wilson }
1628c724e8a9SChris Wilson 
16291b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
16301b50247aSChris Wilson 			     int count, struct list_head *head)
16311b50247aSChris Wilson {
16321b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
16331b50247aSChris Wilson 	int i = 0;
16341b50247aSChris Wilson 
16351b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
16361b50247aSChris Wilson 		capture_bo(err++, obj);
1637c724e8a9SChris Wilson 		if (++i == count)
1638c724e8a9SChris Wilson 			break;
16391b50247aSChris Wilson 	}
1640c724e8a9SChris Wilson 
16411b50247aSChris Wilson 	return i;
16421b50247aSChris Wilson }
16431b50247aSChris Wilson 
16441b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
16451b50247aSChris Wilson 			     int count, struct list_head *head)
16461b50247aSChris Wilson {
16471b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
16481b50247aSChris Wilson 	int i = 0;
16491b50247aSChris Wilson 
16501b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
16511b50247aSChris Wilson 		if (obj->pin_count == 0)
16521b50247aSChris Wilson 			continue;
16531b50247aSChris Wilson 
16541b50247aSChris Wilson 		capture_bo(err++, obj);
16551b50247aSChris Wilson 		if (++i == count)
16561b50247aSChris Wilson 			break;
1657c724e8a9SChris Wilson 	}
1658c724e8a9SChris Wilson 
1659c724e8a9SChris Wilson 	return i;
1660c724e8a9SChris Wilson }
1661c724e8a9SChris Wilson 
1662748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1663748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1664748ebc60SChris Wilson {
1665748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1666748ebc60SChris Wilson 	int i;
1667748ebc60SChris Wilson 
1668748ebc60SChris Wilson 	/* Fences */
1669748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1670775d17b6SDaniel Vetter 	case 7:
1671748ebc60SChris Wilson 	case 6:
167242b5aeabSVille Syrjälä 		for (i = 0; i < dev_priv->num_fence_regs; i++)
1673748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1674748ebc60SChris Wilson 		break;
1675748ebc60SChris Wilson 	case 5:
1676748ebc60SChris Wilson 	case 4:
1677748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1678748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1679748ebc60SChris Wilson 		break;
1680748ebc60SChris Wilson 	case 3:
1681748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1682748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1683748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1684748ebc60SChris Wilson 	case 2:
1685748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1686748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1687748ebc60SChris Wilson 		break;
1688748ebc60SChris Wilson 
16897dbf9d6eSBen Widawsky 	default:
16907dbf9d6eSBen Widawsky 		BUG();
1691748ebc60SChris Wilson 	}
1692748ebc60SChris Wilson }
1693748ebc60SChris Wilson 
1694bcfb2e28SChris Wilson static struct drm_i915_error_object *
1695bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1696bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1697bcfb2e28SChris Wilson {
1698bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1699bcfb2e28SChris Wilson 	u32 seqno;
1700bcfb2e28SChris Wilson 
1701bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1702bcfb2e28SChris Wilson 		return NULL;
1703bcfb2e28SChris Wilson 
1704b45305fcSDaniel Vetter 	if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1705b45305fcSDaniel Vetter 		u32 acthd = I915_READ(ACTHD);
1706b45305fcSDaniel Vetter 
1707b45305fcSDaniel Vetter 		if (WARN_ON(ring->id != RCS))
1708b45305fcSDaniel Vetter 			return NULL;
1709b45305fcSDaniel Vetter 
1710b45305fcSDaniel Vetter 		obj = ring->private;
1711b45305fcSDaniel Vetter 		if (acthd >= obj->gtt_offset &&
1712b45305fcSDaniel Vetter 		    acthd < obj->gtt_offset + obj->base.size)
1713b45305fcSDaniel Vetter 			return i915_error_object_create(dev_priv, obj);
1714b45305fcSDaniel Vetter 	}
1715b45305fcSDaniel Vetter 
1716b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1717bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1718bcfb2e28SChris Wilson 		if (obj->ring != ring)
1719bcfb2e28SChris Wilson 			continue;
1720bcfb2e28SChris Wilson 
17210201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1722bcfb2e28SChris Wilson 			continue;
1723bcfb2e28SChris Wilson 
1724bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1725bcfb2e28SChris Wilson 			continue;
1726bcfb2e28SChris Wilson 
1727bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1728bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1729bcfb2e28SChris Wilson 		 */
1730bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1731bcfb2e28SChris Wilson 	}
1732bcfb2e28SChris Wilson 
1733bcfb2e28SChris Wilson 	return NULL;
1734bcfb2e28SChris Wilson }
1735bcfb2e28SChris Wilson 
1736d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1737d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1738d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1739d27b1e0eSDaniel Vetter {
1740d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1741d27b1e0eSDaniel Vetter 
174233f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
174312f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
174433f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
17457e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
17467e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
17477e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
17487e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
1749df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1750df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
175133f3f518SDaniel Vetter 	}
1752c1cd90edSDaniel Vetter 
1753d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
17549d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1755d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1756d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1757d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1758c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1759050ee91fSBen Widawsky 		if (ring->id == RCS)
1760d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1761d27b1e0eSDaniel Vetter 	} else {
17629d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1763d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1764d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1765d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1766d27b1e0eSDaniel Vetter 	}
1767d27b1e0eSDaniel Vetter 
17689574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1769c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1770b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1771d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1772c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1773c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
17740f3b6849SChris Wilson 	error->ctl[ring->id] = I915_READ_CTL(ring);
17757e3b8737SDaniel Vetter 
17767e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
17777e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1778d27b1e0eSDaniel Vetter }
1779d27b1e0eSDaniel Vetter 
17808c123e54SBen Widawsky 
17818c123e54SBen Widawsky static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
17828c123e54SBen Widawsky 					   struct drm_i915_error_state *error,
17838c123e54SBen Widawsky 					   struct drm_i915_error_ring *ering)
17848c123e54SBen Widawsky {
17858c123e54SBen Widawsky 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
17868c123e54SBen Widawsky 	struct drm_i915_gem_object *obj;
17878c123e54SBen Widawsky 
17888c123e54SBen Widawsky 	/* Currently render ring is the only HW context user */
17898c123e54SBen Widawsky 	if (ring->id != RCS || !error->ccid)
17908c123e54SBen Widawsky 		return;
17918c123e54SBen Widawsky 
17928c123e54SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) {
17938c123e54SBen Widawsky 		if ((error->ccid & PAGE_MASK) == obj->gtt_offset) {
17948c123e54SBen Widawsky 			ering->ctx = i915_error_object_create_sized(dev_priv,
17958c123e54SBen Widawsky 								    obj, 1);
17968c123e54SBen Widawsky 		}
17978c123e54SBen Widawsky 	}
17988c123e54SBen Widawsky }
17998c123e54SBen Widawsky 
180052d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
180152d39a21SChris Wilson 				  struct drm_i915_error_state *error)
180252d39a21SChris Wilson {
180352d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1804b4519513SChris Wilson 	struct intel_ring_buffer *ring;
180552d39a21SChris Wilson 	struct drm_i915_gem_request *request;
180652d39a21SChris Wilson 	int i, count;
180752d39a21SChris Wilson 
1808b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
180952d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
181052d39a21SChris Wilson 
181152d39a21SChris Wilson 		error->ring[i].batchbuffer =
181252d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
181352d39a21SChris Wilson 
181452d39a21SChris Wilson 		error->ring[i].ringbuffer =
181552d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
181652d39a21SChris Wilson 
18178c123e54SBen Widawsky 
18188c123e54SBen Widawsky 		i915_gem_record_active_context(ring, error, &error->ring[i]);
18198c123e54SBen Widawsky 
182052d39a21SChris Wilson 		count = 0;
182152d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
182252d39a21SChris Wilson 			count++;
182352d39a21SChris Wilson 
182452d39a21SChris Wilson 		error->ring[i].num_requests = count;
182552d39a21SChris Wilson 		error->ring[i].requests =
182652d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
182752d39a21SChris Wilson 				GFP_ATOMIC);
182852d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
182952d39a21SChris Wilson 			error->ring[i].num_requests = 0;
183052d39a21SChris Wilson 			continue;
183152d39a21SChris Wilson 		}
183252d39a21SChris Wilson 
183352d39a21SChris Wilson 		count = 0;
183452d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
183552d39a21SChris Wilson 			struct drm_i915_error_request *erq;
183652d39a21SChris Wilson 
183752d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
183852d39a21SChris Wilson 			erq->seqno = request->seqno;
183952d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1840ee4f42b1SChris Wilson 			erq->tail = request->tail;
184152d39a21SChris Wilson 		}
184252d39a21SChris Wilson 	}
184352d39a21SChris Wilson }
184452d39a21SChris Wilson 
18458a905236SJesse Barnes /**
18468a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
18478a905236SJesse Barnes  * @dev: drm device
18488a905236SJesse Barnes  *
18498a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
18508a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
18518a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
18528a905236SJesse Barnes  * to pick up.
18538a905236SJesse Barnes  */
185463eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
185563eeaf38SJesse Barnes {
185663eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
185705394f39SChris Wilson 	struct drm_i915_gem_object *obj;
185863eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
185963eeaf38SJesse Barnes 	unsigned long flags;
18609db4a9c7SJesse Barnes 	int i, pipe;
186163eeaf38SJesse Barnes 
186299584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
186399584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
186499584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
18659df30794SChris Wilson 	if (error)
18669df30794SChris Wilson 		return;
186763eeaf38SJesse Barnes 
18689db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
186933f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
187063eeaf38SJesse Barnes 	if (!error) {
18719df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
18729df30794SChris Wilson 		return;
187363eeaf38SJesse Barnes 	}
187463eeaf38SJesse Barnes 
18752f86f191SBen Widawsky 	DRM_INFO("capturing error event; look for more information in "
18762f86f191SBen Widawsky 		 "/sys/kernel/debug/dri/%d/i915_error_state\n",
1877b6f7833bSChris Wilson 		 dev->primary->index);
18782fa772f3SChris Wilson 
1879742cbee8SDaniel Vetter 	kref_init(&error->ref);
188063eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
188163eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1882211816ecSBen Widawsky 	if (HAS_HW_CONTEXTS(dev))
1883b9a3906bSBen Widawsky 		error->ccid = I915_READ(CCID);
1884be998e2eSBen Widawsky 
1885be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1886be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1887be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1888be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1889be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1890be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1891be998e2eSBen Widawsky 	else
1892be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1893be998e2eSBen Widawsky 
18940f3b6849SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6)
18950f3b6849SChris Wilson 		error->derrmr = I915_READ(DERRMR);
18960f3b6849SChris Wilson 
18970f3b6849SChris Wilson 	if (IS_VALLEYVIEW(dev))
18980f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_VLV);
18990f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 7)
19000f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_MT);
19010f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen == 6)
19020f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE);
19030f3b6849SChris Wilson 
19044f3308b9SPaulo Zanoni 	if (!HAS_PCH_SPLIT(dev))
19059db4a9c7SJesse Barnes 		for_each_pipe(pipe)
19069db4a9c7SJesse Barnes 			error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1907d27b1e0eSDaniel Vetter 
190833f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1909f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
191033f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
191133f3f518SDaniel Vetter 	}
1912add354ddSChris Wilson 
191371e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
191471e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
191571e172e8SBen Widawsky 
1916050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1917050ee91fSBen Widawsky 
1918748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
191952d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
19209df30794SChris Wilson 
1921c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
19229df30794SChris Wilson 	error->active_bo = NULL;
1923c724e8a9SChris Wilson 	error->pinned_bo = NULL;
19249df30794SChris Wilson 
1925bcfb2e28SChris Wilson 	i = 0;
1926bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1927bcfb2e28SChris Wilson 		i++;
1928bcfb2e28SChris Wilson 	error->active_bo_count = i;
19296c085a72SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
19301b50247aSChris Wilson 		if (obj->pin_count)
1931bcfb2e28SChris Wilson 			i++;
1932bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1933c724e8a9SChris Wilson 
19348e934dbfSChris Wilson 	error->active_bo = NULL;
19358e934dbfSChris Wilson 	error->pinned_bo = NULL;
1936bcfb2e28SChris Wilson 	if (i) {
1937bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
19389df30794SChris Wilson 					   GFP_ATOMIC);
1939c724e8a9SChris Wilson 		if (error->active_bo)
1940c724e8a9SChris Wilson 			error->pinned_bo =
1941c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
19429df30794SChris Wilson 	}
1943c724e8a9SChris Wilson 
1944c724e8a9SChris Wilson 	if (error->active_bo)
1945c724e8a9SChris Wilson 		error->active_bo_count =
19461b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1947c724e8a9SChris Wilson 					  error->active_bo_count,
1948c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1949c724e8a9SChris Wilson 
1950c724e8a9SChris Wilson 	if (error->pinned_bo)
1951c724e8a9SChris Wilson 		error->pinned_bo_count =
19521b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1953c724e8a9SChris Wilson 					  error->pinned_bo_count,
19546c085a72SChris Wilson 					  &dev_priv->mm.bound_list);
195563eeaf38SJesse Barnes 
19568a905236SJesse Barnes 	do_gettimeofday(&error->time);
19578a905236SJesse Barnes 
19586ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1959c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
19606ef3d427SChris Wilson 
196199584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
196299584db3SDaniel Vetter 	if (dev_priv->gpu_error.first_error == NULL) {
196399584db3SDaniel Vetter 		dev_priv->gpu_error.first_error = error;
19649df30794SChris Wilson 		error = NULL;
19659df30794SChris Wilson 	}
196699584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19679df30794SChris Wilson 
19689df30794SChris Wilson 	if (error)
1969742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
19709df30794SChris Wilson }
19719df30794SChris Wilson 
19729df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
19739df30794SChris Wilson {
19749df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
19759df30794SChris Wilson 	struct drm_i915_error_state *error;
19766dc0e816SBen Widawsky 	unsigned long flags;
19779df30794SChris Wilson 
197899584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
197999584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
198099584db3SDaniel Vetter 	dev_priv->gpu_error.first_error = NULL;
198199584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
19829df30794SChris Wilson 
19839df30794SChris Wilson 	if (error)
1984742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
198563eeaf38SJesse Barnes }
19863bd3c932SChris Wilson #else
19873bd3c932SChris Wilson #define i915_capture_error_state(x)
19883bd3c932SChris Wilson #endif
198963eeaf38SJesse Barnes 
199035aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1991c0e09200SDave Airlie {
19928a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1993bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
199463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1995050ee91fSBen Widawsky 	int pipe, i;
199663eeaf38SJesse Barnes 
199735aed2e6SChris Wilson 	if (!eir)
199835aed2e6SChris Wilson 		return;
199963eeaf38SJesse Barnes 
2000a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
20018a905236SJesse Barnes 
2002bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2003bd9854f9SBen Widawsky 
20048a905236SJesse Barnes 	if (IS_G4X(dev)) {
20058a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
20068a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
20078a905236SJesse Barnes 
2008a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2009a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2010050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2011050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2012a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2013a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
20148a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20153143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
20168a905236SJesse Barnes 		}
20178a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
20188a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2019a70491ccSJoe Perches 			pr_err("page table error\n");
2020a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
20218a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20223143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
20238a905236SJesse Barnes 		}
20248a905236SJesse Barnes 	}
20258a905236SJesse Barnes 
2026a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
202763eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
202863eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2029a70491ccSJoe Perches 			pr_err("page table error\n");
2030a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
203163eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
20323143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
203363eeaf38SJesse Barnes 		}
20348a905236SJesse Barnes 	}
20358a905236SJesse Barnes 
203663eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2037a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
20389db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2039a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
20409db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
204163eeaf38SJesse Barnes 		/* pipestat has already been acked */
204263eeaf38SJesse Barnes 	}
204363eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2044a70491ccSJoe Perches 		pr_err("instruction error\n");
2045a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2046050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2047050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2048a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
204963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
205063eeaf38SJesse Barnes 
2051a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2052a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2053a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
205463eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
20553143a2bfSChris Wilson 			POSTING_READ(IPEIR);
205663eeaf38SJesse Barnes 		} else {
205763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
205863eeaf38SJesse Barnes 
2059a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2060a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2061a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2062a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
206363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20643143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
206563eeaf38SJesse Barnes 		}
206663eeaf38SJesse Barnes 	}
206763eeaf38SJesse Barnes 
206863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
20693143a2bfSChris Wilson 	POSTING_READ(EIR);
207063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
207163eeaf38SJesse Barnes 	if (eir) {
207263eeaf38SJesse Barnes 		/*
207363eeaf38SJesse Barnes 		 * some errors might have become stuck,
207463eeaf38SJesse Barnes 		 * mask them.
207563eeaf38SJesse Barnes 		 */
207663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
207763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
207863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
207963eeaf38SJesse Barnes 	}
208035aed2e6SChris Wilson }
208135aed2e6SChris Wilson 
208235aed2e6SChris Wilson /**
208335aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
208435aed2e6SChris Wilson  * @dev: drm device
208535aed2e6SChris Wilson  *
208635aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
208735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
208835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
208935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
209035aed2e6SChris Wilson  * of a ring dump etc.).
209135aed2e6SChris Wilson  */
2092527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
209335aed2e6SChris Wilson {
209435aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2095b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2096b4519513SChris Wilson 	int i;
209735aed2e6SChris Wilson 
209835aed2e6SChris Wilson 	i915_capture_error_state(dev);
209935aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
21008a905236SJesse Barnes 
2101ba1234d1SBen Gamari 	if (wedged) {
2102f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2103f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2104ba1234d1SBen Gamari 
210511ed50ecSBen Gamari 		/*
21061f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
21071f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
210811ed50ecSBen Gamari 		 */
2109b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
2110b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
211111ed50ecSBen Gamari 	}
211211ed50ecSBen Gamari 
211399584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
21148a905236SJesse Barnes }
21158a905236SJesse Barnes 
211621ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
21174e5359cdSSimon Farnsworth {
21184e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
21194e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
21204e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
212105394f39SChris Wilson 	struct drm_i915_gem_object *obj;
21224e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
21234e5359cdSSimon Farnsworth 	unsigned long flags;
21244e5359cdSSimon Farnsworth 	bool stall_detected;
21254e5359cdSSimon Farnsworth 
21264e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
21274e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
21284e5359cdSSimon Farnsworth 		return;
21294e5359cdSSimon Farnsworth 
21304e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
21314e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
21324e5359cdSSimon Farnsworth 
2133e7d841caSChris Wilson 	if (work == NULL ||
2134e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2135e7d841caSChris Wilson 	    !work->enable_stall_check) {
21364e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
21374e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
21384e5359cdSSimon Farnsworth 		return;
21394e5359cdSSimon Farnsworth 	}
21404e5359cdSSimon Farnsworth 
21414e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
214205394f39SChris Wilson 	obj = work->pending_flip_obj;
2143a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
21449db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2145446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2146446f2545SArmin Reese 					obj->gtt_offset;
21474e5359cdSSimon Farnsworth 	} else {
21489db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
214905394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
215001f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
21514e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
21524e5359cdSSimon Farnsworth 	}
21534e5359cdSSimon Farnsworth 
21544e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
21554e5359cdSSimon Farnsworth 
21564e5359cdSSimon Farnsworth 	if (stall_detected) {
21574e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
21584e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
21594e5359cdSSimon Farnsworth 	}
21604e5359cdSSimon Farnsworth }
21614e5359cdSSimon Farnsworth 
216242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
216342f52ef8SKeith Packard  * we use as a pipe index
216442f52ef8SKeith Packard  */
2165f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
21660a3e67a4SJesse Barnes {
21670a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2168e9d21d7fSKeith Packard 	unsigned long irqflags;
216971e0ffa5SJesse Barnes 
21705eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
217171e0ffa5SJesse Barnes 		return -EINVAL;
21720a3e67a4SJesse Barnes 
21731ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2174f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
21757c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21767c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
21770a3e67a4SJesse Barnes 	else
21787c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
21797c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
21808692d00eSChris Wilson 
21818692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
21828692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
21836b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
21841ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
21858692d00eSChris Wilson 
21860a3e67a4SJesse Barnes 	return 0;
21870a3e67a4SJesse Barnes }
21880a3e67a4SJesse Barnes 
2189f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2190f796cf8fSJesse Barnes {
2191f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2192f796cf8fSJesse Barnes 	unsigned long irqflags;
2193f796cf8fSJesse Barnes 
2194f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2195f796cf8fSJesse Barnes 		return -EINVAL;
2196f796cf8fSJesse Barnes 
2197f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2198f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
2199f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2200f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2201f796cf8fSJesse Barnes 
2202f796cf8fSJesse Barnes 	return 0;
2203f796cf8fSJesse Barnes }
2204f796cf8fSJesse Barnes 
2205f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
2206b1f14ad0SJesse Barnes {
2207b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2208b1f14ad0SJesse Barnes 	unsigned long irqflags;
2209b1f14ad0SJesse Barnes 
2210b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2211b1f14ad0SJesse Barnes 		return -EINVAL;
2212b1f14ad0SJesse Barnes 
2213b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2214b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
2215b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
2216b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2217b1f14ad0SJesse Barnes 
2218b1f14ad0SJesse Barnes 	return 0;
2219b1f14ad0SJesse Barnes }
2220b1f14ad0SJesse Barnes 
22217e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
22227e231dbeSJesse Barnes {
22237e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22247e231dbeSJesse Barnes 	unsigned long irqflags;
222531acc7f5SJesse Barnes 	u32 imr;
22267e231dbeSJesse Barnes 
22277e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
22287e231dbeSJesse Barnes 		return -EINVAL;
22297e231dbeSJesse Barnes 
22307e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22317e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
223231acc7f5SJesse Barnes 	if (pipe == 0)
22337e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
223431acc7f5SJesse Barnes 	else
22357e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22367e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
223731acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
223831acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
22397e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22407e231dbeSJesse Barnes 
22417e231dbeSJesse Barnes 	return 0;
22427e231dbeSJesse Barnes }
22437e231dbeSJesse Barnes 
224442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
224542f52ef8SKeith Packard  * we use as a pipe index
224642f52ef8SKeith Packard  */
2247f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
22480a3e67a4SJesse Barnes {
22490a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2250e9d21d7fSKeith Packard 	unsigned long irqflags;
22510a3e67a4SJesse Barnes 
22521ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
22538692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
22546b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
22558692d00eSChris Wilson 
22567c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
22577c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
22587c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
22591ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22600a3e67a4SJesse Barnes }
22610a3e67a4SJesse Barnes 
2262f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2263f796cf8fSJesse Barnes {
2264f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2265f796cf8fSJesse Barnes 	unsigned long irqflags;
2266f796cf8fSJesse Barnes 
2267f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2268f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
2269f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
2270f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2271f796cf8fSJesse Barnes }
2272f796cf8fSJesse Barnes 
2273f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
2274b1f14ad0SJesse Barnes {
2275b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2276b1f14ad0SJesse Barnes 	unsigned long irqflags;
2277b1f14ad0SJesse Barnes 
2278b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2279b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
2280b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
2281b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2282b1f14ad0SJesse Barnes }
2283b1f14ad0SJesse Barnes 
22847e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
22857e231dbeSJesse Barnes {
22867e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22877e231dbeSJesse Barnes 	unsigned long irqflags;
228831acc7f5SJesse Barnes 	u32 imr;
22897e231dbeSJesse Barnes 
22907e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
229131acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
229231acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
22937e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
229431acc7f5SJesse Barnes 	if (pipe == 0)
22957e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
229631acc7f5SJesse Barnes 	else
22977e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22987e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
22997e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
23007e231dbeSJesse Barnes }
23017e231dbeSJesse Barnes 
2302893eead0SChris Wilson static u32
2303893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2304852835f3SZou Nan hai {
2305893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2306893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2307893eead0SChris Wilson }
2308893eead0SChris Wilson 
230979ee20dcSMika Kuoppala static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring,
231079ee20dcSMika Kuoppala 				     u32 ring_seqno, bool *err)
2311893eead0SChris Wilson {
2312893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
231379ee20dcSMika Kuoppala 	    i915_seqno_passed(ring_seqno, ring_last_seqno(ring))) {
2314893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
23159574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
23169574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
23179574b3feSBen Widawsky 				  ring->name);
2318893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
2319893eead0SChris Wilson 			*err = true;
2320893eead0SChris Wilson 		}
2321893eead0SChris Wilson 		return true;
2322893eead0SChris Wilson 	}
2323893eead0SChris Wilson 	return false;
2324f65d9421SBen Gamari }
2325f65d9421SBen Gamari 
2326a24a11e6SChris Wilson static bool semaphore_passed(struct intel_ring_buffer *ring)
2327a24a11e6SChris Wilson {
2328a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2329a24a11e6SChris Wilson 	u32 acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2330a24a11e6SChris Wilson 	struct intel_ring_buffer *signaller;
2331a24a11e6SChris Wilson 	u32 cmd, ipehr, acthd_min;
2332a24a11e6SChris Wilson 
2333a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2334a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2335a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
2336a24a11e6SChris Wilson 		return false;
2337a24a11e6SChris Wilson 
2338a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2339a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2340a24a11e6SChris Wilson 	 */
2341a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2342a24a11e6SChris Wilson 	do {
2343a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2344a24a11e6SChris Wilson 		if (cmd == ipehr)
2345a24a11e6SChris Wilson 			break;
2346a24a11e6SChris Wilson 
2347a24a11e6SChris Wilson 		acthd -= 4;
2348a24a11e6SChris Wilson 		if (acthd < acthd_min)
2349a24a11e6SChris Wilson 			return false;
2350a24a11e6SChris Wilson 	} while (1);
2351a24a11e6SChris Wilson 
2352a24a11e6SChris Wilson 	signaller = &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2353a24a11e6SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false),
2354a24a11e6SChris Wilson 				 ioread32(ring->virtual_start+acthd+4)+1);
2355a24a11e6SChris Wilson }
2356a24a11e6SChris Wilson 
23571ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
23581ec14ad3SChris Wilson {
23591ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
23601ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
23611ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
23621ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
23631ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
23641ec14ad3SChris Wilson 			  ring->name);
23651ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
23661ec14ad3SChris Wilson 		return true;
23671ec14ad3SChris Wilson 	}
2368a24a11e6SChris Wilson 
2369a24a11e6SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 &&
2370a24a11e6SChris Wilson 	    tmp & RING_WAIT_SEMAPHORE &&
2371a24a11e6SChris Wilson 	    semaphore_passed(ring)) {
2372a24a11e6SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
2373a24a11e6SChris Wilson 			  ring->name);
2374a24a11e6SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2375a24a11e6SChris Wilson 		return true;
2376a24a11e6SChris Wilson 	}
23771ec14ad3SChris Wilson 	return false;
23781ec14ad3SChris Wilson }
23791ec14ad3SChris Wilson 
2380ed5cbb03SMika Kuoppala static bool i915_hangcheck_ring_hung(struct intel_ring_buffer *ring)
2381d1e61e7fSChris Wilson {
2382ed5cbb03SMika Kuoppala 	if (IS_GEN2(ring->dev))
2383ed5cbb03SMika Kuoppala 		return false;
2384b4519513SChris Wilson 
2385d1e61e7fSChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
2386d1e61e7fSChris Wilson 	 * If so we can simply poke the RB_WAIT bit
2387d1e61e7fSChris Wilson 	 * and break the hang. This should work on
2388d1e61e7fSChris Wilson 	 * all but the second generation chipsets.
2389d1e61e7fSChris Wilson 	 */
2390ed5cbb03SMika Kuoppala 	return !kick_ring(ring);
2391d1e61e7fSChris Wilson }
2392d1e61e7fSChris Wilson 
2393ed5cbb03SMika Kuoppala static bool i915_hangcheck_hung(struct drm_device *dev)
2394ed5cbb03SMika Kuoppala {
2395ed5cbb03SMika Kuoppala 	drm_i915_private_t *dev_priv = dev->dev_private;
2396ed5cbb03SMika Kuoppala 
2397ed5cbb03SMika Kuoppala 	if (dev_priv->gpu_error.hangcheck_count++ > 1) {
2398ed5cbb03SMika Kuoppala 		bool hung = true;
2399ed5cbb03SMika Kuoppala 		struct intel_ring_buffer *ring;
2400ed5cbb03SMika Kuoppala 		int i;
2401ed5cbb03SMika Kuoppala 
2402ed5cbb03SMika Kuoppala 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
2403ed5cbb03SMika Kuoppala 		i915_handle_error(dev, true);
2404ed5cbb03SMika Kuoppala 
2405ed5cbb03SMika Kuoppala 		for_each_ring(ring, dev_priv, i)
2406ed5cbb03SMika Kuoppala 			hung &= i915_hangcheck_ring_hung(ring);
2407ed5cbb03SMika Kuoppala 
2408b4519513SChris Wilson 		return hung;
2409d1e61e7fSChris Wilson 	}
2410d1e61e7fSChris Wilson 
2411d1e61e7fSChris Wilson 	return false;
2412d1e61e7fSChris Wilson }
2413d1e61e7fSChris Wilson 
2414f65d9421SBen Gamari /**
2415f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
2416f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
2417f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
2418f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
2419f65d9421SBen Gamari  */
2420f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
2421f65d9421SBen Gamari {
2422f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2423f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2424b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2425b4519513SChris Wilson 	bool err = false, idle;
2426b4519513SChris Wilson 	int i;
242792cab734SMika Kuoppala 	u32 seqno[I915_NUM_RINGS];
242892cab734SMika Kuoppala 	bool work_done;
2429893eead0SChris Wilson 
24303e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
24313e0dc6b0SBen Widawsky 		return;
24323e0dc6b0SBen Widawsky 
2433b4519513SChris Wilson 	idle = true;
2434b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
243592cab734SMika Kuoppala 		seqno[i] = ring->get_seqno(ring, false);
243692cab734SMika Kuoppala 		idle &= i915_hangcheck_ring_idle(ring, seqno[i], &err);
2437b4519513SChris Wilson 	}
2438b4519513SChris Wilson 
2439893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
2440b4519513SChris Wilson 	if (idle) {
2441d1e61e7fSChris Wilson 		if (err) {
2442d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
2443d1e61e7fSChris Wilson 				return;
2444d1e61e7fSChris Wilson 
2445893eead0SChris Wilson 			goto repeat;
2446d1e61e7fSChris Wilson 		}
2447d1e61e7fSChris Wilson 
244899584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
2449893eead0SChris Wilson 		return;
2450893eead0SChris Wilson 	}
2451f65d9421SBen Gamari 
245292cab734SMika Kuoppala 	work_done = false;
245392cab734SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
245492cab734SMika Kuoppala 		if (ring->hangcheck.seqno != seqno[i]) {
245592cab734SMika Kuoppala 			work_done = true;
245692cab734SMika Kuoppala 			ring->hangcheck.seqno = seqno[i];
245792cab734SMika Kuoppala 		}
245892cab734SMika Kuoppala 	}
245992cab734SMika Kuoppala 
246092cab734SMika Kuoppala 	if (!work_done) {
2461d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
2462f65d9421SBen Gamari 			return;
2463cbb465e7SChris Wilson 	} else {
246499584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
2465cbb465e7SChris Wilson 	}
2466f65d9421SBen Gamari 
2467893eead0SChris Wilson repeat:
2468f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
246999584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
2470cecc21feSChris Wilson 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2471f65d9421SBen Gamari }
2472f65d9421SBen Gamari 
2473c0e09200SDave Airlie /* drm_dma.h hooks
2474c0e09200SDave Airlie */
2475f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2476036a4a7dSZhenyu Wang {
2477036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2478036a4a7dSZhenyu Wang 
24794697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
24804697995bSJesse Barnes 
2481036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2482bdfcdb63SDaniel Vetter 
2483036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
2484036a4a7dSZhenyu Wang 
2485036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2486036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
24873143a2bfSChris Wilson 	POSTING_READ(DEIER);
2488036a4a7dSZhenyu Wang 
2489036a4a7dSZhenyu Wang 	/* and GT */
2490036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2491036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
24923143a2bfSChris Wilson 	POSTING_READ(GTIER);
2493c650156aSZhenyu Wang 
2494*7d99163dSBen Widawsky 	/* south display irq */
2495*7d99163dSBen Widawsky 	I915_WRITE(SDEIMR, 0xffffffff);
2496*7d99163dSBen Widawsky 	/*
2497*7d99163dSBen Widawsky 	 * SDEIER is also touched by the interrupt handler to work around missed
2498*7d99163dSBen Widawsky 	 * PCH interrupts. Hence we can't update it after the interrupt handler
2499*7d99163dSBen Widawsky 	 * is enabled - instead we unconditionally enable all PCH interrupt
2500*7d99163dSBen Widawsky 	 * sources here, but then only unmask them as needed with SDEIMR.
2501*7d99163dSBen Widawsky 	 */
2502*7d99163dSBen Widawsky 	I915_WRITE(SDEIER, 0xffffffff);
2503*7d99163dSBen Widawsky 	POSTING_READ(SDEIER);
2504*7d99163dSBen Widawsky }
2505*7d99163dSBen Widawsky 
2506*7d99163dSBen Widawsky static void ivybridge_irq_preinstall(struct drm_device *dev)
2507*7d99163dSBen Widawsky {
2508*7d99163dSBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2509*7d99163dSBen Widawsky 
2510*7d99163dSBen Widawsky 	atomic_set(&dev_priv->irq_received, 0);
2511*7d99163dSBen Widawsky 
2512*7d99163dSBen Widawsky 	I915_WRITE(HWSTAM, 0xeffe);
2513*7d99163dSBen Widawsky 
2514*7d99163dSBen Widawsky 	/* XXX hotplug from PCH */
2515*7d99163dSBen Widawsky 
2516*7d99163dSBen Widawsky 	I915_WRITE(DEIMR, 0xffffffff);
2517*7d99163dSBen Widawsky 	I915_WRITE(DEIER, 0x0);
2518*7d99163dSBen Widawsky 	POSTING_READ(DEIER);
2519*7d99163dSBen Widawsky 
2520*7d99163dSBen Widawsky 	/* and GT */
2521*7d99163dSBen Widawsky 	I915_WRITE(GTIMR, 0xffffffff);
2522*7d99163dSBen Widawsky 	I915_WRITE(GTIER, 0x0);
2523*7d99163dSBen Widawsky 	POSTING_READ(GTIER);
2524*7d99163dSBen Widawsky 
2525ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2526ab5c608bSBen Widawsky 		return;
2527ab5c608bSBen Widawsky 
2528c650156aSZhenyu Wang 	/* south display irq */
2529c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
253082a28bcfSDaniel Vetter 	/*
253182a28bcfSDaniel Vetter 	 * SDEIER is also touched by the interrupt handler to work around missed
253282a28bcfSDaniel Vetter 	 * PCH interrupts. Hence we can't update it after the interrupt handler
253382a28bcfSDaniel Vetter 	 * is enabled - instead we unconditionally enable all PCH interrupt
253482a28bcfSDaniel Vetter 	 * sources here, but then only unmask them as needed with SDEIMR.
253582a28bcfSDaniel Vetter 	 */
253682a28bcfSDaniel Vetter 	I915_WRITE(SDEIER, 0xffffffff);
25373143a2bfSChris Wilson 	POSTING_READ(SDEIER);
2538036a4a7dSZhenyu Wang }
2539036a4a7dSZhenyu Wang 
25407e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
25417e231dbeSJesse Barnes {
25427e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25437e231dbeSJesse Barnes 	int pipe;
25447e231dbeSJesse Barnes 
25457e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
25467e231dbeSJesse Barnes 
25477e231dbeSJesse Barnes 	/* VLV magic */
25487e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
25497e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
25507e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
25517e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
25527e231dbeSJesse Barnes 
25537e231dbeSJesse Barnes 	/* and GT */
25547e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
25557e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
25567e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
25577e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
25587e231dbeSJesse Barnes 	POSTING_READ(GTIER);
25597e231dbeSJesse Barnes 
25607e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
25617e231dbeSJesse Barnes 
25627e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
25637e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
25647e231dbeSJesse Barnes 	for_each_pipe(pipe)
25657e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
25667e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
25677e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
25687e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
25697e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
25707e231dbeSJesse Barnes }
25717e231dbeSJesse Barnes 
257282a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
257382a28bcfSDaniel Vetter {
257482a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
257582a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
257682a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
257782a28bcfSDaniel Vetter 	u32 mask = ~I915_READ(SDEIMR);
257882a28bcfSDaniel Vetter 	u32 hotplug;
257982a28bcfSDaniel Vetter 
258082a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2581995e6b3dSEgbert Eich 		mask &= ~SDE_HOTPLUG_MASK;
258282a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2583cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
258482a28bcfSDaniel Vetter 				mask |= hpd_ibx[intel_encoder->hpd_pin];
258582a28bcfSDaniel Vetter 	} else {
2586995e6b3dSEgbert Eich 		mask &= ~SDE_HOTPLUG_MASK_CPT;
258782a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2588cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
258982a28bcfSDaniel Vetter 				mask |= hpd_cpt[intel_encoder->hpd_pin];
259082a28bcfSDaniel Vetter 	}
259182a28bcfSDaniel Vetter 
259282a28bcfSDaniel Vetter 	I915_WRITE(SDEIMR, ~mask);
259382a28bcfSDaniel Vetter 
25947fe0b973SKeith Packard 	/*
25957fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
25967fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
25977fe0b973SKeith Packard 	 *
25987fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
25997fe0b973SKeith Packard 	 */
26007fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
26017fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
26027fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
26037fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
26047fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
26057fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
26067fe0b973SKeith Packard }
26077fe0b973SKeith Packard 
2608d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2609d46da437SPaulo Zanoni {
2610d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
261182a28bcfSDaniel Vetter 	u32 mask;
2612d46da437SPaulo Zanoni 
2613692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2614692a04cfSDaniel Vetter 		return;
2615692a04cfSDaniel Vetter 
26168664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
26178664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2618de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
26198664281bSPaulo Zanoni 	} else {
26208664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
26218664281bSPaulo Zanoni 
26228664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
26238664281bSPaulo Zanoni 	}
2624ab5c608bSBen Widawsky 
2625d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2626d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2627d46da437SPaulo Zanoni }
2628d46da437SPaulo Zanoni 
2629f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2630036a4a7dSZhenyu Wang {
2631036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2632036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
2633013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2634ce99c256SDaniel Vetter 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
26358664281bSPaulo Zanoni 			   DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
2636de032bf4SPaulo Zanoni 			   DE_PIPEA_FIFO_UNDERRUN | DE_POISON;
26371ec14ad3SChris Wilson 	u32 render_irqs;
2638036a4a7dSZhenyu Wang 
26391ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2640036a4a7dSZhenyu Wang 
2641036a4a7dSZhenyu Wang 	/* should always can generate irq */
2642036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
26431ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
26441ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
26453143a2bfSChris Wilson 	POSTING_READ(DEIER);
2646036a4a7dSZhenyu Wang 
26471ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
2648036a4a7dSZhenyu Wang 
2649036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
26501ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2651881f47b6SXiang, Haihao 
26521ec14ad3SChris Wilson 	if (IS_GEN6(dev))
26531ec14ad3SChris Wilson 		render_irqs =
26541ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
2655e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
2656e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
26571ec14ad3SChris Wilson 	else
26581ec14ad3SChris Wilson 		render_irqs =
265988f23b8fSChris Wilson 			GT_USER_INTERRUPT |
2660c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
26611ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
26621ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
26633143a2bfSChris Wilson 	POSTING_READ(GTIER);
2664036a4a7dSZhenyu Wang 
2665d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
26667fe0b973SKeith Packard 
2667f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
2668f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
2669f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
2670f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
2671f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2672f97108d1SJesse Barnes 	}
2673f97108d1SJesse Barnes 
2674036a4a7dSZhenyu Wang 	return 0;
2675036a4a7dSZhenyu Wang }
2676036a4a7dSZhenyu Wang 
2677f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2678b1f14ad0SJesse Barnes {
2679b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2680b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2681b615b57aSChris Wilson 	u32 display_mask =
2682b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2683b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
2684b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
2685ce99c256SDaniel Vetter 		DE_PLANEA_FLIP_DONE_IVB |
26868664281bSPaulo Zanoni 		DE_AUX_CHANNEL_A_IVB |
26878664281bSPaulo Zanoni 		DE_ERR_INT_IVB;
2688b1f14ad0SJesse Barnes 	u32 render_irqs;
2689b1f14ad0SJesse Barnes 
2690b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2691b1f14ad0SJesse Barnes 
2692b1f14ad0SJesse Barnes 	/* should always can generate irq */
26938664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2694b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2695b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2696b615b57aSChris Wilson 	I915_WRITE(DEIER,
2697b615b57aSChris Wilson 		   display_mask |
2698b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
2699b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
2700b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
2701b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2702b1f14ad0SJesse Barnes 
270315b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2704b1f14ad0SJesse Barnes 
2705b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2706b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2707b1f14ad0SJesse Barnes 
2708e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
270915b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2710b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
2711b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2712b1f14ad0SJesse Barnes 
2713d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
27147fe0b973SKeith Packard 
2715b1f14ad0SJesse Barnes 	return 0;
2716b1f14ad0SJesse Barnes }
2717b1f14ad0SJesse Barnes 
27187e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
27197e231dbeSJesse Barnes {
27207e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
27217e231dbeSJesse Barnes 	u32 enable_mask;
272231acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
27233bcedbe5SJesse Barnes 	u32 render_irqs;
27247e231dbeSJesse Barnes 
27257e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
272631acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
272731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
272831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
27297e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
27307e231dbeSJesse Barnes 
273131acc7f5SJesse Barnes 	/*
273231acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
273331acc7f5SJesse Barnes 	 * toggle them based on usage.
273431acc7f5SJesse Barnes 	 */
273531acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
273631acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
273731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
27387e231dbeSJesse Barnes 
273920afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
274020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
274120afbda2SDaniel Vetter 
27427e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
27437e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
27447e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
27457e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
27467e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
27477e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
27487e231dbeSJesse Barnes 
274931acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2750515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
275131acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
275231acc7f5SJesse Barnes 
27537e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
27547e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
27557e231dbeSJesse Barnes 
275631acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
275731acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
27583bcedbe5SJesse Barnes 
27593bcedbe5SJesse Barnes 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
27603bcedbe5SJesse Barnes 		GEN6_BLITTER_USER_INTERRUPT;
27613bcedbe5SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
27627e231dbeSJesse Barnes 	POSTING_READ(GTIER);
27637e231dbeSJesse Barnes 
27647e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
27657e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
27667e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
27677e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
27687e231dbeSJesse Barnes #endif
27697e231dbeSJesse Barnes 
27707e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
277120afbda2SDaniel Vetter 
277220afbda2SDaniel Vetter 	return 0;
277320afbda2SDaniel Vetter }
277420afbda2SDaniel Vetter 
27757e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
27767e231dbeSJesse Barnes {
27777e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
27787e231dbeSJesse Barnes 	int pipe;
27797e231dbeSJesse Barnes 
27807e231dbeSJesse Barnes 	if (!dev_priv)
27817e231dbeSJesse Barnes 		return;
27827e231dbeSJesse Barnes 
2783ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2784ac4c16c5SEgbert Eich 
27857e231dbeSJesse Barnes 	for_each_pipe(pipe)
27867e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
27877e231dbeSJesse Barnes 
27887e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
27897e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
27907e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
27917e231dbeSJesse Barnes 	for_each_pipe(pipe)
27927e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
27937e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
27947e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
27957e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
27967e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
27977e231dbeSJesse Barnes }
27987e231dbeSJesse Barnes 
2799f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2800036a4a7dSZhenyu Wang {
2801036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
28024697995bSJesse Barnes 
28034697995bSJesse Barnes 	if (!dev_priv)
28044697995bSJesse Barnes 		return;
28054697995bSJesse Barnes 
2806ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2807ac4c16c5SEgbert Eich 
2808036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2809036a4a7dSZhenyu Wang 
2810036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2811036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2812036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
28138664281bSPaulo Zanoni 	if (IS_GEN7(dev))
28148664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2815036a4a7dSZhenyu Wang 
2816036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2817036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2818036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2819192aac1fSKeith Packard 
2820ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2821ab5c608bSBen Widawsky 		return;
2822ab5c608bSBen Widawsky 
2823192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2824192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2825192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
28268664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
28278664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2828036a4a7dSZhenyu Wang }
2829036a4a7dSZhenyu Wang 
2830c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2831c2798b19SChris Wilson {
2832c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2833c2798b19SChris Wilson 	int pipe;
2834c2798b19SChris Wilson 
2835c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2836c2798b19SChris Wilson 
2837c2798b19SChris Wilson 	for_each_pipe(pipe)
2838c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2839c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2840c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2841c2798b19SChris Wilson 	POSTING_READ16(IER);
2842c2798b19SChris Wilson }
2843c2798b19SChris Wilson 
2844c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2845c2798b19SChris Wilson {
2846c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2847c2798b19SChris Wilson 
2848c2798b19SChris Wilson 	I915_WRITE16(EMR,
2849c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2850c2798b19SChris Wilson 
2851c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2852c2798b19SChris Wilson 	dev_priv->irq_mask =
2853c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2854c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2855c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2856c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2857c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2858c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2859c2798b19SChris Wilson 
2860c2798b19SChris Wilson 	I915_WRITE16(IER,
2861c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2862c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2863c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2864c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2865c2798b19SChris Wilson 	POSTING_READ16(IER);
2866c2798b19SChris Wilson 
2867c2798b19SChris Wilson 	return 0;
2868c2798b19SChris Wilson }
2869c2798b19SChris Wilson 
287090a72f87SVille Syrjälä /*
287190a72f87SVille Syrjälä  * Returns true when a page flip has completed.
287290a72f87SVille Syrjälä  */
287390a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
287490a72f87SVille Syrjälä 			       int pipe, u16 iir)
287590a72f87SVille Syrjälä {
287690a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
287790a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
287890a72f87SVille Syrjälä 
287990a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
288090a72f87SVille Syrjälä 		return false;
288190a72f87SVille Syrjälä 
288290a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
288390a72f87SVille Syrjälä 		return false;
288490a72f87SVille Syrjälä 
288590a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
288690a72f87SVille Syrjälä 
288790a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
288890a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
288990a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
289090a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
289190a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
289290a72f87SVille Syrjälä 	 */
289390a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
289490a72f87SVille Syrjälä 		return false;
289590a72f87SVille Syrjälä 
289690a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
289790a72f87SVille Syrjälä 
289890a72f87SVille Syrjälä 	return true;
289990a72f87SVille Syrjälä }
290090a72f87SVille Syrjälä 
2901ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2902c2798b19SChris Wilson {
2903c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2904c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2905c2798b19SChris Wilson 	u16 iir, new_iir;
2906c2798b19SChris Wilson 	u32 pipe_stats[2];
2907c2798b19SChris Wilson 	unsigned long irqflags;
2908c2798b19SChris Wilson 	int irq_received;
2909c2798b19SChris Wilson 	int pipe;
2910c2798b19SChris Wilson 	u16 flip_mask =
2911c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2912c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2913c2798b19SChris Wilson 
2914c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2915c2798b19SChris Wilson 
2916c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2917c2798b19SChris Wilson 	if (iir == 0)
2918c2798b19SChris Wilson 		return IRQ_NONE;
2919c2798b19SChris Wilson 
2920c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2921c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2922c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2923c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2924c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2925c2798b19SChris Wilson 		 */
2926c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2927c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2928c2798b19SChris Wilson 			i915_handle_error(dev, false);
2929c2798b19SChris Wilson 
2930c2798b19SChris Wilson 		for_each_pipe(pipe) {
2931c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2932c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2933c2798b19SChris Wilson 
2934c2798b19SChris Wilson 			/*
2935c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2936c2798b19SChris Wilson 			 */
2937c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2938c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2939c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2940c2798b19SChris Wilson 							 pipe_name(pipe));
2941c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2942c2798b19SChris Wilson 				irq_received = 1;
2943c2798b19SChris Wilson 			}
2944c2798b19SChris Wilson 		}
2945c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2946c2798b19SChris Wilson 
2947c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2948c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2949c2798b19SChris Wilson 
2950d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2951c2798b19SChris Wilson 
2952c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2953c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2954c2798b19SChris Wilson 
2955c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
295690a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
295790a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2958c2798b19SChris Wilson 
2959c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
296090a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
296190a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2962c2798b19SChris Wilson 
2963c2798b19SChris Wilson 		iir = new_iir;
2964c2798b19SChris Wilson 	}
2965c2798b19SChris Wilson 
2966c2798b19SChris Wilson 	return IRQ_HANDLED;
2967c2798b19SChris Wilson }
2968c2798b19SChris Wilson 
2969c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2970c2798b19SChris Wilson {
2971c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2972c2798b19SChris Wilson 	int pipe;
2973c2798b19SChris Wilson 
2974c2798b19SChris Wilson 	for_each_pipe(pipe) {
2975c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2976c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2977c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2978c2798b19SChris Wilson 	}
2979c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2980c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2981c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2982c2798b19SChris Wilson }
2983c2798b19SChris Wilson 
2984a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2985a266c7d5SChris Wilson {
2986a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2987a266c7d5SChris Wilson 	int pipe;
2988a266c7d5SChris Wilson 
2989a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2990a266c7d5SChris Wilson 
2991a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2992a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2993a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2994a266c7d5SChris Wilson 	}
2995a266c7d5SChris Wilson 
299600d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2997a266c7d5SChris Wilson 	for_each_pipe(pipe)
2998a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2999a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3000a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3001a266c7d5SChris Wilson 	POSTING_READ(IER);
3002a266c7d5SChris Wilson }
3003a266c7d5SChris Wilson 
3004a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3005a266c7d5SChris Wilson {
3006a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
300738bde180SChris Wilson 	u32 enable_mask;
3008a266c7d5SChris Wilson 
300938bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
301038bde180SChris Wilson 
301138bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
301238bde180SChris Wilson 	dev_priv->irq_mask =
301338bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
301438bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
301538bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
301638bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
301738bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
301838bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
301938bde180SChris Wilson 
302038bde180SChris Wilson 	enable_mask =
302138bde180SChris Wilson 		I915_ASLE_INTERRUPT |
302238bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
302338bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
302438bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
302538bde180SChris Wilson 		I915_USER_INTERRUPT;
302638bde180SChris Wilson 
3027a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
302820afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
302920afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
303020afbda2SDaniel Vetter 
3031a266c7d5SChris Wilson 		/* Enable in IER... */
3032a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3033a266c7d5SChris Wilson 		/* and unmask in IMR */
3034a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3035a266c7d5SChris Wilson 	}
3036a266c7d5SChris Wilson 
3037a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3038a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3039a266c7d5SChris Wilson 	POSTING_READ(IER);
3040a266c7d5SChris Wilson 
3041f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
304220afbda2SDaniel Vetter 
304320afbda2SDaniel Vetter 	return 0;
304420afbda2SDaniel Vetter }
304520afbda2SDaniel Vetter 
304690a72f87SVille Syrjälä /*
304790a72f87SVille Syrjälä  * Returns true when a page flip has completed.
304890a72f87SVille Syrjälä  */
304990a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
305090a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
305190a72f87SVille Syrjälä {
305290a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
305390a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
305490a72f87SVille Syrjälä 
305590a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
305690a72f87SVille Syrjälä 		return false;
305790a72f87SVille Syrjälä 
305890a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
305990a72f87SVille Syrjälä 		return false;
306090a72f87SVille Syrjälä 
306190a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
306290a72f87SVille Syrjälä 
306390a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
306490a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
306590a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
306690a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
306790a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
306890a72f87SVille Syrjälä 	 */
306990a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
307090a72f87SVille Syrjälä 		return false;
307190a72f87SVille Syrjälä 
307290a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
307390a72f87SVille Syrjälä 
307490a72f87SVille Syrjälä 	return true;
307590a72f87SVille Syrjälä }
307690a72f87SVille Syrjälä 
3077ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3078a266c7d5SChris Wilson {
3079a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3080a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
30818291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3082a266c7d5SChris Wilson 	unsigned long irqflags;
308338bde180SChris Wilson 	u32 flip_mask =
308438bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
308538bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
308638bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3087a266c7d5SChris Wilson 
3088a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3089a266c7d5SChris Wilson 
3090a266c7d5SChris Wilson 	iir = I915_READ(IIR);
309138bde180SChris Wilson 	do {
309238bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
30938291ee90SChris Wilson 		bool blc_event = false;
3094a266c7d5SChris Wilson 
3095a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3096a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3097a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3098a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3099a266c7d5SChris Wilson 		 */
3100a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3101a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3102a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3103a266c7d5SChris Wilson 
3104a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3105a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3106a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3107a266c7d5SChris Wilson 
310838bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3109a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3110a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3111a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3112a266c7d5SChris Wilson 							 pipe_name(pipe));
3113a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
311438bde180SChris Wilson 				irq_received = true;
3115a266c7d5SChris Wilson 			}
3116a266c7d5SChris Wilson 		}
3117a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3118a266c7d5SChris Wilson 
3119a266c7d5SChris Wilson 		if (!irq_received)
3120a266c7d5SChris Wilson 			break;
3121a266c7d5SChris Wilson 
3122a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3123a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3124a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3125a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3126b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3127a266c7d5SChris Wilson 
3128a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3129a266c7d5SChris Wilson 				  hotplug_status);
3130b543fb04SEgbert Eich 			if (hotplug_trigger) {
3131cd569aedSEgbert Eich 				if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_status_i915))
3132cd569aedSEgbert Eich 					i915_hpd_irq_setup(dev);
3133a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
3134a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
3135b543fb04SEgbert Eich 			}
3136a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
313738bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3138a266c7d5SChris Wilson 		}
3139a266c7d5SChris Wilson 
314038bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3141a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3142a266c7d5SChris Wilson 
3143a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3144a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3145a266c7d5SChris Wilson 
3146a266c7d5SChris Wilson 		for_each_pipe(pipe) {
314738bde180SChris Wilson 			int plane = pipe;
314838bde180SChris Wilson 			if (IS_MOBILE(dev))
314938bde180SChris Wilson 				plane = !plane;
31505e2032d4SVille Syrjälä 
315190a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
315290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
315390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3154a266c7d5SChris Wilson 
3155a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3156a266c7d5SChris Wilson 				blc_event = true;
3157a266c7d5SChris Wilson 		}
3158a266c7d5SChris Wilson 
3159a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3160a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3161a266c7d5SChris Wilson 
3162a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3163a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3164a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3165a266c7d5SChris Wilson 		 * we would never get another interrupt.
3166a266c7d5SChris Wilson 		 *
3167a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3168a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3169a266c7d5SChris Wilson 		 * another one.
3170a266c7d5SChris Wilson 		 *
3171a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3172a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3173a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3174a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3175a266c7d5SChris Wilson 		 * stray interrupts.
3176a266c7d5SChris Wilson 		 */
317738bde180SChris Wilson 		ret = IRQ_HANDLED;
3178a266c7d5SChris Wilson 		iir = new_iir;
317938bde180SChris Wilson 	} while (iir & ~flip_mask);
3180a266c7d5SChris Wilson 
3181d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
31828291ee90SChris Wilson 
3183a266c7d5SChris Wilson 	return ret;
3184a266c7d5SChris Wilson }
3185a266c7d5SChris Wilson 
3186a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3187a266c7d5SChris Wilson {
3188a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3189a266c7d5SChris Wilson 	int pipe;
3190a266c7d5SChris Wilson 
3191ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3192ac4c16c5SEgbert Eich 
3193a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3194a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3195a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3196a266c7d5SChris Wilson 	}
3197a266c7d5SChris Wilson 
319800d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
319955b39755SChris Wilson 	for_each_pipe(pipe) {
320055b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3201a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
320255b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
320355b39755SChris Wilson 	}
3204a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3205a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3206a266c7d5SChris Wilson 
3207a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3208a266c7d5SChris Wilson }
3209a266c7d5SChris Wilson 
3210a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3211a266c7d5SChris Wilson {
3212a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3213a266c7d5SChris Wilson 	int pipe;
3214a266c7d5SChris Wilson 
3215a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
3216a266c7d5SChris Wilson 
3217a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3218a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3219a266c7d5SChris Wilson 
3220a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3221a266c7d5SChris Wilson 	for_each_pipe(pipe)
3222a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3223a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3224a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3225a266c7d5SChris Wilson 	POSTING_READ(IER);
3226a266c7d5SChris Wilson }
3227a266c7d5SChris Wilson 
3228a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3229a266c7d5SChris Wilson {
3230a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3231bbba0a97SChris Wilson 	u32 enable_mask;
3232a266c7d5SChris Wilson 	u32 error_mask;
3233a266c7d5SChris Wilson 
3234a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3235bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3236adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3237bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3238bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3239bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3240bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3241bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3242bbba0a97SChris Wilson 
3243bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
324421ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
324521ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3246bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3247bbba0a97SChris Wilson 
3248bbba0a97SChris Wilson 	if (IS_G4X(dev))
3249bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3250a266c7d5SChris Wilson 
3251515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
3252a266c7d5SChris Wilson 
3253a266c7d5SChris Wilson 	/*
3254a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3255a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3256a266c7d5SChris Wilson 	 */
3257a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3258a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3259a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3260a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3261a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3262a266c7d5SChris Wilson 	} else {
3263a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3264a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3265a266c7d5SChris Wilson 	}
3266a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3267a266c7d5SChris Wilson 
3268a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3269a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3270a266c7d5SChris Wilson 	POSTING_READ(IER);
3271a266c7d5SChris Wilson 
327220afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
327320afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
327420afbda2SDaniel Vetter 
3275f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
327620afbda2SDaniel Vetter 
327720afbda2SDaniel Vetter 	return 0;
327820afbda2SDaniel Vetter }
327920afbda2SDaniel Vetter 
3280bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
328120afbda2SDaniel Vetter {
328220afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3283e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3284cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
328520afbda2SDaniel Vetter 	u32 hotplug_en;
328620afbda2SDaniel Vetter 
3287bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3288bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3289bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3290adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3291e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3292cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3293cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3294cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3295a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3296a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3297a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3298a266c7d5SChris Wilson 		*/
3299a266c7d5SChris Wilson 		if (IS_G4X(dev))
3300a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
330185fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3302a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3303a266c7d5SChris Wilson 
3304a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3305a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3306a266c7d5SChris Wilson 	}
3307bac56d5bSEgbert Eich }
3308a266c7d5SChris Wilson 
3309ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3310a266c7d5SChris Wilson {
3311a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3312a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3313a266c7d5SChris Wilson 	u32 iir, new_iir;
3314a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3315a266c7d5SChris Wilson 	unsigned long irqflags;
3316a266c7d5SChris Wilson 	int irq_received;
3317a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
331821ad8330SVille Syrjälä 	u32 flip_mask =
331921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
332021ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3321a266c7d5SChris Wilson 
3322a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
3323a266c7d5SChris Wilson 
3324a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3325a266c7d5SChris Wilson 
3326a266c7d5SChris Wilson 	for (;;) {
33272c8ba29fSChris Wilson 		bool blc_event = false;
33282c8ba29fSChris Wilson 
332921ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
3330a266c7d5SChris Wilson 
3331a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3332a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3333a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3334a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3335a266c7d5SChris Wilson 		 */
3336a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3337a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3338a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3339a266c7d5SChris Wilson 
3340a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3341a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3342a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3343a266c7d5SChris Wilson 
3344a266c7d5SChris Wilson 			/*
3345a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3346a266c7d5SChris Wilson 			 */
3347a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3348a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3349a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
3350a266c7d5SChris Wilson 							 pipe_name(pipe));
3351a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3352a266c7d5SChris Wilson 				irq_received = 1;
3353a266c7d5SChris Wilson 			}
3354a266c7d5SChris Wilson 		}
3355a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3356a266c7d5SChris Wilson 
3357a266c7d5SChris Wilson 		if (!irq_received)
3358a266c7d5SChris Wilson 			break;
3359a266c7d5SChris Wilson 
3360a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3361a266c7d5SChris Wilson 
3362a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3363adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3364a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3365b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3366b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
3367b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_I965);
3368a266c7d5SChris Wilson 
3369a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3370a266c7d5SChris Wilson 				  hotplug_status);
3371b543fb04SEgbert Eich 			if (hotplug_trigger) {
3372cd569aedSEgbert Eich 				if (hotplug_irq_storm_detect(dev, hotplug_trigger,
3373cd569aedSEgbert Eich 							    IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i965))
3374cd569aedSEgbert Eich 					i915_hpd_irq_setup(dev);
3375a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
3376a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
3377b543fb04SEgbert Eich 			}
3378a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3379a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3380a266c7d5SChris Wilson 		}
3381a266c7d5SChris Wilson 
338221ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3383a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3384a266c7d5SChris Wilson 
3385a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3386a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3387a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3388a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3389a266c7d5SChris Wilson 
3390a266c7d5SChris Wilson 		for_each_pipe(pipe) {
33912c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
339290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
339390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3394a266c7d5SChris Wilson 
3395a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3396a266c7d5SChris Wilson 				blc_event = true;
3397a266c7d5SChris Wilson 		}
3398a266c7d5SChris Wilson 
3399a266c7d5SChris Wilson 
3400a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3401a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3402a266c7d5SChris Wilson 
3403515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3404515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3405515ac2bbSDaniel Vetter 
3406a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3407a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3408a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3409a266c7d5SChris Wilson 		 * we would never get another interrupt.
3410a266c7d5SChris Wilson 		 *
3411a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3412a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3413a266c7d5SChris Wilson 		 * another one.
3414a266c7d5SChris Wilson 		 *
3415a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3416a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3417a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3418a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3419a266c7d5SChris Wilson 		 * stray interrupts.
3420a266c7d5SChris Wilson 		 */
3421a266c7d5SChris Wilson 		iir = new_iir;
3422a266c7d5SChris Wilson 	}
3423a266c7d5SChris Wilson 
3424d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
34252c8ba29fSChris Wilson 
3426a266c7d5SChris Wilson 	return ret;
3427a266c7d5SChris Wilson }
3428a266c7d5SChris Wilson 
3429a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3430a266c7d5SChris Wilson {
3431a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3432a266c7d5SChris Wilson 	int pipe;
3433a266c7d5SChris Wilson 
3434a266c7d5SChris Wilson 	if (!dev_priv)
3435a266c7d5SChris Wilson 		return;
3436a266c7d5SChris Wilson 
3437ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3438ac4c16c5SEgbert Eich 
3439a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3440a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3441a266c7d5SChris Wilson 
3442a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3443a266c7d5SChris Wilson 	for_each_pipe(pipe)
3444a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3445a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3446a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3447a266c7d5SChris Wilson 
3448a266c7d5SChris Wilson 	for_each_pipe(pipe)
3449a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3450a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3451a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3452a266c7d5SChris Wilson }
3453a266c7d5SChris Wilson 
3454ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3455ac4c16c5SEgbert Eich {
3456ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3457ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3458ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3459ac4c16c5SEgbert Eich 	unsigned long irqflags;
3460ac4c16c5SEgbert Eich 	int i;
3461ac4c16c5SEgbert Eich 
3462ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3463ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3464ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3465ac4c16c5SEgbert Eich 
3466ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3467ac4c16c5SEgbert Eich 			continue;
3468ac4c16c5SEgbert Eich 
3469ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3470ac4c16c5SEgbert Eich 
3471ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3472ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3473ac4c16c5SEgbert Eich 
3474ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3475ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3476ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3477ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3478ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3479ac4c16c5SEgbert Eich 				if (!connector->polled)
3480ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3481ac4c16c5SEgbert Eich 			}
3482ac4c16c5SEgbert Eich 		}
3483ac4c16c5SEgbert Eich 	}
3484ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3485ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3486ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3487ac4c16c5SEgbert Eich }
3488ac4c16c5SEgbert Eich 
3489f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3490f71d4af4SJesse Barnes {
34918b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
34928b2e326dSChris Wilson 
34938b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
349499584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3495c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3496a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
34978b2e326dSChris Wilson 
349899584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
349999584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
350061bac78eSDaniel Vetter 		    (unsigned long) dev);
3501ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3502ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
350361bac78eSDaniel Vetter 
350497a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
35059ee32feaSDaniel Vetter 
3506f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
3507f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
35087d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3509f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3510f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3511f71d4af4SJesse Barnes 	}
3512f71d4af4SJesse Barnes 
3513c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3514f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3515c3613de9SKeith Packard 	else
3516c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
3517f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3518f71d4af4SJesse Barnes 
35197e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
35207e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
35217e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
35227e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
35237e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
35247e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
35257e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3526fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
35274a06e201SDaniel Vetter 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
3528*7d99163dSBen Widawsky 		/* Share uninstall handlers with ILK/SNB */
3529f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
3530*7d99163dSBen Widawsky 		dev->driver->irq_preinstall = ivybridge_irq_preinstall;
3531f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
3532f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3533f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
3534f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
353582a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3536f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3537f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3538f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3539f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3540f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3541f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3542f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
354382a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3544f71d4af4SJesse Barnes 	} else {
3545c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3546c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3547c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3548c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3549c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3550a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3551a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3552a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3553a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3554a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
355520afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3556c2798b19SChris Wilson 		} else {
3557a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3558a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3559a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3560a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3561bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3562c2798b19SChris Wilson 		}
3563f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3564f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3565f71d4af4SJesse Barnes 	}
3566f71d4af4SJesse Barnes }
356720afbda2SDaniel Vetter 
356820afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
356920afbda2SDaniel Vetter {
357020afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3571821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3572821450c6SEgbert Eich 	struct drm_connector *connector;
3573821450c6SEgbert Eich 	int i;
357420afbda2SDaniel Vetter 
3575821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3576821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3577821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3578821450c6SEgbert Eich 	}
3579821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3580821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3581821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3582821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3583821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3584821450c6SEgbert Eich 	}
358520afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
358620afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
358720afbda2SDaniel Vetter }
3588