xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 42b5aeabe9286cdaabfc9b0ce5fa869bbe04dcd9)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33760285e7SDavid Howells #include <drm/drmP.h>
34760285e7SDavid Howells #include <drm/i915_drm.h>
35c0e09200SDave Airlie #include "i915_drv.h"
361c5d22f7SChris Wilson #include "i915_trace.h"
3779e53945SJesse Barnes #include "intel_drv.h"
38c0e09200SDave Airlie 
39e5868a31SEgbert Eich static const u32 hpd_ibx[] = {
40e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
41e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
42e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
43e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
44e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
45e5868a31SEgbert Eich };
46e5868a31SEgbert Eich 
47e5868a31SEgbert Eich static const u32 hpd_cpt[] = {
48e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
4973c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
50e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
51e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
52e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
53e5868a31SEgbert Eich };
54e5868a31SEgbert Eich 
55e5868a31SEgbert Eich static const u32 hpd_mask_i915[] = {
56e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
57e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
58e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
59e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
60e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
61e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
62e5868a31SEgbert Eich };
63e5868a31SEgbert Eich 
64e5868a31SEgbert Eich static const u32 hpd_status_gen4[] = {
65e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
66e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
67e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
68e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
69e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
70e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
71e5868a31SEgbert Eich };
72e5868a31SEgbert Eich 
73e5868a31SEgbert Eich static const u32 hpd_status_i965[] = {
74e5868a31SEgbert Eich 	 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
75e5868a31SEgbert Eich 	 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I965,
76e5868a31SEgbert Eich 	 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I965,
77e5868a31SEgbert Eich 	 [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
78e5868a31SEgbert Eich 	 [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
79e5868a31SEgbert Eich 	 [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
80e5868a31SEgbert Eich };
81e5868a31SEgbert Eich 
82e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
83e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
84e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
85e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
86e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
87e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
88e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
89e5868a31SEgbert Eich };
90e5868a31SEgbert Eich 
91e5868a31SEgbert Eich 
92e5868a31SEgbert Eich 
93036a4a7dSZhenyu Wang /* For display hotplug interrupt */
94995b6762SChris Wilson static void
95f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
96036a4a7dSZhenyu Wang {
971ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
981ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
991ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1003143a2bfSChris Wilson 		POSTING_READ(DEIMR);
101036a4a7dSZhenyu Wang 	}
102036a4a7dSZhenyu Wang }
103036a4a7dSZhenyu Wang 
1040ff9800aSPaulo Zanoni static void
105f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
106036a4a7dSZhenyu Wang {
1071ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1081ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1091ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1103143a2bfSChris Wilson 		POSTING_READ(DEIMR);
111036a4a7dSZhenyu Wang 	}
112036a4a7dSZhenyu Wang }
113036a4a7dSZhenyu Wang 
1147c463586SKeith Packard void
1157c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1167c463586SKeith Packard {
1179db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
11846c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
1197c463586SKeith Packard 
12046c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
12146c06a30SVille Syrjälä 		return;
12246c06a30SVille Syrjälä 
1237c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
12446c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
12546c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
1263143a2bfSChris Wilson 	POSTING_READ(reg);
1277c463586SKeith Packard }
1287c463586SKeith Packard 
1297c463586SKeith Packard void
1307c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1317c463586SKeith Packard {
1329db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
13346c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
1347c463586SKeith Packard 
13546c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
13646c06a30SVille Syrjälä 		return;
13746c06a30SVille Syrjälä 
13846c06a30SVille Syrjälä 	pipestat &= ~mask;
13946c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
1403143a2bfSChris Wilson 	POSTING_READ(reg);
1417c463586SKeith Packard }
1427c463586SKeith Packard 
143c0e09200SDave Airlie /**
14401c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
14501c66889SZhao Yakui  */
14601c66889SZhao Yakui void intel_enable_asle(struct drm_device *dev)
14701c66889SZhao Yakui {
1481ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1491ec14ad3SChris Wilson 	unsigned long irqflags;
1501ec14ad3SChris Wilson 
1517e231dbeSJesse Barnes 	/* FIXME: opregion/asle for VLV */
1527e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev))
1537e231dbeSJesse Barnes 		return;
1547e231dbeSJesse Barnes 
1551ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15601c66889SZhao Yakui 
157c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
158f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
159edcb49caSZhao Yakui 	else {
16001c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
161d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
162a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen >= 4)
163edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
164d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
165edcb49caSZhao Yakui 	}
1661ec14ad3SChris Wilson 
1671ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
16801c66889SZhao Yakui }
16901c66889SZhao Yakui 
17001c66889SZhao Yakui /**
1710a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1720a3e67a4SJesse Barnes  * @dev: DRM device
1730a3e67a4SJesse Barnes  * @pipe: pipe to check
1740a3e67a4SJesse Barnes  *
1750a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1760a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1770a3e67a4SJesse Barnes  * before reading such registers if unsure.
1780a3e67a4SJesse Barnes  */
1790a3e67a4SJesse Barnes static int
1800a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1810a3e67a4SJesse Barnes {
1820a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
183702e7a56SPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
184702e7a56SPaulo Zanoni 								      pipe);
185702e7a56SPaulo Zanoni 
186702e7a56SPaulo Zanoni 	return I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_ENABLE;
1870a3e67a4SJesse Barnes }
1880a3e67a4SJesse Barnes 
18942f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
19042f52ef8SKeith Packard  * we use as a pipe index
19142f52ef8SKeith Packard  */
192f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1930a3e67a4SJesse Barnes {
1940a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1950a3e67a4SJesse Barnes 	unsigned long high_frame;
1960a3e67a4SJesse Barnes 	unsigned long low_frame;
1975eddb70bSChris Wilson 	u32 high1, high2, low;
1980a3e67a4SJesse Barnes 
1990a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
20044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
2019db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
2020a3e67a4SJesse Barnes 		return 0;
2030a3e67a4SJesse Barnes 	}
2040a3e67a4SJesse Barnes 
2059db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
2069db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
2075eddb70bSChris Wilson 
2080a3e67a4SJesse Barnes 	/*
2090a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
2100a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
2110a3e67a4SJesse Barnes 	 * register.
2120a3e67a4SJesse Barnes 	 */
2130a3e67a4SJesse Barnes 	do {
2145eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
2155eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
2165eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
2170a3e67a4SJesse Barnes 	} while (high1 != high2);
2180a3e67a4SJesse Barnes 
2195eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
2205eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
2215eddb70bSChris Wilson 	return (high1 << 8) | low;
2220a3e67a4SJesse Barnes }
2230a3e67a4SJesse Barnes 
224f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
2259880b7a5SJesse Barnes {
2269880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2279db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
2289880b7a5SJesse Barnes 
2299880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
23044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
2319db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
2329880b7a5SJesse Barnes 		return 0;
2339880b7a5SJesse Barnes 	}
2349880b7a5SJesse Barnes 
2359880b7a5SJesse Barnes 	return I915_READ(reg);
2369880b7a5SJesse Barnes }
2379880b7a5SJesse Barnes 
238f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
2390af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
2400af7e4dfSMario Kleiner {
2410af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2420af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
2430af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
2440af7e4dfSMario Kleiner 	bool in_vbl = true;
2450af7e4dfSMario Kleiner 	int ret = 0;
246fe2b8f9dSPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
247fe2b8f9dSPaulo Zanoni 								      pipe);
2480af7e4dfSMario Kleiner 
2490af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
2500af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
2519db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
2520af7e4dfSMario Kleiner 		return 0;
2530af7e4dfSMario Kleiner 	}
2540af7e4dfSMario Kleiner 
2550af7e4dfSMario Kleiner 	/* Get vtotal. */
256fe2b8f9dSPaulo Zanoni 	vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
2570af7e4dfSMario Kleiner 
2580af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
2590af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
2600af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
2610af7e4dfSMario Kleiner 		 */
2620af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
2630af7e4dfSMario Kleiner 
2640af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
2650af7e4dfSMario Kleiner 		 * horizontal scanout position.
2660af7e4dfSMario Kleiner 		 */
2670af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
2680af7e4dfSMario Kleiner 		*hpos = 0;
2690af7e4dfSMario Kleiner 	} else {
2700af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
2710af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
2720af7e4dfSMario Kleiner 		 * scanout position.
2730af7e4dfSMario Kleiner 		 */
2740af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
2750af7e4dfSMario Kleiner 
276fe2b8f9dSPaulo Zanoni 		htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
2770af7e4dfSMario Kleiner 		*vpos = position / htotal;
2780af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
2790af7e4dfSMario Kleiner 	}
2800af7e4dfSMario Kleiner 
2810af7e4dfSMario Kleiner 	/* Query vblank area. */
282fe2b8f9dSPaulo Zanoni 	vbl = I915_READ(VBLANK(cpu_transcoder));
2830af7e4dfSMario Kleiner 
2840af7e4dfSMario Kleiner 	/* Test position against vblank region. */
2850af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
2860af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
2870af7e4dfSMario Kleiner 
2880af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
2890af7e4dfSMario Kleiner 		in_vbl = false;
2900af7e4dfSMario Kleiner 
2910af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
2920af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
2930af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
2940af7e4dfSMario Kleiner 
2950af7e4dfSMario Kleiner 	/* Readouts valid? */
2960af7e4dfSMario Kleiner 	if (vbl > 0)
2970af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
2980af7e4dfSMario Kleiner 
2990af7e4dfSMario Kleiner 	/* In vblank? */
3000af7e4dfSMario Kleiner 	if (in_vbl)
3010af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
3020af7e4dfSMario Kleiner 
3030af7e4dfSMario Kleiner 	return ret;
3040af7e4dfSMario Kleiner }
3050af7e4dfSMario Kleiner 
306f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
3070af7e4dfSMario Kleiner 			      int *max_error,
3080af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
3090af7e4dfSMario Kleiner 			      unsigned flags)
3100af7e4dfSMario Kleiner {
3114041b853SChris Wilson 	struct drm_crtc *crtc;
3120af7e4dfSMario Kleiner 
3137eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
3144041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
3150af7e4dfSMario Kleiner 		return -EINVAL;
3160af7e4dfSMario Kleiner 	}
3170af7e4dfSMario Kleiner 
3180af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
3194041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
3204041b853SChris Wilson 	if (crtc == NULL) {
3214041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
3224041b853SChris Wilson 		return -EINVAL;
3234041b853SChris Wilson 	}
3244041b853SChris Wilson 
3254041b853SChris Wilson 	if (!crtc->enabled) {
3264041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
3274041b853SChris Wilson 		return -EBUSY;
3284041b853SChris Wilson 	}
3290af7e4dfSMario Kleiner 
3300af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
3314041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
3324041b853SChris Wilson 						     vblank_time, flags,
3334041b853SChris Wilson 						     crtc);
3340af7e4dfSMario Kleiner }
3350af7e4dfSMario Kleiner 
3365ca58282SJesse Barnes /*
3375ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
3385ca58282SJesse Barnes  */
3395ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
3405ca58282SJesse Barnes {
3415ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3425ca58282SJesse Barnes 						    hotplug_work);
3435ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
344c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
3454ef69c7aSChris Wilson 	struct intel_encoder *encoder;
3465ca58282SJesse Barnes 
34752d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
34852d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
34952d7ecedSDaniel Vetter 		return;
35052d7ecedSDaniel Vetter 
351a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
352e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
353e67189abSJesse Barnes 
3544ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
3554ef69c7aSChris Wilson 		if (encoder->hot_plug)
3564ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
357c31c4ba3SKeith Packard 
35840ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
35940ee3381SKeith Packard 
3605ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
361eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3625ca58282SJesse Barnes }
3635ca58282SJesse Barnes 
36473edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
365f97108d1SJesse Barnes {
366f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
367b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
3689270388eSDaniel Vetter 	u8 new_delay;
3699270388eSDaniel Vetter 	unsigned long flags;
3709270388eSDaniel Vetter 
3719270388eSDaniel Vetter 	spin_lock_irqsave(&mchdev_lock, flags);
372f97108d1SJesse Barnes 
37373edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
37473edd18fSDaniel Vetter 
37520e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
3769270388eSDaniel Vetter 
3777648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
378b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
379b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
380f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
381f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
382f97108d1SJesse Barnes 
383f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
384b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
38520e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
38620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
38720e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
38820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
389b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
39020e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
39120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
39220e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
39320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
394f97108d1SJesse Barnes 	}
395f97108d1SJesse Barnes 
3967648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
39720e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
398f97108d1SJesse Barnes 
3999270388eSDaniel Vetter 	spin_unlock_irqrestore(&mchdev_lock, flags);
4009270388eSDaniel Vetter 
401f97108d1SJesse Barnes 	return;
402f97108d1SJesse Barnes }
403f97108d1SJesse Barnes 
404549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
405549f7365SChris Wilson 			struct intel_ring_buffer *ring)
406549f7365SChris Wilson {
407549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
4089862e600SChris Wilson 
409475553deSChris Wilson 	if (ring->obj == NULL)
410475553deSChris Wilson 		return;
411475553deSChris Wilson 
412b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
4139862e600SChris Wilson 
414549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
4153e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
41699584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
41799584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
418cecc21feSChris Wilson 			  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
4193e0dc6b0SBen Widawsky 	}
420549f7365SChris Wilson }
421549f7365SChris Wilson 
4224912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
4233b8d8d91SJesse Barnes {
4244912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
425c6a828d3SDaniel Vetter 						    rps.work);
4264912d041SBen Widawsky 	u32 pm_iir, pm_imr;
4277b9e0ae6SChris Wilson 	u8 new_delay;
4283b8d8d91SJesse Barnes 
429c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
430c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
431c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
4324912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
433a9e2641dSDaniel Vetter 	I915_WRITE(GEN6_PMIMR, 0);
434c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
4354912d041SBen Widawsky 
4367b9e0ae6SChris Wilson 	if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
4373b8d8d91SJesse Barnes 		return;
4383b8d8d91SJesse Barnes 
4394fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
4407b9e0ae6SChris Wilson 
4417b9e0ae6SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
442c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
4437b9e0ae6SChris Wilson 	else
444c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
4453b8d8d91SJesse Barnes 
44679249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
44779249636SBen Widawsky 	 * interrupt
44879249636SBen Widawsky 	 */
44979249636SBen Widawsky 	if (!(new_delay > dev_priv->rps.max_delay ||
45079249636SBen Widawsky 	      new_delay < dev_priv->rps.min_delay)) {
4514912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
45279249636SBen Widawsky 	}
4533b8d8d91SJesse Barnes 
4544fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
4553b8d8d91SJesse Barnes }
4563b8d8d91SJesse Barnes 
457e3689190SBen Widawsky 
458e3689190SBen Widawsky /**
459e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
460e3689190SBen Widawsky  * occurred.
461e3689190SBen Widawsky  * @work: workqueue struct
462e3689190SBen Widawsky  *
463e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
464e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
465e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
466e3689190SBen Widawsky  */
467e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
468e3689190SBen Widawsky {
469e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
470a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
471e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
472e3689190SBen Widawsky 	char *parity_event[5];
473e3689190SBen Widawsky 	uint32_t misccpctl;
474e3689190SBen Widawsky 	unsigned long flags;
475e3689190SBen Widawsky 
476e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
477e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
478e3689190SBen Widawsky 	 * any time we access those registers.
479e3689190SBen Widawsky 	 */
480e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
481e3689190SBen Widawsky 
482e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
483e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
484e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
485e3689190SBen Widawsky 
486e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
487e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
488e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
489e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
490e3689190SBen Widawsky 
491e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
492e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
493e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
494e3689190SBen Widawsky 
495e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
496e3689190SBen Widawsky 
497e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
498e3689190SBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
499e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
500e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
501e3689190SBen Widawsky 
502e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
503e3689190SBen Widawsky 
504e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
505e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
506e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
507e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
508e3689190SBen Widawsky 	parity_event[4] = NULL;
509e3689190SBen Widawsky 
510e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
511e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
512e3689190SBen Widawsky 
513e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
514e3689190SBen Widawsky 		  row, bank, subbank);
515e3689190SBen Widawsky 
516e3689190SBen Widawsky 	kfree(parity_event[3]);
517e3689190SBen Widawsky 	kfree(parity_event[2]);
518e3689190SBen Widawsky 	kfree(parity_event[1]);
519e3689190SBen Widawsky }
520e3689190SBen Widawsky 
521d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
522e3689190SBen Widawsky {
523e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
524e3689190SBen Widawsky 	unsigned long flags;
525e3689190SBen Widawsky 
526e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
527e3689190SBen Widawsky 		return;
528e3689190SBen Widawsky 
529e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
530e3689190SBen Widawsky 	dev_priv->gt_irq_mask |= GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
531e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
532e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
533e3689190SBen Widawsky 
534a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
535e3689190SBen Widawsky }
536e3689190SBen Widawsky 
537e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
538e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
539e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
540e7b4c6b1SDaniel Vetter {
541e7b4c6b1SDaniel Vetter 
542e7b4c6b1SDaniel Vetter 	if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
543e7b4c6b1SDaniel Vetter 		      GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
544e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
545e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BSD_USER_INTERRUPT)
546e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
547e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
548e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
549e7b4c6b1SDaniel Vetter 
550e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
551e7b4c6b1SDaniel Vetter 		      GT_GEN6_BSD_CS_ERROR_INTERRUPT |
552e7b4c6b1SDaniel Vetter 		      GT_RENDER_CS_ERROR_INTERRUPT)) {
553e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
554e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
555e7b4c6b1SDaniel Vetter 	}
556e3689190SBen Widawsky 
557e3689190SBen Widawsky 	if (gt_iir & GT_GEN7_L3_PARITY_ERROR_INTERRUPT)
558e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
559e7b4c6b1SDaniel Vetter }
560e7b4c6b1SDaniel Vetter 
561fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
562fc6826d1SChris Wilson 				u32 pm_iir)
563fc6826d1SChris Wilson {
564fc6826d1SChris Wilson 	unsigned long flags;
565fc6826d1SChris Wilson 
566fc6826d1SChris Wilson 	/*
567fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
568fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
569fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
570c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
571fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
572fc6826d1SChris Wilson 	 *
573c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
574fc6826d1SChris Wilson 	 */
575fc6826d1SChris Wilson 
576c6a828d3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
577c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
578c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
579fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
580c6a828d3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
581fc6826d1SChris Wilson 
582c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
583fc6826d1SChris Wilson }
584fc6826d1SChris Wilson 
585515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
586515ac2bbSDaniel Vetter {
58728c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
58828c70f16SDaniel Vetter 
58928c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
590515ac2bbSDaniel Vetter }
591515ac2bbSDaniel Vetter 
592ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
593ce99c256SDaniel Vetter {
5949ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
5959ee32feaSDaniel Vetter 
5969ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
597ce99c256SDaniel Vetter }
598ce99c256SDaniel Vetter 
599ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
6007e231dbeSJesse Barnes {
6017e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
6027e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6037e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
6047e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
6057e231dbeSJesse Barnes 	unsigned long irqflags;
6067e231dbeSJesse Barnes 	int pipe;
6077e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
6087e231dbeSJesse Barnes 
6097e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
6107e231dbeSJesse Barnes 
6117e231dbeSJesse Barnes 	while (true) {
6127e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
6137e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
6147e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
6157e231dbeSJesse Barnes 
6167e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
6177e231dbeSJesse Barnes 			goto out;
6187e231dbeSJesse Barnes 
6197e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
6207e231dbeSJesse Barnes 
621e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
6227e231dbeSJesse Barnes 
6237e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6247e231dbeSJesse Barnes 		for_each_pipe(pipe) {
6257e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
6267e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
6277e231dbeSJesse Barnes 
6287e231dbeSJesse Barnes 			/*
6297e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
6307e231dbeSJesse Barnes 			 */
6317e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
6327e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
6337e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
6347e231dbeSJesse Barnes 							 pipe_name(pipe));
6357e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
6367e231dbeSJesse Barnes 			}
6377e231dbeSJesse Barnes 		}
6387e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6397e231dbeSJesse Barnes 
64031acc7f5SJesse Barnes 		for_each_pipe(pipe) {
64131acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
64231acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
64331acc7f5SJesse Barnes 
64431acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
64531acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
64631acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
64731acc7f5SJesse Barnes 			}
64831acc7f5SJesse Barnes 		}
64931acc7f5SJesse Barnes 
6507e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
6517e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
6527e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
6537e231dbeSJesse Barnes 
6547e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
6557e231dbeSJesse Barnes 					 hotplug_status);
656e5868a31SEgbert Eich 			if (hotplug_status & HOTPLUG_INT_STATUS_I915)
6577e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
6587e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
6597e231dbeSJesse Barnes 
6607e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
6617e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
6627e231dbeSJesse Barnes 		}
6637e231dbeSJesse Barnes 
664515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
665515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
6667e231dbeSJesse Barnes 
667fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
668fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
6697e231dbeSJesse Barnes 
6707e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
6717e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
6727e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
6737e231dbeSJesse Barnes 	}
6747e231dbeSJesse Barnes 
6757e231dbeSJesse Barnes out:
6767e231dbeSJesse Barnes 	return ret;
6777e231dbeSJesse Barnes }
6787e231dbeSJesse Barnes 
67923e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
680776ad806SJesse Barnes {
681776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6829db4a9c7SJesse Barnes 	int pipe;
683776ad806SJesse Barnes 
68476e43830SDaniel Vetter 	if (pch_iir & SDE_HOTPLUG_MASK)
68576e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
68676e43830SDaniel Vetter 
687776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
688776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
689776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
690776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
691776ad806SJesse Barnes 
692ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
693ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
694ce99c256SDaniel Vetter 
695776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
696515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
697776ad806SJesse Barnes 
698776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
699776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
700776ad806SJesse Barnes 
701776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
702776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
703776ad806SJesse Barnes 
704776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
705776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
706776ad806SJesse Barnes 
7079db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
7089db4a9c7SJesse Barnes 		for_each_pipe(pipe)
7099db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
7109db4a9c7SJesse Barnes 					 pipe_name(pipe),
7119db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
712776ad806SJesse Barnes 
713776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
714776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
715776ad806SJesse Barnes 
716776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
717776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
718776ad806SJesse Barnes 
719776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
720776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
721776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
722776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
723776ad806SJesse Barnes }
724776ad806SJesse Barnes 
72523e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
72623e81d69SAdam Jackson {
72723e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
72823e81d69SAdam Jackson 	int pipe;
72923e81d69SAdam Jackson 
73076e43830SDaniel Vetter 	if (pch_iir & SDE_HOTPLUG_MASK_CPT)
73176e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
73276e43830SDaniel Vetter 
73323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT)
73423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
73523e81d69SAdam Jackson 				 (pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
73623e81d69SAdam Jackson 				 SDE_AUDIO_POWER_SHIFT_CPT);
73723e81d69SAdam Jackson 
73823e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
739ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
74023e81d69SAdam Jackson 
74123e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
742515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
74323e81d69SAdam Jackson 
74423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
74523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
74623e81d69SAdam Jackson 
74723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
74823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
74923e81d69SAdam Jackson 
75023e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
75123e81d69SAdam Jackson 		for_each_pipe(pipe)
75223e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
75323e81d69SAdam Jackson 					 pipe_name(pipe),
75423e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
75523e81d69SAdam Jackson }
75623e81d69SAdam Jackson 
757ff1f525eSDaniel Vetter static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
758b1f14ad0SJesse Barnes {
759b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
760b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
761ab5c608bSBen Widawsky 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
7620e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
7630e43406bSChris Wilson 	int i;
764b1f14ad0SJesse Barnes 
765b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
766b1f14ad0SJesse Barnes 
767b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
768b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
769b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
7700e43406bSChris Wilson 
77144498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
77244498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
77344498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
77444498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
77544498aeaSPaulo Zanoni 	 * due to its back queue). */
776ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
77744498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
77844498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
77944498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
780ab5c608bSBen Widawsky 	}
78144498aeaSPaulo Zanoni 
7820e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
7830e43406bSChris Wilson 	if (gt_iir) {
7840e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
7850e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
7860e43406bSChris Wilson 		ret = IRQ_HANDLED;
7870e43406bSChris Wilson 	}
788b1f14ad0SJesse Barnes 
789b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
7900e43406bSChris Wilson 	if (de_iir) {
791ce99c256SDaniel Vetter 		if (de_iir & DE_AUX_CHANNEL_A_IVB)
792ce99c256SDaniel Vetter 			dp_aux_irq_handler(dev);
793ce99c256SDaniel Vetter 
794b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
795b1f14ad0SJesse Barnes 			intel_opregion_gse_intr(dev);
796b1f14ad0SJesse Barnes 
7970e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
79874d44445SDaniel Vetter 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
79974d44445SDaniel Vetter 				drm_handle_vblank(dev, i);
8000e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
8010e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
8020e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
803b1f14ad0SJesse Barnes 			}
804b1f14ad0SJesse Barnes 		}
805b1f14ad0SJesse Barnes 
806b1f14ad0SJesse Barnes 		/* check event from PCH */
807ab5c608bSBen Widawsky 		if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
8080e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
8090e43406bSChris Wilson 
81023e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
8110e43406bSChris Wilson 
8120e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
8130e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
814b1f14ad0SJesse Barnes 		}
815b1f14ad0SJesse Barnes 
8160e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
8170e43406bSChris Wilson 		ret = IRQ_HANDLED;
8180e43406bSChris Wilson 	}
8190e43406bSChris Wilson 
8200e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
8210e43406bSChris Wilson 	if (pm_iir) {
822fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
823fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
824b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
8250e43406bSChris Wilson 		ret = IRQ_HANDLED;
8260e43406bSChris Wilson 	}
827b1f14ad0SJesse Barnes 
828b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
829b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
830ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
83144498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
83244498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
833ab5c608bSBen Widawsky 	}
834b1f14ad0SJesse Barnes 
835b1f14ad0SJesse Barnes 	return ret;
836b1f14ad0SJesse Barnes }
837b1f14ad0SJesse Barnes 
838e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
839e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
840e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
841e7b4c6b1SDaniel Vetter {
842e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
843e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
844e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
845e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
846e7b4c6b1SDaniel Vetter }
847e7b4c6b1SDaniel Vetter 
848ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
849036a4a7dSZhenyu Wang {
8504697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
851036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
852036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
85344498aeaSPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
854881f47b6SXiang, Haihao 
8554697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
8564697995bSJesse Barnes 
8572d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
8582d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
8592d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
8603143a2bfSChris Wilson 	POSTING_READ(DEIER);
8612d109a84SZou, Nanhai 
86244498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
86344498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
86444498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
86544498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
86644498aeaSPaulo Zanoni 	 * due to its back queue). */
86744498aeaSPaulo Zanoni 	sde_ier = I915_READ(SDEIER);
86844498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, 0);
86944498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
87044498aeaSPaulo Zanoni 
871036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
872036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
8733b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
874036a4a7dSZhenyu Wang 
875acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
876c7c85101SZou Nan hai 		goto done;
877036a4a7dSZhenyu Wang 
878036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
879036a4a7dSZhenyu Wang 
880e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
881e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
882e7b4c6b1SDaniel Vetter 	else
883e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
884036a4a7dSZhenyu Wang 
885ce99c256SDaniel Vetter 	if (de_iir & DE_AUX_CHANNEL_A)
886ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
887ce99c256SDaniel Vetter 
88801c66889SZhao Yakui 	if (de_iir & DE_GSE)
8893b617967SChris Wilson 		intel_opregion_gse_intr(dev);
89001c66889SZhao Yakui 
89174d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
89274d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
89374d44445SDaniel Vetter 
89474d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
89574d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
89674d44445SDaniel Vetter 
897f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
898013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
8992bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
900013d5aa2SJesse Barnes 	}
901013d5aa2SJesse Barnes 
902f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
903f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
9042bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
905013d5aa2SJesse Barnes 	}
906c062df61SLi Peng 
907c650156aSZhenyu Wang 	/* check event from PCH */
908776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
909acd15b6cSDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
910acd15b6cSDaniel Vetter 
91123e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
91223e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
91323e81d69SAdam Jackson 		else
91423e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
915acd15b6cSDaniel Vetter 
916acd15b6cSDaniel Vetter 		/* should clear PCH hotplug event before clear CPU irq */
917acd15b6cSDaniel Vetter 		I915_WRITE(SDEIIR, pch_iir);
918776ad806SJesse Barnes 	}
919c650156aSZhenyu Wang 
92073edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
92173edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
922f97108d1SJesse Barnes 
923fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
924fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
9253b8d8d91SJesse Barnes 
926c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
927c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
9284912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
929036a4a7dSZhenyu Wang 
930c7c85101SZou Nan hai done:
9312d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
9323143a2bfSChris Wilson 	POSTING_READ(DEIER);
93344498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, sde_ier);
93444498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
9352d109a84SZou, Nanhai 
936036a4a7dSZhenyu Wang 	return ret;
937036a4a7dSZhenyu Wang }
938036a4a7dSZhenyu Wang 
9398a905236SJesse Barnes /**
9408a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
9418a905236SJesse Barnes  * @work: work struct
9428a905236SJesse Barnes  *
9438a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
9448a905236SJesse Barnes  * was detected.
9458a905236SJesse Barnes  */
9468a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
9478a905236SJesse Barnes {
9481f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
9491f83fee0SDaniel Vetter 						    work);
9501f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
9511f83fee0SDaniel Vetter 						    gpu_error);
9528a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
953f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
954f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
955f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
956f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
957f69061beSDaniel Vetter 	int i, ret;
9588a905236SJesse Barnes 
959f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
9608a905236SJesse Barnes 
9617db0ba24SDaniel Vetter 	/*
9627db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
9637db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
9647db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
9657db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
9667db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
9677db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
9687db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
9697db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
9707db0ba24SDaniel Vetter 	 */
9717db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
97244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
9737db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
9747db0ba24SDaniel Vetter 				   reset_event);
9751f83fee0SDaniel Vetter 
976f69061beSDaniel Vetter 		ret = i915_reset(dev);
977f69061beSDaniel Vetter 
978f69061beSDaniel Vetter 		if (ret == 0) {
979f69061beSDaniel Vetter 			/*
980f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
981f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
982f69061beSDaniel Vetter 			 * complete.
983f69061beSDaniel Vetter 			 *
984f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
985f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
986f69061beSDaniel Vetter 			 * updates before
987f69061beSDaniel Vetter 			 * the counter increment.
988f69061beSDaniel Vetter 			 */
989f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
990f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
991f69061beSDaniel Vetter 
992f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
993f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
9941f83fee0SDaniel Vetter 		} else {
9951f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
996f316a42cSBen Gamari 		}
9971f83fee0SDaniel Vetter 
998f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
999f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
1000f69061beSDaniel Vetter 
100196a02917SVille Syrjälä 		intel_display_handle_reset(dev);
100296a02917SVille Syrjälä 
10031f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
1004f316a42cSBen Gamari 	}
10058a905236SJesse Barnes }
10068a905236SJesse Barnes 
100785f9e50dSDaniel Vetter /* NB: please notice the memset */
100885f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
100985f9e50dSDaniel Vetter 				    uint32_t *instdone)
101085f9e50dSDaniel Vetter {
101185f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
101285f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
101385f9e50dSDaniel Vetter 
101485f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
101585f9e50dSDaniel Vetter 	case 2:
101685f9e50dSDaniel Vetter 	case 3:
101785f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
101885f9e50dSDaniel Vetter 		break;
101985f9e50dSDaniel Vetter 	case 4:
102085f9e50dSDaniel Vetter 	case 5:
102185f9e50dSDaniel Vetter 	case 6:
102285f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
102385f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
102485f9e50dSDaniel Vetter 		break;
102585f9e50dSDaniel Vetter 	default:
102685f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
102785f9e50dSDaniel Vetter 	case 7:
102885f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
102985f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
103085f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
103185f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
103285f9e50dSDaniel Vetter 		break;
103385f9e50dSDaniel Vetter 	}
103485f9e50dSDaniel Vetter }
103585f9e50dSDaniel Vetter 
10363bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
10379df30794SChris Wilson static struct drm_i915_error_object *
1038d0d045e8SBen Widawsky i915_error_object_create_sized(struct drm_i915_private *dev_priv,
1039d0d045e8SBen Widawsky 			       struct drm_i915_gem_object *src,
1040d0d045e8SBen Widawsky 			       const int num_pages)
10419df30794SChris Wilson {
10429df30794SChris Wilson 	struct drm_i915_error_object *dst;
1043d0d045e8SBen Widawsky 	int i;
1044e56660ddSChris Wilson 	u32 reloc_offset;
10459df30794SChris Wilson 
104605394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
10479df30794SChris Wilson 		return NULL;
10489df30794SChris Wilson 
1049d0d045e8SBen Widawsky 	dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
10509df30794SChris Wilson 	if (dst == NULL)
10519df30794SChris Wilson 		return NULL;
10529df30794SChris Wilson 
105305394f39SChris Wilson 	reloc_offset = src->gtt_offset;
1054d0d045e8SBen Widawsky 	for (i = 0; i < num_pages; i++) {
1055788885aeSAndrew Morton 		unsigned long flags;
1056e56660ddSChris Wilson 		void *d;
1057788885aeSAndrew Morton 
1058e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
10599df30794SChris Wilson 		if (d == NULL)
10609df30794SChris Wilson 			goto unwind;
1061e56660ddSChris Wilson 
1062788885aeSAndrew Morton 		local_irq_save(flags);
10635d4545aeSBen Widawsky 		if (reloc_offset < dev_priv->gtt.mappable_end &&
106474898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
1065172975aaSChris Wilson 			void __iomem *s;
1066172975aaSChris Wilson 
1067172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
1068172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
1069172975aaSChris Wilson 			 * captures what the GPU read.
1070172975aaSChris Wilson 			 */
1071172975aaSChris Wilson 
10725d4545aeSBen Widawsky 			s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
10733e4d3af5SPeter Zijlstra 						     reloc_offset);
1074e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
10753e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
1076960e3564SChris Wilson 		} else if (src->stolen) {
1077960e3564SChris Wilson 			unsigned long offset;
1078960e3564SChris Wilson 
1079960e3564SChris Wilson 			offset = dev_priv->mm.stolen_base;
1080960e3564SChris Wilson 			offset += src->stolen->start;
1081960e3564SChris Wilson 			offset += i << PAGE_SHIFT;
1082960e3564SChris Wilson 
10831a240d4dSDaniel Vetter 			memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
1084172975aaSChris Wilson 		} else {
10859da3da66SChris Wilson 			struct page *page;
1086172975aaSChris Wilson 			void *s;
1087172975aaSChris Wilson 
10889da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
1089172975aaSChris Wilson 
10909da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
10919da3da66SChris Wilson 
10929da3da66SChris Wilson 			s = kmap_atomic(page);
1093172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
1094172975aaSChris Wilson 			kunmap_atomic(s);
1095172975aaSChris Wilson 
10969da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
1097172975aaSChris Wilson 		}
1098788885aeSAndrew Morton 		local_irq_restore(flags);
1099e56660ddSChris Wilson 
11009da3da66SChris Wilson 		dst->pages[i] = d;
1101e56660ddSChris Wilson 
1102e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
11039df30794SChris Wilson 	}
1104d0d045e8SBen Widawsky 	dst->page_count = num_pages;
110505394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
11069df30794SChris Wilson 
11079df30794SChris Wilson 	return dst;
11089df30794SChris Wilson 
11099df30794SChris Wilson unwind:
11109da3da66SChris Wilson 	while (i--)
11119da3da66SChris Wilson 		kfree(dst->pages[i]);
11129df30794SChris Wilson 	kfree(dst);
11139df30794SChris Wilson 	return NULL;
11149df30794SChris Wilson }
1115d0d045e8SBen Widawsky #define i915_error_object_create(dev_priv, src) \
1116d0d045e8SBen Widawsky 	i915_error_object_create_sized((dev_priv), (src), \
1117d0d045e8SBen Widawsky 				       (src)->base.size>>PAGE_SHIFT)
11189df30794SChris Wilson 
11199df30794SChris Wilson static void
11209df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
11219df30794SChris Wilson {
11229df30794SChris Wilson 	int page;
11239df30794SChris Wilson 
11249df30794SChris Wilson 	if (obj == NULL)
11259df30794SChris Wilson 		return;
11269df30794SChris Wilson 
11279df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
11289df30794SChris Wilson 		kfree(obj->pages[page]);
11299df30794SChris Wilson 
11309df30794SChris Wilson 	kfree(obj);
11319df30794SChris Wilson }
11329df30794SChris Wilson 
1133742cbee8SDaniel Vetter void
1134742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
11359df30794SChris Wilson {
1136742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
1137742cbee8SDaniel Vetter 							  typeof(*error), ref);
1138e2f973d5SChris Wilson 	int i;
1139e2f973d5SChris Wilson 
114052d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
114152d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
114252d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
114352d39a21SChris Wilson 		kfree(error->ring[i].requests);
114452d39a21SChris Wilson 	}
1145e2f973d5SChris Wilson 
11469df30794SChris Wilson 	kfree(error->active_bo);
11476ef3d427SChris Wilson 	kfree(error->overlay);
11489df30794SChris Wilson 	kfree(error);
11499df30794SChris Wilson }
11501b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
11511b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
1152c724e8a9SChris Wilson {
1153c724e8a9SChris Wilson 	err->size = obj->base.size;
1154c724e8a9SChris Wilson 	err->name = obj->base.name;
11550201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
11560201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1157c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
1158c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1159c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1160c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1161c724e8a9SChris Wilson 	err->pinned = 0;
1162c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1163c724e8a9SChris Wilson 		err->pinned = 1;
1164c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1165c724e8a9SChris Wilson 		err->pinned = -1;
1166c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1167c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1168c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
116996154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
117093dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
11711b50247aSChris Wilson }
1172c724e8a9SChris Wilson 
11731b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
11741b50247aSChris Wilson 			     int count, struct list_head *head)
11751b50247aSChris Wilson {
11761b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
11771b50247aSChris Wilson 	int i = 0;
11781b50247aSChris Wilson 
11791b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
11801b50247aSChris Wilson 		capture_bo(err++, obj);
1181c724e8a9SChris Wilson 		if (++i == count)
1182c724e8a9SChris Wilson 			break;
11831b50247aSChris Wilson 	}
1184c724e8a9SChris Wilson 
11851b50247aSChris Wilson 	return i;
11861b50247aSChris Wilson }
11871b50247aSChris Wilson 
11881b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
11891b50247aSChris Wilson 			     int count, struct list_head *head)
11901b50247aSChris Wilson {
11911b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
11921b50247aSChris Wilson 	int i = 0;
11931b50247aSChris Wilson 
11941b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
11951b50247aSChris Wilson 		if (obj->pin_count == 0)
11961b50247aSChris Wilson 			continue;
11971b50247aSChris Wilson 
11981b50247aSChris Wilson 		capture_bo(err++, obj);
11991b50247aSChris Wilson 		if (++i == count)
12001b50247aSChris Wilson 			break;
1201c724e8a9SChris Wilson 	}
1202c724e8a9SChris Wilson 
1203c724e8a9SChris Wilson 	return i;
1204c724e8a9SChris Wilson }
1205c724e8a9SChris Wilson 
1206748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1207748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1208748ebc60SChris Wilson {
1209748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1210748ebc60SChris Wilson 	int i;
1211748ebc60SChris Wilson 
1212748ebc60SChris Wilson 	/* Fences */
1213748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1214775d17b6SDaniel Vetter 	case 7:
1215748ebc60SChris Wilson 	case 6:
1216*42b5aeabSVille Syrjälä 		for (i = 0; i < dev_priv->num_fence_regs; i++)
1217748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1218748ebc60SChris Wilson 		break;
1219748ebc60SChris Wilson 	case 5:
1220748ebc60SChris Wilson 	case 4:
1221748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1222748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1223748ebc60SChris Wilson 		break;
1224748ebc60SChris Wilson 	case 3:
1225748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1226748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1227748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1228748ebc60SChris Wilson 	case 2:
1229748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1230748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1231748ebc60SChris Wilson 		break;
1232748ebc60SChris Wilson 
12337dbf9d6eSBen Widawsky 	default:
12347dbf9d6eSBen Widawsky 		BUG();
1235748ebc60SChris Wilson 	}
1236748ebc60SChris Wilson }
1237748ebc60SChris Wilson 
1238bcfb2e28SChris Wilson static struct drm_i915_error_object *
1239bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1240bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1241bcfb2e28SChris Wilson {
1242bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1243bcfb2e28SChris Wilson 	u32 seqno;
1244bcfb2e28SChris Wilson 
1245bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1246bcfb2e28SChris Wilson 		return NULL;
1247bcfb2e28SChris Wilson 
1248b45305fcSDaniel Vetter 	if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1249b45305fcSDaniel Vetter 		u32 acthd = I915_READ(ACTHD);
1250b45305fcSDaniel Vetter 
1251b45305fcSDaniel Vetter 		if (WARN_ON(ring->id != RCS))
1252b45305fcSDaniel Vetter 			return NULL;
1253b45305fcSDaniel Vetter 
1254b45305fcSDaniel Vetter 		obj = ring->private;
1255b45305fcSDaniel Vetter 		if (acthd >= obj->gtt_offset &&
1256b45305fcSDaniel Vetter 		    acthd < obj->gtt_offset + obj->base.size)
1257b45305fcSDaniel Vetter 			return i915_error_object_create(dev_priv, obj);
1258b45305fcSDaniel Vetter 	}
1259b45305fcSDaniel Vetter 
1260b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1261bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1262bcfb2e28SChris Wilson 		if (obj->ring != ring)
1263bcfb2e28SChris Wilson 			continue;
1264bcfb2e28SChris Wilson 
12650201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1266bcfb2e28SChris Wilson 			continue;
1267bcfb2e28SChris Wilson 
1268bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1269bcfb2e28SChris Wilson 			continue;
1270bcfb2e28SChris Wilson 
1271bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1272bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1273bcfb2e28SChris Wilson 		 */
1274bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1275bcfb2e28SChris Wilson 	}
1276bcfb2e28SChris Wilson 
1277bcfb2e28SChris Wilson 	return NULL;
1278bcfb2e28SChris Wilson }
1279bcfb2e28SChris Wilson 
1280d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1281d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1282d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1283d27b1e0eSDaniel Vetter {
1284d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1285d27b1e0eSDaniel Vetter 
128633f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
128712f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
128833f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
12897e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
12907e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
12917e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
12927e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
1293df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1294df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
129533f3f518SDaniel Vetter 	}
1296c1cd90edSDaniel Vetter 
1297d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
12989d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1299d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1300d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1301d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1302c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1303050ee91fSBen Widawsky 		if (ring->id == RCS)
1304d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1305d27b1e0eSDaniel Vetter 	} else {
13069d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1307d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1308d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1309d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1310d27b1e0eSDaniel Vetter 	}
1311d27b1e0eSDaniel Vetter 
13129574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1313c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1314b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1315d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1316c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1317c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
13180f3b6849SChris Wilson 	error->ctl[ring->id] = I915_READ_CTL(ring);
13197e3b8737SDaniel Vetter 
13207e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
13217e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1322d27b1e0eSDaniel Vetter }
1323d27b1e0eSDaniel Vetter 
13248c123e54SBen Widawsky 
13258c123e54SBen Widawsky static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
13268c123e54SBen Widawsky 					   struct drm_i915_error_state *error,
13278c123e54SBen Widawsky 					   struct drm_i915_error_ring *ering)
13288c123e54SBen Widawsky {
13298c123e54SBen Widawsky 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
13308c123e54SBen Widawsky 	struct drm_i915_gem_object *obj;
13318c123e54SBen Widawsky 
13328c123e54SBen Widawsky 	/* Currently render ring is the only HW context user */
13338c123e54SBen Widawsky 	if (ring->id != RCS || !error->ccid)
13348c123e54SBen Widawsky 		return;
13358c123e54SBen Widawsky 
13368c123e54SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) {
13378c123e54SBen Widawsky 		if ((error->ccid & PAGE_MASK) == obj->gtt_offset) {
13388c123e54SBen Widawsky 			ering->ctx = i915_error_object_create_sized(dev_priv,
13398c123e54SBen Widawsky 								    obj, 1);
13408c123e54SBen Widawsky 		}
13418c123e54SBen Widawsky 	}
13428c123e54SBen Widawsky }
13438c123e54SBen Widawsky 
134452d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
134552d39a21SChris Wilson 				  struct drm_i915_error_state *error)
134652d39a21SChris Wilson {
134752d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1348b4519513SChris Wilson 	struct intel_ring_buffer *ring;
134952d39a21SChris Wilson 	struct drm_i915_gem_request *request;
135052d39a21SChris Wilson 	int i, count;
135152d39a21SChris Wilson 
1352b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
135352d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
135452d39a21SChris Wilson 
135552d39a21SChris Wilson 		error->ring[i].batchbuffer =
135652d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
135752d39a21SChris Wilson 
135852d39a21SChris Wilson 		error->ring[i].ringbuffer =
135952d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
136052d39a21SChris Wilson 
13618c123e54SBen Widawsky 
13628c123e54SBen Widawsky 		i915_gem_record_active_context(ring, error, &error->ring[i]);
13638c123e54SBen Widawsky 
136452d39a21SChris Wilson 		count = 0;
136552d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
136652d39a21SChris Wilson 			count++;
136752d39a21SChris Wilson 
136852d39a21SChris Wilson 		error->ring[i].num_requests = count;
136952d39a21SChris Wilson 		error->ring[i].requests =
137052d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
137152d39a21SChris Wilson 				GFP_ATOMIC);
137252d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
137352d39a21SChris Wilson 			error->ring[i].num_requests = 0;
137452d39a21SChris Wilson 			continue;
137552d39a21SChris Wilson 		}
137652d39a21SChris Wilson 
137752d39a21SChris Wilson 		count = 0;
137852d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
137952d39a21SChris Wilson 			struct drm_i915_error_request *erq;
138052d39a21SChris Wilson 
138152d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
138252d39a21SChris Wilson 			erq->seqno = request->seqno;
138352d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1384ee4f42b1SChris Wilson 			erq->tail = request->tail;
138552d39a21SChris Wilson 		}
138652d39a21SChris Wilson 	}
138752d39a21SChris Wilson }
138852d39a21SChris Wilson 
13898a905236SJesse Barnes /**
13908a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
13918a905236SJesse Barnes  * @dev: drm device
13928a905236SJesse Barnes  *
13938a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
13948a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
13958a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
13968a905236SJesse Barnes  * to pick up.
13978a905236SJesse Barnes  */
139863eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
139963eeaf38SJesse Barnes {
140063eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
140105394f39SChris Wilson 	struct drm_i915_gem_object *obj;
140263eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
140363eeaf38SJesse Barnes 	unsigned long flags;
14049db4a9c7SJesse Barnes 	int i, pipe;
140563eeaf38SJesse Barnes 
140699584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
140799584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
140899584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
14099df30794SChris Wilson 	if (error)
14109df30794SChris Wilson 		return;
141163eeaf38SJesse Barnes 
14129db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
141333f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
141463eeaf38SJesse Barnes 	if (!error) {
14159df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
14169df30794SChris Wilson 		return;
141763eeaf38SJesse Barnes 	}
141863eeaf38SJesse Barnes 
14192f86f191SBen Widawsky 	DRM_INFO("capturing error event; look for more information in "
14202f86f191SBen Widawsky 		 "/sys/kernel/debug/dri/%d/i915_error_state\n",
1421b6f7833bSChris Wilson 		 dev->primary->index);
14222fa772f3SChris Wilson 
1423742cbee8SDaniel Vetter 	kref_init(&error->ref);
142463eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
142563eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1426211816ecSBen Widawsky 	if (HAS_HW_CONTEXTS(dev))
1427b9a3906bSBen Widawsky 		error->ccid = I915_READ(CCID);
1428be998e2eSBen Widawsky 
1429be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1430be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1431be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1432be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1433be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1434be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1435be998e2eSBen Widawsky 	else
1436be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1437be998e2eSBen Widawsky 
14380f3b6849SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6)
14390f3b6849SChris Wilson 		error->derrmr = I915_READ(DERRMR);
14400f3b6849SChris Wilson 
14410f3b6849SChris Wilson 	if (IS_VALLEYVIEW(dev))
14420f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_VLV);
14430f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 7)
14440f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_MT);
14450f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen == 6)
14460f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE);
14470f3b6849SChris Wilson 
14484f3308b9SPaulo Zanoni 	if (!HAS_PCH_SPLIT(dev))
14499db4a9c7SJesse Barnes 		for_each_pipe(pipe)
14509db4a9c7SJesse Barnes 			error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1451d27b1e0eSDaniel Vetter 
145233f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1453f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
145433f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
145533f3f518SDaniel Vetter 	}
1456add354ddSChris Wilson 
145771e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
145871e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
145971e172e8SBen Widawsky 
1460050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1461050ee91fSBen Widawsky 
1462748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
146352d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
14649df30794SChris Wilson 
1465c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
14669df30794SChris Wilson 	error->active_bo = NULL;
1467c724e8a9SChris Wilson 	error->pinned_bo = NULL;
14689df30794SChris Wilson 
1469bcfb2e28SChris Wilson 	i = 0;
1470bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1471bcfb2e28SChris Wilson 		i++;
1472bcfb2e28SChris Wilson 	error->active_bo_count = i;
14736c085a72SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
14741b50247aSChris Wilson 		if (obj->pin_count)
1475bcfb2e28SChris Wilson 			i++;
1476bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1477c724e8a9SChris Wilson 
14788e934dbfSChris Wilson 	error->active_bo = NULL;
14798e934dbfSChris Wilson 	error->pinned_bo = NULL;
1480bcfb2e28SChris Wilson 	if (i) {
1481bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
14829df30794SChris Wilson 					   GFP_ATOMIC);
1483c724e8a9SChris Wilson 		if (error->active_bo)
1484c724e8a9SChris Wilson 			error->pinned_bo =
1485c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
14869df30794SChris Wilson 	}
1487c724e8a9SChris Wilson 
1488c724e8a9SChris Wilson 	if (error->active_bo)
1489c724e8a9SChris Wilson 		error->active_bo_count =
14901b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1491c724e8a9SChris Wilson 					  error->active_bo_count,
1492c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1493c724e8a9SChris Wilson 
1494c724e8a9SChris Wilson 	if (error->pinned_bo)
1495c724e8a9SChris Wilson 		error->pinned_bo_count =
14961b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1497c724e8a9SChris Wilson 					  error->pinned_bo_count,
14986c085a72SChris Wilson 					  &dev_priv->mm.bound_list);
149963eeaf38SJesse Barnes 
15008a905236SJesse Barnes 	do_gettimeofday(&error->time);
15018a905236SJesse Barnes 
15026ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1503c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
15046ef3d427SChris Wilson 
150599584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
150699584db3SDaniel Vetter 	if (dev_priv->gpu_error.first_error == NULL) {
150799584db3SDaniel Vetter 		dev_priv->gpu_error.first_error = error;
15089df30794SChris Wilson 		error = NULL;
15099df30794SChris Wilson 	}
151099584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
15119df30794SChris Wilson 
15129df30794SChris Wilson 	if (error)
1513742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
15149df30794SChris Wilson }
15159df30794SChris Wilson 
15169df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
15179df30794SChris Wilson {
15189df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
15199df30794SChris Wilson 	struct drm_i915_error_state *error;
15206dc0e816SBen Widawsky 	unsigned long flags;
15219df30794SChris Wilson 
152299584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
152399584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
152499584db3SDaniel Vetter 	dev_priv->gpu_error.first_error = NULL;
152599584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
15269df30794SChris Wilson 
15279df30794SChris Wilson 	if (error)
1528742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
152963eeaf38SJesse Barnes }
15303bd3c932SChris Wilson #else
15313bd3c932SChris Wilson #define i915_capture_error_state(x)
15323bd3c932SChris Wilson #endif
153363eeaf38SJesse Barnes 
153435aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1535c0e09200SDave Airlie {
15368a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1537bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
153863eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1539050ee91fSBen Widawsky 	int pipe, i;
154063eeaf38SJesse Barnes 
154135aed2e6SChris Wilson 	if (!eir)
154235aed2e6SChris Wilson 		return;
154363eeaf38SJesse Barnes 
1544a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
15458a905236SJesse Barnes 
1546bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1547bd9854f9SBen Widawsky 
15488a905236SJesse Barnes 	if (IS_G4X(dev)) {
15498a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
15508a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
15518a905236SJesse Barnes 
1552a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1553a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1554050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1555050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1556a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1557a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
15588a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
15593143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
15608a905236SJesse Barnes 		}
15618a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
15628a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1563a70491ccSJoe Perches 			pr_err("page table error\n");
1564a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
15658a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
15663143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
15678a905236SJesse Barnes 		}
15688a905236SJesse Barnes 	}
15698a905236SJesse Barnes 
1570a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
157163eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
157263eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1573a70491ccSJoe Perches 			pr_err("page table error\n");
1574a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
157563eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
15763143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
157763eeaf38SJesse Barnes 		}
15788a905236SJesse Barnes 	}
15798a905236SJesse Barnes 
158063eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1581a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
15829db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1583a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
15849db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
158563eeaf38SJesse Barnes 		/* pipestat has already been acked */
158663eeaf38SJesse Barnes 	}
158763eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1588a70491ccSJoe Perches 		pr_err("instruction error\n");
1589a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1590050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1591050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1592a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
159363eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
159463eeaf38SJesse Barnes 
1595a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1596a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1597a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
159863eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
15993143a2bfSChris Wilson 			POSTING_READ(IPEIR);
160063eeaf38SJesse Barnes 		} else {
160163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
160263eeaf38SJesse Barnes 
1603a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1604a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1605a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1606a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
160763eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
16083143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
160963eeaf38SJesse Barnes 		}
161063eeaf38SJesse Barnes 	}
161163eeaf38SJesse Barnes 
161263eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
16133143a2bfSChris Wilson 	POSTING_READ(EIR);
161463eeaf38SJesse Barnes 	eir = I915_READ(EIR);
161563eeaf38SJesse Barnes 	if (eir) {
161663eeaf38SJesse Barnes 		/*
161763eeaf38SJesse Barnes 		 * some errors might have become stuck,
161863eeaf38SJesse Barnes 		 * mask them.
161963eeaf38SJesse Barnes 		 */
162063eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
162163eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
162263eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
162363eeaf38SJesse Barnes 	}
162435aed2e6SChris Wilson }
162535aed2e6SChris Wilson 
162635aed2e6SChris Wilson /**
162735aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
162835aed2e6SChris Wilson  * @dev: drm device
162935aed2e6SChris Wilson  *
163035aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
163135aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
163235aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
163335aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
163435aed2e6SChris Wilson  * of a ring dump etc.).
163535aed2e6SChris Wilson  */
1636527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
163735aed2e6SChris Wilson {
163835aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1639b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1640b4519513SChris Wilson 	int i;
164135aed2e6SChris Wilson 
164235aed2e6SChris Wilson 	i915_capture_error_state(dev);
164335aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
16448a905236SJesse Barnes 
1645ba1234d1SBen Gamari 	if (wedged) {
1646f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1647f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
1648ba1234d1SBen Gamari 
164911ed50ecSBen Gamari 		/*
16501f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
16511f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
165211ed50ecSBen Gamari 		 */
1653b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1654b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
165511ed50ecSBen Gamari 	}
165611ed50ecSBen Gamari 
165799584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
16588a905236SJesse Barnes }
16598a905236SJesse Barnes 
166021ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
16614e5359cdSSimon Farnsworth {
16624e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
16634e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
16644e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
166505394f39SChris Wilson 	struct drm_i915_gem_object *obj;
16664e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
16674e5359cdSSimon Farnsworth 	unsigned long flags;
16684e5359cdSSimon Farnsworth 	bool stall_detected;
16694e5359cdSSimon Farnsworth 
16704e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
16714e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
16724e5359cdSSimon Farnsworth 		return;
16734e5359cdSSimon Farnsworth 
16744e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
16754e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
16764e5359cdSSimon Farnsworth 
1677e7d841caSChris Wilson 	if (work == NULL ||
1678e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1679e7d841caSChris Wilson 	    !work->enable_stall_check) {
16804e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
16814e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
16824e5359cdSSimon Farnsworth 		return;
16834e5359cdSSimon Farnsworth 	}
16844e5359cdSSimon Farnsworth 
16854e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
168605394f39SChris Wilson 	obj = work->pending_flip_obj;
1687a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
16889db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1689446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1690446f2545SArmin Reese 					obj->gtt_offset;
16914e5359cdSSimon Farnsworth 	} else {
16929db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
169305394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
169401f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
16954e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
16964e5359cdSSimon Farnsworth 	}
16974e5359cdSSimon Farnsworth 
16984e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
16994e5359cdSSimon Farnsworth 
17004e5359cdSSimon Farnsworth 	if (stall_detected) {
17014e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
17024e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
17034e5359cdSSimon Farnsworth 	}
17044e5359cdSSimon Farnsworth }
17054e5359cdSSimon Farnsworth 
170642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
170742f52ef8SKeith Packard  * we use as a pipe index
170842f52ef8SKeith Packard  */
1709f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
17100a3e67a4SJesse Barnes {
17110a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1712e9d21d7fSKeith Packard 	unsigned long irqflags;
171371e0ffa5SJesse Barnes 
17145eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
171571e0ffa5SJesse Barnes 		return -EINVAL;
17160a3e67a4SJesse Barnes 
17171ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1718f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
17197c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
17207c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
17210a3e67a4SJesse Barnes 	else
17227c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
17237c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
17248692d00eSChris Wilson 
17258692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
17268692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
17276b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
17281ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17298692d00eSChris Wilson 
17300a3e67a4SJesse Barnes 	return 0;
17310a3e67a4SJesse Barnes }
17320a3e67a4SJesse Barnes 
1733f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1734f796cf8fSJesse Barnes {
1735f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1736f796cf8fSJesse Barnes 	unsigned long irqflags;
1737f796cf8fSJesse Barnes 
1738f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1739f796cf8fSJesse Barnes 		return -EINVAL;
1740f796cf8fSJesse Barnes 
1741f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1742f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1743f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1744f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1745f796cf8fSJesse Barnes 
1746f796cf8fSJesse Barnes 	return 0;
1747f796cf8fSJesse Barnes }
1748f796cf8fSJesse Barnes 
1749f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1750b1f14ad0SJesse Barnes {
1751b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1752b1f14ad0SJesse Barnes 	unsigned long irqflags;
1753b1f14ad0SJesse Barnes 
1754b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1755b1f14ad0SJesse Barnes 		return -EINVAL;
1756b1f14ad0SJesse Barnes 
1757b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1758b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
1759b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
1760b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1761b1f14ad0SJesse Barnes 
1762b1f14ad0SJesse Barnes 	return 0;
1763b1f14ad0SJesse Barnes }
1764b1f14ad0SJesse Barnes 
17657e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
17667e231dbeSJesse Barnes {
17677e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17687e231dbeSJesse Barnes 	unsigned long irqflags;
176931acc7f5SJesse Barnes 	u32 imr;
17707e231dbeSJesse Barnes 
17717e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
17727e231dbeSJesse Barnes 		return -EINVAL;
17737e231dbeSJesse Barnes 
17747e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17757e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
177631acc7f5SJesse Barnes 	if (pipe == 0)
17777e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
177831acc7f5SJesse Barnes 	else
17797e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
17807e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
178131acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
178231acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
17837e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17847e231dbeSJesse Barnes 
17857e231dbeSJesse Barnes 	return 0;
17867e231dbeSJesse Barnes }
17877e231dbeSJesse Barnes 
178842f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
178942f52ef8SKeith Packard  * we use as a pipe index
179042f52ef8SKeith Packard  */
1791f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
17920a3e67a4SJesse Barnes {
17930a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1794e9d21d7fSKeith Packard 	unsigned long irqflags;
17950a3e67a4SJesse Barnes 
17961ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17978692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
17986b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
17998692d00eSChris Wilson 
18007c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
18017c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
18027c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
18031ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
18040a3e67a4SJesse Barnes }
18050a3e67a4SJesse Barnes 
1806f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1807f796cf8fSJesse Barnes {
1808f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1809f796cf8fSJesse Barnes 	unsigned long irqflags;
1810f796cf8fSJesse Barnes 
1811f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1812f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1813f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1814f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1815f796cf8fSJesse Barnes }
1816f796cf8fSJesse Barnes 
1817f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1818b1f14ad0SJesse Barnes {
1819b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1820b1f14ad0SJesse Barnes 	unsigned long irqflags;
1821b1f14ad0SJesse Barnes 
1822b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1823b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
1824b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
1825b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1826b1f14ad0SJesse Barnes }
1827b1f14ad0SJesse Barnes 
18287e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
18297e231dbeSJesse Barnes {
18307e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18317e231dbeSJesse Barnes 	unsigned long irqflags;
183231acc7f5SJesse Barnes 	u32 imr;
18337e231dbeSJesse Barnes 
18347e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
183531acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
183631acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
18377e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
183831acc7f5SJesse Barnes 	if (pipe == 0)
18397e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
184031acc7f5SJesse Barnes 	else
18417e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
18427e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
18437e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
18447e231dbeSJesse Barnes }
18457e231dbeSJesse Barnes 
1846893eead0SChris Wilson static u32
1847893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1848852835f3SZou Nan hai {
1849893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1850893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1851893eead0SChris Wilson }
1852893eead0SChris Wilson 
1853893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1854893eead0SChris Wilson {
1855893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1856b2eadbc8SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring, false),
1857b2eadbc8SChris Wilson 			      ring_last_seqno(ring))) {
1858893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
18599574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
18609574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
18619574b3feSBen Widawsky 				  ring->name);
1862893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1863893eead0SChris Wilson 			*err = true;
1864893eead0SChris Wilson 		}
1865893eead0SChris Wilson 		return true;
1866893eead0SChris Wilson 	}
1867893eead0SChris Wilson 	return false;
1868f65d9421SBen Gamari }
1869f65d9421SBen Gamari 
1870a24a11e6SChris Wilson static bool semaphore_passed(struct intel_ring_buffer *ring)
1871a24a11e6SChris Wilson {
1872a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1873a24a11e6SChris Wilson 	u32 acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
1874a24a11e6SChris Wilson 	struct intel_ring_buffer *signaller;
1875a24a11e6SChris Wilson 	u32 cmd, ipehr, acthd_min;
1876a24a11e6SChris Wilson 
1877a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
1878a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
1879a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
1880a24a11e6SChris Wilson 		return false;
1881a24a11e6SChris Wilson 
1882a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
1883a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
1884a24a11e6SChris Wilson 	 */
1885a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
1886a24a11e6SChris Wilson 	do {
1887a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
1888a24a11e6SChris Wilson 		if (cmd == ipehr)
1889a24a11e6SChris Wilson 			break;
1890a24a11e6SChris Wilson 
1891a24a11e6SChris Wilson 		acthd -= 4;
1892a24a11e6SChris Wilson 		if (acthd < acthd_min)
1893a24a11e6SChris Wilson 			return false;
1894a24a11e6SChris Wilson 	} while (1);
1895a24a11e6SChris Wilson 
1896a24a11e6SChris Wilson 	signaller = &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
1897a24a11e6SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false),
1898a24a11e6SChris Wilson 				 ioread32(ring->virtual_start+acthd+4)+1);
1899a24a11e6SChris Wilson }
1900a24a11e6SChris Wilson 
19011ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
19021ec14ad3SChris Wilson {
19031ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
19041ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
19051ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
19061ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
19071ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
19081ec14ad3SChris Wilson 			  ring->name);
19091ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
19101ec14ad3SChris Wilson 		return true;
19111ec14ad3SChris Wilson 	}
1912a24a11e6SChris Wilson 
1913a24a11e6SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 &&
1914a24a11e6SChris Wilson 	    tmp & RING_WAIT_SEMAPHORE &&
1915a24a11e6SChris Wilson 	    semaphore_passed(ring)) {
1916a24a11e6SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
1917a24a11e6SChris Wilson 			  ring->name);
1918a24a11e6SChris Wilson 		I915_WRITE_CTL(ring, tmp);
1919a24a11e6SChris Wilson 		return true;
1920a24a11e6SChris Wilson 	}
19211ec14ad3SChris Wilson 	return false;
19221ec14ad3SChris Wilson }
19231ec14ad3SChris Wilson 
1924d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
1925d1e61e7fSChris Wilson {
1926d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1927d1e61e7fSChris Wilson 
192899584db3SDaniel Vetter 	if (dev_priv->gpu_error.hangcheck_count++ > 1) {
1929b4519513SChris Wilson 		bool hung = true;
1930b4519513SChris Wilson 
1931d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1932d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
1933d1e61e7fSChris Wilson 
1934d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
1935b4519513SChris Wilson 			struct intel_ring_buffer *ring;
1936b4519513SChris Wilson 			int i;
1937b4519513SChris Wilson 
1938d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
1939d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
1940d1e61e7fSChris Wilson 			 * and break the hang. This should work on
1941d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
1942d1e61e7fSChris Wilson 			 */
1943b4519513SChris Wilson 			for_each_ring(ring, dev_priv, i)
1944b4519513SChris Wilson 				hung &= !kick_ring(ring);
1945d1e61e7fSChris Wilson 		}
1946d1e61e7fSChris Wilson 
1947b4519513SChris Wilson 		return hung;
1948d1e61e7fSChris Wilson 	}
1949d1e61e7fSChris Wilson 
1950d1e61e7fSChris Wilson 	return false;
1951d1e61e7fSChris Wilson }
1952d1e61e7fSChris Wilson 
1953f65d9421SBen Gamari /**
1954f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1955f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1956f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1957f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1958f65d9421SBen Gamari  */
1959f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1960f65d9421SBen Gamari {
1961f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1962f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1963bd9854f9SBen Widawsky 	uint32_t acthd[I915_NUM_RINGS], instdone[I915_NUM_INSTDONE_REG];
1964b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1965b4519513SChris Wilson 	bool err = false, idle;
1966b4519513SChris Wilson 	int i;
1967893eead0SChris Wilson 
19683e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
19693e0dc6b0SBen Widawsky 		return;
19703e0dc6b0SBen Widawsky 
1971b4519513SChris Wilson 	memset(acthd, 0, sizeof(acthd));
1972b4519513SChris Wilson 	idle = true;
1973b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
1974b4519513SChris Wilson 	    idle &= i915_hangcheck_ring_idle(ring, &err);
1975b4519513SChris Wilson 	    acthd[i] = intel_ring_get_active_head(ring);
1976b4519513SChris Wilson 	}
1977b4519513SChris Wilson 
1978893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
1979b4519513SChris Wilson 	if (idle) {
1980d1e61e7fSChris Wilson 		if (err) {
1981d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
1982d1e61e7fSChris Wilson 				return;
1983d1e61e7fSChris Wilson 
1984893eead0SChris Wilson 			goto repeat;
1985d1e61e7fSChris Wilson 		}
1986d1e61e7fSChris Wilson 
198799584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
1988893eead0SChris Wilson 		return;
1989893eead0SChris Wilson 	}
1990f65d9421SBen Gamari 
1991bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
199299584db3SDaniel Vetter 	if (memcmp(dev_priv->gpu_error.last_acthd, acthd,
199399584db3SDaniel Vetter 		   sizeof(acthd)) == 0 &&
199499584db3SDaniel Vetter 	    memcmp(dev_priv->gpu_error.prev_instdone, instdone,
199599584db3SDaniel Vetter 		   sizeof(instdone)) == 0) {
1996d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
1997f65d9421SBen Gamari 			return;
1998cbb465e7SChris Wilson 	} else {
199999584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
2000cbb465e7SChris Wilson 
200199584db3SDaniel Vetter 		memcpy(dev_priv->gpu_error.last_acthd, acthd,
200299584db3SDaniel Vetter 		       sizeof(acthd));
200399584db3SDaniel Vetter 		memcpy(dev_priv->gpu_error.prev_instdone, instdone,
200499584db3SDaniel Vetter 		       sizeof(instdone));
2005cbb465e7SChris Wilson 	}
2006f65d9421SBen Gamari 
2007893eead0SChris Wilson repeat:
2008f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
200999584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
2010cecc21feSChris Wilson 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2011f65d9421SBen Gamari }
2012f65d9421SBen Gamari 
2013c0e09200SDave Airlie /* drm_dma.h hooks
2014c0e09200SDave Airlie */
2015f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2016036a4a7dSZhenyu Wang {
2017036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2018036a4a7dSZhenyu Wang 
20194697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
20204697995bSJesse Barnes 
2021036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2022bdfcdb63SDaniel Vetter 
2023036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
2024036a4a7dSZhenyu Wang 
2025036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2026036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
20273143a2bfSChris Wilson 	POSTING_READ(DEIER);
2028036a4a7dSZhenyu Wang 
2029036a4a7dSZhenyu Wang 	/* and GT */
2030036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2031036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
20323143a2bfSChris Wilson 	POSTING_READ(GTIER);
2033c650156aSZhenyu Wang 
2034ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2035ab5c608bSBen Widawsky 		return;
2036ab5c608bSBen Widawsky 
2037c650156aSZhenyu Wang 	/* south display irq */
2038c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
203982a28bcfSDaniel Vetter 	/*
204082a28bcfSDaniel Vetter 	 * SDEIER is also touched by the interrupt handler to work around missed
204182a28bcfSDaniel Vetter 	 * PCH interrupts. Hence we can't update it after the interrupt handler
204282a28bcfSDaniel Vetter 	 * is enabled - instead we unconditionally enable all PCH interrupt
204382a28bcfSDaniel Vetter 	 * sources here, but then only unmask them as needed with SDEIMR.
204482a28bcfSDaniel Vetter 	 */
204582a28bcfSDaniel Vetter 	I915_WRITE(SDEIER, 0xffffffff);
20463143a2bfSChris Wilson 	POSTING_READ(SDEIER);
2047036a4a7dSZhenyu Wang }
2048036a4a7dSZhenyu Wang 
20497e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
20507e231dbeSJesse Barnes {
20517e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20527e231dbeSJesse Barnes 	int pipe;
20537e231dbeSJesse Barnes 
20547e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
20557e231dbeSJesse Barnes 
20567e231dbeSJesse Barnes 	/* VLV magic */
20577e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
20587e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
20597e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
20607e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
20617e231dbeSJesse Barnes 
20627e231dbeSJesse Barnes 	/* and GT */
20637e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
20647e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
20657e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
20667e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
20677e231dbeSJesse Barnes 	POSTING_READ(GTIER);
20687e231dbeSJesse Barnes 
20697e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
20707e231dbeSJesse Barnes 
20717e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
20727e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
20737e231dbeSJesse Barnes 	for_each_pipe(pipe)
20747e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
20757e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20767e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
20777e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
20787e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
20797e231dbeSJesse Barnes }
20807e231dbeSJesse Barnes 
208182a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
208282a28bcfSDaniel Vetter {
208382a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
208482a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
208582a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
208682a28bcfSDaniel Vetter 	u32 mask = ~I915_READ(SDEIMR);
208782a28bcfSDaniel Vetter 	u32 hotplug;
208882a28bcfSDaniel Vetter 
208982a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
209082a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
209182a28bcfSDaniel Vetter 			mask |= hpd_ibx[intel_encoder->hpd_pin];
209282a28bcfSDaniel Vetter 	} else {
209382a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
209482a28bcfSDaniel Vetter 			mask |= hpd_cpt[intel_encoder->hpd_pin];
209582a28bcfSDaniel Vetter 	}
209682a28bcfSDaniel Vetter 
209782a28bcfSDaniel Vetter 	I915_WRITE(SDEIMR, ~mask);
209882a28bcfSDaniel Vetter 
20997fe0b973SKeith Packard 	/*
21007fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
21017fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
21027fe0b973SKeith Packard 	 *
21037fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
21047fe0b973SKeith Packard 	 */
21057fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
21067fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
21077fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
21087fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
21097fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
21107fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
21117fe0b973SKeith Packard }
21127fe0b973SKeith Packard 
2113d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2114d46da437SPaulo Zanoni {
2115d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
211682a28bcfSDaniel Vetter 	u32 mask;
2117d46da437SPaulo Zanoni 
211882a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev))
211982a28bcfSDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK;
212082a28bcfSDaniel Vetter 	else
212182a28bcfSDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
2122ab5c608bSBen Widawsky 
2123ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2124ab5c608bSBen Widawsky 		return;
2125ab5c608bSBen Widawsky 
2126d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2127d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2128d46da437SPaulo Zanoni }
2129d46da437SPaulo Zanoni 
2130f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2131036a4a7dSZhenyu Wang {
2132036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2133036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
2134013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2135ce99c256SDaniel Vetter 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
2136ce99c256SDaniel Vetter 			   DE_AUX_CHANNEL_A;
21371ec14ad3SChris Wilson 	u32 render_irqs;
2138036a4a7dSZhenyu Wang 
21391ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2140036a4a7dSZhenyu Wang 
2141036a4a7dSZhenyu Wang 	/* should always can generate irq */
2142036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
21431ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
21441ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
21453143a2bfSChris Wilson 	POSTING_READ(DEIER);
2146036a4a7dSZhenyu Wang 
21471ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
2148036a4a7dSZhenyu Wang 
2149036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
21501ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2151881f47b6SXiang, Haihao 
21521ec14ad3SChris Wilson 	if (IS_GEN6(dev))
21531ec14ad3SChris Wilson 		render_irqs =
21541ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
2155e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
2156e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
21571ec14ad3SChris Wilson 	else
21581ec14ad3SChris Wilson 		render_irqs =
215988f23b8fSChris Wilson 			GT_USER_INTERRUPT |
2160c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
21611ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
21621ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
21633143a2bfSChris Wilson 	POSTING_READ(GTIER);
2164036a4a7dSZhenyu Wang 
2165d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
21667fe0b973SKeith Packard 
2167f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
2168f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
2169f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
2170f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
2171f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2172f97108d1SJesse Barnes 	}
2173f97108d1SJesse Barnes 
2174036a4a7dSZhenyu Wang 	return 0;
2175036a4a7dSZhenyu Wang }
2176036a4a7dSZhenyu Wang 
2177f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2178b1f14ad0SJesse Barnes {
2179b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2180b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2181b615b57aSChris Wilson 	u32 display_mask =
2182b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2183b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
2184b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
2185ce99c256SDaniel Vetter 		DE_PLANEA_FLIP_DONE_IVB |
2186ce99c256SDaniel Vetter 		DE_AUX_CHANNEL_A_IVB;
2187b1f14ad0SJesse Barnes 	u32 render_irqs;
2188b1f14ad0SJesse Barnes 
2189b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2190b1f14ad0SJesse Barnes 
2191b1f14ad0SJesse Barnes 	/* should always can generate irq */
2192b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2193b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2194b615b57aSChris Wilson 	I915_WRITE(DEIER,
2195b615b57aSChris Wilson 		   display_mask |
2196b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
2197b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
2198b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
2199b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2200b1f14ad0SJesse Barnes 
220115b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2202b1f14ad0SJesse Barnes 
2203b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2204b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2205b1f14ad0SJesse Barnes 
2206e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
220715b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2208b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
2209b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2210b1f14ad0SJesse Barnes 
2211d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
22127fe0b973SKeith Packard 
2213b1f14ad0SJesse Barnes 	return 0;
2214b1f14ad0SJesse Barnes }
2215b1f14ad0SJesse Barnes 
22167e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
22177e231dbeSJesse Barnes {
22187e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22197e231dbeSJesse Barnes 	u32 enable_mask;
222031acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
22213bcedbe5SJesse Barnes 	u32 render_irqs;
22227e231dbeSJesse Barnes 	u16 msid;
22237e231dbeSJesse Barnes 
22247e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
222531acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
222631acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
222731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
22287e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22297e231dbeSJesse Barnes 
223031acc7f5SJesse Barnes 	/*
223131acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
223231acc7f5SJesse Barnes 	 * toggle them based on usage.
223331acc7f5SJesse Barnes 	 */
223431acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
223531acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
223631acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22377e231dbeSJesse Barnes 
22387e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
22397e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
22407e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
22417e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
22427e231dbeSJesse Barnes 	msid |= (1<<14);
22437e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
22447e231dbeSJesse Barnes 
224520afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
224620afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
224720afbda2SDaniel Vetter 
22487e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
22497e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
22507e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
22517e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
22527e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
22537e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
22547e231dbeSJesse Barnes 
225531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2256515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
225731acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
225831acc7f5SJesse Barnes 
22597e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
22607e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
22617e231dbeSJesse Barnes 
226231acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
226331acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
22643bcedbe5SJesse Barnes 
22653bcedbe5SJesse Barnes 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
22663bcedbe5SJesse Barnes 		GEN6_BLITTER_USER_INTERRUPT;
22673bcedbe5SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
22687e231dbeSJesse Barnes 	POSTING_READ(GTIER);
22697e231dbeSJesse Barnes 
22707e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
22717e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
22727e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
22737e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
22747e231dbeSJesse Barnes #endif
22757e231dbeSJesse Barnes 
22767e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
227720afbda2SDaniel Vetter 
227820afbda2SDaniel Vetter 	return 0;
227920afbda2SDaniel Vetter }
228020afbda2SDaniel Vetter 
22817e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
22827e231dbeSJesse Barnes {
22837e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22847e231dbeSJesse Barnes 	int pipe;
22857e231dbeSJesse Barnes 
22867e231dbeSJesse Barnes 	if (!dev_priv)
22877e231dbeSJesse Barnes 		return;
22887e231dbeSJesse Barnes 
22897e231dbeSJesse Barnes 	for_each_pipe(pipe)
22907e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
22917e231dbeSJesse Barnes 
22927e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
22937e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
22947e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
22957e231dbeSJesse Barnes 	for_each_pipe(pipe)
22967e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
22977e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
22987e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
22997e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
23007e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
23017e231dbeSJesse Barnes }
23027e231dbeSJesse Barnes 
2303f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2304036a4a7dSZhenyu Wang {
2305036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23064697995bSJesse Barnes 
23074697995bSJesse Barnes 	if (!dev_priv)
23084697995bSJesse Barnes 		return;
23094697995bSJesse Barnes 
2310036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2311036a4a7dSZhenyu Wang 
2312036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2313036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2314036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2315036a4a7dSZhenyu Wang 
2316036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2317036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2318036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2319192aac1fSKeith Packard 
2320ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
2321ab5c608bSBen Widawsky 		return;
2322ab5c608bSBen Widawsky 
2323192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2324192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2325192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2326036a4a7dSZhenyu Wang }
2327036a4a7dSZhenyu Wang 
2328c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2329c2798b19SChris Wilson {
2330c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2331c2798b19SChris Wilson 	int pipe;
2332c2798b19SChris Wilson 
2333c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2334c2798b19SChris Wilson 
2335c2798b19SChris Wilson 	for_each_pipe(pipe)
2336c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2337c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2338c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2339c2798b19SChris Wilson 	POSTING_READ16(IER);
2340c2798b19SChris Wilson }
2341c2798b19SChris Wilson 
2342c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2343c2798b19SChris Wilson {
2344c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2345c2798b19SChris Wilson 
2346c2798b19SChris Wilson 	I915_WRITE16(EMR,
2347c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2348c2798b19SChris Wilson 
2349c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2350c2798b19SChris Wilson 	dev_priv->irq_mask =
2351c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2352c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2353c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2354c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2355c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2356c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2357c2798b19SChris Wilson 
2358c2798b19SChris Wilson 	I915_WRITE16(IER,
2359c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2360c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2361c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2362c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2363c2798b19SChris Wilson 	POSTING_READ16(IER);
2364c2798b19SChris Wilson 
2365c2798b19SChris Wilson 	return 0;
2366c2798b19SChris Wilson }
2367c2798b19SChris Wilson 
236890a72f87SVille Syrjälä /*
236990a72f87SVille Syrjälä  * Returns true when a page flip has completed.
237090a72f87SVille Syrjälä  */
237190a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
237290a72f87SVille Syrjälä 			       int pipe, u16 iir)
237390a72f87SVille Syrjälä {
237490a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
237590a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
237690a72f87SVille Syrjälä 
237790a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
237890a72f87SVille Syrjälä 		return false;
237990a72f87SVille Syrjälä 
238090a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
238190a72f87SVille Syrjälä 		return false;
238290a72f87SVille Syrjälä 
238390a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
238490a72f87SVille Syrjälä 
238590a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
238690a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
238790a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
238890a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
238990a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
239090a72f87SVille Syrjälä 	 */
239190a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
239290a72f87SVille Syrjälä 		return false;
239390a72f87SVille Syrjälä 
239490a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
239590a72f87SVille Syrjälä 
239690a72f87SVille Syrjälä 	return true;
239790a72f87SVille Syrjälä }
239890a72f87SVille Syrjälä 
2399ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2400c2798b19SChris Wilson {
2401c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2402c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2403c2798b19SChris Wilson 	u16 iir, new_iir;
2404c2798b19SChris Wilson 	u32 pipe_stats[2];
2405c2798b19SChris Wilson 	unsigned long irqflags;
2406c2798b19SChris Wilson 	int irq_received;
2407c2798b19SChris Wilson 	int pipe;
2408c2798b19SChris Wilson 	u16 flip_mask =
2409c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2410c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2411c2798b19SChris Wilson 
2412c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2413c2798b19SChris Wilson 
2414c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2415c2798b19SChris Wilson 	if (iir == 0)
2416c2798b19SChris Wilson 		return IRQ_NONE;
2417c2798b19SChris Wilson 
2418c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2419c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2420c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2421c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2422c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2423c2798b19SChris Wilson 		 */
2424c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2425c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2426c2798b19SChris Wilson 			i915_handle_error(dev, false);
2427c2798b19SChris Wilson 
2428c2798b19SChris Wilson 		for_each_pipe(pipe) {
2429c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2430c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2431c2798b19SChris Wilson 
2432c2798b19SChris Wilson 			/*
2433c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2434c2798b19SChris Wilson 			 */
2435c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2436c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2437c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2438c2798b19SChris Wilson 							 pipe_name(pipe));
2439c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2440c2798b19SChris Wilson 				irq_received = 1;
2441c2798b19SChris Wilson 			}
2442c2798b19SChris Wilson 		}
2443c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2444c2798b19SChris Wilson 
2445c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2446c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2447c2798b19SChris Wilson 
2448d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2449c2798b19SChris Wilson 
2450c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2451c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2452c2798b19SChris Wilson 
2453c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
245490a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
245590a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2456c2798b19SChris Wilson 
2457c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
245890a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
245990a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2460c2798b19SChris Wilson 
2461c2798b19SChris Wilson 		iir = new_iir;
2462c2798b19SChris Wilson 	}
2463c2798b19SChris Wilson 
2464c2798b19SChris Wilson 	return IRQ_HANDLED;
2465c2798b19SChris Wilson }
2466c2798b19SChris Wilson 
2467c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2468c2798b19SChris Wilson {
2469c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2470c2798b19SChris Wilson 	int pipe;
2471c2798b19SChris Wilson 
2472c2798b19SChris Wilson 	for_each_pipe(pipe) {
2473c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2474c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2475c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2476c2798b19SChris Wilson 	}
2477c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2478c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2479c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2480c2798b19SChris Wilson }
2481c2798b19SChris Wilson 
2482a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2483a266c7d5SChris Wilson {
2484a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2485a266c7d5SChris Wilson 	int pipe;
2486a266c7d5SChris Wilson 
2487a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2488a266c7d5SChris Wilson 
2489a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2490a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2491a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2492a266c7d5SChris Wilson 	}
2493a266c7d5SChris Wilson 
249400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2495a266c7d5SChris Wilson 	for_each_pipe(pipe)
2496a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2497a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2498a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2499a266c7d5SChris Wilson 	POSTING_READ(IER);
2500a266c7d5SChris Wilson }
2501a266c7d5SChris Wilson 
2502a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2503a266c7d5SChris Wilson {
2504a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
250538bde180SChris Wilson 	u32 enable_mask;
2506a266c7d5SChris Wilson 
250738bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
250838bde180SChris Wilson 
250938bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
251038bde180SChris Wilson 	dev_priv->irq_mask =
251138bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
251238bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
251338bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
251438bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
251538bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
251638bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
251738bde180SChris Wilson 
251838bde180SChris Wilson 	enable_mask =
251938bde180SChris Wilson 		I915_ASLE_INTERRUPT |
252038bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
252138bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
252238bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
252338bde180SChris Wilson 		I915_USER_INTERRUPT;
252438bde180SChris Wilson 
2525a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
252620afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
252720afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
252820afbda2SDaniel Vetter 
2529a266c7d5SChris Wilson 		/* Enable in IER... */
2530a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2531a266c7d5SChris Wilson 		/* and unmask in IMR */
2532a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2533a266c7d5SChris Wilson 	}
2534a266c7d5SChris Wilson 
2535a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2536a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2537a266c7d5SChris Wilson 	POSTING_READ(IER);
2538a266c7d5SChris Wilson 
253920afbda2SDaniel Vetter 	intel_opregion_enable_asle(dev);
254020afbda2SDaniel Vetter 
254120afbda2SDaniel Vetter 	return 0;
254220afbda2SDaniel Vetter }
254320afbda2SDaniel Vetter 
254490a72f87SVille Syrjälä /*
254590a72f87SVille Syrjälä  * Returns true when a page flip has completed.
254690a72f87SVille Syrjälä  */
254790a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
254890a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
254990a72f87SVille Syrjälä {
255090a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
255190a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
255290a72f87SVille Syrjälä 
255390a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
255490a72f87SVille Syrjälä 		return false;
255590a72f87SVille Syrjälä 
255690a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
255790a72f87SVille Syrjälä 		return false;
255890a72f87SVille Syrjälä 
255990a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
256090a72f87SVille Syrjälä 
256190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
256290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
256390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
256490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
256590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
256690a72f87SVille Syrjälä 	 */
256790a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
256890a72f87SVille Syrjälä 		return false;
256990a72f87SVille Syrjälä 
257090a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
257190a72f87SVille Syrjälä 
257290a72f87SVille Syrjälä 	return true;
257390a72f87SVille Syrjälä }
257490a72f87SVille Syrjälä 
2575ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2576a266c7d5SChris Wilson {
2577a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2578a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25798291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2580a266c7d5SChris Wilson 	unsigned long irqflags;
258138bde180SChris Wilson 	u32 flip_mask =
258238bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
258338bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
258438bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2585a266c7d5SChris Wilson 
2586a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2587a266c7d5SChris Wilson 
2588a266c7d5SChris Wilson 	iir = I915_READ(IIR);
258938bde180SChris Wilson 	do {
259038bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
25918291ee90SChris Wilson 		bool blc_event = false;
2592a266c7d5SChris Wilson 
2593a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2594a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2595a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2596a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2597a266c7d5SChris Wilson 		 */
2598a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2599a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2600a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2601a266c7d5SChris Wilson 
2602a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2603a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2604a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2605a266c7d5SChris Wilson 
260638bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2607a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2608a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2609a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2610a266c7d5SChris Wilson 							 pipe_name(pipe));
2611a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
261238bde180SChris Wilson 				irq_received = true;
2613a266c7d5SChris Wilson 			}
2614a266c7d5SChris Wilson 		}
2615a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2616a266c7d5SChris Wilson 
2617a266c7d5SChris Wilson 		if (!irq_received)
2618a266c7d5SChris Wilson 			break;
2619a266c7d5SChris Wilson 
2620a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2621a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2622a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2623a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2624a266c7d5SChris Wilson 
2625a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2626a266c7d5SChris Wilson 				  hotplug_status);
2627e5868a31SEgbert Eich 			if (hotplug_status & HOTPLUG_INT_STATUS_I915)
2628a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2629a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2630a266c7d5SChris Wilson 
2631a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
263238bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2633a266c7d5SChris Wilson 		}
2634a266c7d5SChris Wilson 
263538bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2636a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2637a266c7d5SChris Wilson 
2638a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2639a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2640a266c7d5SChris Wilson 
2641a266c7d5SChris Wilson 		for_each_pipe(pipe) {
264238bde180SChris Wilson 			int plane = pipe;
264338bde180SChris Wilson 			if (IS_MOBILE(dev))
264438bde180SChris Wilson 				plane = !plane;
26455e2032d4SVille Syrjälä 
264690a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
264790a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
264890a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
2649a266c7d5SChris Wilson 
2650a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2651a266c7d5SChris Wilson 				blc_event = true;
2652a266c7d5SChris Wilson 		}
2653a266c7d5SChris Wilson 
2654a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2655a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2656a266c7d5SChris Wilson 
2657a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2658a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2659a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2660a266c7d5SChris Wilson 		 * we would never get another interrupt.
2661a266c7d5SChris Wilson 		 *
2662a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2663a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2664a266c7d5SChris Wilson 		 * another one.
2665a266c7d5SChris Wilson 		 *
2666a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2667a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2668a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2669a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2670a266c7d5SChris Wilson 		 * stray interrupts.
2671a266c7d5SChris Wilson 		 */
267238bde180SChris Wilson 		ret = IRQ_HANDLED;
2673a266c7d5SChris Wilson 		iir = new_iir;
267438bde180SChris Wilson 	} while (iir & ~flip_mask);
2675a266c7d5SChris Wilson 
2676d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
26778291ee90SChris Wilson 
2678a266c7d5SChris Wilson 	return ret;
2679a266c7d5SChris Wilson }
2680a266c7d5SChris Wilson 
2681a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2682a266c7d5SChris Wilson {
2683a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2684a266c7d5SChris Wilson 	int pipe;
2685a266c7d5SChris Wilson 
2686a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2687a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2688a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2689a266c7d5SChris Wilson 	}
2690a266c7d5SChris Wilson 
269100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
269255b39755SChris Wilson 	for_each_pipe(pipe) {
269355b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2694a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
269555b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
269655b39755SChris Wilson 	}
2697a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2698a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2699a266c7d5SChris Wilson 
2700a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2701a266c7d5SChris Wilson }
2702a266c7d5SChris Wilson 
2703a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2704a266c7d5SChris Wilson {
2705a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2706a266c7d5SChris Wilson 	int pipe;
2707a266c7d5SChris Wilson 
2708a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2709a266c7d5SChris Wilson 
2710a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2711a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2712a266c7d5SChris Wilson 
2713a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2714a266c7d5SChris Wilson 	for_each_pipe(pipe)
2715a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2716a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2717a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2718a266c7d5SChris Wilson 	POSTING_READ(IER);
2719a266c7d5SChris Wilson }
2720a266c7d5SChris Wilson 
2721a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2722a266c7d5SChris Wilson {
2723a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2724bbba0a97SChris Wilson 	u32 enable_mask;
2725a266c7d5SChris Wilson 	u32 error_mask;
2726a266c7d5SChris Wilson 
2727a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2728bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2729adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2730bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2731bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2732bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2733bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2734bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2735bbba0a97SChris Wilson 
2736bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
273721ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
273821ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
2739bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2740bbba0a97SChris Wilson 
2741bbba0a97SChris Wilson 	if (IS_G4X(dev))
2742bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2743a266c7d5SChris Wilson 
2744515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
2745a266c7d5SChris Wilson 
2746a266c7d5SChris Wilson 	/*
2747a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2748a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2749a266c7d5SChris Wilson 	 */
2750a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2751a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2752a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2753a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2754a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2755a266c7d5SChris Wilson 	} else {
2756a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2757a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2758a266c7d5SChris Wilson 	}
2759a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2760a266c7d5SChris Wilson 
2761a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2762a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2763a266c7d5SChris Wilson 	POSTING_READ(IER);
2764a266c7d5SChris Wilson 
276520afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
276620afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
276720afbda2SDaniel Vetter 
276820afbda2SDaniel Vetter 	intel_opregion_enable_asle(dev);
276920afbda2SDaniel Vetter 
277020afbda2SDaniel Vetter 	return 0;
277120afbda2SDaniel Vetter }
277220afbda2SDaniel Vetter 
2773bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
277420afbda2SDaniel Vetter {
277520afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2776e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
2777e5868a31SEgbert Eich 	struct intel_encoder *encoder;
277820afbda2SDaniel Vetter 	u32 hotplug_en;
277920afbda2SDaniel Vetter 
2780bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
2781bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2782bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
2783adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
2784e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
2785bac56d5bSEgbert Eich 		list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
2786e5868a31SEgbert Eich 			hotplug_en |= hpd_mask_i915[encoder->hpd_pin];
2787a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2788a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2789a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2790a266c7d5SChris Wilson 		*/
2791a266c7d5SChris Wilson 		if (IS_G4X(dev))
2792a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
279385fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
2794a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2795a266c7d5SChris Wilson 
2796a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2797a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2798a266c7d5SChris Wilson 	}
2799bac56d5bSEgbert Eich }
2800a266c7d5SChris Wilson 
2801ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
2802a266c7d5SChris Wilson {
2803a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2804a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2805a266c7d5SChris Wilson 	u32 iir, new_iir;
2806a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2807a266c7d5SChris Wilson 	unsigned long irqflags;
2808a266c7d5SChris Wilson 	int irq_received;
2809a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
281021ad8330SVille Syrjälä 	u32 flip_mask =
281121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
281221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2813a266c7d5SChris Wilson 
2814a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2815a266c7d5SChris Wilson 
2816a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2817a266c7d5SChris Wilson 
2818a266c7d5SChris Wilson 	for (;;) {
28192c8ba29fSChris Wilson 		bool blc_event = false;
28202c8ba29fSChris Wilson 
282121ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
2822a266c7d5SChris Wilson 
2823a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2824a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2825a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2826a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2827a266c7d5SChris Wilson 		 */
2828a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2829a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2830a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2831a266c7d5SChris Wilson 
2832a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2833a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2834a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2835a266c7d5SChris Wilson 
2836a266c7d5SChris Wilson 			/*
2837a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2838a266c7d5SChris Wilson 			 */
2839a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2840a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2841a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2842a266c7d5SChris Wilson 							 pipe_name(pipe));
2843a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2844a266c7d5SChris Wilson 				irq_received = 1;
2845a266c7d5SChris Wilson 			}
2846a266c7d5SChris Wilson 		}
2847a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2848a266c7d5SChris Wilson 
2849a266c7d5SChris Wilson 		if (!irq_received)
2850a266c7d5SChris Wilson 			break;
2851a266c7d5SChris Wilson 
2852a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2853a266c7d5SChris Wilson 
2854a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2855adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2856a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2857a266c7d5SChris Wilson 
2858a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2859a266c7d5SChris Wilson 				  hotplug_status);
2860e5868a31SEgbert Eich 			if (hotplug_status & (IS_G4X(dev) ?
2861e5868a31SEgbert Eich 					      HOTPLUG_INT_STATUS_G4X :
2862e5868a31SEgbert Eich 					      HOTPLUG_INT_STATUS_I965))
2863a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2864a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2865a266c7d5SChris Wilson 
2866a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2867a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2868a266c7d5SChris Wilson 		}
2869a266c7d5SChris Wilson 
287021ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
2871a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2872a266c7d5SChris Wilson 
2873a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2874a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2875a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2876a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2877a266c7d5SChris Wilson 
2878a266c7d5SChris Wilson 		for_each_pipe(pipe) {
28792c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
288090a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
288190a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
2882a266c7d5SChris Wilson 
2883a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2884a266c7d5SChris Wilson 				blc_event = true;
2885a266c7d5SChris Wilson 		}
2886a266c7d5SChris Wilson 
2887a266c7d5SChris Wilson 
2888a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2889a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2890a266c7d5SChris Wilson 
2891515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
2892515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
2893515ac2bbSDaniel Vetter 
2894a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2895a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2896a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2897a266c7d5SChris Wilson 		 * we would never get another interrupt.
2898a266c7d5SChris Wilson 		 *
2899a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2900a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2901a266c7d5SChris Wilson 		 * another one.
2902a266c7d5SChris Wilson 		 *
2903a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2904a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2905a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2906a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2907a266c7d5SChris Wilson 		 * stray interrupts.
2908a266c7d5SChris Wilson 		 */
2909a266c7d5SChris Wilson 		iir = new_iir;
2910a266c7d5SChris Wilson 	}
2911a266c7d5SChris Wilson 
2912d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
29132c8ba29fSChris Wilson 
2914a266c7d5SChris Wilson 	return ret;
2915a266c7d5SChris Wilson }
2916a266c7d5SChris Wilson 
2917a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
2918a266c7d5SChris Wilson {
2919a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2920a266c7d5SChris Wilson 	int pipe;
2921a266c7d5SChris Wilson 
2922a266c7d5SChris Wilson 	if (!dev_priv)
2923a266c7d5SChris Wilson 		return;
2924a266c7d5SChris Wilson 
2925a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2926a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2927a266c7d5SChris Wilson 
2928a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
2929a266c7d5SChris Wilson 	for_each_pipe(pipe)
2930a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2931a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2932a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2933a266c7d5SChris Wilson 
2934a266c7d5SChris Wilson 	for_each_pipe(pipe)
2935a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
2936a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2937a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2938a266c7d5SChris Wilson }
2939a266c7d5SChris Wilson 
2940f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2941f71d4af4SJesse Barnes {
29428b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
29438b2e326dSChris Wilson 
29448b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
294599584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
2946c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
2947a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
29488b2e326dSChris Wilson 
294999584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
295099584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
295161bac78eSDaniel Vetter 		    (unsigned long) dev);
295261bac78eSDaniel Vetter 
295397a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
29549ee32feaSDaniel Vetter 
2955f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2956f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
29577d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
2958f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2959f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2960f71d4af4SJesse Barnes 	}
2961f71d4af4SJesse Barnes 
2962c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2963f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2964c3613de9SKeith Packard 	else
2965c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2966f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2967f71d4af4SJesse Barnes 
29687e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
29697e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
29707e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
29717e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
29727e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
29737e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
29747e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
2975fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
29764a06e201SDaniel Vetter 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2977f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2978f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2979f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2980f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2981f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2982f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2983f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
298482a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
2985f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2986f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2987f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2988f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2989f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2990f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2991f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
299282a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
2993f71d4af4SJesse Barnes 	} else {
2994c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
2995c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
2996c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
2997c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
2998c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
2999a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3000a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3001a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3002a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3003a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
300420afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3005c2798b19SChris Wilson 		} else {
3006a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3007a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3008a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3009a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3010bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3011c2798b19SChris Wilson 		}
3012f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3013f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3014f71d4af4SJesse Barnes 	}
3015f71d4af4SJesse Barnes }
301620afbda2SDaniel Vetter 
301720afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
301820afbda2SDaniel Vetter {
301920afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
302020afbda2SDaniel Vetter 
302120afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
302220afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
302320afbda2SDaniel Vetter }
3024