xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 3b8d8d91d51c7d15cda51052624169edf7b6dbc6)
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 
2963eeaf38SJesse Barnes #include <linux/sysrq.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31c0e09200SDave Airlie #include "drmP.h"
32c0e09200SDave Airlie #include "drm.h"
33c0e09200SDave Airlie #include "i915_drm.h"
34c0e09200SDave Airlie #include "i915_drv.h"
351c5d22f7SChris Wilson #include "i915_trace.h"
3679e53945SJesse Barnes #include "intel_drv.h"
37c0e09200SDave Airlie 
38c0e09200SDave Airlie #define MAX_NOPID ((u32)~0)
39c0e09200SDave Airlie 
407c463586SKeith Packard /**
417c463586SKeith Packard  * Interrupts that are always left unmasked.
427c463586SKeith Packard  *
437c463586SKeith Packard  * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
447c463586SKeith Packard  * we leave them always unmasked in IMR and then control enabling them through
457c463586SKeith Packard  * PIPESTAT alone.
467c463586SKeith Packard  */
476b95a207SKristian Høgsberg #define I915_INTERRUPT_ENABLE_FIX			\
486b95a207SKristian Høgsberg 	(I915_ASLE_INTERRUPT |				\
490a3e67a4SJesse Barnes 	 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |		\
5063eeaf38SJesse Barnes 	 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |		\
516b95a207SKristian Høgsberg 	 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |	\
526b95a207SKristian Høgsberg 	 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |	\
5363eeaf38SJesse Barnes 	 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
54ed4cb414SEric Anholt 
557c463586SKeith Packard /** Interrupts that we mask and unmask at runtime. */
56d1b851fcSZou Nan hai #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT | I915_BSD_USER_INTERRUPT)
577c463586SKeith Packard 
5879e53945SJesse Barnes #define I915_PIPE_VBLANK_STATUS	(PIPE_START_VBLANK_INTERRUPT_STATUS |\
5979e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_STATUS)
6079e53945SJesse Barnes 
6179e53945SJesse Barnes #define I915_PIPE_VBLANK_ENABLE	(PIPE_START_VBLANK_INTERRUPT_ENABLE |\
6279e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_ENABLE)
6379e53945SJesse Barnes 
6479e53945SJesse Barnes #define DRM_I915_VBLANK_PIPE_ALL	(DRM_I915_VBLANK_PIPE_A | \
6579e53945SJesse Barnes 					 DRM_I915_VBLANK_PIPE_B)
6679e53945SJesse Barnes 
678ee1c3dbSMatthew Garrett void
68f2b115e6SAdam Jackson ironlake_enable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
69036a4a7dSZhenyu Wang {
701ec14ad3SChris Wilson 	if ((dev_priv->gt_irq_mask & mask) != 0) {
711ec14ad3SChris Wilson 		dev_priv->gt_irq_mask &= ~mask;
721ec14ad3SChris Wilson 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
733143a2bfSChris Wilson 		POSTING_READ(GTIMR);
74036a4a7dSZhenyu Wang 	}
75036a4a7dSZhenyu Wang }
76036a4a7dSZhenyu Wang 
7762fdfeafSEric Anholt void
78f2b115e6SAdam Jackson ironlake_disable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
79036a4a7dSZhenyu Wang {
801ec14ad3SChris Wilson 	if ((dev_priv->gt_irq_mask & mask) != mask) {
811ec14ad3SChris Wilson 		dev_priv->gt_irq_mask |= mask;
821ec14ad3SChris Wilson 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
833143a2bfSChris Wilson 		POSTING_READ(GTIMR);
84036a4a7dSZhenyu Wang 	}
85036a4a7dSZhenyu Wang }
86036a4a7dSZhenyu Wang 
87036a4a7dSZhenyu Wang /* For display hotplug interrupt */
88995b6762SChris Wilson static void
89f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
90036a4a7dSZhenyu Wang {
911ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
921ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
931ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
943143a2bfSChris Wilson 		POSTING_READ(DEIMR);
95036a4a7dSZhenyu Wang 	}
96036a4a7dSZhenyu Wang }
97036a4a7dSZhenyu Wang 
98036a4a7dSZhenyu Wang static inline void
99f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
100036a4a7dSZhenyu Wang {
1011ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1021ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1031ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1043143a2bfSChris Wilson 		POSTING_READ(DEIMR);
105036a4a7dSZhenyu Wang 	}
106036a4a7dSZhenyu Wang }
107036a4a7dSZhenyu Wang 
108036a4a7dSZhenyu Wang void
109ed4cb414SEric Anholt i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
110ed4cb414SEric Anholt {
1111ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
1121ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
1131ec14ad3SChris Wilson 		I915_WRITE(IMR, dev_priv->irq_mask);
1143143a2bfSChris Wilson 		POSTING_READ(IMR);
115ed4cb414SEric Anholt 	}
116ed4cb414SEric Anholt }
117ed4cb414SEric Anholt 
11862fdfeafSEric Anholt void
119ed4cb414SEric Anholt i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
120ed4cb414SEric Anholt {
1211ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1221ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1231ec14ad3SChris Wilson 		I915_WRITE(IMR, dev_priv->irq_mask);
1243143a2bfSChris Wilson 		POSTING_READ(IMR);
125ed4cb414SEric Anholt 	}
126ed4cb414SEric Anholt }
127ed4cb414SEric Anholt 
1287c463586SKeith Packard static inline u32
1297c463586SKeith Packard i915_pipestat(int pipe)
1307c463586SKeith Packard {
1317c463586SKeith Packard 	if (pipe == 0)
1327c463586SKeith Packard 		return PIPEASTAT;
1337c463586SKeith Packard 	if (pipe == 1)
1347c463586SKeith Packard 		return PIPEBSTAT;
1359c84ba4eSAndrew Morton 	BUG();
1367c463586SKeith Packard }
1377c463586SKeith Packard 
1387c463586SKeith Packard void
1397c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1407c463586SKeith Packard {
1417c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
1427c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1437c463586SKeith Packard 
1447c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
1457c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
1467c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
1473143a2bfSChris Wilson 		POSTING_READ(reg);
1487c463586SKeith Packard 	}
1497c463586SKeith Packard }
1507c463586SKeith Packard 
1517c463586SKeith Packard void
1527c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1537c463586SKeith Packard {
1547c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
1557c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1567c463586SKeith Packard 
1577c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
1587c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
1593143a2bfSChris Wilson 		POSTING_READ(reg);
1607c463586SKeith Packard 	}
1617c463586SKeith Packard }
1627c463586SKeith Packard 
163c0e09200SDave Airlie /**
16401c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
16501c66889SZhao Yakui  */
16601c66889SZhao Yakui void intel_enable_asle(struct drm_device *dev)
16701c66889SZhao Yakui {
1681ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1691ec14ad3SChris Wilson 	unsigned long irqflags;
1701ec14ad3SChris Wilson 
1711ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17201c66889SZhao Yakui 
173c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
174f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
175edcb49caSZhao Yakui 	else {
17601c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
177d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
178a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen >= 4)
179edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
180d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
181edcb49caSZhao Yakui 	}
1821ec14ad3SChris Wilson 
1831ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
18401c66889SZhao Yakui }
18501c66889SZhao Yakui 
18601c66889SZhao Yakui /**
1870a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1880a3e67a4SJesse Barnes  * @dev: DRM device
1890a3e67a4SJesse Barnes  * @pipe: pipe to check
1900a3e67a4SJesse Barnes  *
1910a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1920a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1930a3e67a4SJesse Barnes  * before reading such registers if unsure.
1940a3e67a4SJesse Barnes  */
1950a3e67a4SJesse Barnes static int
1960a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1970a3e67a4SJesse Barnes {
1980a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1995eddb70bSChris Wilson 	return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
2000a3e67a4SJesse Barnes }
2010a3e67a4SJesse Barnes 
20242f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
20342f52ef8SKeith Packard  * we use as a pipe index
20442f52ef8SKeith Packard  */
20542f52ef8SKeith Packard u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
2060a3e67a4SJesse Barnes {
2070a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2080a3e67a4SJesse Barnes 	unsigned long high_frame;
2090a3e67a4SJesse Barnes 	unsigned long low_frame;
2105eddb70bSChris Wilson 	u32 high1, high2, low;
2110a3e67a4SJesse Barnes 
2120a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
21344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
21444d98a61SZhao Yakui 				"pipe %d\n", pipe);
2150a3e67a4SJesse Barnes 		return 0;
2160a3e67a4SJesse Barnes 	}
2170a3e67a4SJesse Barnes 
2185eddb70bSChris Wilson 	high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
2195eddb70bSChris Wilson 	low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
2205eddb70bSChris Wilson 
2210a3e67a4SJesse Barnes 	/*
2220a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
2230a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
2240a3e67a4SJesse Barnes 	 * register.
2250a3e67a4SJesse Barnes 	 */
2260a3e67a4SJesse Barnes 	do {
2275eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
2285eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
2295eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
2300a3e67a4SJesse Barnes 	} while (high1 != high2);
2310a3e67a4SJesse Barnes 
2325eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
2335eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
2345eddb70bSChris Wilson 	return (high1 << 8) | low;
2350a3e67a4SJesse Barnes }
2360a3e67a4SJesse Barnes 
2379880b7a5SJesse Barnes u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
2389880b7a5SJesse Barnes {
2399880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2409880b7a5SJesse Barnes 	int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
2419880b7a5SJesse Barnes 
2429880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
24344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
24444d98a61SZhao Yakui 					"pipe %d\n", pipe);
2459880b7a5SJesse Barnes 		return 0;
2469880b7a5SJesse Barnes 	}
2479880b7a5SJesse Barnes 
2489880b7a5SJesse Barnes 	return I915_READ(reg);
2499880b7a5SJesse Barnes }
2509880b7a5SJesse Barnes 
2510af7e4dfSMario Kleiner int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
2520af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
2530af7e4dfSMario Kleiner {
2540af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2550af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
2560af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
2570af7e4dfSMario Kleiner 	bool in_vbl = true;
2580af7e4dfSMario Kleiner 	int ret = 0;
2590af7e4dfSMario Kleiner 
2600af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
2610af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
2620af7e4dfSMario Kleiner 					"pipe %d\n", pipe);
2630af7e4dfSMario Kleiner 		return 0;
2640af7e4dfSMario Kleiner 	}
2650af7e4dfSMario Kleiner 
2660af7e4dfSMario Kleiner 	/* Get vtotal. */
2670af7e4dfSMario Kleiner 	vtotal = 1 + ((I915_READ(VTOTAL(pipe)) >> 16) & 0x1fff);
2680af7e4dfSMario Kleiner 
2690af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
2700af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
2710af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
2720af7e4dfSMario Kleiner 		 */
2730af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
2740af7e4dfSMario Kleiner 
2750af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
2760af7e4dfSMario Kleiner 		 * horizontal scanout position.
2770af7e4dfSMario Kleiner 		 */
2780af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
2790af7e4dfSMario Kleiner 		*hpos = 0;
2800af7e4dfSMario Kleiner 	} else {
2810af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
2820af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
2830af7e4dfSMario Kleiner 		 * scanout position.
2840af7e4dfSMario Kleiner 		 */
2850af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
2860af7e4dfSMario Kleiner 
2870af7e4dfSMario Kleiner 		htotal = 1 + ((I915_READ(HTOTAL(pipe)) >> 16) & 0x1fff);
2880af7e4dfSMario Kleiner 		*vpos = position / htotal;
2890af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
2900af7e4dfSMario Kleiner 	}
2910af7e4dfSMario Kleiner 
2920af7e4dfSMario Kleiner 	/* Query vblank area. */
2930af7e4dfSMario Kleiner 	vbl = I915_READ(VBLANK(pipe));
2940af7e4dfSMario Kleiner 
2950af7e4dfSMario Kleiner 	/* Test position against vblank region. */
2960af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
2970af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
2980af7e4dfSMario Kleiner 
2990af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
3000af7e4dfSMario Kleiner 		in_vbl = false;
3010af7e4dfSMario Kleiner 
3020af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
3030af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
3040af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
3050af7e4dfSMario Kleiner 
3060af7e4dfSMario Kleiner 	/* Readouts valid? */
3070af7e4dfSMario Kleiner 	if (vbl > 0)
3080af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
3090af7e4dfSMario Kleiner 
3100af7e4dfSMario Kleiner 	/* In vblank? */
3110af7e4dfSMario Kleiner 	if (in_vbl)
3120af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
3130af7e4dfSMario Kleiner 
3140af7e4dfSMario Kleiner 	return ret;
3150af7e4dfSMario Kleiner }
3160af7e4dfSMario Kleiner 
3170af7e4dfSMario Kleiner int i915_get_vblank_timestamp(struct drm_device *dev, int crtc,
3180af7e4dfSMario Kleiner 			      int *max_error,
3190af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
3200af7e4dfSMario Kleiner 			      unsigned flags)
3210af7e4dfSMario Kleiner {
3220af7e4dfSMario Kleiner 	struct drm_crtc *drmcrtc;
3230af7e4dfSMario Kleiner 
3240af7e4dfSMario Kleiner 	if (crtc < 0 || crtc >= dev->num_crtcs) {
3250af7e4dfSMario Kleiner 		DRM_ERROR("Invalid crtc %d\n", crtc);
3260af7e4dfSMario Kleiner 		return -EINVAL;
3270af7e4dfSMario Kleiner 	}
3280af7e4dfSMario Kleiner 
3290af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
3300af7e4dfSMario Kleiner 	drmcrtc = intel_get_crtc_for_pipe(dev, crtc);
3310af7e4dfSMario Kleiner 
3320af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
3330af7e4dfSMario Kleiner 	return drm_calc_vbltimestamp_from_scanoutpos(dev, crtc, max_error,
3340af7e4dfSMario Kleiner 						     vblank_time, flags, drmcrtc);
3350af7e4dfSMario Kleiner }
3360af7e4dfSMario Kleiner 
3375ca58282SJesse Barnes /*
3385ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
3395ca58282SJesse Barnes  */
3405ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
3415ca58282SJesse Barnes {
3425ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3435ca58282SJesse Barnes 						    hotplug_work);
3445ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
345c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
3464ef69c7aSChris Wilson 	struct intel_encoder *encoder;
3475ca58282SJesse Barnes 
3484ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
3494ef69c7aSChris Wilson 		if (encoder->hot_plug)
3504ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
351c31c4ba3SKeith Packard 
3525ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
353eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3545ca58282SJesse Barnes }
3555ca58282SJesse Barnes 
356f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
357f97108d1SJesse Barnes {
358f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
359b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
360f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
361f97108d1SJesse Barnes 
3627648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
363b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
364b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
365f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
366f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
367f97108d1SJesse Barnes 
368f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
369b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
370f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
371f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
372f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
373f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
374b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
375f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
376f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
377f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
378f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
379f97108d1SJesse Barnes 	}
380f97108d1SJesse Barnes 
3817648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
382f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
383f97108d1SJesse Barnes 
384f97108d1SJesse Barnes 	return;
385f97108d1SJesse Barnes }
386f97108d1SJesse Barnes 
387549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
388549f7365SChris Wilson 			struct intel_ring_buffer *ring)
389549f7365SChris Wilson {
390549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
39178501eacSChris Wilson 	u32 seqno = ring->get_seqno(ring);
392b2223497SChris Wilson 	ring->irq_seqno = seqno;
393549f7365SChris Wilson 	trace_i915_gem_request_complete(dev, seqno);
394549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
395549f7365SChris Wilson 	dev_priv->hangcheck_count = 0;
396549f7365SChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
397549f7365SChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
398549f7365SChris Wilson }
399549f7365SChris Wilson 
400*3b8d8d91SJesse Barnes static void gen6_pm_irq_handler(struct drm_device *dev)
401*3b8d8d91SJesse Barnes {
402*3b8d8d91SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
403*3b8d8d91SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
404*3b8d8d91SJesse Barnes 	u32 pm_iir;
405*3b8d8d91SJesse Barnes 
406*3b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
407*3b8d8d91SJesse Barnes 	if (!pm_iir)
408*3b8d8d91SJesse Barnes 		return;
409*3b8d8d91SJesse Barnes 
410*3b8d8d91SJesse Barnes 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
411*3b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
412*3b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
413*3b8d8d91SJesse Barnes 		if (new_delay > dev_priv->max_delay)
414*3b8d8d91SJesse Barnes 			new_delay = dev_priv->max_delay;
415*3b8d8d91SJesse Barnes 	} else if (pm_iir & (GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT)) {
416*3b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
417*3b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
418*3b8d8d91SJesse Barnes 		if (new_delay < dev_priv->min_delay) {
419*3b8d8d91SJesse Barnes 			new_delay = dev_priv->min_delay;
420*3b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
421*3b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) |
422*3b8d8d91SJesse Barnes 				   ((new_delay << 16) & 0x3f0000));
423*3b8d8d91SJesse Barnes 		} else {
424*3b8d8d91SJesse Barnes 			/* Make sure we continue to get down interrupts
425*3b8d8d91SJesse Barnes 			 * until we hit the minimum frequency */
426*3b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
427*3b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) & ~0x3f0000);
428*3b8d8d91SJesse Barnes 		}
429*3b8d8d91SJesse Barnes 
430*3b8d8d91SJesse Barnes 	}
431*3b8d8d91SJesse Barnes 
432*3b8d8d91SJesse Barnes 	gen6_set_rps(dev, new_delay);
433*3b8d8d91SJesse Barnes 	dev_priv->cur_delay = new_delay;
434*3b8d8d91SJesse Barnes 
435*3b8d8d91SJesse Barnes 	I915_WRITE(GEN6_PMIIR, pm_iir);
436*3b8d8d91SJesse Barnes }
437*3b8d8d91SJesse Barnes 
438995b6762SChris Wilson static irqreturn_t ironlake_irq_handler(struct drm_device *dev)
439036a4a7dSZhenyu Wang {
440036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
441036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
442*3b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
4432d7b8366SYuanhan Liu 	u32 hotplug_mask;
444036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
445881f47b6SXiang, Haihao 	u32 bsd_usr_interrupt = GT_BSD_USER_INTERRUPT;
446881f47b6SXiang, Haihao 
447881f47b6SXiang, Haihao 	if (IS_GEN6(dev))
448881f47b6SXiang, Haihao 		bsd_usr_interrupt = GT_GEN6_BSD_USER_INTERRUPT;
449036a4a7dSZhenyu Wang 
4502d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
4512d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
4522d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
4533143a2bfSChris Wilson 	POSTING_READ(DEIER);
4542d109a84SZou, Nanhai 
455036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
456036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
457c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
458*3b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
459036a4a7dSZhenyu Wang 
460*3b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
461*3b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
462c7c85101SZou Nan hai 		goto done;
463036a4a7dSZhenyu Wang 
4642d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
4652d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
4662d7b8366SYuanhan Liu 	else
4672d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
4682d7b8366SYuanhan Liu 
469036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
470036a4a7dSZhenyu Wang 
471036a4a7dSZhenyu Wang 	if (dev->primary->master) {
472036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
473036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
474036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
475036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
476036a4a7dSZhenyu Wang 	}
477036a4a7dSZhenyu Wang 
478c6df541cSChris Wilson 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
4791ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[RCS]);
480881f47b6SXiang, Haihao 	if (gt_iir & bsd_usr_interrupt)
4811ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[VCS]);
4821ec14ad3SChris Wilson 	if (gt_iir & GT_BLT_USER_INTERRUPT)
4831ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[BCS]);
484036a4a7dSZhenyu Wang 
48501c66889SZhao Yakui 	if (de_iir & DE_GSE)
4863b617967SChris Wilson 		intel_opregion_gse_intr(dev);
48701c66889SZhao Yakui 
488f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
489013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
4902bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
491013d5aa2SJesse Barnes 	}
492013d5aa2SJesse Barnes 
493f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
494f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
4952bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
496013d5aa2SJesse Barnes 	}
497c062df61SLi Peng 
498f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
499f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
500f072d2e7SZhenyu Wang 
501f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
502f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
503f072d2e7SZhenyu Wang 
504c650156aSZhenyu Wang 	/* check event from PCH */
5052d7b8366SYuanhan Liu 	if ((de_iir & DE_PCH_EVENT) && (pch_iir & hotplug_mask))
506c650156aSZhenyu Wang 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
507c650156aSZhenyu Wang 
508f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
5097648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
510f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
511f97108d1SJesse Barnes 	}
512f97108d1SJesse Barnes 
513*3b8d8d91SJesse Barnes 	if (IS_GEN6(dev))
514*3b8d8d91SJesse Barnes 		gen6_pm_irq_handler(dev);
515*3b8d8d91SJesse Barnes 
516c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
517c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
518c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
519c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
520036a4a7dSZhenyu Wang 
521c7c85101SZou Nan hai done:
5222d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
5233143a2bfSChris Wilson 	POSTING_READ(DEIER);
5242d109a84SZou, Nanhai 
525036a4a7dSZhenyu Wang 	return ret;
526036a4a7dSZhenyu Wang }
527036a4a7dSZhenyu Wang 
5288a905236SJesse Barnes /**
5298a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
5308a905236SJesse Barnes  * @work: work struct
5318a905236SJesse Barnes  *
5328a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
5338a905236SJesse Barnes  * was detected.
5348a905236SJesse Barnes  */
5358a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
5368a905236SJesse Barnes {
5378a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5388a905236SJesse Barnes 						    error_work);
5398a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
540f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
541f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
542f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
5438a905236SJesse Barnes 
544f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
5458a905236SJesse Barnes 
546ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
54744d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
548f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
549f803aa55SChris Wilson 		if (!i915_reset(dev, GRDOM_RENDER)) {
550ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
551f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
552f316a42cSBen Gamari 		}
55330dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
554f316a42cSBen Gamari 	}
5558a905236SJesse Barnes }
5568a905236SJesse Barnes 
5573bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
5589df30794SChris Wilson static struct drm_i915_error_object *
5599df30794SChris Wilson i915_error_object_create(struct drm_device *dev,
56005394f39SChris Wilson 			 struct drm_i915_gem_object *src)
5619df30794SChris Wilson {
562e56660ddSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
5639df30794SChris Wilson 	struct drm_i915_error_object *dst;
5649df30794SChris Wilson 	int page, page_count;
565e56660ddSChris Wilson 	u32 reloc_offset;
5669df30794SChris Wilson 
56705394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
5689df30794SChris Wilson 		return NULL;
5699df30794SChris Wilson 
57005394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
5719df30794SChris Wilson 
5729df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof (u32 *), GFP_ATOMIC);
5739df30794SChris Wilson 	if (dst == NULL)
5749df30794SChris Wilson 		return NULL;
5759df30794SChris Wilson 
57605394f39SChris Wilson 	reloc_offset = src->gtt_offset;
5779df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
578788885aeSAndrew Morton 		unsigned long flags;
579e56660ddSChris Wilson 		void __iomem *s;
580e56660ddSChris Wilson 		void *d;
581788885aeSAndrew Morton 
582e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
5839df30794SChris Wilson 		if (d == NULL)
5849df30794SChris Wilson 			goto unwind;
585e56660ddSChris Wilson 
586788885aeSAndrew Morton 		local_irq_save(flags);
587e56660ddSChris Wilson 		s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
5883e4d3af5SPeter Zijlstra 					     reloc_offset);
589e56660ddSChris Wilson 		memcpy_fromio(d, s, PAGE_SIZE);
5903e4d3af5SPeter Zijlstra 		io_mapping_unmap_atomic(s);
591788885aeSAndrew Morton 		local_irq_restore(flags);
592e56660ddSChris Wilson 
5939df30794SChris Wilson 		dst->pages[page] = d;
594e56660ddSChris Wilson 
595e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
5969df30794SChris Wilson 	}
5979df30794SChris Wilson 	dst->page_count = page_count;
59805394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
5999df30794SChris Wilson 
6009df30794SChris Wilson 	return dst;
6019df30794SChris Wilson 
6029df30794SChris Wilson unwind:
6039df30794SChris Wilson 	while (page--)
6049df30794SChris Wilson 		kfree(dst->pages[page]);
6059df30794SChris Wilson 	kfree(dst);
6069df30794SChris Wilson 	return NULL;
6079df30794SChris Wilson }
6089df30794SChris Wilson 
6099df30794SChris Wilson static void
6109df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
6119df30794SChris Wilson {
6129df30794SChris Wilson 	int page;
6139df30794SChris Wilson 
6149df30794SChris Wilson 	if (obj == NULL)
6159df30794SChris Wilson 		return;
6169df30794SChris Wilson 
6179df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
6189df30794SChris Wilson 		kfree(obj->pages[page]);
6199df30794SChris Wilson 
6209df30794SChris Wilson 	kfree(obj);
6219df30794SChris Wilson }
6229df30794SChris Wilson 
6239df30794SChris Wilson static void
6249df30794SChris Wilson i915_error_state_free(struct drm_device *dev,
6259df30794SChris Wilson 		      struct drm_i915_error_state *error)
6269df30794SChris Wilson {
6279df30794SChris Wilson 	i915_error_object_free(error->batchbuffer[0]);
6289df30794SChris Wilson 	i915_error_object_free(error->batchbuffer[1]);
6299df30794SChris Wilson 	i915_error_object_free(error->ringbuffer);
6309df30794SChris Wilson 	kfree(error->active_bo);
6316ef3d427SChris Wilson 	kfree(error->overlay);
6329df30794SChris Wilson 	kfree(error);
6339df30794SChris Wilson }
6349df30794SChris Wilson 
6359df30794SChris Wilson static u32
6369df30794SChris Wilson i915_get_bbaddr(struct drm_device *dev, u32 *ring)
6379df30794SChris Wilson {
6389df30794SChris Wilson 	u32 cmd;
6399df30794SChris Wilson 
6409df30794SChris Wilson 	if (IS_I830(dev) || IS_845G(dev))
6419df30794SChris Wilson 		cmd = MI_BATCH_BUFFER;
642a6c45cf0SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 4)
6439df30794SChris Wilson 		cmd = (MI_BATCH_BUFFER_START | (2 << 6) |
6449df30794SChris Wilson 		       MI_BATCH_NON_SECURE_I965);
6459df30794SChris Wilson 	else
6469df30794SChris Wilson 		cmd = (MI_BATCH_BUFFER_START | (2 << 6));
6479df30794SChris Wilson 
6489df30794SChris Wilson 	return ring[0] == cmd ? ring[1] : 0;
6499df30794SChris Wilson }
6509df30794SChris Wilson 
6519df30794SChris Wilson static u32
6528168bd48SChris Wilson i915_ringbuffer_last_batch(struct drm_device *dev,
6538168bd48SChris Wilson 			   struct intel_ring_buffer *ring)
6549df30794SChris Wilson {
6559df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
6569df30794SChris Wilson 	u32 head, bbaddr;
6578168bd48SChris Wilson 	u32 *val;
6589df30794SChris Wilson 
6599df30794SChris Wilson 	/* Locate the current position in the ringbuffer and walk back
6609df30794SChris Wilson 	 * to find the most recently dispatched batch buffer.
6619df30794SChris Wilson 	 */
6628168bd48SChris Wilson 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
6639df30794SChris Wilson 
664ab5793adSChris Wilson 	val = (u32 *)(ring->virtual_start + head);
6658168bd48SChris Wilson 	while (--val >= (u32 *)ring->virtual_start) {
6668168bd48SChris Wilson 		bbaddr = i915_get_bbaddr(dev, val);
6679df30794SChris Wilson 		if (bbaddr)
668ab5793adSChris Wilson 			return bbaddr;
6699df30794SChris Wilson 	}
6709df30794SChris Wilson 
6718168bd48SChris Wilson 	val = (u32 *)(ring->virtual_start + ring->size);
6728168bd48SChris Wilson 	while (--val >= (u32 *)ring->virtual_start) {
6738168bd48SChris Wilson 		bbaddr = i915_get_bbaddr(dev, val);
6749df30794SChris Wilson 		if (bbaddr)
675ab5793adSChris Wilson 			return bbaddr;
6769df30794SChris Wilson 	}
6779df30794SChris Wilson 
678ab5793adSChris Wilson 	return 0;
6799df30794SChris Wilson }
6809df30794SChris Wilson 
681c724e8a9SChris Wilson static u32 capture_bo_list(struct drm_i915_error_buffer *err,
682c724e8a9SChris Wilson 			   int count,
683c724e8a9SChris Wilson 			   struct list_head *head)
684c724e8a9SChris Wilson {
685c724e8a9SChris Wilson 	struct drm_i915_gem_object *obj;
686c724e8a9SChris Wilson 	int i = 0;
687c724e8a9SChris Wilson 
688c724e8a9SChris Wilson 	list_for_each_entry(obj, head, mm_list) {
689c724e8a9SChris Wilson 		err->size = obj->base.size;
690c724e8a9SChris Wilson 		err->name = obj->base.name;
691c724e8a9SChris Wilson 		err->seqno = obj->last_rendering_seqno;
692c724e8a9SChris Wilson 		err->gtt_offset = obj->gtt_offset;
693c724e8a9SChris Wilson 		err->read_domains = obj->base.read_domains;
694c724e8a9SChris Wilson 		err->write_domain = obj->base.write_domain;
695c724e8a9SChris Wilson 		err->fence_reg = obj->fence_reg;
696c724e8a9SChris Wilson 		err->pinned = 0;
697c724e8a9SChris Wilson 		if (obj->pin_count > 0)
698c724e8a9SChris Wilson 			err->pinned = 1;
699c724e8a9SChris Wilson 		if (obj->user_pin_count > 0)
700c724e8a9SChris Wilson 			err->pinned = -1;
701c724e8a9SChris Wilson 		err->tiling = obj->tiling_mode;
702c724e8a9SChris Wilson 		err->dirty = obj->dirty;
703c724e8a9SChris Wilson 		err->purgeable = obj->madv != I915_MADV_WILLNEED;
7043685092bSChris Wilson 		err->ring = obj->ring ? obj->ring->id : 0;
705c724e8a9SChris Wilson 
706c724e8a9SChris Wilson 		if (++i == count)
707c724e8a9SChris Wilson 			break;
708c724e8a9SChris Wilson 
709c724e8a9SChris Wilson 		err++;
710c724e8a9SChris Wilson 	}
711c724e8a9SChris Wilson 
712c724e8a9SChris Wilson 	return i;
713c724e8a9SChris Wilson }
714c724e8a9SChris Wilson 
715748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
716748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
717748ebc60SChris Wilson {
718748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
719748ebc60SChris Wilson 	int i;
720748ebc60SChris Wilson 
721748ebc60SChris Wilson 	/* Fences */
722748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
723748ebc60SChris Wilson 	case 6:
724748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
725748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
726748ebc60SChris Wilson 		break;
727748ebc60SChris Wilson 	case 5:
728748ebc60SChris Wilson 	case 4:
729748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
730748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
731748ebc60SChris Wilson 		break;
732748ebc60SChris Wilson 	case 3:
733748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
734748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
735748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
736748ebc60SChris Wilson 	case 2:
737748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
738748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
739748ebc60SChris Wilson 		break;
740748ebc60SChris Wilson 
741748ebc60SChris Wilson 	}
742748ebc60SChris Wilson }
743748ebc60SChris Wilson 
7448a905236SJesse Barnes /**
7458a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
7468a905236SJesse Barnes  * @dev: drm device
7478a905236SJesse Barnes  *
7488a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
7498a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
7508a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
7518a905236SJesse Barnes  * to pick up.
7528a905236SJesse Barnes  */
75363eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
75463eeaf38SJesse Barnes {
75563eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
75605394f39SChris Wilson 	struct drm_i915_gem_object *obj;
75763eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
75805394f39SChris Wilson 	struct drm_i915_gem_object *batchbuffer[2];
75963eeaf38SJesse Barnes 	unsigned long flags;
7609df30794SChris Wilson 	u32 bbaddr;
7619df30794SChris Wilson 	int count;
76263eeaf38SJesse Barnes 
76363eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
7649df30794SChris Wilson 	error = dev_priv->first_error;
7659df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
7669df30794SChris Wilson 	if (error)
7679df30794SChris Wilson 		return;
76863eeaf38SJesse Barnes 
76963eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
77063eeaf38SJesse Barnes 	if (!error) {
7719df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
7729df30794SChris Wilson 		return;
77363eeaf38SJesse Barnes 	}
77463eeaf38SJesse Barnes 
7752fa772f3SChris Wilson 	DRM_DEBUG_DRIVER("generating error event\n");
7762fa772f3SChris Wilson 
7771ec14ad3SChris Wilson 	error->seqno = dev_priv->ring[RCS].get_seqno(&dev_priv->ring[RCS]);
77863eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
77963eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
78063eeaf38SJesse Barnes 	error->pipeastat = I915_READ(PIPEASTAT);
78163eeaf38SJesse Barnes 	error->pipebstat = I915_READ(PIPEBSTAT);
78263eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
783f406839fSChris Wilson 	error->error = 0;
784f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 6) {
785f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
786add354ddSChris Wilson 
7871d8f38f4SChris Wilson 		error->bcs_acthd = I915_READ(BCS_ACTHD);
7881d8f38f4SChris Wilson 		error->bcs_ipehr = I915_READ(BCS_IPEHR);
7891d8f38f4SChris Wilson 		error->bcs_ipeir = I915_READ(BCS_IPEIR);
7901d8f38f4SChris Wilson 		error->bcs_instdone = I915_READ(BCS_INSTDONE);
7911d8f38f4SChris Wilson 		error->bcs_seqno = 0;
7921ec14ad3SChris Wilson 		if (dev_priv->ring[BCS].get_seqno)
7931ec14ad3SChris Wilson 			error->bcs_seqno = dev_priv->ring[BCS].get_seqno(&dev_priv->ring[BCS]);
794add354ddSChris Wilson 
795add354ddSChris Wilson 		error->vcs_acthd = I915_READ(VCS_ACTHD);
796add354ddSChris Wilson 		error->vcs_ipehr = I915_READ(VCS_IPEHR);
797add354ddSChris Wilson 		error->vcs_ipeir = I915_READ(VCS_IPEIR);
798add354ddSChris Wilson 		error->vcs_instdone = I915_READ(VCS_INSTDONE);
799add354ddSChris Wilson 		error->vcs_seqno = 0;
8001ec14ad3SChris Wilson 		if (dev_priv->ring[VCS].get_seqno)
8011ec14ad3SChris Wilson 			error->vcs_seqno = dev_priv->ring[VCS].get_seqno(&dev_priv->ring[VCS]);
802f406839fSChris Wilson 	}
803f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
80463eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
80563eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
80663eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
80763eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
80863eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
80963eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
8109df30794SChris Wilson 		error->bbaddr = I915_READ64(BB_ADDR);
811f406839fSChris Wilson 	} else {
812f406839fSChris Wilson 		error->ipeir = I915_READ(IPEIR);
813f406839fSChris Wilson 		error->ipehr = I915_READ(IPEHR);
814f406839fSChris Wilson 		error->instdone = I915_READ(INSTDONE);
815f406839fSChris Wilson 		error->acthd = I915_READ(ACTHD);
816f406839fSChris Wilson 		error->bbaddr = 0;
8179df30794SChris Wilson 	}
818748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
8199df30794SChris Wilson 
8201ec14ad3SChris Wilson 	bbaddr = i915_ringbuffer_last_batch(dev, &dev_priv->ring[RCS]);
8219df30794SChris Wilson 
8229df30794SChris Wilson 	/* Grab the current batchbuffer, most likely to have crashed. */
8239df30794SChris Wilson 	batchbuffer[0] = NULL;
8249df30794SChris Wilson 	batchbuffer[1] = NULL;
8259df30794SChris Wilson 	count = 0;
82605394f39SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
8279df30794SChris Wilson 		if (batchbuffer[0] == NULL &&
82805394f39SChris Wilson 		    bbaddr >= obj->gtt_offset &&
82905394f39SChris Wilson 		    bbaddr < obj->gtt_offset + obj->base.size)
8309df30794SChris Wilson 			batchbuffer[0] = obj;
8319df30794SChris Wilson 
8329df30794SChris Wilson 		if (batchbuffer[1] == NULL &&
83305394f39SChris Wilson 		    error->acthd >= obj->gtt_offset &&
83405394f39SChris Wilson 		    error->acthd < obj->gtt_offset + obj->base.size)
8359df30794SChris Wilson 			batchbuffer[1] = obj;
8369df30794SChris Wilson 
8379df30794SChris Wilson 		count++;
8389df30794SChris Wilson 	}
839e56660ddSChris Wilson 	/* Scan the other lists for completeness for those bizarre errors. */
840e56660ddSChris Wilson 	if (batchbuffer[0] == NULL || batchbuffer[1] == NULL) {
84105394f39SChris Wilson 		list_for_each_entry(obj, &dev_priv->mm.flushing_list, mm_list) {
842e56660ddSChris Wilson 			if (batchbuffer[0] == NULL &&
84305394f39SChris Wilson 			    bbaddr >= obj->gtt_offset &&
84405394f39SChris Wilson 			    bbaddr < obj->gtt_offset + obj->base.size)
845e56660ddSChris Wilson 				batchbuffer[0] = obj;
846e56660ddSChris Wilson 
847e56660ddSChris Wilson 			if (batchbuffer[1] == NULL &&
84805394f39SChris Wilson 			    error->acthd >= obj->gtt_offset &&
84905394f39SChris Wilson 			    error->acthd < obj->gtt_offset + obj->base.size)
850e56660ddSChris Wilson 				batchbuffer[1] = obj;
851e56660ddSChris Wilson 
852e56660ddSChris Wilson 			if (batchbuffer[0] && batchbuffer[1])
853e56660ddSChris Wilson 				break;
854e56660ddSChris Wilson 		}
855e56660ddSChris Wilson 	}
856e56660ddSChris Wilson 	if (batchbuffer[0] == NULL || batchbuffer[1] == NULL) {
85705394f39SChris Wilson 		list_for_each_entry(obj, &dev_priv->mm.inactive_list, mm_list) {
858e56660ddSChris Wilson 			if (batchbuffer[0] == NULL &&
85905394f39SChris Wilson 			    bbaddr >= obj->gtt_offset &&
86005394f39SChris Wilson 			    bbaddr < obj->gtt_offset + obj->base.size)
861e56660ddSChris Wilson 				batchbuffer[0] = obj;
862e56660ddSChris Wilson 
863e56660ddSChris Wilson 			if (batchbuffer[1] == NULL &&
86405394f39SChris Wilson 			    error->acthd >= obj->gtt_offset &&
86505394f39SChris Wilson 			    error->acthd < obj->gtt_offset + obj->base.size)
866e56660ddSChris Wilson 				batchbuffer[1] = obj;
867e56660ddSChris Wilson 
868e56660ddSChris Wilson 			if (batchbuffer[0] && batchbuffer[1])
869e56660ddSChris Wilson 				break;
870e56660ddSChris Wilson 		}
871e56660ddSChris Wilson 	}
8729df30794SChris Wilson 
8739df30794SChris Wilson 	/* We need to copy these to an anonymous buffer as the simplest
874139d363bSAndrea Gelmini 	 * method to avoid being overwritten by userspace.
8759df30794SChris Wilson 	 */
8769df30794SChris Wilson 	error->batchbuffer[0] = i915_error_object_create(dev, batchbuffer[0]);
877e56660ddSChris Wilson 	if (batchbuffer[1] != batchbuffer[0])
8789df30794SChris Wilson 		error->batchbuffer[1] = i915_error_object_create(dev, batchbuffer[1]);
879e56660ddSChris Wilson 	else
880e56660ddSChris Wilson 		error->batchbuffer[1] = NULL;
8819df30794SChris Wilson 
8829df30794SChris Wilson 	/* Record the ringbuffer */
8838187a2b7SZou Nan hai 	error->ringbuffer = i915_error_object_create(dev,
8841ec14ad3SChris Wilson 						     dev_priv->ring[RCS].obj);
8859df30794SChris Wilson 
886c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
8879df30794SChris Wilson 	error->active_bo = NULL;
888c724e8a9SChris Wilson 	error->pinned_bo = NULL;
8899df30794SChris Wilson 
890c724e8a9SChris Wilson 	error->active_bo_count = count;
89105394f39SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
892c724e8a9SChris Wilson 		count++;
893c724e8a9SChris Wilson 	error->pinned_bo_count = count - error->active_bo_count;
894c724e8a9SChris Wilson 
895c724e8a9SChris Wilson 	if (count) {
8969df30794SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*count,
8979df30794SChris Wilson 					   GFP_ATOMIC);
898c724e8a9SChris Wilson 		if (error->active_bo)
899c724e8a9SChris Wilson 			error->pinned_bo =
900c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
9019df30794SChris Wilson 	}
902c724e8a9SChris Wilson 
903c724e8a9SChris Wilson 	if (error->active_bo)
904c724e8a9SChris Wilson 		error->active_bo_count =
905c724e8a9SChris Wilson 			capture_bo_list(error->active_bo,
906c724e8a9SChris Wilson 					error->active_bo_count,
907c724e8a9SChris Wilson 					&dev_priv->mm.active_list);
908c724e8a9SChris Wilson 
909c724e8a9SChris Wilson 	if (error->pinned_bo)
910c724e8a9SChris Wilson 		error->pinned_bo_count =
911c724e8a9SChris Wilson 			capture_bo_list(error->pinned_bo,
912c724e8a9SChris Wilson 					error->pinned_bo_count,
913c724e8a9SChris Wilson 					&dev_priv->mm.pinned_list);
91463eeaf38SJesse Barnes 
9158a905236SJesse Barnes 	do_gettimeofday(&error->time);
9168a905236SJesse Barnes 
9176ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
918c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
9196ef3d427SChris Wilson 
9209df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
9219df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
92263eeaf38SJesse Barnes 		dev_priv->first_error = error;
9239df30794SChris Wilson 		error = NULL;
9249df30794SChris Wilson 	}
92563eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
9269df30794SChris Wilson 
9279df30794SChris Wilson 	if (error)
9289df30794SChris Wilson 		i915_error_state_free(dev, error);
9299df30794SChris Wilson }
9309df30794SChris Wilson 
9319df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
9329df30794SChris Wilson {
9339df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
9349df30794SChris Wilson 	struct drm_i915_error_state *error;
9359df30794SChris Wilson 
9369df30794SChris Wilson 	spin_lock(&dev_priv->error_lock);
9379df30794SChris Wilson 	error = dev_priv->first_error;
9389df30794SChris Wilson 	dev_priv->first_error = NULL;
9399df30794SChris Wilson 	spin_unlock(&dev_priv->error_lock);
9409df30794SChris Wilson 
9419df30794SChris Wilson 	if (error)
9429df30794SChris Wilson 		i915_error_state_free(dev, error);
94363eeaf38SJesse Barnes }
9443bd3c932SChris Wilson #else
9453bd3c932SChris Wilson #define i915_capture_error_state(x)
9463bd3c932SChris Wilson #endif
94763eeaf38SJesse Barnes 
94835aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
949c0e09200SDave Airlie {
9508a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
95163eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
95263eeaf38SJesse Barnes 
95335aed2e6SChris Wilson 	if (!eir)
95435aed2e6SChris Wilson 		return;
95563eeaf38SJesse Barnes 
95663eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
95763eeaf38SJesse Barnes 	       eir);
9588a905236SJesse Barnes 
9598a905236SJesse Barnes 	if (IS_G4X(dev)) {
9608a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
9618a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
9628a905236SJesse Barnes 
9638a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
9648a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
9658a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
9668a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
9678a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
9688a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
9698a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
9708a905236SJesse Barnes 			       I915_READ(INSTPS));
9718a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
9728a905236SJesse Barnes 			       I915_READ(INSTDONE1));
9738a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
9748a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
9758a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
9763143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
9778a905236SJesse Barnes 		}
9788a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
9798a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
9808a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
9818a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
9828a905236SJesse Barnes 			       pgtbl_err);
9838a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
9843143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
9858a905236SJesse Barnes 		}
9868a905236SJesse Barnes 	}
9878a905236SJesse Barnes 
988a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
98963eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
99063eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
99163eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
99263eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
99363eeaf38SJesse Barnes 			       pgtbl_err);
99463eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
9953143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
99663eeaf38SJesse Barnes 		}
9978a905236SJesse Barnes 	}
9988a905236SJesse Barnes 
99963eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
100035aed2e6SChris Wilson 		u32 pipea_stats = I915_READ(PIPEASTAT);
100135aed2e6SChris Wilson 		u32 pipeb_stats = I915_READ(PIPEBSTAT);
100235aed2e6SChris Wilson 
100363eeaf38SJesse Barnes 		printk(KERN_ERR "memory refresh error\n");
100463eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
100563eeaf38SJesse Barnes 		       pipea_stats);
100663eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
100763eeaf38SJesse Barnes 		       pipeb_stats);
100863eeaf38SJesse Barnes 		/* pipestat has already been acked */
100963eeaf38SJesse Barnes 	}
101063eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
101163eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
101263eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
101363eeaf38SJesse Barnes 		       I915_READ(INSTPM));
1014a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
101563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
101663eeaf38SJesse Barnes 
101763eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
101863eeaf38SJesse Barnes 			       I915_READ(IPEIR));
101963eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
102063eeaf38SJesse Barnes 			       I915_READ(IPEHR));
102163eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
102263eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
102363eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
102463eeaf38SJesse Barnes 			       I915_READ(ACTHD));
102563eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
10263143a2bfSChris Wilson 			POSTING_READ(IPEIR);
102763eeaf38SJesse Barnes 		} else {
102863eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
102963eeaf38SJesse Barnes 
103063eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
103163eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
103263eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
103363eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
103463eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
103563eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
103663eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
103763eeaf38SJesse Barnes 			       I915_READ(INSTPS));
103863eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
103963eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
104063eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
104163eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
104263eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
10433143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
104463eeaf38SJesse Barnes 		}
104563eeaf38SJesse Barnes 	}
104663eeaf38SJesse Barnes 
104763eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
10483143a2bfSChris Wilson 	POSTING_READ(EIR);
104963eeaf38SJesse Barnes 	eir = I915_READ(EIR);
105063eeaf38SJesse Barnes 	if (eir) {
105163eeaf38SJesse Barnes 		/*
105263eeaf38SJesse Barnes 		 * some errors might have become stuck,
105363eeaf38SJesse Barnes 		 * mask them.
105463eeaf38SJesse Barnes 		 */
105563eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
105663eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
105763eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
105863eeaf38SJesse Barnes 	}
105935aed2e6SChris Wilson }
106035aed2e6SChris Wilson 
106135aed2e6SChris Wilson /**
106235aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
106335aed2e6SChris Wilson  * @dev: drm device
106435aed2e6SChris Wilson  *
106535aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
106635aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
106735aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
106835aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
106935aed2e6SChris Wilson  * of a ring dump etc.).
107035aed2e6SChris Wilson  */
1071527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
107235aed2e6SChris Wilson {
107335aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
107435aed2e6SChris Wilson 
107535aed2e6SChris Wilson 	i915_capture_error_state(dev);
107635aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
10778a905236SJesse Barnes 
1078ba1234d1SBen Gamari 	if (wedged) {
107930dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1080ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1081ba1234d1SBen Gamari 
108211ed50ecSBen Gamari 		/*
108311ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
108411ed50ecSBen Gamari 		 */
10851ec14ad3SChris Wilson 		wake_up_all(&dev_priv->ring[RCS].irq_queue);
1086f787a5f5SChris Wilson 		if (HAS_BSD(dev))
10871ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[VCS].irq_queue);
1088549f7365SChris Wilson 		if (HAS_BLT(dev))
10891ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[BCS].irq_queue);
109011ed50ecSBen Gamari 	}
109111ed50ecSBen Gamari 
10929c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
10938a905236SJesse Barnes }
10948a905236SJesse Barnes 
10954e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
10964e5359cdSSimon Farnsworth {
10974e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
10984e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10994e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
110005394f39SChris Wilson 	struct drm_i915_gem_object *obj;
11014e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
11024e5359cdSSimon Farnsworth 	unsigned long flags;
11034e5359cdSSimon Farnsworth 	bool stall_detected;
11044e5359cdSSimon Farnsworth 
11054e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
11064e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
11074e5359cdSSimon Farnsworth 		return;
11084e5359cdSSimon Farnsworth 
11094e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
11104e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
11114e5359cdSSimon Farnsworth 
11124e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
11134e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
11144e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
11154e5359cdSSimon Farnsworth 		return;
11164e5359cdSSimon Farnsworth 	}
11174e5359cdSSimon Farnsworth 
11184e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
111905394f39SChris Wilson 	obj = work->pending_flip_obj;
1120a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
11214e5359cdSSimon Farnsworth 		int dspsurf = intel_crtc->plane == 0 ? DSPASURF : DSPBSURF;
112205394f39SChris Wilson 		stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
11234e5359cdSSimon Farnsworth 	} else {
11244e5359cdSSimon Farnsworth 		int dspaddr = intel_crtc->plane == 0 ? DSPAADDR : DSPBADDR;
112505394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
11264e5359cdSSimon Farnsworth 							crtc->y * crtc->fb->pitch +
11274e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
11284e5359cdSSimon Farnsworth 	}
11294e5359cdSSimon Farnsworth 
11304e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
11314e5359cdSSimon Farnsworth 
11324e5359cdSSimon Farnsworth 	if (stall_detected) {
11334e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
11344e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
11354e5359cdSSimon Farnsworth 	}
11364e5359cdSSimon Farnsworth }
11374e5359cdSSimon Farnsworth 
11388a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
11398a905236SJesse Barnes {
11408a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
11418a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11428a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
11438a905236SJesse Barnes 	u32 iir, new_iir;
11448a905236SJesse Barnes 	u32 pipea_stats, pipeb_stats;
11458a905236SJesse Barnes 	u32 vblank_status;
11468a905236SJesse Barnes 	int vblank = 0;
11478a905236SJesse Barnes 	unsigned long irqflags;
11488a905236SJesse Barnes 	int irq_received;
11498a905236SJesse Barnes 	int ret = IRQ_NONE;
11508a905236SJesse Barnes 
11518a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
11528a905236SJesse Barnes 
1153bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1154f2b115e6SAdam Jackson 		return ironlake_irq_handler(dev);
11558a905236SJesse Barnes 
11568a905236SJesse Barnes 	iir = I915_READ(IIR);
11578a905236SJesse Barnes 
1158a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
1159d874bcffSJesse Barnes 		vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
1160e25e6601SJesse Barnes 	else
1161d874bcffSJesse Barnes 		vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
11628a905236SJesse Barnes 
11638a905236SJesse Barnes 	for (;;) {
11648a905236SJesse Barnes 		irq_received = iir != 0;
11658a905236SJesse Barnes 
11668a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
11678a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
11688a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
11698a905236SJesse Barnes 		 * interrupts (for non-MSI).
11708a905236SJesse Barnes 		 */
11711ec14ad3SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
11728a905236SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
11738a905236SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
11748a905236SJesse Barnes 
11758a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
1176ba1234d1SBen Gamari 			i915_handle_error(dev, false);
11778a905236SJesse Barnes 
11788a905236SJesse Barnes 		/*
11798a905236SJesse Barnes 		 * Clear the PIPE(A|B)STAT regs before the IIR
11808a905236SJesse Barnes 		 */
11818a905236SJesse Barnes 		if (pipea_stats & 0x8000ffff) {
11828a905236SJesse Barnes 			if (pipea_stats &  PIPE_FIFO_UNDERRUN_STATUS)
118344d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe a underrun\n");
11848a905236SJesse Barnes 			I915_WRITE(PIPEASTAT, pipea_stats);
11858a905236SJesse Barnes 			irq_received = 1;
11868a905236SJesse Barnes 		}
11878a905236SJesse Barnes 
11888a905236SJesse Barnes 		if (pipeb_stats & 0x8000ffff) {
11898a905236SJesse Barnes 			if (pipeb_stats &  PIPE_FIFO_UNDERRUN_STATUS)
119044d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe b underrun\n");
11918a905236SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
11928a905236SJesse Barnes 			irq_received = 1;
11938a905236SJesse Barnes 		}
11941ec14ad3SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
11958a905236SJesse Barnes 
11968a905236SJesse Barnes 		if (!irq_received)
11978a905236SJesse Barnes 			break;
11988a905236SJesse Barnes 
11998a905236SJesse Barnes 		ret = IRQ_HANDLED;
12008a905236SJesse Barnes 
12018a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
12028a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
12038a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
12048a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
12058a905236SJesse Barnes 
120644d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
12078a905236SJesse Barnes 				  hotplug_status);
12088a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
12099c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
12109c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
12118a905236SJesse Barnes 
12128a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
12138a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
121463eeaf38SJesse Barnes 		}
121563eeaf38SJesse Barnes 
1216673a394bSEric Anholt 		I915_WRITE(IIR, iir);
1217cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
12187c463586SKeith Packard 
12197c1c2871SDave Airlie 		if (dev->primary->master) {
12207c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
12217c1c2871SDave Airlie 			if (master_priv->sarea_priv)
12227c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
1223c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
12247c1c2871SDave Airlie 		}
12250a3e67a4SJesse Barnes 
1226549f7365SChris Wilson 		if (iir & I915_USER_INTERRUPT)
12271ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
12281ec14ad3SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
12291ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
1230d1b851fcSZou Nan hai 
12311afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
12326b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
12331afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
12341afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 0);
12351afe3e9dSJesse Barnes 		}
12366b95a207SKristian Høgsberg 
12371afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
123870565d00SJesse Barnes 			intel_prepare_page_flip(dev, 1);
12391afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
12401afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 1);
12411afe3e9dSJesse Barnes 		}
12426b95a207SKristian Høgsberg 
124305eff845SKeith Packard 		if (pipea_stats & vblank_status) {
12447c463586SKeith Packard 			vblank++;
12457c463586SKeith Packard 			drm_handle_vblank(dev, 0);
12464e5359cdSSimon Farnsworth 			if (!dev_priv->flip_pending_is_done) {
12474e5359cdSSimon Farnsworth 				i915_pageflip_stall_check(dev, 0);
12486b95a207SKristian Høgsberg 				intel_finish_page_flip(dev, 0);
12497c463586SKeith Packard 			}
12504e5359cdSSimon Farnsworth 		}
12517c463586SKeith Packard 
125205eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
12537c463586SKeith Packard 			vblank++;
12547c463586SKeith Packard 			drm_handle_vblank(dev, 1);
12554e5359cdSSimon Farnsworth 			if (!dev_priv->flip_pending_is_done) {
12564e5359cdSSimon Farnsworth 				i915_pageflip_stall_check(dev, 1);
12576b95a207SKristian Høgsberg 				intel_finish_page_flip(dev, 1);
12587c463586SKeith Packard 			}
12594e5359cdSSimon Farnsworth 		}
12607c463586SKeith Packard 
1261d874bcffSJesse Barnes 		if ((pipea_stats & PIPE_LEGACY_BLC_EVENT_STATUS) ||
1262d874bcffSJesse Barnes 		    (pipeb_stats & PIPE_LEGACY_BLC_EVENT_STATUS) ||
12637c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
12643b617967SChris Wilson 			intel_opregion_asle_intr(dev);
12650a3e67a4SJesse Barnes 
1266cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
1267cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
1268cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
1269cdfbc41fSEric Anholt 		 * we would never get another interrupt.
1270cdfbc41fSEric Anholt 		 *
1271cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
1272cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
1273cdfbc41fSEric Anholt 		 * another one.
1274cdfbc41fSEric Anholt 		 *
1275cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
1276cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
1277cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
1278cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
1279cdfbc41fSEric Anholt 		 * stray interrupts.
1280cdfbc41fSEric Anholt 		 */
1281cdfbc41fSEric Anholt 		iir = new_iir;
128205eff845SKeith Packard 	}
1283cdfbc41fSEric Anholt 
128405eff845SKeith Packard 	return ret;
1285c0e09200SDave Airlie }
1286c0e09200SDave Airlie 
1287c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
1288c0e09200SDave Airlie {
1289c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
12907c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1291c0e09200SDave Airlie 
1292c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
1293c0e09200SDave Airlie 
129444d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
1295c0e09200SDave Airlie 
1296c99b058fSKristian Høgsberg 	dev_priv->counter++;
1297c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
1298c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
12997c1c2871SDave Airlie 	if (master_priv->sarea_priv)
13007c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
1301c0e09200SDave Airlie 
1302e1f99ce6SChris Wilson 	if (BEGIN_LP_RING(4) == 0) {
1303585fb111SJesse Barnes 		OUT_RING(MI_STORE_DWORD_INDEX);
13040baf823aSKeith Packard 		OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1305c0e09200SDave Airlie 		OUT_RING(dev_priv->counter);
1306585fb111SJesse Barnes 		OUT_RING(MI_USER_INTERRUPT);
1307c0e09200SDave Airlie 		ADVANCE_LP_RING();
1308e1f99ce6SChris Wilson 	}
1309c0e09200SDave Airlie 
1310c0e09200SDave Airlie 	return dev_priv->counter;
1311c0e09200SDave Airlie }
1312c0e09200SDave Airlie 
13139d34e5dbSChris Wilson void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
13149d34e5dbSChris Wilson {
13159d34e5dbSChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13161ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
13179d34e5dbSChris Wilson 
1318b13c2b96SChris Wilson 	if (dev_priv->trace_irq_seqno == 0 &&
1319b13c2b96SChris Wilson 	    ring->irq_get(ring))
13209d34e5dbSChris Wilson 		dev_priv->trace_irq_seqno = seqno;
13219d34e5dbSChris Wilson }
13229d34e5dbSChris Wilson 
1323c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
1324c0e09200SDave Airlie {
1325c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13267c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1327c0e09200SDave Airlie 	int ret = 0;
13281ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
1329c0e09200SDave Airlie 
133044d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
1331c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
1332c0e09200SDave Airlie 
1333ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
13347c1c2871SDave Airlie 		if (master_priv->sarea_priv)
13357c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
1336c0e09200SDave Airlie 		return 0;
1337ed4cb414SEric Anholt 	}
1338c0e09200SDave Airlie 
13397c1c2871SDave Airlie 	if (master_priv->sarea_priv)
13407c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1341c0e09200SDave Airlie 
1342b13c2b96SChris Wilson 	ret = -ENODEV;
1343b13c2b96SChris Wilson 	if (ring->irq_get(ring)) {
13441ec14ad3SChris Wilson 		DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1345c0e09200SDave Airlie 			    READ_BREADCRUMB(dev_priv) >= irq_nr);
13461ec14ad3SChris Wilson 		ring->irq_put(ring);
1347b13c2b96SChris Wilson 	}
1348c0e09200SDave Airlie 
1349c0e09200SDave Airlie 	if (ret == -EBUSY) {
1350c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
1351c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1352c0e09200SDave Airlie 	}
1353c0e09200SDave Airlie 
1354c0e09200SDave Airlie 	return ret;
1355c0e09200SDave Airlie }
1356c0e09200SDave Airlie 
1357c0e09200SDave Airlie /* Needs the lock as it touches the ring.
1358c0e09200SDave Airlie  */
1359c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
1360c0e09200SDave Airlie 			 struct drm_file *file_priv)
1361c0e09200SDave Airlie {
1362c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1363c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
1364c0e09200SDave Airlie 	int result;
1365c0e09200SDave Airlie 
13661ec14ad3SChris Wilson 	if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
1367c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1368c0e09200SDave Airlie 		return -EINVAL;
1369c0e09200SDave Airlie 	}
1370299eb93cSEric Anholt 
1371299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1372299eb93cSEric Anholt 
1373546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
1374c0e09200SDave Airlie 	result = i915_emit_irq(dev);
1375546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
1376c0e09200SDave Airlie 
1377c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
1378c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
1379c0e09200SDave Airlie 		return -EFAULT;
1380c0e09200SDave Airlie 	}
1381c0e09200SDave Airlie 
1382c0e09200SDave Airlie 	return 0;
1383c0e09200SDave Airlie }
1384c0e09200SDave Airlie 
1385c0e09200SDave Airlie /* Doesn't need the hardware lock.
1386c0e09200SDave Airlie  */
1387c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
1388c0e09200SDave Airlie 			 struct drm_file *file_priv)
1389c0e09200SDave Airlie {
1390c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1391c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
1392c0e09200SDave Airlie 
1393c0e09200SDave Airlie 	if (!dev_priv) {
1394c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1395c0e09200SDave Airlie 		return -EINVAL;
1396c0e09200SDave Airlie 	}
1397c0e09200SDave Airlie 
1398c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
1399c0e09200SDave Airlie }
1400c0e09200SDave Airlie 
140142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
140242f52ef8SKeith Packard  * we use as a pipe index
140342f52ef8SKeith Packard  */
140442f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
14050a3e67a4SJesse Barnes {
14060a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1407e9d21d7fSKeith Packard 	unsigned long irqflags;
140871e0ffa5SJesse Barnes 
14095eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
141071e0ffa5SJesse Barnes 		return -EINVAL;
14110a3e67a4SJesse Barnes 
14121ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1413bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1414c062df61SLi Peng 		ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1415c062df61SLi Peng 					    DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1416a6c45cf0SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 4)
14177c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14187c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
14190a3e67a4SJesse Barnes 	else
14207c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14217c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
14221ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14230a3e67a4SJesse Barnes 	return 0;
14240a3e67a4SJesse Barnes }
14250a3e67a4SJesse Barnes 
142642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
142742f52ef8SKeith Packard  * we use as a pipe index
142842f52ef8SKeith Packard  */
142942f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
14300a3e67a4SJesse Barnes {
14310a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1432e9d21d7fSKeith Packard 	unsigned long irqflags;
14330a3e67a4SJesse Barnes 
14341ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1435bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1436c062df61SLi Peng 		ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1437c062df61SLi Peng 					     DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1438c062df61SLi Peng 	else
14397c463586SKeith Packard 		i915_disable_pipestat(dev_priv, pipe,
14407c463586SKeith Packard 				      PIPE_VBLANK_INTERRUPT_ENABLE |
14417c463586SKeith Packard 				      PIPE_START_VBLANK_INTERRUPT_ENABLE);
14421ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14430a3e67a4SJesse Barnes }
14440a3e67a4SJesse Barnes 
144579e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev)
144679e53945SJesse Barnes {
144779e53945SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1448e170b030SZhenyu Wang 
1449bad720ffSEric Anholt 	if (!HAS_PCH_SPLIT(dev))
14503b617967SChris Wilson 		intel_opregion_enable_asle(dev);
145179e53945SJesse Barnes 	dev_priv->irq_enabled = 1;
145279e53945SJesse Barnes }
145379e53945SJesse Barnes 
145479e53945SJesse Barnes 
1455c0e09200SDave Airlie /* Set the vblank monitor pipe
1456c0e09200SDave Airlie  */
1457c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1458c0e09200SDave Airlie 			 struct drm_file *file_priv)
1459c0e09200SDave Airlie {
1460c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1461c0e09200SDave Airlie 
1462c0e09200SDave Airlie 	if (!dev_priv) {
1463c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1464c0e09200SDave Airlie 		return -EINVAL;
1465c0e09200SDave Airlie 	}
1466c0e09200SDave Airlie 
1467c0e09200SDave Airlie 	return 0;
1468c0e09200SDave Airlie }
1469c0e09200SDave Airlie 
1470c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1471c0e09200SDave Airlie 			 struct drm_file *file_priv)
1472c0e09200SDave Airlie {
1473c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1474c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
1475c0e09200SDave Airlie 
1476c0e09200SDave Airlie 	if (!dev_priv) {
1477c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1478c0e09200SDave Airlie 		return -EINVAL;
1479c0e09200SDave Airlie 	}
1480c0e09200SDave Airlie 
14810a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1482c0e09200SDave Airlie 
1483c0e09200SDave Airlie 	return 0;
1484c0e09200SDave Airlie }
1485c0e09200SDave Airlie 
1486c0e09200SDave Airlie /**
1487c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
1488c0e09200SDave Airlie  */
1489c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
1490c0e09200SDave Airlie 		     struct drm_file *file_priv)
1491c0e09200SDave Airlie {
1492bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1493bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1494bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1495bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1496bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1497bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1498bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1499bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1500bd95e0a4SEric Anholt 	 *
1501bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1502bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1503bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1504bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
15050a3e67a4SJesse Barnes 	 */
1506c0e09200SDave Airlie 	return -EINVAL;
1507c0e09200SDave Airlie }
1508c0e09200SDave Airlie 
1509893eead0SChris Wilson static u32
1510893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1511852835f3SZou Nan hai {
1512893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1513893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1514893eead0SChris Wilson }
1515893eead0SChris Wilson 
1516893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1517893eead0SChris Wilson {
1518893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1519893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1520893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
1521b2223497SChris Wilson 		if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
1522893eead0SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1523893eead0SChris Wilson 				  ring->name,
1524b2223497SChris Wilson 				  ring->waiting_seqno,
1525893eead0SChris Wilson 				  ring->get_seqno(ring));
1526893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1527893eead0SChris Wilson 			*err = true;
1528893eead0SChris Wilson 		}
1529893eead0SChris Wilson 		return true;
1530893eead0SChris Wilson 	}
1531893eead0SChris Wilson 	return false;
1532f65d9421SBen Gamari }
1533f65d9421SBen Gamari 
15341ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
15351ec14ad3SChris Wilson {
15361ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
15371ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
15381ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
15391ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
15401ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
15411ec14ad3SChris Wilson 			  ring->name);
15421ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
15431ec14ad3SChris Wilson 		return true;
15441ec14ad3SChris Wilson 	}
15451ec14ad3SChris Wilson 	if (IS_GEN6(dev) &&
15461ec14ad3SChris Wilson 	    (tmp & RING_WAIT_SEMAPHORE)) {
15471ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
15481ec14ad3SChris Wilson 			  ring->name);
15491ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
15501ec14ad3SChris Wilson 		return true;
15511ec14ad3SChris Wilson 	}
15521ec14ad3SChris Wilson 	return false;
15531ec14ad3SChris Wilson }
15541ec14ad3SChris Wilson 
1555f65d9421SBen Gamari /**
1556f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1557f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1558f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1559f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1560f65d9421SBen Gamari  */
1561f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1562f65d9421SBen Gamari {
1563f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1564f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1565cbb465e7SChris Wilson 	uint32_t acthd, instdone, instdone1;
1566893eead0SChris Wilson 	bool err = false;
1567893eead0SChris Wilson 
1568893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
15691ec14ad3SChris Wilson 	if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
15701ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
15711ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
1572893eead0SChris Wilson 		dev_priv->hangcheck_count = 0;
1573893eead0SChris Wilson 		if (err)
1574893eead0SChris Wilson 			goto repeat;
1575893eead0SChris Wilson 		return;
1576893eead0SChris Wilson 	}
1577f65d9421SBen Gamari 
1578a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1579f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
1580cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1581cbb465e7SChris Wilson 		instdone1 = 0;
1582cbb465e7SChris Wilson 	} else {
1583f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
1584cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1585cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1586cbb465e7SChris Wilson 	}
1587f65d9421SBen Gamari 
1588cbb465e7SChris Wilson 	if (dev_priv->last_acthd == acthd &&
1589cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1590cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1591cbb465e7SChris Wilson 		if (dev_priv->hangcheck_count++ > 1) {
1592f65d9421SBen Gamari 			DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
15938c80b59bSChris Wilson 
15948c80b59bSChris Wilson 			if (!IS_GEN2(dev)) {
15958c80b59bSChris Wilson 				/* Is the chip hanging on a WAIT_FOR_EVENT?
15968c80b59bSChris Wilson 				 * If so we can simply poke the RB_WAIT bit
15978c80b59bSChris Wilson 				 * and break the hang. This should work on
15988c80b59bSChris Wilson 				 * all but the second generation chipsets.
15998c80b59bSChris Wilson 				 */
16001ec14ad3SChris Wilson 
16011ec14ad3SChris Wilson 				if (kick_ring(&dev_priv->ring[RCS]))
1602893eead0SChris Wilson 					goto repeat;
16031ec14ad3SChris Wilson 
16041ec14ad3SChris Wilson 				if (HAS_BSD(dev) &&
16051ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[VCS]))
16061ec14ad3SChris Wilson 					goto repeat;
16071ec14ad3SChris Wilson 
16081ec14ad3SChris Wilson 				if (HAS_BLT(dev) &&
16091ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[BCS]))
16101ec14ad3SChris Wilson 					goto repeat;
16118c80b59bSChris Wilson 			}
16128c80b59bSChris Wilson 
1613ba1234d1SBen Gamari 			i915_handle_error(dev, true);
1614f65d9421SBen Gamari 			return;
1615f65d9421SBen Gamari 		}
1616cbb465e7SChris Wilson 	} else {
1617cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1618cbb465e7SChris Wilson 
1619cbb465e7SChris Wilson 		dev_priv->last_acthd = acthd;
1620cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1621cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1622cbb465e7SChris Wilson 	}
1623f65d9421SBen Gamari 
1624893eead0SChris Wilson repeat:
1625f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1626b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1627b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1628f65d9421SBen Gamari }
1629f65d9421SBen Gamari 
1630c0e09200SDave Airlie /* drm_dma.h hooks
1631c0e09200SDave Airlie */
1632f2b115e6SAdam Jackson static void ironlake_irq_preinstall(struct drm_device *dev)
1633036a4a7dSZhenyu Wang {
1634036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1635036a4a7dSZhenyu Wang 
1636036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1637036a4a7dSZhenyu Wang 
1638036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1639036a4a7dSZhenyu Wang 
1640036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1641036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
16423143a2bfSChris Wilson 	POSTING_READ(DEIER);
1643036a4a7dSZhenyu Wang 
1644036a4a7dSZhenyu Wang 	/* and GT */
1645036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1646036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
16473143a2bfSChris Wilson 	POSTING_READ(GTIER);
1648c650156aSZhenyu Wang 
1649c650156aSZhenyu Wang 	/* south display irq */
1650c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1651c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
16523143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1653036a4a7dSZhenyu Wang }
1654036a4a7dSZhenyu Wang 
1655f2b115e6SAdam Jackson static int ironlake_irq_postinstall(struct drm_device *dev)
1656036a4a7dSZhenyu Wang {
1657036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1658036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1659013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1660013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
16611ec14ad3SChris Wilson 	u32 render_irqs;
16622d7b8366SYuanhan Liu 	u32 hotplug_mask;
1663036a4a7dSZhenyu Wang 
16641ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1665036a4a7dSZhenyu Wang 
1666036a4a7dSZhenyu Wang 	/* should always can generate irq */
1667036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
16681ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
16691ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
16703143a2bfSChris Wilson 	POSTING_READ(DEIER);
1671036a4a7dSZhenyu Wang 
16721ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1673036a4a7dSZhenyu Wang 
1674036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
16751ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1676881f47b6SXiang, Haihao 	if (IS_GEN6(dev)) {
16771ec14ad3SChris Wilson 		I915_WRITE(GEN6_RENDER_IMR, ~GEN6_RENDER_USER_INTERRUPT);
16781ec14ad3SChris Wilson 		I915_WRITE(GEN6_BSD_IMR, ~GEN6_BSD_USER_INTERRUPT);
1679549f7365SChris Wilson 		I915_WRITE(GEN6_BLITTER_IMR, ~GEN6_BLITTER_USER_INTERRUPT);
1680881f47b6SXiang, Haihao 	}
1681881f47b6SXiang, Haihao 
16821ec14ad3SChris Wilson 	if (IS_GEN6(dev))
16831ec14ad3SChris Wilson 		render_irqs =
16841ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
16851ec14ad3SChris Wilson 			GT_GEN6_BSD_USER_INTERRUPT |
16861ec14ad3SChris Wilson 			GT_BLT_USER_INTERRUPT;
16871ec14ad3SChris Wilson 	else
16881ec14ad3SChris Wilson 		render_irqs =
168988f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1690c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
16911ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
16921ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
16933143a2bfSChris Wilson 	POSTING_READ(GTIER);
1694036a4a7dSZhenyu Wang 
16952d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
16962d7b8366SYuanhan Liu 		hotplug_mask = SDE_CRT_HOTPLUG_CPT | SDE_PORTB_HOTPLUG_CPT  |
16972d7b8366SYuanhan Liu 			       SDE_PORTC_HOTPLUG_CPT | SDE_PORTD_HOTPLUG_CPT ;
16982d7b8366SYuanhan Liu 	} else {
16992d7b8366SYuanhan Liu 		hotplug_mask = SDE_CRT_HOTPLUG | SDE_PORTB_HOTPLUG |
17002d7b8366SYuanhan Liu 			       SDE_PORTC_HOTPLUG | SDE_PORTD_HOTPLUG;
17012d7b8366SYuanhan Liu 	}
17022d7b8366SYuanhan Liu 
17031ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1704c650156aSZhenyu Wang 
1705c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
17061ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
17071ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
17083143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1709c650156aSZhenyu Wang 
1710f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1711f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1712f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1713f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1714f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1715f97108d1SJesse Barnes 	}
1716f97108d1SJesse Barnes 
1717036a4a7dSZhenyu Wang 	return 0;
1718036a4a7dSZhenyu Wang }
1719036a4a7dSZhenyu Wang 
1720c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1721c0e09200SDave Airlie {
1722c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1723c0e09200SDave Airlie 
172479e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
172579e53945SJesse Barnes 
1726036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
17278a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1728036a4a7dSZhenyu Wang 
1729bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1730f2b115e6SAdam Jackson 		ironlake_irq_preinstall(dev);
1731036a4a7dSZhenyu Wang 		return;
1732036a4a7dSZhenyu Wang 	}
1733036a4a7dSZhenyu Wang 
17345ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
17355ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
17365ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
17375ca58282SJesse Barnes 	}
17385ca58282SJesse Barnes 
17390a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
17407c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
17417c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
17420a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1743ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
17443143a2bfSChris Wilson 	POSTING_READ(IER);
1745c0e09200SDave Airlie }
1746c0e09200SDave Airlie 
1747b01f2c3aSJesse Barnes /*
1748b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
1749b01f2c3aSJesse Barnes  * enabled correctly.
1750b01f2c3aSJesse Barnes  */
17510a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1752c0e09200SDave Airlie {
1753c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17545ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
175563eeaf38SJesse Barnes 	u32 error_mask;
17560a3e67a4SJesse Barnes 
17571ec14ad3SChris Wilson 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
1758d1b851fcSZou Nan hai 	if (HAS_BSD(dev))
17591ec14ad3SChris Wilson 		DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
1760549f7365SChris Wilson 	if (HAS_BLT(dev))
17611ec14ad3SChris Wilson 		DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
1762d1b851fcSZou Nan hai 
17630a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1764ed4cb414SEric Anholt 
1765bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1766f2b115e6SAdam Jackson 		return ironlake_irq_postinstall(dev);
1767036a4a7dSZhenyu Wang 
17687c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
17691ec14ad3SChris Wilson 	dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
17708ee1c3dbSMatthew Garrett 
17717c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
17727c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
17737c463586SKeith Packard 
17745ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
1775c496fa1fSAdam Jackson 		/* Enable in IER... */
1776c496fa1fSAdam Jackson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1777c496fa1fSAdam Jackson 		/* and unmask in IMR */
17781ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
1779c496fa1fSAdam Jackson 	}
1780c496fa1fSAdam Jackson 
1781c496fa1fSAdam Jackson 	/*
1782c496fa1fSAdam Jackson 	 * Enable some error detection, note the instruction error mask
1783c496fa1fSAdam Jackson 	 * bit is reserved, so we leave it masked.
1784c496fa1fSAdam Jackson 	 */
1785c496fa1fSAdam Jackson 	if (IS_G4X(dev)) {
1786c496fa1fSAdam Jackson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
1787c496fa1fSAdam Jackson 			       GM45_ERROR_MEM_PRIV |
1788c496fa1fSAdam Jackson 			       GM45_ERROR_CP_PRIV |
1789c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1790c496fa1fSAdam Jackson 	} else {
1791c496fa1fSAdam Jackson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
1792c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1793c496fa1fSAdam Jackson 	}
1794c496fa1fSAdam Jackson 	I915_WRITE(EMR, error_mask);
1795c496fa1fSAdam Jackson 
17961ec14ad3SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
1797c496fa1fSAdam Jackson 	I915_WRITE(IER, enable_mask);
17983143a2bfSChris Wilson 	POSTING_READ(IER);
1799c496fa1fSAdam Jackson 
1800c496fa1fSAdam Jackson 	if (I915_HAS_HOTPLUG(dev)) {
18015ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
18025ca58282SJesse Barnes 
1803b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
1804b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1805b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1806b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1807b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1808b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1809b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
1810b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1811b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1812b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1813b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
18142d1c9752SAndy Lutomirski 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
1815b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
18162d1c9752SAndy Lutomirski 
18172d1c9752SAndy Lutomirski 			/* Programming the CRT detection parameters tends
18182d1c9752SAndy Lutomirski 			   to generate a spurious hotplug event about three
18192d1c9752SAndy Lutomirski 			   seconds later.  So just do it once.
18202d1c9752SAndy Lutomirski 			*/
18212d1c9752SAndy Lutomirski 			if (IS_G4X(dev))
18222d1c9752SAndy Lutomirski 				hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
18232d1c9752SAndy Lutomirski 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
18242d1c9752SAndy Lutomirski 		}
18252d1c9752SAndy Lutomirski 
1826b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
1827b01f2c3aSJesse Barnes 
18285ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
18295ca58282SJesse Barnes 	}
18305ca58282SJesse Barnes 
18313b617967SChris Wilson 	intel_opregion_enable_asle(dev);
18320a3e67a4SJesse Barnes 
18330a3e67a4SJesse Barnes 	return 0;
1834c0e09200SDave Airlie }
1835c0e09200SDave Airlie 
1836f2b115e6SAdam Jackson static void ironlake_irq_uninstall(struct drm_device *dev)
1837036a4a7dSZhenyu Wang {
1838036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1839036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1840036a4a7dSZhenyu Wang 
1841036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1842036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1843036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1844036a4a7dSZhenyu Wang 
1845036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1846036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1847036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1848036a4a7dSZhenyu Wang }
1849036a4a7dSZhenyu Wang 
1850c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1851c0e09200SDave Airlie {
1852c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1853c0e09200SDave Airlie 
1854c0e09200SDave Airlie 	if (!dev_priv)
1855c0e09200SDave Airlie 		return;
1856c0e09200SDave Airlie 
18570a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
18580a3e67a4SJesse Barnes 
1859bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1860f2b115e6SAdam Jackson 		ironlake_irq_uninstall(dev);
1861036a4a7dSZhenyu Wang 		return;
1862036a4a7dSZhenyu Wang 	}
1863036a4a7dSZhenyu Wang 
18645ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
18655ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
18665ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
18675ca58282SJesse Barnes 	}
18685ca58282SJesse Barnes 
18690a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
18707c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
18717c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
18720a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1873ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1874c0e09200SDave Airlie 
18757c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
18767c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
18777c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1878c0e09200SDave Airlie }
1879