xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision ae33cdcfc6aba86505f7a8ec288e3e1f7277de62)
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>
33c0e09200SDave Airlie #include "drmP.h"
34c0e09200SDave Airlie #include "drm.h"
35c0e09200SDave Airlie #include "i915_drm.h"
36c0e09200SDave Airlie #include "i915_drv.h"
371c5d22f7SChris Wilson #include "i915_trace.h"
3879e53945SJesse Barnes #include "intel_drv.h"
39c0e09200SDave Airlie 
40036a4a7dSZhenyu Wang /* For display hotplug interrupt */
41995b6762SChris Wilson static void
42f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
43036a4a7dSZhenyu Wang {
441ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
451ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
461ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
473143a2bfSChris Wilson 		POSTING_READ(DEIMR);
48036a4a7dSZhenyu Wang 	}
49036a4a7dSZhenyu Wang }
50036a4a7dSZhenyu Wang 
51036a4a7dSZhenyu Wang static inline void
52f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
53036a4a7dSZhenyu Wang {
541ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
551ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
561ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
573143a2bfSChris Wilson 		POSTING_READ(DEIMR);
58036a4a7dSZhenyu Wang 	}
59036a4a7dSZhenyu Wang }
60036a4a7dSZhenyu Wang 
617c463586SKeith Packard void
627c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
637c463586SKeith Packard {
647c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
659db4a9c7SJesse Barnes 		u32 reg = PIPESTAT(pipe);
667c463586SKeith Packard 
677c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
687c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
697c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
703143a2bfSChris Wilson 		POSTING_READ(reg);
717c463586SKeith Packard 	}
727c463586SKeith Packard }
737c463586SKeith Packard 
747c463586SKeith Packard void
757c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
767c463586SKeith Packard {
777c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
789db4a9c7SJesse Barnes 		u32 reg = PIPESTAT(pipe);
797c463586SKeith Packard 
807c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
817c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
823143a2bfSChris Wilson 		POSTING_READ(reg);
837c463586SKeith Packard 	}
847c463586SKeith Packard }
857c463586SKeith Packard 
86c0e09200SDave Airlie /**
8701c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
8801c66889SZhao Yakui  */
8901c66889SZhao Yakui void intel_enable_asle(struct drm_device *dev)
9001c66889SZhao Yakui {
911ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
921ec14ad3SChris Wilson 	unsigned long irqflags;
931ec14ad3SChris Wilson 
947e231dbeSJesse Barnes 	/* FIXME: opregion/asle for VLV */
957e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev))
967e231dbeSJesse Barnes 		return;
977e231dbeSJesse Barnes 
981ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
9901c66889SZhao Yakui 
100c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
101f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
102edcb49caSZhao Yakui 	else {
10301c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
104d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
105a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen >= 4)
106edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
107d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
108edcb49caSZhao Yakui 	}
1091ec14ad3SChris Wilson 
1101ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
11101c66889SZhao Yakui }
11201c66889SZhao Yakui 
11301c66889SZhao Yakui /**
1140a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1150a3e67a4SJesse Barnes  * @dev: DRM device
1160a3e67a4SJesse Barnes  * @pipe: pipe to check
1170a3e67a4SJesse Barnes  *
1180a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1190a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1200a3e67a4SJesse Barnes  * before reading such registers if unsure.
1210a3e67a4SJesse Barnes  */
1220a3e67a4SJesse Barnes static int
1230a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1240a3e67a4SJesse Barnes {
1250a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1265eddb70bSChris Wilson 	return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
1270a3e67a4SJesse Barnes }
1280a3e67a4SJesse Barnes 
12942f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
13042f52ef8SKeith Packard  * we use as a pipe index
13142f52ef8SKeith Packard  */
132f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1330a3e67a4SJesse Barnes {
1340a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1350a3e67a4SJesse Barnes 	unsigned long high_frame;
1360a3e67a4SJesse Barnes 	unsigned long low_frame;
1375eddb70bSChris Wilson 	u32 high1, high2, low;
1380a3e67a4SJesse Barnes 
1390a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
14044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
1419db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
1420a3e67a4SJesse Barnes 		return 0;
1430a3e67a4SJesse Barnes 	}
1440a3e67a4SJesse Barnes 
1459db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
1469db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
1475eddb70bSChris Wilson 
1480a3e67a4SJesse Barnes 	/*
1490a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
1500a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
1510a3e67a4SJesse Barnes 	 * register.
1520a3e67a4SJesse Barnes 	 */
1530a3e67a4SJesse Barnes 	do {
1545eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1555eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
1565eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1570a3e67a4SJesse Barnes 	} while (high1 != high2);
1580a3e67a4SJesse Barnes 
1595eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
1605eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
1615eddb70bSChris Wilson 	return (high1 << 8) | low;
1620a3e67a4SJesse Barnes }
1630a3e67a4SJesse Barnes 
164f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
1659880b7a5SJesse Barnes {
1669880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1679db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
1689880b7a5SJesse Barnes 
1699880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
17044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
1719db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
1729880b7a5SJesse Barnes 		return 0;
1739880b7a5SJesse Barnes 	}
1749880b7a5SJesse Barnes 
1759880b7a5SJesse Barnes 	return I915_READ(reg);
1769880b7a5SJesse Barnes }
1779880b7a5SJesse Barnes 
178f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
1790af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
1800af7e4dfSMario Kleiner {
1810af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1820af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
1830af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
1840af7e4dfSMario Kleiner 	bool in_vbl = true;
1850af7e4dfSMario Kleiner 	int ret = 0;
1860af7e4dfSMario Kleiner 
1870af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
1880af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
1899db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
1900af7e4dfSMario Kleiner 		return 0;
1910af7e4dfSMario Kleiner 	}
1920af7e4dfSMario Kleiner 
1930af7e4dfSMario Kleiner 	/* Get vtotal. */
1940af7e4dfSMario Kleiner 	vtotal = 1 + ((I915_READ(VTOTAL(pipe)) >> 16) & 0x1fff);
1950af7e4dfSMario Kleiner 
1960af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
1970af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
1980af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
1990af7e4dfSMario Kleiner 		 */
2000af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
2010af7e4dfSMario Kleiner 
2020af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
2030af7e4dfSMario Kleiner 		 * horizontal scanout position.
2040af7e4dfSMario Kleiner 		 */
2050af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
2060af7e4dfSMario Kleiner 		*hpos = 0;
2070af7e4dfSMario Kleiner 	} else {
2080af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
2090af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
2100af7e4dfSMario Kleiner 		 * scanout position.
2110af7e4dfSMario Kleiner 		 */
2120af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
2130af7e4dfSMario Kleiner 
2140af7e4dfSMario Kleiner 		htotal = 1 + ((I915_READ(HTOTAL(pipe)) >> 16) & 0x1fff);
2150af7e4dfSMario Kleiner 		*vpos = position / htotal;
2160af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
2170af7e4dfSMario Kleiner 	}
2180af7e4dfSMario Kleiner 
2190af7e4dfSMario Kleiner 	/* Query vblank area. */
2200af7e4dfSMario Kleiner 	vbl = I915_READ(VBLANK(pipe));
2210af7e4dfSMario Kleiner 
2220af7e4dfSMario Kleiner 	/* Test position against vblank region. */
2230af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
2240af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
2250af7e4dfSMario Kleiner 
2260af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
2270af7e4dfSMario Kleiner 		in_vbl = false;
2280af7e4dfSMario Kleiner 
2290af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
2300af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
2310af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
2320af7e4dfSMario Kleiner 
2330af7e4dfSMario Kleiner 	/* Readouts valid? */
2340af7e4dfSMario Kleiner 	if (vbl > 0)
2350af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
2360af7e4dfSMario Kleiner 
2370af7e4dfSMario Kleiner 	/* In vblank? */
2380af7e4dfSMario Kleiner 	if (in_vbl)
2390af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
2400af7e4dfSMario Kleiner 
2410af7e4dfSMario Kleiner 	return ret;
2420af7e4dfSMario Kleiner }
2430af7e4dfSMario Kleiner 
244f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
2450af7e4dfSMario Kleiner 			      int *max_error,
2460af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
2470af7e4dfSMario Kleiner 			      unsigned flags)
2480af7e4dfSMario Kleiner {
2494041b853SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2504041b853SChris Wilson 	struct drm_crtc *crtc;
2510af7e4dfSMario Kleiner 
2524041b853SChris Wilson 	if (pipe < 0 || pipe >= dev_priv->num_pipe) {
2534041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2540af7e4dfSMario Kleiner 		return -EINVAL;
2550af7e4dfSMario Kleiner 	}
2560af7e4dfSMario Kleiner 
2570af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
2584041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
2594041b853SChris Wilson 	if (crtc == NULL) {
2604041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2614041b853SChris Wilson 		return -EINVAL;
2624041b853SChris Wilson 	}
2634041b853SChris Wilson 
2644041b853SChris Wilson 	if (!crtc->enabled) {
2654041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
2664041b853SChris Wilson 		return -EBUSY;
2674041b853SChris Wilson 	}
2680af7e4dfSMario Kleiner 
2690af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
2704041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
2714041b853SChris Wilson 						     vblank_time, flags,
2724041b853SChris Wilson 						     crtc);
2730af7e4dfSMario Kleiner }
2740af7e4dfSMario Kleiner 
2755ca58282SJesse Barnes /*
2765ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
2775ca58282SJesse Barnes  */
2785ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
2795ca58282SJesse Barnes {
2805ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
2815ca58282SJesse Barnes 						    hotplug_work);
2825ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
283c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
2844ef69c7aSChris Wilson 	struct intel_encoder *encoder;
2855ca58282SJesse Barnes 
286a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
287e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
288e67189abSJesse Barnes 
2894ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
2904ef69c7aSChris Wilson 		if (encoder->hot_plug)
2914ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
292c31c4ba3SKeith Packard 
29340ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
29440ee3381SKeith Packard 
2955ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
296eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
2975ca58282SJesse Barnes }
2985ca58282SJesse Barnes 
2999270388eSDaniel Vetter /* defined intel_pm.c */
3009270388eSDaniel Vetter extern spinlock_t mchdev_lock;
3019270388eSDaniel Vetter 
30273edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
303f97108d1SJesse Barnes {
304f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
305b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
3069270388eSDaniel Vetter 	u8 new_delay;
3079270388eSDaniel Vetter 	unsigned long flags;
3089270388eSDaniel Vetter 
3099270388eSDaniel Vetter 	spin_lock_irqsave(&mchdev_lock, flags);
310f97108d1SJesse Barnes 
31173edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
31273edd18fSDaniel Vetter 
31320e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
3149270388eSDaniel Vetter 
3157648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
316b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
317b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
318f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
319f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
320f97108d1SJesse Barnes 
321f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
322b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
32320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
32420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
32520e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
32620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
327b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
32820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
32920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
33020e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
33120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
332f97108d1SJesse Barnes 	}
333f97108d1SJesse Barnes 
3347648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
33520e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
336f97108d1SJesse Barnes 
3379270388eSDaniel Vetter 	spin_unlock_irqrestore(&mchdev_lock, flags);
3389270388eSDaniel Vetter 
339f97108d1SJesse Barnes 	return;
340f97108d1SJesse Barnes }
341f97108d1SJesse Barnes 
342549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
343549f7365SChris Wilson 			struct intel_ring_buffer *ring)
344549f7365SChris Wilson {
345549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
3469862e600SChris Wilson 
347475553deSChris Wilson 	if (ring->obj == NULL)
348475553deSChris Wilson 		return;
349475553deSChris Wilson 
350b2eadbc8SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
3519862e600SChris Wilson 
352549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3533e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
354549f7365SChris Wilson 		dev_priv->hangcheck_count = 0;
355549f7365SChris Wilson 		mod_timer(&dev_priv->hangcheck_timer,
3563e0dc6b0SBen Widawsky 			  jiffies +
3573e0dc6b0SBen Widawsky 			  msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
3583e0dc6b0SBen Widawsky 	}
359549f7365SChris Wilson }
360549f7365SChris Wilson 
3614912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
3623b8d8d91SJesse Barnes {
3634912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
364c6a828d3SDaniel Vetter 						    rps.work);
3654912d041SBen Widawsky 	u32 pm_iir, pm_imr;
3667b9e0ae6SChris Wilson 	u8 new_delay;
3673b8d8d91SJesse Barnes 
368c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
369c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
370c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
3714912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
372a9e2641dSDaniel Vetter 	I915_WRITE(GEN6_PMIMR, 0);
373c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
3744912d041SBen Widawsky 
3757b9e0ae6SChris Wilson 	if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
3763b8d8d91SJesse Barnes 		return;
3773b8d8d91SJesse Barnes 
3784912d041SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
3797b9e0ae6SChris Wilson 
3807b9e0ae6SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
381c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
3827b9e0ae6SChris Wilson 	else
383c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
3843b8d8d91SJesse Barnes 
38579249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
38679249636SBen Widawsky 	 * interrupt
38779249636SBen Widawsky 	 */
38879249636SBen Widawsky 	if (!(new_delay > dev_priv->rps.max_delay ||
38979249636SBen Widawsky 	      new_delay < dev_priv->rps.min_delay)) {
3904912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
39179249636SBen Widawsky 	}
3923b8d8d91SJesse Barnes 
3934912d041SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
3943b8d8d91SJesse Barnes }
3953b8d8d91SJesse Barnes 
396e3689190SBen Widawsky 
397e3689190SBen Widawsky /**
398e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
399e3689190SBen Widawsky  * occurred.
400e3689190SBen Widawsky  * @work: workqueue struct
401e3689190SBen Widawsky  *
402e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
403e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
404e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
405e3689190SBen Widawsky  */
406e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
407e3689190SBen Widawsky {
408e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
409e3689190SBen Widawsky 						    parity_error_work);
410e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
411e3689190SBen Widawsky 	char *parity_event[5];
412e3689190SBen Widawsky 	uint32_t misccpctl;
413e3689190SBen Widawsky 	unsigned long flags;
414e3689190SBen Widawsky 
415e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
416e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
417e3689190SBen Widawsky 	 * any time we access those registers.
418e3689190SBen Widawsky 	 */
419e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
420e3689190SBen Widawsky 
421e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
422e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
423e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
424e3689190SBen Widawsky 
425e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
426e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
427e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
428e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
429e3689190SBen Widawsky 
430e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
431e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
432e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
433e3689190SBen Widawsky 
434e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
435e3689190SBen Widawsky 
436e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
437e3689190SBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
438e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
439e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
440e3689190SBen Widawsky 
441e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
442e3689190SBen Widawsky 
443e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
444e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
445e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
446e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
447e3689190SBen Widawsky 	parity_event[4] = NULL;
448e3689190SBen Widawsky 
449e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
450e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
451e3689190SBen Widawsky 
452e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
453e3689190SBen Widawsky 		  row, bank, subbank);
454e3689190SBen Widawsky 
455e3689190SBen Widawsky 	kfree(parity_event[3]);
456e3689190SBen Widawsky 	kfree(parity_event[2]);
457e3689190SBen Widawsky 	kfree(parity_event[1]);
458e3689190SBen Widawsky }
459e3689190SBen Widawsky 
460d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
461e3689190SBen Widawsky {
462e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
463e3689190SBen Widawsky 	unsigned long flags;
464e3689190SBen Widawsky 
465e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
466e3689190SBen Widawsky 		return;
467e3689190SBen Widawsky 
468e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
469e3689190SBen Widawsky 	dev_priv->gt_irq_mask |= GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
470e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
471e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
472e3689190SBen Widawsky 
473e3689190SBen Widawsky 	queue_work(dev_priv->wq, &dev_priv->parity_error_work);
474e3689190SBen Widawsky }
475e3689190SBen Widawsky 
476e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
477e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
478e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
479e7b4c6b1SDaniel Vetter {
480e7b4c6b1SDaniel Vetter 
481e7b4c6b1SDaniel Vetter 	if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
482e7b4c6b1SDaniel Vetter 		      GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
483e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
484e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BSD_USER_INTERRUPT)
485e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
486e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
487e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
488e7b4c6b1SDaniel Vetter 
489e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
490e7b4c6b1SDaniel Vetter 		      GT_GEN6_BSD_CS_ERROR_INTERRUPT |
491e7b4c6b1SDaniel Vetter 		      GT_RENDER_CS_ERROR_INTERRUPT)) {
492e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
493e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
494e7b4c6b1SDaniel Vetter 	}
495e3689190SBen Widawsky 
496e3689190SBen Widawsky 	if (gt_iir & GT_GEN7_L3_PARITY_ERROR_INTERRUPT)
497e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
498e7b4c6b1SDaniel Vetter }
499e7b4c6b1SDaniel Vetter 
500fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
501fc6826d1SChris Wilson 				u32 pm_iir)
502fc6826d1SChris Wilson {
503fc6826d1SChris Wilson 	unsigned long flags;
504fc6826d1SChris Wilson 
505fc6826d1SChris Wilson 	/*
506fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
507fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
508fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
509c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
510fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
511fc6826d1SChris Wilson 	 *
512c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
513fc6826d1SChris Wilson 	 */
514fc6826d1SChris Wilson 
515c6a828d3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
516c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
517c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
518fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
519c6a828d3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
520fc6826d1SChris Wilson 
521c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
522fc6826d1SChris Wilson }
523fc6826d1SChris Wilson 
5247e231dbeSJesse Barnes static irqreturn_t valleyview_irq_handler(DRM_IRQ_ARGS)
5257e231dbeSJesse Barnes {
5267e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
5277e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5287e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
5297e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
5307e231dbeSJesse Barnes 	unsigned long irqflags;
5317e231dbeSJesse Barnes 	int pipe;
5327e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
5337e231dbeSJesse Barnes 	bool blc_event;
5347e231dbeSJesse Barnes 
5357e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5367e231dbeSJesse Barnes 
5377e231dbeSJesse Barnes 	while (true) {
5387e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
5397e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
5407e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
5417e231dbeSJesse Barnes 
5427e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
5437e231dbeSJesse Barnes 			goto out;
5447e231dbeSJesse Barnes 
5457e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
5467e231dbeSJesse Barnes 
547e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
5487e231dbeSJesse Barnes 
5497e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5507e231dbeSJesse Barnes 		for_each_pipe(pipe) {
5517e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
5527e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
5537e231dbeSJesse Barnes 
5547e231dbeSJesse Barnes 			/*
5557e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
5567e231dbeSJesse Barnes 			 */
5577e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
5587e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
5597e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
5607e231dbeSJesse Barnes 							 pipe_name(pipe));
5617e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
5627e231dbeSJesse Barnes 			}
5637e231dbeSJesse Barnes 		}
5647e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5657e231dbeSJesse Barnes 
56631acc7f5SJesse Barnes 		for_each_pipe(pipe) {
56731acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
56831acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
56931acc7f5SJesse Barnes 
57031acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
57131acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
57231acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
57331acc7f5SJesse Barnes 			}
57431acc7f5SJesse Barnes 		}
57531acc7f5SJesse Barnes 
5767e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
5777e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
5787e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
5797e231dbeSJesse Barnes 
5807e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
5817e231dbeSJesse Barnes 					 hotplug_status);
5827e231dbeSJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
5837e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
5847e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
5857e231dbeSJesse Barnes 
5867e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
5877e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
5887e231dbeSJesse Barnes 		}
5897e231dbeSJesse Barnes 
5907e231dbeSJesse Barnes 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
5917e231dbeSJesse Barnes 			blc_event = true;
5927e231dbeSJesse Barnes 
593fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
594fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
5957e231dbeSJesse Barnes 
5967e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
5977e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
5987e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
5997e231dbeSJesse Barnes 	}
6007e231dbeSJesse Barnes 
6017e231dbeSJesse Barnes out:
6027e231dbeSJesse Barnes 	return ret;
6037e231dbeSJesse Barnes }
6047e231dbeSJesse Barnes 
60523e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
606776ad806SJesse Barnes {
607776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6089db4a9c7SJesse Barnes 	int pipe;
609776ad806SJesse Barnes 
610776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
611776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
612776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
613776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
614776ad806SJesse Barnes 
615776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
616776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
617776ad806SJesse Barnes 
618776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
619776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
620776ad806SJesse Barnes 
621776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
622776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
623776ad806SJesse Barnes 
624776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
625776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
626776ad806SJesse Barnes 
6279db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
6289db4a9c7SJesse Barnes 		for_each_pipe(pipe)
6299db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
6309db4a9c7SJesse Barnes 					 pipe_name(pipe),
6319db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
632776ad806SJesse Barnes 
633776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
634776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
635776ad806SJesse Barnes 
636776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
637776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
638776ad806SJesse Barnes 
639776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
640776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
641776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
642776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
643776ad806SJesse Barnes }
644776ad806SJesse Barnes 
64523e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
64623e81d69SAdam Jackson {
64723e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
64823e81d69SAdam Jackson 	int pipe;
64923e81d69SAdam Jackson 
65023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT)
65123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
65223e81d69SAdam Jackson 				 (pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
65323e81d69SAdam Jackson 				 SDE_AUDIO_POWER_SHIFT_CPT);
65423e81d69SAdam Jackson 
65523e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
65623e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("AUX channel interrupt\n");
65723e81d69SAdam Jackson 
65823e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
65923e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
66023e81d69SAdam Jackson 
66123e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
66223e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
66323e81d69SAdam Jackson 
66423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
66523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
66623e81d69SAdam Jackson 
66723e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
66823e81d69SAdam Jackson 		for_each_pipe(pipe)
66923e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
67023e81d69SAdam Jackson 					 pipe_name(pipe),
67123e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
67223e81d69SAdam Jackson }
67323e81d69SAdam Jackson 
674f71d4af4SJesse Barnes static irqreturn_t ivybridge_irq_handler(DRM_IRQ_ARGS)
675b1f14ad0SJesse Barnes {
676b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
677b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6780e43406bSChris Wilson 	u32 de_iir, gt_iir, de_ier, pm_iir;
6790e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
6800e43406bSChris Wilson 	int i;
681b1f14ad0SJesse Barnes 
682b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
683b1f14ad0SJesse Barnes 
684b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
685b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
686b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
6870e43406bSChris Wilson 
6880e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
6890e43406bSChris Wilson 	if (gt_iir) {
6900e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
6910e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
6920e43406bSChris Wilson 		ret = IRQ_HANDLED;
6930e43406bSChris Wilson 	}
694b1f14ad0SJesse Barnes 
695b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
6960e43406bSChris Wilson 	if (de_iir) {
697b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
698b1f14ad0SJesse Barnes 			intel_opregion_gse_intr(dev);
699b1f14ad0SJesse Barnes 
7000e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
7010e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
7020e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
7030e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
704b1f14ad0SJesse Barnes 			}
7050e43406bSChris Wilson 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
7060e43406bSChris Wilson 				drm_handle_vblank(dev, i);
707b1f14ad0SJesse Barnes 		}
708b1f14ad0SJesse Barnes 
709b1f14ad0SJesse Barnes 		/* check event from PCH */
710b1f14ad0SJesse Barnes 		if (de_iir & DE_PCH_EVENT_IVB) {
7110e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
7120e43406bSChris Wilson 
713b1f14ad0SJesse Barnes 			if (pch_iir & SDE_HOTPLUG_MASK_CPT)
714b1f14ad0SJesse Barnes 				queue_work(dev_priv->wq, &dev_priv->hotplug_work);
71523e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
7160e43406bSChris Wilson 
7170e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
7180e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
719b1f14ad0SJesse Barnes 		}
720b1f14ad0SJesse Barnes 
7210e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
7220e43406bSChris Wilson 		ret = IRQ_HANDLED;
7230e43406bSChris Wilson 	}
7240e43406bSChris Wilson 
7250e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
7260e43406bSChris Wilson 	if (pm_iir) {
727fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
728fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
729b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
7300e43406bSChris Wilson 		ret = IRQ_HANDLED;
7310e43406bSChris Wilson 	}
732b1f14ad0SJesse Barnes 
733b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
734b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
735b1f14ad0SJesse Barnes 
736b1f14ad0SJesse Barnes 	return ret;
737b1f14ad0SJesse Barnes }
738b1f14ad0SJesse Barnes 
739e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
740e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
741e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
742e7b4c6b1SDaniel Vetter {
743e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
744e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
745e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
746e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
747e7b4c6b1SDaniel Vetter }
748e7b4c6b1SDaniel Vetter 
749f71d4af4SJesse Barnes static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
750036a4a7dSZhenyu Wang {
7514697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
752036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
753036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
7543b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
7552d7b8366SYuanhan Liu 	u32 hotplug_mask;
756881f47b6SXiang, Haihao 
7574697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
7584697995bSJesse Barnes 
7592d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
7602d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
7612d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
7623143a2bfSChris Wilson 	POSTING_READ(DEIER);
7632d109a84SZou, Nanhai 
764036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
765036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
766c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
7673b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
768036a4a7dSZhenyu Wang 
7693b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
7703b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
771c7c85101SZou Nan hai 		goto done;
772036a4a7dSZhenyu Wang 
7732d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
7742d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
7752d7b8366SYuanhan Liu 	else
7762d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
7772d7b8366SYuanhan Liu 
778036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
779036a4a7dSZhenyu Wang 
780e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
781e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
782e7b4c6b1SDaniel Vetter 	else
783e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
784036a4a7dSZhenyu Wang 
78501c66889SZhao Yakui 	if (de_iir & DE_GSE)
7863b617967SChris Wilson 		intel_opregion_gse_intr(dev);
78701c66889SZhao Yakui 
788f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
789013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
7902bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
791013d5aa2SJesse Barnes 	}
792013d5aa2SJesse Barnes 
793f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
794f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
7952bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
796013d5aa2SJesse Barnes 	}
797c062df61SLi Peng 
798f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
799f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
800f072d2e7SZhenyu Wang 
801f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
802f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
803f072d2e7SZhenyu Wang 
804c650156aSZhenyu Wang 	/* check event from PCH */
805776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
806776ad806SJesse Barnes 		if (pch_iir & hotplug_mask)
807c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
80823e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
80923e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
81023e81d69SAdam Jackson 		else
81123e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
812776ad806SJesse Barnes 	}
813c650156aSZhenyu Wang 
81473edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
81573edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
816f97108d1SJesse Barnes 
817fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
818fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
8193b8d8d91SJesse Barnes 
820c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
821c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
822c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
823c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
8244912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
825036a4a7dSZhenyu Wang 
826c7c85101SZou Nan hai done:
8272d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
8283143a2bfSChris Wilson 	POSTING_READ(DEIER);
8292d109a84SZou, Nanhai 
830036a4a7dSZhenyu Wang 	return ret;
831036a4a7dSZhenyu Wang }
832036a4a7dSZhenyu Wang 
8338a905236SJesse Barnes /**
8348a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
8358a905236SJesse Barnes  * @work: work struct
8368a905236SJesse Barnes  *
8378a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
8388a905236SJesse Barnes  * was detected.
8398a905236SJesse Barnes  */
8408a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
8418a905236SJesse Barnes {
8428a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
8438a905236SJesse Barnes 						    error_work);
8448a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
845f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
846f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
847f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
8488a905236SJesse Barnes 
849f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
8508a905236SJesse Barnes 
851ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
85244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
853f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
854d4b8bb2aSDaniel Vetter 		if (!i915_reset(dev)) {
855ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
856f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
857f316a42cSBen Gamari 		}
85830dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
859f316a42cSBen Gamari 	}
8608a905236SJesse Barnes }
8618a905236SJesse Barnes 
86285f9e50dSDaniel Vetter /* NB: please notice the memset */
86385f9e50dSDaniel Vetter static void i915_get_extra_instdone(struct drm_device *dev,
86485f9e50dSDaniel Vetter 				    uint32_t *instdone)
86585f9e50dSDaniel Vetter {
86685f9e50dSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
86785f9e50dSDaniel Vetter 	memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
86885f9e50dSDaniel Vetter 
86985f9e50dSDaniel Vetter 	switch(INTEL_INFO(dev)->gen) {
87085f9e50dSDaniel Vetter 	case 2:
87185f9e50dSDaniel Vetter 	case 3:
87285f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE);
87385f9e50dSDaniel Vetter 		break;
87485f9e50dSDaniel Vetter 	case 4:
87585f9e50dSDaniel Vetter 	case 5:
87685f9e50dSDaniel Vetter 	case 6:
87785f9e50dSDaniel Vetter 		instdone[0] = I915_READ(INSTDONE_I965);
87885f9e50dSDaniel Vetter 		instdone[1] = I915_READ(INSTDONE1);
87985f9e50dSDaniel Vetter 		break;
88085f9e50dSDaniel Vetter 	default:
88185f9e50dSDaniel Vetter 		WARN_ONCE(1, "Unsupported platform\n");
88285f9e50dSDaniel Vetter 	case 7:
88385f9e50dSDaniel Vetter 		instdone[0] = I915_READ(GEN7_INSTDONE_1);
88485f9e50dSDaniel Vetter 		instdone[1] = I915_READ(GEN7_SC_INSTDONE);
88585f9e50dSDaniel Vetter 		instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
88685f9e50dSDaniel Vetter 		instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
88785f9e50dSDaniel Vetter 		break;
88885f9e50dSDaniel Vetter 	}
88985f9e50dSDaniel Vetter }
89085f9e50dSDaniel Vetter 
8913bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
8929df30794SChris Wilson static struct drm_i915_error_object *
893bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
89405394f39SChris Wilson 			 struct drm_i915_gem_object *src)
8959df30794SChris Wilson {
8969df30794SChris Wilson 	struct drm_i915_error_object *dst;
8979da3da66SChris Wilson 	int i, count;
898e56660ddSChris Wilson 	u32 reloc_offset;
8999df30794SChris Wilson 
90005394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
9019df30794SChris Wilson 		return NULL;
9029df30794SChris Wilson 
9039da3da66SChris Wilson 	count = src->base.size / PAGE_SIZE;
9049df30794SChris Wilson 
9059da3da66SChris Wilson 	dst = kmalloc(sizeof(*dst) + count * sizeof(u32 *), GFP_ATOMIC);
9069df30794SChris Wilson 	if (dst == NULL)
9079df30794SChris Wilson 		return NULL;
9089df30794SChris Wilson 
90905394f39SChris Wilson 	reloc_offset = src->gtt_offset;
9109da3da66SChris Wilson 	for (i = 0; i < count; i++) {
911788885aeSAndrew Morton 		unsigned long flags;
912e56660ddSChris Wilson 		void *d;
913788885aeSAndrew Morton 
914e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
9159df30794SChris Wilson 		if (d == NULL)
9169df30794SChris Wilson 			goto unwind;
917e56660ddSChris Wilson 
918788885aeSAndrew Morton 		local_irq_save(flags);
91974898d7eSDaniel Vetter 		if (reloc_offset < dev_priv->mm.gtt_mappable_end &&
92074898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
921172975aaSChris Wilson 			void __iomem *s;
922172975aaSChris Wilson 
923172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
924172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
925172975aaSChris Wilson 			 * captures what the GPU read.
926172975aaSChris Wilson 			 */
927172975aaSChris Wilson 
928e56660ddSChris Wilson 			s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
9293e4d3af5SPeter Zijlstra 						     reloc_offset);
930e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
9313e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
932172975aaSChris Wilson 		} else {
9339da3da66SChris Wilson 			struct page *page;
934172975aaSChris Wilson 			void *s;
935172975aaSChris Wilson 
9369da3da66SChris Wilson 			page = i915_gem_object_get_page(src, i);
937172975aaSChris Wilson 
9389da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
9399da3da66SChris Wilson 
9409da3da66SChris Wilson 			s = kmap_atomic(page);
941172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
942172975aaSChris Wilson 			kunmap_atomic(s);
943172975aaSChris Wilson 
9449da3da66SChris Wilson 			drm_clflush_pages(&page, 1);
945172975aaSChris Wilson 		}
946788885aeSAndrew Morton 		local_irq_restore(flags);
947e56660ddSChris Wilson 
9489da3da66SChris Wilson 		dst->pages[i] = d;
949e56660ddSChris Wilson 
950e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
9519df30794SChris Wilson 	}
9529da3da66SChris Wilson 	dst->page_count = count;
95305394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
9549df30794SChris Wilson 
9559df30794SChris Wilson 	return dst;
9569df30794SChris Wilson 
9579df30794SChris Wilson unwind:
9589da3da66SChris Wilson 	while (i--)
9599da3da66SChris Wilson 		kfree(dst->pages[i]);
9609df30794SChris Wilson 	kfree(dst);
9619df30794SChris Wilson 	return NULL;
9629df30794SChris Wilson }
9639df30794SChris Wilson 
9649df30794SChris Wilson static void
9659df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
9669df30794SChris Wilson {
9679df30794SChris Wilson 	int page;
9689df30794SChris Wilson 
9699df30794SChris Wilson 	if (obj == NULL)
9709df30794SChris Wilson 		return;
9719df30794SChris Wilson 
9729df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
9739df30794SChris Wilson 		kfree(obj->pages[page]);
9749df30794SChris Wilson 
9759df30794SChris Wilson 	kfree(obj);
9769df30794SChris Wilson }
9779df30794SChris Wilson 
978742cbee8SDaniel Vetter void
979742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
9809df30794SChris Wilson {
981742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
982742cbee8SDaniel Vetter 							  typeof(*error), ref);
983e2f973d5SChris Wilson 	int i;
984e2f973d5SChris Wilson 
98552d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
98652d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
98752d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
98852d39a21SChris Wilson 		kfree(error->ring[i].requests);
98952d39a21SChris Wilson 	}
990e2f973d5SChris Wilson 
9919df30794SChris Wilson 	kfree(error->active_bo);
9926ef3d427SChris Wilson 	kfree(error->overlay);
9939df30794SChris Wilson 	kfree(error);
9949df30794SChris Wilson }
9951b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
9961b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
997c724e8a9SChris Wilson {
998c724e8a9SChris Wilson 	err->size = obj->base.size;
999c724e8a9SChris Wilson 	err->name = obj->base.name;
10000201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
10010201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
1002c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
1003c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
1004c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
1005c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
1006c724e8a9SChris Wilson 	err->pinned = 0;
1007c724e8a9SChris Wilson 	if (obj->pin_count > 0)
1008c724e8a9SChris Wilson 		err->pinned = 1;
1009c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
1010c724e8a9SChris Wilson 		err->pinned = -1;
1011c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
1012c724e8a9SChris Wilson 	err->dirty = obj->dirty;
1013c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
101496154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
101593dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
10161b50247aSChris Wilson }
1017c724e8a9SChris Wilson 
10181b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
10191b50247aSChris Wilson 			     int count, struct list_head *head)
10201b50247aSChris Wilson {
10211b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
10221b50247aSChris Wilson 	int i = 0;
10231b50247aSChris Wilson 
10241b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
10251b50247aSChris Wilson 		capture_bo(err++, obj);
1026c724e8a9SChris Wilson 		if (++i == count)
1027c724e8a9SChris Wilson 			break;
10281b50247aSChris Wilson 	}
1029c724e8a9SChris Wilson 
10301b50247aSChris Wilson 	return i;
10311b50247aSChris Wilson }
10321b50247aSChris Wilson 
10331b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
10341b50247aSChris Wilson 			     int count, struct list_head *head)
10351b50247aSChris Wilson {
10361b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
10371b50247aSChris Wilson 	int i = 0;
10381b50247aSChris Wilson 
10391b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
10401b50247aSChris Wilson 		if (obj->pin_count == 0)
10411b50247aSChris Wilson 			continue;
10421b50247aSChris Wilson 
10431b50247aSChris Wilson 		capture_bo(err++, obj);
10441b50247aSChris Wilson 		if (++i == count)
10451b50247aSChris Wilson 			break;
1046c724e8a9SChris Wilson 	}
1047c724e8a9SChris Wilson 
1048c724e8a9SChris Wilson 	return i;
1049c724e8a9SChris Wilson }
1050c724e8a9SChris Wilson 
1051748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1052748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1053748ebc60SChris Wilson {
1054748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1055748ebc60SChris Wilson 	int i;
1056748ebc60SChris Wilson 
1057748ebc60SChris Wilson 	/* Fences */
1058748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1059775d17b6SDaniel Vetter 	case 7:
1060748ebc60SChris Wilson 	case 6:
1061748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1062748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1063748ebc60SChris Wilson 		break;
1064748ebc60SChris Wilson 	case 5:
1065748ebc60SChris Wilson 	case 4:
1066748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1067748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1068748ebc60SChris Wilson 		break;
1069748ebc60SChris Wilson 	case 3:
1070748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1071748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1072748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1073748ebc60SChris Wilson 	case 2:
1074748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1075748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1076748ebc60SChris Wilson 		break;
1077748ebc60SChris Wilson 
1078748ebc60SChris Wilson 	}
1079748ebc60SChris Wilson }
1080748ebc60SChris Wilson 
1081bcfb2e28SChris Wilson static struct drm_i915_error_object *
1082bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1083bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1084bcfb2e28SChris Wilson {
1085bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1086bcfb2e28SChris Wilson 	u32 seqno;
1087bcfb2e28SChris Wilson 
1088bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1089bcfb2e28SChris Wilson 		return NULL;
1090bcfb2e28SChris Wilson 
1091b2eadbc8SChris Wilson 	seqno = ring->get_seqno(ring, false);
1092bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1093bcfb2e28SChris Wilson 		if (obj->ring != ring)
1094bcfb2e28SChris Wilson 			continue;
1095bcfb2e28SChris Wilson 
10960201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1097bcfb2e28SChris Wilson 			continue;
1098bcfb2e28SChris Wilson 
1099bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1100bcfb2e28SChris Wilson 			continue;
1101bcfb2e28SChris Wilson 
1102bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1103bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1104bcfb2e28SChris Wilson 		 */
1105bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1106bcfb2e28SChris Wilson 	}
1107bcfb2e28SChris Wilson 
1108bcfb2e28SChris Wilson 	return NULL;
1109bcfb2e28SChris Wilson }
1110bcfb2e28SChris Wilson 
1111d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1112d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1113d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1114d27b1e0eSDaniel Vetter {
1115d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1116d27b1e0eSDaniel Vetter 
111733f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
111812f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
111933f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
11207e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
11217e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
11227e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
11237e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
112433f3f518SDaniel Vetter 	}
1125c1cd90edSDaniel Vetter 
1126d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
11279d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1128d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1129d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1130d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1131c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1132050ee91fSBen Widawsky 		if (ring->id == RCS)
1133d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1134d27b1e0eSDaniel Vetter 	} else {
11359d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1136d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1137d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1138d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1139d27b1e0eSDaniel Vetter 	}
1140d27b1e0eSDaniel Vetter 
11419574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1142c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1143b2eadbc8SChris Wilson 	error->seqno[ring->id] = ring->get_seqno(ring, false);
1144d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1145c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1146c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
11477e3b8737SDaniel Vetter 
11487e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
11497e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1150d27b1e0eSDaniel Vetter }
1151d27b1e0eSDaniel Vetter 
115252d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
115352d39a21SChris Wilson 				  struct drm_i915_error_state *error)
115452d39a21SChris Wilson {
115552d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1156b4519513SChris Wilson 	struct intel_ring_buffer *ring;
115752d39a21SChris Wilson 	struct drm_i915_gem_request *request;
115852d39a21SChris Wilson 	int i, count;
115952d39a21SChris Wilson 
1160b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
116152d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
116252d39a21SChris Wilson 
116352d39a21SChris Wilson 		error->ring[i].batchbuffer =
116452d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
116552d39a21SChris Wilson 
116652d39a21SChris Wilson 		error->ring[i].ringbuffer =
116752d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
116852d39a21SChris Wilson 
116952d39a21SChris Wilson 		count = 0;
117052d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
117152d39a21SChris Wilson 			count++;
117252d39a21SChris Wilson 
117352d39a21SChris Wilson 		error->ring[i].num_requests = count;
117452d39a21SChris Wilson 		error->ring[i].requests =
117552d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
117652d39a21SChris Wilson 				GFP_ATOMIC);
117752d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
117852d39a21SChris Wilson 			error->ring[i].num_requests = 0;
117952d39a21SChris Wilson 			continue;
118052d39a21SChris Wilson 		}
118152d39a21SChris Wilson 
118252d39a21SChris Wilson 		count = 0;
118352d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
118452d39a21SChris Wilson 			struct drm_i915_error_request *erq;
118552d39a21SChris Wilson 
118652d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
118752d39a21SChris Wilson 			erq->seqno = request->seqno;
118852d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1189ee4f42b1SChris Wilson 			erq->tail = request->tail;
119052d39a21SChris Wilson 		}
119152d39a21SChris Wilson 	}
119252d39a21SChris Wilson }
119352d39a21SChris Wilson 
11948a905236SJesse Barnes /**
11958a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
11968a905236SJesse Barnes  * @dev: drm device
11978a905236SJesse Barnes  *
11988a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
11998a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
12008a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
12018a905236SJesse Barnes  * to pick up.
12028a905236SJesse Barnes  */
120363eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
120463eeaf38SJesse Barnes {
120563eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
120605394f39SChris Wilson 	struct drm_i915_gem_object *obj;
120763eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
120863eeaf38SJesse Barnes 	unsigned long flags;
12099db4a9c7SJesse Barnes 	int i, pipe;
121063eeaf38SJesse Barnes 
121163eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12129df30794SChris Wilson 	error = dev_priv->first_error;
12139df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
12149df30794SChris Wilson 	if (error)
12159df30794SChris Wilson 		return;
121663eeaf38SJesse Barnes 
12179db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
121833f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
121963eeaf38SJesse Barnes 	if (!error) {
12209df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
12219df30794SChris Wilson 		return;
122263eeaf38SJesse Barnes 	}
122363eeaf38SJesse Barnes 
1224b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
1225b6f7833bSChris Wilson 		 dev->primary->index);
12262fa772f3SChris Wilson 
1227742cbee8SDaniel Vetter 	kref_init(&error->ref);
122863eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
122963eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1230b9a3906bSBen Widawsky 	error->ccid = I915_READ(CCID);
1231be998e2eSBen Widawsky 
1232be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1233be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1234be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1235be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1236be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1237be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1238be998e2eSBen Widawsky 	else
1239be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1240be998e2eSBen Widawsky 
12419db4a9c7SJesse Barnes 	for_each_pipe(pipe)
12429db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1243d27b1e0eSDaniel Vetter 
124433f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1245f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
124633f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
124733f3f518SDaniel Vetter 	}
1248add354ddSChris Wilson 
124971e172e8SBen Widawsky 	if (INTEL_INFO(dev)->gen == 7)
125071e172e8SBen Widawsky 		error->err_int = I915_READ(GEN7_ERR_INT);
125171e172e8SBen Widawsky 
1252050ee91fSBen Widawsky 	i915_get_extra_instdone(dev, error->extra_instdone);
1253050ee91fSBen Widawsky 
1254748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
125552d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
12569df30794SChris Wilson 
1257c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
12589df30794SChris Wilson 	error->active_bo = NULL;
1259c724e8a9SChris Wilson 	error->pinned_bo = NULL;
12609df30794SChris Wilson 
1261bcfb2e28SChris Wilson 	i = 0;
1262bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1263bcfb2e28SChris Wilson 		i++;
1264bcfb2e28SChris Wilson 	error->active_bo_count = i;
12656c085a72SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
12661b50247aSChris Wilson 		if (obj->pin_count)
1267bcfb2e28SChris Wilson 			i++;
1268bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1269c724e8a9SChris Wilson 
12708e934dbfSChris Wilson 	error->active_bo = NULL;
12718e934dbfSChris Wilson 	error->pinned_bo = NULL;
1272bcfb2e28SChris Wilson 	if (i) {
1273bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
12749df30794SChris Wilson 					   GFP_ATOMIC);
1275c724e8a9SChris Wilson 		if (error->active_bo)
1276c724e8a9SChris Wilson 			error->pinned_bo =
1277c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
12789df30794SChris Wilson 	}
1279c724e8a9SChris Wilson 
1280c724e8a9SChris Wilson 	if (error->active_bo)
1281c724e8a9SChris Wilson 		error->active_bo_count =
12821b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1283c724e8a9SChris Wilson 					  error->active_bo_count,
1284c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1285c724e8a9SChris Wilson 
1286c724e8a9SChris Wilson 	if (error->pinned_bo)
1287c724e8a9SChris Wilson 		error->pinned_bo_count =
12881b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1289c724e8a9SChris Wilson 					  error->pinned_bo_count,
12906c085a72SChris Wilson 					  &dev_priv->mm.bound_list);
129163eeaf38SJesse Barnes 
12928a905236SJesse Barnes 	do_gettimeofday(&error->time);
12938a905236SJesse Barnes 
12946ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1295c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
12966ef3d427SChris Wilson 
12979df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12989df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
129963eeaf38SJesse Barnes 		dev_priv->first_error = error;
13009df30794SChris Wilson 		error = NULL;
13019df30794SChris Wilson 	}
130263eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
13039df30794SChris Wilson 
13049df30794SChris Wilson 	if (error)
1305742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
13069df30794SChris Wilson }
13079df30794SChris Wilson 
13089df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
13099df30794SChris Wilson {
13109df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
13119df30794SChris Wilson 	struct drm_i915_error_state *error;
13126dc0e816SBen Widawsky 	unsigned long flags;
13139df30794SChris Wilson 
13146dc0e816SBen Widawsky 	spin_lock_irqsave(&dev_priv->error_lock, flags);
13159df30794SChris Wilson 	error = dev_priv->first_error;
13169df30794SChris Wilson 	dev_priv->first_error = NULL;
13176dc0e816SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
13189df30794SChris Wilson 
13199df30794SChris Wilson 	if (error)
1320742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
132163eeaf38SJesse Barnes }
13223bd3c932SChris Wilson #else
13233bd3c932SChris Wilson #define i915_capture_error_state(x)
13243bd3c932SChris Wilson #endif
132563eeaf38SJesse Barnes 
132635aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1327c0e09200SDave Airlie {
13288a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1329bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
133063eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
1331050ee91fSBen Widawsky 	int pipe, i;
133263eeaf38SJesse Barnes 
133335aed2e6SChris Wilson 	if (!eir)
133435aed2e6SChris Wilson 		return;
133563eeaf38SJesse Barnes 
1336a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
13378a905236SJesse Barnes 
1338bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1339bd9854f9SBen Widawsky 
13408a905236SJesse Barnes 	if (IS_G4X(dev)) {
13418a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
13428a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
13438a905236SJesse Barnes 
1344a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1345a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1346050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
1347050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1348a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1349a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
13508a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
13513143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
13528a905236SJesse Barnes 		}
13538a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
13548a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1355a70491ccSJoe Perches 			pr_err("page table error\n");
1356a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
13578a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
13583143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
13598a905236SJesse Barnes 		}
13608a905236SJesse Barnes 	}
13618a905236SJesse Barnes 
1362a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
136363eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
136463eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1365a70491ccSJoe Perches 			pr_err("page table error\n");
1366a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
136763eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
13683143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
136963eeaf38SJesse Barnes 		}
13708a905236SJesse Barnes 	}
13718a905236SJesse Barnes 
137263eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1373a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
13749db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1375a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
13769db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
137763eeaf38SJesse Barnes 		/* pipestat has already been acked */
137863eeaf38SJesse Barnes 	}
137963eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1380a70491ccSJoe Perches 		pr_err("instruction error\n");
1381a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1382050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
1383050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1384a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
138563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
138663eeaf38SJesse Barnes 
1387a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1388a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1389a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
139063eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
13913143a2bfSChris Wilson 			POSTING_READ(IPEIR);
139263eeaf38SJesse Barnes 		} else {
139363eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
139463eeaf38SJesse Barnes 
1395a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1396a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1397a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1398a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
139963eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
14003143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
140163eeaf38SJesse Barnes 		}
140263eeaf38SJesse Barnes 	}
140363eeaf38SJesse Barnes 
140463eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
14053143a2bfSChris Wilson 	POSTING_READ(EIR);
140663eeaf38SJesse Barnes 	eir = I915_READ(EIR);
140763eeaf38SJesse Barnes 	if (eir) {
140863eeaf38SJesse Barnes 		/*
140963eeaf38SJesse Barnes 		 * some errors might have become stuck,
141063eeaf38SJesse Barnes 		 * mask them.
141163eeaf38SJesse Barnes 		 */
141263eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
141363eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
141463eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
141563eeaf38SJesse Barnes 	}
141635aed2e6SChris Wilson }
141735aed2e6SChris Wilson 
141835aed2e6SChris Wilson /**
141935aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
142035aed2e6SChris Wilson  * @dev: drm device
142135aed2e6SChris Wilson  *
142235aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
142335aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
142435aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
142535aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
142635aed2e6SChris Wilson  * of a ring dump etc.).
142735aed2e6SChris Wilson  */
1428527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
142935aed2e6SChris Wilson {
143035aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1431b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1432b4519513SChris Wilson 	int i;
143335aed2e6SChris Wilson 
143435aed2e6SChris Wilson 	i915_capture_error_state(dev);
143535aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
14368a905236SJesse Barnes 
1437ba1234d1SBen Gamari 	if (wedged) {
143830dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1439ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1440ba1234d1SBen Gamari 
144111ed50ecSBen Gamari 		/*
144211ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
144311ed50ecSBen Gamari 		 */
1444b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1445b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
144611ed50ecSBen Gamari 	}
144711ed50ecSBen Gamari 
14489c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
14498a905236SJesse Barnes }
14508a905236SJesse Barnes 
14514e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
14524e5359cdSSimon Farnsworth {
14534e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
14544e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
14554e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
145605394f39SChris Wilson 	struct drm_i915_gem_object *obj;
14574e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
14584e5359cdSSimon Farnsworth 	unsigned long flags;
14594e5359cdSSimon Farnsworth 	bool stall_detected;
14604e5359cdSSimon Farnsworth 
14614e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
14624e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
14634e5359cdSSimon Farnsworth 		return;
14644e5359cdSSimon Farnsworth 
14654e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
14664e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
14674e5359cdSSimon Farnsworth 
14684e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
14694e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
14704e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
14714e5359cdSSimon Farnsworth 		return;
14724e5359cdSSimon Farnsworth 	}
14734e5359cdSSimon Farnsworth 
14744e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
147505394f39SChris Wilson 	obj = work->pending_flip_obj;
1476a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
14779db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1478446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1479446f2545SArmin Reese 					obj->gtt_offset;
14804e5359cdSSimon Farnsworth 	} else {
14819db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
148205394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
148301f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
14844e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
14854e5359cdSSimon Farnsworth 	}
14864e5359cdSSimon Farnsworth 
14874e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
14884e5359cdSSimon Farnsworth 
14894e5359cdSSimon Farnsworth 	if (stall_detected) {
14904e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
14914e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
14924e5359cdSSimon Farnsworth 	}
14934e5359cdSSimon Farnsworth }
14944e5359cdSSimon Farnsworth 
149542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
149642f52ef8SKeith Packard  * we use as a pipe index
149742f52ef8SKeith Packard  */
1498f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
14990a3e67a4SJesse Barnes {
15000a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1501e9d21d7fSKeith Packard 	unsigned long irqflags;
150271e0ffa5SJesse Barnes 
15035eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
150471e0ffa5SJesse Barnes 		return -EINVAL;
15050a3e67a4SJesse Barnes 
15061ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1507f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
15087c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
15097c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
15100a3e67a4SJesse Barnes 	else
15117c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
15127c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
15138692d00eSChris Wilson 
15148692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
15158692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15166b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
15171ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15188692d00eSChris Wilson 
15190a3e67a4SJesse Barnes 	return 0;
15200a3e67a4SJesse Barnes }
15210a3e67a4SJesse Barnes 
1522f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1523f796cf8fSJesse Barnes {
1524f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1525f796cf8fSJesse Barnes 	unsigned long irqflags;
1526f796cf8fSJesse Barnes 
1527f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1528f796cf8fSJesse Barnes 		return -EINVAL;
1529f796cf8fSJesse Barnes 
1530f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1531f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1532f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1533f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1534f796cf8fSJesse Barnes 
1535f796cf8fSJesse Barnes 	return 0;
1536f796cf8fSJesse Barnes }
1537f796cf8fSJesse Barnes 
1538f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1539b1f14ad0SJesse Barnes {
1540b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1541b1f14ad0SJesse Barnes 	unsigned long irqflags;
1542b1f14ad0SJesse Barnes 
1543b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1544b1f14ad0SJesse Barnes 		return -EINVAL;
1545b1f14ad0SJesse Barnes 
1546b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1547b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
1548b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
1549b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1550b1f14ad0SJesse Barnes 
1551b1f14ad0SJesse Barnes 	return 0;
1552b1f14ad0SJesse Barnes }
1553b1f14ad0SJesse Barnes 
15547e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
15557e231dbeSJesse Barnes {
15567e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
15577e231dbeSJesse Barnes 	unsigned long irqflags;
155831acc7f5SJesse Barnes 	u32 imr;
15597e231dbeSJesse Barnes 
15607e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
15617e231dbeSJesse Barnes 		return -EINVAL;
15627e231dbeSJesse Barnes 
15637e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15647e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
156531acc7f5SJesse Barnes 	if (pipe == 0)
15667e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
156731acc7f5SJesse Barnes 	else
15687e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
15697e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
157031acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
157131acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
15727e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15737e231dbeSJesse Barnes 
15747e231dbeSJesse Barnes 	return 0;
15757e231dbeSJesse Barnes }
15767e231dbeSJesse Barnes 
157742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
157842f52ef8SKeith Packard  * we use as a pipe index
157942f52ef8SKeith Packard  */
1580f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
15810a3e67a4SJesse Barnes {
15820a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1583e9d21d7fSKeith Packard 	unsigned long irqflags;
15840a3e67a4SJesse Barnes 
15851ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15868692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15876b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
15888692d00eSChris Wilson 
15897c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
15907c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
15917c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
15921ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15930a3e67a4SJesse Barnes }
15940a3e67a4SJesse Barnes 
1595f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1596f796cf8fSJesse Barnes {
1597f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1598f796cf8fSJesse Barnes 	unsigned long irqflags;
1599f796cf8fSJesse Barnes 
1600f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1601f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1602f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1603f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1604f796cf8fSJesse Barnes }
1605f796cf8fSJesse Barnes 
1606f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1607b1f14ad0SJesse Barnes {
1608b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1609b1f14ad0SJesse Barnes 	unsigned long irqflags;
1610b1f14ad0SJesse Barnes 
1611b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1612b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
1613b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
1614b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1615b1f14ad0SJesse Barnes }
1616b1f14ad0SJesse Barnes 
16177e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
16187e231dbeSJesse Barnes {
16197e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16207e231dbeSJesse Barnes 	unsigned long irqflags;
162131acc7f5SJesse Barnes 	u32 imr;
16227e231dbeSJesse Barnes 
16237e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
162431acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
162531acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
16267e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
162731acc7f5SJesse Barnes 	if (pipe == 0)
16287e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
162931acc7f5SJesse Barnes 	else
16307e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
16317e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
16327e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
16337e231dbeSJesse Barnes }
16347e231dbeSJesse Barnes 
1635893eead0SChris Wilson static u32
1636893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1637852835f3SZou Nan hai {
1638893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1639893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1640893eead0SChris Wilson }
1641893eead0SChris Wilson 
1642893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1643893eead0SChris Wilson {
1644893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1645b2eadbc8SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring, false),
1646b2eadbc8SChris Wilson 			      ring_last_seqno(ring))) {
1647893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
16489574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
16499574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
16509574b3feSBen Widawsky 				  ring->name);
1651893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1652893eead0SChris Wilson 			*err = true;
1653893eead0SChris Wilson 		}
1654893eead0SChris Wilson 		return true;
1655893eead0SChris Wilson 	}
1656893eead0SChris Wilson 	return false;
1657f65d9421SBen Gamari }
1658f65d9421SBen Gamari 
16591ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
16601ec14ad3SChris Wilson {
16611ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
16621ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
16631ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
16641ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
16651ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
16661ec14ad3SChris Wilson 			  ring->name);
16671ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
16681ec14ad3SChris Wilson 		return true;
16691ec14ad3SChris Wilson 	}
16701ec14ad3SChris Wilson 	return false;
16711ec14ad3SChris Wilson }
16721ec14ad3SChris Wilson 
1673d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
1674d1e61e7fSChris Wilson {
1675d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1676d1e61e7fSChris Wilson 
1677d1e61e7fSChris Wilson 	if (dev_priv->hangcheck_count++ > 1) {
1678b4519513SChris Wilson 		bool hung = true;
1679b4519513SChris Wilson 
1680d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1681d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
1682d1e61e7fSChris Wilson 
1683d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
1684b4519513SChris Wilson 			struct intel_ring_buffer *ring;
1685b4519513SChris Wilson 			int i;
1686b4519513SChris Wilson 
1687d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
1688d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
1689d1e61e7fSChris Wilson 			 * and break the hang. This should work on
1690d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
1691d1e61e7fSChris Wilson 			 */
1692b4519513SChris Wilson 			for_each_ring(ring, dev_priv, i)
1693b4519513SChris Wilson 				hung &= !kick_ring(ring);
1694d1e61e7fSChris Wilson 		}
1695d1e61e7fSChris Wilson 
1696b4519513SChris Wilson 		return hung;
1697d1e61e7fSChris Wilson 	}
1698d1e61e7fSChris Wilson 
1699d1e61e7fSChris Wilson 	return false;
1700d1e61e7fSChris Wilson }
1701d1e61e7fSChris Wilson 
1702f65d9421SBen Gamari /**
1703f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1704f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1705f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1706f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1707f65d9421SBen Gamari  */
1708f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1709f65d9421SBen Gamari {
1710f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1711f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1712bd9854f9SBen Widawsky 	uint32_t acthd[I915_NUM_RINGS], instdone[I915_NUM_INSTDONE_REG];
1713b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1714b4519513SChris Wilson 	bool err = false, idle;
1715b4519513SChris Wilson 	int i;
1716893eead0SChris Wilson 
17173e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
17183e0dc6b0SBen Widawsky 		return;
17193e0dc6b0SBen Widawsky 
1720b4519513SChris Wilson 	memset(acthd, 0, sizeof(acthd));
1721b4519513SChris Wilson 	idle = true;
1722b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
1723b4519513SChris Wilson 	    idle &= i915_hangcheck_ring_idle(ring, &err);
1724b4519513SChris Wilson 	    acthd[i] = intel_ring_get_active_head(ring);
1725b4519513SChris Wilson 	}
1726b4519513SChris Wilson 
1727893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
1728b4519513SChris Wilson 	if (idle) {
1729d1e61e7fSChris Wilson 		if (err) {
1730d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
1731d1e61e7fSChris Wilson 				return;
1732d1e61e7fSChris Wilson 
1733893eead0SChris Wilson 			goto repeat;
1734d1e61e7fSChris Wilson 		}
1735d1e61e7fSChris Wilson 
1736d1e61e7fSChris Wilson 		dev_priv->hangcheck_count = 0;
1737893eead0SChris Wilson 		return;
1738893eead0SChris Wilson 	}
1739f65d9421SBen Gamari 
1740bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
1741b4519513SChris Wilson 	if (memcmp(dev_priv->last_acthd, acthd, sizeof(acthd)) == 0 &&
1742050ee91fSBen Widawsky 	    memcmp(dev_priv->prev_instdone, instdone, sizeof(instdone)) == 0) {
1743d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
1744f65d9421SBen Gamari 			return;
1745cbb465e7SChris Wilson 	} else {
1746cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1747cbb465e7SChris Wilson 
1748b4519513SChris Wilson 		memcpy(dev_priv->last_acthd, acthd, sizeof(acthd));
1749050ee91fSBen Widawsky 		memcpy(dev_priv->prev_instdone, instdone, sizeof(instdone));
1750cbb465e7SChris Wilson 	}
1751f65d9421SBen Gamari 
1752893eead0SChris Wilson repeat:
1753f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1754b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1755b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1756f65d9421SBen Gamari }
1757f65d9421SBen Gamari 
1758c0e09200SDave Airlie /* drm_dma.h hooks
1759c0e09200SDave Airlie */
1760f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
1761036a4a7dSZhenyu Wang {
1762036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1763036a4a7dSZhenyu Wang 
17644697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17654697995bSJesse Barnes 
1766036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1767bdfcdb63SDaniel Vetter 
1768036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1769036a4a7dSZhenyu Wang 
1770036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1771036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
17723143a2bfSChris Wilson 	POSTING_READ(DEIER);
1773036a4a7dSZhenyu Wang 
1774036a4a7dSZhenyu Wang 	/* and GT */
1775036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1776036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
17773143a2bfSChris Wilson 	POSTING_READ(GTIER);
1778c650156aSZhenyu Wang 
1779c650156aSZhenyu Wang 	/* south display irq */
1780c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1781c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
17823143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1783036a4a7dSZhenyu Wang }
1784036a4a7dSZhenyu Wang 
17857e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
17867e231dbeSJesse Barnes {
17877e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17887e231dbeSJesse Barnes 	int pipe;
17897e231dbeSJesse Barnes 
17907e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17917e231dbeSJesse Barnes 
17927e231dbeSJesse Barnes 	/* VLV magic */
17937e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
17947e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
17957e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
17967e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
17977e231dbeSJesse Barnes 
17987e231dbeSJesse Barnes 	/* and GT */
17997e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
18007e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
18017e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
18027e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
18037e231dbeSJesse Barnes 	POSTING_READ(GTIER);
18047e231dbeSJesse Barnes 
18057e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
18067e231dbeSJesse Barnes 
18077e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
18087e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
18097e231dbeSJesse Barnes 	for_each_pipe(pipe)
18107e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
18117e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
18127e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
18137e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
18147e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
18157e231dbeSJesse Barnes }
18167e231dbeSJesse Barnes 
18177fe0b973SKeith Packard /*
18187fe0b973SKeith Packard  * Enable digital hotplug on the PCH, and configure the DP short pulse
18197fe0b973SKeith Packard  * duration to 2ms (which is the minimum in the Display Port spec)
18207fe0b973SKeith Packard  *
18217fe0b973SKeith Packard  * This register is the same on all known PCH chips.
18227fe0b973SKeith Packard  */
18237fe0b973SKeith Packard 
18247fe0b973SKeith Packard static void ironlake_enable_pch_hotplug(struct drm_device *dev)
18257fe0b973SKeith Packard {
18267fe0b973SKeith Packard 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18277fe0b973SKeith Packard 	u32	hotplug;
18287fe0b973SKeith Packard 
18297fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
18307fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
18317fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
18327fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
18337fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
18347fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
18357fe0b973SKeith Packard }
18367fe0b973SKeith Packard 
1837f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
1838036a4a7dSZhenyu Wang {
1839036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1840036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1841013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1842013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
18431ec14ad3SChris Wilson 	u32 render_irqs;
18442d7b8366SYuanhan Liu 	u32 hotplug_mask;
1845036a4a7dSZhenyu Wang 
18461ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1847036a4a7dSZhenyu Wang 
1848036a4a7dSZhenyu Wang 	/* should always can generate irq */
1849036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
18501ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
18511ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
18523143a2bfSChris Wilson 	POSTING_READ(DEIER);
1853036a4a7dSZhenyu Wang 
18541ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1855036a4a7dSZhenyu Wang 
1856036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
18571ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1858881f47b6SXiang, Haihao 
18591ec14ad3SChris Wilson 	if (IS_GEN6(dev))
18601ec14ad3SChris Wilson 		render_irqs =
18611ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
1862e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
1863e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
18641ec14ad3SChris Wilson 	else
18651ec14ad3SChris Wilson 		render_irqs =
186688f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1867c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
18681ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
18691ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
18703143a2bfSChris Wilson 	POSTING_READ(GTIER);
1871036a4a7dSZhenyu Wang 
18722d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
18739035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
18749035a97aSChris Wilson 				SDE_PORTB_HOTPLUG_CPT |
18759035a97aSChris Wilson 				SDE_PORTC_HOTPLUG_CPT |
18769035a97aSChris Wilson 				SDE_PORTD_HOTPLUG_CPT);
18772d7b8366SYuanhan Liu 	} else {
18789035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG |
18799035a97aSChris Wilson 				SDE_PORTB_HOTPLUG |
18809035a97aSChris Wilson 				SDE_PORTC_HOTPLUG |
18819035a97aSChris Wilson 				SDE_PORTD_HOTPLUG |
18829035a97aSChris Wilson 				SDE_AUX_MASK);
18832d7b8366SYuanhan Liu 	}
18842d7b8366SYuanhan Liu 
18851ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1886c650156aSZhenyu Wang 
1887c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
18881ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
18891ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
18903143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1891c650156aSZhenyu Wang 
18927fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
18937fe0b973SKeith Packard 
1894f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1895f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1896f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1897f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1898f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1899f97108d1SJesse Barnes 	}
1900f97108d1SJesse Barnes 
1901036a4a7dSZhenyu Wang 	return 0;
1902036a4a7dSZhenyu Wang }
1903036a4a7dSZhenyu Wang 
1904f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
1905b1f14ad0SJesse Barnes {
1906b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1907b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
1908b615b57aSChris Wilson 	u32 display_mask =
1909b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
1910b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
1911b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
1912b615b57aSChris Wilson 		DE_PLANEA_FLIP_DONE_IVB;
1913b1f14ad0SJesse Barnes 	u32 render_irqs;
1914b1f14ad0SJesse Barnes 	u32 hotplug_mask;
1915b1f14ad0SJesse Barnes 
1916b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
1917b1f14ad0SJesse Barnes 
1918b1f14ad0SJesse Barnes 	/* should always can generate irq */
1919b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1920b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
1921b615b57aSChris Wilson 	I915_WRITE(DEIER,
1922b615b57aSChris Wilson 		   display_mask |
1923b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
1924b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
1925b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
1926b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1927b1f14ad0SJesse Barnes 
192815b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
1929b1f14ad0SJesse Barnes 
1930b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1931b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1932b1f14ad0SJesse Barnes 
1933e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
193415b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
1935b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
1936b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
1937b1f14ad0SJesse Barnes 
1938b1f14ad0SJesse Barnes 	hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
1939b1f14ad0SJesse Barnes 			SDE_PORTB_HOTPLUG_CPT |
1940b1f14ad0SJesse Barnes 			SDE_PORTC_HOTPLUG_CPT |
1941b1f14ad0SJesse Barnes 			SDE_PORTD_HOTPLUG_CPT);
1942b1f14ad0SJesse Barnes 	dev_priv->pch_irq_mask = ~hotplug_mask;
1943b1f14ad0SJesse Barnes 
1944b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1945b1f14ad0SJesse Barnes 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1946b1f14ad0SJesse Barnes 	I915_WRITE(SDEIER, hotplug_mask);
1947b1f14ad0SJesse Barnes 	POSTING_READ(SDEIER);
1948b1f14ad0SJesse Barnes 
19497fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
19507fe0b973SKeith Packard 
1951b1f14ad0SJesse Barnes 	return 0;
1952b1f14ad0SJesse Barnes }
1953b1f14ad0SJesse Barnes 
19547e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
19557e231dbeSJesse Barnes {
19567e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19577e231dbeSJesse Barnes 	u32 enable_mask;
19587e231dbeSJesse Barnes 	u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
195931acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
19603bcedbe5SJesse Barnes 	u32 render_irqs;
19617e231dbeSJesse Barnes 	u16 msid;
19627e231dbeSJesse Barnes 
19637e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
196431acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
196531acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
196631acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
19677e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
19687e231dbeSJesse Barnes 
196931acc7f5SJesse Barnes 	/*
197031acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
197131acc7f5SJesse Barnes 	 * toggle them based on usage.
197231acc7f5SJesse Barnes 	 */
197331acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
197431acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
197531acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
19767e231dbeSJesse Barnes 
19777e231dbeSJesse Barnes 	dev_priv->pipestat[0] = 0;
19787e231dbeSJesse Barnes 	dev_priv->pipestat[1] = 0;
19797e231dbeSJesse Barnes 
19807e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
19817e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
19827e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
19837e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
19847e231dbeSJesse Barnes 	msid |= (1<<14);
19857e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
19867e231dbeSJesse Barnes 
19877e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
19887e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
19897e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19907e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
19917e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
19927e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
19937e231dbeSJesse Barnes 
199431acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
199531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
199631acc7f5SJesse Barnes 
19977e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19987e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19997e231dbeSJesse Barnes 
200031acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
200131acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
20023bcedbe5SJesse Barnes 
20033bcedbe5SJesse Barnes 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
20043bcedbe5SJesse Barnes 		GEN6_BLITTER_USER_INTERRUPT;
20053bcedbe5SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
20067e231dbeSJesse Barnes 	POSTING_READ(GTIER);
20077e231dbeSJesse Barnes 
20087e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
20097e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
20107e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
20117e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
20127e231dbeSJesse Barnes #endif
20137e231dbeSJesse Barnes 
20147e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
20157e231dbeSJesse Barnes 	/* Note HDMI and DP share bits */
20167e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
20177e231dbeSJesse Barnes 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
20187e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
20197e231dbeSJesse Barnes 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
20207e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
20217e231dbeSJesse Barnes 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
2022*ae33cdcfSVijay Purushothaman 	if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
20237e231dbeSJesse Barnes 		hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2024*ae33cdcfSVijay Purushothaman 	if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
20257e231dbeSJesse Barnes 		hotplug_en |= SDVOB_HOTPLUG_INT_EN;
20267e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
20277e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_INT_EN;
20287e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
20297e231dbeSJesse Barnes 	}
20307e231dbeSJesse Barnes 
20317e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
20327e231dbeSJesse Barnes 
20337e231dbeSJesse Barnes 	return 0;
20347e231dbeSJesse Barnes }
20357e231dbeSJesse Barnes 
20367e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
20377e231dbeSJesse Barnes {
20387e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20397e231dbeSJesse Barnes 	int pipe;
20407e231dbeSJesse Barnes 
20417e231dbeSJesse Barnes 	if (!dev_priv)
20427e231dbeSJesse Barnes 		return;
20437e231dbeSJesse Barnes 
20447e231dbeSJesse Barnes 	for_each_pipe(pipe)
20457e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
20467e231dbeSJesse Barnes 
20477e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
20487e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
20497e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
20507e231dbeSJesse Barnes 	for_each_pipe(pipe)
20517e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
20527e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20537e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
20547e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
20557e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
20567e231dbeSJesse Barnes }
20577e231dbeSJesse Barnes 
2058f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2059036a4a7dSZhenyu Wang {
2060036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20614697995bSJesse Barnes 
20624697995bSJesse Barnes 	if (!dev_priv)
20634697995bSJesse Barnes 		return;
20644697995bSJesse Barnes 
2065036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2066036a4a7dSZhenyu Wang 
2067036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2068036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2069036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2070036a4a7dSZhenyu Wang 
2071036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2072036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2073036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2074192aac1fSKeith Packard 
2075192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2076192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2077192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2078036a4a7dSZhenyu Wang }
2079036a4a7dSZhenyu Wang 
2080c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2081c2798b19SChris Wilson {
2082c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2083c2798b19SChris Wilson 	int pipe;
2084c2798b19SChris Wilson 
2085c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2086c2798b19SChris Wilson 
2087c2798b19SChris Wilson 	for_each_pipe(pipe)
2088c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2089c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2090c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2091c2798b19SChris Wilson 	POSTING_READ16(IER);
2092c2798b19SChris Wilson }
2093c2798b19SChris Wilson 
2094c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2095c2798b19SChris Wilson {
2096c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2097c2798b19SChris Wilson 
2098c2798b19SChris Wilson 	dev_priv->pipestat[0] = 0;
2099c2798b19SChris Wilson 	dev_priv->pipestat[1] = 0;
2100c2798b19SChris Wilson 
2101c2798b19SChris Wilson 	I915_WRITE16(EMR,
2102c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2103c2798b19SChris Wilson 
2104c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2105c2798b19SChris Wilson 	dev_priv->irq_mask =
2106c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2107c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2108c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2109c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2110c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2111c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2112c2798b19SChris Wilson 
2113c2798b19SChris Wilson 	I915_WRITE16(IER,
2114c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2115c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2116c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2117c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2118c2798b19SChris Wilson 	POSTING_READ16(IER);
2119c2798b19SChris Wilson 
2120c2798b19SChris Wilson 	return 0;
2121c2798b19SChris Wilson }
2122c2798b19SChris Wilson 
2123c2798b19SChris Wilson static irqreturn_t i8xx_irq_handler(DRM_IRQ_ARGS)
2124c2798b19SChris Wilson {
2125c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2126c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2127c2798b19SChris Wilson 	u16 iir, new_iir;
2128c2798b19SChris Wilson 	u32 pipe_stats[2];
2129c2798b19SChris Wilson 	unsigned long irqflags;
2130c2798b19SChris Wilson 	int irq_received;
2131c2798b19SChris Wilson 	int pipe;
2132c2798b19SChris Wilson 	u16 flip_mask =
2133c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2134c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2135c2798b19SChris Wilson 
2136c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2137c2798b19SChris Wilson 
2138c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2139c2798b19SChris Wilson 	if (iir == 0)
2140c2798b19SChris Wilson 		return IRQ_NONE;
2141c2798b19SChris Wilson 
2142c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2143c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2144c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2145c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2146c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2147c2798b19SChris Wilson 		 */
2148c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2149c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2150c2798b19SChris Wilson 			i915_handle_error(dev, false);
2151c2798b19SChris Wilson 
2152c2798b19SChris Wilson 		for_each_pipe(pipe) {
2153c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2154c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2155c2798b19SChris Wilson 
2156c2798b19SChris Wilson 			/*
2157c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2158c2798b19SChris Wilson 			 */
2159c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2160c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2161c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2162c2798b19SChris Wilson 							 pipe_name(pipe));
2163c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2164c2798b19SChris Wilson 				irq_received = 1;
2165c2798b19SChris Wilson 			}
2166c2798b19SChris Wilson 		}
2167c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2168c2798b19SChris Wilson 
2169c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2170c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2171c2798b19SChris Wilson 
2172d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2173c2798b19SChris Wilson 
2174c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2175c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2176c2798b19SChris Wilson 
2177c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
2178c2798b19SChris Wilson 		    drm_handle_vblank(dev, 0)) {
2179c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
2180c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 0);
2181c2798b19SChris Wilson 				intel_finish_page_flip(dev, 0);
2182c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT;
2183c2798b19SChris Wilson 			}
2184c2798b19SChris Wilson 		}
2185c2798b19SChris Wilson 
2186c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
2187c2798b19SChris Wilson 		    drm_handle_vblank(dev, 1)) {
2188c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
2189c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 1);
2190c2798b19SChris Wilson 				intel_finish_page_flip(dev, 1);
2191c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2192c2798b19SChris Wilson 			}
2193c2798b19SChris Wilson 		}
2194c2798b19SChris Wilson 
2195c2798b19SChris Wilson 		iir = new_iir;
2196c2798b19SChris Wilson 	}
2197c2798b19SChris Wilson 
2198c2798b19SChris Wilson 	return IRQ_HANDLED;
2199c2798b19SChris Wilson }
2200c2798b19SChris Wilson 
2201c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2202c2798b19SChris Wilson {
2203c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2204c2798b19SChris Wilson 	int pipe;
2205c2798b19SChris Wilson 
2206c2798b19SChris Wilson 	for_each_pipe(pipe) {
2207c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2208c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2209c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2210c2798b19SChris Wilson 	}
2211c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2212c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2213c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2214c2798b19SChris Wilson }
2215c2798b19SChris Wilson 
2216a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2217a266c7d5SChris Wilson {
2218a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2219a266c7d5SChris Wilson 	int pipe;
2220a266c7d5SChris Wilson 
2221a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2222a266c7d5SChris Wilson 
2223a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2224a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2225a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2226a266c7d5SChris Wilson 	}
2227a266c7d5SChris Wilson 
222800d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2229a266c7d5SChris Wilson 	for_each_pipe(pipe)
2230a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2231a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2232a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2233a266c7d5SChris Wilson 	POSTING_READ(IER);
2234a266c7d5SChris Wilson }
2235a266c7d5SChris Wilson 
2236a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2237a266c7d5SChris Wilson {
2238a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
223938bde180SChris Wilson 	u32 enable_mask;
2240a266c7d5SChris Wilson 
2241a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2242a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2243a266c7d5SChris Wilson 
224438bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
224538bde180SChris Wilson 
224638bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
224738bde180SChris Wilson 	dev_priv->irq_mask =
224838bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
224938bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
225038bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
225138bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
225238bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
225338bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
225438bde180SChris Wilson 
225538bde180SChris Wilson 	enable_mask =
225638bde180SChris Wilson 		I915_ASLE_INTERRUPT |
225738bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
225838bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
225938bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
226038bde180SChris Wilson 		I915_USER_INTERRUPT;
226138bde180SChris Wilson 
2262a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2263a266c7d5SChris Wilson 		/* Enable in IER... */
2264a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2265a266c7d5SChris Wilson 		/* and unmask in IMR */
2266a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2267a266c7d5SChris Wilson 	}
2268a266c7d5SChris Wilson 
2269a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2270a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2271a266c7d5SChris Wilson 	POSTING_READ(IER);
2272a266c7d5SChris Wilson 
2273a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2274a266c7d5SChris Wilson 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2275a266c7d5SChris Wilson 
2276a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2277a266c7d5SChris Wilson 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2278a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2279a266c7d5SChris Wilson 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2280a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2281a266c7d5SChris Wilson 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
2282084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
2283a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2284084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
2285a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2286a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2287a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_INT_EN;
2288a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2289a266c7d5SChris Wilson 		}
2290a266c7d5SChris Wilson 
2291a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2292a266c7d5SChris Wilson 
2293a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2294a266c7d5SChris Wilson 	}
2295a266c7d5SChris Wilson 
2296a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2297a266c7d5SChris Wilson 
2298a266c7d5SChris Wilson 	return 0;
2299a266c7d5SChris Wilson }
2300a266c7d5SChris Wilson 
2301a266c7d5SChris Wilson static irqreturn_t i915_irq_handler(DRM_IRQ_ARGS)
2302a266c7d5SChris Wilson {
2303a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2304a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23058291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2306a266c7d5SChris Wilson 	unsigned long irqflags;
230738bde180SChris Wilson 	u32 flip_mask =
230838bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
230938bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
231038bde180SChris Wilson 	u32 flip[2] = {
231138bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT,
231238bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT
231338bde180SChris Wilson 	};
231438bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2315a266c7d5SChris Wilson 
2316a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2317a266c7d5SChris Wilson 
2318a266c7d5SChris Wilson 	iir = I915_READ(IIR);
231938bde180SChris Wilson 	do {
232038bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
23218291ee90SChris Wilson 		bool blc_event = false;
2322a266c7d5SChris Wilson 
2323a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2324a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2325a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2326a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2327a266c7d5SChris Wilson 		 */
2328a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2329a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2330a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2331a266c7d5SChris Wilson 
2332a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2333a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2334a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2335a266c7d5SChris Wilson 
233638bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2337a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2338a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2339a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2340a266c7d5SChris Wilson 							 pipe_name(pipe));
2341a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
234238bde180SChris Wilson 				irq_received = true;
2343a266c7d5SChris Wilson 			}
2344a266c7d5SChris Wilson 		}
2345a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2346a266c7d5SChris Wilson 
2347a266c7d5SChris Wilson 		if (!irq_received)
2348a266c7d5SChris Wilson 			break;
2349a266c7d5SChris Wilson 
2350a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2351a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2352a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2353a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2354a266c7d5SChris Wilson 
2355a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2356a266c7d5SChris Wilson 				  hotplug_status);
2357a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2358a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2359a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2360a266c7d5SChris Wilson 
2361a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
236238bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2363a266c7d5SChris Wilson 		}
2364a266c7d5SChris Wilson 
236538bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2366a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2367a266c7d5SChris Wilson 
2368a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2369a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2370a266c7d5SChris Wilson 
2371a266c7d5SChris Wilson 		for_each_pipe(pipe) {
237238bde180SChris Wilson 			int plane = pipe;
237338bde180SChris Wilson 			if (IS_MOBILE(dev))
237438bde180SChris Wilson 				plane = !plane;
23758291ee90SChris Wilson 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
2376a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
237738bde180SChris Wilson 				if (iir & flip[plane]) {
237838bde180SChris Wilson 					intel_prepare_page_flip(dev, plane);
2379a266c7d5SChris Wilson 					intel_finish_page_flip(dev, pipe);
238038bde180SChris Wilson 					flip_mask &= ~flip[plane];
238138bde180SChris Wilson 				}
2382a266c7d5SChris Wilson 			}
2383a266c7d5SChris Wilson 
2384a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2385a266c7d5SChris Wilson 				blc_event = true;
2386a266c7d5SChris Wilson 		}
2387a266c7d5SChris Wilson 
2388a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2389a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2390a266c7d5SChris Wilson 
2391a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2392a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2393a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2394a266c7d5SChris Wilson 		 * we would never get another interrupt.
2395a266c7d5SChris Wilson 		 *
2396a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2397a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2398a266c7d5SChris Wilson 		 * another one.
2399a266c7d5SChris Wilson 		 *
2400a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2401a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2402a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2403a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2404a266c7d5SChris Wilson 		 * stray interrupts.
2405a266c7d5SChris Wilson 		 */
240638bde180SChris Wilson 		ret = IRQ_HANDLED;
2407a266c7d5SChris Wilson 		iir = new_iir;
240838bde180SChris Wilson 	} while (iir & ~flip_mask);
2409a266c7d5SChris Wilson 
2410d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
24118291ee90SChris Wilson 
2412a266c7d5SChris Wilson 	return ret;
2413a266c7d5SChris Wilson }
2414a266c7d5SChris Wilson 
2415a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2416a266c7d5SChris Wilson {
2417a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2418a266c7d5SChris Wilson 	int pipe;
2419a266c7d5SChris Wilson 
2420a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2421a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2422a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2423a266c7d5SChris Wilson 	}
2424a266c7d5SChris Wilson 
242500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
242655b39755SChris Wilson 	for_each_pipe(pipe) {
242755b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2428a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
242955b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
243055b39755SChris Wilson 	}
2431a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2432a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2433a266c7d5SChris Wilson 
2434a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2435a266c7d5SChris Wilson }
2436a266c7d5SChris Wilson 
2437a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2438a266c7d5SChris Wilson {
2439a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2440a266c7d5SChris Wilson 	int pipe;
2441a266c7d5SChris Wilson 
2442a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2443a266c7d5SChris Wilson 
2444a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2445a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2446a266c7d5SChris Wilson 
2447a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2448a266c7d5SChris Wilson 	for_each_pipe(pipe)
2449a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2450a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2451a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2452a266c7d5SChris Wilson 	POSTING_READ(IER);
2453a266c7d5SChris Wilson }
2454a266c7d5SChris Wilson 
2455a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2456a266c7d5SChris Wilson {
2457a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2458adca4730SChris Wilson 	u32 hotplug_en;
2459bbba0a97SChris Wilson 	u32 enable_mask;
2460a266c7d5SChris Wilson 	u32 error_mask;
2461a266c7d5SChris Wilson 
2462a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2463bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2464adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2465bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2466bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2467bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2468bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2469bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2470bbba0a97SChris Wilson 
2471bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
2472bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2473bbba0a97SChris Wilson 
2474bbba0a97SChris Wilson 	if (IS_G4X(dev))
2475bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2476a266c7d5SChris Wilson 
2477a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2478a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2479a266c7d5SChris Wilson 
2480a266c7d5SChris Wilson 	/*
2481a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2482a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2483a266c7d5SChris Wilson 	 */
2484a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2485a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2486a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2487a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2488a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2489a266c7d5SChris Wilson 	} else {
2490a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2491a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2492a266c7d5SChris Wilson 	}
2493a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2494a266c7d5SChris Wilson 
2495a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2496a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2497a266c7d5SChris Wilson 	POSTING_READ(IER);
2498a266c7d5SChris Wilson 
2499adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
2500adca4730SChris Wilson 	hotplug_en = 0;
2501a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2502a266c7d5SChris Wilson 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2503a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2504a266c7d5SChris Wilson 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2505a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2506a266c7d5SChris Wilson 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
2507084b612eSChris Wilson 	if (IS_G4X(dev)) {
2508084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_G4X)
2509a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2510084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_G4X)
2511a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2512084b612eSChris Wilson 	} else {
2513084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I965)
2514084b612eSChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2515084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I965)
2516084b612eSChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2517084b612eSChris Wilson 	}
2518a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2519a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_INT_EN;
2520a266c7d5SChris Wilson 
2521a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2522a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2523a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2524a266c7d5SChris Wilson 		   */
2525a266c7d5SChris Wilson 		if (IS_G4X(dev))
2526a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2527a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2528a266c7d5SChris Wilson 	}
2529a266c7d5SChris Wilson 
2530a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
2531a266c7d5SChris Wilson 
2532a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2533a266c7d5SChris Wilson 
2534a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2535a266c7d5SChris Wilson 
2536a266c7d5SChris Wilson 	return 0;
2537a266c7d5SChris Wilson }
2538a266c7d5SChris Wilson 
2539a266c7d5SChris Wilson static irqreturn_t i965_irq_handler(DRM_IRQ_ARGS)
2540a266c7d5SChris Wilson {
2541a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2542a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2543a266c7d5SChris Wilson 	u32 iir, new_iir;
2544a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2545a266c7d5SChris Wilson 	unsigned long irqflags;
2546a266c7d5SChris Wilson 	int irq_received;
2547a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
2548a266c7d5SChris Wilson 
2549a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2550a266c7d5SChris Wilson 
2551a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2552a266c7d5SChris Wilson 
2553a266c7d5SChris Wilson 	for (;;) {
25542c8ba29fSChris Wilson 		bool blc_event = false;
25552c8ba29fSChris Wilson 
2556a266c7d5SChris Wilson 		irq_received = iir != 0;
2557a266c7d5SChris Wilson 
2558a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2559a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2560a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2561a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2562a266c7d5SChris Wilson 		 */
2563a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2564a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2565a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2566a266c7d5SChris Wilson 
2567a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2568a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2569a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2570a266c7d5SChris Wilson 
2571a266c7d5SChris Wilson 			/*
2572a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2573a266c7d5SChris Wilson 			 */
2574a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2575a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2576a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2577a266c7d5SChris Wilson 							 pipe_name(pipe));
2578a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2579a266c7d5SChris Wilson 				irq_received = 1;
2580a266c7d5SChris Wilson 			}
2581a266c7d5SChris Wilson 		}
2582a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2583a266c7d5SChris Wilson 
2584a266c7d5SChris Wilson 		if (!irq_received)
2585a266c7d5SChris Wilson 			break;
2586a266c7d5SChris Wilson 
2587a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2588a266c7d5SChris Wilson 
2589a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2590adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2591a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2592a266c7d5SChris Wilson 
2593a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2594a266c7d5SChris Wilson 				  hotplug_status);
2595a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2596a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2597a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2598a266c7d5SChris Wilson 
2599a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2600a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2601a266c7d5SChris Wilson 		}
2602a266c7d5SChris Wilson 
2603a266c7d5SChris Wilson 		I915_WRITE(IIR, iir);
2604a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2605a266c7d5SChris Wilson 
2606a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2607a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2608a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2609a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2610a266c7d5SChris Wilson 
26114f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
2612a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 0);
2613a266c7d5SChris Wilson 
26144f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
2615a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 1);
2616a266c7d5SChris Wilson 
2617a266c7d5SChris Wilson 		for_each_pipe(pipe) {
26182c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
2619a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
2620a266c7d5SChris Wilson 				i915_pageflip_stall_check(dev, pipe);
2621a266c7d5SChris Wilson 				intel_finish_page_flip(dev, pipe);
2622a266c7d5SChris Wilson 			}
2623a266c7d5SChris Wilson 
2624a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2625a266c7d5SChris Wilson 				blc_event = true;
2626a266c7d5SChris Wilson 		}
2627a266c7d5SChris Wilson 
2628a266c7d5SChris Wilson 
2629a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2630a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2631a266c7d5SChris Wilson 
2632a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2633a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2634a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2635a266c7d5SChris Wilson 		 * we would never get another interrupt.
2636a266c7d5SChris Wilson 		 *
2637a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2638a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2639a266c7d5SChris Wilson 		 * another one.
2640a266c7d5SChris Wilson 		 *
2641a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2642a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2643a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2644a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2645a266c7d5SChris Wilson 		 * stray interrupts.
2646a266c7d5SChris Wilson 		 */
2647a266c7d5SChris Wilson 		iir = new_iir;
2648a266c7d5SChris Wilson 	}
2649a266c7d5SChris Wilson 
2650d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
26512c8ba29fSChris Wilson 
2652a266c7d5SChris Wilson 	return ret;
2653a266c7d5SChris Wilson }
2654a266c7d5SChris Wilson 
2655a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
2656a266c7d5SChris Wilson {
2657a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2658a266c7d5SChris Wilson 	int pipe;
2659a266c7d5SChris Wilson 
2660a266c7d5SChris Wilson 	if (!dev_priv)
2661a266c7d5SChris Wilson 		return;
2662a266c7d5SChris Wilson 
2663a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2664a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2665a266c7d5SChris Wilson 
2666a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
2667a266c7d5SChris Wilson 	for_each_pipe(pipe)
2668a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2669a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2670a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2671a266c7d5SChris Wilson 
2672a266c7d5SChris Wilson 	for_each_pipe(pipe)
2673a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
2674a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2675a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2676a266c7d5SChris Wilson }
2677a266c7d5SChris Wilson 
2678f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2679f71d4af4SJesse Barnes {
26808b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
26818b2e326dSChris Wilson 
26828b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
26838b2e326dSChris Wilson 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
2684c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
268598fd81cdSDaniel Vetter 	INIT_WORK(&dev_priv->parity_error_work, ivybridge_parity_work);
26868b2e326dSChris Wilson 
2687f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2688f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
26897d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
2690f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2691f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2692f71d4af4SJesse Barnes 	}
2693f71d4af4SJesse Barnes 
2694c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2695f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2696c3613de9SKeith Packard 	else
2697c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2698f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2699f71d4af4SJesse Barnes 
27007e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
27017e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
27027e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
27037e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
27047e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
27057e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
27067e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
27077e231dbeSJesse Barnes 	} else if (IS_IVYBRIDGE(dev)) {
2708f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2709f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2710f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2711f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2712f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2713f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2714f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
27157d4e146fSEugeni Dodonov 	} else if (IS_HASWELL(dev)) {
27167d4e146fSEugeni Dodonov 		/* Share interrupts handling with IVB */
27177d4e146fSEugeni Dodonov 		dev->driver->irq_handler = ivybridge_irq_handler;
27187d4e146fSEugeni Dodonov 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
27197d4e146fSEugeni Dodonov 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
27207d4e146fSEugeni Dodonov 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
27217d4e146fSEugeni Dodonov 		dev->driver->enable_vblank = ivybridge_enable_vblank;
27227d4e146fSEugeni Dodonov 		dev->driver->disable_vblank = ivybridge_disable_vblank;
2723f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2724f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2725f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2726f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2727f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2728f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2729f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
2730f71d4af4SJesse Barnes 	} else {
2731c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
2732c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
2733c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
2734c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
2735c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
2736a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
2737a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
2738a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
2739a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
2740a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
2741c2798b19SChris Wilson 		} else {
2742a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
2743a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
2744a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
2745a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
2746c2798b19SChris Wilson 		}
2747f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
2748f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
2749f71d4af4SJesse Barnes 	}
2750f71d4af4SJesse Barnes }
2751