xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 73edd18f610b6dd900cc3a180919dc643fff8513)
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 
299*73edd18fSDaniel Vetter static void ironlake_handle_rps_change(struct drm_device *dev)
300f97108d1SJesse Barnes {
301f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
302b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
303f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
304f97108d1SJesse Barnes 
305*73edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
306*73edd18fSDaniel Vetter 
3077648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
308b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
309b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
310f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
311f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
312f97108d1SJesse Barnes 
313f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
314b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
315f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
316f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
317f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
318f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
319b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
320f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
321f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
322f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
323f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
324f97108d1SJesse Barnes 	}
325f97108d1SJesse Barnes 
3267648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
327f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
328f97108d1SJesse Barnes 
329f97108d1SJesse Barnes 	return;
330f97108d1SJesse Barnes }
331f97108d1SJesse Barnes 
332549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
333549f7365SChris Wilson 			struct intel_ring_buffer *ring)
334549f7365SChris Wilson {
335549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
3369862e600SChris Wilson 
337475553deSChris Wilson 	if (ring->obj == NULL)
338475553deSChris Wilson 		return;
339475553deSChris Wilson 
3406d171cb4SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring));
3419862e600SChris Wilson 
342549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3433e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
344549f7365SChris Wilson 		dev_priv->hangcheck_count = 0;
345549f7365SChris Wilson 		mod_timer(&dev_priv->hangcheck_timer,
3463e0dc6b0SBen Widawsky 			  jiffies +
3473e0dc6b0SBen Widawsky 			  msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
3483e0dc6b0SBen Widawsky 	}
349549f7365SChris Wilson }
350549f7365SChris Wilson 
3514912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
3523b8d8d91SJesse Barnes {
3534912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
354c6a828d3SDaniel Vetter 						    rps.work);
3554912d041SBen Widawsky 	u32 pm_iir, pm_imr;
3567b9e0ae6SChris Wilson 	u8 new_delay;
3573b8d8d91SJesse Barnes 
358c6a828d3SDaniel Vetter 	spin_lock_irq(&dev_priv->rps.lock);
359c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
360c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
3614912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
362a9e2641dSDaniel Vetter 	I915_WRITE(GEN6_PMIMR, 0);
363c6a828d3SDaniel Vetter 	spin_unlock_irq(&dev_priv->rps.lock);
3644912d041SBen Widawsky 
3657b9e0ae6SChris Wilson 	if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
3663b8d8d91SJesse Barnes 		return;
3673b8d8d91SJesse Barnes 
3684912d041SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
3697b9e0ae6SChris Wilson 
3707b9e0ae6SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
371c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay + 1;
3727b9e0ae6SChris Wilson 	else
373c6a828d3SDaniel Vetter 		new_delay = dev_priv->rps.cur_delay - 1;
3743b8d8d91SJesse Barnes 
3754912d041SBen Widawsky 	gen6_set_rps(dev_priv->dev, new_delay);
3763b8d8d91SJesse Barnes 
3774912d041SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
3783b8d8d91SJesse Barnes }
3793b8d8d91SJesse Barnes 
380e3689190SBen Widawsky 
381e3689190SBen Widawsky /**
382e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
383e3689190SBen Widawsky  * occurred.
384e3689190SBen Widawsky  * @work: workqueue struct
385e3689190SBen Widawsky  *
386e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
387e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
388e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
389e3689190SBen Widawsky  */
390e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
391e3689190SBen Widawsky {
392e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
393e3689190SBen Widawsky 						    parity_error_work);
394e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
395e3689190SBen Widawsky 	char *parity_event[5];
396e3689190SBen Widawsky 	uint32_t misccpctl;
397e3689190SBen Widawsky 	unsigned long flags;
398e3689190SBen Widawsky 
399e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
400e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
401e3689190SBen Widawsky 	 * any time we access those registers.
402e3689190SBen Widawsky 	 */
403e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
404e3689190SBen Widawsky 
405e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
406e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
407e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
408e3689190SBen Widawsky 
409e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
410e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
411e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
412e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
413e3689190SBen Widawsky 
414e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
415e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
416e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
417e3689190SBen Widawsky 
418e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
419e3689190SBen Widawsky 
420e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
421e3689190SBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
422e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
423e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
424e3689190SBen Widawsky 
425e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
426e3689190SBen Widawsky 
427e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
428e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
429e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
430e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
431e3689190SBen Widawsky 	parity_event[4] = NULL;
432e3689190SBen Widawsky 
433e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
434e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
435e3689190SBen Widawsky 
436e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
437e3689190SBen Widawsky 		  row, bank, subbank);
438e3689190SBen Widawsky 
439e3689190SBen Widawsky 	kfree(parity_event[3]);
440e3689190SBen Widawsky 	kfree(parity_event[2]);
441e3689190SBen Widawsky 	kfree(parity_event[1]);
442e3689190SBen Widawsky }
443e3689190SBen Widawsky 
444d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
445e3689190SBen Widawsky {
446e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
447e3689190SBen Widawsky 	unsigned long flags;
448e3689190SBen Widawsky 
449e1ef7cc2SBen Widawsky 	if (!HAS_L3_GPU_CACHE(dev))
450e3689190SBen Widawsky 		return;
451e3689190SBen Widawsky 
452e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
453e3689190SBen Widawsky 	dev_priv->gt_irq_mask |= GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
454e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
455e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
456e3689190SBen Widawsky 
457e3689190SBen Widawsky 	queue_work(dev_priv->wq, &dev_priv->parity_error_work);
458e3689190SBen Widawsky }
459e3689190SBen Widawsky 
460e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
461e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
462e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
463e7b4c6b1SDaniel Vetter {
464e7b4c6b1SDaniel Vetter 
465e7b4c6b1SDaniel Vetter 	if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
466e7b4c6b1SDaniel Vetter 		      GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
467e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
468e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BSD_USER_INTERRUPT)
469e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
470e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
471e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
472e7b4c6b1SDaniel Vetter 
473e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
474e7b4c6b1SDaniel Vetter 		      GT_GEN6_BSD_CS_ERROR_INTERRUPT |
475e7b4c6b1SDaniel Vetter 		      GT_RENDER_CS_ERROR_INTERRUPT)) {
476e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
477e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
478e7b4c6b1SDaniel Vetter 	}
479e3689190SBen Widawsky 
480e3689190SBen Widawsky 	if (gt_iir & GT_GEN7_L3_PARITY_ERROR_INTERRUPT)
481e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
482e7b4c6b1SDaniel Vetter }
483e7b4c6b1SDaniel Vetter 
484fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
485fc6826d1SChris Wilson 				u32 pm_iir)
486fc6826d1SChris Wilson {
487fc6826d1SChris Wilson 	unsigned long flags;
488fc6826d1SChris Wilson 
489fc6826d1SChris Wilson 	/*
490fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
491fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
492fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
493c6a828d3SDaniel Vetter 	 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
494fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
495fc6826d1SChris Wilson 	 *
496c6a828d3SDaniel Vetter 	 * The mask bit in IMR is cleared by dev_priv->rps.work.
497fc6826d1SChris Wilson 	 */
498fc6826d1SChris Wilson 
499c6a828d3SDaniel Vetter 	spin_lock_irqsave(&dev_priv->rps.lock, flags);
500c6a828d3SDaniel Vetter 	WARN(dev_priv->rps.pm_iir & pm_iir, "Missed a PM interrupt\n");
501c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir |= pm_iir;
502c6a828d3SDaniel Vetter 	I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
503fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
504c6a828d3SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
505fc6826d1SChris Wilson 
506c6a828d3SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->rps.work);
507fc6826d1SChris Wilson }
508fc6826d1SChris Wilson 
5097e231dbeSJesse Barnes static irqreturn_t valleyview_irq_handler(DRM_IRQ_ARGS)
5107e231dbeSJesse Barnes {
5117e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
5127e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5137e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
5147e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
5157e231dbeSJesse Barnes 	unsigned long irqflags;
5167e231dbeSJesse Barnes 	int pipe;
5177e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
5187e231dbeSJesse Barnes 	bool blc_event;
5197e231dbeSJesse Barnes 
5207e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5217e231dbeSJesse Barnes 
5227e231dbeSJesse Barnes 	while (true) {
5237e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
5247e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
5257e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
5267e231dbeSJesse Barnes 
5277e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
5287e231dbeSJesse Barnes 			goto out;
5297e231dbeSJesse Barnes 
5307e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
5317e231dbeSJesse Barnes 
532e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
5337e231dbeSJesse Barnes 
5347e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5357e231dbeSJesse Barnes 		for_each_pipe(pipe) {
5367e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
5377e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
5387e231dbeSJesse Barnes 
5397e231dbeSJesse Barnes 			/*
5407e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
5417e231dbeSJesse Barnes 			 */
5427e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
5437e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
5447e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
5457e231dbeSJesse Barnes 							 pipe_name(pipe));
5467e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
5477e231dbeSJesse Barnes 			}
5487e231dbeSJesse Barnes 		}
5497e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5507e231dbeSJesse Barnes 
55131acc7f5SJesse Barnes 		for_each_pipe(pipe) {
55231acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
55331acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
55431acc7f5SJesse Barnes 
55531acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
55631acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
55731acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
55831acc7f5SJesse Barnes 			}
55931acc7f5SJesse Barnes 		}
56031acc7f5SJesse Barnes 
5617e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
5627e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
5637e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
5647e231dbeSJesse Barnes 
5657e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
5667e231dbeSJesse Barnes 					 hotplug_status);
5677e231dbeSJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
5687e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
5697e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
5707e231dbeSJesse Barnes 
5717e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
5727e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
5737e231dbeSJesse Barnes 		}
5747e231dbeSJesse Barnes 
5757e231dbeSJesse Barnes 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
5767e231dbeSJesse Barnes 			blc_event = true;
5777e231dbeSJesse Barnes 
578fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
579fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
5807e231dbeSJesse Barnes 
5817e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
5827e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
5837e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
5847e231dbeSJesse Barnes 	}
5857e231dbeSJesse Barnes 
5867e231dbeSJesse Barnes out:
5877e231dbeSJesse Barnes 	return ret;
5887e231dbeSJesse Barnes }
5897e231dbeSJesse Barnes 
59023e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
591776ad806SJesse Barnes {
592776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5939db4a9c7SJesse Barnes 	int pipe;
594776ad806SJesse Barnes 
595776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
596776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
597776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
598776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
599776ad806SJesse Barnes 
600776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
601776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
602776ad806SJesse Barnes 
603776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
604776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
605776ad806SJesse Barnes 
606776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
607776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
608776ad806SJesse Barnes 
609776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
610776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
611776ad806SJesse Barnes 
6129db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
6139db4a9c7SJesse Barnes 		for_each_pipe(pipe)
6149db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
6159db4a9c7SJesse Barnes 					 pipe_name(pipe),
6169db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
617776ad806SJesse Barnes 
618776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
619776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
620776ad806SJesse Barnes 
621776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
622776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
623776ad806SJesse Barnes 
624776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
625776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
626776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
627776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
628776ad806SJesse Barnes }
629776ad806SJesse Barnes 
63023e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
63123e81d69SAdam Jackson {
63223e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
63323e81d69SAdam Jackson 	int pipe;
63423e81d69SAdam Jackson 
63523e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT)
63623e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
63723e81d69SAdam Jackson 				 (pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
63823e81d69SAdam Jackson 				 SDE_AUDIO_POWER_SHIFT_CPT);
63923e81d69SAdam Jackson 
64023e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
64123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("AUX channel interrupt\n");
64223e81d69SAdam Jackson 
64323e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
64423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
64523e81d69SAdam Jackson 
64623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
64723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
64823e81d69SAdam Jackson 
64923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
65023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
65123e81d69SAdam Jackson 
65223e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
65323e81d69SAdam Jackson 		for_each_pipe(pipe)
65423e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
65523e81d69SAdam Jackson 					 pipe_name(pipe),
65623e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
65723e81d69SAdam Jackson }
65823e81d69SAdam Jackson 
659f71d4af4SJesse Barnes static irqreturn_t ivybridge_irq_handler(DRM_IRQ_ARGS)
660b1f14ad0SJesse Barnes {
661b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
662b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6630e43406bSChris Wilson 	u32 de_iir, gt_iir, de_ier, pm_iir;
6640e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
6650e43406bSChris Wilson 	int i;
666b1f14ad0SJesse Barnes 
667b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
668b1f14ad0SJesse Barnes 
669b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
670b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
671b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
6720e43406bSChris Wilson 
6730e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
6740e43406bSChris Wilson 	if (gt_iir) {
6750e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
6760e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
6770e43406bSChris Wilson 		ret = IRQ_HANDLED;
6780e43406bSChris Wilson 	}
679b1f14ad0SJesse Barnes 
680b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
6810e43406bSChris Wilson 	if (de_iir) {
682b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
683b1f14ad0SJesse Barnes 			intel_opregion_gse_intr(dev);
684b1f14ad0SJesse Barnes 
6850e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
6860e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
6870e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
6880e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
689b1f14ad0SJesse Barnes 			}
6900e43406bSChris Wilson 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
6910e43406bSChris Wilson 				drm_handle_vblank(dev, i);
692b1f14ad0SJesse Barnes 		}
693b1f14ad0SJesse Barnes 
694b1f14ad0SJesse Barnes 		/* check event from PCH */
695b1f14ad0SJesse Barnes 		if (de_iir & DE_PCH_EVENT_IVB) {
6960e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
6970e43406bSChris Wilson 
698b1f14ad0SJesse Barnes 			if (pch_iir & SDE_HOTPLUG_MASK_CPT)
699b1f14ad0SJesse Barnes 				queue_work(dev_priv->wq, &dev_priv->hotplug_work);
70023e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
7010e43406bSChris Wilson 
7020e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
7030e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
704b1f14ad0SJesse Barnes 		}
705b1f14ad0SJesse Barnes 
7060e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
7070e43406bSChris Wilson 		ret = IRQ_HANDLED;
7080e43406bSChris Wilson 	}
7090e43406bSChris Wilson 
7100e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
7110e43406bSChris Wilson 	if (pm_iir) {
712fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
713fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
714b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
7150e43406bSChris Wilson 		ret = IRQ_HANDLED;
7160e43406bSChris Wilson 	}
717b1f14ad0SJesse Barnes 
718b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
719b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
720b1f14ad0SJesse Barnes 
721b1f14ad0SJesse Barnes 	return ret;
722b1f14ad0SJesse Barnes }
723b1f14ad0SJesse Barnes 
724e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
725e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
726e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
727e7b4c6b1SDaniel Vetter {
728e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
729e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
730e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
731e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
732e7b4c6b1SDaniel Vetter }
733e7b4c6b1SDaniel Vetter 
734f71d4af4SJesse Barnes static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
735036a4a7dSZhenyu Wang {
7364697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
737036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
738036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
7393b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
7402d7b8366SYuanhan Liu 	u32 hotplug_mask;
741881f47b6SXiang, Haihao 
7424697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
7434697995bSJesse Barnes 
7442d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
7452d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
7462d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
7473143a2bfSChris Wilson 	POSTING_READ(DEIER);
7482d109a84SZou, Nanhai 
749036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
750036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
751c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
7523b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
753036a4a7dSZhenyu Wang 
7543b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
7553b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
756c7c85101SZou Nan hai 		goto done;
757036a4a7dSZhenyu Wang 
7582d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
7592d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
7602d7b8366SYuanhan Liu 	else
7612d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
7622d7b8366SYuanhan Liu 
763036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
764036a4a7dSZhenyu Wang 
765e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
766e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
767e7b4c6b1SDaniel Vetter 	else
768e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
769036a4a7dSZhenyu Wang 
77001c66889SZhao Yakui 	if (de_iir & DE_GSE)
7713b617967SChris Wilson 		intel_opregion_gse_intr(dev);
77201c66889SZhao Yakui 
773f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
774013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
7752bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
776013d5aa2SJesse Barnes 	}
777013d5aa2SJesse Barnes 
778f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
779f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
7802bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
781013d5aa2SJesse Barnes 	}
782c062df61SLi Peng 
783f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
784f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
785f072d2e7SZhenyu Wang 
786f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
787f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
788f072d2e7SZhenyu Wang 
789c650156aSZhenyu Wang 	/* check event from PCH */
790776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
791776ad806SJesse Barnes 		if (pch_iir & hotplug_mask)
792c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
79323e81d69SAdam Jackson 		if (HAS_PCH_CPT(dev))
79423e81d69SAdam Jackson 			cpt_irq_handler(dev, pch_iir);
79523e81d69SAdam Jackson 		else
79623e81d69SAdam Jackson 			ibx_irq_handler(dev, pch_iir);
797776ad806SJesse Barnes 	}
798c650156aSZhenyu Wang 
799*73edd18fSDaniel Vetter 	if (IS_GEN5(dev) &&  de_iir & DE_PCU_EVENT)
800*73edd18fSDaniel Vetter 		ironlake_handle_rps_change(dev);
801f97108d1SJesse Barnes 
802fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
803fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
8043b8d8d91SJesse Barnes 
805c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
806c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
807c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
808c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
8094912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
810036a4a7dSZhenyu Wang 
811c7c85101SZou Nan hai done:
8122d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
8133143a2bfSChris Wilson 	POSTING_READ(DEIER);
8142d109a84SZou, Nanhai 
815036a4a7dSZhenyu Wang 	return ret;
816036a4a7dSZhenyu Wang }
817036a4a7dSZhenyu Wang 
8188a905236SJesse Barnes /**
8198a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
8208a905236SJesse Barnes  * @work: work struct
8218a905236SJesse Barnes  *
8228a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
8238a905236SJesse Barnes  * was detected.
8248a905236SJesse Barnes  */
8258a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
8268a905236SJesse Barnes {
8278a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
8288a905236SJesse Barnes 						    error_work);
8298a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
830f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
831f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
832f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
8338a905236SJesse Barnes 
834f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
8358a905236SJesse Barnes 
836ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
83744d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
838f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
839d4b8bb2aSDaniel Vetter 		if (!i915_reset(dev)) {
840ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
841f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
842f316a42cSBen Gamari 		}
84330dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
844f316a42cSBen Gamari 	}
8458a905236SJesse Barnes }
8468a905236SJesse Barnes 
8473bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
8489df30794SChris Wilson static struct drm_i915_error_object *
849bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
85005394f39SChris Wilson 			 struct drm_i915_gem_object *src)
8519df30794SChris Wilson {
8529df30794SChris Wilson 	struct drm_i915_error_object *dst;
8539df30794SChris Wilson 	int page, page_count;
854e56660ddSChris Wilson 	u32 reloc_offset;
8559df30794SChris Wilson 
85605394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
8579df30794SChris Wilson 		return NULL;
8589df30794SChris Wilson 
85905394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
8609df30794SChris Wilson 
8619df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof(u32 *), GFP_ATOMIC);
8629df30794SChris Wilson 	if (dst == NULL)
8639df30794SChris Wilson 		return NULL;
8649df30794SChris Wilson 
86505394f39SChris Wilson 	reloc_offset = src->gtt_offset;
8669df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
867788885aeSAndrew Morton 		unsigned long flags;
868e56660ddSChris Wilson 		void *d;
869788885aeSAndrew Morton 
870e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
8719df30794SChris Wilson 		if (d == NULL)
8729df30794SChris Wilson 			goto unwind;
873e56660ddSChris Wilson 
874788885aeSAndrew Morton 		local_irq_save(flags);
87574898d7eSDaniel Vetter 		if (reloc_offset < dev_priv->mm.gtt_mappable_end &&
87674898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
877172975aaSChris Wilson 			void __iomem *s;
878172975aaSChris Wilson 
879172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
880172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
881172975aaSChris Wilson 			 * captures what the GPU read.
882172975aaSChris Wilson 			 */
883172975aaSChris Wilson 
884e56660ddSChris Wilson 			s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
8853e4d3af5SPeter Zijlstra 						     reloc_offset);
886e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
8873e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
888172975aaSChris Wilson 		} else {
889172975aaSChris Wilson 			void *s;
890172975aaSChris Wilson 
891172975aaSChris Wilson 			drm_clflush_pages(&src->pages[page], 1);
892172975aaSChris Wilson 
893172975aaSChris Wilson 			s = kmap_atomic(src->pages[page]);
894172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
895172975aaSChris Wilson 			kunmap_atomic(s);
896172975aaSChris Wilson 
897172975aaSChris Wilson 			drm_clflush_pages(&src->pages[page], 1);
898172975aaSChris Wilson 		}
899788885aeSAndrew Morton 		local_irq_restore(flags);
900e56660ddSChris Wilson 
9019df30794SChris Wilson 		dst->pages[page] = d;
902e56660ddSChris Wilson 
903e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
9049df30794SChris Wilson 	}
9059df30794SChris Wilson 	dst->page_count = page_count;
90605394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
9079df30794SChris Wilson 
9089df30794SChris Wilson 	return dst;
9099df30794SChris Wilson 
9109df30794SChris Wilson unwind:
9119df30794SChris Wilson 	while (page--)
9129df30794SChris Wilson 		kfree(dst->pages[page]);
9139df30794SChris Wilson 	kfree(dst);
9149df30794SChris Wilson 	return NULL;
9159df30794SChris Wilson }
9169df30794SChris Wilson 
9179df30794SChris Wilson static void
9189df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
9199df30794SChris Wilson {
9209df30794SChris Wilson 	int page;
9219df30794SChris Wilson 
9229df30794SChris Wilson 	if (obj == NULL)
9239df30794SChris Wilson 		return;
9249df30794SChris Wilson 
9259df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
9269df30794SChris Wilson 		kfree(obj->pages[page]);
9279df30794SChris Wilson 
9289df30794SChris Wilson 	kfree(obj);
9299df30794SChris Wilson }
9309df30794SChris Wilson 
931742cbee8SDaniel Vetter void
932742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
9339df30794SChris Wilson {
934742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
935742cbee8SDaniel Vetter 							  typeof(*error), ref);
936e2f973d5SChris Wilson 	int i;
937e2f973d5SChris Wilson 
93852d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
93952d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
94052d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
94152d39a21SChris Wilson 		kfree(error->ring[i].requests);
94252d39a21SChris Wilson 	}
943e2f973d5SChris Wilson 
9449df30794SChris Wilson 	kfree(error->active_bo);
9456ef3d427SChris Wilson 	kfree(error->overlay);
9469df30794SChris Wilson 	kfree(error);
9479df30794SChris Wilson }
9481b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
9491b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
950c724e8a9SChris Wilson {
951c724e8a9SChris Wilson 	err->size = obj->base.size;
952c724e8a9SChris Wilson 	err->name = obj->base.name;
9530201f1ecSChris Wilson 	err->rseqno = obj->last_read_seqno;
9540201f1ecSChris Wilson 	err->wseqno = obj->last_write_seqno;
955c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
956c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
957c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
958c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
959c724e8a9SChris Wilson 	err->pinned = 0;
960c724e8a9SChris Wilson 	if (obj->pin_count > 0)
961c724e8a9SChris Wilson 		err->pinned = 1;
962c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
963c724e8a9SChris Wilson 		err->pinned = -1;
964c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
965c724e8a9SChris Wilson 	err->dirty = obj->dirty;
966c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
96796154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
96893dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
9691b50247aSChris Wilson }
970c724e8a9SChris Wilson 
9711b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
9721b50247aSChris Wilson 			     int count, struct list_head *head)
9731b50247aSChris Wilson {
9741b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
9751b50247aSChris Wilson 	int i = 0;
9761b50247aSChris Wilson 
9771b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
9781b50247aSChris Wilson 		capture_bo(err++, obj);
979c724e8a9SChris Wilson 		if (++i == count)
980c724e8a9SChris Wilson 			break;
9811b50247aSChris Wilson 	}
982c724e8a9SChris Wilson 
9831b50247aSChris Wilson 	return i;
9841b50247aSChris Wilson }
9851b50247aSChris Wilson 
9861b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
9871b50247aSChris Wilson 			     int count, struct list_head *head)
9881b50247aSChris Wilson {
9891b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
9901b50247aSChris Wilson 	int i = 0;
9911b50247aSChris Wilson 
9921b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
9931b50247aSChris Wilson 		if (obj->pin_count == 0)
9941b50247aSChris Wilson 			continue;
9951b50247aSChris Wilson 
9961b50247aSChris Wilson 		capture_bo(err++, obj);
9971b50247aSChris Wilson 		if (++i == count)
9981b50247aSChris Wilson 			break;
999c724e8a9SChris Wilson 	}
1000c724e8a9SChris Wilson 
1001c724e8a9SChris Wilson 	return i;
1002c724e8a9SChris Wilson }
1003c724e8a9SChris Wilson 
1004748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
1005748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
1006748ebc60SChris Wilson {
1007748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1008748ebc60SChris Wilson 	int i;
1009748ebc60SChris Wilson 
1010748ebc60SChris Wilson 	/* Fences */
1011748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
1012775d17b6SDaniel Vetter 	case 7:
1013748ebc60SChris Wilson 	case 6:
1014748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1015748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1016748ebc60SChris Wilson 		break;
1017748ebc60SChris Wilson 	case 5:
1018748ebc60SChris Wilson 	case 4:
1019748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
1020748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1021748ebc60SChris Wilson 		break;
1022748ebc60SChris Wilson 	case 3:
1023748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1024748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1025748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1026748ebc60SChris Wilson 	case 2:
1027748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1028748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1029748ebc60SChris Wilson 		break;
1030748ebc60SChris Wilson 
1031748ebc60SChris Wilson 	}
1032748ebc60SChris Wilson }
1033748ebc60SChris Wilson 
1034bcfb2e28SChris Wilson static struct drm_i915_error_object *
1035bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1036bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1037bcfb2e28SChris Wilson {
1038bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1039bcfb2e28SChris Wilson 	u32 seqno;
1040bcfb2e28SChris Wilson 
1041bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1042bcfb2e28SChris Wilson 		return NULL;
1043bcfb2e28SChris Wilson 
1044bcfb2e28SChris Wilson 	seqno = ring->get_seqno(ring);
1045bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1046bcfb2e28SChris Wilson 		if (obj->ring != ring)
1047bcfb2e28SChris Wilson 			continue;
1048bcfb2e28SChris Wilson 
10490201f1ecSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_read_seqno))
1050bcfb2e28SChris Wilson 			continue;
1051bcfb2e28SChris Wilson 
1052bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1053bcfb2e28SChris Wilson 			continue;
1054bcfb2e28SChris Wilson 
1055bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1056bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1057bcfb2e28SChris Wilson 		 */
1058bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1059bcfb2e28SChris Wilson 	}
1060bcfb2e28SChris Wilson 
1061bcfb2e28SChris Wilson 	return NULL;
1062bcfb2e28SChris Wilson }
1063bcfb2e28SChris Wilson 
1064d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1065d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1066d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1067d27b1e0eSDaniel Vetter {
1068d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1069d27b1e0eSDaniel Vetter 
107033f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
107112f55818SChris Wilson 		error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
107233f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
10737e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
10747e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
10757e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
10767e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
107733f3f518SDaniel Vetter 	}
1078c1cd90edSDaniel Vetter 
1079d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
10809d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1081d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1082d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1083d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1084c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1085d27b1e0eSDaniel Vetter 		if (ring->id == RCS) {
1086d27b1e0eSDaniel Vetter 			error->instdone1 = I915_READ(INSTDONE1);
1087d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1088d27b1e0eSDaniel Vetter 		}
1089d27b1e0eSDaniel Vetter 	} else {
10909d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1091d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1092d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1093d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1094d27b1e0eSDaniel Vetter 	}
1095d27b1e0eSDaniel Vetter 
10969574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1097c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1098d27b1e0eSDaniel Vetter 	error->seqno[ring->id] = ring->get_seqno(ring);
1099d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1100c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1101c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
11027e3b8737SDaniel Vetter 
11037e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
11047e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1105d27b1e0eSDaniel Vetter }
1106d27b1e0eSDaniel Vetter 
110752d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
110852d39a21SChris Wilson 				  struct drm_i915_error_state *error)
110952d39a21SChris Wilson {
111052d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1111b4519513SChris Wilson 	struct intel_ring_buffer *ring;
111252d39a21SChris Wilson 	struct drm_i915_gem_request *request;
111352d39a21SChris Wilson 	int i, count;
111452d39a21SChris Wilson 
1115b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
111652d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
111752d39a21SChris Wilson 
111852d39a21SChris Wilson 		error->ring[i].batchbuffer =
111952d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
112052d39a21SChris Wilson 
112152d39a21SChris Wilson 		error->ring[i].ringbuffer =
112252d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
112352d39a21SChris Wilson 
112452d39a21SChris Wilson 		count = 0;
112552d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
112652d39a21SChris Wilson 			count++;
112752d39a21SChris Wilson 
112852d39a21SChris Wilson 		error->ring[i].num_requests = count;
112952d39a21SChris Wilson 		error->ring[i].requests =
113052d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
113152d39a21SChris Wilson 				GFP_ATOMIC);
113252d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
113352d39a21SChris Wilson 			error->ring[i].num_requests = 0;
113452d39a21SChris Wilson 			continue;
113552d39a21SChris Wilson 		}
113652d39a21SChris Wilson 
113752d39a21SChris Wilson 		count = 0;
113852d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
113952d39a21SChris Wilson 			struct drm_i915_error_request *erq;
114052d39a21SChris Wilson 
114152d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
114252d39a21SChris Wilson 			erq->seqno = request->seqno;
114352d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1144ee4f42b1SChris Wilson 			erq->tail = request->tail;
114552d39a21SChris Wilson 		}
114652d39a21SChris Wilson 	}
114752d39a21SChris Wilson }
114852d39a21SChris Wilson 
11498a905236SJesse Barnes /**
11508a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
11518a905236SJesse Barnes  * @dev: drm device
11528a905236SJesse Barnes  *
11538a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
11548a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
11558a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
11568a905236SJesse Barnes  * to pick up.
11578a905236SJesse Barnes  */
115863eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
115963eeaf38SJesse Barnes {
116063eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
116105394f39SChris Wilson 	struct drm_i915_gem_object *obj;
116263eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
116363eeaf38SJesse Barnes 	unsigned long flags;
11649db4a9c7SJesse Barnes 	int i, pipe;
116563eeaf38SJesse Barnes 
116663eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
11679df30794SChris Wilson 	error = dev_priv->first_error;
11689df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
11699df30794SChris Wilson 	if (error)
11709df30794SChris Wilson 		return;
117163eeaf38SJesse Barnes 
11729db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
117333f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
117463eeaf38SJesse Barnes 	if (!error) {
11759df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
11769df30794SChris Wilson 		return;
117763eeaf38SJesse Barnes 	}
117863eeaf38SJesse Barnes 
1179b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
1180b6f7833bSChris Wilson 		 dev->primary->index);
11812fa772f3SChris Wilson 
1182742cbee8SDaniel Vetter 	kref_init(&error->ref);
118363eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
118463eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1185b9a3906bSBen Widawsky 	error->ccid = I915_READ(CCID);
1186be998e2eSBen Widawsky 
1187be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1188be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1189be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1190be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1191be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1192be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1193be998e2eSBen Widawsky 	else
1194be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1195be998e2eSBen Widawsky 
11969db4a9c7SJesse Barnes 	for_each_pipe(pipe)
11979db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1198d27b1e0eSDaniel Vetter 
119933f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1200f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
120133f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
120233f3f518SDaniel Vetter 	}
1203add354ddSChris Wilson 
1204748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
120552d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
12069df30794SChris Wilson 
1207c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
12089df30794SChris Wilson 	error->active_bo = NULL;
1209c724e8a9SChris Wilson 	error->pinned_bo = NULL;
12109df30794SChris Wilson 
1211bcfb2e28SChris Wilson 	i = 0;
1212bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1213bcfb2e28SChris Wilson 		i++;
1214bcfb2e28SChris Wilson 	error->active_bo_count = i;
12151b50247aSChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list)
12161b50247aSChris Wilson 		if (obj->pin_count)
1217bcfb2e28SChris Wilson 			i++;
1218bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1219c724e8a9SChris Wilson 
12208e934dbfSChris Wilson 	error->active_bo = NULL;
12218e934dbfSChris Wilson 	error->pinned_bo = NULL;
1222bcfb2e28SChris Wilson 	if (i) {
1223bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
12249df30794SChris Wilson 					   GFP_ATOMIC);
1225c724e8a9SChris Wilson 		if (error->active_bo)
1226c724e8a9SChris Wilson 			error->pinned_bo =
1227c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
12289df30794SChris Wilson 	}
1229c724e8a9SChris Wilson 
1230c724e8a9SChris Wilson 	if (error->active_bo)
1231c724e8a9SChris Wilson 		error->active_bo_count =
12321b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1233c724e8a9SChris Wilson 					  error->active_bo_count,
1234c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1235c724e8a9SChris Wilson 
1236c724e8a9SChris Wilson 	if (error->pinned_bo)
1237c724e8a9SChris Wilson 		error->pinned_bo_count =
12381b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1239c724e8a9SChris Wilson 					  error->pinned_bo_count,
12401b50247aSChris Wilson 					  &dev_priv->mm.gtt_list);
124163eeaf38SJesse Barnes 
12428a905236SJesse Barnes 	do_gettimeofday(&error->time);
12438a905236SJesse Barnes 
12446ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1245c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
12466ef3d427SChris Wilson 
12479df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12489df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
124963eeaf38SJesse Barnes 		dev_priv->first_error = error;
12509df30794SChris Wilson 		error = NULL;
12519df30794SChris Wilson 	}
125263eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
12539df30794SChris Wilson 
12549df30794SChris Wilson 	if (error)
1255742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
12569df30794SChris Wilson }
12579df30794SChris Wilson 
12589df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
12599df30794SChris Wilson {
12609df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
12619df30794SChris Wilson 	struct drm_i915_error_state *error;
12626dc0e816SBen Widawsky 	unsigned long flags;
12639df30794SChris Wilson 
12646dc0e816SBen Widawsky 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12659df30794SChris Wilson 	error = dev_priv->first_error;
12669df30794SChris Wilson 	dev_priv->first_error = NULL;
12676dc0e816SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
12689df30794SChris Wilson 
12699df30794SChris Wilson 	if (error)
1270742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
127163eeaf38SJesse Barnes }
12723bd3c932SChris Wilson #else
12733bd3c932SChris Wilson #define i915_capture_error_state(x)
12743bd3c932SChris Wilson #endif
127563eeaf38SJesse Barnes 
127635aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1277c0e09200SDave Airlie {
12788a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
127963eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
12809db4a9c7SJesse Barnes 	int pipe;
128163eeaf38SJesse Barnes 
128235aed2e6SChris Wilson 	if (!eir)
128335aed2e6SChris Wilson 		return;
128463eeaf38SJesse Barnes 
1285a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
12868a905236SJesse Barnes 
12878a905236SJesse Barnes 	if (IS_G4X(dev)) {
12888a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
12898a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
12908a905236SJesse Barnes 
1291a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1292a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1293a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n",
12948a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
1295a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1296a70491ccSJoe Perches 			pr_err("  INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1297a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
12988a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
12993143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
13008a905236SJesse Barnes 		}
13018a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
13028a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1303a70491ccSJoe Perches 			pr_err("page table error\n");
1304a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
13058a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
13063143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
13078a905236SJesse Barnes 		}
13088a905236SJesse Barnes 	}
13098a905236SJesse Barnes 
1310a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
131163eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
131263eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1313a70491ccSJoe Perches 			pr_err("page table error\n");
1314a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
131563eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
13163143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
131763eeaf38SJesse Barnes 		}
13188a905236SJesse Barnes 	}
13198a905236SJesse Barnes 
132063eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1321a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
13229db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1323a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
13249db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
132563eeaf38SJesse Barnes 		/* pipestat has already been acked */
132663eeaf38SJesse Barnes 	}
132763eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1328a70491ccSJoe Perches 		pr_err("instruction error\n");
1329a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1330a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
133163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
133263eeaf38SJesse Barnes 
1333a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1334a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1335a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n", I915_READ(INSTDONE));
1336a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
133763eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
13383143a2bfSChris Wilson 			POSTING_READ(IPEIR);
133963eeaf38SJesse Barnes 		} else {
134063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
134163eeaf38SJesse Barnes 
1342a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1343a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1344a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n",
134563eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
1346a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1347a70491ccSJoe Perches 			pr_err("  INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1348a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
134963eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
13503143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
135163eeaf38SJesse Barnes 		}
135263eeaf38SJesse Barnes 	}
135363eeaf38SJesse Barnes 
135463eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
13553143a2bfSChris Wilson 	POSTING_READ(EIR);
135663eeaf38SJesse Barnes 	eir = I915_READ(EIR);
135763eeaf38SJesse Barnes 	if (eir) {
135863eeaf38SJesse Barnes 		/*
135963eeaf38SJesse Barnes 		 * some errors might have become stuck,
136063eeaf38SJesse Barnes 		 * mask them.
136163eeaf38SJesse Barnes 		 */
136263eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
136363eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
136463eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
136563eeaf38SJesse Barnes 	}
136635aed2e6SChris Wilson }
136735aed2e6SChris Wilson 
136835aed2e6SChris Wilson /**
136935aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
137035aed2e6SChris Wilson  * @dev: drm device
137135aed2e6SChris Wilson  *
137235aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
137335aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
137435aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
137535aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
137635aed2e6SChris Wilson  * of a ring dump etc.).
137735aed2e6SChris Wilson  */
1378527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
137935aed2e6SChris Wilson {
138035aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1381b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1382b4519513SChris Wilson 	int i;
138335aed2e6SChris Wilson 
138435aed2e6SChris Wilson 	i915_capture_error_state(dev);
138535aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
13868a905236SJesse Barnes 
1387ba1234d1SBen Gamari 	if (wedged) {
138830dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1389ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1390ba1234d1SBen Gamari 
139111ed50ecSBen Gamari 		/*
139211ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
139311ed50ecSBen Gamari 		 */
1394b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1395b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
139611ed50ecSBen Gamari 	}
139711ed50ecSBen Gamari 
13989c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
13998a905236SJesse Barnes }
14008a905236SJesse Barnes 
14014e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
14024e5359cdSSimon Farnsworth {
14034e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
14044e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
14054e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
140605394f39SChris Wilson 	struct drm_i915_gem_object *obj;
14074e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
14084e5359cdSSimon Farnsworth 	unsigned long flags;
14094e5359cdSSimon Farnsworth 	bool stall_detected;
14104e5359cdSSimon Farnsworth 
14114e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
14124e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
14134e5359cdSSimon Farnsworth 		return;
14144e5359cdSSimon Farnsworth 
14154e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
14164e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
14174e5359cdSSimon Farnsworth 
14184e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
14194e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
14204e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
14214e5359cdSSimon Farnsworth 		return;
14224e5359cdSSimon Farnsworth 	}
14234e5359cdSSimon Farnsworth 
14244e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
142505394f39SChris Wilson 	obj = work->pending_flip_obj;
1426a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
14279db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1428446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1429446f2545SArmin Reese 					obj->gtt_offset;
14304e5359cdSSimon Farnsworth 	} else {
14319db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
143205394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
143301f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
14344e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
14354e5359cdSSimon Farnsworth 	}
14364e5359cdSSimon Farnsworth 
14374e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
14384e5359cdSSimon Farnsworth 
14394e5359cdSSimon Farnsworth 	if (stall_detected) {
14404e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
14414e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
14424e5359cdSSimon Farnsworth 	}
14434e5359cdSSimon Farnsworth }
14444e5359cdSSimon Farnsworth 
144542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
144642f52ef8SKeith Packard  * we use as a pipe index
144742f52ef8SKeith Packard  */
1448f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
14490a3e67a4SJesse Barnes {
14500a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1451e9d21d7fSKeith Packard 	unsigned long irqflags;
145271e0ffa5SJesse Barnes 
14535eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
145471e0ffa5SJesse Barnes 		return -EINVAL;
14550a3e67a4SJesse Barnes 
14561ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1457f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
14587c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14597c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
14600a3e67a4SJesse Barnes 	else
14617c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14627c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
14638692d00eSChris Wilson 
14648692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
14658692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
14666b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
14671ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14688692d00eSChris Wilson 
14690a3e67a4SJesse Barnes 	return 0;
14700a3e67a4SJesse Barnes }
14710a3e67a4SJesse Barnes 
1472f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1473f796cf8fSJesse Barnes {
1474f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1475f796cf8fSJesse Barnes 	unsigned long irqflags;
1476f796cf8fSJesse Barnes 
1477f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1478f796cf8fSJesse Barnes 		return -EINVAL;
1479f796cf8fSJesse Barnes 
1480f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1481f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1482f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1483f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1484f796cf8fSJesse Barnes 
1485f796cf8fSJesse Barnes 	return 0;
1486f796cf8fSJesse Barnes }
1487f796cf8fSJesse Barnes 
1488f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1489b1f14ad0SJesse Barnes {
1490b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1491b1f14ad0SJesse Barnes 	unsigned long irqflags;
1492b1f14ad0SJesse Barnes 
1493b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1494b1f14ad0SJesse Barnes 		return -EINVAL;
1495b1f14ad0SJesse Barnes 
1496b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1497b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
1498b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
1499b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1500b1f14ad0SJesse Barnes 
1501b1f14ad0SJesse Barnes 	return 0;
1502b1f14ad0SJesse Barnes }
1503b1f14ad0SJesse Barnes 
15047e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
15057e231dbeSJesse Barnes {
15067e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
15077e231dbeSJesse Barnes 	unsigned long irqflags;
150831acc7f5SJesse Barnes 	u32 imr;
15097e231dbeSJesse Barnes 
15107e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
15117e231dbeSJesse Barnes 		return -EINVAL;
15127e231dbeSJesse Barnes 
15137e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15147e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
151531acc7f5SJesse Barnes 	if (pipe == 0)
15167e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
151731acc7f5SJesse Barnes 	else
15187e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
15197e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
152031acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
152131acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
15227e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15237e231dbeSJesse Barnes 
15247e231dbeSJesse Barnes 	return 0;
15257e231dbeSJesse Barnes }
15267e231dbeSJesse Barnes 
152742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
152842f52ef8SKeith Packard  * we use as a pipe index
152942f52ef8SKeith Packard  */
1530f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
15310a3e67a4SJesse Barnes {
15320a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1533e9d21d7fSKeith Packard 	unsigned long irqflags;
15340a3e67a4SJesse Barnes 
15351ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15368692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15376b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
15388692d00eSChris Wilson 
15397c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
15407c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
15417c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
15421ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15430a3e67a4SJesse Barnes }
15440a3e67a4SJesse Barnes 
1545f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1546f796cf8fSJesse Barnes {
1547f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1548f796cf8fSJesse Barnes 	unsigned long irqflags;
1549f796cf8fSJesse Barnes 
1550f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1551f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1552f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1553f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1554f796cf8fSJesse Barnes }
1555f796cf8fSJesse Barnes 
1556f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1557b1f14ad0SJesse Barnes {
1558b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1559b1f14ad0SJesse Barnes 	unsigned long irqflags;
1560b1f14ad0SJesse Barnes 
1561b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1562b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
1563b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
1564b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1565b1f14ad0SJesse Barnes }
1566b1f14ad0SJesse Barnes 
15677e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
15687e231dbeSJesse Barnes {
15697e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
15707e231dbeSJesse Barnes 	unsigned long irqflags;
157131acc7f5SJesse Barnes 	u32 imr;
15727e231dbeSJesse Barnes 
15737e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
157431acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
157531acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
15767e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
157731acc7f5SJesse Barnes 	if (pipe == 0)
15787e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
157931acc7f5SJesse Barnes 	else
15807e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
15817e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
15827e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15837e231dbeSJesse Barnes }
15847e231dbeSJesse Barnes 
1585893eead0SChris Wilson static u32
1586893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1587852835f3SZou Nan hai {
1588893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1589893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1590893eead0SChris Wilson }
1591893eead0SChris Wilson 
1592893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1593893eead0SChris Wilson {
1594893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1595893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1596893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
15979574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
15989574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
15999574b3feSBen Widawsky 				  ring->name);
1600893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1601893eead0SChris Wilson 			*err = true;
1602893eead0SChris Wilson 		}
1603893eead0SChris Wilson 		return true;
1604893eead0SChris Wilson 	}
1605893eead0SChris Wilson 	return false;
1606f65d9421SBen Gamari }
1607f65d9421SBen Gamari 
16081ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
16091ec14ad3SChris Wilson {
16101ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
16111ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
16121ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
16131ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
16141ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
16151ec14ad3SChris Wilson 			  ring->name);
16161ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
16171ec14ad3SChris Wilson 		return true;
16181ec14ad3SChris Wilson 	}
16191ec14ad3SChris Wilson 	return false;
16201ec14ad3SChris Wilson }
16211ec14ad3SChris Wilson 
1622d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
1623d1e61e7fSChris Wilson {
1624d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1625d1e61e7fSChris Wilson 
1626d1e61e7fSChris Wilson 	if (dev_priv->hangcheck_count++ > 1) {
1627b4519513SChris Wilson 		bool hung = true;
1628b4519513SChris Wilson 
1629d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1630d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
1631d1e61e7fSChris Wilson 
1632d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
1633b4519513SChris Wilson 			struct intel_ring_buffer *ring;
1634b4519513SChris Wilson 			int i;
1635b4519513SChris Wilson 
1636d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
1637d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
1638d1e61e7fSChris Wilson 			 * and break the hang. This should work on
1639d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
1640d1e61e7fSChris Wilson 			 */
1641b4519513SChris Wilson 			for_each_ring(ring, dev_priv, i)
1642b4519513SChris Wilson 				hung &= !kick_ring(ring);
1643d1e61e7fSChris Wilson 		}
1644d1e61e7fSChris Wilson 
1645b4519513SChris Wilson 		return hung;
1646d1e61e7fSChris Wilson 	}
1647d1e61e7fSChris Wilson 
1648d1e61e7fSChris Wilson 	return false;
1649d1e61e7fSChris Wilson }
1650d1e61e7fSChris Wilson 
1651f65d9421SBen Gamari /**
1652f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1653f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1654f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1655f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1656f65d9421SBen Gamari  */
1657f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1658f65d9421SBen Gamari {
1659f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1660f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1661b4519513SChris Wilson 	uint32_t acthd[I915_NUM_RINGS], instdone, instdone1;
1662b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1663b4519513SChris Wilson 	bool err = false, idle;
1664b4519513SChris Wilson 	int i;
1665893eead0SChris Wilson 
16663e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
16673e0dc6b0SBen Widawsky 		return;
16683e0dc6b0SBen Widawsky 
1669b4519513SChris Wilson 	memset(acthd, 0, sizeof(acthd));
1670b4519513SChris Wilson 	idle = true;
1671b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
1672b4519513SChris Wilson 	    idle &= i915_hangcheck_ring_idle(ring, &err);
1673b4519513SChris Wilson 	    acthd[i] = intel_ring_get_active_head(ring);
1674b4519513SChris Wilson 	}
1675b4519513SChris Wilson 
1676893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
1677b4519513SChris Wilson 	if (idle) {
1678d1e61e7fSChris Wilson 		if (err) {
1679d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
1680d1e61e7fSChris Wilson 				return;
1681d1e61e7fSChris Wilson 
1682893eead0SChris Wilson 			goto repeat;
1683d1e61e7fSChris Wilson 		}
1684d1e61e7fSChris Wilson 
1685d1e61e7fSChris Wilson 		dev_priv->hangcheck_count = 0;
1686893eead0SChris Wilson 		return;
1687893eead0SChris Wilson 	}
1688f65d9421SBen Gamari 
1689a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1690cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1691cbb465e7SChris Wilson 		instdone1 = 0;
1692cbb465e7SChris Wilson 	} else {
1693cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1694cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1695cbb465e7SChris Wilson 	}
1696f65d9421SBen Gamari 
1697b4519513SChris Wilson 	if (memcmp(dev_priv->last_acthd, acthd, sizeof(acthd)) == 0 &&
1698cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1699cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1700d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
1701f65d9421SBen Gamari 			return;
1702cbb465e7SChris Wilson 	} else {
1703cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1704cbb465e7SChris Wilson 
1705b4519513SChris Wilson 		memcpy(dev_priv->last_acthd, acthd, sizeof(acthd));
1706cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1707cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1708cbb465e7SChris Wilson 	}
1709f65d9421SBen Gamari 
1710893eead0SChris Wilson repeat:
1711f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1712b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1713b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1714f65d9421SBen Gamari }
1715f65d9421SBen Gamari 
1716c0e09200SDave Airlie /* drm_dma.h hooks
1717c0e09200SDave Airlie */
1718f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
1719036a4a7dSZhenyu Wang {
1720036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1721036a4a7dSZhenyu Wang 
17224697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17234697995bSJesse Barnes 
1724036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1725bdfcdb63SDaniel Vetter 
1726036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1727036a4a7dSZhenyu Wang 
1728036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1729036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
17303143a2bfSChris Wilson 	POSTING_READ(DEIER);
1731036a4a7dSZhenyu Wang 
1732036a4a7dSZhenyu Wang 	/* and GT */
1733036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1734036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
17353143a2bfSChris Wilson 	POSTING_READ(GTIER);
1736c650156aSZhenyu Wang 
1737c650156aSZhenyu Wang 	/* south display irq */
1738c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1739c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
17403143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1741036a4a7dSZhenyu Wang }
1742036a4a7dSZhenyu Wang 
17437e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
17447e231dbeSJesse Barnes {
17457e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17467e231dbeSJesse Barnes 	int pipe;
17477e231dbeSJesse Barnes 
17487e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17497e231dbeSJesse Barnes 
17507e231dbeSJesse Barnes 	/* VLV magic */
17517e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
17527e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
17537e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
17547e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
17557e231dbeSJesse Barnes 
17567e231dbeSJesse Barnes 	/* and GT */
17577e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
17587e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
17597e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
17607e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
17617e231dbeSJesse Barnes 	POSTING_READ(GTIER);
17627e231dbeSJesse Barnes 
17637e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
17647e231dbeSJesse Barnes 
17657e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
17667e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
17677e231dbeSJesse Barnes 	for_each_pipe(pipe)
17687e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
17697e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
17707e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
17717e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
17727e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
17737e231dbeSJesse Barnes }
17747e231dbeSJesse Barnes 
17757fe0b973SKeith Packard /*
17767fe0b973SKeith Packard  * Enable digital hotplug on the PCH, and configure the DP short pulse
17777fe0b973SKeith Packard  * duration to 2ms (which is the minimum in the Display Port spec)
17787fe0b973SKeith Packard  *
17797fe0b973SKeith Packard  * This register is the same on all known PCH chips.
17807fe0b973SKeith Packard  */
17817fe0b973SKeith Packard 
17827fe0b973SKeith Packard static void ironlake_enable_pch_hotplug(struct drm_device *dev)
17837fe0b973SKeith Packard {
17847fe0b973SKeith Packard 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17857fe0b973SKeith Packard 	u32	hotplug;
17867fe0b973SKeith Packard 
17877fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
17887fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
17897fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
17907fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
17917fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
17927fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
17937fe0b973SKeith Packard }
17947fe0b973SKeith Packard 
1795f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
1796036a4a7dSZhenyu Wang {
1797036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1798036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1799013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1800013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
18011ec14ad3SChris Wilson 	u32 render_irqs;
18022d7b8366SYuanhan Liu 	u32 hotplug_mask;
1803036a4a7dSZhenyu Wang 
18041ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1805036a4a7dSZhenyu Wang 
1806036a4a7dSZhenyu Wang 	/* should always can generate irq */
1807036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
18081ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
18091ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
18103143a2bfSChris Wilson 	POSTING_READ(DEIER);
1811036a4a7dSZhenyu Wang 
18121ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1813036a4a7dSZhenyu Wang 
1814036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
18151ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1816881f47b6SXiang, Haihao 
18171ec14ad3SChris Wilson 	if (IS_GEN6(dev))
18181ec14ad3SChris Wilson 		render_irqs =
18191ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
1820e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
1821e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
18221ec14ad3SChris Wilson 	else
18231ec14ad3SChris Wilson 		render_irqs =
182488f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1825c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
18261ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
18271ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
18283143a2bfSChris Wilson 	POSTING_READ(GTIER);
1829036a4a7dSZhenyu Wang 
18302d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
18319035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
18329035a97aSChris Wilson 				SDE_PORTB_HOTPLUG_CPT |
18339035a97aSChris Wilson 				SDE_PORTC_HOTPLUG_CPT |
18349035a97aSChris Wilson 				SDE_PORTD_HOTPLUG_CPT);
18352d7b8366SYuanhan Liu 	} else {
18369035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG |
18379035a97aSChris Wilson 				SDE_PORTB_HOTPLUG |
18389035a97aSChris Wilson 				SDE_PORTC_HOTPLUG |
18399035a97aSChris Wilson 				SDE_PORTD_HOTPLUG |
18409035a97aSChris Wilson 				SDE_AUX_MASK);
18412d7b8366SYuanhan Liu 	}
18422d7b8366SYuanhan Liu 
18431ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1844c650156aSZhenyu Wang 
1845c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
18461ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
18471ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
18483143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1849c650156aSZhenyu Wang 
18507fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
18517fe0b973SKeith Packard 
1852f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1853f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1854f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1855f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1856f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1857f97108d1SJesse Barnes 	}
1858f97108d1SJesse Barnes 
1859036a4a7dSZhenyu Wang 	return 0;
1860036a4a7dSZhenyu Wang }
1861036a4a7dSZhenyu Wang 
1862f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
1863b1f14ad0SJesse Barnes {
1864b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1865b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
1866b615b57aSChris Wilson 	u32 display_mask =
1867b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
1868b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
1869b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
1870b615b57aSChris Wilson 		DE_PLANEA_FLIP_DONE_IVB;
1871b1f14ad0SJesse Barnes 	u32 render_irqs;
1872b1f14ad0SJesse Barnes 	u32 hotplug_mask;
1873b1f14ad0SJesse Barnes 
1874b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
1875b1f14ad0SJesse Barnes 
1876b1f14ad0SJesse Barnes 	/* should always can generate irq */
1877b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1878b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
1879b615b57aSChris Wilson 	I915_WRITE(DEIER,
1880b615b57aSChris Wilson 		   display_mask |
1881b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
1882b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
1883b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
1884b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1885b1f14ad0SJesse Barnes 
188615b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
1887b1f14ad0SJesse Barnes 
1888b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1889b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1890b1f14ad0SJesse Barnes 
1891e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
189215b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
1893b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
1894b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
1895b1f14ad0SJesse Barnes 
1896b1f14ad0SJesse Barnes 	hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
1897b1f14ad0SJesse Barnes 			SDE_PORTB_HOTPLUG_CPT |
1898b1f14ad0SJesse Barnes 			SDE_PORTC_HOTPLUG_CPT |
1899b1f14ad0SJesse Barnes 			SDE_PORTD_HOTPLUG_CPT);
1900b1f14ad0SJesse Barnes 	dev_priv->pch_irq_mask = ~hotplug_mask;
1901b1f14ad0SJesse Barnes 
1902b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1903b1f14ad0SJesse Barnes 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1904b1f14ad0SJesse Barnes 	I915_WRITE(SDEIER, hotplug_mask);
1905b1f14ad0SJesse Barnes 	POSTING_READ(SDEIER);
1906b1f14ad0SJesse Barnes 
19077fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
19087fe0b973SKeith Packard 
1909b1f14ad0SJesse Barnes 	return 0;
1910b1f14ad0SJesse Barnes }
1911b1f14ad0SJesse Barnes 
19127e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
19137e231dbeSJesse Barnes {
19147e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19157e231dbeSJesse Barnes 	u32 enable_mask;
19167e231dbeSJesse Barnes 	u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
191731acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
19187e231dbeSJesse Barnes 	u16 msid;
19197e231dbeSJesse Barnes 
19207e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
192131acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
192231acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
192331acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
19247e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
19257e231dbeSJesse Barnes 
192631acc7f5SJesse Barnes 	/*
192731acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
192831acc7f5SJesse Barnes 	 * toggle them based on usage.
192931acc7f5SJesse Barnes 	 */
193031acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
193131acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
193231acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
19337e231dbeSJesse Barnes 
19347e231dbeSJesse Barnes 	dev_priv->pipestat[0] = 0;
19357e231dbeSJesse Barnes 	dev_priv->pipestat[1] = 0;
19367e231dbeSJesse Barnes 
19377e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
19387e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
19397e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
19407e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
19417e231dbeSJesse Barnes 	msid |= (1<<14);
19427e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
19437e231dbeSJesse Barnes 
19447e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
19457e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
19467e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19477e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
19487e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
19497e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
19507e231dbeSJesse Barnes 
195131acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
195231acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
195331acc7f5SJesse Barnes 
19547e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19557e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19567e231dbeSJesse Barnes 
195731acc7f5SJesse Barnes 	dev_priv->gt_irq_mask = ~0;
195831acc7f5SJesse Barnes 
195931acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
196031acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
196131acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
196231acc7f5SJesse Barnes 	I915_WRITE(GTIER, GT_GEN6_BLT_FLUSHDW_NOTIFY_INTERRUPT |
19637e231dbeSJesse Barnes 		   GT_GEN6_BLT_CS_ERROR_INTERRUPT |
1964e2a1e2f0SBen Widawsky 		   GT_GEN6_BLT_USER_INTERRUPT |
19657e231dbeSJesse Barnes 		   GT_GEN6_BSD_USER_INTERRUPT |
19667e231dbeSJesse Barnes 		   GT_GEN6_BSD_CS_ERROR_INTERRUPT |
19677e231dbeSJesse Barnes 		   GT_GEN7_L3_PARITY_ERROR_INTERRUPT |
19687e231dbeSJesse Barnes 		   GT_PIPE_NOTIFY |
19697e231dbeSJesse Barnes 		   GT_RENDER_CS_ERROR_INTERRUPT |
19707e231dbeSJesse Barnes 		   GT_SYNC_STATUS |
197131acc7f5SJesse Barnes 		   GT_USER_INTERRUPT);
19727e231dbeSJesse Barnes 	POSTING_READ(GTIER);
19737e231dbeSJesse Barnes 
19747e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
19757e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
19767e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
19777e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
19787e231dbeSJesse Barnes #endif
19797e231dbeSJesse Barnes 
19807e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
19817e231dbeSJesse Barnes #if 0 /* FIXME: check register definitions; some have moved */
19827e231dbeSJesse Barnes 	/* Note HDMI and DP share bits */
19837e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
19847e231dbeSJesse Barnes 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
19857e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
19867e231dbeSJesse Barnes 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
19877e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
19887e231dbeSJesse Barnes 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
19897e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
19907e231dbeSJesse Barnes 		hotplug_en |= SDVOC_HOTPLUG_INT_EN;
19917e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
19927e231dbeSJesse Barnes 		hotplug_en |= SDVOB_HOTPLUG_INT_EN;
19937e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
19947e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_INT_EN;
19957e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
19967e231dbeSJesse Barnes 	}
19977e231dbeSJesse Barnes #endif
19987e231dbeSJesse Barnes 
19997e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
20007e231dbeSJesse Barnes 
20017e231dbeSJesse Barnes 	return 0;
20027e231dbeSJesse Barnes }
20037e231dbeSJesse Barnes 
20047e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
20057e231dbeSJesse Barnes {
20067e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20077e231dbeSJesse Barnes 	int pipe;
20087e231dbeSJesse Barnes 
20097e231dbeSJesse Barnes 	if (!dev_priv)
20107e231dbeSJesse Barnes 		return;
20117e231dbeSJesse Barnes 
20127e231dbeSJesse Barnes 	for_each_pipe(pipe)
20137e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
20147e231dbeSJesse Barnes 
20157e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
20167e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
20177e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
20187e231dbeSJesse Barnes 	for_each_pipe(pipe)
20197e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
20207e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20217e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
20227e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
20237e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
20247e231dbeSJesse Barnes }
20257e231dbeSJesse Barnes 
2026f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2027036a4a7dSZhenyu Wang {
2028036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20294697995bSJesse Barnes 
20304697995bSJesse Barnes 	if (!dev_priv)
20314697995bSJesse Barnes 		return;
20324697995bSJesse Barnes 
2033036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2034036a4a7dSZhenyu Wang 
2035036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2036036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2037036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2038036a4a7dSZhenyu Wang 
2039036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2040036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2041036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2042192aac1fSKeith Packard 
2043192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2044192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2045192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2046036a4a7dSZhenyu Wang }
2047036a4a7dSZhenyu Wang 
2048c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2049c2798b19SChris Wilson {
2050c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2051c2798b19SChris Wilson 	int pipe;
2052c2798b19SChris Wilson 
2053c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2054c2798b19SChris Wilson 
2055c2798b19SChris Wilson 	for_each_pipe(pipe)
2056c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2057c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2058c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2059c2798b19SChris Wilson 	POSTING_READ16(IER);
2060c2798b19SChris Wilson }
2061c2798b19SChris Wilson 
2062c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2063c2798b19SChris Wilson {
2064c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2065c2798b19SChris Wilson 
2066c2798b19SChris Wilson 	dev_priv->pipestat[0] = 0;
2067c2798b19SChris Wilson 	dev_priv->pipestat[1] = 0;
2068c2798b19SChris Wilson 
2069c2798b19SChris Wilson 	I915_WRITE16(EMR,
2070c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2071c2798b19SChris Wilson 
2072c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2073c2798b19SChris Wilson 	dev_priv->irq_mask =
2074c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2075c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2076c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2077c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2078c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2079c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2080c2798b19SChris Wilson 
2081c2798b19SChris Wilson 	I915_WRITE16(IER,
2082c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2083c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2084c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2085c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2086c2798b19SChris Wilson 	POSTING_READ16(IER);
2087c2798b19SChris Wilson 
2088c2798b19SChris Wilson 	return 0;
2089c2798b19SChris Wilson }
2090c2798b19SChris Wilson 
2091c2798b19SChris Wilson static irqreturn_t i8xx_irq_handler(DRM_IRQ_ARGS)
2092c2798b19SChris Wilson {
2093c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2094c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2095c2798b19SChris Wilson 	u16 iir, new_iir;
2096c2798b19SChris Wilson 	u32 pipe_stats[2];
2097c2798b19SChris Wilson 	unsigned long irqflags;
2098c2798b19SChris Wilson 	int irq_received;
2099c2798b19SChris Wilson 	int pipe;
2100c2798b19SChris Wilson 	u16 flip_mask =
2101c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2102c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2103c2798b19SChris Wilson 
2104c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2105c2798b19SChris Wilson 
2106c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2107c2798b19SChris Wilson 	if (iir == 0)
2108c2798b19SChris Wilson 		return IRQ_NONE;
2109c2798b19SChris Wilson 
2110c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2111c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2112c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2113c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2114c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2115c2798b19SChris Wilson 		 */
2116c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2117c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2118c2798b19SChris Wilson 			i915_handle_error(dev, false);
2119c2798b19SChris Wilson 
2120c2798b19SChris Wilson 		for_each_pipe(pipe) {
2121c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2122c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2123c2798b19SChris Wilson 
2124c2798b19SChris Wilson 			/*
2125c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2126c2798b19SChris Wilson 			 */
2127c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2128c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2129c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2130c2798b19SChris Wilson 							 pipe_name(pipe));
2131c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2132c2798b19SChris Wilson 				irq_received = 1;
2133c2798b19SChris Wilson 			}
2134c2798b19SChris Wilson 		}
2135c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2136c2798b19SChris Wilson 
2137c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2138c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2139c2798b19SChris Wilson 
2140d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2141c2798b19SChris Wilson 
2142c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2143c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2144c2798b19SChris Wilson 
2145c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
2146c2798b19SChris Wilson 		    drm_handle_vblank(dev, 0)) {
2147c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
2148c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 0);
2149c2798b19SChris Wilson 				intel_finish_page_flip(dev, 0);
2150c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT;
2151c2798b19SChris Wilson 			}
2152c2798b19SChris Wilson 		}
2153c2798b19SChris Wilson 
2154c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
2155c2798b19SChris Wilson 		    drm_handle_vblank(dev, 1)) {
2156c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
2157c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 1);
2158c2798b19SChris Wilson 				intel_finish_page_flip(dev, 1);
2159c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2160c2798b19SChris Wilson 			}
2161c2798b19SChris Wilson 		}
2162c2798b19SChris Wilson 
2163c2798b19SChris Wilson 		iir = new_iir;
2164c2798b19SChris Wilson 	}
2165c2798b19SChris Wilson 
2166c2798b19SChris Wilson 	return IRQ_HANDLED;
2167c2798b19SChris Wilson }
2168c2798b19SChris Wilson 
2169c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2170c2798b19SChris Wilson {
2171c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2172c2798b19SChris Wilson 	int pipe;
2173c2798b19SChris Wilson 
2174c2798b19SChris Wilson 	for_each_pipe(pipe) {
2175c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2176c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2177c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2178c2798b19SChris Wilson 	}
2179c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2180c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2181c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2182c2798b19SChris Wilson }
2183c2798b19SChris Wilson 
2184a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2185a266c7d5SChris Wilson {
2186a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2187a266c7d5SChris Wilson 	int pipe;
2188a266c7d5SChris Wilson 
2189a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2190a266c7d5SChris Wilson 
2191a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2192a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2193a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2194a266c7d5SChris Wilson 	}
2195a266c7d5SChris Wilson 
219600d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2197a266c7d5SChris Wilson 	for_each_pipe(pipe)
2198a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2199a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2200a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2201a266c7d5SChris Wilson 	POSTING_READ(IER);
2202a266c7d5SChris Wilson }
2203a266c7d5SChris Wilson 
2204a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2205a266c7d5SChris Wilson {
2206a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
220738bde180SChris Wilson 	u32 enable_mask;
2208a266c7d5SChris Wilson 
2209a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2210a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2211a266c7d5SChris Wilson 
221238bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
221338bde180SChris Wilson 
221438bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
221538bde180SChris Wilson 	dev_priv->irq_mask =
221638bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
221738bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
221838bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
221938bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
222038bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
222138bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
222238bde180SChris Wilson 
222338bde180SChris Wilson 	enable_mask =
222438bde180SChris Wilson 		I915_ASLE_INTERRUPT |
222538bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
222638bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
222738bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
222838bde180SChris Wilson 		I915_USER_INTERRUPT;
222938bde180SChris Wilson 
2230a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2231a266c7d5SChris Wilson 		/* Enable in IER... */
2232a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2233a266c7d5SChris Wilson 		/* and unmask in IMR */
2234a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2235a266c7d5SChris Wilson 	}
2236a266c7d5SChris Wilson 
2237a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2238a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2239a266c7d5SChris Wilson 	POSTING_READ(IER);
2240a266c7d5SChris Wilson 
2241a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2242a266c7d5SChris Wilson 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2243a266c7d5SChris Wilson 
2244a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2245a266c7d5SChris Wilson 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2246a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2247a266c7d5SChris Wilson 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2248a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2249a266c7d5SChris Wilson 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
2250084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
2251a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2252084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
2253a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2254a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2255a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_INT_EN;
2256a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2257a266c7d5SChris Wilson 		}
2258a266c7d5SChris Wilson 
2259a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2260a266c7d5SChris Wilson 
2261a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2262a266c7d5SChris Wilson 	}
2263a266c7d5SChris Wilson 
2264a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2265a266c7d5SChris Wilson 
2266a266c7d5SChris Wilson 	return 0;
2267a266c7d5SChris Wilson }
2268a266c7d5SChris Wilson 
2269a266c7d5SChris Wilson static irqreturn_t i915_irq_handler(DRM_IRQ_ARGS)
2270a266c7d5SChris Wilson {
2271a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2272a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22738291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2274a266c7d5SChris Wilson 	unsigned long irqflags;
227538bde180SChris Wilson 	u32 flip_mask =
227638bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
227738bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
227838bde180SChris Wilson 	u32 flip[2] = {
227938bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT,
228038bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT
228138bde180SChris Wilson 	};
228238bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2283a266c7d5SChris Wilson 
2284a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2285a266c7d5SChris Wilson 
2286a266c7d5SChris Wilson 	iir = I915_READ(IIR);
228738bde180SChris Wilson 	do {
228838bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
22898291ee90SChris Wilson 		bool blc_event = false;
2290a266c7d5SChris Wilson 
2291a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2292a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2293a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2294a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2295a266c7d5SChris Wilson 		 */
2296a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2297a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2298a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2299a266c7d5SChris Wilson 
2300a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2301a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2302a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2303a266c7d5SChris Wilson 
230438bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2305a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2306a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2307a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2308a266c7d5SChris Wilson 							 pipe_name(pipe));
2309a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
231038bde180SChris Wilson 				irq_received = true;
2311a266c7d5SChris Wilson 			}
2312a266c7d5SChris Wilson 		}
2313a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2314a266c7d5SChris Wilson 
2315a266c7d5SChris Wilson 		if (!irq_received)
2316a266c7d5SChris Wilson 			break;
2317a266c7d5SChris Wilson 
2318a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2319a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2320a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2321a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2322a266c7d5SChris Wilson 
2323a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2324a266c7d5SChris Wilson 				  hotplug_status);
2325a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2326a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2327a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2328a266c7d5SChris Wilson 
2329a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
233038bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2331a266c7d5SChris Wilson 		}
2332a266c7d5SChris Wilson 
233338bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2334a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2335a266c7d5SChris Wilson 
2336a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2337a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2338a266c7d5SChris Wilson 
2339a266c7d5SChris Wilson 		for_each_pipe(pipe) {
234038bde180SChris Wilson 			int plane = pipe;
234138bde180SChris Wilson 			if (IS_MOBILE(dev))
234238bde180SChris Wilson 				plane = !plane;
23438291ee90SChris Wilson 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
2344a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
234538bde180SChris Wilson 				if (iir & flip[plane]) {
234638bde180SChris Wilson 					intel_prepare_page_flip(dev, plane);
2347a266c7d5SChris Wilson 					intel_finish_page_flip(dev, pipe);
234838bde180SChris Wilson 					flip_mask &= ~flip[plane];
234938bde180SChris Wilson 				}
2350a266c7d5SChris Wilson 			}
2351a266c7d5SChris Wilson 
2352a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2353a266c7d5SChris Wilson 				blc_event = true;
2354a266c7d5SChris Wilson 		}
2355a266c7d5SChris Wilson 
2356a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2357a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2358a266c7d5SChris Wilson 
2359a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2360a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2361a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2362a266c7d5SChris Wilson 		 * we would never get another interrupt.
2363a266c7d5SChris Wilson 		 *
2364a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2365a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2366a266c7d5SChris Wilson 		 * another one.
2367a266c7d5SChris Wilson 		 *
2368a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2369a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2370a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2371a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2372a266c7d5SChris Wilson 		 * stray interrupts.
2373a266c7d5SChris Wilson 		 */
237438bde180SChris Wilson 		ret = IRQ_HANDLED;
2375a266c7d5SChris Wilson 		iir = new_iir;
237638bde180SChris Wilson 	} while (iir & ~flip_mask);
2377a266c7d5SChris Wilson 
2378d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
23798291ee90SChris Wilson 
2380a266c7d5SChris Wilson 	return ret;
2381a266c7d5SChris Wilson }
2382a266c7d5SChris Wilson 
2383a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2384a266c7d5SChris Wilson {
2385a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2386a266c7d5SChris Wilson 	int pipe;
2387a266c7d5SChris Wilson 
2388a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2389a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2390a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2391a266c7d5SChris Wilson 	}
2392a266c7d5SChris Wilson 
239300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
239455b39755SChris Wilson 	for_each_pipe(pipe) {
239555b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2396a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
239755b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
239855b39755SChris Wilson 	}
2399a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2400a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2401a266c7d5SChris Wilson 
2402a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2403a266c7d5SChris Wilson }
2404a266c7d5SChris Wilson 
2405a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2406a266c7d5SChris Wilson {
2407a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2408a266c7d5SChris Wilson 	int pipe;
2409a266c7d5SChris Wilson 
2410a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2411a266c7d5SChris Wilson 
2412a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2413a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2414a266c7d5SChris Wilson 
2415a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2416a266c7d5SChris Wilson 	for_each_pipe(pipe)
2417a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2418a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2419a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2420a266c7d5SChris Wilson 	POSTING_READ(IER);
2421a266c7d5SChris Wilson }
2422a266c7d5SChris Wilson 
2423a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2424a266c7d5SChris Wilson {
2425a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2426adca4730SChris Wilson 	u32 hotplug_en;
2427bbba0a97SChris Wilson 	u32 enable_mask;
2428a266c7d5SChris Wilson 	u32 error_mask;
2429a266c7d5SChris Wilson 
2430a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2431bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2432adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2433bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2434bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2435bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2436bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2437bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2438bbba0a97SChris Wilson 
2439bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
2440bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2441bbba0a97SChris Wilson 
2442bbba0a97SChris Wilson 	if (IS_G4X(dev))
2443bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2444a266c7d5SChris Wilson 
2445a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2446a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2447a266c7d5SChris Wilson 
2448a266c7d5SChris Wilson 	/*
2449a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2450a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2451a266c7d5SChris Wilson 	 */
2452a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2453a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2454a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2455a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2456a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2457a266c7d5SChris Wilson 	} else {
2458a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2459a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2460a266c7d5SChris Wilson 	}
2461a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2462a266c7d5SChris Wilson 
2463a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2464a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2465a266c7d5SChris Wilson 	POSTING_READ(IER);
2466a266c7d5SChris Wilson 
2467adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
2468adca4730SChris Wilson 	hotplug_en = 0;
2469a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2470a266c7d5SChris Wilson 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2471a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2472a266c7d5SChris Wilson 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2473a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2474a266c7d5SChris Wilson 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
2475084b612eSChris Wilson 	if (IS_G4X(dev)) {
2476084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_G4X)
2477a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2478084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_G4X)
2479a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2480084b612eSChris Wilson 	} else {
2481084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I965)
2482084b612eSChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2483084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I965)
2484084b612eSChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2485084b612eSChris Wilson 	}
2486a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2487a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_INT_EN;
2488a266c7d5SChris Wilson 
2489a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2490a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2491a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2492a266c7d5SChris Wilson 		   */
2493a266c7d5SChris Wilson 		if (IS_G4X(dev))
2494a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2495a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2496a266c7d5SChris Wilson 	}
2497a266c7d5SChris Wilson 
2498a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
2499a266c7d5SChris Wilson 
2500a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2501a266c7d5SChris Wilson 
2502a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2503a266c7d5SChris Wilson 
2504a266c7d5SChris Wilson 	return 0;
2505a266c7d5SChris Wilson }
2506a266c7d5SChris Wilson 
2507a266c7d5SChris Wilson static irqreturn_t i965_irq_handler(DRM_IRQ_ARGS)
2508a266c7d5SChris Wilson {
2509a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2510a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2511a266c7d5SChris Wilson 	u32 iir, new_iir;
2512a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2513a266c7d5SChris Wilson 	unsigned long irqflags;
2514a266c7d5SChris Wilson 	int irq_received;
2515a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
2516a266c7d5SChris Wilson 
2517a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2518a266c7d5SChris Wilson 
2519a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2520a266c7d5SChris Wilson 
2521a266c7d5SChris Wilson 	for (;;) {
25222c8ba29fSChris Wilson 		bool blc_event = false;
25232c8ba29fSChris Wilson 
2524a266c7d5SChris Wilson 		irq_received = iir != 0;
2525a266c7d5SChris Wilson 
2526a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2527a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2528a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2529a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2530a266c7d5SChris Wilson 		 */
2531a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2532a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2533a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2534a266c7d5SChris Wilson 
2535a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2536a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2537a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2538a266c7d5SChris Wilson 
2539a266c7d5SChris Wilson 			/*
2540a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2541a266c7d5SChris Wilson 			 */
2542a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2543a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2544a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2545a266c7d5SChris Wilson 							 pipe_name(pipe));
2546a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2547a266c7d5SChris Wilson 				irq_received = 1;
2548a266c7d5SChris Wilson 			}
2549a266c7d5SChris Wilson 		}
2550a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2551a266c7d5SChris Wilson 
2552a266c7d5SChris Wilson 		if (!irq_received)
2553a266c7d5SChris Wilson 			break;
2554a266c7d5SChris Wilson 
2555a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2556a266c7d5SChris Wilson 
2557a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2558adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2559a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2560a266c7d5SChris Wilson 
2561a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2562a266c7d5SChris Wilson 				  hotplug_status);
2563a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2564a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2565a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2566a266c7d5SChris Wilson 
2567a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2568a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2569a266c7d5SChris Wilson 		}
2570a266c7d5SChris Wilson 
2571a266c7d5SChris Wilson 		I915_WRITE(IIR, iir);
2572a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2573a266c7d5SChris Wilson 
2574a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2575a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2576a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2577a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2578a266c7d5SChris Wilson 
25794f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
2580a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 0);
2581a266c7d5SChris Wilson 
25824f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
2583a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 1);
2584a266c7d5SChris Wilson 
2585a266c7d5SChris Wilson 		for_each_pipe(pipe) {
25862c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
2587a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
2588a266c7d5SChris Wilson 				i915_pageflip_stall_check(dev, pipe);
2589a266c7d5SChris Wilson 				intel_finish_page_flip(dev, pipe);
2590a266c7d5SChris Wilson 			}
2591a266c7d5SChris Wilson 
2592a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2593a266c7d5SChris Wilson 				blc_event = true;
2594a266c7d5SChris Wilson 		}
2595a266c7d5SChris Wilson 
2596a266c7d5SChris Wilson 
2597a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2598a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2599a266c7d5SChris Wilson 
2600a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2601a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2602a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2603a266c7d5SChris Wilson 		 * we would never get another interrupt.
2604a266c7d5SChris Wilson 		 *
2605a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2606a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2607a266c7d5SChris Wilson 		 * another one.
2608a266c7d5SChris Wilson 		 *
2609a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2610a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2611a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2612a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2613a266c7d5SChris Wilson 		 * stray interrupts.
2614a266c7d5SChris Wilson 		 */
2615a266c7d5SChris Wilson 		iir = new_iir;
2616a266c7d5SChris Wilson 	}
2617a266c7d5SChris Wilson 
2618d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
26192c8ba29fSChris Wilson 
2620a266c7d5SChris Wilson 	return ret;
2621a266c7d5SChris Wilson }
2622a266c7d5SChris Wilson 
2623a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
2624a266c7d5SChris Wilson {
2625a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2626a266c7d5SChris Wilson 	int pipe;
2627a266c7d5SChris Wilson 
2628a266c7d5SChris Wilson 	if (!dev_priv)
2629a266c7d5SChris Wilson 		return;
2630a266c7d5SChris Wilson 
2631a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2632a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2633a266c7d5SChris Wilson 
2634a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
2635a266c7d5SChris Wilson 	for_each_pipe(pipe)
2636a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2637a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2638a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2639a266c7d5SChris Wilson 
2640a266c7d5SChris Wilson 	for_each_pipe(pipe)
2641a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
2642a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2643a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2644a266c7d5SChris Wilson }
2645a266c7d5SChris Wilson 
2646f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2647f71d4af4SJesse Barnes {
26488b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
26498b2e326dSChris Wilson 
26508b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
26518b2e326dSChris Wilson 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
2652c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
265398fd81cdSDaniel Vetter 	INIT_WORK(&dev_priv->parity_error_work, ivybridge_parity_work);
26548b2e326dSChris Wilson 
2655f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2656f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
26577d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
2658f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2659f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2660f71d4af4SJesse Barnes 	}
2661f71d4af4SJesse Barnes 
2662c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2663f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2664c3613de9SKeith Packard 	else
2665c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2666f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2667f71d4af4SJesse Barnes 
26687e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
26697e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
26707e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
26717e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
26727e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
26737e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
26747e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
26757e231dbeSJesse Barnes 	} else if (IS_IVYBRIDGE(dev)) {
2676f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2677f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2678f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2679f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2680f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2681f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2682f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
26837d4e146fSEugeni Dodonov 	} else if (IS_HASWELL(dev)) {
26847d4e146fSEugeni Dodonov 		/* Share interrupts handling with IVB */
26857d4e146fSEugeni Dodonov 		dev->driver->irq_handler = ivybridge_irq_handler;
26867d4e146fSEugeni Dodonov 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
26877d4e146fSEugeni Dodonov 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
26887d4e146fSEugeni Dodonov 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
26897d4e146fSEugeni Dodonov 		dev->driver->enable_vblank = ivybridge_enable_vblank;
26907d4e146fSEugeni Dodonov 		dev->driver->disable_vblank = ivybridge_disable_vblank;
2691f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2692f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2693f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2694f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2695f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2696f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2697f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
2698f71d4af4SJesse Barnes 	} else {
2699c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
2700c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
2701c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
2702c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
2703c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
2704a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
27054f7d1e79SChris Wilson 			/* IIR "flip pending" means done if this bit is set */
27064f7d1e79SChris Wilson 			I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
27074f7d1e79SChris Wilson 
2708a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
2709a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
2710a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
2711a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
2712c2798b19SChris Wilson 		} else {
2713a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
2714a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
2715a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
2716a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
2717c2798b19SChris Wilson 		}
2718f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
2719f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
2720f71d4af4SJesse Barnes 	}
2721f71d4af4SJesse Barnes }
2722