xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 4697995b98417c6da9ab2708a36f5e2bc926c8ac)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
2963eeaf38SJesse Barnes #include <linux/sysrq.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31c0e09200SDave Airlie #include "drmP.h"
32c0e09200SDave Airlie #include "drm.h"
33c0e09200SDave Airlie #include "i915_drm.h"
34c0e09200SDave Airlie #include "i915_drv.h"
351c5d22f7SChris Wilson #include "i915_trace.h"
3679e53945SJesse Barnes #include "intel_drv.h"
37c0e09200SDave Airlie 
38c0e09200SDave Airlie #define MAX_NOPID ((u32)~0)
39c0e09200SDave Airlie 
407c463586SKeith Packard /**
417c463586SKeith Packard  * Interrupts that are always left unmasked.
427c463586SKeith Packard  *
437c463586SKeith Packard  * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
447c463586SKeith Packard  * we leave them always unmasked in IMR and then control enabling them through
457c463586SKeith Packard  * PIPESTAT alone.
467c463586SKeith Packard  */
476b95a207SKristian Høgsberg #define I915_INTERRUPT_ENABLE_FIX			\
486b95a207SKristian Høgsberg 	(I915_ASLE_INTERRUPT |				\
490a3e67a4SJesse Barnes 	 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |		\
5063eeaf38SJesse Barnes 	 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |		\
516b95a207SKristian Høgsberg 	 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |	\
526b95a207SKristian Høgsberg 	 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |	\
5363eeaf38SJesse Barnes 	 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
54ed4cb414SEric Anholt 
557c463586SKeith Packard /** Interrupts that we mask and unmask at runtime. */
56d1b851fcSZou Nan hai #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT | I915_BSD_USER_INTERRUPT)
577c463586SKeith Packard 
5879e53945SJesse Barnes #define I915_PIPE_VBLANK_STATUS	(PIPE_START_VBLANK_INTERRUPT_STATUS |\
5979e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_STATUS)
6079e53945SJesse Barnes 
6179e53945SJesse Barnes #define I915_PIPE_VBLANK_ENABLE	(PIPE_START_VBLANK_INTERRUPT_ENABLE |\
6279e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_ENABLE)
6379e53945SJesse Barnes 
6479e53945SJesse Barnes #define DRM_I915_VBLANK_PIPE_ALL	(DRM_I915_VBLANK_PIPE_A | \
6579e53945SJesse Barnes 					 DRM_I915_VBLANK_PIPE_B)
6679e53945SJesse Barnes 
67036a4a7dSZhenyu Wang /* For display hotplug interrupt */
68995b6762SChris Wilson static void
69f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
70036a4a7dSZhenyu Wang {
711ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
721ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
731ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
743143a2bfSChris Wilson 		POSTING_READ(DEIMR);
75036a4a7dSZhenyu Wang 	}
76036a4a7dSZhenyu Wang }
77036a4a7dSZhenyu Wang 
78036a4a7dSZhenyu Wang static inline void
79f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
80036a4a7dSZhenyu Wang {
811ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
821ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
831ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
843143a2bfSChris Wilson 		POSTING_READ(DEIMR);
85036a4a7dSZhenyu Wang 	}
86036a4a7dSZhenyu Wang }
87036a4a7dSZhenyu Wang 
887c463586SKeith Packard void
897c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
907c463586SKeith Packard {
917c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
929db4a9c7SJesse Barnes 		u32 reg = PIPESTAT(pipe);
937c463586SKeith Packard 
947c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
957c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
967c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
973143a2bfSChris Wilson 		POSTING_READ(reg);
987c463586SKeith Packard 	}
997c463586SKeith Packard }
1007c463586SKeith Packard 
1017c463586SKeith Packard void
1027c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1037c463586SKeith Packard {
1047c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
1059db4a9c7SJesse Barnes 		u32 reg = PIPESTAT(pipe);
1067c463586SKeith Packard 
1077c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
1087c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
1093143a2bfSChris Wilson 		POSTING_READ(reg);
1107c463586SKeith Packard 	}
1117c463586SKeith Packard }
1127c463586SKeith Packard 
113c0e09200SDave Airlie /**
11401c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
11501c66889SZhao Yakui  */
11601c66889SZhao Yakui void intel_enable_asle(struct drm_device *dev)
11701c66889SZhao Yakui {
1181ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1191ec14ad3SChris Wilson 	unsigned long irqflags;
1201ec14ad3SChris Wilson 
1211ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
12201c66889SZhao Yakui 
123c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
124f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
125edcb49caSZhao Yakui 	else {
12601c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
127d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
128a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen >= 4)
129edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
130d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
131edcb49caSZhao Yakui 	}
1321ec14ad3SChris Wilson 
1331ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
13401c66889SZhao Yakui }
13501c66889SZhao Yakui 
13601c66889SZhao Yakui /**
1370a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1380a3e67a4SJesse Barnes  * @dev: DRM device
1390a3e67a4SJesse Barnes  * @pipe: pipe to check
1400a3e67a4SJesse Barnes  *
1410a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1420a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1430a3e67a4SJesse Barnes  * before reading such registers if unsure.
1440a3e67a4SJesse Barnes  */
1450a3e67a4SJesse Barnes static int
1460a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1470a3e67a4SJesse Barnes {
1480a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1495eddb70bSChris Wilson 	return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
1500a3e67a4SJesse Barnes }
1510a3e67a4SJesse Barnes 
15242f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
15342f52ef8SKeith Packard  * we use as a pipe index
15442f52ef8SKeith Packard  */
15542f52ef8SKeith Packard u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1560a3e67a4SJesse Barnes {
1570a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1580a3e67a4SJesse Barnes 	unsigned long high_frame;
1590a3e67a4SJesse Barnes 	unsigned long low_frame;
1605eddb70bSChris Wilson 	u32 high1, high2, low;
1610a3e67a4SJesse Barnes 
1620a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
16344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
1649db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
1650a3e67a4SJesse Barnes 		return 0;
1660a3e67a4SJesse Barnes 	}
1670a3e67a4SJesse Barnes 
1689db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
1699db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
1705eddb70bSChris Wilson 
1710a3e67a4SJesse Barnes 	/*
1720a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
1730a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
1740a3e67a4SJesse Barnes 	 * register.
1750a3e67a4SJesse Barnes 	 */
1760a3e67a4SJesse Barnes 	do {
1775eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1785eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
1795eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1800a3e67a4SJesse Barnes 	} while (high1 != high2);
1810a3e67a4SJesse Barnes 
1825eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
1835eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
1845eddb70bSChris Wilson 	return (high1 << 8) | low;
1850a3e67a4SJesse Barnes }
1860a3e67a4SJesse Barnes 
1879880b7a5SJesse Barnes u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
1889880b7a5SJesse Barnes {
1899880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1909db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
1919880b7a5SJesse Barnes 
1929880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
19344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
1949db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
1959880b7a5SJesse Barnes 		return 0;
1969880b7a5SJesse Barnes 	}
1979880b7a5SJesse Barnes 
1989880b7a5SJesse Barnes 	return I915_READ(reg);
1999880b7a5SJesse Barnes }
2009880b7a5SJesse Barnes 
2010af7e4dfSMario Kleiner int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
2020af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
2030af7e4dfSMario Kleiner {
2040af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2050af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
2060af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
2070af7e4dfSMario Kleiner 	bool in_vbl = true;
2080af7e4dfSMario Kleiner 	int ret = 0;
2090af7e4dfSMario Kleiner 
2100af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
2110af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
2129db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
2130af7e4dfSMario Kleiner 		return 0;
2140af7e4dfSMario Kleiner 	}
2150af7e4dfSMario Kleiner 
2160af7e4dfSMario Kleiner 	/* Get vtotal. */
2170af7e4dfSMario Kleiner 	vtotal = 1 + ((I915_READ(VTOTAL(pipe)) >> 16) & 0x1fff);
2180af7e4dfSMario Kleiner 
2190af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
2200af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
2210af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
2220af7e4dfSMario Kleiner 		 */
2230af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
2240af7e4dfSMario Kleiner 
2250af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
2260af7e4dfSMario Kleiner 		 * horizontal scanout position.
2270af7e4dfSMario Kleiner 		 */
2280af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
2290af7e4dfSMario Kleiner 		*hpos = 0;
2300af7e4dfSMario Kleiner 	} else {
2310af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
2320af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
2330af7e4dfSMario Kleiner 		 * scanout position.
2340af7e4dfSMario Kleiner 		 */
2350af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
2360af7e4dfSMario Kleiner 
2370af7e4dfSMario Kleiner 		htotal = 1 + ((I915_READ(HTOTAL(pipe)) >> 16) & 0x1fff);
2380af7e4dfSMario Kleiner 		*vpos = position / htotal;
2390af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
2400af7e4dfSMario Kleiner 	}
2410af7e4dfSMario Kleiner 
2420af7e4dfSMario Kleiner 	/* Query vblank area. */
2430af7e4dfSMario Kleiner 	vbl = I915_READ(VBLANK(pipe));
2440af7e4dfSMario Kleiner 
2450af7e4dfSMario Kleiner 	/* Test position against vblank region. */
2460af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
2470af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
2480af7e4dfSMario Kleiner 
2490af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
2500af7e4dfSMario Kleiner 		in_vbl = false;
2510af7e4dfSMario Kleiner 
2520af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
2530af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
2540af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
2550af7e4dfSMario Kleiner 
2560af7e4dfSMario Kleiner 	/* Readouts valid? */
2570af7e4dfSMario Kleiner 	if (vbl > 0)
2580af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
2590af7e4dfSMario Kleiner 
2600af7e4dfSMario Kleiner 	/* In vblank? */
2610af7e4dfSMario Kleiner 	if (in_vbl)
2620af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
2630af7e4dfSMario Kleiner 
2640af7e4dfSMario Kleiner 	return ret;
2650af7e4dfSMario Kleiner }
2660af7e4dfSMario Kleiner 
2674041b853SChris Wilson int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
2680af7e4dfSMario Kleiner 			      int *max_error,
2690af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
2700af7e4dfSMario Kleiner 			      unsigned flags)
2710af7e4dfSMario Kleiner {
2724041b853SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2734041b853SChris Wilson 	struct drm_crtc *crtc;
2740af7e4dfSMario Kleiner 
2754041b853SChris Wilson 	if (pipe < 0 || pipe >= dev_priv->num_pipe) {
2764041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2770af7e4dfSMario Kleiner 		return -EINVAL;
2780af7e4dfSMario Kleiner 	}
2790af7e4dfSMario Kleiner 
2800af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
2814041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
2824041b853SChris Wilson 	if (crtc == NULL) {
2834041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2844041b853SChris Wilson 		return -EINVAL;
2854041b853SChris Wilson 	}
2864041b853SChris Wilson 
2874041b853SChris Wilson 	if (!crtc->enabled) {
2884041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
2894041b853SChris Wilson 		return -EBUSY;
2904041b853SChris Wilson 	}
2910af7e4dfSMario Kleiner 
2920af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
2934041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
2944041b853SChris Wilson 						     vblank_time, flags,
2954041b853SChris Wilson 						     crtc);
2960af7e4dfSMario Kleiner }
2970af7e4dfSMario Kleiner 
2985ca58282SJesse Barnes /*
2995ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
3005ca58282SJesse Barnes  */
3015ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
3025ca58282SJesse Barnes {
3035ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3045ca58282SJesse Barnes 						    hotplug_work);
3055ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
306c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
3074ef69c7aSChris Wilson 	struct intel_encoder *encoder;
3085ca58282SJesse Barnes 
309e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
310e67189abSJesse Barnes 
3114ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
3124ef69c7aSChris Wilson 		if (encoder->hot_plug)
3134ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
314c31c4ba3SKeith Packard 
3155ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
316eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3175ca58282SJesse Barnes }
3185ca58282SJesse Barnes 
319f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
320f97108d1SJesse Barnes {
321f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
322b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
323f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
324f97108d1SJesse Barnes 
3257648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
326b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
327b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
328f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
329f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
330f97108d1SJesse Barnes 
331f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
332b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
333f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
334f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
335f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
336f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
337b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
338f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
339f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
340f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
341f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
342f97108d1SJesse Barnes 	}
343f97108d1SJesse Barnes 
3447648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
345f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
346f97108d1SJesse Barnes 
347f97108d1SJesse Barnes 	return;
348f97108d1SJesse Barnes }
349f97108d1SJesse Barnes 
350549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
351549f7365SChris Wilson 			struct intel_ring_buffer *ring)
352549f7365SChris Wilson {
353549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
354475553deSChris Wilson 	u32 seqno;
3559862e600SChris Wilson 
356475553deSChris Wilson 	if (ring->obj == NULL)
357475553deSChris Wilson 		return;
358475553deSChris Wilson 
359475553deSChris Wilson 	seqno = ring->get_seqno(ring);
360db53a302SChris Wilson 	trace_i915_gem_request_complete(ring, seqno);
3619862e600SChris Wilson 
3629862e600SChris Wilson 	ring->irq_seqno = seqno;
363549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3649862e600SChris Wilson 
365549f7365SChris Wilson 	dev_priv->hangcheck_count = 0;
366549f7365SChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
367549f7365SChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
368549f7365SChris Wilson }
369549f7365SChris Wilson 
3704912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
3713b8d8d91SJesse Barnes {
3724912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3734912d041SBen Widawsky 						    rps_work);
3743b8d8d91SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
3754912d041SBen Widawsky 	u32 pm_iir, pm_imr;
3763b8d8d91SJesse Barnes 
3774912d041SBen Widawsky 	spin_lock_irq(&dev_priv->rps_lock);
3784912d041SBen Widawsky 	pm_iir = dev_priv->pm_iir;
3794912d041SBen Widawsky 	dev_priv->pm_iir = 0;
3804912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
3814912d041SBen Widawsky 	spin_unlock_irq(&dev_priv->rps_lock);
3824912d041SBen Widawsky 
3833b8d8d91SJesse Barnes 	if (!pm_iir)
3843b8d8d91SJesse Barnes 		return;
3853b8d8d91SJesse Barnes 
3864912d041SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
3873b8d8d91SJesse Barnes 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
3883b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
3893b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
3903b8d8d91SJesse Barnes 		if (new_delay > dev_priv->max_delay)
3913b8d8d91SJesse Barnes 			new_delay = dev_priv->max_delay;
3923b8d8d91SJesse Barnes 	} else if (pm_iir & (GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT)) {
3934912d041SBen Widawsky 		gen6_gt_force_wake_get(dev_priv);
3943b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
3953b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
3963b8d8d91SJesse Barnes 		if (new_delay < dev_priv->min_delay) {
3973b8d8d91SJesse Barnes 			new_delay = dev_priv->min_delay;
3983b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3993b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) |
4003b8d8d91SJesse Barnes 				   ((new_delay << 16) & 0x3f0000));
4013b8d8d91SJesse Barnes 		} else {
4023b8d8d91SJesse Barnes 			/* Make sure we continue to get down interrupts
4033b8d8d91SJesse Barnes 			 * until we hit the minimum frequency */
4043b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4053b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) & ~0x3f0000);
4063b8d8d91SJesse Barnes 		}
4074912d041SBen Widawsky 		gen6_gt_force_wake_put(dev_priv);
4083b8d8d91SJesse Barnes 	}
4093b8d8d91SJesse Barnes 
4104912d041SBen Widawsky 	gen6_set_rps(dev_priv->dev, new_delay);
4113b8d8d91SJesse Barnes 	dev_priv->cur_delay = new_delay;
4123b8d8d91SJesse Barnes 
4134912d041SBen Widawsky 	/*
4144912d041SBen Widawsky 	 * rps_lock not held here because clearing is non-destructive. There is
4154912d041SBen Widawsky 	 * an *extremely* unlikely race with gen6_rps_enable() that is prevented
4164912d041SBen Widawsky 	 * by holding struct_mutex for the duration of the write.
4174912d041SBen Widawsky 	 */
4184912d041SBen Widawsky 	I915_WRITE(GEN6_PMIMR, pm_imr & ~pm_iir);
4194912d041SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
4203b8d8d91SJesse Barnes }
4213b8d8d91SJesse Barnes 
422776ad806SJesse Barnes static void pch_irq_handler(struct drm_device *dev)
423776ad806SJesse Barnes {
424776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
425776ad806SJesse Barnes 	u32 pch_iir;
4269db4a9c7SJesse Barnes 	int pipe;
427776ad806SJesse Barnes 
428776ad806SJesse Barnes 	pch_iir = I915_READ(SDEIIR);
429776ad806SJesse Barnes 
430776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
431776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
432776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
433776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
434776ad806SJesse Barnes 
435776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
436776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
437776ad806SJesse Barnes 
438776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
439776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
440776ad806SJesse Barnes 
441776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
442776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
443776ad806SJesse Barnes 
444776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
445776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
446776ad806SJesse Barnes 
4479db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
4489db4a9c7SJesse Barnes 		for_each_pipe(pipe)
4499db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
4509db4a9c7SJesse Barnes 					 pipe_name(pipe),
4519db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
452776ad806SJesse Barnes 
453776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
454776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
455776ad806SJesse Barnes 
456776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
457776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
458776ad806SJesse Barnes 
459776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
460776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
461776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
462776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
463776ad806SJesse Barnes }
464776ad806SJesse Barnes 
465*4697995bSJesse Barnes irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
466036a4a7dSZhenyu Wang {
467*4697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
468036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
469036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
4703b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
4712d7b8366SYuanhan Liu 	u32 hotplug_mask;
472036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
473881f47b6SXiang, Haihao 	u32 bsd_usr_interrupt = GT_BSD_USER_INTERRUPT;
474881f47b6SXiang, Haihao 
475*4697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
476*4697995bSJesse Barnes 
477881f47b6SXiang, Haihao 	if (IS_GEN6(dev))
478881f47b6SXiang, Haihao 		bsd_usr_interrupt = GT_GEN6_BSD_USER_INTERRUPT;
479036a4a7dSZhenyu Wang 
4802d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
4812d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
4822d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
4833143a2bfSChris Wilson 	POSTING_READ(DEIER);
4842d109a84SZou, Nanhai 
485036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
486036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
487c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
4883b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
489036a4a7dSZhenyu Wang 
4903b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
4913b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
492c7c85101SZou Nan hai 		goto done;
493036a4a7dSZhenyu Wang 
4942d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
4952d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
4962d7b8366SYuanhan Liu 	else
4972d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
4982d7b8366SYuanhan Liu 
499036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
500036a4a7dSZhenyu Wang 
501036a4a7dSZhenyu Wang 	if (dev->primary->master) {
502036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
503036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
504036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
505036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
506036a4a7dSZhenyu Wang 	}
507036a4a7dSZhenyu Wang 
508c6df541cSChris Wilson 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
5091ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[RCS]);
510881f47b6SXiang, Haihao 	if (gt_iir & bsd_usr_interrupt)
5111ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[VCS]);
5121ec14ad3SChris Wilson 	if (gt_iir & GT_BLT_USER_INTERRUPT)
5131ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[BCS]);
514036a4a7dSZhenyu Wang 
51501c66889SZhao Yakui 	if (de_iir & DE_GSE)
5163b617967SChris Wilson 		intel_opregion_gse_intr(dev);
51701c66889SZhao Yakui 
518f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
519013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
5202bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
521013d5aa2SJesse Barnes 	}
522013d5aa2SJesse Barnes 
523f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
524f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
5252bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
526013d5aa2SJesse Barnes 	}
527c062df61SLi Peng 
528f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
529f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
530f072d2e7SZhenyu Wang 
531f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
532f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
533f072d2e7SZhenyu Wang 
534c650156aSZhenyu Wang 	/* check event from PCH */
535776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
536776ad806SJesse Barnes 		if (pch_iir & hotplug_mask)
537c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
538776ad806SJesse Barnes 		pch_irq_handler(dev);
539776ad806SJesse Barnes 	}
540c650156aSZhenyu Wang 
541f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
5427648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
543f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
544f97108d1SJesse Barnes 	}
545f97108d1SJesse Barnes 
5464912d041SBen Widawsky 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS) {
5474912d041SBen Widawsky 		/*
5484912d041SBen Widawsky 		 * IIR bits should never already be set because IMR should
5494912d041SBen Widawsky 		 * prevent an interrupt from being shown in IIR. The warning
5504912d041SBen Widawsky 		 * displays a case where we've unsafely cleared
5514912d041SBen Widawsky 		 * dev_priv->pm_iir. Although missing an interrupt of the same
5524912d041SBen Widawsky 		 * type is not a problem, it displays a problem in the logic.
5534912d041SBen Widawsky 		 *
5544912d041SBen Widawsky 		 * The mask bit in IMR is cleared by rps_work.
5554912d041SBen Widawsky 		 */
5564912d041SBen Widawsky 		unsigned long flags;
5574912d041SBen Widawsky 		spin_lock_irqsave(&dev_priv->rps_lock, flags);
5584912d041SBen Widawsky 		WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
5594912d041SBen Widawsky 		I915_WRITE(GEN6_PMIMR, pm_iir);
5604912d041SBen Widawsky 		dev_priv->pm_iir |= pm_iir;
5614912d041SBen Widawsky 		spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
5624912d041SBen Widawsky 		queue_work(dev_priv->wq, &dev_priv->rps_work);
5634912d041SBen Widawsky 	}
5643b8d8d91SJesse Barnes 
565c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
566c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
567c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
568c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
5694912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
570036a4a7dSZhenyu Wang 
571c7c85101SZou Nan hai done:
5722d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
5733143a2bfSChris Wilson 	POSTING_READ(DEIER);
5742d109a84SZou, Nanhai 
575036a4a7dSZhenyu Wang 	return ret;
576036a4a7dSZhenyu Wang }
577036a4a7dSZhenyu Wang 
5788a905236SJesse Barnes /**
5798a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
5808a905236SJesse Barnes  * @work: work struct
5818a905236SJesse Barnes  *
5828a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
5838a905236SJesse Barnes  * was detected.
5848a905236SJesse Barnes  */
5858a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
5868a905236SJesse Barnes {
5878a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5888a905236SJesse Barnes 						    error_work);
5898a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
590f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
591f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
592f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
5938a905236SJesse Barnes 
594f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
5958a905236SJesse Barnes 
596ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
59744d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
598f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
599f803aa55SChris Wilson 		if (!i915_reset(dev, GRDOM_RENDER)) {
600ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
601f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
602f316a42cSBen Gamari 		}
60330dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
604f316a42cSBen Gamari 	}
6058a905236SJesse Barnes }
6068a905236SJesse Barnes 
6073bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
6089df30794SChris Wilson static struct drm_i915_error_object *
609bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
61005394f39SChris Wilson 			 struct drm_i915_gem_object *src)
6119df30794SChris Wilson {
6129df30794SChris Wilson 	struct drm_i915_error_object *dst;
6139df30794SChris Wilson 	int page, page_count;
614e56660ddSChris Wilson 	u32 reloc_offset;
6159df30794SChris Wilson 
61605394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
6179df30794SChris Wilson 		return NULL;
6189df30794SChris Wilson 
61905394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
6209df30794SChris Wilson 
6219df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof (u32 *), GFP_ATOMIC);
6229df30794SChris Wilson 	if (dst == NULL)
6239df30794SChris Wilson 		return NULL;
6249df30794SChris Wilson 
62505394f39SChris Wilson 	reloc_offset = src->gtt_offset;
6269df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
627788885aeSAndrew Morton 		unsigned long flags;
628e56660ddSChris Wilson 		void __iomem *s;
629e56660ddSChris Wilson 		void *d;
630788885aeSAndrew Morton 
631e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
6329df30794SChris Wilson 		if (d == NULL)
6339df30794SChris Wilson 			goto unwind;
634e56660ddSChris Wilson 
635788885aeSAndrew Morton 		local_irq_save(flags);
636e56660ddSChris Wilson 		s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
6373e4d3af5SPeter Zijlstra 					     reloc_offset);
638e56660ddSChris Wilson 		memcpy_fromio(d, s, PAGE_SIZE);
6393e4d3af5SPeter Zijlstra 		io_mapping_unmap_atomic(s);
640788885aeSAndrew Morton 		local_irq_restore(flags);
641e56660ddSChris Wilson 
6429df30794SChris Wilson 		dst->pages[page] = d;
643e56660ddSChris Wilson 
644e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
6459df30794SChris Wilson 	}
6469df30794SChris Wilson 	dst->page_count = page_count;
64705394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
6489df30794SChris Wilson 
6499df30794SChris Wilson 	return dst;
6509df30794SChris Wilson 
6519df30794SChris Wilson unwind:
6529df30794SChris Wilson 	while (page--)
6539df30794SChris Wilson 		kfree(dst->pages[page]);
6549df30794SChris Wilson 	kfree(dst);
6559df30794SChris Wilson 	return NULL;
6569df30794SChris Wilson }
6579df30794SChris Wilson 
6589df30794SChris Wilson static void
6599df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
6609df30794SChris Wilson {
6619df30794SChris Wilson 	int page;
6629df30794SChris Wilson 
6639df30794SChris Wilson 	if (obj == NULL)
6649df30794SChris Wilson 		return;
6659df30794SChris Wilson 
6669df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
6679df30794SChris Wilson 		kfree(obj->pages[page]);
6689df30794SChris Wilson 
6699df30794SChris Wilson 	kfree(obj);
6709df30794SChris Wilson }
6719df30794SChris Wilson 
6729df30794SChris Wilson static void
6739df30794SChris Wilson i915_error_state_free(struct drm_device *dev,
6749df30794SChris Wilson 		      struct drm_i915_error_state *error)
6759df30794SChris Wilson {
676e2f973d5SChris Wilson 	int i;
677e2f973d5SChris Wilson 
678e2f973d5SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->batchbuffer); i++)
679e2f973d5SChris Wilson 		i915_error_object_free(error->batchbuffer[i]);
680e2f973d5SChris Wilson 
681e2f973d5SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ringbuffer); i++)
682e2f973d5SChris Wilson 		i915_error_object_free(error->ringbuffer[i]);
683e2f973d5SChris Wilson 
6849df30794SChris Wilson 	kfree(error->active_bo);
6856ef3d427SChris Wilson 	kfree(error->overlay);
6869df30794SChris Wilson 	kfree(error);
6879df30794SChris Wilson }
6889df30794SChris Wilson 
689c724e8a9SChris Wilson static u32 capture_bo_list(struct drm_i915_error_buffer *err,
690c724e8a9SChris Wilson 			   int count,
691c724e8a9SChris Wilson 			   struct list_head *head)
692c724e8a9SChris Wilson {
693c724e8a9SChris Wilson 	struct drm_i915_gem_object *obj;
694c724e8a9SChris Wilson 	int i = 0;
695c724e8a9SChris Wilson 
696c724e8a9SChris Wilson 	list_for_each_entry(obj, head, mm_list) {
697c724e8a9SChris Wilson 		err->size = obj->base.size;
698c724e8a9SChris Wilson 		err->name = obj->base.name;
699c724e8a9SChris Wilson 		err->seqno = obj->last_rendering_seqno;
700c724e8a9SChris Wilson 		err->gtt_offset = obj->gtt_offset;
701c724e8a9SChris Wilson 		err->read_domains = obj->base.read_domains;
702c724e8a9SChris Wilson 		err->write_domain = obj->base.write_domain;
703c724e8a9SChris Wilson 		err->fence_reg = obj->fence_reg;
704c724e8a9SChris Wilson 		err->pinned = 0;
705c724e8a9SChris Wilson 		if (obj->pin_count > 0)
706c724e8a9SChris Wilson 			err->pinned = 1;
707c724e8a9SChris Wilson 		if (obj->user_pin_count > 0)
708c724e8a9SChris Wilson 			err->pinned = -1;
709c724e8a9SChris Wilson 		err->tiling = obj->tiling_mode;
710c724e8a9SChris Wilson 		err->dirty = obj->dirty;
711c724e8a9SChris Wilson 		err->purgeable = obj->madv != I915_MADV_WILLNEED;
7123685092bSChris Wilson 		err->ring = obj->ring ? obj->ring->id : 0;
71393dfb40cSChris Wilson 		err->cache_level = obj->cache_level;
714c724e8a9SChris Wilson 
715c724e8a9SChris Wilson 		if (++i == count)
716c724e8a9SChris Wilson 			break;
717c724e8a9SChris Wilson 
718c724e8a9SChris Wilson 		err++;
719c724e8a9SChris Wilson 	}
720c724e8a9SChris Wilson 
721c724e8a9SChris Wilson 	return i;
722c724e8a9SChris Wilson }
723c724e8a9SChris Wilson 
724748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
725748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
726748ebc60SChris Wilson {
727748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
728748ebc60SChris Wilson 	int i;
729748ebc60SChris Wilson 
730748ebc60SChris Wilson 	/* Fences */
731748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
732748ebc60SChris Wilson 	case 6:
733748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
734748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
735748ebc60SChris Wilson 		break;
736748ebc60SChris Wilson 	case 5:
737748ebc60SChris Wilson 	case 4:
738748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
739748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
740748ebc60SChris Wilson 		break;
741748ebc60SChris Wilson 	case 3:
742748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
743748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
744748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
745748ebc60SChris Wilson 	case 2:
746748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
747748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
748748ebc60SChris Wilson 		break;
749748ebc60SChris Wilson 
750748ebc60SChris Wilson 	}
751748ebc60SChris Wilson }
752748ebc60SChris Wilson 
753bcfb2e28SChris Wilson static struct drm_i915_error_object *
754bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
755bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
756bcfb2e28SChris Wilson {
757bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
758bcfb2e28SChris Wilson 	u32 seqno;
759bcfb2e28SChris Wilson 
760bcfb2e28SChris Wilson 	if (!ring->get_seqno)
761bcfb2e28SChris Wilson 		return NULL;
762bcfb2e28SChris Wilson 
763bcfb2e28SChris Wilson 	seqno = ring->get_seqno(ring);
764bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
765bcfb2e28SChris Wilson 		if (obj->ring != ring)
766bcfb2e28SChris Wilson 			continue;
767bcfb2e28SChris Wilson 
768c37d9a5dSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
769bcfb2e28SChris Wilson 			continue;
770bcfb2e28SChris Wilson 
771bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
772bcfb2e28SChris Wilson 			continue;
773bcfb2e28SChris Wilson 
774bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
775bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
776bcfb2e28SChris Wilson 		 */
777bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
778bcfb2e28SChris Wilson 	}
779bcfb2e28SChris Wilson 
780bcfb2e28SChris Wilson 	return NULL;
781bcfb2e28SChris Wilson }
782bcfb2e28SChris Wilson 
7838a905236SJesse Barnes /**
7848a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
7858a905236SJesse Barnes  * @dev: drm device
7868a905236SJesse Barnes  *
7878a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
7888a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
7898a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
7908a905236SJesse Barnes  * to pick up.
7918a905236SJesse Barnes  */
79263eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
79363eeaf38SJesse Barnes {
79463eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
79505394f39SChris Wilson 	struct drm_i915_gem_object *obj;
79663eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
79763eeaf38SJesse Barnes 	unsigned long flags;
7989db4a9c7SJesse Barnes 	int i, pipe;
79963eeaf38SJesse Barnes 
80063eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
8019df30794SChris Wilson 	error = dev_priv->first_error;
8029df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
8039df30794SChris Wilson 	if (error)
8049df30794SChris Wilson 		return;
80563eeaf38SJesse Barnes 
8069db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
80763eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
80863eeaf38SJesse Barnes 	if (!error) {
8099df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
8109df30794SChris Wilson 		return;
81163eeaf38SJesse Barnes 	}
81263eeaf38SJesse Barnes 
813b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
814b6f7833bSChris Wilson 		 dev->primary->index);
8152fa772f3SChris Wilson 
8161ec14ad3SChris Wilson 	error->seqno = dev_priv->ring[RCS].get_seqno(&dev_priv->ring[RCS]);
81763eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
81863eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
8199db4a9c7SJesse Barnes 	for_each_pipe(pipe)
8209db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
82163eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
822f406839fSChris Wilson 	error->error = 0;
823f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 6) {
824f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
825add354ddSChris Wilson 
8261d8f38f4SChris Wilson 		error->bcs_acthd = I915_READ(BCS_ACTHD);
8271d8f38f4SChris Wilson 		error->bcs_ipehr = I915_READ(BCS_IPEHR);
8281d8f38f4SChris Wilson 		error->bcs_ipeir = I915_READ(BCS_IPEIR);
8291d8f38f4SChris Wilson 		error->bcs_instdone = I915_READ(BCS_INSTDONE);
8301d8f38f4SChris Wilson 		error->bcs_seqno = 0;
8311ec14ad3SChris Wilson 		if (dev_priv->ring[BCS].get_seqno)
8321ec14ad3SChris Wilson 			error->bcs_seqno = dev_priv->ring[BCS].get_seqno(&dev_priv->ring[BCS]);
833add354ddSChris Wilson 
834add354ddSChris Wilson 		error->vcs_acthd = I915_READ(VCS_ACTHD);
835add354ddSChris Wilson 		error->vcs_ipehr = I915_READ(VCS_IPEHR);
836add354ddSChris Wilson 		error->vcs_ipeir = I915_READ(VCS_IPEIR);
837add354ddSChris Wilson 		error->vcs_instdone = I915_READ(VCS_INSTDONE);
838add354ddSChris Wilson 		error->vcs_seqno = 0;
8391ec14ad3SChris Wilson 		if (dev_priv->ring[VCS].get_seqno)
8401ec14ad3SChris Wilson 			error->vcs_seqno = dev_priv->ring[VCS].get_seqno(&dev_priv->ring[VCS]);
841f406839fSChris Wilson 	}
842f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
84363eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
84463eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
84563eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
84663eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
84763eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
84863eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
8499df30794SChris Wilson 		error->bbaddr = I915_READ64(BB_ADDR);
850f406839fSChris Wilson 	} else {
851f406839fSChris Wilson 		error->ipeir = I915_READ(IPEIR);
852f406839fSChris Wilson 		error->ipehr = I915_READ(IPEHR);
853f406839fSChris Wilson 		error->instdone = I915_READ(INSTDONE);
854f406839fSChris Wilson 		error->acthd = I915_READ(ACTHD);
855f406839fSChris Wilson 		error->bbaddr = 0;
8569df30794SChris Wilson 	}
857748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
8589df30794SChris Wilson 
859e2f973d5SChris Wilson 	/* Record the active batch and ring buffers */
860e2f973d5SChris Wilson 	for (i = 0; i < I915_NUM_RINGS; i++) {
861bcfb2e28SChris Wilson 		error->batchbuffer[i] =
862bcfb2e28SChris Wilson 			i915_error_first_batchbuffer(dev_priv,
863bcfb2e28SChris Wilson 						     &dev_priv->ring[i]);
8649df30794SChris Wilson 
865e2f973d5SChris Wilson 		error->ringbuffer[i] =
866e2f973d5SChris Wilson 			i915_error_object_create(dev_priv,
867e2f973d5SChris Wilson 						 dev_priv->ring[i].obj);
868e2f973d5SChris Wilson 	}
8699df30794SChris Wilson 
870c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
8719df30794SChris Wilson 	error->active_bo = NULL;
872c724e8a9SChris Wilson 	error->pinned_bo = NULL;
8739df30794SChris Wilson 
874bcfb2e28SChris Wilson 	i = 0;
875bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
876bcfb2e28SChris Wilson 		i++;
877bcfb2e28SChris Wilson 	error->active_bo_count = i;
87805394f39SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
879bcfb2e28SChris Wilson 		i++;
880bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
881c724e8a9SChris Wilson 
8828e934dbfSChris Wilson 	error->active_bo = NULL;
8838e934dbfSChris Wilson 	error->pinned_bo = NULL;
884bcfb2e28SChris Wilson 	if (i) {
885bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
8869df30794SChris Wilson 					   GFP_ATOMIC);
887c724e8a9SChris Wilson 		if (error->active_bo)
888c724e8a9SChris Wilson 			error->pinned_bo =
889c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
8909df30794SChris Wilson 	}
891c724e8a9SChris Wilson 
892c724e8a9SChris Wilson 	if (error->active_bo)
893c724e8a9SChris Wilson 		error->active_bo_count =
894c724e8a9SChris Wilson 			capture_bo_list(error->active_bo,
895c724e8a9SChris Wilson 					error->active_bo_count,
896c724e8a9SChris Wilson 					&dev_priv->mm.active_list);
897c724e8a9SChris Wilson 
898c724e8a9SChris Wilson 	if (error->pinned_bo)
899c724e8a9SChris Wilson 		error->pinned_bo_count =
900c724e8a9SChris Wilson 			capture_bo_list(error->pinned_bo,
901c724e8a9SChris Wilson 					error->pinned_bo_count,
902c724e8a9SChris Wilson 					&dev_priv->mm.pinned_list);
90363eeaf38SJesse Barnes 
9048a905236SJesse Barnes 	do_gettimeofday(&error->time);
9058a905236SJesse Barnes 
9066ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
907c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
9086ef3d427SChris Wilson 
9099df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
9109df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
91163eeaf38SJesse Barnes 		dev_priv->first_error = error;
9129df30794SChris Wilson 		error = NULL;
9139df30794SChris Wilson 	}
91463eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
9159df30794SChris Wilson 
9169df30794SChris Wilson 	if (error)
9179df30794SChris Wilson 		i915_error_state_free(dev, error);
9189df30794SChris Wilson }
9199df30794SChris Wilson 
9209df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
9219df30794SChris Wilson {
9229df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
9239df30794SChris Wilson 	struct drm_i915_error_state *error;
9249df30794SChris Wilson 
9259df30794SChris Wilson 	spin_lock(&dev_priv->error_lock);
9269df30794SChris Wilson 	error = dev_priv->first_error;
9279df30794SChris Wilson 	dev_priv->first_error = NULL;
9289df30794SChris Wilson 	spin_unlock(&dev_priv->error_lock);
9299df30794SChris Wilson 
9309df30794SChris Wilson 	if (error)
9319df30794SChris Wilson 		i915_error_state_free(dev, error);
93263eeaf38SJesse Barnes }
9333bd3c932SChris Wilson #else
9343bd3c932SChris Wilson #define i915_capture_error_state(x)
9353bd3c932SChris Wilson #endif
93663eeaf38SJesse Barnes 
93735aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
938c0e09200SDave Airlie {
9398a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
94063eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
9419db4a9c7SJesse Barnes 	int pipe;
94263eeaf38SJesse Barnes 
94335aed2e6SChris Wilson 	if (!eir)
94435aed2e6SChris Wilson 		return;
94563eeaf38SJesse Barnes 
94663eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
94763eeaf38SJesse Barnes 	       eir);
9488a905236SJesse Barnes 
9498a905236SJesse Barnes 	if (IS_G4X(dev)) {
9508a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
9518a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
9528a905236SJesse Barnes 
9538a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
9548a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
9558a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
9568a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
9578a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
9588a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
9598a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
9608a905236SJesse Barnes 			       I915_READ(INSTPS));
9618a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
9628a905236SJesse Barnes 			       I915_READ(INSTDONE1));
9638a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
9648a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
9658a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
9663143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
9678a905236SJesse Barnes 		}
9688a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
9698a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
9708a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
9718a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
9728a905236SJesse Barnes 			       pgtbl_err);
9738a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
9743143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
9758a905236SJesse Barnes 		}
9768a905236SJesse Barnes 	}
9778a905236SJesse Barnes 
978a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
97963eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
98063eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
98163eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
98263eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
98363eeaf38SJesse Barnes 			       pgtbl_err);
98463eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
9853143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
98663eeaf38SJesse Barnes 		}
9878a905236SJesse Barnes 	}
9888a905236SJesse Barnes 
98963eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
9909db4a9c7SJesse Barnes 		printk(KERN_ERR "memory refresh error:\n");
9919db4a9c7SJesse Barnes 		for_each_pipe(pipe)
9929db4a9c7SJesse Barnes 			printk(KERN_ERR "pipe %c stat: 0x%08x\n",
9939db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
99463eeaf38SJesse Barnes 		/* pipestat has already been acked */
99563eeaf38SJesse Barnes 	}
99663eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
99763eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
99863eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
99963eeaf38SJesse Barnes 		       I915_READ(INSTPM));
1000a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
100163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
100263eeaf38SJesse Barnes 
100363eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
100463eeaf38SJesse Barnes 			       I915_READ(IPEIR));
100563eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
100663eeaf38SJesse Barnes 			       I915_READ(IPEHR));
100763eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
100863eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
100963eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
101063eeaf38SJesse Barnes 			       I915_READ(ACTHD));
101163eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
10123143a2bfSChris Wilson 			POSTING_READ(IPEIR);
101363eeaf38SJesse Barnes 		} else {
101463eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
101563eeaf38SJesse Barnes 
101663eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
101763eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
101863eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
101963eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
102063eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
102163eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
102263eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
102363eeaf38SJesse Barnes 			       I915_READ(INSTPS));
102463eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
102563eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
102663eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
102763eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
102863eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
10293143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
103063eeaf38SJesse Barnes 		}
103163eeaf38SJesse Barnes 	}
103263eeaf38SJesse Barnes 
103363eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
10343143a2bfSChris Wilson 	POSTING_READ(EIR);
103563eeaf38SJesse Barnes 	eir = I915_READ(EIR);
103663eeaf38SJesse Barnes 	if (eir) {
103763eeaf38SJesse Barnes 		/*
103863eeaf38SJesse Barnes 		 * some errors might have become stuck,
103963eeaf38SJesse Barnes 		 * mask them.
104063eeaf38SJesse Barnes 		 */
104163eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
104263eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
104363eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
104463eeaf38SJesse Barnes 	}
104535aed2e6SChris Wilson }
104635aed2e6SChris Wilson 
104735aed2e6SChris Wilson /**
104835aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
104935aed2e6SChris Wilson  * @dev: drm device
105035aed2e6SChris Wilson  *
105135aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
105235aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
105335aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
105435aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
105535aed2e6SChris Wilson  * of a ring dump etc.).
105635aed2e6SChris Wilson  */
1057527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
105835aed2e6SChris Wilson {
105935aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
106035aed2e6SChris Wilson 
106135aed2e6SChris Wilson 	i915_capture_error_state(dev);
106235aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
10638a905236SJesse Barnes 
1064ba1234d1SBen Gamari 	if (wedged) {
106530dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1066ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1067ba1234d1SBen Gamari 
106811ed50ecSBen Gamari 		/*
106911ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
107011ed50ecSBen Gamari 		 */
10711ec14ad3SChris Wilson 		wake_up_all(&dev_priv->ring[RCS].irq_queue);
1072f787a5f5SChris Wilson 		if (HAS_BSD(dev))
10731ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[VCS].irq_queue);
1074549f7365SChris Wilson 		if (HAS_BLT(dev))
10751ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[BCS].irq_queue);
107611ed50ecSBen Gamari 	}
107711ed50ecSBen Gamari 
10789c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
10798a905236SJesse Barnes }
10808a905236SJesse Barnes 
10814e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
10824e5359cdSSimon Farnsworth {
10834e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
10844e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10854e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
108605394f39SChris Wilson 	struct drm_i915_gem_object *obj;
10874e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
10884e5359cdSSimon Farnsworth 	unsigned long flags;
10894e5359cdSSimon Farnsworth 	bool stall_detected;
10904e5359cdSSimon Farnsworth 
10914e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
10924e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
10934e5359cdSSimon Farnsworth 		return;
10944e5359cdSSimon Farnsworth 
10954e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
10964e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
10974e5359cdSSimon Farnsworth 
10984e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
10994e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
11004e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
11014e5359cdSSimon Farnsworth 		return;
11024e5359cdSSimon Farnsworth 	}
11034e5359cdSSimon Farnsworth 
11044e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
110505394f39SChris Wilson 	obj = work->pending_flip_obj;
1106a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
11079db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
110805394f39SChris Wilson 		stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
11094e5359cdSSimon Farnsworth 	} else {
11109db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
111105394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
11124e5359cdSSimon Farnsworth 							crtc->y * crtc->fb->pitch +
11134e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
11144e5359cdSSimon Farnsworth 	}
11154e5359cdSSimon Farnsworth 
11164e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
11174e5359cdSSimon Farnsworth 
11184e5359cdSSimon Farnsworth 	if (stall_detected) {
11194e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
11204e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
11214e5359cdSSimon Farnsworth 	}
11224e5359cdSSimon Farnsworth }
11234e5359cdSSimon Farnsworth 
11248a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
11258a905236SJesse Barnes {
11268a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
11278a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11288a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
11298a905236SJesse Barnes 	u32 iir, new_iir;
11309db4a9c7SJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
11318a905236SJesse Barnes 	u32 vblank_status;
11328a905236SJesse Barnes 	int vblank = 0;
11338a905236SJesse Barnes 	unsigned long irqflags;
11348a905236SJesse Barnes 	int irq_received;
11359db4a9c7SJesse Barnes 	int ret = IRQ_NONE, pipe;
11369db4a9c7SJesse Barnes 	bool blc_event = false;
11378a905236SJesse Barnes 
11388a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
11398a905236SJesse Barnes 
11408a905236SJesse Barnes 	iir = I915_READ(IIR);
11418a905236SJesse Barnes 
1142a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
1143d874bcffSJesse Barnes 		vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
1144e25e6601SJesse Barnes 	else
1145d874bcffSJesse Barnes 		vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
11468a905236SJesse Barnes 
11478a905236SJesse Barnes 	for (;;) {
11488a905236SJesse Barnes 		irq_received = iir != 0;
11498a905236SJesse Barnes 
11508a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
11518a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
11528a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
11538a905236SJesse Barnes 		 * interrupts (for non-MSI).
11548a905236SJesse Barnes 		 */
11551ec14ad3SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
11568a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
1157ba1234d1SBen Gamari 			i915_handle_error(dev, false);
11588a905236SJesse Barnes 
11599db4a9c7SJesse Barnes 		for_each_pipe(pipe) {
11609db4a9c7SJesse Barnes 			int reg = PIPESTAT(pipe);
11619db4a9c7SJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
11629db4a9c7SJesse Barnes 
11638a905236SJesse Barnes 			/*
11649db4a9c7SJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
11658a905236SJesse Barnes 			 */
11669db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
11679db4a9c7SJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
11689db4a9c7SJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
11699db4a9c7SJesse Barnes 							 pipe_name(pipe));
11709db4a9c7SJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
11718a905236SJesse Barnes 				irq_received = 1;
11728a905236SJesse Barnes 			}
11738a905236SJesse Barnes 		}
11741ec14ad3SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
11758a905236SJesse Barnes 
11768a905236SJesse Barnes 		if (!irq_received)
11778a905236SJesse Barnes 			break;
11788a905236SJesse Barnes 
11798a905236SJesse Barnes 		ret = IRQ_HANDLED;
11808a905236SJesse Barnes 
11818a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
11828a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
11838a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
11848a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
11858a905236SJesse Barnes 
118644d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
11878a905236SJesse Barnes 				  hotplug_status);
11888a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
11899c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
11909c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
11918a905236SJesse Barnes 
11928a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
11938a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
119463eeaf38SJesse Barnes 		}
119563eeaf38SJesse Barnes 
1196673a394bSEric Anholt 		I915_WRITE(IIR, iir);
1197cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
11987c463586SKeith Packard 
11997c1c2871SDave Airlie 		if (dev->primary->master) {
12007c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
12017c1c2871SDave Airlie 			if (master_priv->sarea_priv)
12027c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
1203c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
12047c1c2871SDave Airlie 		}
12050a3e67a4SJesse Barnes 
1206549f7365SChris Wilson 		if (iir & I915_USER_INTERRUPT)
12071ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
12081ec14ad3SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
12091ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
1210d1b851fcSZou Nan hai 
12111afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
12126b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
12131afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
12141afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 0);
12151afe3e9dSJesse Barnes 		}
12166b95a207SKristian Høgsberg 
12171afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
121870565d00SJesse Barnes 			intel_prepare_page_flip(dev, 1);
12191afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
12201afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 1);
12211afe3e9dSJesse Barnes 		}
12226b95a207SKristian Høgsberg 
12239db4a9c7SJesse Barnes 		for_each_pipe(pipe) {
12249db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & vblank_status &&
12259db4a9c7SJesse Barnes 			    drm_handle_vblank(dev, pipe)) {
12267c463586SKeith Packard 				vblank++;
12274e5359cdSSimon Farnsworth 				if (!dev_priv->flip_pending_is_done) {
12289db4a9c7SJesse Barnes 					i915_pageflip_stall_check(dev, pipe);
12299db4a9c7SJesse Barnes 					intel_finish_page_flip(dev, pipe);
12307c463586SKeith Packard 				}
12314e5359cdSSimon Farnsworth 			}
12327c463586SKeith Packard 
12339db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
12349db4a9c7SJesse Barnes 				blc_event = true;
12354e5359cdSSimon Farnsworth 		}
12367c463586SKeith Packard 
12379db4a9c7SJesse Barnes 
12389db4a9c7SJesse Barnes 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
12393b617967SChris Wilson 			intel_opregion_asle_intr(dev);
12400a3e67a4SJesse Barnes 
1241cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
1242cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
1243cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
1244cdfbc41fSEric Anholt 		 * we would never get another interrupt.
1245cdfbc41fSEric Anholt 		 *
1246cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
1247cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
1248cdfbc41fSEric Anholt 		 * another one.
1249cdfbc41fSEric Anholt 		 *
1250cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
1251cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
1252cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
1253cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
1254cdfbc41fSEric Anholt 		 * stray interrupts.
1255cdfbc41fSEric Anholt 		 */
1256cdfbc41fSEric Anholt 		iir = new_iir;
125705eff845SKeith Packard 	}
1258cdfbc41fSEric Anholt 
125905eff845SKeith Packard 	return ret;
1260c0e09200SDave Airlie }
1261c0e09200SDave Airlie 
1262c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
1263c0e09200SDave Airlie {
1264c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
12657c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1266c0e09200SDave Airlie 
1267c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
1268c0e09200SDave Airlie 
126944d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
1270c0e09200SDave Airlie 
1271c99b058fSKristian Høgsberg 	dev_priv->counter++;
1272c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
1273c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
12747c1c2871SDave Airlie 	if (master_priv->sarea_priv)
12757c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
1276c0e09200SDave Airlie 
1277e1f99ce6SChris Wilson 	if (BEGIN_LP_RING(4) == 0) {
1278585fb111SJesse Barnes 		OUT_RING(MI_STORE_DWORD_INDEX);
12790baf823aSKeith Packard 		OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1280c0e09200SDave Airlie 		OUT_RING(dev_priv->counter);
1281585fb111SJesse Barnes 		OUT_RING(MI_USER_INTERRUPT);
1282c0e09200SDave Airlie 		ADVANCE_LP_RING();
1283e1f99ce6SChris Wilson 	}
1284c0e09200SDave Airlie 
1285c0e09200SDave Airlie 	return dev_priv->counter;
1286c0e09200SDave Airlie }
1287c0e09200SDave Airlie 
1288c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
1289c0e09200SDave Airlie {
1290c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
12917c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1292c0e09200SDave Airlie 	int ret = 0;
12931ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
1294c0e09200SDave Airlie 
129544d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
1296c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
1297c0e09200SDave Airlie 
1298ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
12997c1c2871SDave Airlie 		if (master_priv->sarea_priv)
13007c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
1301c0e09200SDave Airlie 		return 0;
1302ed4cb414SEric Anholt 	}
1303c0e09200SDave Airlie 
13047c1c2871SDave Airlie 	if (master_priv->sarea_priv)
13057c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1306c0e09200SDave Airlie 
1307b13c2b96SChris Wilson 	if (ring->irq_get(ring)) {
13081ec14ad3SChris Wilson 		DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1309c0e09200SDave Airlie 			    READ_BREADCRUMB(dev_priv) >= irq_nr);
13101ec14ad3SChris Wilson 		ring->irq_put(ring);
13115a9a8d1aSChris Wilson 	} else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
13125a9a8d1aSChris Wilson 		ret = -EBUSY;
1313c0e09200SDave Airlie 
1314c0e09200SDave Airlie 	if (ret == -EBUSY) {
1315c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
1316c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1317c0e09200SDave Airlie 	}
1318c0e09200SDave Airlie 
1319c0e09200SDave Airlie 	return ret;
1320c0e09200SDave Airlie }
1321c0e09200SDave Airlie 
1322c0e09200SDave Airlie /* Needs the lock as it touches the ring.
1323c0e09200SDave Airlie  */
1324c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
1325c0e09200SDave Airlie 			 struct drm_file *file_priv)
1326c0e09200SDave Airlie {
1327c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1328c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
1329c0e09200SDave Airlie 	int result;
1330c0e09200SDave Airlie 
13311ec14ad3SChris Wilson 	if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
1332c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1333c0e09200SDave Airlie 		return -EINVAL;
1334c0e09200SDave Airlie 	}
1335299eb93cSEric Anholt 
1336299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1337299eb93cSEric Anholt 
1338546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
1339c0e09200SDave Airlie 	result = i915_emit_irq(dev);
1340546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
1341c0e09200SDave Airlie 
1342c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
1343c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
1344c0e09200SDave Airlie 		return -EFAULT;
1345c0e09200SDave Airlie 	}
1346c0e09200SDave Airlie 
1347c0e09200SDave Airlie 	return 0;
1348c0e09200SDave Airlie }
1349c0e09200SDave Airlie 
1350c0e09200SDave Airlie /* Doesn't need the hardware lock.
1351c0e09200SDave Airlie  */
1352c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
1353c0e09200SDave Airlie 			 struct drm_file *file_priv)
1354c0e09200SDave Airlie {
1355c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1356c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
1357c0e09200SDave Airlie 
1358c0e09200SDave Airlie 	if (!dev_priv) {
1359c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1360c0e09200SDave Airlie 		return -EINVAL;
1361c0e09200SDave Airlie 	}
1362c0e09200SDave Airlie 
1363c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
1364c0e09200SDave Airlie }
1365c0e09200SDave Airlie 
136642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
136742f52ef8SKeith Packard  * we use as a pipe index
136842f52ef8SKeith Packard  */
136942f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
13700a3e67a4SJesse Barnes {
13710a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1372e9d21d7fSKeith Packard 	unsigned long irqflags;
137371e0ffa5SJesse Barnes 
13745eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
137571e0ffa5SJesse Barnes 		return -EINVAL;
13760a3e67a4SJesse Barnes 
13771ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1378bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1379c062df61SLi Peng 		ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1380c062df61SLi Peng 					    DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1381a6c45cf0SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 4)
13827c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
13837c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
13840a3e67a4SJesse Barnes 	else
13857c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
13867c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
13878692d00eSChris Wilson 
13888692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
13898692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
13908692d00eSChris Wilson 		I915_WRITE(INSTPM, INSTPM_AGPBUSY_DIS << 16);
13911ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
13928692d00eSChris Wilson 
13930a3e67a4SJesse Barnes 	return 0;
13940a3e67a4SJesse Barnes }
13950a3e67a4SJesse Barnes 
139642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
139742f52ef8SKeith Packard  * we use as a pipe index
139842f52ef8SKeith Packard  */
139942f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
14000a3e67a4SJesse Barnes {
14010a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1402e9d21d7fSKeith Packard 	unsigned long irqflags;
14030a3e67a4SJesse Barnes 
14041ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
14058692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
14068692d00eSChris Wilson 		I915_WRITE(INSTPM,
14078692d00eSChris Wilson 			   INSTPM_AGPBUSY_DIS << 16 | INSTPM_AGPBUSY_DIS);
14088692d00eSChris Wilson 
1409bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1410c062df61SLi Peng 		ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1411c062df61SLi Peng 					     DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1412c062df61SLi Peng 	else
14137c463586SKeith Packard 		i915_disable_pipestat(dev_priv, pipe,
14147c463586SKeith Packard 				      PIPE_VBLANK_INTERRUPT_ENABLE |
14157c463586SKeith Packard 				      PIPE_START_VBLANK_INTERRUPT_ENABLE);
14161ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14170a3e67a4SJesse Barnes }
14180a3e67a4SJesse Barnes 
1419c0e09200SDave Airlie /* Set the vblank monitor pipe
1420c0e09200SDave Airlie  */
1421c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1422c0e09200SDave Airlie 			 struct drm_file *file_priv)
1423c0e09200SDave Airlie {
1424c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1425c0e09200SDave Airlie 
1426c0e09200SDave Airlie 	if (!dev_priv) {
1427c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1428c0e09200SDave Airlie 		return -EINVAL;
1429c0e09200SDave Airlie 	}
1430c0e09200SDave Airlie 
1431c0e09200SDave Airlie 	return 0;
1432c0e09200SDave Airlie }
1433c0e09200SDave Airlie 
1434c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1435c0e09200SDave Airlie 			 struct drm_file *file_priv)
1436c0e09200SDave Airlie {
1437c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1438c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
1439c0e09200SDave Airlie 
1440c0e09200SDave Airlie 	if (!dev_priv) {
1441c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1442c0e09200SDave Airlie 		return -EINVAL;
1443c0e09200SDave Airlie 	}
1444c0e09200SDave Airlie 
14450a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1446c0e09200SDave Airlie 
1447c0e09200SDave Airlie 	return 0;
1448c0e09200SDave Airlie }
1449c0e09200SDave Airlie 
1450c0e09200SDave Airlie /**
1451c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
1452c0e09200SDave Airlie  */
1453c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
1454c0e09200SDave Airlie 		     struct drm_file *file_priv)
1455c0e09200SDave Airlie {
1456bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1457bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1458bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1459bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1460bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1461bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1462bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1463bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1464bd95e0a4SEric Anholt 	 *
1465bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1466bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1467bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1468bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
14690a3e67a4SJesse Barnes 	 */
1470c0e09200SDave Airlie 	return -EINVAL;
1471c0e09200SDave Airlie }
1472c0e09200SDave Airlie 
1473893eead0SChris Wilson static u32
1474893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1475852835f3SZou Nan hai {
1476893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1477893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1478893eead0SChris Wilson }
1479893eead0SChris Wilson 
1480893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1481893eead0SChris Wilson {
1482893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1483893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1484893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
1485b2223497SChris Wilson 		if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
1486893eead0SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1487893eead0SChris Wilson 				  ring->name,
1488b2223497SChris Wilson 				  ring->waiting_seqno,
1489893eead0SChris Wilson 				  ring->get_seqno(ring));
1490893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1491893eead0SChris Wilson 			*err = true;
1492893eead0SChris Wilson 		}
1493893eead0SChris Wilson 		return true;
1494893eead0SChris Wilson 	}
1495893eead0SChris Wilson 	return false;
1496f65d9421SBen Gamari }
1497f65d9421SBen Gamari 
14981ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
14991ec14ad3SChris Wilson {
15001ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
15011ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
15021ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
15031ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
15041ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
15051ec14ad3SChris Wilson 			  ring->name);
15061ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
15071ec14ad3SChris Wilson 		return true;
15081ec14ad3SChris Wilson 	}
15091ec14ad3SChris Wilson 	if (IS_GEN6(dev) &&
15101ec14ad3SChris Wilson 	    (tmp & RING_WAIT_SEMAPHORE)) {
15111ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
15121ec14ad3SChris Wilson 			  ring->name);
15131ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
15141ec14ad3SChris Wilson 		return true;
15151ec14ad3SChris Wilson 	}
15161ec14ad3SChris Wilson 	return false;
15171ec14ad3SChris Wilson }
15181ec14ad3SChris Wilson 
1519f65d9421SBen Gamari /**
1520f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1521f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1522f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1523f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1524f65d9421SBen Gamari  */
1525f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1526f65d9421SBen Gamari {
1527f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1528f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1529cbb465e7SChris Wilson 	uint32_t acthd, instdone, instdone1;
1530893eead0SChris Wilson 	bool err = false;
1531893eead0SChris Wilson 
1532893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
15331ec14ad3SChris Wilson 	if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
15341ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
15351ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
1536893eead0SChris Wilson 		dev_priv->hangcheck_count = 0;
1537893eead0SChris Wilson 		if (err)
1538893eead0SChris Wilson 			goto repeat;
1539893eead0SChris Wilson 		return;
1540893eead0SChris Wilson 	}
1541f65d9421SBen Gamari 
1542a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1543f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
1544cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1545cbb465e7SChris Wilson 		instdone1 = 0;
1546cbb465e7SChris Wilson 	} else {
1547f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
1548cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1549cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1550cbb465e7SChris Wilson 	}
1551f65d9421SBen Gamari 
1552cbb465e7SChris Wilson 	if (dev_priv->last_acthd == acthd &&
1553cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1554cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1555cbb465e7SChris Wilson 		if (dev_priv->hangcheck_count++ > 1) {
1556f65d9421SBen Gamari 			DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
15578c80b59bSChris Wilson 
15588c80b59bSChris Wilson 			if (!IS_GEN2(dev)) {
15598c80b59bSChris Wilson 				/* Is the chip hanging on a WAIT_FOR_EVENT?
15608c80b59bSChris Wilson 				 * If so we can simply poke the RB_WAIT bit
15618c80b59bSChris Wilson 				 * and break the hang. This should work on
15628c80b59bSChris Wilson 				 * all but the second generation chipsets.
15638c80b59bSChris Wilson 				 */
15641ec14ad3SChris Wilson 
15651ec14ad3SChris Wilson 				if (kick_ring(&dev_priv->ring[RCS]))
1566893eead0SChris Wilson 					goto repeat;
15671ec14ad3SChris Wilson 
15681ec14ad3SChris Wilson 				if (HAS_BSD(dev) &&
15691ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[VCS]))
15701ec14ad3SChris Wilson 					goto repeat;
15711ec14ad3SChris Wilson 
15721ec14ad3SChris Wilson 				if (HAS_BLT(dev) &&
15731ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[BCS]))
15741ec14ad3SChris Wilson 					goto repeat;
15758c80b59bSChris Wilson 			}
15768c80b59bSChris Wilson 
1577ba1234d1SBen Gamari 			i915_handle_error(dev, true);
1578f65d9421SBen Gamari 			return;
1579f65d9421SBen Gamari 		}
1580cbb465e7SChris Wilson 	} else {
1581cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1582cbb465e7SChris Wilson 
1583cbb465e7SChris Wilson 		dev_priv->last_acthd = acthd;
1584cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1585cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1586cbb465e7SChris Wilson 	}
1587f65d9421SBen Gamari 
1588893eead0SChris Wilson repeat:
1589f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1590b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1591b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1592f65d9421SBen Gamari }
1593f65d9421SBen Gamari 
1594c0e09200SDave Airlie /* drm_dma.h hooks
1595c0e09200SDave Airlie */
1596*4697995bSJesse Barnes void ironlake_irq_preinstall(struct drm_device *dev)
1597036a4a7dSZhenyu Wang {
1598036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1599036a4a7dSZhenyu Wang 
1600*4697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
1601*4697995bSJesse Barnes 
1602*4697995bSJesse Barnes 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
1603*4697995bSJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1604*4697995bSJesse Barnes 
1605036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1606036a4a7dSZhenyu Wang 
1607036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1608036a4a7dSZhenyu Wang 
1609036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1610036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
16113143a2bfSChris Wilson 	POSTING_READ(DEIER);
1612036a4a7dSZhenyu Wang 
1613036a4a7dSZhenyu Wang 	/* and GT */
1614036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1615036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
16163143a2bfSChris Wilson 	POSTING_READ(GTIER);
1617c650156aSZhenyu Wang 
1618c650156aSZhenyu Wang 	/* south display irq */
1619c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1620c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
16213143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1622036a4a7dSZhenyu Wang }
1623036a4a7dSZhenyu Wang 
1624*4697995bSJesse Barnes int ironlake_irq_postinstall(struct drm_device *dev)
1625036a4a7dSZhenyu Wang {
1626036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1627036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1628013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1629013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
16301ec14ad3SChris Wilson 	u32 render_irqs;
16312d7b8366SYuanhan Liu 	u32 hotplug_mask;
1632036a4a7dSZhenyu Wang 
1633*4697995bSJesse Barnes 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
1634*4697995bSJesse Barnes 	if (HAS_BSD(dev))
1635*4697995bSJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
1636*4697995bSJesse Barnes 	if (HAS_BLT(dev))
1637*4697995bSJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
1638*4697995bSJesse Barnes 
1639*4697995bSJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
16401ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1641036a4a7dSZhenyu Wang 
1642036a4a7dSZhenyu Wang 	/* should always can generate irq */
1643036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
16441ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
16451ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
16463143a2bfSChris Wilson 	POSTING_READ(DEIER);
1647036a4a7dSZhenyu Wang 
16481ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1649036a4a7dSZhenyu Wang 
1650036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
16511ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1652881f47b6SXiang, Haihao 
16531ec14ad3SChris Wilson 	if (IS_GEN6(dev))
16541ec14ad3SChris Wilson 		render_irqs =
16551ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
16561ec14ad3SChris Wilson 			GT_GEN6_BSD_USER_INTERRUPT |
16571ec14ad3SChris Wilson 			GT_BLT_USER_INTERRUPT;
16581ec14ad3SChris Wilson 	else
16591ec14ad3SChris Wilson 		render_irqs =
166088f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1661c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
16621ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
16631ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
16643143a2bfSChris Wilson 	POSTING_READ(GTIER);
1665036a4a7dSZhenyu Wang 
16662d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
16679035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
16689035a97aSChris Wilson 				SDE_PORTB_HOTPLUG_CPT |
16699035a97aSChris Wilson 				SDE_PORTC_HOTPLUG_CPT |
16709035a97aSChris Wilson 				SDE_PORTD_HOTPLUG_CPT);
16712d7b8366SYuanhan Liu 	} else {
16729035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG |
16739035a97aSChris Wilson 				SDE_PORTB_HOTPLUG |
16749035a97aSChris Wilson 				SDE_PORTC_HOTPLUG |
16759035a97aSChris Wilson 				SDE_PORTD_HOTPLUG |
16769035a97aSChris Wilson 				SDE_AUX_MASK);
16772d7b8366SYuanhan Liu 	}
16782d7b8366SYuanhan Liu 
16791ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1680c650156aSZhenyu Wang 
1681c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
16821ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
16831ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
16843143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1685c650156aSZhenyu Wang 
1686f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1687f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1688f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1689f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1690f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1691f97108d1SJesse Barnes 	}
1692f97108d1SJesse Barnes 
1693036a4a7dSZhenyu Wang 	return 0;
1694036a4a7dSZhenyu Wang }
1695036a4a7dSZhenyu Wang 
1696c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1697c0e09200SDave Airlie {
1698c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16999db4a9c7SJesse Barnes 	int pipe;
1700c0e09200SDave Airlie 
170179e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
170279e53945SJesse Barnes 
1703036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
17048a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
17054912d041SBen Widawsky 	INIT_WORK(&dev_priv->rps_work, gen6_pm_rps_work);
1706036a4a7dSZhenyu Wang 
17075ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
17085ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
17095ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
17105ca58282SJesse Barnes 	}
17115ca58282SJesse Barnes 
17120a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
17139db4a9c7SJesse Barnes 	for_each_pipe(pipe)
17149db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0);
17150a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1716ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
17173143a2bfSChris Wilson 	POSTING_READ(IER);
1718c0e09200SDave Airlie }
1719c0e09200SDave Airlie 
1720b01f2c3aSJesse Barnes /*
1721b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
1722b01f2c3aSJesse Barnes  * enabled correctly.
1723b01f2c3aSJesse Barnes  */
17240a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1725c0e09200SDave Airlie {
1726c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17275ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
172863eeaf38SJesse Barnes 	u32 error_mask;
17290a3e67a4SJesse Barnes 
17300a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1731ed4cb414SEric Anholt 
17327c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
17331ec14ad3SChris Wilson 	dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
17348ee1c3dbSMatthew Garrett 
17357c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
17367c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
17377c463586SKeith Packard 
17385ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
1739c496fa1fSAdam Jackson 		/* Enable in IER... */
1740c496fa1fSAdam Jackson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1741c496fa1fSAdam Jackson 		/* and unmask in IMR */
17421ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
1743c496fa1fSAdam Jackson 	}
1744c496fa1fSAdam Jackson 
1745c496fa1fSAdam Jackson 	/*
1746c496fa1fSAdam Jackson 	 * Enable some error detection, note the instruction error mask
1747c496fa1fSAdam Jackson 	 * bit is reserved, so we leave it masked.
1748c496fa1fSAdam Jackson 	 */
1749c496fa1fSAdam Jackson 	if (IS_G4X(dev)) {
1750c496fa1fSAdam Jackson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
1751c496fa1fSAdam Jackson 			       GM45_ERROR_MEM_PRIV |
1752c496fa1fSAdam Jackson 			       GM45_ERROR_CP_PRIV |
1753c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1754c496fa1fSAdam Jackson 	} else {
1755c496fa1fSAdam Jackson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
1756c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1757c496fa1fSAdam Jackson 	}
1758c496fa1fSAdam Jackson 	I915_WRITE(EMR, error_mask);
1759c496fa1fSAdam Jackson 
17601ec14ad3SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
1761c496fa1fSAdam Jackson 	I915_WRITE(IER, enable_mask);
17623143a2bfSChris Wilson 	POSTING_READ(IER);
1763c496fa1fSAdam Jackson 
1764c496fa1fSAdam Jackson 	if (I915_HAS_HOTPLUG(dev)) {
17655ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
17665ca58282SJesse Barnes 
1767b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
1768b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1769b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1770b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1771b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1772b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1773b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
1774b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1775b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1776b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1777b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
17782d1c9752SAndy Lutomirski 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
1779b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
17802d1c9752SAndy Lutomirski 
17812d1c9752SAndy Lutomirski 			/* Programming the CRT detection parameters tends
17822d1c9752SAndy Lutomirski 			   to generate a spurious hotplug event about three
17832d1c9752SAndy Lutomirski 			   seconds later.  So just do it once.
17842d1c9752SAndy Lutomirski 			*/
17852d1c9752SAndy Lutomirski 			if (IS_G4X(dev))
17862d1c9752SAndy Lutomirski 				hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
17872d1c9752SAndy Lutomirski 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
17882d1c9752SAndy Lutomirski 		}
17892d1c9752SAndy Lutomirski 
1790b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
1791b01f2c3aSJesse Barnes 
17925ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
17935ca58282SJesse Barnes 	}
17945ca58282SJesse Barnes 
17953b617967SChris Wilson 	intel_opregion_enable_asle(dev);
17960a3e67a4SJesse Barnes 
17970a3e67a4SJesse Barnes 	return 0;
1798c0e09200SDave Airlie }
1799c0e09200SDave Airlie 
1800*4697995bSJesse Barnes void ironlake_irq_uninstall(struct drm_device *dev)
1801036a4a7dSZhenyu Wang {
1802036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1803*4697995bSJesse Barnes 
1804*4697995bSJesse Barnes 	if (!dev_priv)
1805*4697995bSJesse Barnes 		return;
1806*4697995bSJesse Barnes 
1807*4697995bSJesse Barnes 	dev_priv->vblank_pipe = 0;
1808*4697995bSJesse Barnes 
1809036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1810036a4a7dSZhenyu Wang 
1811036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1812036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1813036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1814036a4a7dSZhenyu Wang 
1815036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1816036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1817036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1818036a4a7dSZhenyu Wang }
1819036a4a7dSZhenyu Wang 
1820c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1821c0e09200SDave Airlie {
1822c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18239db4a9c7SJesse Barnes 	int pipe;
1824c0e09200SDave Airlie 
1825c0e09200SDave Airlie 	if (!dev_priv)
1826c0e09200SDave Airlie 		return;
1827c0e09200SDave Airlie 
18280a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
18290a3e67a4SJesse Barnes 
18305ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
18315ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
18325ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
18335ca58282SJesse Barnes 	}
18345ca58282SJesse Barnes 
18350a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
18369db4a9c7SJesse Barnes 	for_each_pipe(pipe)
18379db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0);
18380a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1839ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1840c0e09200SDave Airlie 
18419db4a9c7SJesse Barnes 	for_each_pipe(pipe)
18429db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe),
18439db4a9c7SJesse Barnes 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
18447c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1845c0e09200SDave Airlie }
1846