xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 776ad8062bb77697b8728a9794e3a394b28cf885)
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 
67036a4a7dSZhenyu Wang /* For display hotplug interrupt */
68995b6762SChris Wilson static void
69f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
70036a4a7dSZhenyu Wang {
711ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
721ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
731ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
743143a2bfSChris Wilson 		POSTING_READ(DEIMR);
75036a4a7dSZhenyu Wang 	}
76036a4a7dSZhenyu Wang }
77036a4a7dSZhenyu Wang 
78036a4a7dSZhenyu Wang static inline void
79f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
80036a4a7dSZhenyu Wang {
811ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
821ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
831ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
843143a2bfSChris Wilson 		POSTING_READ(DEIMR);
85036a4a7dSZhenyu Wang 	}
86036a4a7dSZhenyu Wang }
87036a4a7dSZhenyu Wang 
887c463586SKeith Packard static inline u32
897c463586SKeith Packard i915_pipestat(int pipe)
907c463586SKeith Packard {
917c463586SKeith Packard 	if (pipe == 0)
927c463586SKeith Packard 		return PIPEASTAT;
937c463586SKeith Packard 	if (pipe == 1)
947c463586SKeith Packard 		return PIPEBSTAT;
959c84ba4eSAndrew Morton 	BUG();
967c463586SKeith Packard }
977c463586SKeith Packard 
987c463586SKeith Packard void
997c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1007c463586SKeith Packard {
1017c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
1027c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1037c463586SKeith Packard 
1047c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
1057c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
1067c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
1073143a2bfSChris Wilson 		POSTING_READ(reg);
1087c463586SKeith Packard 	}
1097c463586SKeith Packard }
1107c463586SKeith Packard 
1117c463586SKeith Packard void
1127c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1137c463586SKeith Packard {
1147c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
1157c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1167c463586SKeith Packard 
1177c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
1187c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
1193143a2bfSChris Wilson 		POSTING_READ(reg);
1207c463586SKeith Packard 	}
1217c463586SKeith Packard }
1227c463586SKeith Packard 
123c0e09200SDave Airlie /**
12401c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
12501c66889SZhao Yakui  */
12601c66889SZhao Yakui void intel_enable_asle(struct drm_device *dev)
12701c66889SZhao Yakui {
1281ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1291ec14ad3SChris Wilson 	unsigned long irqflags;
1301ec14ad3SChris Wilson 
1311ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
13201c66889SZhao Yakui 
133c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
134f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
135edcb49caSZhao Yakui 	else {
13601c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
137d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
138a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen >= 4)
139edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
140d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
141edcb49caSZhao Yakui 	}
1421ec14ad3SChris Wilson 
1431ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14401c66889SZhao Yakui }
14501c66889SZhao Yakui 
14601c66889SZhao Yakui /**
1470a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1480a3e67a4SJesse Barnes  * @dev: DRM device
1490a3e67a4SJesse Barnes  * @pipe: pipe to check
1500a3e67a4SJesse Barnes  *
1510a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1520a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1530a3e67a4SJesse Barnes  * before reading such registers if unsure.
1540a3e67a4SJesse Barnes  */
1550a3e67a4SJesse Barnes static int
1560a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1570a3e67a4SJesse Barnes {
1580a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1595eddb70bSChris Wilson 	return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
1600a3e67a4SJesse Barnes }
1610a3e67a4SJesse Barnes 
16242f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
16342f52ef8SKeith Packard  * we use as a pipe index
16442f52ef8SKeith Packard  */
16542f52ef8SKeith Packard u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1660a3e67a4SJesse Barnes {
1670a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1680a3e67a4SJesse Barnes 	unsigned long high_frame;
1690a3e67a4SJesse Barnes 	unsigned long low_frame;
1705eddb70bSChris Wilson 	u32 high1, high2, low;
1710a3e67a4SJesse Barnes 
1720a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
17344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
17444d98a61SZhao Yakui 				"pipe %d\n", pipe);
1750a3e67a4SJesse Barnes 		return 0;
1760a3e67a4SJesse Barnes 	}
1770a3e67a4SJesse Barnes 
1785eddb70bSChris Wilson 	high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
1795eddb70bSChris Wilson 	low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
1805eddb70bSChris Wilson 
1810a3e67a4SJesse Barnes 	/*
1820a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
1830a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
1840a3e67a4SJesse Barnes 	 * register.
1850a3e67a4SJesse Barnes 	 */
1860a3e67a4SJesse Barnes 	do {
1875eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1885eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
1895eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1900a3e67a4SJesse Barnes 	} while (high1 != high2);
1910a3e67a4SJesse Barnes 
1925eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
1935eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
1945eddb70bSChris Wilson 	return (high1 << 8) | low;
1950a3e67a4SJesse Barnes }
1960a3e67a4SJesse Barnes 
1979880b7a5SJesse Barnes u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
1989880b7a5SJesse Barnes {
1999880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2009880b7a5SJesse Barnes 	int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
2019880b7a5SJesse Barnes 
2029880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
20344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
20444d98a61SZhao Yakui 					"pipe %d\n", pipe);
2059880b7a5SJesse Barnes 		return 0;
2069880b7a5SJesse Barnes 	}
2079880b7a5SJesse Barnes 
2089880b7a5SJesse Barnes 	return I915_READ(reg);
2099880b7a5SJesse Barnes }
2109880b7a5SJesse Barnes 
2110af7e4dfSMario Kleiner int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
2120af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
2130af7e4dfSMario Kleiner {
2140af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2150af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
2160af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
2170af7e4dfSMario Kleiner 	bool in_vbl = true;
2180af7e4dfSMario Kleiner 	int ret = 0;
2190af7e4dfSMario Kleiner 
2200af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
2210af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
2220af7e4dfSMario Kleiner 					"pipe %d\n", pipe);
2230af7e4dfSMario Kleiner 		return 0;
2240af7e4dfSMario Kleiner 	}
2250af7e4dfSMario Kleiner 
2260af7e4dfSMario Kleiner 	/* Get vtotal. */
2270af7e4dfSMario Kleiner 	vtotal = 1 + ((I915_READ(VTOTAL(pipe)) >> 16) & 0x1fff);
2280af7e4dfSMario Kleiner 
2290af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
2300af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
2310af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
2320af7e4dfSMario Kleiner 		 */
2330af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
2340af7e4dfSMario Kleiner 
2350af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
2360af7e4dfSMario Kleiner 		 * horizontal scanout position.
2370af7e4dfSMario Kleiner 		 */
2380af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
2390af7e4dfSMario Kleiner 		*hpos = 0;
2400af7e4dfSMario Kleiner 	} else {
2410af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
2420af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
2430af7e4dfSMario Kleiner 		 * scanout position.
2440af7e4dfSMario Kleiner 		 */
2450af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
2460af7e4dfSMario Kleiner 
2470af7e4dfSMario Kleiner 		htotal = 1 + ((I915_READ(HTOTAL(pipe)) >> 16) & 0x1fff);
2480af7e4dfSMario Kleiner 		*vpos = position / htotal;
2490af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
2500af7e4dfSMario Kleiner 	}
2510af7e4dfSMario Kleiner 
2520af7e4dfSMario Kleiner 	/* Query vblank area. */
2530af7e4dfSMario Kleiner 	vbl = I915_READ(VBLANK(pipe));
2540af7e4dfSMario Kleiner 
2550af7e4dfSMario Kleiner 	/* Test position against vblank region. */
2560af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
2570af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
2580af7e4dfSMario Kleiner 
2590af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
2600af7e4dfSMario Kleiner 		in_vbl = false;
2610af7e4dfSMario Kleiner 
2620af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
2630af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
2640af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
2650af7e4dfSMario Kleiner 
2660af7e4dfSMario Kleiner 	/* Readouts valid? */
2670af7e4dfSMario Kleiner 	if (vbl > 0)
2680af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
2690af7e4dfSMario Kleiner 
2700af7e4dfSMario Kleiner 	/* In vblank? */
2710af7e4dfSMario Kleiner 	if (in_vbl)
2720af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
2730af7e4dfSMario Kleiner 
2740af7e4dfSMario Kleiner 	return ret;
2750af7e4dfSMario Kleiner }
2760af7e4dfSMario Kleiner 
2770af7e4dfSMario Kleiner int i915_get_vblank_timestamp(struct drm_device *dev, int crtc,
2780af7e4dfSMario Kleiner 			      int *max_error,
2790af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
2800af7e4dfSMario Kleiner 			      unsigned flags)
2810af7e4dfSMario Kleiner {
2820af7e4dfSMario Kleiner 	struct drm_crtc *drmcrtc;
2830af7e4dfSMario Kleiner 
2840af7e4dfSMario Kleiner 	if (crtc < 0 || crtc >= dev->num_crtcs) {
2850af7e4dfSMario Kleiner 		DRM_ERROR("Invalid crtc %d\n", crtc);
2860af7e4dfSMario Kleiner 		return -EINVAL;
2870af7e4dfSMario Kleiner 	}
2880af7e4dfSMario Kleiner 
2890af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
2900af7e4dfSMario Kleiner 	drmcrtc = intel_get_crtc_for_pipe(dev, crtc);
2910af7e4dfSMario Kleiner 
2920af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
2930af7e4dfSMario Kleiner 	return drm_calc_vbltimestamp_from_scanoutpos(dev, crtc, max_error,
2940af7e4dfSMario Kleiner 						     vblank_time, flags, drmcrtc);
2950af7e4dfSMario Kleiner }
2960af7e4dfSMario Kleiner 
2975ca58282SJesse Barnes /*
2985ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
2995ca58282SJesse Barnes  */
3005ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
3015ca58282SJesse Barnes {
3025ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3035ca58282SJesse Barnes 						    hotplug_work);
3045ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
305c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
3064ef69c7aSChris Wilson 	struct intel_encoder *encoder;
3075ca58282SJesse Barnes 
3084ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
3094ef69c7aSChris Wilson 		if (encoder->hot_plug)
3104ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
311c31c4ba3SKeith Packard 
3125ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
313eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3145ca58282SJesse Barnes }
3155ca58282SJesse Barnes 
316f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
317f97108d1SJesse Barnes {
318f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
319b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
320f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
321f97108d1SJesse Barnes 
3227648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
323b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
324b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
325f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
326f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
327f97108d1SJesse Barnes 
328f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
329b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
330f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
331f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
332f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
333f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
334b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
335f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
336f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
337f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
338f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
339f97108d1SJesse Barnes 	}
340f97108d1SJesse Barnes 
3417648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
342f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
343f97108d1SJesse Barnes 
344f97108d1SJesse Barnes 	return;
345f97108d1SJesse Barnes }
346f97108d1SJesse Barnes 
347549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
348549f7365SChris Wilson 			struct intel_ring_buffer *ring)
349549f7365SChris Wilson {
350549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
35178501eacSChris Wilson 	u32 seqno = ring->get_seqno(ring);
3529862e600SChris Wilson 
353549f7365SChris Wilson 	trace_i915_gem_request_complete(dev, seqno);
3549862e600SChris Wilson 
3559862e600SChris Wilson 	ring->irq_seqno = seqno;
356549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3579862e600SChris Wilson 
358549f7365SChris Wilson 	dev_priv->hangcheck_count = 0;
359549f7365SChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
360549f7365SChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
361549f7365SChris Wilson }
362549f7365SChris Wilson 
3633b8d8d91SJesse Barnes static void gen6_pm_irq_handler(struct drm_device *dev)
3643b8d8d91SJesse Barnes {
3653b8d8d91SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3663b8d8d91SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
3673b8d8d91SJesse Barnes 	u32 pm_iir;
3683b8d8d91SJesse Barnes 
3693b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
3703b8d8d91SJesse Barnes 	if (!pm_iir)
3713b8d8d91SJesse Barnes 		return;
3723b8d8d91SJesse Barnes 
3733b8d8d91SJesse Barnes 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
3743b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
3753b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
3763b8d8d91SJesse Barnes 		if (new_delay > dev_priv->max_delay)
3773b8d8d91SJesse Barnes 			new_delay = dev_priv->max_delay;
3783b8d8d91SJesse Barnes 	} else if (pm_iir & (GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT)) {
3793b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
3803b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
3813b8d8d91SJesse Barnes 		if (new_delay < dev_priv->min_delay) {
3823b8d8d91SJesse Barnes 			new_delay = dev_priv->min_delay;
3833b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3843b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) |
3853b8d8d91SJesse Barnes 				   ((new_delay << 16) & 0x3f0000));
3863b8d8d91SJesse Barnes 		} else {
3873b8d8d91SJesse Barnes 			/* Make sure we continue to get down interrupts
3883b8d8d91SJesse Barnes 			 * until we hit the minimum frequency */
3893b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3903b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) & ~0x3f0000);
3913b8d8d91SJesse Barnes 		}
3923b8d8d91SJesse Barnes 
3933b8d8d91SJesse Barnes 	}
3943b8d8d91SJesse Barnes 
3953b8d8d91SJesse Barnes 	gen6_set_rps(dev, new_delay);
3963b8d8d91SJesse Barnes 	dev_priv->cur_delay = new_delay;
3973b8d8d91SJesse Barnes 
3983b8d8d91SJesse Barnes 	I915_WRITE(GEN6_PMIIR, pm_iir);
3993b8d8d91SJesse Barnes }
4003b8d8d91SJesse Barnes 
401*776ad806SJesse Barnes static void pch_irq_handler(struct drm_device *dev)
402*776ad806SJesse Barnes {
403*776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
404*776ad806SJesse Barnes 	u32 pch_iir;
405*776ad806SJesse Barnes 
406*776ad806SJesse Barnes 	pch_iir = I915_READ(SDEIIR);
407*776ad806SJesse Barnes 
408*776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
409*776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
410*776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
411*776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
412*776ad806SJesse Barnes 
413*776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
414*776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
415*776ad806SJesse Barnes 
416*776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
417*776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
418*776ad806SJesse Barnes 
419*776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
420*776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
421*776ad806SJesse Barnes 
422*776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
423*776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
424*776ad806SJesse Barnes 
425*776ad806SJesse Barnes 	if (pch_iir & SDE_FDI_MASK) {
426*776ad806SJesse Barnes 		u32 fdia, fdib;
427*776ad806SJesse Barnes 
428*776ad806SJesse Barnes 		fdia = I915_READ(FDI_RXA_IIR);
429*776ad806SJesse Barnes 		fdib = I915_READ(FDI_RXB_IIR);
430*776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH FDI RX interrupt; FDI RXA IIR: 0x%08x, FDI RXB IIR: 0x%08x\n", fdia, fdib);
431*776ad806SJesse Barnes 	}
432*776ad806SJesse Barnes 
433*776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
434*776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
435*776ad806SJesse Barnes 
436*776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
437*776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
438*776ad806SJesse Barnes 
439*776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
440*776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
441*776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
442*776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
443*776ad806SJesse Barnes }
444*776ad806SJesse Barnes 
445995b6762SChris Wilson static irqreturn_t ironlake_irq_handler(struct drm_device *dev)
446036a4a7dSZhenyu Wang {
447036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
448036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
4493b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
4502d7b8366SYuanhan Liu 	u32 hotplug_mask;
451036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
452881f47b6SXiang, Haihao 	u32 bsd_usr_interrupt = GT_BSD_USER_INTERRUPT;
453881f47b6SXiang, Haihao 
454881f47b6SXiang, Haihao 	if (IS_GEN6(dev))
455881f47b6SXiang, Haihao 		bsd_usr_interrupt = GT_GEN6_BSD_USER_INTERRUPT;
456036a4a7dSZhenyu Wang 
4572d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
4582d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
4592d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
4603143a2bfSChris Wilson 	POSTING_READ(DEIER);
4612d109a84SZou, Nanhai 
462036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
463036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
464c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
4653b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
466036a4a7dSZhenyu Wang 
4673b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
4683b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
469c7c85101SZou Nan hai 		goto done;
470036a4a7dSZhenyu Wang 
4712d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
4722d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
4732d7b8366SYuanhan Liu 	else
4742d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
4752d7b8366SYuanhan Liu 
476036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
477036a4a7dSZhenyu Wang 
478036a4a7dSZhenyu Wang 	if (dev->primary->master) {
479036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
480036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
481036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
482036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
483036a4a7dSZhenyu Wang 	}
484036a4a7dSZhenyu Wang 
485c6df541cSChris Wilson 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
4861ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[RCS]);
487881f47b6SXiang, Haihao 	if (gt_iir & bsd_usr_interrupt)
4881ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[VCS]);
4891ec14ad3SChris Wilson 	if (gt_iir & GT_BLT_USER_INTERRUPT)
4901ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[BCS]);
491036a4a7dSZhenyu Wang 
49201c66889SZhao Yakui 	if (de_iir & DE_GSE)
4933b617967SChris Wilson 		intel_opregion_gse_intr(dev);
49401c66889SZhao Yakui 
495f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
496013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
4972bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
498013d5aa2SJesse Barnes 	}
499013d5aa2SJesse Barnes 
500f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
501f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
5022bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
503013d5aa2SJesse Barnes 	}
504c062df61SLi Peng 
505f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
506f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
507f072d2e7SZhenyu Wang 
508f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
509f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
510f072d2e7SZhenyu Wang 
511c650156aSZhenyu Wang 	/* check event from PCH */
512*776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
513*776ad806SJesse Barnes 		if (pch_iir & hotplug_mask)
514c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
515*776ad806SJesse Barnes 		pch_irq_handler(dev);
516*776ad806SJesse Barnes 	}
517c650156aSZhenyu Wang 
518f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
5197648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
520f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
521f97108d1SJesse Barnes 	}
522f97108d1SJesse Barnes 
5233b8d8d91SJesse Barnes 	if (IS_GEN6(dev))
5243b8d8d91SJesse Barnes 		gen6_pm_irq_handler(dev);
5253b8d8d91SJesse Barnes 
526c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
527c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
528c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
529c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
530036a4a7dSZhenyu Wang 
531c7c85101SZou Nan hai done:
5322d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
5333143a2bfSChris Wilson 	POSTING_READ(DEIER);
5342d109a84SZou, Nanhai 
535036a4a7dSZhenyu Wang 	return ret;
536036a4a7dSZhenyu Wang }
537036a4a7dSZhenyu Wang 
5388a905236SJesse Barnes /**
5398a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
5408a905236SJesse Barnes  * @work: work struct
5418a905236SJesse Barnes  *
5428a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
5438a905236SJesse Barnes  * was detected.
5448a905236SJesse Barnes  */
5458a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
5468a905236SJesse Barnes {
5478a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5488a905236SJesse Barnes 						    error_work);
5498a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
550f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
551f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
552f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
5538a905236SJesse Barnes 
554f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
5558a905236SJesse Barnes 
556ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
55744d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
558f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
559f803aa55SChris Wilson 		if (!i915_reset(dev, GRDOM_RENDER)) {
560ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
561f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
562f316a42cSBen Gamari 		}
56330dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
564f316a42cSBen Gamari 	}
5658a905236SJesse Barnes }
5668a905236SJesse Barnes 
5673bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
5689df30794SChris Wilson static struct drm_i915_error_object *
5699df30794SChris Wilson i915_error_object_create(struct drm_device *dev,
57005394f39SChris Wilson 			 struct drm_i915_gem_object *src)
5719df30794SChris Wilson {
572e56660ddSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
5739df30794SChris Wilson 	struct drm_i915_error_object *dst;
5749df30794SChris Wilson 	int page, page_count;
575e56660ddSChris Wilson 	u32 reloc_offset;
5769df30794SChris Wilson 
57705394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
5789df30794SChris Wilson 		return NULL;
5799df30794SChris Wilson 
58005394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
5819df30794SChris Wilson 
5829df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof (u32 *), GFP_ATOMIC);
5839df30794SChris Wilson 	if (dst == NULL)
5849df30794SChris Wilson 		return NULL;
5859df30794SChris Wilson 
58605394f39SChris Wilson 	reloc_offset = src->gtt_offset;
5879df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
588788885aeSAndrew Morton 		unsigned long flags;
589e56660ddSChris Wilson 		void __iomem *s;
590e56660ddSChris Wilson 		void *d;
591788885aeSAndrew Morton 
592e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
5939df30794SChris Wilson 		if (d == NULL)
5949df30794SChris Wilson 			goto unwind;
595e56660ddSChris Wilson 
596788885aeSAndrew Morton 		local_irq_save(flags);
597e56660ddSChris Wilson 		s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
5983e4d3af5SPeter Zijlstra 					     reloc_offset);
599e56660ddSChris Wilson 		memcpy_fromio(d, s, PAGE_SIZE);
6003e4d3af5SPeter Zijlstra 		io_mapping_unmap_atomic(s);
601788885aeSAndrew Morton 		local_irq_restore(flags);
602e56660ddSChris Wilson 
6039df30794SChris Wilson 		dst->pages[page] = d;
604e56660ddSChris Wilson 
605e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
6069df30794SChris Wilson 	}
6079df30794SChris Wilson 	dst->page_count = page_count;
60805394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
6099df30794SChris Wilson 
6109df30794SChris Wilson 	return dst;
6119df30794SChris Wilson 
6129df30794SChris Wilson unwind:
6139df30794SChris Wilson 	while (page--)
6149df30794SChris Wilson 		kfree(dst->pages[page]);
6159df30794SChris Wilson 	kfree(dst);
6169df30794SChris Wilson 	return NULL;
6179df30794SChris Wilson }
6189df30794SChris Wilson 
6199df30794SChris Wilson static void
6209df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
6219df30794SChris Wilson {
6229df30794SChris Wilson 	int page;
6239df30794SChris Wilson 
6249df30794SChris Wilson 	if (obj == NULL)
6259df30794SChris Wilson 		return;
6269df30794SChris Wilson 
6279df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
6289df30794SChris Wilson 		kfree(obj->pages[page]);
6299df30794SChris Wilson 
6309df30794SChris Wilson 	kfree(obj);
6319df30794SChris Wilson }
6329df30794SChris Wilson 
6339df30794SChris Wilson static void
6349df30794SChris Wilson i915_error_state_free(struct drm_device *dev,
6359df30794SChris Wilson 		      struct drm_i915_error_state *error)
6369df30794SChris Wilson {
6379df30794SChris Wilson 	i915_error_object_free(error->batchbuffer[0]);
6389df30794SChris Wilson 	i915_error_object_free(error->batchbuffer[1]);
6399df30794SChris Wilson 	i915_error_object_free(error->ringbuffer);
6409df30794SChris Wilson 	kfree(error->active_bo);
6416ef3d427SChris Wilson 	kfree(error->overlay);
6429df30794SChris Wilson 	kfree(error);
6439df30794SChris Wilson }
6449df30794SChris Wilson 
6459df30794SChris Wilson static u32
6469df30794SChris Wilson i915_get_bbaddr(struct drm_device *dev, u32 *ring)
6479df30794SChris Wilson {
6489df30794SChris Wilson 	u32 cmd;
6499df30794SChris Wilson 
6509df30794SChris Wilson 	if (IS_I830(dev) || IS_845G(dev))
6519df30794SChris Wilson 		cmd = MI_BATCH_BUFFER;
652a6c45cf0SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 4)
6539df30794SChris Wilson 		cmd = (MI_BATCH_BUFFER_START | (2 << 6) |
6549df30794SChris Wilson 		       MI_BATCH_NON_SECURE_I965);
6559df30794SChris Wilson 	else
6569df30794SChris Wilson 		cmd = (MI_BATCH_BUFFER_START | (2 << 6));
6579df30794SChris Wilson 
6589df30794SChris Wilson 	return ring[0] == cmd ? ring[1] : 0;
6599df30794SChris Wilson }
6609df30794SChris Wilson 
6619df30794SChris Wilson static u32
6628168bd48SChris Wilson i915_ringbuffer_last_batch(struct drm_device *dev,
6638168bd48SChris Wilson 			   struct intel_ring_buffer *ring)
6649df30794SChris Wilson {
6659df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
6669df30794SChris Wilson 	u32 head, bbaddr;
6678168bd48SChris Wilson 	u32 *val;
6689df30794SChris Wilson 
6699df30794SChris Wilson 	/* Locate the current position in the ringbuffer and walk back
6709df30794SChris Wilson 	 * to find the most recently dispatched batch buffer.
6719df30794SChris Wilson 	 */
6728168bd48SChris Wilson 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
6739df30794SChris Wilson 
674ab5793adSChris Wilson 	val = (u32 *)(ring->virtual_start + head);
6758168bd48SChris Wilson 	while (--val >= (u32 *)ring->virtual_start) {
6768168bd48SChris Wilson 		bbaddr = i915_get_bbaddr(dev, val);
6779df30794SChris Wilson 		if (bbaddr)
678ab5793adSChris Wilson 			return bbaddr;
6799df30794SChris Wilson 	}
6809df30794SChris Wilson 
6818168bd48SChris Wilson 	val = (u32 *)(ring->virtual_start + ring->size);
6828168bd48SChris Wilson 	while (--val >= (u32 *)ring->virtual_start) {
6838168bd48SChris Wilson 		bbaddr = i915_get_bbaddr(dev, val);
6849df30794SChris Wilson 		if (bbaddr)
685ab5793adSChris Wilson 			return bbaddr;
6869df30794SChris Wilson 	}
6879df30794SChris Wilson 
688ab5793adSChris Wilson 	return 0;
6899df30794SChris Wilson }
6909df30794SChris Wilson 
691c724e8a9SChris Wilson static u32 capture_bo_list(struct drm_i915_error_buffer *err,
692c724e8a9SChris Wilson 			   int count,
693c724e8a9SChris Wilson 			   struct list_head *head)
694c724e8a9SChris Wilson {
695c724e8a9SChris Wilson 	struct drm_i915_gem_object *obj;
696c724e8a9SChris Wilson 	int i = 0;
697c724e8a9SChris Wilson 
698c724e8a9SChris Wilson 	list_for_each_entry(obj, head, mm_list) {
699c724e8a9SChris Wilson 		err->size = obj->base.size;
700c724e8a9SChris Wilson 		err->name = obj->base.name;
701c724e8a9SChris Wilson 		err->seqno = obj->last_rendering_seqno;
702c724e8a9SChris Wilson 		err->gtt_offset = obj->gtt_offset;
703c724e8a9SChris Wilson 		err->read_domains = obj->base.read_domains;
704c724e8a9SChris Wilson 		err->write_domain = obj->base.write_domain;
705c724e8a9SChris Wilson 		err->fence_reg = obj->fence_reg;
706c724e8a9SChris Wilson 		err->pinned = 0;
707c724e8a9SChris Wilson 		if (obj->pin_count > 0)
708c724e8a9SChris Wilson 			err->pinned = 1;
709c724e8a9SChris Wilson 		if (obj->user_pin_count > 0)
710c724e8a9SChris Wilson 			err->pinned = -1;
711c724e8a9SChris Wilson 		err->tiling = obj->tiling_mode;
712c724e8a9SChris Wilson 		err->dirty = obj->dirty;
713c724e8a9SChris Wilson 		err->purgeable = obj->madv != I915_MADV_WILLNEED;
7143685092bSChris Wilson 		err->ring = obj->ring ? obj->ring->id : 0;
715c724e8a9SChris Wilson 
716c724e8a9SChris Wilson 		if (++i == count)
717c724e8a9SChris Wilson 			break;
718c724e8a9SChris Wilson 
719c724e8a9SChris Wilson 		err++;
720c724e8a9SChris Wilson 	}
721c724e8a9SChris Wilson 
722c724e8a9SChris Wilson 	return i;
723c724e8a9SChris Wilson }
724c724e8a9SChris Wilson 
725748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
726748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
727748ebc60SChris Wilson {
728748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
729748ebc60SChris Wilson 	int i;
730748ebc60SChris Wilson 
731748ebc60SChris Wilson 	/* Fences */
732748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
733748ebc60SChris Wilson 	case 6:
734748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
735748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
736748ebc60SChris Wilson 		break;
737748ebc60SChris Wilson 	case 5:
738748ebc60SChris Wilson 	case 4:
739748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
740748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
741748ebc60SChris Wilson 		break;
742748ebc60SChris Wilson 	case 3:
743748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
744748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
745748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
746748ebc60SChris Wilson 	case 2:
747748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
748748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
749748ebc60SChris Wilson 		break;
750748ebc60SChris Wilson 
751748ebc60SChris Wilson 	}
752748ebc60SChris Wilson }
753748ebc60SChris Wilson 
7548a905236SJesse Barnes /**
7558a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
7568a905236SJesse Barnes  * @dev: drm device
7578a905236SJesse Barnes  *
7588a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
7598a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
7608a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
7618a905236SJesse Barnes  * to pick up.
7628a905236SJesse Barnes  */
76363eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
76463eeaf38SJesse Barnes {
76563eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
76605394f39SChris Wilson 	struct drm_i915_gem_object *obj;
76763eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
76805394f39SChris Wilson 	struct drm_i915_gem_object *batchbuffer[2];
76963eeaf38SJesse Barnes 	unsigned long flags;
7709df30794SChris Wilson 	u32 bbaddr;
7719df30794SChris Wilson 	int count;
77263eeaf38SJesse Barnes 
77363eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
7749df30794SChris Wilson 	error = dev_priv->first_error;
7759df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
7769df30794SChris Wilson 	if (error)
7779df30794SChris Wilson 		return;
77863eeaf38SJesse Barnes 
77963eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
78063eeaf38SJesse Barnes 	if (!error) {
7819df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
7829df30794SChris Wilson 		return;
78363eeaf38SJesse Barnes 	}
78463eeaf38SJesse Barnes 
7852fa772f3SChris Wilson 	DRM_DEBUG_DRIVER("generating error event\n");
7862fa772f3SChris Wilson 
7871ec14ad3SChris Wilson 	error->seqno = dev_priv->ring[RCS].get_seqno(&dev_priv->ring[RCS]);
78863eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
78963eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
79063eeaf38SJesse Barnes 	error->pipeastat = I915_READ(PIPEASTAT);
79163eeaf38SJesse Barnes 	error->pipebstat = I915_READ(PIPEBSTAT);
79263eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
793f406839fSChris Wilson 	error->error = 0;
794f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 6) {
795f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
796add354ddSChris Wilson 
7971d8f38f4SChris Wilson 		error->bcs_acthd = I915_READ(BCS_ACTHD);
7981d8f38f4SChris Wilson 		error->bcs_ipehr = I915_READ(BCS_IPEHR);
7991d8f38f4SChris Wilson 		error->bcs_ipeir = I915_READ(BCS_IPEIR);
8001d8f38f4SChris Wilson 		error->bcs_instdone = I915_READ(BCS_INSTDONE);
8011d8f38f4SChris Wilson 		error->bcs_seqno = 0;
8021ec14ad3SChris Wilson 		if (dev_priv->ring[BCS].get_seqno)
8031ec14ad3SChris Wilson 			error->bcs_seqno = dev_priv->ring[BCS].get_seqno(&dev_priv->ring[BCS]);
804add354ddSChris Wilson 
805add354ddSChris Wilson 		error->vcs_acthd = I915_READ(VCS_ACTHD);
806add354ddSChris Wilson 		error->vcs_ipehr = I915_READ(VCS_IPEHR);
807add354ddSChris Wilson 		error->vcs_ipeir = I915_READ(VCS_IPEIR);
808add354ddSChris Wilson 		error->vcs_instdone = I915_READ(VCS_INSTDONE);
809add354ddSChris Wilson 		error->vcs_seqno = 0;
8101ec14ad3SChris Wilson 		if (dev_priv->ring[VCS].get_seqno)
8111ec14ad3SChris Wilson 			error->vcs_seqno = dev_priv->ring[VCS].get_seqno(&dev_priv->ring[VCS]);
812f406839fSChris Wilson 	}
813f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
81463eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
81563eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
81663eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
81763eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
81863eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
81963eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
8209df30794SChris Wilson 		error->bbaddr = I915_READ64(BB_ADDR);
821f406839fSChris Wilson 	} else {
822f406839fSChris Wilson 		error->ipeir = I915_READ(IPEIR);
823f406839fSChris Wilson 		error->ipehr = I915_READ(IPEHR);
824f406839fSChris Wilson 		error->instdone = I915_READ(INSTDONE);
825f406839fSChris Wilson 		error->acthd = I915_READ(ACTHD);
826f406839fSChris Wilson 		error->bbaddr = 0;
8279df30794SChris Wilson 	}
828748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
8299df30794SChris Wilson 
8301ec14ad3SChris Wilson 	bbaddr = i915_ringbuffer_last_batch(dev, &dev_priv->ring[RCS]);
8319df30794SChris Wilson 
8329df30794SChris Wilson 	/* Grab the current batchbuffer, most likely to have crashed. */
8339df30794SChris Wilson 	batchbuffer[0] = NULL;
8349df30794SChris Wilson 	batchbuffer[1] = NULL;
8359df30794SChris Wilson 	count = 0;
83605394f39SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
8379df30794SChris Wilson 		if (batchbuffer[0] == NULL &&
83805394f39SChris Wilson 		    bbaddr >= obj->gtt_offset &&
83905394f39SChris Wilson 		    bbaddr < obj->gtt_offset + obj->base.size)
8409df30794SChris Wilson 			batchbuffer[0] = obj;
8419df30794SChris Wilson 
8429df30794SChris Wilson 		if (batchbuffer[1] == NULL &&
84305394f39SChris Wilson 		    error->acthd >= obj->gtt_offset &&
84405394f39SChris Wilson 		    error->acthd < obj->gtt_offset + obj->base.size)
8459df30794SChris Wilson 			batchbuffer[1] = obj;
8469df30794SChris Wilson 
8479df30794SChris Wilson 		count++;
8489df30794SChris Wilson 	}
849e56660ddSChris Wilson 	/* Scan the other lists for completeness for those bizarre errors. */
850e56660ddSChris Wilson 	if (batchbuffer[0] == NULL || batchbuffer[1] == NULL) {
85105394f39SChris Wilson 		list_for_each_entry(obj, &dev_priv->mm.flushing_list, mm_list) {
852e56660ddSChris Wilson 			if (batchbuffer[0] == NULL &&
85305394f39SChris Wilson 			    bbaddr >= obj->gtt_offset &&
85405394f39SChris Wilson 			    bbaddr < obj->gtt_offset + obj->base.size)
855e56660ddSChris Wilson 				batchbuffer[0] = obj;
856e56660ddSChris Wilson 
857e56660ddSChris Wilson 			if (batchbuffer[1] == NULL &&
85805394f39SChris Wilson 			    error->acthd >= obj->gtt_offset &&
85905394f39SChris Wilson 			    error->acthd < obj->gtt_offset + obj->base.size)
860e56660ddSChris Wilson 				batchbuffer[1] = obj;
861e56660ddSChris Wilson 
862e56660ddSChris Wilson 			if (batchbuffer[0] && batchbuffer[1])
863e56660ddSChris Wilson 				break;
864e56660ddSChris Wilson 		}
865e56660ddSChris Wilson 	}
866e56660ddSChris Wilson 	if (batchbuffer[0] == NULL || batchbuffer[1] == NULL) {
86705394f39SChris Wilson 		list_for_each_entry(obj, &dev_priv->mm.inactive_list, mm_list) {
868e56660ddSChris Wilson 			if (batchbuffer[0] == NULL &&
86905394f39SChris Wilson 			    bbaddr >= obj->gtt_offset &&
87005394f39SChris Wilson 			    bbaddr < obj->gtt_offset + obj->base.size)
871e56660ddSChris Wilson 				batchbuffer[0] = obj;
872e56660ddSChris Wilson 
873e56660ddSChris Wilson 			if (batchbuffer[1] == NULL &&
87405394f39SChris Wilson 			    error->acthd >= obj->gtt_offset &&
87505394f39SChris Wilson 			    error->acthd < obj->gtt_offset + obj->base.size)
876e56660ddSChris Wilson 				batchbuffer[1] = obj;
877e56660ddSChris Wilson 
878e56660ddSChris Wilson 			if (batchbuffer[0] && batchbuffer[1])
879e56660ddSChris Wilson 				break;
880e56660ddSChris Wilson 		}
881e56660ddSChris Wilson 	}
8829df30794SChris Wilson 
8839df30794SChris Wilson 	/* We need to copy these to an anonymous buffer as the simplest
884139d363bSAndrea Gelmini 	 * method to avoid being overwritten by userspace.
8859df30794SChris Wilson 	 */
8869df30794SChris Wilson 	error->batchbuffer[0] = i915_error_object_create(dev, batchbuffer[0]);
887e56660ddSChris Wilson 	if (batchbuffer[1] != batchbuffer[0])
8889df30794SChris Wilson 		error->batchbuffer[1] = i915_error_object_create(dev, batchbuffer[1]);
889e56660ddSChris Wilson 	else
890e56660ddSChris Wilson 		error->batchbuffer[1] = NULL;
8919df30794SChris Wilson 
8929df30794SChris Wilson 	/* Record the ringbuffer */
8938187a2b7SZou Nan hai 	error->ringbuffer = i915_error_object_create(dev,
8941ec14ad3SChris Wilson 						     dev_priv->ring[RCS].obj);
8959df30794SChris Wilson 
896c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
8979df30794SChris Wilson 	error->active_bo = NULL;
898c724e8a9SChris Wilson 	error->pinned_bo = NULL;
8999df30794SChris Wilson 
900c724e8a9SChris Wilson 	error->active_bo_count = count;
90105394f39SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
902c724e8a9SChris Wilson 		count++;
903c724e8a9SChris Wilson 	error->pinned_bo_count = count - error->active_bo_count;
904c724e8a9SChris Wilson 
905c724e8a9SChris Wilson 	if (count) {
9069df30794SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*count,
9079df30794SChris Wilson 					   GFP_ATOMIC);
908c724e8a9SChris Wilson 		if (error->active_bo)
909c724e8a9SChris Wilson 			error->pinned_bo =
910c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
9119df30794SChris Wilson 	}
912c724e8a9SChris Wilson 
913c724e8a9SChris Wilson 	if (error->active_bo)
914c724e8a9SChris Wilson 		error->active_bo_count =
915c724e8a9SChris Wilson 			capture_bo_list(error->active_bo,
916c724e8a9SChris Wilson 					error->active_bo_count,
917c724e8a9SChris Wilson 					&dev_priv->mm.active_list);
918c724e8a9SChris Wilson 
919c724e8a9SChris Wilson 	if (error->pinned_bo)
920c724e8a9SChris Wilson 		error->pinned_bo_count =
921c724e8a9SChris Wilson 			capture_bo_list(error->pinned_bo,
922c724e8a9SChris Wilson 					error->pinned_bo_count,
923c724e8a9SChris Wilson 					&dev_priv->mm.pinned_list);
92463eeaf38SJesse Barnes 
9258a905236SJesse Barnes 	do_gettimeofday(&error->time);
9268a905236SJesse Barnes 
9276ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
928c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
9296ef3d427SChris Wilson 
9309df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
9319df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
93263eeaf38SJesse Barnes 		dev_priv->first_error = error;
9339df30794SChris Wilson 		error = NULL;
9349df30794SChris Wilson 	}
93563eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
9369df30794SChris Wilson 
9379df30794SChris Wilson 	if (error)
9389df30794SChris Wilson 		i915_error_state_free(dev, error);
9399df30794SChris Wilson }
9409df30794SChris Wilson 
9419df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
9429df30794SChris Wilson {
9439df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
9449df30794SChris Wilson 	struct drm_i915_error_state *error;
9459df30794SChris Wilson 
9469df30794SChris Wilson 	spin_lock(&dev_priv->error_lock);
9479df30794SChris Wilson 	error = dev_priv->first_error;
9489df30794SChris Wilson 	dev_priv->first_error = NULL;
9499df30794SChris Wilson 	spin_unlock(&dev_priv->error_lock);
9509df30794SChris Wilson 
9519df30794SChris Wilson 	if (error)
9529df30794SChris Wilson 		i915_error_state_free(dev, error);
95363eeaf38SJesse Barnes }
9543bd3c932SChris Wilson #else
9553bd3c932SChris Wilson #define i915_capture_error_state(x)
9563bd3c932SChris Wilson #endif
95763eeaf38SJesse Barnes 
95835aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
959c0e09200SDave Airlie {
9608a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
96163eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
96263eeaf38SJesse Barnes 
96335aed2e6SChris Wilson 	if (!eir)
96435aed2e6SChris Wilson 		return;
96563eeaf38SJesse Barnes 
96663eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
96763eeaf38SJesse Barnes 	       eir);
9688a905236SJesse Barnes 
9698a905236SJesse Barnes 	if (IS_G4X(dev)) {
9708a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
9718a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
9728a905236SJesse Barnes 
9738a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
9748a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
9758a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
9768a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
9778a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
9788a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
9798a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
9808a905236SJesse Barnes 			       I915_READ(INSTPS));
9818a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
9828a905236SJesse Barnes 			       I915_READ(INSTDONE1));
9838a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
9848a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
9858a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
9863143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
9878a905236SJesse Barnes 		}
9888a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
9898a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
9908a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
9918a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
9928a905236SJesse Barnes 			       pgtbl_err);
9938a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
9943143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
9958a905236SJesse Barnes 		}
9968a905236SJesse Barnes 	}
9978a905236SJesse Barnes 
998a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
99963eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
100063eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
100163eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
100263eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
100363eeaf38SJesse Barnes 			       pgtbl_err);
100463eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
10053143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
100663eeaf38SJesse Barnes 		}
10078a905236SJesse Barnes 	}
10088a905236SJesse Barnes 
100963eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
101035aed2e6SChris Wilson 		u32 pipea_stats = I915_READ(PIPEASTAT);
101135aed2e6SChris Wilson 		u32 pipeb_stats = I915_READ(PIPEBSTAT);
101235aed2e6SChris Wilson 
101363eeaf38SJesse Barnes 		printk(KERN_ERR "memory refresh error\n");
101463eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
101563eeaf38SJesse Barnes 		       pipea_stats);
101663eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
101763eeaf38SJesse Barnes 		       pipeb_stats);
101863eeaf38SJesse Barnes 		/* pipestat has already been acked */
101963eeaf38SJesse Barnes 	}
102063eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
102163eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
102263eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
102363eeaf38SJesse Barnes 		       I915_READ(INSTPM));
1024a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
102563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
102663eeaf38SJesse Barnes 
102763eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
102863eeaf38SJesse Barnes 			       I915_READ(IPEIR));
102963eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
103063eeaf38SJesse Barnes 			       I915_READ(IPEHR));
103163eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
103263eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
103363eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
103463eeaf38SJesse Barnes 			       I915_READ(ACTHD));
103563eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
10363143a2bfSChris Wilson 			POSTING_READ(IPEIR);
103763eeaf38SJesse Barnes 		} else {
103863eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
103963eeaf38SJesse Barnes 
104063eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
104163eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
104263eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
104363eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
104463eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
104563eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
104663eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
104763eeaf38SJesse Barnes 			       I915_READ(INSTPS));
104863eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
104963eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
105063eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
105163eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
105263eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
10533143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
105463eeaf38SJesse Barnes 		}
105563eeaf38SJesse Barnes 	}
105663eeaf38SJesse Barnes 
105763eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
10583143a2bfSChris Wilson 	POSTING_READ(EIR);
105963eeaf38SJesse Barnes 	eir = I915_READ(EIR);
106063eeaf38SJesse Barnes 	if (eir) {
106163eeaf38SJesse Barnes 		/*
106263eeaf38SJesse Barnes 		 * some errors might have become stuck,
106363eeaf38SJesse Barnes 		 * mask them.
106463eeaf38SJesse Barnes 		 */
106563eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
106663eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
106763eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
106863eeaf38SJesse Barnes 	}
106935aed2e6SChris Wilson }
107035aed2e6SChris Wilson 
107135aed2e6SChris Wilson /**
107235aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
107335aed2e6SChris Wilson  * @dev: drm device
107435aed2e6SChris Wilson  *
107535aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
107635aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
107735aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
107835aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
107935aed2e6SChris Wilson  * of a ring dump etc.).
108035aed2e6SChris Wilson  */
1081527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
108235aed2e6SChris Wilson {
108335aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
108435aed2e6SChris Wilson 
108535aed2e6SChris Wilson 	i915_capture_error_state(dev);
108635aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
10878a905236SJesse Barnes 
1088ba1234d1SBen Gamari 	if (wedged) {
108930dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1090ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1091ba1234d1SBen Gamari 
109211ed50ecSBen Gamari 		/*
109311ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
109411ed50ecSBen Gamari 		 */
10951ec14ad3SChris Wilson 		wake_up_all(&dev_priv->ring[RCS].irq_queue);
1096f787a5f5SChris Wilson 		if (HAS_BSD(dev))
10971ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[VCS].irq_queue);
1098549f7365SChris Wilson 		if (HAS_BLT(dev))
10991ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[BCS].irq_queue);
110011ed50ecSBen Gamari 	}
110111ed50ecSBen Gamari 
11029c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
11038a905236SJesse Barnes }
11048a905236SJesse Barnes 
11054e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
11064e5359cdSSimon Farnsworth {
11074e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
11084e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11094e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
111005394f39SChris Wilson 	struct drm_i915_gem_object *obj;
11114e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
11124e5359cdSSimon Farnsworth 	unsigned long flags;
11134e5359cdSSimon Farnsworth 	bool stall_detected;
11144e5359cdSSimon Farnsworth 
11154e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
11164e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
11174e5359cdSSimon Farnsworth 		return;
11184e5359cdSSimon Farnsworth 
11194e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
11204e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
11214e5359cdSSimon Farnsworth 
11224e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
11234e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
11244e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
11254e5359cdSSimon Farnsworth 		return;
11264e5359cdSSimon Farnsworth 	}
11274e5359cdSSimon Farnsworth 
11284e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
112905394f39SChris Wilson 	obj = work->pending_flip_obj;
1130a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
11314e5359cdSSimon Farnsworth 		int dspsurf = intel_crtc->plane == 0 ? DSPASURF : DSPBSURF;
113205394f39SChris Wilson 		stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
11334e5359cdSSimon Farnsworth 	} else {
11344e5359cdSSimon Farnsworth 		int dspaddr = intel_crtc->plane == 0 ? DSPAADDR : DSPBADDR;
113505394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
11364e5359cdSSimon Farnsworth 							crtc->y * crtc->fb->pitch +
11374e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
11384e5359cdSSimon Farnsworth 	}
11394e5359cdSSimon Farnsworth 
11404e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
11414e5359cdSSimon Farnsworth 
11424e5359cdSSimon Farnsworth 	if (stall_detected) {
11434e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
11444e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
11454e5359cdSSimon Farnsworth 	}
11464e5359cdSSimon Farnsworth }
11474e5359cdSSimon Farnsworth 
11488a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
11498a905236SJesse Barnes {
11508a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
11518a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11528a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
11538a905236SJesse Barnes 	u32 iir, new_iir;
11548a905236SJesse Barnes 	u32 pipea_stats, pipeb_stats;
11558a905236SJesse Barnes 	u32 vblank_status;
11568a905236SJesse Barnes 	int vblank = 0;
11578a905236SJesse Barnes 	unsigned long irqflags;
11588a905236SJesse Barnes 	int irq_received;
11598a905236SJesse Barnes 	int ret = IRQ_NONE;
11608a905236SJesse Barnes 
11618a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
11628a905236SJesse Barnes 
1163bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1164f2b115e6SAdam Jackson 		return ironlake_irq_handler(dev);
11658a905236SJesse Barnes 
11668a905236SJesse Barnes 	iir = I915_READ(IIR);
11678a905236SJesse Barnes 
1168a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
1169d874bcffSJesse Barnes 		vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
1170e25e6601SJesse Barnes 	else
1171d874bcffSJesse Barnes 		vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
11728a905236SJesse Barnes 
11738a905236SJesse Barnes 	for (;;) {
11748a905236SJesse Barnes 		irq_received = iir != 0;
11758a905236SJesse Barnes 
11768a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
11778a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
11788a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
11798a905236SJesse Barnes 		 * interrupts (for non-MSI).
11808a905236SJesse Barnes 		 */
11811ec14ad3SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
11828a905236SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
11838a905236SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
11848a905236SJesse Barnes 
11858a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
1186ba1234d1SBen Gamari 			i915_handle_error(dev, false);
11878a905236SJesse Barnes 
11888a905236SJesse Barnes 		/*
11898a905236SJesse Barnes 		 * Clear the PIPE(A|B)STAT regs before the IIR
11908a905236SJesse Barnes 		 */
11918a905236SJesse Barnes 		if (pipea_stats & 0x8000ffff) {
11928a905236SJesse Barnes 			if (pipea_stats &  PIPE_FIFO_UNDERRUN_STATUS)
119344d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe a underrun\n");
11948a905236SJesse Barnes 			I915_WRITE(PIPEASTAT, pipea_stats);
11958a905236SJesse Barnes 			irq_received = 1;
11968a905236SJesse Barnes 		}
11978a905236SJesse Barnes 
11988a905236SJesse Barnes 		if (pipeb_stats & 0x8000ffff) {
11998a905236SJesse Barnes 			if (pipeb_stats &  PIPE_FIFO_UNDERRUN_STATUS)
120044d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe b underrun\n");
12018a905236SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
12028a905236SJesse Barnes 			irq_received = 1;
12038a905236SJesse Barnes 		}
12041ec14ad3SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
12058a905236SJesse Barnes 
12068a905236SJesse Barnes 		if (!irq_received)
12078a905236SJesse Barnes 			break;
12088a905236SJesse Barnes 
12098a905236SJesse Barnes 		ret = IRQ_HANDLED;
12108a905236SJesse Barnes 
12118a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
12128a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
12138a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
12148a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
12158a905236SJesse Barnes 
121644d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
12178a905236SJesse Barnes 				  hotplug_status);
12188a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
12199c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
12209c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
12218a905236SJesse Barnes 
12228a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
12238a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
122463eeaf38SJesse Barnes 		}
122563eeaf38SJesse Barnes 
1226673a394bSEric Anholt 		I915_WRITE(IIR, iir);
1227cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
12287c463586SKeith Packard 
12297c1c2871SDave Airlie 		if (dev->primary->master) {
12307c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
12317c1c2871SDave Airlie 			if (master_priv->sarea_priv)
12327c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
1233c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
12347c1c2871SDave Airlie 		}
12350a3e67a4SJesse Barnes 
1236549f7365SChris Wilson 		if (iir & I915_USER_INTERRUPT)
12371ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
12381ec14ad3SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
12391ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
1240d1b851fcSZou Nan hai 
12411afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
12426b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
12431afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
12441afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 0);
12451afe3e9dSJesse Barnes 		}
12466b95a207SKristian Høgsberg 
12471afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
124870565d00SJesse Barnes 			intel_prepare_page_flip(dev, 1);
12491afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
12501afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 1);
12511afe3e9dSJesse Barnes 		}
12526b95a207SKristian Høgsberg 
125305eff845SKeith Packard 		if (pipea_stats & vblank_status) {
12547c463586SKeith Packard 			vblank++;
12557c463586SKeith Packard 			drm_handle_vblank(dev, 0);
12564e5359cdSSimon Farnsworth 			if (!dev_priv->flip_pending_is_done) {
12574e5359cdSSimon Farnsworth 				i915_pageflip_stall_check(dev, 0);
12586b95a207SKristian Høgsberg 				intel_finish_page_flip(dev, 0);
12597c463586SKeith Packard 			}
12604e5359cdSSimon Farnsworth 		}
12617c463586SKeith Packard 
126205eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
12637c463586SKeith Packard 			vblank++;
12647c463586SKeith Packard 			drm_handle_vblank(dev, 1);
12654e5359cdSSimon Farnsworth 			if (!dev_priv->flip_pending_is_done) {
12664e5359cdSSimon Farnsworth 				i915_pageflip_stall_check(dev, 1);
12676b95a207SKristian Høgsberg 				intel_finish_page_flip(dev, 1);
12687c463586SKeith Packard 			}
12694e5359cdSSimon Farnsworth 		}
12707c463586SKeith Packard 
1271d874bcffSJesse Barnes 		if ((pipea_stats & PIPE_LEGACY_BLC_EVENT_STATUS) ||
1272d874bcffSJesse Barnes 		    (pipeb_stats & PIPE_LEGACY_BLC_EVENT_STATUS) ||
12737c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
12743b617967SChris Wilson 			intel_opregion_asle_intr(dev);
12750a3e67a4SJesse Barnes 
1276cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
1277cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
1278cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
1279cdfbc41fSEric Anholt 		 * we would never get another interrupt.
1280cdfbc41fSEric Anholt 		 *
1281cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
1282cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
1283cdfbc41fSEric Anholt 		 * another one.
1284cdfbc41fSEric Anholt 		 *
1285cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
1286cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
1287cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
1288cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
1289cdfbc41fSEric Anholt 		 * stray interrupts.
1290cdfbc41fSEric Anholt 		 */
1291cdfbc41fSEric Anholt 		iir = new_iir;
129205eff845SKeith Packard 	}
1293cdfbc41fSEric Anholt 
129405eff845SKeith Packard 	return ret;
1295c0e09200SDave Airlie }
1296c0e09200SDave Airlie 
1297c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
1298c0e09200SDave Airlie {
1299c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
13007c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1301c0e09200SDave Airlie 
1302c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
1303c0e09200SDave Airlie 
130444d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
1305c0e09200SDave Airlie 
1306c99b058fSKristian Høgsberg 	dev_priv->counter++;
1307c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
1308c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
13097c1c2871SDave Airlie 	if (master_priv->sarea_priv)
13107c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
1311c0e09200SDave Airlie 
1312e1f99ce6SChris Wilson 	if (BEGIN_LP_RING(4) == 0) {
1313585fb111SJesse Barnes 		OUT_RING(MI_STORE_DWORD_INDEX);
13140baf823aSKeith Packard 		OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1315c0e09200SDave Airlie 		OUT_RING(dev_priv->counter);
1316585fb111SJesse Barnes 		OUT_RING(MI_USER_INTERRUPT);
1317c0e09200SDave Airlie 		ADVANCE_LP_RING();
1318e1f99ce6SChris Wilson 	}
1319c0e09200SDave Airlie 
1320c0e09200SDave Airlie 	return dev_priv->counter;
1321c0e09200SDave Airlie }
1322c0e09200SDave Airlie 
13239d34e5dbSChris Wilson void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
13249d34e5dbSChris Wilson {
13259d34e5dbSChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13261ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
13279d34e5dbSChris Wilson 
1328b13c2b96SChris Wilson 	if (dev_priv->trace_irq_seqno == 0 &&
1329b13c2b96SChris Wilson 	    ring->irq_get(ring))
13309d34e5dbSChris Wilson 		dev_priv->trace_irq_seqno = seqno;
13319d34e5dbSChris Wilson }
13329d34e5dbSChris Wilson 
1333c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
1334c0e09200SDave Airlie {
1335c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13367c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1337c0e09200SDave Airlie 	int ret = 0;
13381ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
1339c0e09200SDave Airlie 
134044d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
1341c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
1342c0e09200SDave Airlie 
1343ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
13447c1c2871SDave Airlie 		if (master_priv->sarea_priv)
13457c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
1346c0e09200SDave Airlie 		return 0;
1347ed4cb414SEric Anholt 	}
1348c0e09200SDave Airlie 
13497c1c2871SDave Airlie 	if (master_priv->sarea_priv)
13507c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1351c0e09200SDave Airlie 
1352b13c2b96SChris Wilson 	ret = -ENODEV;
1353b13c2b96SChris Wilson 	if (ring->irq_get(ring)) {
13541ec14ad3SChris Wilson 		DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1355c0e09200SDave Airlie 			    READ_BREADCRUMB(dev_priv) >= irq_nr);
13561ec14ad3SChris Wilson 		ring->irq_put(ring);
1357b13c2b96SChris Wilson 	}
1358c0e09200SDave Airlie 
1359c0e09200SDave Airlie 	if (ret == -EBUSY) {
1360c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
1361c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1362c0e09200SDave Airlie 	}
1363c0e09200SDave Airlie 
1364c0e09200SDave Airlie 	return ret;
1365c0e09200SDave Airlie }
1366c0e09200SDave Airlie 
1367c0e09200SDave Airlie /* Needs the lock as it touches the ring.
1368c0e09200SDave Airlie  */
1369c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
1370c0e09200SDave Airlie 			 struct drm_file *file_priv)
1371c0e09200SDave Airlie {
1372c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1373c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
1374c0e09200SDave Airlie 	int result;
1375c0e09200SDave Airlie 
13761ec14ad3SChris Wilson 	if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
1377c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1378c0e09200SDave Airlie 		return -EINVAL;
1379c0e09200SDave Airlie 	}
1380299eb93cSEric Anholt 
1381299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1382299eb93cSEric Anholt 
1383546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
1384c0e09200SDave Airlie 	result = i915_emit_irq(dev);
1385546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
1386c0e09200SDave Airlie 
1387c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
1388c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
1389c0e09200SDave Airlie 		return -EFAULT;
1390c0e09200SDave Airlie 	}
1391c0e09200SDave Airlie 
1392c0e09200SDave Airlie 	return 0;
1393c0e09200SDave Airlie }
1394c0e09200SDave Airlie 
1395c0e09200SDave Airlie /* Doesn't need the hardware lock.
1396c0e09200SDave Airlie  */
1397c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
1398c0e09200SDave Airlie 			 struct drm_file *file_priv)
1399c0e09200SDave Airlie {
1400c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1401c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
1402c0e09200SDave Airlie 
1403c0e09200SDave Airlie 	if (!dev_priv) {
1404c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1405c0e09200SDave Airlie 		return -EINVAL;
1406c0e09200SDave Airlie 	}
1407c0e09200SDave Airlie 
1408c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
1409c0e09200SDave Airlie }
1410c0e09200SDave Airlie 
141142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
141242f52ef8SKeith Packard  * we use as a pipe index
141342f52ef8SKeith Packard  */
141442f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
14150a3e67a4SJesse Barnes {
14160a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1417e9d21d7fSKeith Packard 	unsigned long irqflags;
141871e0ffa5SJesse Barnes 
14195eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
142071e0ffa5SJesse Barnes 		return -EINVAL;
14210a3e67a4SJesse Barnes 
14221ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1423bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1424c062df61SLi Peng 		ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1425c062df61SLi Peng 					    DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1426a6c45cf0SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 4)
14277c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14287c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
14290a3e67a4SJesse Barnes 	else
14307c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14317c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
14321ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14330a3e67a4SJesse Barnes 	return 0;
14340a3e67a4SJesse Barnes }
14350a3e67a4SJesse Barnes 
143642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
143742f52ef8SKeith Packard  * we use as a pipe index
143842f52ef8SKeith Packard  */
143942f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
14400a3e67a4SJesse Barnes {
14410a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1442e9d21d7fSKeith Packard 	unsigned long irqflags;
14430a3e67a4SJesse Barnes 
14441ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1445bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1446c062df61SLi Peng 		ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1447c062df61SLi Peng 					     DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1448c062df61SLi Peng 	else
14497c463586SKeith Packard 		i915_disable_pipestat(dev_priv, pipe,
14507c463586SKeith Packard 				      PIPE_VBLANK_INTERRUPT_ENABLE |
14517c463586SKeith Packard 				      PIPE_START_VBLANK_INTERRUPT_ENABLE);
14521ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14530a3e67a4SJesse Barnes }
14540a3e67a4SJesse Barnes 
145579e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev)
145679e53945SJesse Barnes {
145779e53945SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1458e170b030SZhenyu Wang 
1459bad720ffSEric Anholt 	if (!HAS_PCH_SPLIT(dev))
14603b617967SChris Wilson 		intel_opregion_enable_asle(dev);
146179e53945SJesse Barnes 	dev_priv->irq_enabled = 1;
146279e53945SJesse Barnes }
146379e53945SJesse Barnes 
146479e53945SJesse Barnes 
1465c0e09200SDave Airlie /* Set the vblank monitor pipe
1466c0e09200SDave Airlie  */
1467c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1468c0e09200SDave Airlie 			 struct drm_file *file_priv)
1469c0e09200SDave Airlie {
1470c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1471c0e09200SDave Airlie 
1472c0e09200SDave Airlie 	if (!dev_priv) {
1473c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1474c0e09200SDave Airlie 		return -EINVAL;
1475c0e09200SDave Airlie 	}
1476c0e09200SDave Airlie 
1477c0e09200SDave Airlie 	return 0;
1478c0e09200SDave Airlie }
1479c0e09200SDave Airlie 
1480c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1481c0e09200SDave Airlie 			 struct drm_file *file_priv)
1482c0e09200SDave Airlie {
1483c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1484c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
1485c0e09200SDave Airlie 
1486c0e09200SDave Airlie 	if (!dev_priv) {
1487c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1488c0e09200SDave Airlie 		return -EINVAL;
1489c0e09200SDave Airlie 	}
1490c0e09200SDave Airlie 
14910a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1492c0e09200SDave Airlie 
1493c0e09200SDave Airlie 	return 0;
1494c0e09200SDave Airlie }
1495c0e09200SDave Airlie 
1496c0e09200SDave Airlie /**
1497c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
1498c0e09200SDave Airlie  */
1499c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
1500c0e09200SDave Airlie 		     struct drm_file *file_priv)
1501c0e09200SDave Airlie {
1502bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1503bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1504bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1505bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1506bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1507bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1508bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1509bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1510bd95e0a4SEric Anholt 	 *
1511bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1512bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1513bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1514bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
15150a3e67a4SJesse Barnes 	 */
1516c0e09200SDave Airlie 	return -EINVAL;
1517c0e09200SDave Airlie }
1518c0e09200SDave Airlie 
1519893eead0SChris Wilson static u32
1520893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1521852835f3SZou Nan hai {
1522893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1523893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1524893eead0SChris Wilson }
1525893eead0SChris Wilson 
1526893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1527893eead0SChris Wilson {
1528893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1529893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1530893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
1531b2223497SChris Wilson 		if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
1532893eead0SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1533893eead0SChris Wilson 				  ring->name,
1534b2223497SChris Wilson 				  ring->waiting_seqno,
1535893eead0SChris Wilson 				  ring->get_seqno(ring));
1536893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1537893eead0SChris Wilson 			*err = true;
1538893eead0SChris Wilson 		}
1539893eead0SChris Wilson 		return true;
1540893eead0SChris Wilson 	}
1541893eead0SChris Wilson 	return false;
1542f65d9421SBen Gamari }
1543f65d9421SBen Gamari 
15441ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
15451ec14ad3SChris Wilson {
15461ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
15471ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
15481ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
15491ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
15501ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
15511ec14ad3SChris Wilson 			  ring->name);
15521ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
15531ec14ad3SChris Wilson 		return true;
15541ec14ad3SChris Wilson 	}
15551ec14ad3SChris Wilson 	if (IS_GEN6(dev) &&
15561ec14ad3SChris Wilson 	    (tmp & RING_WAIT_SEMAPHORE)) {
15571ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
15581ec14ad3SChris Wilson 			  ring->name);
15591ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
15601ec14ad3SChris Wilson 		return true;
15611ec14ad3SChris Wilson 	}
15621ec14ad3SChris Wilson 	return false;
15631ec14ad3SChris Wilson }
15641ec14ad3SChris Wilson 
1565f65d9421SBen Gamari /**
1566f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1567f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1568f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1569f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1570f65d9421SBen Gamari  */
1571f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1572f65d9421SBen Gamari {
1573f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1574f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1575cbb465e7SChris Wilson 	uint32_t acthd, instdone, instdone1;
1576893eead0SChris Wilson 	bool err = false;
1577893eead0SChris Wilson 
1578893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
15791ec14ad3SChris Wilson 	if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
15801ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
15811ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
1582893eead0SChris Wilson 		dev_priv->hangcheck_count = 0;
1583893eead0SChris Wilson 		if (err)
1584893eead0SChris Wilson 			goto repeat;
1585893eead0SChris Wilson 		return;
1586893eead0SChris Wilson 	}
1587f65d9421SBen Gamari 
1588a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1589f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
1590cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1591cbb465e7SChris Wilson 		instdone1 = 0;
1592cbb465e7SChris Wilson 	} else {
1593f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
1594cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1595cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1596cbb465e7SChris Wilson 	}
1597f65d9421SBen Gamari 
1598cbb465e7SChris Wilson 	if (dev_priv->last_acthd == acthd &&
1599cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1600cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1601cbb465e7SChris Wilson 		if (dev_priv->hangcheck_count++ > 1) {
1602f65d9421SBen Gamari 			DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
16038c80b59bSChris Wilson 
16048c80b59bSChris Wilson 			if (!IS_GEN2(dev)) {
16058c80b59bSChris Wilson 				/* Is the chip hanging on a WAIT_FOR_EVENT?
16068c80b59bSChris Wilson 				 * If so we can simply poke the RB_WAIT bit
16078c80b59bSChris Wilson 				 * and break the hang. This should work on
16088c80b59bSChris Wilson 				 * all but the second generation chipsets.
16098c80b59bSChris Wilson 				 */
16101ec14ad3SChris Wilson 
16111ec14ad3SChris Wilson 				if (kick_ring(&dev_priv->ring[RCS]))
1612893eead0SChris Wilson 					goto repeat;
16131ec14ad3SChris Wilson 
16141ec14ad3SChris Wilson 				if (HAS_BSD(dev) &&
16151ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[VCS]))
16161ec14ad3SChris Wilson 					goto repeat;
16171ec14ad3SChris Wilson 
16181ec14ad3SChris Wilson 				if (HAS_BLT(dev) &&
16191ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[BCS]))
16201ec14ad3SChris Wilson 					goto repeat;
16218c80b59bSChris Wilson 			}
16228c80b59bSChris Wilson 
1623ba1234d1SBen Gamari 			i915_handle_error(dev, true);
1624f65d9421SBen Gamari 			return;
1625f65d9421SBen Gamari 		}
1626cbb465e7SChris Wilson 	} else {
1627cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1628cbb465e7SChris Wilson 
1629cbb465e7SChris Wilson 		dev_priv->last_acthd = acthd;
1630cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1631cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1632cbb465e7SChris Wilson 	}
1633f65d9421SBen Gamari 
1634893eead0SChris Wilson repeat:
1635f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1636b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1637b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1638f65d9421SBen Gamari }
1639f65d9421SBen Gamari 
1640c0e09200SDave Airlie /* drm_dma.h hooks
1641c0e09200SDave Airlie */
1642f2b115e6SAdam Jackson static void ironlake_irq_preinstall(struct drm_device *dev)
1643036a4a7dSZhenyu Wang {
1644036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1645036a4a7dSZhenyu Wang 
1646036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1647036a4a7dSZhenyu Wang 
1648036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1649036a4a7dSZhenyu Wang 
1650036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1651036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
16523143a2bfSChris Wilson 	POSTING_READ(DEIER);
1653036a4a7dSZhenyu Wang 
1654036a4a7dSZhenyu Wang 	/* and GT */
1655036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1656036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
16573143a2bfSChris Wilson 	POSTING_READ(GTIER);
1658c650156aSZhenyu Wang 
1659c650156aSZhenyu Wang 	/* south display irq */
1660c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1661c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
16623143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1663036a4a7dSZhenyu Wang }
1664036a4a7dSZhenyu Wang 
1665f2b115e6SAdam Jackson static int ironlake_irq_postinstall(struct drm_device *dev)
1666036a4a7dSZhenyu Wang {
1667036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1668036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1669013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1670013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
16711ec14ad3SChris Wilson 	u32 render_irqs;
16722d7b8366SYuanhan Liu 	u32 hotplug_mask;
1673036a4a7dSZhenyu Wang 
16741ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1675036a4a7dSZhenyu Wang 
1676036a4a7dSZhenyu Wang 	/* should always can generate irq */
1677036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
16781ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
16791ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
16803143a2bfSChris Wilson 	POSTING_READ(DEIER);
1681036a4a7dSZhenyu Wang 
16821ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1683036a4a7dSZhenyu Wang 
1684036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
16851ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1686881f47b6SXiang, Haihao 
16871ec14ad3SChris Wilson 	if (IS_GEN6(dev))
16881ec14ad3SChris Wilson 		render_irqs =
16891ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
16901ec14ad3SChris Wilson 			GT_GEN6_BSD_USER_INTERRUPT |
16911ec14ad3SChris Wilson 			GT_BLT_USER_INTERRUPT;
16921ec14ad3SChris Wilson 	else
16931ec14ad3SChris Wilson 		render_irqs =
169488f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1695c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
16961ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
16971ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
16983143a2bfSChris Wilson 	POSTING_READ(GTIER);
1699036a4a7dSZhenyu Wang 
17002d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
17012d7b8366SYuanhan Liu 		hotplug_mask = SDE_CRT_HOTPLUG_CPT | SDE_PORTB_HOTPLUG_CPT  |
17022d7b8366SYuanhan Liu 			       SDE_PORTC_HOTPLUG_CPT | SDE_PORTD_HOTPLUG_CPT ;
17032d7b8366SYuanhan Liu 	} else {
17042d7b8366SYuanhan Liu 		hotplug_mask = SDE_CRT_HOTPLUG | SDE_PORTB_HOTPLUG |
17052d7b8366SYuanhan Liu 			       SDE_PORTC_HOTPLUG | SDE_PORTD_HOTPLUG;
1706*776ad806SJesse Barnes 		hotplug_mask |= SDE_AUX_MASK | SDE_FDI_MASK | SDE_TRANS_MASK;
1707*776ad806SJesse Barnes 		I915_WRITE(FDI_RXA_IMR, 0);
1708*776ad806SJesse Barnes 		I915_WRITE(FDI_RXB_IMR, 0);
17092d7b8366SYuanhan Liu 	}
17102d7b8366SYuanhan Liu 
17111ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1712c650156aSZhenyu Wang 
1713c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
17141ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
17151ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
17163143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1717c650156aSZhenyu Wang 
1718f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1719f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1720f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1721f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1722f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1723f97108d1SJesse Barnes 	}
1724f97108d1SJesse Barnes 
1725036a4a7dSZhenyu Wang 	return 0;
1726036a4a7dSZhenyu Wang }
1727036a4a7dSZhenyu Wang 
1728c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1729c0e09200SDave Airlie {
1730c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1731c0e09200SDave Airlie 
173279e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
173379e53945SJesse Barnes 
1734036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
17358a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1736036a4a7dSZhenyu Wang 
1737bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1738f2b115e6SAdam Jackson 		ironlake_irq_preinstall(dev);
1739036a4a7dSZhenyu Wang 		return;
1740036a4a7dSZhenyu Wang 	}
1741036a4a7dSZhenyu Wang 
17425ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
17435ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
17445ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
17455ca58282SJesse Barnes 	}
17465ca58282SJesse Barnes 
17470a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
17487c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
17497c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
17500a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1751ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
17523143a2bfSChris Wilson 	POSTING_READ(IER);
1753c0e09200SDave Airlie }
1754c0e09200SDave Airlie 
1755b01f2c3aSJesse Barnes /*
1756b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
1757b01f2c3aSJesse Barnes  * enabled correctly.
1758b01f2c3aSJesse Barnes  */
17590a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1760c0e09200SDave Airlie {
1761c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17625ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
176363eeaf38SJesse Barnes 	u32 error_mask;
17640a3e67a4SJesse Barnes 
17651ec14ad3SChris Wilson 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
1766d1b851fcSZou Nan hai 	if (HAS_BSD(dev))
17671ec14ad3SChris Wilson 		DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
1768549f7365SChris Wilson 	if (HAS_BLT(dev))
17691ec14ad3SChris Wilson 		DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
1770d1b851fcSZou Nan hai 
17710a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1772ed4cb414SEric Anholt 
1773bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1774f2b115e6SAdam Jackson 		return ironlake_irq_postinstall(dev);
1775036a4a7dSZhenyu Wang 
17767c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
17771ec14ad3SChris Wilson 	dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
17788ee1c3dbSMatthew Garrett 
17797c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
17807c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
17817c463586SKeith Packard 
17825ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
1783c496fa1fSAdam Jackson 		/* Enable in IER... */
1784c496fa1fSAdam Jackson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1785c496fa1fSAdam Jackson 		/* and unmask in IMR */
17861ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
1787c496fa1fSAdam Jackson 	}
1788c496fa1fSAdam Jackson 
1789c496fa1fSAdam Jackson 	/*
1790c496fa1fSAdam Jackson 	 * Enable some error detection, note the instruction error mask
1791c496fa1fSAdam Jackson 	 * bit is reserved, so we leave it masked.
1792c496fa1fSAdam Jackson 	 */
1793c496fa1fSAdam Jackson 	if (IS_G4X(dev)) {
1794c496fa1fSAdam Jackson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
1795c496fa1fSAdam Jackson 			       GM45_ERROR_MEM_PRIV |
1796c496fa1fSAdam Jackson 			       GM45_ERROR_CP_PRIV |
1797c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1798c496fa1fSAdam Jackson 	} else {
1799c496fa1fSAdam Jackson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
1800c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1801c496fa1fSAdam Jackson 	}
1802c496fa1fSAdam Jackson 	I915_WRITE(EMR, error_mask);
1803c496fa1fSAdam Jackson 
18041ec14ad3SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
1805c496fa1fSAdam Jackson 	I915_WRITE(IER, enable_mask);
18063143a2bfSChris Wilson 	POSTING_READ(IER);
1807c496fa1fSAdam Jackson 
1808c496fa1fSAdam Jackson 	if (I915_HAS_HOTPLUG(dev)) {
18095ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
18105ca58282SJesse Barnes 
1811b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
1812b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1813b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1814b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1815b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1816b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1817b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
1818b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1819b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1820b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1821b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
18222d1c9752SAndy Lutomirski 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
1823b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
18242d1c9752SAndy Lutomirski 
18252d1c9752SAndy Lutomirski 			/* Programming the CRT detection parameters tends
18262d1c9752SAndy Lutomirski 			   to generate a spurious hotplug event about three
18272d1c9752SAndy Lutomirski 			   seconds later.  So just do it once.
18282d1c9752SAndy Lutomirski 			*/
18292d1c9752SAndy Lutomirski 			if (IS_G4X(dev))
18302d1c9752SAndy Lutomirski 				hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
18312d1c9752SAndy Lutomirski 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
18322d1c9752SAndy Lutomirski 		}
18332d1c9752SAndy Lutomirski 
1834b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
1835b01f2c3aSJesse Barnes 
18365ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
18375ca58282SJesse Barnes 	}
18385ca58282SJesse Barnes 
18393b617967SChris Wilson 	intel_opregion_enable_asle(dev);
18400a3e67a4SJesse Barnes 
18410a3e67a4SJesse Barnes 	return 0;
1842c0e09200SDave Airlie }
1843c0e09200SDave Airlie 
1844f2b115e6SAdam Jackson static void ironlake_irq_uninstall(struct drm_device *dev)
1845036a4a7dSZhenyu Wang {
1846036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1847036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1848036a4a7dSZhenyu Wang 
1849036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1850036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1851036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1852036a4a7dSZhenyu Wang 
1853036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1854036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1855036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1856036a4a7dSZhenyu Wang }
1857036a4a7dSZhenyu Wang 
1858c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1859c0e09200SDave Airlie {
1860c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1861c0e09200SDave Airlie 
1862c0e09200SDave Airlie 	if (!dev_priv)
1863c0e09200SDave Airlie 		return;
1864c0e09200SDave Airlie 
18650a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
18660a3e67a4SJesse Barnes 
1867bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1868f2b115e6SAdam Jackson 		ironlake_irq_uninstall(dev);
1869036a4a7dSZhenyu Wang 		return;
1870036a4a7dSZhenyu Wang 	}
1871036a4a7dSZhenyu Wang 
18725ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
18735ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
18745ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
18755ca58282SJesse Barnes 	}
18765ca58282SJesse Barnes 
18770a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
18787c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
18797c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
18800a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1881ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1882c0e09200SDave Airlie 
18837c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
18847c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
18857c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1886c0e09200SDave Airlie }
1887