xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 4fc688ce79772496503d22263d61b071a8fb596e)
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 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33760285e7SDavid Howells #include <drm/drmP.h>
34760285e7SDavid Howells #include <drm/i915_drm.h>
35c0e09200SDave Airlie #include "i915_drv.h"
361c5d22f7SChris Wilson #include "i915_trace.h"
3779e53945SJesse Barnes #include "intel_drv.h"
38c0e09200SDave Airlie 
39036a4a7dSZhenyu Wang /* For display hotplug interrupt */
40995b6762SChris Wilson static void
41f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
42036a4a7dSZhenyu Wang {
431ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
441ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
451ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
463143a2bfSChris Wilson 		POSTING_READ(DEIMR);
47036a4a7dSZhenyu Wang 	}
48036a4a7dSZhenyu Wang }
49036a4a7dSZhenyu Wang 
50036a4a7dSZhenyu Wang static inline void
51f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
52036a4a7dSZhenyu Wang {
531ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
541ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
551ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
563143a2bfSChris Wilson 		POSTING_READ(DEIMR);
57036a4a7dSZhenyu Wang 	}
58036a4a7dSZhenyu Wang }
59036a4a7dSZhenyu Wang 
607c463586SKeith Packard void
617c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
627c463586SKeith Packard {
637c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
649db4a9c7SJesse Barnes 		u32 reg = PIPESTAT(pipe);
657c463586SKeith Packard 
667c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
677c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
687c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
693143a2bfSChris Wilson 		POSTING_READ(reg);
707c463586SKeith Packard 	}
717c463586SKeith Packard }
727c463586SKeith Packard 
737c463586SKeith Packard void
747c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
757c463586SKeith Packard {
767c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
779db4a9c7SJesse Barnes 		u32 reg = PIPESTAT(pipe);
787c463586SKeith Packard 
797c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
807c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
813143a2bfSChris Wilson 		POSTING_READ(reg);
827c463586SKeith Packard 	}
837c463586SKeith Packard }
847c463586SKeith Packard 
85c0e09200SDave Airlie /**
8601c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
8701c66889SZhao Yakui  */
8801c66889SZhao Yakui void intel_enable_asle(struct drm_device *dev)
8901c66889SZhao Yakui {
901ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
911ec14ad3SChris Wilson 	unsigned long irqflags;
921ec14ad3SChris Wilson 
937e231dbeSJesse Barnes 	/* FIXME: opregion/asle for VLV */
947e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev))
957e231dbeSJesse Barnes 		return;
967e231dbeSJesse Barnes 
971ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
9801c66889SZhao Yakui 
99c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
100f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
101edcb49caSZhao Yakui 	else {
10201c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
103d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
104a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen >= 4)
105edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
106d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
107edcb49caSZhao Yakui 	}
1081ec14ad3SChris Wilson 
1091ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
11001c66889SZhao Yakui }
11101c66889SZhao Yakui 
11201c66889SZhao Yakui /**
1130a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1140a3e67a4SJesse Barnes  * @dev: DRM device
1150a3e67a4SJesse Barnes  * @pipe: pipe to check
1160a3e67a4SJesse Barnes  *
1170a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1180a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1190a3e67a4SJesse Barnes  * before reading such registers if unsure.
1200a3e67a4SJesse Barnes  */
1210a3e67a4SJesse Barnes static int
1220a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1230a3e67a4SJesse Barnes {
1240a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
125702e7a56SPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
126702e7a56SPaulo Zanoni 								      pipe);
127702e7a56SPaulo Zanoni 
128702e7a56SPaulo Zanoni 	return I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_ENABLE;
1290a3e67a4SJesse Barnes }
1300a3e67a4SJesse Barnes 
13142f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
13242f52ef8SKeith Packard  * we use as a pipe index
13342f52ef8SKeith Packard  */
134f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1350a3e67a4SJesse Barnes {
1360a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1370a3e67a4SJesse Barnes 	unsigned long high_frame;
1380a3e67a4SJesse Barnes 	unsigned long low_frame;
1395eddb70bSChris Wilson 	u32 high1, high2, low;
1400a3e67a4SJesse Barnes 
1410a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
14244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
1439db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
1440a3e67a4SJesse Barnes 		return 0;
1450a3e67a4SJesse Barnes 	}
1460a3e67a4SJesse Barnes 
1479db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
1489db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
1495eddb70bSChris Wilson 
1500a3e67a4SJesse Barnes 	/*
1510a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
1520a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
1530a3e67a4SJesse Barnes 	 * register.
1540a3e67a4SJesse Barnes 	 */
1550a3e67a4SJesse Barnes 	do {
1565eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1575eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
1585eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1590a3e67a4SJesse Barnes 	} while (high1 != high2);
1600a3e67a4SJesse Barnes 
1615eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
1625eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
1635eddb70bSChris Wilson 	return (high1 << 8) | low;
1640a3e67a4SJesse Barnes }
1650a3e67a4SJesse Barnes 
166f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
1679880b7a5SJesse Barnes {
1689880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1699db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
1709880b7a5SJesse Barnes 
1719880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
17244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
1739db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
1749880b7a5SJesse Barnes 		return 0;
1759880b7a5SJesse Barnes 	}
1769880b7a5SJesse Barnes 
1779880b7a5SJesse Barnes 	return I915_READ(reg);
1789880b7a5SJesse Barnes }
1799880b7a5SJesse Barnes 
180f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
1810af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
1820af7e4dfSMario Kleiner {
1830af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1840af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
1850af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
1860af7e4dfSMario Kleiner 	bool in_vbl = true;
1870af7e4dfSMario Kleiner 	int ret = 0;
188fe2b8f9dSPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
189fe2b8f9dSPaulo Zanoni 								      pipe);
1900af7e4dfSMario Kleiner 
1910af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
1920af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
1939db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
1940af7e4dfSMario Kleiner 		return 0;
1950af7e4dfSMario Kleiner 	}
1960af7e4dfSMario Kleiner 
1970af7e4dfSMario Kleiner 	/* Get vtotal. */
198fe2b8f9dSPaulo Zanoni 	vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
1990af7e4dfSMario Kleiner 
2000af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
2010af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
2020af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
2030af7e4dfSMario Kleiner 		 */
2040af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
2050af7e4dfSMario Kleiner 
2060af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
2070af7e4dfSMario Kleiner 		 * horizontal scanout position.
2080af7e4dfSMario Kleiner 		 */
2090af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
2100af7e4dfSMario Kleiner 		*hpos = 0;
2110af7e4dfSMario Kleiner 	} else {
2120af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
2130af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
2140af7e4dfSMario Kleiner 		 * scanout position.
2150af7e4dfSMario Kleiner 		 */
2160af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
2170af7e4dfSMario Kleiner 
218fe2b8f9dSPaulo Zanoni 		htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
2190af7e4dfSMario Kleiner 		*vpos = position / htotal;
2200af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
2210af7e4dfSMario Kleiner 	}
2220af7e4dfSMario Kleiner 
2230af7e4dfSMario Kleiner 	/* Query vblank area. */
224fe2b8f9dSPaulo Zanoni 	vbl = I915_READ(VBLANK(cpu_transcoder));
2250af7e4dfSMario Kleiner 
2260af7e4dfSMario Kleiner 	/* Test position against vblank region. */
2270af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
2280af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
2290af7e4dfSMario Kleiner 
2300af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
2310af7e4dfSMario Kleiner 		in_vbl = false;
2320af7e4dfSMario Kleiner 
2330af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
2340af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
2350af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
2360af7e4dfSMario Kleiner 
2370af7e4dfSMario Kleiner 	/* Readouts valid? */
2380af7e4dfSMario Kleiner 	if (vbl > 0)
2390af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
2400af7e4dfSMario Kleiner 
2410af7e4dfSMario Kleiner 	/* In vblank? */
2420af7e4dfSMario Kleiner 	if (in_vbl)
2430af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
2440af7e4dfSMario Kleiner 
2450af7e4dfSMario Kleiner 	return ret;
2460af7e4dfSMario Kleiner }
2470af7e4dfSMario Kleiner 
248f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
2490af7e4dfSMario Kleiner 			      int *max_error,
2500af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
2510af7e4dfSMario Kleiner 			      unsigned flags)
2520af7e4dfSMario Kleiner {
2534041b853SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2544041b853SChris Wilson 	struct drm_crtc *crtc;
2550af7e4dfSMario Kleiner 
2564041b853SChris Wilson 	if (pipe < 0 || pipe >= dev_priv->num_pipe) {
2574041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2580af7e4dfSMario Kleiner 		return -EINVAL;
2590af7e4dfSMario Kleiner 	}
2600af7e4dfSMario Kleiner 
2610af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
2624041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
2634041b853SChris Wilson 	if (crtc == NULL) {
2644041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2654041b853SChris Wilson 		return -EINVAL;
2664041b853SChris Wilson 	}
2674041b853SChris Wilson 
2684041b853SChris Wilson 	if (!crtc->enabled) {
2694041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
2704041b853SChris Wilson 		return -EBUSY;
2714041b853SChris Wilson 	}
2720af7e4dfSMario Kleiner 
2730af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
2744041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
2754041b853SChris Wilson 						     vblank_time, flags,
2764041b853SChris Wilson 						     crtc);
2770af7e4dfSMario Kleiner }
2780af7e4dfSMario Kleiner 
2795ca58282SJesse Barnes /*
2805ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
2815ca58282SJesse Barnes  */
2825ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
2835ca58282SJesse Barnes {
2845ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
2855ca58282SJesse Barnes 						    hotplug_work);
2865ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
287c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
2884ef69c7aSChris Wilson 	struct intel_encoder *encoder;
2895ca58282SJesse Barnes 
290a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
291e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
292e67189abSJesse Barnes 
2934ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
2944ef69c7aSChris Wilson 		if (encoder->hot_plug)
2954ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
296c31c4ba3SKeith Packard 
29740ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
29840ee3381SKeith Packard 
2995ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
300eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3015ca58282SJesse Barnes }
3025ca58282SJesse Barnes 
3039270388eSDaniel Vetter /* defined intel_pm.c */
3049270388eSDaniel Vetter extern spinlock_t mchdev_lock;
3059270388eSDaniel Vetter 
30673edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
307f97108d1SJesse Barnes {
308f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
309b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
3109270388eSDaniel Vetter 	u8 new_delay;
3119270388eSDaniel Vetter 	unsigned long flags;
3129270388eSDaniel Vetter 
3139270388eSDaniel Vetter 	spin_lock_irqsave(&mchdev_lock, flags);
314f97108d1SJesse Barnes 
31573edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
31673edd18fSDaniel Vetter 
31720e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
3189270388eSDaniel Vetter 
3197648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
320b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
321b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
322f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
323f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
324f97108d1SJesse Barnes 
325f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
326b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
32720e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
32820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
32920e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
33020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
331b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
33220e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
33320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
33420e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
33520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
336f97108d1SJesse Barnes 	}
337f97108d1SJesse Barnes 
3387648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
33920e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
340f97108d1SJesse Barnes 
3419270388eSDaniel Vetter 	spin_unlock_irqrestore(&mchdev_lock, flags);
3429270388eSDaniel Vetter 
343f97108d1SJesse Barnes 	return;
344f97108d1SJesse Barnes }
345f97108d1SJesse Barnes 
346549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
347549f7365SChris Wilson 			struct intel_ring_buffer *ring)
348549f7365SChris Wilson {
349549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
3509862e600SChris Wilson 
351475553deSChris Wilson 	if (ring->obj == NULL)
352475553deSChris Wilson 		return;
353475553deSChris Wilson 
354b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
3559862e600SChris Wilson 
356549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3573e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
358549f7365SChris Wilson 		dev_priv->hangcheck_count = 0;
359549f7365SChris Wilson 		mod_timer(&dev_priv->hangcheck_timer,
360cecc21feSChris Wilson 			  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
3613e0dc6b0SBen Widawsky 	}
362549f7365SChris Wilson }
363549f7365SChris Wilson 
3644912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
3653b8d8d91SJesse Barnes {
3664912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
367c6a828d3SDaniel Vetter 						    rps.work);
3684912d041SBen Widawsky 	u32 pm_iir, pm_imr;
3697b9e0ae6SChris Wilson 	u8 new_delay;
3703b8d8d91SJesse Barnes 
371c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
372c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
373c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
3744912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
375a9e2641dSDaniel Vetter 	I915_WRITE(GEN6_PMIMR, 0);
376c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
3774912d041SBen Widawsky 
3787b9e0ae6SChris Wilson 	if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
3793b8d8d91SJesse Barnes 		return;
3803b8d8d91SJesse Barnes 
381*4fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
3827b9e0ae6SChris Wilson 
3837b9e0ae6SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
384c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
3857b9e0ae6SChris Wilson 	else
386c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
3873b8d8d91SJesse Barnes 
38879249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
38979249636SBen Widawsky 	 * interrupt
39079249636SBen Widawsky 	 */
39179249636SBen Widawsky 	if (!(new_delay > dev_priv->rps.max_delay ||
39279249636SBen Widawsky 	      new_delay < dev_priv->rps.min_delay)) {
3934912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
39479249636SBen Widawsky 	}
3953b8d8d91SJesse Barnes 
396*4fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
3973b8d8d91SJesse Barnes }
3983b8d8d91SJesse Barnes 
399e3689190SBen Widawsky 
400e3689190SBen Widawsky /**
401e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
402e3689190SBen Widawsky  * occurred.
403e3689190SBen Widawsky  * @work: workqueue struct
404e3689190SBen Widawsky  *
405e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
406e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
407e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
408e3689190SBen Widawsky  */
409e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
410e3689190SBen Widawsky {
411e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
412a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
413e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
414e3689190SBen Widawsky 	char *parity_event[5];
415e3689190SBen Widawsky 	uint32_t misccpctl;
416e3689190SBen Widawsky 	unsigned long flags;
417e3689190SBen Widawsky 
418e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
419e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
420e3689190SBen Widawsky 	 * any time we access those registers.
421e3689190SBen Widawsky 	 */
422e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
423e3689190SBen Widawsky 
424e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
425e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
426e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
427e3689190SBen Widawsky 
428e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
429e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
430e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
431e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
432e3689190SBen Widawsky 
433e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
434e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
435e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
436e3689190SBen Widawsky 
437e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
438e3689190SBen Widawsky 
439e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
440e3689190SBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
441e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
442e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
443e3689190SBen Widawsky 
444e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
445e3689190SBen Widawsky 
446e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
447e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
448e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
449e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
450e3689190SBen Widawsky 	parity_event[4] = NULL;
451e3689190SBen Widawsky 
452e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
453e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
454e3689190SBen Widawsky 
455e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
456e3689190SBen Widawsky 		  row, bank, subbank);
457e3689190SBen Widawsky 
458e3689190SBen Widawsky 	kfree(parity_event[3]);
459e3689190SBen Widawsky 	kfree(parity_event[2]);
460e3689190SBen Widawsky 	kfree(parity_event[1]);
461e3689190SBen Widawsky }
462e3689190SBen Widawsky 
463d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
464e3689190SBen Widawsky {
465e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
466e3689190SBen Widawsky 	unsigned long flags;
467e3689190SBen Widawsky 
468e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
469e3689190SBen Widawsky 		return;
470e3689190SBen Widawsky 
471e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
472e3689190SBen Widawsky 	dev_priv->gt_irq_mask |= GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
473e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
474e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
475e3689190SBen Widawsky 
476a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
477e3689190SBen Widawsky }
478e3689190SBen Widawsky 
479e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
480e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
481e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
482e7b4c6b1SDaniel Vetter {
483e7b4c6b1SDaniel Vetter 
484e7b4c6b1SDaniel Vetter 	if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
485e7b4c6b1SDaniel Vetter 		      GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
486e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
487e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BSD_USER_INTERRUPT)
488e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
489e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
490e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
491e7b4c6b1SDaniel Vetter 
492e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
493e7b4c6b1SDaniel Vetter 		      GT_GEN6_BSD_CS_ERROR_INTERRUPT |
494e7b4c6b1SDaniel Vetter 		      GT_RENDER_CS_ERROR_INTERRUPT)) {
495e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
496e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
497e7b4c6b1SDaniel Vetter 	}
498e3689190SBen Widawsky 
499e3689190SBen Widawsky 	if (gt_iir & GT_GEN7_L3_PARITY_ERROR_INTERRUPT)
500e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
501e7b4c6b1SDaniel Vetter }
502e7b4c6b1SDaniel Vetter 
503fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
504fc6826d1SChris Wilson 				u32 pm_iir)
505fc6826d1SChris Wilson {
506fc6826d1SChris Wilson 	unsigned long flags;
507fc6826d1SChris Wilson 
508fc6826d1SChris Wilson 	/*
509fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
510fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
511fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
512c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
513fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
514fc6826d1SChris Wilson 	 *
515c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
516fc6826d1SChris Wilson 	 */
517fc6826d1SChris Wilson 
518c6a828d3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
519c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
520c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
521fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
522c6a828d3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
523fc6826d1SChris Wilson 
524c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
525fc6826d1SChris Wilson }
526fc6826d1SChris Wilson 
527ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
5287e231dbeSJesse Barnes {
5297e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
5307e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5317e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
5327e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
5337e231dbeSJesse Barnes 	unsigned long irqflags;
5347e231dbeSJesse Barnes 	int pipe;
5357e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
5367e231dbeSJesse Barnes 	bool blc_event;
5377e231dbeSJesse Barnes 
5387e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5397e231dbeSJesse Barnes 
5407e231dbeSJesse Barnes 	while (true) {
5417e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
5427e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
5437e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
5447e231dbeSJesse Barnes 
5457e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
5467e231dbeSJesse Barnes 			goto out;
5477e231dbeSJesse Barnes 
5487e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
5497e231dbeSJesse Barnes 
550e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
5517e231dbeSJesse Barnes 
5527e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5537e231dbeSJesse Barnes 		for_each_pipe(pipe) {
5547e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
5557e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
5567e231dbeSJesse Barnes 
5577e231dbeSJesse Barnes 			/*
5587e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
5597e231dbeSJesse Barnes 			 */
5607e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
5617e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
5627e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
5637e231dbeSJesse Barnes 							 pipe_name(pipe));
5647e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
5657e231dbeSJesse Barnes 			}
5667e231dbeSJesse Barnes 		}
5677e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5687e231dbeSJesse Barnes 
56931acc7f5SJesse Barnes 		for_each_pipe(pipe) {
57031acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
57131acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
57231acc7f5SJesse Barnes 
57331acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
57431acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
57531acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
57631acc7f5SJesse Barnes 			}
57731acc7f5SJesse Barnes 		}
57831acc7f5SJesse Barnes 
5797e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
5807e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
5817e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
5827e231dbeSJesse Barnes 
5837e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
5847e231dbeSJesse Barnes 					 hotplug_status);
5857e231dbeSJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
5867e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
5877e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
5887e231dbeSJesse Barnes 
5897e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
5907e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
5917e231dbeSJesse Barnes 		}
5927e231dbeSJesse Barnes 
5937e231dbeSJesse Barnes 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
5947e231dbeSJesse Barnes 			blc_event = true;
5957e231dbeSJesse Barnes 
596fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
597fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
5987e231dbeSJesse Barnes 
5997e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
6007e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
6017e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
6027e231dbeSJesse Barnes 	}
6037e231dbeSJesse Barnes 
6047e231dbeSJesse Barnes out:
6057e231dbeSJesse Barnes 	return ret;
6067e231dbeSJesse Barnes }
6077e231dbeSJesse Barnes 
60823e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
609776ad806SJesse Barnes {
610776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6119db4a9c7SJesse Barnes 	int pipe;
612776ad806SJesse Barnes 
61376e43830SDaniel Vetter 	if (pch_iir & SDE_HOTPLUG_MASK)
61476e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
61576e43830SDaniel Vetter 
616776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
617776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
618776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
619776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
620776ad806SJesse Barnes 
621776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
622776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
623776ad806SJesse Barnes 
624776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
625776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
626776ad806SJesse Barnes 
627776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
628776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
629776ad806SJesse Barnes 
630776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
631776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
632776ad806SJesse Barnes 
6339db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
6349db4a9c7SJesse Barnes 		for_each_pipe(pipe)
6359db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
6369db4a9c7SJesse Barnes 					 pipe_name(pipe),
6379db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
638776ad806SJesse Barnes 
639776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
640776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
641776ad806SJesse Barnes 
642776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
643776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
644776ad806SJesse Barnes 
645776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
646776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
647776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
648776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
649776ad806SJesse Barnes }
650776ad806SJesse Barnes 
65123e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
65223e81d69SAdam Jackson {
65323e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
65423e81d69SAdam Jackson 	int pipe;
65523e81d69SAdam Jackson 
65676e43830SDaniel Vetter 	if (pch_iir & SDE_HOTPLUG_MASK_CPT)
65776e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
65876e43830SDaniel Vetter 
65923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT)
66023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
66123e81d69SAdam Jackson 				 (pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
66223e81d69SAdam Jackson 				 SDE_AUDIO_POWER_SHIFT_CPT);
66323e81d69SAdam Jackson 
66423e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
66523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("AUX channel interrupt\n");
66623e81d69SAdam Jackson 
66723e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
66823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
66923e81d69SAdam Jackson 
67023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
67123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
67223e81d69SAdam Jackson 
67323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
67423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
67523e81d69SAdam Jackson 
67623e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
67723e81d69SAdam Jackson 		for_each_pipe(pipe)
67823e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
67923e81d69SAdam Jackson 					 pipe_name(pipe),
68023e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
68123e81d69SAdam Jackson }
68223e81d69SAdam Jackson 
683ff1f525eSDaniel Vetter static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
684b1f14ad0SJesse Barnes {
685b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
686b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6870e43406bSChris Wilson 	u32 de_iir, gt_iir, de_ier, pm_iir;
6880e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
6890e43406bSChris Wilson 	int i;
690b1f14ad0SJesse Barnes 
691b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
692b1f14ad0SJesse Barnes 
693b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
694b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
695b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
6960e43406bSChris Wilson 
6970e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
6980e43406bSChris Wilson 	if (gt_iir) {
6990e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
7000e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
7010e43406bSChris Wilson 		ret = IRQ_HANDLED;
7020e43406bSChris Wilson 	}
703b1f14ad0SJesse Barnes 
704b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
7050e43406bSChris Wilson 	if (de_iir) {
706b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
707b1f14ad0SJesse Barnes 			intel_opregion_gse_intr(dev);
708b1f14ad0SJesse Barnes 
7090e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
71074d44445SDaniel Vetter 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
71174d44445SDaniel Vetter 				drm_handle_vblank(dev, i);
7120e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
7130e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
7140e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
715b1f14ad0SJesse Barnes 			}
716b1f14ad0SJesse Barnes 		}
717b1f14ad0SJesse Barnes 
718b1f14ad0SJesse Barnes 		/* check event from PCH */
719b1f14ad0SJesse Barnes 		if (de_iir & DE_PCH_EVENT_IVB) {
7200e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
7210e43406bSChris Wilson 
72223e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
7230e43406bSChris Wilson 
7240e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
7250e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
726b1f14ad0SJesse Barnes 		}
727b1f14ad0SJesse Barnes 
7280e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
7290e43406bSChris Wilson 		ret = IRQ_HANDLED;
7300e43406bSChris Wilson 	}
7310e43406bSChris Wilson 
7320e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
7330e43406bSChris Wilson 	if (pm_iir) {
734fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
735fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
736b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
7370e43406bSChris Wilson 		ret = IRQ_HANDLED;
7380e43406bSChris Wilson 	}
739b1f14ad0SJesse Barnes 
740b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
741b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
742b1f14ad0SJesse Barnes 
743b1f14ad0SJesse Barnes 	return ret;
744b1f14ad0SJesse Barnes }
745b1f14ad0SJesse Barnes 
746e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
747e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
748e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
749e7b4c6b1SDaniel Vetter {
750e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
751e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
752e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
753e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
754e7b4c6b1SDaniel Vetter }
755e7b4c6b1SDaniel Vetter 
756ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
757036a4a7dSZhenyu Wang {
7584697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
759036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
760036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
7613b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
762881f47b6SXiang, Haihao 
7634697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
7644697995bSJesse Barnes 
7652d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
7662d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
7672d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
7683143a2bfSChris Wilson 	POSTING_READ(DEIER);
7692d109a84SZou, Nanhai 
770036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
771036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
772c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
7733b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
774036a4a7dSZhenyu Wang 
7753b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
7763b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
777c7c85101SZou Nan hai 		goto done;
778036a4a7dSZhenyu Wang 
779036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
780036a4a7dSZhenyu Wang 
781e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
782e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
783e7b4c6b1SDaniel Vetter 	else
784e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
785036a4a7dSZhenyu Wang 
78601c66889SZhao Yakui 	if (de_iir & DE_GSE)
7873b617967SChris Wilson 		intel_opregion_gse_intr(dev);
78801c66889SZhao Yakui 
78974d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
79074d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
79174d44445SDaniel Vetter 
79274d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
79374d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
79474d44445SDaniel Vetter 
795f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
796013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
7972bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
798013d5aa2SJesse Barnes 	}
799013d5aa2SJesse Barnes 
800f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
801f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
8022bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
803013d5aa2SJesse Barnes 	}
804c062df61SLi Peng 
805c650156aSZhenyu Wang 	/* check event from PCH */
806776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
80723e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
80823e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
80923e81d69SAdam Jackson 		else
81023e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
811776ad806SJesse Barnes 	}
812c650156aSZhenyu Wang 
81373edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
81473edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
815f97108d1SJesse Barnes 
816fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
817fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
8183b8d8d91SJesse Barnes 
819c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
820c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
821c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
822c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
8234912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
824036a4a7dSZhenyu Wang 
825c7c85101SZou Nan hai done:
8262d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
8273143a2bfSChris Wilson 	POSTING_READ(DEIER);
8282d109a84SZou, Nanhai 
829036a4a7dSZhenyu Wang 	return ret;
830036a4a7dSZhenyu Wang }
831036a4a7dSZhenyu Wang 
8328a905236SJesse Barnes /**
8338a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
8348a905236SJesse Barnes  * @work: work struct
8358a905236SJesse Barnes  *
8368a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
8378a905236SJesse Barnes  * was detected.
8388a905236SJesse Barnes  */
8398a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
8408a905236SJesse Barnes {
8418a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
8428a905236SJesse Barnes 						    error_work);
8438a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
844f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
845f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
846f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
8478a905236SJesse Barnes 
848f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
8498a905236SJesse Barnes 
850ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
85144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
852f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
853d4b8bb2aSDaniel Vetter 		if (!i915_reset(dev)) {
854ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
855f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
856f316a42cSBen Gamari 		}
85730dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
858f316a42cSBen Gamari 	}
8598a905236SJesse Barnes }
8608a905236SJesse Barnes 
86185f9e50dSDaniel Vetter /* NB: please notice the memset */
86285f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
86385f9e50dSDaniel Vetter 				    uint32_t *instdone)
86485f9e50dSDaniel Vetter {
86585f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
86685f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
86785f9e50dSDaniel Vetter 
86885f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
86985f9e50dSDaniel Vetter 	case 2:
87085f9e50dSDaniel Vetter 	case 3:
87185f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
87285f9e50dSDaniel Vetter 		break;
87385f9e50dSDaniel Vetter 	case 4:
87485f9e50dSDaniel Vetter 	case 5:
87585f9e50dSDaniel Vetter 	case 6:
87685f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
87785f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
87885f9e50dSDaniel Vetter 		break;
87985f9e50dSDaniel Vetter 	default:
88085f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
88185f9e50dSDaniel Vetter 	case 7:
88285f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
88385f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
88485f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
88585f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
88685f9e50dSDaniel Vetter 		break;
88785f9e50dSDaniel Vetter 	}
88885f9e50dSDaniel Vetter }
88985f9e50dSDaniel Vetter 
8903bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
8919df30794SChris Wilson static struct drm_i915_error_object *
892bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
89305394f39SChris Wilson 			 struct drm_i915_gem_object *src)
8949df30794SChris Wilson {
8959df30794SChris Wilson 	struct drm_i915_error_object *dst;
8969da3da66SChris Wilson 	int i, count;
897e56660ddSChris Wilson 	u32 reloc_offset;
8989df30794SChris Wilson 
89905394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
9009df30794SChris Wilson 		return NULL;
9019df30794SChris Wilson 
9029da3da66SChris Wilson 	count = src->base.size / PAGE_SIZE;
9039df30794SChris Wilson 
9049da3da66SChris Wilson 	dst = kmalloc(sizeof(*dst) + count * sizeof(u32 *), GFP_ATOMIC);
9059df30794SChris Wilson 	if (dst == NULL)
9069df30794SChris Wilson 		return NULL;
9079df30794SChris Wilson 
90805394f39SChris Wilson 	reloc_offset = src->gtt_offset;
9099da3da66SChris Wilson 	for (i = 0; i < count; i++) {
910788885aeSAndrew Morton 		unsigned long flags;
911e56660ddSChris Wilson 		void *d;
912788885aeSAndrew Morton 
913e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
9149df30794SChris Wilson 		if (d == NULL)
9159df30794SChris Wilson 			goto unwind;
916e56660ddSChris Wilson 
917788885aeSAndrew Morton 		local_irq_save(flags);
91874898d7eSDaniel Vetter 		if (reloc_offset < dev_priv->mm.gtt_mappable_end &&
91974898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
920172975aaSChris Wilson 			void __iomem *s;
921172975aaSChris Wilson 
922172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
923172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
924172975aaSChris Wilson 			 * captures what the GPU read.
925172975aaSChris Wilson 			 */
926172975aaSChris Wilson 
927e56660ddSChris Wilson 			s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
9283e4d3af5SPeter Zijlstra 						     reloc_offset);
929e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
9303e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
931172975aaSChris Wilson 		} else {
9329da3da66SChris Wilson 			struct page *page;
933172975aaSChris Wilson 			void *s;
934172975aaSChris Wilson 
9359da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
936172975aaSChris Wilson 
9379da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
9389da3da66SChris Wilson 
9399da3da66SChris Wilson 			s = kmap_atomic(page);
940172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
941172975aaSChris Wilson 			kunmap_atomic(s);
942172975aaSChris Wilson 
9439da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
944172975aaSChris Wilson 		}
945788885aeSAndrew Morton 		local_irq_restore(flags);
946e56660ddSChris Wilson 
9479da3da66SChris Wilson 		dst->pages[i] = d;
948e56660ddSChris Wilson 
949e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
9509df30794SChris Wilson 	}
9519da3da66SChris Wilson 	dst->page_count = count;
95205394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
9539df30794SChris Wilson 
9549df30794SChris Wilson 	return dst;
9559df30794SChris Wilson 
9569df30794SChris Wilson unwind:
9579da3da66SChris Wilson 	while (i--)
9589da3da66SChris Wilson 		kfree(dst->pages[i]);
9599df30794SChris Wilson 	kfree(dst);
9609df30794SChris Wilson 	return NULL;
9619df30794SChris Wilson }
9629df30794SChris Wilson 
9639df30794SChris Wilson static void
9649df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
9659df30794SChris Wilson {
9669df30794SChris Wilson 	int page;
9679df30794SChris Wilson 
9689df30794SChris Wilson 	if (obj == NULL)
9699df30794SChris Wilson 		return;
9709df30794SChris Wilson 
9719df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
9729df30794SChris Wilson 		kfree(obj->pages[page]);
9739df30794SChris Wilson 
9749df30794SChris Wilson 	kfree(obj);
9759df30794SChris Wilson }
9769df30794SChris Wilson 
977742cbee8SDaniel Vetter void
978742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
9799df30794SChris Wilson {
980742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
981742cbee8SDaniel Vetter 							  typeof(*error), ref);
982e2f973d5SChris Wilson 	int i;
983e2f973d5SChris Wilson 
98452d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
98552d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
98652d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
98752d39a21SChris Wilson 		kfree(error->ring[i].requests);
98852d39a21SChris Wilson 	}
989e2f973d5SChris Wilson 
9909df30794SChris Wilson 	kfree(error->active_bo);
9916ef3d427SChris Wilson 	kfree(error->overlay);
9929df30794SChris Wilson 	kfree(error);
9939df30794SChris Wilson }
9941b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
9951b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
996c724e8a9SChris Wilson {
997c724e8a9SChris Wilson 	err->size = obj->base.size;
998c724e8a9SChris Wilson 	err->name = obj->base.name;
9990201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
10000201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1001c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
1002c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1003c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1004c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1005c724e8a9SChris Wilson 	err->pinned = 0;
1006c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1007c724e8a9SChris Wilson 		err->pinned = 1;
1008c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1009c724e8a9SChris Wilson 		err->pinned = -1;
1010c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1011c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1012c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
101396154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
101493dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
10151b50247aSChris Wilson }
1016c724e8a9SChris Wilson 
10171b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
10181b50247aSChris Wilson 			     int count, struct list_head *head)
10191b50247aSChris Wilson {
10201b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
10211b50247aSChris Wilson 	int i = 0;
10221b50247aSChris Wilson 
10231b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
10241b50247aSChris Wilson 		capture_bo(err++, obj);
1025c724e8a9SChris Wilson 		if (++i == count)
1026c724e8a9SChris Wilson 			break;
10271b50247aSChris Wilson 	}
1028c724e8a9SChris Wilson 
10291b50247aSChris Wilson 	return i;
10301b50247aSChris Wilson }
10311b50247aSChris Wilson 
10321b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
10331b50247aSChris Wilson 			     int count, struct list_head *head)
10341b50247aSChris Wilson {
10351b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
10361b50247aSChris Wilson 	int i = 0;
10371b50247aSChris Wilson 
10381b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
10391b50247aSChris Wilson 		if (obj->pin_count == 0)
10401b50247aSChris Wilson 			continue;
10411b50247aSChris Wilson 
10421b50247aSChris Wilson 		capture_bo(err++, obj);
10431b50247aSChris Wilson 		if (++i == count)
10441b50247aSChris Wilson 			break;
1045c724e8a9SChris Wilson 	}
1046c724e8a9SChris Wilson 
1047c724e8a9SChris Wilson 	return i;
1048c724e8a9SChris Wilson }
1049c724e8a9SChris Wilson 
1050748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1051748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1052748ebc60SChris Wilson {
1053748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1054748ebc60SChris Wilson 	int i;
1055748ebc60SChris Wilson 
1056748ebc60SChris Wilson 	/* Fences */
1057748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1058775d17b6SDaniel Vetter 	case 7:
1059748ebc60SChris Wilson 	case 6:
1060748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1061748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1062748ebc60SChris Wilson 		break;
1063748ebc60SChris Wilson 	case 5:
1064748ebc60SChris Wilson 	case 4:
1065748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1066748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1067748ebc60SChris Wilson 		break;
1068748ebc60SChris Wilson 	case 3:
1069748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1070748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1071748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1072748ebc60SChris Wilson 	case 2:
1073748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1074748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1075748ebc60SChris Wilson 		break;
1076748ebc60SChris Wilson 
1077748ebc60SChris Wilson 	}
1078748ebc60SChris Wilson }
1079748ebc60SChris Wilson 
1080bcfb2e28SChris Wilson static struct drm_i915_error_object *
1081bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1082bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1083bcfb2e28SChris Wilson {
1084bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1085bcfb2e28SChris Wilson 	u32 seqno;
1086bcfb2e28SChris Wilson 
1087bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1088bcfb2e28SChris Wilson 		return NULL;
1089bcfb2e28SChris Wilson 
1090b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1091bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1092bcfb2e28SChris Wilson 		if (obj->ring != ring)
1093bcfb2e28SChris Wilson 			continue;
1094bcfb2e28SChris Wilson 
10950201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1096bcfb2e28SChris Wilson 			continue;
1097bcfb2e28SChris Wilson 
1098bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1099bcfb2e28SChris Wilson 			continue;
1100bcfb2e28SChris Wilson 
1101bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1102bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1103bcfb2e28SChris Wilson 		 */
1104bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1105bcfb2e28SChris Wilson 	}
1106bcfb2e28SChris Wilson 
1107bcfb2e28SChris Wilson 	return NULL;
1108bcfb2e28SChris Wilson }
1109bcfb2e28SChris Wilson 
1110d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1111d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1112d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1113d27b1e0eSDaniel Vetter {
1114d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1115d27b1e0eSDaniel Vetter 
111633f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
111712f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
111833f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
11197e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
11207e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
11217e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
11227e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
112333f3f518SDaniel Vetter 	}
1124c1cd90edSDaniel Vetter 
1125d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
11269d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1127d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1128d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1129d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1130c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1131050ee91fSBen Widawsky 		if (ring->id == RCS)
1132d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1133d27b1e0eSDaniel Vetter 	} else {
11349d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1135d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1136d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1137d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1138d27b1e0eSDaniel Vetter 	}
1139d27b1e0eSDaniel Vetter 
11409574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1141c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1142b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1143d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1144c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1145c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
11467e3b8737SDaniel Vetter 
11477e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
11487e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1149d27b1e0eSDaniel Vetter }
1150d27b1e0eSDaniel Vetter 
115152d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
115252d39a21SChris Wilson 				  struct drm_i915_error_state *error)
115352d39a21SChris Wilson {
115452d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1155b4519513SChris Wilson 	struct intel_ring_buffer *ring;
115652d39a21SChris Wilson 	struct drm_i915_gem_request *request;
115752d39a21SChris Wilson 	int i, count;
115852d39a21SChris Wilson 
1159b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
116052d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
116152d39a21SChris Wilson 
116252d39a21SChris Wilson 		error->ring[i].batchbuffer =
116352d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
116452d39a21SChris Wilson 
116552d39a21SChris Wilson 		error->ring[i].ringbuffer =
116652d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
116752d39a21SChris Wilson 
116852d39a21SChris Wilson 		count = 0;
116952d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
117052d39a21SChris Wilson 			count++;
117152d39a21SChris Wilson 
117252d39a21SChris Wilson 		error->ring[i].num_requests = count;
117352d39a21SChris Wilson 		error->ring[i].requests =
117452d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
117552d39a21SChris Wilson 				GFP_ATOMIC);
117652d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
117752d39a21SChris Wilson 			error->ring[i].num_requests = 0;
117852d39a21SChris Wilson 			continue;
117952d39a21SChris Wilson 		}
118052d39a21SChris Wilson 
118152d39a21SChris Wilson 		count = 0;
118252d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
118352d39a21SChris Wilson 			struct drm_i915_error_request *erq;
118452d39a21SChris Wilson 
118552d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
118652d39a21SChris Wilson 			erq->seqno = request->seqno;
118752d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1188ee4f42b1SChris Wilson 			erq->tail = request->tail;
118952d39a21SChris Wilson 		}
119052d39a21SChris Wilson 	}
119152d39a21SChris Wilson }
119252d39a21SChris Wilson 
11938a905236SJesse Barnes /**
11948a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
11958a905236SJesse Barnes  * @dev: drm device
11968a905236SJesse Barnes  *
11978a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
11988a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
11998a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
12008a905236SJesse Barnes  * to pick up.
12018a905236SJesse Barnes  */
120263eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
120363eeaf38SJesse Barnes {
120463eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
120505394f39SChris Wilson 	struct drm_i915_gem_object *obj;
120663eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
120763eeaf38SJesse Barnes 	unsigned long flags;
12089db4a9c7SJesse Barnes 	int i, pipe;
120963eeaf38SJesse Barnes 
121063eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12119df30794SChris Wilson 	error = dev_priv->first_error;
12129df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
12139df30794SChris Wilson 	if (error)
12149df30794SChris Wilson 		return;
121563eeaf38SJesse Barnes 
12169db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
121733f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
121863eeaf38SJesse Barnes 	if (!error) {
12199df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
12209df30794SChris Wilson 		return;
122163eeaf38SJesse Barnes 	}
122263eeaf38SJesse Barnes 
1223b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
1224b6f7833bSChris Wilson 		 dev->primary->index);
12252fa772f3SChris Wilson 
1226742cbee8SDaniel Vetter 	kref_init(&error->ref);
122763eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
122863eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1229b9a3906bSBen Widawsky 	error->ccid = I915_READ(CCID);
1230be998e2eSBen Widawsky 
1231be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1232be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1233be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1234be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1235be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1236be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1237be998e2eSBen Widawsky 	else
1238be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1239be998e2eSBen Widawsky 
12409db4a9c7SJesse Barnes 	for_each_pipe(pipe)
12419db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1242d27b1e0eSDaniel Vetter 
124333f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1244f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
124533f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
124633f3f518SDaniel Vetter 	}
1247add354ddSChris Wilson 
124871e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
124971e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
125071e172e8SBen Widawsky 
1251050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1252050ee91fSBen Widawsky 
1253748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
125452d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
12559df30794SChris Wilson 
1256c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
12579df30794SChris Wilson 	error->active_bo = NULL;
1258c724e8a9SChris Wilson 	error->pinned_bo = NULL;
12599df30794SChris Wilson 
1260bcfb2e28SChris Wilson 	i = 0;
1261bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1262bcfb2e28SChris Wilson 		i++;
1263bcfb2e28SChris Wilson 	error->active_bo_count = i;
12646c085a72SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
12651b50247aSChris Wilson 		if (obj->pin_count)
1266bcfb2e28SChris Wilson 			i++;
1267bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1268c724e8a9SChris Wilson 
12698e934dbfSChris Wilson 	error->active_bo = NULL;
12708e934dbfSChris Wilson 	error->pinned_bo = NULL;
1271bcfb2e28SChris Wilson 	if (i) {
1272bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
12739df30794SChris Wilson 					   GFP_ATOMIC);
1274c724e8a9SChris Wilson 		if (error->active_bo)
1275c724e8a9SChris Wilson 			error->pinned_bo =
1276c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
12779df30794SChris Wilson 	}
1278c724e8a9SChris Wilson 
1279c724e8a9SChris Wilson 	if (error->active_bo)
1280c724e8a9SChris Wilson 		error->active_bo_count =
12811b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1282c724e8a9SChris Wilson 					  error->active_bo_count,
1283c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1284c724e8a9SChris Wilson 
1285c724e8a9SChris Wilson 	if (error->pinned_bo)
1286c724e8a9SChris Wilson 		error->pinned_bo_count =
12871b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1288c724e8a9SChris Wilson 					  error->pinned_bo_count,
12896c085a72SChris Wilson 					  &dev_priv->mm.bound_list);
129063eeaf38SJesse Barnes 
12918a905236SJesse Barnes 	do_gettimeofday(&error->time);
12928a905236SJesse Barnes 
12936ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1294c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
12956ef3d427SChris Wilson 
12969df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12979df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
129863eeaf38SJesse Barnes 		dev_priv->first_error = error;
12999df30794SChris Wilson 		error = NULL;
13009df30794SChris Wilson 	}
130163eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
13029df30794SChris Wilson 
13039df30794SChris Wilson 	if (error)
1304742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
13059df30794SChris Wilson }
13069df30794SChris Wilson 
13079df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
13089df30794SChris Wilson {
13099df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
13109df30794SChris Wilson 	struct drm_i915_error_state *error;
13116dc0e816SBen Widawsky 	unsigned long flags;
13129df30794SChris Wilson 
13136dc0e816SBen Widawsky 	spin_lock_irqsave(&dev_priv->error_lock, flags);
13149df30794SChris Wilson 	error = dev_priv->first_error;
13159df30794SChris Wilson 	dev_priv->first_error = NULL;
13166dc0e816SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
13179df30794SChris Wilson 
13189df30794SChris Wilson 	if (error)
1319742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
132063eeaf38SJesse Barnes }
13213bd3c932SChris Wilson #else
13223bd3c932SChris Wilson #define i915_capture_error_state(x)
13233bd3c932SChris Wilson #endif
132463eeaf38SJesse Barnes 
132535aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1326c0e09200SDave Airlie {
13278a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1328bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
132963eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1330050ee91fSBen Widawsky 	int pipe, i;
133163eeaf38SJesse Barnes 
133235aed2e6SChris Wilson 	if (!eir)
133335aed2e6SChris Wilson 		return;
133463eeaf38SJesse Barnes 
1335a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
13368a905236SJesse Barnes 
1337bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1338bd9854f9SBen Widawsky 
13398a905236SJesse Barnes 	if (IS_G4X(dev)) {
13408a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
13418a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
13428a905236SJesse Barnes 
1343a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1344a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1345050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1346050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1347a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1348a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
13498a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
13503143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
13518a905236SJesse Barnes 		}
13528a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
13538a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1354a70491ccSJoe Perches 			pr_err("page table error\n");
1355a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
13568a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
13573143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
13588a905236SJesse Barnes 		}
13598a905236SJesse Barnes 	}
13608a905236SJesse Barnes 
1361a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
136263eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
136363eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1364a70491ccSJoe Perches 			pr_err("page table error\n");
1365a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
136663eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
13673143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
136863eeaf38SJesse Barnes 		}
13698a905236SJesse Barnes 	}
13708a905236SJesse Barnes 
137163eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1372a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
13739db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1374a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
13759db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
137663eeaf38SJesse Barnes 		/* pipestat has already been acked */
137763eeaf38SJesse Barnes 	}
137863eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1379a70491ccSJoe Perches 		pr_err("instruction error\n");
1380a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1381050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1382050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1383a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
138463eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
138563eeaf38SJesse Barnes 
1386a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1387a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1388a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
138963eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
13903143a2bfSChris Wilson 			POSTING_READ(IPEIR);
139163eeaf38SJesse Barnes 		} else {
139263eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
139363eeaf38SJesse Barnes 
1394a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1395a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1396a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1397a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
139863eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
13993143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
140063eeaf38SJesse Barnes 		}
140163eeaf38SJesse Barnes 	}
140263eeaf38SJesse Barnes 
140363eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
14043143a2bfSChris Wilson 	POSTING_READ(EIR);
140563eeaf38SJesse Barnes 	eir = I915_READ(EIR);
140663eeaf38SJesse Barnes 	if (eir) {
140763eeaf38SJesse Barnes 		/*
140863eeaf38SJesse Barnes 		 * some errors might have become stuck,
140963eeaf38SJesse Barnes 		 * mask them.
141063eeaf38SJesse Barnes 		 */
141163eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
141263eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
141363eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
141463eeaf38SJesse Barnes 	}
141535aed2e6SChris Wilson }
141635aed2e6SChris Wilson 
141735aed2e6SChris Wilson /**
141835aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
141935aed2e6SChris Wilson  * @dev: drm device
142035aed2e6SChris Wilson  *
142135aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
142235aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
142335aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
142435aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
142535aed2e6SChris Wilson  * of a ring dump etc.).
142635aed2e6SChris Wilson  */
1427527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
142835aed2e6SChris Wilson {
142935aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1430b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1431b4519513SChris Wilson 	int i;
143235aed2e6SChris Wilson 
143335aed2e6SChris Wilson 	i915_capture_error_state(dev);
143435aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
14358a905236SJesse Barnes 
1436ba1234d1SBen Gamari 	if (wedged) {
143730dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1438ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1439ba1234d1SBen Gamari 
144011ed50ecSBen Gamari 		/*
144111ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
144211ed50ecSBen Gamari 		 */
1443b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1444b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
144511ed50ecSBen Gamari 	}
144611ed50ecSBen Gamari 
14479c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
14488a905236SJesse Barnes }
14498a905236SJesse Barnes 
14504e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
14514e5359cdSSimon Farnsworth {
14524e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
14534e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
14544e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
145505394f39SChris Wilson 	struct drm_i915_gem_object *obj;
14564e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
14574e5359cdSSimon Farnsworth 	unsigned long flags;
14584e5359cdSSimon Farnsworth 	bool stall_detected;
14594e5359cdSSimon Farnsworth 
14604e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
14614e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
14624e5359cdSSimon Farnsworth 		return;
14634e5359cdSSimon Farnsworth 
14644e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
14654e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
14664e5359cdSSimon Farnsworth 
14674e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
14684e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
14694e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
14704e5359cdSSimon Farnsworth 		return;
14714e5359cdSSimon Farnsworth 	}
14724e5359cdSSimon Farnsworth 
14734e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
147405394f39SChris Wilson 	obj = work->pending_flip_obj;
1475a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
14769db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1477446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1478446f2545SArmin Reese 					obj->gtt_offset;
14794e5359cdSSimon Farnsworth 	} else {
14809db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
148105394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
148201f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
14834e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
14844e5359cdSSimon Farnsworth 	}
14854e5359cdSSimon Farnsworth 
14864e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
14874e5359cdSSimon Farnsworth 
14884e5359cdSSimon Farnsworth 	if (stall_detected) {
14894e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
14904e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
14914e5359cdSSimon Farnsworth 	}
14924e5359cdSSimon Farnsworth }
14934e5359cdSSimon Farnsworth 
149442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
149542f52ef8SKeith Packard  * we use as a pipe index
149642f52ef8SKeith Packard  */
1497f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
14980a3e67a4SJesse Barnes {
14990a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1500e9d21d7fSKeith Packard 	unsigned long irqflags;
150171e0ffa5SJesse Barnes 
15025eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
150371e0ffa5SJesse Barnes 		return -EINVAL;
15040a3e67a4SJesse Barnes 
15051ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1506f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
15077c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
15087c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
15090a3e67a4SJesse Barnes 	else
15107c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
15117c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
15128692d00eSChris Wilson 
15138692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
15148692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15156b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
15161ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15178692d00eSChris Wilson 
15180a3e67a4SJesse Barnes 	return 0;
15190a3e67a4SJesse Barnes }
15200a3e67a4SJesse Barnes 
1521f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1522f796cf8fSJesse Barnes {
1523f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1524f796cf8fSJesse Barnes 	unsigned long irqflags;
1525f796cf8fSJesse Barnes 
1526f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1527f796cf8fSJesse Barnes 		return -EINVAL;
1528f796cf8fSJesse Barnes 
1529f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1530f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1531f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1532f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1533f796cf8fSJesse Barnes 
1534f796cf8fSJesse Barnes 	return 0;
1535f796cf8fSJesse Barnes }
1536f796cf8fSJesse Barnes 
1537f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1538b1f14ad0SJesse Barnes {
1539b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1540b1f14ad0SJesse Barnes 	unsigned long irqflags;
1541b1f14ad0SJesse Barnes 
1542b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1543b1f14ad0SJesse Barnes 		return -EINVAL;
1544b1f14ad0SJesse Barnes 
1545b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1546b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
1547b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
1548b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1549b1f14ad0SJesse Barnes 
1550b1f14ad0SJesse Barnes 	return 0;
1551b1f14ad0SJesse Barnes }
1552b1f14ad0SJesse Barnes 
15537e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
15547e231dbeSJesse Barnes {
15557e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
15567e231dbeSJesse Barnes 	unsigned long irqflags;
155731acc7f5SJesse Barnes 	u32 imr;
15587e231dbeSJesse Barnes 
15597e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
15607e231dbeSJesse Barnes 		return -EINVAL;
15617e231dbeSJesse Barnes 
15627e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15637e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
156431acc7f5SJesse Barnes 	if (pipe == 0)
15657e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
156631acc7f5SJesse Barnes 	else
15677e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
15687e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
156931acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
157031acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
15717e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15727e231dbeSJesse Barnes 
15737e231dbeSJesse Barnes 	return 0;
15747e231dbeSJesse Barnes }
15757e231dbeSJesse Barnes 
157642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
157742f52ef8SKeith Packard  * we use as a pipe index
157842f52ef8SKeith Packard  */
1579f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
15800a3e67a4SJesse Barnes {
15810a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1582e9d21d7fSKeith Packard 	unsigned long irqflags;
15830a3e67a4SJesse Barnes 
15841ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15858692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15866b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
15878692d00eSChris Wilson 
15887c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
15897c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
15907c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
15911ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15920a3e67a4SJesse Barnes }
15930a3e67a4SJesse Barnes 
1594f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1595f796cf8fSJesse Barnes {
1596f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1597f796cf8fSJesse Barnes 	unsigned long irqflags;
1598f796cf8fSJesse Barnes 
1599f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1600f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1601f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1602f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1603f796cf8fSJesse Barnes }
1604f796cf8fSJesse Barnes 
1605f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1606b1f14ad0SJesse Barnes {
1607b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1608b1f14ad0SJesse Barnes 	unsigned long irqflags;
1609b1f14ad0SJesse Barnes 
1610b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1611b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
1612b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
1613b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1614b1f14ad0SJesse Barnes }
1615b1f14ad0SJesse Barnes 
16167e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
16177e231dbeSJesse Barnes {
16187e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16197e231dbeSJesse Barnes 	unsigned long irqflags;
162031acc7f5SJesse Barnes 	u32 imr;
16217e231dbeSJesse Barnes 
16227e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
162331acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
162431acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
16257e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
162631acc7f5SJesse Barnes 	if (pipe == 0)
16277e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
162831acc7f5SJesse Barnes 	else
16297e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
16307e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
16317e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
16327e231dbeSJesse Barnes }
16337e231dbeSJesse Barnes 
1634893eead0SChris Wilson static u32
1635893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1636852835f3SZou Nan hai {
1637893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1638893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1639893eead0SChris Wilson }
1640893eead0SChris Wilson 
1641893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1642893eead0SChris Wilson {
1643893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1644b2eadbc8SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring, false),
1645b2eadbc8SChris Wilson 			      ring_last_seqno(ring))) {
1646893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
16479574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
16489574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
16499574b3feSBen Widawsky 				  ring->name);
1650893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1651893eead0SChris Wilson 			*err = true;
1652893eead0SChris Wilson 		}
1653893eead0SChris Wilson 		return true;
1654893eead0SChris Wilson 	}
1655893eead0SChris Wilson 	return false;
1656f65d9421SBen Gamari }
1657f65d9421SBen Gamari 
16581ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
16591ec14ad3SChris Wilson {
16601ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
16611ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
16621ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
16631ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
16641ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
16651ec14ad3SChris Wilson 			  ring->name);
16661ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
16671ec14ad3SChris Wilson 		return true;
16681ec14ad3SChris Wilson 	}
16691ec14ad3SChris Wilson 	return false;
16701ec14ad3SChris Wilson }
16711ec14ad3SChris Wilson 
1672d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
1673d1e61e7fSChris Wilson {
1674d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1675d1e61e7fSChris Wilson 
1676d1e61e7fSChris Wilson 	if (dev_priv->hangcheck_count++ > 1) {
1677b4519513SChris Wilson 		bool hung = true;
1678b4519513SChris Wilson 
1679d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1680d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
1681d1e61e7fSChris Wilson 
1682d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
1683b4519513SChris Wilson 			struct intel_ring_buffer *ring;
1684b4519513SChris Wilson 			int i;
1685b4519513SChris Wilson 
1686d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
1687d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
1688d1e61e7fSChris Wilson 			 * and break the hang. This should work on
1689d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
1690d1e61e7fSChris Wilson 			 */
1691b4519513SChris Wilson 			for_each_ring(ring, dev_priv, i)
1692b4519513SChris Wilson 				hung &= !kick_ring(ring);
1693d1e61e7fSChris Wilson 		}
1694d1e61e7fSChris Wilson 
1695b4519513SChris Wilson 		return hung;
1696d1e61e7fSChris Wilson 	}
1697d1e61e7fSChris Wilson 
1698d1e61e7fSChris Wilson 	return false;
1699d1e61e7fSChris Wilson }
1700d1e61e7fSChris Wilson 
1701f65d9421SBen Gamari /**
1702f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1703f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1704f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1705f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1706f65d9421SBen Gamari  */
1707f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1708f65d9421SBen Gamari {
1709f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1710f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1711bd9854f9SBen Widawsky 	uint32_t acthd[I915_NUM_RINGS], instdone[I915_NUM_INSTDONE_REG];
1712b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1713b4519513SChris Wilson 	bool err = false, idle;
1714b4519513SChris Wilson 	int i;
1715893eead0SChris Wilson 
17163e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
17173e0dc6b0SBen Widawsky 		return;
17183e0dc6b0SBen Widawsky 
1719b4519513SChris Wilson 	memset(acthd, 0, sizeof(acthd));
1720b4519513SChris Wilson 	idle = true;
1721b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
1722b4519513SChris Wilson 	    idle &= i915_hangcheck_ring_idle(ring, &err);
1723b4519513SChris Wilson 	    acthd[i] = intel_ring_get_active_head(ring);
1724b4519513SChris Wilson 	}
1725b4519513SChris Wilson 
1726893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
1727b4519513SChris Wilson 	if (idle) {
1728d1e61e7fSChris Wilson 		if (err) {
1729d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
1730d1e61e7fSChris Wilson 				return;
1731d1e61e7fSChris Wilson 
1732893eead0SChris Wilson 			goto repeat;
1733d1e61e7fSChris Wilson 		}
1734d1e61e7fSChris Wilson 
1735d1e61e7fSChris Wilson 		dev_priv->hangcheck_count = 0;
1736893eead0SChris Wilson 		return;
1737893eead0SChris Wilson 	}
1738f65d9421SBen Gamari 
1739bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1740b4519513SChris Wilson 	if (memcmp(dev_priv->last_acthd, acthd, sizeof(acthd)) == 0 &&
1741050ee91fSBen Widawsky 	    memcmp(dev_priv->prev_instdone, instdone, sizeof(instdone)) == 0) {
1742d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
1743f65d9421SBen Gamari 			return;
1744cbb465e7SChris Wilson 	} else {
1745cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1746cbb465e7SChris Wilson 
1747b4519513SChris Wilson 		memcpy(dev_priv->last_acthd, acthd, sizeof(acthd));
1748050ee91fSBen Widawsky 		memcpy(dev_priv->prev_instdone, instdone, sizeof(instdone));
1749cbb465e7SChris Wilson 	}
1750f65d9421SBen Gamari 
1751893eead0SChris Wilson repeat:
1752f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1753b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1754cecc21feSChris Wilson 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
1755f65d9421SBen Gamari }
1756f65d9421SBen Gamari 
1757c0e09200SDave Airlie /* drm_dma.h hooks
1758c0e09200SDave Airlie */
1759f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
1760036a4a7dSZhenyu Wang {
1761036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1762036a4a7dSZhenyu Wang 
17634697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17644697995bSJesse Barnes 
1765036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1766bdfcdb63SDaniel Vetter 
1767036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1768036a4a7dSZhenyu Wang 
1769036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1770036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
17713143a2bfSChris Wilson 	POSTING_READ(DEIER);
1772036a4a7dSZhenyu Wang 
1773036a4a7dSZhenyu Wang 	/* and GT */
1774036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1775036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
17763143a2bfSChris Wilson 	POSTING_READ(GTIER);
1777c650156aSZhenyu Wang 
1778c650156aSZhenyu Wang 	/* south display irq */
1779c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1780c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
17813143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1782036a4a7dSZhenyu Wang }
1783036a4a7dSZhenyu Wang 
17847e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
17857e231dbeSJesse Barnes {
17867e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17877e231dbeSJesse Barnes 	int pipe;
17887e231dbeSJesse Barnes 
17897e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17907e231dbeSJesse Barnes 
17917e231dbeSJesse Barnes 	/* VLV magic */
17927e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
17937e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
17947e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
17957e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
17967e231dbeSJesse Barnes 
17977e231dbeSJesse Barnes 	/* and GT */
17987e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
17997e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
18007e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
18017e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
18027e231dbeSJesse Barnes 	POSTING_READ(GTIER);
18037e231dbeSJesse Barnes 
18047e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
18057e231dbeSJesse Barnes 
18067e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
18077e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
18087e231dbeSJesse Barnes 	for_each_pipe(pipe)
18097e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
18107e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
18117e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
18127e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
18137e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
18147e231dbeSJesse Barnes }
18157e231dbeSJesse Barnes 
18167fe0b973SKeith Packard /*
18177fe0b973SKeith Packard  * Enable digital hotplug on the PCH, and configure the DP short pulse
18187fe0b973SKeith Packard  * duration to 2ms (which is the minimum in the Display Port spec)
18197fe0b973SKeith Packard  *
18207fe0b973SKeith Packard  * This register is the same on all known PCH chips.
18217fe0b973SKeith Packard  */
18227fe0b973SKeith Packard 
18237fe0b973SKeith Packard static void ironlake_enable_pch_hotplug(struct drm_device *dev)
18247fe0b973SKeith Packard {
18257fe0b973SKeith Packard 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18267fe0b973SKeith Packard 	u32	hotplug;
18277fe0b973SKeith Packard 
18287fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
18297fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
18307fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
18317fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
18327fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
18337fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
18347fe0b973SKeith Packard }
18357fe0b973SKeith Packard 
1836f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
1837036a4a7dSZhenyu Wang {
1838036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1839036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1840013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1841013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
18421ec14ad3SChris Wilson 	u32 render_irqs;
18432d7b8366SYuanhan Liu 	u32 hotplug_mask;
1844036a4a7dSZhenyu Wang 
18451ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1846036a4a7dSZhenyu Wang 
1847036a4a7dSZhenyu Wang 	/* should always can generate irq */
1848036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
18491ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
18501ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
18513143a2bfSChris Wilson 	POSTING_READ(DEIER);
1852036a4a7dSZhenyu Wang 
18531ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1854036a4a7dSZhenyu Wang 
1855036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
18561ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1857881f47b6SXiang, Haihao 
18581ec14ad3SChris Wilson 	if (IS_GEN6(dev))
18591ec14ad3SChris Wilson 		render_irqs =
18601ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
1861e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
1862e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
18631ec14ad3SChris Wilson 	else
18641ec14ad3SChris Wilson 		render_irqs =
186588f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1866c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
18671ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
18681ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
18693143a2bfSChris Wilson 	POSTING_READ(GTIER);
1870036a4a7dSZhenyu Wang 
18712d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
18729035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
18739035a97aSChris Wilson 				SDE_PORTB_HOTPLUG_CPT |
18749035a97aSChris Wilson 				SDE_PORTC_HOTPLUG_CPT |
18759035a97aSChris Wilson 				SDE_PORTD_HOTPLUG_CPT);
18762d7b8366SYuanhan Liu 	} else {
18779035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG |
18789035a97aSChris Wilson 				SDE_PORTB_HOTPLUG |
18799035a97aSChris Wilson 				SDE_PORTC_HOTPLUG |
18809035a97aSChris Wilson 				SDE_PORTD_HOTPLUG |
18819035a97aSChris Wilson 				SDE_AUX_MASK);
18822d7b8366SYuanhan Liu 	}
18832d7b8366SYuanhan Liu 
18841ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1885c650156aSZhenyu Wang 
1886c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
18871ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
18881ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
18893143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1890c650156aSZhenyu Wang 
18917fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
18927fe0b973SKeith Packard 
1893f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1894f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1895f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1896f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1897f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1898f97108d1SJesse Barnes 	}
1899f97108d1SJesse Barnes 
1900036a4a7dSZhenyu Wang 	return 0;
1901036a4a7dSZhenyu Wang }
1902036a4a7dSZhenyu Wang 
1903f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
1904b1f14ad0SJesse Barnes {
1905b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1906b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
1907b615b57aSChris Wilson 	u32 display_mask =
1908b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
1909b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
1910b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
1911b615b57aSChris Wilson 		DE_PLANEA_FLIP_DONE_IVB;
1912b1f14ad0SJesse Barnes 	u32 render_irqs;
1913b1f14ad0SJesse Barnes 	u32 hotplug_mask;
1914b1f14ad0SJesse Barnes 
1915b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
1916b1f14ad0SJesse Barnes 
1917b1f14ad0SJesse Barnes 	/* should always can generate irq */
1918b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1919b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
1920b615b57aSChris Wilson 	I915_WRITE(DEIER,
1921b615b57aSChris Wilson 		   display_mask |
1922b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
1923b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
1924b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
1925b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1926b1f14ad0SJesse Barnes 
192715b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
1928b1f14ad0SJesse Barnes 
1929b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1930b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1931b1f14ad0SJesse Barnes 
1932e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
193315b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
1934b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
1935b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
1936b1f14ad0SJesse Barnes 
1937b1f14ad0SJesse Barnes 	hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
1938b1f14ad0SJesse Barnes 			SDE_PORTB_HOTPLUG_CPT |
1939b1f14ad0SJesse Barnes 			SDE_PORTC_HOTPLUG_CPT |
1940b1f14ad0SJesse Barnes 			SDE_PORTD_HOTPLUG_CPT);
1941b1f14ad0SJesse Barnes 	dev_priv->pch_irq_mask = ~hotplug_mask;
1942b1f14ad0SJesse Barnes 
1943b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1944b1f14ad0SJesse Barnes 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1945b1f14ad0SJesse Barnes 	I915_WRITE(SDEIER, hotplug_mask);
1946b1f14ad0SJesse Barnes 	POSTING_READ(SDEIER);
1947b1f14ad0SJesse Barnes 
19487fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
19497fe0b973SKeith Packard 
1950b1f14ad0SJesse Barnes 	return 0;
1951b1f14ad0SJesse Barnes }
1952b1f14ad0SJesse Barnes 
19537e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
19547e231dbeSJesse Barnes {
19557e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19567e231dbeSJesse Barnes 	u32 enable_mask;
19577e231dbeSJesse Barnes 	u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
195831acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
19593bcedbe5SJesse Barnes 	u32 render_irqs;
19607e231dbeSJesse Barnes 	u16 msid;
19617e231dbeSJesse Barnes 
19627e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
196331acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
196431acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
196531acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
19667e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
19677e231dbeSJesse Barnes 
196831acc7f5SJesse Barnes 	/*
196931acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
197031acc7f5SJesse Barnes 	 * toggle them based on usage.
197131acc7f5SJesse Barnes 	 */
197231acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
197331acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
197431acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
19757e231dbeSJesse Barnes 
19767e231dbeSJesse Barnes 	dev_priv->pipestat[0] = 0;
19777e231dbeSJesse Barnes 	dev_priv->pipestat[1] = 0;
19787e231dbeSJesse Barnes 
19797e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
19807e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
19817e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
19827e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
19837e231dbeSJesse Barnes 	msid |= (1<<14);
19847e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
19857e231dbeSJesse Barnes 
19867e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
19877e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
19887e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19897e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
19907e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
19917e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
19927e231dbeSJesse Barnes 
199331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
199431acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
199531acc7f5SJesse Barnes 
19967e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19977e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19987e231dbeSJesse Barnes 
199931acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
200031acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
20013bcedbe5SJesse Barnes 
20023bcedbe5SJesse Barnes 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
20033bcedbe5SJesse Barnes 		GEN6_BLITTER_USER_INTERRUPT;
20043bcedbe5SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
20057e231dbeSJesse Barnes 	POSTING_READ(GTIER);
20067e231dbeSJesse Barnes 
20077e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
20087e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
20097e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
20107e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
20117e231dbeSJesse Barnes #endif
20127e231dbeSJesse Barnes 
20137e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
20147e231dbeSJesse Barnes 	/* Note HDMI and DP share bits */
20157e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
20167e231dbeSJesse Barnes 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
20177e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
20187e231dbeSJesse Barnes 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
20197e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
20207e231dbeSJesse Barnes 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
2021ae33cdcfSVijay Purushothaman 	if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
20227e231dbeSJesse Barnes 		hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2023ae33cdcfSVijay Purushothaman 	if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
20247e231dbeSJesse Barnes 		hotplug_en |= SDVOB_HOTPLUG_INT_EN;
20257e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
20267e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_INT_EN;
20277e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
20287e231dbeSJesse Barnes 	}
20297e231dbeSJesse Barnes 
20307e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
20317e231dbeSJesse Barnes 
20327e231dbeSJesse Barnes 	return 0;
20337e231dbeSJesse Barnes }
20347e231dbeSJesse Barnes 
20357e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
20367e231dbeSJesse Barnes {
20377e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20387e231dbeSJesse Barnes 	int pipe;
20397e231dbeSJesse Barnes 
20407e231dbeSJesse Barnes 	if (!dev_priv)
20417e231dbeSJesse Barnes 		return;
20427e231dbeSJesse Barnes 
20437e231dbeSJesse Barnes 	for_each_pipe(pipe)
20447e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
20457e231dbeSJesse Barnes 
20467e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
20477e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
20487e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
20497e231dbeSJesse Barnes 	for_each_pipe(pipe)
20507e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
20517e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20527e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
20537e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
20547e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
20557e231dbeSJesse Barnes }
20567e231dbeSJesse Barnes 
2057f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2058036a4a7dSZhenyu Wang {
2059036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20604697995bSJesse Barnes 
20614697995bSJesse Barnes 	if (!dev_priv)
20624697995bSJesse Barnes 		return;
20634697995bSJesse Barnes 
2064036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2065036a4a7dSZhenyu Wang 
2066036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2067036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2068036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2069036a4a7dSZhenyu Wang 
2070036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2071036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2072036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2073192aac1fSKeith Packard 
2074192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2075192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2076192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2077036a4a7dSZhenyu Wang }
2078036a4a7dSZhenyu Wang 
2079c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2080c2798b19SChris Wilson {
2081c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2082c2798b19SChris Wilson 	int pipe;
2083c2798b19SChris Wilson 
2084c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2085c2798b19SChris Wilson 
2086c2798b19SChris Wilson 	for_each_pipe(pipe)
2087c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2088c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2089c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2090c2798b19SChris Wilson 	POSTING_READ16(IER);
2091c2798b19SChris Wilson }
2092c2798b19SChris Wilson 
2093c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2094c2798b19SChris Wilson {
2095c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2096c2798b19SChris Wilson 
2097c2798b19SChris Wilson 	dev_priv->pipestat[0] = 0;
2098c2798b19SChris Wilson 	dev_priv->pipestat[1] = 0;
2099c2798b19SChris Wilson 
2100c2798b19SChris Wilson 	I915_WRITE16(EMR,
2101c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2102c2798b19SChris Wilson 
2103c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2104c2798b19SChris Wilson 	dev_priv->irq_mask =
2105c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2106c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2107c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2108c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2109c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2110c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2111c2798b19SChris Wilson 
2112c2798b19SChris Wilson 	I915_WRITE16(IER,
2113c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2114c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2115c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2116c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2117c2798b19SChris Wilson 	POSTING_READ16(IER);
2118c2798b19SChris Wilson 
2119c2798b19SChris Wilson 	return 0;
2120c2798b19SChris Wilson }
2121c2798b19SChris Wilson 
2122ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2123c2798b19SChris Wilson {
2124c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2125c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2126c2798b19SChris Wilson 	u16 iir, new_iir;
2127c2798b19SChris Wilson 	u32 pipe_stats[2];
2128c2798b19SChris Wilson 	unsigned long irqflags;
2129c2798b19SChris Wilson 	int irq_received;
2130c2798b19SChris Wilson 	int pipe;
2131c2798b19SChris Wilson 	u16 flip_mask =
2132c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2133c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2134c2798b19SChris Wilson 
2135c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2136c2798b19SChris Wilson 
2137c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2138c2798b19SChris Wilson 	if (iir == 0)
2139c2798b19SChris Wilson 		return IRQ_NONE;
2140c2798b19SChris Wilson 
2141c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2142c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2143c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2144c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2145c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2146c2798b19SChris Wilson 		 */
2147c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2148c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2149c2798b19SChris Wilson 			i915_handle_error(dev, false);
2150c2798b19SChris Wilson 
2151c2798b19SChris Wilson 		for_each_pipe(pipe) {
2152c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2153c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2154c2798b19SChris Wilson 
2155c2798b19SChris Wilson 			/*
2156c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2157c2798b19SChris Wilson 			 */
2158c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2159c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2160c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2161c2798b19SChris Wilson 							 pipe_name(pipe));
2162c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2163c2798b19SChris Wilson 				irq_received = 1;
2164c2798b19SChris Wilson 			}
2165c2798b19SChris Wilson 		}
2166c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2167c2798b19SChris Wilson 
2168c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2169c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2170c2798b19SChris Wilson 
2171d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2172c2798b19SChris Wilson 
2173c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2174c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2175c2798b19SChris Wilson 
2176c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
2177c2798b19SChris Wilson 		    drm_handle_vblank(dev, 0)) {
2178c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
2179c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 0);
2180c2798b19SChris Wilson 				intel_finish_page_flip(dev, 0);
2181c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT;
2182c2798b19SChris Wilson 			}
2183c2798b19SChris Wilson 		}
2184c2798b19SChris Wilson 
2185c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
2186c2798b19SChris Wilson 		    drm_handle_vblank(dev, 1)) {
2187c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
2188c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 1);
2189c2798b19SChris Wilson 				intel_finish_page_flip(dev, 1);
2190c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2191c2798b19SChris Wilson 			}
2192c2798b19SChris Wilson 		}
2193c2798b19SChris Wilson 
2194c2798b19SChris Wilson 		iir = new_iir;
2195c2798b19SChris Wilson 	}
2196c2798b19SChris Wilson 
2197c2798b19SChris Wilson 	return IRQ_HANDLED;
2198c2798b19SChris Wilson }
2199c2798b19SChris Wilson 
2200c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2201c2798b19SChris Wilson {
2202c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2203c2798b19SChris Wilson 	int pipe;
2204c2798b19SChris Wilson 
2205c2798b19SChris Wilson 	for_each_pipe(pipe) {
2206c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2207c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2208c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2209c2798b19SChris Wilson 	}
2210c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2211c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2212c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2213c2798b19SChris Wilson }
2214c2798b19SChris Wilson 
2215a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2216a266c7d5SChris Wilson {
2217a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2218a266c7d5SChris Wilson 	int pipe;
2219a266c7d5SChris Wilson 
2220a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2221a266c7d5SChris Wilson 
2222a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2223a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2224a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2225a266c7d5SChris Wilson 	}
2226a266c7d5SChris Wilson 
222700d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2228a266c7d5SChris Wilson 	for_each_pipe(pipe)
2229a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2230a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2231a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2232a266c7d5SChris Wilson 	POSTING_READ(IER);
2233a266c7d5SChris Wilson }
2234a266c7d5SChris Wilson 
2235a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2236a266c7d5SChris Wilson {
2237a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
223838bde180SChris Wilson 	u32 enable_mask;
2239a266c7d5SChris Wilson 
2240a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2241a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2242a266c7d5SChris Wilson 
224338bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
224438bde180SChris Wilson 
224538bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
224638bde180SChris Wilson 	dev_priv->irq_mask =
224738bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
224838bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
224938bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
225038bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
225138bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
225238bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
225338bde180SChris Wilson 
225438bde180SChris Wilson 	enable_mask =
225538bde180SChris Wilson 		I915_ASLE_INTERRUPT |
225638bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
225738bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
225838bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
225938bde180SChris Wilson 		I915_USER_INTERRUPT;
226038bde180SChris Wilson 
2261a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2262a266c7d5SChris Wilson 		/* Enable in IER... */
2263a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2264a266c7d5SChris Wilson 		/* and unmask in IMR */
2265a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2266a266c7d5SChris Wilson 	}
2267a266c7d5SChris Wilson 
2268a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2269a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2270a266c7d5SChris Wilson 	POSTING_READ(IER);
2271a266c7d5SChris Wilson 
2272a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2273a266c7d5SChris Wilson 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2274a266c7d5SChris Wilson 
2275a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2276a266c7d5SChris Wilson 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2277a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2278a266c7d5SChris Wilson 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2279a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2280a266c7d5SChris Wilson 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
2281084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
2282a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2283084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
2284a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2285a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2286a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_INT_EN;
2287a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2288a266c7d5SChris Wilson 		}
2289a266c7d5SChris Wilson 
2290a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2291a266c7d5SChris Wilson 
2292a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2293a266c7d5SChris Wilson 	}
2294a266c7d5SChris Wilson 
2295a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2296a266c7d5SChris Wilson 
2297a266c7d5SChris Wilson 	return 0;
2298a266c7d5SChris Wilson }
2299a266c7d5SChris Wilson 
2300ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2301a266c7d5SChris Wilson {
2302a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2303a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23048291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2305a266c7d5SChris Wilson 	unsigned long irqflags;
230638bde180SChris Wilson 	u32 flip_mask =
230738bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
230838bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
230938bde180SChris Wilson 	u32 flip[2] = {
231038bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT,
231138bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT
231238bde180SChris Wilson 	};
231338bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2314a266c7d5SChris Wilson 
2315a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2316a266c7d5SChris Wilson 
2317a266c7d5SChris Wilson 	iir = I915_READ(IIR);
231838bde180SChris Wilson 	do {
231938bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
23208291ee90SChris Wilson 		bool blc_event = false;
2321a266c7d5SChris Wilson 
2322a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2323a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2324a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2325a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2326a266c7d5SChris Wilson 		 */
2327a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2328a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2329a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2330a266c7d5SChris Wilson 
2331a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2332a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2333a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2334a266c7d5SChris Wilson 
233538bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2336a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2337a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2338a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2339a266c7d5SChris Wilson 							 pipe_name(pipe));
2340a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
234138bde180SChris Wilson 				irq_received = true;
2342a266c7d5SChris Wilson 			}
2343a266c7d5SChris Wilson 		}
2344a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2345a266c7d5SChris Wilson 
2346a266c7d5SChris Wilson 		if (!irq_received)
2347a266c7d5SChris Wilson 			break;
2348a266c7d5SChris Wilson 
2349a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2350a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2351a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2352a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2353a266c7d5SChris Wilson 
2354a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2355a266c7d5SChris Wilson 				  hotplug_status);
2356a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2357a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2358a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2359a266c7d5SChris Wilson 
2360a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
236138bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2362a266c7d5SChris Wilson 		}
2363a266c7d5SChris Wilson 
236438bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2365a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2366a266c7d5SChris Wilson 
2367a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2368a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2369a266c7d5SChris Wilson 
2370a266c7d5SChris Wilson 		for_each_pipe(pipe) {
237138bde180SChris Wilson 			int plane = pipe;
237238bde180SChris Wilson 			if (IS_MOBILE(dev))
237338bde180SChris Wilson 				plane = !plane;
23748291ee90SChris Wilson 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
2375a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
237638bde180SChris Wilson 				if (iir & flip[plane]) {
237738bde180SChris Wilson 					intel_prepare_page_flip(dev, plane);
2378a266c7d5SChris Wilson 					intel_finish_page_flip(dev, pipe);
237938bde180SChris Wilson 					flip_mask &= ~flip[plane];
238038bde180SChris Wilson 				}
2381a266c7d5SChris Wilson 			}
2382a266c7d5SChris Wilson 
2383a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2384a266c7d5SChris Wilson 				blc_event = true;
2385a266c7d5SChris Wilson 		}
2386a266c7d5SChris Wilson 
2387a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2388a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2389a266c7d5SChris Wilson 
2390a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2391a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2392a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2393a266c7d5SChris Wilson 		 * we would never get another interrupt.
2394a266c7d5SChris Wilson 		 *
2395a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2396a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2397a266c7d5SChris Wilson 		 * another one.
2398a266c7d5SChris Wilson 		 *
2399a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2400a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2401a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2402a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2403a266c7d5SChris Wilson 		 * stray interrupts.
2404a266c7d5SChris Wilson 		 */
240538bde180SChris Wilson 		ret = IRQ_HANDLED;
2406a266c7d5SChris Wilson 		iir = new_iir;
240738bde180SChris Wilson 	} while (iir & ~flip_mask);
2408a266c7d5SChris Wilson 
2409d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
24108291ee90SChris Wilson 
2411a266c7d5SChris Wilson 	return ret;
2412a266c7d5SChris Wilson }
2413a266c7d5SChris Wilson 
2414a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2415a266c7d5SChris Wilson {
2416a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2417a266c7d5SChris Wilson 	int pipe;
2418a266c7d5SChris Wilson 
2419a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2420a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2421a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2422a266c7d5SChris Wilson 	}
2423a266c7d5SChris Wilson 
242400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
242555b39755SChris Wilson 	for_each_pipe(pipe) {
242655b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2427a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
242855b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
242955b39755SChris Wilson 	}
2430a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2431a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2432a266c7d5SChris Wilson 
2433a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2434a266c7d5SChris Wilson }
2435a266c7d5SChris Wilson 
2436a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2437a266c7d5SChris Wilson {
2438a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2439a266c7d5SChris Wilson 	int pipe;
2440a266c7d5SChris Wilson 
2441a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2442a266c7d5SChris Wilson 
2443a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2444a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2445a266c7d5SChris Wilson 
2446a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2447a266c7d5SChris Wilson 	for_each_pipe(pipe)
2448a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2449a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2450a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2451a266c7d5SChris Wilson 	POSTING_READ(IER);
2452a266c7d5SChris Wilson }
2453a266c7d5SChris Wilson 
2454a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2455a266c7d5SChris Wilson {
2456a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2457adca4730SChris Wilson 	u32 hotplug_en;
2458bbba0a97SChris Wilson 	u32 enable_mask;
2459a266c7d5SChris Wilson 	u32 error_mask;
2460a266c7d5SChris Wilson 
2461a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2462bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2463adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2464bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2465bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2466bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2467bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2468bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2469bbba0a97SChris Wilson 
2470bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
2471bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2472bbba0a97SChris Wilson 
2473bbba0a97SChris Wilson 	if (IS_G4X(dev))
2474bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2475a266c7d5SChris Wilson 
2476a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2477a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2478a266c7d5SChris Wilson 
2479a266c7d5SChris Wilson 	/*
2480a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2481a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2482a266c7d5SChris Wilson 	 */
2483a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2484a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2485a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2486a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2487a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2488a266c7d5SChris Wilson 	} else {
2489a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2490a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2491a266c7d5SChris Wilson 	}
2492a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2493a266c7d5SChris Wilson 
2494a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2495a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2496a266c7d5SChris Wilson 	POSTING_READ(IER);
2497a266c7d5SChris Wilson 
2498adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
2499adca4730SChris Wilson 	hotplug_en = 0;
2500a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2501a266c7d5SChris Wilson 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2502a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2503a266c7d5SChris Wilson 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2504a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2505a266c7d5SChris Wilson 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
2506084b612eSChris Wilson 	if (IS_G4X(dev)) {
2507084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_G4X)
2508a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2509084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_G4X)
2510a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2511084b612eSChris Wilson 	} else {
2512084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I965)
2513084b612eSChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2514084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I965)
2515084b612eSChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2516084b612eSChris Wilson 	}
2517a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2518a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_INT_EN;
2519a266c7d5SChris Wilson 
2520a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2521a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2522a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2523a266c7d5SChris Wilson 		   */
2524a266c7d5SChris Wilson 		if (IS_G4X(dev))
2525a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2526a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2527a266c7d5SChris Wilson 	}
2528a266c7d5SChris Wilson 
2529a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
2530a266c7d5SChris Wilson 
2531a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2532a266c7d5SChris Wilson 
2533a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2534a266c7d5SChris Wilson 
2535a266c7d5SChris Wilson 	return 0;
2536a266c7d5SChris Wilson }
2537a266c7d5SChris Wilson 
2538ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
2539a266c7d5SChris Wilson {
2540a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2541a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2542a266c7d5SChris Wilson 	u32 iir, new_iir;
2543a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2544a266c7d5SChris Wilson 	unsigned long irqflags;
2545a266c7d5SChris Wilson 	int irq_received;
2546a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
2547a266c7d5SChris Wilson 
2548a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2549a266c7d5SChris Wilson 
2550a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2551a266c7d5SChris Wilson 
2552a266c7d5SChris Wilson 	for (;;) {
25532c8ba29fSChris Wilson 		bool blc_event = false;
25542c8ba29fSChris Wilson 
2555a266c7d5SChris Wilson 		irq_received = iir != 0;
2556a266c7d5SChris Wilson 
2557a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2558a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2559a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2560a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2561a266c7d5SChris Wilson 		 */
2562a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2563a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2564a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2565a266c7d5SChris Wilson 
2566a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2567a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2568a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2569a266c7d5SChris Wilson 
2570a266c7d5SChris Wilson 			/*
2571a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2572a266c7d5SChris Wilson 			 */
2573a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2574a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2575a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2576a266c7d5SChris Wilson 							 pipe_name(pipe));
2577a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2578a266c7d5SChris Wilson 				irq_received = 1;
2579a266c7d5SChris Wilson 			}
2580a266c7d5SChris Wilson 		}
2581a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2582a266c7d5SChris Wilson 
2583a266c7d5SChris Wilson 		if (!irq_received)
2584a266c7d5SChris Wilson 			break;
2585a266c7d5SChris Wilson 
2586a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2587a266c7d5SChris Wilson 
2588a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2589adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2590a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2591a266c7d5SChris Wilson 
2592a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2593a266c7d5SChris Wilson 				  hotplug_status);
2594a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2595a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2596a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2597a266c7d5SChris Wilson 
2598a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2599a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2600a266c7d5SChris Wilson 		}
2601a266c7d5SChris Wilson 
2602a266c7d5SChris Wilson 		I915_WRITE(IIR, iir);
2603a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2604a266c7d5SChris Wilson 
2605a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2606a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2607a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2608a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2609a266c7d5SChris Wilson 
26104f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
2611a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 0);
2612a266c7d5SChris Wilson 
26134f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
2614a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 1);
2615a266c7d5SChris Wilson 
2616a266c7d5SChris Wilson 		for_each_pipe(pipe) {
26172c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
2618a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
2619a266c7d5SChris Wilson 				i915_pageflip_stall_check(dev, pipe);
2620a266c7d5SChris Wilson 				intel_finish_page_flip(dev, pipe);
2621a266c7d5SChris Wilson 			}
2622a266c7d5SChris Wilson 
2623a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2624a266c7d5SChris Wilson 				blc_event = true;
2625a266c7d5SChris Wilson 		}
2626a266c7d5SChris Wilson 
2627a266c7d5SChris Wilson 
2628a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2629a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2630a266c7d5SChris Wilson 
2631a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2632a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2633a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2634a266c7d5SChris Wilson 		 * we would never get another interrupt.
2635a266c7d5SChris Wilson 		 *
2636a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2637a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2638a266c7d5SChris Wilson 		 * another one.
2639a266c7d5SChris Wilson 		 *
2640a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2641a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2642a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2643a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2644a266c7d5SChris Wilson 		 * stray interrupts.
2645a266c7d5SChris Wilson 		 */
2646a266c7d5SChris Wilson 		iir = new_iir;
2647a266c7d5SChris Wilson 	}
2648a266c7d5SChris Wilson 
2649d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
26502c8ba29fSChris Wilson 
2651a266c7d5SChris Wilson 	return ret;
2652a266c7d5SChris Wilson }
2653a266c7d5SChris Wilson 
2654a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
2655a266c7d5SChris Wilson {
2656a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2657a266c7d5SChris Wilson 	int pipe;
2658a266c7d5SChris Wilson 
2659a266c7d5SChris Wilson 	if (!dev_priv)
2660a266c7d5SChris Wilson 		return;
2661a266c7d5SChris Wilson 
2662a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2663a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2664a266c7d5SChris Wilson 
2665a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
2666a266c7d5SChris Wilson 	for_each_pipe(pipe)
2667a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2668a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2669a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2670a266c7d5SChris Wilson 
2671a266c7d5SChris Wilson 	for_each_pipe(pipe)
2672a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
2673a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2674a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2675a266c7d5SChris Wilson }
2676a266c7d5SChris Wilson 
2677f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2678f71d4af4SJesse Barnes {
26798b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
26808b2e326dSChris Wilson 
26818b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
26828b2e326dSChris Wilson 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
2683c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
2684a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
26858b2e326dSChris Wilson 
2686f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2687f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
26887d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
2689f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2690f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2691f71d4af4SJesse Barnes 	}
2692f71d4af4SJesse Barnes 
2693c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2694f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2695c3613de9SKeith Packard 	else
2696c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2697f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2698f71d4af4SJesse Barnes 
26997e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
27007e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
27017e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
27027e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
27037e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
27047e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
27057e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
27067e231dbeSJesse Barnes 	} else if (IS_IVYBRIDGE(dev)) {
2707f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2708f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2709f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2710f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2711f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2712f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2713f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
27147d4e146fSEugeni Dodonov 	} else if (IS_HASWELL(dev)) {
27157d4e146fSEugeni Dodonov 		/* Share interrupts handling with IVB */
27167d4e146fSEugeni Dodonov 		dev->driver->irq_handler = ivybridge_irq_handler;
27177d4e146fSEugeni Dodonov 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
27187d4e146fSEugeni Dodonov 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
27197d4e146fSEugeni Dodonov 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
27207d4e146fSEugeni Dodonov 		dev->driver->enable_vblank = ivybridge_enable_vblank;
27217d4e146fSEugeni Dodonov 		dev->driver->disable_vblank = ivybridge_disable_vblank;
2722f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2723f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2724f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2725f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2726f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2727f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2728f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
2729f71d4af4SJesse Barnes 	} else {
2730c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
2731c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
2732c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
2733c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
2734c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
2735a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
2736a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
2737a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
2738a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
2739a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
2740c2798b19SChris Wilson 		} else {
2741a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
2742a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
2743a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
2744a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
2745c2798b19SChris Wilson 		}
2746f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
2747f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
2748f71d4af4SJesse Barnes 	}
2749f71d4af4SJesse Barnes }
2750