xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 46c06a30dfd63b1200dda2337c145e262798b9cf)
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 {
639db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
64*46c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
657c463586SKeith Packard 
66*46c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
67*46c06a30SVille Syrjälä 		return;
68*46c06a30SVille Syrjälä 
697c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
70*46c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
71*46c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
723143a2bfSChris Wilson 	POSTING_READ(reg);
737c463586SKeith Packard }
747c463586SKeith Packard 
757c463586SKeith Packard void
767c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
777c463586SKeith Packard {
789db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
79*46c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
807c463586SKeith Packard 
81*46c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
82*46c06a30SVille Syrjälä 		return;
83*46c06a30SVille Syrjälä 
84*46c06a30SVille Syrjälä 	pipestat &= ~mask;
85*46c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
863143a2bfSChris Wilson 	POSTING_READ(reg);
877c463586SKeith Packard }
887c463586SKeith Packard 
89c0e09200SDave Airlie /**
9001c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
9101c66889SZhao Yakui  */
9201c66889SZhao Yakui void intel_enable_asle(struct drm_device *dev)
9301c66889SZhao Yakui {
941ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
951ec14ad3SChris Wilson 	unsigned long irqflags;
961ec14ad3SChris Wilson 
977e231dbeSJesse Barnes 	/* FIXME: opregion/asle for VLV */
987e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev))
997e231dbeSJesse Barnes 		return;
1007e231dbeSJesse Barnes 
1011ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
10201c66889SZhao Yakui 
103c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
104f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
105edcb49caSZhao Yakui 	else {
10601c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
107d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
108a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen >= 4)
109edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
110d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
111edcb49caSZhao Yakui 	}
1121ec14ad3SChris Wilson 
1131ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
11401c66889SZhao Yakui }
11501c66889SZhao Yakui 
11601c66889SZhao Yakui /**
1170a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1180a3e67a4SJesse Barnes  * @dev: DRM device
1190a3e67a4SJesse Barnes  * @pipe: pipe to check
1200a3e67a4SJesse Barnes  *
1210a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1220a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1230a3e67a4SJesse Barnes  * before reading such registers if unsure.
1240a3e67a4SJesse Barnes  */
1250a3e67a4SJesse Barnes static int
1260a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1270a3e67a4SJesse Barnes {
1280a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
129702e7a56SPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
130702e7a56SPaulo Zanoni 								      pipe);
131702e7a56SPaulo Zanoni 
132702e7a56SPaulo Zanoni 	return I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_ENABLE;
1330a3e67a4SJesse Barnes }
1340a3e67a4SJesse Barnes 
13542f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
13642f52ef8SKeith Packard  * we use as a pipe index
13742f52ef8SKeith Packard  */
138f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1390a3e67a4SJesse Barnes {
1400a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1410a3e67a4SJesse Barnes 	unsigned long high_frame;
1420a3e67a4SJesse Barnes 	unsigned long low_frame;
1435eddb70bSChris Wilson 	u32 high1, high2, low;
1440a3e67a4SJesse Barnes 
1450a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
14644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
1479db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
1480a3e67a4SJesse Barnes 		return 0;
1490a3e67a4SJesse Barnes 	}
1500a3e67a4SJesse Barnes 
1519db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
1529db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
1535eddb70bSChris Wilson 
1540a3e67a4SJesse Barnes 	/*
1550a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
1560a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
1570a3e67a4SJesse Barnes 	 * register.
1580a3e67a4SJesse Barnes 	 */
1590a3e67a4SJesse Barnes 	do {
1605eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1615eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
1625eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1630a3e67a4SJesse Barnes 	} while (high1 != high2);
1640a3e67a4SJesse Barnes 
1655eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
1665eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
1675eddb70bSChris Wilson 	return (high1 << 8) | low;
1680a3e67a4SJesse Barnes }
1690a3e67a4SJesse Barnes 
170f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
1719880b7a5SJesse Barnes {
1729880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1739db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
1749880b7a5SJesse Barnes 
1759880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
17644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
1779db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
1789880b7a5SJesse Barnes 		return 0;
1799880b7a5SJesse Barnes 	}
1809880b7a5SJesse Barnes 
1819880b7a5SJesse Barnes 	return I915_READ(reg);
1829880b7a5SJesse Barnes }
1839880b7a5SJesse Barnes 
184f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
1850af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
1860af7e4dfSMario Kleiner {
1870af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1880af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
1890af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
1900af7e4dfSMario Kleiner 	bool in_vbl = true;
1910af7e4dfSMario Kleiner 	int ret = 0;
192fe2b8f9dSPaulo Zanoni 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
193fe2b8f9dSPaulo Zanoni 								      pipe);
1940af7e4dfSMario Kleiner 
1950af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
1960af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
1979db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
1980af7e4dfSMario Kleiner 		return 0;
1990af7e4dfSMario Kleiner 	}
2000af7e4dfSMario Kleiner 
2010af7e4dfSMario Kleiner 	/* Get vtotal. */
202fe2b8f9dSPaulo Zanoni 	vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
2030af7e4dfSMario Kleiner 
2040af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
2050af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
2060af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
2070af7e4dfSMario Kleiner 		 */
2080af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
2090af7e4dfSMario Kleiner 
2100af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
2110af7e4dfSMario Kleiner 		 * horizontal scanout position.
2120af7e4dfSMario Kleiner 		 */
2130af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
2140af7e4dfSMario Kleiner 		*hpos = 0;
2150af7e4dfSMario Kleiner 	} else {
2160af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
2170af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
2180af7e4dfSMario Kleiner 		 * scanout position.
2190af7e4dfSMario Kleiner 		 */
2200af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
2210af7e4dfSMario Kleiner 
222fe2b8f9dSPaulo Zanoni 		htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
2230af7e4dfSMario Kleiner 		*vpos = position / htotal;
2240af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
2250af7e4dfSMario Kleiner 	}
2260af7e4dfSMario Kleiner 
2270af7e4dfSMario Kleiner 	/* Query vblank area. */
228fe2b8f9dSPaulo Zanoni 	vbl = I915_READ(VBLANK(cpu_transcoder));
2290af7e4dfSMario Kleiner 
2300af7e4dfSMario Kleiner 	/* Test position against vblank region. */
2310af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
2320af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
2330af7e4dfSMario Kleiner 
2340af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
2350af7e4dfSMario Kleiner 		in_vbl = false;
2360af7e4dfSMario Kleiner 
2370af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
2380af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
2390af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
2400af7e4dfSMario Kleiner 
2410af7e4dfSMario Kleiner 	/* Readouts valid? */
2420af7e4dfSMario Kleiner 	if (vbl > 0)
2430af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
2440af7e4dfSMario Kleiner 
2450af7e4dfSMario Kleiner 	/* In vblank? */
2460af7e4dfSMario Kleiner 	if (in_vbl)
2470af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
2480af7e4dfSMario Kleiner 
2490af7e4dfSMario Kleiner 	return ret;
2500af7e4dfSMario Kleiner }
2510af7e4dfSMario Kleiner 
252f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
2530af7e4dfSMario Kleiner 			      int *max_error,
2540af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
2550af7e4dfSMario Kleiner 			      unsigned flags)
2560af7e4dfSMario Kleiner {
2574041b853SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2584041b853SChris Wilson 	struct drm_crtc *crtc;
2590af7e4dfSMario Kleiner 
2604041b853SChris Wilson 	if (pipe < 0 || pipe >= dev_priv->num_pipe) {
2614041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2620af7e4dfSMario Kleiner 		return -EINVAL;
2630af7e4dfSMario Kleiner 	}
2640af7e4dfSMario Kleiner 
2650af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
2664041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
2674041b853SChris Wilson 	if (crtc == NULL) {
2684041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2694041b853SChris Wilson 		return -EINVAL;
2704041b853SChris Wilson 	}
2714041b853SChris Wilson 
2724041b853SChris Wilson 	if (!crtc->enabled) {
2734041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
2744041b853SChris Wilson 		return -EBUSY;
2754041b853SChris Wilson 	}
2760af7e4dfSMario Kleiner 
2770af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
2784041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
2794041b853SChris Wilson 						     vblank_time, flags,
2804041b853SChris Wilson 						     crtc);
2810af7e4dfSMario Kleiner }
2820af7e4dfSMario Kleiner 
2835ca58282SJesse Barnes /*
2845ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
2855ca58282SJesse Barnes  */
2865ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
2875ca58282SJesse Barnes {
2885ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
2895ca58282SJesse Barnes 						    hotplug_work);
2905ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
291c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
2924ef69c7aSChris Wilson 	struct intel_encoder *encoder;
2935ca58282SJesse Barnes 
29452d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
29552d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
29652d7ecedSDaniel Vetter 		return;
29752d7ecedSDaniel Vetter 
298a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
299e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
300e67189abSJesse Barnes 
3014ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
3024ef69c7aSChris Wilson 		if (encoder->hot_plug)
3034ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
304c31c4ba3SKeith Packard 
30540ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
30640ee3381SKeith Packard 
3075ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
308eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3095ca58282SJesse Barnes }
3105ca58282SJesse Barnes 
31173edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
312f97108d1SJesse Barnes {
313f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
314b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
3159270388eSDaniel Vetter 	u8 new_delay;
3169270388eSDaniel Vetter 	unsigned long flags;
3179270388eSDaniel Vetter 
3189270388eSDaniel Vetter 	spin_lock_irqsave(&mchdev_lock, flags);
319f97108d1SJesse Barnes 
32073edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
32173edd18fSDaniel Vetter 
32220e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
3239270388eSDaniel Vetter 
3247648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
325b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
326b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
327f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
328f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
329f97108d1SJesse Barnes 
330f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
331b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
33220e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
33320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
33420e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
33520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
336b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
33720e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
33820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
33920e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
34020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
341f97108d1SJesse Barnes 	}
342f97108d1SJesse Barnes 
3437648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
34420e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
345f97108d1SJesse Barnes 
3469270388eSDaniel Vetter 	spin_unlock_irqrestore(&mchdev_lock, flags);
3479270388eSDaniel Vetter 
348f97108d1SJesse Barnes 	return;
349f97108d1SJesse Barnes }
350f97108d1SJesse Barnes 
351549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
352549f7365SChris Wilson 			struct intel_ring_buffer *ring)
353549f7365SChris Wilson {
354549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
3559862e600SChris Wilson 
356475553deSChris Wilson 	if (ring->obj == NULL)
357475553deSChris Wilson 		return;
358475553deSChris Wilson 
359b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
3609862e600SChris Wilson 
361549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3623e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
36399584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
36499584db3SDaniel Vetter 		mod_timer(&dev_priv->gpu_error.hangcheck_timer,
365cecc21feSChris Wilson 			  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
3663e0dc6b0SBen Widawsky 	}
367549f7365SChris Wilson }
368549f7365SChris Wilson 
3694912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
3703b8d8d91SJesse Barnes {
3714912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
372c6a828d3SDaniel Vetter 						    rps.work);
3734912d041SBen Widawsky 	u32 pm_iir, pm_imr;
3747b9e0ae6SChris Wilson 	u8 new_delay;
3753b8d8d91SJesse Barnes 
376c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
377c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
378c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
3794912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
380a9e2641dSDaniel Vetter 	I915_WRITE(GEN6_PMIMR, 0);
381c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
3824912d041SBen Widawsky 
3837b9e0ae6SChris Wilson 	if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
3843b8d8d91SJesse Barnes 		return;
3853b8d8d91SJesse Barnes 
3864fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
3877b9e0ae6SChris Wilson 
3887b9e0ae6SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
389c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
3907b9e0ae6SChris Wilson 	else
391c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
3923b8d8d91SJesse Barnes 
39379249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
39479249636SBen Widawsky 	 * interrupt
39579249636SBen Widawsky 	 */
39679249636SBen Widawsky 	if (!(new_delay > dev_priv->rps.max_delay ||
39779249636SBen Widawsky 	      new_delay < dev_priv->rps.min_delay)) {
3984912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
39979249636SBen Widawsky 	}
4003b8d8d91SJesse Barnes 
4014fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
4023b8d8d91SJesse Barnes }
4033b8d8d91SJesse Barnes 
404e3689190SBen Widawsky 
405e3689190SBen Widawsky /**
406e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
407e3689190SBen Widawsky  * occurred.
408e3689190SBen Widawsky  * @work: workqueue struct
409e3689190SBen Widawsky  *
410e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
411e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
412e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
413e3689190SBen Widawsky  */
414e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
415e3689190SBen Widawsky {
416e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
417a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
418e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
419e3689190SBen Widawsky 	char *parity_event[5];
420e3689190SBen Widawsky 	uint32_t misccpctl;
421e3689190SBen Widawsky 	unsigned long flags;
422e3689190SBen Widawsky 
423e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
424e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
425e3689190SBen Widawsky 	 * any time we access those registers.
426e3689190SBen Widawsky 	 */
427e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
428e3689190SBen Widawsky 
429e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
430e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
431e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
432e3689190SBen Widawsky 
433e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
434e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
435e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
436e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
437e3689190SBen Widawsky 
438e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
439e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
440e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
441e3689190SBen Widawsky 
442e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
443e3689190SBen Widawsky 
444e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
445e3689190SBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
446e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
447e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
448e3689190SBen Widawsky 
449e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
450e3689190SBen Widawsky 
451e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
452e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
453e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
454e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
455e3689190SBen Widawsky 	parity_event[4] = NULL;
456e3689190SBen Widawsky 
457e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
458e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
459e3689190SBen Widawsky 
460e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
461e3689190SBen Widawsky 		  row, bank, subbank);
462e3689190SBen Widawsky 
463e3689190SBen Widawsky 	kfree(parity_event[3]);
464e3689190SBen Widawsky 	kfree(parity_event[2]);
465e3689190SBen Widawsky 	kfree(parity_event[1]);
466e3689190SBen Widawsky }
467e3689190SBen Widawsky 
468d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
469e3689190SBen Widawsky {
470e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
471e3689190SBen Widawsky 	unsigned long flags;
472e3689190SBen Widawsky 
473e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
474e3689190SBen Widawsky 		return;
475e3689190SBen Widawsky 
476e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
477e3689190SBen Widawsky 	dev_priv->gt_irq_mask |= GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
478e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
479e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
480e3689190SBen Widawsky 
481a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
482e3689190SBen Widawsky }
483e3689190SBen Widawsky 
484e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
485e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
486e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
487e7b4c6b1SDaniel Vetter {
488e7b4c6b1SDaniel Vetter 
489e7b4c6b1SDaniel Vetter 	if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
490e7b4c6b1SDaniel Vetter 		      GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
491e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
492e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BSD_USER_INTERRUPT)
493e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
494e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
495e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
496e7b4c6b1SDaniel Vetter 
497e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
498e7b4c6b1SDaniel Vetter 		      GT_GEN6_BSD_CS_ERROR_INTERRUPT |
499e7b4c6b1SDaniel Vetter 		      GT_RENDER_CS_ERROR_INTERRUPT)) {
500e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
501e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
502e7b4c6b1SDaniel Vetter 	}
503e3689190SBen Widawsky 
504e3689190SBen Widawsky 	if (gt_iir & GT_GEN7_L3_PARITY_ERROR_INTERRUPT)
505e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
506e7b4c6b1SDaniel Vetter }
507e7b4c6b1SDaniel Vetter 
508fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
509fc6826d1SChris Wilson 				u32 pm_iir)
510fc6826d1SChris Wilson {
511fc6826d1SChris Wilson 	unsigned long flags;
512fc6826d1SChris Wilson 
513fc6826d1SChris Wilson 	/*
514fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
515fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
516fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
517c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
518fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
519fc6826d1SChris Wilson 	 *
520c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
521fc6826d1SChris Wilson 	 */
522fc6826d1SChris Wilson 
523c6a828d3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
524c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
525c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
526fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
527c6a828d3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
528fc6826d1SChris Wilson 
529c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
530fc6826d1SChris Wilson }
531fc6826d1SChris Wilson 
532515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
533515ac2bbSDaniel Vetter {
53428c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
53528c70f16SDaniel Vetter 
53628c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
537515ac2bbSDaniel Vetter }
538515ac2bbSDaniel Vetter 
539ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
540ce99c256SDaniel Vetter {
5419ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
5429ee32feaSDaniel Vetter 
5439ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
544ce99c256SDaniel Vetter }
545ce99c256SDaniel Vetter 
546ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
5477e231dbeSJesse Barnes {
5487e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
5497e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5507e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
5517e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
5527e231dbeSJesse Barnes 	unsigned long irqflags;
5537e231dbeSJesse Barnes 	int pipe;
5547e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
5557e231dbeSJesse Barnes 
5567e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5577e231dbeSJesse Barnes 
5587e231dbeSJesse Barnes 	while (true) {
5597e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
5607e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
5617e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
5627e231dbeSJesse Barnes 
5637e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
5647e231dbeSJesse Barnes 			goto out;
5657e231dbeSJesse Barnes 
5667e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
5677e231dbeSJesse Barnes 
568e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
5697e231dbeSJesse Barnes 
5707e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5717e231dbeSJesse Barnes 		for_each_pipe(pipe) {
5727e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
5737e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
5747e231dbeSJesse Barnes 
5757e231dbeSJesse Barnes 			/*
5767e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
5777e231dbeSJesse Barnes 			 */
5787e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
5797e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
5807e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
5817e231dbeSJesse Barnes 							 pipe_name(pipe));
5827e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
5837e231dbeSJesse Barnes 			}
5847e231dbeSJesse Barnes 		}
5857e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5867e231dbeSJesse Barnes 
58731acc7f5SJesse Barnes 		for_each_pipe(pipe) {
58831acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
58931acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
59031acc7f5SJesse Barnes 
59131acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
59231acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
59331acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
59431acc7f5SJesse Barnes 			}
59531acc7f5SJesse Barnes 		}
59631acc7f5SJesse Barnes 
5977e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
5987e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
5997e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
6007e231dbeSJesse Barnes 
6017e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
6027e231dbeSJesse Barnes 					 hotplug_status);
6037e231dbeSJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
6047e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
6057e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
6067e231dbeSJesse Barnes 
6077e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
6087e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
6097e231dbeSJesse Barnes 		}
6107e231dbeSJesse Barnes 
611515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
612515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
6137e231dbeSJesse Barnes 
614fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
615fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
6167e231dbeSJesse Barnes 
6177e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
6187e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
6197e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
6207e231dbeSJesse Barnes 	}
6217e231dbeSJesse Barnes 
6227e231dbeSJesse Barnes out:
6237e231dbeSJesse Barnes 	return ret;
6247e231dbeSJesse Barnes }
6257e231dbeSJesse Barnes 
62623e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
627776ad806SJesse Barnes {
628776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6299db4a9c7SJesse Barnes 	int pipe;
630776ad806SJesse Barnes 
63176e43830SDaniel Vetter 	if (pch_iir & SDE_HOTPLUG_MASK)
63276e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
63376e43830SDaniel Vetter 
634776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
635776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
636776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
637776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
638776ad806SJesse Barnes 
639ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
640ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
641ce99c256SDaniel Vetter 
642776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
643515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
644776ad806SJesse Barnes 
645776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
646776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
647776ad806SJesse Barnes 
648776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
649776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
650776ad806SJesse Barnes 
651776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
652776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
653776ad806SJesse Barnes 
6549db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
6559db4a9c7SJesse Barnes 		for_each_pipe(pipe)
6569db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
6579db4a9c7SJesse Barnes 					 pipe_name(pipe),
6589db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
659776ad806SJesse Barnes 
660776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
661776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
662776ad806SJesse Barnes 
663776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
664776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
665776ad806SJesse Barnes 
666776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
667776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
668776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
669776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
670776ad806SJesse Barnes }
671776ad806SJesse Barnes 
67223e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
67323e81d69SAdam Jackson {
67423e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
67523e81d69SAdam Jackson 	int pipe;
67623e81d69SAdam Jackson 
67776e43830SDaniel Vetter 	if (pch_iir & SDE_HOTPLUG_MASK_CPT)
67876e43830SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
67976e43830SDaniel Vetter 
68023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT)
68123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
68223e81d69SAdam Jackson 				 (pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
68323e81d69SAdam Jackson 				 SDE_AUDIO_POWER_SHIFT_CPT);
68423e81d69SAdam Jackson 
68523e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
686ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
68723e81d69SAdam Jackson 
68823e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
689515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
69023e81d69SAdam Jackson 
69123e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
69223e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
69323e81d69SAdam Jackson 
69423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
69523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
69623e81d69SAdam Jackson 
69723e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
69823e81d69SAdam Jackson 		for_each_pipe(pipe)
69923e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
70023e81d69SAdam Jackson 					 pipe_name(pipe),
70123e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
70223e81d69SAdam Jackson }
70323e81d69SAdam Jackson 
704ff1f525eSDaniel Vetter static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
705b1f14ad0SJesse Barnes {
706b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
707b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7080e43406bSChris Wilson 	u32 de_iir, gt_iir, de_ier, pm_iir;
7090e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
7100e43406bSChris Wilson 	int i;
711b1f14ad0SJesse Barnes 
712b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
713b1f14ad0SJesse Barnes 
714b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
715b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
716b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
7170e43406bSChris Wilson 
7180e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
7190e43406bSChris Wilson 	if (gt_iir) {
7200e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
7210e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
7220e43406bSChris Wilson 		ret = IRQ_HANDLED;
7230e43406bSChris Wilson 	}
724b1f14ad0SJesse Barnes 
725b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
7260e43406bSChris Wilson 	if (de_iir) {
727ce99c256SDaniel Vetter 		if (de_iir & DE_AUX_CHANNEL_A_IVB)
728ce99c256SDaniel Vetter 			dp_aux_irq_handler(dev);
729ce99c256SDaniel Vetter 
730b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
731b1f14ad0SJesse Barnes 			intel_opregion_gse_intr(dev);
732b1f14ad0SJesse Barnes 
7330e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
73474d44445SDaniel Vetter 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
73574d44445SDaniel Vetter 				drm_handle_vblank(dev, i);
7360e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
7370e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
7380e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
739b1f14ad0SJesse Barnes 			}
740b1f14ad0SJesse Barnes 		}
741b1f14ad0SJesse Barnes 
742b1f14ad0SJesse Barnes 		/* check event from PCH */
743b1f14ad0SJesse Barnes 		if (de_iir & DE_PCH_EVENT_IVB) {
7440e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
7450e43406bSChris Wilson 
74623e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
7470e43406bSChris Wilson 
7480e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
7490e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
750b1f14ad0SJesse Barnes 		}
751b1f14ad0SJesse Barnes 
7520e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
7530e43406bSChris Wilson 		ret = IRQ_HANDLED;
7540e43406bSChris Wilson 	}
7550e43406bSChris Wilson 
7560e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
7570e43406bSChris Wilson 	if (pm_iir) {
758fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
759fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
760b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
7610e43406bSChris Wilson 		ret = IRQ_HANDLED;
7620e43406bSChris Wilson 	}
763b1f14ad0SJesse Barnes 
764b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
765b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
766b1f14ad0SJesse Barnes 
767b1f14ad0SJesse Barnes 	return ret;
768b1f14ad0SJesse Barnes }
769b1f14ad0SJesse Barnes 
770e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
771e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
772e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
773e7b4c6b1SDaniel Vetter {
774e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
775e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
776e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
777e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
778e7b4c6b1SDaniel Vetter }
779e7b4c6b1SDaniel Vetter 
780ff1f525eSDaniel Vetter static irqreturn_t ironlake_irq_handler(int irq, void *arg)
781036a4a7dSZhenyu Wang {
7824697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
783036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
784036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
785acd15b6cSDaniel Vetter 	u32 de_iir, gt_iir, de_ier, pm_iir;
786881f47b6SXiang, Haihao 
7874697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
7884697995bSJesse Barnes 
7892d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
7902d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
7912d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
7923143a2bfSChris Wilson 	POSTING_READ(DEIER);
7932d109a84SZou, Nanhai 
794036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
795036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
7963b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
797036a4a7dSZhenyu Wang 
798acd15b6cSDaniel Vetter 	if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
799c7c85101SZou Nan hai 		goto done;
800036a4a7dSZhenyu Wang 
801036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
802036a4a7dSZhenyu Wang 
803e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
804e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
805e7b4c6b1SDaniel Vetter 	else
806e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
807036a4a7dSZhenyu Wang 
808ce99c256SDaniel Vetter 	if (de_iir & DE_AUX_CHANNEL_A)
809ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
810ce99c256SDaniel Vetter 
81101c66889SZhao Yakui 	if (de_iir & DE_GSE)
8123b617967SChris Wilson 		intel_opregion_gse_intr(dev);
81301c66889SZhao Yakui 
81474d44445SDaniel Vetter 	if (de_iir & DE_PIPEA_VBLANK)
81574d44445SDaniel Vetter 		drm_handle_vblank(dev, 0);
81674d44445SDaniel Vetter 
81774d44445SDaniel Vetter 	if (de_iir & DE_PIPEB_VBLANK)
81874d44445SDaniel Vetter 		drm_handle_vblank(dev, 1);
81974d44445SDaniel Vetter 
820f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
821013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
8222bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
823013d5aa2SJesse Barnes 	}
824013d5aa2SJesse Barnes 
825f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
826f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
8272bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
828013d5aa2SJesse Barnes 	}
829c062df61SLi Peng 
830c650156aSZhenyu Wang 	/* check event from PCH */
831776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
832acd15b6cSDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
833acd15b6cSDaniel Vetter 
83423e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
83523e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
83623e81d69SAdam Jackson 		else
83723e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
838acd15b6cSDaniel Vetter 
839acd15b6cSDaniel Vetter 		/* should clear PCH hotplug event before clear CPU irq */
840acd15b6cSDaniel Vetter 		I915_WRITE(SDEIIR, pch_iir);
841776ad806SJesse Barnes 	}
842c650156aSZhenyu Wang 
84373edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
84473edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
845f97108d1SJesse Barnes 
846fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
847fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
8483b8d8d91SJesse Barnes 
849c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
850c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
8514912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
852036a4a7dSZhenyu Wang 
853c7c85101SZou Nan hai done:
8542d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
8553143a2bfSChris Wilson 	POSTING_READ(DEIER);
8562d109a84SZou, Nanhai 
857036a4a7dSZhenyu Wang 	return ret;
858036a4a7dSZhenyu Wang }
859036a4a7dSZhenyu Wang 
8608a905236SJesse Barnes /**
8618a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
8628a905236SJesse Barnes  * @work: work struct
8638a905236SJesse Barnes  *
8648a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
8658a905236SJesse Barnes  * was detected.
8668a905236SJesse Barnes  */
8678a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
8688a905236SJesse Barnes {
8691f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
8701f83fee0SDaniel Vetter 						    work);
8711f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
8721f83fee0SDaniel Vetter 						    gpu_error);
8738a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
874f69061beSDaniel Vetter 	struct intel_ring_buffer *ring;
875f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
876f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
877f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
878f69061beSDaniel Vetter 	int i, ret;
8798a905236SJesse Barnes 
880f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
8818a905236SJesse Barnes 
8827db0ba24SDaniel Vetter 	/*
8837db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
8847db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
8857db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
8867db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
8877db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
8887db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
8897db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
8907db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
8917db0ba24SDaniel Vetter 	 */
8927db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
89344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
8947db0ba24SDaniel Vetter 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
8957db0ba24SDaniel Vetter 				   reset_event);
8961f83fee0SDaniel Vetter 
897f69061beSDaniel Vetter 		ret = i915_reset(dev);
898f69061beSDaniel Vetter 
899f69061beSDaniel Vetter 		if (ret == 0) {
900f69061beSDaniel Vetter 			/*
901f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
902f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
903f69061beSDaniel Vetter 			 * complete.
904f69061beSDaniel Vetter 			 *
905f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
906f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
907f69061beSDaniel Vetter 			 * updates before
908f69061beSDaniel Vetter 			 * the counter increment.
909f69061beSDaniel Vetter 			 */
910f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
911f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
912f69061beSDaniel Vetter 
913f69061beSDaniel Vetter 			kobject_uevent_env(&dev->primary->kdev.kobj,
914f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
9151f83fee0SDaniel Vetter 		} else {
9161f83fee0SDaniel Vetter 			atomic_set(&error->reset_counter, I915_WEDGED);
917f316a42cSBen Gamari 		}
9181f83fee0SDaniel Vetter 
919f69061beSDaniel Vetter 		for_each_ring(ring, dev_priv, i)
920f69061beSDaniel Vetter 			wake_up_all(&ring->irq_queue);
921f69061beSDaniel Vetter 
92296a02917SVille Syrjälä 		intel_display_handle_reset(dev);
92396a02917SVille Syrjälä 
9241f83fee0SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
925f316a42cSBen Gamari 	}
9268a905236SJesse Barnes }
9278a905236SJesse Barnes 
92885f9e50dSDaniel Vetter /* NB: please notice the memset */
92985f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
93085f9e50dSDaniel Vetter 				    uint32_t *instdone)
93185f9e50dSDaniel Vetter {
93285f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
93385f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
93485f9e50dSDaniel Vetter 
93585f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
93685f9e50dSDaniel Vetter 	case 2:
93785f9e50dSDaniel Vetter 	case 3:
93885f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
93985f9e50dSDaniel Vetter 		break;
94085f9e50dSDaniel Vetter 	case 4:
94185f9e50dSDaniel Vetter 	case 5:
94285f9e50dSDaniel Vetter 	case 6:
94385f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
94485f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
94585f9e50dSDaniel Vetter 		break;
94685f9e50dSDaniel Vetter 	default:
94785f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
94885f9e50dSDaniel Vetter 	case 7:
94985f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
95085f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
95185f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
95285f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
95385f9e50dSDaniel Vetter 		break;
95485f9e50dSDaniel Vetter 	}
95585f9e50dSDaniel Vetter }
95685f9e50dSDaniel Vetter 
9573bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
9589df30794SChris Wilson static struct drm_i915_error_object *
959bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
96005394f39SChris Wilson 			 struct drm_i915_gem_object *src)
9619df30794SChris Wilson {
9629df30794SChris Wilson 	struct drm_i915_error_object *dst;
9639da3da66SChris Wilson 	int i, count;
964e56660ddSChris Wilson 	u32 reloc_offset;
9659df30794SChris Wilson 
96605394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
9679df30794SChris Wilson 		return NULL;
9689df30794SChris Wilson 
9699da3da66SChris Wilson 	count = src->base.size / PAGE_SIZE;
9709df30794SChris Wilson 
9719da3da66SChris Wilson 	dst = kmalloc(sizeof(*dst) + count * sizeof(u32 *), GFP_ATOMIC);
9729df30794SChris Wilson 	if (dst == NULL)
9739df30794SChris Wilson 		return NULL;
9749df30794SChris Wilson 
97505394f39SChris Wilson 	reloc_offset = src->gtt_offset;
9769da3da66SChris Wilson 	for (i = 0; i < count; i++) {
977788885aeSAndrew Morton 		unsigned long flags;
978e56660ddSChris Wilson 		void *d;
979788885aeSAndrew Morton 
980e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
9819df30794SChris Wilson 		if (d == NULL)
9829df30794SChris Wilson 			goto unwind;
983e56660ddSChris Wilson 
984788885aeSAndrew Morton 		local_irq_save(flags);
9855d4545aeSBen Widawsky 		if (reloc_offset < dev_priv->gtt.mappable_end &&
98674898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
987172975aaSChris Wilson 			void __iomem *s;
988172975aaSChris Wilson 
989172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
990172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
991172975aaSChris Wilson 			 * captures what the GPU read.
992172975aaSChris Wilson 			 */
993172975aaSChris Wilson 
9945d4545aeSBen Widawsky 			s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
9953e4d3af5SPeter Zijlstra 						     reloc_offset);
996e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
9973e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
998960e3564SChris Wilson 		} else if (src->stolen) {
999960e3564SChris Wilson 			unsigned long offset;
1000960e3564SChris Wilson 
1001960e3564SChris Wilson 			offset = dev_priv->mm.stolen_base;
1002960e3564SChris Wilson 			offset += src->stolen->start;
1003960e3564SChris Wilson 			offset += i << PAGE_SHIFT;
1004960e3564SChris Wilson 
10051a240d4dSDaniel Vetter 			memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
1006172975aaSChris Wilson 		} else {
10079da3da66SChris Wilson 			struct page *page;
1008172975aaSChris Wilson 			void *s;
1009172975aaSChris Wilson 
10109da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
1011172975aaSChris Wilson 
10129da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
10139da3da66SChris Wilson 
10149da3da66SChris Wilson 			s = kmap_atomic(page);
1015172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
1016172975aaSChris Wilson 			kunmap_atomic(s);
1017172975aaSChris Wilson 
10189da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
1019172975aaSChris Wilson 		}
1020788885aeSAndrew Morton 		local_irq_restore(flags);
1021e56660ddSChris Wilson 
10229da3da66SChris Wilson 		dst->pages[i] = d;
1023e56660ddSChris Wilson 
1024e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
10259df30794SChris Wilson 	}
10269da3da66SChris Wilson 	dst->page_count = count;
102705394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
10289df30794SChris Wilson 
10299df30794SChris Wilson 	return dst;
10309df30794SChris Wilson 
10319df30794SChris Wilson unwind:
10329da3da66SChris Wilson 	while (i--)
10339da3da66SChris Wilson 		kfree(dst->pages[i]);
10349df30794SChris Wilson 	kfree(dst);
10359df30794SChris Wilson 	return NULL;
10369df30794SChris Wilson }
10379df30794SChris Wilson 
10389df30794SChris Wilson static void
10399df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
10409df30794SChris Wilson {
10419df30794SChris Wilson 	int page;
10429df30794SChris Wilson 
10439df30794SChris Wilson 	if (obj == NULL)
10449df30794SChris Wilson 		return;
10459df30794SChris Wilson 
10469df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
10479df30794SChris Wilson 		kfree(obj->pages[page]);
10489df30794SChris Wilson 
10499df30794SChris Wilson 	kfree(obj);
10509df30794SChris Wilson }
10519df30794SChris Wilson 
1052742cbee8SDaniel Vetter void
1053742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
10549df30794SChris Wilson {
1055742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
1056742cbee8SDaniel Vetter 							  typeof(*error), ref);
1057e2f973d5SChris Wilson 	int i;
1058e2f973d5SChris Wilson 
105952d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
106052d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
106152d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
106252d39a21SChris Wilson 		kfree(error->ring[i].requests);
106352d39a21SChris Wilson 	}
1064e2f973d5SChris Wilson 
10659df30794SChris Wilson 	kfree(error->active_bo);
10666ef3d427SChris Wilson 	kfree(error->overlay);
10679df30794SChris Wilson 	kfree(error);
10689df30794SChris Wilson }
10691b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
10701b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
1071c724e8a9SChris Wilson {
1072c724e8a9SChris Wilson 	err->size = obj->base.size;
1073c724e8a9SChris Wilson 	err->name = obj->base.name;
10740201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
10750201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1076c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
1077c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1078c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1079c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1080c724e8a9SChris Wilson 	err->pinned = 0;
1081c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1082c724e8a9SChris Wilson 		err->pinned = 1;
1083c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1084c724e8a9SChris Wilson 		err->pinned = -1;
1085c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1086c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1087c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
108896154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
108993dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
10901b50247aSChris Wilson }
1091c724e8a9SChris Wilson 
10921b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
10931b50247aSChris Wilson 			     int count, struct list_head *head)
10941b50247aSChris Wilson {
10951b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
10961b50247aSChris Wilson 	int i = 0;
10971b50247aSChris Wilson 
10981b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
10991b50247aSChris Wilson 		capture_bo(err++, obj);
1100c724e8a9SChris Wilson 		if (++i == count)
1101c724e8a9SChris Wilson 			break;
11021b50247aSChris Wilson 	}
1103c724e8a9SChris Wilson 
11041b50247aSChris Wilson 	return i;
11051b50247aSChris Wilson }
11061b50247aSChris Wilson 
11071b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
11081b50247aSChris Wilson 			     int count, struct list_head *head)
11091b50247aSChris Wilson {
11101b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
11111b50247aSChris Wilson 	int i = 0;
11121b50247aSChris Wilson 
11131b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
11141b50247aSChris Wilson 		if (obj->pin_count == 0)
11151b50247aSChris Wilson 			continue;
11161b50247aSChris Wilson 
11171b50247aSChris Wilson 		capture_bo(err++, obj);
11181b50247aSChris Wilson 		if (++i == count)
11191b50247aSChris Wilson 			break;
1120c724e8a9SChris Wilson 	}
1121c724e8a9SChris Wilson 
1122c724e8a9SChris Wilson 	return i;
1123c724e8a9SChris Wilson }
1124c724e8a9SChris Wilson 
1125748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1126748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1127748ebc60SChris Wilson {
1128748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1129748ebc60SChris Wilson 	int i;
1130748ebc60SChris Wilson 
1131748ebc60SChris Wilson 	/* Fences */
1132748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1133775d17b6SDaniel Vetter 	case 7:
1134748ebc60SChris Wilson 	case 6:
1135748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1136748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1137748ebc60SChris Wilson 		break;
1138748ebc60SChris Wilson 	case 5:
1139748ebc60SChris Wilson 	case 4:
1140748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1141748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1142748ebc60SChris Wilson 		break;
1143748ebc60SChris Wilson 	case 3:
1144748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1145748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1146748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1147748ebc60SChris Wilson 	case 2:
1148748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1149748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1150748ebc60SChris Wilson 		break;
1151748ebc60SChris Wilson 
11527dbf9d6eSBen Widawsky 	default:
11537dbf9d6eSBen Widawsky 		BUG();
1154748ebc60SChris Wilson 	}
1155748ebc60SChris Wilson }
1156748ebc60SChris Wilson 
1157bcfb2e28SChris Wilson static struct drm_i915_error_object *
1158bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1159bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1160bcfb2e28SChris Wilson {
1161bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1162bcfb2e28SChris Wilson 	u32 seqno;
1163bcfb2e28SChris Wilson 
1164bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1165bcfb2e28SChris Wilson 		return NULL;
1166bcfb2e28SChris Wilson 
1167b45305fcSDaniel Vetter 	if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1168b45305fcSDaniel Vetter 		u32 acthd = I915_READ(ACTHD);
1169b45305fcSDaniel Vetter 
1170b45305fcSDaniel Vetter 		if (WARN_ON(ring->id != RCS))
1171b45305fcSDaniel Vetter 			return NULL;
1172b45305fcSDaniel Vetter 
1173b45305fcSDaniel Vetter 		obj = ring->private;
1174b45305fcSDaniel Vetter 		if (acthd >= obj->gtt_offset &&
1175b45305fcSDaniel Vetter 		    acthd < obj->gtt_offset + obj->base.size)
1176b45305fcSDaniel Vetter 			return i915_error_object_create(dev_priv, obj);
1177b45305fcSDaniel Vetter 	}
1178b45305fcSDaniel Vetter 
1179b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1180bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1181bcfb2e28SChris Wilson 		if (obj->ring != ring)
1182bcfb2e28SChris Wilson 			continue;
1183bcfb2e28SChris Wilson 
11840201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1185bcfb2e28SChris Wilson 			continue;
1186bcfb2e28SChris Wilson 
1187bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1188bcfb2e28SChris Wilson 			continue;
1189bcfb2e28SChris Wilson 
1190bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1191bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1192bcfb2e28SChris Wilson 		 */
1193bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1194bcfb2e28SChris Wilson 	}
1195bcfb2e28SChris Wilson 
1196bcfb2e28SChris Wilson 	return NULL;
1197bcfb2e28SChris Wilson }
1198bcfb2e28SChris Wilson 
1199d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1200d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1201d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1202d27b1e0eSDaniel Vetter {
1203d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1204d27b1e0eSDaniel Vetter 
120533f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
120612f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
120733f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
12087e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
12097e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
12107e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
12117e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
1212df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1213df2b23d9SChris Wilson 		error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
121433f3f518SDaniel Vetter 	}
1215c1cd90edSDaniel Vetter 
1216d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
12179d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1218d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1219d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1220d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1221c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1222050ee91fSBen Widawsky 		if (ring->id == RCS)
1223d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1224d27b1e0eSDaniel Vetter 	} else {
12259d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1226d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1227d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1228d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1229d27b1e0eSDaniel Vetter 	}
1230d27b1e0eSDaniel Vetter 
12319574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1232c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1233b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1234d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1235c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1236c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
12370f3b6849SChris Wilson 	error->ctl[ring->id] = I915_READ_CTL(ring);
12387e3b8737SDaniel Vetter 
12397e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
12407e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1241d27b1e0eSDaniel Vetter }
1242d27b1e0eSDaniel Vetter 
124352d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
124452d39a21SChris Wilson 				  struct drm_i915_error_state *error)
124552d39a21SChris Wilson {
124652d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1247b4519513SChris Wilson 	struct intel_ring_buffer *ring;
124852d39a21SChris Wilson 	struct drm_i915_gem_request *request;
124952d39a21SChris Wilson 	int i, count;
125052d39a21SChris Wilson 
1251b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
125252d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
125352d39a21SChris Wilson 
125452d39a21SChris Wilson 		error->ring[i].batchbuffer =
125552d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
125652d39a21SChris Wilson 
125752d39a21SChris Wilson 		error->ring[i].ringbuffer =
125852d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
125952d39a21SChris Wilson 
126052d39a21SChris Wilson 		count = 0;
126152d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
126252d39a21SChris Wilson 			count++;
126352d39a21SChris Wilson 
126452d39a21SChris Wilson 		error->ring[i].num_requests = count;
126552d39a21SChris Wilson 		error->ring[i].requests =
126652d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
126752d39a21SChris Wilson 				GFP_ATOMIC);
126852d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
126952d39a21SChris Wilson 			error->ring[i].num_requests = 0;
127052d39a21SChris Wilson 			continue;
127152d39a21SChris Wilson 		}
127252d39a21SChris Wilson 
127352d39a21SChris Wilson 		count = 0;
127452d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
127552d39a21SChris Wilson 			struct drm_i915_error_request *erq;
127652d39a21SChris Wilson 
127752d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
127852d39a21SChris Wilson 			erq->seqno = request->seqno;
127952d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1280ee4f42b1SChris Wilson 			erq->tail = request->tail;
128152d39a21SChris Wilson 		}
128252d39a21SChris Wilson 	}
128352d39a21SChris Wilson }
128452d39a21SChris Wilson 
12858a905236SJesse Barnes /**
12868a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
12878a905236SJesse Barnes  * @dev: drm device
12888a905236SJesse Barnes  *
12898a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
12908a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
12918a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
12928a905236SJesse Barnes  * to pick up.
12938a905236SJesse Barnes  */
129463eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
129563eeaf38SJesse Barnes {
129663eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
129705394f39SChris Wilson 	struct drm_i915_gem_object *obj;
129863eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
129963eeaf38SJesse Barnes 	unsigned long flags;
13009db4a9c7SJesse Barnes 	int i, pipe;
130163eeaf38SJesse Barnes 
130299584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
130399584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
130499584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
13059df30794SChris Wilson 	if (error)
13069df30794SChris Wilson 		return;
130763eeaf38SJesse Barnes 
13089db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
130933f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
131063eeaf38SJesse Barnes 	if (!error) {
13119df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
13129df30794SChris Wilson 		return;
131363eeaf38SJesse Barnes 	}
131463eeaf38SJesse Barnes 
13152f86f191SBen Widawsky 	DRM_INFO("capturing error event; look for more information in"
13162f86f191SBen Widawsky 		 "/sys/kernel/debug/dri/%d/i915_error_state\n",
1317b6f7833bSChris Wilson 		 dev->primary->index);
13182fa772f3SChris Wilson 
1319742cbee8SDaniel Vetter 	kref_init(&error->ref);
132063eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
132163eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1322b9a3906bSBen Widawsky 	error->ccid = I915_READ(CCID);
1323be998e2eSBen Widawsky 
1324be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1325be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1326be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1327be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1328be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1329be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1330be998e2eSBen Widawsky 	else
1331be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1332be998e2eSBen Widawsky 
13330f3b6849SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6)
13340f3b6849SChris Wilson 		error->derrmr = I915_READ(DERRMR);
13350f3b6849SChris Wilson 
13360f3b6849SChris Wilson 	if (IS_VALLEYVIEW(dev))
13370f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_VLV);
13380f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 7)
13390f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE_MT);
13400f3b6849SChris Wilson 	else if (INTEL_INFO(dev)->gen == 6)
13410f3b6849SChris Wilson 		error->forcewake = I915_READ(FORCEWAKE);
13420f3b6849SChris Wilson 
13439db4a9c7SJesse Barnes 	for_each_pipe(pipe)
13449db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1345d27b1e0eSDaniel Vetter 
134633f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1347f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
134833f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
134933f3f518SDaniel Vetter 	}
1350add354ddSChris Wilson 
135171e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
135271e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
135371e172e8SBen Widawsky 
1354050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1355050ee91fSBen Widawsky 
1356748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
135752d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
13589df30794SChris Wilson 
1359c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
13609df30794SChris Wilson 	error->active_bo = NULL;
1361c724e8a9SChris Wilson 	error->pinned_bo = NULL;
13629df30794SChris Wilson 
1363bcfb2e28SChris Wilson 	i = 0;
1364bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1365bcfb2e28SChris Wilson 		i++;
1366bcfb2e28SChris Wilson 	error->active_bo_count = i;
13676c085a72SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
13681b50247aSChris Wilson 		if (obj->pin_count)
1369bcfb2e28SChris Wilson 			i++;
1370bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1371c724e8a9SChris Wilson 
13728e934dbfSChris Wilson 	error->active_bo = NULL;
13738e934dbfSChris Wilson 	error->pinned_bo = NULL;
1374bcfb2e28SChris Wilson 	if (i) {
1375bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
13769df30794SChris Wilson 					   GFP_ATOMIC);
1377c724e8a9SChris Wilson 		if (error->active_bo)
1378c724e8a9SChris Wilson 			error->pinned_bo =
1379c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
13809df30794SChris Wilson 	}
1381c724e8a9SChris Wilson 
1382c724e8a9SChris Wilson 	if (error->active_bo)
1383c724e8a9SChris Wilson 		error->active_bo_count =
13841b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1385c724e8a9SChris Wilson 					  error->active_bo_count,
1386c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1387c724e8a9SChris Wilson 
1388c724e8a9SChris Wilson 	if (error->pinned_bo)
1389c724e8a9SChris Wilson 		error->pinned_bo_count =
13901b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1391c724e8a9SChris Wilson 					  error->pinned_bo_count,
13926c085a72SChris Wilson 					  &dev_priv->mm.bound_list);
139363eeaf38SJesse Barnes 
13948a905236SJesse Barnes 	do_gettimeofday(&error->time);
13958a905236SJesse Barnes 
13966ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1397c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
13986ef3d427SChris Wilson 
139999584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
140099584db3SDaniel Vetter 	if (dev_priv->gpu_error.first_error == NULL) {
140199584db3SDaniel Vetter 		dev_priv->gpu_error.first_error = error;
14029df30794SChris Wilson 		error = NULL;
14039df30794SChris Wilson 	}
140499584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
14059df30794SChris Wilson 
14069df30794SChris Wilson 	if (error)
1407742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
14089df30794SChris Wilson }
14099df30794SChris Wilson 
14109df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
14119df30794SChris Wilson {
14129df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
14139df30794SChris Wilson 	struct drm_i915_error_state *error;
14146dc0e816SBen Widawsky 	unsigned long flags;
14159df30794SChris Wilson 
141699584db3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
141799584db3SDaniel Vetter 	error = dev_priv->gpu_error.first_error;
141899584db3SDaniel Vetter 	dev_priv->gpu_error.first_error = NULL;
141999584db3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
14209df30794SChris Wilson 
14219df30794SChris Wilson 	if (error)
1422742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
142363eeaf38SJesse Barnes }
14243bd3c932SChris Wilson #else
14253bd3c932SChris Wilson #define i915_capture_error_state(x)
14263bd3c932SChris Wilson #endif
142763eeaf38SJesse Barnes 
142835aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1429c0e09200SDave Airlie {
14308a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1431bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
143263eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1433050ee91fSBen Widawsky 	int pipe, i;
143463eeaf38SJesse Barnes 
143535aed2e6SChris Wilson 	if (!eir)
143635aed2e6SChris Wilson 		return;
143763eeaf38SJesse Barnes 
1438a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
14398a905236SJesse Barnes 
1440bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1441bd9854f9SBen Widawsky 
14428a905236SJesse Barnes 	if (IS_G4X(dev)) {
14438a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
14448a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
14458a905236SJesse Barnes 
1446a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1447a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1448050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1449050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1450a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1451a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
14528a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
14533143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
14548a905236SJesse Barnes 		}
14558a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
14568a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1457a70491ccSJoe Perches 			pr_err("page table error\n");
1458a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
14598a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
14603143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
14618a905236SJesse Barnes 		}
14628a905236SJesse Barnes 	}
14638a905236SJesse Barnes 
1464a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
146563eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
146663eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1467a70491ccSJoe Perches 			pr_err("page table error\n");
1468a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
146963eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
14703143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
147163eeaf38SJesse Barnes 		}
14728a905236SJesse Barnes 	}
14738a905236SJesse Barnes 
147463eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1475a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
14769db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1477a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
14789db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
147963eeaf38SJesse Barnes 		/* pipestat has already been acked */
148063eeaf38SJesse Barnes 	}
148163eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1482a70491ccSJoe Perches 		pr_err("instruction error\n");
1483a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1484050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1485050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1486a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
148763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
148863eeaf38SJesse Barnes 
1489a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1490a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1491a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
149263eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
14933143a2bfSChris Wilson 			POSTING_READ(IPEIR);
149463eeaf38SJesse Barnes 		} else {
149563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
149663eeaf38SJesse Barnes 
1497a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1498a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1499a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1500a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
150163eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
15023143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
150363eeaf38SJesse Barnes 		}
150463eeaf38SJesse Barnes 	}
150563eeaf38SJesse Barnes 
150663eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
15073143a2bfSChris Wilson 	POSTING_READ(EIR);
150863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
150963eeaf38SJesse Barnes 	if (eir) {
151063eeaf38SJesse Barnes 		/*
151163eeaf38SJesse Barnes 		 * some errors might have become stuck,
151263eeaf38SJesse Barnes 		 * mask them.
151363eeaf38SJesse Barnes 		 */
151463eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
151563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
151663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
151763eeaf38SJesse Barnes 	}
151835aed2e6SChris Wilson }
151935aed2e6SChris Wilson 
152035aed2e6SChris Wilson /**
152135aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
152235aed2e6SChris Wilson  * @dev: drm device
152335aed2e6SChris Wilson  *
152435aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
152535aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
152635aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
152735aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
152835aed2e6SChris Wilson  * of a ring dump etc.).
152935aed2e6SChris Wilson  */
1530527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
153135aed2e6SChris Wilson {
153235aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1533b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1534b4519513SChris Wilson 	int i;
153535aed2e6SChris Wilson 
153635aed2e6SChris Wilson 	i915_capture_error_state(dev);
153735aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
15388a905236SJesse Barnes 
1539ba1234d1SBen Gamari 	if (wedged) {
1540f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1541f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
1542ba1234d1SBen Gamari 
154311ed50ecSBen Gamari 		/*
15441f83fee0SDaniel Vetter 		 * Wakeup waiting processes so that the reset work item
15451f83fee0SDaniel Vetter 		 * doesn't deadlock trying to grab various locks.
154611ed50ecSBen Gamari 		 */
1547b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1548b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
154911ed50ecSBen Gamari 	}
155011ed50ecSBen Gamari 
155199584db3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
15528a905236SJesse Barnes }
15538a905236SJesse Barnes 
155421ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
15554e5359cdSSimon Farnsworth {
15564e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
15574e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
15584e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
155905394f39SChris Wilson 	struct drm_i915_gem_object *obj;
15604e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
15614e5359cdSSimon Farnsworth 	unsigned long flags;
15624e5359cdSSimon Farnsworth 	bool stall_detected;
15634e5359cdSSimon Farnsworth 
15644e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
15654e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
15664e5359cdSSimon Farnsworth 		return;
15674e5359cdSSimon Farnsworth 
15684e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
15694e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
15704e5359cdSSimon Farnsworth 
1571e7d841caSChris Wilson 	if (work == NULL ||
1572e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1573e7d841caSChris Wilson 	    !work->enable_stall_check) {
15744e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
15754e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
15764e5359cdSSimon Farnsworth 		return;
15774e5359cdSSimon Farnsworth 	}
15784e5359cdSSimon Farnsworth 
15794e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
158005394f39SChris Wilson 	obj = work->pending_flip_obj;
1581a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
15829db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1583446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1584446f2545SArmin Reese 					obj->gtt_offset;
15854e5359cdSSimon Farnsworth 	} else {
15869db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
158705394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
158801f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
15894e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
15904e5359cdSSimon Farnsworth 	}
15914e5359cdSSimon Farnsworth 
15924e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
15934e5359cdSSimon Farnsworth 
15944e5359cdSSimon Farnsworth 	if (stall_detected) {
15954e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
15964e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
15974e5359cdSSimon Farnsworth 	}
15984e5359cdSSimon Farnsworth }
15994e5359cdSSimon Farnsworth 
160042f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
160142f52ef8SKeith Packard  * we use as a pipe index
160242f52ef8SKeith Packard  */
1603f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
16040a3e67a4SJesse Barnes {
16050a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1606e9d21d7fSKeith Packard 	unsigned long irqflags;
160771e0ffa5SJesse Barnes 
16085eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
160971e0ffa5SJesse Barnes 		return -EINVAL;
16100a3e67a4SJesse Barnes 
16111ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1612f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
16137c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
16147c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
16150a3e67a4SJesse Barnes 	else
16167c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
16177c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
16188692d00eSChris Wilson 
16198692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
16208692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
16216b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
16221ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
16238692d00eSChris Wilson 
16240a3e67a4SJesse Barnes 	return 0;
16250a3e67a4SJesse Barnes }
16260a3e67a4SJesse Barnes 
1627f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1628f796cf8fSJesse Barnes {
1629f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1630f796cf8fSJesse Barnes 	unsigned long irqflags;
1631f796cf8fSJesse Barnes 
1632f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1633f796cf8fSJesse Barnes 		return -EINVAL;
1634f796cf8fSJesse Barnes 
1635f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1636f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1637f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1638f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1639f796cf8fSJesse Barnes 
1640f796cf8fSJesse Barnes 	return 0;
1641f796cf8fSJesse Barnes }
1642f796cf8fSJesse Barnes 
1643f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1644b1f14ad0SJesse Barnes {
1645b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1646b1f14ad0SJesse Barnes 	unsigned long irqflags;
1647b1f14ad0SJesse Barnes 
1648b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1649b1f14ad0SJesse Barnes 		return -EINVAL;
1650b1f14ad0SJesse Barnes 
1651b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1652b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
1653b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
1654b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1655b1f14ad0SJesse Barnes 
1656b1f14ad0SJesse Barnes 	return 0;
1657b1f14ad0SJesse Barnes }
1658b1f14ad0SJesse Barnes 
16597e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
16607e231dbeSJesse Barnes {
16617e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16627e231dbeSJesse Barnes 	unsigned long irqflags;
166331acc7f5SJesse Barnes 	u32 imr;
16647e231dbeSJesse Barnes 
16657e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
16667e231dbeSJesse Barnes 		return -EINVAL;
16677e231dbeSJesse Barnes 
16687e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
16697e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
167031acc7f5SJesse Barnes 	if (pipe == 0)
16717e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
167231acc7f5SJesse Barnes 	else
16737e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
16747e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
167531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
167631acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
16777e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
16787e231dbeSJesse Barnes 
16797e231dbeSJesse Barnes 	return 0;
16807e231dbeSJesse Barnes }
16817e231dbeSJesse Barnes 
168242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
168342f52ef8SKeith Packard  * we use as a pipe index
168442f52ef8SKeith Packard  */
1685f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
16860a3e67a4SJesse Barnes {
16870a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1688e9d21d7fSKeith Packard 	unsigned long irqflags;
16890a3e67a4SJesse Barnes 
16901ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
16918692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
16926b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
16938692d00eSChris Wilson 
16947c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
16957c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
16967c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
16971ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
16980a3e67a4SJesse Barnes }
16990a3e67a4SJesse Barnes 
1700f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1701f796cf8fSJesse Barnes {
1702f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1703f796cf8fSJesse Barnes 	unsigned long irqflags;
1704f796cf8fSJesse Barnes 
1705f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1706f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1707f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1708f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1709f796cf8fSJesse Barnes }
1710f796cf8fSJesse Barnes 
1711f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1712b1f14ad0SJesse Barnes {
1713b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1714b1f14ad0SJesse Barnes 	unsigned long irqflags;
1715b1f14ad0SJesse Barnes 
1716b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1717b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
1718b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
1719b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1720b1f14ad0SJesse Barnes }
1721b1f14ad0SJesse Barnes 
17227e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
17237e231dbeSJesse Barnes {
17247e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17257e231dbeSJesse Barnes 	unsigned long irqflags;
172631acc7f5SJesse Barnes 	u32 imr;
17277e231dbeSJesse Barnes 
17287e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
172931acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
173031acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
17317e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
173231acc7f5SJesse Barnes 	if (pipe == 0)
17337e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
173431acc7f5SJesse Barnes 	else
17357e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
17367e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
17377e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17387e231dbeSJesse Barnes }
17397e231dbeSJesse Barnes 
1740893eead0SChris Wilson static u32
1741893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1742852835f3SZou Nan hai {
1743893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1744893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1745893eead0SChris Wilson }
1746893eead0SChris Wilson 
1747893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1748893eead0SChris Wilson {
1749893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1750b2eadbc8SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring, false),
1751b2eadbc8SChris Wilson 			      ring_last_seqno(ring))) {
1752893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
17539574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
17549574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
17559574b3feSBen Widawsky 				  ring->name);
1756893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1757893eead0SChris Wilson 			*err = true;
1758893eead0SChris Wilson 		}
1759893eead0SChris Wilson 		return true;
1760893eead0SChris Wilson 	}
1761893eead0SChris Wilson 	return false;
1762f65d9421SBen Gamari }
1763f65d9421SBen Gamari 
17641ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
17651ec14ad3SChris Wilson {
17661ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
17671ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
17681ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
17691ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
17701ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
17711ec14ad3SChris Wilson 			  ring->name);
17721ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
17731ec14ad3SChris Wilson 		return true;
17741ec14ad3SChris Wilson 	}
17751ec14ad3SChris Wilson 	return false;
17761ec14ad3SChris Wilson }
17771ec14ad3SChris Wilson 
1778d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
1779d1e61e7fSChris Wilson {
1780d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1781d1e61e7fSChris Wilson 
178299584db3SDaniel Vetter 	if (dev_priv->gpu_error.hangcheck_count++ > 1) {
1783b4519513SChris Wilson 		bool hung = true;
1784b4519513SChris Wilson 
1785d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1786d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
1787d1e61e7fSChris Wilson 
1788d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
1789b4519513SChris Wilson 			struct intel_ring_buffer *ring;
1790b4519513SChris Wilson 			int i;
1791b4519513SChris Wilson 
1792d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
1793d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
1794d1e61e7fSChris Wilson 			 * and break the hang. This should work on
1795d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
1796d1e61e7fSChris Wilson 			 */
1797b4519513SChris Wilson 			for_each_ring(ring, dev_priv, i)
1798b4519513SChris Wilson 				hung &= !kick_ring(ring);
1799d1e61e7fSChris Wilson 		}
1800d1e61e7fSChris Wilson 
1801b4519513SChris Wilson 		return hung;
1802d1e61e7fSChris Wilson 	}
1803d1e61e7fSChris Wilson 
1804d1e61e7fSChris Wilson 	return false;
1805d1e61e7fSChris Wilson }
1806d1e61e7fSChris Wilson 
1807f65d9421SBen Gamari /**
1808f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1809f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1810f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1811f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1812f65d9421SBen Gamari  */
1813f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1814f65d9421SBen Gamari {
1815f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1816f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1817bd9854f9SBen Widawsky 	uint32_t acthd[I915_NUM_RINGS], instdone[I915_NUM_INSTDONE_REG];
1818b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1819b4519513SChris Wilson 	bool err = false, idle;
1820b4519513SChris Wilson 	int i;
1821893eead0SChris Wilson 
18223e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
18233e0dc6b0SBen Widawsky 		return;
18243e0dc6b0SBen Widawsky 
1825b4519513SChris Wilson 	memset(acthd, 0, sizeof(acthd));
1826b4519513SChris Wilson 	idle = true;
1827b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
1828b4519513SChris Wilson 	    idle &= i915_hangcheck_ring_idle(ring, &err);
1829b4519513SChris Wilson 	    acthd[i] = intel_ring_get_active_head(ring);
1830b4519513SChris Wilson 	}
1831b4519513SChris Wilson 
1832893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
1833b4519513SChris Wilson 	if (idle) {
1834d1e61e7fSChris Wilson 		if (err) {
1835d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
1836d1e61e7fSChris Wilson 				return;
1837d1e61e7fSChris Wilson 
1838893eead0SChris Wilson 			goto repeat;
1839d1e61e7fSChris Wilson 		}
1840d1e61e7fSChris Wilson 
184199584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
1842893eead0SChris Wilson 		return;
1843893eead0SChris Wilson 	}
1844f65d9421SBen Gamari 
1845bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
184699584db3SDaniel Vetter 	if (memcmp(dev_priv->gpu_error.last_acthd, acthd,
184799584db3SDaniel Vetter 		   sizeof(acthd)) == 0 &&
184899584db3SDaniel Vetter 	    memcmp(dev_priv->gpu_error.prev_instdone, instdone,
184999584db3SDaniel Vetter 		   sizeof(instdone)) == 0) {
1850d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
1851f65d9421SBen Gamari 			return;
1852cbb465e7SChris Wilson 	} else {
185399584db3SDaniel Vetter 		dev_priv->gpu_error.hangcheck_count = 0;
1854cbb465e7SChris Wilson 
185599584db3SDaniel Vetter 		memcpy(dev_priv->gpu_error.last_acthd, acthd,
185699584db3SDaniel Vetter 		       sizeof(acthd));
185799584db3SDaniel Vetter 		memcpy(dev_priv->gpu_error.prev_instdone, instdone,
185899584db3SDaniel Vetter 		       sizeof(instdone));
1859cbb465e7SChris Wilson 	}
1860f65d9421SBen Gamari 
1861893eead0SChris Wilson repeat:
1862f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
186399584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
1864cecc21feSChris Wilson 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
1865f65d9421SBen Gamari }
1866f65d9421SBen Gamari 
1867c0e09200SDave Airlie /* drm_dma.h hooks
1868c0e09200SDave Airlie */
1869f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
1870036a4a7dSZhenyu Wang {
1871036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1872036a4a7dSZhenyu Wang 
18734697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
18744697995bSJesse Barnes 
1875036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1876bdfcdb63SDaniel Vetter 
1877036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1878036a4a7dSZhenyu Wang 
1879036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1880036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
18813143a2bfSChris Wilson 	POSTING_READ(DEIER);
1882036a4a7dSZhenyu Wang 
1883036a4a7dSZhenyu Wang 	/* and GT */
1884036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1885036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
18863143a2bfSChris Wilson 	POSTING_READ(GTIER);
1887c650156aSZhenyu Wang 
1888c650156aSZhenyu Wang 	/* south display irq */
1889c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1890c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
18913143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1892036a4a7dSZhenyu Wang }
1893036a4a7dSZhenyu Wang 
18947e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
18957e231dbeSJesse Barnes {
18967e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18977e231dbeSJesse Barnes 	int pipe;
18987e231dbeSJesse Barnes 
18997e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
19007e231dbeSJesse Barnes 
19017e231dbeSJesse Barnes 	/* VLV magic */
19027e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
19037e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
19047e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
19057e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
19067e231dbeSJesse Barnes 
19077e231dbeSJesse Barnes 	/* and GT */
19087e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
19097e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
19107e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
19117e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
19127e231dbeSJesse Barnes 	POSTING_READ(GTIER);
19137e231dbeSJesse Barnes 
19147e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
19157e231dbeSJesse Barnes 
19167e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
19177e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
19187e231dbeSJesse Barnes 	for_each_pipe(pipe)
19197e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
19207e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19217e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
19227e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
19237e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
19247e231dbeSJesse Barnes }
19257e231dbeSJesse Barnes 
19267fe0b973SKeith Packard /*
19277fe0b973SKeith Packard  * Enable digital hotplug on the PCH, and configure the DP short pulse
19287fe0b973SKeith Packard  * duration to 2ms (which is the minimum in the Display Port spec)
19297fe0b973SKeith Packard  *
19307fe0b973SKeith Packard  * This register is the same on all known PCH chips.
19317fe0b973SKeith Packard  */
19327fe0b973SKeith Packard 
1933d46da437SPaulo Zanoni static void ibx_enable_hotplug(struct drm_device *dev)
19347fe0b973SKeith Packard {
19357fe0b973SKeith Packard 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19367fe0b973SKeith Packard 	u32	hotplug;
19377fe0b973SKeith Packard 
19387fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
19397fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
19407fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
19417fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
19427fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
19437fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
19447fe0b973SKeith Packard }
19457fe0b973SKeith Packard 
1946d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
1947d46da437SPaulo Zanoni {
1948d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1949d46da437SPaulo Zanoni 	u32 mask;
1950d46da437SPaulo Zanoni 
1951d46da437SPaulo Zanoni 	if (HAS_PCH_IBX(dev))
1952d46da437SPaulo Zanoni 		mask = SDE_HOTPLUG_MASK |
1953d46da437SPaulo Zanoni 		       SDE_GMBUS |
1954d46da437SPaulo Zanoni 		       SDE_AUX_MASK;
1955d46da437SPaulo Zanoni 	else
1956d46da437SPaulo Zanoni 		mask = SDE_HOTPLUG_MASK_CPT |
1957d46da437SPaulo Zanoni 		       SDE_GMBUS_CPT |
1958d46da437SPaulo Zanoni 		       SDE_AUX_MASK_CPT;
1959d46da437SPaulo Zanoni 
1960d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1961d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
1962d46da437SPaulo Zanoni 	I915_WRITE(SDEIER, mask);
1963d46da437SPaulo Zanoni 	POSTING_READ(SDEIER);
1964d46da437SPaulo Zanoni 
1965d46da437SPaulo Zanoni 	ibx_enable_hotplug(dev);
1966d46da437SPaulo Zanoni }
1967d46da437SPaulo Zanoni 
1968f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
1969036a4a7dSZhenyu Wang {
1970036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1971036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1972013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1973ce99c256SDaniel Vetter 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
1974ce99c256SDaniel Vetter 			   DE_AUX_CHANNEL_A;
19751ec14ad3SChris Wilson 	u32 render_irqs;
1976036a4a7dSZhenyu Wang 
19771ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1978036a4a7dSZhenyu Wang 
1979036a4a7dSZhenyu Wang 	/* should always can generate irq */
1980036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
19811ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
19821ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
19833143a2bfSChris Wilson 	POSTING_READ(DEIER);
1984036a4a7dSZhenyu Wang 
19851ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1986036a4a7dSZhenyu Wang 
1987036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
19881ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1989881f47b6SXiang, Haihao 
19901ec14ad3SChris Wilson 	if (IS_GEN6(dev))
19911ec14ad3SChris Wilson 		render_irqs =
19921ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
1993e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
1994e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
19951ec14ad3SChris Wilson 	else
19961ec14ad3SChris Wilson 		render_irqs =
199788f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1998c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
19991ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
20001ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
20013143a2bfSChris Wilson 	POSTING_READ(GTIER);
2002036a4a7dSZhenyu Wang 
2003d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
20047fe0b973SKeith Packard 
2005f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
2006f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
2007f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
2008f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
2009f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2010f97108d1SJesse Barnes 	}
2011f97108d1SJesse Barnes 
2012036a4a7dSZhenyu Wang 	return 0;
2013036a4a7dSZhenyu Wang }
2014036a4a7dSZhenyu Wang 
2015f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2016b1f14ad0SJesse Barnes {
2017b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2018b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2019b615b57aSChris Wilson 	u32 display_mask =
2020b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2021b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
2022b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
2023ce99c256SDaniel Vetter 		DE_PLANEA_FLIP_DONE_IVB |
2024ce99c256SDaniel Vetter 		DE_AUX_CHANNEL_A_IVB;
2025b1f14ad0SJesse Barnes 	u32 render_irqs;
2026b1f14ad0SJesse Barnes 
2027b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2028b1f14ad0SJesse Barnes 
2029b1f14ad0SJesse Barnes 	/* should always can generate irq */
2030b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2031b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2032b615b57aSChris Wilson 	I915_WRITE(DEIER,
2033b615b57aSChris Wilson 		   display_mask |
2034b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
2035b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
2036b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
2037b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2038b1f14ad0SJesse Barnes 
203915b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2040b1f14ad0SJesse Barnes 
2041b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2042b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2043b1f14ad0SJesse Barnes 
2044e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
204515b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
2046b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
2047b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2048b1f14ad0SJesse Barnes 
2049d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
20507fe0b973SKeith Packard 
2051b1f14ad0SJesse Barnes 	return 0;
2052b1f14ad0SJesse Barnes }
2053b1f14ad0SJesse Barnes 
20547e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
20557e231dbeSJesse Barnes {
20567e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20577e231dbeSJesse Barnes 	u32 enable_mask;
205831acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
20593bcedbe5SJesse Barnes 	u32 render_irqs;
20607e231dbeSJesse Barnes 	u16 msid;
20617e231dbeSJesse Barnes 
20627e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
206331acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
206431acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
206531acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
20667e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
20677e231dbeSJesse Barnes 
206831acc7f5SJesse Barnes 	/*
206931acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
207031acc7f5SJesse Barnes 	 * toggle them based on usage.
207131acc7f5SJesse Barnes 	 */
207231acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
207331acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
207431acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
20757e231dbeSJesse Barnes 
20767e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
20777e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
20787e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
20797e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
20807e231dbeSJesse Barnes 	msid |= (1<<14);
20817e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
20827e231dbeSJesse Barnes 
208320afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
208420afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
208520afbda2SDaniel Vetter 
20867e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
20877e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
20887e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20897e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
20907e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
20917e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
20927e231dbeSJesse Barnes 
209331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2094515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
209531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
209631acc7f5SJesse Barnes 
20977e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20987e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20997e231dbeSJesse Barnes 
210031acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
210131acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
21023bcedbe5SJesse Barnes 
21033bcedbe5SJesse Barnes 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
21043bcedbe5SJesse Barnes 		GEN6_BLITTER_USER_INTERRUPT;
21053bcedbe5SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
21067e231dbeSJesse Barnes 	POSTING_READ(GTIER);
21077e231dbeSJesse Barnes 
21087e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
21097e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
21107e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
21117e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
21127e231dbeSJesse Barnes #endif
21137e231dbeSJesse Barnes 
21147e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
211520afbda2SDaniel Vetter 
211620afbda2SDaniel Vetter 	return 0;
211720afbda2SDaniel Vetter }
211820afbda2SDaniel Vetter 
211920afbda2SDaniel Vetter static void valleyview_hpd_irq_setup(struct drm_device *dev)
212020afbda2SDaniel Vetter {
212120afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
212220afbda2SDaniel Vetter 	u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
212320afbda2SDaniel Vetter 
21247e231dbeSJesse Barnes 	/* Note HDMI and DP share bits */
212526739f12SDaniel Vetter 	if (dev_priv->hotplug_supported_mask & PORTB_HOTPLUG_INT_STATUS)
212626739f12SDaniel Vetter 		hotplug_en |= PORTB_HOTPLUG_INT_EN;
212726739f12SDaniel Vetter 	if (dev_priv->hotplug_supported_mask & PORTC_HOTPLUG_INT_STATUS)
212826739f12SDaniel Vetter 		hotplug_en |= PORTC_HOTPLUG_INT_EN;
212926739f12SDaniel Vetter 	if (dev_priv->hotplug_supported_mask & PORTD_HOTPLUG_INT_STATUS)
213026739f12SDaniel Vetter 		hotplug_en |= PORTD_HOTPLUG_INT_EN;
2131ae33cdcfSVijay Purushothaman 	if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
21327e231dbeSJesse Barnes 		hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2133ae33cdcfSVijay Purushothaman 	if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
21347e231dbeSJesse Barnes 		hotplug_en |= SDVOB_HOTPLUG_INT_EN;
21357e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
21367e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_INT_EN;
21377e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
21387e231dbeSJesse Barnes 	}
21397e231dbeSJesse Barnes 
21407e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
21417e231dbeSJesse Barnes }
21427e231dbeSJesse Barnes 
21437e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
21447e231dbeSJesse Barnes {
21457e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21467e231dbeSJesse Barnes 	int pipe;
21477e231dbeSJesse Barnes 
21487e231dbeSJesse Barnes 	if (!dev_priv)
21497e231dbeSJesse Barnes 		return;
21507e231dbeSJesse Barnes 
21517e231dbeSJesse Barnes 	for_each_pipe(pipe)
21527e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
21537e231dbeSJesse Barnes 
21547e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
21557e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
21567e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
21577e231dbeSJesse Barnes 	for_each_pipe(pipe)
21587e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
21597e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
21607e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
21617e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
21627e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
21637e231dbeSJesse Barnes }
21647e231dbeSJesse Barnes 
2165f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2166036a4a7dSZhenyu Wang {
2167036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21684697995bSJesse Barnes 
21694697995bSJesse Barnes 	if (!dev_priv)
21704697995bSJesse Barnes 		return;
21714697995bSJesse Barnes 
2172036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2173036a4a7dSZhenyu Wang 
2174036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2175036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2176036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2177036a4a7dSZhenyu Wang 
2178036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2179036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2180036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2181192aac1fSKeith Packard 
2182192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2183192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2184192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2185036a4a7dSZhenyu Wang }
2186036a4a7dSZhenyu Wang 
2187c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2188c2798b19SChris Wilson {
2189c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2190c2798b19SChris Wilson 	int pipe;
2191c2798b19SChris Wilson 
2192c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2193c2798b19SChris Wilson 
2194c2798b19SChris Wilson 	for_each_pipe(pipe)
2195c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2196c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2197c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2198c2798b19SChris Wilson 	POSTING_READ16(IER);
2199c2798b19SChris Wilson }
2200c2798b19SChris Wilson 
2201c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2202c2798b19SChris Wilson {
2203c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2204c2798b19SChris Wilson 
2205c2798b19SChris Wilson 	I915_WRITE16(EMR,
2206c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2207c2798b19SChris Wilson 
2208c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2209c2798b19SChris Wilson 	dev_priv->irq_mask =
2210c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2211c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2212c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2213c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2214c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2215c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2216c2798b19SChris Wilson 
2217c2798b19SChris Wilson 	I915_WRITE16(IER,
2218c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2219c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2220c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2221c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2222c2798b19SChris Wilson 	POSTING_READ16(IER);
2223c2798b19SChris Wilson 
2224c2798b19SChris Wilson 	return 0;
2225c2798b19SChris Wilson }
2226c2798b19SChris Wilson 
222790a72f87SVille Syrjälä /*
222890a72f87SVille Syrjälä  * Returns true when a page flip has completed.
222990a72f87SVille Syrjälä  */
223090a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
223190a72f87SVille Syrjälä 			       int pipe, u16 iir)
223290a72f87SVille Syrjälä {
223390a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
223490a72f87SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
223590a72f87SVille Syrjälä 
223690a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
223790a72f87SVille Syrjälä 		return false;
223890a72f87SVille Syrjälä 
223990a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
224090a72f87SVille Syrjälä 		return false;
224190a72f87SVille Syrjälä 
224290a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, pipe);
224390a72f87SVille Syrjälä 
224490a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
224590a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
224690a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
224790a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
224890a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
224990a72f87SVille Syrjälä 	 */
225090a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
225190a72f87SVille Syrjälä 		return false;
225290a72f87SVille Syrjälä 
225390a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
225490a72f87SVille Syrjälä 
225590a72f87SVille Syrjälä 	return true;
225690a72f87SVille Syrjälä }
225790a72f87SVille Syrjälä 
2258ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2259c2798b19SChris Wilson {
2260c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2261c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2262c2798b19SChris Wilson 	u16 iir, new_iir;
2263c2798b19SChris Wilson 	u32 pipe_stats[2];
2264c2798b19SChris Wilson 	unsigned long irqflags;
2265c2798b19SChris Wilson 	int irq_received;
2266c2798b19SChris Wilson 	int pipe;
2267c2798b19SChris Wilson 	u16 flip_mask =
2268c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2269c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2270c2798b19SChris Wilson 
2271c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2272c2798b19SChris Wilson 
2273c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2274c2798b19SChris Wilson 	if (iir == 0)
2275c2798b19SChris Wilson 		return IRQ_NONE;
2276c2798b19SChris Wilson 
2277c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2278c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2279c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2280c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2281c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2282c2798b19SChris Wilson 		 */
2283c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2284c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2285c2798b19SChris Wilson 			i915_handle_error(dev, false);
2286c2798b19SChris Wilson 
2287c2798b19SChris Wilson 		for_each_pipe(pipe) {
2288c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2289c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2290c2798b19SChris Wilson 
2291c2798b19SChris Wilson 			/*
2292c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2293c2798b19SChris Wilson 			 */
2294c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2295c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2296c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2297c2798b19SChris Wilson 							 pipe_name(pipe));
2298c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2299c2798b19SChris Wilson 				irq_received = 1;
2300c2798b19SChris Wilson 			}
2301c2798b19SChris Wilson 		}
2302c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2303c2798b19SChris Wilson 
2304c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2305c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2306c2798b19SChris Wilson 
2307d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2308c2798b19SChris Wilson 
2309c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2310c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2311c2798b19SChris Wilson 
2312c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
231390a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 0, iir))
231490a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2315c2798b19SChris Wilson 
2316c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
231790a72f87SVille Syrjälä 		    i8xx_handle_vblank(dev, 1, iir))
231890a72f87SVille Syrjälä 			flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2319c2798b19SChris Wilson 
2320c2798b19SChris Wilson 		iir = new_iir;
2321c2798b19SChris Wilson 	}
2322c2798b19SChris Wilson 
2323c2798b19SChris Wilson 	return IRQ_HANDLED;
2324c2798b19SChris Wilson }
2325c2798b19SChris Wilson 
2326c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2327c2798b19SChris Wilson {
2328c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2329c2798b19SChris Wilson 	int pipe;
2330c2798b19SChris Wilson 
2331c2798b19SChris Wilson 	for_each_pipe(pipe) {
2332c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2333c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2334c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2335c2798b19SChris Wilson 	}
2336c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2337c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2338c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2339c2798b19SChris Wilson }
2340c2798b19SChris Wilson 
2341a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2342a266c7d5SChris Wilson {
2343a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2344a266c7d5SChris Wilson 	int pipe;
2345a266c7d5SChris Wilson 
2346a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2347a266c7d5SChris Wilson 
2348a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2349a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2350a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2351a266c7d5SChris Wilson 	}
2352a266c7d5SChris Wilson 
235300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2354a266c7d5SChris Wilson 	for_each_pipe(pipe)
2355a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2356a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2357a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2358a266c7d5SChris Wilson 	POSTING_READ(IER);
2359a266c7d5SChris Wilson }
2360a266c7d5SChris Wilson 
2361a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2362a266c7d5SChris Wilson {
2363a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
236438bde180SChris Wilson 	u32 enable_mask;
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 
241126739f12SDaniel Vetter 		if (dev_priv->hotplug_supported_mask & PORTB_HOTPLUG_INT_STATUS)
241226739f12SDaniel Vetter 			hotplug_en |= PORTB_HOTPLUG_INT_EN;
241326739f12SDaniel Vetter 		if (dev_priv->hotplug_supported_mask & PORTC_HOTPLUG_INT_STATUS)
241426739f12SDaniel Vetter 			hotplug_en |= PORTC_HOTPLUG_INT_EN;
241526739f12SDaniel Vetter 		if (dev_priv->hotplug_supported_mask & PORTD_HOTPLUG_INT_STATUS)
241626739f12SDaniel 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 
243290a72f87SVille Syrjälä /*
243390a72f87SVille Syrjälä  * Returns true when a page flip has completed.
243490a72f87SVille Syrjälä  */
243590a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
243690a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
243790a72f87SVille Syrjälä {
243890a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
243990a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
244090a72f87SVille Syrjälä 
244190a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
244290a72f87SVille Syrjälä 		return false;
244390a72f87SVille Syrjälä 
244490a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
244590a72f87SVille Syrjälä 		return false;
244690a72f87SVille Syrjälä 
244790a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
244890a72f87SVille Syrjälä 
244990a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
245090a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
245190a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
245290a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
245390a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
245490a72f87SVille Syrjälä 	 */
245590a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
245690a72f87SVille Syrjälä 		return false;
245790a72f87SVille Syrjälä 
245890a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
245990a72f87SVille Syrjälä 
246090a72f87SVille Syrjälä 	return true;
246190a72f87SVille Syrjälä }
246290a72f87SVille Syrjälä 
2463ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
2464a266c7d5SChris Wilson {
2465a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2466a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24678291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2468a266c7d5SChris Wilson 	unsigned long irqflags;
246938bde180SChris Wilson 	u32 flip_mask =
247038bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
247138bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
247238bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2473a266c7d5SChris Wilson 
2474a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2475a266c7d5SChris Wilson 
2476a266c7d5SChris Wilson 	iir = I915_READ(IIR);
247738bde180SChris Wilson 	do {
247838bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
24798291ee90SChris Wilson 		bool blc_event = false;
2480a266c7d5SChris Wilson 
2481a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2482a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2483a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2484a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2485a266c7d5SChris Wilson 		 */
2486a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2487a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2488a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2489a266c7d5SChris Wilson 
2490a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2491a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2492a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2493a266c7d5SChris Wilson 
249438bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2495a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2496a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2497a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2498a266c7d5SChris Wilson 							 pipe_name(pipe));
2499a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
250038bde180SChris Wilson 				irq_received = true;
2501a266c7d5SChris Wilson 			}
2502a266c7d5SChris Wilson 		}
2503a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2504a266c7d5SChris Wilson 
2505a266c7d5SChris Wilson 		if (!irq_received)
2506a266c7d5SChris Wilson 			break;
2507a266c7d5SChris Wilson 
2508a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2509a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2510a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2511a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2512a266c7d5SChris Wilson 
2513a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2514a266c7d5SChris Wilson 				  hotplug_status);
2515a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2516a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2517a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2518a266c7d5SChris Wilson 
2519a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
252038bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2521a266c7d5SChris Wilson 		}
2522a266c7d5SChris Wilson 
252338bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2524a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2525a266c7d5SChris Wilson 
2526a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2527a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2528a266c7d5SChris Wilson 
2529a266c7d5SChris Wilson 		for_each_pipe(pipe) {
253038bde180SChris Wilson 			int plane = pipe;
253138bde180SChris Wilson 			if (IS_MOBILE(dev))
253238bde180SChris Wilson 				plane = !plane;
25335e2032d4SVille Syrjälä 
253490a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
253590a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
253690a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
2537a266c7d5SChris Wilson 
2538a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2539a266c7d5SChris Wilson 				blc_event = true;
2540a266c7d5SChris Wilson 		}
2541a266c7d5SChris Wilson 
2542a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2543a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2544a266c7d5SChris Wilson 
2545a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2546a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2547a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2548a266c7d5SChris Wilson 		 * we would never get another interrupt.
2549a266c7d5SChris Wilson 		 *
2550a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2551a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2552a266c7d5SChris Wilson 		 * another one.
2553a266c7d5SChris Wilson 		 *
2554a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2555a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2556a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2557a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2558a266c7d5SChris Wilson 		 * stray interrupts.
2559a266c7d5SChris Wilson 		 */
256038bde180SChris Wilson 		ret = IRQ_HANDLED;
2561a266c7d5SChris Wilson 		iir = new_iir;
256238bde180SChris Wilson 	} while (iir & ~flip_mask);
2563a266c7d5SChris Wilson 
2564d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
25658291ee90SChris Wilson 
2566a266c7d5SChris Wilson 	return ret;
2567a266c7d5SChris Wilson }
2568a266c7d5SChris Wilson 
2569a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2570a266c7d5SChris Wilson {
2571a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2572a266c7d5SChris Wilson 	int pipe;
2573a266c7d5SChris Wilson 
2574a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2575a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2576a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2577a266c7d5SChris Wilson 	}
2578a266c7d5SChris Wilson 
257900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
258055b39755SChris Wilson 	for_each_pipe(pipe) {
258155b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2582a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
258355b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
258455b39755SChris Wilson 	}
2585a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2586a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2587a266c7d5SChris Wilson 
2588a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2589a266c7d5SChris Wilson }
2590a266c7d5SChris Wilson 
2591a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2592a266c7d5SChris Wilson {
2593a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2594a266c7d5SChris Wilson 	int pipe;
2595a266c7d5SChris Wilson 
2596a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2597a266c7d5SChris Wilson 
2598a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2599a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2600a266c7d5SChris Wilson 
2601a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2602a266c7d5SChris Wilson 	for_each_pipe(pipe)
2603a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2604a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2605a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2606a266c7d5SChris Wilson 	POSTING_READ(IER);
2607a266c7d5SChris Wilson }
2608a266c7d5SChris Wilson 
2609a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2610a266c7d5SChris Wilson {
2611a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2612bbba0a97SChris Wilson 	u32 enable_mask;
2613a266c7d5SChris Wilson 	u32 error_mask;
2614a266c7d5SChris Wilson 
2615a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2616bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2617adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2618bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2619bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2620bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2621bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2622bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2623bbba0a97SChris Wilson 
2624bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
262521ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
262621ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
2627bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2628bbba0a97SChris Wilson 
2629bbba0a97SChris Wilson 	if (IS_G4X(dev))
2630bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2631a266c7d5SChris Wilson 
2632515ac2bbSDaniel Vetter 	i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
2633a266c7d5SChris Wilson 
2634a266c7d5SChris Wilson 	/*
2635a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2636a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2637a266c7d5SChris Wilson 	 */
2638a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2639a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2640a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2641a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2642a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2643a266c7d5SChris Wilson 	} else {
2644a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2645a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2646a266c7d5SChris Wilson 	}
2647a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2648a266c7d5SChris Wilson 
2649a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2650a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2651a266c7d5SChris Wilson 	POSTING_READ(IER);
2652a266c7d5SChris Wilson 
265320afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
265420afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
265520afbda2SDaniel Vetter 
265620afbda2SDaniel Vetter 	intel_opregion_enable_asle(dev);
265720afbda2SDaniel Vetter 
265820afbda2SDaniel Vetter 	return 0;
265920afbda2SDaniel Vetter }
266020afbda2SDaniel Vetter 
266120afbda2SDaniel Vetter static void i965_hpd_irq_setup(struct drm_device *dev)
266220afbda2SDaniel Vetter {
266320afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
266420afbda2SDaniel Vetter 	u32 hotplug_en;
266520afbda2SDaniel Vetter 
2666adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
2667adca4730SChris Wilson 	hotplug_en = 0;
266826739f12SDaniel Vetter 	if (dev_priv->hotplug_supported_mask & PORTB_HOTPLUG_INT_STATUS)
266926739f12SDaniel Vetter 		hotplug_en |= PORTB_HOTPLUG_INT_EN;
267026739f12SDaniel Vetter 	if (dev_priv->hotplug_supported_mask & PORTC_HOTPLUG_INT_STATUS)
267126739f12SDaniel Vetter 		hotplug_en |= PORTC_HOTPLUG_INT_EN;
267226739f12SDaniel Vetter 	if (dev_priv->hotplug_supported_mask & PORTD_HOTPLUG_INT_STATUS)
267326739f12SDaniel Vetter 		hotplug_en |= PORTD_HOTPLUG_INT_EN;
2674084b612eSChris Wilson 	if (IS_G4X(dev)) {
2675084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_G4X)
2676a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2677084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_G4X)
2678a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2679084b612eSChris Wilson 	} else {
2680084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I965)
2681084b612eSChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2682084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I965)
2683084b612eSChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2684084b612eSChris Wilson 	}
2685a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2686a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_INT_EN;
2687a266c7d5SChris Wilson 
2688a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2689a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2690a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2691a266c7d5SChris Wilson 		   */
2692a266c7d5SChris Wilson 		if (IS_G4X(dev))
2693a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2694a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2695a266c7d5SChris Wilson 	}
2696a266c7d5SChris Wilson 
2697a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
2698a266c7d5SChris Wilson 
2699a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2700a266c7d5SChris Wilson }
2701a266c7d5SChris Wilson 
2702ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
2703a266c7d5SChris Wilson {
2704a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2705a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2706a266c7d5SChris Wilson 	u32 iir, new_iir;
2707a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2708a266c7d5SChris Wilson 	unsigned long irqflags;
2709a266c7d5SChris Wilson 	int irq_received;
2710a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
271121ad8330SVille Syrjälä 	u32 flip_mask =
271221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
271321ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2714a266c7d5SChris Wilson 
2715a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2716a266c7d5SChris Wilson 
2717a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2718a266c7d5SChris Wilson 
2719a266c7d5SChris Wilson 	for (;;) {
27202c8ba29fSChris Wilson 		bool blc_event = false;
27212c8ba29fSChris Wilson 
272221ad8330SVille Syrjälä 		irq_received = (iir & ~flip_mask) != 0;
2723a266c7d5SChris Wilson 
2724a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2725a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2726a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2727a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2728a266c7d5SChris Wilson 		 */
2729a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2730a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2731a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2732a266c7d5SChris Wilson 
2733a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2734a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2735a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2736a266c7d5SChris Wilson 
2737a266c7d5SChris Wilson 			/*
2738a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2739a266c7d5SChris Wilson 			 */
2740a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2741a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2742a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2743a266c7d5SChris Wilson 							 pipe_name(pipe));
2744a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2745a266c7d5SChris Wilson 				irq_received = 1;
2746a266c7d5SChris Wilson 			}
2747a266c7d5SChris Wilson 		}
2748a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2749a266c7d5SChris Wilson 
2750a266c7d5SChris Wilson 		if (!irq_received)
2751a266c7d5SChris Wilson 			break;
2752a266c7d5SChris Wilson 
2753a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2754a266c7d5SChris Wilson 
2755a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2756adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2757a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2758a266c7d5SChris Wilson 
2759a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2760a266c7d5SChris Wilson 				  hotplug_status);
2761a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2762a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2763a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2764a266c7d5SChris Wilson 
2765a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2766a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2767a266c7d5SChris Wilson 		}
2768a266c7d5SChris Wilson 
276921ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
2770a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2771a266c7d5SChris Wilson 
2772a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2773a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2774a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2775a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2776a266c7d5SChris Wilson 
2777a266c7d5SChris Wilson 		for_each_pipe(pipe) {
27782c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
277990a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
278090a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
2781a266c7d5SChris Wilson 
2782a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2783a266c7d5SChris Wilson 				blc_event = true;
2784a266c7d5SChris Wilson 		}
2785a266c7d5SChris Wilson 
2786a266c7d5SChris Wilson 
2787a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2788a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2789a266c7d5SChris Wilson 
2790515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
2791515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
2792515ac2bbSDaniel Vetter 
2793a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2794a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2795a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2796a266c7d5SChris Wilson 		 * we would never get another interrupt.
2797a266c7d5SChris Wilson 		 *
2798a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2799a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2800a266c7d5SChris Wilson 		 * another one.
2801a266c7d5SChris Wilson 		 *
2802a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2803a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2804a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2805a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2806a266c7d5SChris Wilson 		 * stray interrupts.
2807a266c7d5SChris Wilson 		 */
2808a266c7d5SChris Wilson 		iir = new_iir;
2809a266c7d5SChris Wilson 	}
2810a266c7d5SChris Wilson 
2811d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
28122c8ba29fSChris Wilson 
2813a266c7d5SChris Wilson 	return ret;
2814a266c7d5SChris Wilson }
2815a266c7d5SChris Wilson 
2816a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
2817a266c7d5SChris Wilson {
2818a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2819a266c7d5SChris Wilson 	int pipe;
2820a266c7d5SChris Wilson 
2821a266c7d5SChris Wilson 	if (!dev_priv)
2822a266c7d5SChris Wilson 		return;
2823a266c7d5SChris Wilson 
2824a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2825a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2826a266c7d5SChris Wilson 
2827a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
2828a266c7d5SChris Wilson 	for_each_pipe(pipe)
2829a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2830a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2831a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2832a266c7d5SChris Wilson 
2833a266c7d5SChris Wilson 	for_each_pipe(pipe)
2834a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
2835a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2836a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2837a266c7d5SChris Wilson }
2838a266c7d5SChris Wilson 
2839f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2840f71d4af4SJesse Barnes {
28418b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28428b2e326dSChris Wilson 
28438b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
284499584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
2845c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
2846a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
28478b2e326dSChris Wilson 
284899584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
284999584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
285061bac78eSDaniel Vetter 		    (unsigned long) dev);
285161bac78eSDaniel Vetter 
285297a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
28539ee32feaSDaniel Vetter 
2854f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2855f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
28567d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
2857f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2858f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2859f71d4af4SJesse Barnes 	}
2860f71d4af4SJesse Barnes 
2861c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2862f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2863c3613de9SKeith Packard 	else
2864c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2865f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2866f71d4af4SJesse Barnes 
28677e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
28687e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
28697e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
28707e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
28717e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
28727e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
28737e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
287420afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup = valleyview_hpd_irq_setup;
28754a06e201SDaniel Vetter 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2876f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2877f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2878f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2879f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2880f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2881f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2882f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
2883f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2884f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2885f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2886f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2887f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2888f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2889f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
2890f71d4af4SJesse Barnes 	} else {
2891c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
2892c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
2893c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
2894c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
2895c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
2896a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
2897a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
2898a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
2899a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
2900a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
290120afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
2902c2798b19SChris Wilson 		} else {
2903a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
2904a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
2905a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
2906a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
290720afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i965_hpd_irq_setup;
2908c2798b19SChris Wilson 		}
2909f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
2910f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
2911f71d4af4SJesse Barnes 	}
2912f71d4af4SJesse Barnes }
291320afbda2SDaniel Vetter 
291420afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
291520afbda2SDaniel Vetter {
291620afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
291720afbda2SDaniel Vetter 
291820afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
291920afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
292020afbda2SDaniel Vetter }
2921