xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision b9a3906b609c4b546f00483089516c65fb2ff014)
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 
299f97108d1SJesse Barnes static void i915_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 
3057648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
306b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
307b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
308f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
309f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
310f97108d1SJesse Barnes 
311f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
312b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
313f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
314f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
315f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
316f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
317b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
318f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
319f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
320f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
321f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
322f97108d1SJesse Barnes 	}
323f97108d1SJesse Barnes 
3247648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
325f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
326f97108d1SJesse Barnes 
327f97108d1SJesse Barnes 	return;
328f97108d1SJesse Barnes }
329f97108d1SJesse Barnes 
330549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
331549f7365SChris Wilson 			struct intel_ring_buffer *ring)
332549f7365SChris Wilson {
333549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
3349862e600SChris Wilson 
335475553deSChris Wilson 	if (ring->obj == NULL)
336475553deSChris Wilson 		return;
337475553deSChris Wilson 
3386d171cb4SChris Wilson 	trace_i915_gem_request_complete(ring, ring->get_seqno(ring));
3399862e600SChris Wilson 
340549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3413e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
342549f7365SChris Wilson 		dev_priv->hangcheck_count = 0;
343549f7365SChris Wilson 		mod_timer(&dev_priv->hangcheck_timer,
3443e0dc6b0SBen Widawsky 			  jiffies +
3453e0dc6b0SBen Widawsky 			  msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
3463e0dc6b0SBen Widawsky 	}
347549f7365SChris Wilson }
348549f7365SChris Wilson 
3494912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
3503b8d8d91SJesse Barnes {
3514912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3524912d041SBen Widawsky 						    rps_work);
3534912d041SBen Widawsky 	u32 pm_iir, pm_imr;
3547b9e0ae6SChris Wilson 	u8 new_delay;
3553b8d8d91SJesse Barnes 
3564912d041SBen Widawsky 	spin_lock_irq(&dev_priv->rps_lock);
3574912d041SBen Widawsky 	pm_iir = dev_priv->pm_iir;
3584912d041SBen Widawsky 	dev_priv->pm_iir = 0;
3594912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
360a9e2641dSDaniel Vetter 	I915_WRITE(GEN6_PMIMR, 0);
3614912d041SBen Widawsky 	spin_unlock_irq(&dev_priv->rps_lock);
3624912d041SBen Widawsky 
3637b9e0ae6SChris Wilson 	if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
3643b8d8d91SJesse Barnes 		return;
3653b8d8d91SJesse Barnes 
3664912d041SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
3677b9e0ae6SChris Wilson 
3687b9e0ae6SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
3693b8d8d91SJesse Barnes 		new_delay = dev_priv->cur_delay + 1;
3707b9e0ae6SChris Wilson 	else
3713b8d8d91SJesse Barnes 		new_delay = dev_priv->cur_delay - 1;
3723b8d8d91SJesse Barnes 
3734912d041SBen Widawsky 	gen6_set_rps(dev_priv->dev, new_delay);
3743b8d8d91SJesse Barnes 
3754912d041SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
3763b8d8d91SJesse Barnes }
3773b8d8d91SJesse Barnes 
378e3689190SBen Widawsky 
379e3689190SBen Widawsky /**
380e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
381e3689190SBen Widawsky  * occurred.
382e3689190SBen Widawsky  * @work: workqueue struct
383e3689190SBen Widawsky  *
384e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
385e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
386e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
387e3689190SBen Widawsky  */
388e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
389e3689190SBen Widawsky {
390e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
391e3689190SBen Widawsky 						    parity_error_work);
392e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
393e3689190SBen Widawsky 	char *parity_event[5];
394e3689190SBen Widawsky 	uint32_t misccpctl;
395e3689190SBen Widawsky 	unsigned long flags;
396e3689190SBen Widawsky 
397e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
398e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
399e3689190SBen Widawsky 	 * any time we access those registers.
400e3689190SBen Widawsky 	 */
401e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
402e3689190SBen Widawsky 
403e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
404e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
405e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
406e3689190SBen Widawsky 
407e3689190SBen Widawsky 	error_status = I915_READ(GEN7_L3CDERRST1);
408e3689190SBen Widawsky 	row = GEN7_PARITY_ERROR_ROW(error_status);
409e3689190SBen Widawsky 	bank = GEN7_PARITY_ERROR_BANK(error_status);
410e3689190SBen Widawsky 	subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
411e3689190SBen Widawsky 
412e3689190SBen Widawsky 	I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
413e3689190SBen Widawsky 				    GEN7_L3CDERRST1_ENABLE);
414e3689190SBen Widawsky 	POSTING_READ(GEN7_L3CDERRST1);
415e3689190SBen Widawsky 
416e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
417e3689190SBen Widawsky 
418e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
419e3689190SBen Widawsky 	dev_priv->gt_irq_mask &= ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
420e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
421e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
422e3689190SBen Widawsky 
423e3689190SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
424e3689190SBen Widawsky 
425e3689190SBen Widawsky 	parity_event[0] = "L3_PARITY_ERROR=1";
426e3689190SBen Widawsky 	parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
427e3689190SBen Widawsky 	parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
428e3689190SBen Widawsky 	parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
429e3689190SBen Widawsky 	parity_event[4] = NULL;
430e3689190SBen Widawsky 
431e3689190SBen Widawsky 	kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
432e3689190SBen Widawsky 			   KOBJ_CHANGE, parity_event);
433e3689190SBen Widawsky 
434e3689190SBen Widawsky 	DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
435e3689190SBen Widawsky 		  row, bank, subbank);
436e3689190SBen Widawsky 
437e3689190SBen Widawsky 	kfree(parity_event[3]);
438e3689190SBen Widawsky 	kfree(parity_event[2]);
439e3689190SBen Widawsky 	kfree(parity_event[1]);
440e3689190SBen Widawsky }
441e3689190SBen Widawsky 
442d2ba8470SDaniel Vetter static void ivybridge_handle_parity_error(struct drm_device *dev)
443e3689190SBen Widawsky {
444e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
445e3689190SBen Widawsky 	unsigned long flags;
446e3689190SBen Widawsky 
447e3689190SBen Widawsky 	if (!IS_IVYBRIDGE(dev))
448e3689190SBen Widawsky 		return;
449e3689190SBen Widawsky 
450e3689190SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
451e3689190SBen Widawsky 	dev_priv->gt_irq_mask |= GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
452e3689190SBen Widawsky 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
453e3689190SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
454e3689190SBen Widawsky 
455e3689190SBen Widawsky 	queue_work(dev_priv->wq, &dev_priv->parity_error_work);
456e3689190SBen Widawsky }
457e3689190SBen Widawsky 
458e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
459e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
460e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
461e7b4c6b1SDaniel Vetter {
462e7b4c6b1SDaniel Vetter 
463e7b4c6b1SDaniel Vetter 	if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
464e7b4c6b1SDaniel Vetter 		      GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
465e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
466e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BSD_USER_INTERRUPT)
467e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
468e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
469e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
470e7b4c6b1SDaniel Vetter 
471e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
472e7b4c6b1SDaniel Vetter 		      GT_GEN6_BSD_CS_ERROR_INTERRUPT |
473e7b4c6b1SDaniel Vetter 		      GT_RENDER_CS_ERROR_INTERRUPT)) {
474e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
475e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
476e7b4c6b1SDaniel Vetter 	}
477e3689190SBen Widawsky 
478e3689190SBen Widawsky 	if (gt_iir & GT_GEN7_L3_PARITY_ERROR_INTERRUPT)
479e3689190SBen Widawsky 		ivybridge_handle_parity_error(dev);
480e7b4c6b1SDaniel Vetter }
481e7b4c6b1SDaniel Vetter 
482fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
483fc6826d1SChris Wilson 				u32 pm_iir)
484fc6826d1SChris Wilson {
485fc6826d1SChris Wilson 	unsigned long flags;
486fc6826d1SChris Wilson 
487fc6826d1SChris Wilson 	/*
488fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
489fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
490fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
491fc6826d1SChris Wilson 	 * dev_priv->pm_iir. Although missing an interrupt of the same
492fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
493fc6826d1SChris Wilson 	 *
494fc6826d1SChris Wilson 	 * The mask bit in IMR is cleared by rps_work.
495fc6826d1SChris Wilson 	 */
496fc6826d1SChris Wilson 
497fc6826d1SChris Wilson 	spin_lock_irqsave(&dev_priv->rps_lock, flags);
498fc6826d1SChris Wilson 	WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
499fc6826d1SChris Wilson 	dev_priv->pm_iir |= pm_iir;
500fc6826d1SChris Wilson 	I915_WRITE(GEN6_PMIMR, dev_priv->pm_iir);
501fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
502fc6826d1SChris Wilson 	spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
503fc6826d1SChris Wilson 
504fc6826d1SChris Wilson 	queue_work(dev_priv->wq, &dev_priv->rps_work);
505fc6826d1SChris Wilson }
506fc6826d1SChris Wilson 
5077e231dbeSJesse Barnes static irqreturn_t valleyview_irq_handler(DRM_IRQ_ARGS)
5087e231dbeSJesse Barnes {
5097e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
5107e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5117e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
5127e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
5137e231dbeSJesse Barnes 	unsigned long irqflags;
5147e231dbeSJesse Barnes 	int pipe;
5157e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
5167e231dbeSJesse Barnes 	u32 vblank_status;
5177e231dbeSJesse Barnes 	int vblank = 0;
5187e231dbeSJesse Barnes 	bool blc_event;
5197e231dbeSJesse Barnes 
5207e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5217e231dbeSJesse Barnes 
5227e231dbeSJesse Barnes 	vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS |
5237e231dbeSJesse Barnes 		PIPE_VBLANK_INTERRUPT_STATUS;
5247e231dbeSJesse Barnes 
5257e231dbeSJesse Barnes 	while (true) {
5267e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
5277e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
5287e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
5297e231dbeSJesse Barnes 
5307e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
5317e231dbeSJesse Barnes 			goto out;
5327e231dbeSJesse Barnes 
5337e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
5347e231dbeSJesse Barnes 
535e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
5367e231dbeSJesse Barnes 
5377e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5387e231dbeSJesse Barnes 		for_each_pipe(pipe) {
5397e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
5407e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
5417e231dbeSJesse Barnes 
5427e231dbeSJesse Barnes 			/*
5437e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
5447e231dbeSJesse Barnes 			 */
5457e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
5467e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
5477e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
5487e231dbeSJesse Barnes 							 pipe_name(pipe));
5497e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
5507e231dbeSJesse Barnes 			}
5517e231dbeSJesse Barnes 		}
5527e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5537e231dbeSJesse Barnes 
5547e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
5557e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
5567e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
5577e231dbeSJesse Barnes 
5587e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
5597e231dbeSJesse Barnes 					 hotplug_status);
5607e231dbeSJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
5617e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
5627e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
5637e231dbeSJesse Barnes 
5647e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
5657e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
5667e231dbeSJesse Barnes 		}
5677e231dbeSJesse Barnes 
5687e231dbeSJesse Barnes 
5697e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT) {
5707e231dbeSJesse Barnes 			drm_handle_vblank(dev, 0);
5717e231dbeSJesse Barnes 			vblank++;
5727e231dbeSJesse Barnes 			intel_finish_page_flip(dev, 0);
5737e231dbeSJesse Barnes 		}
5747e231dbeSJesse Barnes 
5757e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT) {
5767e231dbeSJesse Barnes 			drm_handle_vblank(dev, 1);
5777e231dbeSJesse Barnes 			vblank++;
5787e231dbeSJesse Barnes 			intel_finish_page_flip(dev, 0);
5797e231dbeSJesse Barnes 		}
5807e231dbeSJesse Barnes 
5817e231dbeSJesse Barnes 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
5827e231dbeSJesse Barnes 			blc_event = true;
5837e231dbeSJesse Barnes 
584fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
585fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
5867e231dbeSJesse Barnes 
5877e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
5887e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
5897e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
5907e231dbeSJesse Barnes 	}
5917e231dbeSJesse Barnes 
5927e231dbeSJesse Barnes out:
5937e231dbeSJesse Barnes 	return ret;
5947e231dbeSJesse Barnes }
5957e231dbeSJesse Barnes 
5969adab8b5SChris Wilson static void pch_irq_handler(struct drm_device *dev, u32 pch_iir)
597776ad806SJesse Barnes {
598776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5999db4a9c7SJesse Barnes 	int pipe;
600776ad806SJesse Barnes 
601776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
602776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
603776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
604776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
605776ad806SJesse Barnes 
606776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
607776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
608776ad806SJesse Barnes 
609776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
610776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
611776ad806SJesse Barnes 
612776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
613776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
614776ad806SJesse Barnes 
615776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
616776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
617776ad806SJesse Barnes 
6189db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
6199db4a9c7SJesse Barnes 		for_each_pipe(pipe)
6209db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
6219db4a9c7SJesse Barnes 					 pipe_name(pipe),
6229db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
623776ad806SJesse Barnes 
624776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
625776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
626776ad806SJesse Barnes 
627776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
628776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
629776ad806SJesse Barnes 
630776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
631776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
632776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
633776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
634776ad806SJesse Barnes }
635776ad806SJesse Barnes 
636f71d4af4SJesse Barnes static irqreturn_t ivybridge_irq_handler(DRM_IRQ_ARGS)
637b1f14ad0SJesse Barnes {
638b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
639b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6400e43406bSChris Wilson 	u32 de_iir, gt_iir, de_ier, pm_iir;
6410e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
6420e43406bSChris Wilson 	int i;
643b1f14ad0SJesse Barnes 
644b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
645b1f14ad0SJesse Barnes 
646b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
647b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
648b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
6490e43406bSChris Wilson 
6500e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
6510e43406bSChris Wilson 	if (gt_iir) {
6520e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
6530e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
6540e43406bSChris Wilson 		ret = IRQ_HANDLED;
6550e43406bSChris Wilson 	}
656b1f14ad0SJesse Barnes 
657b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
6580e43406bSChris Wilson 	if (de_iir) {
659b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
660b1f14ad0SJesse Barnes 			intel_opregion_gse_intr(dev);
661b1f14ad0SJesse Barnes 
6620e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
6630e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
6640e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
6650e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
666b1f14ad0SJesse Barnes 			}
6670e43406bSChris Wilson 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
6680e43406bSChris Wilson 				drm_handle_vblank(dev, i);
669b1f14ad0SJesse Barnes 		}
670b1f14ad0SJesse Barnes 
671b1f14ad0SJesse Barnes 		/* check event from PCH */
672b1f14ad0SJesse Barnes 		if (de_iir & DE_PCH_EVENT_IVB) {
6730e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
6740e43406bSChris Wilson 
675b1f14ad0SJesse Barnes 			if (pch_iir & SDE_HOTPLUG_MASK_CPT)
676b1f14ad0SJesse Barnes 				queue_work(dev_priv->wq, &dev_priv->hotplug_work);
6779adab8b5SChris Wilson 			pch_irq_handler(dev, pch_iir);
6780e43406bSChris Wilson 
6790e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
6800e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
681b1f14ad0SJesse Barnes 		}
682b1f14ad0SJesse Barnes 
6830e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
6840e43406bSChris Wilson 		ret = IRQ_HANDLED;
6850e43406bSChris Wilson 	}
6860e43406bSChris Wilson 
6870e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
6880e43406bSChris Wilson 	if (pm_iir) {
689fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
690fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
691b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
6920e43406bSChris Wilson 		ret = IRQ_HANDLED;
6930e43406bSChris Wilson 	}
694b1f14ad0SJesse Barnes 
695b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
696b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
697b1f14ad0SJesse Barnes 
698b1f14ad0SJesse Barnes 	return ret;
699b1f14ad0SJesse Barnes }
700b1f14ad0SJesse Barnes 
701e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
702e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
703e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
704e7b4c6b1SDaniel Vetter {
705e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
706e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
707e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
708e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
709e7b4c6b1SDaniel Vetter }
710e7b4c6b1SDaniel Vetter 
711f71d4af4SJesse Barnes static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
712036a4a7dSZhenyu Wang {
7134697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
714036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
715036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
7163b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
7172d7b8366SYuanhan Liu 	u32 hotplug_mask;
718881f47b6SXiang, Haihao 
7194697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
7204697995bSJesse Barnes 
7212d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
7222d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
7232d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
7243143a2bfSChris Wilson 	POSTING_READ(DEIER);
7252d109a84SZou, Nanhai 
726036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
727036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
728c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
7293b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
730036a4a7dSZhenyu Wang 
7313b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
7323b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
733c7c85101SZou Nan hai 		goto done;
734036a4a7dSZhenyu Wang 
7352d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
7362d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
7372d7b8366SYuanhan Liu 	else
7382d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
7392d7b8366SYuanhan Liu 
740036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
741036a4a7dSZhenyu Wang 
742e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
743e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
744e7b4c6b1SDaniel Vetter 	else
745e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
746036a4a7dSZhenyu Wang 
74701c66889SZhao Yakui 	if (de_iir & DE_GSE)
7483b617967SChris Wilson 		intel_opregion_gse_intr(dev);
74901c66889SZhao Yakui 
750f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
751013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
7522bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
753013d5aa2SJesse Barnes 	}
754013d5aa2SJesse Barnes 
755f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
756f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
7572bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
758013d5aa2SJesse Barnes 	}
759c062df61SLi Peng 
760f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
761f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
762f072d2e7SZhenyu Wang 
763f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
764f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
765f072d2e7SZhenyu Wang 
766c650156aSZhenyu Wang 	/* check event from PCH */
767776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
768776ad806SJesse Barnes 		if (pch_iir & hotplug_mask)
769c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
7709adab8b5SChris Wilson 		pch_irq_handler(dev, pch_iir);
771776ad806SJesse Barnes 	}
772c650156aSZhenyu Wang 
773f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
7747648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
775f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
776f97108d1SJesse Barnes 	}
777f97108d1SJesse Barnes 
778fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
779fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
7803b8d8d91SJesse Barnes 
781c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
782c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
783c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
784c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
7854912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
786036a4a7dSZhenyu Wang 
787c7c85101SZou Nan hai done:
7882d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
7893143a2bfSChris Wilson 	POSTING_READ(DEIER);
7902d109a84SZou, Nanhai 
791036a4a7dSZhenyu Wang 	return ret;
792036a4a7dSZhenyu Wang }
793036a4a7dSZhenyu Wang 
7948a905236SJesse Barnes /**
7958a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
7968a905236SJesse Barnes  * @work: work struct
7978a905236SJesse Barnes  *
7988a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
7998a905236SJesse Barnes  * was detected.
8008a905236SJesse Barnes  */
8018a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
8028a905236SJesse Barnes {
8038a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
8048a905236SJesse Barnes 						    error_work);
8058a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
806f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
807f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
808f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
8098a905236SJesse Barnes 
810f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
8118a905236SJesse Barnes 
812ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
81344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
814f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
815d4b8bb2aSDaniel Vetter 		if (!i915_reset(dev)) {
816ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
817f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
818f316a42cSBen Gamari 		}
81930dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
820f316a42cSBen Gamari 	}
8218a905236SJesse Barnes }
8228a905236SJesse Barnes 
8233bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
8249df30794SChris Wilson static struct drm_i915_error_object *
825bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
82605394f39SChris Wilson 			 struct drm_i915_gem_object *src)
8279df30794SChris Wilson {
8289df30794SChris Wilson 	struct drm_i915_error_object *dst;
8299df30794SChris Wilson 	int page, page_count;
830e56660ddSChris Wilson 	u32 reloc_offset;
8319df30794SChris Wilson 
83205394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
8339df30794SChris Wilson 		return NULL;
8349df30794SChris Wilson 
83505394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
8369df30794SChris Wilson 
8379df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof(u32 *), GFP_ATOMIC);
8389df30794SChris Wilson 	if (dst == NULL)
8399df30794SChris Wilson 		return NULL;
8409df30794SChris Wilson 
84105394f39SChris Wilson 	reloc_offset = src->gtt_offset;
8429df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
843788885aeSAndrew Morton 		unsigned long flags;
844e56660ddSChris Wilson 		void *d;
845788885aeSAndrew Morton 
846e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
8479df30794SChris Wilson 		if (d == NULL)
8489df30794SChris Wilson 			goto unwind;
849e56660ddSChris Wilson 
850788885aeSAndrew Morton 		local_irq_save(flags);
85174898d7eSDaniel Vetter 		if (reloc_offset < dev_priv->mm.gtt_mappable_end &&
85274898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
853172975aaSChris Wilson 			void __iomem *s;
854172975aaSChris Wilson 
855172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
856172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
857172975aaSChris Wilson 			 * captures what the GPU read.
858172975aaSChris Wilson 			 */
859172975aaSChris Wilson 
860e56660ddSChris Wilson 			s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
8613e4d3af5SPeter Zijlstra 						     reloc_offset);
862e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
8633e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
864172975aaSChris Wilson 		} else {
865172975aaSChris Wilson 			void *s;
866172975aaSChris Wilson 
867172975aaSChris Wilson 			drm_clflush_pages(&src->pages[page], 1);
868172975aaSChris Wilson 
869172975aaSChris Wilson 			s = kmap_atomic(src->pages[page]);
870172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
871172975aaSChris Wilson 			kunmap_atomic(s);
872172975aaSChris Wilson 
873172975aaSChris Wilson 			drm_clflush_pages(&src->pages[page], 1);
874172975aaSChris Wilson 		}
875788885aeSAndrew Morton 		local_irq_restore(flags);
876e56660ddSChris Wilson 
8779df30794SChris Wilson 		dst->pages[page] = d;
878e56660ddSChris Wilson 
879e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
8809df30794SChris Wilson 	}
8819df30794SChris Wilson 	dst->page_count = page_count;
88205394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
8839df30794SChris Wilson 
8849df30794SChris Wilson 	return dst;
8859df30794SChris Wilson 
8869df30794SChris Wilson unwind:
8879df30794SChris Wilson 	while (page--)
8889df30794SChris Wilson 		kfree(dst->pages[page]);
8899df30794SChris Wilson 	kfree(dst);
8909df30794SChris Wilson 	return NULL;
8919df30794SChris Wilson }
8929df30794SChris Wilson 
8939df30794SChris Wilson static void
8949df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
8959df30794SChris Wilson {
8969df30794SChris Wilson 	int page;
8979df30794SChris Wilson 
8989df30794SChris Wilson 	if (obj == NULL)
8999df30794SChris Wilson 		return;
9009df30794SChris Wilson 
9019df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
9029df30794SChris Wilson 		kfree(obj->pages[page]);
9039df30794SChris Wilson 
9049df30794SChris Wilson 	kfree(obj);
9059df30794SChris Wilson }
9069df30794SChris Wilson 
907742cbee8SDaniel Vetter void
908742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
9099df30794SChris Wilson {
910742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
911742cbee8SDaniel Vetter 							  typeof(*error), ref);
912e2f973d5SChris Wilson 	int i;
913e2f973d5SChris Wilson 
91452d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
91552d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
91652d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
91752d39a21SChris Wilson 		kfree(error->ring[i].requests);
91852d39a21SChris Wilson 	}
919e2f973d5SChris Wilson 
9209df30794SChris Wilson 	kfree(error->active_bo);
9216ef3d427SChris Wilson 	kfree(error->overlay);
9229df30794SChris Wilson 	kfree(error);
9239df30794SChris Wilson }
9241b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
9251b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
926c724e8a9SChris Wilson {
927c724e8a9SChris Wilson 	err->size = obj->base.size;
928c724e8a9SChris Wilson 	err->name = obj->base.name;
929c724e8a9SChris Wilson 	err->seqno = obj->last_rendering_seqno;
930c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
931c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
932c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
933c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
934c724e8a9SChris Wilson 	err->pinned = 0;
935c724e8a9SChris Wilson 	if (obj->pin_count > 0)
936c724e8a9SChris Wilson 		err->pinned = 1;
937c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
938c724e8a9SChris Wilson 		err->pinned = -1;
939c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
940c724e8a9SChris Wilson 	err->dirty = obj->dirty;
941c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
94296154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
94393dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
9441b50247aSChris Wilson }
945c724e8a9SChris Wilson 
9461b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
9471b50247aSChris Wilson 			     int count, struct list_head *head)
9481b50247aSChris Wilson {
9491b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
9501b50247aSChris Wilson 	int i = 0;
9511b50247aSChris Wilson 
9521b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
9531b50247aSChris Wilson 		capture_bo(err++, obj);
954c724e8a9SChris Wilson 		if (++i == count)
955c724e8a9SChris Wilson 			break;
9561b50247aSChris Wilson 	}
957c724e8a9SChris Wilson 
9581b50247aSChris Wilson 	return i;
9591b50247aSChris Wilson }
9601b50247aSChris Wilson 
9611b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
9621b50247aSChris Wilson 			     int count, struct list_head *head)
9631b50247aSChris Wilson {
9641b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
9651b50247aSChris Wilson 	int i = 0;
9661b50247aSChris Wilson 
9671b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
9681b50247aSChris Wilson 		if (obj->pin_count == 0)
9691b50247aSChris Wilson 			continue;
9701b50247aSChris Wilson 
9711b50247aSChris Wilson 		capture_bo(err++, obj);
9721b50247aSChris Wilson 		if (++i == count)
9731b50247aSChris Wilson 			break;
974c724e8a9SChris Wilson 	}
975c724e8a9SChris Wilson 
976c724e8a9SChris Wilson 	return i;
977c724e8a9SChris Wilson }
978c724e8a9SChris Wilson 
979748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
980748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
981748ebc60SChris Wilson {
982748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
983748ebc60SChris Wilson 	int i;
984748ebc60SChris Wilson 
985748ebc60SChris Wilson 	/* Fences */
986748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
987775d17b6SDaniel Vetter 	case 7:
988748ebc60SChris Wilson 	case 6:
989748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
990748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
991748ebc60SChris Wilson 		break;
992748ebc60SChris Wilson 	case 5:
993748ebc60SChris Wilson 	case 4:
994748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
995748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
996748ebc60SChris Wilson 		break;
997748ebc60SChris Wilson 	case 3:
998748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
999748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
1000748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1001748ebc60SChris Wilson 	case 2:
1002748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
1003748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1004748ebc60SChris Wilson 		break;
1005748ebc60SChris Wilson 
1006748ebc60SChris Wilson 	}
1007748ebc60SChris Wilson }
1008748ebc60SChris Wilson 
1009bcfb2e28SChris Wilson static struct drm_i915_error_object *
1010bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1011bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1012bcfb2e28SChris Wilson {
1013bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1014bcfb2e28SChris Wilson 	u32 seqno;
1015bcfb2e28SChris Wilson 
1016bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1017bcfb2e28SChris Wilson 		return NULL;
1018bcfb2e28SChris Wilson 
1019bcfb2e28SChris Wilson 	seqno = ring->get_seqno(ring);
1020bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1021bcfb2e28SChris Wilson 		if (obj->ring != ring)
1022bcfb2e28SChris Wilson 			continue;
1023bcfb2e28SChris Wilson 
1024c37d9a5dSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
1025bcfb2e28SChris Wilson 			continue;
1026bcfb2e28SChris Wilson 
1027bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1028bcfb2e28SChris Wilson 			continue;
1029bcfb2e28SChris Wilson 
1030bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1031bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1032bcfb2e28SChris Wilson 		 */
1033bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1034bcfb2e28SChris Wilson 	}
1035bcfb2e28SChris Wilson 
1036bcfb2e28SChris Wilson 	return NULL;
1037bcfb2e28SChris Wilson }
1038bcfb2e28SChris Wilson 
1039d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1040d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1041d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1042d27b1e0eSDaniel Vetter {
1043d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1044d27b1e0eSDaniel Vetter 
104533f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
104633f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
10477e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
10487e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
10497e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
10507e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
105133f3f518SDaniel Vetter 	}
1052c1cd90edSDaniel Vetter 
1053d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
10549d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1055d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1056d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1057d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1058c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1059d27b1e0eSDaniel Vetter 		if (ring->id == RCS) {
1060d27b1e0eSDaniel Vetter 			error->instdone1 = I915_READ(INSTDONE1);
1061d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1062d27b1e0eSDaniel Vetter 		}
1063d27b1e0eSDaniel Vetter 	} else {
10649d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1065d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1066d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1067d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1068d27b1e0eSDaniel Vetter 	}
1069d27b1e0eSDaniel Vetter 
10709574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1071c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1072d27b1e0eSDaniel Vetter 	error->seqno[ring->id] = ring->get_seqno(ring);
1073d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1074c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1075c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
10767e3b8737SDaniel Vetter 
10777e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
10787e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1079d27b1e0eSDaniel Vetter }
1080d27b1e0eSDaniel Vetter 
108152d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
108252d39a21SChris Wilson 				  struct drm_i915_error_state *error)
108352d39a21SChris Wilson {
108452d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1085b4519513SChris Wilson 	struct intel_ring_buffer *ring;
108652d39a21SChris Wilson 	struct drm_i915_gem_request *request;
108752d39a21SChris Wilson 	int i, count;
108852d39a21SChris Wilson 
1089b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
109052d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
109152d39a21SChris Wilson 
109252d39a21SChris Wilson 		error->ring[i].batchbuffer =
109352d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
109452d39a21SChris Wilson 
109552d39a21SChris Wilson 		error->ring[i].ringbuffer =
109652d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
109752d39a21SChris Wilson 
109852d39a21SChris Wilson 		count = 0;
109952d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
110052d39a21SChris Wilson 			count++;
110152d39a21SChris Wilson 
110252d39a21SChris Wilson 		error->ring[i].num_requests = count;
110352d39a21SChris Wilson 		error->ring[i].requests =
110452d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
110552d39a21SChris Wilson 				GFP_ATOMIC);
110652d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
110752d39a21SChris Wilson 			error->ring[i].num_requests = 0;
110852d39a21SChris Wilson 			continue;
110952d39a21SChris Wilson 		}
111052d39a21SChris Wilson 
111152d39a21SChris Wilson 		count = 0;
111252d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
111352d39a21SChris Wilson 			struct drm_i915_error_request *erq;
111452d39a21SChris Wilson 
111552d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
111652d39a21SChris Wilson 			erq->seqno = request->seqno;
111752d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1118ee4f42b1SChris Wilson 			erq->tail = request->tail;
111952d39a21SChris Wilson 		}
112052d39a21SChris Wilson 	}
112152d39a21SChris Wilson }
112252d39a21SChris Wilson 
11238a905236SJesse Barnes /**
11248a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
11258a905236SJesse Barnes  * @dev: drm device
11268a905236SJesse Barnes  *
11278a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
11288a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
11298a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
11308a905236SJesse Barnes  * to pick up.
11318a905236SJesse Barnes  */
113263eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
113363eeaf38SJesse Barnes {
113463eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
113505394f39SChris Wilson 	struct drm_i915_gem_object *obj;
113663eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
113763eeaf38SJesse Barnes 	unsigned long flags;
11389db4a9c7SJesse Barnes 	int i, pipe;
113963eeaf38SJesse Barnes 
114063eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
11419df30794SChris Wilson 	error = dev_priv->first_error;
11429df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
11439df30794SChris Wilson 	if (error)
11449df30794SChris Wilson 		return;
114563eeaf38SJesse Barnes 
11469db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
114733f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
114863eeaf38SJesse Barnes 	if (!error) {
11499df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
11509df30794SChris Wilson 		return;
115163eeaf38SJesse Barnes 	}
115263eeaf38SJesse Barnes 
1153b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
1154b6f7833bSChris Wilson 		 dev->primary->index);
11552fa772f3SChris Wilson 
1156742cbee8SDaniel Vetter 	kref_init(&error->ref);
115763eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
115863eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1159*b9a3906bSBen Widawsky 	error->ccid = I915_READ(CCID);
1160be998e2eSBen Widawsky 
1161be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1162be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1163be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1164be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1165be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1166be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1167be998e2eSBen Widawsky 	else
1168be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1169be998e2eSBen Widawsky 
11709db4a9c7SJesse Barnes 	for_each_pipe(pipe)
11719db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1172d27b1e0eSDaniel Vetter 
117333f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1174f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
117533f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
117633f3f518SDaniel Vetter 	}
1177add354ddSChris Wilson 
1178748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
117952d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
11809df30794SChris Wilson 
1181c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
11829df30794SChris Wilson 	error->active_bo = NULL;
1183c724e8a9SChris Wilson 	error->pinned_bo = NULL;
11849df30794SChris Wilson 
1185bcfb2e28SChris Wilson 	i = 0;
1186bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1187bcfb2e28SChris Wilson 		i++;
1188bcfb2e28SChris Wilson 	error->active_bo_count = i;
11891b50247aSChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list)
11901b50247aSChris Wilson 		if (obj->pin_count)
1191bcfb2e28SChris Wilson 			i++;
1192bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1193c724e8a9SChris Wilson 
11948e934dbfSChris Wilson 	error->active_bo = NULL;
11958e934dbfSChris Wilson 	error->pinned_bo = NULL;
1196bcfb2e28SChris Wilson 	if (i) {
1197bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
11989df30794SChris Wilson 					   GFP_ATOMIC);
1199c724e8a9SChris Wilson 		if (error->active_bo)
1200c724e8a9SChris Wilson 			error->pinned_bo =
1201c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
12029df30794SChris Wilson 	}
1203c724e8a9SChris Wilson 
1204c724e8a9SChris Wilson 	if (error->active_bo)
1205c724e8a9SChris Wilson 		error->active_bo_count =
12061b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1207c724e8a9SChris Wilson 					  error->active_bo_count,
1208c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1209c724e8a9SChris Wilson 
1210c724e8a9SChris Wilson 	if (error->pinned_bo)
1211c724e8a9SChris Wilson 		error->pinned_bo_count =
12121b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1213c724e8a9SChris Wilson 					  error->pinned_bo_count,
12141b50247aSChris Wilson 					  &dev_priv->mm.gtt_list);
121563eeaf38SJesse Barnes 
12168a905236SJesse Barnes 	do_gettimeofday(&error->time);
12178a905236SJesse Barnes 
12186ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1219c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
12206ef3d427SChris Wilson 
12219df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12229df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
122363eeaf38SJesse Barnes 		dev_priv->first_error = error;
12249df30794SChris Wilson 		error = NULL;
12259df30794SChris Wilson 	}
122663eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
12279df30794SChris Wilson 
12289df30794SChris Wilson 	if (error)
1229742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
12309df30794SChris Wilson }
12319df30794SChris Wilson 
12329df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
12339df30794SChris Wilson {
12349df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
12359df30794SChris Wilson 	struct drm_i915_error_state *error;
12366dc0e816SBen Widawsky 	unsigned long flags;
12379df30794SChris Wilson 
12386dc0e816SBen Widawsky 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12399df30794SChris Wilson 	error = dev_priv->first_error;
12409df30794SChris Wilson 	dev_priv->first_error = NULL;
12416dc0e816SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
12429df30794SChris Wilson 
12439df30794SChris Wilson 	if (error)
1244742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
124563eeaf38SJesse Barnes }
12463bd3c932SChris Wilson #else
12473bd3c932SChris Wilson #define i915_capture_error_state(x)
12483bd3c932SChris Wilson #endif
124963eeaf38SJesse Barnes 
125035aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1251c0e09200SDave Airlie {
12528a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
125363eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
12549db4a9c7SJesse Barnes 	int pipe;
125563eeaf38SJesse Barnes 
125635aed2e6SChris Wilson 	if (!eir)
125735aed2e6SChris Wilson 		return;
125863eeaf38SJesse Barnes 
1259a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
12608a905236SJesse Barnes 
12618a905236SJesse Barnes 	if (IS_G4X(dev)) {
12628a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
12638a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
12648a905236SJesse Barnes 
1265a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1266a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1267a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n",
12688a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
1269a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1270a70491ccSJoe Perches 			pr_err("  INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1271a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
12728a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
12733143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
12748a905236SJesse Barnes 		}
12758a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
12768a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1277a70491ccSJoe Perches 			pr_err("page table error\n");
1278a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
12798a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
12803143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
12818a905236SJesse Barnes 		}
12828a905236SJesse Barnes 	}
12838a905236SJesse Barnes 
1284a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
128563eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
128663eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1287a70491ccSJoe Perches 			pr_err("page table error\n");
1288a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
128963eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
12903143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
129163eeaf38SJesse Barnes 		}
12928a905236SJesse Barnes 	}
12938a905236SJesse Barnes 
129463eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1295a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
12969db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1297a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
12989db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
129963eeaf38SJesse Barnes 		/* pipestat has already been acked */
130063eeaf38SJesse Barnes 	}
130163eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1302a70491ccSJoe Perches 		pr_err("instruction error\n");
1303a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1304a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
130563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
130663eeaf38SJesse Barnes 
1307a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1308a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1309a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n", I915_READ(INSTDONE));
1310a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
131163eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
13123143a2bfSChris Wilson 			POSTING_READ(IPEIR);
131363eeaf38SJesse Barnes 		} else {
131463eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
131563eeaf38SJesse Barnes 
1316a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1317a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1318a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n",
131963eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
1320a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1321a70491ccSJoe Perches 			pr_err("  INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1322a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
132363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
13243143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
132563eeaf38SJesse Barnes 		}
132663eeaf38SJesse Barnes 	}
132763eeaf38SJesse Barnes 
132863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
13293143a2bfSChris Wilson 	POSTING_READ(EIR);
133063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
133163eeaf38SJesse Barnes 	if (eir) {
133263eeaf38SJesse Barnes 		/*
133363eeaf38SJesse Barnes 		 * some errors might have become stuck,
133463eeaf38SJesse Barnes 		 * mask them.
133563eeaf38SJesse Barnes 		 */
133663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
133763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
133863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
133963eeaf38SJesse Barnes 	}
134035aed2e6SChris Wilson }
134135aed2e6SChris Wilson 
134235aed2e6SChris Wilson /**
134335aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
134435aed2e6SChris Wilson  * @dev: drm device
134535aed2e6SChris Wilson  *
134635aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
134735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
134835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
134935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
135035aed2e6SChris Wilson  * of a ring dump etc.).
135135aed2e6SChris Wilson  */
1352527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
135335aed2e6SChris Wilson {
135435aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1355b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1356b4519513SChris Wilson 	int i;
135735aed2e6SChris Wilson 
135835aed2e6SChris Wilson 	i915_capture_error_state(dev);
135935aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
13608a905236SJesse Barnes 
1361ba1234d1SBen Gamari 	if (wedged) {
136230dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1363ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1364ba1234d1SBen Gamari 
136511ed50ecSBen Gamari 		/*
136611ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
136711ed50ecSBen Gamari 		 */
1368b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1369b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
137011ed50ecSBen Gamari 	}
137111ed50ecSBen Gamari 
13729c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
13738a905236SJesse Barnes }
13748a905236SJesse Barnes 
13754e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
13764e5359cdSSimon Farnsworth {
13774e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
13784e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13794e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
138005394f39SChris Wilson 	struct drm_i915_gem_object *obj;
13814e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
13824e5359cdSSimon Farnsworth 	unsigned long flags;
13834e5359cdSSimon Farnsworth 	bool stall_detected;
13844e5359cdSSimon Farnsworth 
13854e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
13864e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
13874e5359cdSSimon Farnsworth 		return;
13884e5359cdSSimon Farnsworth 
13894e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
13904e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
13914e5359cdSSimon Farnsworth 
13924e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
13934e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
13944e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
13954e5359cdSSimon Farnsworth 		return;
13964e5359cdSSimon Farnsworth 	}
13974e5359cdSSimon Farnsworth 
13984e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
139905394f39SChris Wilson 	obj = work->pending_flip_obj;
1400a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
14019db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1402446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1403446f2545SArmin Reese 					obj->gtt_offset;
14044e5359cdSSimon Farnsworth 	} else {
14059db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
140605394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
140701f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
14084e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
14094e5359cdSSimon Farnsworth 	}
14104e5359cdSSimon Farnsworth 
14114e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
14124e5359cdSSimon Farnsworth 
14134e5359cdSSimon Farnsworth 	if (stall_detected) {
14144e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
14154e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
14164e5359cdSSimon Farnsworth 	}
14174e5359cdSSimon Farnsworth }
14184e5359cdSSimon Farnsworth 
141942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
142042f52ef8SKeith Packard  * we use as a pipe index
142142f52ef8SKeith Packard  */
1422f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
14230a3e67a4SJesse Barnes {
14240a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1425e9d21d7fSKeith Packard 	unsigned long irqflags;
142671e0ffa5SJesse Barnes 
14275eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
142871e0ffa5SJesse Barnes 		return -EINVAL;
14290a3e67a4SJesse Barnes 
14301ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1431f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
14327c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14337c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
14340a3e67a4SJesse Barnes 	else
14357c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14367c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
14378692d00eSChris Wilson 
14388692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
14398692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
14406b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
14411ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14428692d00eSChris Wilson 
14430a3e67a4SJesse Barnes 	return 0;
14440a3e67a4SJesse Barnes }
14450a3e67a4SJesse Barnes 
1446f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1447f796cf8fSJesse Barnes {
1448f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1449f796cf8fSJesse Barnes 	unsigned long irqflags;
1450f796cf8fSJesse Barnes 
1451f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1452f796cf8fSJesse Barnes 		return -EINVAL;
1453f796cf8fSJesse Barnes 
1454f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1455f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1456f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1457f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1458f796cf8fSJesse Barnes 
1459f796cf8fSJesse Barnes 	return 0;
1460f796cf8fSJesse Barnes }
1461f796cf8fSJesse Barnes 
1462f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1463b1f14ad0SJesse Barnes {
1464b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1465b1f14ad0SJesse Barnes 	unsigned long irqflags;
1466b1f14ad0SJesse Barnes 
1467b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1468b1f14ad0SJesse Barnes 		return -EINVAL;
1469b1f14ad0SJesse Barnes 
1470b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1471b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
1472b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
1473b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1474b1f14ad0SJesse Barnes 
1475b1f14ad0SJesse Barnes 	return 0;
1476b1f14ad0SJesse Barnes }
1477b1f14ad0SJesse Barnes 
14787e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
14797e231dbeSJesse Barnes {
14807e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
14817e231dbeSJesse Barnes 	unsigned long irqflags;
14827e231dbeSJesse Barnes 	u32 dpfl, imr;
14837e231dbeSJesse Barnes 
14847e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
14857e231dbeSJesse Barnes 		return -EINVAL;
14867e231dbeSJesse Barnes 
14877e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
14887e231dbeSJesse Barnes 	dpfl = I915_READ(VLV_DPFLIPSTAT);
14897e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
14907e231dbeSJesse Barnes 	if (pipe == 0) {
14917e231dbeSJesse Barnes 		dpfl |= PIPEA_VBLANK_INT_EN;
14927e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
14937e231dbeSJesse Barnes 	} else {
14947e231dbeSJesse Barnes 		dpfl |= PIPEA_VBLANK_INT_EN;
14957e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
14967e231dbeSJesse Barnes 	}
14977e231dbeSJesse Barnes 	I915_WRITE(VLV_DPFLIPSTAT, dpfl);
14987e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
14997e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15007e231dbeSJesse Barnes 
15017e231dbeSJesse Barnes 	return 0;
15027e231dbeSJesse Barnes }
15037e231dbeSJesse Barnes 
150442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
150542f52ef8SKeith Packard  * we use as a pipe index
150642f52ef8SKeith Packard  */
1507f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
15080a3e67a4SJesse Barnes {
15090a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1510e9d21d7fSKeith Packard 	unsigned long irqflags;
15110a3e67a4SJesse Barnes 
15121ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15138692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15146b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
15158692d00eSChris Wilson 
15167c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
15177c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
15187c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
15191ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15200a3e67a4SJesse Barnes }
15210a3e67a4SJesse Barnes 
1522f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1523f796cf8fSJesse Barnes {
1524f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1525f796cf8fSJesse Barnes 	unsigned long irqflags;
1526f796cf8fSJesse Barnes 
1527f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1528f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1529f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1530f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1531f796cf8fSJesse Barnes }
1532f796cf8fSJesse Barnes 
1533f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1534b1f14ad0SJesse Barnes {
1535b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1536b1f14ad0SJesse Barnes 	unsigned long irqflags;
1537b1f14ad0SJesse Barnes 
1538b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1539b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
1540b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
1541b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1542b1f14ad0SJesse Barnes }
1543b1f14ad0SJesse Barnes 
15447e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
15457e231dbeSJesse Barnes {
15467e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
15477e231dbeSJesse Barnes 	unsigned long irqflags;
15487e231dbeSJesse Barnes 	u32 dpfl, imr;
15497e231dbeSJesse Barnes 
15507e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15517e231dbeSJesse Barnes 	dpfl = I915_READ(VLV_DPFLIPSTAT);
15527e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
15537e231dbeSJesse Barnes 	if (pipe == 0) {
15547e231dbeSJesse Barnes 		dpfl &= ~PIPEA_VBLANK_INT_EN;
15557e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
15567e231dbeSJesse Barnes 	} else {
15577e231dbeSJesse Barnes 		dpfl &= ~PIPEB_VBLANK_INT_EN;
15587e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
15597e231dbeSJesse Barnes 	}
15607e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
15617e231dbeSJesse Barnes 	I915_WRITE(VLV_DPFLIPSTAT, dpfl);
15627e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15637e231dbeSJesse Barnes }
15647e231dbeSJesse Barnes 
1565893eead0SChris Wilson static u32
1566893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1567852835f3SZou Nan hai {
1568893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1569893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1570893eead0SChris Wilson }
1571893eead0SChris Wilson 
1572893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1573893eead0SChris Wilson {
1574893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1575893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1576893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
15779574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
15789574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
15799574b3feSBen Widawsky 				  ring->name);
1580893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1581893eead0SChris Wilson 			*err = true;
1582893eead0SChris Wilson 		}
1583893eead0SChris Wilson 		return true;
1584893eead0SChris Wilson 	}
1585893eead0SChris Wilson 	return false;
1586f65d9421SBen Gamari }
1587f65d9421SBen Gamari 
15881ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
15891ec14ad3SChris Wilson {
15901ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
15911ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
15921ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
15931ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
15941ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
15951ec14ad3SChris Wilson 			  ring->name);
15961ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
15971ec14ad3SChris Wilson 		return true;
15981ec14ad3SChris Wilson 	}
15991ec14ad3SChris Wilson 	return false;
16001ec14ad3SChris Wilson }
16011ec14ad3SChris Wilson 
1602d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
1603d1e61e7fSChris Wilson {
1604d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1605d1e61e7fSChris Wilson 
1606d1e61e7fSChris Wilson 	if (dev_priv->hangcheck_count++ > 1) {
1607b4519513SChris Wilson 		bool hung = true;
1608b4519513SChris Wilson 
1609d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1610d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
1611d1e61e7fSChris Wilson 
1612d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
1613b4519513SChris Wilson 			struct intel_ring_buffer *ring;
1614b4519513SChris Wilson 			int i;
1615b4519513SChris Wilson 
1616d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
1617d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
1618d1e61e7fSChris Wilson 			 * and break the hang. This should work on
1619d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
1620d1e61e7fSChris Wilson 			 */
1621b4519513SChris Wilson 			for_each_ring(ring, dev_priv, i)
1622b4519513SChris Wilson 				hung &= !kick_ring(ring);
1623d1e61e7fSChris Wilson 		}
1624d1e61e7fSChris Wilson 
1625b4519513SChris Wilson 		return hung;
1626d1e61e7fSChris Wilson 	}
1627d1e61e7fSChris Wilson 
1628d1e61e7fSChris Wilson 	return false;
1629d1e61e7fSChris Wilson }
1630d1e61e7fSChris Wilson 
1631f65d9421SBen Gamari /**
1632f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1633f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1634f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1635f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1636f65d9421SBen Gamari  */
1637f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1638f65d9421SBen Gamari {
1639f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1640f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1641b4519513SChris Wilson 	uint32_t acthd[I915_NUM_RINGS], instdone, instdone1;
1642b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1643b4519513SChris Wilson 	bool err = false, idle;
1644b4519513SChris Wilson 	int i;
1645893eead0SChris Wilson 
16463e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
16473e0dc6b0SBen Widawsky 		return;
16483e0dc6b0SBen Widawsky 
1649b4519513SChris Wilson 	memset(acthd, 0, sizeof(acthd));
1650b4519513SChris Wilson 	idle = true;
1651b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
1652b4519513SChris Wilson 	    idle &= i915_hangcheck_ring_idle(ring, &err);
1653b4519513SChris Wilson 	    acthd[i] = intel_ring_get_active_head(ring);
1654b4519513SChris Wilson 	}
1655b4519513SChris Wilson 
1656893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
1657b4519513SChris Wilson 	if (idle) {
1658d1e61e7fSChris Wilson 		if (err) {
1659d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
1660d1e61e7fSChris Wilson 				return;
1661d1e61e7fSChris Wilson 
1662893eead0SChris Wilson 			goto repeat;
1663d1e61e7fSChris Wilson 		}
1664d1e61e7fSChris Wilson 
1665d1e61e7fSChris Wilson 		dev_priv->hangcheck_count = 0;
1666893eead0SChris Wilson 		return;
1667893eead0SChris Wilson 	}
1668f65d9421SBen Gamari 
1669a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1670cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1671cbb465e7SChris Wilson 		instdone1 = 0;
1672cbb465e7SChris Wilson 	} else {
1673cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1674cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1675cbb465e7SChris Wilson 	}
1676f65d9421SBen Gamari 
1677b4519513SChris Wilson 	if (memcmp(dev_priv->last_acthd, acthd, sizeof(acthd)) == 0 &&
1678cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1679cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1680d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
1681f65d9421SBen Gamari 			return;
1682cbb465e7SChris Wilson 	} else {
1683cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1684cbb465e7SChris Wilson 
1685b4519513SChris Wilson 		memcpy(dev_priv->last_acthd, acthd, sizeof(acthd));
1686cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1687cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1688cbb465e7SChris Wilson 	}
1689f65d9421SBen Gamari 
1690893eead0SChris Wilson repeat:
1691f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1692b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1693b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1694f65d9421SBen Gamari }
1695f65d9421SBen Gamari 
1696c0e09200SDave Airlie /* drm_dma.h hooks
1697c0e09200SDave Airlie */
1698f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
1699036a4a7dSZhenyu Wang {
1700036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1701036a4a7dSZhenyu Wang 
17024697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17034697995bSJesse Barnes 
1704036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1705bdfcdb63SDaniel Vetter 
1706036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1707036a4a7dSZhenyu Wang 
1708036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1709036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
17103143a2bfSChris Wilson 	POSTING_READ(DEIER);
1711036a4a7dSZhenyu Wang 
1712036a4a7dSZhenyu Wang 	/* and GT */
1713036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1714036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
17153143a2bfSChris Wilson 	POSTING_READ(GTIER);
1716c650156aSZhenyu Wang 
1717c650156aSZhenyu Wang 	/* south display irq */
1718c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1719c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
17203143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1721036a4a7dSZhenyu Wang }
1722036a4a7dSZhenyu Wang 
17237e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
17247e231dbeSJesse Barnes {
17257e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17267e231dbeSJesse Barnes 	int pipe;
17277e231dbeSJesse Barnes 
17287e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17297e231dbeSJesse Barnes 
17307e231dbeSJesse Barnes 	/* VLV magic */
17317e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
17327e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
17337e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
17347e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
17357e231dbeSJesse Barnes 
17367e231dbeSJesse Barnes 	/* and GT */
17377e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
17387e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
17397e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
17407e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
17417e231dbeSJesse Barnes 	POSTING_READ(GTIER);
17427e231dbeSJesse Barnes 
17437e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
17447e231dbeSJesse Barnes 
17457e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
17467e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
17477e231dbeSJesse Barnes 	for_each_pipe(pipe)
17487e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
17497e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
17507e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
17517e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
17527e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
17537e231dbeSJesse Barnes }
17547e231dbeSJesse Barnes 
17557fe0b973SKeith Packard /*
17567fe0b973SKeith Packard  * Enable digital hotplug on the PCH, and configure the DP short pulse
17577fe0b973SKeith Packard  * duration to 2ms (which is the minimum in the Display Port spec)
17587fe0b973SKeith Packard  *
17597fe0b973SKeith Packard  * This register is the same on all known PCH chips.
17607fe0b973SKeith Packard  */
17617fe0b973SKeith Packard 
17627fe0b973SKeith Packard static void ironlake_enable_pch_hotplug(struct drm_device *dev)
17637fe0b973SKeith Packard {
17647fe0b973SKeith Packard 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17657fe0b973SKeith Packard 	u32	hotplug;
17667fe0b973SKeith Packard 
17677fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
17687fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
17697fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
17707fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
17717fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
17727fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
17737fe0b973SKeith Packard }
17747fe0b973SKeith Packard 
1775f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
1776036a4a7dSZhenyu Wang {
1777036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1778036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1779013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1780013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
17811ec14ad3SChris Wilson 	u32 render_irqs;
17822d7b8366SYuanhan Liu 	u32 hotplug_mask;
1783036a4a7dSZhenyu Wang 
17841ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1785036a4a7dSZhenyu Wang 
1786036a4a7dSZhenyu Wang 	/* should always can generate irq */
1787036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
17881ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
17891ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
17903143a2bfSChris Wilson 	POSTING_READ(DEIER);
1791036a4a7dSZhenyu Wang 
17921ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1793036a4a7dSZhenyu Wang 
1794036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
17951ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1796881f47b6SXiang, Haihao 
17971ec14ad3SChris Wilson 	if (IS_GEN6(dev))
17981ec14ad3SChris Wilson 		render_irqs =
17991ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
1800e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
1801e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
18021ec14ad3SChris Wilson 	else
18031ec14ad3SChris Wilson 		render_irqs =
180488f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1805c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
18061ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
18071ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
18083143a2bfSChris Wilson 	POSTING_READ(GTIER);
1809036a4a7dSZhenyu Wang 
18102d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
18119035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
18129035a97aSChris Wilson 				SDE_PORTB_HOTPLUG_CPT |
18139035a97aSChris Wilson 				SDE_PORTC_HOTPLUG_CPT |
18149035a97aSChris Wilson 				SDE_PORTD_HOTPLUG_CPT);
18152d7b8366SYuanhan Liu 	} else {
18169035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG |
18179035a97aSChris Wilson 				SDE_PORTB_HOTPLUG |
18189035a97aSChris Wilson 				SDE_PORTC_HOTPLUG |
18199035a97aSChris Wilson 				SDE_PORTD_HOTPLUG |
18209035a97aSChris Wilson 				SDE_AUX_MASK);
18212d7b8366SYuanhan Liu 	}
18222d7b8366SYuanhan Liu 
18231ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1824c650156aSZhenyu Wang 
1825c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
18261ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
18271ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
18283143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1829c650156aSZhenyu Wang 
18307fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
18317fe0b973SKeith Packard 
1832f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1833f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1834f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1835f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1836f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1837f97108d1SJesse Barnes 	}
1838f97108d1SJesse Barnes 
1839036a4a7dSZhenyu Wang 	return 0;
1840036a4a7dSZhenyu Wang }
1841036a4a7dSZhenyu Wang 
1842f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
1843b1f14ad0SJesse Barnes {
1844b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1845b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
1846b615b57aSChris Wilson 	u32 display_mask =
1847b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
1848b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
1849b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
1850b615b57aSChris Wilson 		DE_PLANEA_FLIP_DONE_IVB;
1851b1f14ad0SJesse Barnes 	u32 render_irqs;
1852b1f14ad0SJesse Barnes 	u32 hotplug_mask;
1853b1f14ad0SJesse Barnes 
1854b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
1855b1f14ad0SJesse Barnes 
1856b1f14ad0SJesse Barnes 	/* should always can generate irq */
1857b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1858b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
1859b615b57aSChris Wilson 	I915_WRITE(DEIER,
1860b615b57aSChris Wilson 		   display_mask |
1861b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
1862b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
1863b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
1864b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1865b1f14ad0SJesse Barnes 
186615b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
1867b1f14ad0SJesse Barnes 
1868b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1869b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1870b1f14ad0SJesse Barnes 
1871e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
187215b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
1873b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
1874b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
1875b1f14ad0SJesse Barnes 
1876b1f14ad0SJesse Barnes 	hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
1877b1f14ad0SJesse Barnes 			SDE_PORTB_HOTPLUG_CPT |
1878b1f14ad0SJesse Barnes 			SDE_PORTC_HOTPLUG_CPT |
1879b1f14ad0SJesse Barnes 			SDE_PORTD_HOTPLUG_CPT);
1880b1f14ad0SJesse Barnes 	dev_priv->pch_irq_mask = ~hotplug_mask;
1881b1f14ad0SJesse Barnes 
1882b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1883b1f14ad0SJesse Barnes 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1884b1f14ad0SJesse Barnes 	I915_WRITE(SDEIER, hotplug_mask);
1885b1f14ad0SJesse Barnes 	POSTING_READ(SDEIER);
1886b1f14ad0SJesse Barnes 
18877fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
18887fe0b973SKeith Packard 
1889b1f14ad0SJesse Barnes 	return 0;
1890b1f14ad0SJesse Barnes }
1891b1f14ad0SJesse Barnes 
18927e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
18937e231dbeSJesse Barnes {
18947e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18957e231dbeSJesse Barnes 	u32 render_irqs;
18967e231dbeSJesse Barnes 	u32 enable_mask;
18977e231dbeSJesse Barnes 	u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
18987e231dbeSJesse Barnes 	u16 msid;
18997e231dbeSJesse Barnes 
19007e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
19017e231dbeSJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
19027e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
19037e231dbeSJesse Barnes 
19047e231dbeSJesse Barnes 	dev_priv->irq_mask = ~enable_mask;
19057e231dbeSJesse Barnes 
19067e231dbeSJesse Barnes 	dev_priv->pipestat[0] = 0;
19077e231dbeSJesse Barnes 	dev_priv->pipestat[1] = 0;
19087e231dbeSJesse Barnes 
19097e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
19107e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
19117e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
19127e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
19137e231dbeSJesse Barnes 	msid |= (1<<14);
19147e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
19157e231dbeSJesse Barnes 
19167e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
19177e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
19187e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19197e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
19207e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
19217e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
19227e231dbeSJesse Barnes 
19237e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19247e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19257e231dbeSJesse Barnes 
19267e231dbeSJesse Barnes 	render_irqs = GT_GEN6_BLT_FLUSHDW_NOTIFY_INTERRUPT |
19277e231dbeSJesse Barnes 		GT_GEN6_BLT_CS_ERROR_INTERRUPT |
1928e2a1e2f0SBen Widawsky 		GT_GEN6_BLT_USER_INTERRUPT |
19297e231dbeSJesse Barnes 		GT_GEN6_BSD_USER_INTERRUPT |
19307e231dbeSJesse Barnes 		GT_GEN6_BSD_CS_ERROR_INTERRUPT |
19317e231dbeSJesse Barnes 		GT_GEN7_L3_PARITY_ERROR_INTERRUPT |
19327e231dbeSJesse Barnes 		GT_PIPE_NOTIFY |
19337e231dbeSJesse Barnes 		GT_RENDER_CS_ERROR_INTERRUPT |
19347e231dbeSJesse Barnes 		GT_SYNC_STATUS |
19357e231dbeSJesse Barnes 		GT_USER_INTERRUPT;
19367e231dbeSJesse Barnes 
19377e231dbeSJesse Barnes 	dev_priv->gt_irq_mask = ~render_irqs;
19387e231dbeSJesse Barnes 
19397e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
19407e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
19417e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0);
19427e231dbeSJesse Barnes 	I915_WRITE(GTIER, render_irqs);
19437e231dbeSJesse Barnes 	POSTING_READ(GTIER);
19447e231dbeSJesse Barnes 
19457e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
19467e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
19477e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
19487e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
19497e231dbeSJesse Barnes #endif
19507e231dbeSJesse Barnes 
19517e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
19527e231dbeSJesse Barnes #if 0 /* FIXME: check register definitions; some have moved */
19537e231dbeSJesse Barnes 	/* Note HDMI and DP share bits */
19547e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
19557e231dbeSJesse Barnes 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
19567e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
19577e231dbeSJesse Barnes 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
19587e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
19597e231dbeSJesse Barnes 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
19607e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
19617e231dbeSJesse Barnes 		hotplug_en |= SDVOC_HOTPLUG_INT_EN;
19627e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
19637e231dbeSJesse Barnes 		hotplug_en |= SDVOB_HOTPLUG_INT_EN;
19647e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
19657e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_INT_EN;
19667e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
19677e231dbeSJesse Barnes 	}
19687e231dbeSJesse Barnes #endif
19697e231dbeSJesse Barnes 
19707e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
19717e231dbeSJesse Barnes 
19727e231dbeSJesse Barnes 	return 0;
19737e231dbeSJesse Barnes }
19747e231dbeSJesse Barnes 
19757e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
19767e231dbeSJesse Barnes {
19777e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19787e231dbeSJesse Barnes 	int pipe;
19797e231dbeSJesse Barnes 
19807e231dbeSJesse Barnes 	if (!dev_priv)
19817e231dbeSJesse Barnes 		return;
19827e231dbeSJesse Barnes 
19837e231dbeSJesse Barnes 	for_each_pipe(pipe)
19847e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
19857e231dbeSJesse Barnes 
19867e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
19877e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
19887e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
19897e231dbeSJesse Barnes 	for_each_pipe(pipe)
19907e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
19917e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19927e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
19937e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
19947e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
19957e231dbeSJesse Barnes }
19967e231dbeSJesse Barnes 
1997f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
1998036a4a7dSZhenyu Wang {
1999036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20004697995bSJesse Barnes 
20014697995bSJesse Barnes 	if (!dev_priv)
20024697995bSJesse Barnes 		return;
20034697995bSJesse Barnes 
2004036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2005036a4a7dSZhenyu Wang 
2006036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2007036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2008036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2009036a4a7dSZhenyu Wang 
2010036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2011036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2012036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2013192aac1fSKeith Packard 
2014192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2015192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2016192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2017036a4a7dSZhenyu Wang }
2018036a4a7dSZhenyu Wang 
2019c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2020c2798b19SChris Wilson {
2021c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2022c2798b19SChris Wilson 	int pipe;
2023c2798b19SChris Wilson 
2024c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2025c2798b19SChris Wilson 
2026c2798b19SChris Wilson 	for_each_pipe(pipe)
2027c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2028c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2029c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2030c2798b19SChris Wilson 	POSTING_READ16(IER);
2031c2798b19SChris Wilson }
2032c2798b19SChris Wilson 
2033c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2034c2798b19SChris Wilson {
2035c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2036c2798b19SChris Wilson 
2037c2798b19SChris Wilson 	dev_priv->pipestat[0] = 0;
2038c2798b19SChris Wilson 	dev_priv->pipestat[1] = 0;
2039c2798b19SChris Wilson 
2040c2798b19SChris Wilson 	I915_WRITE16(EMR,
2041c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2042c2798b19SChris Wilson 
2043c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2044c2798b19SChris Wilson 	dev_priv->irq_mask =
2045c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2046c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2047c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2048c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2049c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2050c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2051c2798b19SChris Wilson 
2052c2798b19SChris Wilson 	I915_WRITE16(IER,
2053c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2054c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2055c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2056c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2057c2798b19SChris Wilson 	POSTING_READ16(IER);
2058c2798b19SChris Wilson 
2059c2798b19SChris Wilson 	return 0;
2060c2798b19SChris Wilson }
2061c2798b19SChris Wilson 
2062c2798b19SChris Wilson static irqreturn_t i8xx_irq_handler(DRM_IRQ_ARGS)
2063c2798b19SChris Wilson {
2064c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2065c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2066c2798b19SChris Wilson 	u16 iir, new_iir;
2067c2798b19SChris Wilson 	u32 pipe_stats[2];
2068c2798b19SChris Wilson 	unsigned long irqflags;
2069c2798b19SChris Wilson 	int irq_received;
2070c2798b19SChris Wilson 	int pipe;
2071c2798b19SChris Wilson 	u16 flip_mask =
2072c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2073c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2074c2798b19SChris Wilson 
2075c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2076c2798b19SChris Wilson 
2077c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2078c2798b19SChris Wilson 	if (iir == 0)
2079c2798b19SChris Wilson 		return IRQ_NONE;
2080c2798b19SChris Wilson 
2081c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2082c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2083c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2084c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2085c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2086c2798b19SChris Wilson 		 */
2087c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2088c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2089c2798b19SChris Wilson 			i915_handle_error(dev, false);
2090c2798b19SChris Wilson 
2091c2798b19SChris Wilson 		for_each_pipe(pipe) {
2092c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2093c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2094c2798b19SChris Wilson 
2095c2798b19SChris Wilson 			/*
2096c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2097c2798b19SChris Wilson 			 */
2098c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2099c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2100c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2101c2798b19SChris Wilson 							 pipe_name(pipe));
2102c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2103c2798b19SChris Wilson 				irq_received = 1;
2104c2798b19SChris Wilson 			}
2105c2798b19SChris Wilson 		}
2106c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2107c2798b19SChris Wilson 
2108c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2109c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2110c2798b19SChris Wilson 
2111d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2112c2798b19SChris Wilson 
2113c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2114c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2115c2798b19SChris Wilson 
2116c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
2117c2798b19SChris Wilson 		    drm_handle_vblank(dev, 0)) {
2118c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
2119c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 0);
2120c2798b19SChris Wilson 				intel_finish_page_flip(dev, 0);
2121c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT;
2122c2798b19SChris Wilson 			}
2123c2798b19SChris Wilson 		}
2124c2798b19SChris Wilson 
2125c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
2126c2798b19SChris Wilson 		    drm_handle_vblank(dev, 1)) {
2127c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
2128c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 1);
2129c2798b19SChris Wilson 				intel_finish_page_flip(dev, 1);
2130c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2131c2798b19SChris Wilson 			}
2132c2798b19SChris Wilson 		}
2133c2798b19SChris Wilson 
2134c2798b19SChris Wilson 		iir = new_iir;
2135c2798b19SChris Wilson 	}
2136c2798b19SChris Wilson 
2137c2798b19SChris Wilson 	return IRQ_HANDLED;
2138c2798b19SChris Wilson }
2139c2798b19SChris Wilson 
2140c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2141c2798b19SChris Wilson {
2142c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2143c2798b19SChris Wilson 	int pipe;
2144c2798b19SChris Wilson 
2145c2798b19SChris Wilson 	for_each_pipe(pipe) {
2146c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2147c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2148c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2149c2798b19SChris Wilson 	}
2150c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2151c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2152c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2153c2798b19SChris Wilson }
2154c2798b19SChris Wilson 
2155a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2156a266c7d5SChris Wilson {
2157a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2158a266c7d5SChris Wilson 	int pipe;
2159a266c7d5SChris Wilson 
2160a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2161a266c7d5SChris Wilson 
2162a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2163a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2164a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2165a266c7d5SChris Wilson 	}
2166a266c7d5SChris Wilson 
216700d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2168a266c7d5SChris Wilson 	for_each_pipe(pipe)
2169a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2170a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2171a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2172a266c7d5SChris Wilson 	POSTING_READ(IER);
2173a266c7d5SChris Wilson }
2174a266c7d5SChris Wilson 
2175a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2176a266c7d5SChris Wilson {
2177a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
217838bde180SChris Wilson 	u32 enable_mask;
2179a266c7d5SChris Wilson 
2180a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2181a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2182a266c7d5SChris Wilson 
218338bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
218438bde180SChris Wilson 
218538bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
218638bde180SChris Wilson 	dev_priv->irq_mask =
218738bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
218838bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
218938bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
219038bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
219138bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
219238bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
219338bde180SChris Wilson 
219438bde180SChris Wilson 	enable_mask =
219538bde180SChris Wilson 		I915_ASLE_INTERRUPT |
219638bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
219738bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
219838bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
219938bde180SChris Wilson 		I915_USER_INTERRUPT;
220038bde180SChris Wilson 
2201a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2202a266c7d5SChris Wilson 		/* Enable in IER... */
2203a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2204a266c7d5SChris Wilson 		/* and unmask in IMR */
2205a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2206a266c7d5SChris Wilson 	}
2207a266c7d5SChris Wilson 
2208a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2209a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2210a266c7d5SChris Wilson 	POSTING_READ(IER);
2211a266c7d5SChris Wilson 
2212a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2213a266c7d5SChris Wilson 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2214a266c7d5SChris Wilson 
2215a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2216a266c7d5SChris Wilson 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2217a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2218a266c7d5SChris Wilson 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2219a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2220a266c7d5SChris Wilson 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
2221084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
2222a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2223084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
2224a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2225a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2226a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_INT_EN;
2227a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2228a266c7d5SChris Wilson 		}
2229a266c7d5SChris Wilson 
2230a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2231a266c7d5SChris Wilson 
2232a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2233a266c7d5SChris Wilson 	}
2234a266c7d5SChris Wilson 
2235a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2236a266c7d5SChris Wilson 
2237a266c7d5SChris Wilson 	return 0;
2238a266c7d5SChris Wilson }
2239a266c7d5SChris Wilson 
2240a266c7d5SChris Wilson static irqreturn_t i915_irq_handler(DRM_IRQ_ARGS)
2241a266c7d5SChris Wilson {
2242a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2243a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22448291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2245a266c7d5SChris Wilson 	unsigned long irqflags;
224638bde180SChris Wilson 	u32 flip_mask =
224738bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
224838bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
224938bde180SChris Wilson 	u32 flip[2] = {
225038bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT,
225138bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT
225238bde180SChris Wilson 	};
225338bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2254a266c7d5SChris Wilson 
2255a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2256a266c7d5SChris Wilson 
2257a266c7d5SChris Wilson 	iir = I915_READ(IIR);
225838bde180SChris Wilson 	do {
225938bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
22608291ee90SChris Wilson 		bool blc_event = false;
2261a266c7d5SChris Wilson 
2262a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2263a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2264a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2265a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2266a266c7d5SChris Wilson 		 */
2267a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2268a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2269a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2270a266c7d5SChris Wilson 
2271a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2272a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2273a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2274a266c7d5SChris Wilson 
227538bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2276a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2277a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2278a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2279a266c7d5SChris Wilson 							 pipe_name(pipe));
2280a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
228138bde180SChris Wilson 				irq_received = true;
2282a266c7d5SChris Wilson 			}
2283a266c7d5SChris Wilson 		}
2284a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2285a266c7d5SChris Wilson 
2286a266c7d5SChris Wilson 		if (!irq_received)
2287a266c7d5SChris Wilson 			break;
2288a266c7d5SChris Wilson 
2289a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2290a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2291a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2292a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2293a266c7d5SChris Wilson 
2294a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2295a266c7d5SChris Wilson 				  hotplug_status);
2296a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2297a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2298a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2299a266c7d5SChris Wilson 
2300a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
230138bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2302a266c7d5SChris Wilson 		}
2303a266c7d5SChris Wilson 
230438bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2305a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2306a266c7d5SChris Wilson 
2307a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2308a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2309a266c7d5SChris Wilson 
2310a266c7d5SChris Wilson 		for_each_pipe(pipe) {
231138bde180SChris Wilson 			int plane = pipe;
231238bde180SChris Wilson 			if (IS_MOBILE(dev))
231338bde180SChris Wilson 				plane = !plane;
23148291ee90SChris Wilson 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
2315a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
231638bde180SChris Wilson 				if (iir & flip[plane]) {
231738bde180SChris Wilson 					intel_prepare_page_flip(dev, plane);
2318a266c7d5SChris Wilson 					intel_finish_page_flip(dev, pipe);
231938bde180SChris Wilson 					flip_mask &= ~flip[plane];
232038bde180SChris Wilson 				}
2321a266c7d5SChris Wilson 			}
2322a266c7d5SChris Wilson 
2323a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2324a266c7d5SChris Wilson 				blc_event = true;
2325a266c7d5SChris Wilson 		}
2326a266c7d5SChris Wilson 
2327a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2328a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2329a266c7d5SChris Wilson 
2330a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2331a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2332a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2333a266c7d5SChris Wilson 		 * we would never get another interrupt.
2334a266c7d5SChris Wilson 		 *
2335a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2336a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2337a266c7d5SChris Wilson 		 * another one.
2338a266c7d5SChris Wilson 		 *
2339a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2340a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2341a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2342a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2343a266c7d5SChris Wilson 		 * stray interrupts.
2344a266c7d5SChris Wilson 		 */
234538bde180SChris Wilson 		ret = IRQ_HANDLED;
2346a266c7d5SChris Wilson 		iir = new_iir;
234738bde180SChris Wilson 	} while (iir & ~flip_mask);
2348a266c7d5SChris Wilson 
2349d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
23508291ee90SChris Wilson 
2351a266c7d5SChris Wilson 	return ret;
2352a266c7d5SChris Wilson }
2353a266c7d5SChris Wilson 
2354a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2355a266c7d5SChris Wilson {
2356a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2357a266c7d5SChris Wilson 	int pipe;
2358a266c7d5SChris Wilson 
2359a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2360a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2361a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2362a266c7d5SChris Wilson 	}
2363a266c7d5SChris Wilson 
236400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
236555b39755SChris Wilson 	for_each_pipe(pipe) {
236655b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2367a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
236855b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
236955b39755SChris Wilson 	}
2370a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2371a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2372a266c7d5SChris Wilson 
2373a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2374a266c7d5SChris Wilson }
2375a266c7d5SChris Wilson 
2376a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2377a266c7d5SChris Wilson {
2378a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2379a266c7d5SChris Wilson 	int pipe;
2380a266c7d5SChris Wilson 
2381a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2382a266c7d5SChris Wilson 
2383a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2384a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2385a266c7d5SChris Wilson 
2386a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2387a266c7d5SChris Wilson 	for_each_pipe(pipe)
2388a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2389a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2390a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2391a266c7d5SChris Wilson 	POSTING_READ(IER);
2392a266c7d5SChris Wilson }
2393a266c7d5SChris Wilson 
2394a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2395a266c7d5SChris Wilson {
2396a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2397adca4730SChris Wilson 	u32 hotplug_en;
2398bbba0a97SChris Wilson 	u32 enable_mask;
2399a266c7d5SChris Wilson 	u32 error_mask;
2400a266c7d5SChris Wilson 
2401a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2402bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2403adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2404bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2405bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2406bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2407bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2408bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2409bbba0a97SChris Wilson 
2410bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
2411bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2412bbba0a97SChris Wilson 
2413bbba0a97SChris Wilson 	if (IS_G4X(dev))
2414bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2415a266c7d5SChris Wilson 
2416a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2417a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2418a266c7d5SChris Wilson 
2419a266c7d5SChris Wilson 	/*
2420a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2421a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2422a266c7d5SChris Wilson 	 */
2423a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2424a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2425a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2426a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2427a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2428a266c7d5SChris Wilson 	} else {
2429a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2430a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2431a266c7d5SChris Wilson 	}
2432a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2433a266c7d5SChris Wilson 
2434a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2435a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2436a266c7d5SChris Wilson 	POSTING_READ(IER);
2437a266c7d5SChris Wilson 
2438adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
2439adca4730SChris Wilson 	hotplug_en = 0;
2440a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2441a266c7d5SChris Wilson 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2442a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2443a266c7d5SChris Wilson 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2444a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2445a266c7d5SChris Wilson 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
2446084b612eSChris Wilson 	if (IS_G4X(dev)) {
2447084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_G4X)
2448a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2449084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_G4X)
2450a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2451084b612eSChris Wilson 	} else {
2452084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I965)
2453084b612eSChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2454084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I965)
2455084b612eSChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2456084b612eSChris Wilson 	}
2457a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2458a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_INT_EN;
2459a266c7d5SChris Wilson 
2460a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2461a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2462a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2463a266c7d5SChris Wilson 		   */
2464a266c7d5SChris Wilson 		if (IS_G4X(dev))
2465a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2466a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2467a266c7d5SChris Wilson 	}
2468a266c7d5SChris Wilson 
2469a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
2470a266c7d5SChris Wilson 
2471a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2472a266c7d5SChris Wilson 
2473a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2474a266c7d5SChris Wilson 
2475a266c7d5SChris Wilson 	return 0;
2476a266c7d5SChris Wilson }
2477a266c7d5SChris Wilson 
2478a266c7d5SChris Wilson static irqreturn_t i965_irq_handler(DRM_IRQ_ARGS)
2479a266c7d5SChris Wilson {
2480a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2481a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2482a266c7d5SChris Wilson 	u32 iir, new_iir;
2483a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2484a266c7d5SChris Wilson 	unsigned long irqflags;
2485a266c7d5SChris Wilson 	int irq_received;
2486a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
2487a266c7d5SChris Wilson 
2488a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2489a266c7d5SChris Wilson 
2490a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2491a266c7d5SChris Wilson 
2492a266c7d5SChris Wilson 	for (;;) {
24932c8ba29fSChris Wilson 		bool blc_event = false;
24942c8ba29fSChris Wilson 
2495a266c7d5SChris Wilson 		irq_received = iir != 0;
2496a266c7d5SChris Wilson 
2497a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2498a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2499a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2500a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2501a266c7d5SChris Wilson 		 */
2502a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2503a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2504a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2505a266c7d5SChris Wilson 
2506a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2507a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2508a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2509a266c7d5SChris Wilson 
2510a266c7d5SChris Wilson 			/*
2511a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2512a266c7d5SChris Wilson 			 */
2513a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2514a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2515a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2516a266c7d5SChris Wilson 							 pipe_name(pipe));
2517a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2518a266c7d5SChris Wilson 				irq_received = 1;
2519a266c7d5SChris Wilson 			}
2520a266c7d5SChris Wilson 		}
2521a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2522a266c7d5SChris Wilson 
2523a266c7d5SChris Wilson 		if (!irq_received)
2524a266c7d5SChris Wilson 			break;
2525a266c7d5SChris Wilson 
2526a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2527a266c7d5SChris Wilson 
2528a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2529adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2530a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2531a266c7d5SChris Wilson 
2532a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2533a266c7d5SChris Wilson 				  hotplug_status);
2534a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2535a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2536a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2537a266c7d5SChris Wilson 
2538a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2539a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2540a266c7d5SChris Wilson 		}
2541a266c7d5SChris Wilson 
2542a266c7d5SChris Wilson 		I915_WRITE(IIR, iir);
2543a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2544a266c7d5SChris Wilson 
2545a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2546a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2547a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2548a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2549a266c7d5SChris Wilson 
25504f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
2551a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 0);
2552a266c7d5SChris Wilson 
25534f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
2554a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 1);
2555a266c7d5SChris Wilson 
2556a266c7d5SChris Wilson 		for_each_pipe(pipe) {
25572c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
2558a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
2559a266c7d5SChris Wilson 				i915_pageflip_stall_check(dev, pipe);
2560a266c7d5SChris Wilson 				intel_finish_page_flip(dev, pipe);
2561a266c7d5SChris Wilson 			}
2562a266c7d5SChris Wilson 
2563a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2564a266c7d5SChris Wilson 				blc_event = true;
2565a266c7d5SChris Wilson 		}
2566a266c7d5SChris Wilson 
2567a266c7d5SChris Wilson 
2568a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2569a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2570a266c7d5SChris Wilson 
2571a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2572a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2573a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2574a266c7d5SChris Wilson 		 * we would never get another interrupt.
2575a266c7d5SChris Wilson 		 *
2576a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2577a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2578a266c7d5SChris Wilson 		 * another one.
2579a266c7d5SChris Wilson 		 *
2580a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2581a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2582a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2583a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2584a266c7d5SChris Wilson 		 * stray interrupts.
2585a266c7d5SChris Wilson 		 */
2586a266c7d5SChris Wilson 		iir = new_iir;
2587a266c7d5SChris Wilson 	}
2588a266c7d5SChris Wilson 
2589d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
25902c8ba29fSChris Wilson 
2591a266c7d5SChris Wilson 	return ret;
2592a266c7d5SChris Wilson }
2593a266c7d5SChris Wilson 
2594a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
2595a266c7d5SChris Wilson {
2596a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2597a266c7d5SChris Wilson 	int pipe;
2598a266c7d5SChris Wilson 
2599a266c7d5SChris Wilson 	if (!dev_priv)
2600a266c7d5SChris Wilson 		return;
2601a266c7d5SChris Wilson 
2602a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2603a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2604a266c7d5SChris Wilson 
2605a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
2606a266c7d5SChris Wilson 	for_each_pipe(pipe)
2607a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2608a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2609a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2610a266c7d5SChris Wilson 
2611a266c7d5SChris Wilson 	for_each_pipe(pipe)
2612a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
2613a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2614a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2615a266c7d5SChris Wilson }
2616a266c7d5SChris Wilson 
2617f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2618f71d4af4SJesse Barnes {
26198b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
26208b2e326dSChris Wilson 
26218b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
26228b2e326dSChris Wilson 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
26238b2e326dSChris Wilson 	INIT_WORK(&dev_priv->rps_work, gen6_pm_rps_work);
262498fd81cdSDaniel Vetter 	INIT_WORK(&dev_priv->parity_error_work, ivybridge_parity_work);
26258b2e326dSChris Wilson 
2626f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2627f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
26287d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
2629f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2630f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2631f71d4af4SJesse Barnes 	}
2632f71d4af4SJesse Barnes 
2633c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2634f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2635c3613de9SKeith Packard 	else
2636c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2637f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2638f71d4af4SJesse Barnes 
26397e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
26407e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
26417e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
26427e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
26437e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
26447e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
26457e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
26467e231dbeSJesse Barnes 	} else if (IS_IVYBRIDGE(dev)) {
2647f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2648f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2649f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2650f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2651f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2652f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2653f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
26547d4e146fSEugeni Dodonov 	} else if (IS_HASWELL(dev)) {
26557d4e146fSEugeni Dodonov 		/* Share interrupts handling with IVB */
26567d4e146fSEugeni Dodonov 		dev->driver->irq_handler = ivybridge_irq_handler;
26577d4e146fSEugeni Dodonov 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
26587d4e146fSEugeni Dodonov 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
26597d4e146fSEugeni Dodonov 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
26607d4e146fSEugeni Dodonov 		dev->driver->enable_vblank = ivybridge_enable_vblank;
26617d4e146fSEugeni Dodonov 		dev->driver->disable_vblank = ivybridge_disable_vblank;
2662f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2663f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2664f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2665f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2666f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2667f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2668f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
2669f71d4af4SJesse Barnes 	} else {
2670c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
2671c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
2672c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
2673c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
2674c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
2675a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
26764f7d1e79SChris Wilson 			/* IIR "flip pending" means done if this bit is set */
26774f7d1e79SChris Wilson 			I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
26784f7d1e79SChris Wilson 
2679a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
2680a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
2681a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
2682a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
2683c2798b19SChris Wilson 		} else {
2684a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
2685a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
2686a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
2687a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
2688c2798b19SChris Wilson 		}
2689f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
2690f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
2691f71d4af4SJesse Barnes 	}
2692f71d4af4SJesse Barnes }
2693