xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 0ff9800ade77160df35764f3fe15cd893605762b)
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 
104*0ff9800aSPaulo 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;
76144498aeaSPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
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). */
77644498aeaSPaulo Zanoni 	sde_ier = I915_READ(SDEIER);
77744498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, 0);
77844498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
77944498aeaSPaulo Zanoni 
7800e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
7810e43406bSChris Wilson 	if (gt_iir) {
7820e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
7830e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
7840e43406bSChris Wilson 		ret = IRQ_HANDLED;
7850e43406bSChris Wilson 	}
786b1f14ad0SJesse Barnes 
787b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
7880e43406bSChris Wilson 	if (de_iir) {
789ce99c256SDaniel Vetter 		if (de_iir & DE_AUX_CHANNEL_A_IVB)
790ce99c256SDaniel Vetter 			dp_aux_irq_handler(dev);
791ce99c256SDaniel Vetter 
792b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
793b1f14ad0SJesse Barnes 			intel_opregion_gse_intr(dev);
794b1f14ad0SJesse Barnes 
7950e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
79674d44445SDaniel Vetter 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
79774d44445SDaniel Vetter 				drm_handle_vblank(dev, i);
7980e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
7990e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
8000e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
801b1f14ad0SJesse Barnes 			}
802b1f14ad0SJesse Barnes 		}
803b1f14ad0SJesse Barnes 
804b1f14ad0SJesse Barnes 		/* check event from PCH */
805b1f14ad0SJesse Barnes 		if (de_iir & DE_PCH_EVENT_IVB) {
8060e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
8070e43406bSChris Wilson 
80823e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
8090e43406bSChris Wilson 
8100e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
8110e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
812b1f14ad0SJesse Barnes 		}
813b1f14ad0SJesse Barnes 
8140e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
8150e43406bSChris Wilson 		ret = IRQ_HANDLED;
8160e43406bSChris Wilson 	}
8170e43406bSChris Wilson 
8180e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
8190e43406bSChris Wilson 	if (pm_iir) {
820fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
821fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
822b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
8230e43406bSChris Wilson 		ret = IRQ_HANDLED;
8240e43406bSChris Wilson 	}
825b1f14ad0SJesse Barnes 
826b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
827b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
82844498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, sde_ier);
82944498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
830b1f14ad0SJesse Barnes 
831b1f14ad0SJesse Barnes 	return ret;
832b1f14ad0SJesse Barnes }
833b1f14ad0SJesse Barnes 
834e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
835e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
836e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
837e7b4c6b1SDaniel Vetter {
838e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
839e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
840e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
841e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
842e7b4c6b1SDaniel Vetter }
843e7b4c6b1SDaniel Vetter 
844ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
845036a4a7dSZhenyu Wang {
8464697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
847036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
848036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
84944498aeaSPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
850881f47b6SXiang, Haihao 
8514697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
8524697995bSJesse Barnes 
8532d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
8542d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
8552d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
8563143a2bfSChris Wilson 	POSTING_READ(DEIER);
8572d109a84SZou, Nanhai 
85844498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
85944498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
86044498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
86144498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
86244498aeaSPaulo Zanoni 	 * due to its back queue). */
86344498aeaSPaulo Zanoni 	sde_ier = I915_READ(SDEIER);
86444498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, 0);
86544498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
86644498aeaSPaulo Zanoni 
867036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
868036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
8693b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
870036a4a7dSZhenyu Wang 
871acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
872c7c85101SZou Nan hai 		goto done;
873036a4a7dSZhenyu Wang 
874036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
875036a4a7dSZhenyu Wang 
876e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
877e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
878e7b4c6b1SDaniel Vetter 	else
879e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
880036a4a7dSZhenyu Wang 
881ce99c256SDaniel Vetter 	if (de_iir & DE_AUX_CHANNEL_A)
882ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
883ce99c256SDaniel Vetter 
88401c66889SZhao Yakui 	if (de_iir & DE_GSE)
8853b617967SChris Wilson 		intel_opregion_gse_intr(dev);
88601c66889SZhao Yakui 
88774d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
88874d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
88974d44445SDaniel Vetter 
89074d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
89174d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
89274d44445SDaniel Vetter 
893f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
894013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
8952bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
896013d5aa2SJesse Barnes 	}
897013d5aa2SJesse Barnes 
898f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
899f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
9002bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
901013d5aa2SJesse Barnes 	}
902c062df61SLi Peng 
903c650156aSZhenyu Wang 	/* check event from PCH */
904776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
905acd15b6cSDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
906acd15b6cSDaniel Vetter 
90723e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
90823e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
90923e81d69SAdam Jackson 		else
91023e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
911acd15b6cSDaniel Vetter 
912acd15b6cSDaniel Vetter 		/* should clear PCH hotplug event before clear CPU irq */
913acd15b6cSDaniel Vetter 		I915_WRITE(SDEIIR, pch_iir);
914776ad806SJesse Barnes 	}
915c650156aSZhenyu Wang 
91673edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
91773edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
918f97108d1SJesse Barnes 
919fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
920fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
9213b8d8d91SJesse Barnes 
922c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
923c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
9244912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
925036a4a7dSZhenyu Wang 
926c7c85101SZou Nan hai done:
9272d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
9283143a2bfSChris Wilson 	POSTING_READ(DEIER);
92944498aeaSPaulo Zanoni 	I915_WRITE(SDEIER, sde_ier);
93044498aeaSPaulo Zanoni 	POSTING_READ(SDEIER);
9312d109a84SZou, Nanhai 
932036a4a7dSZhenyu Wang 	return ret;
933036a4a7dSZhenyu Wang }
934036a4a7dSZhenyu Wang 
9358a905236SJesse Barnes /**
9368a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
9378a905236SJesse Barnes  * @work: work struct
9388a905236SJesse Barnes  *
9398a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
9408a905236SJesse Barnes  * was detected.
9418a905236SJesse Barnes  */
9428a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
9438a905236SJesse Barnes {
9441f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
9451f83fee0SDaniel Vetter 						    work);
9461f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
9471f83fee0SDaniel Vetter 						    gpu_error);
9488a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
949f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
950f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
951f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
952f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
953f69061beSDaniel Vetter 	int i, ret;
9548a905236SJesse Barnes 
955f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
9568a905236SJesse Barnes 
9577db0ba24SDaniel Vetter 	/*
9587db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
9597db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
9607db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
9617db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
9627db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
9637db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
9647db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
9657db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
9667db0ba24SDaniel Vetter 	 */
9677db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
96844d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
9697db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
9707db0ba24SDaniel Vetter 				   reset_event);
9711f83fee0SDaniel Vetter 
972f69061beSDaniel Vetter 		ret = i915_reset(dev);
973f69061beSDaniel Vetter 
974f69061beSDaniel Vetter 		if (ret == 0) {
975f69061beSDaniel Vetter 			/*
976f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
977f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
978f69061beSDaniel Vetter 			 * complete.
979f69061beSDaniel Vetter 			 *
980f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
981f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
982f69061beSDaniel Vetter 			 * updates before
983f69061beSDaniel Vetter 			 * the counter increment.
984f69061beSDaniel Vetter 			 */
985f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
986f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
987f69061beSDaniel Vetter 
988f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
989f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
9901f83fee0SDaniel Vetter 		} else {
9911f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
992f316a42cSBen Gamari 		}
9931f83fee0SDaniel Vetter 
994f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
995f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
996f69061beSDaniel Vetter 
99796a02917SVille Syrjälä 		intel_display_handle_reset(dev);
99896a02917SVille Syrjälä 
9991f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
1000f316a42cSBen Gamari 	}
10018a905236SJesse Barnes }
10028a905236SJesse Barnes 
100385f9e50dSDaniel Vetter /* NB: please notice the memset */
100485f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
100585f9e50dSDaniel Vetter 				    uint32_t *instdone)
100685f9e50dSDaniel Vetter {
100785f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
100885f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
100985f9e50dSDaniel Vetter 
101085f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
101185f9e50dSDaniel Vetter 	case 2:
101285f9e50dSDaniel Vetter 	case 3:
101385f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
101485f9e50dSDaniel Vetter 		break;
101585f9e50dSDaniel Vetter 	case 4:
101685f9e50dSDaniel Vetter 	case 5:
101785f9e50dSDaniel Vetter 	case 6:
101885f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
101985f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
102085f9e50dSDaniel Vetter 		break;
102185f9e50dSDaniel Vetter 	default:
102285f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
102385f9e50dSDaniel Vetter 	case 7:
102485f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
102585f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
102685f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
102785f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
102885f9e50dSDaniel Vetter 		break;
102985f9e50dSDaniel Vetter 	}
103085f9e50dSDaniel Vetter }
103185f9e50dSDaniel Vetter 
10323bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
10339df30794SChris Wilson static struct drm_i915_error_object *
1034d0d045e8SBen Widawsky i915_error_object_create_sized(struct drm_i915_private *dev_priv,
1035d0d045e8SBen Widawsky 			       struct drm_i915_gem_object *src,
1036d0d045e8SBen Widawsky 			       const int num_pages)
10379df30794SChris Wilson {
10389df30794SChris Wilson 	struct drm_i915_error_object *dst;
1039d0d045e8SBen Widawsky 	int i;
1040e56660ddSChris Wilson 	u32 reloc_offset;
10419df30794SChris Wilson 
104205394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
10439df30794SChris Wilson 		return NULL;
10449df30794SChris Wilson 
1045d0d045e8SBen Widawsky 	dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
10469df30794SChris Wilson 	if (dst == NULL)
10479df30794SChris Wilson 		return NULL;
10489df30794SChris Wilson 
104905394f39SChris Wilson 	reloc_offset = src->gtt_offset;
1050d0d045e8SBen Widawsky 	for (i = 0; i < num_pages; i++) {
1051788885aeSAndrew Morton 		unsigned long flags;
1052e56660ddSChris Wilson 		void *d;
1053788885aeSAndrew Morton 
1054e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
10559df30794SChris Wilson 		if (d == NULL)
10569df30794SChris Wilson 			goto unwind;
1057e56660ddSChris Wilson 
1058788885aeSAndrew Morton 		local_irq_save(flags);
10595d4545aeSBen Widawsky 		if (reloc_offset < dev_priv->gtt.mappable_end &&
106074898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
1061172975aaSChris Wilson 			void __iomem *s;
1062172975aaSChris Wilson 
1063172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
1064172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
1065172975aaSChris Wilson 			 * captures what the GPU read.
1066172975aaSChris Wilson 			 */
1067172975aaSChris Wilson 
10685d4545aeSBen Widawsky 			s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
10693e4d3af5SPeter Zijlstra 						     reloc_offset);
1070e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
10713e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
1072960e3564SChris Wilson 		} else if (src->stolen) {
1073960e3564SChris Wilson 			unsigned long offset;
1074960e3564SChris Wilson 
1075960e3564SChris Wilson 			offset = dev_priv->mm.stolen_base;
1076960e3564SChris Wilson 			offset += src->stolen->start;
1077960e3564SChris Wilson 			offset += i << PAGE_SHIFT;
1078960e3564SChris Wilson 
10791a240d4dSDaniel Vetter 			memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
1080172975aaSChris Wilson 		} else {
10819da3da66SChris Wilson 			struct page *page;
1082172975aaSChris Wilson 			void *s;
1083172975aaSChris Wilson 
10849da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
1085172975aaSChris Wilson 
10869da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
10879da3da66SChris Wilson 
10889da3da66SChris Wilson 			s = kmap_atomic(page);
1089172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
1090172975aaSChris Wilson 			kunmap_atomic(s);
1091172975aaSChris Wilson 
10929da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
1093172975aaSChris Wilson 		}
1094788885aeSAndrew Morton 		local_irq_restore(flags);
1095e56660ddSChris Wilson 
10969da3da66SChris Wilson 		dst->pages[i] = d;
1097e56660ddSChris Wilson 
1098e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
10999df30794SChris Wilson 	}
1100d0d045e8SBen Widawsky 	dst->page_count = num_pages;
110105394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
11029df30794SChris Wilson 
11039df30794SChris Wilson 	return dst;
11049df30794SChris Wilson 
11059df30794SChris Wilson unwind:
11069da3da66SChris Wilson 	while (i--)
11079da3da66SChris Wilson 		kfree(dst->pages[i]);
11089df30794SChris Wilson 	kfree(dst);
11099df30794SChris Wilson 	return NULL;
11109df30794SChris Wilson }
1111d0d045e8SBen Widawsky #define i915_error_object_create(dev_priv, src) \
1112d0d045e8SBen Widawsky 	i915_error_object_create_sized((dev_priv), (src), \
1113d0d045e8SBen Widawsky 				       (src)->base.size>>PAGE_SHIFT)
11149df30794SChris Wilson 
11159df30794SChris Wilson static void
11169df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
11179df30794SChris Wilson {
11189df30794SChris Wilson 	int page;
11199df30794SChris Wilson 
11209df30794SChris Wilson 	if (obj == NULL)
11219df30794SChris Wilson 		return;
11229df30794SChris Wilson 
11239df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
11249df30794SChris Wilson 		kfree(obj->pages[page]);
11259df30794SChris Wilson 
11269df30794SChris Wilson 	kfree(obj);
11279df30794SChris Wilson }
11289df30794SChris Wilson 
1129742cbee8SDaniel Vetter void
1130742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
11319df30794SChris Wilson {
1132742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
1133742cbee8SDaniel Vetter 							  typeof(*error), ref);
1134e2f973d5SChris Wilson 	int i;
1135e2f973d5SChris Wilson 
113652d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
113752d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
113852d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
113952d39a21SChris Wilson 		kfree(error->ring[i].requests);
114052d39a21SChris Wilson 	}
1141e2f973d5SChris Wilson 
11429df30794SChris Wilson 	kfree(error->active_bo);
11436ef3d427SChris Wilson 	kfree(error->overlay);
11449df30794SChris Wilson 	kfree(error);
11459df30794SChris Wilson }
11461b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
11471b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
1148c724e8a9SChris Wilson {
1149c724e8a9SChris Wilson 	err->size = obj->base.size;
1150c724e8a9SChris Wilson 	err->name = obj->base.name;
11510201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
11520201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1153c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
1154c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1155c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1156c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1157c724e8a9SChris Wilson 	err->pinned = 0;
1158c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1159c724e8a9SChris Wilson 		err->pinned = 1;
1160c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1161c724e8a9SChris Wilson 		err->pinned = -1;
1162c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1163c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1164c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
116596154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
116693dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
11671b50247aSChris Wilson }
1168c724e8a9SChris Wilson 
11691b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
11701b50247aSChris Wilson 			     int count, struct list_head *head)
11711b50247aSChris Wilson {
11721b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
11731b50247aSChris Wilson 	int i = 0;
11741b50247aSChris Wilson 
11751b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
11761b50247aSChris Wilson 		capture_bo(err++, obj);
1177c724e8a9SChris Wilson 		if (++i == count)
1178c724e8a9SChris Wilson 			break;
11791b50247aSChris Wilson 	}
1180c724e8a9SChris Wilson 
11811b50247aSChris Wilson 	return i;
11821b50247aSChris Wilson }
11831b50247aSChris Wilson 
11841b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
11851b50247aSChris Wilson 			     int count, struct list_head *head)
11861b50247aSChris Wilson {
11871b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
11881b50247aSChris Wilson 	int i = 0;
11891b50247aSChris Wilson 
11901b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
11911b50247aSChris Wilson 		if (obj->pin_count == 0)
11921b50247aSChris Wilson 			continue;
11931b50247aSChris Wilson 
11941b50247aSChris Wilson 		capture_bo(err++, obj);
11951b50247aSChris Wilson 		if (++i == count)
11961b50247aSChris Wilson 			break;
1197c724e8a9SChris Wilson 	}
1198c724e8a9SChris Wilson 
1199c724e8a9SChris Wilson 	return i;
1200c724e8a9SChris Wilson }
1201c724e8a9SChris Wilson 
1202748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1203748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1204748ebc60SChris Wilson {
1205748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1206748ebc60SChris Wilson 	int i;
1207748ebc60SChris Wilson 
1208748ebc60SChris Wilson 	/* Fences */
1209748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1210775d17b6SDaniel Vetter 	case 7:
1211748ebc60SChris Wilson 	case 6:
1212748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1213748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1214748ebc60SChris Wilson 		break;
1215748ebc60SChris Wilson 	case 5:
1216748ebc60SChris Wilson 	case 4:
1217748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1218748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1219748ebc60SChris Wilson 		break;
1220748ebc60SChris Wilson 	case 3:
1221748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1222748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1223748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1224748ebc60SChris Wilson 	case 2:
1225748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1226748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1227748ebc60SChris Wilson 		break;
1228748ebc60SChris Wilson 
12297dbf9d6eSBen Widawsky 	default:
12307dbf9d6eSBen Widawsky 		BUG();
1231748ebc60SChris Wilson 	}
1232748ebc60SChris Wilson }
1233748ebc60SChris Wilson 
1234bcfb2e28SChris Wilson static struct drm_i915_error_object *
1235bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1236bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1237bcfb2e28SChris Wilson {
1238bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1239bcfb2e28SChris Wilson 	u32 seqno;
1240bcfb2e28SChris Wilson 
1241bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1242bcfb2e28SChris Wilson 		return NULL;
1243bcfb2e28SChris Wilson 
1244b45305fcSDaniel Vetter 	if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1245b45305fcSDaniel Vetter 		u32 acthd = I915_READ(ACTHD);
1246b45305fcSDaniel Vetter 
1247b45305fcSDaniel Vetter 		if (WARN_ON(ring->id != RCS))
1248b45305fcSDaniel Vetter 			return NULL;
1249b45305fcSDaniel Vetter 
1250b45305fcSDaniel Vetter 		obj = ring->private;
1251b45305fcSDaniel Vetter 		if (acthd >= obj->gtt_offset &&
1252b45305fcSDaniel Vetter 		    acthd < obj->gtt_offset + obj->base.size)
1253b45305fcSDaniel Vetter 			return i915_error_object_create(dev_priv, obj);
1254b45305fcSDaniel Vetter 	}
1255b45305fcSDaniel Vetter 
1256b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1257bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1258bcfb2e28SChris Wilson 		if (obj->ring != ring)
1259bcfb2e28SChris Wilson 			continue;
1260bcfb2e28SChris Wilson 
12610201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1262bcfb2e28SChris Wilson 			continue;
1263bcfb2e28SChris Wilson 
1264bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1265bcfb2e28SChris Wilson 			continue;
1266bcfb2e28SChris Wilson 
1267bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1268bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1269bcfb2e28SChris Wilson 		 */
1270bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1271bcfb2e28SChris Wilson 	}
1272bcfb2e28SChris Wilson 
1273bcfb2e28SChris Wilson 	return NULL;
1274bcfb2e28SChris Wilson }
1275bcfb2e28SChris Wilson 
1276d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1277d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1278d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1279d27b1e0eSDaniel Vetter {
1280d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1281d27b1e0eSDaniel Vetter 
128233f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
128312f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
128433f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
12857e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
12867e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
12877e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
12887e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
1289df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1290df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
129133f3f518SDaniel Vetter 	}
1292c1cd90edSDaniel Vetter 
1293d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
12949d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1295d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1296d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1297d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1298c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1299050ee91fSBen Widawsky 		if (ring->id == RCS)
1300d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1301d27b1e0eSDaniel Vetter 	} else {
13029d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1303d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1304d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1305d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1306d27b1e0eSDaniel Vetter 	}
1307d27b1e0eSDaniel Vetter 
13089574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1309c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1310b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1311d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1312c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1313c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
13140f3b6849SChris Wilson 	error->ctl[ring->id] = I915_READ_CTL(ring);
13157e3b8737SDaniel Vetter 
13167e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
13177e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1318d27b1e0eSDaniel Vetter }
1319d27b1e0eSDaniel Vetter 
13208c123e54SBen Widawsky 
13218c123e54SBen Widawsky static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
13228c123e54SBen Widawsky 					   struct drm_i915_error_state *error,
13238c123e54SBen Widawsky 					   struct drm_i915_error_ring *ering)
13248c123e54SBen Widawsky {
13258c123e54SBen Widawsky 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
13268c123e54SBen Widawsky 	struct drm_i915_gem_object *obj;
13278c123e54SBen Widawsky 
13288c123e54SBen Widawsky 	/* Currently render ring is the only HW context user */
13298c123e54SBen Widawsky 	if (ring->id != RCS || !error->ccid)
13308c123e54SBen Widawsky 		return;
13318c123e54SBen Widawsky 
13328c123e54SBen Widawsky 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) {
13338c123e54SBen Widawsky 		if ((error->ccid & PAGE_MASK) == obj->gtt_offset) {
13348c123e54SBen Widawsky 			ering->ctx = i915_error_object_create_sized(dev_priv,
13358c123e54SBen Widawsky 								    obj, 1);
13368c123e54SBen Widawsky 		}
13378c123e54SBen Widawsky 	}
13388c123e54SBen Widawsky }
13398c123e54SBen Widawsky 
134052d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
134152d39a21SChris Wilson 				  struct drm_i915_error_state *error)
134252d39a21SChris Wilson {
134352d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1344b4519513SChris Wilson 	struct intel_ring_buffer *ring;
134552d39a21SChris Wilson 	struct drm_i915_gem_request *request;
134652d39a21SChris Wilson 	int i, count;
134752d39a21SChris Wilson 
1348b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
134952d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
135052d39a21SChris Wilson 
135152d39a21SChris Wilson 		error->ring[i].batchbuffer =
135252d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
135352d39a21SChris Wilson 
135452d39a21SChris Wilson 		error->ring[i].ringbuffer =
135552d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
135652d39a21SChris Wilson 
13578c123e54SBen Widawsky 
13588c123e54SBen Widawsky 		i915_gem_record_active_context(ring, error, &error->ring[i]);
13598c123e54SBen Widawsky 
136052d39a21SChris Wilson 		count = 0;
136152d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
136252d39a21SChris Wilson 			count++;
136352d39a21SChris Wilson 
136452d39a21SChris Wilson 		error->ring[i].num_requests = count;
136552d39a21SChris Wilson 		error->ring[i].requests =
136652d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
136752d39a21SChris Wilson 				GFP_ATOMIC);
136852d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
136952d39a21SChris Wilson 			error->ring[i].num_requests = 0;
137052d39a21SChris Wilson 			continue;
137152d39a21SChris Wilson 		}
137252d39a21SChris Wilson 
137352d39a21SChris Wilson 		count = 0;
137452d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
137552d39a21SChris Wilson 			struct drm_i915_error_request *erq;
137652d39a21SChris Wilson 
137752d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
137852d39a21SChris Wilson 			erq->seqno = request->seqno;
137952d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1380ee4f42b1SChris Wilson 			erq->tail = request->tail;
138152d39a21SChris Wilson 		}
138252d39a21SChris Wilson 	}
138352d39a21SChris Wilson }
138452d39a21SChris Wilson 
13858a905236SJesse Barnes /**
13868a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
13878a905236SJesse Barnes  * @dev: drm device
13888a905236SJesse Barnes  *
13898a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
13908a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
13918a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
13928a905236SJesse Barnes  * to pick up.
13938a905236SJesse Barnes  */
139463eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
139563eeaf38SJesse Barnes {
139663eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
139705394f39SChris Wilson 	struct drm_i915_gem_object *obj;
139863eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
139963eeaf38SJesse Barnes 	unsigned long flags;
14009db4a9c7SJesse Barnes 	int i, pipe;
140163eeaf38SJesse Barnes 
140299584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
140399584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
140499584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
14059df30794SChris Wilson 	if (error)
14069df30794SChris Wilson 		return;
140763eeaf38SJesse Barnes 
14089db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
140933f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
141063eeaf38SJesse Barnes 	if (!error) {
14119df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
14129df30794SChris Wilson 		return;
141363eeaf38SJesse Barnes 	}
141463eeaf38SJesse Barnes 
14152f86f191SBen Widawsky 	DRM_INFO("capturing error event; look for more information in "
14162f86f191SBen Widawsky 		 "/sys/kernel/debug/dri/%d/i915_error_state\n",
1417b6f7833bSChris Wilson 		 dev->primary->index);
14182fa772f3SChris Wilson 
1419742cbee8SDaniel Vetter 	kref_init(&error->ref);
142063eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
142163eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1422211816ecSBen Widawsky 	if (HAS_HW_CONTEXTS(dev))
1423b9a3906bSBen Widawsky 		error->ccid = I915_READ(CCID);
1424be998e2eSBen Widawsky 
1425be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1426be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1427be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1428be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1429be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1430be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1431be998e2eSBen Widawsky 	else
1432be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1433be998e2eSBen Widawsky 
14340f3b6849SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6)
14350f3b6849SChris Wilson 		error->derrmr = I915_READ(DERRMR);
14360f3b6849SChris Wilson 
14370f3b6849SChris Wilson 	if (IS_VALLEYVIEW(dev))
14380f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_VLV);
14390f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 7)
14400f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_MT);
14410f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen == 6)
14420f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE);
14430f3b6849SChris Wilson 
14444f3308b9SPaulo Zanoni 	if (!HAS_PCH_SPLIT(dev))
14459db4a9c7SJesse Barnes 		for_each_pipe(pipe)
14469db4a9c7SJesse Barnes 			error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1447d27b1e0eSDaniel Vetter 
144833f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1449f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
145033f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
145133f3f518SDaniel Vetter 	}
1452add354ddSChris Wilson 
145371e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
145471e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
145571e172e8SBen Widawsky 
1456050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1457050ee91fSBen Widawsky 
1458748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
145952d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
14609df30794SChris Wilson 
1461c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
14629df30794SChris Wilson 	error->active_bo = NULL;
1463c724e8a9SChris Wilson 	error->pinned_bo = NULL;
14649df30794SChris Wilson 
1465bcfb2e28SChris Wilson 	i = 0;
1466bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1467bcfb2e28SChris Wilson 		i++;
1468bcfb2e28SChris Wilson 	error->active_bo_count = i;
14696c085a72SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
14701b50247aSChris Wilson 		if (obj->pin_count)
1471bcfb2e28SChris Wilson 			i++;
1472bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1473c724e8a9SChris Wilson 
14748e934dbfSChris Wilson 	error->active_bo = NULL;
14758e934dbfSChris Wilson 	error->pinned_bo = NULL;
1476bcfb2e28SChris Wilson 	if (i) {
1477bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
14789df30794SChris Wilson 					   GFP_ATOMIC);
1479c724e8a9SChris Wilson 		if (error->active_bo)
1480c724e8a9SChris Wilson 			error->pinned_bo =
1481c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
14829df30794SChris Wilson 	}
1483c724e8a9SChris Wilson 
1484c724e8a9SChris Wilson 	if (error->active_bo)
1485c724e8a9SChris Wilson 		error->active_bo_count =
14861b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1487c724e8a9SChris Wilson 					  error->active_bo_count,
1488c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1489c724e8a9SChris Wilson 
1490c724e8a9SChris Wilson 	if (error->pinned_bo)
1491c724e8a9SChris Wilson 		error->pinned_bo_count =
14921b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1493c724e8a9SChris Wilson 					  error->pinned_bo_count,
14946c085a72SChris Wilson 					  &dev_priv->mm.bound_list);
149563eeaf38SJesse Barnes 
14968a905236SJesse Barnes 	do_gettimeofday(&error->time);
14978a905236SJesse Barnes 
14986ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1499c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
15006ef3d427SChris Wilson 
150199584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
150299584db3SDaniel Vetter 	if (dev_priv->gpu_error.first_error == NULL) {
150399584db3SDaniel Vetter 		dev_priv->gpu_error.first_error = error;
15049df30794SChris Wilson 		error = NULL;
15059df30794SChris Wilson 	}
150699584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
15079df30794SChris Wilson 
15089df30794SChris Wilson 	if (error)
1509742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
15109df30794SChris Wilson }
15119df30794SChris Wilson 
15129df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
15139df30794SChris Wilson {
15149df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
15159df30794SChris Wilson 	struct drm_i915_error_state *error;
15166dc0e816SBen Widawsky 	unsigned long flags;
15179df30794SChris Wilson 
151899584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
151999584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
152099584db3SDaniel Vetter 	dev_priv->gpu_error.first_error = NULL;
152199584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
15229df30794SChris Wilson 
15239df30794SChris Wilson 	if (error)
1524742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
152563eeaf38SJesse Barnes }
15263bd3c932SChris Wilson #else
15273bd3c932SChris Wilson #define i915_capture_error_state(x)
15283bd3c932SChris Wilson #endif
152963eeaf38SJesse Barnes 
153035aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1531c0e09200SDave Airlie {
15328a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1533bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
153463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1535050ee91fSBen Widawsky 	int pipe, i;
153663eeaf38SJesse Barnes 
153735aed2e6SChris Wilson 	if (!eir)
153835aed2e6SChris Wilson 		return;
153963eeaf38SJesse Barnes 
1540a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
15418a905236SJesse Barnes 
1542bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1543bd9854f9SBen Widawsky 
15448a905236SJesse Barnes 	if (IS_G4X(dev)) {
15458a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
15468a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
15478a905236SJesse Barnes 
1548a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1549a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1550050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1551050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1552a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1553a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
15548a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
15553143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
15568a905236SJesse Barnes 		}
15578a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
15588a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1559a70491ccSJoe Perches 			pr_err("page table error\n");
1560a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
15618a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
15623143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
15638a905236SJesse Barnes 		}
15648a905236SJesse Barnes 	}
15658a905236SJesse Barnes 
1566a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
156763eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
156863eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1569a70491ccSJoe Perches 			pr_err("page table error\n");
1570a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
157163eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
15723143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
157363eeaf38SJesse Barnes 		}
15748a905236SJesse Barnes 	}
15758a905236SJesse Barnes 
157663eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1577a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
15789db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1579a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
15809db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
158163eeaf38SJesse Barnes 		/* pipestat has already been acked */
158263eeaf38SJesse Barnes 	}
158363eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1584a70491ccSJoe Perches 		pr_err("instruction error\n");
1585a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1586050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1587050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1588a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
158963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
159063eeaf38SJesse Barnes 
1591a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1592a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1593a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
159463eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
15953143a2bfSChris Wilson 			POSTING_READ(IPEIR);
159663eeaf38SJesse Barnes 		} else {
159763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
159863eeaf38SJesse Barnes 
1599a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1600a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1601a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1602a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
160363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
16043143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
160563eeaf38SJesse Barnes 		}
160663eeaf38SJesse Barnes 	}
160763eeaf38SJesse Barnes 
160863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
16093143a2bfSChris Wilson 	POSTING_READ(EIR);
161063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
161163eeaf38SJesse Barnes 	if (eir) {
161263eeaf38SJesse Barnes 		/*
161363eeaf38SJesse Barnes 		 * some errors might have become stuck,
161463eeaf38SJesse Barnes 		 * mask them.
161563eeaf38SJesse Barnes 		 */
161663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
161763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
161863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
161963eeaf38SJesse Barnes 	}
162035aed2e6SChris Wilson }
162135aed2e6SChris Wilson 
162235aed2e6SChris Wilson /**
162335aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
162435aed2e6SChris Wilson  * @dev: drm device
162535aed2e6SChris Wilson  *
162635aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
162735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
162835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
162935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
163035aed2e6SChris Wilson  * of a ring dump etc.).
163135aed2e6SChris Wilson  */
1632527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
163335aed2e6SChris Wilson {
163435aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1635b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1636b4519513SChris Wilson 	int i;
163735aed2e6SChris Wilson 
163835aed2e6SChris Wilson 	i915_capture_error_state(dev);
163935aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
16408a905236SJesse Barnes 
1641ba1234d1SBen Gamari 	if (wedged) {
1642f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1643f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
1644ba1234d1SBen Gamari 
164511ed50ecSBen Gamari 		/*
16461f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
16471f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
164811ed50ecSBen Gamari 		 */
1649b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1650b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
165111ed50ecSBen Gamari 	}
165211ed50ecSBen Gamari 
165399584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
16548a905236SJesse Barnes }
16558a905236SJesse Barnes 
165621ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
16574e5359cdSSimon Farnsworth {
16584e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
16594e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
16604e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
166105394f39SChris Wilson 	struct drm_i915_gem_object *obj;
16624e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
16634e5359cdSSimon Farnsworth 	unsigned long flags;
16644e5359cdSSimon Farnsworth 	bool stall_detected;
16654e5359cdSSimon Farnsworth 
16664e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
16674e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
16684e5359cdSSimon Farnsworth 		return;
16694e5359cdSSimon Farnsworth 
16704e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
16714e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
16724e5359cdSSimon Farnsworth 
1673e7d841caSChris Wilson 	if (work == NULL ||
1674e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1675e7d841caSChris Wilson 	    !work->enable_stall_check) {
16764e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
16774e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
16784e5359cdSSimon Farnsworth 		return;
16794e5359cdSSimon Farnsworth 	}
16804e5359cdSSimon Farnsworth 
16814e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
168205394f39SChris Wilson 	obj = work->pending_flip_obj;
1683a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
16849db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1685446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1686446f2545SArmin Reese 					obj->gtt_offset;
16874e5359cdSSimon Farnsworth 	} else {
16889db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
168905394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
169001f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
16914e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
16924e5359cdSSimon Farnsworth 	}
16934e5359cdSSimon Farnsworth 
16944e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
16954e5359cdSSimon Farnsworth 
16964e5359cdSSimon Farnsworth 	if (stall_detected) {
16974e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
16984e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
16994e5359cdSSimon Farnsworth 	}
17004e5359cdSSimon Farnsworth }
17014e5359cdSSimon Farnsworth 
170242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
170342f52ef8SKeith Packard  * we use as a pipe index
170442f52ef8SKeith Packard  */
1705f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
17060a3e67a4SJesse Barnes {
17070a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1708e9d21d7fSKeith Packard 	unsigned long irqflags;
170971e0ffa5SJesse Barnes 
17105eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
171171e0ffa5SJesse Barnes 		return -EINVAL;
17120a3e67a4SJesse Barnes 
17131ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1714f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
17157c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
17167c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
17170a3e67a4SJesse Barnes 	else
17187c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
17197c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
17208692d00eSChris Wilson 
17218692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
17228692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
17236b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
17241ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17258692d00eSChris Wilson 
17260a3e67a4SJesse Barnes 	return 0;
17270a3e67a4SJesse Barnes }
17280a3e67a4SJesse Barnes 
1729f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1730f796cf8fSJesse Barnes {
1731f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1732f796cf8fSJesse Barnes 	unsigned long irqflags;
1733f796cf8fSJesse Barnes 
1734f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1735f796cf8fSJesse Barnes 		return -EINVAL;
1736f796cf8fSJesse Barnes 
1737f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1738f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1739f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1740f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1741f796cf8fSJesse Barnes 
1742f796cf8fSJesse Barnes 	return 0;
1743f796cf8fSJesse Barnes }
1744f796cf8fSJesse Barnes 
1745f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1746b1f14ad0SJesse Barnes {
1747b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1748b1f14ad0SJesse Barnes 	unsigned long irqflags;
1749b1f14ad0SJesse Barnes 
1750b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1751b1f14ad0SJesse Barnes 		return -EINVAL;
1752b1f14ad0SJesse Barnes 
1753b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1754b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
1755b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
1756b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1757b1f14ad0SJesse Barnes 
1758b1f14ad0SJesse Barnes 	return 0;
1759b1f14ad0SJesse Barnes }
1760b1f14ad0SJesse Barnes 
17617e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
17627e231dbeSJesse Barnes {
17637e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17647e231dbeSJesse Barnes 	unsigned long irqflags;
176531acc7f5SJesse Barnes 	u32 imr;
17667e231dbeSJesse Barnes 
17677e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
17687e231dbeSJesse Barnes 		return -EINVAL;
17697e231dbeSJesse Barnes 
17707e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17717e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
177231acc7f5SJesse Barnes 	if (pipe == 0)
17737e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
177431acc7f5SJesse Barnes 	else
17757e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
17767e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
177731acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
177831acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
17797e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17807e231dbeSJesse Barnes 
17817e231dbeSJesse Barnes 	return 0;
17827e231dbeSJesse Barnes }
17837e231dbeSJesse Barnes 
178442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
178542f52ef8SKeith Packard  * we use as a pipe index
178642f52ef8SKeith Packard  */
1787f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
17880a3e67a4SJesse Barnes {
17890a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1790e9d21d7fSKeith Packard 	unsigned long irqflags;
17910a3e67a4SJesse Barnes 
17921ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17938692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
17946b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
17958692d00eSChris Wilson 
17967c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
17977c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
17987c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
17991ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
18000a3e67a4SJesse Barnes }
18010a3e67a4SJesse Barnes 
1802f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1803f796cf8fSJesse Barnes {
1804f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1805f796cf8fSJesse Barnes 	unsigned long irqflags;
1806f796cf8fSJesse Barnes 
1807f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1808f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1809f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1810f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1811f796cf8fSJesse Barnes }
1812f796cf8fSJesse Barnes 
1813f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1814b1f14ad0SJesse Barnes {
1815b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1816b1f14ad0SJesse Barnes 	unsigned long irqflags;
1817b1f14ad0SJesse Barnes 
1818b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1819b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
1820b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
1821b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1822b1f14ad0SJesse Barnes }
1823b1f14ad0SJesse Barnes 
18247e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
18257e231dbeSJesse Barnes {
18267e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18277e231dbeSJesse Barnes 	unsigned long irqflags;
182831acc7f5SJesse Barnes 	u32 imr;
18297e231dbeSJesse Barnes 
18307e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
183131acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
183231acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
18337e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
183431acc7f5SJesse Barnes 	if (pipe == 0)
18357e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
183631acc7f5SJesse Barnes 	else
18377e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
18387e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
18397e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
18407e231dbeSJesse Barnes }
18417e231dbeSJesse Barnes 
1842893eead0SChris Wilson static u32
1843893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1844852835f3SZou Nan hai {
1845893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1846893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1847893eead0SChris Wilson }
1848893eead0SChris Wilson 
1849893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1850893eead0SChris Wilson {
1851893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1852b2eadbc8SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring, false),
1853b2eadbc8SChris Wilson 			      ring_last_seqno(ring))) {
1854893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
18559574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
18569574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
18579574b3feSBen Widawsky 				  ring->name);
1858893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1859893eead0SChris Wilson 			*err = true;
1860893eead0SChris Wilson 		}
1861893eead0SChris Wilson 		return true;
1862893eead0SChris Wilson 	}
1863893eead0SChris Wilson 	return false;
1864f65d9421SBen Gamari }
1865f65d9421SBen Gamari 
1866a24a11e6SChris Wilson static bool semaphore_passed(struct intel_ring_buffer *ring)
1867a24a11e6SChris Wilson {
1868a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1869a24a11e6SChris Wilson 	u32 acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
1870a24a11e6SChris Wilson 	struct intel_ring_buffer *signaller;
1871a24a11e6SChris Wilson 	u32 cmd, ipehr, acthd_min;
1872a24a11e6SChris Wilson 
1873a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
1874a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
1875a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
1876a24a11e6SChris Wilson 		return false;
1877a24a11e6SChris Wilson 
1878a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
1879a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
1880a24a11e6SChris Wilson 	 */
1881a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
1882a24a11e6SChris Wilson 	do {
1883a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
1884a24a11e6SChris Wilson 		if (cmd == ipehr)
1885a24a11e6SChris Wilson 			break;
1886a24a11e6SChris Wilson 
1887a24a11e6SChris Wilson 		acthd -= 4;
1888a24a11e6SChris Wilson 		if (acthd < acthd_min)
1889a24a11e6SChris Wilson 			return false;
1890a24a11e6SChris Wilson 	} while (1);
1891a24a11e6SChris Wilson 
1892a24a11e6SChris Wilson 	signaller = &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
1893a24a11e6SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false),
1894a24a11e6SChris Wilson 				 ioread32(ring->virtual_start+acthd+4)+1);
1895a24a11e6SChris Wilson }
1896a24a11e6SChris Wilson 
18971ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
18981ec14ad3SChris Wilson {
18991ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
19001ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
19011ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
19021ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
19031ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
19041ec14ad3SChris Wilson 			  ring->name);
19051ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
19061ec14ad3SChris Wilson 		return true;
19071ec14ad3SChris Wilson 	}
1908a24a11e6SChris Wilson 
1909a24a11e6SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 &&
1910a24a11e6SChris Wilson 	    tmp & RING_WAIT_SEMAPHORE &&
1911a24a11e6SChris Wilson 	    semaphore_passed(ring)) {
1912a24a11e6SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
1913a24a11e6SChris Wilson 			  ring->name);
1914a24a11e6SChris Wilson 		I915_WRITE_CTL(ring, tmp);
1915a24a11e6SChris Wilson 		return true;
1916a24a11e6SChris Wilson 	}
19171ec14ad3SChris Wilson 	return false;
19181ec14ad3SChris Wilson }
19191ec14ad3SChris Wilson 
1920d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
1921d1e61e7fSChris Wilson {
1922d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1923d1e61e7fSChris Wilson 
192499584db3SDaniel Vetter 	if (dev_priv->gpu_error.hangcheck_count++ > 1) {
1925b4519513SChris Wilson 		bool hung = true;
1926b4519513SChris Wilson 
1927d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1928d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
1929d1e61e7fSChris Wilson 
1930d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
1931b4519513SChris Wilson 			struct intel_ring_buffer *ring;
1932b4519513SChris Wilson 			int i;
1933b4519513SChris Wilson 
1934d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
1935d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
1936d1e61e7fSChris Wilson 			 * and break the hang. This should work on
1937d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
1938d1e61e7fSChris Wilson 			 */
1939b4519513SChris Wilson 			for_each_ring(ring, dev_priv, i)
1940b4519513SChris Wilson 				hung &= !kick_ring(ring);
1941d1e61e7fSChris Wilson 		}
1942d1e61e7fSChris Wilson 
1943b4519513SChris Wilson 		return hung;
1944d1e61e7fSChris Wilson 	}
1945d1e61e7fSChris Wilson 
1946d1e61e7fSChris Wilson 	return false;
1947d1e61e7fSChris Wilson }
1948d1e61e7fSChris Wilson 
1949f65d9421SBen Gamari /**
1950f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1951f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1952f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1953f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1954f65d9421SBen Gamari  */
1955f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1956f65d9421SBen Gamari {
1957f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1958f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1959bd9854f9SBen Widawsky 	uint32_t acthd[I915_NUM_RINGS], instdone[I915_NUM_INSTDONE_REG];
1960b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1961b4519513SChris Wilson 	bool err = false, idle;
1962b4519513SChris Wilson 	int i;
1963893eead0SChris Wilson 
19643e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
19653e0dc6b0SBen Widawsky 		return;
19663e0dc6b0SBen Widawsky 
1967b4519513SChris Wilson 	memset(acthd, 0, sizeof(acthd));
1968b4519513SChris Wilson 	idle = true;
1969b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
1970b4519513SChris Wilson 	    idle &= i915_hangcheck_ring_idle(ring, &err);
1971b4519513SChris Wilson 	    acthd[i] = intel_ring_get_active_head(ring);
1972b4519513SChris Wilson 	}
1973b4519513SChris Wilson 
1974893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
1975b4519513SChris Wilson 	if (idle) {
1976d1e61e7fSChris Wilson 		if (err) {
1977d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
1978d1e61e7fSChris Wilson 				return;
1979d1e61e7fSChris Wilson 
1980893eead0SChris Wilson 			goto repeat;
1981d1e61e7fSChris Wilson 		}
1982d1e61e7fSChris Wilson 
198399584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
1984893eead0SChris Wilson 		return;
1985893eead0SChris Wilson 	}
1986f65d9421SBen Gamari 
1987bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
198899584db3SDaniel Vetter 	if (memcmp(dev_priv->gpu_error.last_acthd, acthd,
198999584db3SDaniel Vetter 		   sizeof(acthd)) == 0 &&
199099584db3SDaniel Vetter 	    memcmp(dev_priv->gpu_error.prev_instdone, instdone,
199199584db3SDaniel Vetter 		   sizeof(instdone)) == 0) {
1992d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
1993f65d9421SBen Gamari 			return;
1994cbb465e7SChris Wilson 	} else {
199599584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
1996cbb465e7SChris Wilson 
199799584db3SDaniel Vetter 		memcpy(dev_priv->gpu_error.last_acthd, acthd,
199899584db3SDaniel Vetter 		       sizeof(acthd));
199999584db3SDaniel Vetter 		memcpy(dev_priv->gpu_error.prev_instdone, instdone,
200099584db3SDaniel Vetter 		       sizeof(instdone));
2001cbb465e7SChris Wilson 	}
2002f65d9421SBen Gamari 
2003893eead0SChris Wilson repeat:
2004f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
200599584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
2006cecc21feSChris Wilson 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2007f65d9421SBen Gamari }
2008f65d9421SBen Gamari 
2009c0e09200SDave Airlie /* drm_dma.h hooks
2010c0e09200SDave Airlie */
2011f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2012036a4a7dSZhenyu Wang {
2013036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2014036a4a7dSZhenyu Wang 
20154697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
20164697995bSJesse Barnes 
2017036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2018bdfcdb63SDaniel Vetter 
2019036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
2020036a4a7dSZhenyu Wang 
2021036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2022036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
20233143a2bfSChris Wilson 	POSTING_READ(DEIER);
2024036a4a7dSZhenyu Wang 
2025036a4a7dSZhenyu Wang 	/* and GT */
2026036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2027036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
20283143a2bfSChris Wilson 	POSTING_READ(GTIER);
2029c650156aSZhenyu Wang 
2030c650156aSZhenyu Wang 	/* south display irq */
2031c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
203282a28bcfSDaniel Vetter 	/*
203382a28bcfSDaniel Vetter 	 * SDEIER is also touched by the interrupt handler to work around missed
203482a28bcfSDaniel Vetter 	 * PCH interrupts. Hence we can't update it after the interrupt handler
203582a28bcfSDaniel Vetter 	 * is enabled - instead we unconditionally enable all PCH interrupt
203682a28bcfSDaniel Vetter 	 * sources here, but then only unmask them as needed with SDEIMR.
203782a28bcfSDaniel Vetter 	 */
203882a28bcfSDaniel Vetter 	I915_WRITE(SDEIER, 0xffffffff);
20393143a2bfSChris Wilson 	POSTING_READ(SDEIER);
2040036a4a7dSZhenyu Wang }
2041036a4a7dSZhenyu Wang 
20427e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
20437e231dbeSJesse Barnes {
20447e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20457e231dbeSJesse Barnes 	int pipe;
20467e231dbeSJesse Barnes 
20477e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
20487e231dbeSJesse Barnes 
20497e231dbeSJesse Barnes 	/* VLV magic */
20507e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
20517e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
20527e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
20537e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
20547e231dbeSJesse Barnes 
20557e231dbeSJesse Barnes 	/* and GT */
20567e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
20577e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
20587e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
20597e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
20607e231dbeSJesse Barnes 	POSTING_READ(GTIER);
20617e231dbeSJesse Barnes 
20627e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
20637e231dbeSJesse Barnes 
20647e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
20657e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
20667e231dbeSJesse Barnes 	for_each_pipe(pipe)
20677e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
20687e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20697e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
20707e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
20717e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
20727e231dbeSJesse Barnes }
20737e231dbeSJesse Barnes 
207482a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
207582a28bcfSDaniel Vetter {
207682a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
207782a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
207882a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
207982a28bcfSDaniel Vetter 	u32 mask = ~I915_READ(SDEIMR);
208082a28bcfSDaniel Vetter 	u32 hotplug;
208182a28bcfSDaniel Vetter 
208282a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
208382a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
208482a28bcfSDaniel Vetter 			mask |= hpd_ibx[intel_encoder->hpd_pin];
208582a28bcfSDaniel Vetter 	} else {
208682a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
208782a28bcfSDaniel Vetter 			mask |= hpd_cpt[intel_encoder->hpd_pin];
208882a28bcfSDaniel Vetter 	}
208982a28bcfSDaniel Vetter 
209082a28bcfSDaniel Vetter 	I915_WRITE(SDEIMR, ~mask);
209182a28bcfSDaniel Vetter 
20927fe0b973SKeith Packard 	/*
20937fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
20947fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
20957fe0b973SKeith Packard 	 *
20967fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
20977fe0b973SKeith Packard 	 */
20987fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
20997fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
21007fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
21017fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
21027fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
21037fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
21047fe0b973SKeith Packard }
21057fe0b973SKeith Packard 
2106d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2107d46da437SPaulo Zanoni {
2108d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
210982a28bcfSDaniel Vetter 	u32 mask;
2110d46da437SPaulo Zanoni 
211182a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev))
211282a28bcfSDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK;
211382a28bcfSDaniel Vetter 	else
211482a28bcfSDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
2115d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2116d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2117d46da437SPaulo Zanoni }
2118d46da437SPaulo Zanoni 
2119f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2120036a4a7dSZhenyu Wang {
2121036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2122036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
2123013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2124ce99c256SDaniel Vetter 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
2125ce99c256SDaniel Vetter 			   DE_AUX_CHANNEL_A;
21261ec14ad3SChris Wilson 	u32 render_irqs;
2127036a4a7dSZhenyu Wang 
21281ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2129036a4a7dSZhenyu Wang 
2130036a4a7dSZhenyu Wang 	/* should always can generate irq */
2131036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
21321ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
21331ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
21343143a2bfSChris Wilson 	POSTING_READ(DEIER);
2135036a4a7dSZhenyu Wang 
21361ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
2137036a4a7dSZhenyu Wang 
2138036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
21391ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2140881f47b6SXiang, Haihao 
21411ec14ad3SChris Wilson 	if (IS_GEN6(dev))
21421ec14ad3SChris Wilson 		render_irqs =
21431ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
2144e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
2145e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
21461ec14ad3SChris Wilson 	else
21471ec14ad3SChris Wilson 		render_irqs =
214888f23b8fSChris Wilson 			GT_USER_INTERRUPT |
2149c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
21501ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
21511ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
21523143a2bfSChris Wilson 	POSTING_READ(GTIER);
2153036a4a7dSZhenyu Wang 
2154d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
21557fe0b973SKeith Packard 
2156f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
2157f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
2158f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
2159f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
2160f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2161f97108d1SJesse Barnes 	}
2162f97108d1SJesse Barnes 
2163036a4a7dSZhenyu Wang 	return 0;
2164036a4a7dSZhenyu Wang }
2165036a4a7dSZhenyu Wang 
2166f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2167b1f14ad0SJesse Barnes {
2168b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2169b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2170b615b57aSChris Wilson 	u32 display_mask =
2171b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2172b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
2173b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
2174ce99c256SDaniel Vetter 		DE_PLANEA_FLIP_DONE_IVB |
2175ce99c256SDaniel Vetter 		DE_AUX_CHANNEL_A_IVB;
2176b1f14ad0SJesse Barnes 	u32 render_irqs;
2177b1f14ad0SJesse Barnes 
2178b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2179b1f14ad0SJesse Barnes 
2180b1f14ad0SJesse Barnes 	/* should always can generate irq */
2181b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2182b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2183b615b57aSChris Wilson 	I915_WRITE(DEIER,
2184b615b57aSChris Wilson 		   display_mask |
2185b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
2186b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
2187b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
2188b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2189b1f14ad0SJesse Barnes 
219015b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2191b1f14ad0SJesse Barnes 
2192b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2193b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2194b1f14ad0SJesse Barnes 
2195e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
219615b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2197b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
2198b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2199b1f14ad0SJesse Barnes 
2200d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
22017fe0b973SKeith Packard 
2202b1f14ad0SJesse Barnes 	return 0;
2203b1f14ad0SJesse Barnes }
2204b1f14ad0SJesse Barnes 
22057e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
22067e231dbeSJesse Barnes {
22077e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22087e231dbeSJesse Barnes 	u32 enable_mask;
220931acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
22103bcedbe5SJesse Barnes 	u32 render_irqs;
22117e231dbeSJesse Barnes 	u16 msid;
22127e231dbeSJesse Barnes 
22137e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
221431acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
221531acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
221631acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
22177e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22187e231dbeSJesse Barnes 
221931acc7f5SJesse Barnes 	/*
222031acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
222131acc7f5SJesse Barnes 	 * toggle them based on usage.
222231acc7f5SJesse Barnes 	 */
222331acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
222431acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
222531acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
22267e231dbeSJesse Barnes 
22277e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
22287e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
22297e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
22307e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
22317e231dbeSJesse Barnes 	msid |= (1<<14);
22327e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
22337e231dbeSJesse Barnes 
223420afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
223520afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
223620afbda2SDaniel Vetter 
22377e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
22387e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
22397e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
22407e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
22417e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
22427e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
22437e231dbeSJesse Barnes 
224431acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2245515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
224631acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
224731acc7f5SJesse Barnes 
22487e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
22497e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
22507e231dbeSJesse Barnes 
225131acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
225231acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
22533bcedbe5SJesse Barnes 
22543bcedbe5SJesse Barnes 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
22553bcedbe5SJesse Barnes 		GEN6_BLITTER_USER_INTERRUPT;
22563bcedbe5SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
22577e231dbeSJesse Barnes 	POSTING_READ(GTIER);
22587e231dbeSJesse Barnes 
22597e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
22607e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
22617e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
22627e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
22637e231dbeSJesse Barnes #endif
22647e231dbeSJesse Barnes 
22657e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
226620afbda2SDaniel Vetter 
226720afbda2SDaniel Vetter 	return 0;
226820afbda2SDaniel Vetter }
226920afbda2SDaniel Vetter 
22707e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
22717e231dbeSJesse Barnes {
22727e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22737e231dbeSJesse Barnes 	int pipe;
22747e231dbeSJesse Barnes 
22757e231dbeSJesse Barnes 	if (!dev_priv)
22767e231dbeSJesse Barnes 		return;
22777e231dbeSJesse Barnes 
22787e231dbeSJesse Barnes 	for_each_pipe(pipe)
22797e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
22807e231dbeSJesse Barnes 
22817e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
22827e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
22837e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
22847e231dbeSJesse Barnes 	for_each_pipe(pipe)
22857e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
22867e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
22877e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
22887e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
22897e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
22907e231dbeSJesse Barnes }
22917e231dbeSJesse Barnes 
2292f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2293036a4a7dSZhenyu Wang {
2294036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22954697995bSJesse Barnes 
22964697995bSJesse Barnes 	if (!dev_priv)
22974697995bSJesse Barnes 		return;
22984697995bSJesse Barnes 
2299036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2300036a4a7dSZhenyu Wang 
2301036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2302036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2303036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2304036a4a7dSZhenyu Wang 
2305036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2306036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2307036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2308192aac1fSKeith Packard 
2309192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2310192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2311192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2312036a4a7dSZhenyu Wang }
2313036a4a7dSZhenyu Wang 
2314c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2315c2798b19SChris Wilson {
2316c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2317c2798b19SChris Wilson 	int pipe;
2318c2798b19SChris Wilson 
2319c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2320c2798b19SChris Wilson 
2321c2798b19SChris Wilson 	for_each_pipe(pipe)
2322c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2323c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2324c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2325c2798b19SChris Wilson 	POSTING_READ16(IER);
2326c2798b19SChris Wilson }
2327c2798b19SChris Wilson 
2328c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2329c2798b19SChris Wilson {
2330c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2331c2798b19SChris Wilson 
2332c2798b19SChris Wilson 	I915_WRITE16(EMR,
2333c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2334c2798b19SChris Wilson 
2335c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2336c2798b19SChris Wilson 	dev_priv->irq_mask =
2337c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2338c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2339c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2340c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2341c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2342c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2343c2798b19SChris Wilson 
2344c2798b19SChris Wilson 	I915_WRITE16(IER,
2345c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2346c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2347c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2348c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2349c2798b19SChris Wilson 	POSTING_READ16(IER);
2350c2798b19SChris Wilson 
2351c2798b19SChris Wilson 	return 0;
2352c2798b19SChris Wilson }
2353c2798b19SChris Wilson 
235490a72f87SVille Syrjälä /*
235590a72f87SVille Syrjälä  * Returns true when a page flip has completed.
235690a72f87SVille Syrjälä  */
235790a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
235890a72f87SVille Syrjälä 			       int pipe, u16 iir)
235990a72f87SVille Syrjälä {
236090a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
236190a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
236290a72f87SVille Syrjälä 
236390a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
236490a72f87SVille Syrjälä 		return false;
236590a72f87SVille Syrjälä 
236690a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
236790a72f87SVille Syrjälä 		return false;
236890a72f87SVille Syrjälä 
236990a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
237090a72f87SVille Syrjälä 
237190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
237290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
237390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
237490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
237590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
237690a72f87SVille Syrjälä 	 */
237790a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
237890a72f87SVille Syrjälä 		return false;
237990a72f87SVille Syrjälä 
238090a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
238190a72f87SVille Syrjälä 
238290a72f87SVille Syrjälä 	return true;
238390a72f87SVille Syrjälä }
238490a72f87SVille Syrjälä 
2385ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2386c2798b19SChris Wilson {
2387c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2388c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2389c2798b19SChris Wilson 	u16 iir, new_iir;
2390c2798b19SChris Wilson 	u32 pipe_stats[2];
2391c2798b19SChris Wilson 	unsigned long irqflags;
2392c2798b19SChris Wilson 	int irq_received;
2393c2798b19SChris Wilson 	int pipe;
2394c2798b19SChris Wilson 	u16 flip_mask =
2395c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2396c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2397c2798b19SChris Wilson 
2398c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2399c2798b19SChris Wilson 
2400c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2401c2798b19SChris Wilson 	if (iir == 0)
2402c2798b19SChris Wilson 		return IRQ_NONE;
2403c2798b19SChris Wilson 
2404c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2405c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2406c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2407c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2408c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2409c2798b19SChris Wilson 		 */
2410c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2411c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2412c2798b19SChris Wilson 			i915_handle_error(dev, false);
2413c2798b19SChris Wilson 
2414c2798b19SChris Wilson 		for_each_pipe(pipe) {
2415c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2416c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2417c2798b19SChris Wilson 
2418c2798b19SChris Wilson 			/*
2419c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2420c2798b19SChris Wilson 			 */
2421c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2422c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2423c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2424c2798b19SChris Wilson 							 pipe_name(pipe));
2425c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2426c2798b19SChris Wilson 				irq_received = 1;
2427c2798b19SChris Wilson 			}
2428c2798b19SChris Wilson 		}
2429c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2430c2798b19SChris Wilson 
2431c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2432c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2433c2798b19SChris Wilson 
2434d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2435c2798b19SChris Wilson 
2436c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2437c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2438c2798b19SChris Wilson 
2439c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
244090a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
244190a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2442c2798b19SChris Wilson 
2443c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
244490a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
244590a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2446c2798b19SChris Wilson 
2447c2798b19SChris Wilson 		iir = new_iir;
2448c2798b19SChris Wilson 	}
2449c2798b19SChris Wilson 
2450c2798b19SChris Wilson 	return IRQ_HANDLED;
2451c2798b19SChris Wilson }
2452c2798b19SChris Wilson 
2453c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2454c2798b19SChris Wilson {
2455c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2456c2798b19SChris Wilson 	int pipe;
2457c2798b19SChris Wilson 
2458c2798b19SChris Wilson 	for_each_pipe(pipe) {
2459c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2460c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2461c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2462c2798b19SChris Wilson 	}
2463c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2464c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2465c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2466c2798b19SChris Wilson }
2467c2798b19SChris Wilson 
2468a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2469a266c7d5SChris Wilson {
2470a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2471a266c7d5SChris Wilson 	int pipe;
2472a266c7d5SChris Wilson 
2473a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2474a266c7d5SChris Wilson 
2475a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2476a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2477a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2478a266c7d5SChris Wilson 	}
2479a266c7d5SChris Wilson 
248000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2481a266c7d5SChris Wilson 	for_each_pipe(pipe)
2482a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2483a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2484a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2485a266c7d5SChris Wilson 	POSTING_READ(IER);
2486a266c7d5SChris Wilson }
2487a266c7d5SChris Wilson 
2488a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2489a266c7d5SChris Wilson {
2490a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
249138bde180SChris Wilson 	u32 enable_mask;
2492a266c7d5SChris Wilson 
249338bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
249438bde180SChris Wilson 
249538bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
249638bde180SChris Wilson 	dev_priv->irq_mask =
249738bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
249838bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
249938bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
250038bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
250138bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
250238bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
250338bde180SChris Wilson 
250438bde180SChris Wilson 	enable_mask =
250538bde180SChris Wilson 		I915_ASLE_INTERRUPT |
250638bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
250738bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
250838bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
250938bde180SChris Wilson 		I915_USER_INTERRUPT;
251038bde180SChris Wilson 
2511a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
251220afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
251320afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
251420afbda2SDaniel Vetter 
2515a266c7d5SChris Wilson 		/* Enable in IER... */
2516a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2517a266c7d5SChris Wilson 		/* and unmask in IMR */
2518a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2519a266c7d5SChris Wilson 	}
2520a266c7d5SChris Wilson 
2521a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2522a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2523a266c7d5SChris Wilson 	POSTING_READ(IER);
2524a266c7d5SChris Wilson 
252520afbda2SDaniel Vetter 	intel_opregion_enable_asle(dev);
252620afbda2SDaniel Vetter 
252720afbda2SDaniel Vetter 	return 0;
252820afbda2SDaniel Vetter }
252920afbda2SDaniel Vetter 
253090a72f87SVille Syrjälä /*
253190a72f87SVille Syrjälä  * Returns true when a page flip has completed.
253290a72f87SVille Syrjälä  */
253390a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
253490a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
253590a72f87SVille Syrjälä {
253690a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
253790a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
253890a72f87SVille Syrjälä 
253990a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
254090a72f87SVille Syrjälä 		return false;
254190a72f87SVille Syrjälä 
254290a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
254390a72f87SVille Syrjälä 		return false;
254490a72f87SVille Syrjälä 
254590a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
254690a72f87SVille Syrjälä 
254790a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
254890a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
254990a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
255090a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
255190a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
255290a72f87SVille Syrjälä 	 */
255390a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
255490a72f87SVille Syrjälä 		return false;
255590a72f87SVille Syrjälä 
255690a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
255790a72f87SVille Syrjälä 
255890a72f87SVille Syrjälä 	return true;
255990a72f87SVille Syrjälä }
256090a72f87SVille Syrjälä 
2561ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2562a266c7d5SChris Wilson {
2563a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2564a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
25658291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2566a266c7d5SChris Wilson 	unsigned long irqflags;
256738bde180SChris Wilson 	u32 flip_mask =
256838bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
256938bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
257038bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2571a266c7d5SChris Wilson 
2572a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2573a266c7d5SChris Wilson 
2574a266c7d5SChris Wilson 	iir = I915_READ(IIR);
257538bde180SChris Wilson 	do {
257638bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
25778291ee90SChris Wilson 		bool blc_event = false;
2578a266c7d5SChris Wilson 
2579a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2580a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2581a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2582a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2583a266c7d5SChris Wilson 		 */
2584a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2585a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2586a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2587a266c7d5SChris Wilson 
2588a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2589a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2590a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2591a266c7d5SChris Wilson 
259238bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2593a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2594a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2595a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2596a266c7d5SChris Wilson 							 pipe_name(pipe));
2597a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
259838bde180SChris Wilson 				irq_received = true;
2599a266c7d5SChris Wilson 			}
2600a266c7d5SChris Wilson 		}
2601a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2602a266c7d5SChris Wilson 
2603a266c7d5SChris Wilson 		if (!irq_received)
2604a266c7d5SChris Wilson 			break;
2605a266c7d5SChris Wilson 
2606a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2607a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2608a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2609a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2610a266c7d5SChris Wilson 
2611a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2612a266c7d5SChris Wilson 				  hotplug_status);
2613e5868a31SEgbert Eich 			if (hotplug_status & HOTPLUG_INT_STATUS_I915)
2614a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2615a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2616a266c7d5SChris Wilson 
2617a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
261838bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2619a266c7d5SChris Wilson 		}
2620a266c7d5SChris Wilson 
262138bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2622a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2623a266c7d5SChris Wilson 
2624a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2625a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2626a266c7d5SChris Wilson 
2627a266c7d5SChris Wilson 		for_each_pipe(pipe) {
262838bde180SChris Wilson 			int plane = pipe;
262938bde180SChris Wilson 			if (IS_MOBILE(dev))
263038bde180SChris Wilson 				plane = !plane;
26315e2032d4SVille Syrjälä 
263290a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
263390a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
263490a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
2635a266c7d5SChris Wilson 
2636a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2637a266c7d5SChris Wilson 				blc_event = true;
2638a266c7d5SChris Wilson 		}
2639a266c7d5SChris Wilson 
2640a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2641a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2642a266c7d5SChris Wilson 
2643a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2644a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2645a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2646a266c7d5SChris Wilson 		 * we would never get another interrupt.
2647a266c7d5SChris Wilson 		 *
2648a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2649a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2650a266c7d5SChris Wilson 		 * another one.
2651a266c7d5SChris Wilson 		 *
2652a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2653a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2654a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2655a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2656a266c7d5SChris Wilson 		 * stray interrupts.
2657a266c7d5SChris Wilson 		 */
265838bde180SChris Wilson 		ret = IRQ_HANDLED;
2659a266c7d5SChris Wilson 		iir = new_iir;
266038bde180SChris Wilson 	} while (iir & ~flip_mask);
2661a266c7d5SChris Wilson 
2662d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
26638291ee90SChris Wilson 
2664a266c7d5SChris Wilson 	return ret;
2665a266c7d5SChris Wilson }
2666a266c7d5SChris Wilson 
2667a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2668a266c7d5SChris Wilson {
2669a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2670a266c7d5SChris Wilson 	int pipe;
2671a266c7d5SChris Wilson 
2672a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2673a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2674a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2675a266c7d5SChris Wilson 	}
2676a266c7d5SChris Wilson 
267700d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
267855b39755SChris Wilson 	for_each_pipe(pipe) {
267955b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2680a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
268155b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
268255b39755SChris Wilson 	}
2683a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2684a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2685a266c7d5SChris Wilson 
2686a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2687a266c7d5SChris Wilson }
2688a266c7d5SChris Wilson 
2689a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2690a266c7d5SChris Wilson {
2691a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2692a266c7d5SChris Wilson 	int pipe;
2693a266c7d5SChris Wilson 
2694a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2695a266c7d5SChris Wilson 
2696a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2697a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2698a266c7d5SChris Wilson 
2699a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2700a266c7d5SChris Wilson 	for_each_pipe(pipe)
2701a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2702a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2703a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2704a266c7d5SChris Wilson 	POSTING_READ(IER);
2705a266c7d5SChris Wilson }
2706a266c7d5SChris Wilson 
2707a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2708a266c7d5SChris Wilson {
2709a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2710bbba0a97SChris Wilson 	u32 enable_mask;
2711a266c7d5SChris Wilson 	u32 error_mask;
2712a266c7d5SChris Wilson 
2713a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2714bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2715adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2716bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2717bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2718bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2719bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2720bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2721bbba0a97SChris Wilson 
2722bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
272321ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
272421ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
2725bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2726bbba0a97SChris Wilson 
2727bbba0a97SChris Wilson 	if (IS_G4X(dev))
2728bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2729a266c7d5SChris Wilson 
2730515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
2731a266c7d5SChris Wilson 
2732a266c7d5SChris Wilson 	/*
2733a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2734a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2735a266c7d5SChris Wilson 	 */
2736a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2737a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2738a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2739a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2740a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2741a266c7d5SChris Wilson 	} else {
2742a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2743a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2744a266c7d5SChris Wilson 	}
2745a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2746a266c7d5SChris Wilson 
2747a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2748a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2749a266c7d5SChris Wilson 	POSTING_READ(IER);
2750a266c7d5SChris Wilson 
275120afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
275220afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
275320afbda2SDaniel Vetter 
275420afbda2SDaniel Vetter 	intel_opregion_enable_asle(dev);
275520afbda2SDaniel Vetter 
275620afbda2SDaniel Vetter 	return 0;
275720afbda2SDaniel Vetter }
275820afbda2SDaniel Vetter 
2759bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
276020afbda2SDaniel Vetter {
276120afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2762e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
2763e5868a31SEgbert Eich 	struct intel_encoder *encoder;
276420afbda2SDaniel Vetter 	u32 hotplug_en;
276520afbda2SDaniel Vetter 
2766bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
2767bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2768bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
2769adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
2770e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
2771bac56d5bSEgbert Eich 		list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
2772e5868a31SEgbert Eich 			hotplug_en |= hpd_mask_i915[encoder->hpd_pin];
2773a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2774a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2775a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2776a266c7d5SChris Wilson 		*/
2777a266c7d5SChris Wilson 		if (IS_G4X(dev))
2778a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
277985fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
2780a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2781a266c7d5SChris Wilson 
2782a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2783a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2784a266c7d5SChris Wilson 	}
2785bac56d5bSEgbert Eich }
2786a266c7d5SChris Wilson 
2787ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
2788a266c7d5SChris Wilson {
2789a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2790a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2791a266c7d5SChris Wilson 	u32 iir, new_iir;
2792a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2793a266c7d5SChris Wilson 	unsigned long irqflags;
2794a266c7d5SChris Wilson 	int irq_received;
2795a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
279621ad8330SVille Syrjälä 	u32 flip_mask =
279721ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
279821ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2799a266c7d5SChris Wilson 
2800a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2801a266c7d5SChris Wilson 
2802a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2803a266c7d5SChris Wilson 
2804a266c7d5SChris Wilson 	for (;;) {
28052c8ba29fSChris Wilson 		bool blc_event = false;
28062c8ba29fSChris Wilson 
280721ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
2808a266c7d5SChris Wilson 
2809a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2810a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2811a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2812a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2813a266c7d5SChris Wilson 		 */
2814a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2815a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2816a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2817a266c7d5SChris Wilson 
2818a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2819a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2820a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2821a266c7d5SChris Wilson 
2822a266c7d5SChris Wilson 			/*
2823a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2824a266c7d5SChris Wilson 			 */
2825a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2826a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2827a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2828a266c7d5SChris Wilson 							 pipe_name(pipe));
2829a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2830a266c7d5SChris Wilson 				irq_received = 1;
2831a266c7d5SChris Wilson 			}
2832a266c7d5SChris Wilson 		}
2833a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2834a266c7d5SChris Wilson 
2835a266c7d5SChris Wilson 		if (!irq_received)
2836a266c7d5SChris Wilson 			break;
2837a266c7d5SChris Wilson 
2838a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2839a266c7d5SChris Wilson 
2840a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2841adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2842a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2843a266c7d5SChris Wilson 
2844a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2845a266c7d5SChris Wilson 				  hotplug_status);
2846e5868a31SEgbert Eich 			if (hotplug_status & (IS_G4X(dev) ?
2847e5868a31SEgbert Eich 					      HOTPLUG_INT_STATUS_G4X :
2848e5868a31SEgbert Eich 					      HOTPLUG_INT_STATUS_I965))
2849a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2850a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2851a266c7d5SChris Wilson 
2852a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2853a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2854a266c7d5SChris Wilson 		}
2855a266c7d5SChris Wilson 
285621ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
2857a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2858a266c7d5SChris Wilson 
2859a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2860a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2861a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2862a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2863a266c7d5SChris Wilson 
2864a266c7d5SChris Wilson 		for_each_pipe(pipe) {
28652c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
286690a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
286790a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
2868a266c7d5SChris Wilson 
2869a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2870a266c7d5SChris Wilson 				blc_event = true;
2871a266c7d5SChris Wilson 		}
2872a266c7d5SChris Wilson 
2873a266c7d5SChris Wilson 
2874a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2875a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2876a266c7d5SChris Wilson 
2877515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
2878515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
2879515ac2bbSDaniel Vetter 
2880a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2881a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2882a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2883a266c7d5SChris Wilson 		 * we would never get another interrupt.
2884a266c7d5SChris Wilson 		 *
2885a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2886a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2887a266c7d5SChris Wilson 		 * another one.
2888a266c7d5SChris Wilson 		 *
2889a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2890a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2891a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2892a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2893a266c7d5SChris Wilson 		 * stray interrupts.
2894a266c7d5SChris Wilson 		 */
2895a266c7d5SChris Wilson 		iir = new_iir;
2896a266c7d5SChris Wilson 	}
2897a266c7d5SChris Wilson 
2898d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
28992c8ba29fSChris Wilson 
2900a266c7d5SChris Wilson 	return ret;
2901a266c7d5SChris Wilson }
2902a266c7d5SChris Wilson 
2903a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
2904a266c7d5SChris Wilson {
2905a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2906a266c7d5SChris Wilson 	int pipe;
2907a266c7d5SChris Wilson 
2908a266c7d5SChris Wilson 	if (!dev_priv)
2909a266c7d5SChris Wilson 		return;
2910a266c7d5SChris Wilson 
2911a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2912a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2913a266c7d5SChris Wilson 
2914a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
2915a266c7d5SChris Wilson 	for_each_pipe(pipe)
2916a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2917a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2918a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2919a266c7d5SChris Wilson 
2920a266c7d5SChris Wilson 	for_each_pipe(pipe)
2921a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
2922a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2923a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2924a266c7d5SChris Wilson }
2925a266c7d5SChris Wilson 
2926f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2927f71d4af4SJesse Barnes {
29288b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
29298b2e326dSChris Wilson 
29308b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
293199584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
2932c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
2933a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
29348b2e326dSChris Wilson 
293599584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
293699584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
293761bac78eSDaniel Vetter 		    (unsigned long) dev);
293861bac78eSDaniel Vetter 
293997a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
29409ee32feaSDaniel Vetter 
2941f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2942f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
29437d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
2944f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2945f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2946f71d4af4SJesse Barnes 	}
2947f71d4af4SJesse Barnes 
2948c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2949f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2950c3613de9SKeith Packard 	else
2951c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2952f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2953f71d4af4SJesse Barnes 
29547e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
29557e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
29567e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
29577e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
29587e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
29597e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
29607e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
2961fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
29624a06e201SDaniel Vetter 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2963f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2964f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2965f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2966f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2967f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2968f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2969f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
297082a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
2971f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2972f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2973f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2974f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2975f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2976f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2977f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
297882a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
2979f71d4af4SJesse Barnes 	} else {
2980c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
2981c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
2982c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
2983c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
2984c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
2985a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
2986a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
2987a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
2988a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
2989a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
299020afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
2991c2798b19SChris Wilson 		} else {
2992a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
2993a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
2994a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
2995a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
2996bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
2997c2798b19SChris Wilson 		}
2998f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
2999f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3000f71d4af4SJesse Barnes 	}
3001f71d4af4SJesse Barnes }
300220afbda2SDaniel Vetter 
300320afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
300420afbda2SDaniel Vetter {
300520afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
300620afbda2SDaniel Vetter 
300720afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
300820afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
300920afbda2SDaniel Vetter }
3010