xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 9719fb9852e4301d5b8d74feec141d3c3e60fae0)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33760285e7SDavid Howells #include <drm/drmP.h>
34760285e7SDavid Howells #include <drm/i915_drm.h>
35c0e09200SDave Airlie #include "i915_drv.h"
361c5d22f7SChris Wilson #include "i915_trace.h"
3779e53945SJesse Barnes #include "intel_drv.h"
38c0e09200SDave Airlie 
39e5868a31SEgbert Eich static const u32 hpd_ibx[] = {
40e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
41e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
42e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
43e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
44e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
45e5868a31SEgbert Eich };
46e5868a31SEgbert Eich 
47e5868a31SEgbert Eich static const u32 hpd_cpt[] = {
48e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
4973c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
50e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
51e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
52e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
53e5868a31SEgbert Eich };
54e5868a31SEgbert Eich 
55e5868a31SEgbert Eich static const u32 hpd_mask_i915[] = {
56e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
57e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
58e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
59e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
60e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
61e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
62e5868a31SEgbert Eich };
63e5868a31SEgbert Eich 
64e5868a31SEgbert Eich static const u32 hpd_status_gen4[] = {
65e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
66e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
67e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
68e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
69e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
70e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
71e5868a31SEgbert Eich };
72e5868a31SEgbert Eich 
73e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
74e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
75e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
76e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
77e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
78e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
79e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
80e5868a31SEgbert Eich };
81e5868a31SEgbert Eich 
82036a4a7dSZhenyu Wang /* For display hotplug interrupt */
83995b6762SChris Wilson static void
84f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
85036a4a7dSZhenyu Wang {
864bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
874bc9d430SDaniel Vetter 
881ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
891ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
901ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
913143a2bfSChris Wilson 		POSTING_READ(DEIMR);
92036a4a7dSZhenyu Wang 	}
93036a4a7dSZhenyu Wang }
94036a4a7dSZhenyu Wang 
950ff9800aSPaulo Zanoni static void
96f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
97036a4a7dSZhenyu Wang {
984bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
994bc9d430SDaniel Vetter 
1001ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1011ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1021ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1033143a2bfSChris Wilson 		POSTING_READ(DEIMR);
104036a4a7dSZhenyu Wang 	}
105036a4a7dSZhenyu Wang }
106036a4a7dSZhenyu Wang 
1078664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
1088664281bSPaulo Zanoni {
1098664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1108664281bSPaulo Zanoni 	struct intel_crtc *crtc;
1118664281bSPaulo Zanoni 	enum pipe pipe;
1128664281bSPaulo Zanoni 
1134bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1144bc9d430SDaniel Vetter 
1158664281bSPaulo Zanoni 	for_each_pipe(pipe) {
1168664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
1178664281bSPaulo Zanoni 
1188664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
1198664281bSPaulo Zanoni 			return false;
1208664281bSPaulo Zanoni 	}
1218664281bSPaulo Zanoni 
1228664281bSPaulo Zanoni 	return true;
1238664281bSPaulo Zanoni }
1248664281bSPaulo Zanoni 
1258664281bSPaulo Zanoni static bool cpt_can_enable_serr_int(struct drm_device *dev)
1268664281bSPaulo Zanoni {
1278664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1288664281bSPaulo Zanoni 	enum pipe pipe;
1298664281bSPaulo Zanoni 	struct intel_crtc *crtc;
1308664281bSPaulo Zanoni 
131fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
132fee884edSDaniel Vetter 
1338664281bSPaulo Zanoni 	for_each_pipe(pipe) {
1348664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
1358664281bSPaulo Zanoni 
1368664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
1378664281bSPaulo Zanoni 			return false;
1388664281bSPaulo Zanoni 	}
1398664281bSPaulo Zanoni 
1408664281bSPaulo Zanoni 	return true;
1418664281bSPaulo Zanoni }
1428664281bSPaulo Zanoni 
1438664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
1448664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
1458664281bSPaulo Zanoni {
1468664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1478664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
1488664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
1498664281bSPaulo Zanoni 
1508664281bSPaulo Zanoni 	if (enable)
1518664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
1528664281bSPaulo Zanoni 	else
1538664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
1548664281bSPaulo Zanoni }
1558664281bSPaulo Zanoni 
1568664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
1577336df65SDaniel Vetter 						  enum pipe pipe, bool enable)
1588664281bSPaulo Zanoni {
1598664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1608664281bSPaulo Zanoni 	if (enable) {
1617336df65SDaniel Vetter 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
1627336df65SDaniel Vetter 
1638664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
1648664281bSPaulo Zanoni 			return;
1658664281bSPaulo Zanoni 
1668664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
1678664281bSPaulo Zanoni 	} else {
1687336df65SDaniel Vetter 		bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
1697336df65SDaniel Vetter 
1707336df65SDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
1718664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
1727336df65SDaniel Vetter 
1737336df65SDaniel Vetter 		if (!was_enabled &&
1747336df65SDaniel Vetter 		    (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
1757336df65SDaniel Vetter 			DRM_DEBUG_KMS("uncleared fifo underrun on pipe %c\n",
1767336df65SDaniel Vetter 				      pipe_name(pipe));
1777336df65SDaniel Vetter 		}
1788664281bSPaulo Zanoni 	}
1798664281bSPaulo Zanoni }
1808664281bSPaulo Zanoni 
181fee884edSDaniel Vetter /**
182fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
183fee884edSDaniel Vetter  * @dev_priv: driver private
184fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
185fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
186fee884edSDaniel Vetter  */
187fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
188fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
189fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
190fee884edSDaniel Vetter {
191fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
192fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
193fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
194fee884edSDaniel Vetter 
195fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
196fee884edSDaniel Vetter 
197fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
198fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
199fee884edSDaniel Vetter }
200fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
201fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
202fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
203fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
204fee884edSDaniel Vetter 
205de28075dSDaniel Vetter static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
206de28075dSDaniel Vetter 					    enum transcoder pch_transcoder,
2078664281bSPaulo Zanoni 					    bool enable)
2088664281bSPaulo Zanoni {
2098664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
210de28075dSDaniel Vetter 	uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
211de28075dSDaniel Vetter 		       SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
2128664281bSPaulo Zanoni 
2138664281bSPaulo Zanoni 	if (enable)
214fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
2158664281bSPaulo Zanoni 	else
216fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
2178664281bSPaulo Zanoni }
2188664281bSPaulo Zanoni 
2198664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
2208664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
2218664281bSPaulo Zanoni 					    bool enable)
2228664281bSPaulo Zanoni {
2238664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2248664281bSPaulo Zanoni 
2258664281bSPaulo Zanoni 	if (enable) {
2261dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
2271dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
2281dd246fbSDaniel Vetter 
2298664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
2308664281bSPaulo Zanoni 			return;
2318664281bSPaulo Zanoni 
232fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
2338664281bSPaulo Zanoni 	} else {
2341dd246fbSDaniel Vetter 		uint32_t tmp = I915_READ(SERR_INT);
2351dd246fbSDaniel Vetter 		bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
2361dd246fbSDaniel Vetter 
2371dd246fbSDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
238fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
2391dd246fbSDaniel Vetter 
2401dd246fbSDaniel Vetter 		if (!was_enabled &&
2411dd246fbSDaniel Vetter 		    (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
2421dd246fbSDaniel Vetter 			DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
2431dd246fbSDaniel Vetter 				      transcoder_name(pch_transcoder));
2441dd246fbSDaniel Vetter 		}
2458664281bSPaulo Zanoni 	}
2468664281bSPaulo Zanoni }
2478664281bSPaulo Zanoni 
2488664281bSPaulo Zanoni /**
2498664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
2508664281bSPaulo Zanoni  * @dev: drm device
2518664281bSPaulo Zanoni  * @pipe: pipe
2528664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
2538664281bSPaulo Zanoni  *
2548664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
2558664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
2568664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
2578664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
2588664281bSPaulo Zanoni  * bit for all the pipes.
2598664281bSPaulo Zanoni  *
2608664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
2618664281bSPaulo Zanoni  */
2628664281bSPaulo Zanoni bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
2638664281bSPaulo Zanoni 					   enum pipe pipe, bool enable)
2648664281bSPaulo Zanoni {
2658664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2668664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
2678664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2688664281bSPaulo Zanoni 	unsigned long flags;
2698664281bSPaulo Zanoni 	bool ret;
2708664281bSPaulo Zanoni 
2718664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
2728664281bSPaulo Zanoni 
2738664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
2748664281bSPaulo Zanoni 
2758664281bSPaulo Zanoni 	if (enable == ret)
2768664281bSPaulo Zanoni 		goto done;
2778664281bSPaulo Zanoni 
2788664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
2798664281bSPaulo Zanoni 
2808664281bSPaulo Zanoni 	if (IS_GEN5(dev) || IS_GEN6(dev))
2818664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
2828664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
2837336df65SDaniel Vetter 		ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
2848664281bSPaulo Zanoni 
2858664281bSPaulo Zanoni done:
2868664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
2878664281bSPaulo Zanoni 	return ret;
2888664281bSPaulo Zanoni }
2898664281bSPaulo Zanoni 
2908664281bSPaulo Zanoni /**
2918664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
2928664281bSPaulo Zanoni  * @dev: drm device
2938664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
2948664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
2958664281bSPaulo Zanoni  *
2968664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
2978664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
2988664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
2998664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
3008664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
3018664281bSPaulo Zanoni  *
3028664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
3038664281bSPaulo Zanoni  */
3048664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
3058664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
3068664281bSPaulo Zanoni 					   bool enable)
3078664281bSPaulo Zanoni {
3088664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
309de28075dSDaniel Vetter 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
310de28075dSDaniel Vetter 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3118664281bSPaulo Zanoni 	unsigned long flags;
3128664281bSPaulo Zanoni 	bool ret;
3138664281bSPaulo Zanoni 
314de28075dSDaniel Vetter 	/*
315de28075dSDaniel Vetter 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
316de28075dSDaniel Vetter 	 * has only one pch transcoder A that all pipes can use. To avoid racy
317de28075dSDaniel Vetter 	 * pch transcoder -> pipe lookups from interrupt code simply store the
318de28075dSDaniel Vetter 	 * underrun statistics in crtc A. Since we never expose this anywhere
319de28075dSDaniel Vetter 	 * nor use it outside of the fifo underrun code here using the "wrong"
320de28075dSDaniel Vetter 	 * crtc on LPT won't cause issues.
321de28075dSDaniel Vetter 	 */
3228664281bSPaulo Zanoni 
3238664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
3248664281bSPaulo Zanoni 
3258664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
3268664281bSPaulo Zanoni 
3278664281bSPaulo Zanoni 	if (enable == ret)
3288664281bSPaulo Zanoni 		goto done;
3298664281bSPaulo Zanoni 
3308664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
3318664281bSPaulo Zanoni 
3328664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
333de28075dSDaniel Vetter 		ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
3348664281bSPaulo Zanoni 	else
3358664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
3368664281bSPaulo Zanoni 
3378664281bSPaulo Zanoni done:
3388664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
3398664281bSPaulo Zanoni 	return ret;
3408664281bSPaulo Zanoni }
3418664281bSPaulo Zanoni 
3428664281bSPaulo Zanoni 
3437c463586SKeith Packard void
3447c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3457c463586SKeith Packard {
3469db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
34746c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3487c463586SKeith Packard 
349b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
350b79480baSDaniel Vetter 
35146c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
35246c06a30SVille Syrjälä 		return;
35346c06a30SVille Syrjälä 
3547c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
35546c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
35646c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3573143a2bfSChris Wilson 	POSTING_READ(reg);
3587c463586SKeith Packard }
3597c463586SKeith Packard 
3607c463586SKeith Packard void
3617c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
3627c463586SKeith Packard {
3639db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
36446c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
3657c463586SKeith Packard 
366b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
367b79480baSDaniel Vetter 
36846c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
36946c06a30SVille Syrjälä 		return;
37046c06a30SVille Syrjälä 
37146c06a30SVille Syrjälä 	pipestat &= ~mask;
37246c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3733143a2bfSChris Wilson 	POSTING_READ(reg);
3747c463586SKeith Packard }
3757c463586SKeith Packard 
376c0e09200SDave Airlie /**
377f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
37801c66889SZhao Yakui  */
379f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
38001c66889SZhao Yakui {
3811ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
3821ec14ad3SChris Wilson 	unsigned long irqflags;
3831ec14ad3SChris Wilson 
384f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
385f49e38ddSJani Nikula 		return;
386f49e38ddSJani Nikula 
3871ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
38801c66889SZhao Yakui 
389f898780bSJani Nikula 	i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
390a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
391f898780bSJani Nikula 		i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
3921ec14ad3SChris Wilson 
3931ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
39401c66889SZhao Yakui }
39501c66889SZhao Yakui 
39601c66889SZhao Yakui /**
3970a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
3980a3e67a4SJesse Barnes  * @dev: DRM device
3990a3e67a4SJesse Barnes  * @pipe: pipe to check
4000a3e67a4SJesse Barnes  *
4010a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
4020a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
4030a3e67a4SJesse Barnes  * before reading such registers if unsure.
4040a3e67a4SJesse Barnes  */
4050a3e67a4SJesse Barnes static int
4060a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
4070a3e67a4SJesse Barnes {
4080a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
409702e7a56SPaulo Zanoni 
410a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
411a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
412a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
413a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
41471f8ba6bSPaulo Zanoni 
415a01025afSDaniel Vetter 		return intel_crtc->active;
416a01025afSDaniel Vetter 	} else {
417a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
418a01025afSDaniel Vetter 	}
4190a3e67a4SJesse Barnes }
4200a3e67a4SJesse Barnes 
42142f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
42242f52ef8SKeith Packard  * we use as a pipe index
42342f52ef8SKeith Packard  */
424f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
4250a3e67a4SJesse Barnes {
4260a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4270a3e67a4SJesse Barnes 	unsigned long high_frame;
4280a3e67a4SJesse Barnes 	unsigned long low_frame;
4295eddb70bSChris Wilson 	u32 high1, high2, low;
4300a3e67a4SJesse Barnes 
4310a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
43244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4339db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
4340a3e67a4SJesse Barnes 		return 0;
4350a3e67a4SJesse Barnes 	}
4360a3e67a4SJesse Barnes 
4379db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
4389db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
4395eddb70bSChris Wilson 
4400a3e67a4SJesse Barnes 	/*
4410a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
4420a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
4430a3e67a4SJesse Barnes 	 * register.
4440a3e67a4SJesse Barnes 	 */
4450a3e67a4SJesse Barnes 	do {
4465eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4475eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
4485eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
4490a3e67a4SJesse Barnes 	} while (high1 != high2);
4500a3e67a4SJesse Barnes 
4515eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
4525eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
4535eddb70bSChris Wilson 	return (high1 << 8) | low;
4540a3e67a4SJesse Barnes }
4550a3e67a4SJesse Barnes 
456f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
4579880b7a5SJesse Barnes {
4589880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4599db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
4609880b7a5SJesse Barnes 
4619880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
46244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
4639db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4649880b7a5SJesse Barnes 		return 0;
4659880b7a5SJesse Barnes 	}
4669880b7a5SJesse Barnes 
4679880b7a5SJesse Barnes 	return I915_READ(reg);
4689880b7a5SJesse Barnes }
4699880b7a5SJesse Barnes 
470f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
4710af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
4720af7e4dfSMario Kleiner {
4730af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4740af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
4750af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
4760af7e4dfSMario Kleiner 	bool in_vbl = true;
4770af7e4dfSMario Kleiner 	int ret = 0;
478fe2b8f9dSPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
479fe2b8f9dSPaulo Zanoni 								      pipe);
4800af7e4dfSMario Kleiner 
4810af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
4820af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
4839db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
4840af7e4dfSMario Kleiner 		return 0;
4850af7e4dfSMario Kleiner 	}
4860af7e4dfSMario Kleiner 
4870af7e4dfSMario Kleiner 	/* Get vtotal. */
488fe2b8f9dSPaulo Zanoni 	vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
4890af7e4dfSMario Kleiner 
4900af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
4910af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
4920af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
4930af7e4dfSMario Kleiner 		 */
4940af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
4950af7e4dfSMario Kleiner 
4960af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
4970af7e4dfSMario Kleiner 		 * horizontal scanout position.
4980af7e4dfSMario Kleiner 		 */
4990af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
5000af7e4dfSMario Kleiner 		*hpos = 0;
5010af7e4dfSMario Kleiner 	} else {
5020af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
5030af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
5040af7e4dfSMario Kleiner 		 * scanout position.
5050af7e4dfSMario Kleiner 		 */
5060af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
5070af7e4dfSMario Kleiner 
508fe2b8f9dSPaulo Zanoni 		htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
5090af7e4dfSMario Kleiner 		*vpos = position / htotal;
5100af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
5110af7e4dfSMario Kleiner 	}
5120af7e4dfSMario Kleiner 
5130af7e4dfSMario Kleiner 	/* Query vblank area. */
514fe2b8f9dSPaulo Zanoni 	vbl = I915_READ(VBLANK(cpu_transcoder));
5150af7e4dfSMario Kleiner 
5160af7e4dfSMario Kleiner 	/* Test position against vblank region. */
5170af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
5180af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
5190af7e4dfSMario Kleiner 
5200af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
5210af7e4dfSMario Kleiner 		in_vbl = false;
5220af7e4dfSMario Kleiner 
5230af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
5240af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
5250af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
5260af7e4dfSMario Kleiner 
5270af7e4dfSMario Kleiner 	/* Readouts valid? */
5280af7e4dfSMario Kleiner 	if (vbl > 0)
5290af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
5300af7e4dfSMario Kleiner 
5310af7e4dfSMario Kleiner 	/* In vblank? */
5320af7e4dfSMario Kleiner 	if (in_vbl)
5330af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
5340af7e4dfSMario Kleiner 
5350af7e4dfSMario Kleiner 	return ret;
5360af7e4dfSMario Kleiner }
5370af7e4dfSMario Kleiner 
538f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
5390af7e4dfSMario Kleiner 			      int *max_error,
5400af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
5410af7e4dfSMario Kleiner 			      unsigned flags)
5420af7e4dfSMario Kleiner {
5434041b853SChris Wilson 	struct drm_crtc *crtc;
5440af7e4dfSMario Kleiner 
5457eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
5464041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5470af7e4dfSMario Kleiner 		return -EINVAL;
5480af7e4dfSMario Kleiner 	}
5490af7e4dfSMario Kleiner 
5500af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
5514041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
5524041b853SChris Wilson 	if (crtc == NULL) {
5534041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
5544041b853SChris Wilson 		return -EINVAL;
5554041b853SChris Wilson 	}
5564041b853SChris Wilson 
5574041b853SChris Wilson 	if (!crtc->enabled) {
5584041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
5594041b853SChris Wilson 		return -EBUSY;
5604041b853SChris Wilson 	}
5610af7e4dfSMario Kleiner 
5620af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
5634041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
5644041b853SChris Wilson 						     vblank_time, flags,
5654041b853SChris Wilson 						     crtc);
5660af7e4dfSMario Kleiner }
5670af7e4dfSMario Kleiner 
568321a1b30SEgbert Eich static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
569321a1b30SEgbert Eich {
570321a1b30SEgbert Eich 	enum drm_connector_status old_status;
571321a1b30SEgbert Eich 
572321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
573321a1b30SEgbert Eich 	old_status = connector->status;
574321a1b30SEgbert Eich 
575321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
576321a1b30SEgbert Eich 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
577321a1b30SEgbert Eich 		      connector->base.id,
578321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
579321a1b30SEgbert Eich 		      old_status, connector->status);
580321a1b30SEgbert Eich 	return (old_status != connector->status);
581321a1b30SEgbert Eich }
582321a1b30SEgbert Eich 
5835ca58282SJesse Barnes /*
5845ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
5855ca58282SJesse Barnes  */
586ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
587ac4c16c5SEgbert Eich 
5885ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
5895ca58282SJesse Barnes {
5905ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5915ca58282SJesse Barnes 						    hotplug_work);
5925ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
593c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
594cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
595cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
596cd569aedSEgbert Eich 	struct drm_connector *connector;
597cd569aedSEgbert Eich 	unsigned long irqflags;
598cd569aedSEgbert Eich 	bool hpd_disabled = false;
599321a1b30SEgbert Eich 	bool changed = false;
600142e2398SEgbert Eich 	u32 hpd_event_bits;
6015ca58282SJesse Barnes 
60252d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
60352d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
60452d7ecedSDaniel Vetter 		return;
60552d7ecedSDaniel Vetter 
606a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
607e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
608e67189abSJesse Barnes 
609cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
610142e2398SEgbert Eich 
611142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
612142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
613cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
614cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
615cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
616cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
617cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
618cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
619cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
620cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
621cd569aedSEgbert Eich 				drm_get_connector_name(connector));
622cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
623cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
624cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
625cd569aedSEgbert Eich 			hpd_disabled = true;
626cd569aedSEgbert Eich 		}
627142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
628142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
629142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
630142e2398SEgbert Eich 		}
631cd569aedSEgbert Eich 	}
632cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
633cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
634cd569aedSEgbert Eich 	  * some connectors */
635ac4c16c5SEgbert Eich 	if (hpd_disabled) {
636cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
637ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
638ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
639ac4c16c5SEgbert Eich 	}
640cd569aedSEgbert Eich 
641cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
642cd569aedSEgbert Eich 
643321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
644321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
645321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
646321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
647cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
648cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
649321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
650321a1b30SEgbert Eich 				changed = true;
651321a1b30SEgbert Eich 		}
652321a1b30SEgbert Eich 	}
65340ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
65440ee3381SKeith Packard 
655321a1b30SEgbert Eich 	if (changed)
656321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
6575ca58282SJesse Barnes }
6585ca58282SJesse Barnes 
659d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
660f97108d1SJesse Barnes {
661f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
662b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
6639270388eSDaniel Vetter 	u8 new_delay;
6649270388eSDaniel Vetter 
665d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
666f97108d1SJesse Barnes 
66773edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
66873edd18fSDaniel Vetter 
66920e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
6709270388eSDaniel Vetter 
6717648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
672b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
673b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
674f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
675f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
676f97108d1SJesse Barnes 
677f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
678b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
67920e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
68020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
68120e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
68220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
683b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
68420e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
68520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
68620e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
68720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
688f97108d1SJesse Barnes 	}
689f97108d1SJesse Barnes 
6907648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
69120e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
692f97108d1SJesse Barnes 
693d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
6949270388eSDaniel Vetter 
695f97108d1SJesse Barnes 	return;
696f97108d1SJesse Barnes }
697f97108d1SJesse Barnes 
698549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
699549f7365SChris Wilson 			struct intel_ring_buffer *ring)
700549f7365SChris Wilson {
701475553deSChris Wilson 	if (ring->obj == NULL)
702475553deSChris Wilson 		return;
703475553deSChris Wilson 
704b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
7059862e600SChris Wilson 
706549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
70710cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
708549f7365SChris Wilson }
709549f7365SChris Wilson 
7104912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
7113b8d8d91SJesse Barnes {
7124912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
713c6a828d3SDaniel Vetter 						    rps.work);
7144912d041SBen Widawsky 	u32 pm_iir, pm_imr;
7157b9e0ae6SChris Wilson 	u8 new_delay;
7163b8d8d91SJesse Barnes 
71759cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
718c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
719c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
7204912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
7214848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
7224848405cSBen Widawsky 	I915_WRITE(GEN6_PMIMR, pm_imr & ~GEN6_PM_RPS_EVENTS);
72359cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
7244912d041SBen Widawsky 
7254848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
7263b8d8d91SJesse Barnes 		return;
7273b8d8d91SJesse Barnes 
7284fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
7297b9e0ae6SChris Wilson 
7307425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
731c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
7327425034aSVille Syrjälä 
7337425034aSVille Syrjälä 		/*
7347425034aSVille Syrjälä 		 * For better performance, jump directly
7357425034aSVille Syrjälä 		 * to RPe if we're below it.
7367425034aSVille Syrjälä 		 */
7377425034aSVille Syrjälä 		if (IS_VALLEYVIEW(dev_priv->dev) &&
7387425034aSVille Syrjälä 		    dev_priv->rps.cur_delay < dev_priv->rps.rpe_delay)
7397425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
7407425034aSVille Syrjälä 	} else
741c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
7423b8d8d91SJesse Barnes 
74379249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
74479249636SBen Widawsky 	 * interrupt
74579249636SBen Widawsky 	 */
746d8289c9eSVille Syrjälä 	if (new_delay >= dev_priv->rps.min_delay &&
747d8289c9eSVille Syrjälä 	    new_delay <= dev_priv->rps.max_delay) {
7480a073b84SJesse Barnes 		if (IS_VALLEYVIEW(dev_priv->dev))
7490a073b84SJesse Barnes 			valleyview_set_rps(dev_priv->dev, new_delay);
7500a073b84SJesse Barnes 		else
7514912d041SBen Widawsky 			gen6_set_rps(dev_priv->dev, new_delay);
75279249636SBen Widawsky 	}
7533b8d8d91SJesse Barnes 
75452ceb908SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev)) {
75552ceb908SJesse Barnes 		/*
75652ceb908SJesse Barnes 		 * On VLV, when we enter RC6 we may not be at the minimum
75752ceb908SJesse Barnes 		 * voltage level, so arm a timer to check.  It should only
75852ceb908SJesse Barnes 		 * fire when there's activity or once after we've entered
75952ceb908SJesse Barnes 		 * RC6, and then won't be re-armed until the next RPS interrupt.
76052ceb908SJesse Barnes 		 */
76152ceb908SJesse Barnes 		mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
76252ceb908SJesse Barnes 				 msecs_to_jiffies(100));
76352ceb908SJesse Barnes 	}
76452ceb908SJesse Barnes 
7654fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
7663b8d8d91SJesse Barnes }
7673b8d8d91SJesse Barnes 
768e3689190SBen Widawsky 
769e3689190SBen Widawsky /**
770e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
771e3689190SBen Widawsky  * occurred.
772e3689190SBen Widawsky  * @work: workqueue struct
773e3689190SBen Widawsky  *
774e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
775e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
776e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
777e3689190SBen Widawsky  */
778e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
779e3689190SBen Widawsky {
780e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
781a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
782e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
783e3689190SBen Widawsky 	char *parity_event[5];
784e3689190SBen Widawsky 	uint32_t misccpctl;
785e3689190SBen Widawsky 	unsigned long flags;
786e3689190SBen Widawsky 
787e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
788e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
789e3689190SBen Widawsky 	 * any time we access those registers.
790e3689190SBen Widawsky 	 */
791e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
792e3689190SBen Widawsky 
793e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
794e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
795e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
796e3689190SBen Widawsky 
797e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
798e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
799e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
800e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
801e3689190SBen Widawsky 
802e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
803e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
804e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
805e3689190SBen Widawsky 
806e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
807e3689190SBen Widawsky 
808e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
809cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
810e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
811e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
812e3689190SBen Widawsky 
813e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
814e3689190SBen Widawsky 
815e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
816e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
817e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
818e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
819e3689190SBen Widawsky 	parity_event[4] = NULL;
820e3689190SBen Widawsky 
821e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
822e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
823e3689190SBen Widawsky 
824e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
825e3689190SBen Widawsky 		  row, bank, subbank);
826e3689190SBen Widawsky 
827e3689190SBen Widawsky 	kfree(parity_event[3]);
828e3689190SBen Widawsky 	kfree(parity_event[2]);
829e3689190SBen Widawsky 	kfree(parity_event[1]);
830e3689190SBen Widawsky }
831e3689190SBen Widawsky 
832d0ecd7e2SDaniel Vetter static void ivybridge_parity_error_irq_handler(struct drm_device *dev)
833e3689190SBen Widawsky {
834e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
835e3689190SBen Widawsky 
836e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
837e3689190SBen Widawsky 		return;
838e3689190SBen Widawsky 
839d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
840cc609d5dSBen Widawsky 	dev_priv->gt_irq_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
841e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
842d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
843e3689190SBen Widawsky 
844a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
845e3689190SBen Widawsky }
846e3689190SBen Widawsky 
847e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
848e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
849e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
850e7b4c6b1SDaniel Vetter {
851e7b4c6b1SDaniel Vetter 
852cc609d5dSBen Widawsky 	if (gt_iir &
853cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
854e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
855cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
856e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
857cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
858e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
859e7b4c6b1SDaniel Vetter 
860cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
861cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
862cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
863e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
864e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
865e7b4c6b1SDaniel Vetter 	}
866e3689190SBen Widawsky 
867cc609d5dSBen Widawsky 	if (gt_iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
868d0ecd7e2SDaniel Vetter 		ivybridge_parity_error_irq_handler(dev);
869e7b4c6b1SDaniel Vetter }
870e7b4c6b1SDaniel Vetter 
871baf02a1fSBen Widawsky /* Legacy way of handling PM interrupts */
872d0ecd7e2SDaniel Vetter static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv,
873fc6826d1SChris Wilson 				 u32 pm_iir)
874fc6826d1SChris Wilson {
875fc6826d1SChris Wilson 	/*
876fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
877fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
878fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
879c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
880fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
881fc6826d1SChris Wilson 	 *
882c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
883fc6826d1SChris Wilson 	 */
884fc6826d1SChris Wilson 
88559cdb63dSDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
886c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
887c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
888fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
88959cdb63dSDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
890fc6826d1SChris Wilson 
891c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
892fc6826d1SChris Wilson }
893fc6826d1SChris Wilson 
894b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
895b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
896b543fb04SEgbert Eich 
89710a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
898b543fb04SEgbert Eich 					 u32 hotplug_trigger,
899b543fb04SEgbert Eich 					 const u32 *hpd)
900b543fb04SEgbert Eich {
901b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
902b543fb04SEgbert Eich 	int i;
90310a504deSDaniel Vetter 	bool storm_detected = false;
904b543fb04SEgbert Eich 
90591d131d2SDaniel Vetter 	if (!hotplug_trigger)
90691d131d2SDaniel Vetter 		return;
90791d131d2SDaniel Vetter 
908b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
909b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
910821450c6SEgbert Eich 
911b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
912b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
913b543fb04SEgbert Eich 			continue;
914b543fb04SEgbert Eich 
915bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
916b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
917b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
918b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
919b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
920b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
921b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
922b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
923142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
924b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
92510a504deSDaniel Vetter 			storm_detected = true;
926b543fb04SEgbert Eich 		} else {
927b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
928b543fb04SEgbert Eich 		}
929b543fb04SEgbert Eich 	}
930b543fb04SEgbert Eich 
93110a504deSDaniel Vetter 	if (storm_detected)
93210a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
933b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
9345876fa0dSDaniel Vetter 
9355876fa0dSDaniel Vetter 	queue_work(dev_priv->wq,
9365876fa0dSDaniel Vetter 		   &dev_priv->hotplug_work);
937b543fb04SEgbert Eich }
938b543fb04SEgbert Eich 
939515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
940515ac2bbSDaniel Vetter {
94128c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
94228c70f16SDaniel Vetter 
94328c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
944515ac2bbSDaniel Vetter }
945515ac2bbSDaniel Vetter 
946ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
947ce99c256SDaniel Vetter {
9489ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
9499ee32feaSDaniel Vetter 
9509ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
951ce99c256SDaniel Vetter }
952ce99c256SDaniel Vetter 
953d0ecd7e2SDaniel Vetter /* Unlike gen6_rps_irq_handler() from which this function is originally derived,
954baf02a1fSBen Widawsky  * we must be able to deal with other PM interrupts. This is complicated because
955baf02a1fSBen Widawsky  * of the way in which we use the masks to defer the RPS work (which for
956baf02a1fSBen Widawsky  * posterity is necessary because of forcewake).
957baf02a1fSBen Widawsky  */
958baf02a1fSBen Widawsky static void hsw_pm_irq_handler(struct drm_i915_private *dev_priv,
959baf02a1fSBen Widawsky 			       u32 pm_iir)
960baf02a1fSBen Widawsky {
96141a05a3aSDaniel Vetter 	if (pm_iir & GEN6_PM_RPS_EVENTS) {
96259cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
9634848405cSBen Widawsky 		dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
964baf02a1fSBen Widawsky 		I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
965baf02a1fSBen Widawsky 		/* never want to mask useful interrupts. (also posting read) */
9664848405cSBen Widawsky 		WARN_ON(I915_READ_NOTRACE(GEN6_PMIMR) & ~GEN6_PM_RPS_EVENTS);
96759cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
9682adbee62SDaniel Vetter 
9692adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
97041a05a3aSDaniel Vetter 	}
971baf02a1fSBen Widawsky 
97212638c57SBen Widawsky 	if (pm_iir & PM_VEBOX_USER_INTERRUPT)
97312638c57SBen Widawsky 		notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
97412638c57SBen Widawsky 
97512638c57SBen Widawsky 	if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
97612638c57SBen Widawsky 		DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
97712638c57SBen Widawsky 		i915_handle_error(dev_priv->dev, false);
97812638c57SBen Widawsky 	}
97912638c57SBen Widawsky }
980baf02a1fSBen Widawsky 
981ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
9827e231dbeSJesse Barnes {
9837e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
9847e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
9857e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
9867e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
9877e231dbeSJesse Barnes 	unsigned long irqflags;
9887e231dbeSJesse Barnes 	int pipe;
9897e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
9907e231dbeSJesse Barnes 
9917e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
9927e231dbeSJesse Barnes 
9937e231dbeSJesse Barnes 	while (true) {
9947e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
9957e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
9967e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
9977e231dbeSJesse Barnes 
9987e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
9997e231dbeSJesse Barnes 			goto out;
10007e231dbeSJesse Barnes 
10017e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
10027e231dbeSJesse Barnes 
1003e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
10047e231dbeSJesse Barnes 
10057e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
10067e231dbeSJesse Barnes 		for_each_pipe(pipe) {
10077e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
10087e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
10097e231dbeSJesse Barnes 
10107e231dbeSJesse Barnes 			/*
10117e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
10127e231dbeSJesse Barnes 			 */
10137e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
10147e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
10157e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
10167e231dbeSJesse Barnes 							 pipe_name(pipe));
10177e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
10187e231dbeSJesse Barnes 			}
10197e231dbeSJesse Barnes 		}
10207e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
10217e231dbeSJesse Barnes 
102231acc7f5SJesse Barnes 		for_each_pipe(pipe) {
102331acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
102431acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
102531acc7f5SJesse Barnes 
102631acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
102731acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
102831acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
102931acc7f5SJesse Barnes 			}
103031acc7f5SJesse Barnes 		}
103131acc7f5SJesse Barnes 
10327e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
10337e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
10347e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1035b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
10367e231dbeSJesse Barnes 
10377e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
10387e231dbeSJesse Barnes 					 hotplug_status);
103991d131d2SDaniel Vetter 
104010a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
104191d131d2SDaniel Vetter 
10427e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
10437e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
10447e231dbeSJesse Barnes 		}
10457e231dbeSJesse Barnes 
1046515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1047515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
10487e231dbeSJesse Barnes 
10494848405cSBen Widawsky 		if (pm_iir & GEN6_PM_RPS_EVENTS)
1050d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
10517e231dbeSJesse Barnes 
10527e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
10537e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
10547e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
10557e231dbeSJesse Barnes 	}
10567e231dbeSJesse Barnes 
10577e231dbeSJesse Barnes out:
10587e231dbeSJesse Barnes 	return ret;
10597e231dbeSJesse Barnes }
10607e231dbeSJesse Barnes 
106123e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1062776ad806SJesse Barnes {
1063776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10649db4a9c7SJesse Barnes 	int pipe;
1065b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1066776ad806SJesse Barnes 
106710a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
106891d131d2SDaniel Vetter 
1069cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1070cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1071776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1072cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1073cfc33bf7SVille Syrjälä 				 port_name(port));
1074cfc33bf7SVille Syrjälä 	}
1075776ad806SJesse Barnes 
1076ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1077ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1078ce99c256SDaniel Vetter 
1079776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1080515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1081776ad806SJesse Barnes 
1082776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1083776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1084776ad806SJesse Barnes 
1085776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1086776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1087776ad806SJesse Barnes 
1088776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1089776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1090776ad806SJesse Barnes 
10919db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
10929db4a9c7SJesse Barnes 		for_each_pipe(pipe)
10939db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
10949db4a9c7SJesse Barnes 					 pipe_name(pipe),
10959db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1096776ad806SJesse Barnes 
1097776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1098776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1099776ad806SJesse Barnes 
1100776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1101776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1102776ad806SJesse Barnes 
1103776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
11048664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
11058664281bSPaulo Zanoni 							  false))
11068664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
11078664281bSPaulo Zanoni 
11088664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
11098664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
11108664281bSPaulo Zanoni 							  false))
11118664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
11128664281bSPaulo Zanoni }
11138664281bSPaulo Zanoni 
11148664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
11158664281bSPaulo Zanoni {
11168664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
11178664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
11188664281bSPaulo Zanoni 
1119de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1120de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1121de032bf4SPaulo Zanoni 
11228664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_A)
11238664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
11248664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
11258664281bSPaulo Zanoni 
11268664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_B)
11278664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
11288664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
11298664281bSPaulo Zanoni 
11308664281bSPaulo Zanoni 	if (err_int & ERR_INT_FIFO_UNDERRUN_C)
11318664281bSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
11328664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
11338664281bSPaulo Zanoni 
11348664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
11358664281bSPaulo Zanoni }
11368664281bSPaulo Zanoni 
11378664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
11388664281bSPaulo Zanoni {
11398664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
11408664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
11418664281bSPaulo Zanoni 
1142de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1143de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1144de032bf4SPaulo Zanoni 
11458664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
11468664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
11478664281bSPaulo Zanoni 							  false))
11488664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
11498664281bSPaulo Zanoni 
11508664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
11518664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
11528664281bSPaulo Zanoni 							  false))
11538664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
11548664281bSPaulo Zanoni 
11558664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
11568664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
11578664281bSPaulo Zanoni 							  false))
11588664281bSPaulo Zanoni 			DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
11598664281bSPaulo Zanoni 
11608664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1161776ad806SJesse Barnes }
1162776ad806SJesse Barnes 
116323e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
116423e81d69SAdam Jackson {
116523e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
116623e81d69SAdam Jackson 	int pipe;
1167b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
116823e81d69SAdam Jackson 
116910a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
117091d131d2SDaniel Vetter 
1171cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1172cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
117323e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1174cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1175cfc33bf7SVille Syrjälä 				 port_name(port));
1176cfc33bf7SVille Syrjälä 	}
117723e81d69SAdam Jackson 
117823e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1179ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
118023e81d69SAdam Jackson 
118123e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1182515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
118323e81d69SAdam Jackson 
118423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
118523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
118623e81d69SAdam Jackson 
118723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
118823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
118923e81d69SAdam Jackson 
119023e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
119123e81d69SAdam Jackson 		for_each_pipe(pipe)
119223e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
119323e81d69SAdam Jackson 					 pipe_name(pipe),
119423e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
11958664281bSPaulo Zanoni 
11968664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
11978664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
119823e81d69SAdam Jackson }
119923e81d69SAdam Jackson 
1200c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1201c008bc6eSPaulo Zanoni {
1202c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1203c008bc6eSPaulo Zanoni 
1204c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1205c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1206c008bc6eSPaulo Zanoni 
1207c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1208c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1209c008bc6eSPaulo Zanoni 
1210c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEA_VBLANK)
1211c008bc6eSPaulo Zanoni 		drm_handle_vblank(dev, 0);
1212c008bc6eSPaulo Zanoni 
1213c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEB_VBLANK)
1214c008bc6eSPaulo Zanoni 		drm_handle_vblank(dev, 1);
1215c008bc6eSPaulo Zanoni 
1216c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1217c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1218c008bc6eSPaulo Zanoni 
1219c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
1220c008bc6eSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1221c008bc6eSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1222c008bc6eSPaulo Zanoni 
1223c008bc6eSPaulo Zanoni 	if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
1224c008bc6eSPaulo Zanoni 		if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1225c008bc6eSPaulo Zanoni 			DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1226c008bc6eSPaulo Zanoni 
1227c008bc6eSPaulo Zanoni 	if (de_iir & DE_PLANEA_FLIP_DONE) {
1228c008bc6eSPaulo Zanoni 		intel_prepare_page_flip(dev, 0);
1229c008bc6eSPaulo Zanoni 		intel_finish_page_flip_plane(dev, 0);
1230c008bc6eSPaulo Zanoni 	}
1231c008bc6eSPaulo Zanoni 
1232c008bc6eSPaulo Zanoni 	if (de_iir & DE_PLANEB_FLIP_DONE) {
1233c008bc6eSPaulo Zanoni 		intel_prepare_page_flip(dev, 1);
1234c008bc6eSPaulo Zanoni 		intel_finish_page_flip_plane(dev, 1);
1235c008bc6eSPaulo Zanoni 	}
1236c008bc6eSPaulo Zanoni 
1237c008bc6eSPaulo Zanoni 	/* check event from PCH */
1238c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1239c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1240c008bc6eSPaulo Zanoni 
1241c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1242c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1243c008bc6eSPaulo Zanoni 		else
1244c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1245c008bc6eSPaulo Zanoni 
1246c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1247c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1248c008bc6eSPaulo Zanoni 	}
1249c008bc6eSPaulo Zanoni 
1250c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1251c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1252c008bc6eSPaulo Zanoni }
1253c008bc6eSPaulo Zanoni 
1254*9719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
1255*9719fb98SPaulo Zanoni {
1256*9719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
1257*9719fb98SPaulo Zanoni 	int i;
1258*9719fb98SPaulo Zanoni 
1259*9719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
1260*9719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
1261*9719fb98SPaulo Zanoni 
1262*9719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
1263*9719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
1264*9719fb98SPaulo Zanoni 
1265*9719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
1266*9719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
1267*9719fb98SPaulo Zanoni 
1268*9719fb98SPaulo Zanoni 	for (i = 0; i < 3; i++) {
1269*9719fb98SPaulo Zanoni 		if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
1270*9719fb98SPaulo Zanoni 			drm_handle_vblank(dev, i);
1271*9719fb98SPaulo Zanoni 		if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
1272*9719fb98SPaulo Zanoni 			intel_prepare_page_flip(dev, i);
1273*9719fb98SPaulo Zanoni 			intel_finish_page_flip_plane(dev, i);
1274*9719fb98SPaulo Zanoni 		}
1275*9719fb98SPaulo Zanoni 	}
1276*9719fb98SPaulo Zanoni 
1277*9719fb98SPaulo Zanoni 	/* check event from PCH */
1278*9719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
1279*9719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1280*9719fb98SPaulo Zanoni 
1281*9719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
1282*9719fb98SPaulo Zanoni 
1283*9719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
1284*9719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1285*9719fb98SPaulo Zanoni 	}
1286*9719fb98SPaulo Zanoni }
1287*9719fb98SPaulo Zanoni 
1288ff1f525eSDaniel Vetter static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
1289b1f14ad0SJesse Barnes {
1290b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1291b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1292ab5c608bSBen Widawsky 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
12930e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1294b1f14ad0SJesse Barnes 
1295b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
1296b1f14ad0SJesse Barnes 
12978664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
12988664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
12998664281bSPaulo Zanoni 	if (IS_HASWELL(dev) &&
13008664281bSPaulo Zanoni 	    (I915_READ_NOTRACE(FPGA_DBG) & FPGA_DBG_RM_NOCLAIM)) {
13018664281bSPaulo Zanoni 		DRM_ERROR("Unclaimed register before interrupt\n");
13028664281bSPaulo Zanoni 		I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
13038664281bSPaulo Zanoni 	}
13048664281bSPaulo Zanoni 
1305b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1306b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1307b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
13080e43406bSChris Wilson 
130944498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
131044498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
131144498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
131244498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
131344498aeaSPaulo Zanoni 	 * due to its back queue). */
1314ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
131544498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
131644498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
131744498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1318ab5c608bSBen Widawsky 	}
131944498aeaSPaulo Zanoni 
13208664281bSPaulo Zanoni 	/* On Haswell, also mask ERR_INT because we don't want to risk
13218664281bSPaulo Zanoni 	 * generating "unclaimed register" interrupts from inside the interrupt
13228664281bSPaulo Zanoni 	 * handler. */
13234bc9d430SDaniel Vetter 	if (IS_HASWELL(dev)) {
13244bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
13258664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
13264bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
13274bc9d430SDaniel Vetter 	}
13288664281bSPaulo Zanoni 
13290e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
13300e43406bSChris Wilson 	if (gt_iir) {
13310e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
13320e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
13330e43406bSChris Wilson 		ret = IRQ_HANDLED;
13340e43406bSChris Wilson 	}
1335b1f14ad0SJesse Barnes 
1336b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
13370e43406bSChris Wilson 	if (de_iir) {
1338*9719fb98SPaulo Zanoni 		ivb_display_irq_handler(dev, de_iir);
1339b1f14ad0SJesse Barnes 
13400e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
13410e43406bSChris Wilson 		ret = IRQ_HANDLED;
13420e43406bSChris Wilson 	}
13430e43406bSChris Wilson 
13440e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
13450e43406bSChris Wilson 	if (pm_iir) {
1346baf02a1fSBen Widawsky 		if (IS_HASWELL(dev))
1347baf02a1fSBen Widawsky 			hsw_pm_irq_handler(dev_priv, pm_iir);
13484848405cSBen Widawsky 		else if (pm_iir & GEN6_PM_RPS_EVENTS)
1349d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
1350b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
13510e43406bSChris Wilson 		ret = IRQ_HANDLED;
13520e43406bSChris Wilson 	}
1353b1f14ad0SJesse Barnes 
13544bc9d430SDaniel Vetter 	if (IS_HASWELL(dev)) {
13554bc9d430SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
13564bc9d430SDaniel Vetter 		if (ivb_can_enable_err_int(dev))
13578664281bSPaulo Zanoni 			ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
13584bc9d430SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
13594bc9d430SDaniel Vetter 	}
13608664281bSPaulo Zanoni 
1361b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1362b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1363ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
136444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
136544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1366ab5c608bSBen Widawsky 	}
1367b1f14ad0SJesse Barnes 
1368b1f14ad0SJesse Barnes 	return ret;
1369b1f14ad0SJesse Barnes }
1370b1f14ad0SJesse Barnes 
1371e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
1372e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1373e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1374e7b4c6b1SDaniel Vetter {
1375cc609d5dSBen Widawsky 	if (gt_iir &
1376cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1377e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1378cc609d5dSBen Widawsky 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1379e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1380e7b4c6b1SDaniel Vetter }
1381e7b4c6b1SDaniel Vetter 
1382ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1383036a4a7dSZhenyu Wang {
13844697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1385036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1386036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
138744498aeaSPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
1388881f47b6SXiang, Haihao 
13894697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
13904697995bSJesse Barnes 
13912d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
13922d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
13932d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
13943143a2bfSChris Wilson 	POSTING_READ(DEIER);
13952d109a84SZou, Nanhai 
139644498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
139744498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
139844498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
139944498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
140044498aeaSPaulo Zanoni 	 * due to its back queue). */
140144498aeaSPaulo Zanoni 	sde_ier = I915_READ(SDEIER);
140244498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, 0);
140344498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
140444498aeaSPaulo Zanoni 
1405036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
1406036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
14073b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
1408036a4a7dSZhenyu Wang 
1409acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
1410c7c85101SZou Nan hai 		goto done;
1411036a4a7dSZhenyu Wang 
1412036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
1413036a4a7dSZhenyu Wang 
1414e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
1415e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
1416e7b4c6b1SDaniel Vetter 	else
1417e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1418036a4a7dSZhenyu Wang 
1419c008bc6eSPaulo Zanoni 	if (de_iir)
1420c008bc6eSPaulo Zanoni 		ilk_display_irq_handler(dev, de_iir);
1421f97108d1SJesse Barnes 
14224848405cSBen Widawsky 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_RPS_EVENTS)
1423d0ecd7e2SDaniel Vetter 		gen6_rps_irq_handler(dev_priv, pm_iir);
14243b8d8d91SJesse Barnes 
1425c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
1426c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
14274912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
1428036a4a7dSZhenyu Wang 
1429c7c85101SZou Nan hai done:
14302d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
14313143a2bfSChris Wilson 	POSTING_READ(DEIER);
143244498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, sde_ier);
143344498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
14342d109a84SZou, Nanhai 
1435036a4a7dSZhenyu Wang 	return ret;
1436036a4a7dSZhenyu Wang }
1437036a4a7dSZhenyu Wang 
14388a905236SJesse Barnes /**
14398a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
14408a905236SJesse Barnes  * @work: work struct
14418a905236SJesse Barnes  *
14428a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
14438a905236SJesse Barnes  * was detected.
14448a905236SJesse Barnes  */
14458a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
14468a905236SJesse Barnes {
14471f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
14481f83fee0SDaniel Vetter 						    work);
14491f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
14501f83fee0SDaniel Vetter 						    gpu_error);
14518a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1452f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
1453f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
1454f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
1455f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
1456f69061beSDaniel Vetter 	int i, ret;
14578a905236SJesse Barnes 
1458f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
14598a905236SJesse Barnes 
14607db0ba24SDaniel Vetter 	/*
14617db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
14627db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
14637db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
14647db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
14657db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
14667db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
14677db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
14687db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
14697db0ba24SDaniel Vetter 	 */
14707db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
147144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
14727db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
14737db0ba24SDaniel Vetter 				   reset_event);
14741f83fee0SDaniel Vetter 
1475f69061beSDaniel Vetter 		ret = i915_reset(dev);
1476f69061beSDaniel Vetter 
1477f69061beSDaniel Vetter 		if (ret == 0) {
1478f69061beSDaniel Vetter 			/*
1479f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
1480f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
1481f69061beSDaniel Vetter 			 * complete.
1482f69061beSDaniel Vetter 			 *
1483f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
1484f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
1485f69061beSDaniel Vetter 			 * updates before
1486f69061beSDaniel Vetter 			 * the counter increment.
1487f69061beSDaniel Vetter 			 */
1488f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
1489f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
1490f69061beSDaniel Vetter 
1491f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
1492f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
14931f83fee0SDaniel Vetter 		} else {
14941f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
1495f316a42cSBen Gamari 		}
14961f83fee0SDaniel Vetter 
1497f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
1498f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
1499f69061beSDaniel Vetter 
150096a02917SVille Syrjälä 		intel_display_handle_reset(dev);
150196a02917SVille Syrjälä 
15021f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
1503f316a42cSBen Gamari 	}
15048a905236SJesse Barnes }
15058a905236SJesse Barnes 
150635aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1507c0e09200SDave Airlie {
15088a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1509bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
151063eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1511050ee91fSBen Widawsky 	int pipe, i;
151263eeaf38SJesse Barnes 
151335aed2e6SChris Wilson 	if (!eir)
151435aed2e6SChris Wilson 		return;
151563eeaf38SJesse Barnes 
1516a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
15178a905236SJesse Barnes 
1518bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1519bd9854f9SBen Widawsky 
15208a905236SJesse Barnes 	if (IS_G4X(dev)) {
15218a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
15228a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
15238a905236SJesse Barnes 
1524a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1525a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1526050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1527050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1528a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1529a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
15308a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
15313143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
15328a905236SJesse Barnes 		}
15338a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
15348a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1535a70491ccSJoe Perches 			pr_err("page table error\n");
1536a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
15378a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
15383143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
15398a905236SJesse Barnes 		}
15408a905236SJesse Barnes 	}
15418a905236SJesse Barnes 
1542a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
154363eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
154463eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1545a70491ccSJoe Perches 			pr_err("page table error\n");
1546a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
154763eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
15483143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
154963eeaf38SJesse Barnes 		}
15508a905236SJesse Barnes 	}
15518a905236SJesse Barnes 
155263eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1553a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
15549db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1555a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
15569db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
155763eeaf38SJesse Barnes 		/* pipestat has already been acked */
155863eeaf38SJesse Barnes 	}
155963eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1560a70491ccSJoe Perches 		pr_err("instruction error\n");
1561a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1562050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1563050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1564a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
156563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
156663eeaf38SJesse Barnes 
1567a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1568a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1569a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
157063eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
15713143a2bfSChris Wilson 			POSTING_READ(IPEIR);
157263eeaf38SJesse Barnes 		} else {
157363eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
157463eeaf38SJesse Barnes 
1575a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1576a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1577a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1578a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
157963eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
15803143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
158163eeaf38SJesse Barnes 		}
158263eeaf38SJesse Barnes 	}
158363eeaf38SJesse Barnes 
158463eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
15853143a2bfSChris Wilson 	POSTING_READ(EIR);
158663eeaf38SJesse Barnes 	eir = I915_READ(EIR);
158763eeaf38SJesse Barnes 	if (eir) {
158863eeaf38SJesse Barnes 		/*
158963eeaf38SJesse Barnes 		 * some errors might have become stuck,
159063eeaf38SJesse Barnes 		 * mask them.
159163eeaf38SJesse Barnes 		 */
159263eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
159363eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
159463eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
159563eeaf38SJesse Barnes 	}
159635aed2e6SChris Wilson }
159735aed2e6SChris Wilson 
159835aed2e6SChris Wilson /**
159935aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
160035aed2e6SChris Wilson  * @dev: drm device
160135aed2e6SChris Wilson  *
160235aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
160335aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
160435aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
160535aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
160635aed2e6SChris Wilson  * of a ring dump etc.).
160735aed2e6SChris Wilson  */
1608527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
160935aed2e6SChris Wilson {
161035aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1611b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1612b4519513SChris Wilson 	int i;
161335aed2e6SChris Wilson 
161435aed2e6SChris Wilson 	i915_capture_error_state(dev);
161535aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
16168a905236SJesse Barnes 
1617ba1234d1SBen Gamari 	if (wedged) {
1618f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1619f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
1620ba1234d1SBen Gamari 
162111ed50ecSBen Gamari 		/*
16221f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
16231f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
162411ed50ecSBen Gamari 		 */
1625b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1626b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
162711ed50ecSBen Gamari 	}
162811ed50ecSBen Gamari 
162999584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
16308a905236SJesse Barnes }
16318a905236SJesse Barnes 
163221ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
16334e5359cdSSimon Farnsworth {
16344e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
16354e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
16364e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
163705394f39SChris Wilson 	struct drm_i915_gem_object *obj;
16384e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
16394e5359cdSSimon Farnsworth 	unsigned long flags;
16404e5359cdSSimon Farnsworth 	bool stall_detected;
16414e5359cdSSimon Farnsworth 
16424e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
16434e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
16444e5359cdSSimon Farnsworth 		return;
16454e5359cdSSimon Farnsworth 
16464e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
16474e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
16484e5359cdSSimon Farnsworth 
1649e7d841caSChris Wilson 	if (work == NULL ||
1650e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1651e7d841caSChris Wilson 	    !work->enable_stall_check) {
16524e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
16534e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
16544e5359cdSSimon Farnsworth 		return;
16554e5359cdSSimon Farnsworth 	}
16564e5359cdSSimon Farnsworth 
16574e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
165805394f39SChris Wilson 	obj = work->pending_flip_obj;
1659a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
16609db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1661446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1662f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
16634e5359cdSSimon Farnsworth 	} else {
16649db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
1665f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
166601f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
16674e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
16684e5359cdSSimon Farnsworth 	}
16694e5359cdSSimon Farnsworth 
16704e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
16714e5359cdSSimon Farnsworth 
16724e5359cdSSimon Farnsworth 	if (stall_detected) {
16734e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
16744e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
16754e5359cdSSimon Farnsworth 	}
16764e5359cdSSimon Farnsworth }
16774e5359cdSSimon Farnsworth 
167842f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
167942f52ef8SKeith Packard  * we use as a pipe index
168042f52ef8SKeith Packard  */
1681f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
16820a3e67a4SJesse Barnes {
16830a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1684e9d21d7fSKeith Packard 	unsigned long irqflags;
168571e0ffa5SJesse Barnes 
16865eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
168771e0ffa5SJesse Barnes 		return -EINVAL;
16880a3e67a4SJesse Barnes 
16891ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1690f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
16917c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
16927c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
16930a3e67a4SJesse Barnes 	else
16947c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
16957c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
16968692d00eSChris Wilson 
16978692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
16988692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
16996b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
17001ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17018692d00eSChris Wilson 
17020a3e67a4SJesse Barnes 	return 0;
17030a3e67a4SJesse Barnes }
17040a3e67a4SJesse Barnes 
1705f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1706f796cf8fSJesse Barnes {
1707f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1708f796cf8fSJesse Barnes 	unsigned long irqflags;
1709f796cf8fSJesse Barnes 
1710f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1711f796cf8fSJesse Barnes 		return -EINVAL;
1712f796cf8fSJesse Barnes 
1713f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1714f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1715f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1716f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1717f796cf8fSJesse Barnes 
1718f796cf8fSJesse Barnes 	return 0;
1719f796cf8fSJesse Barnes }
1720f796cf8fSJesse Barnes 
1721f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1722b1f14ad0SJesse Barnes {
1723b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1724b1f14ad0SJesse Barnes 	unsigned long irqflags;
1725b1f14ad0SJesse Barnes 
1726b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1727b1f14ad0SJesse Barnes 		return -EINVAL;
1728b1f14ad0SJesse Barnes 
1729b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1730b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
1731b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
1732b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1733b1f14ad0SJesse Barnes 
1734b1f14ad0SJesse Barnes 	return 0;
1735b1f14ad0SJesse Barnes }
1736b1f14ad0SJesse Barnes 
17377e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
17387e231dbeSJesse Barnes {
17397e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17407e231dbeSJesse Barnes 	unsigned long irqflags;
174131acc7f5SJesse Barnes 	u32 imr;
17427e231dbeSJesse Barnes 
17437e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
17447e231dbeSJesse Barnes 		return -EINVAL;
17457e231dbeSJesse Barnes 
17467e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17477e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
174831acc7f5SJesse Barnes 	if (pipe == 0)
17497e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
175031acc7f5SJesse Barnes 	else
17517e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
17527e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
175331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
175431acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
17557e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17567e231dbeSJesse Barnes 
17577e231dbeSJesse Barnes 	return 0;
17587e231dbeSJesse Barnes }
17597e231dbeSJesse Barnes 
176042f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
176142f52ef8SKeith Packard  * we use as a pipe index
176242f52ef8SKeith Packard  */
1763f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
17640a3e67a4SJesse Barnes {
17650a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1766e9d21d7fSKeith Packard 	unsigned long irqflags;
17670a3e67a4SJesse Barnes 
17681ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17698692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
17706b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
17718692d00eSChris Wilson 
17727c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
17737c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
17747c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
17751ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17760a3e67a4SJesse Barnes }
17770a3e67a4SJesse Barnes 
1778f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1779f796cf8fSJesse Barnes {
1780f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1781f796cf8fSJesse Barnes 	unsigned long irqflags;
1782f796cf8fSJesse Barnes 
1783f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1784f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1785f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1786f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1787f796cf8fSJesse Barnes }
1788f796cf8fSJesse Barnes 
1789f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1790b1f14ad0SJesse Barnes {
1791b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1792b1f14ad0SJesse Barnes 	unsigned long irqflags;
1793b1f14ad0SJesse Barnes 
1794b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1795b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
1796b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
1797b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1798b1f14ad0SJesse Barnes }
1799b1f14ad0SJesse Barnes 
18007e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
18017e231dbeSJesse Barnes {
18027e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18037e231dbeSJesse Barnes 	unsigned long irqflags;
180431acc7f5SJesse Barnes 	u32 imr;
18057e231dbeSJesse Barnes 
18067e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
180731acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
180831acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
18097e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
181031acc7f5SJesse Barnes 	if (pipe == 0)
18117e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
181231acc7f5SJesse Barnes 	else
18137e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
18147e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
18157e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
18167e231dbeSJesse Barnes }
18177e231dbeSJesse Barnes 
1818893eead0SChris Wilson static u32
1819893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1820852835f3SZou Nan hai {
1821893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1822893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1823893eead0SChris Wilson }
1824893eead0SChris Wilson 
18259107e9d2SChris Wilson static bool
18269107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
1827893eead0SChris Wilson {
18289107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
18299107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
1830f65d9421SBen Gamari }
1831f65d9421SBen Gamari 
18326274f212SChris Wilson static struct intel_ring_buffer *
18336274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
1834a24a11e6SChris Wilson {
1835a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
18366274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
1837a24a11e6SChris Wilson 
1838a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
1839a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
1840a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
18416274f212SChris Wilson 		return NULL;
1842a24a11e6SChris Wilson 
1843a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
1844a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
1845a24a11e6SChris Wilson 	 */
18466274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
1847a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
1848a24a11e6SChris Wilson 	do {
1849a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
1850a24a11e6SChris Wilson 		if (cmd == ipehr)
1851a24a11e6SChris Wilson 			break;
1852a24a11e6SChris Wilson 
1853a24a11e6SChris Wilson 		acthd -= 4;
1854a24a11e6SChris Wilson 		if (acthd < acthd_min)
18556274f212SChris Wilson 			return NULL;
1856a24a11e6SChris Wilson 	} while (1);
1857a24a11e6SChris Wilson 
18586274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
18596274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
1860a24a11e6SChris Wilson }
1861a24a11e6SChris Wilson 
18626274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
18636274f212SChris Wilson {
18646274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
18656274f212SChris Wilson 	struct intel_ring_buffer *signaller;
18666274f212SChris Wilson 	u32 seqno, ctl;
18676274f212SChris Wilson 
18686274f212SChris Wilson 	ring->hangcheck.deadlock = true;
18696274f212SChris Wilson 
18706274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
18716274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
18726274f212SChris Wilson 		return -1;
18736274f212SChris Wilson 
18746274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
18756274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
18766274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
18776274f212SChris Wilson 		return -1;
18786274f212SChris Wilson 
18796274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
18806274f212SChris Wilson }
18816274f212SChris Wilson 
18826274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
18836274f212SChris Wilson {
18846274f212SChris Wilson 	struct intel_ring_buffer *ring;
18856274f212SChris Wilson 	int i;
18866274f212SChris Wilson 
18876274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
18886274f212SChris Wilson 		ring->hangcheck.deadlock = false;
18896274f212SChris Wilson }
18906274f212SChris Wilson 
1891ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
1892ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
18931ec14ad3SChris Wilson {
18941ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
18951ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
18969107e9d2SChris Wilson 	u32 tmp;
18979107e9d2SChris Wilson 
18986274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
18996274f212SChris Wilson 		return active;
19006274f212SChris Wilson 
19019107e9d2SChris Wilson 	if (IS_GEN2(dev))
19026274f212SChris Wilson 		return hung;
19039107e9d2SChris Wilson 
19049107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
19059107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
19069107e9d2SChris Wilson 	 * and break the hang. This should work on
19079107e9d2SChris Wilson 	 * all but the second generation chipsets.
19089107e9d2SChris Wilson 	 */
19099107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
19101ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
19111ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
19121ec14ad3SChris Wilson 			  ring->name);
19131ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
19146274f212SChris Wilson 		return kick;
19151ec14ad3SChris Wilson 	}
1916a24a11e6SChris Wilson 
19176274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
19186274f212SChris Wilson 		switch (semaphore_passed(ring)) {
19196274f212SChris Wilson 		default:
19206274f212SChris Wilson 			return hung;
19216274f212SChris Wilson 		case 1:
1922a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
1923a24a11e6SChris Wilson 				  ring->name);
1924a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
19256274f212SChris Wilson 			return kick;
19266274f212SChris Wilson 		case 0:
19276274f212SChris Wilson 			return wait;
19286274f212SChris Wilson 		}
19299107e9d2SChris Wilson 	}
19309107e9d2SChris Wilson 
19316274f212SChris Wilson 	return hung;
1932a24a11e6SChris Wilson }
1933d1e61e7fSChris Wilson 
1934f65d9421SBen Gamari /**
1935f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
193605407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
193705407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
193805407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
193905407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
194005407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
1941f65d9421SBen Gamari  */
1942f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1943f65d9421SBen Gamari {
1944f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1945f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1946b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1947b4519513SChris Wilson 	int i;
194805407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
19499107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
19509107e9d2SChris Wilson #define BUSY 1
19519107e9d2SChris Wilson #define KICK 5
19529107e9d2SChris Wilson #define HUNG 20
19539107e9d2SChris Wilson #define FIRE 30
1954893eead0SChris Wilson 
19553e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
19563e0dc6b0SBen Widawsky 		return;
19573e0dc6b0SBen Widawsky 
1958b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
195905407ff8SMika Kuoppala 		u32 seqno, acthd;
19609107e9d2SChris Wilson 		bool busy = true;
1961b4519513SChris Wilson 
19626274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
19636274f212SChris Wilson 
196405407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
196505407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
196605407ff8SMika Kuoppala 
196705407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
19689107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
19699107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
19709107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
19719107e9d2SChris Wilson 					DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
19729107e9d2SChris Wilson 						  ring->name);
19739107e9d2SChris Wilson 					wake_up_all(&ring->irq_queue);
19749107e9d2SChris Wilson 					ring->hangcheck.score += HUNG;
19759107e9d2SChris Wilson 				} else
19769107e9d2SChris Wilson 					busy = false;
197705407ff8SMika Kuoppala 			} else {
19789107e9d2SChris Wilson 				int score;
19799107e9d2SChris Wilson 
19806274f212SChris Wilson 				/* We always increment the hangcheck score
19816274f212SChris Wilson 				 * if the ring is busy and still processing
19826274f212SChris Wilson 				 * the same request, so that no single request
19836274f212SChris Wilson 				 * can run indefinitely (such as a chain of
19846274f212SChris Wilson 				 * batches). The only time we do not increment
19856274f212SChris Wilson 				 * the hangcheck score on this ring, if this
19866274f212SChris Wilson 				 * ring is in a legitimate wait for another
19876274f212SChris Wilson 				 * ring. In that case the waiting ring is a
19886274f212SChris Wilson 				 * victim and we want to be sure we catch the
19896274f212SChris Wilson 				 * right culprit. Then every time we do kick
19906274f212SChris Wilson 				 * the ring, add a small increment to the
19916274f212SChris Wilson 				 * score so that we can catch a batch that is
19926274f212SChris Wilson 				 * being repeatedly kicked and so responsible
19936274f212SChris Wilson 				 * for stalling the machine.
19949107e9d2SChris Wilson 				 */
1995ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
1996ad8beaeaSMika Kuoppala 								    acthd);
1997ad8beaeaSMika Kuoppala 
1998ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
19996274f212SChris Wilson 				case wait:
20006274f212SChris Wilson 					score = 0;
20016274f212SChris Wilson 					break;
20026274f212SChris Wilson 				case active:
20039107e9d2SChris Wilson 					score = BUSY;
20046274f212SChris Wilson 					break;
20056274f212SChris Wilson 				case kick:
20066274f212SChris Wilson 					score = KICK;
20076274f212SChris Wilson 					break;
20086274f212SChris Wilson 				case hung:
20096274f212SChris Wilson 					score = HUNG;
20106274f212SChris Wilson 					stuck[i] = true;
20116274f212SChris Wilson 					break;
20126274f212SChris Wilson 				}
20139107e9d2SChris Wilson 				ring->hangcheck.score += score;
201405407ff8SMika Kuoppala 			}
20159107e9d2SChris Wilson 		} else {
20169107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
20179107e9d2SChris Wilson 			 * attempts across multiple batches.
20189107e9d2SChris Wilson 			 */
20199107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
20209107e9d2SChris Wilson 				ring->hangcheck.score--;
2021cbb465e7SChris Wilson 		}
2022f65d9421SBen Gamari 
202305407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
202405407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
20259107e9d2SChris Wilson 		busy_count += busy;
202605407ff8SMika Kuoppala 	}
202705407ff8SMika Kuoppala 
202805407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
20299107e9d2SChris Wilson 		if (ring->hangcheck.score > FIRE) {
2030acd78c11SBen Widawsky 			DRM_ERROR("%s on %s\n",
203105407ff8SMika Kuoppala 				  stuck[i] ? "stuck" : "no progress",
2032a43adf07SChris Wilson 				  ring->name);
2033a43adf07SChris Wilson 			rings_hung++;
203405407ff8SMika Kuoppala 		}
203505407ff8SMika Kuoppala 	}
203605407ff8SMika Kuoppala 
203705407ff8SMika Kuoppala 	if (rings_hung)
203805407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
203905407ff8SMika Kuoppala 
204005407ff8SMika Kuoppala 	if (busy_count)
204105407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
204205407ff8SMika Kuoppala 		 * being added */
204310cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
204410cd45b6SMika Kuoppala }
204510cd45b6SMika Kuoppala 
204610cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
204710cd45b6SMika Kuoppala {
204810cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
204910cd45b6SMika Kuoppala 	if (!i915_enable_hangcheck)
205010cd45b6SMika Kuoppala 		return;
205110cd45b6SMika Kuoppala 
205299584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
205310cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2054f65d9421SBen Gamari }
2055f65d9421SBen Gamari 
205691738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
205791738a95SPaulo Zanoni {
205891738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
205991738a95SPaulo Zanoni 
206091738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
206191738a95SPaulo Zanoni 		return;
206291738a95SPaulo Zanoni 
206391738a95SPaulo Zanoni 	/* south display irq */
206491738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
206591738a95SPaulo Zanoni 	/*
206691738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
206791738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
206891738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
206991738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
207091738a95SPaulo Zanoni 	 */
207191738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
207291738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
207391738a95SPaulo Zanoni }
207491738a95SPaulo Zanoni 
2075d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2076d18ea1b5SDaniel Vetter {
2077d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2078d18ea1b5SDaniel Vetter 
2079d18ea1b5SDaniel Vetter 	/* and GT */
2080d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2081d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2082d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2083d18ea1b5SDaniel Vetter 
2084d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2085d18ea1b5SDaniel Vetter 		/* and PM */
2086d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2087d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2088d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2089d18ea1b5SDaniel Vetter 	}
2090d18ea1b5SDaniel Vetter }
2091d18ea1b5SDaniel Vetter 
2092c0e09200SDave Airlie /* drm_dma.h hooks
2093c0e09200SDave Airlie */
2094f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2095036a4a7dSZhenyu Wang {
2096036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2097036a4a7dSZhenyu Wang 
20984697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
20994697995bSJesse Barnes 
2100036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2101bdfcdb63SDaniel Vetter 
2102036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2103036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
21043143a2bfSChris Wilson 	POSTING_READ(DEIER);
2105036a4a7dSZhenyu Wang 
2106d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2107c650156aSZhenyu Wang 
210891738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
21097d99163dSBen Widawsky }
21107d99163dSBen Widawsky 
21117e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
21127e231dbeSJesse Barnes {
21137e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21147e231dbeSJesse Barnes 	int pipe;
21157e231dbeSJesse Barnes 
21167e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
21177e231dbeSJesse Barnes 
21187e231dbeSJesse Barnes 	/* VLV magic */
21197e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
21207e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
21217e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
21227e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
21237e231dbeSJesse Barnes 
21247e231dbeSJesse Barnes 	/* and GT */
21257e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
21267e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2127d18ea1b5SDaniel Vetter 
2128d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
21297e231dbeSJesse Barnes 
21307e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
21317e231dbeSJesse Barnes 
21327e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
21337e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
21347e231dbeSJesse Barnes 	for_each_pipe(pipe)
21357e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
21367e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
21377e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
21387e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
21397e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
21407e231dbeSJesse Barnes }
21417e231dbeSJesse Barnes 
214282a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
214382a28bcfSDaniel Vetter {
214482a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
214582a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
214682a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2147fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
214882a28bcfSDaniel Vetter 
214982a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2150fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
215182a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2152cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2153fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
215482a28bcfSDaniel Vetter 	} else {
2155fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
215682a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2157cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2158fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
215982a28bcfSDaniel Vetter 	}
216082a28bcfSDaniel Vetter 
2161fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
216282a28bcfSDaniel Vetter 
21637fe0b973SKeith Packard 	/*
21647fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
21657fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
21667fe0b973SKeith Packard 	 *
21677fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
21687fe0b973SKeith Packard 	 */
21697fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
21707fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
21717fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
21727fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
21737fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
21747fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
21757fe0b973SKeith Packard }
21767fe0b973SKeith Packard 
2177d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2178d46da437SPaulo Zanoni {
2179d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
218082a28bcfSDaniel Vetter 	u32 mask;
2181d46da437SPaulo Zanoni 
2182692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2183692a04cfSDaniel Vetter 		return;
2184692a04cfSDaniel Vetter 
21858664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
21868664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2187de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
21888664281bSPaulo Zanoni 	} else {
21898664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
21908664281bSPaulo Zanoni 
21918664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
21928664281bSPaulo Zanoni 	}
2193ab5c608bSBen Widawsky 
2194d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2195d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2196d46da437SPaulo Zanoni }
2197d46da437SPaulo Zanoni 
21980a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
21990a9a8c91SDaniel Vetter {
22000a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
22010a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
22020a9a8c91SDaniel Vetter 
22030a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
22040a9a8c91SDaniel Vetter 
22050a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
22060a9a8c91SDaniel Vetter 	if (HAS_L3_GPU_CACHE(dev)) {
22070a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
22080a9a8c91SDaniel Vetter 		dev_priv->gt_irq_mask = ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
22090a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
22100a9a8c91SDaniel Vetter 	}
22110a9a8c91SDaniel Vetter 
22120a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
22130a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
22140a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
22150a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
22160a9a8c91SDaniel Vetter 	} else {
22170a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
22180a9a8c91SDaniel Vetter 	}
22190a9a8c91SDaniel Vetter 
22200a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
22210a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
22220a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
22230a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
22240a9a8c91SDaniel Vetter 
22250a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
22260a9a8c91SDaniel Vetter 		pm_irqs |= GEN6_PM_RPS_EVENTS;
22270a9a8c91SDaniel Vetter 
22280a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
22290a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
22300a9a8c91SDaniel Vetter 
22310a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
22320a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
22330a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
22340a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
22350a9a8c91SDaniel Vetter 	}
22360a9a8c91SDaniel Vetter }
22370a9a8c91SDaniel Vetter 
2238f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2239036a4a7dSZhenyu Wang {
22404bc9d430SDaniel Vetter 	unsigned long irqflags;
22414bc9d430SDaniel Vetter 
2242036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2243036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
2244013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2245ce99c256SDaniel Vetter 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
22468664281bSPaulo Zanoni 			   DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
2247de032bf4SPaulo Zanoni 			   DE_PIPEA_FIFO_UNDERRUN | DE_POISON;
2248036a4a7dSZhenyu Wang 
22491ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2250036a4a7dSZhenyu Wang 
2251036a4a7dSZhenyu Wang 	/* should always can generate irq */
2252036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
22531ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
22546005ce42SDaniel Vetter 	I915_WRITE(DEIER, display_mask |
22556005ce42SDaniel Vetter 			  DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT);
22563143a2bfSChris Wilson 	POSTING_READ(DEIER);
2257036a4a7dSZhenyu Wang 
22580a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
2259036a4a7dSZhenyu Wang 
2260d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
22617fe0b973SKeith Packard 
2262f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
22636005ce42SDaniel Vetter 		/* Enable PCU event interrupts
22646005ce42SDaniel Vetter 		 *
22656005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
22664bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
22674bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
22684bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2269f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
22704bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2271f97108d1SJesse Barnes 	}
2272f97108d1SJesse Barnes 
2273036a4a7dSZhenyu Wang 	return 0;
2274036a4a7dSZhenyu Wang }
2275036a4a7dSZhenyu Wang 
2276f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2277b1f14ad0SJesse Barnes {
2278b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2279b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2280b615b57aSChris Wilson 	u32 display_mask =
2281b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2282b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
2283b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
2284ce99c256SDaniel Vetter 		DE_PLANEA_FLIP_DONE_IVB |
22858664281bSPaulo Zanoni 		DE_AUX_CHANNEL_A_IVB |
22868664281bSPaulo Zanoni 		DE_ERR_INT_IVB;
2287b1f14ad0SJesse Barnes 
2288b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2289b1f14ad0SJesse Barnes 
2290b1f14ad0SJesse Barnes 	/* should always can generate irq */
22918664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2292b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2293b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2294b615b57aSChris Wilson 	I915_WRITE(DEIER,
2295b615b57aSChris Wilson 		   display_mask |
2296b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
2297b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
2298b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
2299b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2300b1f14ad0SJesse Barnes 
23010a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
2302eda63ffbSBen Widawsky 
2303d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
23047fe0b973SKeith Packard 
2305b1f14ad0SJesse Barnes 	return 0;
2306b1f14ad0SJesse Barnes }
2307b1f14ad0SJesse Barnes 
23087e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
23097e231dbeSJesse Barnes {
23107e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23117e231dbeSJesse Barnes 	u32 enable_mask;
231231acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
2313b79480baSDaniel Vetter 	unsigned long irqflags;
23147e231dbeSJesse Barnes 
23157e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
231631acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
231731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
231831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
23197e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23207e231dbeSJesse Barnes 
232131acc7f5SJesse Barnes 	/*
232231acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
232331acc7f5SJesse Barnes 	 * toggle them based on usage.
232431acc7f5SJesse Barnes 	 */
232531acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
232631acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
232731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23287e231dbeSJesse Barnes 
232920afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
233020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
233120afbda2SDaniel Vetter 
23327e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
23337e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
23347e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
23357e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
23367e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
23377e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
23387e231dbeSJesse Barnes 
2339b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2340b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2341b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
234231acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2343515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
234431acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
2345b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
234631acc7f5SJesse Barnes 
23477e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
23487e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
23497e231dbeSJesse Barnes 
23500a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
23517e231dbeSJesse Barnes 
23527e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
23537e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
23547e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
23557e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
23567e231dbeSJesse Barnes #endif
23577e231dbeSJesse Barnes 
23587e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
235920afbda2SDaniel Vetter 
236020afbda2SDaniel Vetter 	return 0;
236120afbda2SDaniel Vetter }
236220afbda2SDaniel Vetter 
23637e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
23647e231dbeSJesse Barnes {
23657e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23667e231dbeSJesse Barnes 	int pipe;
23677e231dbeSJesse Barnes 
23687e231dbeSJesse Barnes 	if (!dev_priv)
23697e231dbeSJesse Barnes 		return;
23707e231dbeSJesse Barnes 
2371ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2372ac4c16c5SEgbert Eich 
23737e231dbeSJesse Barnes 	for_each_pipe(pipe)
23747e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
23757e231dbeSJesse Barnes 
23767e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
23777e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
23787e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
23797e231dbeSJesse Barnes 	for_each_pipe(pipe)
23807e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
23817e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
23827e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
23837e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
23847e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
23857e231dbeSJesse Barnes }
23867e231dbeSJesse Barnes 
2387f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2388036a4a7dSZhenyu Wang {
2389036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23904697995bSJesse Barnes 
23914697995bSJesse Barnes 	if (!dev_priv)
23924697995bSJesse Barnes 		return;
23934697995bSJesse Barnes 
2394ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2395ac4c16c5SEgbert Eich 
2396036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2397036a4a7dSZhenyu Wang 
2398036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2399036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2400036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
24018664281bSPaulo Zanoni 	if (IS_GEN7(dev))
24028664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2403036a4a7dSZhenyu Wang 
2404036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2405036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2406036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2407192aac1fSKeith Packard 
2408ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2409ab5c608bSBen Widawsky 		return;
2410ab5c608bSBen Widawsky 
2411192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2412192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2413192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
24148664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
24158664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2416036a4a7dSZhenyu Wang }
2417036a4a7dSZhenyu Wang 
2418c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2419c2798b19SChris Wilson {
2420c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2421c2798b19SChris Wilson 	int pipe;
2422c2798b19SChris Wilson 
2423c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2424c2798b19SChris Wilson 
2425c2798b19SChris Wilson 	for_each_pipe(pipe)
2426c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2427c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2428c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2429c2798b19SChris Wilson 	POSTING_READ16(IER);
2430c2798b19SChris Wilson }
2431c2798b19SChris Wilson 
2432c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2433c2798b19SChris Wilson {
2434c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2435c2798b19SChris Wilson 
2436c2798b19SChris Wilson 	I915_WRITE16(EMR,
2437c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2438c2798b19SChris Wilson 
2439c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2440c2798b19SChris Wilson 	dev_priv->irq_mask =
2441c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2442c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2443c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2444c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2445c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2446c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2447c2798b19SChris Wilson 
2448c2798b19SChris Wilson 	I915_WRITE16(IER,
2449c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2450c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2451c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2452c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2453c2798b19SChris Wilson 	POSTING_READ16(IER);
2454c2798b19SChris Wilson 
2455c2798b19SChris Wilson 	return 0;
2456c2798b19SChris Wilson }
2457c2798b19SChris Wilson 
245890a72f87SVille Syrjälä /*
245990a72f87SVille Syrjälä  * Returns true when a page flip has completed.
246090a72f87SVille Syrjälä  */
246190a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
246290a72f87SVille Syrjälä 			       int pipe, u16 iir)
246390a72f87SVille Syrjälä {
246490a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
246590a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
246690a72f87SVille Syrjälä 
246790a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
246890a72f87SVille Syrjälä 		return false;
246990a72f87SVille Syrjälä 
247090a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
247190a72f87SVille Syrjälä 		return false;
247290a72f87SVille Syrjälä 
247390a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
247490a72f87SVille Syrjälä 
247590a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
247690a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
247790a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
247890a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
247990a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
248090a72f87SVille Syrjälä 	 */
248190a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
248290a72f87SVille Syrjälä 		return false;
248390a72f87SVille Syrjälä 
248490a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
248590a72f87SVille Syrjälä 
248690a72f87SVille Syrjälä 	return true;
248790a72f87SVille Syrjälä }
248890a72f87SVille Syrjälä 
2489ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2490c2798b19SChris Wilson {
2491c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2492c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2493c2798b19SChris Wilson 	u16 iir, new_iir;
2494c2798b19SChris Wilson 	u32 pipe_stats[2];
2495c2798b19SChris Wilson 	unsigned long irqflags;
2496c2798b19SChris Wilson 	int irq_received;
2497c2798b19SChris Wilson 	int pipe;
2498c2798b19SChris Wilson 	u16 flip_mask =
2499c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2500c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2501c2798b19SChris Wilson 
2502c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2503c2798b19SChris Wilson 
2504c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2505c2798b19SChris Wilson 	if (iir == 0)
2506c2798b19SChris Wilson 		return IRQ_NONE;
2507c2798b19SChris Wilson 
2508c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2509c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2510c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2511c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2512c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2513c2798b19SChris Wilson 		 */
2514c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2515c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2516c2798b19SChris Wilson 			i915_handle_error(dev, false);
2517c2798b19SChris Wilson 
2518c2798b19SChris Wilson 		for_each_pipe(pipe) {
2519c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2520c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2521c2798b19SChris Wilson 
2522c2798b19SChris Wilson 			/*
2523c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2524c2798b19SChris Wilson 			 */
2525c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2526c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2527c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2528c2798b19SChris Wilson 							 pipe_name(pipe));
2529c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2530c2798b19SChris Wilson 				irq_received = 1;
2531c2798b19SChris Wilson 			}
2532c2798b19SChris Wilson 		}
2533c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2534c2798b19SChris Wilson 
2535c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2536c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2537c2798b19SChris Wilson 
2538d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2539c2798b19SChris Wilson 
2540c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2541c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2542c2798b19SChris Wilson 
2543c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
254490a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
254590a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2546c2798b19SChris Wilson 
2547c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
254890a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
254990a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2550c2798b19SChris Wilson 
2551c2798b19SChris Wilson 		iir = new_iir;
2552c2798b19SChris Wilson 	}
2553c2798b19SChris Wilson 
2554c2798b19SChris Wilson 	return IRQ_HANDLED;
2555c2798b19SChris Wilson }
2556c2798b19SChris Wilson 
2557c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2558c2798b19SChris Wilson {
2559c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2560c2798b19SChris Wilson 	int pipe;
2561c2798b19SChris Wilson 
2562c2798b19SChris Wilson 	for_each_pipe(pipe) {
2563c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2564c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2565c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2566c2798b19SChris Wilson 	}
2567c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2568c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2569c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2570c2798b19SChris Wilson }
2571c2798b19SChris Wilson 
2572a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2573a266c7d5SChris Wilson {
2574a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2575a266c7d5SChris Wilson 	int pipe;
2576a266c7d5SChris Wilson 
2577a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2578a266c7d5SChris Wilson 
2579a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2580a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2581a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2582a266c7d5SChris Wilson 	}
2583a266c7d5SChris Wilson 
258400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2585a266c7d5SChris Wilson 	for_each_pipe(pipe)
2586a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2587a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2588a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2589a266c7d5SChris Wilson 	POSTING_READ(IER);
2590a266c7d5SChris Wilson }
2591a266c7d5SChris Wilson 
2592a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2593a266c7d5SChris Wilson {
2594a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
259538bde180SChris Wilson 	u32 enable_mask;
2596a266c7d5SChris Wilson 
259738bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
259838bde180SChris Wilson 
259938bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
260038bde180SChris Wilson 	dev_priv->irq_mask =
260138bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
260238bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
260338bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
260438bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
260538bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
260638bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
260738bde180SChris Wilson 
260838bde180SChris Wilson 	enable_mask =
260938bde180SChris Wilson 		I915_ASLE_INTERRUPT |
261038bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
261138bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
261238bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
261338bde180SChris Wilson 		I915_USER_INTERRUPT;
261438bde180SChris Wilson 
2615a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
261620afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
261720afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
261820afbda2SDaniel Vetter 
2619a266c7d5SChris Wilson 		/* Enable in IER... */
2620a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2621a266c7d5SChris Wilson 		/* and unmask in IMR */
2622a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2623a266c7d5SChris Wilson 	}
2624a266c7d5SChris Wilson 
2625a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2626a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2627a266c7d5SChris Wilson 	POSTING_READ(IER);
2628a266c7d5SChris Wilson 
2629f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
263020afbda2SDaniel Vetter 
263120afbda2SDaniel Vetter 	return 0;
263220afbda2SDaniel Vetter }
263320afbda2SDaniel Vetter 
263490a72f87SVille Syrjälä /*
263590a72f87SVille Syrjälä  * Returns true when a page flip has completed.
263690a72f87SVille Syrjälä  */
263790a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
263890a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
263990a72f87SVille Syrjälä {
264090a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
264190a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
264290a72f87SVille Syrjälä 
264390a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
264490a72f87SVille Syrjälä 		return false;
264590a72f87SVille Syrjälä 
264690a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
264790a72f87SVille Syrjälä 		return false;
264890a72f87SVille Syrjälä 
264990a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
265090a72f87SVille Syrjälä 
265190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
265290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
265390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
265490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
265590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
265690a72f87SVille Syrjälä 	 */
265790a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
265890a72f87SVille Syrjälä 		return false;
265990a72f87SVille Syrjälä 
266090a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
266190a72f87SVille Syrjälä 
266290a72f87SVille Syrjälä 	return true;
266390a72f87SVille Syrjälä }
266490a72f87SVille Syrjälä 
2665ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2666a266c7d5SChris Wilson {
2667a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2668a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
26698291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2670a266c7d5SChris Wilson 	unsigned long irqflags;
267138bde180SChris Wilson 	u32 flip_mask =
267238bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
267338bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
267438bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2675a266c7d5SChris Wilson 
2676a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2677a266c7d5SChris Wilson 
2678a266c7d5SChris Wilson 	iir = I915_READ(IIR);
267938bde180SChris Wilson 	do {
268038bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
26818291ee90SChris Wilson 		bool blc_event = false;
2682a266c7d5SChris Wilson 
2683a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2684a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2685a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2686a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2687a266c7d5SChris Wilson 		 */
2688a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2689a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2690a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2691a266c7d5SChris Wilson 
2692a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2693a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2694a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2695a266c7d5SChris Wilson 
269638bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2697a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2698a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2699a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2700a266c7d5SChris Wilson 							 pipe_name(pipe));
2701a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
270238bde180SChris Wilson 				irq_received = true;
2703a266c7d5SChris Wilson 			}
2704a266c7d5SChris Wilson 		}
2705a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2706a266c7d5SChris Wilson 
2707a266c7d5SChris Wilson 		if (!irq_received)
2708a266c7d5SChris Wilson 			break;
2709a266c7d5SChris Wilson 
2710a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2711a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2712a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2713a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2714b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
2715a266c7d5SChris Wilson 
2716a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2717a266c7d5SChris Wilson 				  hotplug_status);
271891d131d2SDaniel Vetter 
271910a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
272091d131d2SDaniel Vetter 
2721a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
272238bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2723a266c7d5SChris Wilson 		}
2724a266c7d5SChris Wilson 
272538bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2726a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2727a266c7d5SChris Wilson 
2728a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2729a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2730a266c7d5SChris Wilson 
2731a266c7d5SChris Wilson 		for_each_pipe(pipe) {
273238bde180SChris Wilson 			int plane = pipe;
273338bde180SChris Wilson 			if (IS_MOBILE(dev))
273438bde180SChris Wilson 				plane = !plane;
27355e2032d4SVille Syrjälä 
273690a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
273790a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
273890a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
2739a266c7d5SChris Wilson 
2740a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2741a266c7d5SChris Wilson 				blc_event = true;
2742a266c7d5SChris Wilson 		}
2743a266c7d5SChris Wilson 
2744a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2745a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2746a266c7d5SChris Wilson 
2747a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2748a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2749a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2750a266c7d5SChris Wilson 		 * we would never get another interrupt.
2751a266c7d5SChris Wilson 		 *
2752a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2753a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2754a266c7d5SChris Wilson 		 * another one.
2755a266c7d5SChris Wilson 		 *
2756a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2757a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2758a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2759a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2760a266c7d5SChris Wilson 		 * stray interrupts.
2761a266c7d5SChris Wilson 		 */
276238bde180SChris Wilson 		ret = IRQ_HANDLED;
2763a266c7d5SChris Wilson 		iir = new_iir;
276438bde180SChris Wilson 	} while (iir & ~flip_mask);
2765a266c7d5SChris Wilson 
2766d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
27678291ee90SChris Wilson 
2768a266c7d5SChris Wilson 	return ret;
2769a266c7d5SChris Wilson }
2770a266c7d5SChris Wilson 
2771a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2772a266c7d5SChris Wilson {
2773a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2774a266c7d5SChris Wilson 	int pipe;
2775a266c7d5SChris Wilson 
2776ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
2777ac4c16c5SEgbert Eich 
2778a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2779a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2780a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2781a266c7d5SChris Wilson 	}
2782a266c7d5SChris Wilson 
278300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
278455b39755SChris Wilson 	for_each_pipe(pipe) {
278555b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2786a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
278755b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
278855b39755SChris Wilson 	}
2789a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2790a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2791a266c7d5SChris Wilson 
2792a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2793a266c7d5SChris Wilson }
2794a266c7d5SChris Wilson 
2795a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2796a266c7d5SChris Wilson {
2797a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2798a266c7d5SChris Wilson 	int pipe;
2799a266c7d5SChris Wilson 
2800a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2801a266c7d5SChris Wilson 
2802a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2803a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2804a266c7d5SChris Wilson 
2805a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2806a266c7d5SChris Wilson 	for_each_pipe(pipe)
2807a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2808a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2809a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2810a266c7d5SChris Wilson 	POSTING_READ(IER);
2811a266c7d5SChris Wilson }
2812a266c7d5SChris Wilson 
2813a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2814a266c7d5SChris Wilson {
2815a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2816bbba0a97SChris Wilson 	u32 enable_mask;
2817a266c7d5SChris Wilson 	u32 error_mask;
2818b79480baSDaniel Vetter 	unsigned long irqflags;
2819a266c7d5SChris Wilson 
2820a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2821bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2822adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2823bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2824bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2825bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2826bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2827bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2828bbba0a97SChris Wilson 
2829bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
283021ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
283121ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
2832bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2833bbba0a97SChris Wilson 
2834bbba0a97SChris Wilson 	if (IS_G4X(dev))
2835bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2836a266c7d5SChris Wilson 
2837b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2838b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2839b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2840515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
2841b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2842a266c7d5SChris Wilson 
2843a266c7d5SChris Wilson 	/*
2844a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2845a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2846a266c7d5SChris Wilson 	 */
2847a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2848a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2849a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2850a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2851a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2852a266c7d5SChris Wilson 	} else {
2853a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2854a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2855a266c7d5SChris Wilson 	}
2856a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2857a266c7d5SChris Wilson 
2858a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2859a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2860a266c7d5SChris Wilson 	POSTING_READ(IER);
2861a266c7d5SChris Wilson 
286220afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
286320afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
286420afbda2SDaniel Vetter 
2865f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
286620afbda2SDaniel Vetter 
286720afbda2SDaniel Vetter 	return 0;
286820afbda2SDaniel Vetter }
286920afbda2SDaniel Vetter 
2870bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
287120afbda2SDaniel Vetter {
287220afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2873e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
2874cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
287520afbda2SDaniel Vetter 	u32 hotplug_en;
287620afbda2SDaniel Vetter 
2877b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2878b5ea2d56SDaniel Vetter 
2879bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
2880bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2881bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
2882adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
2883e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
2884cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2885cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2886cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
2887a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2888a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2889a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2890a266c7d5SChris Wilson 		*/
2891a266c7d5SChris Wilson 		if (IS_G4X(dev))
2892a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
289385fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
2894a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2895a266c7d5SChris Wilson 
2896a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2897a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2898a266c7d5SChris Wilson 	}
2899bac56d5bSEgbert Eich }
2900a266c7d5SChris Wilson 
2901ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
2902a266c7d5SChris Wilson {
2903a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2904a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2905a266c7d5SChris Wilson 	u32 iir, new_iir;
2906a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2907a266c7d5SChris Wilson 	unsigned long irqflags;
2908a266c7d5SChris Wilson 	int irq_received;
2909a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
291021ad8330SVille Syrjälä 	u32 flip_mask =
291121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
291221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2913a266c7d5SChris Wilson 
2914a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2915a266c7d5SChris Wilson 
2916a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2917a266c7d5SChris Wilson 
2918a266c7d5SChris Wilson 	for (;;) {
29192c8ba29fSChris Wilson 		bool blc_event = false;
29202c8ba29fSChris Wilson 
292121ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
2922a266c7d5SChris Wilson 
2923a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2924a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2925a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2926a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2927a266c7d5SChris Wilson 		 */
2928a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2929a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2930a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2931a266c7d5SChris Wilson 
2932a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2933a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2934a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2935a266c7d5SChris Wilson 
2936a266c7d5SChris Wilson 			/*
2937a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2938a266c7d5SChris Wilson 			 */
2939a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2940a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2941a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2942a266c7d5SChris Wilson 							 pipe_name(pipe));
2943a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2944a266c7d5SChris Wilson 				irq_received = 1;
2945a266c7d5SChris Wilson 			}
2946a266c7d5SChris Wilson 		}
2947a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2948a266c7d5SChris Wilson 
2949a266c7d5SChris Wilson 		if (!irq_received)
2950a266c7d5SChris Wilson 			break;
2951a266c7d5SChris Wilson 
2952a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2953a266c7d5SChris Wilson 
2954a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2955adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2956a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2957b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
2958b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
29594f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
2960a266c7d5SChris Wilson 
2961a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2962a266c7d5SChris Wilson 				  hotplug_status);
296391d131d2SDaniel Vetter 
296410a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
296510a504deSDaniel Vetter 					      IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
296691d131d2SDaniel Vetter 
2967a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2968a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2969a266c7d5SChris Wilson 		}
2970a266c7d5SChris Wilson 
297121ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
2972a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2973a266c7d5SChris Wilson 
2974a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2975a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2976a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2977a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2978a266c7d5SChris Wilson 
2979a266c7d5SChris Wilson 		for_each_pipe(pipe) {
29802c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
298190a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
298290a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
2983a266c7d5SChris Wilson 
2984a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2985a266c7d5SChris Wilson 				blc_event = true;
2986a266c7d5SChris Wilson 		}
2987a266c7d5SChris Wilson 
2988a266c7d5SChris Wilson 
2989a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2990a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2991a266c7d5SChris Wilson 
2992515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
2993515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
2994515ac2bbSDaniel Vetter 
2995a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2996a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2997a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2998a266c7d5SChris Wilson 		 * we would never get another interrupt.
2999a266c7d5SChris Wilson 		 *
3000a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3001a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3002a266c7d5SChris Wilson 		 * another one.
3003a266c7d5SChris Wilson 		 *
3004a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3005a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3006a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3007a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3008a266c7d5SChris Wilson 		 * stray interrupts.
3009a266c7d5SChris Wilson 		 */
3010a266c7d5SChris Wilson 		iir = new_iir;
3011a266c7d5SChris Wilson 	}
3012a266c7d5SChris Wilson 
3013d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
30142c8ba29fSChris Wilson 
3015a266c7d5SChris Wilson 	return ret;
3016a266c7d5SChris Wilson }
3017a266c7d5SChris Wilson 
3018a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3019a266c7d5SChris Wilson {
3020a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3021a266c7d5SChris Wilson 	int pipe;
3022a266c7d5SChris Wilson 
3023a266c7d5SChris Wilson 	if (!dev_priv)
3024a266c7d5SChris Wilson 		return;
3025a266c7d5SChris Wilson 
3026ac4c16c5SEgbert Eich 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
3027ac4c16c5SEgbert Eich 
3028a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3029a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3030a266c7d5SChris Wilson 
3031a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3032a266c7d5SChris Wilson 	for_each_pipe(pipe)
3033a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3034a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3035a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3036a266c7d5SChris Wilson 
3037a266c7d5SChris Wilson 	for_each_pipe(pipe)
3038a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3039a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3040a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3041a266c7d5SChris Wilson }
3042a266c7d5SChris Wilson 
3043ac4c16c5SEgbert Eich static void i915_reenable_hotplug_timer_func(unsigned long data)
3044ac4c16c5SEgbert Eich {
3045ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3046ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3047ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3048ac4c16c5SEgbert Eich 	unsigned long irqflags;
3049ac4c16c5SEgbert Eich 	int i;
3050ac4c16c5SEgbert Eich 
3051ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3052ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3053ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3054ac4c16c5SEgbert Eich 
3055ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3056ac4c16c5SEgbert Eich 			continue;
3057ac4c16c5SEgbert Eich 
3058ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3059ac4c16c5SEgbert Eich 
3060ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3061ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3062ac4c16c5SEgbert Eich 
3063ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3064ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3065ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3066ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3067ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3068ac4c16c5SEgbert Eich 				if (!connector->polled)
3069ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3070ac4c16c5SEgbert Eich 			}
3071ac4c16c5SEgbert Eich 		}
3072ac4c16c5SEgbert Eich 	}
3073ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3074ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3075ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3076ac4c16c5SEgbert Eich }
3077ac4c16c5SEgbert Eich 
3078f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3079f71d4af4SJesse Barnes {
30808b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
30818b2e326dSChris Wilson 
30828b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
308399584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3084c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3085a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
30868b2e326dSChris Wilson 
308799584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
308899584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
308961bac78eSDaniel Vetter 		    (unsigned long) dev);
3090ac4c16c5SEgbert Eich 	setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3091ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
309261bac78eSDaniel Vetter 
309397a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
30949ee32feaSDaniel Vetter 
3095f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
3096f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
30977d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3098f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3099f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3100f71d4af4SJesse Barnes 	}
3101f71d4af4SJesse Barnes 
3102c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3103f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3104c3613de9SKeith Packard 	else
3105c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
3106f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3107f71d4af4SJesse Barnes 
31087e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
31097e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
31107e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
31117e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
31127e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
31137e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
31147e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3115fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
31164a06e201SDaniel Vetter 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
31177d99163dSBen Widawsky 		/* Share uninstall handlers with ILK/SNB */
3118f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
311931694658SPaulo Zanoni 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3120f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
3121f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3122f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
3123f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
312482a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3125f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3126f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3127f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3128f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3129f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3130f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3131f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
313282a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3133f71d4af4SJesse Barnes 	} else {
3134c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3135c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3136c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3137c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3138c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3139a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3140a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3141a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3142a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3143a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
314420afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3145c2798b19SChris Wilson 		} else {
3146a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3147a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3148a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3149a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3150bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3151c2798b19SChris Wilson 		}
3152f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3153f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3154f71d4af4SJesse Barnes 	}
3155f71d4af4SJesse Barnes }
315620afbda2SDaniel Vetter 
315720afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
315820afbda2SDaniel Vetter {
315920afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3160821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3161821450c6SEgbert Eich 	struct drm_connector *connector;
3162b5ea2d56SDaniel Vetter 	unsigned long irqflags;
3163821450c6SEgbert Eich 	int i;
316420afbda2SDaniel Vetter 
3165821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3166821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3167821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3168821450c6SEgbert Eich 	}
3169821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3170821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3171821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3172821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3173821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3174821450c6SEgbert Eich 	}
3175b5ea2d56SDaniel Vetter 
3176b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3177b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
3178b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
317920afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
318020afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
3181b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
318220afbda2SDaniel Vetter }
3183