xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 192aac1f19e7a2c69fe486b863c914aabd7acc69)
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  */
155f71d4af4SJesse Barnes static 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 
187f71d4af4SJesse Barnes static 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 
201f71d4af4SJesse Barnes static 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 
267f71d4af4SJesse Barnes static 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 
309a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
310e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
311e67189abSJesse Barnes 
3124ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
3134ef69c7aSChris Wilson 		if (encoder->hot_plug)
3144ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
315c31c4ba3SKeith Packard 
31640ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
31740ee3381SKeith Packard 
3185ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
319eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3205ca58282SJesse Barnes }
3215ca58282SJesse Barnes 
322f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
323f97108d1SJesse Barnes {
324f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
325b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
326f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
327f97108d1SJesse Barnes 
3287648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
329b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
330b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
331f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
332f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
333f97108d1SJesse Barnes 
334f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
335b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
336f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
337f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
338f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
339f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
340b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
341f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
342f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
343f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
344f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
345f97108d1SJesse Barnes 	}
346f97108d1SJesse Barnes 
3477648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
348f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
349f97108d1SJesse Barnes 
350f97108d1SJesse Barnes 	return;
351f97108d1SJesse Barnes }
352f97108d1SJesse Barnes 
353549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
354549f7365SChris Wilson 			struct intel_ring_buffer *ring)
355549f7365SChris Wilson {
356549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
357475553deSChris Wilson 	u32 seqno;
3589862e600SChris Wilson 
359475553deSChris Wilson 	if (ring->obj == NULL)
360475553deSChris Wilson 		return;
361475553deSChris Wilson 
362475553deSChris Wilson 	seqno = ring->get_seqno(ring);
363db53a302SChris Wilson 	trace_i915_gem_request_complete(ring, seqno);
3649862e600SChris Wilson 
3659862e600SChris Wilson 	ring->irq_seqno = seqno;
366549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3673e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
368549f7365SChris Wilson 		dev_priv->hangcheck_count = 0;
369549f7365SChris Wilson 		mod_timer(&dev_priv->hangcheck_timer,
3703e0dc6b0SBen Widawsky 			  jiffies +
3713e0dc6b0SBen Widawsky 			  msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
3723e0dc6b0SBen Widawsky 	}
373549f7365SChris Wilson }
374549f7365SChris Wilson 
3754912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
3763b8d8d91SJesse Barnes {
3774912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3784912d041SBen Widawsky 						    rps_work);
3793b8d8d91SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
3804912d041SBen Widawsky 	u32 pm_iir, pm_imr;
3813b8d8d91SJesse Barnes 
3824912d041SBen Widawsky 	spin_lock_irq(&dev_priv->rps_lock);
3834912d041SBen Widawsky 	pm_iir = dev_priv->pm_iir;
3844912d041SBen Widawsky 	dev_priv->pm_iir = 0;
3854912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
3864912d041SBen Widawsky 	spin_unlock_irq(&dev_priv->rps_lock);
3874912d041SBen Widawsky 
3883b8d8d91SJesse Barnes 	if (!pm_iir)
3893b8d8d91SJesse Barnes 		return;
3903b8d8d91SJesse Barnes 
3914912d041SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
3923b8d8d91SJesse Barnes 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
3933b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
3943b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
3953b8d8d91SJesse Barnes 		if (new_delay > dev_priv->max_delay)
3963b8d8d91SJesse Barnes 			new_delay = dev_priv->max_delay;
3973b8d8d91SJesse Barnes 	} else if (pm_iir & (GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT)) {
3984912d041SBen Widawsky 		gen6_gt_force_wake_get(dev_priv);
3993b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
4003b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
4013b8d8d91SJesse Barnes 		if (new_delay < dev_priv->min_delay) {
4023b8d8d91SJesse Barnes 			new_delay = dev_priv->min_delay;
4033b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4043b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) |
4053b8d8d91SJesse Barnes 				   ((new_delay << 16) & 0x3f0000));
4063b8d8d91SJesse Barnes 		} else {
4073b8d8d91SJesse Barnes 			/* Make sure we continue to get down interrupts
4083b8d8d91SJesse Barnes 			 * until we hit the minimum frequency */
4093b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4103b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) & ~0x3f0000);
4113b8d8d91SJesse Barnes 		}
4124912d041SBen Widawsky 		gen6_gt_force_wake_put(dev_priv);
4133b8d8d91SJesse Barnes 	}
4143b8d8d91SJesse Barnes 
4154912d041SBen Widawsky 	gen6_set_rps(dev_priv->dev, new_delay);
4163b8d8d91SJesse Barnes 	dev_priv->cur_delay = new_delay;
4173b8d8d91SJesse Barnes 
4184912d041SBen Widawsky 	/*
4194912d041SBen Widawsky 	 * rps_lock not held here because clearing is non-destructive. There is
4204912d041SBen Widawsky 	 * an *extremely* unlikely race with gen6_rps_enable() that is prevented
4214912d041SBen Widawsky 	 * by holding struct_mutex for the duration of the write.
4224912d041SBen Widawsky 	 */
4234912d041SBen Widawsky 	I915_WRITE(GEN6_PMIMR, pm_imr & ~pm_iir);
4244912d041SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
4253b8d8d91SJesse Barnes }
4263b8d8d91SJesse Barnes 
427776ad806SJesse Barnes static void pch_irq_handler(struct drm_device *dev)
428776ad806SJesse Barnes {
429776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
430776ad806SJesse Barnes 	u32 pch_iir;
4319db4a9c7SJesse Barnes 	int pipe;
432776ad806SJesse Barnes 
433776ad806SJesse Barnes 	pch_iir = I915_READ(SDEIIR);
434776ad806SJesse Barnes 
435776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
436776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
437776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
438776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
439776ad806SJesse Barnes 
440776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
441776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
442776ad806SJesse Barnes 
443776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
444776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
445776ad806SJesse Barnes 
446776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
447776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
448776ad806SJesse Barnes 
449776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
450776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
451776ad806SJesse Barnes 
4529db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
4539db4a9c7SJesse Barnes 		for_each_pipe(pipe)
4549db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
4559db4a9c7SJesse Barnes 					 pipe_name(pipe),
4569db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
457776ad806SJesse Barnes 
458776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
459776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
460776ad806SJesse Barnes 
461776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
462776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
463776ad806SJesse Barnes 
464776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
465776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
466776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
467776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
468776ad806SJesse Barnes }
469776ad806SJesse Barnes 
470f71d4af4SJesse Barnes static irqreturn_t ivybridge_irq_handler(DRM_IRQ_ARGS)
471b1f14ad0SJesse Barnes {
472b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
473b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
474b1f14ad0SJesse Barnes 	int ret = IRQ_NONE;
475b1f14ad0SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
476b1f14ad0SJesse Barnes 	struct drm_i915_master_private *master_priv;
477b1f14ad0SJesse Barnes 
478b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
479b1f14ad0SJesse Barnes 
480b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
481b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
482b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
483b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
484b1f14ad0SJesse Barnes 
485b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
486b1f14ad0SJesse Barnes 	gt_iir = I915_READ(GTIIR);
487b1f14ad0SJesse Barnes 	pch_iir = I915_READ(SDEIIR);
488b1f14ad0SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
489b1f14ad0SJesse Barnes 
490b1f14ad0SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 && pm_iir == 0)
491b1f14ad0SJesse Barnes 		goto done;
492b1f14ad0SJesse Barnes 
493b1f14ad0SJesse Barnes 	ret = IRQ_HANDLED;
494b1f14ad0SJesse Barnes 
495b1f14ad0SJesse Barnes 	if (dev->primary->master) {
496b1f14ad0SJesse Barnes 		master_priv = dev->primary->master->driver_priv;
497b1f14ad0SJesse Barnes 		if (master_priv->sarea_priv)
498b1f14ad0SJesse Barnes 			master_priv->sarea_priv->last_dispatch =
499b1f14ad0SJesse Barnes 				READ_BREADCRUMB(dev_priv);
500b1f14ad0SJesse Barnes 	}
501b1f14ad0SJesse Barnes 
502b1f14ad0SJesse Barnes 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
503b1f14ad0SJesse Barnes 		notify_ring(dev, &dev_priv->ring[RCS]);
504b1f14ad0SJesse Barnes 	if (gt_iir & GT_GEN6_BSD_USER_INTERRUPT)
505b1f14ad0SJesse Barnes 		notify_ring(dev, &dev_priv->ring[VCS]);
506b1f14ad0SJesse Barnes 	if (gt_iir & GT_BLT_USER_INTERRUPT)
507b1f14ad0SJesse Barnes 		notify_ring(dev, &dev_priv->ring[BCS]);
508b1f14ad0SJesse Barnes 
509b1f14ad0SJesse Barnes 	if (de_iir & DE_GSE_IVB)
510b1f14ad0SJesse Barnes 		intel_opregion_gse_intr(dev);
511b1f14ad0SJesse Barnes 
512b1f14ad0SJesse Barnes 	if (de_iir & DE_PLANEA_FLIP_DONE_IVB) {
513b1f14ad0SJesse Barnes 		intel_prepare_page_flip(dev, 0);
514b1f14ad0SJesse Barnes 		intel_finish_page_flip_plane(dev, 0);
515b1f14ad0SJesse Barnes 	}
516b1f14ad0SJesse Barnes 
517b1f14ad0SJesse Barnes 	if (de_iir & DE_PLANEB_FLIP_DONE_IVB) {
518b1f14ad0SJesse Barnes 		intel_prepare_page_flip(dev, 1);
519b1f14ad0SJesse Barnes 		intel_finish_page_flip_plane(dev, 1);
520b1f14ad0SJesse Barnes 	}
521b1f14ad0SJesse Barnes 
522b1f14ad0SJesse Barnes 	if (de_iir & DE_PIPEA_VBLANK_IVB)
523b1f14ad0SJesse Barnes 		drm_handle_vblank(dev, 0);
524b1f14ad0SJesse Barnes 
525f6b07f45SDan Carpenter 	if (de_iir & DE_PIPEB_VBLANK_IVB)
526b1f14ad0SJesse Barnes 		drm_handle_vblank(dev, 1);
527b1f14ad0SJesse Barnes 
528b1f14ad0SJesse Barnes 	/* check event from PCH */
529b1f14ad0SJesse Barnes 	if (de_iir & DE_PCH_EVENT_IVB) {
530b1f14ad0SJesse Barnes 		if (pch_iir & SDE_HOTPLUG_MASK_CPT)
531b1f14ad0SJesse Barnes 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
532b1f14ad0SJesse Barnes 		pch_irq_handler(dev);
533b1f14ad0SJesse Barnes 	}
534b1f14ad0SJesse Barnes 
535b1f14ad0SJesse Barnes 	if (pm_iir & GEN6_PM_DEFERRED_EVENTS) {
536b1f14ad0SJesse Barnes 		unsigned long flags;
537b1f14ad0SJesse Barnes 		spin_lock_irqsave(&dev_priv->rps_lock, flags);
538b1f14ad0SJesse Barnes 		WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
539b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIMR, pm_iir);
540b1f14ad0SJesse Barnes 		dev_priv->pm_iir |= pm_iir;
541b1f14ad0SJesse Barnes 		spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
542b1f14ad0SJesse Barnes 		queue_work(dev_priv->wq, &dev_priv->rps_work);
543b1f14ad0SJesse Barnes 	}
544b1f14ad0SJesse Barnes 
545b1f14ad0SJesse Barnes 	/* should clear PCH hotplug event before clear CPU irq */
546b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, pch_iir);
547b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, gt_iir);
548b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, de_iir);
549b1f14ad0SJesse Barnes 	I915_WRITE(GEN6_PMIIR, pm_iir);
550b1f14ad0SJesse Barnes 
551b1f14ad0SJesse Barnes done:
552b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
553b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
554b1f14ad0SJesse Barnes 
555b1f14ad0SJesse Barnes 	return ret;
556b1f14ad0SJesse Barnes }
557b1f14ad0SJesse Barnes 
558f71d4af4SJesse Barnes static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
559036a4a7dSZhenyu Wang {
5604697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
561036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
562036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
5633b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
5642d7b8366SYuanhan Liu 	u32 hotplug_mask;
565036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
566881f47b6SXiang, Haihao 	u32 bsd_usr_interrupt = GT_BSD_USER_INTERRUPT;
567881f47b6SXiang, Haihao 
5684697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5694697995bSJesse Barnes 
570881f47b6SXiang, Haihao 	if (IS_GEN6(dev))
571881f47b6SXiang, Haihao 		bsd_usr_interrupt = GT_GEN6_BSD_USER_INTERRUPT;
572036a4a7dSZhenyu Wang 
5732d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
5742d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
5752d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
5763143a2bfSChris Wilson 	POSTING_READ(DEIER);
5772d109a84SZou, Nanhai 
578036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
579036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
580c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
5813b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
582036a4a7dSZhenyu Wang 
5833b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
5843b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
585c7c85101SZou Nan hai 		goto done;
586036a4a7dSZhenyu Wang 
5872d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
5882d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
5892d7b8366SYuanhan Liu 	else
5902d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
5912d7b8366SYuanhan Liu 
592036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
593036a4a7dSZhenyu Wang 
594036a4a7dSZhenyu Wang 	if (dev->primary->master) {
595036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
596036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
597036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
598036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
599036a4a7dSZhenyu Wang 	}
600036a4a7dSZhenyu Wang 
601c6df541cSChris Wilson 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
6021ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[RCS]);
603881f47b6SXiang, Haihao 	if (gt_iir & bsd_usr_interrupt)
6041ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[VCS]);
6051ec14ad3SChris Wilson 	if (gt_iir & GT_BLT_USER_INTERRUPT)
6061ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[BCS]);
607036a4a7dSZhenyu Wang 
60801c66889SZhao Yakui 	if (de_iir & DE_GSE)
6093b617967SChris Wilson 		intel_opregion_gse_intr(dev);
61001c66889SZhao Yakui 
611f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
612013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
6132bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
614013d5aa2SJesse Barnes 	}
615013d5aa2SJesse Barnes 
616f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
617f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
6182bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
619013d5aa2SJesse Barnes 	}
620c062df61SLi Peng 
621f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
622f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
623f072d2e7SZhenyu Wang 
624f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
625f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
626f072d2e7SZhenyu Wang 
627c650156aSZhenyu Wang 	/* check event from PCH */
628776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
629776ad806SJesse Barnes 		if (pch_iir & hotplug_mask)
630c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
631776ad806SJesse Barnes 		pch_irq_handler(dev);
632776ad806SJesse Barnes 	}
633c650156aSZhenyu Wang 
634f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
6357648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
636f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
637f97108d1SJesse Barnes 	}
638f97108d1SJesse Barnes 
6394912d041SBen Widawsky 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS) {
6404912d041SBen Widawsky 		/*
6414912d041SBen Widawsky 		 * IIR bits should never already be set because IMR should
6424912d041SBen Widawsky 		 * prevent an interrupt from being shown in IIR. The warning
6434912d041SBen Widawsky 		 * displays a case where we've unsafely cleared
6444912d041SBen Widawsky 		 * dev_priv->pm_iir. Although missing an interrupt of the same
6454912d041SBen Widawsky 		 * type is not a problem, it displays a problem in the logic.
6464912d041SBen Widawsky 		 *
6474912d041SBen Widawsky 		 * The mask bit in IMR is cleared by rps_work.
6484912d041SBen Widawsky 		 */
6494912d041SBen Widawsky 		unsigned long flags;
6504912d041SBen Widawsky 		spin_lock_irqsave(&dev_priv->rps_lock, flags);
6514912d041SBen Widawsky 		WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
6524912d041SBen Widawsky 		I915_WRITE(GEN6_PMIMR, pm_iir);
6534912d041SBen Widawsky 		dev_priv->pm_iir |= pm_iir;
6544912d041SBen Widawsky 		spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
6554912d041SBen Widawsky 		queue_work(dev_priv->wq, &dev_priv->rps_work);
6564912d041SBen Widawsky 	}
6573b8d8d91SJesse Barnes 
658c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
659c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
660c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
661c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
6624912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
663036a4a7dSZhenyu Wang 
664c7c85101SZou Nan hai done:
6652d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
6663143a2bfSChris Wilson 	POSTING_READ(DEIER);
6672d109a84SZou, Nanhai 
668036a4a7dSZhenyu Wang 	return ret;
669036a4a7dSZhenyu Wang }
670036a4a7dSZhenyu Wang 
6718a905236SJesse Barnes /**
6728a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
6738a905236SJesse Barnes  * @work: work struct
6748a905236SJesse Barnes  *
6758a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
6768a905236SJesse Barnes  * was detected.
6778a905236SJesse Barnes  */
6788a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
6798a905236SJesse Barnes {
6808a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
6818a905236SJesse Barnes 						    error_work);
6828a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
683f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
684f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
685f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
6868a905236SJesse Barnes 
687f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
6888a905236SJesse Barnes 
689ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
69044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
691f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
692f803aa55SChris Wilson 		if (!i915_reset(dev, GRDOM_RENDER)) {
693ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
694f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
695f316a42cSBen Gamari 		}
69630dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
697f316a42cSBen Gamari 	}
6988a905236SJesse Barnes }
6998a905236SJesse Barnes 
7003bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
7019df30794SChris Wilson static struct drm_i915_error_object *
702bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
70305394f39SChris Wilson 			 struct drm_i915_gem_object *src)
7049df30794SChris Wilson {
7059df30794SChris Wilson 	struct drm_i915_error_object *dst;
7069df30794SChris Wilson 	int page, page_count;
707e56660ddSChris Wilson 	u32 reloc_offset;
7089df30794SChris Wilson 
70905394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
7109df30794SChris Wilson 		return NULL;
7119df30794SChris Wilson 
71205394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
7139df30794SChris Wilson 
7149df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof (u32 *), GFP_ATOMIC);
7159df30794SChris Wilson 	if (dst == NULL)
7169df30794SChris Wilson 		return NULL;
7179df30794SChris Wilson 
71805394f39SChris Wilson 	reloc_offset = src->gtt_offset;
7199df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
720788885aeSAndrew Morton 		unsigned long flags;
721e56660ddSChris Wilson 		void __iomem *s;
722e56660ddSChris Wilson 		void *d;
723788885aeSAndrew Morton 
724e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
7259df30794SChris Wilson 		if (d == NULL)
7269df30794SChris Wilson 			goto unwind;
727e56660ddSChris Wilson 
728788885aeSAndrew Morton 		local_irq_save(flags);
729e56660ddSChris Wilson 		s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
7303e4d3af5SPeter Zijlstra 					     reloc_offset);
731e56660ddSChris Wilson 		memcpy_fromio(d, s, PAGE_SIZE);
7323e4d3af5SPeter Zijlstra 		io_mapping_unmap_atomic(s);
733788885aeSAndrew Morton 		local_irq_restore(flags);
734e56660ddSChris Wilson 
7359df30794SChris Wilson 		dst->pages[page] = d;
736e56660ddSChris Wilson 
737e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
7389df30794SChris Wilson 	}
7399df30794SChris Wilson 	dst->page_count = page_count;
74005394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
7419df30794SChris Wilson 
7429df30794SChris Wilson 	return dst;
7439df30794SChris Wilson 
7449df30794SChris Wilson unwind:
7459df30794SChris Wilson 	while (page--)
7469df30794SChris Wilson 		kfree(dst->pages[page]);
7479df30794SChris Wilson 	kfree(dst);
7489df30794SChris Wilson 	return NULL;
7499df30794SChris Wilson }
7509df30794SChris Wilson 
7519df30794SChris Wilson static void
7529df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
7539df30794SChris Wilson {
7549df30794SChris Wilson 	int page;
7559df30794SChris Wilson 
7569df30794SChris Wilson 	if (obj == NULL)
7579df30794SChris Wilson 		return;
7589df30794SChris Wilson 
7599df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
7609df30794SChris Wilson 		kfree(obj->pages[page]);
7619df30794SChris Wilson 
7629df30794SChris Wilson 	kfree(obj);
7639df30794SChris Wilson }
7649df30794SChris Wilson 
7659df30794SChris Wilson static void
7669df30794SChris Wilson i915_error_state_free(struct drm_device *dev,
7679df30794SChris Wilson 		      struct drm_i915_error_state *error)
7689df30794SChris Wilson {
769e2f973d5SChris Wilson 	int i;
770e2f973d5SChris Wilson 
771e2f973d5SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->batchbuffer); i++)
772e2f973d5SChris Wilson 		i915_error_object_free(error->batchbuffer[i]);
773e2f973d5SChris Wilson 
774e2f973d5SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ringbuffer); i++)
775e2f973d5SChris Wilson 		i915_error_object_free(error->ringbuffer[i]);
776e2f973d5SChris Wilson 
7779df30794SChris Wilson 	kfree(error->active_bo);
7786ef3d427SChris Wilson 	kfree(error->overlay);
7799df30794SChris Wilson 	kfree(error);
7809df30794SChris Wilson }
7819df30794SChris Wilson 
782c724e8a9SChris Wilson static u32 capture_bo_list(struct drm_i915_error_buffer *err,
783c724e8a9SChris Wilson 			   int count,
784c724e8a9SChris Wilson 			   struct list_head *head)
785c724e8a9SChris Wilson {
786c724e8a9SChris Wilson 	struct drm_i915_gem_object *obj;
787c724e8a9SChris Wilson 	int i = 0;
788c724e8a9SChris Wilson 
789c724e8a9SChris Wilson 	list_for_each_entry(obj, head, mm_list) {
790c724e8a9SChris Wilson 		err->size = obj->base.size;
791c724e8a9SChris Wilson 		err->name = obj->base.name;
792c724e8a9SChris Wilson 		err->seqno = obj->last_rendering_seqno;
793c724e8a9SChris Wilson 		err->gtt_offset = obj->gtt_offset;
794c724e8a9SChris Wilson 		err->read_domains = obj->base.read_domains;
795c724e8a9SChris Wilson 		err->write_domain = obj->base.write_domain;
796c724e8a9SChris Wilson 		err->fence_reg = obj->fence_reg;
797c724e8a9SChris Wilson 		err->pinned = 0;
798c724e8a9SChris Wilson 		if (obj->pin_count > 0)
799c724e8a9SChris Wilson 			err->pinned = 1;
800c724e8a9SChris Wilson 		if (obj->user_pin_count > 0)
801c724e8a9SChris Wilson 			err->pinned = -1;
802c724e8a9SChris Wilson 		err->tiling = obj->tiling_mode;
803c724e8a9SChris Wilson 		err->dirty = obj->dirty;
804c724e8a9SChris Wilson 		err->purgeable = obj->madv != I915_MADV_WILLNEED;
8053685092bSChris Wilson 		err->ring = obj->ring ? obj->ring->id : 0;
80693dfb40cSChris Wilson 		err->cache_level = obj->cache_level;
807c724e8a9SChris Wilson 
808c724e8a9SChris Wilson 		if (++i == count)
809c724e8a9SChris Wilson 			break;
810c724e8a9SChris Wilson 
811c724e8a9SChris Wilson 		err++;
812c724e8a9SChris Wilson 	}
813c724e8a9SChris Wilson 
814c724e8a9SChris Wilson 	return i;
815c724e8a9SChris Wilson }
816c724e8a9SChris Wilson 
817748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
818748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
819748ebc60SChris Wilson {
820748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
821748ebc60SChris Wilson 	int i;
822748ebc60SChris Wilson 
823748ebc60SChris Wilson 	/* Fences */
824748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
825748ebc60SChris Wilson 	case 6:
826748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
827748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
828748ebc60SChris Wilson 		break;
829748ebc60SChris Wilson 	case 5:
830748ebc60SChris Wilson 	case 4:
831748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
832748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
833748ebc60SChris Wilson 		break;
834748ebc60SChris Wilson 	case 3:
835748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
836748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
837748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
838748ebc60SChris Wilson 	case 2:
839748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
840748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
841748ebc60SChris Wilson 		break;
842748ebc60SChris Wilson 
843748ebc60SChris Wilson 	}
844748ebc60SChris Wilson }
845748ebc60SChris Wilson 
846bcfb2e28SChris Wilson static struct drm_i915_error_object *
847bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
848bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
849bcfb2e28SChris Wilson {
850bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
851bcfb2e28SChris Wilson 	u32 seqno;
852bcfb2e28SChris Wilson 
853bcfb2e28SChris Wilson 	if (!ring->get_seqno)
854bcfb2e28SChris Wilson 		return NULL;
855bcfb2e28SChris Wilson 
856bcfb2e28SChris Wilson 	seqno = ring->get_seqno(ring);
857bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
858bcfb2e28SChris Wilson 		if (obj->ring != ring)
859bcfb2e28SChris Wilson 			continue;
860bcfb2e28SChris Wilson 
861c37d9a5dSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
862bcfb2e28SChris Wilson 			continue;
863bcfb2e28SChris Wilson 
864bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
865bcfb2e28SChris Wilson 			continue;
866bcfb2e28SChris Wilson 
867bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
868bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
869bcfb2e28SChris Wilson 		 */
870bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
871bcfb2e28SChris Wilson 	}
872bcfb2e28SChris Wilson 
873bcfb2e28SChris Wilson 	return NULL;
874bcfb2e28SChris Wilson }
875bcfb2e28SChris Wilson 
8768a905236SJesse Barnes /**
8778a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
8788a905236SJesse Barnes  * @dev: drm device
8798a905236SJesse Barnes  *
8808a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
8818a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
8828a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
8838a905236SJesse Barnes  * to pick up.
8848a905236SJesse Barnes  */
88563eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
88663eeaf38SJesse Barnes {
88763eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
88805394f39SChris Wilson 	struct drm_i915_gem_object *obj;
88963eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
89063eeaf38SJesse Barnes 	unsigned long flags;
8919db4a9c7SJesse Barnes 	int i, pipe;
89263eeaf38SJesse Barnes 
89363eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
8949df30794SChris Wilson 	error = dev_priv->first_error;
8959df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
8969df30794SChris Wilson 	if (error)
8979df30794SChris Wilson 		return;
89863eeaf38SJesse Barnes 
8999db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
90063eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
90163eeaf38SJesse Barnes 	if (!error) {
9029df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
9039df30794SChris Wilson 		return;
90463eeaf38SJesse Barnes 	}
90563eeaf38SJesse Barnes 
906b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
907b6f7833bSChris Wilson 		 dev->primary->index);
9082fa772f3SChris Wilson 
9091ec14ad3SChris Wilson 	error->seqno = dev_priv->ring[RCS].get_seqno(&dev_priv->ring[RCS]);
91063eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
91163eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
9129db4a9c7SJesse Barnes 	for_each_pipe(pipe)
9139db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
91463eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
915f406839fSChris Wilson 	error->error = 0;
916f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 6) {
917f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
918add354ddSChris Wilson 
9191d8f38f4SChris Wilson 		error->bcs_acthd = I915_READ(BCS_ACTHD);
9201d8f38f4SChris Wilson 		error->bcs_ipehr = I915_READ(BCS_IPEHR);
9211d8f38f4SChris Wilson 		error->bcs_ipeir = I915_READ(BCS_IPEIR);
9221d8f38f4SChris Wilson 		error->bcs_instdone = I915_READ(BCS_INSTDONE);
9231d8f38f4SChris Wilson 		error->bcs_seqno = 0;
9241ec14ad3SChris Wilson 		if (dev_priv->ring[BCS].get_seqno)
9251ec14ad3SChris Wilson 			error->bcs_seqno = dev_priv->ring[BCS].get_seqno(&dev_priv->ring[BCS]);
926add354ddSChris Wilson 
927add354ddSChris Wilson 		error->vcs_acthd = I915_READ(VCS_ACTHD);
928add354ddSChris Wilson 		error->vcs_ipehr = I915_READ(VCS_IPEHR);
929add354ddSChris Wilson 		error->vcs_ipeir = I915_READ(VCS_IPEIR);
930add354ddSChris Wilson 		error->vcs_instdone = I915_READ(VCS_INSTDONE);
931add354ddSChris Wilson 		error->vcs_seqno = 0;
9321ec14ad3SChris Wilson 		if (dev_priv->ring[VCS].get_seqno)
9331ec14ad3SChris Wilson 			error->vcs_seqno = dev_priv->ring[VCS].get_seqno(&dev_priv->ring[VCS]);
934f406839fSChris Wilson 	}
935f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
93663eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
93763eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
93863eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
93963eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
94063eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
94163eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
9429df30794SChris Wilson 		error->bbaddr = I915_READ64(BB_ADDR);
943f406839fSChris Wilson 	} else {
944f406839fSChris Wilson 		error->ipeir = I915_READ(IPEIR);
945f406839fSChris Wilson 		error->ipehr = I915_READ(IPEHR);
946f406839fSChris Wilson 		error->instdone = I915_READ(INSTDONE);
947f406839fSChris Wilson 		error->acthd = I915_READ(ACTHD);
948f406839fSChris Wilson 		error->bbaddr = 0;
9499df30794SChris Wilson 	}
950748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
9519df30794SChris Wilson 
952e2f973d5SChris Wilson 	/* Record the active batch and ring buffers */
953e2f973d5SChris Wilson 	for (i = 0; i < I915_NUM_RINGS; i++) {
954bcfb2e28SChris Wilson 		error->batchbuffer[i] =
955bcfb2e28SChris Wilson 			i915_error_first_batchbuffer(dev_priv,
956bcfb2e28SChris Wilson 						     &dev_priv->ring[i]);
9579df30794SChris Wilson 
958e2f973d5SChris Wilson 		error->ringbuffer[i] =
959e2f973d5SChris Wilson 			i915_error_object_create(dev_priv,
960e2f973d5SChris Wilson 						 dev_priv->ring[i].obj);
961e2f973d5SChris Wilson 	}
9629df30794SChris Wilson 
963c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
9649df30794SChris Wilson 	error->active_bo = NULL;
965c724e8a9SChris Wilson 	error->pinned_bo = NULL;
9669df30794SChris Wilson 
967bcfb2e28SChris Wilson 	i = 0;
968bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
969bcfb2e28SChris Wilson 		i++;
970bcfb2e28SChris Wilson 	error->active_bo_count = i;
97105394f39SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
972bcfb2e28SChris Wilson 		i++;
973bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
974c724e8a9SChris Wilson 
9758e934dbfSChris Wilson 	error->active_bo = NULL;
9768e934dbfSChris Wilson 	error->pinned_bo = NULL;
977bcfb2e28SChris Wilson 	if (i) {
978bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
9799df30794SChris Wilson 					   GFP_ATOMIC);
980c724e8a9SChris Wilson 		if (error->active_bo)
981c724e8a9SChris Wilson 			error->pinned_bo =
982c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
9839df30794SChris Wilson 	}
984c724e8a9SChris Wilson 
985c724e8a9SChris Wilson 	if (error->active_bo)
986c724e8a9SChris Wilson 		error->active_bo_count =
987c724e8a9SChris Wilson 			capture_bo_list(error->active_bo,
988c724e8a9SChris Wilson 					error->active_bo_count,
989c724e8a9SChris Wilson 					&dev_priv->mm.active_list);
990c724e8a9SChris Wilson 
991c724e8a9SChris Wilson 	if (error->pinned_bo)
992c724e8a9SChris Wilson 		error->pinned_bo_count =
993c724e8a9SChris Wilson 			capture_bo_list(error->pinned_bo,
994c724e8a9SChris Wilson 					error->pinned_bo_count,
995c724e8a9SChris Wilson 					&dev_priv->mm.pinned_list);
99663eeaf38SJesse Barnes 
9978a905236SJesse Barnes 	do_gettimeofday(&error->time);
9988a905236SJesse Barnes 
9996ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1000c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
10016ef3d427SChris Wilson 
10029df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
10039df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
100463eeaf38SJesse Barnes 		dev_priv->first_error = error;
10059df30794SChris Wilson 		error = NULL;
10069df30794SChris Wilson 	}
100763eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
10089df30794SChris Wilson 
10099df30794SChris Wilson 	if (error)
10109df30794SChris Wilson 		i915_error_state_free(dev, error);
10119df30794SChris Wilson }
10129df30794SChris Wilson 
10139df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
10149df30794SChris Wilson {
10159df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
10169df30794SChris Wilson 	struct drm_i915_error_state *error;
10179df30794SChris Wilson 
10189df30794SChris Wilson 	spin_lock(&dev_priv->error_lock);
10199df30794SChris Wilson 	error = dev_priv->first_error;
10209df30794SChris Wilson 	dev_priv->first_error = NULL;
10219df30794SChris Wilson 	spin_unlock(&dev_priv->error_lock);
10229df30794SChris Wilson 
10239df30794SChris Wilson 	if (error)
10249df30794SChris Wilson 		i915_error_state_free(dev, error);
102563eeaf38SJesse Barnes }
10263bd3c932SChris Wilson #else
10273bd3c932SChris Wilson #define i915_capture_error_state(x)
10283bd3c932SChris Wilson #endif
102963eeaf38SJesse Barnes 
103035aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1031c0e09200SDave Airlie {
10328a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
103363eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
10349db4a9c7SJesse Barnes 	int pipe;
103563eeaf38SJesse Barnes 
103635aed2e6SChris Wilson 	if (!eir)
103735aed2e6SChris Wilson 		return;
103863eeaf38SJesse Barnes 
103963eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
104063eeaf38SJesse Barnes 	       eir);
10418a905236SJesse Barnes 
10428a905236SJesse Barnes 	if (IS_G4X(dev)) {
10438a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
10448a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
10458a905236SJesse Barnes 
10468a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
10478a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
10488a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
10498a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
10508a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
10518a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
10528a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
10538a905236SJesse Barnes 			       I915_READ(INSTPS));
10548a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
10558a905236SJesse Barnes 			       I915_READ(INSTDONE1));
10568a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
10578a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
10588a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
10593143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
10608a905236SJesse Barnes 		}
10618a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
10628a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
10638a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
10648a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
10658a905236SJesse Barnes 			       pgtbl_err);
10668a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
10673143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
10688a905236SJesse Barnes 		}
10698a905236SJesse Barnes 	}
10708a905236SJesse Barnes 
1071a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
107263eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
107363eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
107463eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
107563eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
107663eeaf38SJesse Barnes 			       pgtbl_err);
107763eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
10783143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
107963eeaf38SJesse Barnes 		}
10808a905236SJesse Barnes 	}
10818a905236SJesse Barnes 
108263eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
10839db4a9c7SJesse Barnes 		printk(KERN_ERR "memory refresh error:\n");
10849db4a9c7SJesse Barnes 		for_each_pipe(pipe)
10859db4a9c7SJesse Barnes 			printk(KERN_ERR "pipe %c stat: 0x%08x\n",
10869db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
108763eeaf38SJesse Barnes 		/* pipestat has already been acked */
108863eeaf38SJesse Barnes 	}
108963eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
109063eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
109163eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
109263eeaf38SJesse Barnes 		       I915_READ(INSTPM));
1093a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
109463eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
109563eeaf38SJesse Barnes 
109663eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
109763eeaf38SJesse Barnes 			       I915_READ(IPEIR));
109863eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
109963eeaf38SJesse Barnes 			       I915_READ(IPEHR));
110063eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
110163eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
110263eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
110363eeaf38SJesse Barnes 			       I915_READ(ACTHD));
110463eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
11053143a2bfSChris Wilson 			POSTING_READ(IPEIR);
110663eeaf38SJesse Barnes 		} else {
110763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
110863eeaf38SJesse Barnes 
110963eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
111063eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
111163eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
111263eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
111363eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
111463eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
111563eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
111663eeaf38SJesse Barnes 			       I915_READ(INSTPS));
111763eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
111863eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
111963eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
112063eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
112163eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
11223143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
112363eeaf38SJesse Barnes 		}
112463eeaf38SJesse Barnes 	}
112563eeaf38SJesse Barnes 
112663eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
11273143a2bfSChris Wilson 	POSTING_READ(EIR);
112863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
112963eeaf38SJesse Barnes 	if (eir) {
113063eeaf38SJesse Barnes 		/*
113163eeaf38SJesse Barnes 		 * some errors might have become stuck,
113263eeaf38SJesse Barnes 		 * mask them.
113363eeaf38SJesse Barnes 		 */
113463eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
113563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
113663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
113763eeaf38SJesse Barnes 	}
113835aed2e6SChris Wilson }
113935aed2e6SChris Wilson 
114035aed2e6SChris Wilson /**
114135aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
114235aed2e6SChris Wilson  * @dev: drm device
114335aed2e6SChris Wilson  *
114435aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
114535aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
114635aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
114735aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
114835aed2e6SChris Wilson  * of a ring dump etc.).
114935aed2e6SChris Wilson  */
1150527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
115135aed2e6SChris Wilson {
115235aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
115335aed2e6SChris Wilson 
115435aed2e6SChris Wilson 	i915_capture_error_state(dev);
115535aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
11568a905236SJesse Barnes 
1157ba1234d1SBen Gamari 	if (wedged) {
115830dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1159ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1160ba1234d1SBen Gamari 
116111ed50ecSBen Gamari 		/*
116211ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
116311ed50ecSBen Gamari 		 */
11641ec14ad3SChris Wilson 		wake_up_all(&dev_priv->ring[RCS].irq_queue);
1165f787a5f5SChris Wilson 		if (HAS_BSD(dev))
11661ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[VCS].irq_queue);
1167549f7365SChris Wilson 		if (HAS_BLT(dev))
11681ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[BCS].irq_queue);
116911ed50ecSBen Gamari 	}
117011ed50ecSBen Gamari 
11719c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
11728a905236SJesse Barnes }
11738a905236SJesse Barnes 
11744e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
11754e5359cdSSimon Farnsworth {
11764e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
11774e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11784e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
117905394f39SChris Wilson 	struct drm_i915_gem_object *obj;
11804e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
11814e5359cdSSimon Farnsworth 	unsigned long flags;
11824e5359cdSSimon Farnsworth 	bool stall_detected;
11834e5359cdSSimon Farnsworth 
11844e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
11854e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
11864e5359cdSSimon Farnsworth 		return;
11874e5359cdSSimon Farnsworth 
11884e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
11894e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
11904e5359cdSSimon Farnsworth 
11914e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
11924e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
11934e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
11944e5359cdSSimon Farnsworth 		return;
11954e5359cdSSimon Farnsworth 	}
11964e5359cdSSimon Farnsworth 
11974e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
119805394f39SChris Wilson 	obj = work->pending_flip_obj;
1199a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
12009db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
120105394f39SChris Wilson 		stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
12024e5359cdSSimon Farnsworth 	} else {
12039db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
120405394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
12054e5359cdSSimon Farnsworth 							crtc->y * crtc->fb->pitch +
12064e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
12074e5359cdSSimon Farnsworth 	}
12084e5359cdSSimon Farnsworth 
12094e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
12104e5359cdSSimon Farnsworth 
12114e5359cdSSimon Farnsworth 	if (stall_detected) {
12124e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
12134e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
12144e5359cdSSimon Farnsworth 	}
12154e5359cdSSimon Farnsworth }
12164e5359cdSSimon Farnsworth 
1217f71d4af4SJesse Barnes static irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
12188a905236SJesse Barnes {
12198a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
12208a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
12218a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
12228a905236SJesse Barnes 	u32 iir, new_iir;
12239db4a9c7SJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
12248a905236SJesse Barnes 	u32 vblank_status;
12258a905236SJesse Barnes 	int vblank = 0;
12268a905236SJesse Barnes 	unsigned long irqflags;
12278a905236SJesse Barnes 	int irq_received;
12289db4a9c7SJesse Barnes 	int ret = IRQ_NONE, pipe;
12299db4a9c7SJesse Barnes 	bool blc_event = false;
12308a905236SJesse Barnes 
12318a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
12328a905236SJesse Barnes 
12338a905236SJesse Barnes 	iir = I915_READ(IIR);
12348a905236SJesse Barnes 
1235a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
1236d874bcffSJesse Barnes 		vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
1237e25e6601SJesse Barnes 	else
1238d874bcffSJesse Barnes 		vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
12398a905236SJesse Barnes 
12408a905236SJesse Barnes 	for (;;) {
12418a905236SJesse Barnes 		irq_received = iir != 0;
12428a905236SJesse Barnes 
12438a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
12448a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
12458a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
12468a905236SJesse Barnes 		 * interrupts (for non-MSI).
12478a905236SJesse Barnes 		 */
12481ec14ad3SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
12498a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
1250ba1234d1SBen Gamari 			i915_handle_error(dev, false);
12518a905236SJesse Barnes 
12529db4a9c7SJesse Barnes 		for_each_pipe(pipe) {
12539db4a9c7SJesse Barnes 			int reg = PIPESTAT(pipe);
12549db4a9c7SJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
12559db4a9c7SJesse Barnes 
12568a905236SJesse Barnes 			/*
12579db4a9c7SJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
12588a905236SJesse Barnes 			 */
12599db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
12609db4a9c7SJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
12619db4a9c7SJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
12629db4a9c7SJesse Barnes 							 pipe_name(pipe));
12639db4a9c7SJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
12648a905236SJesse Barnes 				irq_received = 1;
12658a905236SJesse Barnes 			}
12668a905236SJesse Barnes 		}
12671ec14ad3SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
12688a905236SJesse Barnes 
12698a905236SJesse Barnes 		if (!irq_received)
12708a905236SJesse Barnes 			break;
12718a905236SJesse Barnes 
12728a905236SJesse Barnes 		ret = IRQ_HANDLED;
12738a905236SJesse Barnes 
12748a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
12758a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
12768a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
12778a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
12788a905236SJesse Barnes 
127944d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
12808a905236SJesse Barnes 				  hotplug_status);
12818a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
12829c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
12839c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
12848a905236SJesse Barnes 
12858a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
12868a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
128763eeaf38SJesse Barnes 		}
128863eeaf38SJesse Barnes 
1289673a394bSEric Anholt 		I915_WRITE(IIR, iir);
1290cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
12917c463586SKeith Packard 
12927c1c2871SDave Airlie 		if (dev->primary->master) {
12937c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
12947c1c2871SDave Airlie 			if (master_priv->sarea_priv)
12957c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
1296c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
12977c1c2871SDave Airlie 		}
12980a3e67a4SJesse Barnes 
1299549f7365SChris Wilson 		if (iir & I915_USER_INTERRUPT)
13001ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
13011ec14ad3SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
13021ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
1303d1b851fcSZou Nan hai 
13041afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
13056b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
13061afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
13071afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 0);
13081afe3e9dSJesse Barnes 		}
13096b95a207SKristian Høgsberg 
13101afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
131170565d00SJesse Barnes 			intel_prepare_page_flip(dev, 1);
13121afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
13131afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 1);
13141afe3e9dSJesse Barnes 		}
13156b95a207SKristian Høgsberg 
13169db4a9c7SJesse Barnes 		for_each_pipe(pipe) {
13179db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & vblank_status &&
13189db4a9c7SJesse Barnes 			    drm_handle_vblank(dev, pipe)) {
13197c463586SKeith Packard 				vblank++;
13204e5359cdSSimon Farnsworth 				if (!dev_priv->flip_pending_is_done) {
13219db4a9c7SJesse Barnes 					i915_pageflip_stall_check(dev, pipe);
13229db4a9c7SJesse Barnes 					intel_finish_page_flip(dev, pipe);
13237c463586SKeith Packard 				}
13244e5359cdSSimon Farnsworth 			}
13257c463586SKeith Packard 
13269db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
13279db4a9c7SJesse Barnes 				blc_event = true;
13284e5359cdSSimon Farnsworth 		}
13297c463586SKeith Packard 
13309db4a9c7SJesse Barnes 
13319db4a9c7SJesse Barnes 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
13323b617967SChris Wilson 			intel_opregion_asle_intr(dev);
13330a3e67a4SJesse Barnes 
1334cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
1335cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
1336cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
1337cdfbc41fSEric Anholt 		 * we would never get another interrupt.
1338cdfbc41fSEric Anholt 		 *
1339cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
1340cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
1341cdfbc41fSEric Anholt 		 * another one.
1342cdfbc41fSEric Anholt 		 *
1343cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
1344cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
1345cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
1346cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
1347cdfbc41fSEric Anholt 		 * stray interrupts.
1348cdfbc41fSEric Anholt 		 */
1349cdfbc41fSEric Anholt 		iir = new_iir;
135005eff845SKeith Packard 	}
1351cdfbc41fSEric Anholt 
135205eff845SKeith Packard 	return ret;
1353c0e09200SDave Airlie }
1354c0e09200SDave Airlie 
1355c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
1356c0e09200SDave Airlie {
1357c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
13587c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1359c0e09200SDave Airlie 
1360c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
1361c0e09200SDave Airlie 
136244d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
1363c0e09200SDave Airlie 
1364c99b058fSKristian Høgsberg 	dev_priv->counter++;
1365c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
1366c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
13677c1c2871SDave Airlie 	if (master_priv->sarea_priv)
13687c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
1369c0e09200SDave Airlie 
1370e1f99ce6SChris Wilson 	if (BEGIN_LP_RING(4) == 0) {
1371585fb111SJesse Barnes 		OUT_RING(MI_STORE_DWORD_INDEX);
13720baf823aSKeith Packard 		OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1373c0e09200SDave Airlie 		OUT_RING(dev_priv->counter);
1374585fb111SJesse Barnes 		OUT_RING(MI_USER_INTERRUPT);
1375c0e09200SDave Airlie 		ADVANCE_LP_RING();
1376e1f99ce6SChris Wilson 	}
1377c0e09200SDave Airlie 
1378c0e09200SDave Airlie 	return dev_priv->counter;
1379c0e09200SDave Airlie }
1380c0e09200SDave Airlie 
1381c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
1382c0e09200SDave Airlie {
1383c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13847c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1385c0e09200SDave Airlie 	int ret = 0;
13861ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
1387c0e09200SDave Airlie 
138844d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
1389c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
1390c0e09200SDave Airlie 
1391ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
13927c1c2871SDave Airlie 		if (master_priv->sarea_priv)
13937c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
1394c0e09200SDave Airlie 		return 0;
1395ed4cb414SEric Anholt 	}
1396c0e09200SDave Airlie 
13977c1c2871SDave Airlie 	if (master_priv->sarea_priv)
13987c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1399c0e09200SDave Airlie 
1400b13c2b96SChris Wilson 	if (ring->irq_get(ring)) {
14011ec14ad3SChris Wilson 		DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1402c0e09200SDave Airlie 			    READ_BREADCRUMB(dev_priv) >= irq_nr);
14031ec14ad3SChris Wilson 		ring->irq_put(ring);
14045a9a8d1aSChris Wilson 	} else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
14055a9a8d1aSChris Wilson 		ret = -EBUSY;
1406c0e09200SDave Airlie 
1407c0e09200SDave Airlie 	if (ret == -EBUSY) {
1408c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
1409c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1410c0e09200SDave Airlie 	}
1411c0e09200SDave Airlie 
1412c0e09200SDave Airlie 	return ret;
1413c0e09200SDave Airlie }
1414c0e09200SDave Airlie 
1415c0e09200SDave Airlie /* Needs the lock as it touches the ring.
1416c0e09200SDave Airlie  */
1417c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
1418c0e09200SDave Airlie 			 struct drm_file *file_priv)
1419c0e09200SDave Airlie {
1420c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1421c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
1422c0e09200SDave Airlie 	int result;
1423c0e09200SDave Airlie 
14241ec14ad3SChris Wilson 	if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
1425c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1426c0e09200SDave Airlie 		return -EINVAL;
1427c0e09200SDave Airlie 	}
1428299eb93cSEric Anholt 
1429299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1430299eb93cSEric Anholt 
1431546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
1432c0e09200SDave Airlie 	result = i915_emit_irq(dev);
1433546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
1434c0e09200SDave Airlie 
1435c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
1436c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
1437c0e09200SDave Airlie 		return -EFAULT;
1438c0e09200SDave Airlie 	}
1439c0e09200SDave Airlie 
1440c0e09200SDave Airlie 	return 0;
1441c0e09200SDave Airlie }
1442c0e09200SDave Airlie 
1443c0e09200SDave Airlie /* Doesn't need the hardware lock.
1444c0e09200SDave Airlie  */
1445c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
1446c0e09200SDave Airlie 			 struct drm_file *file_priv)
1447c0e09200SDave Airlie {
1448c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1449c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
1450c0e09200SDave Airlie 
1451c0e09200SDave Airlie 	if (!dev_priv) {
1452c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1453c0e09200SDave Airlie 		return -EINVAL;
1454c0e09200SDave Airlie 	}
1455c0e09200SDave Airlie 
1456c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
1457c0e09200SDave Airlie }
1458c0e09200SDave Airlie 
145942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
146042f52ef8SKeith Packard  * we use as a pipe index
146142f52ef8SKeith Packard  */
1462f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
14630a3e67a4SJesse Barnes {
14640a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1465e9d21d7fSKeith Packard 	unsigned long irqflags;
146671e0ffa5SJesse Barnes 
14675eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
146871e0ffa5SJesse Barnes 		return -EINVAL;
14690a3e67a4SJesse Barnes 
14701ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1471f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
14727c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14737c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
14740a3e67a4SJesse Barnes 	else
14757c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14767c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
14778692d00eSChris Wilson 
14788692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
14798692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
14808692d00eSChris Wilson 		I915_WRITE(INSTPM, INSTPM_AGPBUSY_DIS << 16);
14811ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14828692d00eSChris Wilson 
14830a3e67a4SJesse Barnes 	return 0;
14840a3e67a4SJesse Barnes }
14850a3e67a4SJesse Barnes 
1486f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1487f796cf8fSJesse Barnes {
1488f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1489f796cf8fSJesse Barnes 	unsigned long irqflags;
1490f796cf8fSJesse Barnes 
1491f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1492f796cf8fSJesse Barnes 		return -EINVAL;
1493f796cf8fSJesse Barnes 
1494f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1495f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1496f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1497f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1498f796cf8fSJesse Barnes 
1499f796cf8fSJesse Barnes 	return 0;
1500f796cf8fSJesse Barnes }
1501f796cf8fSJesse Barnes 
1502f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1503b1f14ad0SJesse Barnes {
1504b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1505b1f14ad0SJesse Barnes 	unsigned long irqflags;
1506b1f14ad0SJesse Barnes 
1507b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1508b1f14ad0SJesse Barnes 		return -EINVAL;
1509b1f14ad0SJesse Barnes 
1510b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1511b1f14ad0SJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1512b1f14ad0SJesse Barnes 				    DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1513b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1514b1f14ad0SJesse Barnes 
1515b1f14ad0SJesse Barnes 	return 0;
1516b1f14ad0SJesse Barnes }
1517b1f14ad0SJesse Barnes 
151842f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
151942f52ef8SKeith Packard  * we use as a pipe index
152042f52ef8SKeith Packard  */
1521f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
15220a3e67a4SJesse Barnes {
15230a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1524e9d21d7fSKeith Packard 	unsigned long irqflags;
15250a3e67a4SJesse Barnes 
15261ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15278692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15288692d00eSChris Wilson 		I915_WRITE(INSTPM,
15298692d00eSChris Wilson 			   INSTPM_AGPBUSY_DIS << 16 | INSTPM_AGPBUSY_DIS);
15308692d00eSChris Wilson 
15317c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
15327c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
15337c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
15341ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15350a3e67a4SJesse Barnes }
15360a3e67a4SJesse Barnes 
1537f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1538f796cf8fSJesse Barnes {
1539f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1540f796cf8fSJesse Barnes 	unsigned long irqflags;
1541f796cf8fSJesse Barnes 
1542f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1543f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1544f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1545f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1546f796cf8fSJesse Barnes }
1547f796cf8fSJesse Barnes 
1548f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1549b1f14ad0SJesse Barnes {
1550b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1551b1f14ad0SJesse Barnes 	unsigned long irqflags;
1552b1f14ad0SJesse Barnes 
1553b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1554b1f14ad0SJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1555b1f14ad0SJesse Barnes 				     DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1556b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1557b1f14ad0SJesse Barnes }
1558b1f14ad0SJesse Barnes 
1559c0e09200SDave Airlie /* Set the vblank monitor pipe
1560c0e09200SDave Airlie  */
1561c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1562c0e09200SDave Airlie 			 struct drm_file *file_priv)
1563c0e09200SDave Airlie {
1564c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1565c0e09200SDave Airlie 
1566c0e09200SDave Airlie 	if (!dev_priv) {
1567c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1568c0e09200SDave Airlie 		return -EINVAL;
1569c0e09200SDave Airlie 	}
1570c0e09200SDave Airlie 
1571c0e09200SDave Airlie 	return 0;
1572c0e09200SDave Airlie }
1573c0e09200SDave Airlie 
1574c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1575c0e09200SDave Airlie 			 struct drm_file *file_priv)
1576c0e09200SDave Airlie {
1577c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1578c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
1579c0e09200SDave Airlie 
1580c0e09200SDave Airlie 	if (!dev_priv) {
1581c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1582c0e09200SDave Airlie 		return -EINVAL;
1583c0e09200SDave Airlie 	}
1584c0e09200SDave Airlie 
15850a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1586c0e09200SDave Airlie 
1587c0e09200SDave Airlie 	return 0;
1588c0e09200SDave Airlie }
1589c0e09200SDave Airlie 
1590c0e09200SDave Airlie /**
1591c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
1592c0e09200SDave Airlie  */
1593c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
1594c0e09200SDave Airlie 		     struct drm_file *file_priv)
1595c0e09200SDave Airlie {
1596bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1597bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1598bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1599bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1600bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1601bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1602bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1603bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1604bd95e0a4SEric Anholt 	 *
1605bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1606bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1607bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1608bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
16090a3e67a4SJesse Barnes 	 */
1610c0e09200SDave Airlie 	return -EINVAL;
1611c0e09200SDave Airlie }
1612c0e09200SDave Airlie 
1613893eead0SChris Wilson static u32
1614893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1615852835f3SZou Nan hai {
1616893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1617893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1618893eead0SChris Wilson }
1619893eead0SChris Wilson 
1620893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1621893eead0SChris Wilson {
1622893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1623893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1624893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
1625b2223497SChris Wilson 		if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
1626893eead0SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1627893eead0SChris Wilson 				  ring->name,
1628b2223497SChris Wilson 				  ring->waiting_seqno,
1629893eead0SChris Wilson 				  ring->get_seqno(ring));
1630893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1631893eead0SChris Wilson 			*err = true;
1632893eead0SChris Wilson 		}
1633893eead0SChris Wilson 		return true;
1634893eead0SChris Wilson 	}
1635893eead0SChris Wilson 	return false;
1636f65d9421SBen Gamari }
1637f65d9421SBen Gamari 
16381ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
16391ec14ad3SChris Wilson {
16401ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
16411ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
16421ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
16431ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
16441ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
16451ec14ad3SChris Wilson 			  ring->name);
16461ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
16471ec14ad3SChris Wilson 		return true;
16481ec14ad3SChris Wilson 	}
16491ec14ad3SChris Wilson 	if (IS_GEN6(dev) &&
16501ec14ad3SChris Wilson 	    (tmp & RING_WAIT_SEMAPHORE)) {
16511ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
16521ec14ad3SChris Wilson 			  ring->name);
16531ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
16541ec14ad3SChris Wilson 		return true;
16551ec14ad3SChris Wilson 	}
16561ec14ad3SChris Wilson 	return false;
16571ec14ad3SChris Wilson }
16581ec14ad3SChris Wilson 
1659f65d9421SBen Gamari /**
1660f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1661f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1662f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1663f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1664f65d9421SBen Gamari  */
1665f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1666f65d9421SBen Gamari {
1667f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1668f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1669cbb465e7SChris Wilson 	uint32_t acthd, instdone, instdone1;
1670893eead0SChris Wilson 	bool err = false;
1671893eead0SChris Wilson 
16723e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
16733e0dc6b0SBen Widawsky 		return;
16743e0dc6b0SBen Widawsky 
1675893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
16761ec14ad3SChris Wilson 	if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
16771ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
16781ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
1679893eead0SChris Wilson 		dev_priv->hangcheck_count = 0;
1680893eead0SChris Wilson 		if (err)
1681893eead0SChris Wilson 			goto repeat;
1682893eead0SChris Wilson 		return;
1683893eead0SChris Wilson 	}
1684f65d9421SBen Gamari 
1685a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1686f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
1687cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1688cbb465e7SChris Wilson 		instdone1 = 0;
1689cbb465e7SChris Wilson 	} else {
1690f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
1691cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1692cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1693cbb465e7SChris Wilson 	}
1694f65d9421SBen Gamari 
1695cbb465e7SChris Wilson 	if (dev_priv->last_acthd == acthd &&
1696cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1697cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1698cbb465e7SChris Wilson 		if (dev_priv->hangcheck_count++ > 1) {
1699f65d9421SBen Gamari 			DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
17008c80b59bSChris Wilson 
17018c80b59bSChris Wilson 			if (!IS_GEN2(dev)) {
17028c80b59bSChris Wilson 				/* Is the chip hanging on a WAIT_FOR_EVENT?
17038c80b59bSChris Wilson 				 * If so we can simply poke the RB_WAIT bit
17048c80b59bSChris Wilson 				 * and break the hang. This should work on
17058c80b59bSChris Wilson 				 * all but the second generation chipsets.
17068c80b59bSChris Wilson 				 */
17071ec14ad3SChris Wilson 
17081ec14ad3SChris Wilson 				if (kick_ring(&dev_priv->ring[RCS]))
1709893eead0SChris Wilson 					goto repeat;
17101ec14ad3SChris Wilson 
17111ec14ad3SChris Wilson 				if (HAS_BSD(dev) &&
17121ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[VCS]))
17131ec14ad3SChris Wilson 					goto repeat;
17141ec14ad3SChris Wilson 
17151ec14ad3SChris Wilson 				if (HAS_BLT(dev) &&
17161ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[BCS]))
17171ec14ad3SChris Wilson 					goto repeat;
17188c80b59bSChris Wilson 			}
17198c80b59bSChris Wilson 
1720ba1234d1SBen Gamari 			i915_handle_error(dev, true);
1721f65d9421SBen Gamari 			return;
1722f65d9421SBen Gamari 		}
1723cbb465e7SChris Wilson 	} else {
1724cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1725cbb465e7SChris Wilson 
1726cbb465e7SChris Wilson 		dev_priv->last_acthd = acthd;
1727cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1728cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1729cbb465e7SChris Wilson 	}
1730f65d9421SBen Gamari 
1731893eead0SChris Wilson repeat:
1732f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1733b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1734b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1735f65d9421SBen Gamari }
1736f65d9421SBen Gamari 
1737c0e09200SDave Airlie /* drm_dma.h hooks
1738c0e09200SDave Airlie */
1739f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
1740036a4a7dSZhenyu Wang {
1741036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1742036a4a7dSZhenyu Wang 
17434697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17444697995bSJesse Barnes 
17454697995bSJesse Barnes 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
17464697995bSJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
17479e3c256dSJesse Barnes 	if (IS_GEN6(dev) || IS_IVYBRIDGE(dev))
17489e3c256dSJesse Barnes 		INIT_WORK(&dev_priv->rps_work, gen6_pm_rps_work);
17494697995bSJesse Barnes 
1750036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
17512b1ecb73SJesse Barnes 	if (IS_GEN6(dev) || IS_GEN7(dev)) {
1752498e720bSDaniel J Blueman 		/* Workaround stalls observed on Sandy Bridge GPUs by
1753498e720bSDaniel J Blueman 		 * making the blitter command streamer generate a
1754498e720bSDaniel J Blueman 		 * write to the Hardware Status Page for
1755498e720bSDaniel J Blueman 		 * MI_USER_INTERRUPT.  This appears to serialize the
1756498e720bSDaniel J Blueman 		 * previous seqno write out before the interrupt
1757498e720bSDaniel J Blueman 		 * happens.
1758498e720bSDaniel J Blueman 		 */
1759498e720bSDaniel J Blueman 		I915_WRITE(GEN6_BLITTER_HWSTAM, ~GEN6_BLITTER_USER_INTERRUPT);
1760ec6a890dSChris Wilson 		I915_WRITE(GEN6_BSD_HWSTAM, ~GEN6_BSD_USER_INTERRUPT);
1761498e720bSDaniel J Blueman 	}
1762036a4a7dSZhenyu Wang 
1763036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1764036a4a7dSZhenyu Wang 
1765036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1766036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
17673143a2bfSChris Wilson 	POSTING_READ(DEIER);
1768036a4a7dSZhenyu Wang 
1769036a4a7dSZhenyu Wang 	/* and GT */
1770036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1771036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
17723143a2bfSChris Wilson 	POSTING_READ(GTIER);
1773c650156aSZhenyu Wang 
1774c650156aSZhenyu Wang 	/* south display irq */
1775c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1776c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
17773143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1778036a4a7dSZhenyu Wang }
1779036a4a7dSZhenyu Wang 
17807fe0b973SKeith Packard /*
17817fe0b973SKeith Packard  * Enable digital hotplug on the PCH, and configure the DP short pulse
17827fe0b973SKeith Packard  * duration to 2ms (which is the minimum in the Display Port spec)
17837fe0b973SKeith Packard  *
17847fe0b973SKeith Packard  * This register is the same on all known PCH chips.
17857fe0b973SKeith Packard  */
17867fe0b973SKeith Packard 
17877fe0b973SKeith Packard static void ironlake_enable_pch_hotplug(struct drm_device *dev)
17887fe0b973SKeith Packard {
17897fe0b973SKeith Packard 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17907fe0b973SKeith Packard 	u32	hotplug;
17917fe0b973SKeith Packard 
17927fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
17937fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
17947fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
17957fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
17967fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
17977fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
17987fe0b973SKeith Packard }
17997fe0b973SKeith Packard 
1800f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
1801036a4a7dSZhenyu Wang {
1802036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1803036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1804013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1805013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
18061ec14ad3SChris Wilson 	u32 render_irqs;
18072d7b8366SYuanhan Liu 	u32 hotplug_mask;
1808036a4a7dSZhenyu Wang 
18094697995bSJesse Barnes 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
18104697995bSJesse Barnes 	if (HAS_BSD(dev))
18114697995bSJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
18124697995bSJesse Barnes 	if (HAS_BLT(dev))
18134697995bSJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
18144697995bSJesse Barnes 
18154697995bSJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
18161ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1817036a4a7dSZhenyu Wang 
1818036a4a7dSZhenyu Wang 	/* should always can generate irq */
1819036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
18201ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
18211ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
18223143a2bfSChris Wilson 	POSTING_READ(DEIER);
1823036a4a7dSZhenyu Wang 
18241ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1825036a4a7dSZhenyu Wang 
1826036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
18271ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1828881f47b6SXiang, Haihao 
18291ec14ad3SChris Wilson 	if (IS_GEN6(dev))
18301ec14ad3SChris Wilson 		render_irqs =
18311ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
18321ec14ad3SChris Wilson 			GT_GEN6_BSD_USER_INTERRUPT |
18331ec14ad3SChris Wilson 			GT_BLT_USER_INTERRUPT;
18341ec14ad3SChris Wilson 	else
18351ec14ad3SChris Wilson 		render_irqs =
183688f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1837c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
18381ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
18391ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
18403143a2bfSChris Wilson 	POSTING_READ(GTIER);
1841036a4a7dSZhenyu Wang 
18422d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
18439035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
18449035a97aSChris Wilson 				SDE_PORTB_HOTPLUG_CPT |
18459035a97aSChris Wilson 				SDE_PORTC_HOTPLUG_CPT |
18469035a97aSChris Wilson 				SDE_PORTD_HOTPLUG_CPT);
18472d7b8366SYuanhan Liu 	} else {
18489035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG |
18499035a97aSChris Wilson 				SDE_PORTB_HOTPLUG |
18509035a97aSChris Wilson 				SDE_PORTC_HOTPLUG |
18519035a97aSChris Wilson 				SDE_PORTD_HOTPLUG |
18529035a97aSChris Wilson 				SDE_AUX_MASK);
18532d7b8366SYuanhan Liu 	}
18542d7b8366SYuanhan Liu 
18551ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1856c650156aSZhenyu Wang 
1857c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
18581ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
18591ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
18603143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1861c650156aSZhenyu Wang 
18627fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
18637fe0b973SKeith Packard 
1864f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1865f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1866f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1867f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1868f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1869f97108d1SJesse Barnes 	}
1870f97108d1SJesse Barnes 
1871036a4a7dSZhenyu Wang 	return 0;
1872036a4a7dSZhenyu Wang }
1873036a4a7dSZhenyu Wang 
1874f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
1875b1f14ad0SJesse Barnes {
1876b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1877b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
1878b1f14ad0SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
1879b1f14ad0SJesse Barnes 		DE_PCH_EVENT_IVB | DE_PLANEA_FLIP_DONE_IVB |
1880b1f14ad0SJesse Barnes 		DE_PLANEB_FLIP_DONE_IVB;
1881b1f14ad0SJesse Barnes 	u32 render_irqs;
1882b1f14ad0SJesse Barnes 	u32 hotplug_mask;
1883b1f14ad0SJesse Barnes 
1884b1f14ad0SJesse Barnes 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
1885b1f14ad0SJesse Barnes 	if (HAS_BSD(dev))
1886b1f14ad0SJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
1887b1f14ad0SJesse Barnes 	if (HAS_BLT(dev))
1888b1f14ad0SJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
1889b1f14ad0SJesse Barnes 
1890b1f14ad0SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1891b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
1892b1f14ad0SJesse Barnes 
1893b1f14ad0SJesse Barnes 	/* should always can generate irq */
1894b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1895b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
1896b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK_IVB |
1897b1f14ad0SJesse Barnes 		   DE_PIPEB_VBLANK_IVB);
1898b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1899b1f14ad0SJesse Barnes 
1900b1f14ad0SJesse Barnes 	dev_priv->gt_irq_mask = ~0;
1901b1f14ad0SJesse Barnes 
1902b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1903b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1904b1f14ad0SJesse Barnes 
1905b1f14ad0SJesse Barnes 	render_irqs = GT_USER_INTERRUPT | GT_GEN6_BSD_USER_INTERRUPT |
1906b1f14ad0SJesse Barnes 		GT_BLT_USER_INTERRUPT;
1907b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
1908b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
1909b1f14ad0SJesse Barnes 
1910b1f14ad0SJesse Barnes 	hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
1911b1f14ad0SJesse Barnes 			SDE_PORTB_HOTPLUG_CPT |
1912b1f14ad0SJesse Barnes 			SDE_PORTC_HOTPLUG_CPT |
1913b1f14ad0SJesse Barnes 			SDE_PORTD_HOTPLUG_CPT);
1914b1f14ad0SJesse Barnes 	dev_priv->pch_irq_mask = ~hotplug_mask;
1915b1f14ad0SJesse Barnes 
1916b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1917b1f14ad0SJesse Barnes 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1918b1f14ad0SJesse Barnes 	I915_WRITE(SDEIER, hotplug_mask);
1919b1f14ad0SJesse Barnes 	POSTING_READ(SDEIER);
1920b1f14ad0SJesse Barnes 
19217fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
19227fe0b973SKeith Packard 
1923b1f14ad0SJesse Barnes 	return 0;
1924b1f14ad0SJesse Barnes }
1925b1f14ad0SJesse Barnes 
1926f71d4af4SJesse Barnes static void i915_driver_irq_preinstall(struct drm_device * dev)
1927c0e09200SDave Airlie {
1928c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19299db4a9c7SJesse Barnes 	int pipe;
1930c0e09200SDave Airlie 
193179e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
193279e53945SJesse Barnes 
1933036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
19348a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1935036a4a7dSZhenyu Wang 
19365ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
19375ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
19385ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
19395ca58282SJesse Barnes 	}
19405ca58282SJesse Barnes 
19410a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
19429db4a9c7SJesse Barnes 	for_each_pipe(pipe)
19439db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0);
19440a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1945ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
19463143a2bfSChris Wilson 	POSTING_READ(IER);
1947c0e09200SDave Airlie }
1948c0e09200SDave Airlie 
1949b01f2c3aSJesse Barnes /*
1950b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
1951b01f2c3aSJesse Barnes  * enabled correctly.
1952b01f2c3aSJesse Barnes  */
1953f71d4af4SJesse Barnes static int i915_driver_irq_postinstall(struct drm_device *dev)
1954c0e09200SDave Airlie {
1955c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19565ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
195763eeaf38SJesse Barnes 	u32 error_mask;
19580a3e67a4SJesse Barnes 
19590a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1960ed4cb414SEric Anholt 
19617c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
19621ec14ad3SChris Wilson 	dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
19638ee1c3dbSMatthew Garrett 
19647c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
19657c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
19667c463586SKeith Packard 
19675ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
1968c496fa1fSAdam Jackson 		/* Enable in IER... */
1969c496fa1fSAdam Jackson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1970c496fa1fSAdam Jackson 		/* and unmask in IMR */
19711ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
1972c496fa1fSAdam Jackson 	}
1973c496fa1fSAdam Jackson 
1974c496fa1fSAdam Jackson 	/*
1975c496fa1fSAdam Jackson 	 * Enable some error detection, note the instruction error mask
1976c496fa1fSAdam Jackson 	 * bit is reserved, so we leave it masked.
1977c496fa1fSAdam Jackson 	 */
1978c496fa1fSAdam Jackson 	if (IS_G4X(dev)) {
1979c496fa1fSAdam Jackson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
1980c496fa1fSAdam Jackson 			       GM45_ERROR_MEM_PRIV |
1981c496fa1fSAdam Jackson 			       GM45_ERROR_CP_PRIV |
1982c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1983c496fa1fSAdam Jackson 	} else {
1984c496fa1fSAdam Jackson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
1985c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1986c496fa1fSAdam Jackson 	}
1987c496fa1fSAdam Jackson 	I915_WRITE(EMR, error_mask);
1988c496fa1fSAdam Jackson 
19891ec14ad3SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
1990c496fa1fSAdam Jackson 	I915_WRITE(IER, enable_mask);
19913143a2bfSChris Wilson 	POSTING_READ(IER);
1992c496fa1fSAdam Jackson 
1993c496fa1fSAdam Jackson 	if (I915_HAS_HOTPLUG(dev)) {
19945ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
19955ca58282SJesse Barnes 
1996b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
1997b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1998b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1999b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2000b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2001b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2002b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
2003b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
2004b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2005b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
2006b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
20072d1c9752SAndy Lutomirski 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2008b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
20092d1c9752SAndy Lutomirski 
20102d1c9752SAndy Lutomirski 			/* Programming the CRT detection parameters tends
20112d1c9752SAndy Lutomirski 			   to generate a spurious hotplug event about three
20122d1c9752SAndy Lutomirski 			   seconds later.  So just do it once.
20132d1c9752SAndy Lutomirski 			*/
20142d1c9752SAndy Lutomirski 			if (IS_G4X(dev))
20152d1c9752SAndy Lutomirski 				hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
20162d1c9752SAndy Lutomirski 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
20172d1c9752SAndy Lutomirski 		}
20182d1c9752SAndy Lutomirski 
2019b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
2020b01f2c3aSJesse Barnes 
20215ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
20225ca58282SJesse Barnes 	}
20235ca58282SJesse Barnes 
20243b617967SChris Wilson 	intel_opregion_enable_asle(dev);
20250a3e67a4SJesse Barnes 
20260a3e67a4SJesse Barnes 	return 0;
2027c0e09200SDave Airlie }
2028c0e09200SDave Airlie 
2029f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2030036a4a7dSZhenyu Wang {
2031036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20324697995bSJesse Barnes 
20334697995bSJesse Barnes 	if (!dev_priv)
20344697995bSJesse Barnes 		return;
20354697995bSJesse Barnes 
20364697995bSJesse Barnes 	dev_priv->vblank_pipe = 0;
20374697995bSJesse Barnes 
2038036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2039036a4a7dSZhenyu Wang 
2040036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2041036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2042036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2043036a4a7dSZhenyu Wang 
2044036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2045036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2046036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2047*192aac1fSKeith Packard 
2048*192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2049*192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2050*192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2051036a4a7dSZhenyu Wang }
2052036a4a7dSZhenyu Wang 
2053f71d4af4SJesse Barnes static void i915_driver_irq_uninstall(struct drm_device * dev)
2054c0e09200SDave Airlie {
2055c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20569db4a9c7SJesse Barnes 	int pipe;
2057c0e09200SDave Airlie 
2058c0e09200SDave Airlie 	if (!dev_priv)
2059c0e09200SDave Airlie 		return;
2060c0e09200SDave Airlie 
20610a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
20620a3e67a4SJesse Barnes 
20635ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
20645ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
20655ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
20665ca58282SJesse Barnes 	}
20675ca58282SJesse Barnes 
20680a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
20699db4a9c7SJesse Barnes 	for_each_pipe(pipe)
20709db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0);
20710a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
2072ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
2073c0e09200SDave Airlie 
20749db4a9c7SJesse Barnes 	for_each_pipe(pipe)
20759db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe),
20769db4a9c7SJesse Barnes 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
20777c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
2078c0e09200SDave Airlie }
2079f71d4af4SJesse Barnes 
2080f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2081f71d4af4SJesse Barnes {
2082f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2083f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
2084f71d4af4SJesse Barnes 	if (IS_G4X(dev) || IS_GEN5(dev) || IS_GEN6(dev) || IS_IVYBRIDGE(dev)) {
2085f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2086f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2087f71d4af4SJesse Barnes 	}
2088f71d4af4SJesse Barnes 
2089c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2090f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2091c3613de9SKeith Packard 	else
2092c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2093f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2094f71d4af4SJesse Barnes 
2095f71d4af4SJesse Barnes 	if (IS_IVYBRIDGE(dev)) {
2096f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2097f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2098f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2099f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2100f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2101f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2102f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
2103f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2104f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2105f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2106f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2107f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2108f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2109f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
2110f71d4af4SJesse Barnes 	} else {
2111f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = i915_driver_irq_preinstall;
2112f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = i915_driver_irq_postinstall;
2113f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = i915_driver_irq_uninstall;
2114f71d4af4SJesse Barnes 		dev->driver->irq_handler = i915_driver_irq_handler;
2115f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
2116f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
2117f71d4af4SJesse Barnes 	}
2118f71d4af4SJesse Barnes }
2119