xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision f6b07f45e2df42d63a0292dcd13aaaa18d6c01f7)
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) {
929db4a9c7SJesse 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) {
1059db4a9c7SJesse 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 "
1649db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
1650a3e67a4SJesse Barnes 		return 0;
1660a3e67a4SJesse Barnes 	}
1670a3e67a4SJesse Barnes 
1689db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
1699db4a9c7SJesse 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;
1909db4a9c7SJesse 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 "
1949db4a9c7SJesse 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 "
2129db4a9c7SJesse 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 
309e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
310e67189abSJesse Barnes 
3114ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
3124ef69c7aSChris Wilson 		if (encoder->hot_plug)
3134ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
314c31c4ba3SKeith Packard 
3155ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
316eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3175ca58282SJesse Barnes }
3185ca58282SJesse Barnes 
319f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
320f97108d1SJesse Barnes {
321f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
322b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
323f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
324f97108d1SJesse Barnes 
3257648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
326b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
327b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
328f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
329f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
330f97108d1SJesse Barnes 
331f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
332b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
333f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
334f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
335f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
336f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
337b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
338f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
339f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
340f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
341f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
342f97108d1SJesse Barnes 	}
343f97108d1SJesse Barnes 
3447648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
345f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
346f97108d1SJesse Barnes 
347f97108d1SJesse Barnes 	return;
348f97108d1SJesse Barnes }
349f97108d1SJesse Barnes 
350549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
351549f7365SChris Wilson 			struct intel_ring_buffer *ring)
352549f7365SChris Wilson {
353549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
354475553deSChris Wilson 	u32 seqno;
3559862e600SChris Wilson 
356475553deSChris Wilson 	if (ring->obj == NULL)
357475553deSChris Wilson 		return;
358475553deSChris Wilson 
359475553deSChris Wilson 	seqno = ring->get_seqno(ring);
360db53a302SChris Wilson 	trace_i915_gem_request_complete(ring, seqno);
3619862e600SChris Wilson 
3629862e600SChris Wilson 	ring->irq_seqno = seqno;
363549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3649862e600SChris Wilson 
365549f7365SChris Wilson 	dev_priv->hangcheck_count = 0;
366549f7365SChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
367549f7365SChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
368549f7365SChris Wilson }
369549f7365SChris Wilson 
3704912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
3713b8d8d91SJesse Barnes {
3724912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3734912d041SBen Widawsky 						    rps_work);
3743b8d8d91SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
3754912d041SBen Widawsky 	u32 pm_iir, pm_imr;
3763b8d8d91SJesse Barnes 
3774912d041SBen Widawsky 	spin_lock_irq(&dev_priv->rps_lock);
3784912d041SBen Widawsky 	pm_iir = dev_priv->pm_iir;
3794912d041SBen Widawsky 	dev_priv->pm_iir = 0;
3804912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
3814912d041SBen Widawsky 	spin_unlock_irq(&dev_priv->rps_lock);
3824912d041SBen Widawsky 
3833b8d8d91SJesse Barnes 	if (!pm_iir)
3843b8d8d91SJesse Barnes 		return;
3853b8d8d91SJesse Barnes 
3864912d041SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
3873b8d8d91SJesse Barnes 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
3883b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
3893b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
3903b8d8d91SJesse Barnes 		if (new_delay > dev_priv->max_delay)
3913b8d8d91SJesse Barnes 			new_delay = dev_priv->max_delay;
3923b8d8d91SJesse Barnes 	} else if (pm_iir & (GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT)) {
3934912d041SBen Widawsky 		gen6_gt_force_wake_get(dev_priv);
3943b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
3953b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
3963b8d8d91SJesse Barnes 		if (new_delay < dev_priv->min_delay) {
3973b8d8d91SJesse Barnes 			new_delay = dev_priv->min_delay;
3983b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3993b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) |
4003b8d8d91SJesse Barnes 				   ((new_delay << 16) & 0x3f0000));
4013b8d8d91SJesse Barnes 		} else {
4023b8d8d91SJesse Barnes 			/* Make sure we continue to get down interrupts
4033b8d8d91SJesse Barnes 			 * until we hit the minimum frequency */
4043b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4053b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) & ~0x3f0000);
4063b8d8d91SJesse Barnes 		}
4074912d041SBen Widawsky 		gen6_gt_force_wake_put(dev_priv);
4083b8d8d91SJesse Barnes 	}
4093b8d8d91SJesse Barnes 
4104912d041SBen Widawsky 	gen6_set_rps(dev_priv->dev, new_delay);
4113b8d8d91SJesse Barnes 	dev_priv->cur_delay = new_delay;
4123b8d8d91SJesse Barnes 
4134912d041SBen Widawsky 	/*
4144912d041SBen Widawsky 	 * rps_lock not held here because clearing is non-destructive. There is
4154912d041SBen Widawsky 	 * an *extremely* unlikely race with gen6_rps_enable() that is prevented
4164912d041SBen Widawsky 	 * by holding struct_mutex for the duration of the write.
4174912d041SBen Widawsky 	 */
4184912d041SBen Widawsky 	I915_WRITE(GEN6_PMIMR, pm_imr & ~pm_iir);
4194912d041SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
4203b8d8d91SJesse Barnes }
4213b8d8d91SJesse Barnes 
422776ad806SJesse Barnes static void pch_irq_handler(struct drm_device *dev)
423776ad806SJesse Barnes {
424776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
425776ad806SJesse Barnes 	u32 pch_iir;
4269db4a9c7SJesse Barnes 	int pipe;
427776ad806SJesse Barnes 
428776ad806SJesse Barnes 	pch_iir = I915_READ(SDEIIR);
429776ad806SJesse Barnes 
430776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
431776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
432776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
433776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
434776ad806SJesse Barnes 
435776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
436776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
437776ad806SJesse Barnes 
438776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
439776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
440776ad806SJesse Barnes 
441776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
442776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
443776ad806SJesse Barnes 
444776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
445776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
446776ad806SJesse Barnes 
4479db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
4489db4a9c7SJesse Barnes 		for_each_pipe(pipe)
4499db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
4509db4a9c7SJesse Barnes 					 pipe_name(pipe),
4519db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
452776ad806SJesse Barnes 
453776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
454776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
455776ad806SJesse Barnes 
456776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
457776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
458776ad806SJesse Barnes 
459776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
460776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
461776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
462776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
463776ad806SJesse Barnes }
464776ad806SJesse Barnes 
465b1f14ad0SJesse Barnes irqreturn_t ivybridge_irq_handler(DRM_IRQ_ARGS)
466b1f14ad0SJesse Barnes {
467b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
468b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
469b1f14ad0SJesse Barnes 	int ret = IRQ_NONE;
470b1f14ad0SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
471b1f14ad0SJesse Barnes 	struct drm_i915_master_private *master_priv;
472b1f14ad0SJesse Barnes 
473b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
474b1f14ad0SJesse Barnes 
475b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
476b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
477b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
478b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
479b1f14ad0SJesse Barnes 
480b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
481b1f14ad0SJesse Barnes 	gt_iir = I915_READ(GTIIR);
482b1f14ad0SJesse Barnes 	pch_iir = I915_READ(SDEIIR);
483b1f14ad0SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
484b1f14ad0SJesse Barnes 
485b1f14ad0SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 && pm_iir == 0)
486b1f14ad0SJesse Barnes 		goto done;
487b1f14ad0SJesse Barnes 
488b1f14ad0SJesse Barnes 	ret = IRQ_HANDLED;
489b1f14ad0SJesse Barnes 
490b1f14ad0SJesse Barnes 	if (dev->primary->master) {
491b1f14ad0SJesse Barnes 		master_priv = dev->primary->master->driver_priv;
492b1f14ad0SJesse Barnes 		if (master_priv->sarea_priv)
493b1f14ad0SJesse Barnes 			master_priv->sarea_priv->last_dispatch =
494b1f14ad0SJesse Barnes 				READ_BREADCRUMB(dev_priv);
495b1f14ad0SJesse Barnes 	}
496b1f14ad0SJesse Barnes 
497b1f14ad0SJesse Barnes 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
498b1f14ad0SJesse Barnes 		notify_ring(dev, &dev_priv->ring[RCS]);
499b1f14ad0SJesse Barnes 	if (gt_iir & GT_GEN6_BSD_USER_INTERRUPT)
500b1f14ad0SJesse Barnes 		notify_ring(dev, &dev_priv->ring[VCS]);
501b1f14ad0SJesse Barnes 	if (gt_iir & GT_BLT_USER_INTERRUPT)
502b1f14ad0SJesse Barnes 		notify_ring(dev, &dev_priv->ring[BCS]);
503b1f14ad0SJesse Barnes 
504b1f14ad0SJesse Barnes 	if (de_iir & DE_GSE_IVB)
505b1f14ad0SJesse Barnes 		intel_opregion_gse_intr(dev);
506b1f14ad0SJesse Barnes 
507b1f14ad0SJesse Barnes 	if (de_iir & DE_PLANEA_FLIP_DONE_IVB) {
508b1f14ad0SJesse Barnes 		intel_prepare_page_flip(dev, 0);
509b1f14ad0SJesse Barnes 		intel_finish_page_flip_plane(dev, 0);
510b1f14ad0SJesse Barnes 	}
511b1f14ad0SJesse Barnes 
512b1f14ad0SJesse Barnes 	if (de_iir & DE_PLANEB_FLIP_DONE_IVB) {
513b1f14ad0SJesse Barnes 		intel_prepare_page_flip(dev, 1);
514b1f14ad0SJesse Barnes 		intel_finish_page_flip_plane(dev, 1);
515b1f14ad0SJesse Barnes 	}
516b1f14ad0SJesse Barnes 
517b1f14ad0SJesse Barnes 	if (de_iir & DE_PIPEA_VBLANK_IVB)
518b1f14ad0SJesse Barnes 		drm_handle_vblank(dev, 0);
519b1f14ad0SJesse Barnes 
520*f6b07f45SDan Carpenter 	if (de_iir & DE_PIPEB_VBLANK_IVB)
521b1f14ad0SJesse Barnes 		drm_handle_vblank(dev, 1);
522b1f14ad0SJesse Barnes 
523b1f14ad0SJesse Barnes 	/* check event from PCH */
524b1f14ad0SJesse Barnes 	if (de_iir & DE_PCH_EVENT_IVB) {
525b1f14ad0SJesse Barnes 		if (pch_iir & SDE_HOTPLUG_MASK_CPT)
526b1f14ad0SJesse Barnes 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
527b1f14ad0SJesse Barnes 		pch_irq_handler(dev);
528b1f14ad0SJesse Barnes 	}
529b1f14ad0SJesse Barnes 
530b1f14ad0SJesse Barnes 	if (pm_iir & GEN6_PM_DEFERRED_EVENTS) {
531b1f14ad0SJesse Barnes 		unsigned long flags;
532b1f14ad0SJesse Barnes 		spin_lock_irqsave(&dev_priv->rps_lock, flags);
533b1f14ad0SJesse Barnes 		WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
534b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIMR, pm_iir);
535b1f14ad0SJesse Barnes 		dev_priv->pm_iir |= pm_iir;
536b1f14ad0SJesse Barnes 		spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
537b1f14ad0SJesse Barnes 		queue_work(dev_priv->wq, &dev_priv->rps_work);
538b1f14ad0SJesse Barnes 	}
539b1f14ad0SJesse Barnes 
540b1f14ad0SJesse Barnes 	/* should clear PCH hotplug event before clear CPU irq */
541b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, pch_iir);
542b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, gt_iir);
543b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, de_iir);
544b1f14ad0SJesse Barnes 	I915_WRITE(GEN6_PMIIR, pm_iir);
545b1f14ad0SJesse Barnes 
546b1f14ad0SJesse Barnes done:
547b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
548b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
549b1f14ad0SJesse Barnes 
550b1f14ad0SJesse Barnes 	return ret;
551b1f14ad0SJesse Barnes }
552b1f14ad0SJesse Barnes 
5534697995bSJesse Barnes irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
554036a4a7dSZhenyu Wang {
5554697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
556036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
557036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
5583b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
5592d7b8366SYuanhan Liu 	u32 hotplug_mask;
560036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
561881f47b6SXiang, Haihao 	u32 bsd_usr_interrupt = GT_BSD_USER_INTERRUPT;
562881f47b6SXiang, Haihao 
5634697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5644697995bSJesse Barnes 
565881f47b6SXiang, Haihao 	if (IS_GEN6(dev))
566881f47b6SXiang, Haihao 		bsd_usr_interrupt = GT_GEN6_BSD_USER_INTERRUPT;
567036a4a7dSZhenyu Wang 
5682d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
5692d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
5702d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
5713143a2bfSChris Wilson 	POSTING_READ(DEIER);
5722d109a84SZou, Nanhai 
573036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
574036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
575c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
5763b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
577036a4a7dSZhenyu Wang 
5783b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
5793b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
580c7c85101SZou Nan hai 		goto done;
581036a4a7dSZhenyu Wang 
5822d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
5832d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
5842d7b8366SYuanhan Liu 	else
5852d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
5862d7b8366SYuanhan Liu 
587036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
588036a4a7dSZhenyu Wang 
589036a4a7dSZhenyu Wang 	if (dev->primary->master) {
590036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
591036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
592036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
593036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
594036a4a7dSZhenyu Wang 	}
595036a4a7dSZhenyu Wang 
596c6df541cSChris Wilson 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
5971ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[RCS]);
598881f47b6SXiang, Haihao 	if (gt_iir & bsd_usr_interrupt)
5991ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[VCS]);
6001ec14ad3SChris Wilson 	if (gt_iir & GT_BLT_USER_INTERRUPT)
6011ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[BCS]);
602036a4a7dSZhenyu Wang 
60301c66889SZhao Yakui 	if (de_iir & DE_GSE)
6043b617967SChris Wilson 		intel_opregion_gse_intr(dev);
60501c66889SZhao Yakui 
606f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
607013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
6082bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
609013d5aa2SJesse Barnes 	}
610013d5aa2SJesse Barnes 
611f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
612f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
6132bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
614013d5aa2SJesse Barnes 	}
615c062df61SLi Peng 
616f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
617f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
618f072d2e7SZhenyu Wang 
619f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
620f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
621f072d2e7SZhenyu Wang 
622c650156aSZhenyu Wang 	/* check event from PCH */
623776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
624776ad806SJesse Barnes 		if (pch_iir & hotplug_mask)
625c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
626776ad806SJesse Barnes 		pch_irq_handler(dev);
627776ad806SJesse Barnes 	}
628c650156aSZhenyu Wang 
629f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
6307648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
631f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
632f97108d1SJesse Barnes 	}
633f97108d1SJesse Barnes 
6344912d041SBen Widawsky 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS) {
6354912d041SBen Widawsky 		/*
6364912d041SBen Widawsky 		 * IIR bits should never already be set because IMR should
6374912d041SBen Widawsky 		 * prevent an interrupt from being shown in IIR. The warning
6384912d041SBen Widawsky 		 * displays a case where we've unsafely cleared
6394912d041SBen Widawsky 		 * dev_priv->pm_iir. Although missing an interrupt of the same
6404912d041SBen Widawsky 		 * type is not a problem, it displays a problem in the logic.
6414912d041SBen Widawsky 		 *
6424912d041SBen Widawsky 		 * The mask bit in IMR is cleared by rps_work.
6434912d041SBen Widawsky 		 */
6444912d041SBen Widawsky 		unsigned long flags;
6454912d041SBen Widawsky 		spin_lock_irqsave(&dev_priv->rps_lock, flags);
6464912d041SBen Widawsky 		WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
6474912d041SBen Widawsky 		I915_WRITE(GEN6_PMIMR, pm_iir);
6484912d041SBen Widawsky 		dev_priv->pm_iir |= pm_iir;
6494912d041SBen Widawsky 		spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
6504912d041SBen Widawsky 		queue_work(dev_priv->wq, &dev_priv->rps_work);
6514912d041SBen Widawsky 	}
6523b8d8d91SJesse Barnes 
653c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
654c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
655c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
656c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
6574912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
658036a4a7dSZhenyu Wang 
659c7c85101SZou Nan hai done:
6602d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
6613143a2bfSChris Wilson 	POSTING_READ(DEIER);
6622d109a84SZou, Nanhai 
663036a4a7dSZhenyu Wang 	return ret;
664036a4a7dSZhenyu Wang }
665036a4a7dSZhenyu Wang 
6668a905236SJesse Barnes /**
6678a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
6688a905236SJesse Barnes  * @work: work struct
6698a905236SJesse Barnes  *
6708a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
6718a905236SJesse Barnes  * was detected.
6728a905236SJesse Barnes  */
6738a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
6748a905236SJesse Barnes {
6758a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
6768a905236SJesse Barnes 						    error_work);
6778a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
678f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
679f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
680f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
6818a905236SJesse Barnes 
682f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
6838a905236SJesse Barnes 
684ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
68544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
686f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
687f803aa55SChris Wilson 		if (!i915_reset(dev, GRDOM_RENDER)) {
688ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
689f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
690f316a42cSBen Gamari 		}
69130dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
692f316a42cSBen Gamari 	}
6938a905236SJesse Barnes }
6948a905236SJesse Barnes 
6953bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
6969df30794SChris Wilson static struct drm_i915_error_object *
697bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
69805394f39SChris Wilson 			 struct drm_i915_gem_object *src)
6999df30794SChris Wilson {
7009df30794SChris Wilson 	struct drm_i915_error_object *dst;
7019df30794SChris Wilson 	int page, page_count;
702e56660ddSChris Wilson 	u32 reloc_offset;
7039df30794SChris Wilson 
70405394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
7059df30794SChris Wilson 		return NULL;
7069df30794SChris Wilson 
70705394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
7089df30794SChris Wilson 
7099df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof (u32 *), GFP_ATOMIC);
7109df30794SChris Wilson 	if (dst == NULL)
7119df30794SChris Wilson 		return NULL;
7129df30794SChris Wilson 
71305394f39SChris Wilson 	reloc_offset = src->gtt_offset;
7149df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
715788885aeSAndrew Morton 		unsigned long flags;
716e56660ddSChris Wilson 		void __iomem *s;
717e56660ddSChris Wilson 		void *d;
718788885aeSAndrew Morton 
719e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
7209df30794SChris Wilson 		if (d == NULL)
7219df30794SChris Wilson 			goto unwind;
722e56660ddSChris Wilson 
723788885aeSAndrew Morton 		local_irq_save(flags);
724e56660ddSChris Wilson 		s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
7253e4d3af5SPeter Zijlstra 					     reloc_offset);
726e56660ddSChris Wilson 		memcpy_fromio(d, s, PAGE_SIZE);
7273e4d3af5SPeter Zijlstra 		io_mapping_unmap_atomic(s);
728788885aeSAndrew Morton 		local_irq_restore(flags);
729e56660ddSChris Wilson 
7309df30794SChris Wilson 		dst->pages[page] = d;
731e56660ddSChris Wilson 
732e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
7339df30794SChris Wilson 	}
7349df30794SChris Wilson 	dst->page_count = page_count;
73505394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
7369df30794SChris Wilson 
7379df30794SChris Wilson 	return dst;
7389df30794SChris Wilson 
7399df30794SChris Wilson unwind:
7409df30794SChris Wilson 	while (page--)
7419df30794SChris Wilson 		kfree(dst->pages[page]);
7429df30794SChris Wilson 	kfree(dst);
7439df30794SChris Wilson 	return NULL;
7449df30794SChris Wilson }
7459df30794SChris Wilson 
7469df30794SChris Wilson static void
7479df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
7489df30794SChris Wilson {
7499df30794SChris Wilson 	int page;
7509df30794SChris Wilson 
7519df30794SChris Wilson 	if (obj == NULL)
7529df30794SChris Wilson 		return;
7539df30794SChris Wilson 
7549df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
7559df30794SChris Wilson 		kfree(obj->pages[page]);
7569df30794SChris Wilson 
7579df30794SChris Wilson 	kfree(obj);
7589df30794SChris Wilson }
7599df30794SChris Wilson 
7609df30794SChris Wilson static void
7619df30794SChris Wilson i915_error_state_free(struct drm_device *dev,
7629df30794SChris Wilson 		      struct drm_i915_error_state *error)
7639df30794SChris Wilson {
764e2f973d5SChris Wilson 	int i;
765e2f973d5SChris Wilson 
766e2f973d5SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->batchbuffer); i++)
767e2f973d5SChris Wilson 		i915_error_object_free(error->batchbuffer[i]);
768e2f973d5SChris Wilson 
769e2f973d5SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ringbuffer); i++)
770e2f973d5SChris Wilson 		i915_error_object_free(error->ringbuffer[i]);
771e2f973d5SChris Wilson 
7729df30794SChris Wilson 	kfree(error->active_bo);
7736ef3d427SChris Wilson 	kfree(error->overlay);
7749df30794SChris Wilson 	kfree(error);
7759df30794SChris Wilson }
7769df30794SChris Wilson 
777c724e8a9SChris Wilson static u32 capture_bo_list(struct drm_i915_error_buffer *err,
778c724e8a9SChris Wilson 			   int count,
779c724e8a9SChris Wilson 			   struct list_head *head)
780c724e8a9SChris Wilson {
781c724e8a9SChris Wilson 	struct drm_i915_gem_object *obj;
782c724e8a9SChris Wilson 	int i = 0;
783c724e8a9SChris Wilson 
784c724e8a9SChris Wilson 	list_for_each_entry(obj, head, mm_list) {
785c724e8a9SChris Wilson 		err->size = obj->base.size;
786c724e8a9SChris Wilson 		err->name = obj->base.name;
787c724e8a9SChris Wilson 		err->seqno = obj->last_rendering_seqno;
788c724e8a9SChris Wilson 		err->gtt_offset = obj->gtt_offset;
789c724e8a9SChris Wilson 		err->read_domains = obj->base.read_domains;
790c724e8a9SChris Wilson 		err->write_domain = obj->base.write_domain;
791c724e8a9SChris Wilson 		err->fence_reg = obj->fence_reg;
792c724e8a9SChris Wilson 		err->pinned = 0;
793c724e8a9SChris Wilson 		if (obj->pin_count > 0)
794c724e8a9SChris Wilson 			err->pinned = 1;
795c724e8a9SChris Wilson 		if (obj->user_pin_count > 0)
796c724e8a9SChris Wilson 			err->pinned = -1;
797c724e8a9SChris Wilson 		err->tiling = obj->tiling_mode;
798c724e8a9SChris Wilson 		err->dirty = obj->dirty;
799c724e8a9SChris Wilson 		err->purgeable = obj->madv != I915_MADV_WILLNEED;
8003685092bSChris Wilson 		err->ring = obj->ring ? obj->ring->id : 0;
80193dfb40cSChris Wilson 		err->cache_level = obj->cache_level;
802c724e8a9SChris Wilson 
803c724e8a9SChris Wilson 		if (++i == count)
804c724e8a9SChris Wilson 			break;
805c724e8a9SChris Wilson 
806c724e8a9SChris Wilson 		err++;
807c724e8a9SChris Wilson 	}
808c724e8a9SChris Wilson 
809c724e8a9SChris Wilson 	return i;
810c724e8a9SChris Wilson }
811c724e8a9SChris Wilson 
812748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
813748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
814748ebc60SChris Wilson {
815748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
816748ebc60SChris Wilson 	int i;
817748ebc60SChris Wilson 
818748ebc60SChris Wilson 	/* Fences */
819748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
820748ebc60SChris Wilson 	case 6:
821748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
822748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
823748ebc60SChris Wilson 		break;
824748ebc60SChris Wilson 	case 5:
825748ebc60SChris Wilson 	case 4:
826748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
827748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
828748ebc60SChris Wilson 		break;
829748ebc60SChris Wilson 	case 3:
830748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
831748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
832748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
833748ebc60SChris Wilson 	case 2:
834748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
835748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
836748ebc60SChris Wilson 		break;
837748ebc60SChris Wilson 
838748ebc60SChris Wilson 	}
839748ebc60SChris Wilson }
840748ebc60SChris Wilson 
841bcfb2e28SChris Wilson static struct drm_i915_error_object *
842bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
843bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
844bcfb2e28SChris Wilson {
845bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
846bcfb2e28SChris Wilson 	u32 seqno;
847bcfb2e28SChris Wilson 
848bcfb2e28SChris Wilson 	if (!ring->get_seqno)
849bcfb2e28SChris Wilson 		return NULL;
850bcfb2e28SChris Wilson 
851bcfb2e28SChris Wilson 	seqno = ring->get_seqno(ring);
852bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
853bcfb2e28SChris Wilson 		if (obj->ring != ring)
854bcfb2e28SChris Wilson 			continue;
855bcfb2e28SChris Wilson 
856c37d9a5dSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
857bcfb2e28SChris Wilson 			continue;
858bcfb2e28SChris Wilson 
859bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
860bcfb2e28SChris Wilson 			continue;
861bcfb2e28SChris Wilson 
862bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
863bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
864bcfb2e28SChris Wilson 		 */
865bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
866bcfb2e28SChris Wilson 	}
867bcfb2e28SChris Wilson 
868bcfb2e28SChris Wilson 	return NULL;
869bcfb2e28SChris Wilson }
870bcfb2e28SChris Wilson 
8718a905236SJesse Barnes /**
8728a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
8738a905236SJesse Barnes  * @dev: drm device
8748a905236SJesse Barnes  *
8758a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
8768a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
8778a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
8788a905236SJesse Barnes  * to pick up.
8798a905236SJesse Barnes  */
88063eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
88163eeaf38SJesse Barnes {
88263eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
88305394f39SChris Wilson 	struct drm_i915_gem_object *obj;
88463eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
88563eeaf38SJesse Barnes 	unsigned long flags;
8869db4a9c7SJesse Barnes 	int i, pipe;
88763eeaf38SJesse Barnes 
88863eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
8899df30794SChris Wilson 	error = dev_priv->first_error;
8909df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
8919df30794SChris Wilson 	if (error)
8929df30794SChris Wilson 		return;
89363eeaf38SJesse Barnes 
8949db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
89563eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
89663eeaf38SJesse Barnes 	if (!error) {
8979df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
8989df30794SChris Wilson 		return;
89963eeaf38SJesse Barnes 	}
90063eeaf38SJesse Barnes 
901b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
902b6f7833bSChris Wilson 		 dev->primary->index);
9032fa772f3SChris Wilson 
9041ec14ad3SChris Wilson 	error->seqno = dev_priv->ring[RCS].get_seqno(&dev_priv->ring[RCS]);
90563eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
90663eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
9079db4a9c7SJesse Barnes 	for_each_pipe(pipe)
9089db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
90963eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
910f406839fSChris Wilson 	error->error = 0;
911f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 6) {
912f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
913add354ddSChris Wilson 
9141d8f38f4SChris Wilson 		error->bcs_acthd = I915_READ(BCS_ACTHD);
9151d8f38f4SChris Wilson 		error->bcs_ipehr = I915_READ(BCS_IPEHR);
9161d8f38f4SChris Wilson 		error->bcs_ipeir = I915_READ(BCS_IPEIR);
9171d8f38f4SChris Wilson 		error->bcs_instdone = I915_READ(BCS_INSTDONE);
9181d8f38f4SChris Wilson 		error->bcs_seqno = 0;
9191ec14ad3SChris Wilson 		if (dev_priv->ring[BCS].get_seqno)
9201ec14ad3SChris Wilson 			error->bcs_seqno = dev_priv->ring[BCS].get_seqno(&dev_priv->ring[BCS]);
921add354ddSChris Wilson 
922add354ddSChris Wilson 		error->vcs_acthd = I915_READ(VCS_ACTHD);
923add354ddSChris Wilson 		error->vcs_ipehr = I915_READ(VCS_IPEHR);
924add354ddSChris Wilson 		error->vcs_ipeir = I915_READ(VCS_IPEIR);
925add354ddSChris Wilson 		error->vcs_instdone = I915_READ(VCS_INSTDONE);
926add354ddSChris Wilson 		error->vcs_seqno = 0;
9271ec14ad3SChris Wilson 		if (dev_priv->ring[VCS].get_seqno)
9281ec14ad3SChris Wilson 			error->vcs_seqno = dev_priv->ring[VCS].get_seqno(&dev_priv->ring[VCS]);
929f406839fSChris Wilson 	}
930f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
93163eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
93263eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
93363eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
93463eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
93563eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
93663eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
9379df30794SChris Wilson 		error->bbaddr = I915_READ64(BB_ADDR);
938f406839fSChris Wilson 	} else {
939f406839fSChris Wilson 		error->ipeir = I915_READ(IPEIR);
940f406839fSChris Wilson 		error->ipehr = I915_READ(IPEHR);
941f406839fSChris Wilson 		error->instdone = I915_READ(INSTDONE);
942f406839fSChris Wilson 		error->acthd = I915_READ(ACTHD);
943f406839fSChris Wilson 		error->bbaddr = 0;
9449df30794SChris Wilson 	}
945748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
9469df30794SChris Wilson 
947e2f973d5SChris Wilson 	/* Record the active batch and ring buffers */
948e2f973d5SChris Wilson 	for (i = 0; i < I915_NUM_RINGS; i++) {
949bcfb2e28SChris Wilson 		error->batchbuffer[i] =
950bcfb2e28SChris Wilson 			i915_error_first_batchbuffer(dev_priv,
951bcfb2e28SChris Wilson 						     &dev_priv->ring[i]);
9529df30794SChris Wilson 
953e2f973d5SChris Wilson 		error->ringbuffer[i] =
954e2f973d5SChris Wilson 			i915_error_object_create(dev_priv,
955e2f973d5SChris Wilson 						 dev_priv->ring[i].obj);
956e2f973d5SChris Wilson 	}
9579df30794SChris Wilson 
958c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
9599df30794SChris Wilson 	error->active_bo = NULL;
960c724e8a9SChris Wilson 	error->pinned_bo = NULL;
9619df30794SChris Wilson 
962bcfb2e28SChris Wilson 	i = 0;
963bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
964bcfb2e28SChris Wilson 		i++;
965bcfb2e28SChris Wilson 	error->active_bo_count = i;
96605394f39SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
967bcfb2e28SChris Wilson 		i++;
968bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
969c724e8a9SChris Wilson 
9708e934dbfSChris Wilson 	error->active_bo = NULL;
9718e934dbfSChris Wilson 	error->pinned_bo = NULL;
972bcfb2e28SChris Wilson 	if (i) {
973bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
9749df30794SChris Wilson 					   GFP_ATOMIC);
975c724e8a9SChris Wilson 		if (error->active_bo)
976c724e8a9SChris Wilson 			error->pinned_bo =
977c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
9789df30794SChris Wilson 	}
979c724e8a9SChris Wilson 
980c724e8a9SChris Wilson 	if (error->active_bo)
981c724e8a9SChris Wilson 		error->active_bo_count =
982c724e8a9SChris Wilson 			capture_bo_list(error->active_bo,
983c724e8a9SChris Wilson 					error->active_bo_count,
984c724e8a9SChris Wilson 					&dev_priv->mm.active_list);
985c724e8a9SChris Wilson 
986c724e8a9SChris Wilson 	if (error->pinned_bo)
987c724e8a9SChris Wilson 		error->pinned_bo_count =
988c724e8a9SChris Wilson 			capture_bo_list(error->pinned_bo,
989c724e8a9SChris Wilson 					error->pinned_bo_count,
990c724e8a9SChris Wilson 					&dev_priv->mm.pinned_list);
99163eeaf38SJesse Barnes 
9928a905236SJesse Barnes 	do_gettimeofday(&error->time);
9938a905236SJesse Barnes 
9946ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
995c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
9966ef3d427SChris Wilson 
9979df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
9989df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
99963eeaf38SJesse Barnes 		dev_priv->first_error = error;
10009df30794SChris Wilson 		error = NULL;
10019df30794SChris Wilson 	}
100263eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
10039df30794SChris Wilson 
10049df30794SChris Wilson 	if (error)
10059df30794SChris Wilson 		i915_error_state_free(dev, error);
10069df30794SChris Wilson }
10079df30794SChris Wilson 
10089df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
10099df30794SChris Wilson {
10109df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
10119df30794SChris Wilson 	struct drm_i915_error_state *error;
10129df30794SChris Wilson 
10139df30794SChris Wilson 	spin_lock(&dev_priv->error_lock);
10149df30794SChris Wilson 	error = dev_priv->first_error;
10159df30794SChris Wilson 	dev_priv->first_error = NULL;
10169df30794SChris Wilson 	spin_unlock(&dev_priv->error_lock);
10179df30794SChris Wilson 
10189df30794SChris Wilson 	if (error)
10199df30794SChris Wilson 		i915_error_state_free(dev, error);
102063eeaf38SJesse Barnes }
10213bd3c932SChris Wilson #else
10223bd3c932SChris Wilson #define i915_capture_error_state(x)
10233bd3c932SChris Wilson #endif
102463eeaf38SJesse Barnes 
102535aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1026c0e09200SDave Airlie {
10278a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
102863eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
10299db4a9c7SJesse Barnes 	int pipe;
103063eeaf38SJesse Barnes 
103135aed2e6SChris Wilson 	if (!eir)
103235aed2e6SChris Wilson 		return;
103363eeaf38SJesse Barnes 
103463eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
103563eeaf38SJesse Barnes 	       eir);
10368a905236SJesse Barnes 
10378a905236SJesse Barnes 	if (IS_G4X(dev)) {
10388a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
10398a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
10408a905236SJesse Barnes 
10418a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
10428a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
10438a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
10448a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
10458a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
10468a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
10478a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
10488a905236SJesse Barnes 			       I915_READ(INSTPS));
10498a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
10508a905236SJesse Barnes 			       I915_READ(INSTDONE1));
10518a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
10528a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
10538a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
10543143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
10558a905236SJesse Barnes 		}
10568a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
10578a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
10588a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
10598a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
10608a905236SJesse Barnes 			       pgtbl_err);
10618a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
10623143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
10638a905236SJesse Barnes 		}
10648a905236SJesse Barnes 	}
10658a905236SJesse Barnes 
1066a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
106763eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
106863eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
106963eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
107063eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
107163eeaf38SJesse Barnes 			       pgtbl_err);
107263eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
10733143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
107463eeaf38SJesse Barnes 		}
10758a905236SJesse Barnes 	}
10768a905236SJesse Barnes 
107763eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
10789db4a9c7SJesse Barnes 		printk(KERN_ERR "memory refresh error:\n");
10799db4a9c7SJesse Barnes 		for_each_pipe(pipe)
10809db4a9c7SJesse Barnes 			printk(KERN_ERR "pipe %c stat: 0x%08x\n",
10819db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
108263eeaf38SJesse Barnes 		/* pipestat has already been acked */
108363eeaf38SJesse Barnes 	}
108463eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
108563eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
108663eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
108763eeaf38SJesse Barnes 		       I915_READ(INSTPM));
1088a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
108963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
109063eeaf38SJesse Barnes 
109163eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
109263eeaf38SJesse Barnes 			       I915_READ(IPEIR));
109363eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
109463eeaf38SJesse Barnes 			       I915_READ(IPEHR));
109563eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
109663eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
109763eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
109863eeaf38SJesse Barnes 			       I915_READ(ACTHD));
109963eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
11003143a2bfSChris Wilson 			POSTING_READ(IPEIR);
110163eeaf38SJesse Barnes 		} else {
110263eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
110363eeaf38SJesse Barnes 
110463eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
110563eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
110663eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
110763eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
110863eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
110963eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
111063eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
111163eeaf38SJesse Barnes 			       I915_READ(INSTPS));
111263eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
111363eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
111463eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
111563eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
111663eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
11173143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
111863eeaf38SJesse Barnes 		}
111963eeaf38SJesse Barnes 	}
112063eeaf38SJesse Barnes 
112163eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
11223143a2bfSChris Wilson 	POSTING_READ(EIR);
112363eeaf38SJesse Barnes 	eir = I915_READ(EIR);
112463eeaf38SJesse Barnes 	if (eir) {
112563eeaf38SJesse Barnes 		/*
112663eeaf38SJesse Barnes 		 * some errors might have become stuck,
112763eeaf38SJesse Barnes 		 * mask them.
112863eeaf38SJesse Barnes 		 */
112963eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
113063eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
113163eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
113263eeaf38SJesse Barnes 	}
113335aed2e6SChris Wilson }
113435aed2e6SChris Wilson 
113535aed2e6SChris Wilson /**
113635aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
113735aed2e6SChris Wilson  * @dev: drm device
113835aed2e6SChris Wilson  *
113935aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
114035aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
114135aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
114235aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
114335aed2e6SChris Wilson  * of a ring dump etc.).
114435aed2e6SChris Wilson  */
1145527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
114635aed2e6SChris Wilson {
114735aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
114835aed2e6SChris Wilson 
114935aed2e6SChris Wilson 	i915_capture_error_state(dev);
115035aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
11518a905236SJesse Barnes 
1152ba1234d1SBen Gamari 	if (wedged) {
115330dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1154ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1155ba1234d1SBen Gamari 
115611ed50ecSBen Gamari 		/*
115711ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
115811ed50ecSBen Gamari 		 */
11591ec14ad3SChris Wilson 		wake_up_all(&dev_priv->ring[RCS].irq_queue);
1160f787a5f5SChris Wilson 		if (HAS_BSD(dev))
11611ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[VCS].irq_queue);
1162549f7365SChris Wilson 		if (HAS_BLT(dev))
11631ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[BCS].irq_queue);
116411ed50ecSBen Gamari 	}
116511ed50ecSBen Gamari 
11669c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
11678a905236SJesse Barnes }
11688a905236SJesse Barnes 
11694e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
11704e5359cdSSimon Farnsworth {
11714e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
11724e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11734e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
117405394f39SChris Wilson 	struct drm_i915_gem_object *obj;
11754e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
11764e5359cdSSimon Farnsworth 	unsigned long flags;
11774e5359cdSSimon Farnsworth 	bool stall_detected;
11784e5359cdSSimon Farnsworth 
11794e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
11804e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
11814e5359cdSSimon Farnsworth 		return;
11824e5359cdSSimon Farnsworth 
11834e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
11844e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
11854e5359cdSSimon Farnsworth 
11864e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
11874e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
11884e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
11894e5359cdSSimon Farnsworth 		return;
11904e5359cdSSimon Farnsworth 	}
11914e5359cdSSimon Farnsworth 
11924e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
119305394f39SChris Wilson 	obj = work->pending_flip_obj;
1194a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
11959db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
119605394f39SChris Wilson 		stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
11974e5359cdSSimon Farnsworth 	} else {
11989db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
119905394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
12004e5359cdSSimon Farnsworth 							crtc->y * crtc->fb->pitch +
12014e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
12024e5359cdSSimon Farnsworth 	}
12034e5359cdSSimon Farnsworth 
12044e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
12054e5359cdSSimon Farnsworth 
12064e5359cdSSimon Farnsworth 	if (stall_detected) {
12074e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
12084e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
12094e5359cdSSimon Farnsworth 	}
12104e5359cdSSimon Farnsworth }
12114e5359cdSSimon Farnsworth 
12128a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
12138a905236SJesse Barnes {
12148a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
12158a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
12168a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
12178a905236SJesse Barnes 	u32 iir, new_iir;
12189db4a9c7SJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
12198a905236SJesse Barnes 	u32 vblank_status;
12208a905236SJesse Barnes 	int vblank = 0;
12218a905236SJesse Barnes 	unsigned long irqflags;
12228a905236SJesse Barnes 	int irq_received;
12239db4a9c7SJesse Barnes 	int ret = IRQ_NONE, pipe;
12249db4a9c7SJesse Barnes 	bool blc_event = false;
12258a905236SJesse Barnes 
12268a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
12278a905236SJesse Barnes 
12288a905236SJesse Barnes 	iir = I915_READ(IIR);
12298a905236SJesse Barnes 
1230a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
1231d874bcffSJesse Barnes 		vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
1232e25e6601SJesse Barnes 	else
1233d874bcffSJesse Barnes 		vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
12348a905236SJesse Barnes 
12358a905236SJesse Barnes 	for (;;) {
12368a905236SJesse Barnes 		irq_received = iir != 0;
12378a905236SJesse Barnes 
12388a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
12398a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
12408a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
12418a905236SJesse Barnes 		 * interrupts (for non-MSI).
12428a905236SJesse Barnes 		 */
12431ec14ad3SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
12448a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
1245ba1234d1SBen Gamari 			i915_handle_error(dev, false);
12468a905236SJesse Barnes 
12479db4a9c7SJesse Barnes 		for_each_pipe(pipe) {
12489db4a9c7SJesse Barnes 			int reg = PIPESTAT(pipe);
12499db4a9c7SJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
12509db4a9c7SJesse Barnes 
12518a905236SJesse Barnes 			/*
12529db4a9c7SJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
12538a905236SJesse Barnes 			 */
12549db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
12559db4a9c7SJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
12569db4a9c7SJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
12579db4a9c7SJesse Barnes 							 pipe_name(pipe));
12589db4a9c7SJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
12598a905236SJesse Barnes 				irq_received = 1;
12608a905236SJesse Barnes 			}
12618a905236SJesse Barnes 		}
12621ec14ad3SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
12638a905236SJesse Barnes 
12648a905236SJesse Barnes 		if (!irq_received)
12658a905236SJesse Barnes 			break;
12668a905236SJesse Barnes 
12678a905236SJesse Barnes 		ret = IRQ_HANDLED;
12688a905236SJesse Barnes 
12698a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
12708a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
12718a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
12728a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
12738a905236SJesse Barnes 
127444d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
12758a905236SJesse Barnes 				  hotplug_status);
12768a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
12779c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
12789c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
12798a905236SJesse Barnes 
12808a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
12818a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
128263eeaf38SJesse Barnes 		}
128363eeaf38SJesse Barnes 
1284673a394bSEric Anholt 		I915_WRITE(IIR, iir);
1285cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
12867c463586SKeith Packard 
12877c1c2871SDave Airlie 		if (dev->primary->master) {
12887c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
12897c1c2871SDave Airlie 			if (master_priv->sarea_priv)
12907c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
1291c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
12927c1c2871SDave Airlie 		}
12930a3e67a4SJesse Barnes 
1294549f7365SChris Wilson 		if (iir & I915_USER_INTERRUPT)
12951ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
12961ec14ad3SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
12971ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
1298d1b851fcSZou Nan hai 
12991afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
13006b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
13011afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
13021afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 0);
13031afe3e9dSJesse Barnes 		}
13046b95a207SKristian Høgsberg 
13051afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
130670565d00SJesse Barnes 			intel_prepare_page_flip(dev, 1);
13071afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
13081afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 1);
13091afe3e9dSJesse Barnes 		}
13106b95a207SKristian Høgsberg 
13119db4a9c7SJesse Barnes 		for_each_pipe(pipe) {
13129db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & vblank_status &&
13139db4a9c7SJesse Barnes 			    drm_handle_vblank(dev, pipe)) {
13147c463586SKeith Packard 				vblank++;
13154e5359cdSSimon Farnsworth 				if (!dev_priv->flip_pending_is_done) {
13169db4a9c7SJesse Barnes 					i915_pageflip_stall_check(dev, pipe);
13179db4a9c7SJesse Barnes 					intel_finish_page_flip(dev, pipe);
13187c463586SKeith Packard 				}
13194e5359cdSSimon Farnsworth 			}
13207c463586SKeith Packard 
13219db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
13229db4a9c7SJesse Barnes 				blc_event = true;
13234e5359cdSSimon Farnsworth 		}
13247c463586SKeith Packard 
13259db4a9c7SJesse Barnes 
13269db4a9c7SJesse Barnes 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
13273b617967SChris Wilson 			intel_opregion_asle_intr(dev);
13280a3e67a4SJesse Barnes 
1329cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
1330cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
1331cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
1332cdfbc41fSEric Anholt 		 * we would never get another interrupt.
1333cdfbc41fSEric Anholt 		 *
1334cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
1335cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
1336cdfbc41fSEric Anholt 		 * another one.
1337cdfbc41fSEric Anholt 		 *
1338cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
1339cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
1340cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
1341cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
1342cdfbc41fSEric Anholt 		 * stray interrupts.
1343cdfbc41fSEric Anholt 		 */
1344cdfbc41fSEric Anholt 		iir = new_iir;
134505eff845SKeith Packard 	}
1346cdfbc41fSEric Anholt 
134705eff845SKeith Packard 	return ret;
1348c0e09200SDave Airlie }
1349c0e09200SDave Airlie 
1350c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
1351c0e09200SDave Airlie {
1352c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
13537c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1354c0e09200SDave Airlie 
1355c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
1356c0e09200SDave Airlie 
135744d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
1358c0e09200SDave Airlie 
1359c99b058fSKristian Høgsberg 	dev_priv->counter++;
1360c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
1361c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
13627c1c2871SDave Airlie 	if (master_priv->sarea_priv)
13637c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
1364c0e09200SDave Airlie 
1365e1f99ce6SChris Wilson 	if (BEGIN_LP_RING(4) == 0) {
1366585fb111SJesse Barnes 		OUT_RING(MI_STORE_DWORD_INDEX);
13670baf823aSKeith Packard 		OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1368c0e09200SDave Airlie 		OUT_RING(dev_priv->counter);
1369585fb111SJesse Barnes 		OUT_RING(MI_USER_INTERRUPT);
1370c0e09200SDave Airlie 		ADVANCE_LP_RING();
1371e1f99ce6SChris Wilson 	}
1372c0e09200SDave Airlie 
1373c0e09200SDave Airlie 	return dev_priv->counter;
1374c0e09200SDave Airlie }
1375c0e09200SDave Airlie 
1376c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
1377c0e09200SDave Airlie {
1378c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13797c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1380c0e09200SDave Airlie 	int ret = 0;
13811ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
1382c0e09200SDave Airlie 
138344d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
1384c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
1385c0e09200SDave Airlie 
1386ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
13877c1c2871SDave Airlie 		if (master_priv->sarea_priv)
13887c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
1389c0e09200SDave Airlie 		return 0;
1390ed4cb414SEric Anholt 	}
1391c0e09200SDave Airlie 
13927c1c2871SDave Airlie 	if (master_priv->sarea_priv)
13937c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1394c0e09200SDave Airlie 
1395b13c2b96SChris Wilson 	if (ring->irq_get(ring)) {
13961ec14ad3SChris Wilson 		DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1397c0e09200SDave Airlie 			    READ_BREADCRUMB(dev_priv) >= irq_nr);
13981ec14ad3SChris Wilson 		ring->irq_put(ring);
13995a9a8d1aSChris Wilson 	} else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
14005a9a8d1aSChris Wilson 		ret = -EBUSY;
1401c0e09200SDave Airlie 
1402c0e09200SDave Airlie 	if (ret == -EBUSY) {
1403c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
1404c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1405c0e09200SDave Airlie 	}
1406c0e09200SDave Airlie 
1407c0e09200SDave Airlie 	return ret;
1408c0e09200SDave Airlie }
1409c0e09200SDave Airlie 
1410c0e09200SDave Airlie /* Needs the lock as it touches the ring.
1411c0e09200SDave Airlie  */
1412c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
1413c0e09200SDave Airlie 			 struct drm_file *file_priv)
1414c0e09200SDave Airlie {
1415c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1416c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
1417c0e09200SDave Airlie 	int result;
1418c0e09200SDave Airlie 
14191ec14ad3SChris Wilson 	if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
1420c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1421c0e09200SDave Airlie 		return -EINVAL;
1422c0e09200SDave Airlie 	}
1423299eb93cSEric Anholt 
1424299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1425299eb93cSEric Anholt 
1426546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
1427c0e09200SDave Airlie 	result = i915_emit_irq(dev);
1428546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
1429c0e09200SDave Airlie 
1430c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
1431c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
1432c0e09200SDave Airlie 		return -EFAULT;
1433c0e09200SDave Airlie 	}
1434c0e09200SDave Airlie 
1435c0e09200SDave Airlie 	return 0;
1436c0e09200SDave Airlie }
1437c0e09200SDave Airlie 
1438c0e09200SDave Airlie /* Doesn't need the hardware lock.
1439c0e09200SDave Airlie  */
1440c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
1441c0e09200SDave Airlie 			 struct drm_file *file_priv)
1442c0e09200SDave Airlie {
1443c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1444c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
1445c0e09200SDave Airlie 
1446c0e09200SDave Airlie 	if (!dev_priv) {
1447c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1448c0e09200SDave Airlie 		return -EINVAL;
1449c0e09200SDave Airlie 	}
1450c0e09200SDave Airlie 
1451c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
1452c0e09200SDave Airlie }
1453c0e09200SDave Airlie 
145442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
145542f52ef8SKeith Packard  * we use as a pipe index
145642f52ef8SKeith Packard  */
145742f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
14580a3e67a4SJesse Barnes {
14590a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1460e9d21d7fSKeith Packard 	unsigned long irqflags;
146171e0ffa5SJesse Barnes 
14625eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
146371e0ffa5SJesse Barnes 		return -EINVAL;
14640a3e67a4SJesse Barnes 
14651ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1466f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
14677c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14687c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
14690a3e67a4SJesse Barnes 	else
14707c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14717c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
14728692d00eSChris Wilson 
14738692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
14748692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
14758692d00eSChris Wilson 		I915_WRITE(INSTPM, INSTPM_AGPBUSY_DIS << 16);
14761ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14778692d00eSChris Wilson 
14780a3e67a4SJesse Barnes 	return 0;
14790a3e67a4SJesse Barnes }
14800a3e67a4SJesse Barnes 
1481f796cf8fSJesse Barnes int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1482f796cf8fSJesse Barnes {
1483f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1484f796cf8fSJesse Barnes 	unsigned long irqflags;
1485f796cf8fSJesse Barnes 
1486f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1487f796cf8fSJesse Barnes 		return -EINVAL;
1488f796cf8fSJesse Barnes 
1489f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1490f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1491f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1492f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1493f796cf8fSJesse Barnes 
1494f796cf8fSJesse Barnes 	return 0;
1495f796cf8fSJesse Barnes }
1496f796cf8fSJesse Barnes 
1497b1f14ad0SJesse Barnes int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1498b1f14ad0SJesse Barnes {
1499b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1500b1f14ad0SJesse Barnes 	unsigned long irqflags;
1501b1f14ad0SJesse Barnes 
1502b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1503b1f14ad0SJesse Barnes 		return -EINVAL;
1504b1f14ad0SJesse Barnes 
1505b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1506b1f14ad0SJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1507b1f14ad0SJesse Barnes 				    DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1508b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1509b1f14ad0SJesse Barnes 
1510b1f14ad0SJesse Barnes 	return 0;
1511b1f14ad0SJesse Barnes }
1512b1f14ad0SJesse Barnes 
151342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
151442f52ef8SKeith Packard  * we use as a pipe index
151542f52ef8SKeith Packard  */
151642f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
15170a3e67a4SJesse Barnes {
15180a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1519e9d21d7fSKeith Packard 	unsigned long irqflags;
15200a3e67a4SJesse Barnes 
15211ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15228692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15238692d00eSChris Wilson 		I915_WRITE(INSTPM,
15248692d00eSChris Wilson 			   INSTPM_AGPBUSY_DIS << 16 | INSTPM_AGPBUSY_DIS);
15258692d00eSChris Wilson 
15267c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
15277c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
15287c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
15291ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15300a3e67a4SJesse Barnes }
15310a3e67a4SJesse Barnes 
1532f796cf8fSJesse Barnes void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1533f796cf8fSJesse Barnes {
1534f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1535f796cf8fSJesse Barnes 	unsigned long irqflags;
1536f796cf8fSJesse Barnes 
1537f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1538f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1539f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1540f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1541f796cf8fSJesse Barnes }
1542f796cf8fSJesse Barnes 
1543b1f14ad0SJesse Barnes void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1544b1f14ad0SJesse Barnes {
1545b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1546b1f14ad0SJesse Barnes 	unsigned long irqflags;
1547b1f14ad0SJesse Barnes 
1548b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1549b1f14ad0SJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1550b1f14ad0SJesse Barnes 				     DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1551b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1552b1f14ad0SJesse Barnes }
1553b1f14ad0SJesse Barnes 
1554c0e09200SDave Airlie /* Set the vblank monitor pipe
1555c0e09200SDave Airlie  */
1556c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1557c0e09200SDave Airlie 			 struct drm_file *file_priv)
1558c0e09200SDave Airlie {
1559c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1560c0e09200SDave Airlie 
1561c0e09200SDave Airlie 	if (!dev_priv) {
1562c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1563c0e09200SDave Airlie 		return -EINVAL;
1564c0e09200SDave Airlie 	}
1565c0e09200SDave Airlie 
1566c0e09200SDave Airlie 	return 0;
1567c0e09200SDave Airlie }
1568c0e09200SDave Airlie 
1569c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1570c0e09200SDave Airlie 			 struct drm_file *file_priv)
1571c0e09200SDave Airlie {
1572c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1573c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
1574c0e09200SDave Airlie 
1575c0e09200SDave Airlie 	if (!dev_priv) {
1576c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1577c0e09200SDave Airlie 		return -EINVAL;
1578c0e09200SDave Airlie 	}
1579c0e09200SDave Airlie 
15800a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1581c0e09200SDave Airlie 
1582c0e09200SDave Airlie 	return 0;
1583c0e09200SDave Airlie }
1584c0e09200SDave Airlie 
1585c0e09200SDave Airlie /**
1586c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
1587c0e09200SDave Airlie  */
1588c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
1589c0e09200SDave Airlie 		     struct drm_file *file_priv)
1590c0e09200SDave Airlie {
1591bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1592bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1593bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1594bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1595bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1596bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1597bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1598bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1599bd95e0a4SEric Anholt 	 *
1600bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1601bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1602bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1603bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
16040a3e67a4SJesse Barnes 	 */
1605c0e09200SDave Airlie 	return -EINVAL;
1606c0e09200SDave Airlie }
1607c0e09200SDave Airlie 
1608893eead0SChris Wilson static u32
1609893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1610852835f3SZou Nan hai {
1611893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1612893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1613893eead0SChris Wilson }
1614893eead0SChris Wilson 
1615893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1616893eead0SChris Wilson {
1617893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1618893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1619893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
1620b2223497SChris Wilson 		if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
1621893eead0SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1622893eead0SChris Wilson 				  ring->name,
1623b2223497SChris Wilson 				  ring->waiting_seqno,
1624893eead0SChris Wilson 				  ring->get_seqno(ring));
1625893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1626893eead0SChris Wilson 			*err = true;
1627893eead0SChris Wilson 		}
1628893eead0SChris Wilson 		return true;
1629893eead0SChris Wilson 	}
1630893eead0SChris Wilson 	return false;
1631f65d9421SBen Gamari }
1632f65d9421SBen Gamari 
16331ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
16341ec14ad3SChris Wilson {
16351ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
16361ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
16371ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
16381ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
16391ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
16401ec14ad3SChris Wilson 			  ring->name);
16411ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
16421ec14ad3SChris Wilson 		return true;
16431ec14ad3SChris Wilson 	}
16441ec14ad3SChris Wilson 	if (IS_GEN6(dev) &&
16451ec14ad3SChris Wilson 	    (tmp & RING_WAIT_SEMAPHORE)) {
16461ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
16471ec14ad3SChris Wilson 			  ring->name);
16481ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
16491ec14ad3SChris Wilson 		return true;
16501ec14ad3SChris Wilson 	}
16511ec14ad3SChris Wilson 	return false;
16521ec14ad3SChris Wilson }
16531ec14ad3SChris Wilson 
1654f65d9421SBen Gamari /**
1655f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1656f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1657f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1658f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1659f65d9421SBen Gamari  */
1660f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1661f65d9421SBen Gamari {
1662f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1663f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1664cbb465e7SChris Wilson 	uint32_t acthd, instdone, instdone1;
1665893eead0SChris Wilson 	bool err = false;
1666893eead0SChris Wilson 
1667893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
16681ec14ad3SChris Wilson 	if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
16691ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
16701ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
1671893eead0SChris Wilson 		dev_priv->hangcheck_count = 0;
1672893eead0SChris Wilson 		if (err)
1673893eead0SChris Wilson 			goto repeat;
1674893eead0SChris Wilson 		return;
1675893eead0SChris Wilson 	}
1676f65d9421SBen Gamari 
1677a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1678f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
1679cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1680cbb465e7SChris Wilson 		instdone1 = 0;
1681cbb465e7SChris Wilson 	} else {
1682f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
1683cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1684cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1685cbb465e7SChris Wilson 	}
1686f65d9421SBen Gamari 
1687cbb465e7SChris Wilson 	if (dev_priv->last_acthd == acthd &&
1688cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1689cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1690cbb465e7SChris Wilson 		if (dev_priv->hangcheck_count++ > 1) {
1691f65d9421SBen Gamari 			DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
16928c80b59bSChris Wilson 
16938c80b59bSChris Wilson 			if (!IS_GEN2(dev)) {
16948c80b59bSChris Wilson 				/* Is the chip hanging on a WAIT_FOR_EVENT?
16958c80b59bSChris Wilson 				 * If so we can simply poke the RB_WAIT bit
16968c80b59bSChris Wilson 				 * and break the hang. This should work on
16978c80b59bSChris Wilson 				 * all but the second generation chipsets.
16988c80b59bSChris Wilson 				 */
16991ec14ad3SChris Wilson 
17001ec14ad3SChris Wilson 				if (kick_ring(&dev_priv->ring[RCS]))
1701893eead0SChris Wilson 					goto repeat;
17021ec14ad3SChris Wilson 
17031ec14ad3SChris Wilson 				if (HAS_BSD(dev) &&
17041ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[VCS]))
17051ec14ad3SChris Wilson 					goto repeat;
17061ec14ad3SChris Wilson 
17071ec14ad3SChris Wilson 				if (HAS_BLT(dev) &&
17081ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[BCS]))
17091ec14ad3SChris Wilson 					goto repeat;
17108c80b59bSChris Wilson 			}
17118c80b59bSChris Wilson 
1712ba1234d1SBen Gamari 			i915_handle_error(dev, true);
1713f65d9421SBen Gamari 			return;
1714f65d9421SBen Gamari 		}
1715cbb465e7SChris Wilson 	} else {
1716cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1717cbb465e7SChris Wilson 
1718cbb465e7SChris Wilson 		dev_priv->last_acthd = acthd;
1719cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1720cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1721cbb465e7SChris Wilson 	}
1722f65d9421SBen Gamari 
1723893eead0SChris Wilson repeat:
1724f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1725b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1726b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1727f65d9421SBen Gamari }
1728f65d9421SBen Gamari 
1729c0e09200SDave Airlie /* drm_dma.h hooks
1730c0e09200SDave Airlie */
17314697995bSJesse Barnes void ironlake_irq_preinstall(struct drm_device *dev)
1732036a4a7dSZhenyu Wang {
1733036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1734036a4a7dSZhenyu Wang 
17354697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17364697995bSJesse Barnes 
17374697995bSJesse Barnes 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
17384697995bSJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
17399e3c256dSJesse Barnes 	if (IS_GEN6(dev) || IS_IVYBRIDGE(dev))
17409e3c256dSJesse Barnes 		INIT_WORK(&dev_priv->rps_work, gen6_pm_rps_work);
17414697995bSJesse Barnes 
1742036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1743036a4a7dSZhenyu Wang 
1744036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1745036a4a7dSZhenyu Wang 
1746036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1747036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
17483143a2bfSChris Wilson 	POSTING_READ(DEIER);
1749036a4a7dSZhenyu Wang 
1750036a4a7dSZhenyu Wang 	/* and GT */
1751036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1752036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
17533143a2bfSChris Wilson 	POSTING_READ(GTIER);
1754c650156aSZhenyu Wang 
1755c650156aSZhenyu Wang 	/* south display irq */
1756c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1757c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
17583143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1759036a4a7dSZhenyu Wang }
1760036a4a7dSZhenyu Wang 
17614697995bSJesse Barnes int ironlake_irq_postinstall(struct drm_device *dev)
1762036a4a7dSZhenyu Wang {
1763036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1764036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1765013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1766013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
17671ec14ad3SChris Wilson 	u32 render_irqs;
17682d7b8366SYuanhan Liu 	u32 hotplug_mask;
1769036a4a7dSZhenyu Wang 
17704697995bSJesse Barnes 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
17714697995bSJesse Barnes 	if (HAS_BSD(dev))
17724697995bSJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
17734697995bSJesse Barnes 	if (HAS_BLT(dev))
17744697995bSJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
17754697995bSJesse Barnes 
17764697995bSJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
17771ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1778036a4a7dSZhenyu Wang 
1779036a4a7dSZhenyu Wang 	/* should always can generate irq */
1780036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
17811ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
17821ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
17833143a2bfSChris Wilson 	POSTING_READ(DEIER);
1784036a4a7dSZhenyu Wang 
17851ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1786036a4a7dSZhenyu Wang 
1787036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
17881ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1789881f47b6SXiang, Haihao 
17901ec14ad3SChris Wilson 	if (IS_GEN6(dev))
17911ec14ad3SChris Wilson 		render_irqs =
17921ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
17931ec14ad3SChris Wilson 			GT_GEN6_BSD_USER_INTERRUPT |
17941ec14ad3SChris Wilson 			GT_BLT_USER_INTERRUPT;
17951ec14ad3SChris Wilson 	else
17961ec14ad3SChris Wilson 		render_irqs =
179788f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1798c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
17991ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
18001ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
18013143a2bfSChris Wilson 	POSTING_READ(GTIER);
1802036a4a7dSZhenyu Wang 
18032d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
18049035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
18059035a97aSChris Wilson 				SDE_PORTB_HOTPLUG_CPT |
18069035a97aSChris Wilson 				SDE_PORTC_HOTPLUG_CPT |
18079035a97aSChris Wilson 				SDE_PORTD_HOTPLUG_CPT);
18082d7b8366SYuanhan Liu 	} else {
18099035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG |
18109035a97aSChris Wilson 				SDE_PORTB_HOTPLUG |
18119035a97aSChris Wilson 				SDE_PORTC_HOTPLUG |
18129035a97aSChris Wilson 				SDE_PORTD_HOTPLUG |
18139035a97aSChris Wilson 				SDE_AUX_MASK);
18142d7b8366SYuanhan Liu 	}
18152d7b8366SYuanhan Liu 
18161ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1817c650156aSZhenyu Wang 
1818c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
18191ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
18201ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
18213143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1822c650156aSZhenyu Wang 
1823f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1824f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1825f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1826f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1827f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1828f97108d1SJesse Barnes 	}
1829f97108d1SJesse Barnes 
1830036a4a7dSZhenyu Wang 	return 0;
1831036a4a7dSZhenyu Wang }
1832036a4a7dSZhenyu Wang 
1833b1f14ad0SJesse Barnes int ivybridge_irq_postinstall(struct drm_device *dev)
1834b1f14ad0SJesse Barnes {
1835b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1836b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
1837b1f14ad0SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
1838b1f14ad0SJesse Barnes 		DE_PCH_EVENT_IVB | DE_PLANEA_FLIP_DONE_IVB |
1839b1f14ad0SJesse Barnes 		DE_PLANEB_FLIP_DONE_IVB;
1840b1f14ad0SJesse Barnes 	u32 render_irqs;
1841b1f14ad0SJesse Barnes 	u32 hotplug_mask;
1842b1f14ad0SJesse Barnes 
1843b1f14ad0SJesse Barnes 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
1844b1f14ad0SJesse Barnes 	if (HAS_BSD(dev))
1845b1f14ad0SJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
1846b1f14ad0SJesse Barnes 	if (HAS_BLT(dev))
1847b1f14ad0SJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
1848b1f14ad0SJesse Barnes 
1849b1f14ad0SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1850b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
1851b1f14ad0SJesse Barnes 
1852b1f14ad0SJesse Barnes 	/* should always can generate irq */
1853b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1854b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
1855b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK_IVB |
1856b1f14ad0SJesse Barnes 		   DE_PIPEB_VBLANK_IVB);
1857b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1858b1f14ad0SJesse Barnes 
1859b1f14ad0SJesse Barnes 	dev_priv->gt_irq_mask = ~0;
1860b1f14ad0SJesse Barnes 
1861b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1862b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1863b1f14ad0SJesse Barnes 
1864b1f14ad0SJesse Barnes 	render_irqs = GT_USER_INTERRUPT | GT_GEN6_BSD_USER_INTERRUPT |
1865b1f14ad0SJesse Barnes 		GT_BLT_USER_INTERRUPT;
1866b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
1867b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
1868b1f14ad0SJesse Barnes 
1869b1f14ad0SJesse Barnes 	hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
1870b1f14ad0SJesse Barnes 			SDE_PORTB_HOTPLUG_CPT |
1871b1f14ad0SJesse Barnes 			SDE_PORTC_HOTPLUG_CPT |
1872b1f14ad0SJesse Barnes 			SDE_PORTD_HOTPLUG_CPT);
1873b1f14ad0SJesse Barnes 	dev_priv->pch_irq_mask = ~hotplug_mask;
1874b1f14ad0SJesse Barnes 
1875b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1876b1f14ad0SJesse Barnes 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1877b1f14ad0SJesse Barnes 	I915_WRITE(SDEIER, hotplug_mask);
1878b1f14ad0SJesse Barnes 	POSTING_READ(SDEIER);
1879b1f14ad0SJesse Barnes 
1880b1f14ad0SJesse Barnes 	return 0;
1881b1f14ad0SJesse Barnes }
1882b1f14ad0SJesse Barnes 
1883c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1884c0e09200SDave Airlie {
1885c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18869db4a9c7SJesse Barnes 	int pipe;
1887c0e09200SDave Airlie 
188879e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
188979e53945SJesse Barnes 
1890036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
18918a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1892036a4a7dSZhenyu Wang 
18935ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
18945ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
18955ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
18965ca58282SJesse Barnes 	}
18975ca58282SJesse Barnes 
18980a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
18999db4a9c7SJesse Barnes 	for_each_pipe(pipe)
19009db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0);
19010a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1902ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
19033143a2bfSChris Wilson 	POSTING_READ(IER);
1904c0e09200SDave Airlie }
1905c0e09200SDave Airlie 
1906b01f2c3aSJesse Barnes /*
1907b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
1908b01f2c3aSJesse Barnes  * enabled correctly.
1909b01f2c3aSJesse Barnes  */
19100a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1911c0e09200SDave Airlie {
1912c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19135ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
191463eeaf38SJesse Barnes 	u32 error_mask;
19150a3e67a4SJesse Barnes 
19160a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1917ed4cb414SEric Anholt 
19187c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
19191ec14ad3SChris Wilson 	dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
19208ee1c3dbSMatthew Garrett 
19217c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
19227c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
19237c463586SKeith Packard 
19245ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
1925c496fa1fSAdam Jackson 		/* Enable in IER... */
1926c496fa1fSAdam Jackson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1927c496fa1fSAdam Jackson 		/* and unmask in IMR */
19281ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
1929c496fa1fSAdam Jackson 	}
1930c496fa1fSAdam Jackson 
1931c496fa1fSAdam Jackson 	/*
1932c496fa1fSAdam Jackson 	 * Enable some error detection, note the instruction error mask
1933c496fa1fSAdam Jackson 	 * bit is reserved, so we leave it masked.
1934c496fa1fSAdam Jackson 	 */
1935c496fa1fSAdam Jackson 	if (IS_G4X(dev)) {
1936c496fa1fSAdam Jackson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
1937c496fa1fSAdam Jackson 			       GM45_ERROR_MEM_PRIV |
1938c496fa1fSAdam Jackson 			       GM45_ERROR_CP_PRIV |
1939c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1940c496fa1fSAdam Jackson 	} else {
1941c496fa1fSAdam Jackson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
1942c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1943c496fa1fSAdam Jackson 	}
1944c496fa1fSAdam Jackson 	I915_WRITE(EMR, error_mask);
1945c496fa1fSAdam Jackson 
19461ec14ad3SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
1947c496fa1fSAdam Jackson 	I915_WRITE(IER, enable_mask);
19483143a2bfSChris Wilson 	POSTING_READ(IER);
1949c496fa1fSAdam Jackson 
1950c496fa1fSAdam Jackson 	if (I915_HAS_HOTPLUG(dev)) {
19515ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
19525ca58282SJesse Barnes 
1953b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
1954b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1955b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1956b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1957b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1958b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1959b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
1960b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1961b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1962b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1963b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
19642d1c9752SAndy Lutomirski 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
1965b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
19662d1c9752SAndy Lutomirski 
19672d1c9752SAndy Lutomirski 			/* Programming the CRT detection parameters tends
19682d1c9752SAndy Lutomirski 			   to generate a spurious hotplug event about three
19692d1c9752SAndy Lutomirski 			   seconds later.  So just do it once.
19702d1c9752SAndy Lutomirski 			*/
19712d1c9752SAndy Lutomirski 			if (IS_G4X(dev))
19722d1c9752SAndy Lutomirski 				hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
19732d1c9752SAndy Lutomirski 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
19742d1c9752SAndy Lutomirski 		}
19752d1c9752SAndy Lutomirski 
1976b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
1977b01f2c3aSJesse Barnes 
19785ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
19795ca58282SJesse Barnes 	}
19805ca58282SJesse Barnes 
19813b617967SChris Wilson 	intel_opregion_enable_asle(dev);
19820a3e67a4SJesse Barnes 
19830a3e67a4SJesse Barnes 	return 0;
1984c0e09200SDave Airlie }
1985c0e09200SDave Airlie 
19864697995bSJesse Barnes void ironlake_irq_uninstall(struct drm_device *dev)
1987036a4a7dSZhenyu Wang {
1988036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19894697995bSJesse Barnes 
19904697995bSJesse Barnes 	if (!dev_priv)
19914697995bSJesse Barnes 		return;
19924697995bSJesse Barnes 
19934697995bSJesse Barnes 	dev_priv->vblank_pipe = 0;
19944697995bSJesse Barnes 
1995036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1996036a4a7dSZhenyu Wang 
1997036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1998036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1999036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2000036a4a7dSZhenyu Wang 
2001036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2002036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2003036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2004036a4a7dSZhenyu Wang }
2005036a4a7dSZhenyu Wang 
2006c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
2007c0e09200SDave Airlie {
2008c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20099db4a9c7SJesse Barnes 	int pipe;
2010c0e09200SDave Airlie 
2011c0e09200SDave Airlie 	if (!dev_priv)
2012c0e09200SDave Airlie 		return;
2013c0e09200SDave Airlie 
20140a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
20150a3e67a4SJesse Barnes 
20165ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
20175ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
20185ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
20195ca58282SJesse Barnes 	}
20205ca58282SJesse Barnes 
20210a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
20229db4a9c7SJesse Barnes 	for_each_pipe(pipe)
20239db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0);
20240a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
2025ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
2026c0e09200SDave Airlie 
20279db4a9c7SJesse Barnes 	for_each_pipe(pipe)
20289db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe),
20299db4a9c7SJesse Barnes 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
20307c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
2031c0e09200SDave Airlie }
2032