xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 26739f12cf210cb8df35969258a1f064e8e12b63)
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 
29052d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
29152d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
29252d7ecedSDaniel Vetter 		return;
29352d7ecedSDaniel Vetter 
294a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
295e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
296e67189abSJesse Barnes 
2974ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
2984ef69c7aSChris Wilson 		if (encoder->hot_plug)
2994ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
300c31c4ba3SKeith Packard 
30140ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
30240ee3381SKeith Packard 
3035ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
304eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3055ca58282SJesse Barnes }
3065ca58282SJesse Barnes 
30773edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
308f97108d1SJesse Barnes {
309f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
310b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
3119270388eSDaniel Vetter 	u8 new_delay;
3129270388eSDaniel Vetter 	unsigned long flags;
3139270388eSDaniel Vetter 
3149270388eSDaniel Vetter 	spin_lock_irqsave(&mchdev_lock, flags);
315f97108d1SJesse Barnes 
31673edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
31773edd18fSDaniel Vetter 
31820e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
3199270388eSDaniel Vetter 
3207648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
321b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
322b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
323f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
324f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
325f97108d1SJesse Barnes 
326f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
327b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
32820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
32920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
33020e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
33120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
332b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
33320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
33420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
33520e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
33620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
337f97108d1SJesse Barnes 	}
338f97108d1SJesse Barnes 
3397648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
34020e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
341f97108d1SJesse Barnes 
3429270388eSDaniel Vetter 	spin_unlock_irqrestore(&mchdev_lock, flags);
3439270388eSDaniel Vetter 
344f97108d1SJesse Barnes 	return;
345f97108d1SJesse Barnes }
346f97108d1SJesse Barnes 
347549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
348549f7365SChris Wilson 			struct intel_ring_buffer *ring)
349549f7365SChris Wilson {
350549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
3519862e600SChris Wilson 
352475553deSChris Wilson 	if (ring->obj == NULL)
353475553deSChris Wilson 		return;
354475553deSChris Wilson 
355b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
3569862e600SChris Wilson 
357549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3583e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
35999584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
36099584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
361cecc21feSChris Wilson 			  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
3623e0dc6b0SBen Widawsky 	}
363549f7365SChris Wilson }
364549f7365SChris Wilson 
3654912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
3663b8d8d91SJesse Barnes {
3674912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
368c6a828d3SDaniel Vetter 						    rps.work);
3694912d041SBen Widawsky 	u32 pm_iir, pm_imr;
3707b9e0ae6SChris Wilson 	u8 new_delay;
3713b8d8d91SJesse Barnes 
372c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
373c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
374c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
3754912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
376a9e2641dSDaniel Vetter 	I915_WRITE(GEN6_PMIMR, 0);
377c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
3784912d041SBen Widawsky 
3797b9e0ae6SChris Wilson 	if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
3803b8d8d91SJesse Barnes 		return;
3813b8d8d91SJesse Barnes 
3824fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
3837b9e0ae6SChris Wilson 
3847b9e0ae6SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
385c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
3867b9e0ae6SChris Wilson 	else
387c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
3883b8d8d91SJesse Barnes 
38979249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
39079249636SBen Widawsky 	 * interrupt
39179249636SBen Widawsky 	 */
39279249636SBen Widawsky 	if (!(new_delay > dev_priv->rps.max_delay ||
39379249636SBen Widawsky 	      new_delay < dev_priv->rps.min_delay)) {
3944912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
39579249636SBen Widawsky 	}
3963b8d8d91SJesse Barnes 
3974fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
3983b8d8d91SJesse Barnes }
3993b8d8d91SJesse Barnes 
400e3689190SBen Widawsky 
401e3689190SBen Widawsky /**
402e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
403e3689190SBen Widawsky  * occurred.
404e3689190SBen Widawsky  * @work: workqueue struct
405e3689190SBen Widawsky  *
406e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
407e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
408e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
409e3689190SBen Widawsky  */
410e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
411e3689190SBen Widawsky {
412e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
413a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
414e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
415e3689190SBen Widawsky 	char *parity_event[5];
416e3689190SBen Widawsky 	uint32_t misccpctl;
417e3689190SBen Widawsky 	unsigned long flags;
418e3689190SBen Widawsky 
419e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
420e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
421e3689190SBen Widawsky 	 * any time we access those registers.
422e3689190SBen Widawsky 	 */
423e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
424e3689190SBen Widawsky 
425e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
426e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
427e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
428e3689190SBen Widawsky 
429e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
430e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
431e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
432e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
433e3689190SBen Widawsky 
434e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
435e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
436e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
437e3689190SBen Widawsky 
438e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
439e3689190SBen Widawsky 
440e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
441e3689190SBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
442e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
443e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
444e3689190SBen Widawsky 
445e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
446e3689190SBen Widawsky 
447e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
448e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
449e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
450e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
451e3689190SBen Widawsky 	parity_event[4] = NULL;
452e3689190SBen Widawsky 
453e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
454e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
455e3689190SBen Widawsky 
456e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
457e3689190SBen Widawsky 		  row, bank, subbank);
458e3689190SBen Widawsky 
459e3689190SBen Widawsky 	kfree(parity_event[3]);
460e3689190SBen Widawsky 	kfree(parity_event[2]);
461e3689190SBen Widawsky 	kfree(parity_event[1]);
462e3689190SBen Widawsky }
463e3689190SBen Widawsky 
464d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
465e3689190SBen Widawsky {
466e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
467e3689190SBen Widawsky 	unsigned long flags;
468e3689190SBen Widawsky 
469e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
470e3689190SBen Widawsky 		return;
471e3689190SBen Widawsky 
472e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
473e3689190SBen Widawsky 	dev_priv->gt_irq_mask |= GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
474e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
475e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
476e3689190SBen Widawsky 
477a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
478e3689190SBen Widawsky }
479e3689190SBen Widawsky 
480e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
481e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
482e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
483e7b4c6b1SDaniel Vetter {
484e7b4c6b1SDaniel Vetter 
485e7b4c6b1SDaniel Vetter 	if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
486e7b4c6b1SDaniel Vetter 		      GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
487e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
488e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BSD_USER_INTERRUPT)
489e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
490e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
491e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
492e7b4c6b1SDaniel Vetter 
493e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
494e7b4c6b1SDaniel Vetter 		      GT_GEN6_BSD_CS_ERROR_INTERRUPT |
495e7b4c6b1SDaniel Vetter 		      GT_RENDER_CS_ERROR_INTERRUPT)) {
496e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
497e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
498e7b4c6b1SDaniel Vetter 	}
499e3689190SBen Widawsky 
500e3689190SBen Widawsky 	if (gt_iir & GT_GEN7_L3_PARITY_ERROR_INTERRUPT)
501e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
502e7b4c6b1SDaniel Vetter }
503e7b4c6b1SDaniel Vetter 
504fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
505fc6826d1SChris Wilson 				u32 pm_iir)
506fc6826d1SChris Wilson {
507fc6826d1SChris Wilson 	unsigned long flags;
508fc6826d1SChris Wilson 
509fc6826d1SChris Wilson 	/*
510fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
511fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
512fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
513c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
514fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
515fc6826d1SChris Wilson 	 *
516c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
517fc6826d1SChris Wilson 	 */
518fc6826d1SChris Wilson 
519c6a828d3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
520c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
521c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
522fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
523c6a828d3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
524fc6826d1SChris Wilson 
525c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
526fc6826d1SChris Wilson }
527fc6826d1SChris Wilson 
528515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
529515ac2bbSDaniel Vetter {
53028c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
53128c70f16SDaniel Vetter 
53228c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
533515ac2bbSDaniel Vetter }
534515ac2bbSDaniel Vetter 
535ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
536ce99c256SDaniel Vetter {
5379ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
5389ee32feaSDaniel Vetter 
5399ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
540ce99c256SDaniel Vetter }
541ce99c256SDaniel Vetter 
542ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
5437e231dbeSJesse Barnes {
5447e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
5457e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5467e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
5477e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
5487e231dbeSJesse Barnes 	unsigned long irqflags;
5497e231dbeSJesse Barnes 	int pipe;
5507e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
5517e231dbeSJesse Barnes 
5527e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5537e231dbeSJesse Barnes 
5547e231dbeSJesse Barnes 	while (true) {
5557e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
5567e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
5577e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
5587e231dbeSJesse Barnes 
5597e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
5607e231dbeSJesse Barnes 			goto out;
5617e231dbeSJesse Barnes 
5627e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
5637e231dbeSJesse Barnes 
564e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
5657e231dbeSJesse Barnes 
5667e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5677e231dbeSJesse Barnes 		for_each_pipe(pipe) {
5687e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
5697e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
5707e231dbeSJesse Barnes 
5717e231dbeSJesse Barnes 			/*
5727e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
5737e231dbeSJesse Barnes 			 */
5747e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
5757e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
5767e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
5777e231dbeSJesse Barnes 							 pipe_name(pipe));
5787e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
5797e231dbeSJesse Barnes 			}
5807e231dbeSJesse Barnes 		}
5817e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5827e231dbeSJesse Barnes 
58331acc7f5SJesse Barnes 		for_each_pipe(pipe) {
58431acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
58531acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
58631acc7f5SJesse Barnes 
58731acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
58831acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
58931acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
59031acc7f5SJesse Barnes 			}
59131acc7f5SJesse Barnes 		}
59231acc7f5SJesse Barnes 
5937e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
5947e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
5957e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
5967e231dbeSJesse Barnes 
5977e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
5987e231dbeSJesse Barnes 					 hotplug_status);
5997e231dbeSJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
6007e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
6017e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
6027e231dbeSJesse Barnes 
6037e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
6047e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
6057e231dbeSJesse Barnes 		}
6067e231dbeSJesse Barnes 
607515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
608515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
6097e231dbeSJesse Barnes 
610fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
611fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
6127e231dbeSJesse Barnes 
6137e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
6147e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
6157e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
6167e231dbeSJesse Barnes 	}
6177e231dbeSJesse Barnes 
6187e231dbeSJesse Barnes out:
6197e231dbeSJesse Barnes 	return ret;
6207e231dbeSJesse Barnes }
6217e231dbeSJesse Barnes 
62223e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
623776ad806SJesse Barnes {
624776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6259db4a9c7SJesse Barnes 	int pipe;
626776ad806SJesse Barnes 
62776e43830SDaniel Vetter 	if (pch_iir & SDE_HOTPLUG_MASK)
62876e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
62976e43830SDaniel Vetter 
630776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
631776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
632776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
633776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
634776ad806SJesse Barnes 
635ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
636ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
637ce99c256SDaniel Vetter 
638776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
639515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
640776ad806SJesse Barnes 
641776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
642776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
643776ad806SJesse Barnes 
644776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
645776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
646776ad806SJesse Barnes 
647776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
648776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
649776ad806SJesse Barnes 
6509db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
6519db4a9c7SJesse Barnes 		for_each_pipe(pipe)
6529db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
6539db4a9c7SJesse Barnes 					 pipe_name(pipe),
6549db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
655776ad806SJesse Barnes 
656776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
657776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
658776ad806SJesse Barnes 
659776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
660776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
661776ad806SJesse Barnes 
662776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
663776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
664776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
665776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
666776ad806SJesse Barnes }
667776ad806SJesse Barnes 
66823e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
66923e81d69SAdam Jackson {
67023e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
67123e81d69SAdam Jackson 	int pipe;
67223e81d69SAdam Jackson 
67376e43830SDaniel Vetter 	if (pch_iir & SDE_HOTPLUG_MASK_CPT)
67476e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
67576e43830SDaniel Vetter 
67623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT)
67723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
67823e81d69SAdam Jackson 				 (pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
67923e81d69SAdam Jackson 				 SDE_AUDIO_POWER_SHIFT_CPT);
68023e81d69SAdam Jackson 
68123e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
682ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
68323e81d69SAdam Jackson 
68423e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
685515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
68623e81d69SAdam Jackson 
68723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
68823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
68923e81d69SAdam Jackson 
69023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
69123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
69223e81d69SAdam Jackson 
69323e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
69423e81d69SAdam Jackson 		for_each_pipe(pipe)
69523e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
69623e81d69SAdam Jackson 					 pipe_name(pipe),
69723e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
69823e81d69SAdam Jackson }
69923e81d69SAdam Jackson 
700ff1f525eSDaniel Vetter static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
701b1f14ad0SJesse Barnes {
702b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
703b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7040e43406bSChris Wilson 	u32 de_iir, gt_iir, de_ier, pm_iir;
7050e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
7060e43406bSChris Wilson 	int i;
707b1f14ad0SJesse Barnes 
708b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
709b1f14ad0SJesse Barnes 
710b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
711b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
712b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
7130e43406bSChris Wilson 
7140e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
7150e43406bSChris Wilson 	if (gt_iir) {
7160e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
7170e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
7180e43406bSChris Wilson 		ret = IRQ_HANDLED;
7190e43406bSChris Wilson 	}
720b1f14ad0SJesse Barnes 
721b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
7220e43406bSChris Wilson 	if (de_iir) {
723ce99c256SDaniel Vetter 		if (de_iir & DE_AUX_CHANNEL_A_IVB)
724ce99c256SDaniel Vetter 			dp_aux_irq_handler(dev);
725ce99c256SDaniel Vetter 
726b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
727b1f14ad0SJesse Barnes 			intel_opregion_gse_intr(dev);
728b1f14ad0SJesse Barnes 
7290e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
73074d44445SDaniel Vetter 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
73174d44445SDaniel Vetter 				drm_handle_vblank(dev, i);
7320e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
7330e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
7340e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
735b1f14ad0SJesse Barnes 			}
736b1f14ad0SJesse Barnes 		}
737b1f14ad0SJesse Barnes 
738b1f14ad0SJesse Barnes 		/* check event from PCH */
739b1f14ad0SJesse Barnes 		if (de_iir & DE_PCH_EVENT_IVB) {
7400e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
7410e43406bSChris Wilson 
74223e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
7430e43406bSChris Wilson 
7440e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
7450e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
746b1f14ad0SJesse Barnes 		}
747b1f14ad0SJesse Barnes 
7480e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
7490e43406bSChris Wilson 		ret = IRQ_HANDLED;
7500e43406bSChris Wilson 	}
7510e43406bSChris Wilson 
7520e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
7530e43406bSChris Wilson 	if (pm_iir) {
754fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
755fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
756b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
7570e43406bSChris Wilson 		ret = IRQ_HANDLED;
7580e43406bSChris Wilson 	}
759b1f14ad0SJesse Barnes 
760b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
761b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
762b1f14ad0SJesse Barnes 
763b1f14ad0SJesse Barnes 	return ret;
764b1f14ad0SJesse Barnes }
765b1f14ad0SJesse Barnes 
766e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
767e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
768e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
769e7b4c6b1SDaniel Vetter {
770e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
771e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
772e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
773e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
774e7b4c6b1SDaniel Vetter }
775e7b4c6b1SDaniel Vetter 
776ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
777036a4a7dSZhenyu Wang {
7784697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
779036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
780036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
781acd15b6cSDaniel Vetter 	u32 de_iir, gt_iir, de_ier, pm_iir;
782881f47b6SXiang, Haihao 
7834697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
7844697995bSJesse Barnes 
7852d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
7862d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
7872d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
7883143a2bfSChris Wilson 	POSTING_READ(DEIER);
7892d109a84SZou, Nanhai 
790036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
791036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
7923b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
793036a4a7dSZhenyu Wang 
794acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
795c7c85101SZou Nan hai 		goto done;
796036a4a7dSZhenyu Wang 
797036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
798036a4a7dSZhenyu Wang 
799e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
800e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
801e7b4c6b1SDaniel Vetter 	else
802e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
803036a4a7dSZhenyu Wang 
804ce99c256SDaniel Vetter 	if (de_iir & DE_AUX_CHANNEL_A)
805ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
806ce99c256SDaniel Vetter 
80701c66889SZhao Yakui 	if (de_iir & DE_GSE)
8083b617967SChris Wilson 		intel_opregion_gse_intr(dev);
80901c66889SZhao Yakui 
81074d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
81174d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
81274d44445SDaniel Vetter 
81374d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
81474d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
81574d44445SDaniel Vetter 
816f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
817013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
8182bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
819013d5aa2SJesse Barnes 	}
820013d5aa2SJesse Barnes 
821f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
822f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
8232bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
824013d5aa2SJesse Barnes 	}
825c062df61SLi Peng 
826c650156aSZhenyu Wang 	/* check event from PCH */
827776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
828acd15b6cSDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
829acd15b6cSDaniel Vetter 
83023e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
83123e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
83223e81d69SAdam Jackson 		else
83323e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
834acd15b6cSDaniel Vetter 
835acd15b6cSDaniel Vetter 		/* should clear PCH hotplug event before clear CPU irq */
836acd15b6cSDaniel Vetter 		I915_WRITE(SDEIIR, pch_iir);
837776ad806SJesse Barnes 	}
838c650156aSZhenyu Wang 
83973edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
84073edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
841f97108d1SJesse Barnes 
842fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
843fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
8443b8d8d91SJesse Barnes 
845c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
846c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
8474912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
848036a4a7dSZhenyu Wang 
849c7c85101SZou Nan hai done:
8502d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
8513143a2bfSChris Wilson 	POSTING_READ(DEIER);
8522d109a84SZou, Nanhai 
853036a4a7dSZhenyu Wang 	return ret;
854036a4a7dSZhenyu Wang }
855036a4a7dSZhenyu Wang 
8568a905236SJesse Barnes /**
8578a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
8588a905236SJesse Barnes  * @work: work struct
8598a905236SJesse Barnes  *
8608a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
8618a905236SJesse Barnes  * was detected.
8628a905236SJesse Barnes  */
8638a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
8648a905236SJesse Barnes {
8651f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
8661f83fee0SDaniel Vetter 						    work);
8671f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
8681f83fee0SDaniel Vetter 						    gpu_error);
8698a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
870f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
871f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
872f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
873f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
874f69061beSDaniel Vetter 	int i, ret;
8758a905236SJesse Barnes 
876f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
8778a905236SJesse Barnes 
8787db0ba24SDaniel Vetter 	/*
8797db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
8807db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
8817db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
8827db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
8837db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
8847db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
8857db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
8867db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
8877db0ba24SDaniel Vetter 	 */
8887db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
88944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
8907db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
8917db0ba24SDaniel Vetter 				   reset_event);
8921f83fee0SDaniel Vetter 
893f69061beSDaniel Vetter 		ret = i915_reset(dev);
894f69061beSDaniel Vetter 
895f69061beSDaniel Vetter 		if (ret == 0) {
896f69061beSDaniel Vetter 			/*
897f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
898f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
899f69061beSDaniel Vetter 			 * complete.
900f69061beSDaniel Vetter 			 *
901f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
902f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
903f69061beSDaniel Vetter 			 * updates before
904f69061beSDaniel Vetter 			 * the counter increment.
905f69061beSDaniel Vetter 			 */
906f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
907f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
908f69061beSDaniel Vetter 
909f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
910f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
9111f83fee0SDaniel Vetter 		} else {
9121f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
913f316a42cSBen Gamari 		}
9141f83fee0SDaniel Vetter 
915f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
916f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
917f69061beSDaniel Vetter 
9181f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
919f316a42cSBen Gamari 	}
9208a905236SJesse Barnes }
9218a905236SJesse Barnes 
92285f9e50dSDaniel Vetter /* NB: please notice the memset */
92385f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
92485f9e50dSDaniel Vetter 				    uint32_t *instdone)
92585f9e50dSDaniel Vetter {
92685f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
92785f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
92885f9e50dSDaniel Vetter 
92985f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
93085f9e50dSDaniel Vetter 	case 2:
93185f9e50dSDaniel Vetter 	case 3:
93285f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
93385f9e50dSDaniel Vetter 		break;
93485f9e50dSDaniel Vetter 	case 4:
93585f9e50dSDaniel Vetter 	case 5:
93685f9e50dSDaniel Vetter 	case 6:
93785f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
93885f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
93985f9e50dSDaniel Vetter 		break;
94085f9e50dSDaniel Vetter 	default:
94185f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
94285f9e50dSDaniel Vetter 	case 7:
94385f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
94485f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
94585f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
94685f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
94785f9e50dSDaniel Vetter 		break;
94885f9e50dSDaniel Vetter 	}
94985f9e50dSDaniel Vetter }
95085f9e50dSDaniel Vetter 
9513bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
9529df30794SChris Wilson static struct drm_i915_error_object *
953bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
95405394f39SChris Wilson 			 struct drm_i915_gem_object *src)
9559df30794SChris Wilson {
9569df30794SChris Wilson 	struct drm_i915_error_object *dst;
9579da3da66SChris Wilson 	int i, count;
958e56660ddSChris Wilson 	u32 reloc_offset;
9599df30794SChris Wilson 
96005394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
9619df30794SChris Wilson 		return NULL;
9629df30794SChris Wilson 
9639da3da66SChris Wilson 	count = src->base.size / PAGE_SIZE;
9649df30794SChris Wilson 
9659da3da66SChris Wilson 	dst = kmalloc(sizeof(*dst) + count * sizeof(u32 *), GFP_ATOMIC);
9669df30794SChris Wilson 	if (dst == NULL)
9679df30794SChris Wilson 		return NULL;
9689df30794SChris Wilson 
96905394f39SChris Wilson 	reloc_offset = src->gtt_offset;
9709da3da66SChris Wilson 	for (i = 0; i < count; i++) {
971788885aeSAndrew Morton 		unsigned long flags;
972e56660ddSChris Wilson 		void *d;
973788885aeSAndrew Morton 
974e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
9759df30794SChris Wilson 		if (d == NULL)
9769df30794SChris Wilson 			goto unwind;
977e56660ddSChris Wilson 
978788885aeSAndrew Morton 		local_irq_save(flags);
9795d4545aeSBen Widawsky 		if (reloc_offset < dev_priv->gtt.mappable_end &&
98074898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
981172975aaSChris Wilson 			void __iomem *s;
982172975aaSChris Wilson 
983172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
984172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
985172975aaSChris Wilson 			 * captures what the GPU read.
986172975aaSChris Wilson 			 */
987172975aaSChris Wilson 
9885d4545aeSBen Widawsky 			s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
9893e4d3af5SPeter Zijlstra 						     reloc_offset);
990e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
9913e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
992960e3564SChris Wilson 		} else if (src->stolen) {
993960e3564SChris Wilson 			unsigned long offset;
994960e3564SChris Wilson 
995960e3564SChris Wilson 			offset = dev_priv->mm.stolen_base;
996960e3564SChris Wilson 			offset += src->stolen->start;
997960e3564SChris Wilson 			offset += i << PAGE_SHIFT;
998960e3564SChris Wilson 
9991a240d4dSDaniel Vetter 			memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
1000172975aaSChris Wilson 		} else {
10019da3da66SChris Wilson 			struct page *page;
1002172975aaSChris Wilson 			void *s;
1003172975aaSChris Wilson 
10049da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
1005172975aaSChris Wilson 
10069da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
10079da3da66SChris Wilson 
10089da3da66SChris Wilson 			s = kmap_atomic(page);
1009172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
1010172975aaSChris Wilson 			kunmap_atomic(s);
1011172975aaSChris Wilson 
10129da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
1013172975aaSChris Wilson 		}
1014788885aeSAndrew Morton 		local_irq_restore(flags);
1015e56660ddSChris Wilson 
10169da3da66SChris Wilson 		dst->pages[i] = d;
1017e56660ddSChris Wilson 
1018e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
10199df30794SChris Wilson 	}
10209da3da66SChris Wilson 	dst->page_count = count;
102105394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
10229df30794SChris Wilson 
10239df30794SChris Wilson 	return dst;
10249df30794SChris Wilson 
10259df30794SChris Wilson unwind:
10269da3da66SChris Wilson 	while (i--)
10279da3da66SChris Wilson 		kfree(dst->pages[i]);
10289df30794SChris Wilson 	kfree(dst);
10299df30794SChris Wilson 	return NULL;
10309df30794SChris Wilson }
10319df30794SChris Wilson 
10329df30794SChris Wilson static void
10339df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
10349df30794SChris Wilson {
10359df30794SChris Wilson 	int page;
10369df30794SChris Wilson 
10379df30794SChris Wilson 	if (obj == NULL)
10389df30794SChris Wilson 		return;
10399df30794SChris Wilson 
10409df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
10419df30794SChris Wilson 		kfree(obj->pages[page]);
10429df30794SChris Wilson 
10439df30794SChris Wilson 	kfree(obj);
10449df30794SChris Wilson }
10459df30794SChris Wilson 
1046742cbee8SDaniel Vetter void
1047742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
10489df30794SChris Wilson {
1049742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
1050742cbee8SDaniel Vetter 							  typeof(*error), ref);
1051e2f973d5SChris Wilson 	int i;
1052e2f973d5SChris Wilson 
105352d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
105452d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
105552d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
105652d39a21SChris Wilson 		kfree(error->ring[i].requests);
105752d39a21SChris Wilson 	}
1058e2f973d5SChris Wilson 
10599df30794SChris Wilson 	kfree(error->active_bo);
10606ef3d427SChris Wilson 	kfree(error->overlay);
10619df30794SChris Wilson 	kfree(error);
10629df30794SChris Wilson }
10631b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
10641b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
1065c724e8a9SChris Wilson {
1066c724e8a9SChris Wilson 	err->size = obj->base.size;
1067c724e8a9SChris Wilson 	err->name = obj->base.name;
10680201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
10690201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1070c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
1071c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1072c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1073c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1074c724e8a9SChris Wilson 	err->pinned = 0;
1075c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1076c724e8a9SChris Wilson 		err->pinned = 1;
1077c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1078c724e8a9SChris Wilson 		err->pinned = -1;
1079c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1080c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1081c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
108296154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
108393dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
10841b50247aSChris Wilson }
1085c724e8a9SChris Wilson 
10861b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
10871b50247aSChris Wilson 			     int count, struct list_head *head)
10881b50247aSChris Wilson {
10891b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
10901b50247aSChris Wilson 	int i = 0;
10911b50247aSChris Wilson 
10921b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
10931b50247aSChris Wilson 		capture_bo(err++, obj);
1094c724e8a9SChris Wilson 		if (++i == count)
1095c724e8a9SChris Wilson 			break;
10961b50247aSChris Wilson 	}
1097c724e8a9SChris Wilson 
10981b50247aSChris Wilson 	return i;
10991b50247aSChris Wilson }
11001b50247aSChris Wilson 
11011b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
11021b50247aSChris Wilson 			     int count, struct list_head *head)
11031b50247aSChris Wilson {
11041b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
11051b50247aSChris Wilson 	int i = 0;
11061b50247aSChris Wilson 
11071b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
11081b50247aSChris Wilson 		if (obj->pin_count == 0)
11091b50247aSChris Wilson 			continue;
11101b50247aSChris Wilson 
11111b50247aSChris Wilson 		capture_bo(err++, obj);
11121b50247aSChris Wilson 		if (++i == count)
11131b50247aSChris Wilson 			break;
1114c724e8a9SChris Wilson 	}
1115c724e8a9SChris Wilson 
1116c724e8a9SChris Wilson 	return i;
1117c724e8a9SChris Wilson }
1118c724e8a9SChris Wilson 
1119748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1120748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1121748ebc60SChris Wilson {
1122748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1123748ebc60SChris Wilson 	int i;
1124748ebc60SChris Wilson 
1125748ebc60SChris Wilson 	/* Fences */
1126748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1127775d17b6SDaniel Vetter 	case 7:
1128748ebc60SChris Wilson 	case 6:
1129748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1130748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1131748ebc60SChris Wilson 		break;
1132748ebc60SChris Wilson 	case 5:
1133748ebc60SChris Wilson 	case 4:
1134748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1135748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1136748ebc60SChris Wilson 		break;
1137748ebc60SChris Wilson 	case 3:
1138748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1139748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1140748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1141748ebc60SChris Wilson 	case 2:
1142748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1143748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1144748ebc60SChris Wilson 		break;
1145748ebc60SChris Wilson 
11467dbf9d6eSBen Widawsky 	default:
11477dbf9d6eSBen Widawsky 		BUG();
1148748ebc60SChris Wilson 	}
1149748ebc60SChris Wilson }
1150748ebc60SChris Wilson 
1151bcfb2e28SChris Wilson static struct drm_i915_error_object *
1152bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1153bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1154bcfb2e28SChris Wilson {
1155bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1156bcfb2e28SChris Wilson 	u32 seqno;
1157bcfb2e28SChris Wilson 
1158bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1159bcfb2e28SChris Wilson 		return NULL;
1160bcfb2e28SChris Wilson 
1161b45305fcSDaniel Vetter 	if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1162b45305fcSDaniel Vetter 		u32 acthd = I915_READ(ACTHD);
1163b45305fcSDaniel Vetter 
1164b45305fcSDaniel Vetter 		if (WARN_ON(ring->id != RCS))
1165b45305fcSDaniel Vetter 			return NULL;
1166b45305fcSDaniel Vetter 
1167b45305fcSDaniel Vetter 		obj = ring->private;
1168b45305fcSDaniel Vetter 		if (acthd >= obj->gtt_offset &&
1169b45305fcSDaniel Vetter 		    acthd < obj->gtt_offset + obj->base.size)
1170b45305fcSDaniel Vetter 			return i915_error_object_create(dev_priv, obj);
1171b45305fcSDaniel Vetter 	}
1172b45305fcSDaniel Vetter 
1173b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1174bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1175bcfb2e28SChris Wilson 		if (obj->ring != ring)
1176bcfb2e28SChris Wilson 			continue;
1177bcfb2e28SChris Wilson 
11780201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1179bcfb2e28SChris Wilson 			continue;
1180bcfb2e28SChris Wilson 
1181bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1182bcfb2e28SChris Wilson 			continue;
1183bcfb2e28SChris Wilson 
1184bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1185bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1186bcfb2e28SChris Wilson 		 */
1187bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1188bcfb2e28SChris Wilson 	}
1189bcfb2e28SChris Wilson 
1190bcfb2e28SChris Wilson 	return NULL;
1191bcfb2e28SChris Wilson }
1192bcfb2e28SChris Wilson 
1193d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1194d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1195d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1196d27b1e0eSDaniel Vetter {
1197d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1198d27b1e0eSDaniel Vetter 
119933f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
120012f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
120133f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
12027e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
12037e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
12047e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
12057e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
1206df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1207df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
120833f3f518SDaniel Vetter 	}
1209c1cd90edSDaniel Vetter 
1210d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
12119d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1212d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1213d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1214d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1215c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1216050ee91fSBen Widawsky 		if (ring->id == RCS)
1217d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1218d27b1e0eSDaniel Vetter 	} else {
12199d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1220d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1221d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1222d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1223d27b1e0eSDaniel Vetter 	}
1224d27b1e0eSDaniel Vetter 
12259574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1226c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1227b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1228d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1229c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1230c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
12310f3b6849SChris Wilson 	error->ctl[ring->id] = I915_READ_CTL(ring);
12327e3b8737SDaniel Vetter 
12337e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
12347e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1235d27b1e0eSDaniel Vetter }
1236d27b1e0eSDaniel Vetter 
123752d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
123852d39a21SChris Wilson 				  struct drm_i915_error_state *error)
123952d39a21SChris Wilson {
124052d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1241b4519513SChris Wilson 	struct intel_ring_buffer *ring;
124252d39a21SChris Wilson 	struct drm_i915_gem_request *request;
124352d39a21SChris Wilson 	int i, count;
124452d39a21SChris Wilson 
1245b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
124652d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
124752d39a21SChris Wilson 
124852d39a21SChris Wilson 		error->ring[i].batchbuffer =
124952d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
125052d39a21SChris Wilson 
125152d39a21SChris Wilson 		error->ring[i].ringbuffer =
125252d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
125352d39a21SChris Wilson 
125452d39a21SChris Wilson 		count = 0;
125552d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
125652d39a21SChris Wilson 			count++;
125752d39a21SChris Wilson 
125852d39a21SChris Wilson 		error->ring[i].num_requests = count;
125952d39a21SChris Wilson 		error->ring[i].requests =
126052d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
126152d39a21SChris Wilson 				GFP_ATOMIC);
126252d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
126352d39a21SChris Wilson 			error->ring[i].num_requests = 0;
126452d39a21SChris Wilson 			continue;
126552d39a21SChris Wilson 		}
126652d39a21SChris Wilson 
126752d39a21SChris Wilson 		count = 0;
126852d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
126952d39a21SChris Wilson 			struct drm_i915_error_request *erq;
127052d39a21SChris Wilson 
127152d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
127252d39a21SChris Wilson 			erq->seqno = request->seqno;
127352d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1274ee4f42b1SChris Wilson 			erq->tail = request->tail;
127552d39a21SChris Wilson 		}
127652d39a21SChris Wilson 	}
127752d39a21SChris Wilson }
127852d39a21SChris Wilson 
12798a905236SJesse Barnes /**
12808a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
12818a905236SJesse Barnes  * @dev: drm device
12828a905236SJesse Barnes  *
12838a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
12848a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
12858a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
12868a905236SJesse Barnes  * to pick up.
12878a905236SJesse Barnes  */
128863eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
128963eeaf38SJesse Barnes {
129063eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
129105394f39SChris Wilson 	struct drm_i915_gem_object *obj;
129263eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
129363eeaf38SJesse Barnes 	unsigned long flags;
12949db4a9c7SJesse Barnes 	int i, pipe;
129563eeaf38SJesse Barnes 
129699584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
129799584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
129899584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
12999df30794SChris Wilson 	if (error)
13009df30794SChris Wilson 		return;
130163eeaf38SJesse Barnes 
13029db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
130333f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
130463eeaf38SJesse Barnes 	if (!error) {
13059df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
13069df30794SChris Wilson 		return;
130763eeaf38SJesse Barnes 	}
130863eeaf38SJesse Barnes 
13092f86f191SBen Widawsky 	DRM_INFO("capturing error event; look for more information in"
13102f86f191SBen Widawsky 		 "/sys/kernel/debug/dri/%d/i915_error_state\n",
1311b6f7833bSChris Wilson 		 dev->primary->index);
13122fa772f3SChris Wilson 
1313742cbee8SDaniel Vetter 	kref_init(&error->ref);
131463eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
131563eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1316b9a3906bSBen Widawsky 	error->ccid = I915_READ(CCID);
1317be998e2eSBen Widawsky 
1318be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1319be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1320be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1321be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1322be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1323be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1324be998e2eSBen Widawsky 	else
1325be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1326be998e2eSBen Widawsky 
13270f3b6849SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6)
13280f3b6849SChris Wilson 		error->derrmr = I915_READ(DERRMR);
13290f3b6849SChris Wilson 
13300f3b6849SChris Wilson 	if (IS_VALLEYVIEW(dev))
13310f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_VLV);
13320f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 7)
13330f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_MT);
13340f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen == 6)
13350f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE);
13360f3b6849SChris Wilson 
13379db4a9c7SJesse Barnes 	for_each_pipe(pipe)
13389db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1339d27b1e0eSDaniel Vetter 
134033f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1341f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
134233f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
134333f3f518SDaniel Vetter 	}
1344add354ddSChris Wilson 
134571e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
134671e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
134771e172e8SBen Widawsky 
1348050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1349050ee91fSBen Widawsky 
1350748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
135152d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
13529df30794SChris Wilson 
1353c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
13549df30794SChris Wilson 	error->active_bo = NULL;
1355c724e8a9SChris Wilson 	error->pinned_bo = NULL;
13569df30794SChris Wilson 
1357bcfb2e28SChris Wilson 	i = 0;
1358bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1359bcfb2e28SChris Wilson 		i++;
1360bcfb2e28SChris Wilson 	error->active_bo_count = i;
13616c085a72SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
13621b50247aSChris Wilson 		if (obj->pin_count)
1363bcfb2e28SChris Wilson 			i++;
1364bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1365c724e8a9SChris Wilson 
13668e934dbfSChris Wilson 	error->active_bo = NULL;
13678e934dbfSChris Wilson 	error->pinned_bo = NULL;
1368bcfb2e28SChris Wilson 	if (i) {
1369bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
13709df30794SChris Wilson 					   GFP_ATOMIC);
1371c724e8a9SChris Wilson 		if (error->active_bo)
1372c724e8a9SChris Wilson 			error->pinned_bo =
1373c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
13749df30794SChris Wilson 	}
1375c724e8a9SChris Wilson 
1376c724e8a9SChris Wilson 	if (error->active_bo)
1377c724e8a9SChris Wilson 		error->active_bo_count =
13781b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1379c724e8a9SChris Wilson 					  error->active_bo_count,
1380c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1381c724e8a9SChris Wilson 
1382c724e8a9SChris Wilson 	if (error->pinned_bo)
1383c724e8a9SChris Wilson 		error->pinned_bo_count =
13841b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1385c724e8a9SChris Wilson 					  error->pinned_bo_count,
13866c085a72SChris Wilson 					  &dev_priv->mm.bound_list);
138763eeaf38SJesse Barnes 
13888a905236SJesse Barnes 	do_gettimeofday(&error->time);
13898a905236SJesse Barnes 
13906ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1391c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
13926ef3d427SChris Wilson 
139399584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
139499584db3SDaniel Vetter 	if (dev_priv->gpu_error.first_error == NULL) {
139599584db3SDaniel Vetter 		dev_priv->gpu_error.first_error = error;
13969df30794SChris Wilson 		error = NULL;
13979df30794SChris Wilson 	}
139899584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
13999df30794SChris Wilson 
14009df30794SChris Wilson 	if (error)
1401742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
14029df30794SChris Wilson }
14039df30794SChris Wilson 
14049df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
14059df30794SChris Wilson {
14069df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
14079df30794SChris Wilson 	struct drm_i915_error_state *error;
14086dc0e816SBen Widawsky 	unsigned long flags;
14099df30794SChris Wilson 
141099584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
141199584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
141299584db3SDaniel Vetter 	dev_priv->gpu_error.first_error = NULL;
141399584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
14149df30794SChris Wilson 
14159df30794SChris Wilson 	if (error)
1416742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
141763eeaf38SJesse Barnes }
14183bd3c932SChris Wilson #else
14193bd3c932SChris Wilson #define i915_capture_error_state(x)
14203bd3c932SChris Wilson #endif
142163eeaf38SJesse Barnes 
142235aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1423c0e09200SDave Airlie {
14248a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1425bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
142663eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1427050ee91fSBen Widawsky 	int pipe, i;
142863eeaf38SJesse Barnes 
142935aed2e6SChris Wilson 	if (!eir)
143035aed2e6SChris Wilson 		return;
143163eeaf38SJesse Barnes 
1432a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
14338a905236SJesse Barnes 
1434bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1435bd9854f9SBen Widawsky 
14368a905236SJesse Barnes 	if (IS_G4X(dev)) {
14378a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
14388a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
14398a905236SJesse Barnes 
1440a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1441a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1442050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1443050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1444a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1445a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
14468a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
14473143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
14488a905236SJesse Barnes 		}
14498a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
14508a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1451a70491ccSJoe Perches 			pr_err("page table error\n");
1452a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
14538a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
14543143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
14558a905236SJesse Barnes 		}
14568a905236SJesse Barnes 	}
14578a905236SJesse Barnes 
1458a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
145963eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
146063eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1461a70491ccSJoe Perches 			pr_err("page table error\n");
1462a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
146363eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
14643143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
146563eeaf38SJesse Barnes 		}
14668a905236SJesse Barnes 	}
14678a905236SJesse Barnes 
146863eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1469a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
14709db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1471a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
14729db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
147363eeaf38SJesse Barnes 		/* pipestat has already been acked */
147463eeaf38SJesse Barnes 	}
147563eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1476a70491ccSJoe Perches 		pr_err("instruction error\n");
1477a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1478050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1479050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1480a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
148163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
148263eeaf38SJesse Barnes 
1483a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1484a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1485a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
148663eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
14873143a2bfSChris Wilson 			POSTING_READ(IPEIR);
148863eeaf38SJesse Barnes 		} else {
148963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
149063eeaf38SJesse Barnes 
1491a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1492a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1493a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1494a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
149563eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
14963143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
149763eeaf38SJesse Barnes 		}
149863eeaf38SJesse Barnes 	}
149963eeaf38SJesse Barnes 
150063eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
15013143a2bfSChris Wilson 	POSTING_READ(EIR);
150263eeaf38SJesse Barnes 	eir = I915_READ(EIR);
150363eeaf38SJesse Barnes 	if (eir) {
150463eeaf38SJesse Barnes 		/*
150563eeaf38SJesse Barnes 		 * some errors might have become stuck,
150663eeaf38SJesse Barnes 		 * mask them.
150763eeaf38SJesse Barnes 		 */
150863eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
150963eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
151063eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
151163eeaf38SJesse Barnes 	}
151235aed2e6SChris Wilson }
151335aed2e6SChris Wilson 
151435aed2e6SChris Wilson /**
151535aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
151635aed2e6SChris Wilson  * @dev: drm device
151735aed2e6SChris Wilson  *
151835aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
151935aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
152035aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
152135aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
152235aed2e6SChris Wilson  * of a ring dump etc.).
152335aed2e6SChris Wilson  */
1524527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
152535aed2e6SChris Wilson {
152635aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1527b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1528b4519513SChris Wilson 	int i;
152935aed2e6SChris Wilson 
153035aed2e6SChris Wilson 	i915_capture_error_state(dev);
153135aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
15328a905236SJesse Barnes 
1533ba1234d1SBen Gamari 	if (wedged) {
1534f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1535f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
1536ba1234d1SBen Gamari 
153711ed50ecSBen Gamari 		/*
15381f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
15391f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
154011ed50ecSBen Gamari 		 */
1541b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1542b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
154311ed50ecSBen Gamari 	}
154411ed50ecSBen Gamari 
154599584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
15468a905236SJesse Barnes }
15478a905236SJesse Barnes 
15484e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
15494e5359cdSSimon Farnsworth {
15504e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
15514e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
15524e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
155305394f39SChris Wilson 	struct drm_i915_gem_object *obj;
15544e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
15554e5359cdSSimon Farnsworth 	unsigned long flags;
15564e5359cdSSimon Farnsworth 	bool stall_detected;
15574e5359cdSSimon Farnsworth 
15584e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
15594e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
15604e5359cdSSimon Farnsworth 		return;
15614e5359cdSSimon Farnsworth 
15624e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
15634e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
15644e5359cdSSimon Farnsworth 
1565e7d841caSChris Wilson 	if (work == NULL ||
1566e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1567e7d841caSChris Wilson 	    !work->enable_stall_check) {
15684e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
15694e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
15704e5359cdSSimon Farnsworth 		return;
15714e5359cdSSimon Farnsworth 	}
15724e5359cdSSimon Farnsworth 
15734e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
157405394f39SChris Wilson 	obj = work->pending_flip_obj;
1575a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
15769db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1577446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1578446f2545SArmin Reese 					obj->gtt_offset;
15794e5359cdSSimon Farnsworth 	} else {
15809db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
158105394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
158201f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
15834e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
15844e5359cdSSimon Farnsworth 	}
15854e5359cdSSimon Farnsworth 
15864e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
15874e5359cdSSimon Farnsworth 
15884e5359cdSSimon Farnsworth 	if (stall_detected) {
15894e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
15904e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
15914e5359cdSSimon Farnsworth 	}
15924e5359cdSSimon Farnsworth }
15934e5359cdSSimon Farnsworth 
159442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
159542f52ef8SKeith Packard  * we use as a pipe index
159642f52ef8SKeith Packard  */
1597f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
15980a3e67a4SJesse Barnes {
15990a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1600e9d21d7fSKeith Packard 	unsigned long irqflags;
160171e0ffa5SJesse Barnes 
16025eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
160371e0ffa5SJesse Barnes 		return -EINVAL;
16040a3e67a4SJesse Barnes 
16051ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1606f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
16077c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
16087c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
16090a3e67a4SJesse Barnes 	else
16107c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
16117c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
16128692d00eSChris Wilson 
16138692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
16148692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
16156b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
16161ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
16178692d00eSChris Wilson 
16180a3e67a4SJesse Barnes 	return 0;
16190a3e67a4SJesse Barnes }
16200a3e67a4SJesse Barnes 
1621f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1622f796cf8fSJesse Barnes {
1623f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1624f796cf8fSJesse Barnes 	unsigned long irqflags;
1625f796cf8fSJesse Barnes 
1626f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1627f796cf8fSJesse Barnes 		return -EINVAL;
1628f796cf8fSJesse Barnes 
1629f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1630f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1631f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1632f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1633f796cf8fSJesse Barnes 
1634f796cf8fSJesse Barnes 	return 0;
1635f796cf8fSJesse Barnes }
1636f796cf8fSJesse Barnes 
1637f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1638b1f14ad0SJesse Barnes {
1639b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1640b1f14ad0SJesse Barnes 	unsigned long irqflags;
1641b1f14ad0SJesse Barnes 
1642b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1643b1f14ad0SJesse Barnes 		return -EINVAL;
1644b1f14ad0SJesse Barnes 
1645b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1646b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
1647b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
1648b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1649b1f14ad0SJesse Barnes 
1650b1f14ad0SJesse Barnes 	return 0;
1651b1f14ad0SJesse Barnes }
1652b1f14ad0SJesse Barnes 
16537e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
16547e231dbeSJesse Barnes {
16557e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16567e231dbeSJesse Barnes 	unsigned long irqflags;
165731acc7f5SJesse Barnes 	u32 imr;
16587e231dbeSJesse Barnes 
16597e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
16607e231dbeSJesse Barnes 		return -EINVAL;
16617e231dbeSJesse Barnes 
16627e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
16637e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
166431acc7f5SJesse Barnes 	if (pipe == 0)
16657e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
166631acc7f5SJesse Barnes 	else
16677e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
16687e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
166931acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
167031acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
16717e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
16727e231dbeSJesse Barnes 
16737e231dbeSJesse Barnes 	return 0;
16747e231dbeSJesse Barnes }
16757e231dbeSJesse Barnes 
167642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
167742f52ef8SKeith Packard  * we use as a pipe index
167842f52ef8SKeith Packard  */
1679f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
16800a3e67a4SJesse Barnes {
16810a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1682e9d21d7fSKeith Packard 	unsigned long irqflags;
16830a3e67a4SJesse Barnes 
16841ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
16858692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
16866b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
16878692d00eSChris Wilson 
16887c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
16897c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
16907c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
16911ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
16920a3e67a4SJesse Barnes }
16930a3e67a4SJesse Barnes 
1694f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1695f796cf8fSJesse Barnes {
1696f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1697f796cf8fSJesse Barnes 	unsigned long irqflags;
1698f796cf8fSJesse Barnes 
1699f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1700f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1701f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1702f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1703f796cf8fSJesse Barnes }
1704f796cf8fSJesse Barnes 
1705f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1706b1f14ad0SJesse Barnes {
1707b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1708b1f14ad0SJesse Barnes 	unsigned long irqflags;
1709b1f14ad0SJesse Barnes 
1710b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1711b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
1712b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
1713b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1714b1f14ad0SJesse Barnes }
1715b1f14ad0SJesse Barnes 
17167e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
17177e231dbeSJesse Barnes {
17187e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17197e231dbeSJesse Barnes 	unsigned long irqflags;
172031acc7f5SJesse Barnes 	u32 imr;
17217e231dbeSJesse Barnes 
17227e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
172331acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
172431acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
17257e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
172631acc7f5SJesse Barnes 	if (pipe == 0)
17277e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
172831acc7f5SJesse Barnes 	else
17297e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
17307e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
17317e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17327e231dbeSJesse Barnes }
17337e231dbeSJesse Barnes 
1734893eead0SChris Wilson static u32
1735893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1736852835f3SZou Nan hai {
1737893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1738893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1739893eead0SChris Wilson }
1740893eead0SChris Wilson 
1741893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1742893eead0SChris Wilson {
1743893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1744b2eadbc8SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring, false),
1745b2eadbc8SChris Wilson 			      ring_last_seqno(ring))) {
1746893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
17479574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
17489574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
17499574b3feSBen Widawsky 				  ring->name);
1750893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1751893eead0SChris Wilson 			*err = true;
1752893eead0SChris Wilson 		}
1753893eead0SChris Wilson 		return true;
1754893eead0SChris Wilson 	}
1755893eead0SChris Wilson 	return false;
1756f65d9421SBen Gamari }
1757f65d9421SBen Gamari 
17581ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
17591ec14ad3SChris Wilson {
17601ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
17611ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
17621ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
17631ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
17641ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
17651ec14ad3SChris Wilson 			  ring->name);
17661ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
17671ec14ad3SChris Wilson 		return true;
17681ec14ad3SChris Wilson 	}
17691ec14ad3SChris Wilson 	return false;
17701ec14ad3SChris Wilson }
17711ec14ad3SChris Wilson 
1772d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
1773d1e61e7fSChris Wilson {
1774d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1775d1e61e7fSChris Wilson 
177699584db3SDaniel Vetter 	if (dev_priv->gpu_error.hangcheck_count++ > 1) {
1777b4519513SChris Wilson 		bool hung = true;
1778b4519513SChris Wilson 
1779d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1780d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
1781d1e61e7fSChris Wilson 
1782d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
1783b4519513SChris Wilson 			struct intel_ring_buffer *ring;
1784b4519513SChris Wilson 			int i;
1785b4519513SChris Wilson 
1786d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
1787d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
1788d1e61e7fSChris Wilson 			 * and break the hang. This should work on
1789d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
1790d1e61e7fSChris Wilson 			 */
1791b4519513SChris Wilson 			for_each_ring(ring, dev_priv, i)
1792b4519513SChris Wilson 				hung &= !kick_ring(ring);
1793d1e61e7fSChris Wilson 		}
1794d1e61e7fSChris Wilson 
1795b4519513SChris Wilson 		return hung;
1796d1e61e7fSChris Wilson 	}
1797d1e61e7fSChris Wilson 
1798d1e61e7fSChris Wilson 	return false;
1799d1e61e7fSChris Wilson }
1800d1e61e7fSChris Wilson 
1801f65d9421SBen Gamari /**
1802f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1803f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1804f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1805f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1806f65d9421SBen Gamari  */
1807f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1808f65d9421SBen Gamari {
1809f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1810f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1811bd9854f9SBen Widawsky 	uint32_t acthd[I915_NUM_RINGS], instdone[I915_NUM_INSTDONE_REG];
1812b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1813b4519513SChris Wilson 	bool err = false, idle;
1814b4519513SChris Wilson 	int i;
1815893eead0SChris Wilson 
18163e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
18173e0dc6b0SBen Widawsky 		return;
18183e0dc6b0SBen Widawsky 
1819b4519513SChris Wilson 	memset(acthd, 0, sizeof(acthd));
1820b4519513SChris Wilson 	idle = true;
1821b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
1822b4519513SChris Wilson 	    idle &= i915_hangcheck_ring_idle(ring, &err);
1823b4519513SChris Wilson 	    acthd[i] = intel_ring_get_active_head(ring);
1824b4519513SChris Wilson 	}
1825b4519513SChris Wilson 
1826893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
1827b4519513SChris Wilson 	if (idle) {
1828d1e61e7fSChris Wilson 		if (err) {
1829d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
1830d1e61e7fSChris Wilson 				return;
1831d1e61e7fSChris Wilson 
1832893eead0SChris Wilson 			goto repeat;
1833d1e61e7fSChris Wilson 		}
1834d1e61e7fSChris Wilson 
183599584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
1836893eead0SChris Wilson 		return;
1837893eead0SChris Wilson 	}
1838f65d9421SBen Gamari 
1839bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
184099584db3SDaniel Vetter 	if (memcmp(dev_priv->gpu_error.last_acthd, acthd,
184199584db3SDaniel Vetter 		   sizeof(acthd)) == 0 &&
184299584db3SDaniel Vetter 	    memcmp(dev_priv->gpu_error.prev_instdone, instdone,
184399584db3SDaniel Vetter 		   sizeof(instdone)) == 0) {
1844d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
1845f65d9421SBen Gamari 			return;
1846cbb465e7SChris Wilson 	} else {
184799584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
1848cbb465e7SChris Wilson 
184999584db3SDaniel Vetter 		memcpy(dev_priv->gpu_error.last_acthd, acthd,
185099584db3SDaniel Vetter 		       sizeof(acthd));
185199584db3SDaniel Vetter 		memcpy(dev_priv->gpu_error.prev_instdone, instdone,
185299584db3SDaniel Vetter 		       sizeof(instdone));
1853cbb465e7SChris Wilson 	}
1854f65d9421SBen Gamari 
1855893eead0SChris Wilson repeat:
1856f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
185799584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
1858cecc21feSChris Wilson 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
1859f65d9421SBen Gamari }
1860f65d9421SBen Gamari 
1861c0e09200SDave Airlie /* drm_dma.h hooks
1862c0e09200SDave Airlie */
1863f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
1864036a4a7dSZhenyu Wang {
1865036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1866036a4a7dSZhenyu Wang 
18674697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
18684697995bSJesse Barnes 
1869036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1870bdfcdb63SDaniel Vetter 
1871036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1872036a4a7dSZhenyu Wang 
1873036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1874036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
18753143a2bfSChris Wilson 	POSTING_READ(DEIER);
1876036a4a7dSZhenyu Wang 
1877036a4a7dSZhenyu Wang 	/* and GT */
1878036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1879036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
18803143a2bfSChris Wilson 	POSTING_READ(GTIER);
1881c650156aSZhenyu Wang 
1882c650156aSZhenyu Wang 	/* south display irq */
1883c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1884c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
18853143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1886036a4a7dSZhenyu Wang }
1887036a4a7dSZhenyu Wang 
18887e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
18897e231dbeSJesse Barnes {
18907e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18917e231dbeSJesse Barnes 	int pipe;
18927e231dbeSJesse Barnes 
18937e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
18947e231dbeSJesse Barnes 
18957e231dbeSJesse Barnes 	/* VLV magic */
18967e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
18977e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
18987e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
18997e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
19007e231dbeSJesse Barnes 
19017e231dbeSJesse Barnes 	/* and GT */
19027e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
19037e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
19047e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
19057e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
19067e231dbeSJesse Barnes 	POSTING_READ(GTIER);
19077e231dbeSJesse Barnes 
19087e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
19097e231dbeSJesse Barnes 
19107e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
19117e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
19127e231dbeSJesse Barnes 	for_each_pipe(pipe)
19137e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
19147e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19157e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
19167e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
19177e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
19187e231dbeSJesse Barnes }
19197e231dbeSJesse Barnes 
19207fe0b973SKeith Packard /*
19217fe0b973SKeith Packard  * Enable digital hotplug on the PCH, and configure the DP short pulse
19227fe0b973SKeith Packard  * duration to 2ms (which is the minimum in the Display Port spec)
19237fe0b973SKeith Packard  *
19247fe0b973SKeith Packard  * This register is the same on all known PCH chips.
19257fe0b973SKeith Packard  */
19267fe0b973SKeith Packard 
19277fe0b973SKeith Packard static void ironlake_enable_pch_hotplug(struct drm_device *dev)
19287fe0b973SKeith Packard {
19297fe0b973SKeith Packard 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19307fe0b973SKeith Packard 	u32	hotplug;
19317fe0b973SKeith Packard 
19327fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
19337fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
19347fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
19357fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
19367fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
19377fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
19387fe0b973SKeith Packard }
19397fe0b973SKeith Packard 
1940f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
1941036a4a7dSZhenyu Wang {
1942036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1943036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1944013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1945ce99c256SDaniel Vetter 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
1946ce99c256SDaniel Vetter 			   DE_AUX_CHANNEL_A;
19471ec14ad3SChris Wilson 	u32 render_irqs;
19482d7b8366SYuanhan Liu 	u32 hotplug_mask;
1949af5163acSEgbert Eich 	u32 pch_irq_mask;
1950036a4a7dSZhenyu Wang 
19511ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1952036a4a7dSZhenyu Wang 
1953036a4a7dSZhenyu Wang 	/* should always can generate irq */
1954036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
19551ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
19561ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
19573143a2bfSChris Wilson 	POSTING_READ(DEIER);
1958036a4a7dSZhenyu Wang 
19591ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1960036a4a7dSZhenyu Wang 
1961036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
19621ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1963881f47b6SXiang, Haihao 
19641ec14ad3SChris Wilson 	if (IS_GEN6(dev))
19651ec14ad3SChris Wilson 		render_irqs =
19661ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
1967e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
1968e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
19691ec14ad3SChris Wilson 	else
19701ec14ad3SChris Wilson 		render_irqs =
197188f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1972c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
19731ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
19741ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
19753143a2bfSChris Wilson 	POSTING_READ(GTIER);
1976036a4a7dSZhenyu Wang 
19772d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
19789035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
19799035a97aSChris Wilson 				SDE_PORTB_HOTPLUG_CPT |
19809035a97aSChris Wilson 				SDE_PORTC_HOTPLUG_CPT |
1981515ac2bbSDaniel Vetter 				SDE_PORTD_HOTPLUG_CPT |
1982ce99c256SDaniel Vetter 				SDE_GMBUS_CPT |
1983ce99c256SDaniel Vetter 				SDE_AUX_MASK_CPT);
19842d7b8366SYuanhan Liu 	} else {
19859035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG |
19869035a97aSChris Wilson 				SDE_PORTB_HOTPLUG |
19879035a97aSChris Wilson 				SDE_PORTC_HOTPLUG |
19889035a97aSChris Wilson 				SDE_PORTD_HOTPLUG |
1989515ac2bbSDaniel Vetter 				SDE_GMBUS |
19909035a97aSChris Wilson 				SDE_AUX_MASK);
19912d7b8366SYuanhan Liu 	}
19922d7b8366SYuanhan Liu 
1993af5163acSEgbert Eich 	pch_irq_mask = ~hotplug_mask;
1994c650156aSZhenyu Wang 
1995c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1996af5163acSEgbert Eich 	I915_WRITE(SDEIMR, pch_irq_mask);
19971ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
19983143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1999c650156aSZhenyu Wang 
20007fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
20017fe0b973SKeith Packard 
2002f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
2003f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
2004f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
2005f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
2006f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2007f97108d1SJesse Barnes 	}
2008f97108d1SJesse Barnes 
2009036a4a7dSZhenyu Wang 	return 0;
2010036a4a7dSZhenyu Wang }
2011036a4a7dSZhenyu Wang 
2012f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2013b1f14ad0SJesse Barnes {
2014b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2015b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2016b615b57aSChris Wilson 	u32 display_mask =
2017b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2018b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
2019b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
2020ce99c256SDaniel Vetter 		DE_PLANEA_FLIP_DONE_IVB |
2021ce99c256SDaniel Vetter 		DE_AUX_CHANNEL_A_IVB;
2022b1f14ad0SJesse Barnes 	u32 render_irqs;
2023b1f14ad0SJesse Barnes 	u32 hotplug_mask;
2024af5163acSEgbert Eich 	u32 pch_irq_mask;
2025b1f14ad0SJesse Barnes 
2026b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2027b1f14ad0SJesse Barnes 
2028b1f14ad0SJesse Barnes 	/* should always can generate irq */
2029b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2030b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2031b615b57aSChris Wilson 	I915_WRITE(DEIER,
2032b615b57aSChris Wilson 		   display_mask |
2033b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
2034b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
2035b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
2036b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2037b1f14ad0SJesse Barnes 
203815b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2039b1f14ad0SJesse Barnes 
2040b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2041b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2042b1f14ad0SJesse Barnes 
2043e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
204415b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2045b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
2046b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2047b1f14ad0SJesse Barnes 
2048b1f14ad0SJesse Barnes 	hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
2049b1f14ad0SJesse Barnes 			SDE_PORTB_HOTPLUG_CPT |
2050b1f14ad0SJesse Barnes 			SDE_PORTC_HOTPLUG_CPT |
2051515ac2bbSDaniel Vetter 			SDE_PORTD_HOTPLUG_CPT |
2052ce99c256SDaniel Vetter 			SDE_GMBUS_CPT |
2053ce99c256SDaniel Vetter 			SDE_AUX_MASK_CPT);
2054af5163acSEgbert Eich 	pch_irq_mask = ~hotplug_mask;
2055b1f14ad0SJesse Barnes 
2056b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2057af5163acSEgbert Eich 	I915_WRITE(SDEIMR, pch_irq_mask);
2058b1f14ad0SJesse Barnes 	I915_WRITE(SDEIER, hotplug_mask);
2059b1f14ad0SJesse Barnes 	POSTING_READ(SDEIER);
2060b1f14ad0SJesse Barnes 
20617fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
20627fe0b973SKeith Packard 
2063b1f14ad0SJesse Barnes 	return 0;
2064b1f14ad0SJesse Barnes }
2065b1f14ad0SJesse Barnes 
20667e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
20677e231dbeSJesse Barnes {
20687e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20697e231dbeSJesse Barnes 	u32 enable_mask;
207031acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
20713bcedbe5SJesse Barnes 	u32 render_irqs;
20727e231dbeSJesse Barnes 	u16 msid;
20737e231dbeSJesse Barnes 
20747e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
207531acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
207631acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
207731acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
20787e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
20797e231dbeSJesse Barnes 
208031acc7f5SJesse Barnes 	/*
208131acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
208231acc7f5SJesse Barnes 	 * toggle them based on usage.
208331acc7f5SJesse Barnes 	 */
208431acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
208531acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
208631acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
20877e231dbeSJesse Barnes 
20887e231dbeSJesse Barnes 	dev_priv->pipestat[0] = 0;
20897e231dbeSJesse Barnes 	dev_priv->pipestat[1] = 0;
20907e231dbeSJesse Barnes 
20917e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
20927e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
20937e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
20947e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
20957e231dbeSJesse Barnes 	msid |= (1<<14);
20967e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
20977e231dbeSJesse Barnes 
209820afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
209920afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
210020afbda2SDaniel Vetter 
21017e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
21027e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
21037e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
21047e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
21057e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
21067e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
21077e231dbeSJesse Barnes 
210831acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2109515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
211031acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
211131acc7f5SJesse Barnes 
21127e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
21137e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
21147e231dbeSJesse Barnes 
211531acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
211631acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
21173bcedbe5SJesse Barnes 
21183bcedbe5SJesse Barnes 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
21193bcedbe5SJesse Barnes 		GEN6_BLITTER_USER_INTERRUPT;
21203bcedbe5SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
21217e231dbeSJesse Barnes 	POSTING_READ(GTIER);
21227e231dbeSJesse Barnes 
21237e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
21247e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
21257e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
21267e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
21277e231dbeSJesse Barnes #endif
21287e231dbeSJesse Barnes 
21297e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
213020afbda2SDaniel Vetter 
213120afbda2SDaniel Vetter 	return 0;
213220afbda2SDaniel Vetter }
213320afbda2SDaniel Vetter 
213420afbda2SDaniel Vetter static void valleyview_hpd_irq_setup(struct drm_device *dev)
213520afbda2SDaniel Vetter {
213620afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
213720afbda2SDaniel Vetter 	u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
213820afbda2SDaniel Vetter 
21397e231dbeSJesse Barnes 	/* Note HDMI and DP share bits */
2140*26739f12SDaniel Vetter 	if (dev_priv->hotplug_supported_mask & PORTB_HOTPLUG_INT_STATUS)
2141*26739f12SDaniel Vetter 		hotplug_en |= PORTB_HOTPLUG_INT_EN;
2142*26739f12SDaniel Vetter 	if (dev_priv->hotplug_supported_mask & PORTC_HOTPLUG_INT_STATUS)
2143*26739f12SDaniel Vetter 		hotplug_en |= PORTC_HOTPLUG_INT_EN;
2144*26739f12SDaniel Vetter 	if (dev_priv->hotplug_supported_mask & PORTD_HOTPLUG_INT_STATUS)
2145*26739f12SDaniel Vetter 		hotplug_en |= PORTD_HOTPLUG_INT_EN;
2146ae33cdcfSVijay Purushothaman 	if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
21477e231dbeSJesse Barnes 		hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2148ae33cdcfSVijay Purushothaman 	if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
21497e231dbeSJesse Barnes 		hotplug_en |= SDVOB_HOTPLUG_INT_EN;
21507e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
21517e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_INT_EN;
21527e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
21537e231dbeSJesse Barnes 	}
21547e231dbeSJesse Barnes 
21557e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
21567e231dbeSJesse Barnes }
21577e231dbeSJesse Barnes 
21587e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
21597e231dbeSJesse Barnes {
21607e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21617e231dbeSJesse Barnes 	int pipe;
21627e231dbeSJesse Barnes 
21637e231dbeSJesse Barnes 	if (!dev_priv)
21647e231dbeSJesse Barnes 		return;
21657e231dbeSJesse Barnes 
21667e231dbeSJesse Barnes 	for_each_pipe(pipe)
21677e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
21687e231dbeSJesse Barnes 
21697e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
21707e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
21717e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
21727e231dbeSJesse Barnes 	for_each_pipe(pipe)
21737e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
21747e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
21757e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
21767e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
21777e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
21787e231dbeSJesse Barnes }
21797e231dbeSJesse Barnes 
2180f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2181036a4a7dSZhenyu Wang {
2182036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21834697995bSJesse Barnes 
21844697995bSJesse Barnes 	if (!dev_priv)
21854697995bSJesse Barnes 		return;
21864697995bSJesse Barnes 
2187036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2188036a4a7dSZhenyu Wang 
2189036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2190036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2191036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2192036a4a7dSZhenyu Wang 
2193036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2194036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2195036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2196192aac1fSKeith Packard 
2197192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2198192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2199192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2200036a4a7dSZhenyu Wang }
2201036a4a7dSZhenyu Wang 
2202c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2203c2798b19SChris Wilson {
2204c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2205c2798b19SChris Wilson 	int pipe;
2206c2798b19SChris Wilson 
2207c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2208c2798b19SChris Wilson 
2209c2798b19SChris Wilson 	for_each_pipe(pipe)
2210c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2211c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2212c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2213c2798b19SChris Wilson 	POSTING_READ16(IER);
2214c2798b19SChris Wilson }
2215c2798b19SChris Wilson 
2216c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2217c2798b19SChris Wilson {
2218c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2219c2798b19SChris Wilson 
2220c2798b19SChris Wilson 	dev_priv->pipestat[0] = 0;
2221c2798b19SChris Wilson 	dev_priv->pipestat[1] = 0;
2222c2798b19SChris Wilson 
2223c2798b19SChris Wilson 	I915_WRITE16(EMR,
2224c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2225c2798b19SChris Wilson 
2226c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2227c2798b19SChris Wilson 	dev_priv->irq_mask =
2228c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2229c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2230c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2231c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2232c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2233c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2234c2798b19SChris Wilson 
2235c2798b19SChris Wilson 	I915_WRITE16(IER,
2236c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2237c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2238c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2239c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2240c2798b19SChris Wilson 	POSTING_READ16(IER);
2241c2798b19SChris Wilson 
2242c2798b19SChris Wilson 	return 0;
2243c2798b19SChris Wilson }
2244c2798b19SChris Wilson 
2245ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2246c2798b19SChris Wilson {
2247c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2248c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2249c2798b19SChris Wilson 	u16 iir, new_iir;
2250c2798b19SChris Wilson 	u32 pipe_stats[2];
2251c2798b19SChris Wilson 	unsigned long irqflags;
2252c2798b19SChris Wilson 	int irq_received;
2253c2798b19SChris Wilson 	int pipe;
2254c2798b19SChris Wilson 	u16 flip_mask =
2255c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2256c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2257c2798b19SChris Wilson 
2258c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2259c2798b19SChris Wilson 
2260c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2261c2798b19SChris Wilson 	if (iir == 0)
2262c2798b19SChris Wilson 		return IRQ_NONE;
2263c2798b19SChris Wilson 
2264c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2265c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2266c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2267c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2268c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2269c2798b19SChris Wilson 		 */
2270c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2271c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2272c2798b19SChris Wilson 			i915_handle_error(dev, false);
2273c2798b19SChris Wilson 
2274c2798b19SChris Wilson 		for_each_pipe(pipe) {
2275c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2276c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2277c2798b19SChris Wilson 
2278c2798b19SChris Wilson 			/*
2279c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2280c2798b19SChris Wilson 			 */
2281c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2282c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2283c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2284c2798b19SChris Wilson 							 pipe_name(pipe));
2285c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2286c2798b19SChris Wilson 				irq_received = 1;
2287c2798b19SChris Wilson 			}
2288c2798b19SChris Wilson 		}
2289c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2290c2798b19SChris Wilson 
2291c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2292c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2293c2798b19SChris Wilson 
2294d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2295c2798b19SChris Wilson 
2296c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2297c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2298c2798b19SChris Wilson 
2299c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
2300c2798b19SChris Wilson 		    drm_handle_vblank(dev, 0)) {
2301c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
2302c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 0);
2303c2798b19SChris Wilson 				intel_finish_page_flip(dev, 0);
2304c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT;
2305c2798b19SChris Wilson 			}
2306c2798b19SChris Wilson 		}
2307c2798b19SChris Wilson 
2308c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
2309c2798b19SChris Wilson 		    drm_handle_vblank(dev, 1)) {
2310c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
2311c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 1);
2312c2798b19SChris Wilson 				intel_finish_page_flip(dev, 1);
2313c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2314c2798b19SChris Wilson 			}
2315c2798b19SChris Wilson 		}
2316c2798b19SChris Wilson 
2317c2798b19SChris Wilson 		iir = new_iir;
2318c2798b19SChris Wilson 	}
2319c2798b19SChris Wilson 
2320c2798b19SChris Wilson 	return IRQ_HANDLED;
2321c2798b19SChris Wilson }
2322c2798b19SChris Wilson 
2323c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2324c2798b19SChris Wilson {
2325c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2326c2798b19SChris Wilson 	int pipe;
2327c2798b19SChris Wilson 
2328c2798b19SChris Wilson 	for_each_pipe(pipe) {
2329c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2330c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2331c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2332c2798b19SChris Wilson 	}
2333c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2334c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2335c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2336c2798b19SChris Wilson }
2337c2798b19SChris Wilson 
2338a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2339a266c7d5SChris Wilson {
2340a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2341a266c7d5SChris Wilson 	int pipe;
2342a266c7d5SChris Wilson 
2343a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2344a266c7d5SChris Wilson 
2345a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2346a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2347a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2348a266c7d5SChris Wilson 	}
2349a266c7d5SChris Wilson 
235000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2351a266c7d5SChris Wilson 	for_each_pipe(pipe)
2352a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2353a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2354a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2355a266c7d5SChris Wilson 	POSTING_READ(IER);
2356a266c7d5SChris Wilson }
2357a266c7d5SChris Wilson 
2358a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2359a266c7d5SChris Wilson {
2360a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
236138bde180SChris Wilson 	u32 enable_mask;
2362a266c7d5SChris Wilson 
2363a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2364a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2365a266c7d5SChris Wilson 
236638bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
236738bde180SChris Wilson 
236838bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
236938bde180SChris Wilson 	dev_priv->irq_mask =
237038bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
237138bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
237238bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
237338bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
237438bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
237538bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
237638bde180SChris Wilson 
237738bde180SChris Wilson 	enable_mask =
237838bde180SChris Wilson 		I915_ASLE_INTERRUPT |
237938bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
238038bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
238138bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
238238bde180SChris Wilson 		I915_USER_INTERRUPT;
238338bde180SChris Wilson 
2384a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
238520afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
238620afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
238720afbda2SDaniel Vetter 
2388a266c7d5SChris Wilson 		/* Enable in IER... */
2389a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2390a266c7d5SChris Wilson 		/* and unmask in IMR */
2391a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2392a266c7d5SChris Wilson 	}
2393a266c7d5SChris Wilson 
2394a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2395a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2396a266c7d5SChris Wilson 	POSTING_READ(IER);
2397a266c7d5SChris Wilson 
239820afbda2SDaniel Vetter 	intel_opregion_enable_asle(dev);
239920afbda2SDaniel Vetter 
240020afbda2SDaniel Vetter 	return 0;
240120afbda2SDaniel Vetter }
240220afbda2SDaniel Vetter 
240320afbda2SDaniel Vetter static void i915_hpd_irq_setup(struct drm_device *dev)
240420afbda2SDaniel Vetter {
240520afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
240620afbda2SDaniel Vetter 	u32 hotplug_en;
240720afbda2SDaniel Vetter 
2408a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
240920afbda2SDaniel Vetter 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2410a266c7d5SChris Wilson 
2411*26739f12SDaniel Vetter 		if (dev_priv->hotplug_supported_mask & PORTB_HOTPLUG_INT_STATUS)
2412*26739f12SDaniel Vetter 			hotplug_en |= PORTB_HOTPLUG_INT_EN;
2413*26739f12SDaniel Vetter 		if (dev_priv->hotplug_supported_mask & PORTC_HOTPLUG_INT_STATUS)
2414*26739f12SDaniel Vetter 			hotplug_en |= PORTC_HOTPLUG_INT_EN;
2415*26739f12SDaniel Vetter 		if (dev_priv->hotplug_supported_mask & PORTD_HOTPLUG_INT_STATUS)
2416*26739f12SDaniel Vetter 			hotplug_en |= PORTD_HOTPLUG_INT_EN;
2417084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
2418a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2419084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
2420a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2421a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2422a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_INT_EN;
2423a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2424a266c7d5SChris Wilson 		}
2425a266c7d5SChris Wilson 
2426a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2427a266c7d5SChris Wilson 
2428a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2429a266c7d5SChris Wilson 	}
2430a266c7d5SChris Wilson }
2431a266c7d5SChris Wilson 
2432ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2433a266c7d5SChris Wilson {
2434a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2435a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24368291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2437a266c7d5SChris Wilson 	unsigned long irqflags;
243838bde180SChris Wilson 	u32 flip_mask =
243938bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
244038bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
244138bde180SChris Wilson 	u32 flip[2] = {
244238bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT,
244338bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT
244438bde180SChris Wilson 	};
244538bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2446a266c7d5SChris Wilson 
2447a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2448a266c7d5SChris Wilson 
2449a266c7d5SChris Wilson 	iir = I915_READ(IIR);
245038bde180SChris Wilson 	do {
245138bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
24528291ee90SChris Wilson 		bool blc_event = false;
2453a266c7d5SChris Wilson 
2454a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2455a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2456a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2457a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2458a266c7d5SChris Wilson 		 */
2459a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2460a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2461a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2462a266c7d5SChris Wilson 
2463a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2464a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2465a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2466a266c7d5SChris Wilson 
246738bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2468a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2469a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2470a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2471a266c7d5SChris Wilson 							 pipe_name(pipe));
2472a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
247338bde180SChris Wilson 				irq_received = true;
2474a266c7d5SChris Wilson 			}
2475a266c7d5SChris Wilson 		}
2476a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2477a266c7d5SChris Wilson 
2478a266c7d5SChris Wilson 		if (!irq_received)
2479a266c7d5SChris Wilson 			break;
2480a266c7d5SChris Wilson 
2481a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2482a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2483a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2484a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2485a266c7d5SChris Wilson 
2486a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2487a266c7d5SChris Wilson 				  hotplug_status);
2488a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2489a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2490a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2491a266c7d5SChris Wilson 
2492a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
249338bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2494a266c7d5SChris Wilson 		}
2495a266c7d5SChris Wilson 
249638bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2497a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2498a266c7d5SChris Wilson 
2499a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2500a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2501a266c7d5SChris Wilson 
2502a266c7d5SChris Wilson 		for_each_pipe(pipe) {
250338bde180SChris Wilson 			int plane = pipe;
250438bde180SChris Wilson 			if (IS_MOBILE(dev))
250538bde180SChris Wilson 				plane = !plane;
25068291ee90SChris Wilson 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
2507a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
250838bde180SChris Wilson 				if (iir & flip[plane]) {
250938bde180SChris Wilson 					intel_prepare_page_flip(dev, plane);
2510a266c7d5SChris Wilson 					intel_finish_page_flip(dev, pipe);
251138bde180SChris Wilson 					flip_mask &= ~flip[plane];
251238bde180SChris Wilson 				}
2513a266c7d5SChris Wilson 			}
2514a266c7d5SChris Wilson 
2515a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2516a266c7d5SChris Wilson 				blc_event = true;
2517a266c7d5SChris Wilson 		}
2518a266c7d5SChris Wilson 
2519a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2520a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2521a266c7d5SChris Wilson 
2522a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2523a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2524a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2525a266c7d5SChris Wilson 		 * we would never get another interrupt.
2526a266c7d5SChris Wilson 		 *
2527a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2528a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2529a266c7d5SChris Wilson 		 * another one.
2530a266c7d5SChris Wilson 		 *
2531a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2532a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2533a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2534a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2535a266c7d5SChris Wilson 		 * stray interrupts.
2536a266c7d5SChris Wilson 		 */
253738bde180SChris Wilson 		ret = IRQ_HANDLED;
2538a266c7d5SChris Wilson 		iir = new_iir;
253938bde180SChris Wilson 	} while (iir & ~flip_mask);
2540a266c7d5SChris Wilson 
2541d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
25428291ee90SChris Wilson 
2543a266c7d5SChris Wilson 	return ret;
2544a266c7d5SChris Wilson }
2545a266c7d5SChris Wilson 
2546a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2547a266c7d5SChris Wilson {
2548a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2549a266c7d5SChris Wilson 	int pipe;
2550a266c7d5SChris Wilson 
2551a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2552a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2553a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2554a266c7d5SChris Wilson 	}
2555a266c7d5SChris Wilson 
255600d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
255755b39755SChris Wilson 	for_each_pipe(pipe) {
255855b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2559a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
256055b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
256155b39755SChris Wilson 	}
2562a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2563a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2564a266c7d5SChris Wilson 
2565a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2566a266c7d5SChris Wilson }
2567a266c7d5SChris Wilson 
2568a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2569a266c7d5SChris Wilson {
2570a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2571a266c7d5SChris Wilson 	int pipe;
2572a266c7d5SChris Wilson 
2573a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2574a266c7d5SChris Wilson 
2575a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2576a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2577a266c7d5SChris Wilson 
2578a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2579a266c7d5SChris Wilson 	for_each_pipe(pipe)
2580a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2581a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2582a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2583a266c7d5SChris Wilson 	POSTING_READ(IER);
2584a266c7d5SChris Wilson }
2585a266c7d5SChris Wilson 
2586a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2587a266c7d5SChris Wilson {
2588a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2589bbba0a97SChris Wilson 	u32 enable_mask;
2590a266c7d5SChris Wilson 	u32 error_mask;
2591a266c7d5SChris Wilson 
2592a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2593bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2594adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2595bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2596bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2597bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2598bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2599bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2600bbba0a97SChris Wilson 
2601bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
2602bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2603bbba0a97SChris Wilson 
2604bbba0a97SChris Wilson 	if (IS_G4X(dev))
2605bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2606a266c7d5SChris Wilson 
2607a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2608a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2609515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
2610a266c7d5SChris Wilson 
2611a266c7d5SChris Wilson 	/*
2612a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2613a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2614a266c7d5SChris Wilson 	 */
2615a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2616a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2617a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2618a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2619a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2620a266c7d5SChris Wilson 	} else {
2621a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2622a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2623a266c7d5SChris Wilson 	}
2624a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2625a266c7d5SChris Wilson 
2626a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2627a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2628a266c7d5SChris Wilson 	POSTING_READ(IER);
2629a266c7d5SChris Wilson 
263020afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
263120afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
263220afbda2SDaniel Vetter 
263320afbda2SDaniel Vetter 	intel_opregion_enable_asle(dev);
263420afbda2SDaniel Vetter 
263520afbda2SDaniel Vetter 	return 0;
263620afbda2SDaniel Vetter }
263720afbda2SDaniel Vetter 
263820afbda2SDaniel Vetter static void i965_hpd_irq_setup(struct drm_device *dev)
263920afbda2SDaniel Vetter {
264020afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
264120afbda2SDaniel Vetter 	u32 hotplug_en;
264220afbda2SDaniel Vetter 
2643adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
2644adca4730SChris Wilson 	hotplug_en = 0;
2645*26739f12SDaniel Vetter 	if (dev_priv->hotplug_supported_mask & PORTB_HOTPLUG_INT_STATUS)
2646*26739f12SDaniel Vetter 		hotplug_en |= PORTB_HOTPLUG_INT_EN;
2647*26739f12SDaniel Vetter 	if (dev_priv->hotplug_supported_mask & PORTC_HOTPLUG_INT_STATUS)
2648*26739f12SDaniel Vetter 		hotplug_en |= PORTC_HOTPLUG_INT_EN;
2649*26739f12SDaniel Vetter 	if (dev_priv->hotplug_supported_mask & PORTD_HOTPLUG_INT_STATUS)
2650*26739f12SDaniel Vetter 		hotplug_en |= PORTD_HOTPLUG_INT_EN;
2651084b612eSChris Wilson 	if (IS_G4X(dev)) {
2652084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_G4X)
2653a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2654084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_G4X)
2655a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2656084b612eSChris Wilson 	} else {
2657084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I965)
2658084b612eSChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2659084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I965)
2660084b612eSChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2661084b612eSChris Wilson 	}
2662a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2663a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_INT_EN;
2664a266c7d5SChris Wilson 
2665a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2666a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2667a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2668a266c7d5SChris Wilson 		   */
2669a266c7d5SChris Wilson 		if (IS_G4X(dev))
2670a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2671a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2672a266c7d5SChris Wilson 	}
2673a266c7d5SChris Wilson 
2674a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
2675a266c7d5SChris Wilson 
2676a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2677a266c7d5SChris Wilson }
2678a266c7d5SChris Wilson 
2679ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
2680a266c7d5SChris Wilson {
2681a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2682a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2683a266c7d5SChris Wilson 	u32 iir, new_iir;
2684a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2685a266c7d5SChris Wilson 	unsigned long irqflags;
2686a266c7d5SChris Wilson 	int irq_received;
2687a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
2688a266c7d5SChris Wilson 
2689a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2690a266c7d5SChris Wilson 
2691a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2692a266c7d5SChris Wilson 
2693a266c7d5SChris Wilson 	for (;;) {
26942c8ba29fSChris Wilson 		bool blc_event = false;
26952c8ba29fSChris Wilson 
2696a266c7d5SChris Wilson 		irq_received = iir != 0;
2697a266c7d5SChris Wilson 
2698a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2699a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2700a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2701a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2702a266c7d5SChris Wilson 		 */
2703a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2704a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2705a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2706a266c7d5SChris Wilson 
2707a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2708a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2709a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2710a266c7d5SChris Wilson 
2711a266c7d5SChris Wilson 			/*
2712a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2713a266c7d5SChris Wilson 			 */
2714a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2715a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2716a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2717a266c7d5SChris Wilson 							 pipe_name(pipe));
2718a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2719a266c7d5SChris Wilson 				irq_received = 1;
2720a266c7d5SChris Wilson 			}
2721a266c7d5SChris Wilson 		}
2722a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2723a266c7d5SChris Wilson 
2724a266c7d5SChris Wilson 		if (!irq_received)
2725a266c7d5SChris Wilson 			break;
2726a266c7d5SChris Wilson 
2727a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2728a266c7d5SChris Wilson 
2729a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2730adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2731a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2732a266c7d5SChris Wilson 
2733a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2734a266c7d5SChris Wilson 				  hotplug_status);
2735a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2736a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2737a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2738a266c7d5SChris Wilson 
2739a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2740a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2741a266c7d5SChris Wilson 		}
2742a266c7d5SChris Wilson 
2743a266c7d5SChris Wilson 		I915_WRITE(IIR, iir);
2744a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2745a266c7d5SChris Wilson 
2746a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2747a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2748a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2749a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2750a266c7d5SChris Wilson 
27514f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
2752a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 0);
2753a266c7d5SChris Wilson 
27544f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
2755a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 1);
2756a266c7d5SChris Wilson 
2757a266c7d5SChris Wilson 		for_each_pipe(pipe) {
27582c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
2759a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
2760a266c7d5SChris Wilson 				i915_pageflip_stall_check(dev, pipe);
2761a266c7d5SChris Wilson 				intel_finish_page_flip(dev, pipe);
2762a266c7d5SChris Wilson 			}
2763a266c7d5SChris Wilson 
2764a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2765a266c7d5SChris Wilson 				blc_event = true;
2766a266c7d5SChris Wilson 		}
2767a266c7d5SChris Wilson 
2768a266c7d5SChris Wilson 
2769a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2770a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2771a266c7d5SChris Wilson 
2772515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
2773515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
2774515ac2bbSDaniel Vetter 
2775a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2776a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2777a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2778a266c7d5SChris Wilson 		 * we would never get another interrupt.
2779a266c7d5SChris Wilson 		 *
2780a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2781a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2782a266c7d5SChris Wilson 		 * another one.
2783a266c7d5SChris Wilson 		 *
2784a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2785a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2786a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2787a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2788a266c7d5SChris Wilson 		 * stray interrupts.
2789a266c7d5SChris Wilson 		 */
2790a266c7d5SChris Wilson 		iir = new_iir;
2791a266c7d5SChris Wilson 	}
2792a266c7d5SChris Wilson 
2793d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
27942c8ba29fSChris Wilson 
2795a266c7d5SChris Wilson 	return ret;
2796a266c7d5SChris Wilson }
2797a266c7d5SChris Wilson 
2798a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
2799a266c7d5SChris Wilson {
2800a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2801a266c7d5SChris Wilson 	int pipe;
2802a266c7d5SChris Wilson 
2803a266c7d5SChris Wilson 	if (!dev_priv)
2804a266c7d5SChris Wilson 		return;
2805a266c7d5SChris Wilson 
2806a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2807a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2808a266c7d5SChris Wilson 
2809a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
2810a266c7d5SChris Wilson 	for_each_pipe(pipe)
2811a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2812a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2813a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2814a266c7d5SChris Wilson 
2815a266c7d5SChris Wilson 	for_each_pipe(pipe)
2816a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
2817a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2818a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2819a266c7d5SChris Wilson }
2820a266c7d5SChris Wilson 
2821f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2822f71d4af4SJesse Barnes {
28238b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28248b2e326dSChris Wilson 
28258b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
282699584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
2827c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
2828a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
28298b2e326dSChris Wilson 
283099584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
283199584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
283261bac78eSDaniel Vetter 		    (unsigned long) dev);
283361bac78eSDaniel Vetter 
283497a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
28359ee32feaSDaniel Vetter 
2836f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2837f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
28387d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
2839f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2840f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2841f71d4af4SJesse Barnes 	}
2842f71d4af4SJesse Barnes 
2843c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2844f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2845c3613de9SKeith Packard 	else
2846c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2847f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2848f71d4af4SJesse Barnes 
28497e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
28507e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
28517e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
28527e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
28537e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
28547e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
28557e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
285620afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup = valleyview_hpd_irq_setup;
28574a06e201SDaniel Vetter 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2858f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2859f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2860f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2861f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2862f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2863f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2864f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
2865f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2866f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2867f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2868f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2869f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2870f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2871f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
2872f71d4af4SJesse Barnes 	} else {
2873c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
2874c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
2875c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
2876c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
2877c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
2878a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
2879a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
2880a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
2881a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
2882a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
288320afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
2884c2798b19SChris Wilson 		} else {
2885a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
2886a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
2887a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
2888a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
288920afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i965_hpd_irq_setup;
2890c2798b19SChris Wilson 		}
2891f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
2892f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
2893f71d4af4SJesse Barnes 	}
2894f71d4af4SJesse Barnes }
289520afbda2SDaniel Vetter 
289620afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
289720afbda2SDaniel Vetter {
289820afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
289920afbda2SDaniel Vetter 
290020afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
290120afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
290220afbda2SDaniel Vetter }
2903