xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision acd15b6cc20f85bcef9e08b6ed4f142c34791c32)
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 
3814fc688ceSJesse 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 
3964fc688ceSJesse 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;
761*acd15b6cSDaniel Vetter 	u32 de_iir, gt_iir, de_ier, 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);
7723b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
773036a4a7dSZhenyu Wang 
774*acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
775c7c85101SZou Nan hai 		goto done;
776036a4a7dSZhenyu Wang 
777036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
778036a4a7dSZhenyu Wang 
779e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
780e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
781e7b4c6b1SDaniel Vetter 	else
782e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
783036a4a7dSZhenyu Wang 
78401c66889SZhao Yakui 	if (de_iir & DE_GSE)
7853b617967SChris Wilson 		intel_opregion_gse_intr(dev);
78601c66889SZhao Yakui 
78774d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
78874d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
78974d44445SDaniel Vetter 
79074d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
79174d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
79274d44445SDaniel Vetter 
793f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
794013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
7952bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
796013d5aa2SJesse Barnes 	}
797013d5aa2SJesse Barnes 
798f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
799f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
8002bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
801013d5aa2SJesse Barnes 	}
802c062df61SLi Peng 
803c650156aSZhenyu Wang 	/* check event from PCH */
804776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
805*acd15b6cSDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
806*acd15b6cSDaniel Vetter 
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);
811*acd15b6cSDaniel Vetter 
812*acd15b6cSDaniel Vetter 		/* should clear PCH hotplug event before clear CPU irq */
813*acd15b6cSDaniel Vetter 		I915_WRITE(SDEIIR, pch_iir);
814776ad806SJesse Barnes 	}
815c650156aSZhenyu Wang 
81673edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
81773edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
818f97108d1SJesse Barnes 
819fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
820fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
8213b8d8d91SJesse Barnes 
822c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
823c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
8244912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
825036a4a7dSZhenyu Wang 
826c7c85101SZou Nan hai done:
8272d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
8283143a2bfSChris Wilson 	POSTING_READ(DEIER);
8292d109a84SZou, Nanhai 
830036a4a7dSZhenyu Wang 	return ret;
831036a4a7dSZhenyu Wang }
832036a4a7dSZhenyu Wang 
8338a905236SJesse Barnes /**
8348a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
8358a905236SJesse Barnes  * @work: work struct
8368a905236SJesse Barnes  *
8378a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
8388a905236SJesse Barnes  * was detected.
8398a905236SJesse Barnes  */
8408a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
8418a905236SJesse Barnes {
8428a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
8438a905236SJesse Barnes 						    error_work);
8448a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
845f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
846f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
847f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
8488a905236SJesse Barnes 
849f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
8508a905236SJesse Barnes 
851ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
85244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
853f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
854d4b8bb2aSDaniel Vetter 		if (!i915_reset(dev)) {
855ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
856f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
857f316a42cSBen Gamari 		}
85830dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
859f316a42cSBen Gamari 	}
8608a905236SJesse Barnes }
8618a905236SJesse Barnes 
86285f9e50dSDaniel Vetter /* NB: please notice the memset */
86385f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
86485f9e50dSDaniel Vetter 				    uint32_t *instdone)
86585f9e50dSDaniel Vetter {
86685f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
86785f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
86885f9e50dSDaniel Vetter 
86985f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
87085f9e50dSDaniel Vetter 	case 2:
87185f9e50dSDaniel Vetter 	case 3:
87285f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
87385f9e50dSDaniel Vetter 		break;
87485f9e50dSDaniel Vetter 	case 4:
87585f9e50dSDaniel Vetter 	case 5:
87685f9e50dSDaniel Vetter 	case 6:
87785f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
87885f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
87985f9e50dSDaniel Vetter 		break;
88085f9e50dSDaniel Vetter 	default:
88185f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
88285f9e50dSDaniel Vetter 	case 7:
88385f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
88485f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
88585f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
88685f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
88785f9e50dSDaniel Vetter 		break;
88885f9e50dSDaniel Vetter 	}
88985f9e50dSDaniel Vetter }
89085f9e50dSDaniel Vetter 
8913bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
8929df30794SChris Wilson static struct drm_i915_error_object *
893bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
89405394f39SChris Wilson 			 struct drm_i915_gem_object *src)
8959df30794SChris Wilson {
8969df30794SChris Wilson 	struct drm_i915_error_object *dst;
8979da3da66SChris Wilson 	int i, count;
898e56660ddSChris Wilson 	u32 reloc_offset;
8999df30794SChris Wilson 
90005394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
9019df30794SChris Wilson 		return NULL;
9029df30794SChris Wilson 
9039da3da66SChris Wilson 	count = src->base.size / PAGE_SIZE;
9049df30794SChris Wilson 
9059da3da66SChris Wilson 	dst = kmalloc(sizeof(*dst) + count * sizeof(u32 *), GFP_ATOMIC);
9069df30794SChris Wilson 	if (dst == NULL)
9079df30794SChris Wilson 		return NULL;
9089df30794SChris Wilson 
90905394f39SChris Wilson 	reloc_offset = src->gtt_offset;
9109da3da66SChris Wilson 	for (i = 0; i < count; i++) {
911788885aeSAndrew Morton 		unsigned long flags;
912e56660ddSChris Wilson 		void *d;
913788885aeSAndrew Morton 
914e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
9159df30794SChris Wilson 		if (d == NULL)
9169df30794SChris Wilson 			goto unwind;
917e56660ddSChris Wilson 
918788885aeSAndrew Morton 		local_irq_save(flags);
91974898d7eSDaniel Vetter 		if (reloc_offset < dev_priv->mm.gtt_mappable_end &&
92074898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
921172975aaSChris Wilson 			void __iomem *s;
922172975aaSChris Wilson 
923172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
924172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
925172975aaSChris Wilson 			 * captures what the GPU read.
926172975aaSChris Wilson 			 */
927172975aaSChris Wilson 
928e56660ddSChris Wilson 			s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
9293e4d3af5SPeter Zijlstra 						     reloc_offset);
930e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
9313e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
932172975aaSChris Wilson 		} else {
9339da3da66SChris Wilson 			struct page *page;
934172975aaSChris Wilson 			void *s;
935172975aaSChris Wilson 
9369da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
937172975aaSChris Wilson 
9389da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
9399da3da66SChris Wilson 
9409da3da66SChris Wilson 			s = kmap_atomic(page);
941172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
942172975aaSChris Wilson 			kunmap_atomic(s);
943172975aaSChris Wilson 
9449da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
945172975aaSChris Wilson 		}
946788885aeSAndrew Morton 		local_irq_restore(flags);
947e56660ddSChris Wilson 
9489da3da66SChris Wilson 		dst->pages[i] = d;
949e56660ddSChris Wilson 
950e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
9519df30794SChris Wilson 	}
9529da3da66SChris Wilson 	dst->page_count = count;
95305394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
9549df30794SChris Wilson 
9559df30794SChris Wilson 	return dst;
9569df30794SChris Wilson 
9579df30794SChris Wilson unwind:
9589da3da66SChris Wilson 	while (i--)
9599da3da66SChris Wilson 		kfree(dst->pages[i]);
9609df30794SChris Wilson 	kfree(dst);
9619df30794SChris Wilson 	return NULL;
9629df30794SChris Wilson }
9639df30794SChris Wilson 
9649df30794SChris Wilson static void
9659df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
9669df30794SChris Wilson {
9679df30794SChris Wilson 	int page;
9689df30794SChris Wilson 
9699df30794SChris Wilson 	if (obj == NULL)
9709df30794SChris Wilson 		return;
9719df30794SChris Wilson 
9729df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
9739df30794SChris Wilson 		kfree(obj->pages[page]);
9749df30794SChris Wilson 
9759df30794SChris Wilson 	kfree(obj);
9769df30794SChris Wilson }
9779df30794SChris Wilson 
978742cbee8SDaniel Vetter void
979742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
9809df30794SChris Wilson {
981742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
982742cbee8SDaniel Vetter 							  typeof(*error), ref);
983e2f973d5SChris Wilson 	int i;
984e2f973d5SChris Wilson 
98552d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
98652d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
98752d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
98852d39a21SChris Wilson 		kfree(error->ring[i].requests);
98952d39a21SChris Wilson 	}
990e2f973d5SChris Wilson 
9919df30794SChris Wilson 	kfree(error->active_bo);
9926ef3d427SChris Wilson 	kfree(error->overlay);
9939df30794SChris Wilson 	kfree(error);
9949df30794SChris Wilson }
9951b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
9961b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
997c724e8a9SChris Wilson {
998c724e8a9SChris Wilson 	err->size = obj->base.size;
999c724e8a9SChris Wilson 	err->name = obj->base.name;
10000201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
10010201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1002c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
1003c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1004c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1005c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1006c724e8a9SChris Wilson 	err->pinned = 0;
1007c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1008c724e8a9SChris Wilson 		err->pinned = 1;
1009c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1010c724e8a9SChris Wilson 		err->pinned = -1;
1011c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1012c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1013c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
101496154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
101593dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
10161b50247aSChris Wilson }
1017c724e8a9SChris Wilson 
10181b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
10191b50247aSChris Wilson 			     int count, struct list_head *head)
10201b50247aSChris Wilson {
10211b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
10221b50247aSChris Wilson 	int i = 0;
10231b50247aSChris Wilson 
10241b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
10251b50247aSChris Wilson 		capture_bo(err++, obj);
1026c724e8a9SChris Wilson 		if (++i == count)
1027c724e8a9SChris Wilson 			break;
10281b50247aSChris Wilson 	}
1029c724e8a9SChris Wilson 
10301b50247aSChris Wilson 	return i;
10311b50247aSChris Wilson }
10321b50247aSChris Wilson 
10331b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
10341b50247aSChris Wilson 			     int count, struct list_head *head)
10351b50247aSChris Wilson {
10361b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
10371b50247aSChris Wilson 	int i = 0;
10381b50247aSChris Wilson 
10391b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
10401b50247aSChris Wilson 		if (obj->pin_count == 0)
10411b50247aSChris Wilson 			continue;
10421b50247aSChris Wilson 
10431b50247aSChris Wilson 		capture_bo(err++, obj);
10441b50247aSChris Wilson 		if (++i == count)
10451b50247aSChris Wilson 			break;
1046c724e8a9SChris Wilson 	}
1047c724e8a9SChris Wilson 
1048c724e8a9SChris Wilson 	return i;
1049c724e8a9SChris Wilson }
1050c724e8a9SChris Wilson 
1051748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1052748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1053748ebc60SChris Wilson {
1054748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1055748ebc60SChris Wilson 	int i;
1056748ebc60SChris Wilson 
1057748ebc60SChris Wilson 	/* Fences */
1058748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1059775d17b6SDaniel Vetter 	case 7:
1060748ebc60SChris Wilson 	case 6:
1061748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1062748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1063748ebc60SChris Wilson 		break;
1064748ebc60SChris Wilson 	case 5:
1065748ebc60SChris Wilson 	case 4:
1066748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1067748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1068748ebc60SChris Wilson 		break;
1069748ebc60SChris Wilson 	case 3:
1070748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1071748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1072748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1073748ebc60SChris Wilson 	case 2:
1074748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1075748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1076748ebc60SChris Wilson 		break;
1077748ebc60SChris Wilson 
1078748ebc60SChris Wilson 	}
1079748ebc60SChris Wilson }
1080748ebc60SChris Wilson 
1081bcfb2e28SChris Wilson static struct drm_i915_error_object *
1082bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1083bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1084bcfb2e28SChris Wilson {
1085bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1086bcfb2e28SChris Wilson 	u32 seqno;
1087bcfb2e28SChris Wilson 
1088bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1089bcfb2e28SChris Wilson 		return NULL;
1090bcfb2e28SChris Wilson 
1091b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1092bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1093bcfb2e28SChris Wilson 		if (obj->ring != ring)
1094bcfb2e28SChris Wilson 			continue;
1095bcfb2e28SChris Wilson 
10960201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1097bcfb2e28SChris Wilson 			continue;
1098bcfb2e28SChris Wilson 
1099bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1100bcfb2e28SChris Wilson 			continue;
1101bcfb2e28SChris Wilson 
1102bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1103bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1104bcfb2e28SChris Wilson 		 */
1105bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1106bcfb2e28SChris Wilson 	}
1107bcfb2e28SChris Wilson 
1108bcfb2e28SChris Wilson 	return NULL;
1109bcfb2e28SChris Wilson }
1110bcfb2e28SChris Wilson 
1111d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1112d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1113d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1114d27b1e0eSDaniel Vetter {
1115d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1116d27b1e0eSDaniel Vetter 
111733f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
111812f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
111933f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
11207e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
11217e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
11227e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
11237e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
1124df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1125df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
112633f3f518SDaniel Vetter 	}
1127c1cd90edSDaniel Vetter 
1128d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
11299d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1130d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1131d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1132d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1133c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1134050ee91fSBen Widawsky 		if (ring->id == RCS)
1135d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1136d27b1e0eSDaniel Vetter 	} else {
11379d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1138d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1139d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1140d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1141d27b1e0eSDaniel Vetter 	}
1142d27b1e0eSDaniel Vetter 
11439574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1144c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1145b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1146d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1147c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1148c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
11497e3b8737SDaniel Vetter 
11507e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
11517e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1152d27b1e0eSDaniel Vetter }
1153d27b1e0eSDaniel Vetter 
115452d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
115552d39a21SChris Wilson 				  struct drm_i915_error_state *error)
115652d39a21SChris Wilson {
115752d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1158b4519513SChris Wilson 	struct intel_ring_buffer *ring;
115952d39a21SChris Wilson 	struct drm_i915_gem_request *request;
116052d39a21SChris Wilson 	int i, count;
116152d39a21SChris Wilson 
1162b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
116352d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
116452d39a21SChris Wilson 
116552d39a21SChris Wilson 		error->ring[i].batchbuffer =
116652d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
116752d39a21SChris Wilson 
116852d39a21SChris Wilson 		error->ring[i].ringbuffer =
116952d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
117052d39a21SChris Wilson 
117152d39a21SChris Wilson 		count = 0;
117252d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
117352d39a21SChris Wilson 			count++;
117452d39a21SChris Wilson 
117552d39a21SChris Wilson 		error->ring[i].num_requests = count;
117652d39a21SChris Wilson 		error->ring[i].requests =
117752d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
117852d39a21SChris Wilson 				GFP_ATOMIC);
117952d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
118052d39a21SChris Wilson 			error->ring[i].num_requests = 0;
118152d39a21SChris Wilson 			continue;
118252d39a21SChris Wilson 		}
118352d39a21SChris Wilson 
118452d39a21SChris Wilson 		count = 0;
118552d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
118652d39a21SChris Wilson 			struct drm_i915_error_request *erq;
118752d39a21SChris Wilson 
118852d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
118952d39a21SChris Wilson 			erq->seqno = request->seqno;
119052d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1191ee4f42b1SChris Wilson 			erq->tail = request->tail;
119252d39a21SChris Wilson 		}
119352d39a21SChris Wilson 	}
119452d39a21SChris Wilson }
119552d39a21SChris Wilson 
11968a905236SJesse Barnes /**
11978a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
11988a905236SJesse Barnes  * @dev: drm device
11998a905236SJesse Barnes  *
12008a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
12018a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
12028a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
12038a905236SJesse Barnes  * to pick up.
12048a905236SJesse Barnes  */
120563eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
120663eeaf38SJesse Barnes {
120763eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
120805394f39SChris Wilson 	struct drm_i915_gem_object *obj;
120963eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
121063eeaf38SJesse Barnes 	unsigned long flags;
12119db4a9c7SJesse Barnes 	int i, pipe;
121263eeaf38SJesse Barnes 
121363eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12149df30794SChris Wilson 	error = dev_priv->first_error;
12159df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
12169df30794SChris Wilson 	if (error)
12179df30794SChris Wilson 		return;
121863eeaf38SJesse Barnes 
12199db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
122033f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
122163eeaf38SJesse Barnes 	if (!error) {
12229df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
12239df30794SChris Wilson 		return;
122463eeaf38SJesse Barnes 	}
122563eeaf38SJesse Barnes 
1226b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
1227b6f7833bSChris Wilson 		 dev->primary->index);
12282fa772f3SChris Wilson 
1229742cbee8SDaniel Vetter 	kref_init(&error->ref);
123063eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
123163eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1232b9a3906bSBen Widawsky 	error->ccid = I915_READ(CCID);
1233be998e2eSBen Widawsky 
1234be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1235be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1236be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1237be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1238be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1239be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1240be998e2eSBen Widawsky 	else
1241be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1242be998e2eSBen Widawsky 
12439db4a9c7SJesse Barnes 	for_each_pipe(pipe)
12449db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1245d27b1e0eSDaniel Vetter 
124633f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1247f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
124833f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
124933f3f518SDaniel Vetter 	}
1250add354ddSChris Wilson 
125171e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
125271e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
125371e172e8SBen Widawsky 
1254050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1255050ee91fSBen Widawsky 
1256748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
125752d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
12589df30794SChris Wilson 
1259c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
12609df30794SChris Wilson 	error->active_bo = NULL;
1261c724e8a9SChris Wilson 	error->pinned_bo = NULL;
12629df30794SChris Wilson 
1263bcfb2e28SChris Wilson 	i = 0;
1264bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1265bcfb2e28SChris Wilson 		i++;
1266bcfb2e28SChris Wilson 	error->active_bo_count = i;
12676c085a72SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
12681b50247aSChris Wilson 		if (obj->pin_count)
1269bcfb2e28SChris Wilson 			i++;
1270bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1271c724e8a9SChris Wilson 
12728e934dbfSChris Wilson 	error->active_bo = NULL;
12738e934dbfSChris Wilson 	error->pinned_bo = NULL;
1274bcfb2e28SChris Wilson 	if (i) {
1275bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
12769df30794SChris Wilson 					   GFP_ATOMIC);
1277c724e8a9SChris Wilson 		if (error->active_bo)
1278c724e8a9SChris Wilson 			error->pinned_bo =
1279c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
12809df30794SChris Wilson 	}
1281c724e8a9SChris Wilson 
1282c724e8a9SChris Wilson 	if (error->active_bo)
1283c724e8a9SChris Wilson 		error->active_bo_count =
12841b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1285c724e8a9SChris Wilson 					  error->active_bo_count,
1286c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1287c724e8a9SChris Wilson 
1288c724e8a9SChris Wilson 	if (error->pinned_bo)
1289c724e8a9SChris Wilson 		error->pinned_bo_count =
12901b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1291c724e8a9SChris Wilson 					  error->pinned_bo_count,
12926c085a72SChris Wilson 					  &dev_priv->mm.bound_list);
129363eeaf38SJesse Barnes 
12948a905236SJesse Barnes 	do_gettimeofday(&error->time);
12958a905236SJesse Barnes 
12966ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1297c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
12986ef3d427SChris Wilson 
12999df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
13009df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
130163eeaf38SJesse Barnes 		dev_priv->first_error = error;
13029df30794SChris Wilson 		error = NULL;
13039df30794SChris Wilson 	}
130463eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
13059df30794SChris Wilson 
13069df30794SChris Wilson 	if (error)
1307742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
13089df30794SChris Wilson }
13099df30794SChris Wilson 
13109df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
13119df30794SChris Wilson {
13129df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
13139df30794SChris Wilson 	struct drm_i915_error_state *error;
13146dc0e816SBen Widawsky 	unsigned long flags;
13159df30794SChris Wilson 
13166dc0e816SBen Widawsky 	spin_lock_irqsave(&dev_priv->error_lock, flags);
13179df30794SChris Wilson 	error = dev_priv->first_error;
13189df30794SChris Wilson 	dev_priv->first_error = NULL;
13196dc0e816SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
13209df30794SChris Wilson 
13219df30794SChris Wilson 	if (error)
1322742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
132363eeaf38SJesse Barnes }
13243bd3c932SChris Wilson #else
13253bd3c932SChris Wilson #define i915_capture_error_state(x)
13263bd3c932SChris Wilson #endif
132763eeaf38SJesse Barnes 
132835aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1329c0e09200SDave Airlie {
13308a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1331bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
133263eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1333050ee91fSBen Widawsky 	int pipe, i;
133463eeaf38SJesse Barnes 
133535aed2e6SChris Wilson 	if (!eir)
133635aed2e6SChris Wilson 		return;
133763eeaf38SJesse Barnes 
1338a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
13398a905236SJesse Barnes 
1340bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1341bd9854f9SBen Widawsky 
13428a905236SJesse Barnes 	if (IS_G4X(dev)) {
13438a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
13448a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
13458a905236SJesse Barnes 
1346a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1347a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1348050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1349050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1350a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1351a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
13528a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
13533143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
13548a905236SJesse Barnes 		}
13558a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
13568a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1357a70491ccSJoe Perches 			pr_err("page table error\n");
1358a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
13598a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
13603143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
13618a905236SJesse Barnes 		}
13628a905236SJesse Barnes 	}
13638a905236SJesse Barnes 
1364a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
136563eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
136663eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1367a70491ccSJoe Perches 			pr_err("page table error\n");
1368a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
136963eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
13703143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
137163eeaf38SJesse Barnes 		}
13728a905236SJesse Barnes 	}
13738a905236SJesse Barnes 
137463eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1375a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
13769db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1377a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
13789db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
137963eeaf38SJesse Barnes 		/* pipestat has already been acked */
138063eeaf38SJesse Barnes 	}
138163eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1382a70491ccSJoe Perches 		pr_err("instruction error\n");
1383a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1384050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1385050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1386a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
138763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
138863eeaf38SJesse Barnes 
1389a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1390a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1391a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
139263eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
13933143a2bfSChris Wilson 			POSTING_READ(IPEIR);
139463eeaf38SJesse Barnes 		} else {
139563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
139663eeaf38SJesse Barnes 
1397a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1398a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1399a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1400a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
140163eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
14023143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
140363eeaf38SJesse Barnes 		}
140463eeaf38SJesse Barnes 	}
140563eeaf38SJesse Barnes 
140663eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
14073143a2bfSChris Wilson 	POSTING_READ(EIR);
140863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
140963eeaf38SJesse Barnes 	if (eir) {
141063eeaf38SJesse Barnes 		/*
141163eeaf38SJesse Barnes 		 * some errors might have become stuck,
141263eeaf38SJesse Barnes 		 * mask them.
141363eeaf38SJesse Barnes 		 */
141463eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
141563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
141663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
141763eeaf38SJesse Barnes 	}
141835aed2e6SChris Wilson }
141935aed2e6SChris Wilson 
142035aed2e6SChris Wilson /**
142135aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
142235aed2e6SChris Wilson  * @dev: drm device
142335aed2e6SChris Wilson  *
142435aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
142535aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
142635aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
142735aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
142835aed2e6SChris Wilson  * of a ring dump etc.).
142935aed2e6SChris Wilson  */
1430527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
143135aed2e6SChris Wilson {
143235aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1433b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1434b4519513SChris Wilson 	int i;
143535aed2e6SChris Wilson 
143635aed2e6SChris Wilson 	i915_capture_error_state(dev);
143735aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
14388a905236SJesse Barnes 
1439ba1234d1SBen Gamari 	if (wedged) {
144030dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1441ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1442ba1234d1SBen Gamari 
144311ed50ecSBen Gamari 		/*
144411ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
144511ed50ecSBen Gamari 		 */
1446b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1447b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
144811ed50ecSBen Gamari 	}
144911ed50ecSBen Gamari 
14509c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
14518a905236SJesse Barnes }
14528a905236SJesse Barnes 
14534e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
14544e5359cdSSimon Farnsworth {
14554e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
14564e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
14574e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
145805394f39SChris Wilson 	struct drm_i915_gem_object *obj;
14594e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
14604e5359cdSSimon Farnsworth 	unsigned long flags;
14614e5359cdSSimon Farnsworth 	bool stall_detected;
14624e5359cdSSimon Farnsworth 
14634e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
14644e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
14654e5359cdSSimon Farnsworth 		return;
14664e5359cdSSimon Farnsworth 
14674e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
14684e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
14694e5359cdSSimon Farnsworth 
14704e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
14714e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
14724e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
14734e5359cdSSimon Farnsworth 		return;
14744e5359cdSSimon Farnsworth 	}
14754e5359cdSSimon Farnsworth 
14764e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
147705394f39SChris Wilson 	obj = work->pending_flip_obj;
1478a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
14799db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1480446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1481446f2545SArmin Reese 					obj->gtt_offset;
14824e5359cdSSimon Farnsworth 	} else {
14839db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
148405394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
148501f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
14864e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
14874e5359cdSSimon Farnsworth 	}
14884e5359cdSSimon Farnsworth 
14894e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
14904e5359cdSSimon Farnsworth 
14914e5359cdSSimon Farnsworth 	if (stall_detected) {
14924e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
14934e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
14944e5359cdSSimon Farnsworth 	}
14954e5359cdSSimon Farnsworth }
14964e5359cdSSimon Farnsworth 
149742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
149842f52ef8SKeith Packard  * we use as a pipe index
149942f52ef8SKeith Packard  */
1500f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
15010a3e67a4SJesse Barnes {
15020a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1503e9d21d7fSKeith Packard 	unsigned long irqflags;
150471e0ffa5SJesse Barnes 
15055eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
150671e0ffa5SJesse Barnes 		return -EINVAL;
15070a3e67a4SJesse Barnes 
15081ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1509f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
15107c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
15117c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
15120a3e67a4SJesse Barnes 	else
15137c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
15147c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
15158692d00eSChris Wilson 
15168692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
15178692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15186b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
15191ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15208692d00eSChris Wilson 
15210a3e67a4SJesse Barnes 	return 0;
15220a3e67a4SJesse Barnes }
15230a3e67a4SJesse Barnes 
1524f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1525f796cf8fSJesse Barnes {
1526f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1527f796cf8fSJesse Barnes 	unsigned long irqflags;
1528f796cf8fSJesse Barnes 
1529f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1530f796cf8fSJesse Barnes 		return -EINVAL;
1531f796cf8fSJesse Barnes 
1532f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1533f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1534f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1535f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1536f796cf8fSJesse Barnes 
1537f796cf8fSJesse Barnes 	return 0;
1538f796cf8fSJesse Barnes }
1539f796cf8fSJesse Barnes 
1540f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1541b1f14ad0SJesse Barnes {
1542b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1543b1f14ad0SJesse Barnes 	unsigned long irqflags;
1544b1f14ad0SJesse Barnes 
1545b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1546b1f14ad0SJesse Barnes 		return -EINVAL;
1547b1f14ad0SJesse Barnes 
1548b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1549b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
1550b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
1551b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1552b1f14ad0SJesse Barnes 
1553b1f14ad0SJesse Barnes 	return 0;
1554b1f14ad0SJesse Barnes }
1555b1f14ad0SJesse Barnes 
15567e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
15577e231dbeSJesse Barnes {
15587e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
15597e231dbeSJesse Barnes 	unsigned long irqflags;
156031acc7f5SJesse Barnes 	u32 imr;
15617e231dbeSJesse Barnes 
15627e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
15637e231dbeSJesse Barnes 		return -EINVAL;
15647e231dbeSJesse Barnes 
15657e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15667e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
156731acc7f5SJesse Barnes 	if (pipe == 0)
15687e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
156931acc7f5SJesse Barnes 	else
15707e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
15717e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
157231acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
157331acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
15747e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15757e231dbeSJesse Barnes 
15767e231dbeSJesse Barnes 	return 0;
15777e231dbeSJesse Barnes }
15787e231dbeSJesse Barnes 
157942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
158042f52ef8SKeith Packard  * we use as a pipe index
158142f52ef8SKeith Packard  */
1582f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
15830a3e67a4SJesse Barnes {
15840a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1585e9d21d7fSKeith Packard 	unsigned long irqflags;
15860a3e67a4SJesse Barnes 
15871ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15888692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15896b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
15908692d00eSChris Wilson 
15917c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
15927c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
15937c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
15941ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15950a3e67a4SJesse Barnes }
15960a3e67a4SJesse Barnes 
1597f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1598f796cf8fSJesse Barnes {
1599f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1600f796cf8fSJesse Barnes 	unsigned long irqflags;
1601f796cf8fSJesse Barnes 
1602f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1603f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1604f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1605f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1606f796cf8fSJesse Barnes }
1607f796cf8fSJesse Barnes 
1608f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1609b1f14ad0SJesse Barnes {
1610b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1611b1f14ad0SJesse Barnes 	unsigned long irqflags;
1612b1f14ad0SJesse Barnes 
1613b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1614b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
1615b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
1616b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1617b1f14ad0SJesse Barnes }
1618b1f14ad0SJesse Barnes 
16197e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
16207e231dbeSJesse Barnes {
16217e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16227e231dbeSJesse Barnes 	unsigned long irqflags;
162331acc7f5SJesse Barnes 	u32 imr;
16247e231dbeSJesse Barnes 
16257e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
162631acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
162731acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
16287e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
162931acc7f5SJesse Barnes 	if (pipe == 0)
16307e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
163131acc7f5SJesse Barnes 	else
16327e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
16337e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
16347e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
16357e231dbeSJesse Barnes }
16367e231dbeSJesse Barnes 
1637893eead0SChris Wilson static u32
1638893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1639852835f3SZou Nan hai {
1640893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1641893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1642893eead0SChris Wilson }
1643893eead0SChris Wilson 
1644893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1645893eead0SChris Wilson {
1646893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1647b2eadbc8SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring, false),
1648b2eadbc8SChris Wilson 			      ring_last_seqno(ring))) {
1649893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
16509574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
16519574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
16529574b3feSBen Widawsky 				  ring->name);
1653893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1654893eead0SChris Wilson 			*err = true;
1655893eead0SChris Wilson 		}
1656893eead0SChris Wilson 		return true;
1657893eead0SChris Wilson 	}
1658893eead0SChris Wilson 	return false;
1659f65d9421SBen Gamari }
1660f65d9421SBen Gamari 
16611ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
16621ec14ad3SChris Wilson {
16631ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
16641ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
16651ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
16661ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
16671ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
16681ec14ad3SChris Wilson 			  ring->name);
16691ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
16701ec14ad3SChris Wilson 		return true;
16711ec14ad3SChris Wilson 	}
16721ec14ad3SChris Wilson 	return false;
16731ec14ad3SChris Wilson }
16741ec14ad3SChris Wilson 
1675d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
1676d1e61e7fSChris Wilson {
1677d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1678d1e61e7fSChris Wilson 
1679d1e61e7fSChris Wilson 	if (dev_priv->hangcheck_count++ > 1) {
1680b4519513SChris Wilson 		bool hung = true;
1681b4519513SChris Wilson 
1682d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1683d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
1684d1e61e7fSChris Wilson 
1685d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
1686b4519513SChris Wilson 			struct intel_ring_buffer *ring;
1687b4519513SChris Wilson 			int i;
1688b4519513SChris Wilson 
1689d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
1690d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
1691d1e61e7fSChris Wilson 			 * and break the hang. This should work on
1692d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
1693d1e61e7fSChris Wilson 			 */
1694b4519513SChris Wilson 			for_each_ring(ring, dev_priv, i)
1695b4519513SChris Wilson 				hung &= !kick_ring(ring);
1696d1e61e7fSChris Wilson 		}
1697d1e61e7fSChris Wilson 
1698b4519513SChris Wilson 		return hung;
1699d1e61e7fSChris Wilson 	}
1700d1e61e7fSChris Wilson 
1701d1e61e7fSChris Wilson 	return false;
1702d1e61e7fSChris Wilson }
1703d1e61e7fSChris Wilson 
1704f65d9421SBen Gamari /**
1705f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1706f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1707f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1708f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1709f65d9421SBen Gamari  */
1710f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1711f65d9421SBen Gamari {
1712f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1713f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1714bd9854f9SBen Widawsky 	uint32_t acthd[I915_NUM_RINGS], instdone[I915_NUM_INSTDONE_REG];
1715b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1716b4519513SChris Wilson 	bool err = false, idle;
1717b4519513SChris Wilson 	int i;
1718893eead0SChris Wilson 
17193e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
17203e0dc6b0SBen Widawsky 		return;
17213e0dc6b0SBen Widawsky 
1722b4519513SChris Wilson 	memset(acthd, 0, sizeof(acthd));
1723b4519513SChris Wilson 	idle = true;
1724b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
1725b4519513SChris Wilson 	    idle &= i915_hangcheck_ring_idle(ring, &err);
1726b4519513SChris Wilson 	    acthd[i] = intel_ring_get_active_head(ring);
1727b4519513SChris Wilson 	}
1728b4519513SChris Wilson 
1729893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
1730b4519513SChris Wilson 	if (idle) {
1731d1e61e7fSChris Wilson 		if (err) {
1732d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
1733d1e61e7fSChris Wilson 				return;
1734d1e61e7fSChris Wilson 
1735893eead0SChris Wilson 			goto repeat;
1736d1e61e7fSChris Wilson 		}
1737d1e61e7fSChris Wilson 
1738d1e61e7fSChris Wilson 		dev_priv->hangcheck_count = 0;
1739893eead0SChris Wilson 		return;
1740893eead0SChris Wilson 	}
1741f65d9421SBen Gamari 
1742bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1743b4519513SChris Wilson 	if (memcmp(dev_priv->last_acthd, acthd, sizeof(acthd)) == 0 &&
1744050ee91fSBen Widawsky 	    memcmp(dev_priv->prev_instdone, instdone, sizeof(instdone)) == 0) {
1745d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
1746f65d9421SBen Gamari 			return;
1747cbb465e7SChris Wilson 	} else {
1748cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1749cbb465e7SChris Wilson 
1750b4519513SChris Wilson 		memcpy(dev_priv->last_acthd, acthd, sizeof(acthd));
1751050ee91fSBen Widawsky 		memcpy(dev_priv->prev_instdone, instdone, sizeof(instdone));
1752cbb465e7SChris Wilson 	}
1753f65d9421SBen Gamari 
1754893eead0SChris Wilson repeat:
1755f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1756b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1757cecc21feSChris Wilson 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
1758f65d9421SBen Gamari }
1759f65d9421SBen Gamari 
1760c0e09200SDave Airlie /* drm_dma.h hooks
1761c0e09200SDave Airlie */
1762f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
1763036a4a7dSZhenyu Wang {
1764036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1765036a4a7dSZhenyu Wang 
17664697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17674697995bSJesse Barnes 
1768036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1769bdfcdb63SDaniel Vetter 
1770036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1771036a4a7dSZhenyu Wang 
1772036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1773036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
17743143a2bfSChris Wilson 	POSTING_READ(DEIER);
1775036a4a7dSZhenyu Wang 
1776036a4a7dSZhenyu Wang 	/* and GT */
1777036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1778036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
17793143a2bfSChris Wilson 	POSTING_READ(GTIER);
1780c650156aSZhenyu Wang 
1781c650156aSZhenyu Wang 	/* south display irq */
1782c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1783c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
17843143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1785036a4a7dSZhenyu Wang }
1786036a4a7dSZhenyu Wang 
17877e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
17887e231dbeSJesse Barnes {
17897e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17907e231dbeSJesse Barnes 	int pipe;
17917e231dbeSJesse Barnes 
17927e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17937e231dbeSJesse Barnes 
17947e231dbeSJesse Barnes 	/* VLV magic */
17957e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
17967e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
17977e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
17987e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
17997e231dbeSJesse Barnes 
18007e231dbeSJesse Barnes 	/* and GT */
18017e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
18027e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
18037e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
18047e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
18057e231dbeSJesse Barnes 	POSTING_READ(GTIER);
18067e231dbeSJesse Barnes 
18077e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
18087e231dbeSJesse Barnes 
18097e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
18107e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
18117e231dbeSJesse Barnes 	for_each_pipe(pipe)
18127e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
18137e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
18147e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
18157e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
18167e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
18177e231dbeSJesse Barnes }
18187e231dbeSJesse Barnes 
18197fe0b973SKeith Packard /*
18207fe0b973SKeith Packard  * Enable digital hotplug on the PCH, and configure the DP short pulse
18217fe0b973SKeith Packard  * duration to 2ms (which is the minimum in the Display Port spec)
18227fe0b973SKeith Packard  *
18237fe0b973SKeith Packard  * This register is the same on all known PCH chips.
18247fe0b973SKeith Packard  */
18257fe0b973SKeith Packard 
18267fe0b973SKeith Packard static void ironlake_enable_pch_hotplug(struct drm_device *dev)
18277fe0b973SKeith Packard {
18287fe0b973SKeith Packard 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18297fe0b973SKeith Packard 	u32	hotplug;
18307fe0b973SKeith Packard 
18317fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
18327fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
18337fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
18347fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
18357fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
18367fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
18377fe0b973SKeith Packard }
18387fe0b973SKeith Packard 
1839f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
1840036a4a7dSZhenyu Wang {
1841036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1842036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1843013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1844013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
18451ec14ad3SChris Wilson 	u32 render_irqs;
18462d7b8366SYuanhan Liu 	u32 hotplug_mask;
1847036a4a7dSZhenyu Wang 
18481ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1849036a4a7dSZhenyu Wang 
1850036a4a7dSZhenyu Wang 	/* should always can generate irq */
1851036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
18521ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
18531ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
18543143a2bfSChris Wilson 	POSTING_READ(DEIER);
1855036a4a7dSZhenyu Wang 
18561ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1857036a4a7dSZhenyu Wang 
1858036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
18591ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1860881f47b6SXiang, Haihao 
18611ec14ad3SChris Wilson 	if (IS_GEN6(dev))
18621ec14ad3SChris Wilson 		render_irqs =
18631ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
1864e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
1865e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
18661ec14ad3SChris Wilson 	else
18671ec14ad3SChris Wilson 		render_irqs =
186888f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1869c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
18701ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
18711ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
18723143a2bfSChris Wilson 	POSTING_READ(GTIER);
1873036a4a7dSZhenyu Wang 
18742d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
18759035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
18769035a97aSChris Wilson 				SDE_PORTB_HOTPLUG_CPT |
18779035a97aSChris Wilson 				SDE_PORTC_HOTPLUG_CPT |
18789035a97aSChris Wilson 				SDE_PORTD_HOTPLUG_CPT);
18792d7b8366SYuanhan Liu 	} else {
18809035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG |
18819035a97aSChris Wilson 				SDE_PORTB_HOTPLUG |
18829035a97aSChris Wilson 				SDE_PORTC_HOTPLUG |
18839035a97aSChris Wilson 				SDE_PORTD_HOTPLUG |
18849035a97aSChris Wilson 				SDE_AUX_MASK);
18852d7b8366SYuanhan Liu 	}
18862d7b8366SYuanhan Liu 
18871ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1888c650156aSZhenyu Wang 
1889c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
18901ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
18911ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
18923143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1893c650156aSZhenyu Wang 
18947fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
18957fe0b973SKeith Packard 
1896f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1897f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1898f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1899f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1900f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1901f97108d1SJesse Barnes 	}
1902f97108d1SJesse Barnes 
1903036a4a7dSZhenyu Wang 	return 0;
1904036a4a7dSZhenyu Wang }
1905036a4a7dSZhenyu Wang 
1906f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
1907b1f14ad0SJesse Barnes {
1908b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1909b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
1910b615b57aSChris Wilson 	u32 display_mask =
1911b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
1912b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
1913b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
1914b615b57aSChris Wilson 		DE_PLANEA_FLIP_DONE_IVB;
1915b1f14ad0SJesse Barnes 	u32 render_irqs;
1916b1f14ad0SJesse Barnes 	u32 hotplug_mask;
1917b1f14ad0SJesse Barnes 
1918b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
1919b1f14ad0SJesse Barnes 
1920b1f14ad0SJesse Barnes 	/* should always can generate irq */
1921b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1922b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
1923b615b57aSChris Wilson 	I915_WRITE(DEIER,
1924b615b57aSChris Wilson 		   display_mask |
1925b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
1926b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
1927b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
1928b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1929b1f14ad0SJesse Barnes 
193015b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
1931b1f14ad0SJesse Barnes 
1932b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1933b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1934b1f14ad0SJesse Barnes 
1935e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
193615b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
1937b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
1938b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
1939b1f14ad0SJesse Barnes 
1940b1f14ad0SJesse Barnes 	hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
1941b1f14ad0SJesse Barnes 			SDE_PORTB_HOTPLUG_CPT |
1942b1f14ad0SJesse Barnes 			SDE_PORTC_HOTPLUG_CPT |
1943b1f14ad0SJesse Barnes 			SDE_PORTD_HOTPLUG_CPT);
1944b1f14ad0SJesse Barnes 	dev_priv->pch_irq_mask = ~hotplug_mask;
1945b1f14ad0SJesse Barnes 
1946b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1947b1f14ad0SJesse Barnes 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1948b1f14ad0SJesse Barnes 	I915_WRITE(SDEIER, hotplug_mask);
1949b1f14ad0SJesse Barnes 	POSTING_READ(SDEIER);
1950b1f14ad0SJesse Barnes 
19517fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
19527fe0b973SKeith Packard 
1953b1f14ad0SJesse Barnes 	return 0;
1954b1f14ad0SJesse Barnes }
1955b1f14ad0SJesse Barnes 
19567e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
19577e231dbeSJesse Barnes {
19587e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19597e231dbeSJesse Barnes 	u32 enable_mask;
19607e231dbeSJesse Barnes 	u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
196131acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
19623bcedbe5SJesse Barnes 	u32 render_irqs;
19637e231dbeSJesse Barnes 	u16 msid;
19647e231dbeSJesse Barnes 
19657e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
196631acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
196731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
196831acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
19697e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
19707e231dbeSJesse Barnes 
197131acc7f5SJesse Barnes 	/*
197231acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
197331acc7f5SJesse Barnes 	 * toggle them based on usage.
197431acc7f5SJesse Barnes 	 */
197531acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
197631acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
197731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
19787e231dbeSJesse Barnes 
19797e231dbeSJesse Barnes 	dev_priv->pipestat[0] = 0;
19807e231dbeSJesse Barnes 	dev_priv->pipestat[1] = 0;
19817e231dbeSJesse Barnes 
19827e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
19837e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
19847e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
19857e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
19867e231dbeSJesse Barnes 	msid |= (1<<14);
19877e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
19887e231dbeSJesse Barnes 
19897e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
19907e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
19917e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19927e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
19937e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
19947e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
19957e231dbeSJesse Barnes 
199631acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
199731acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
199831acc7f5SJesse Barnes 
19997e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20007e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20017e231dbeSJesse Barnes 
200231acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
200331acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
20043bcedbe5SJesse Barnes 
20053bcedbe5SJesse Barnes 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
20063bcedbe5SJesse Barnes 		GEN6_BLITTER_USER_INTERRUPT;
20073bcedbe5SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
20087e231dbeSJesse Barnes 	POSTING_READ(GTIER);
20097e231dbeSJesse Barnes 
20107e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
20117e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
20127e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
20137e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
20147e231dbeSJesse Barnes #endif
20157e231dbeSJesse Barnes 
20167e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
20177e231dbeSJesse Barnes 	/* Note HDMI and DP share bits */
20187e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
20197e231dbeSJesse Barnes 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
20207e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
20217e231dbeSJesse Barnes 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
20227e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
20237e231dbeSJesse Barnes 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
2024ae33cdcfSVijay Purushothaman 	if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
20257e231dbeSJesse Barnes 		hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2026ae33cdcfSVijay Purushothaman 	if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
20277e231dbeSJesse Barnes 		hotplug_en |= SDVOB_HOTPLUG_INT_EN;
20287e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
20297e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_INT_EN;
20307e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
20317e231dbeSJesse Barnes 	}
20327e231dbeSJesse Barnes 
20337e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
20347e231dbeSJesse Barnes 
20357e231dbeSJesse Barnes 	return 0;
20367e231dbeSJesse Barnes }
20377e231dbeSJesse Barnes 
20387e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
20397e231dbeSJesse Barnes {
20407e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20417e231dbeSJesse Barnes 	int pipe;
20427e231dbeSJesse Barnes 
20437e231dbeSJesse Barnes 	if (!dev_priv)
20447e231dbeSJesse Barnes 		return;
20457e231dbeSJesse Barnes 
20467e231dbeSJesse Barnes 	for_each_pipe(pipe)
20477e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
20487e231dbeSJesse Barnes 
20497e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
20507e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
20517e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
20527e231dbeSJesse Barnes 	for_each_pipe(pipe)
20537e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
20547e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20557e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
20567e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
20577e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
20587e231dbeSJesse Barnes }
20597e231dbeSJesse Barnes 
2060f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2061036a4a7dSZhenyu Wang {
2062036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20634697995bSJesse Barnes 
20644697995bSJesse Barnes 	if (!dev_priv)
20654697995bSJesse Barnes 		return;
20664697995bSJesse Barnes 
2067036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2068036a4a7dSZhenyu Wang 
2069036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2070036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2071036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2072036a4a7dSZhenyu Wang 
2073036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2074036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2075036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2076192aac1fSKeith Packard 
2077192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2078192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2079192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2080036a4a7dSZhenyu Wang }
2081036a4a7dSZhenyu Wang 
2082c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2083c2798b19SChris Wilson {
2084c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2085c2798b19SChris Wilson 	int pipe;
2086c2798b19SChris Wilson 
2087c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2088c2798b19SChris Wilson 
2089c2798b19SChris Wilson 	for_each_pipe(pipe)
2090c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2091c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2092c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2093c2798b19SChris Wilson 	POSTING_READ16(IER);
2094c2798b19SChris Wilson }
2095c2798b19SChris Wilson 
2096c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2097c2798b19SChris Wilson {
2098c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2099c2798b19SChris Wilson 
2100c2798b19SChris Wilson 	dev_priv->pipestat[0] = 0;
2101c2798b19SChris Wilson 	dev_priv->pipestat[1] = 0;
2102c2798b19SChris Wilson 
2103c2798b19SChris Wilson 	I915_WRITE16(EMR,
2104c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2105c2798b19SChris Wilson 
2106c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2107c2798b19SChris Wilson 	dev_priv->irq_mask =
2108c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2109c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2110c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2111c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2112c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2113c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2114c2798b19SChris Wilson 
2115c2798b19SChris Wilson 	I915_WRITE16(IER,
2116c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2117c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2118c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2119c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2120c2798b19SChris Wilson 	POSTING_READ16(IER);
2121c2798b19SChris Wilson 
2122c2798b19SChris Wilson 	return 0;
2123c2798b19SChris Wilson }
2124c2798b19SChris Wilson 
2125ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2126c2798b19SChris Wilson {
2127c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2128c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2129c2798b19SChris Wilson 	u16 iir, new_iir;
2130c2798b19SChris Wilson 	u32 pipe_stats[2];
2131c2798b19SChris Wilson 	unsigned long irqflags;
2132c2798b19SChris Wilson 	int irq_received;
2133c2798b19SChris Wilson 	int pipe;
2134c2798b19SChris Wilson 	u16 flip_mask =
2135c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2136c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2137c2798b19SChris Wilson 
2138c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2139c2798b19SChris Wilson 
2140c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2141c2798b19SChris Wilson 	if (iir == 0)
2142c2798b19SChris Wilson 		return IRQ_NONE;
2143c2798b19SChris Wilson 
2144c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2145c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2146c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2147c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2148c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2149c2798b19SChris Wilson 		 */
2150c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2151c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2152c2798b19SChris Wilson 			i915_handle_error(dev, false);
2153c2798b19SChris Wilson 
2154c2798b19SChris Wilson 		for_each_pipe(pipe) {
2155c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2156c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2157c2798b19SChris Wilson 
2158c2798b19SChris Wilson 			/*
2159c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2160c2798b19SChris Wilson 			 */
2161c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2162c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2163c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2164c2798b19SChris Wilson 							 pipe_name(pipe));
2165c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2166c2798b19SChris Wilson 				irq_received = 1;
2167c2798b19SChris Wilson 			}
2168c2798b19SChris Wilson 		}
2169c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2170c2798b19SChris Wilson 
2171c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2172c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2173c2798b19SChris Wilson 
2174d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2175c2798b19SChris Wilson 
2176c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2177c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2178c2798b19SChris Wilson 
2179c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
2180c2798b19SChris Wilson 		    drm_handle_vblank(dev, 0)) {
2181c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
2182c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 0);
2183c2798b19SChris Wilson 				intel_finish_page_flip(dev, 0);
2184c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT;
2185c2798b19SChris Wilson 			}
2186c2798b19SChris Wilson 		}
2187c2798b19SChris Wilson 
2188c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
2189c2798b19SChris Wilson 		    drm_handle_vblank(dev, 1)) {
2190c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
2191c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 1);
2192c2798b19SChris Wilson 				intel_finish_page_flip(dev, 1);
2193c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2194c2798b19SChris Wilson 			}
2195c2798b19SChris Wilson 		}
2196c2798b19SChris Wilson 
2197c2798b19SChris Wilson 		iir = new_iir;
2198c2798b19SChris Wilson 	}
2199c2798b19SChris Wilson 
2200c2798b19SChris Wilson 	return IRQ_HANDLED;
2201c2798b19SChris Wilson }
2202c2798b19SChris Wilson 
2203c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2204c2798b19SChris Wilson {
2205c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2206c2798b19SChris Wilson 	int pipe;
2207c2798b19SChris Wilson 
2208c2798b19SChris Wilson 	for_each_pipe(pipe) {
2209c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2210c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2211c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2212c2798b19SChris Wilson 	}
2213c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2214c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2215c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2216c2798b19SChris Wilson }
2217c2798b19SChris Wilson 
2218a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2219a266c7d5SChris Wilson {
2220a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2221a266c7d5SChris Wilson 	int pipe;
2222a266c7d5SChris Wilson 
2223a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2224a266c7d5SChris Wilson 
2225a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2226a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2227a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2228a266c7d5SChris Wilson 	}
2229a266c7d5SChris Wilson 
223000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2231a266c7d5SChris Wilson 	for_each_pipe(pipe)
2232a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2233a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2234a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2235a266c7d5SChris Wilson 	POSTING_READ(IER);
2236a266c7d5SChris Wilson }
2237a266c7d5SChris Wilson 
2238a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2239a266c7d5SChris Wilson {
2240a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
224138bde180SChris Wilson 	u32 enable_mask;
2242a266c7d5SChris Wilson 
2243a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2244a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2245a266c7d5SChris Wilson 
224638bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
224738bde180SChris Wilson 
224838bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
224938bde180SChris Wilson 	dev_priv->irq_mask =
225038bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
225138bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
225238bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
225338bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
225438bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
225538bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
225638bde180SChris Wilson 
225738bde180SChris Wilson 	enable_mask =
225838bde180SChris Wilson 		I915_ASLE_INTERRUPT |
225938bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
226038bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
226138bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
226238bde180SChris Wilson 		I915_USER_INTERRUPT;
226338bde180SChris Wilson 
2264a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2265a266c7d5SChris Wilson 		/* Enable in IER... */
2266a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2267a266c7d5SChris Wilson 		/* and unmask in IMR */
2268a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2269a266c7d5SChris Wilson 	}
2270a266c7d5SChris Wilson 
2271a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2272a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2273a266c7d5SChris Wilson 	POSTING_READ(IER);
2274a266c7d5SChris Wilson 
2275a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2276a266c7d5SChris Wilson 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2277a266c7d5SChris Wilson 
2278a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2279a266c7d5SChris Wilson 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2280a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2281a266c7d5SChris Wilson 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2282a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2283a266c7d5SChris Wilson 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
2284084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
2285a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2286084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
2287a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2288a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2289a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_INT_EN;
2290a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2291a266c7d5SChris Wilson 		}
2292a266c7d5SChris Wilson 
2293a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2294a266c7d5SChris Wilson 
2295a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2296a266c7d5SChris Wilson 	}
2297a266c7d5SChris Wilson 
2298a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2299a266c7d5SChris Wilson 
2300a266c7d5SChris Wilson 	return 0;
2301a266c7d5SChris Wilson }
2302a266c7d5SChris Wilson 
2303ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2304a266c7d5SChris Wilson {
2305a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2306a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23078291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2308a266c7d5SChris Wilson 	unsigned long irqflags;
230938bde180SChris Wilson 	u32 flip_mask =
231038bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
231138bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
231238bde180SChris Wilson 	u32 flip[2] = {
231338bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT,
231438bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT
231538bde180SChris Wilson 	};
231638bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2317a266c7d5SChris Wilson 
2318a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2319a266c7d5SChris Wilson 
2320a266c7d5SChris Wilson 	iir = I915_READ(IIR);
232138bde180SChris Wilson 	do {
232238bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
23238291ee90SChris Wilson 		bool blc_event = false;
2324a266c7d5SChris Wilson 
2325a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2326a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2327a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2328a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2329a266c7d5SChris Wilson 		 */
2330a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2331a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2332a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2333a266c7d5SChris Wilson 
2334a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2335a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2336a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2337a266c7d5SChris Wilson 
233838bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2339a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2340a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2341a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2342a266c7d5SChris Wilson 							 pipe_name(pipe));
2343a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
234438bde180SChris Wilson 				irq_received = true;
2345a266c7d5SChris Wilson 			}
2346a266c7d5SChris Wilson 		}
2347a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2348a266c7d5SChris Wilson 
2349a266c7d5SChris Wilson 		if (!irq_received)
2350a266c7d5SChris Wilson 			break;
2351a266c7d5SChris Wilson 
2352a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2353a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2354a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2355a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2356a266c7d5SChris Wilson 
2357a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2358a266c7d5SChris Wilson 				  hotplug_status);
2359a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2360a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2361a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2362a266c7d5SChris Wilson 
2363a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
236438bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2365a266c7d5SChris Wilson 		}
2366a266c7d5SChris Wilson 
236738bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2368a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2369a266c7d5SChris Wilson 
2370a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2371a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2372a266c7d5SChris Wilson 
2373a266c7d5SChris Wilson 		for_each_pipe(pipe) {
237438bde180SChris Wilson 			int plane = pipe;
237538bde180SChris Wilson 			if (IS_MOBILE(dev))
237638bde180SChris Wilson 				plane = !plane;
23778291ee90SChris Wilson 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
2378a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
237938bde180SChris Wilson 				if (iir & flip[plane]) {
238038bde180SChris Wilson 					intel_prepare_page_flip(dev, plane);
2381a266c7d5SChris Wilson 					intel_finish_page_flip(dev, pipe);
238238bde180SChris Wilson 					flip_mask &= ~flip[plane];
238338bde180SChris Wilson 				}
2384a266c7d5SChris Wilson 			}
2385a266c7d5SChris Wilson 
2386a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2387a266c7d5SChris Wilson 				blc_event = true;
2388a266c7d5SChris Wilson 		}
2389a266c7d5SChris Wilson 
2390a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2391a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2392a266c7d5SChris Wilson 
2393a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2394a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2395a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2396a266c7d5SChris Wilson 		 * we would never get another interrupt.
2397a266c7d5SChris Wilson 		 *
2398a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2399a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2400a266c7d5SChris Wilson 		 * another one.
2401a266c7d5SChris Wilson 		 *
2402a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2403a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2404a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2405a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2406a266c7d5SChris Wilson 		 * stray interrupts.
2407a266c7d5SChris Wilson 		 */
240838bde180SChris Wilson 		ret = IRQ_HANDLED;
2409a266c7d5SChris Wilson 		iir = new_iir;
241038bde180SChris Wilson 	} while (iir & ~flip_mask);
2411a266c7d5SChris Wilson 
2412d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
24138291ee90SChris Wilson 
2414a266c7d5SChris Wilson 	return ret;
2415a266c7d5SChris Wilson }
2416a266c7d5SChris Wilson 
2417a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2418a266c7d5SChris Wilson {
2419a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2420a266c7d5SChris Wilson 	int pipe;
2421a266c7d5SChris Wilson 
2422a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2423a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2424a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2425a266c7d5SChris Wilson 	}
2426a266c7d5SChris Wilson 
242700d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
242855b39755SChris Wilson 	for_each_pipe(pipe) {
242955b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2430a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
243155b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
243255b39755SChris Wilson 	}
2433a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2434a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2435a266c7d5SChris Wilson 
2436a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2437a266c7d5SChris Wilson }
2438a266c7d5SChris Wilson 
2439a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2440a266c7d5SChris Wilson {
2441a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2442a266c7d5SChris Wilson 	int pipe;
2443a266c7d5SChris Wilson 
2444a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2445a266c7d5SChris Wilson 
2446a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2447a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2448a266c7d5SChris Wilson 
2449a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2450a266c7d5SChris Wilson 	for_each_pipe(pipe)
2451a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2452a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2453a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2454a266c7d5SChris Wilson 	POSTING_READ(IER);
2455a266c7d5SChris Wilson }
2456a266c7d5SChris Wilson 
2457a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2458a266c7d5SChris Wilson {
2459a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2460adca4730SChris Wilson 	u32 hotplug_en;
2461bbba0a97SChris Wilson 	u32 enable_mask;
2462a266c7d5SChris Wilson 	u32 error_mask;
2463a266c7d5SChris Wilson 
2464a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2465bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2466adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2467bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2468bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2469bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2470bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2471bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2472bbba0a97SChris Wilson 
2473bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
2474bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2475bbba0a97SChris Wilson 
2476bbba0a97SChris Wilson 	if (IS_G4X(dev))
2477bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2478a266c7d5SChris Wilson 
2479a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2480a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2481a266c7d5SChris Wilson 
2482a266c7d5SChris Wilson 	/*
2483a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2484a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2485a266c7d5SChris Wilson 	 */
2486a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2487a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2488a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2489a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2490a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2491a266c7d5SChris Wilson 	} else {
2492a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2493a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2494a266c7d5SChris Wilson 	}
2495a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2496a266c7d5SChris Wilson 
2497a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2498a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2499a266c7d5SChris Wilson 	POSTING_READ(IER);
2500a266c7d5SChris Wilson 
2501adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
2502adca4730SChris Wilson 	hotplug_en = 0;
2503a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2504a266c7d5SChris Wilson 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2505a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2506a266c7d5SChris Wilson 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2507a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2508a266c7d5SChris Wilson 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
2509084b612eSChris Wilson 	if (IS_G4X(dev)) {
2510084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_G4X)
2511a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2512084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_G4X)
2513a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2514084b612eSChris Wilson 	} else {
2515084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I965)
2516084b612eSChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2517084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I965)
2518084b612eSChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2519084b612eSChris Wilson 	}
2520a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2521a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_INT_EN;
2522a266c7d5SChris Wilson 
2523a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2524a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2525a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2526a266c7d5SChris Wilson 		   */
2527a266c7d5SChris Wilson 		if (IS_G4X(dev))
2528a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2529a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2530a266c7d5SChris Wilson 	}
2531a266c7d5SChris Wilson 
2532a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
2533a266c7d5SChris Wilson 
2534a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2535a266c7d5SChris Wilson 
2536a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2537a266c7d5SChris Wilson 
2538a266c7d5SChris Wilson 	return 0;
2539a266c7d5SChris Wilson }
2540a266c7d5SChris Wilson 
2541ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
2542a266c7d5SChris Wilson {
2543a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2544a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2545a266c7d5SChris Wilson 	u32 iir, new_iir;
2546a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2547a266c7d5SChris Wilson 	unsigned long irqflags;
2548a266c7d5SChris Wilson 	int irq_received;
2549a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
2550a266c7d5SChris Wilson 
2551a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2552a266c7d5SChris Wilson 
2553a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2554a266c7d5SChris Wilson 
2555a266c7d5SChris Wilson 	for (;;) {
25562c8ba29fSChris Wilson 		bool blc_event = false;
25572c8ba29fSChris Wilson 
2558a266c7d5SChris Wilson 		irq_received = iir != 0;
2559a266c7d5SChris Wilson 
2560a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2561a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2562a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2563a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2564a266c7d5SChris Wilson 		 */
2565a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2566a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2567a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2568a266c7d5SChris Wilson 
2569a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2570a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2571a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2572a266c7d5SChris Wilson 
2573a266c7d5SChris Wilson 			/*
2574a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2575a266c7d5SChris Wilson 			 */
2576a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2577a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2578a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2579a266c7d5SChris Wilson 							 pipe_name(pipe));
2580a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2581a266c7d5SChris Wilson 				irq_received = 1;
2582a266c7d5SChris Wilson 			}
2583a266c7d5SChris Wilson 		}
2584a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2585a266c7d5SChris Wilson 
2586a266c7d5SChris Wilson 		if (!irq_received)
2587a266c7d5SChris Wilson 			break;
2588a266c7d5SChris Wilson 
2589a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2590a266c7d5SChris Wilson 
2591a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2592adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2593a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2594a266c7d5SChris Wilson 
2595a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2596a266c7d5SChris Wilson 				  hotplug_status);
2597a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2598a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2599a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2600a266c7d5SChris Wilson 
2601a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2602a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2603a266c7d5SChris Wilson 		}
2604a266c7d5SChris Wilson 
2605a266c7d5SChris Wilson 		I915_WRITE(IIR, iir);
2606a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2607a266c7d5SChris Wilson 
2608a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2609a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2610a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2611a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2612a266c7d5SChris Wilson 
26134f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
2614a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 0);
2615a266c7d5SChris Wilson 
26164f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
2617a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 1);
2618a266c7d5SChris Wilson 
2619a266c7d5SChris Wilson 		for_each_pipe(pipe) {
26202c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
2621a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
2622a266c7d5SChris Wilson 				i915_pageflip_stall_check(dev, pipe);
2623a266c7d5SChris Wilson 				intel_finish_page_flip(dev, pipe);
2624a266c7d5SChris Wilson 			}
2625a266c7d5SChris Wilson 
2626a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2627a266c7d5SChris Wilson 				blc_event = true;
2628a266c7d5SChris Wilson 		}
2629a266c7d5SChris Wilson 
2630a266c7d5SChris Wilson 
2631a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2632a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2633a266c7d5SChris Wilson 
2634a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2635a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2636a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2637a266c7d5SChris Wilson 		 * we would never get another interrupt.
2638a266c7d5SChris Wilson 		 *
2639a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2640a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2641a266c7d5SChris Wilson 		 * another one.
2642a266c7d5SChris Wilson 		 *
2643a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2644a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2645a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2646a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2647a266c7d5SChris Wilson 		 * stray interrupts.
2648a266c7d5SChris Wilson 		 */
2649a266c7d5SChris Wilson 		iir = new_iir;
2650a266c7d5SChris Wilson 	}
2651a266c7d5SChris Wilson 
2652d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
26532c8ba29fSChris Wilson 
2654a266c7d5SChris Wilson 	return ret;
2655a266c7d5SChris Wilson }
2656a266c7d5SChris Wilson 
2657a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
2658a266c7d5SChris Wilson {
2659a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2660a266c7d5SChris Wilson 	int pipe;
2661a266c7d5SChris Wilson 
2662a266c7d5SChris Wilson 	if (!dev_priv)
2663a266c7d5SChris Wilson 		return;
2664a266c7d5SChris Wilson 
2665a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2666a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2667a266c7d5SChris Wilson 
2668a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
2669a266c7d5SChris Wilson 	for_each_pipe(pipe)
2670a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2671a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2672a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2673a266c7d5SChris Wilson 
2674a266c7d5SChris Wilson 	for_each_pipe(pipe)
2675a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
2676a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2677a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2678a266c7d5SChris Wilson }
2679a266c7d5SChris Wilson 
2680f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2681f71d4af4SJesse Barnes {
26828b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
26838b2e326dSChris Wilson 
26848b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
26858b2e326dSChris Wilson 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
2686c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
2687a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
26888b2e326dSChris Wilson 
2689f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2690f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
26917d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
2692f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2693f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2694f71d4af4SJesse Barnes 	}
2695f71d4af4SJesse Barnes 
2696c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2697f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2698c3613de9SKeith Packard 	else
2699c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2700f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2701f71d4af4SJesse Barnes 
27027e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
27037e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
27047e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
27057e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
27067e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
27077e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
27087e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
27097e231dbeSJesse Barnes 	} else if (IS_IVYBRIDGE(dev)) {
2710f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2711f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2712f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2713f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2714f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2715f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2716f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
27177d4e146fSEugeni Dodonov 	} else if (IS_HASWELL(dev)) {
27187d4e146fSEugeni Dodonov 		/* Share interrupts handling with IVB */
27197d4e146fSEugeni Dodonov 		dev->driver->irq_handler = ivybridge_irq_handler;
27207d4e146fSEugeni Dodonov 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
27217d4e146fSEugeni Dodonov 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
27227d4e146fSEugeni Dodonov 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
27237d4e146fSEugeni Dodonov 		dev->driver->enable_vblank = ivybridge_enable_vblank;
27247d4e146fSEugeni Dodonov 		dev->driver->disable_vblank = ivybridge_disable_vblank;
2725f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2726f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2727f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2728f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2729f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2730f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2731f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
2732f71d4af4SJesse Barnes 	} else {
2733c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
2734c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
2735c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
2736c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
2737c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
2738a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
2739a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
2740a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
2741a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
2742a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
2743c2798b19SChris Wilson 		} else {
2744a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
2745a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
2746a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
2747a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
2748c2798b19SChris Wilson 		}
2749f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
2750f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
2751f71d4af4SJesse Barnes 	}
2752f71d4af4SJesse Barnes }
2753