xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 9db4a9c7b2a3bd5b4952846bc0c2f58daa80ddd7)
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 void
897c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
907c463586SKeith Packard {
917c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
92*9db4a9c7SJesse Barnes 		u32 reg = PIPESTAT(pipe);
937c463586SKeith Packard 
947c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
957c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
967c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
973143a2bfSChris Wilson 		POSTING_READ(reg);
987c463586SKeith Packard 	}
997c463586SKeith Packard }
1007c463586SKeith Packard 
1017c463586SKeith Packard void
1027c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1037c463586SKeith Packard {
1047c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
105*9db4a9c7SJesse Barnes 		u32 reg = PIPESTAT(pipe);
1067c463586SKeith Packard 
1077c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
1087c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
1093143a2bfSChris Wilson 		POSTING_READ(reg);
1107c463586SKeith Packard 	}
1117c463586SKeith Packard }
1127c463586SKeith Packard 
113c0e09200SDave Airlie /**
11401c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
11501c66889SZhao Yakui  */
11601c66889SZhao Yakui void intel_enable_asle(struct drm_device *dev)
11701c66889SZhao Yakui {
1181ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1191ec14ad3SChris Wilson 	unsigned long irqflags;
1201ec14ad3SChris Wilson 
1211ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
12201c66889SZhao Yakui 
123c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
124f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
125edcb49caSZhao Yakui 	else {
12601c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
127d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
128a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen >= 4)
129edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
130d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
131edcb49caSZhao Yakui 	}
1321ec14ad3SChris Wilson 
1331ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
13401c66889SZhao Yakui }
13501c66889SZhao Yakui 
13601c66889SZhao Yakui /**
1370a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1380a3e67a4SJesse Barnes  * @dev: DRM device
1390a3e67a4SJesse Barnes  * @pipe: pipe to check
1400a3e67a4SJesse Barnes  *
1410a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1420a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1430a3e67a4SJesse Barnes  * before reading such registers if unsure.
1440a3e67a4SJesse Barnes  */
1450a3e67a4SJesse Barnes static int
1460a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1470a3e67a4SJesse Barnes {
1480a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1495eddb70bSChris Wilson 	return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
1500a3e67a4SJesse Barnes }
1510a3e67a4SJesse Barnes 
15242f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
15342f52ef8SKeith Packard  * we use as a pipe index
15442f52ef8SKeith Packard  */
15542f52ef8SKeith Packard u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1560a3e67a4SJesse Barnes {
1570a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1580a3e67a4SJesse Barnes 	unsigned long high_frame;
1590a3e67a4SJesse Barnes 	unsigned long low_frame;
1605eddb70bSChris Wilson 	u32 high1, high2, low;
1610a3e67a4SJesse Barnes 
1620a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
16344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
164*9db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
1650a3e67a4SJesse Barnes 		return 0;
1660a3e67a4SJesse Barnes 	}
1670a3e67a4SJesse Barnes 
168*9db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
169*9db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
1705eddb70bSChris Wilson 
1710a3e67a4SJesse Barnes 	/*
1720a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
1730a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
1740a3e67a4SJesse Barnes 	 * register.
1750a3e67a4SJesse Barnes 	 */
1760a3e67a4SJesse Barnes 	do {
1775eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1785eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
1795eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1800a3e67a4SJesse Barnes 	} while (high1 != high2);
1810a3e67a4SJesse Barnes 
1825eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
1835eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
1845eddb70bSChris Wilson 	return (high1 << 8) | low;
1850a3e67a4SJesse Barnes }
1860a3e67a4SJesse Barnes 
1879880b7a5SJesse Barnes u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
1889880b7a5SJesse Barnes {
1899880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
190*9db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
1919880b7a5SJesse Barnes 
1929880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
19344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
194*9db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
1959880b7a5SJesse Barnes 		return 0;
1969880b7a5SJesse Barnes 	}
1979880b7a5SJesse Barnes 
1989880b7a5SJesse Barnes 	return I915_READ(reg);
1999880b7a5SJesse Barnes }
2009880b7a5SJesse Barnes 
2010af7e4dfSMario Kleiner int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
2020af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
2030af7e4dfSMario Kleiner {
2040af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2050af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
2060af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
2070af7e4dfSMario Kleiner 	bool in_vbl = true;
2080af7e4dfSMario Kleiner 	int ret = 0;
2090af7e4dfSMario Kleiner 
2100af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
2110af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
212*9db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
2130af7e4dfSMario Kleiner 		return 0;
2140af7e4dfSMario Kleiner 	}
2150af7e4dfSMario Kleiner 
2160af7e4dfSMario Kleiner 	/* Get vtotal. */
2170af7e4dfSMario Kleiner 	vtotal = 1 + ((I915_READ(VTOTAL(pipe)) >> 16) & 0x1fff);
2180af7e4dfSMario Kleiner 
2190af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
2200af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
2210af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
2220af7e4dfSMario Kleiner 		 */
2230af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
2240af7e4dfSMario Kleiner 
2250af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
2260af7e4dfSMario Kleiner 		 * horizontal scanout position.
2270af7e4dfSMario Kleiner 		 */
2280af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
2290af7e4dfSMario Kleiner 		*hpos = 0;
2300af7e4dfSMario Kleiner 	} else {
2310af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
2320af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
2330af7e4dfSMario Kleiner 		 * scanout position.
2340af7e4dfSMario Kleiner 		 */
2350af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
2360af7e4dfSMario Kleiner 
2370af7e4dfSMario Kleiner 		htotal = 1 + ((I915_READ(HTOTAL(pipe)) >> 16) & 0x1fff);
2380af7e4dfSMario Kleiner 		*vpos = position / htotal;
2390af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
2400af7e4dfSMario Kleiner 	}
2410af7e4dfSMario Kleiner 
2420af7e4dfSMario Kleiner 	/* Query vblank area. */
2430af7e4dfSMario Kleiner 	vbl = I915_READ(VBLANK(pipe));
2440af7e4dfSMario Kleiner 
2450af7e4dfSMario Kleiner 	/* Test position against vblank region. */
2460af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
2470af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
2480af7e4dfSMario Kleiner 
2490af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
2500af7e4dfSMario Kleiner 		in_vbl = false;
2510af7e4dfSMario Kleiner 
2520af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
2530af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
2540af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
2550af7e4dfSMario Kleiner 
2560af7e4dfSMario Kleiner 	/* Readouts valid? */
2570af7e4dfSMario Kleiner 	if (vbl > 0)
2580af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
2590af7e4dfSMario Kleiner 
2600af7e4dfSMario Kleiner 	/* In vblank? */
2610af7e4dfSMario Kleiner 	if (in_vbl)
2620af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
2630af7e4dfSMario Kleiner 
2640af7e4dfSMario Kleiner 	return ret;
2650af7e4dfSMario Kleiner }
2660af7e4dfSMario Kleiner 
2674041b853SChris Wilson int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
2680af7e4dfSMario Kleiner 			      int *max_error,
2690af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
2700af7e4dfSMario Kleiner 			      unsigned flags)
2710af7e4dfSMario Kleiner {
2724041b853SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2734041b853SChris Wilson 	struct drm_crtc *crtc;
2740af7e4dfSMario Kleiner 
2754041b853SChris Wilson 	if (pipe < 0 || pipe >= dev_priv->num_pipe) {
2764041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2770af7e4dfSMario Kleiner 		return -EINVAL;
2780af7e4dfSMario Kleiner 	}
2790af7e4dfSMario Kleiner 
2800af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
2814041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
2824041b853SChris Wilson 	if (crtc == NULL) {
2834041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2844041b853SChris Wilson 		return -EINVAL;
2854041b853SChris Wilson 	}
2864041b853SChris Wilson 
2874041b853SChris Wilson 	if (!crtc->enabled) {
2884041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
2894041b853SChris Wilson 		return -EBUSY;
2904041b853SChris Wilson 	}
2910af7e4dfSMario Kleiner 
2920af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
2934041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
2944041b853SChris Wilson 						     vblank_time, flags,
2954041b853SChris Wilson 						     crtc);
2960af7e4dfSMario Kleiner }
2970af7e4dfSMario Kleiner 
2985ca58282SJesse Barnes /*
2995ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
3005ca58282SJesse Barnes  */
3015ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
3025ca58282SJesse Barnes {
3035ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3045ca58282SJesse Barnes 						    hotplug_work);
3055ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
306c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
3074ef69c7aSChris Wilson 	struct intel_encoder *encoder;
3085ca58282SJesse Barnes 
3094ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
3104ef69c7aSChris Wilson 		if (encoder->hot_plug)
3114ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
312c31c4ba3SKeith Packard 
3135ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
314eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3155ca58282SJesse Barnes }
3165ca58282SJesse Barnes 
317f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
318f97108d1SJesse Barnes {
319f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
320b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
321f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
322f97108d1SJesse Barnes 
3237648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
324b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
325b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
326f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
327f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
328f97108d1SJesse Barnes 
329f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
330b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
331f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
332f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
333f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
334f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
335b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
336f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
337f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
338f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
339f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
340f97108d1SJesse Barnes 	}
341f97108d1SJesse Barnes 
3427648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
343f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
344f97108d1SJesse Barnes 
345f97108d1SJesse Barnes 	return;
346f97108d1SJesse Barnes }
347f97108d1SJesse Barnes 
348549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
349549f7365SChris Wilson 			struct intel_ring_buffer *ring)
350549f7365SChris Wilson {
351549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
352475553deSChris Wilson 	u32 seqno;
3539862e600SChris Wilson 
354475553deSChris Wilson 	if (ring->obj == NULL)
355475553deSChris Wilson 		return;
356475553deSChris Wilson 
357475553deSChris Wilson 	seqno = ring->get_seqno(ring);
358db53a302SChris Wilson 	trace_i915_gem_request_complete(ring, seqno);
3599862e600SChris Wilson 
3609862e600SChris Wilson 	ring->irq_seqno = seqno;
361549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3629862e600SChris Wilson 
363549f7365SChris Wilson 	dev_priv->hangcheck_count = 0;
364549f7365SChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
365549f7365SChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
366549f7365SChris Wilson }
367549f7365SChris Wilson 
3683b8d8d91SJesse Barnes static void gen6_pm_irq_handler(struct drm_device *dev)
3693b8d8d91SJesse Barnes {
3703b8d8d91SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3713b8d8d91SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
3723b8d8d91SJesse Barnes 	u32 pm_iir;
3733b8d8d91SJesse Barnes 
3743b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
3753b8d8d91SJesse Barnes 	if (!pm_iir)
3763b8d8d91SJesse Barnes 		return;
3773b8d8d91SJesse Barnes 
3783b8d8d91SJesse Barnes 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
3793b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
3803b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
3813b8d8d91SJesse Barnes 		if (new_delay > dev_priv->max_delay)
3823b8d8d91SJesse Barnes 			new_delay = dev_priv->max_delay;
3833b8d8d91SJesse Barnes 	} else if (pm_iir & (GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT)) {
3843b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
3853b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
3863b8d8d91SJesse Barnes 		if (new_delay < dev_priv->min_delay) {
3873b8d8d91SJesse Barnes 			new_delay = dev_priv->min_delay;
3883b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3893b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) |
3903b8d8d91SJesse Barnes 				   ((new_delay << 16) & 0x3f0000));
3913b8d8d91SJesse Barnes 		} else {
3923b8d8d91SJesse Barnes 			/* Make sure we continue to get down interrupts
3933b8d8d91SJesse Barnes 			 * until we hit the minimum frequency */
3943b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3953b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) & ~0x3f0000);
3963b8d8d91SJesse Barnes 		}
3973b8d8d91SJesse Barnes 
3983b8d8d91SJesse Barnes 	}
3993b8d8d91SJesse Barnes 
4003b8d8d91SJesse Barnes 	gen6_set_rps(dev, new_delay);
4013b8d8d91SJesse Barnes 	dev_priv->cur_delay = new_delay;
4023b8d8d91SJesse Barnes 
4033b8d8d91SJesse Barnes 	I915_WRITE(GEN6_PMIIR, pm_iir);
4043b8d8d91SJesse Barnes }
4053b8d8d91SJesse Barnes 
406776ad806SJesse Barnes static void pch_irq_handler(struct drm_device *dev)
407776ad806SJesse Barnes {
408776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
409776ad806SJesse Barnes 	u32 pch_iir;
410*9db4a9c7SJesse Barnes 	int pipe;
411776ad806SJesse Barnes 
412776ad806SJesse Barnes 	pch_iir = I915_READ(SDEIIR);
413776ad806SJesse Barnes 
414776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
415776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
416776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
417776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
418776ad806SJesse Barnes 
419776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
420776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
421776ad806SJesse Barnes 
422776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
423776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
424776ad806SJesse Barnes 
425776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
426776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
427776ad806SJesse Barnes 
428776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
429776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
430776ad806SJesse Barnes 
431*9db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
432*9db4a9c7SJesse Barnes 		for_each_pipe(pipe)
433*9db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
434*9db4a9c7SJesse Barnes 					 pipe_name(pipe),
435*9db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
436776ad806SJesse Barnes 
437776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
438776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
439776ad806SJesse Barnes 
440776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
441776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
442776ad806SJesse Barnes 
443776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
444776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
445776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
446776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
447776ad806SJesse Barnes }
448776ad806SJesse Barnes 
449995b6762SChris Wilson static irqreturn_t ironlake_irq_handler(struct drm_device *dev)
450036a4a7dSZhenyu Wang {
451036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
452036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
4533b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
4542d7b8366SYuanhan Liu 	u32 hotplug_mask;
455036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
456881f47b6SXiang, Haihao 	u32 bsd_usr_interrupt = GT_BSD_USER_INTERRUPT;
457881f47b6SXiang, Haihao 
458881f47b6SXiang, Haihao 	if (IS_GEN6(dev))
459881f47b6SXiang, Haihao 		bsd_usr_interrupt = GT_GEN6_BSD_USER_INTERRUPT;
460036a4a7dSZhenyu Wang 
4612d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
4622d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
4632d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
4643143a2bfSChris Wilson 	POSTING_READ(DEIER);
4652d109a84SZou, Nanhai 
466036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
467036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
468c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
4693b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
470036a4a7dSZhenyu Wang 
4713b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
4723b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
473c7c85101SZou Nan hai 		goto done;
474036a4a7dSZhenyu Wang 
4752d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
4762d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
4772d7b8366SYuanhan Liu 	else
4782d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
4792d7b8366SYuanhan Liu 
480036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
481036a4a7dSZhenyu Wang 
482036a4a7dSZhenyu Wang 	if (dev->primary->master) {
483036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
484036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
485036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
486036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
487036a4a7dSZhenyu Wang 	}
488036a4a7dSZhenyu Wang 
489c6df541cSChris Wilson 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
4901ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[RCS]);
491881f47b6SXiang, Haihao 	if (gt_iir & bsd_usr_interrupt)
4921ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[VCS]);
4931ec14ad3SChris Wilson 	if (gt_iir & GT_BLT_USER_INTERRUPT)
4941ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[BCS]);
495036a4a7dSZhenyu Wang 
49601c66889SZhao Yakui 	if (de_iir & DE_GSE)
4973b617967SChris Wilson 		intel_opregion_gse_intr(dev);
49801c66889SZhao Yakui 
499f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
500013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
5012bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
502013d5aa2SJesse Barnes 	}
503013d5aa2SJesse Barnes 
504f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
505f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
5062bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
507013d5aa2SJesse Barnes 	}
508c062df61SLi Peng 
509f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
510f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
511f072d2e7SZhenyu Wang 
512f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
513f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
514f072d2e7SZhenyu Wang 
515c650156aSZhenyu Wang 	/* check event from PCH */
516776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
517776ad806SJesse Barnes 		if (pch_iir & hotplug_mask)
518c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
519776ad806SJesse Barnes 		pch_irq_handler(dev);
520776ad806SJesse Barnes 	}
521c650156aSZhenyu Wang 
522f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
5237648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
524f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
525f97108d1SJesse Barnes 	}
526f97108d1SJesse Barnes 
5273b8d8d91SJesse Barnes 	if (IS_GEN6(dev))
5283b8d8d91SJesse Barnes 		gen6_pm_irq_handler(dev);
5293b8d8d91SJesse Barnes 
530c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
531c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
532c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
533c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
534036a4a7dSZhenyu Wang 
535c7c85101SZou Nan hai done:
5362d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
5373143a2bfSChris Wilson 	POSTING_READ(DEIER);
5382d109a84SZou, Nanhai 
539036a4a7dSZhenyu Wang 	return ret;
540036a4a7dSZhenyu Wang }
541036a4a7dSZhenyu Wang 
5428a905236SJesse Barnes /**
5438a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
5448a905236SJesse Barnes  * @work: work struct
5458a905236SJesse Barnes  *
5468a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
5478a905236SJesse Barnes  * was detected.
5488a905236SJesse Barnes  */
5498a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
5508a905236SJesse Barnes {
5518a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5528a905236SJesse Barnes 						    error_work);
5538a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
554f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
555f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
556f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
5578a905236SJesse Barnes 
558f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
5598a905236SJesse Barnes 
560ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
56144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
562f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
563f803aa55SChris Wilson 		if (!i915_reset(dev, GRDOM_RENDER)) {
564ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
565f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
566f316a42cSBen Gamari 		}
56730dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
568f316a42cSBen Gamari 	}
5698a905236SJesse Barnes }
5708a905236SJesse Barnes 
5713bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
5729df30794SChris Wilson static struct drm_i915_error_object *
573bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
57405394f39SChris Wilson 			 struct drm_i915_gem_object *src)
5759df30794SChris Wilson {
5769df30794SChris Wilson 	struct drm_i915_error_object *dst;
5779df30794SChris Wilson 	int page, page_count;
578e56660ddSChris Wilson 	u32 reloc_offset;
5799df30794SChris Wilson 
58005394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
5819df30794SChris Wilson 		return NULL;
5829df30794SChris Wilson 
58305394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
5849df30794SChris Wilson 
5859df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof (u32 *), GFP_ATOMIC);
5869df30794SChris Wilson 	if (dst == NULL)
5879df30794SChris Wilson 		return NULL;
5889df30794SChris Wilson 
58905394f39SChris Wilson 	reloc_offset = src->gtt_offset;
5909df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
591788885aeSAndrew Morton 		unsigned long flags;
592e56660ddSChris Wilson 		void __iomem *s;
593e56660ddSChris Wilson 		void *d;
594788885aeSAndrew Morton 
595e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
5969df30794SChris Wilson 		if (d == NULL)
5979df30794SChris Wilson 			goto unwind;
598e56660ddSChris Wilson 
599788885aeSAndrew Morton 		local_irq_save(flags);
600e56660ddSChris Wilson 		s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
6013e4d3af5SPeter Zijlstra 					     reloc_offset);
602e56660ddSChris Wilson 		memcpy_fromio(d, s, PAGE_SIZE);
6033e4d3af5SPeter Zijlstra 		io_mapping_unmap_atomic(s);
604788885aeSAndrew Morton 		local_irq_restore(flags);
605e56660ddSChris Wilson 
6069df30794SChris Wilson 		dst->pages[page] = d;
607e56660ddSChris Wilson 
608e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
6099df30794SChris Wilson 	}
6109df30794SChris Wilson 	dst->page_count = page_count;
61105394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
6129df30794SChris Wilson 
6139df30794SChris Wilson 	return dst;
6149df30794SChris Wilson 
6159df30794SChris Wilson unwind:
6169df30794SChris Wilson 	while (page--)
6179df30794SChris Wilson 		kfree(dst->pages[page]);
6189df30794SChris Wilson 	kfree(dst);
6199df30794SChris Wilson 	return NULL;
6209df30794SChris Wilson }
6219df30794SChris Wilson 
6229df30794SChris Wilson static void
6239df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
6249df30794SChris Wilson {
6259df30794SChris Wilson 	int page;
6269df30794SChris Wilson 
6279df30794SChris Wilson 	if (obj == NULL)
6289df30794SChris Wilson 		return;
6299df30794SChris Wilson 
6309df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
6319df30794SChris Wilson 		kfree(obj->pages[page]);
6329df30794SChris Wilson 
6339df30794SChris Wilson 	kfree(obj);
6349df30794SChris Wilson }
6359df30794SChris Wilson 
6369df30794SChris Wilson static void
6379df30794SChris Wilson i915_error_state_free(struct drm_device *dev,
6389df30794SChris Wilson 		      struct drm_i915_error_state *error)
6399df30794SChris Wilson {
640e2f973d5SChris Wilson 	int i;
641e2f973d5SChris Wilson 
642e2f973d5SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->batchbuffer); i++)
643e2f973d5SChris Wilson 		i915_error_object_free(error->batchbuffer[i]);
644e2f973d5SChris Wilson 
645e2f973d5SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ringbuffer); i++)
646e2f973d5SChris Wilson 		i915_error_object_free(error->ringbuffer[i]);
647e2f973d5SChris Wilson 
6489df30794SChris Wilson 	kfree(error->active_bo);
6496ef3d427SChris Wilson 	kfree(error->overlay);
6509df30794SChris Wilson 	kfree(error);
6519df30794SChris Wilson }
6529df30794SChris Wilson 
653c724e8a9SChris Wilson static u32 capture_bo_list(struct drm_i915_error_buffer *err,
654c724e8a9SChris Wilson 			   int count,
655c724e8a9SChris Wilson 			   struct list_head *head)
656c724e8a9SChris Wilson {
657c724e8a9SChris Wilson 	struct drm_i915_gem_object *obj;
658c724e8a9SChris Wilson 	int i = 0;
659c724e8a9SChris Wilson 
660c724e8a9SChris Wilson 	list_for_each_entry(obj, head, mm_list) {
661c724e8a9SChris Wilson 		err->size = obj->base.size;
662c724e8a9SChris Wilson 		err->name = obj->base.name;
663c724e8a9SChris Wilson 		err->seqno = obj->last_rendering_seqno;
664c724e8a9SChris Wilson 		err->gtt_offset = obj->gtt_offset;
665c724e8a9SChris Wilson 		err->read_domains = obj->base.read_domains;
666c724e8a9SChris Wilson 		err->write_domain = obj->base.write_domain;
667c724e8a9SChris Wilson 		err->fence_reg = obj->fence_reg;
668c724e8a9SChris Wilson 		err->pinned = 0;
669c724e8a9SChris Wilson 		if (obj->pin_count > 0)
670c724e8a9SChris Wilson 			err->pinned = 1;
671c724e8a9SChris Wilson 		if (obj->user_pin_count > 0)
672c724e8a9SChris Wilson 			err->pinned = -1;
673c724e8a9SChris Wilson 		err->tiling = obj->tiling_mode;
674c724e8a9SChris Wilson 		err->dirty = obj->dirty;
675c724e8a9SChris Wilson 		err->purgeable = obj->madv != I915_MADV_WILLNEED;
6763685092bSChris Wilson 		err->ring = obj->ring ? obj->ring->id : 0;
677a779e5abSChris Wilson 		err->agp_type = obj->agp_type == AGP_USER_CACHED_MEMORY;
678c724e8a9SChris Wilson 
679c724e8a9SChris Wilson 		if (++i == count)
680c724e8a9SChris Wilson 			break;
681c724e8a9SChris Wilson 
682c724e8a9SChris Wilson 		err++;
683c724e8a9SChris Wilson 	}
684c724e8a9SChris Wilson 
685c724e8a9SChris Wilson 	return i;
686c724e8a9SChris Wilson }
687c724e8a9SChris Wilson 
688748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
689748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
690748ebc60SChris Wilson {
691748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
692748ebc60SChris Wilson 	int i;
693748ebc60SChris Wilson 
694748ebc60SChris Wilson 	/* Fences */
695748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
696748ebc60SChris Wilson 	case 6:
697748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
698748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
699748ebc60SChris Wilson 		break;
700748ebc60SChris Wilson 	case 5:
701748ebc60SChris Wilson 	case 4:
702748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
703748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
704748ebc60SChris Wilson 		break;
705748ebc60SChris Wilson 	case 3:
706748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
707748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
708748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
709748ebc60SChris Wilson 	case 2:
710748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
711748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
712748ebc60SChris Wilson 		break;
713748ebc60SChris Wilson 
714748ebc60SChris Wilson 	}
715748ebc60SChris Wilson }
716748ebc60SChris Wilson 
717bcfb2e28SChris Wilson static struct drm_i915_error_object *
718bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
719bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
720bcfb2e28SChris Wilson {
721bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
722bcfb2e28SChris Wilson 	u32 seqno;
723bcfb2e28SChris Wilson 
724bcfb2e28SChris Wilson 	if (!ring->get_seqno)
725bcfb2e28SChris Wilson 		return NULL;
726bcfb2e28SChris Wilson 
727bcfb2e28SChris Wilson 	seqno = ring->get_seqno(ring);
728bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
729bcfb2e28SChris Wilson 		if (obj->ring != ring)
730bcfb2e28SChris Wilson 			continue;
731bcfb2e28SChris Wilson 
732c37d9a5dSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
733bcfb2e28SChris Wilson 			continue;
734bcfb2e28SChris Wilson 
735bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
736bcfb2e28SChris Wilson 			continue;
737bcfb2e28SChris Wilson 
738bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
739bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
740bcfb2e28SChris Wilson 		 */
741bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
742bcfb2e28SChris Wilson 	}
743bcfb2e28SChris Wilson 
744bcfb2e28SChris Wilson 	return NULL;
745bcfb2e28SChris Wilson }
746bcfb2e28SChris Wilson 
7478a905236SJesse Barnes /**
7488a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
7498a905236SJesse Barnes  * @dev: drm device
7508a905236SJesse Barnes  *
7518a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
7528a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
7538a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
7548a905236SJesse Barnes  * to pick up.
7558a905236SJesse Barnes  */
75663eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
75763eeaf38SJesse Barnes {
75863eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
75905394f39SChris Wilson 	struct drm_i915_gem_object *obj;
76063eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
76163eeaf38SJesse Barnes 	unsigned long flags;
762*9db4a9c7SJesse Barnes 	int i, pipe;
76363eeaf38SJesse Barnes 
76463eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
7659df30794SChris Wilson 	error = dev_priv->first_error;
7669df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
7679df30794SChris Wilson 	if (error)
7689df30794SChris Wilson 		return;
76963eeaf38SJesse Barnes 
770*9db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
77163eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
77263eeaf38SJesse Barnes 	if (!error) {
7739df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
7749df30794SChris Wilson 		return;
77563eeaf38SJesse Barnes 	}
77663eeaf38SJesse Barnes 
777b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
778b6f7833bSChris Wilson 		 dev->primary->index);
7792fa772f3SChris Wilson 
7801ec14ad3SChris Wilson 	error->seqno = dev_priv->ring[RCS].get_seqno(&dev_priv->ring[RCS]);
78163eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
78263eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
783*9db4a9c7SJesse Barnes 	for_each_pipe(pipe)
784*9db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
78563eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
786f406839fSChris Wilson 	error->error = 0;
787f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 6) {
788f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
789add354ddSChris Wilson 
7901d8f38f4SChris Wilson 		error->bcs_acthd = I915_READ(BCS_ACTHD);
7911d8f38f4SChris Wilson 		error->bcs_ipehr = I915_READ(BCS_IPEHR);
7921d8f38f4SChris Wilson 		error->bcs_ipeir = I915_READ(BCS_IPEIR);
7931d8f38f4SChris Wilson 		error->bcs_instdone = I915_READ(BCS_INSTDONE);
7941d8f38f4SChris Wilson 		error->bcs_seqno = 0;
7951ec14ad3SChris Wilson 		if (dev_priv->ring[BCS].get_seqno)
7961ec14ad3SChris Wilson 			error->bcs_seqno = dev_priv->ring[BCS].get_seqno(&dev_priv->ring[BCS]);
797add354ddSChris Wilson 
798add354ddSChris Wilson 		error->vcs_acthd = I915_READ(VCS_ACTHD);
799add354ddSChris Wilson 		error->vcs_ipehr = I915_READ(VCS_IPEHR);
800add354ddSChris Wilson 		error->vcs_ipeir = I915_READ(VCS_IPEIR);
801add354ddSChris Wilson 		error->vcs_instdone = I915_READ(VCS_INSTDONE);
802add354ddSChris Wilson 		error->vcs_seqno = 0;
8031ec14ad3SChris Wilson 		if (dev_priv->ring[VCS].get_seqno)
8041ec14ad3SChris Wilson 			error->vcs_seqno = dev_priv->ring[VCS].get_seqno(&dev_priv->ring[VCS]);
805f406839fSChris Wilson 	}
806f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
80763eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
80863eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
80963eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
81063eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
81163eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
81263eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
8139df30794SChris Wilson 		error->bbaddr = I915_READ64(BB_ADDR);
814f406839fSChris Wilson 	} else {
815f406839fSChris Wilson 		error->ipeir = I915_READ(IPEIR);
816f406839fSChris Wilson 		error->ipehr = I915_READ(IPEHR);
817f406839fSChris Wilson 		error->instdone = I915_READ(INSTDONE);
818f406839fSChris Wilson 		error->acthd = I915_READ(ACTHD);
819f406839fSChris Wilson 		error->bbaddr = 0;
8209df30794SChris Wilson 	}
821748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
8229df30794SChris Wilson 
823e2f973d5SChris Wilson 	/* Record the active batch and ring buffers */
824e2f973d5SChris Wilson 	for (i = 0; i < I915_NUM_RINGS; i++) {
825bcfb2e28SChris Wilson 		error->batchbuffer[i] =
826bcfb2e28SChris Wilson 			i915_error_first_batchbuffer(dev_priv,
827bcfb2e28SChris Wilson 						     &dev_priv->ring[i]);
8289df30794SChris Wilson 
829e2f973d5SChris Wilson 		error->ringbuffer[i] =
830e2f973d5SChris Wilson 			i915_error_object_create(dev_priv,
831e2f973d5SChris Wilson 						 dev_priv->ring[i].obj);
832e2f973d5SChris Wilson 	}
8339df30794SChris Wilson 
834c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
8359df30794SChris Wilson 	error->active_bo = NULL;
836c724e8a9SChris Wilson 	error->pinned_bo = NULL;
8379df30794SChris Wilson 
838bcfb2e28SChris Wilson 	i = 0;
839bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
840bcfb2e28SChris Wilson 		i++;
841bcfb2e28SChris Wilson 	error->active_bo_count = i;
84205394f39SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
843bcfb2e28SChris Wilson 		i++;
844bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
845c724e8a9SChris Wilson 
8468e934dbfSChris Wilson 	error->active_bo = NULL;
8478e934dbfSChris Wilson 	error->pinned_bo = NULL;
848bcfb2e28SChris Wilson 	if (i) {
849bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
8509df30794SChris Wilson 					   GFP_ATOMIC);
851c724e8a9SChris Wilson 		if (error->active_bo)
852c724e8a9SChris Wilson 			error->pinned_bo =
853c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
8549df30794SChris Wilson 	}
855c724e8a9SChris Wilson 
856c724e8a9SChris Wilson 	if (error->active_bo)
857c724e8a9SChris Wilson 		error->active_bo_count =
858c724e8a9SChris Wilson 			capture_bo_list(error->active_bo,
859c724e8a9SChris Wilson 					error->active_bo_count,
860c724e8a9SChris Wilson 					&dev_priv->mm.active_list);
861c724e8a9SChris Wilson 
862c724e8a9SChris Wilson 	if (error->pinned_bo)
863c724e8a9SChris Wilson 		error->pinned_bo_count =
864c724e8a9SChris Wilson 			capture_bo_list(error->pinned_bo,
865c724e8a9SChris Wilson 					error->pinned_bo_count,
866c724e8a9SChris Wilson 					&dev_priv->mm.pinned_list);
86763eeaf38SJesse Barnes 
8688a905236SJesse Barnes 	do_gettimeofday(&error->time);
8698a905236SJesse Barnes 
8706ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
871c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
8726ef3d427SChris Wilson 
8739df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
8749df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
87563eeaf38SJesse Barnes 		dev_priv->first_error = error;
8769df30794SChris Wilson 		error = NULL;
8779df30794SChris Wilson 	}
87863eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
8799df30794SChris Wilson 
8809df30794SChris Wilson 	if (error)
8819df30794SChris Wilson 		i915_error_state_free(dev, error);
8829df30794SChris Wilson }
8839df30794SChris Wilson 
8849df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
8859df30794SChris Wilson {
8869df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
8879df30794SChris Wilson 	struct drm_i915_error_state *error;
8889df30794SChris Wilson 
8899df30794SChris Wilson 	spin_lock(&dev_priv->error_lock);
8909df30794SChris Wilson 	error = dev_priv->first_error;
8919df30794SChris Wilson 	dev_priv->first_error = NULL;
8929df30794SChris Wilson 	spin_unlock(&dev_priv->error_lock);
8939df30794SChris Wilson 
8949df30794SChris Wilson 	if (error)
8959df30794SChris Wilson 		i915_error_state_free(dev, error);
89663eeaf38SJesse Barnes }
8973bd3c932SChris Wilson #else
8983bd3c932SChris Wilson #define i915_capture_error_state(x)
8993bd3c932SChris Wilson #endif
90063eeaf38SJesse Barnes 
90135aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
902c0e09200SDave Airlie {
9038a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
90463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
905*9db4a9c7SJesse Barnes 	int pipe;
90663eeaf38SJesse Barnes 
90735aed2e6SChris Wilson 	if (!eir)
90835aed2e6SChris Wilson 		return;
90963eeaf38SJesse Barnes 
91063eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
91163eeaf38SJesse Barnes 	       eir);
9128a905236SJesse Barnes 
9138a905236SJesse Barnes 	if (IS_G4X(dev)) {
9148a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
9158a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
9168a905236SJesse Barnes 
9178a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
9188a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
9198a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
9208a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
9218a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
9228a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
9238a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
9248a905236SJesse Barnes 			       I915_READ(INSTPS));
9258a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
9268a905236SJesse Barnes 			       I915_READ(INSTDONE1));
9278a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
9288a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
9298a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
9303143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
9318a905236SJesse Barnes 		}
9328a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
9338a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
9348a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
9358a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
9368a905236SJesse Barnes 			       pgtbl_err);
9378a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
9383143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
9398a905236SJesse Barnes 		}
9408a905236SJesse Barnes 	}
9418a905236SJesse Barnes 
942a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
94363eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
94463eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
94563eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
94663eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
94763eeaf38SJesse Barnes 			       pgtbl_err);
94863eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
9493143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
95063eeaf38SJesse Barnes 		}
9518a905236SJesse Barnes 	}
9528a905236SJesse Barnes 
95363eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
954*9db4a9c7SJesse Barnes 		printk(KERN_ERR "memory refresh error:\n");
955*9db4a9c7SJesse Barnes 		for_each_pipe(pipe)
956*9db4a9c7SJesse Barnes 			printk(KERN_ERR "pipe %c stat: 0x%08x\n",
957*9db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
95863eeaf38SJesse Barnes 		/* pipestat has already been acked */
95963eeaf38SJesse Barnes 	}
96063eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
96163eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
96263eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
96363eeaf38SJesse Barnes 		       I915_READ(INSTPM));
964a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
96563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
96663eeaf38SJesse Barnes 
96763eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
96863eeaf38SJesse Barnes 			       I915_READ(IPEIR));
96963eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
97063eeaf38SJesse Barnes 			       I915_READ(IPEHR));
97163eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
97263eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
97363eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
97463eeaf38SJesse Barnes 			       I915_READ(ACTHD));
97563eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
9763143a2bfSChris Wilson 			POSTING_READ(IPEIR);
97763eeaf38SJesse Barnes 		} else {
97863eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
97963eeaf38SJesse Barnes 
98063eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
98163eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
98263eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
98363eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
98463eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
98563eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
98663eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
98763eeaf38SJesse Barnes 			       I915_READ(INSTPS));
98863eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
98963eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
99063eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
99163eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
99263eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
9933143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
99463eeaf38SJesse Barnes 		}
99563eeaf38SJesse Barnes 	}
99663eeaf38SJesse Barnes 
99763eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
9983143a2bfSChris Wilson 	POSTING_READ(EIR);
99963eeaf38SJesse Barnes 	eir = I915_READ(EIR);
100063eeaf38SJesse Barnes 	if (eir) {
100163eeaf38SJesse Barnes 		/*
100263eeaf38SJesse Barnes 		 * some errors might have become stuck,
100363eeaf38SJesse Barnes 		 * mask them.
100463eeaf38SJesse Barnes 		 */
100563eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
100663eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
100763eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
100863eeaf38SJesse Barnes 	}
100935aed2e6SChris Wilson }
101035aed2e6SChris Wilson 
101135aed2e6SChris Wilson /**
101235aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
101335aed2e6SChris Wilson  * @dev: drm device
101435aed2e6SChris Wilson  *
101535aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
101635aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
101735aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
101835aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
101935aed2e6SChris Wilson  * of a ring dump etc.).
102035aed2e6SChris Wilson  */
1021527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
102235aed2e6SChris Wilson {
102335aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
102435aed2e6SChris Wilson 
102535aed2e6SChris Wilson 	i915_capture_error_state(dev);
102635aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
10278a905236SJesse Barnes 
1028ba1234d1SBen Gamari 	if (wedged) {
102930dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1030ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1031ba1234d1SBen Gamari 
103211ed50ecSBen Gamari 		/*
103311ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
103411ed50ecSBen Gamari 		 */
10351ec14ad3SChris Wilson 		wake_up_all(&dev_priv->ring[RCS].irq_queue);
1036f787a5f5SChris Wilson 		if (HAS_BSD(dev))
10371ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[VCS].irq_queue);
1038549f7365SChris Wilson 		if (HAS_BLT(dev))
10391ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[BCS].irq_queue);
104011ed50ecSBen Gamari 	}
104111ed50ecSBen Gamari 
10429c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
10438a905236SJesse Barnes }
10448a905236SJesse Barnes 
10454e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
10464e5359cdSSimon Farnsworth {
10474e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
10484e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10494e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
105005394f39SChris Wilson 	struct drm_i915_gem_object *obj;
10514e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
10524e5359cdSSimon Farnsworth 	unsigned long flags;
10534e5359cdSSimon Farnsworth 	bool stall_detected;
10544e5359cdSSimon Farnsworth 
10554e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
10564e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
10574e5359cdSSimon Farnsworth 		return;
10584e5359cdSSimon Farnsworth 
10594e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
10604e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
10614e5359cdSSimon Farnsworth 
10624e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
10634e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
10644e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
10654e5359cdSSimon Farnsworth 		return;
10664e5359cdSSimon Farnsworth 	}
10674e5359cdSSimon Farnsworth 
10684e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
106905394f39SChris Wilson 	obj = work->pending_flip_obj;
1070a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
1071*9db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
107205394f39SChris Wilson 		stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
10734e5359cdSSimon Farnsworth 	} else {
1074*9db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
107505394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
10764e5359cdSSimon Farnsworth 							crtc->y * crtc->fb->pitch +
10774e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
10784e5359cdSSimon Farnsworth 	}
10794e5359cdSSimon Farnsworth 
10804e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
10814e5359cdSSimon Farnsworth 
10824e5359cdSSimon Farnsworth 	if (stall_detected) {
10834e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
10844e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
10854e5359cdSSimon Farnsworth 	}
10864e5359cdSSimon Farnsworth }
10874e5359cdSSimon Farnsworth 
10888a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
10898a905236SJesse Barnes {
10908a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
10918a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10928a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
10938a905236SJesse Barnes 	u32 iir, new_iir;
1094*9db4a9c7SJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
10958a905236SJesse Barnes 	u32 vblank_status;
10968a905236SJesse Barnes 	int vblank = 0;
10978a905236SJesse Barnes 	unsigned long irqflags;
10988a905236SJesse Barnes 	int irq_received;
1099*9db4a9c7SJesse Barnes 	int ret = IRQ_NONE, pipe;
1100*9db4a9c7SJesse Barnes 	bool blc_event = false;
11018a905236SJesse Barnes 
11028a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
11038a905236SJesse Barnes 
1104bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1105f2b115e6SAdam Jackson 		return ironlake_irq_handler(dev);
11068a905236SJesse Barnes 
11078a905236SJesse Barnes 	iir = I915_READ(IIR);
11088a905236SJesse Barnes 
1109a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
1110d874bcffSJesse Barnes 		vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
1111e25e6601SJesse Barnes 	else
1112d874bcffSJesse Barnes 		vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
11138a905236SJesse Barnes 
11148a905236SJesse Barnes 	for (;;) {
11158a905236SJesse Barnes 		irq_received = iir != 0;
11168a905236SJesse Barnes 
11178a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
11188a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
11198a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
11208a905236SJesse Barnes 		 * interrupts (for non-MSI).
11218a905236SJesse Barnes 		 */
11221ec14ad3SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
11238a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
1124ba1234d1SBen Gamari 			i915_handle_error(dev, false);
11258a905236SJesse Barnes 
1126*9db4a9c7SJesse Barnes 		for_each_pipe(pipe) {
1127*9db4a9c7SJesse Barnes 			int reg = PIPESTAT(pipe);
1128*9db4a9c7SJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
1129*9db4a9c7SJesse Barnes 
11308a905236SJesse Barnes 			/*
1131*9db4a9c7SJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
11328a905236SJesse Barnes 			 */
1133*9db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
1134*9db4a9c7SJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1135*9db4a9c7SJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
1136*9db4a9c7SJesse Barnes 							 pipe_name(pipe));
1137*9db4a9c7SJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
11388a905236SJesse Barnes 				irq_received = 1;
11398a905236SJesse Barnes 			}
11408a905236SJesse Barnes 		}
11411ec14ad3SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
11428a905236SJesse Barnes 
11438a905236SJesse Barnes 		if (!irq_received)
11448a905236SJesse Barnes 			break;
11458a905236SJesse Barnes 
11468a905236SJesse Barnes 		ret = IRQ_HANDLED;
11478a905236SJesse Barnes 
11488a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
11498a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
11508a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
11518a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
11528a905236SJesse Barnes 
115344d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
11548a905236SJesse Barnes 				  hotplug_status);
11558a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
11569c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
11579c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
11588a905236SJesse Barnes 
11598a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
11608a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
116163eeaf38SJesse Barnes 		}
116263eeaf38SJesse Barnes 
1163673a394bSEric Anholt 		I915_WRITE(IIR, iir);
1164cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
11657c463586SKeith Packard 
11667c1c2871SDave Airlie 		if (dev->primary->master) {
11677c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
11687c1c2871SDave Airlie 			if (master_priv->sarea_priv)
11697c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
1170c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
11717c1c2871SDave Airlie 		}
11720a3e67a4SJesse Barnes 
1173549f7365SChris Wilson 		if (iir & I915_USER_INTERRUPT)
11741ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
11751ec14ad3SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
11761ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
1177d1b851fcSZou Nan hai 
11781afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
11796b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
11801afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
11811afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 0);
11821afe3e9dSJesse Barnes 		}
11836b95a207SKristian Høgsberg 
11841afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
118570565d00SJesse Barnes 			intel_prepare_page_flip(dev, 1);
11861afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
11871afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 1);
11881afe3e9dSJesse Barnes 		}
11896b95a207SKristian Høgsberg 
1190*9db4a9c7SJesse Barnes 		for_each_pipe(pipe) {
1191*9db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & vblank_status &&
1192*9db4a9c7SJesse Barnes 			    drm_handle_vblank(dev, pipe)) {
11937c463586SKeith Packard 				vblank++;
11944e5359cdSSimon Farnsworth 				if (!dev_priv->flip_pending_is_done) {
1195*9db4a9c7SJesse Barnes 					i915_pageflip_stall_check(dev, pipe);
1196*9db4a9c7SJesse Barnes 					intel_finish_page_flip(dev, pipe);
11977c463586SKeith Packard 				}
11984e5359cdSSimon Farnsworth 			}
11997c463586SKeith Packard 
1200*9db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1201*9db4a9c7SJesse Barnes 				blc_event = true;
12024e5359cdSSimon Farnsworth 		}
12037c463586SKeith Packard 
1204*9db4a9c7SJesse Barnes 
1205*9db4a9c7SJesse Barnes 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
12063b617967SChris Wilson 			intel_opregion_asle_intr(dev);
12070a3e67a4SJesse Barnes 
1208cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
1209cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
1210cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
1211cdfbc41fSEric Anholt 		 * we would never get another interrupt.
1212cdfbc41fSEric Anholt 		 *
1213cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
1214cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
1215cdfbc41fSEric Anholt 		 * another one.
1216cdfbc41fSEric Anholt 		 *
1217cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
1218cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
1219cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
1220cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
1221cdfbc41fSEric Anholt 		 * stray interrupts.
1222cdfbc41fSEric Anholt 		 */
1223cdfbc41fSEric Anholt 		iir = new_iir;
122405eff845SKeith Packard 	}
1225cdfbc41fSEric Anholt 
122605eff845SKeith Packard 	return ret;
1227c0e09200SDave Airlie }
1228c0e09200SDave Airlie 
1229c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
1230c0e09200SDave Airlie {
1231c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
12327c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1233c0e09200SDave Airlie 
1234c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
1235c0e09200SDave Airlie 
123644d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
1237c0e09200SDave Airlie 
1238c99b058fSKristian Høgsberg 	dev_priv->counter++;
1239c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
1240c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
12417c1c2871SDave Airlie 	if (master_priv->sarea_priv)
12427c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
1243c0e09200SDave Airlie 
1244e1f99ce6SChris Wilson 	if (BEGIN_LP_RING(4) == 0) {
1245585fb111SJesse Barnes 		OUT_RING(MI_STORE_DWORD_INDEX);
12460baf823aSKeith Packard 		OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1247c0e09200SDave Airlie 		OUT_RING(dev_priv->counter);
1248585fb111SJesse Barnes 		OUT_RING(MI_USER_INTERRUPT);
1249c0e09200SDave Airlie 		ADVANCE_LP_RING();
1250e1f99ce6SChris Wilson 	}
1251c0e09200SDave Airlie 
1252c0e09200SDave Airlie 	return dev_priv->counter;
1253c0e09200SDave Airlie }
1254c0e09200SDave Airlie 
1255c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
1256c0e09200SDave Airlie {
1257c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
12587c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1259c0e09200SDave Airlie 	int ret = 0;
12601ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
1261c0e09200SDave Airlie 
126244d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
1263c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
1264c0e09200SDave Airlie 
1265ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
12667c1c2871SDave Airlie 		if (master_priv->sarea_priv)
12677c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
1268c0e09200SDave Airlie 		return 0;
1269ed4cb414SEric Anholt 	}
1270c0e09200SDave Airlie 
12717c1c2871SDave Airlie 	if (master_priv->sarea_priv)
12727c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1273c0e09200SDave Airlie 
1274b13c2b96SChris Wilson 	if (ring->irq_get(ring)) {
12751ec14ad3SChris Wilson 		DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1276c0e09200SDave Airlie 			    READ_BREADCRUMB(dev_priv) >= irq_nr);
12771ec14ad3SChris Wilson 		ring->irq_put(ring);
12785a9a8d1aSChris Wilson 	} else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
12795a9a8d1aSChris Wilson 		ret = -EBUSY;
1280c0e09200SDave Airlie 
1281c0e09200SDave Airlie 	if (ret == -EBUSY) {
1282c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
1283c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1284c0e09200SDave Airlie 	}
1285c0e09200SDave Airlie 
1286c0e09200SDave Airlie 	return ret;
1287c0e09200SDave Airlie }
1288c0e09200SDave Airlie 
1289c0e09200SDave Airlie /* Needs the lock as it touches the ring.
1290c0e09200SDave Airlie  */
1291c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
1292c0e09200SDave Airlie 			 struct drm_file *file_priv)
1293c0e09200SDave Airlie {
1294c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1295c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
1296c0e09200SDave Airlie 	int result;
1297c0e09200SDave Airlie 
12981ec14ad3SChris Wilson 	if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
1299c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1300c0e09200SDave Airlie 		return -EINVAL;
1301c0e09200SDave Airlie 	}
1302299eb93cSEric Anholt 
1303299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1304299eb93cSEric Anholt 
1305546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
1306c0e09200SDave Airlie 	result = i915_emit_irq(dev);
1307546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
1308c0e09200SDave Airlie 
1309c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
1310c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
1311c0e09200SDave Airlie 		return -EFAULT;
1312c0e09200SDave Airlie 	}
1313c0e09200SDave Airlie 
1314c0e09200SDave Airlie 	return 0;
1315c0e09200SDave Airlie }
1316c0e09200SDave Airlie 
1317c0e09200SDave Airlie /* Doesn't need the hardware lock.
1318c0e09200SDave Airlie  */
1319c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
1320c0e09200SDave Airlie 			 struct drm_file *file_priv)
1321c0e09200SDave Airlie {
1322c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1323c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
1324c0e09200SDave Airlie 
1325c0e09200SDave Airlie 	if (!dev_priv) {
1326c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1327c0e09200SDave Airlie 		return -EINVAL;
1328c0e09200SDave Airlie 	}
1329c0e09200SDave Airlie 
1330c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
1331c0e09200SDave Airlie }
1332c0e09200SDave Airlie 
1333b0b544cdSChris Wilson static void i915_vblank_work_func(struct work_struct *work)
1334b0b544cdSChris Wilson {
1335b0b544cdSChris Wilson 	drm_i915_private_t *dev_priv =
1336b0b544cdSChris Wilson 		container_of(work, drm_i915_private_t, vblank_work);
1337b0b544cdSChris Wilson 
1338b0b544cdSChris Wilson 	if (atomic_read(&dev_priv->vblank_enabled)) {
1339b0b544cdSChris Wilson 		if (!dev_priv->vblank_pm_qos.pm_qos_class)
1340b0b544cdSChris Wilson 			pm_qos_add_request(&dev_priv->vblank_pm_qos,
1341b0b544cdSChris Wilson 					   PM_QOS_CPU_DMA_LATENCY,
1342b0b544cdSChris Wilson 					   15); //>=20 won't work
1343b0b544cdSChris Wilson 	} else {
1344b0b544cdSChris Wilson 		if (dev_priv->vblank_pm_qos.pm_qos_class)
1345b0b544cdSChris Wilson 			pm_qos_remove_request(&dev_priv->vblank_pm_qos);
1346b0b544cdSChris Wilson 	}
1347b0b544cdSChris Wilson }
1348b0b544cdSChris Wilson 
134942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
135042f52ef8SKeith Packard  * we use as a pipe index
135142f52ef8SKeith Packard  */
135242f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
13530a3e67a4SJesse Barnes {
13540a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1355e9d21d7fSKeith Packard 	unsigned long irqflags;
135671e0ffa5SJesse Barnes 
13575eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
135871e0ffa5SJesse Barnes 		return -EINVAL;
13590a3e67a4SJesse Barnes 
13601ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1361bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1362c062df61SLi Peng 		ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1363c062df61SLi Peng 					    DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1364a6c45cf0SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 4)
13657c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
13667c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
13670a3e67a4SJesse Barnes 	else
13687c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
13697c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
13701ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1371b0b544cdSChris Wilson 
1372b0b544cdSChris Wilson 	/* gen3 platforms have an issue with vsync interrupts not reaching
1373b0b544cdSChris Wilson 	 * cpu during deep c-state sleep (>C1), so we need to install a
1374b0b544cdSChris Wilson 	 * PM QoS handle to prevent C-state starvation of the GPU.
1375b0b544cdSChris Wilson 	 */
1376b0b544cdSChris Wilson 	if (dev_priv->info->gen == 3 && !dev_priv->info->is_g33) {
1377b0b544cdSChris Wilson 		atomic_inc(&dev_priv->vblank_enabled);
1378b0b544cdSChris Wilson 		queue_work(dev_priv->wq, &dev_priv->vblank_work);
1379b0b544cdSChris Wilson 	}
1380b0b544cdSChris Wilson 
13810a3e67a4SJesse Barnes 	return 0;
13820a3e67a4SJesse Barnes }
13830a3e67a4SJesse Barnes 
138442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
138542f52ef8SKeith Packard  * we use as a pipe index
138642f52ef8SKeith Packard  */
138742f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
13880a3e67a4SJesse Barnes {
13890a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1390e9d21d7fSKeith Packard 	unsigned long irqflags;
13910a3e67a4SJesse Barnes 
1392b0b544cdSChris Wilson 	if (dev_priv->info->gen == 3 && !dev_priv->info->is_g33) {
1393b0b544cdSChris Wilson 		atomic_dec(&dev_priv->vblank_enabled);
1394b0b544cdSChris Wilson 		queue_work(dev_priv->wq, &dev_priv->vblank_work);
1395b0b544cdSChris Wilson 	}
1396b0b544cdSChris Wilson 
13971ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1398bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1399c062df61SLi Peng 		ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1400c062df61SLi Peng 					     DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1401c062df61SLi Peng 	else
14027c463586SKeith Packard 		i915_disable_pipestat(dev_priv, pipe,
14037c463586SKeith Packard 				      PIPE_VBLANK_INTERRUPT_ENABLE |
14047c463586SKeith Packard 				      PIPE_START_VBLANK_INTERRUPT_ENABLE);
14051ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14060a3e67a4SJesse Barnes }
14070a3e67a4SJesse Barnes 
1408c0e09200SDave Airlie /* Set the vblank monitor pipe
1409c0e09200SDave Airlie  */
1410c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1411c0e09200SDave Airlie 			 struct drm_file *file_priv)
1412c0e09200SDave Airlie {
1413c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1414c0e09200SDave Airlie 
1415c0e09200SDave Airlie 	if (!dev_priv) {
1416c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1417c0e09200SDave Airlie 		return -EINVAL;
1418c0e09200SDave Airlie 	}
1419c0e09200SDave Airlie 
1420c0e09200SDave Airlie 	return 0;
1421c0e09200SDave Airlie }
1422c0e09200SDave Airlie 
1423c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1424c0e09200SDave Airlie 			 struct drm_file *file_priv)
1425c0e09200SDave Airlie {
1426c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1427c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
1428c0e09200SDave Airlie 
1429c0e09200SDave Airlie 	if (!dev_priv) {
1430c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1431c0e09200SDave Airlie 		return -EINVAL;
1432c0e09200SDave Airlie 	}
1433c0e09200SDave Airlie 
14340a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1435c0e09200SDave Airlie 
1436c0e09200SDave Airlie 	return 0;
1437c0e09200SDave Airlie }
1438c0e09200SDave Airlie 
1439c0e09200SDave Airlie /**
1440c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
1441c0e09200SDave Airlie  */
1442c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
1443c0e09200SDave Airlie 		     struct drm_file *file_priv)
1444c0e09200SDave Airlie {
1445bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1446bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1447bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1448bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1449bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1450bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1451bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1452bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1453bd95e0a4SEric Anholt 	 *
1454bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1455bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1456bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1457bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
14580a3e67a4SJesse Barnes 	 */
1459c0e09200SDave Airlie 	return -EINVAL;
1460c0e09200SDave Airlie }
1461c0e09200SDave Airlie 
1462893eead0SChris Wilson static u32
1463893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1464852835f3SZou Nan hai {
1465893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1466893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1467893eead0SChris Wilson }
1468893eead0SChris Wilson 
1469893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1470893eead0SChris Wilson {
1471893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1472893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1473893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
1474b2223497SChris Wilson 		if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
1475893eead0SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1476893eead0SChris Wilson 				  ring->name,
1477b2223497SChris Wilson 				  ring->waiting_seqno,
1478893eead0SChris Wilson 				  ring->get_seqno(ring));
1479893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1480893eead0SChris Wilson 			*err = true;
1481893eead0SChris Wilson 		}
1482893eead0SChris Wilson 		return true;
1483893eead0SChris Wilson 	}
1484893eead0SChris Wilson 	return false;
1485f65d9421SBen Gamari }
1486f65d9421SBen Gamari 
14871ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
14881ec14ad3SChris Wilson {
14891ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
14901ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
14911ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
14921ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
14931ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
14941ec14ad3SChris Wilson 			  ring->name);
14951ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
14961ec14ad3SChris Wilson 		return true;
14971ec14ad3SChris Wilson 	}
14981ec14ad3SChris Wilson 	if (IS_GEN6(dev) &&
14991ec14ad3SChris Wilson 	    (tmp & RING_WAIT_SEMAPHORE)) {
15001ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
15011ec14ad3SChris Wilson 			  ring->name);
15021ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
15031ec14ad3SChris Wilson 		return true;
15041ec14ad3SChris Wilson 	}
15051ec14ad3SChris Wilson 	return false;
15061ec14ad3SChris Wilson }
15071ec14ad3SChris Wilson 
1508f65d9421SBen Gamari /**
1509f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1510f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1511f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1512f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1513f65d9421SBen Gamari  */
1514f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1515f65d9421SBen Gamari {
1516f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1517f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1518cbb465e7SChris Wilson 	uint32_t acthd, instdone, instdone1;
1519893eead0SChris Wilson 	bool err = false;
1520893eead0SChris Wilson 
1521893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
15221ec14ad3SChris Wilson 	if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
15231ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
15241ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
1525893eead0SChris Wilson 		dev_priv->hangcheck_count = 0;
1526893eead0SChris Wilson 		if (err)
1527893eead0SChris Wilson 			goto repeat;
1528893eead0SChris Wilson 		return;
1529893eead0SChris Wilson 	}
1530f65d9421SBen Gamari 
1531a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1532f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
1533cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1534cbb465e7SChris Wilson 		instdone1 = 0;
1535cbb465e7SChris Wilson 	} else {
1536f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
1537cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1538cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1539cbb465e7SChris Wilson 	}
1540f65d9421SBen Gamari 
1541cbb465e7SChris Wilson 	if (dev_priv->last_acthd == acthd &&
1542cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1543cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1544cbb465e7SChris Wilson 		if (dev_priv->hangcheck_count++ > 1) {
1545f65d9421SBen Gamari 			DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
15468c80b59bSChris Wilson 
15478c80b59bSChris Wilson 			if (!IS_GEN2(dev)) {
15488c80b59bSChris Wilson 				/* Is the chip hanging on a WAIT_FOR_EVENT?
15498c80b59bSChris Wilson 				 * If so we can simply poke the RB_WAIT bit
15508c80b59bSChris Wilson 				 * and break the hang. This should work on
15518c80b59bSChris Wilson 				 * all but the second generation chipsets.
15528c80b59bSChris Wilson 				 */
15531ec14ad3SChris Wilson 
15541ec14ad3SChris Wilson 				if (kick_ring(&dev_priv->ring[RCS]))
1555893eead0SChris Wilson 					goto repeat;
15561ec14ad3SChris Wilson 
15571ec14ad3SChris Wilson 				if (HAS_BSD(dev) &&
15581ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[VCS]))
15591ec14ad3SChris Wilson 					goto repeat;
15601ec14ad3SChris Wilson 
15611ec14ad3SChris Wilson 				if (HAS_BLT(dev) &&
15621ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[BCS]))
15631ec14ad3SChris Wilson 					goto repeat;
15648c80b59bSChris Wilson 			}
15658c80b59bSChris Wilson 
1566ba1234d1SBen Gamari 			i915_handle_error(dev, true);
1567f65d9421SBen Gamari 			return;
1568f65d9421SBen Gamari 		}
1569cbb465e7SChris Wilson 	} else {
1570cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1571cbb465e7SChris Wilson 
1572cbb465e7SChris Wilson 		dev_priv->last_acthd = acthd;
1573cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1574cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1575cbb465e7SChris Wilson 	}
1576f65d9421SBen Gamari 
1577893eead0SChris Wilson repeat:
1578f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1579b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1580b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1581f65d9421SBen Gamari }
1582f65d9421SBen Gamari 
1583c0e09200SDave Airlie /* drm_dma.h hooks
1584c0e09200SDave Airlie */
1585f2b115e6SAdam Jackson static void ironlake_irq_preinstall(struct drm_device *dev)
1586036a4a7dSZhenyu Wang {
1587036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1588036a4a7dSZhenyu Wang 
1589036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1590036a4a7dSZhenyu Wang 
1591036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1592036a4a7dSZhenyu Wang 
1593036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1594036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
15953143a2bfSChris Wilson 	POSTING_READ(DEIER);
1596036a4a7dSZhenyu Wang 
1597036a4a7dSZhenyu Wang 	/* and GT */
1598036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1599036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
16003143a2bfSChris Wilson 	POSTING_READ(GTIER);
1601c650156aSZhenyu Wang 
1602c650156aSZhenyu Wang 	/* south display irq */
1603c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1604c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
16053143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1606036a4a7dSZhenyu Wang }
1607036a4a7dSZhenyu Wang 
1608f2b115e6SAdam Jackson static int ironlake_irq_postinstall(struct drm_device *dev)
1609036a4a7dSZhenyu Wang {
1610036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1611036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1612013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1613013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
16141ec14ad3SChris Wilson 	u32 render_irqs;
16152d7b8366SYuanhan Liu 	u32 hotplug_mask;
1616*9db4a9c7SJesse Barnes 	int pipe;
1617036a4a7dSZhenyu Wang 
16181ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1619036a4a7dSZhenyu Wang 
1620036a4a7dSZhenyu Wang 	/* should always can generate irq */
1621036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
16221ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
16231ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
16243143a2bfSChris Wilson 	POSTING_READ(DEIER);
1625036a4a7dSZhenyu Wang 
16261ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1627036a4a7dSZhenyu Wang 
1628036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
16291ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1630881f47b6SXiang, Haihao 
16311ec14ad3SChris Wilson 	if (IS_GEN6(dev))
16321ec14ad3SChris Wilson 		render_irqs =
16331ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
16341ec14ad3SChris Wilson 			GT_GEN6_BSD_USER_INTERRUPT |
16351ec14ad3SChris Wilson 			GT_BLT_USER_INTERRUPT;
16361ec14ad3SChris Wilson 	else
16371ec14ad3SChris Wilson 		render_irqs =
163888f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1639c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
16401ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
16411ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
16423143a2bfSChris Wilson 	POSTING_READ(GTIER);
1643036a4a7dSZhenyu Wang 
16442d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
16452d7b8366SYuanhan Liu 		hotplug_mask = SDE_CRT_HOTPLUG_CPT | SDE_PORTB_HOTPLUG_CPT  |
16462d7b8366SYuanhan Liu 			       SDE_PORTC_HOTPLUG_CPT | SDE_PORTD_HOTPLUG_CPT ;
16472d7b8366SYuanhan Liu 	} else {
16482d7b8366SYuanhan Liu 		hotplug_mask = SDE_CRT_HOTPLUG | SDE_PORTB_HOTPLUG |
16492d7b8366SYuanhan Liu 			       SDE_PORTC_HOTPLUG | SDE_PORTD_HOTPLUG;
1650776ad806SJesse Barnes 		hotplug_mask |= SDE_AUX_MASK | SDE_FDI_MASK | SDE_TRANS_MASK;
1651*9db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1652*9db4a9c7SJesse Barnes 			I915_WRITE(FDI_RX_IMR(pipe), 0);
16532d7b8366SYuanhan Liu 	}
16542d7b8366SYuanhan Liu 
16551ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1656c650156aSZhenyu Wang 
1657c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
16581ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
16591ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
16603143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1661c650156aSZhenyu Wang 
1662f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1663f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1664f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1665f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1666f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1667f97108d1SJesse Barnes 	}
1668f97108d1SJesse Barnes 
1669036a4a7dSZhenyu Wang 	return 0;
1670036a4a7dSZhenyu Wang }
1671036a4a7dSZhenyu Wang 
1672c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1673c0e09200SDave Airlie {
1674c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1675*9db4a9c7SJesse Barnes 	int pipe;
1676c0e09200SDave Airlie 
167779e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
1678b0b544cdSChris Wilson 	atomic_set(&dev_priv->vblank_enabled, 0);
167979e53945SJesse Barnes 
1680036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
16818a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1682b0b544cdSChris Wilson 	INIT_WORK(&dev_priv->vblank_work, i915_vblank_work_func);
1683036a4a7dSZhenyu Wang 
1684bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1685f2b115e6SAdam Jackson 		ironlake_irq_preinstall(dev);
1686036a4a7dSZhenyu Wang 		return;
1687036a4a7dSZhenyu Wang 	}
1688036a4a7dSZhenyu Wang 
16895ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
16905ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
16915ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
16925ca58282SJesse Barnes 	}
16935ca58282SJesse Barnes 
16940a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
1695*9db4a9c7SJesse Barnes 	for_each_pipe(pipe)
1696*9db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0);
16970a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1698ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
16993143a2bfSChris Wilson 	POSTING_READ(IER);
1700c0e09200SDave Airlie }
1701c0e09200SDave Airlie 
1702b01f2c3aSJesse Barnes /*
1703b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
1704b01f2c3aSJesse Barnes  * enabled correctly.
1705b01f2c3aSJesse Barnes  */
17060a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1707c0e09200SDave Airlie {
1708c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17095ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
171063eeaf38SJesse Barnes 	u32 error_mask;
17110a3e67a4SJesse Barnes 
17121ec14ad3SChris Wilson 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
1713d1b851fcSZou Nan hai 	if (HAS_BSD(dev))
17141ec14ad3SChris Wilson 		DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
1715549f7365SChris Wilson 	if (HAS_BLT(dev))
17161ec14ad3SChris Wilson 		DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
1717d1b851fcSZou Nan hai 
17180a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1719ed4cb414SEric Anholt 
1720bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1721f2b115e6SAdam Jackson 		return ironlake_irq_postinstall(dev);
1722036a4a7dSZhenyu Wang 
17237c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
17241ec14ad3SChris Wilson 	dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
17258ee1c3dbSMatthew Garrett 
17267c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
17277c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
17287c463586SKeith Packard 
17295ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
1730c496fa1fSAdam Jackson 		/* Enable in IER... */
1731c496fa1fSAdam Jackson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1732c496fa1fSAdam Jackson 		/* and unmask in IMR */
17331ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
1734c496fa1fSAdam Jackson 	}
1735c496fa1fSAdam Jackson 
1736c496fa1fSAdam Jackson 	/*
1737c496fa1fSAdam Jackson 	 * Enable some error detection, note the instruction error mask
1738c496fa1fSAdam Jackson 	 * bit is reserved, so we leave it masked.
1739c496fa1fSAdam Jackson 	 */
1740c496fa1fSAdam Jackson 	if (IS_G4X(dev)) {
1741c496fa1fSAdam Jackson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
1742c496fa1fSAdam Jackson 			       GM45_ERROR_MEM_PRIV |
1743c496fa1fSAdam Jackson 			       GM45_ERROR_CP_PRIV |
1744c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1745c496fa1fSAdam Jackson 	} else {
1746c496fa1fSAdam Jackson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
1747c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1748c496fa1fSAdam Jackson 	}
1749c496fa1fSAdam Jackson 	I915_WRITE(EMR, error_mask);
1750c496fa1fSAdam Jackson 
17511ec14ad3SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
1752c496fa1fSAdam Jackson 	I915_WRITE(IER, enable_mask);
17533143a2bfSChris Wilson 	POSTING_READ(IER);
1754c496fa1fSAdam Jackson 
1755c496fa1fSAdam Jackson 	if (I915_HAS_HOTPLUG(dev)) {
17565ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
17575ca58282SJesse Barnes 
1758b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
1759b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1760b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1761b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1762b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1763b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1764b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
1765b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1766b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1767b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1768b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
17692d1c9752SAndy Lutomirski 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
1770b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
17712d1c9752SAndy Lutomirski 
17722d1c9752SAndy Lutomirski 			/* Programming the CRT detection parameters tends
17732d1c9752SAndy Lutomirski 			   to generate a spurious hotplug event about three
17742d1c9752SAndy Lutomirski 			   seconds later.  So just do it once.
17752d1c9752SAndy Lutomirski 			*/
17762d1c9752SAndy Lutomirski 			if (IS_G4X(dev))
17772d1c9752SAndy Lutomirski 				hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
17782d1c9752SAndy Lutomirski 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
17792d1c9752SAndy Lutomirski 		}
17802d1c9752SAndy Lutomirski 
1781b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
1782b01f2c3aSJesse Barnes 
17835ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
17845ca58282SJesse Barnes 	}
17855ca58282SJesse Barnes 
17863b617967SChris Wilson 	intel_opregion_enable_asle(dev);
17870a3e67a4SJesse Barnes 
17880a3e67a4SJesse Barnes 	return 0;
1789c0e09200SDave Airlie }
1790c0e09200SDave Airlie 
1791f2b115e6SAdam Jackson static void ironlake_irq_uninstall(struct drm_device *dev)
1792036a4a7dSZhenyu Wang {
1793036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1794036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1795036a4a7dSZhenyu Wang 
1796036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1797036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1798036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1799036a4a7dSZhenyu Wang 
1800036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1801036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1802036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1803036a4a7dSZhenyu Wang }
1804036a4a7dSZhenyu Wang 
1805c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1806c0e09200SDave Airlie {
1807c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1808*9db4a9c7SJesse Barnes 	int pipe;
1809c0e09200SDave Airlie 
1810c0e09200SDave Airlie 	if (!dev_priv)
1811c0e09200SDave Airlie 		return;
1812c0e09200SDave Airlie 
18130a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
18140a3e67a4SJesse Barnes 
1815bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1816f2b115e6SAdam Jackson 		ironlake_irq_uninstall(dev);
1817036a4a7dSZhenyu Wang 		return;
1818036a4a7dSZhenyu Wang 	}
1819036a4a7dSZhenyu Wang 
18205ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
18215ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
18225ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
18235ca58282SJesse Barnes 	}
18245ca58282SJesse Barnes 
18250a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
1826*9db4a9c7SJesse Barnes 	for_each_pipe(pipe)
1827*9db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0);
18280a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1829ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1830c0e09200SDave Airlie 
1831*9db4a9c7SJesse Barnes 	for_each_pipe(pipe)
1832*9db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe),
1833*9db4a9c7SJesse Barnes 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
18347c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1835c0e09200SDave Airlie }
1836