xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 31acc7f59aac34477423a7dde654ae998b48d666)
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 	bool blc_event;
5177e231dbeSJesse Barnes 
5187e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5197e231dbeSJesse Barnes 
5207e231dbeSJesse Barnes 	while (true) {
5217e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
5227e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
5237e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
5247e231dbeSJesse Barnes 
5257e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
5267e231dbeSJesse Barnes 			goto out;
5277e231dbeSJesse Barnes 
5287e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
5297e231dbeSJesse Barnes 
530e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
5317e231dbeSJesse Barnes 
5327e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5337e231dbeSJesse Barnes 		for_each_pipe(pipe) {
5347e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
5357e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
5367e231dbeSJesse Barnes 
5377e231dbeSJesse Barnes 			/*
5387e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
5397e231dbeSJesse Barnes 			 */
5407e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
5417e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
5427e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
5437e231dbeSJesse Barnes 							 pipe_name(pipe));
5447e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
5457e231dbeSJesse Barnes 			}
5467e231dbeSJesse Barnes 		}
5477e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5487e231dbeSJesse Barnes 
549*31acc7f5SJesse Barnes 		for_each_pipe(pipe) {
550*31acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
551*31acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
552*31acc7f5SJesse Barnes 
553*31acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
554*31acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
555*31acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
556*31acc7f5SJesse Barnes 			}
557*31acc7f5SJesse Barnes 		}
558*31acc7f5SJesse Barnes 
5597e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
5607e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
5617e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
5627e231dbeSJesse Barnes 
5637e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
5647e231dbeSJesse Barnes 					 hotplug_status);
5657e231dbeSJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
5667e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
5677e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
5687e231dbeSJesse Barnes 
5697e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
5707e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
5717e231dbeSJesse Barnes 		}
5727e231dbeSJesse Barnes 
5737e231dbeSJesse Barnes 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
5747e231dbeSJesse Barnes 			blc_event = true;
5757e231dbeSJesse Barnes 
576fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
577fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
5787e231dbeSJesse Barnes 
5797e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
5807e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
5817e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
5827e231dbeSJesse Barnes 	}
5837e231dbeSJesse Barnes 
5847e231dbeSJesse Barnes out:
5857e231dbeSJesse Barnes 	return ret;
5867e231dbeSJesse Barnes }
5877e231dbeSJesse Barnes 
5889adab8b5SChris Wilson static void pch_irq_handler(struct drm_device *dev, u32 pch_iir)
589776ad806SJesse Barnes {
590776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5919db4a9c7SJesse Barnes 	int pipe;
592776ad806SJesse Barnes 
593776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
594776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
595776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
596776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
597776ad806SJesse Barnes 
598776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
599776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
600776ad806SJesse Barnes 
601776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
602776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
603776ad806SJesse Barnes 
604776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
605776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
606776ad806SJesse Barnes 
607776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
608776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
609776ad806SJesse Barnes 
6109db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
6119db4a9c7SJesse Barnes 		for_each_pipe(pipe)
6129db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
6139db4a9c7SJesse Barnes 					 pipe_name(pipe),
6149db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
615776ad806SJesse Barnes 
616776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
617776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
618776ad806SJesse Barnes 
619776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
620776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
621776ad806SJesse Barnes 
622776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
623776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
624776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
625776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
626776ad806SJesse Barnes }
627776ad806SJesse Barnes 
628f71d4af4SJesse Barnes static irqreturn_t ivybridge_irq_handler(DRM_IRQ_ARGS)
629b1f14ad0SJesse Barnes {
630b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
631b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6320e43406bSChris Wilson 	u32 de_iir, gt_iir, de_ier, pm_iir;
6330e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
6340e43406bSChris Wilson 	int i;
635b1f14ad0SJesse Barnes 
636b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
637b1f14ad0SJesse Barnes 
638b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
639b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
640b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
6410e43406bSChris Wilson 
6420e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
6430e43406bSChris Wilson 	if (gt_iir) {
6440e43406bSChris Wilson 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
6450e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
6460e43406bSChris Wilson 		ret = IRQ_HANDLED;
6470e43406bSChris Wilson 	}
648b1f14ad0SJesse Barnes 
649b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
6500e43406bSChris Wilson 	if (de_iir) {
651b1f14ad0SJesse Barnes 		if (de_iir & DE_GSE_IVB)
652b1f14ad0SJesse Barnes 			intel_opregion_gse_intr(dev);
653b1f14ad0SJesse Barnes 
6540e43406bSChris Wilson 		for (i = 0; i < 3; i++) {
6550e43406bSChris Wilson 			if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
6560e43406bSChris Wilson 				intel_prepare_page_flip(dev, i);
6570e43406bSChris Wilson 				intel_finish_page_flip_plane(dev, i);
658b1f14ad0SJesse Barnes 			}
6590e43406bSChris Wilson 			if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
6600e43406bSChris Wilson 				drm_handle_vblank(dev, i);
661b1f14ad0SJesse Barnes 		}
662b1f14ad0SJesse Barnes 
663b1f14ad0SJesse Barnes 		/* check event from PCH */
664b1f14ad0SJesse Barnes 		if (de_iir & DE_PCH_EVENT_IVB) {
6650e43406bSChris Wilson 			u32 pch_iir = I915_READ(SDEIIR);
6660e43406bSChris Wilson 
667b1f14ad0SJesse Barnes 			if (pch_iir & SDE_HOTPLUG_MASK_CPT)
668b1f14ad0SJesse Barnes 				queue_work(dev_priv->wq, &dev_priv->hotplug_work);
6699adab8b5SChris Wilson 			pch_irq_handler(dev, pch_iir);
6700e43406bSChris Wilson 
6710e43406bSChris Wilson 			/* clear PCH hotplug event before clear CPU irq */
6720e43406bSChris Wilson 			I915_WRITE(SDEIIR, pch_iir);
673b1f14ad0SJesse Barnes 		}
674b1f14ad0SJesse Barnes 
6750e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
6760e43406bSChris Wilson 		ret = IRQ_HANDLED;
6770e43406bSChris Wilson 	}
6780e43406bSChris Wilson 
6790e43406bSChris Wilson 	pm_iir = I915_READ(GEN6_PMIIR);
6800e43406bSChris Wilson 	if (pm_iir) {
681fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
682fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
683b1f14ad0SJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
6840e43406bSChris Wilson 		ret = IRQ_HANDLED;
6850e43406bSChris Wilson 	}
686b1f14ad0SJesse Barnes 
687b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
688b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
689b1f14ad0SJesse Barnes 
690b1f14ad0SJesse Barnes 	return ret;
691b1f14ad0SJesse Barnes }
692b1f14ad0SJesse Barnes 
693e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
694e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
695e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
696e7b4c6b1SDaniel Vetter {
697e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
698e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
699e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
700e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
701e7b4c6b1SDaniel Vetter }
702e7b4c6b1SDaniel Vetter 
703f71d4af4SJesse Barnes static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
704036a4a7dSZhenyu Wang {
7054697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
706036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
707036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
7083b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
7092d7b8366SYuanhan Liu 	u32 hotplug_mask;
710881f47b6SXiang, Haihao 
7114697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
7124697995bSJesse Barnes 
7132d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
7142d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
7152d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
7163143a2bfSChris Wilson 	POSTING_READ(DEIER);
7172d109a84SZou, Nanhai 
718036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
719036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
720c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
7213b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
722036a4a7dSZhenyu Wang 
7233b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
7243b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
725c7c85101SZou Nan hai 		goto done;
726036a4a7dSZhenyu Wang 
7272d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
7282d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
7292d7b8366SYuanhan Liu 	else
7302d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
7312d7b8366SYuanhan Liu 
732036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
733036a4a7dSZhenyu Wang 
734e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
735e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
736e7b4c6b1SDaniel Vetter 	else
737e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
738036a4a7dSZhenyu Wang 
73901c66889SZhao Yakui 	if (de_iir & DE_GSE)
7403b617967SChris Wilson 		intel_opregion_gse_intr(dev);
74101c66889SZhao Yakui 
742f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
743013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
7442bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
745013d5aa2SJesse Barnes 	}
746013d5aa2SJesse Barnes 
747f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
748f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
7492bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
750013d5aa2SJesse Barnes 	}
751c062df61SLi Peng 
752f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
753f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
754f072d2e7SZhenyu Wang 
755f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
756f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
757f072d2e7SZhenyu Wang 
758c650156aSZhenyu Wang 	/* check event from PCH */
759776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
760776ad806SJesse Barnes 		if (pch_iir & hotplug_mask)
761c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
7629adab8b5SChris Wilson 		pch_irq_handler(dev, pch_iir);
763776ad806SJesse Barnes 	}
764c650156aSZhenyu Wang 
765f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
7667648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
767f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
768f97108d1SJesse Barnes 	}
769f97108d1SJesse Barnes 
770fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
771fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
7723b8d8d91SJesse Barnes 
773c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
774c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
775c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
776c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
7774912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
778036a4a7dSZhenyu Wang 
779c7c85101SZou Nan hai done:
7802d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
7813143a2bfSChris Wilson 	POSTING_READ(DEIER);
7822d109a84SZou, Nanhai 
783036a4a7dSZhenyu Wang 	return ret;
784036a4a7dSZhenyu Wang }
785036a4a7dSZhenyu Wang 
7868a905236SJesse Barnes /**
7878a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
7888a905236SJesse Barnes  * @work: work struct
7898a905236SJesse Barnes  *
7908a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
7918a905236SJesse Barnes  * was detected.
7928a905236SJesse Barnes  */
7938a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
7948a905236SJesse Barnes {
7958a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
7968a905236SJesse Barnes 						    error_work);
7978a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
798f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
799f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
800f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
8018a905236SJesse Barnes 
802f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
8038a905236SJesse Barnes 
804ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
80544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
806f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
807d4b8bb2aSDaniel Vetter 		if (!i915_reset(dev)) {
808ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
809f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
810f316a42cSBen Gamari 		}
81130dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
812f316a42cSBen Gamari 	}
8138a905236SJesse Barnes }
8148a905236SJesse Barnes 
8153bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
8169df30794SChris Wilson static struct drm_i915_error_object *
817bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
81805394f39SChris Wilson 			 struct drm_i915_gem_object *src)
8199df30794SChris Wilson {
8209df30794SChris Wilson 	struct drm_i915_error_object *dst;
8219df30794SChris Wilson 	int page, page_count;
822e56660ddSChris Wilson 	u32 reloc_offset;
8239df30794SChris Wilson 
82405394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
8259df30794SChris Wilson 		return NULL;
8269df30794SChris Wilson 
82705394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
8289df30794SChris Wilson 
8299df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof(u32 *), GFP_ATOMIC);
8309df30794SChris Wilson 	if (dst == NULL)
8319df30794SChris Wilson 		return NULL;
8329df30794SChris Wilson 
83305394f39SChris Wilson 	reloc_offset = src->gtt_offset;
8349df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
835788885aeSAndrew Morton 		unsigned long flags;
836e56660ddSChris Wilson 		void *d;
837788885aeSAndrew Morton 
838e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
8399df30794SChris Wilson 		if (d == NULL)
8409df30794SChris Wilson 			goto unwind;
841e56660ddSChris Wilson 
842788885aeSAndrew Morton 		local_irq_save(flags);
84374898d7eSDaniel Vetter 		if (reloc_offset < dev_priv->mm.gtt_mappable_end &&
84474898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
845172975aaSChris Wilson 			void __iomem *s;
846172975aaSChris Wilson 
847172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
848172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
849172975aaSChris Wilson 			 * captures what the GPU read.
850172975aaSChris Wilson 			 */
851172975aaSChris Wilson 
852e56660ddSChris Wilson 			s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
8533e4d3af5SPeter Zijlstra 						     reloc_offset);
854e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
8553e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
856172975aaSChris Wilson 		} else {
857172975aaSChris Wilson 			void *s;
858172975aaSChris Wilson 
859172975aaSChris Wilson 			drm_clflush_pages(&src->pages[page], 1);
860172975aaSChris Wilson 
861172975aaSChris Wilson 			s = kmap_atomic(src->pages[page]);
862172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
863172975aaSChris Wilson 			kunmap_atomic(s);
864172975aaSChris Wilson 
865172975aaSChris Wilson 			drm_clflush_pages(&src->pages[page], 1);
866172975aaSChris Wilson 		}
867788885aeSAndrew Morton 		local_irq_restore(flags);
868e56660ddSChris Wilson 
8699df30794SChris Wilson 		dst->pages[page] = d;
870e56660ddSChris Wilson 
871e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
8729df30794SChris Wilson 	}
8739df30794SChris Wilson 	dst->page_count = page_count;
87405394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
8759df30794SChris Wilson 
8769df30794SChris Wilson 	return dst;
8779df30794SChris Wilson 
8789df30794SChris Wilson unwind:
8799df30794SChris Wilson 	while (page--)
8809df30794SChris Wilson 		kfree(dst->pages[page]);
8819df30794SChris Wilson 	kfree(dst);
8829df30794SChris Wilson 	return NULL;
8839df30794SChris Wilson }
8849df30794SChris Wilson 
8859df30794SChris Wilson static void
8869df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
8879df30794SChris Wilson {
8889df30794SChris Wilson 	int page;
8899df30794SChris Wilson 
8909df30794SChris Wilson 	if (obj == NULL)
8919df30794SChris Wilson 		return;
8929df30794SChris Wilson 
8939df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
8949df30794SChris Wilson 		kfree(obj->pages[page]);
8959df30794SChris Wilson 
8969df30794SChris Wilson 	kfree(obj);
8979df30794SChris Wilson }
8989df30794SChris Wilson 
899742cbee8SDaniel Vetter void
900742cbee8SDaniel Vetter i915_error_state_free(struct kref *error_ref)
9019df30794SChris Wilson {
902742cbee8SDaniel Vetter 	struct drm_i915_error_state *error = container_of(error_ref,
903742cbee8SDaniel Vetter 							  typeof(*error), ref);
904e2f973d5SChris Wilson 	int i;
905e2f973d5SChris Wilson 
90652d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
90752d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
90852d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
90952d39a21SChris Wilson 		kfree(error->ring[i].requests);
91052d39a21SChris Wilson 	}
911e2f973d5SChris Wilson 
9129df30794SChris Wilson 	kfree(error->active_bo);
9136ef3d427SChris Wilson 	kfree(error->overlay);
9149df30794SChris Wilson 	kfree(error);
9159df30794SChris Wilson }
9161b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
9171b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
918c724e8a9SChris Wilson {
919c724e8a9SChris Wilson 	err->size = obj->base.size;
920c724e8a9SChris Wilson 	err->name = obj->base.name;
921c724e8a9SChris Wilson 	err->seqno = obj->last_rendering_seqno;
922c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
923c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
924c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
925c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
926c724e8a9SChris Wilson 	err->pinned = 0;
927c724e8a9SChris Wilson 	if (obj->pin_count > 0)
928c724e8a9SChris Wilson 		err->pinned = 1;
929c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
930c724e8a9SChris Wilson 		err->pinned = -1;
931c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
932c724e8a9SChris Wilson 	err->dirty = obj->dirty;
933c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
93496154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
93593dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
9361b50247aSChris Wilson }
937c724e8a9SChris Wilson 
9381b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
9391b50247aSChris Wilson 			     int count, struct list_head *head)
9401b50247aSChris Wilson {
9411b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
9421b50247aSChris Wilson 	int i = 0;
9431b50247aSChris Wilson 
9441b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
9451b50247aSChris Wilson 		capture_bo(err++, obj);
946c724e8a9SChris Wilson 		if (++i == count)
947c724e8a9SChris Wilson 			break;
9481b50247aSChris Wilson 	}
949c724e8a9SChris Wilson 
9501b50247aSChris Wilson 	return i;
9511b50247aSChris Wilson }
9521b50247aSChris Wilson 
9531b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
9541b50247aSChris Wilson 			     int count, struct list_head *head)
9551b50247aSChris Wilson {
9561b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
9571b50247aSChris Wilson 	int i = 0;
9581b50247aSChris Wilson 
9591b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
9601b50247aSChris Wilson 		if (obj->pin_count == 0)
9611b50247aSChris Wilson 			continue;
9621b50247aSChris Wilson 
9631b50247aSChris Wilson 		capture_bo(err++, obj);
9641b50247aSChris Wilson 		if (++i == count)
9651b50247aSChris Wilson 			break;
966c724e8a9SChris Wilson 	}
967c724e8a9SChris Wilson 
968c724e8a9SChris Wilson 	return i;
969c724e8a9SChris Wilson }
970c724e8a9SChris Wilson 
971748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
972748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
973748ebc60SChris Wilson {
974748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
975748ebc60SChris Wilson 	int i;
976748ebc60SChris Wilson 
977748ebc60SChris Wilson 	/* Fences */
978748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
979775d17b6SDaniel Vetter 	case 7:
980748ebc60SChris Wilson 	case 6:
981748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
982748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
983748ebc60SChris Wilson 		break;
984748ebc60SChris Wilson 	case 5:
985748ebc60SChris Wilson 	case 4:
986748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
987748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
988748ebc60SChris Wilson 		break;
989748ebc60SChris Wilson 	case 3:
990748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
991748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
992748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
993748ebc60SChris Wilson 	case 2:
994748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
995748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
996748ebc60SChris Wilson 		break;
997748ebc60SChris Wilson 
998748ebc60SChris Wilson 	}
999748ebc60SChris Wilson }
1000748ebc60SChris Wilson 
1001bcfb2e28SChris Wilson static struct drm_i915_error_object *
1002bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1003bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
1004bcfb2e28SChris Wilson {
1005bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1006bcfb2e28SChris Wilson 	u32 seqno;
1007bcfb2e28SChris Wilson 
1008bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1009bcfb2e28SChris Wilson 		return NULL;
1010bcfb2e28SChris Wilson 
1011bcfb2e28SChris Wilson 	seqno = ring->get_seqno(ring);
1012bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1013bcfb2e28SChris Wilson 		if (obj->ring != ring)
1014bcfb2e28SChris Wilson 			continue;
1015bcfb2e28SChris Wilson 
1016c37d9a5dSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
1017bcfb2e28SChris Wilson 			continue;
1018bcfb2e28SChris Wilson 
1019bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1020bcfb2e28SChris Wilson 			continue;
1021bcfb2e28SChris Wilson 
1022bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1023bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1024bcfb2e28SChris Wilson 		 */
1025bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1026bcfb2e28SChris Wilson 	}
1027bcfb2e28SChris Wilson 
1028bcfb2e28SChris Wilson 	return NULL;
1029bcfb2e28SChris Wilson }
1030bcfb2e28SChris Wilson 
1031d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1032d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1033d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1034d27b1e0eSDaniel Vetter {
1035d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1036d27b1e0eSDaniel Vetter 
103733f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
103833f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
10397e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
10407e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
10417e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
10427e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
104333f3f518SDaniel Vetter 	}
1044c1cd90edSDaniel Vetter 
1045d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
10469d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1047d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1048d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1049d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1050c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1051d27b1e0eSDaniel Vetter 		if (ring->id == RCS) {
1052d27b1e0eSDaniel Vetter 			error->instdone1 = I915_READ(INSTDONE1);
1053d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1054d27b1e0eSDaniel Vetter 		}
1055d27b1e0eSDaniel Vetter 	} else {
10569d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1057d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1058d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1059d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1060d27b1e0eSDaniel Vetter 	}
1061d27b1e0eSDaniel Vetter 
10629574b3feSBen Widawsky 	error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
1063c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1064d27b1e0eSDaniel Vetter 	error->seqno[ring->id] = ring->get_seqno(ring);
1065d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1066c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1067c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
10687e3b8737SDaniel Vetter 
10697e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
10707e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1071d27b1e0eSDaniel Vetter }
1072d27b1e0eSDaniel Vetter 
107352d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
107452d39a21SChris Wilson 				  struct drm_i915_error_state *error)
107552d39a21SChris Wilson {
107652d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1077b4519513SChris Wilson 	struct intel_ring_buffer *ring;
107852d39a21SChris Wilson 	struct drm_i915_gem_request *request;
107952d39a21SChris Wilson 	int i, count;
108052d39a21SChris Wilson 
1081b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
108252d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
108352d39a21SChris Wilson 
108452d39a21SChris Wilson 		error->ring[i].batchbuffer =
108552d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
108652d39a21SChris Wilson 
108752d39a21SChris Wilson 		error->ring[i].ringbuffer =
108852d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
108952d39a21SChris Wilson 
109052d39a21SChris Wilson 		count = 0;
109152d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
109252d39a21SChris Wilson 			count++;
109352d39a21SChris Wilson 
109452d39a21SChris Wilson 		error->ring[i].num_requests = count;
109552d39a21SChris Wilson 		error->ring[i].requests =
109652d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
109752d39a21SChris Wilson 				GFP_ATOMIC);
109852d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
109952d39a21SChris Wilson 			error->ring[i].num_requests = 0;
110052d39a21SChris Wilson 			continue;
110152d39a21SChris Wilson 		}
110252d39a21SChris Wilson 
110352d39a21SChris Wilson 		count = 0;
110452d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
110552d39a21SChris Wilson 			struct drm_i915_error_request *erq;
110652d39a21SChris Wilson 
110752d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
110852d39a21SChris Wilson 			erq->seqno = request->seqno;
110952d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1110ee4f42b1SChris Wilson 			erq->tail = request->tail;
111152d39a21SChris Wilson 		}
111252d39a21SChris Wilson 	}
111352d39a21SChris Wilson }
111452d39a21SChris Wilson 
11158a905236SJesse Barnes /**
11168a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
11178a905236SJesse Barnes  * @dev: drm device
11188a905236SJesse Barnes  *
11198a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
11208a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
11218a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
11228a905236SJesse Barnes  * to pick up.
11238a905236SJesse Barnes  */
112463eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
112563eeaf38SJesse Barnes {
112663eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
112705394f39SChris Wilson 	struct drm_i915_gem_object *obj;
112863eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
112963eeaf38SJesse Barnes 	unsigned long flags;
11309db4a9c7SJesse Barnes 	int i, pipe;
113163eeaf38SJesse Barnes 
113263eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
11339df30794SChris Wilson 	error = dev_priv->first_error;
11349df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
11359df30794SChris Wilson 	if (error)
11369df30794SChris Wilson 		return;
113763eeaf38SJesse Barnes 
11389db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
113933f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
114063eeaf38SJesse Barnes 	if (!error) {
11419df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
11429df30794SChris Wilson 		return;
114363eeaf38SJesse Barnes 	}
114463eeaf38SJesse Barnes 
1145b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
1146b6f7833bSChris Wilson 		 dev->primary->index);
11472fa772f3SChris Wilson 
1148742cbee8SDaniel Vetter 	kref_init(&error->ref);
114963eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
115063eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
1151b9a3906bSBen Widawsky 	error->ccid = I915_READ(CCID);
1152be998e2eSBen Widawsky 
1153be998e2eSBen Widawsky 	if (HAS_PCH_SPLIT(dev))
1154be998e2eSBen Widawsky 		error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1155be998e2eSBen Widawsky 	else if (IS_VALLEYVIEW(dev))
1156be998e2eSBen Widawsky 		error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1157be998e2eSBen Widawsky 	else if (IS_GEN2(dev))
1158be998e2eSBen Widawsky 		error->ier = I915_READ16(IER);
1159be998e2eSBen Widawsky 	else
1160be998e2eSBen Widawsky 		error->ier = I915_READ(IER);
1161be998e2eSBen Widawsky 
11629db4a9c7SJesse Barnes 	for_each_pipe(pipe)
11639db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1164d27b1e0eSDaniel Vetter 
116533f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1166f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
116733f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
116833f3f518SDaniel Vetter 	}
1169add354ddSChris Wilson 
1170748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
117152d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
11729df30794SChris Wilson 
1173c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
11749df30794SChris Wilson 	error->active_bo = NULL;
1175c724e8a9SChris Wilson 	error->pinned_bo = NULL;
11769df30794SChris Wilson 
1177bcfb2e28SChris Wilson 	i = 0;
1178bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1179bcfb2e28SChris Wilson 		i++;
1180bcfb2e28SChris Wilson 	error->active_bo_count = i;
11811b50247aSChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list)
11821b50247aSChris Wilson 		if (obj->pin_count)
1183bcfb2e28SChris Wilson 			i++;
1184bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1185c724e8a9SChris Wilson 
11868e934dbfSChris Wilson 	error->active_bo = NULL;
11878e934dbfSChris Wilson 	error->pinned_bo = NULL;
1188bcfb2e28SChris Wilson 	if (i) {
1189bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
11909df30794SChris Wilson 					   GFP_ATOMIC);
1191c724e8a9SChris Wilson 		if (error->active_bo)
1192c724e8a9SChris Wilson 			error->pinned_bo =
1193c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
11949df30794SChris Wilson 	}
1195c724e8a9SChris Wilson 
1196c724e8a9SChris Wilson 	if (error->active_bo)
1197c724e8a9SChris Wilson 		error->active_bo_count =
11981b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1199c724e8a9SChris Wilson 					  error->active_bo_count,
1200c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1201c724e8a9SChris Wilson 
1202c724e8a9SChris Wilson 	if (error->pinned_bo)
1203c724e8a9SChris Wilson 		error->pinned_bo_count =
12041b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1205c724e8a9SChris Wilson 					  error->pinned_bo_count,
12061b50247aSChris Wilson 					  &dev_priv->mm.gtt_list);
120763eeaf38SJesse Barnes 
12088a905236SJesse Barnes 	do_gettimeofday(&error->time);
12098a905236SJesse Barnes 
12106ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1211c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
12126ef3d427SChris Wilson 
12139df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12149df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
121563eeaf38SJesse Barnes 		dev_priv->first_error = error;
12169df30794SChris Wilson 		error = NULL;
12179df30794SChris Wilson 	}
121863eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
12199df30794SChris Wilson 
12209df30794SChris Wilson 	if (error)
1221742cbee8SDaniel Vetter 		i915_error_state_free(&error->ref);
12229df30794SChris Wilson }
12239df30794SChris Wilson 
12249df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
12259df30794SChris Wilson {
12269df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
12279df30794SChris Wilson 	struct drm_i915_error_state *error;
12286dc0e816SBen Widawsky 	unsigned long flags;
12299df30794SChris Wilson 
12306dc0e816SBen Widawsky 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12319df30794SChris Wilson 	error = dev_priv->first_error;
12329df30794SChris Wilson 	dev_priv->first_error = NULL;
12336dc0e816SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
12349df30794SChris Wilson 
12359df30794SChris Wilson 	if (error)
1236742cbee8SDaniel Vetter 		kref_put(&error->ref, i915_error_state_free);
123763eeaf38SJesse Barnes }
12383bd3c932SChris Wilson #else
12393bd3c932SChris Wilson #define i915_capture_error_state(x)
12403bd3c932SChris Wilson #endif
124163eeaf38SJesse Barnes 
124235aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1243c0e09200SDave Airlie {
12448a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
124563eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
12469db4a9c7SJesse Barnes 	int pipe;
124763eeaf38SJesse Barnes 
124835aed2e6SChris Wilson 	if (!eir)
124935aed2e6SChris Wilson 		return;
125063eeaf38SJesse Barnes 
1251a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
12528a905236SJesse Barnes 
12538a905236SJesse Barnes 	if (IS_G4X(dev)) {
12548a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
12558a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
12568a905236SJesse Barnes 
1257a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1258a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1259a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n",
12608a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
1261a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1262a70491ccSJoe Perches 			pr_err("  INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1263a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
12648a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
12653143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
12668a905236SJesse Barnes 		}
12678a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
12688a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1269a70491ccSJoe Perches 			pr_err("page table error\n");
1270a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
12718a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
12723143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
12738a905236SJesse Barnes 		}
12748a905236SJesse Barnes 	}
12758a905236SJesse Barnes 
1276a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
127763eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
127863eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1279a70491ccSJoe Perches 			pr_err("page table error\n");
1280a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
128163eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
12823143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
128363eeaf38SJesse Barnes 		}
12848a905236SJesse Barnes 	}
12858a905236SJesse Barnes 
128663eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1287a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
12889db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1289a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
12909db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
129163eeaf38SJesse Barnes 		/* pipestat has already been acked */
129263eeaf38SJesse Barnes 	}
129363eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1294a70491ccSJoe Perches 		pr_err("instruction error\n");
1295a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1296a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
129763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
129863eeaf38SJesse Barnes 
1299a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1300a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1301a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n", I915_READ(INSTDONE));
1302a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
130363eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
13043143a2bfSChris Wilson 			POSTING_READ(IPEIR);
130563eeaf38SJesse Barnes 		} else {
130663eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
130763eeaf38SJesse Barnes 
1308a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1309a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1310a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n",
131163eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
1312a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1313a70491ccSJoe Perches 			pr_err("  INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1314a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
131563eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
13163143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
131763eeaf38SJesse Barnes 		}
131863eeaf38SJesse Barnes 	}
131963eeaf38SJesse Barnes 
132063eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
13213143a2bfSChris Wilson 	POSTING_READ(EIR);
132263eeaf38SJesse Barnes 	eir = I915_READ(EIR);
132363eeaf38SJesse Barnes 	if (eir) {
132463eeaf38SJesse Barnes 		/*
132563eeaf38SJesse Barnes 		 * some errors might have become stuck,
132663eeaf38SJesse Barnes 		 * mask them.
132763eeaf38SJesse Barnes 		 */
132863eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
132963eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
133063eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
133163eeaf38SJesse Barnes 	}
133235aed2e6SChris Wilson }
133335aed2e6SChris Wilson 
133435aed2e6SChris Wilson /**
133535aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
133635aed2e6SChris Wilson  * @dev: drm device
133735aed2e6SChris Wilson  *
133835aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
133935aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
134035aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
134135aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
134235aed2e6SChris Wilson  * of a ring dump etc.).
134335aed2e6SChris Wilson  */
1344527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
134535aed2e6SChris Wilson {
134635aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
1347b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1348b4519513SChris Wilson 	int i;
134935aed2e6SChris Wilson 
135035aed2e6SChris Wilson 	i915_capture_error_state(dev);
135135aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
13528a905236SJesse Barnes 
1353ba1234d1SBen Gamari 	if (wedged) {
135430dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1355ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1356ba1234d1SBen Gamari 
135711ed50ecSBen Gamari 		/*
135811ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
135911ed50ecSBen Gamari 		 */
1360b4519513SChris Wilson 		for_each_ring(ring, dev_priv, i)
1361b4519513SChris Wilson 			wake_up_all(&ring->irq_queue);
136211ed50ecSBen Gamari 	}
136311ed50ecSBen Gamari 
13649c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
13658a905236SJesse Barnes }
13668a905236SJesse Barnes 
13674e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
13684e5359cdSSimon Farnsworth {
13694e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
13704e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13714e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
137205394f39SChris Wilson 	struct drm_i915_gem_object *obj;
13734e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
13744e5359cdSSimon Farnsworth 	unsigned long flags;
13754e5359cdSSimon Farnsworth 	bool stall_detected;
13764e5359cdSSimon Farnsworth 
13774e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
13784e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
13794e5359cdSSimon Farnsworth 		return;
13804e5359cdSSimon Farnsworth 
13814e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
13824e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
13834e5359cdSSimon Farnsworth 
13844e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
13854e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
13864e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
13874e5359cdSSimon Farnsworth 		return;
13884e5359cdSSimon Farnsworth 	}
13894e5359cdSSimon Farnsworth 
13904e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
139105394f39SChris Wilson 	obj = work->pending_flip_obj;
1392a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
13939db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1394446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1395446f2545SArmin Reese 					obj->gtt_offset;
13964e5359cdSSimon Farnsworth 	} else {
13979db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
139805394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
139901f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
14004e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
14014e5359cdSSimon Farnsworth 	}
14024e5359cdSSimon Farnsworth 
14034e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
14044e5359cdSSimon Farnsworth 
14054e5359cdSSimon Farnsworth 	if (stall_detected) {
14064e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
14074e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
14084e5359cdSSimon Farnsworth 	}
14094e5359cdSSimon Farnsworth }
14104e5359cdSSimon Farnsworth 
141142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
141242f52ef8SKeith Packard  * we use as a pipe index
141342f52ef8SKeith Packard  */
1414f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
14150a3e67a4SJesse Barnes {
14160a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1417e9d21d7fSKeith Packard 	unsigned long irqflags;
141871e0ffa5SJesse Barnes 
14195eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
142071e0ffa5SJesse Barnes 		return -EINVAL;
14210a3e67a4SJesse Barnes 
14221ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1423f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
14247c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14257c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
14260a3e67a4SJesse Barnes 	else
14277c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
14287c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
14298692d00eSChris Wilson 
14308692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
14318692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
14326b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
14331ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14348692d00eSChris Wilson 
14350a3e67a4SJesse Barnes 	return 0;
14360a3e67a4SJesse Barnes }
14370a3e67a4SJesse Barnes 
1438f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1439f796cf8fSJesse Barnes {
1440f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1441f796cf8fSJesse Barnes 	unsigned long irqflags;
1442f796cf8fSJesse Barnes 
1443f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1444f796cf8fSJesse Barnes 		return -EINVAL;
1445f796cf8fSJesse Barnes 
1446f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1447f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1448f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1449f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1450f796cf8fSJesse Barnes 
1451f796cf8fSJesse Barnes 	return 0;
1452f796cf8fSJesse Barnes }
1453f796cf8fSJesse Barnes 
1454f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1455b1f14ad0SJesse Barnes {
1456b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1457b1f14ad0SJesse Barnes 	unsigned long irqflags;
1458b1f14ad0SJesse Barnes 
1459b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1460b1f14ad0SJesse Barnes 		return -EINVAL;
1461b1f14ad0SJesse Barnes 
1462b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1463b615b57aSChris Wilson 	ironlake_enable_display_irq(dev_priv,
1464b615b57aSChris Wilson 				    DE_PIPEA_VBLANK_IVB << (5 * pipe));
1465b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1466b1f14ad0SJesse Barnes 
1467b1f14ad0SJesse Barnes 	return 0;
1468b1f14ad0SJesse Barnes }
1469b1f14ad0SJesse Barnes 
14707e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
14717e231dbeSJesse Barnes {
14727e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
14737e231dbeSJesse Barnes 	unsigned long irqflags;
1474*31acc7f5SJesse Barnes 	u32 imr;
14757e231dbeSJesse Barnes 
14767e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
14777e231dbeSJesse Barnes 		return -EINVAL;
14787e231dbeSJesse Barnes 
14797e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
14807e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
1481*31acc7f5SJesse Barnes 	if (pipe == 0)
14827e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
1483*31acc7f5SJesse Barnes 	else
14847e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
14857e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
1486*31acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
1487*31acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
14887e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14897e231dbeSJesse Barnes 
14907e231dbeSJesse Barnes 	return 0;
14917e231dbeSJesse Barnes }
14927e231dbeSJesse Barnes 
149342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
149442f52ef8SKeith Packard  * we use as a pipe index
149542f52ef8SKeith Packard  */
1496f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
14970a3e67a4SJesse Barnes {
14980a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1499e9d21d7fSKeith Packard 	unsigned long irqflags;
15000a3e67a4SJesse Barnes 
15011ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15028692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15036b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
15048692d00eSChris Wilson 
15057c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
15067c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
15077c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
15081ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15090a3e67a4SJesse Barnes }
15100a3e67a4SJesse Barnes 
1511f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1512f796cf8fSJesse Barnes {
1513f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1514f796cf8fSJesse Barnes 	unsigned long irqflags;
1515f796cf8fSJesse Barnes 
1516f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1517f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1518f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1519f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1520f796cf8fSJesse Barnes }
1521f796cf8fSJesse Barnes 
1522f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1523b1f14ad0SJesse Barnes {
1524b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1525b1f14ad0SJesse Barnes 	unsigned long irqflags;
1526b1f14ad0SJesse Barnes 
1527b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1528b615b57aSChris Wilson 	ironlake_disable_display_irq(dev_priv,
1529b615b57aSChris Wilson 				     DE_PIPEA_VBLANK_IVB << (pipe * 5));
1530b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1531b1f14ad0SJesse Barnes }
1532b1f14ad0SJesse Barnes 
15337e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
15347e231dbeSJesse Barnes {
15357e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
15367e231dbeSJesse Barnes 	unsigned long irqflags;
1537*31acc7f5SJesse Barnes 	u32 imr;
15387e231dbeSJesse Barnes 
15397e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1540*31acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
1541*31acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
15427e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
1543*31acc7f5SJesse Barnes 	if (pipe == 0)
15447e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
1545*31acc7f5SJesse Barnes 	else
15467e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
15477e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
15487e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15497e231dbeSJesse Barnes }
15507e231dbeSJesse Barnes 
1551893eead0SChris Wilson static u32
1552893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1553852835f3SZou Nan hai {
1554893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1555893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1556893eead0SChris Wilson }
1557893eead0SChris Wilson 
1558893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1559893eead0SChris Wilson {
1560893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1561893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1562893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
15639574b3feSBen Widawsky 		if (waitqueue_active(&ring->irq_queue)) {
15649574b3feSBen Widawsky 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
15659574b3feSBen Widawsky 				  ring->name);
1566893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1567893eead0SChris Wilson 			*err = true;
1568893eead0SChris Wilson 		}
1569893eead0SChris Wilson 		return true;
1570893eead0SChris Wilson 	}
1571893eead0SChris Wilson 	return false;
1572f65d9421SBen Gamari }
1573f65d9421SBen Gamari 
15741ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
15751ec14ad3SChris Wilson {
15761ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
15771ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
15781ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
15791ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
15801ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
15811ec14ad3SChris Wilson 			  ring->name);
15821ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
15831ec14ad3SChris Wilson 		return true;
15841ec14ad3SChris Wilson 	}
15851ec14ad3SChris Wilson 	return false;
15861ec14ad3SChris Wilson }
15871ec14ad3SChris Wilson 
1588d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
1589d1e61e7fSChris Wilson {
1590d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1591d1e61e7fSChris Wilson 
1592d1e61e7fSChris Wilson 	if (dev_priv->hangcheck_count++ > 1) {
1593b4519513SChris Wilson 		bool hung = true;
1594b4519513SChris Wilson 
1595d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1596d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
1597d1e61e7fSChris Wilson 
1598d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
1599b4519513SChris Wilson 			struct intel_ring_buffer *ring;
1600b4519513SChris Wilson 			int i;
1601b4519513SChris Wilson 
1602d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
1603d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
1604d1e61e7fSChris Wilson 			 * and break the hang. This should work on
1605d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
1606d1e61e7fSChris Wilson 			 */
1607b4519513SChris Wilson 			for_each_ring(ring, dev_priv, i)
1608b4519513SChris Wilson 				hung &= !kick_ring(ring);
1609d1e61e7fSChris Wilson 		}
1610d1e61e7fSChris Wilson 
1611b4519513SChris Wilson 		return hung;
1612d1e61e7fSChris Wilson 	}
1613d1e61e7fSChris Wilson 
1614d1e61e7fSChris Wilson 	return false;
1615d1e61e7fSChris Wilson }
1616d1e61e7fSChris Wilson 
1617f65d9421SBen Gamari /**
1618f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1619f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1620f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1621f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1622f65d9421SBen Gamari  */
1623f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1624f65d9421SBen Gamari {
1625f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1626f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1627b4519513SChris Wilson 	uint32_t acthd[I915_NUM_RINGS], instdone, instdone1;
1628b4519513SChris Wilson 	struct intel_ring_buffer *ring;
1629b4519513SChris Wilson 	bool err = false, idle;
1630b4519513SChris Wilson 	int i;
1631893eead0SChris Wilson 
16323e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
16333e0dc6b0SBen Widawsky 		return;
16343e0dc6b0SBen Widawsky 
1635b4519513SChris Wilson 	memset(acthd, 0, sizeof(acthd));
1636b4519513SChris Wilson 	idle = true;
1637b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
1638b4519513SChris Wilson 	    idle &= i915_hangcheck_ring_idle(ring, &err);
1639b4519513SChris Wilson 	    acthd[i] = intel_ring_get_active_head(ring);
1640b4519513SChris Wilson 	}
1641b4519513SChris Wilson 
1642893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
1643b4519513SChris Wilson 	if (idle) {
1644d1e61e7fSChris Wilson 		if (err) {
1645d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
1646d1e61e7fSChris Wilson 				return;
1647d1e61e7fSChris Wilson 
1648893eead0SChris Wilson 			goto repeat;
1649d1e61e7fSChris Wilson 		}
1650d1e61e7fSChris Wilson 
1651d1e61e7fSChris Wilson 		dev_priv->hangcheck_count = 0;
1652893eead0SChris Wilson 		return;
1653893eead0SChris Wilson 	}
1654f65d9421SBen Gamari 
1655a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1656cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1657cbb465e7SChris Wilson 		instdone1 = 0;
1658cbb465e7SChris Wilson 	} else {
1659cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1660cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1661cbb465e7SChris Wilson 	}
1662f65d9421SBen Gamari 
1663b4519513SChris Wilson 	if (memcmp(dev_priv->last_acthd, acthd, sizeof(acthd)) == 0 &&
1664cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1665cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1666d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
1667f65d9421SBen Gamari 			return;
1668cbb465e7SChris Wilson 	} else {
1669cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1670cbb465e7SChris Wilson 
1671b4519513SChris Wilson 		memcpy(dev_priv->last_acthd, acthd, sizeof(acthd));
1672cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1673cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1674cbb465e7SChris Wilson 	}
1675f65d9421SBen Gamari 
1676893eead0SChris Wilson repeat:
1677f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1678b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1679b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1680f65d9421SBen Gamari }
1681f65d9421SBen Gamari 
1682c0e09200SDave Airlie /* drm_dma.h hooks
1683c0e09200SDave Airlie */
1684f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
1685036a4a7dSZhenyu Wang {
1686036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1687036a4a7dSZhenyu Wang 
16884697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
16894697995bSJesse Barnes 
1690036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1691bdfcdb63SDaniel Vetter 
1692036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1693036a4a7dSZhenyu Wang 
1694036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1695036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
16963143a2bfSChris Wilson 	POSTING_READ(DEIER);
1697036a4a7dSZhenyu Wang 
1698036a4a7dSZhenyu Wang 	/* and GT */
1699036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1700036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
17013143a2bfSChris Wilson 	POSTING_READ(GTIER);
1702c650156aSZhenyu Wang 
1703c650156aSZhenyu Wang 	/* south display irq */
1704c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1705c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
17063143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1707036a4a7dSZhenyu Wang }
1708036a4a7dSZhenyu Wang 
17097e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
17107e231dbeSJesse Barnes {
17117e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17127e231dbeSJesse Barnes 	int pipe;
17137e231dbeSJesse Barnes 
17147e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
17157e231dbeSJesse Barnes 
17167e231dbeSJesse Barnes 	/* VLV magic */
17177e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
17187e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
17197e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
17207e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
17217e231dbeSJesse Barnes 
17227e231dbeSJesse Barnes 	/* and GT */
17237e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
17247e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
17257e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
17267e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
17277e231dbeSJesse Barnes 	POSTING_READ(GTIER);
17287e231dbeSJesse Barnes 
17297e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
17307e231dbeSJesse Barnes 
17317e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
17327e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
17337e231dbeSJesse Barnes 	for_each_pipe(pipe)
17347e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
17357e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
17367e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
17377e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
17387e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
17397e231dbeSJesse Barnes }
17407e231dbeSJesse Barnes 
17417fe0b973SKeith Packard /*
17427fe0b973SKeith Packard  * Enable digital hotplug on the PCH, and configure the DP short pulse
17437fe0b973SKeith Packard  * duration to 2ms (which is the minimum in the Display Port spec)
17447fe0b973SKeith Packard  *
17457fe0b973SKeith Packard  * This register is the same on all known PCH chips.
17467fe0b973SKeith Packard  */
17477fe0b973SKeith Packard 
17487fe0b973SKeith Packard static void ironlake_enable_pch_hotplug(struct drm_device *dev)
17497fe0b973SKeith Packard {
17507fe0b973SKeith Packard 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17517fe0b973SKeith Packard 	u32	hotplug;
17527fe0b973SKeith Packard 
17537fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
17547fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
17557fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
17567fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
17577fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
17587fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
17597fe0b973SKeith Packard }
17607fe0b973SKeith Packard 
1761f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
1762036a4a7dSZhenyu Wang {
1763036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1764036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1765013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1766013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
17671ec14ad3SChris Wilson 	u32 render_irqs;
17682d7b8366SYuanhan Liu 	u32 hotplug_mask;
1769036a4a7dSZhenyu Wang 
17701ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1771036a4a7dSZhenyu Wang 
1772036a4a7dSZhenyu Wang 	/* should always can generate irq */
1773036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
17741ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
17751ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
17763143a2bfSChris Wilson 	POSTING_READ(DEIER);
1777036a4a7dSZhenyu Wang 
17781ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1779036a4a7dSZhenyu Wang 
1780036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
17811ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1782881f47b6SXiang, Haihao 
17831ec14ad3SChris Wilson 	if (IS_GEN6(dev))
17841ec14ad3SChris Wilson 		render_irqs =
17851ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
1786e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
1787e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
17881ec14ad3SChris Wilson 	else
17891ec14ad3SChris Wilson 		render_irqs =
179088f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1791c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
17921ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
17931ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
17943143a2bfSChris Wilson 	POSTING_READ(GTIER);
1795036a4a7dSZhenyu Wang 
17962d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
17979035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
17989035a97aSChris Wilson 				SDE_PORTB_HOTPLUG_CPT |
17999035a97aSChris Wilson 				SDE_PORTC_HOTPLUG_CPT |
18009035a97aSChris Wilson 				SDE_PORTD_HOTPLUG_CPT);
18012d7b8366SYuanhan Liu 	} else {
18029035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG |
18039035a97aSChris Wilson 				SDE_PORTB_HOTPLUG |
18049035a97aSChris Wilson 				SDE_PORTC_HOTPLUG |
18059035a97aSChris Wilson 				SDE_PORTD_HOTPLUG |
18069035a97aSChris Wilson 				SDE_AUX_MASK);
18072d7b8366SYuanhan Liu 	}
18082d7b8366SYuanhan Liu 
18091ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1810c650156aSZhenyu Wang 
1811c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
18121ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
18131ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
18143143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1815c650156aSZhenyu Wang 
18167fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
18177fe0b973SKeith Packard 
1818f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1819f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1820f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1821f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1822f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1823f97108d1SJesse Barnes 	}
1824f97108d1SJesse Barnes 
1825036a4a7dSZhenyu Wang 	return 0;
1826036a4a7dSZhenyu Wang }
1827036a4a7dSZhenyu Wang 
1828f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
1829b1f14ad0SJesse Barnes {
1830b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1831b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
1832b615b57aSChris Wilson 	u32 display_mask =
1833b615b57aSChris Wilson 		DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
1834b615b57aSChris Wilson 		DE_PLANEC_FLIP_DONE_IVB |
1835b615b57aSChris Wilson 		DE_PLANEB_FLIP_DONE_IVB |
1836b615b57aSChris Wilson 		DE_PLANEA_FLIP_DONE_IVB;
1837b1f14ad0SJesse Barnes 	u32 render_irqs;
1838b1f14ad0SJesse Barnes 	u32 hotplug_mask;
1839b1f14ad0SJesse Barnes 
1840b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
1841b1f14ad0SJesse Barnes 
1842b1f14ad0SJesse Barnes 	/* should always can generate irq */
1843b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1844b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
1845b615b57aSChris Wilson 	I915_WRITE(DEIER,
1846b615b57aSChris Wilson 		   display_mask |
1847b615b57aSChris Wilson 		   DE_PIPEC_VBLANK_IVB |
1848b615b57aSChris Wilson 		   DE_PIPEB_VBLANK_IVB |
1849b615b57aSChris Wilson 		   DE_PIPEA_VBLANK_IVB);
1850b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1851b1f14ad0SJesse Barnes 
185215b9f80eSBen Widawsky 	dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
1853b1f14ad0SJesse Barnes 
1854b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1855b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1856b1f14ad0SJesse Barnes 
1857e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
185815b9f80eSBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
1859b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
1860b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
1861b1f14ad0SJesse Barnes 
1862b1f14ad0SJesse Barnes 	hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
1863b1f14ad0SJesse Barnes 			SDE_PORTB_HOTPLUG_CPT |
1864b1f14ad0SJesse Barnes 			SDE_PORTC_HOTPLUG_CPT |
1865b1f14ad0SJesse Barnes 			SDE_PORTD_HOTPLUG_CPT);
1866b1f14ad0SJesse Barnes 	dev_priv->pch_irq_mask = ~hotplug_mask;
1867b1f14ad0SJesse Barnes 
1868b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1869b1f14ad0SJesse Barnes 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1870b1f14ad0SJesse Barnes 	I915_WRITE(SDEIER, hotplug_mask);
1871b1f14ad0SJesse Barnes 	POSTING_READ(SDEIER);
1872b1f14ad0SJesse Barnes 
18737fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
18747fe0b973SKeith Packard 
1875b1f14ad0SJesse Barnes 	return 0;
1876b1f14ad0SJesse Barnes }
1877b1f14ad0SJesse Barnes 
18787e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
18797e231dbeSJesse Barnes {
18807e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18817e231dbeSJesse Barnes 	u32 enable_mask;
18827e231dbeSJesse Barnes 	u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
1883*31acc7f5SJesse Barnes 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
18847e231dbeSJesse Barnes 	u16 msid;
18857e231dbeSJesse Barnes 
18867e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
1887*31acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
1888*31acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
1889*31acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
18907e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
18917e231dbeSJesse Barnes 
1892*31acc7f5SJesse Barnes 	/*
1893*31acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
1894*31acc7f5SJesse Barnes 	 * toggle them based on usage.
1895*31acc7f5SJesse Barnes 	 */
1896*31acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
1897*31acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
1898*31acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
18997e231dbeSJesse Barnes 
19007e231dbeSJesse Barnes 	dev_priv->pipestat[0] = 0;
19017e231dbeSJesse Barnes 	dev_priv->pipestat[1] = 0;
19027e231dbeSJesse Barnes 
19037e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
19047e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
19057e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
19067e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
19077e231dbeSJesse Barnes 	msid |= (1<<14);
19087e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
19097e231dbeSJesse Barnes 
19107e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
19117e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
19127e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19137e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
19147e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
19157e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
19167e231dbeSJesse Barnes 
1917*31acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 0, pipestat_enable);
1918*31acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, 1, pipestat_enable);
1919*31acc7f5SJesse Barnes 
19207e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19217e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19227e231dbeSJesse Barnes 
1923*31acc7f5SJesse Barnes 	dev_priv->gt_irq_mask = ~0;
1924*31acc7f5SJesse Barnes 
1925*31acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1926*31acc7f5SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1927*31acc7f5SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1928*31acc7f5SJesse Barnes 	I915_WRITE(GTIER, GT_GEN6_BLT_FLUSHDW_NOTIFY_INTERRUPT |
19297e231dbeSJesse Barnes 		   GT_GEN6_BLT_CS_ERROR_INTERRUPT |
1930e2a1e2f0SBen Widawsky 		   GT_GEN6_BLT_USER_INTERRUPT |
19317e231dbeSJesse Barnes 		   GT_GEN6_BSD_USER_INTERRUPT |
19327e231dbeSJesse Barnes 		   GT_GEN6_BSD_CS_ERROR_INTERRUPT |
19337e231dbeSJesse Barnes 		   GT_GEN7_L3_PARITY_ERROR_INTERRUPT |
19347e231dbeSJesse Barnes 		   GT_PIPE_NOTIFY |
19357e231dbeSJesse Barnes 		   GT_RENDER_CS_ERROR_INTERRUPT |
19367e231dbeSJesse Barnes 		   GT_SYNC_STATUS |
1937*31acc7f5SJesse Barnes 		   GT_USER_INTERRUPT);
19387e231dbeSJesse Barnes 	POSTING_READ(GTIER);
19397e231dbeSJesse Barnes 
19407e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
19417e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
19427e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
19437e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
19447e231dbeSJesse Barnes #endif
19457e231dbeSJesse Barnes 
19467e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
19477e231dbeSJesse Barnes #if 0 /* FIXME: check register definitions; some have moved */
19487e231dbeSJesse Barnes 	/* Note HDMI and DP share bits */
19497e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
19507e231dbeSJesse Barnes 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
19517e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
19527e231dbeSJesse Barnes 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
19537e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
19547e231dbeSJesse Barnes 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
19557e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
19567e231dbeSJesse Barnes 		hotplug_en |= SDVOC_HOTPLUG_INT_EN;
19577e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
19587e231dbeSJesse Barnes 		hotplug_en |= SDVOB_HOTPLUG_INT_EN;
19597e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
19607e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_INT_EN;
19617e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
19627e231dbeSJesse Barnes 	}
19637e231dbeSJesse Barnes #endif
19647e231dbeSJesse Barnes 
19657e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
19667e231dbeSJesse Barnes 
19677e231dbeSJesse Barnes 	return 0;
19687e231dbeSJesse Barnes }
19697e231dbeSJesse Barnes 
19707e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
19717e231dbeSJesse Barnes {
19727e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19737e231dbeSJesse Barnes 	int pipe;
19747e231dbeSJesse Barnes 
19757e231dbeSJesse Barnes 	if (!dev_priv)
19767e231dbeSJesse Barnes 		return;
19777e231dbeSJesse Barnes 
19787e231dbeSJesse Barnes 	for_each_pipe(pipe)
19797e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
19807e231dbeSJesse Barnes 
19817e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
19827e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
19837e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
19847e231dbeSJesse Barnes 	for_each_pipe(pipe)
19857e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
19867e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
19877e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
19887e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
19897e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
19907e231dbeSJesse Barnes }
19917e231dbeSJesse Barnes 
1992f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
1993036a4a7dSZhenyu Wang {
1994036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19954697995bSJesse Barnes 
19964697995bSJesse Barnes 	if (!dev_priv)
19974697995bSJesse Barnes 		return;
19984697995bSJesse Barnes 
1999036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2000036a4a7dSZhenyu Wang 
2001036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2002036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2003036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2004036a4a7dSZhenyu Wang 
2005036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2006036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2007036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2008192aac1fSKeith Packard 
2009192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2010192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2011192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2012036a4a7dSZhenyu Wang }
2013036a4a7dSZhenyu Wang 
2014c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2015c2798b19SChris Wilson {
2016c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2017c2798b19SChris Wilson 	int pipe;
2018c2798b19SChris Wilson 
2019c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2020c2798b19SChris Wilson 
2021c2798b19SChris Wilson 	for_each_pipe(pipe)
2022c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2023c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2024c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2025c2798b19SChris Wilson 	POSTING_READ16(IER);
2026c2798b19SChris Wilson }
2027c2798b19SChris Wilson 
2028c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2029c2798b19SChris Wilson {
2030c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2031c2798b19SChris Wilson 
2032c2798b19SChris Wilson 	dev_priv->pipestat[0] = 0;
2033c2798b19SChris Wilson 	dev_priv->pipestat[1] = 0;
2034c2798b19SChris Wilson 
2035c2798b19SChris Wilson 	I915_WRITE16(EMR,
2036c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2037c2798b19SChris Wilson 
2038c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2039c2798b19SChris Wilson 	dev_priv->irq_mask =
2040c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2041c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2042c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2043c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2044c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2045c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2046c2798b19SChris Wilson 
2047c2798b19SChris Wilson 	I915_WRITE16(IER,
2048c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2049c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2050c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2051c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2052c2798b19SChris Wilson 	POSTING_READ16(IER);
2053c2798b19SChris Wilson 
2054c2798b19SChris Wilson 	return 0;
2055c2798b19SChris Wilson }
2056c2798b19SChris Wilson 
2057c2798b19SChris Wilson static irqreturn_t i8xx_irq_handler(DRM_IRQ_ARGS)
2058c2798b19SChris Wilson {
2059c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2060c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2061c2798b19SChris Wilson 	u16 iir, new_iir;
2062c2798b19SChris Wilson 	u32 pipe_stats[2];
2063c2798b19SChris Wilson 	unsigned long irqflags;
2064c2798b19SChris Wilson 	int irq_received;
2065c2798b19SChris Wilson 	int pipe;
2066c2798b19SChris Wilson 	u16 flip_mask =
2067c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2068c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2069c2798b19SChris Wilson 
2070c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2071c2798b19SChris Wilson 
2072c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2073c2798b19SChris Wilson 	if (iir == 0)
2074c2798b19SChris Wilson 		return IRQ_NONE;
2075c2798b19SChris Wilson 
2076c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2077c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2078c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2079c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2080c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2081c2798b19SChris Wilson 		 */
2082c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2083c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2084c2798b19SChris Wilson 			i915_handle_error(dev, false);
2085c2798b19SChris Wilson 
2086c2798b19SChris Wilson 		for_each_pipe(pipe) {
2087c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2088c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2089c2798b19SChris Wilson 
2090c2798b19SChris Wilson 			/*
2091c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2092c2798b19SChris Wilson 			 */
2093c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2094c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2095c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2096c2798b19SChris Wilson 							 pipe_name(pipe));
2097c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2098c2798b19SChris Wilson 				irq_received = 1;
2099c2798b19SChris Wilson 			}
2100c2798b19SChris Wilson 		}
2101c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2102c2798b19SChris Wilson 
2103c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2104c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2105c2798b19SChris Wilson 
2106d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
2107c2798b19SChris Wilson 
2108c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2109c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2110c2798b19SChris Wilson 
2111c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
2112c2798b19SChris Wilson 		    drm_handle_vblank(dev, 0)) {
2113c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
2114c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 0);
2115c2798b19SChris Wilson 				intel_finish_page_flip(dev, 0);
2116c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT;
2117c2798b19SChris Wilson 			}
2118c2798b19SChris Wilson 		}
2119c2798b19SChris Wilson 
2120c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
2121c2798b19SChris Wilson 		    drm_handle_vblank(dev, 1)) {
2122c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
2123c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 1);
2124c2798b19SChris Wilson 				intel_finish_page_flip(dev, 1);
2125c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2126c2798b19SChris Wilson 			}
2127c2798b19SChris Wilson 		}
2128c2798b19SChris Wilson 
2129c2798b19SChris Wilson 		iir = new_iir;
2130c2798b19SChris Wilson 	}
2131c2798b19SChris Wilson 
2132c2798b19SChris Wilson 	return IRQ_HANDLED;
2133c2798b19SChris Wilson }
2134c2798b19SChris Wilson 
2135c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2136c2798b19SChris Wilson {
2137c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2138c2798b19SChris Wilson 	int pipe;
2139c2798b19SChris Wilson 
2140c2798b19SChris Wilson 	for_each_pipe(pipe) {
2141c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2142c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2143c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2144c2798b19SChris Wilson 	}
2145c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2146c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2147c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2148c2798b19SChris Wilson }
2149c2798b19SChris Wilson 
2150a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2151a266c7d5SChris Wilson {
2152a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2153a266c7d5SChris Wilson 	int pipe;
2154a266c7d5SChris Wilson 
2155a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2156a266c7d5SChris Wilson 
2157a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2158a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2159a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2160a266c7d5SChris Wilson 	}
2161a266c7d5SChris Wilson 
216200d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2163a266c7d5SChris Wilson 	for_each_pipe(pipe)
2164a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2165a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2166a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2167a266c7d5SChris Wilson 	POSTING_READ(IER);
2168a266c7d5SChris Wilson }
2169a266c7d5SChris Wilson 
2170a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2171a266c7d5SChris Wilson {
2172a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
217338bde180SChris Wilson 	u32 enable_mask;
2174a266c7d5SChris Wilson 
2175a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2176a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2177a266c7d5SChris Wilson 
217838bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
217938bde180SChris Wilson 
218038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
218138bde180SChris Wilson 	dev_priv->irq_mask =
218238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
218338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
218438bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
218538bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
218638bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
218738bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
218838bde180SChris Wilson 
218938bde180SChris Wilson 	enable_mask =
219038bde180SChris Wilson 		I915_ASLE_INTERRUPT |
219138bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
219238bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
219338bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
219438bde180SChris Wilson 		I915_USER_INTERRUPT;
219538bde180SChris Wilson 
2196a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2197a266c7d5SChris Wilson 		/* Enable in IER... */
2198a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2199a266c7d5SChris Wilson 		/* and unmask in IMR */
2200a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2201a266c7d5SChris Wilson 	}
2202a266c7d5SChris Wilson 
2203a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2204a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2205a266c7d5SChris Wilson 	POSTING_READ(IER);
2206a266c7d5SChris Wilson 
2207a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2208a266c7d5SChris Wilson 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2209a266c7d5SChris Wilson 
2210a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2211a266c7d5SChris Wilson 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2212a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2213a266c7d5SChris Wilson 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2214a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2215a266c7d5SChris Wilson 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
2216084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
2217a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2218084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
2219a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2220a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2221a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_INT_EN;
2222a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2223a266c7d5SChris Wilson 		}
2224a266c7d5SChris Wilson 
2225a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2226a266c7d5SChris Wilson 
2227a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2228a266c7d5SChris Wilson 	}
2229a266c7d5SChris Wilson 
2230a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2231a266c7d5SChris Wilson 
2232a266c7d5SChris Wilson 	return 0;
2233a266c7d5SChris Wilson }
2234a266c7d5SChris Wilson 
2235a266c7d5SChris Wilson static irqreturn_t i915_irq_handler(DRM_IRQ_ARGS)
2236a266c7d5SChris Wilson {
2237a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2238a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22398291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2240a266c7d5SChris Wilson 	unsigned long irqflags;
224138bde180SChris Wilson 	u32 flip_mask =
224238bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
224338bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
224438bde180SChris Wilson 	u32 flip[2] = {
224538bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT,
224638bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT
224738bde180SChris Wilson 	};
224838bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2249a266c7d5SChris Wilson 
2250a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2251a266c7d5SChris Wilson 
2252a266c7d5SChris Wilson 	iir = I915_READ(IIR);
225338bde180SChris Wilson 	do {
225438bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
22558291ee90SChris Wilson 		bool blc_event = false;
2256a266c7d5SChris Wilson 
2257a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2258a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2259a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2260a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2261a266c7d5SChris Wilson 		 */
2262a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2263a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2264a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2265a266c7d5SChris Wilson 
2266a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2267a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2268a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2269a266c7d5SChris Wilson 
227038bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2271a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2272a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2273a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2274a266c7d5SChris Wilson 							 pipe_name(pipe));
2275a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
227638bde180SChris Wilson 				irq_received = true;
2277a266c7d5SChris Wilson 			}
2278a266c7d5SChris Wilson 		}
2279a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2280a266c7d5SChris Wilson 
2281a266c7d5SChris Wilson 		if (!irq_received)
2282a266c7d5SChris Wilson 			break;
2283a266c7d5SChris Wilson 
2284a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2285a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2286a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2287a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2288a266c7d5SChris Wilson 
2289a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2290a266c7d5SChris Wilson 				  hotplug_status);
2291a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2292a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2293a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2294a266c7d5SChris Wilson 
2295a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
229638bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2297a266c7d5SChris Wilson 		}
2298a266c7d5SChris Wilson 
229938bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2300a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2301a266c7d5SChris Wilson 
2302a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2303a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2304a266c7d5SChris Wilson 
2305a266c7d5SChris Wilson 		for_each_pipe(pipe) {
230638bde180SChris Wilson 			int plane = pipe;
230738bde180SChris Wilson 			if (IS_MOBILE(dev))
230838bde180SChris Wilson 				plane = !plane;
23098291ee90SChris Wilson 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
2310a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
231138bde180SChris Wilson 				if (iir & flip[plane]) {
231238bde180SChris Wilson 					intel_prepare_page_flip(dev, plane);
2313a266c7d5SChris Wilson 					intel_finish_page_flip(dev, pipe);
231438bde180SChris Wilson 					flip_mask &= ~flip[plane];
231538bde180SChris Wilson 				}
2316a266c7d5SChris Wilson 			}
2317a266c7d5SChris Wilson 
2318a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2319a266c7d5SChris Wilson 				blc_event = true;
2320a266c7d5SChris Wilson 		}
2321a266c7d5SChris Wilson 
2322a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2323a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2324a266c7d5SChris Wilson 
2325a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2326a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2327a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2328a266c7d5SChris Wilson 		 * we would never get another interrupt.
2329a266c7d5SChris Wilson 		 *
2330a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2331a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2332a266c7d5SChris Wilson 		 * another one.
2333a266c7d5SChris Wilson 		 *
2334a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2335a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2336a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2337a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2338a266c7d5SChris Wilson 		 * stray interrupts.
2339a266c7d5SChris Wilson 		 */
234038bde180SChris Wilson 		ret = IRQ_HANDLED;
2341a266c7d5SChris Wilson 		iir = new_iir;
234238bde180SChris Wilson 	} while (iir & ~flip_mask);
2343a266c7d5SChris Wilson 
2344d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
23458291ee90SChris Wilson 
2346a266c7d5SChris Wilson 	return ret;
2347a266c7d5SChris Wilson }
2348a266c7d5SChris Wilson 
2349a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2350a266c7d5SChris Wilson {
2351a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2352a266c7d5SChris Wilson 	int pipe;
2353a266c7d5SChris Wilson 
2354a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2355a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2356a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2357a266c7d5SChris Wilson 	}
2358a266c7d5SChris Wilson 
235900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
236055b39755SChris Wilson 	for_each_pipe(pipe) {
236155b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2362a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
236355b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
236455b39755SChris Wilson 	}
2365a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2366a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2367a266c7d5SChris Wilson 
2368a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2369a266c7d5SChris Wilson }
2370a266c7d5SChris Wilson 
2371a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2372a266c7d5SChris Wilson {
2373a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2374a266c7d5SChris Wilson 	int pipe;
2375a266c7d5SChris Wilson 
2376a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2377a266c7d5SChris Wilson 
2378a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2379a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2380a266c7d5SChris Wilson 
2381a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2382a266c7d5SChris Wilson 	for_each_pipe(pipe)
2383a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2384a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2385a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2386a266c7d5SChris Wilson 	POSTING_READ(IER);
2387a266c7d5SChris Wilson }
2388a266c7d5SChris Wilson 
2389a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2390a266c7d5SChris Wilson {
2391a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2392adca4730SChris Wilson 	u32 hotplug_en;
2393bbba0a97SChris Wilson 	u32 enable_mask;
2394a266c7d5SChris Wilson 	u32 error_mask;
2395a266c7d5SChris Wilson 
2396a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2397bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2398adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
2399bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2400bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2401bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2402bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2403bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2404bbba0a97SChris Wilson 
2405bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
2406bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2407bbba0a97SChris Wilson 
2408bbba0a97SChris Wilson 	if (IS_G4X(dev))
2409bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2410a266c7d5SChris Wilson 
2411a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2412a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2413a266c7d5SChris Wilson 
2414a266c7d5SChris Wilson 	/*
2415a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2416a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2417a266c7d5SChris Wilson 	 */
2418a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2419a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2420a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2421a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2422a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2423a266c7d5SChris Wilson 	} else {
2424a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2425a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2426a266c7d5SChris Wilson 	}
2427a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2428a266c7d5SChris Wilson 
2429a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2430a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2431a266c7d5SChris Wilson 	POSTING_READ(IER);
2432a266c7d5SChris Wilson 
2433adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
2434adca4730SChris Wilson 	hotplug_en = 0;
2435a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2436a266c7d5SChris Wilson 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2437a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2438a266c7d5SChris Wilson 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2439a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2440a266c7d5SChris Wilson 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
2441084b612eSChris Wilson 	if (IS_G4X(dev)) {
2442084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_G4X)
2443a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2444084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_G4X)
2445a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2446084b612eSChris Wilson 	} else {
2447084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I965)
2448084b612eSChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2449084b612eSChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I965)
2450084b612eSChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2451084b612eSChris Wilson 	}
2452a266c7d5SChris Wilson 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2453a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_INT_EN;
2454a266c7d5SChris Wilson 
2455a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
2456a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
2457a266c7d5SChris Wilson 		   seconds later.  So just do it once.
2458a266c7d5SChris Wilson 		   */
2459a266c7d5SChris Wilson 		if (IS_G4X(dev))
2460a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2461a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2462a266c7d5SChris Wilson 	}
2463a266c7d5SChris Wilson 
2464a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
2465a266c7d5SChris Wilson 
2466a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2467a266c7d5SChris Wilson 
2468a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2469a266c7d5SChris Wilson 
2470a266c7d5SChris Wilson 	return 0;
2471a266c7d5SChris Wilson }
2472a266c7d5SChris Wilson 
2473a266c7d5SChris Wilson static irqreturn_t i965_irq_handler(DRM_IRQ_ARGS)
2474a266c7d5SChris Wilson {
2475a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2476a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2477a266c7d5SChris Wilson 	u32 iir, new_iir;
2478a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2479a266c7d5SChris Wilson 	unsigned long irqflags;
2480a266c7d5SChris Wilson 	int irq_received;
2481a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
2482a266c7d5SChris Wilson 
2483a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2484a266c7d5SChris Wilson 
2485a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2486a266c7d5SChris Wilson 
2487a266c7d5SChris Wilson 	for (;;) {
24882c8ba29fSChris Wilson 		bool blc_event = false;
24892c8ba29fSChris Wilson 
2490a266c7d5SChris Wilson 		irq_received = iir != 0;
2491a266c7d5SChris Wilson 
2492a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2493a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2494a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2495a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2496a266c7d5SChris Wilson 		 */
2497a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2498a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2499a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2500a266c7d5SChris Wilson 
2501a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2502a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2503a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2504a266c7d5SChris Wilson 
2505a266c7d5SChris Wilson 			/*
2506a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2507a266c7d5SChris Wilson 			 */
2508a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2509a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2510a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2511a266c7d5SChris Wilson 							 pipe_name(pipe));
2512a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2513a266c7d5SChris Wilson 				irq_received = 1;
2514a266c7d5SChris Wilson 			}
2515a266c7d5SChris Wilson 		}
2516a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2517a266c7d5SChris Wilson 
2518a266c7d5SChris Wilson 		if (!irq_received)
2519a266c7d5SChris Wilson 			break;
2520a266c7d5SChris Wilson 
2521a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2522a266c7d5SChris Wilson 
2523a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2524adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2525a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2526a266c7d5SChris Wilson 
2527a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2528a266c7d5SChris Wilson 				  hotplug_status);
2529a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2530a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2531a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2532a266c7d5SChris Wilson 
2533a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2534a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2535a266c7d5SChris Wilson 		}
2536a266c7d5SChris Wilson 
2537a266c7d5SChris Wilson 		I915_WRITE(IIR, iir);
2538a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2539a266c7d5SChris Wilson 
2540a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2541a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2542a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2543a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2544a266c7d5SChris Wilson 
25454f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
2546a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 0);
2547a266c7d5SChris Wilson 
25484f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
2549a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 1);
2550a266c7d5SChris Wilson 
2551a266c7d5SChris Wilson 		for_each_pipe(pipe) {
25522c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
2553a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
2554a266c7d5SChris Wilson 				i915_pageflip_stall_check(dev, pipe);
2555a266c7d5SChris Wilson 				intel_finish_page_flip(dev, pipe);
2556a266c7d5SChris Wilson 			}
2557a266c7d5SChris Wilson 
2558a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2559a266c7d5SChris Wilson 				blc_event = true;
2560a266c7d5SChris Wilson 		}
2561a266c7d5SChris Wilson 
2562a266c7d5SChris Wilson 
2563a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2564a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2565a266c7d5SChris Wilson 
2566a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2567a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2568a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2569a266c7d5SChris Wilson 		 * we would never get another interrupt.
2570a266c7d5SChris Wilson 		 *
2571a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2572a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2573a266c7d5SChris Wilson 		 * another one.
2574a266c7d5SChris Wilson 		 *
2575a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2576a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2577a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2578a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2579a266c7d5SChris Wilson 		 * stray interrupts.
2580a266c7d5SChris Wilson 		 */
2581a266c7d5SChris Wilson 		iir = new_iir;
2582a266c7d5SChris Wilson 	}
2583a266c7d5SChris Wilson 
2584d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
25852c8ba29fSChris Wilson 
2586a266c7d5SChris Wilson 	return ret;
2587a266c7d5SChris Wilson }
2588a266c7d5SChris Wilson 
2589a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
2590a266c7d5SChris Wilson {
2591a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2592a266c7d5SChris Wilson 	int pipe;
2593a266c7d5SChris Wilson 
2594a266c7d5SChris Wilson 	if (!dev_priv)
2595a266c7d5SChris Wilson 		return;
2596a266c7d5SChris Wilson 
2597a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
2598a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2599a266c7d5SChris Wilson 
2600a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
2601a266c7d5SChris Wilson 	for_each_pipe(pipe)
2602a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2603a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2604a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2605a266c7d5SChris Wilson 
2606a266c7d5SChris Wilson 	for_each_pipe(pipe)
2607a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
2608a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2609a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2610a266c7d5SChris Wilson }
2611a266c7d5SChris Wilson 
2612f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2613f71d4af4SJesse Barnes {
26148b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
26158b2e326dSChris Wilson 
26168b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
26178b2e326dSChris Wilson 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
26188b2e326dSChris Wilson 	INIT_WORK(&dev_priv->rps_work, gen6_pm_rps_work);
261998fd81cdSDaniel Vetter 	INIT_WORK(&dev_priv->parity_error_work, ivybridge_parity_work);
26208b2e326dSChris Wilson 
2621f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2622f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
26237d4e146fSEugeni Dodonov 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
2624f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2625f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2626f71d4af4SJesse Barnes 	}
2627f71d4af4SJesse Barnes 
2628c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2629f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2630c3613de9SKeith Packard 	else
2631c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2632f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2633f71d4af4SJesse Barnes 
26347e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
26357e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
26367e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
26377e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
26387e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
26397e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
26407e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
26417e231dbeSJesse Barnes 	} else if (IS_IVYBRIDGE(dev)) {
2642f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2643f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2644f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2645f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2646f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2647f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2648f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
26497d4e146fSEugeni Dodonov 	} else if (IS_HASWELL(dev)) {
26507d4e146fSEugeni Dodonov 		/* Share interrupts handling with IVB */
26517d4e146fSEugeni Dodonov 		dev->driver->irq_handler = ivybridge_irq_handler;
26527d4e146fSEugeni Dodonov 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
26537d4e146fSEugeni Dodonov 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
26547d4e146fSEugeni Dodonov 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
26557d4e146fSEugeni Dodonov 		dev->driver->enable_vblank = ivybridge_enable_vblank;
26567d4e146fSEugeni Dodonov 		dev->driver->disable_vblank = ivybridge_disable_vblank;
2657f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2658f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2659f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2660f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2661f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2662f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2663f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
2664f71d4af4SJesse Barnes 	} else {
2665c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
2666c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
2667c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
2668c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
2669c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
2670a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
26714f7d1e79SChris Wilson 			/* IIR "flip pending" means done if this bit is set */
26724f7d1e79SChris Wilson 			I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
26734f7d1e79SChris Wilson 
2674a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
2675a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
2676a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
2677a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
2678c2798b19SChris Wilson 		} else {
2679a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
2680a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
2681a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
2682a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
2683c2798b19SChris Wilson 		}
2684f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
2685f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
2686f71d4af4SJesse Barnes 	}
2687f71d4af4SJesse Barnes }
2688