xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision b6f7833b9712c0c01f94cc3a518dc62b07bd610b)
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 
2774041b853SChris Wilson int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
2780af7e4dfSMario Kleiner 			      int *max_error,
2790af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
2800af7e4dfSMario Kleiner 			      unsigned flags)
2810af7e4dfSMario Kleiner {
2824041b853SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2834041b853SChris Wilson 	struct drm_crtc *crtc;
2840af7e4dfSMario Kleiner 
2854041b853SChris Wilson 	if (pipe < 0 || pipe >= dev_priv->num_pipe) {
2864041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2870af7e4dfSMario Kleiner 		return -EINVAL;
2880af7e4dfSMario Kleiner 	}
2890af7e4dfSMario Kleiner 
2900af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
2914041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
2924041b853SChris Wilson 	if (crtc == NULL) {
2934041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2944041b853SChris Wilson 		return -EINVAL;
2954041b853SChris Wilson 	}
2964041b853SChris Wilson 
2974041b853SChris Wilson 	if (!crtc->enabled) {
2984041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
2994041b853SChris Wilson 		return -EBUSY;
3004041b853SChris Wilson 	}
3010af7e4dfSMario Kleiner 
3020af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
3034041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
3044041b853SChris Wilson 						     vblank_time, flags,
3054041b853SChris Wilson 						     crtc);
3060af7e4dfSMario Kleiner }
3070af7e4dfSMario Kleiner 
3085ca58282SJesse Barnes /*
3095ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
3105ca58282SJesse Barnes  */
3115ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
3125ca58282SJesse Barnes {
3135ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3145ca58282SJesse Barnes 						    hotplug_work);
3155ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
316c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
3174ef69c7aSChris Wilson 	struct intel_encoder *encoder;
3185ca58282SJesse Barnes 
3194ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
3204ef69c7aSChris Wilson 		if (encoder->hot_plug)
3214ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
322c31c4ba3SKeith Packard 
3235ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
324eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3255ca58282SJesse Barnes }
3265ca58282SJesse Barnes 
327f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
328f97108d1SJesse Barnes {
329f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
330b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
331f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
332f97108d1SJesse Barnes 
3337648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
334b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
335b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
336f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
337f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
338f97108d1SJesse Barnes 
339f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
340b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
341f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
342f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
343f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
344f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
345b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
346f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
347f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
348f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
349f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
350f97108d1SJesse Barnes 	}
351f97108d1SJesse Barnes 
3527648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
353f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
354f97108d1SJesse Barnes 
355f97108d1SJesse Barnes 	return;
356f97108d1SJesse Barnes }
357f97108d1SJesse Barnes 
358549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
359549f7365SChris Wilson 			struct intel_ring_buffer *ring)
360549f7365SChris Wilson {
361549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
362475553deSChris Wilson 	u32 seqno;
3639862e600SChris Wilson 
364475553deSChris Wilson 	if (ring->obj == NULL)
365475553deSChris Wilson 		return;
366475553deSChris Wilson 
367475553deSChris Wilson 	seqno = ring->get_seqno(ring);
368549f7365SChris Wilson 	trace_i915_gem_request_complete(dev, seqno);
3699862e600SChris Wilson 
3709862e600SChris Wilson 	ring->irq_seqno = seqno;
371549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3729862e600SChris Wilson 
373549f7365SChris Wilson 	dev_priv->hangcheck_count = 0;
374549f7365SChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
375549f7365SChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
376549f7365SChris Wilson }
377549f7365SChris Wilson 
3783b8d8d91SJesse Barnes static void gen6_pm_irq_handler(struct drm_device *dev)
3793b8d8d91SJesse Barnes {
3803b8d8d91SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3813b8d8d91SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
3823b8d8d91SJesse Barnes 	u32 pm_iir;
3833b8d8d91SJesse Barnes 
3843b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
3853b8d8d91SJesse Barnes 	if (!pm_iir)
3863b8d8d91SJesse Barnes 		return;
3873b8d8d91SJesse Barnes 
3883b8d8d91SJesse Barnes 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
3893b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
3903b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
3913b8d8d91SJesse Barnes 		if (new_delay > dev_priv->max_delay)
3923b8d8d91SJesse Barnes 			new_delay = dev_priv->max_delay;
3933b8d8d91SJesse Barnes 	} else if (pm_iir & (GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT)) {
3943b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
3953b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
3963b8d8d91SJesse Barnes 		if (new_delay < dev_priv->min_delay) {
3973b8d8d91SJesse Barnes 			new_delay = dev_priv->min_delay;
3983b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3993b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) |
4003b8d8d91SJesse Barnes 				   ((new_delay << 16) & 0x3f0000));
4013b8d8d91SJesse Barnes 		} else {
4023b8d8d91SJesse Barnes 			/* Make sure we continue to get down interrupts
4033b8d8d91SJesse Barnes 			 * until we hit the minimum frequency */
4043b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4053b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) & ~0x3f0000);
4063b8d8d91SJesse Barnes 		}
4073b8d8d91SJesse Barnes 
4083b8d8d91SJesse Barnes 	}
4093b8d8d91SJesse Barnes 
4103b8d8d91SJesse Barnes 	gen6_set_rps(dev, new_delay);
4113b8d8d91SJesse Barnes 	dev_priv->cur_delay = new_delay;
4123b8d8d91SJesse Barnes 
4133b8d8d91SJesse Barnes 	I915_WRITE(GEN6_PMIIR, pm_iir);
4143b8d8d91SJesse Barnes }
4153b8d8d91SJesse Barnes 
416776ad806SJesse Barnes static void pch_irq_handler(struct drm_device *dev)
417776ad806SJesse Barnes {
418776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
419776ad806SJesse Barnes 	u32 pch_iir;
420776ad806SJesse Barnes 
421776ad806SJesse Barnes 	pch_iir = I915_READ(SDEIIR);
422776ad806SJesse Barnes 
423776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
424776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
425776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
426776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
427776ad806SJesse Barnes 
428776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
429776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
430776ad806SJesse Barnes 
431776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
432776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
433776ad806SJesse Barnes 
434776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
435776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
436776ad806SJesse Barnes 
437776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
438776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
439776ad806SJesse Barnes 
440776ad806SJesse Barnes 	if (pch_iir & SDE_FDI_MASK) {
441776ad806SJesse Barnes 		u32 fdia, fdib;
442776ad806SJesse Barnes 
443776ad806SJesse Barnes 		fdia = I915_READ(FDI_RXA_IIR);
444776ad806SJesse Barnes 		fdib = I915_READ(FDI_RXB_IIR);
445776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH FDI RX interrupt; FDI RXA IIR: 0x%08x, FDI RXB IIR: 0x%08x\n", fdia, fdib);
446776ad806SJesse Barnes 	}
447776ad806SJesse Barnes 
448776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
449776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
450776ad806SJesse Barnes 
451776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
452776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
453776ad806SJesse Barnes 
454776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
455776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
456776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
457776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
458776ad806SJesse Barnes }
459776ad806SJesse Barnes 
460995b6762SChris Wilson static irqreturn_t ironlake_irq_handler(struct drm_device *dev)
461036a4a7dSZhenyu Wang {
462036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
463036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
4643b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
4652d7b8366SYuanhan Liu 	u32 hotplug_mask;
466036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
467881f47b6SXiang, Haihao 	u32 bsd_usr_interrupt = GT_BSD_USER_INTERRUPT;
468881f47b6SXiang, Haihao 
469881f47b6SXiang, Haihao 	if (IS_GEN6(dev))
470881f47b6SXiang, Haihao 		bsd_usr_interrupt = GT_GEN6_BSD_USER_INTERRUPT;
471036a4a7dSZhenyu Wang 
4722d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
4732d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
4742d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
4753143a2bfSChris Wilson 	POSTING_READ(DEIER);
4762d109a84SZou, Nanhai 
477036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
478036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
479c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
4803b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
481036a4a7dSZhenyu Wang 
4823b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
4833b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
484c7c85101SZou Nan hai 		goto done;
485036a4a7dSZhenyu Wang 
4862d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
4872d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
4882d7b8366SYuanhan Liu 	else
4892d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
4902d7b8366SYuanhan Liu 
491036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
492036a4a7dSZhenyu Wang 
493036a4a7dSZhenyu Wang 	if (dev->primary->master) {
494036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
495036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
496036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
497036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
498036a4a7dSZhenyu Wang 	}
499036a4a7dSZhenyu Wang 
500c6df541cSChris Wilson 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
5011ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[RCS]);
502881f47b6SXiang, Haihao 	if (gt_iir & bsd_usr_interrupt)
5031ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[VCS]);
5041ec14ad3SChris Wilson 	if (gt_iir & GT_BLT_USER_INTERRUPT)
5051ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[BCS]);
506036a4a7dSZhenyu Wang 
50701c66889SZhao Yakui 	if (de_iir & DE_GSE)
5083b617967SChris Wilson 		intel_opregion_gse_intr(dev);
50901c66889SZhao Yakui 
510f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
511013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
5122bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
513013d5aa2SJesse Barnes 	}
514013d5aa2SJesse Barnes 
515f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
516f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
5172bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
518013d5aa2SJesse Barnes 	}
519c062df61SLi Peng 
520f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
521f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
522f072d2e7SZhenyu Wang 
523f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
524f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
525f072d2e7SZhenyu Wang 
526c650156aSZhenyu Wang 	/* check event from PCH */
527776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
528776ad806SJesse Barnes 		if (pch_iir & hotplug_mask)
529c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
530776ad806SJesse Barnes 		pch_irq_handler(dev);
531776ad806SJesse Barnes 	}
532c650156aSZhenyu Wang 
533f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
5347648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
535f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
536f97108d1SJesse Barnes 	}
537f97108d1SJesse Barnes 
5383b8d8d91SJesse Barnes 	if (IS_GEN6(dev))
5393b8d8d91SJesse Barnes 		gen6_pm_irq_handler(dev);
5403b8d8d91SJesse Barnes 
541c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
542c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
543c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
544c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
545036a4a7dSZhenyu Wang 
546c7c85101SZou Nan hai done:
5472d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
5483143a2bfSChris Wilson 	POSTING_READ(DEIER);
5492d109a84SZou, Nanhai 
550036a4a7dSZhenyu Wang 	return ret;
551036a4a7dSZhenyu Wang }
552036a4a7dSZhenyu Wang 
5538a905236SJesse Barnes /**
5548a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
5558a905236SJesse Barnes  * @work: work struct
5568a905236SJesse Barnes  *
5578a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
5588a905236SJesse Barnes  * was detected.
5598a905236SJesse Barnes  */
5608a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
5618a905236SJesse Barnes {
5628a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5638a905236SJesse Barnes 						    error_work);
5648a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
565f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
566f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
567f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
5688a905236SJesse Barnes 
569f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
5708a905236SJesse Barnes 
571ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
57244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
573f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
574f803aa55SChris Wilson 		if (!i915_reset(dev, GRDOM_RENDER)) {
575ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
576f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
577f316a42cSBen Gamari 		}
57830dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
579f316a42cSBen Gamari 	}
5808a905236SJesse Barnes }
5818a905236SJesse Barnes 
5823bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
5839df30794SChris Wilson static struct drm_i915_error_object *
584bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
58505394f39SChris Wilson 			 struct drm_i915_gem_object *src)
5869df30794SChris Wilson {
5879df30794SChris Wilson 	struct drm_i915_error_object *dst;
5889df30794SChris Wilson 	int page, page_count;
589e56660ddSChris Wilson 	u32 reloc_offset;
5909df30794SChris Wilson 
59105394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
5929df30794SChris Wilson 		return NULL;
5939df30794SChris Wilson 
59405394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
5959df30794SChris Wilson 
5969df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof (u32 *), GFP_ATOMIC);
5979df30794SChris Wilson 	if (dst == NULL)
5989df30794SChris Wilson 		return NULL;
5999df30794SChris Wilson 
60005394f39SChris Wilson 	reloc_offset = src->gtt_offset;
6019df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
602788885aeSAndrew Morton 		unsigned long flags;
603e56660ddSChris Wilson 		void __iomem *s;
604e56660ddSChris Wilson 		void *d;
605788885aeSAndrew Morton 
606e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
6079df30794SChris Wilson 		if (d == NULL)
6089df30794SChris Wilson 			goto unwind;
609e56660ddSChris Wilson 
610788885aeSAndrew Morton 		local_irq_save(flags);
611e56660ddSChris Wilson 		s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
6123e4d3af5SPeter Zijlstra 					     reloc_offset);
613e56660ddSChris Wilson 		memcpy_fromio(d, s, PAGE_SIZE);
6143e4d3af5SPeter Zijlstra 		io_mapping_unmap_atomic(s);
615788885aeSAndrew Morton 		local_irq_restore(flags);
616e56660ddSChris Wilson 
6179df30794SChris Wilson 		dst->pages[page] = d;
618e56660ddSChris Wilson 
619e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
6209df30794SChris Wilson 	}
6219df30794SChris Wilson 	dst->page_count = page_count;
62205394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
6239df30794SChris Wilson 
6249df30794SChris Wilson 	return dst;
6259df30794SChris Wilson 
6269df30794SChris Wilson unwind:
6279df30794SChris Wilson 	while (page--)
6289df30794SChris Wilson 		kfree(dst->pages[page]);
6299df30794SChris Wilson 	kfree(dst);
6309df30794SChris Wilson 	return NULL;
6319df30794SChris Wilson }
6329df30794SChris Wilson 
6339df30794SChris Wilson static void
6349df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
6359df30794SChris Wilson {
6369df30794SChris Wilson 	int page;
6379df30794SChris Wilson 
6389df30794SChris Wilson 	if (obj == NULL)
6399df30794SChris Wilson 		return;
6409df30794SChris Wilson 
6419df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
6429df30794SChris Wilson 		kfree(obj->pages[page]);
6439df30794SChris Wilson 
6449df30794SChris Wilson 	kfree(obj);
6459df30794SChris Wilson }
6469df30794SChris Wilson 
6479df30794SChris Wilson static void
6489df30794SChris Wilson i915_error_state_free(struct drm_device *dev,
6499df30794SChris Wilson 		      struct drm_i915_error_state *error)
6509df30794SChris Wilson {
651e2f973d5SChris Wilson 	int i;
652e2f973d5SChris Wilson 
653e2f973d5SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->batchbuffer); i++)
654e2f973d5SChris Wilson 		i915_error_object_free(error->batchbuffer[i]);
655e2f973d5SChris Wilson 
656e2f973d5SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ringbuffer); i++)
657e2f973d5SChris Wilson 		i915_error_object_free(error->ringbuffer[i]);
658e2f973d5SChris Wilson 
6599df30794SChris Wilson 	kfree(error->active_bo);
6606ef3d427SChris Wilson 	kfree(error->overlay);
6619df30794SChris Wilson 	kfree(error);
6629df30794SChris Wilson }
6639df30794SChris Wilson 
664c724e8a9SChris Wilson static u32 capture_bo_list(struct drm_i915_error_buffer *err,
665c724e8a9SChris Wilson 			   int count,
666c724e8a9SChris Wilson 			   struct list_head *head)
667c724e8a9SChris Wilson {
668c724e8a9SChris Wilson 	struct drm_i915_gem_object *obj;
669c724e8a9SChris Wilson 	int i = 0;
670c724e8a9SChris Wilson 
671c724e8a9SChris Wilson 	list_for_each_entry(obj, head, mm_list) {
672c724e8a9SChris Wilson 		err->size = obj->base.size;
673c724e8a9SChris Wilson 		err->name = obj->base.name;
674c724e8a9SChris Wilson 		err->seqno = obj->last_rendering_seqno;
675c724e8a9SChris Wilson 		err->gtt_offset = obj->gtt_offset;
676c724e8a9SChris Wilson 		err->read_domains = obj->base.read_domains;
677c724e8a9SChris Wilson 		err->write_domain = obj->base.write_domain;
678c724e8a9SChris Wilson 		err->fence_reg = obj->fence_reg;
679c724e8a9SChris Wilson 		err->pinned = 0;
680c724e8a9SChris Wilson 		if (obj->pin_count > 0)
681c724e8a9SChris Wilson 			err->pinned = 1;
682c724e8a9SChris Wilson 		if (obj->user_pin_count > 0)
683c724e8a9SChris Wilson 			err->pinned = -1;
684c724e8a9SChris Wilson 		err->tiling = obj->tiling_mode;
685c724e8a9SChris Wilson 		err->dirty = obj->dirty;
686c724e8a9SChris Wilson 		err->purgeable = obj->madv != I915_MADV_WILLNEED;
6873685092bSChris Wilson 		err->ring = obj->ring ? obj->ring->id : 0;
688a779e5abSChris Wilson 		err->agp_type = obj->agp_type == AGP_USER_CACHED_MEMORY;
689c724e8a9SChris Wilson 
690c724e8a9SChris Wilson 		if (++i == count)
691c724e8a9SChris Wilson 			break;
692c724e8a9SChris Wilson 
693c724e8a9SChris Wilson 		err++;
694c724e8a9SChris Wilson 	}
695c724e8a9SChris Wilson 
696c724e8a9SChris Wilson 	return i;
697c724e8a9SChris Wilson }
698c724e8a9SChris Wilson 
699748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
700748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
701748ebc60SChris Wilson {
702748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
703748ebc60SChris Wilson 	int i;
704748ebc60SChris Wilson 
705748ebc60SChris Wilson 	/* Fences */
706748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
707748ebc60SChris Wilson 	case 6:
708748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
709748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
710748ebc60SChris Wilson 		break;
711748ebc60SChris Wilson 	case 5:
712748ebc60SChris Wilson 	case 4:
713748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
714748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
715748ebc60SChris Wilson 		break;
716748ebc60SChris Wilson 	case 3:
717748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
718748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
719748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
720748ebc60SChris Wilson 	case 2:
721748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
722748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
723748ebc60SChris Wilson 		break;
724748ebc60SChris Wilson 
725748ebc60SChris Wilson 	}
726748ebc60SChris Wilson }
727748ebc60SChris Wilson 
728bcfb2e28SChris Wilson static struct drm_i915_error_object *
729bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
730bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
731bcfb2e28SChris Wilson {
732bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
733bcfb2e28SChris Wilson 	u32 seqno;
734bcfb2e28SChris Wilson 
735bcfb2e28SChris Wilson 	if (!ring->get_seqno)
736bcfb2e28SChris Wilson 		return NULL;
737bcfb2e28SChris Wilson 
738bcfb2e28SChris Wilson 	seqno = ring->get_seqno(ring);
739bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
740bcfb2e28SChris Wilson 		if (obj->ring != ring)
741bcfb2e28SChris Wilson 			continue;
742bcfb2e28SChris Wilson 
743c37d9a5dSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
744bcfb2e28SChris Wilson 			continue;
745bcfb2e28SChris Wilson 
746bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
747bcfb2e28SChris Wilson 			continue;
748bcfb2e28SChris Wilson 
749bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
750bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
751bcfb2e28SChris Wilson 		 */
752bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
753bcfb2e28SChris Wilson 	}
754bcfb2e28SChris Wilson 
755bcfb2e28SChris Wilson 	return NULL;
756bcfb2e28SChris Wilson }
757bcfb2e28SChris Wilson 
7588a905236SJesse Barnes /**
7598a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
7608a905236SJesse Barnes  * @dev: drm device
7618a905236SJesse Barnes  *
7628a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
7638a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
7648a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
7658a905236SJesse Barnes  * to pick up.
7668a905236SJesse Barnes  */
76763eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
76863eeaf38SJesse Barnes {
76963eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
77005394f39SChris Wilson 	struct drm_i915_gem_object *obj;
77163eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
77263eeaf38SJesse Barnes 	unsigned long flags;
773bcfb2e28SChris Wilson 	int i;
77463eeaf38SJesse Barnes 
77563eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
7769df30794SChris Wilson 	error = dev_priv->first_error;
7779df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
7789df30794SChris Wilson 	if (error)
7799df30794SChris Wilson 		return;
78063eeaf38SJesse Barnes 
78163eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
78263eeaf38SJesse Barnes 	if (!error) {
7839df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
7849df30794SChris Wilson 		return;
78563eeaf38SJesse Barnes 	}
78663eeaf38SJesse Barnes 
787*b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
788*b6f7833bSChris Wilson 		 dev->primary->index);
7892fa772f3SChris Wilson 
7901ec14ad3SChris Wilson 	error->seqno = dev_priv->ring[RCS].get_seqno(&dev_priv->ring[RCS]);
79163eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
79263eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
79363eeaf38SJesse Barnes 	error->pipeastat = I915_READ(PIPEASTAT);
79463eeaf38SJesse Barnes 	error->pipebstat = I915_READ(PIPEBSTAT);
79563eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
796f406839fSChris Wilson 	error->error = 0;
797f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 6) {
798f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
799add354ddSChris Wilson 
8001d8f38f4SChris Wilson 		error->bcs_acthd = I915_READ(BCS_ACTHD);
8011d8f38f4SChris Wilson 		error->bcs_ipehr = I915_READ(BCS_IPEHR);
8021d8f38f4SChris Wilson 		error->bcs_ipeir = I915_READ(BCS_IPEIR);
8031d8f38f4SChris Wilson 		error->bcs_instdone = I915_READ(BCS_INSTDONE);
8041d8f38f4SChris Wilson 		error->bcs_seqno = 0;
8051ec14ad3SChris Wilson 		if (dev_priv->ring[BCS].get_seqno)
8061ec14ad3SChris Wilson 			error->bcs_seqno = dev_priv->ring[BCS].get_seqno(&dev_priv->ring[BCS]);
807add354ddSChris Wilson 
808add354ddSChris Wilson 		error->vcs_acthd = I915_READ(VCS_ACTHD);
809add354ddSChris Wilson 		error->vcs_ipehr = I915_READ(VCS_IPEHR);
810add354ddSChris Wilson 		error->vcs_ipeir = I915_READ(VCS_IPEIR);
811add354ddSChris Wilson 		error->vcs_instdone = I915_READ(VCS_INSTDONE);
812add354ddSChris Wilson 		error->vcs_seqno = 0;
8131ec14ad3SChris Wilson 		if (dev_priv->ring[VCS].get_seqno)
8141ec14ad3SChris Wilson 			error->vcs_seqno = dev_priv->ring[VCS].get_seqno(&dev_priv->ring[VCS]);
815f406839fSChris Wilson 	}
816f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
81763eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
81863eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
81963eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
82063eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
82163eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
82263eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
8239df30794SChris Wilson 		error->bbaddr = I915_READ64(BB_ADDR);
824f406839fSChris Wilson 	} else {
825f406839fSChris Wilson 		error->ipeir = I915_READ(IPEIR);
826f406839fSChris Wilson 		error->ipehr = I915_READ(IPEHR);
827f406839fSChris Wilson 		error->instdone = I915_READ(INSTDONE);
828f406839fSChris Wilson 		error->acthd = I915_READ(ACTHD);
829f406839fSChris Wilson 		error->bbaddr = 0;
8309df30794SChris Wilson 	}
831748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
8329df30794SChris Wilson 
833e2f973d5SChris Wilson 	/* Record the active batch and ring buffers */
834e2f973d5SChris Wilson 	for (i = 0; i < I915_NUM_RINGS; i++) {
835bcfb2e28SChris Wilson 		error->batchbuffer[i] =
836bcfb2e28SChris Wilson 			i915_error_first_batchbuffer(dev_priv,
837bcfb2e28SChris Wilson 						     &dev_priv->ring[i]);
8389df30794SChris Wilson 
839e2f973d5SChris Wilson 		error->ringbuffer[i] =
840e2f973d5SChris Wilson 			i915_error_object_create(dev_priv,
841e2f973d5SChris Wilson 						 dev_priv->ring[i].obj);
842e2f973d5SChris Wilson 	}
8439df30794SChris Wilson 
844c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
8459df30794SChris Wilson 	error->active_bo = NULL;
846c724e8a9SChris Wilson 	error->pinned_bo = NULL;
8479df30794SChris Wilson 
848bcfb2e28SChris Wilson 	i = 0;
849bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
850bcfb2e28SChris Wilson 		i++;
851bcfb2e28SChris Wilson 	error->active_bo_count = i;
85205394f39SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
853bcfb2e28SChris Wilson 		i++;
854bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
855c724e8a9SChris Wilson 
8568e934dbfSChris Wilson 	error->active_bo = NULL;
8578e934dbfSChris Wilson 	error->pinned_bo = NULL;
858bcfb2e28SChris Wilson 	if (i) {
859bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
8609df30794SChris Wilson 					   GFP_ATOMIC);
861c724e8a9SChris Wilson 		if (error->active_bo)
862c724e8a9SChris Wilson 			error->pinned_bo =
863c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
8649df30794SChris Wilson 	}
865c724e8a9SChris Wilson 
866c724e8a9SChris Wilson 	if (error->active_bo)
867c724e8a9SChris Wilson 		error->active_bo_count =
868c724e8a9SChris Wilson 			capture_bo_list(error->active_bo,
869c724e8a9SChris Wilson 					error->active_bo_count,
870c724e8a9SChris Wilson 					&dev_priv->mm.active_list);
871c724e8a9SChris Wilson 
872c724e8a9SChris Wilson 	if (error->pinned_bo)
873c724e8a9SChris Wilson 		error->pinned_bo_count =
874c724e8a9SChris Wilson 			capture_bo_list(error->pinned_bo,
875c724e8a9SChris Wilson 					error->pinned_bo_count,
876c724e8a9SChris Wilson 					&dev_priv->mm.pinned_list);
87763eeaf38SJesse Barnes 
8788a905236SJesse Barnes 	do_gettimeofday(&error->time);
8798a905236SJesse Barnes 
8806ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
881c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
8826ef3d427SChris Wilson 
8839df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
8849df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
88563eeaf38SJesse Barnes 		dev_priv->first_error = error;
8869df30794SChris Wilson 		error = NULL;
8879df30794SChris Wilson 	}
88863eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
8899df30794SChris Wilson 
8909df30794SChris Wilson 	if (error)
8919df30794SChris Wilson 		i915_error_state_free(dev, error);
8929df30794SChris Wilson }
8939df30794SChris Wilson 
8949df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
8959df30794SChris Wilson {
8969df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
8979df30794SChris Wilson 	struct drm_i915_error_state *error;
8989df30794SChris Wilson 
8999df30794SChris Wilson 	spin_lock(&dev_priv->error_lock);
9009df30794SChris Wilson 	error = dev_priv->first_error;
9019df30794SChris Wilson 	dev_priv->first_error = NULL;
9029df30794SChris Wilson 	spin_unlock(&dev_priv->error_lock);
9039df30794SChris Wilson 
9049df30794SChris Wilson 	if (error)
9059df30794SChris Wilson 		i915_error_state_free(dev, error);
90663eeaf38SJesse Barnes }
9073bd3c932SChris Wilson #else
9083bd3c932SChris Wilson #define i915_capture_error_state(x)
9093bd3c932SChris Wilson #endif
91063eeaf38SJesse Barnes 
91135aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
912c0e09200SDave Airlie {
9138a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
91463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
91563eeaf38SJesse Barnes 
91635aed2e6SChris Wilson 	if (!eir)
91735aed2e6SChris Wilson 		return;
91863eeaf38SJesse Barnes 
91963eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
92063eeaf38SJesse Barnes 	       eir);
9218a905236SJesse Barnes 
9228a905236SJesse Barnes 	if (IS_G4X(dev)) {
9238a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
9248a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
9258a905236SJesse Barnes 
9268a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
9278a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
9288a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
9298a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
9308a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
9318a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
9328a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
9338a905236SJesse Barnes 			       I915_READ(INSTPS));
9348a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
9358a905236SJesse Barnes 			       I915_READ(INSTDONE1));
9368a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
9378a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
9388a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
9393143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
9408a905236SJesse Barnes 		}
9418a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
9428a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
9438a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
9448a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
9458a905236SJesse Barnes 			       pgtbl_err);
9468a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
9473143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
9488a905236SJesse Barnes 		}
9498a905236SJesse Barnes 	}
9508a905236SJesse Barnes 
951a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
95263eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
95363eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
95463eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
95563eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
95663eeaf38SJesse Barnes 			       pgtbl_err);
95763eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
9583143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
95963eeaf38SJesse Barnes 		}
9608a905236SJesse Barnes 	}
9618a905236SJesse Barnes 
96263eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
96335aed2e6SChris Wilson 		u32 pipea_stats = I915_READ(PIPEASTAT);
96435aed2e6SChris Wilson 		u32 pipeb_stats = I915_READ(PIPEBSTAT);
96535aed2e6SChris Wilson 
96663eeaf38SJesse Barnes 		printk(KERN_ERR "memory refresh error\n");
96763eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
96863eeaf38SJesse Barnes 		       pipea_stats);
96963eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
97063eeaf38SJesse Barnes 		       pipeb_stats);
97163eeaf38SJesse Barnes 		/* pipestat has already been acked */
97263eeaf38SJesse Barnes 	}
97363eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
97463eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
97563eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
97663eeaf38SJesse Barnes 		       I915_READ(INSTPM));
977a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
97863eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
97963eeaf38SJesse Barnes 
98063eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
98163eeaf38SJesse Barnes 			       I915_READ(IPEIR));
98263eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
98363eeaf38SJesse Barnes 			       I915_READ(IPEHR));
98463eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
98563eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
98663eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
98763eeaf38SJesse Barnes 			       I915_READ(ACTHD));
98863eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
9893143a2bfSChris Wilson 			POSTING_READ(IPEIR);
99063eeaf38SJesse Barnes 		} else {
99163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
99263eeaf38SJesse Barnes 
99363eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
99463eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
99563eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
99663eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
99763eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
99863eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
99963eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
100063eeaf38SJesse Barnes 			       I915_READ(INSTPS));
100163eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
100263eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
100363eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
100463eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
100563eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
10063143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
100763eeaf38SJesse Barnes 		}
100863eeaf38SJesse Barnes 	}
100963eeaf38SJesse Barnes 
101063eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
10113143a2bfSChris Wilson 	POSTING_READ(EIR);
101263eeaf38SJesse Barnes 	eir = I915_READ(EIR);
101363eeaf38SJesse Barnes 	if (eir) {
101463eeaf38SJesse Barnes 		/*
101563eeaf38SJesse Barnes 		 * some errors might have become stuck,
101663eeaf38SJesse Barnes 		 * mask them.
101763eeaf38SJesse Barnes 		 */
101863eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
101963eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
102063eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
102163eeaf38SJesse Barnes 	}
102235aed2e6SChris Wilson }
102335aed2e6SChris Wilson 
102435aed2e6SChris Wilson /**
102535aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
102635aed2e6SChris Wilson  * @dev: drm device
102735aed2e6SChris Wilson  *
102835aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
102935aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
103035aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
103135aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
103235aed2e6SChris Wilson  * of a ring dump etc.).
103335aed2e6SChris Wilson  */
1034527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
103535aed2e6SChris Wilson {
103635aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
103735aed2e6SChris Wilson 
103835aed2e6SChris Wilson 	i915_capture_error_state(dev);
103935aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
10408a905236SJesse Barnes 
1041ba1234d1SBen Gamari 	if (wedged) {
104230dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1043ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1044ba1234d1SBen Gamari 
104511ed50ecSBen Gamari 		/*
104611ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
104711ed50ecSBen Gamari 		 */
10481ec14ad3SChris Wilson 		wake_up_all(&dev_priv->ring[RCS].irq_queue);
1049f787a5f5SChris Wilson 		if (HAS_BSD(dev))
10501ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[VCS].irq_queue);
1051549f7365SChris Wilson 		if (HAS_BLT(dev))
10521ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[BCS].irq_queue);
105311ed50ecSBen Gamari 	}
105411ed50ecSBen Gamari 
10559c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
10568a905236SJesse Barnes }
10578a905236SJesse Barnes 
10584e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
10594e5359cdSSimon Farnsworth {
10604e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
10614e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10624e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
106305394f39SChris Wilson 	struct drm_i915_gem_object *obj;
10644e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
10654e5359cdSSimon Farnsworth 	unsigned long flags;
10664e5359cdSSimon Farnsworth 	bool stall_detected;
10674e5359cdSSimon Farnsworth 
10684e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
10694e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
10704e5359cdSSimon Farnsworth 		return;
10714e5359cdSSimon Farnsworth 
10724e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
10734e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
10744e5359cdSSimon Farnsworth 
10754e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
10764e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
10774e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
10784e5359cdSSimon Farnsworth 		return;
10794e5359cdSSimon Farnsworth 	}
10804e5359cdSSimon Farnsworth 
10814e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
108205394f39SChris Wilson 	obj = work->pending_flip_obj;
1083a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
10844e5359cdSSimon Farnsworth 		int dspsurf = intel_crtc->plane == 0 ? DSPASURF : DSPBSURF;
108505394f39SChris Wilson 		stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
10864e5359cdSSimon Farnsworth 	} else {
10874e5359cdSSimon Farnsworth 		int dspaddr = intel_crtc->plane == 0 ? DSPAADDR : DSPBADDR;
108805394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
10894e5359cdSSimon Farnsworth 							crtc->y * crtc->fb->pitch +
10904e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
10914e5359cdSSimon Farnsworth 	}
10924e5359cdSSimon Farnsworth 
10934e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
10944e5359cdSSimon Farnsworth 
10954e5359cdSSimon Farnsworth 	if (stall_detected) {
10964e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
10974e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
10984e5359cdSSimon Farnsworth 	}
10994e5359cdSSimon Farnsworth }
11004e5359cdSSimon Farnsworth 
11018a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
11028a905236SJesse Barnes {
11038a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
11048a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11058a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
11068a905236SJesse Barnes 	u32 iir, new_iir;
11078a905236SJesse Barnes 	u32 pipea_stats, pipeb_stats;
11088a905236SJesse Barnes 	u32 vblank_status;
11098a905236SJesse Barnes 	int vblank = 0;
11108a905236SJesse Barnes 	unsigned long irqflags;
11118a905236SJesse Barnes 	int irq_received;
11128a905236SJesse Barnes 	int ret = IRQ_NONE;
11138a905236SJesse Barnes 
11148a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
11158a905236SJesse Barnes 
1116bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1117f2b115e6SAdam Jackson 		return ironlake_irq_handler(dev);
11188a905236SJesse Barnes 
11198a905236SJesse Barnes 	iir = I915_READ(IIR);
11208a905236SJesse Barnes 
1121a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
1122d874bcffSJesse Barnes 		vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
1123e25e6601SJesse Barnes 	else
1124d874bcffSJesse Barnes 		vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
11258a905236SJesse Barnes 
11268a905236SJesse Barnes 	for (;;) {
11278a905236SJesse Barnes 		irq_received = iir != 0;
11288a905236SJesse Barnes 
11298a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
11308a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
11318a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
11328a905236SJesse Barnes 		 * interrupts (for non-MSI).
11338a905236SJesse Barnes 		 */
11341ec14ad3SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
11358a905236SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
11368a905236SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
11378a905236SJesse Barnes 
11388a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
1139ba1234d1SBen Gamari 			i915_handle_error(dev, false);
11408a905236SJesse Barnes 
11418a905236SJesse Barnes 		/*
11428a905236SJesse Barnes 		 * Clear the PIPE(A|B)STAT regs before the IIR
11438a905236SJesse Barnes 		 */
11448a905236SJesse Barnes 		if (pipea_stats & 0x8000ffff) {
11458a905236SJesse Barnes 			if (pipea_stats &  PIPE_FIFO_UNDERRUN_STATUS)
114644d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe a underrun\n");
11478a905236SJesse Barnes 			I915_WRITE(PIPEASTAT, pipea_stats);
11488a905236SJesse Barnes 			irq_received = 1;
11498a905236SJesse Barnes 		}
11508a905236SJesse Barnes 
11518a905236SJesse Barnes 		if (pipeb_stats & 0x8000ffff) {
11528a905236SJesse Barnes 			if (pipeb_stats &  PIPE_FIFO_UNDERRUN_STATUS)
115344d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe b underrun\n");
11548a905236SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
11558a905236SJesse Barnes 			irq_received = 1;
11568a905236SJesse Barnes 		}
11571ec14ad3SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
11588a905236SJesse Barnes 
11598a905236SJesse Barnes 		if (!irq_received)
11608a905236SJesse Barnes 			break;
11618a905236SJesse Barnes 
11628a905236SJesse Barnes 		ret = IRQ_HANDLED;
11638a905236SJesse Barnes 
11648a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
11658a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
11668a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
11678a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
11688a905236SJesse Barnes 
116944d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
11708a905236SJesse Barnes 				  hotplug_status);
11718a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
11729c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
11739c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
11748a905236SJesse Barnes 
11758a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
11768a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
117763eeaf38SJesse Barnes 		}
117863eeaf38SJesse Barnes 
1179673a394bSEric Anholt 		I915_WRITE(IIR, iir);
1180cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
11817c463586SKeith Packard 
11827c1c2871SDave Airlie 		if (dev->primary->master) {
11837c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
11847c1c2871SDave Airlie 			if (master_priv->sarea_priv)
11857c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
1186c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
11877c1c2871SDave Airlie 		}
11880a3e67a4SJesse Barnes 
1189549f7365SChris Wilson 		if (iir & I915_USER_INTERRUPT)
11901ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
11911ec14ad3SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
11921ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
1193d1b851fcSZou Nan hai 
11941afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
11956b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
11961afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
11971afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 0);
11981afe3e9dSJesse Barnes 		}
11996b95a207SKristian Høgsberg 
12001afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
120170565d00SJesse Barnes 			intel_prepare_page_flip(dev, 1);
12021afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
12031afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 1);
12041afe3e9dSJesse Barnes 		}
12056b95a207SKristian Høgsberg 
120678c6e170SChris Wilson 		if (pipea_stats & vblank_status &&
120778c6e170SChris Wilson 		    drm_handle_vblank(dev, 0)) {
12087c463586SKeith Packard 			vblank++;
12094e5359cdSSimon Farnsworth 			if (!dev_priv->flip_pending_is_done) {
12104e5359cdSSimon Farnsworth 				i915_pageflip_stall_check(dev, 0);
12116b95a207SKristian Høgsberg 				intel_finish_page_flip(dev, 0);
12127c463586SKeith Packard 			}
12134e5359cdSSimon Farnsworth 		}
12147c463586SKeith Packard 
121578c6e170SChris Wilson 		if (pipeb_stats & vblank_status &&
121678c6e170SChris Wilson 		    drm_handle_vblank(dev, 1)) {
12177c463586SKeith Packard 			vblank++;
12184e5359cdSSimon Farnsworth 			if (!dev_priv->flip_pending_is_done) {
12194e5359cdSSimon Farnsworth 				i915_pageflip_stall_check(dev, 1);
12206b95a207SKristian Høgsberg 				intel_finish_page_flip(dev, 1);
12217c463586SKeith Packard 			}
12224e5359cdSSimon Farnsworth 		}
12237c463586SKeith Packard 
1224d874bcffSJesse Barnes 		if ((pipea_stats & PIPE_LEGACY_BLC_EVENT_STATUS) ||
1225d874bcffSJesse Barnes 		    (pipeb_stats & PIPE_LEGACY_BLC_EVENT_STATUS) ||
12267c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
12273b617967SChris Wilson 			intel_opregion_asle_intr(dev);
12280a3e67a4SJesse Barnes 
1229cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
1230cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
1231cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
1232cdfbc41fSEric Anholt 		 * we would never get another interrupt.
1233cdfbc41fSEric Anholt 		 *
1234cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
1235cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
1236cdfbc41fSEric Anholt 		 * another one.
1237cdfbc41fSEric Anholt 		 *
1238cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
1239cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
1240cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
1241cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
1242cdfbc41fSEric Anholt 		 * stray interrupts.
1243cdfbc41fSEric Anholt 		 */
1244cdfbc41fSEric Anholt 		iir = new_iir;
124505eff845SKeith Packard 	}
1246cdfbc41fSEric Anholt 
124705eff845SKeith Packard 	return ret;
1248c0e09200SDave Airlie }
1249c0e09200SDave Airlie 
1250c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
1251c0e09200SDave Airlie {
1252c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
12537c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1254c0e09200SDave Airlie 
1255c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
1256c0e09200SDave Airlie 
125744d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
1258c0e09200SDave Airlie 
1259c99b058fSKristian Høgsberg 	dev_priv->counter++;
1260c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
1261c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
12627c1c2871SDave Airlie 	if (master_priv->sarea_priv)
12637c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
1264c0e09200SDave Airlie 
1265e1f99ce6SChris Wilson 	if (BEGIN_LP_RING(4) == 0) {
1266585fb111SJesse Barnes 		OUT_RING(MI_STORE_DWORD_INDEX);
12670baf823aSKeith Packard 		OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1268c0e09200SDave Airlie 		OUT_RING(dev_priv->counter);
1269585fb111SJesse Barnes 		OUT_RING(MI_USER_INTERRUPT);
1270c0e09200SDave Airlie 		ADVANCE_LP_RING();
1271e1f99ce6SChris Wilson 	}
1272c0e09200SDave Airlie 
1273c0e09200SDave Airlie 	return dev_priv->counter;
1274c0e09200SDave Airlie }
1275c0e09200SDave Airlie 
12769d34e5dbSChris Wilson void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
12779d34e5dbSChris Wilson {
12789d34e5dbSChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
12791ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
12809d34e5dbSChris Wilson 
1281b13c2b96SChris Wilson 	if (dev_priv->trace_irq_seqno == 0 &&
1282b13c2b96SChris Wilson 	    ring->irq_get(ring))
12839d34e5dbSChris Wilson 		dev_priv->trace_irq_seqno = seqno;
12849d34e5dbSChris Wilson }
12859d34e5dbSChris Wilson 
1286c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
1287c0e09200SDave Airlie {
1288c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
12897c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1290c0e09200SDave Airlie 	int ret = 0;
12911ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
1292c0e09200SDave Airlie 
129344d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
1294c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
1295c0e09200SDave Airlie 
1296ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
12977c1c2871SDave Airlie 		if (master_priv->sarea_priv)
12987c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
1299c0e09200SDave Airlie 		return 0;
1300ed4cb414SEric Anholt 	}
1301c0e09200SDave Airlie 
13027c1c2871SDave Airlie 	if (master_priv->sarea_priv)
13037c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1304c0e09200SDave Airlie 
1305b13c2b96SChris Wilson 	if (ring->irq_get(ring)) {
13061ec14ad3SChris Wilson 		DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1307c0e09200SDave Airlie 			    READ_BREADCRUMB(dev_priv) >= irq_nr);
13081ec14ad3SChris Wilson 		ring->irq_put(ring);
13095a9a8d1aSChris Wilson 	} else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
13105a9a8d1aSChris Wilson 		ret = -EBUSY;
1311c0e09200SDave Airlie 
1312c0e09200SDave Airlie 	if (ret == -EBUSY) {
1313c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
1314c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1315c0e09200SDave Airlie 	}
1316c0e09200SDave Airlie 
1317c0e09200SDave Airlie 	return ret;
1318c0e09200SDave Airlie }
1319c0e09200SDave Airlie 
1320c0e09200SDave Airlie /* Needs the lock as it touches the ring.
1321c0e09200SDave Airlie  */
1322c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
1323c0e09200SDave Airlie 			 struct drm_file *file_priv)
1324c0e09200SDave Airlie {
1325c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1326c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
1327c0e09200SDave Airlie 	int result;
1328c0e09200SDave Airlie 
13291ec14ad3SChris Wilson 	if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
1330c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1331c0e09200SDave Airlie 		return -EINVAL;
1332c0e09200SDave Airlie 	}
1333299eb93cSEric Anholt 
1334299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1335299eb93cSEric Anholt 
1336546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
1337c0e09200SDave Airlie 	result = i915_emit_irq(dev);
1338546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
1339c0e09200SDave Airlie 
1340c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
1341c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
1342c0e09200SDave Airlie 		return -EFAULT;
1343c0e09200SDave Airlie 	}
1344c0e09200SDave Airlie 
1345c0e09200SDave Airlie 	return 0;
1346c0e09200SDave Airlie }
1347c0e09200SDave Airlie 
1348c0e09200SDave Airlie /* Doesn't need the hardware lock.
1349c0e09200SDave Airlie  */
1350c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
1351c0e09200SDave Airlie 			 struct drm_file *file_priv)
1352c0e09200SDave Airlie {
1353c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1354c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
1355c0e09200SDave Airlie 
1356c0e09200SDave Airlie 	if (!dev_priv) {
1357c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1358c0e09200SDave Airlie 		return -EINVAL;
1359c0e09200SDave Airlie 	}
1360c0e09200SDave Airlie 
1361c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
1362c0e09200SDave Airlie }
1363c0e09200SDave Airlie 
1364b0b544cdSChris Wilson static void i915_vblank_work_func(struct work_struct *work)
1365b0b544cdSChris Wilson {
1366b0b544cdSChris Wilson 	drm_i915_private_t *dev_priv =
1367b0b544cdSChris Wilson 		container_of(work, drm_i915_private_t, vblank_work);
1368b0b544cdSChris Wilson 
1369b0b544cdSChris Wilson 	if (atomic_read(&dev_priv->vblank_enabled)) {
1370b0b544cdSChris Wilson 		if (!dev_priv->vblank_pm_qos.pm_qos_class)
1371b0b544cdSChris Wilson 			pm_qos_add_request(&dev_priv->vblank_pm_qos,
1372b0b544cdSChris Wilson 					   PM_QOS_CPU_DMA_LATENCY,
1373b0b544cdSChris Wilson 					   15); //>=20 won't work
1374b0b544cdSChris Wilson 	} else {
1375b0b544cdSChris Wilson 		if (dev_priv->vblank_pm_qos.pm_qos_class)
1376b0b544cdSChris Wilson 			pm_qos_remove_request(&dev_priv->vblank_pm_qos);
1377b0b544cdSChris Wilson 	}
1378b0b544cdSChris Wilson }
1379b0b544cdSChris Wilson 
138042f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
138142f52ef8SKeith Packard  * we use as a pipe index
138242f52ef8SKeith Packard  */
138342f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
13840a3e67a4SJesse Barnes {
13850a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1386e9d21d7fSKeith Packard 	unsigned long irqflags;
138771e0ffa5SJesse Barnes 
13885eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
138971e0ffa5SJesse Barnes 		return -EINVAL;
13900a3e67a4SJesse Barnes 
13911ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1392bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1393c062df61SLi Peng 		ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1394c062df61SLi Peng 					    DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1395a6c45cf0SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 4)
13967c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
13977c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
13980a3e67a4SJesse Barnes 	else
13997c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14007c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
14011ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1402b0b544cdSChris Wilson 
1403b0b544cdSChris Wilson 	/* gen3 platforms have an issue with vsync interrupts not reaching
1404b0b544cdSChris Wilson 	 * cpu during deep c-state sleep (>C1), so we need to install a
1405b0b544cdSChris Wilson 	 * PM QoS handle to prevent C-state starvation of the GPU.
1406b0b544cdSChris Wilson 	 */
1407b0b544cdSChris Wilson 	if (dev_priv->info->gen == 3 && !dev_priv->info->is_g33) {
1408b0b544cdSChris Wilson 		atomic_inc(&dev_priv->vblank_enabled);
1409b0b544cdSChris Wilson 		queue_work(dev_priv->wq, &dev_priv->vblank_work);
1410b0b544cdSChris Wilson 	}
1411b0b544cdSChris Wilson 
14120a3e67a4SJesse Barnes 	return 0;
14130a3e67a4SJesse Barnes }
14140a3e67a4SJesse Barnes 
141542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
141642f52ef8SKeith Packard  * we use as a pipe index
141742f52ef8SKeith Packard  */
141842f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
14190a3e67a4SJesse Barnes {
14200a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1421e9d21d7fSKeith Packard 	unsigned long irqflags;
14220a3e67a4SJesse Barnes 
1423b0b544cdSChris Wilson 	if (dev_priv->info->gen == 3 && !dev_priv->info->is_g33) {
1424b0b544cdSChris Wilson 		atomic_dec(&dev_priv->vblank_enabled);
1425b0b544cdSChris Wilson 		queue_work(dev_priv->wq, &dev_priv->vblank_work);
1426b0b544cdSChris Wilson 	}
1427b0b544cdSChris Wilson 
14281ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1429bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1430c062df61SLi Peng 		ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1431c062df61SLi Peng 					     DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1432c062df61SLi Peng 	else
14337c463586SKeith Packard 		i915_disable_pipestat(dev_priv, pipe,
14347c463586SKeith Packard 				      PIPE_VBLANK_INTERRUPT_ENABLE |
14357c463586SKeith Packard 				      PIPE_START_VBLANK_INTERRUPT_ENABLE);
14361ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14370a3e67a4SJesse Barnes }
14380a3e67a4SJesse Barnes 
1439c0e09200SDave Airlie /* Set the vblank monitor pipe
1440c0e09200SDave Airlie  */
1441c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1442c0e09200SDave Airlie 			 struct drm_file *file_priv)
1443c0e09200SDave Airlie {
1444c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1445c0e09200SDave Airlie 
1446c0e09200SDave Airlie 	if (!dev_priv) {
1447c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1448c0e09200SDave Airlie 		return -EINVAL;
1449c0e09200SDave Airlie 	}
1450c0e09200SDave Airlie 
1451c0e09200SDave Airlie 	return 0;
1452c0e09200SDave Airlie }
1453c0e09200SDave Airlie 
1454c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1455c0e09200SDave Airlie 			 struct drm_file *file_priv)
1456c0e09200SDave Airlie {
1457c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1458c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
1459c0e09200SDave Airlie 
1460c0e09200SDave Airlie 	if (!dev_priv) {
1461c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1462c0e09200SDave Airlie 		return -EINVAL;
1463c0e09200SDave Airlie 	}
1464c0e09200SDave Airlie 
14650a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1466c0e09200SDave Airlie 
1467c0e09200SDave Airlie 	return 0;
1468c0e09200SDave Airlie }
1469c0e09200SDave Airlie 
1470c0e09200SDave Airlie /**
1471c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
1472c0e09200SDave Airlie  */
1473c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
1474c0e09200SDave Airlie 		     struct drm_file *file_priv)
1475c0e09200SDave Airlie {
1476bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1477bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1478bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1479bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1480bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1481bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1482bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1483bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1484bd95e0a4SEric Anholt 	 *
1485bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1486bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1487bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1488bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
14890a3e67a4SJesse Barnes 	 */
1490c0e09200SDave Airlie 	return -EINVAL;
1491c0e09200SDave Airlie }
1492c0e09200SDave Airlie 
1493893eead0SChris Wilson static u32
1494893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1495852835f3SZou Nan hai {
1496893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1497893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1498893eead0SChris Wilson }
1499893eead0SChris Wilson 
1500893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1501893eead0SChris Wilson {
1502893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1503893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1504893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
1505b2223497SChris Wilson 		if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
1506893eead0SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1507893eead0SChris Wilson 				  ring->name,
1508b2223497SChris Wilson 				  ring->waiting_seqno,
1509893eead0SChris Wilson 				  ring->get_seqno(ring));
1510893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1511893eead0SChris Wilson 			*err = true;
1512893eead0SChris Wilson 		}
1513893eead0SChris Wilson 		return true;
1514893eead0SChris Wilson 	}
1515893eead0SChris Wilson 	return false;
1516f65d9421SBen Gamari }
1517f65d9421SBen Gamari 
15181ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
15191ec14ad3SChris Wilson {
15201ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
15211ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
15221ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
15231ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
15241ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
15251ec14ad3SChris Wilson 			  ring->name);
15261ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
15271ec14ad3SChris Wilson 		return true;
15281ec14ad3SChris Wilson 	}
15291ec14ad3SChris Wilson 	if (IS_GEN6(dev) &&
15301ec14ad3SChris Wilson 	    (tmp & RING_WAIT_SEMAPHORE)) {
15311ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
15321ec14ad3SChris Wilson 			  ring->name);
15331ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
15341ec14ad3SChris Wilson 		return true;
15351ec14ad3SChris Wilson 	}
15361ec14ad3SChris Wilson 	return false;
15371ec14ad3SChris Wilson }
15381ec14ad3SChris Wilson 
1539f65d9421SBen Gamari /**
1540f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1541f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1542f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1543f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1544f65d9421SBen Gamari  */
1545f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1546f65d9421SBen Gamari {
1547f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1548f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1549cbb465e7SChris Wilson 	uint32_t acthd, instdone, instdone1;
1550893eead0SChris Wilson 	bool err = false;
1551893eead0SChris Wilson 
1552893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
15531ec14ad3SChris Wilson 	if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
15541ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
15551ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
1556893eead0SChris Wilson 		dev_priv->hangcheck_count = 0;
1557893eead0SChris Wilson 		if (err)
1558893eead0SChris Wilson 			goto repeat;
1559893eead0SChris Wilson 		return;
1560893eead0SChris Wilson 	}
1561f65d9421SBen Gamari 
1562a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1563f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
1564cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1565cbb465e7SChris Wilson 		instdone1 = 0;
1566cbb465e7SChris Wilson 	} else {
1567f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
1568cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1569cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1570cbb465e7SChris Wilson 	}
1571f65d9421SBen Gamari 
1572cbb465e7SChris Wilson 	if (dev_priv->last_acthd == acthd &&
1573cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1574cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1575cbb465e7SChris Wilson 		if (dev_priv->hangcheck_count++ > 1) {
1576f65d9421SBen Gamari 			DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
15778c80b59bSChris Wilson 
15788c80b59bSChris Wilson 			if (!IS_GEN2(dev)) {
15798c80b59bSChris Wilson 				/* Is the chip hanging on a WAIT_FOR_EVENT?
15808c80b59bSChris Wilson 				 * If so we can simply poke the RB_WAIT bit
15818c80b59bSChris Wilson 				 * and break the hang. This should work on
15828c80b59bSChris Wilson 				 * all but the second generation chipsets.
15838c80b59bSChris Wilson 				 */
15841ec14ad3SChris Wilson 
15851ec14ad3SChris Wilson 				if (kick_ring(&dev_priv->ring[RCS]))
1586893eead0SChris Wilson 					goto repeat;
15871ec14ad3SChris Wilson 
15881ec14ad3SChris Wilson 				if (HAS_BSD(dev) &&
15891ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[VCS]))
15901ec14ad3SChris Wilson 					goto repeat;
15911ec14ad3SChris Wilson 
15921ec14ad3SChris Wilson 				if (HAS_BLT(dev) &&
15931ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[BCS]))
15941ec14ad3SChris Wilson 					goto repeat;
15958c80b59bSChris Wilson 			}
15968c80b59bSChris Wilson 
1597ba1234d1SBen Gamari 			i915_handle_error(dev, true);
1598f65d9421SBen Gamari 			return;
1599f65d9421SBen Gamari 		}
1600cbb465e7SChris Wilson 	} else {
1601cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1602cbb465e7SChris Wilson 
1603cbb465e7SChris Wilson 		dev_priv->last_acthd = acthd;
1604cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1605cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1606cbb465e7SChris Wilson 	}
1607f65d9421SBen Gamari 
1608893eead0SChris Wilson repeat:
1609f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1610b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1611b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1612f65d9421SBen Gamari }
1613f65d9421SBen Gamari 
1614c0e09200SDave Airlie /* drm_dma.h hooks
1615c0e09200SDave Airlie */
1616f2b115e6SAdam Jackson static void ironlake_irq_preinstall(struct drm_device *dev)
1617036a4a7dSZhenyu Wang {
1618036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1619036a4a7dSZhenyu Wang 
1620036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1621036a4a7dSZhenyu Wang 
1622036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1623036a4a7dSZhenyu Wang 
1624036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1625036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
16263143a2bfSChris Wilson 	POSTING_READ(DEIER);
1627036a4a7dSZhenyu Wang 
1628036a4a7dSZhenyu Wang 	/* and GT */
1629036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1630036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
16313143a2bfSChris Wilson 	POSTING_READ(GTIER);
1632c650156aSZhenyu Wang 
1633c650156aSZhenyu Wang 	/* south display irq */
1634c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1635c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
16363143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1637036a4a7dSZhenyu Wang }
1638036a4a7dSZhenyu Wang 
1639f2b115e6SAdam Jackson static int ironlake_irq_postinstall(struct drm_device *dev)
1640036a4a7dSZhenyu Wang {
1641036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1642036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1643013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1644013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
16451ec14ad3SChris Wilson 	u32 render_irqs;
16462d7b8366SYuanhan Liu 	u32 hotplug_mask;
1647036a4a7dSZhenyu Wang 
16481ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1649036a4a7dSZhenyu Wang 
1650036a4a7dSZhenyu Wang 	/* should always can generate irq */
1651036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
16521ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
16531ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
16543143a2bfSChris Wilson 	POSTING_READ(DEIER);
1655036a4a7dSZhenyu Wang 
16561ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1657036a4a7dSZhenyu Wang 
1658036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
16591ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1660881f47b6SXiang, Haihao 
16611ec14ad3SChris Wilson 	if (IS_GEN6(dev))
16621ec14ad3SChris Wilson 		render_irqs =
16631ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
16641ec14ad3SChris Wilson 			GT_GEN6_BSD_USER_INTERRUPT |
16651ec14ad3SChris Wilson 			GT_BLT_USER_INTERRUPT;
16661ec14ad3SChris Wilson 	else
16671ec14ad3SChris Wilson 		render_irqs =
166888f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1669c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
16701ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
16711ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
16723143a2bfSChris Wilson 	POSTING_READ(GTIER);
1673036a4a7dSZhenyu Wang 
16742d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
16752d7b8366SYuanhan Liu 		hotplug_mask = SDE_CRT_HOTPLUG_CPT | SDE_PORTB_HOTPLUG_CPT  |
16762d7b8366SYuanhan Liu 			       SDE_PORTC_HOTPLUG_CPT | SDE_PORTD_HOTPLUG_CPT ;
16772d7b8366SYuanhan Liu 	} else {
16782d7b8366SYuanhan Liu 		hotplug_mask = SDE_CRT_HOTPLUG | SDE_PORTB_HOTPLUG |
16792d7b8366SYuanhan Liu 			       SDE_PORTC_HOTPLUG | SDE_PORTD_HOTPLUG;
1680776ad806SJesse Barnes 		hotplug_mask |= SDE_AUX_MASK | SDE_FDI_MASK | SDE_TRANS_MASK;
1681776ad806SJesse Barnes 		I915_WRITE(FDI_RXA_IMR, 0);
1682776ad806SJesse Barnes 		I915_WRITE(FDI_RXB_IMR, 0);
16832d7b8366SYuanhan Liu 	}
16842d7b8366SYuanhan Liu 
16851ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1686c650156aSZhenyu Wang 
1687c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
16881ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
16891ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
16903143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1691c650156aSZhenyu Wang 
1692f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1693f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1694f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1695f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1696f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1697f97108d1SJesse Barnes 	}
1698f97108d1SJesse Barnes 
1699036a4a7dSZhenyu Wang 	return 0;
1700036a4a7dSZhenyu Wang }
1701036a4a7dSZhenyu Wang 
1702c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1703c0e09200SDave Airlie {
1704c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1705c0e09200SDave Airlie 
170679e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
1707b0b544cdSChris Wilson 	atomic_set(&dev_priv->vblank_enabled, 0);
170879e53945SJesse Barnes 
1709036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
17108a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1711b0b544cdSChris Wilson 	INIT_WORK(&dev_priv->vblank_work, i915_vblank_work_func);
1712036a4a7dSZhenyu Wang 
1713bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1714f2b115e6SAdam Jackson 		ironlake_irq_preinstall(dev);
1715036a4a7dSZhenyu Wang 		return;
1716036a4a7dSZhenyu Wang 	}
1717036a4a7dSZhenyu Wang 
17185ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
17195ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
17205ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
17215ca58282SJesse Barnes 	}
17225ca58282SJesse Barnes 
17230a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
17247c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
17257c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
17260a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1727ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
17283143a2bfSChris Wilson 	POSTING_READ(IER);
1729c0e09200SDave Airlie }
1730c0e09200SDave Airlie 
1731b01f2c3aSJesse Barnes /*
1732b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
1733b01f2c3aSJesse Barnes  * enabled correctly.
1734b01f2c3aSJesse Barnes  */
17350a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1736c0e09200SDave Airlie {
1737c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17385ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
173963eeaf38SJesse Barnes 	u32 error_mask;
17400a3e67a4SJesse Barnes 
17411ec14ad3SChris Wilson 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
1742d1b851fcSZou Nan hai 	if (HAS_BSD(dev))
17431ec14ad3SChris Wilson 		DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
1744549f7365SChris Wilson 	if (HAS_BLT(dev))
17451ec14ad3SChris Wilson 		DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
1746d1b851fcSZou Nan hai 
17470a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1748ed4cb414SEric Anholt 
1749bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1750f2b115e6SAdam Jackson 		return ironlake_irq_postinstall(dev);
1751036a4a7dSZhenyu Wang 
17527c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
17531ec14ad3SChris Wilson 	dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
17548ee1c3dbSMatthew Garrett 
17557c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
17567c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
17577c463586SKeith Packard 
17585ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
1759c496fa1fSAdam Jackson 		/* Enable in IER... */
1760c496fa1fSAdam Jackson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1761c496fa1fSAdam Jackson 		/* and unmask in IMR */
17621ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
1763c496fa1fSAdam Jackson 	}
1764c496fa1fSAdam Jackson 
1765c496fa1fSAdam Jackson 	/*
1766c496fa1fSAdam Jackson 	 * Enable some error detection, note the instruction error mask
1767c496fa1fSAdam Jackson 	 * bit is reserved, so we leave it masked.
1768c496fa1fSAdam Jackson 	 */
1769c496fa1fSAdam Jackson 	if (IS_G4X(dev)) {
1770c496fa1fSAdam Jackson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
1771c496fa1fSAdam Jackson 			       GM45_ERROR_MEM_PRIV |
1772c496fa1fSAdam Jackson 			       GM45_ERROR_CP_PRIV |
1773c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1774c496fa1fSAdam Jackson 	} else {
1775c496fa1fSAdam Jackson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
1776c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1777c496fa1fSAdam Jackson 	}
1778c496fa1fSAdam Jackson 	I915_WRITE(EMR, error_mask);
1779c496fa1fSAdam Jackson 
17801ec14ad3SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
1781c496fa1fSAdam Jackson 	I915_WRITE(IER, enable_mask);
17823143a2bfSChris Wilson 	POSTING_READ(IER);
1783c496fa1fSAdam Jackson 
1784c496fa1fSAdam Jackson 	if (I915_HAS_HOTPLUG(dev)) {
17855ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
17865ca58282SJesse Barnes 
1787b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
1788b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1789b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1790b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1791b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1792b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1793b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
1794b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1795b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1796b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1797b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
17982d1c9752SAndy Lutomirski 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
1799b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
18002d1c9752SAndy Lutomirski 
18012d1c9752SAndy Lutomirski 			/* Programming the CRT detection parameters tends
18022d1c9752SAndy Lutomirski 			   to generate a spurious hotplug event about three
18032d1c9752SAndy Lutomirski 			   seconds later.  So just do it once.
18042d1c9752SAndy Lutomirski 			*/
18052d1c9752SAndy Lutomirski 			if (IS_G4X(dev))
18062d1c9752SAndy Lutomirski 				hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
18072d1c9752SAndy Lutomirski 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
18082d1c9752SAndy Lutomirski 		}
18092d1c9752SAndy Lutomirski 
1810b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
1811b01f2c3aSJesse Barnes 
18125ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
18135ca58282SJesse Barnes 	}
18145ca58282SJesse Barnes 
18153b617967SChris Wilson 	intel_opregion_enable_asle(dev);
18160a3e67a4SJesse Barnes 
18170a3e67a4SJesse Barnes 	return 0;
1818c0e09200SDave Airlie }
1819c0e09200SDave Airlie 
1820f2b115e6SAdam Jackson static void ironlake_irq_uninstall(struct drm_device *dev)
1821036a4a7dSZhenyu Wang {
1822036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1823036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1824036a4a7dSZhenyu Wang 
1825036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1826036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1827036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1828036a4a7dSZhenyu Wang 
1829036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1830036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1831036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1832036a4a7dSZhenyu Wang }
1833036a4a7dSZhenyu Wang 
1834c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1835c0e09200SDave Airlie {
1836c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1837c0e09200SDave Airlie 
1838c0e09200SDave Airlie 	if (!dev_priv)
1839c0e09200SDave Airlie 		return;
1840c0e09200SDave Airlie 
18410a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
18420a3e67a4SJesse Barnes 
1843bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1844f2b115e6SAdam Jackson 		ironlake_irq_uninstall(dev);
1845036a4a7dSZhenyu Wang 		return;
1846036a4a7dSZhenyu Wang 	}
1847036a4a7dSZhenyu Wang 
18485ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
18495ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
18505ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
18515ca58282SJesse Barnes 	}
18525ca58282SJesse Barnes 
18530a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
18547c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
18557c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
18560a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1857ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1858c0e09200SDave Airlie 
18597c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
18607c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
18617c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1862c0e09200SDave Airlie }
1863