xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision d1e61e7fc4456c4cb9a33ed182edf40e34ddedea)
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 
40c0e09200SDave Airlie #define MAX_NOPID ((u32)~0)
41c0e09200SDave Airlie 
427c463586SKeith Packard /**
437c463586SKeith Packard  * Interrupts that are always left unmasked.
447c463586SKeith Packard  *
457c463586SKeith Packard  * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
467c463586SKeith Packard  * we leave them always unmasked in IMR and then control enabling them through
477c463586SKeith Packard  * PIPESTAT alone.
487c463586SKeith Packard  */
496b95a207SKristian Høgsberg #define I915_INTERRUPT_ENABLE_FIX			\
506b95a207SKristian Høgsberg 	(I915_ASLE_INTERRUPT |				\
510a3e67a4SJesse Barnes 	 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |		\
5263eeaf38SJesse Barnes 	 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |		\
536b95a207SKristian Høgsberg 	 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |	\
546b95a207SKristian Høgsberg 	 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |	\
5563eeaf38SJesse Barnes 	 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
56ed4cb414SEric Anholt 
577c463586SKeith Packard /** Interrupts that we mask and unmask at runtime. */
58d1b851fcSZou Nan hai #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT | I915_BSD_USER_INTERRUPT)
597c463586SKeith Packard 
6079e53945SJesse Barnes #define I915_PIPE_VBLANK_STATUS	(PIPE_START_VBLANK_INTERRUPT_STATUS |\
6179e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_STATUS)
6279e53945SJesse Barnes 
6379e53945SJesse Barnes #define I915_PIPE_VBLANK_ENABLE	(PIPE_START_VBLANK_INTERRUPT_ENABLE |\
6479e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_ENABLE)
6579e53945SJesse Barnes 
6679e53945SJesse Barnes #define DRM_I915_VBLANK_PIPE_ALL	(DRM_I915_VBLANK_PIPE_A | \
6779e53945SJesse Barnes 					 DRM_I915_VBLANK_PIPE_B)
6879e53945SJesse Barnes 
69036a4a7dSZhenyu Wang /* For display hotplug interrupt */
70995b6762SChris Wilson static void
71f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
72036a4a7dSZhenyu Wang {
731ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
741ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
751ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
763143a2bfSChris Wilson 		POSTING_READ(DEIMR);
77036a4a7dSZhenyu Wang 	}
78036a4a7dSZhenyu Wang }
79036a4a7dSZhenyu Wang 
80036a4a7dSZhenyu Wang static inline void
81f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
82036a4a7dSZhenyu Wang {
831ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
841ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
851ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
863143a2bfSChris Wilson 		POSTING_READ(DEIMR);
87036a4a7dSZhenyu Wang 	}
88036a4a7dSZhenyu Wang }
89036a4a7dSZhenyu Wang 
907c463586SKeith Packard void
917c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
927c463586SKeith Packard {
937c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
949db4a9c7SJesse Barnes 		u32 reg = PIPESTAT(pipe);
957c463586SKeith Packard 
967c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
977c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
987c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
993143a2bfSChris Wilson 		POSTING_READ(reg);
1007c463586SKeith Packard 	}
1017c463586SKeith Packard }
1027c463586SKeith Packard 
1037c463586SKeith Packard void
1047c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1057c463586SKeith Packard {
1067c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
1079db4a9c7SJesse Barnes 		u32 reg = PIPESTAT(pipe);
1087c463586SKeith Packard 
1097c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
1107c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
1113143a2bfSChris Wilson 		POSTING_READ(reg);
1127c463586SKeith Packard 	}
1137c463586SKeith Packard }
1147c463586SKeith Packard 
115c0e09200SDave Airlie /**
11601c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
11701c66889SZhao Yakui  */
11801c66889SZhao Yakui void intel_enable_asle(struct drm_device *dev)
11901c66889SZhao Yakui {
1201ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1211ec14ad3SChris Wilson 	unsigned long irqflags;
1221ec14ad3SChris Wilson 
1237e231dbeSJesse Barnes 	/* FIXME: opregion/asle for VLV */
1247e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev))
1257e231dbeSJesse Barnes 		return;
1267e231dbeSJesse Barnes 
1271ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
12801c66889SZhao Yakui 
129c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
130f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
131edcb49caSZhao Yakui 	else {
13201c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
133d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
134a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen >= 4)
135edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
136d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
137edcb49caSZhao Yakui 	}
1381ec14ad3SChris Wilson 
1391ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14001c66889SZhao Yakui }
14101c66889SZhao Yakui 
14201c66889SZhao Yakui /**
1430a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1440a3e67a4SJesse Barnes  * @dev: DRM device
1450a3e67a4SJesse Barnes  * @pipe: pipe to check
1460a3e67a4SJesse Barnes  *
1470a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1480a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1490a3e67a4SJesse Barnes  * before reading such registers if unsure.
1500a3e67a4SJesse Barnes  */
1510a3e67a4SJesse Barnes static int
1520a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1530a3e67a4SJesse Barnes {
1540a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1555eddb70bSChris Wilson 	return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
1560a3e67a4SJesse Barnes }
1570a3e67a4SJesse Barnes 
15842f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
15942f52ef8SKeith Packard  * we use as a pipe index
16042f52ef8SKeith Packard  */
161f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1620a3e67a4SJesse Barnes {
1630a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1640a3e67a4SJesse Barnes 	unsigned long high_frame;
1650a3e67a4SJesse Barnes 	unsigned long low_frame;
1665eddb70bSChris Wilson 	u32 high1, high2, low;
1670a3e67a4SJesse Barnes 
1680a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
16944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
1709db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
1710a3e67a4SJesse Barnes 		return 0;
1720a3e67a4SJesse Barnes 	}
1730a3e67a4SJesse Barnes 
1749db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
1759db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
1765eddb70bSChris Wilson 
1770a3e67a4SJesse Barnes 	/*
1780a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
1790a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
1800a3e67a4SJesse Barnes 	 * register.
1810a3e67a4SJesse Barnes 	 */
1820a3e67a4SJesse Barnes 	do {
1835eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1845eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
1855eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1860a3e67a4SJesse Barnes 	} while (high1 != high2);
1870a3e67a4SJesse Barnes 
1885eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
1895eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
1905eddb70bSChris Wilson 	return (high1 << 8) | low;
1910a3e67a4SJesse Barnes }
1920a3e67a4SJesse Barnes 
193f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
1949880b7a5SJesse Barnes {
1959880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1969db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
1979880b7a5SJesse Barnes 
1989880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
19944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
2009db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
2019880b7a5SJesse Barnes 		return 0;
2029880b7a5SJesse Barnes 	}
2039880b7a5SJesse Barnes 
2049880b7a5SJesse Barnes 	return I915_READ(reg);
2059880b7a5SJesse Barnes }
2069880b7a5SJesse Barnes 
207f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
2080af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
2090af7e4dfSMario Kleiner {
2100af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2110af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
2120af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
2130af7e4dfSMario Kleiner 	bool in_vbl = true;
2140af7e4dfSMario Kleiner 	int ret = 0;
2150af7e4dfSMario Kleiner 
2160af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
2170af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
2189db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
2190af7e4dfSMario Kleiner 		return 0;
2200af7e4dfSMario Kleiner 	}
2210af7e4dfSMario Kleiner 
2220af7e4dfSMario Kleiner 	/* Get vtotal. */
2230af7e4dfSMario Kleiner 	vtotal = 1 + ((I915_READ(VTOTAL(pipe)) >> 16) & 0x1fff);
2240af7e4dfSMario Kleiner 
2250af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
2260af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
2270af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
2280af7e4dfSMario Kleiner 		 */
2290af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
2300af7e4dfSMario Kleiner 
2310af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
2320af7e4dfSMario Kleiner 		 * horizontal scanout position.
2330af7e4dfSMario Kleiner 		 */
2340af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
2350af7e4dfSMario Kleiner 		*hpos = 0;
2360af7e4dfSMario Kleiner 	} else {
2370af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
2380af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
2390af7e4dfSMario Kleiner 		 * scanout position.
2400af7e4dfSMario Kleiner 		 */
2410af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
2420af7e4dfSMario Kleiner 
2430af7e4dfSMario Kleiner 		htotal = 1 + ((I915_READ(HTOTAL(pipe)) >> 16) & 0x1fff);
2440af7e4dfSMario Kleiner 		*vpos = position / htotal;
2450af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
2460af7e4dfSMario Kleiner 	}
2470af7e4dfSMario Kleiner 
2480af7e4dfSMario Kleiner 	/* Query vblank area. */
2490af7e4dfSMario Kleiner 	vbl = I915_READ(VBLANK(pipe));
2500af7e4dfSMario Kleiner 
2510af7e4dfSMario Kleiner 	/* Test position against vblank region. */
2520af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
2530af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
2540af7e4dfSMario Kleiner 
2550af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
2560af7e4dfSMario Kleiner 		in_vbl = false;
2570af7e4dfSMario Kleiner 
2580af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
2590af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
2600af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
2610af7e4dfSMario Kleiner 
2620af7e4dfSMario Kleiner 	/* Readouts valid? */
2630af7e4dfSMario Kleiner 	if (vbl > 0)
2640af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
2650af7e4dfSMario Kleiner 
2660af7e4dfSMario Kleiner 	/* In vblank? */
2670af7e4dfSMario Kleiner 	if (in_vbl)
2680af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
2690af7e4dfSMario Kleiner 
2700af7e4dfSMario Kleiner 	return ret;
2710af7e4dfSMario Kleiner }
2720af7e4dfSMario Kleiner 
273f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
2740af7e4dfSMario Kleiner 			      int *max_error,
2750af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
2760af7e4dfSMario Kleiner 			      unsigned flags)
2770af7e4dfSMario Kleiner {
2784041b853SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2794041b853SChris Wilson 	struct drm_crtc *crtc;
2800af7e4dfSMario Kleiner 
2814041b853SChris Wilson 	if (pipe < 0 || pipe >= dev_priv->num_pipe) {
2824041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2830af7e4dfSMario Kleiner 		return -EINVAL;
2840af7e4dfSMario Kleiner 	}
2850af7e4dfSMario Kleiner 
2860af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
2874041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
2884041b853SChris Wilson 	if (crtc == NULL) {
2894041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2904041b853SChris Wilson 		return -EINVAL;
2914041b853SChris Wilson 	}
2924041b853SChris Wilson 
2934041b853SChris Wilson 	if (!crtc->enabled) {
2944041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
2954041b853SChris Wilson 		return -EBUSY;
2964041b853SChris Wilson 	}
2970af7e4dfSMario Kleiner 
2980af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
2994041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
3004041b853SChris Wilson 						     vblank_time, flags,
3014041b853SChris Wilson 						     crtc);
3020af7e4dfSMario Kleiner }
3030af7e4dfSMario Kleiner 
3045ca58282SJesse Barnes /*
3055ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
3065ca58282SJesse Barnes  */
3075ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
3085ca58282SJesse Barnes {
3095ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3105ca58282SJesse Barnes 						    hotplug_work);
3115ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
312c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
3134ef69c7aSChris Wilson 	struct intel_encoder *encoder;
3145ca58282SJesse Barnes 
315a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
316e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
317e67189abSJesse Barnes 
3184ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
3194ef69c7aSChris Wilson 		if (encoder->hot_plug)
3204ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
321c31c4ba3SKeith Packard 
32240ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
32340ee3381SKeith Packard 
3245ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
325eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3265ca58282SJesse Barnes }
3275ca58282SJesse Barnes 
328f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
329f97108d1SJesse Barnes {
330f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
331b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
332f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
333f97108d1SJesse Barnes 
3347648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
335b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
336b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
337f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
338f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
339f97108d1SJesse Barnes 
340f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
341b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
342f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
343f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
344f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
345f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
346b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
347f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
348f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
349f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
350f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
351f97108d1SJesse Barnes 	}
352f97108d1SJesse Barnes 
3537648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
354f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
355f97108d1SJesse Barnes 
356f97108d1SJesse Barnes 	return;
357f97108d1SJesse Barnes }
358f97108d1SJesse Barnes 
359549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
360549f7365SChris Wilson 			struct intel_ring_buffer *ring)
361549f7365SChris Wilson {
362549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
363475553deSChris Wilson 	u32 seqno;
3649862e600SChris Wilson 
365475553deSChris Wilson 	if (ring->obj == NULL)
366475553deSChris Wilson 		return;
367475553deSChris Wilson 
368475553deSChris Wilson 	seqno = ring->get_seqno(ring);
369db53a302SChris Wilson 	trace_i915_gem_request_complete(ring, seqno);
3709862e600SChris Wilson 
3719862e600SChris Wilson 	ring->irq_seqno = seqno;
372549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3733e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
374549f7365SChris Wilson 		dev_priv->hangcheck_count = 0;
375549f7365SChris Wilson 		mod_timer(&dev_priv->hangcheck_timer,
3763e0dc6b0SBen Widawsky 			  jiffies +
3773e0dc6b0SBen Widawsky 			  msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
3783e0dc6b0SBen Widawsky 	}
379549f7365SChris Wilson }
380549f7365SChris Wilson 
3814912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
3823b8d8d91SJesse Barnes {
3834912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3844912d041SBen Widawsky 						    rps_work);
3853b8d8d91SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
3864912d041SBen Widawsky 	u32 pm_iir, pm_imr;
3873b8d8d91SJesse Barnes 
3884912d041SBen Widawsky 	spin_lock_irq(&dev_priv->rps_lock);
3894912d041SBen Widawsky 	pm_iir = dev_priv->pm_iir;
3904912d041SBen Widawsky 	dev_priv->pm_iir = 0;
3914912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
392a9e2641dSDaniel Vetter 	I915_WRITE(GEN6_PMIMR, 0);
3934912d041SBen Widawsky 	spin_unlock_irq(&dev_priv->rps_lock);
3944912d041SBen Widawsky 
3953b8d8d91SJesse Barnes 	if (!pm_iir)
3963b8d8d91SJesse Barnes 		return;
3973b8d8d91SJesse Barnes 
3984912d041SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
3993b8d8d91SJesse Barnes 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
4003b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
4013b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
4023b8d8d91SJesse Barnes 		if (new_delay > dev_priv->max_delay)
4033b8d8d91SJesse Barnes 			new_delay = dev_priv->max_delay;
4043b8d8d91SJesse Barnes 	} else if (pm_iir & (GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT)) {
4054912d041SBen Widawsky 		gen6_gt_force_wake_get(dev_priv);
4063b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
4073b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
4083b8d8d91SJesse Barnes 		if (new_delay < dev_priv->min_delay) {
4093b8d8d91SJesse Barnes 			new_delay = dev_priv->min_delay;
4103b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4113b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) |
4123b8d8d91SJesse Barnes 				   ((new_delay << 16) & 0x3f0000));
4133b8d8d91SJesse Barnes 		} else {
4143b8d8d91SJesse Barnes 			/* Make sure we continue to get down interrupts
4153b8d8d91SJesse Barnes 			 * until we hit the minimum frequency */
4163b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4173b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) & ~0x3f0000);
4183b8d8d91SJesse Barnes 		}
4194912d041SBen Widawsky 		gen6_gt_force_wake_put(dev_priv);
4203b8d8d91SJesse Barnes 	}
4213b8d8d91SJesse Barnes 
4224912d041SBen Widawsky 	gen6_set_rps(dev_priv->dev, new_delay);
4233b8d8d91SJesse Barnes 	dev_priv->cur_delay = new_delay;
4243b8d8d91SJesse Barnes 
4254912d041SBen Widawsky 	/*
4264912d041SBen Widawsky 	 * rps_lock not held here because clearing is non-destructive. There is
4274912d041SBen Widawsky 	 * an *extremely* unlikely race with gen6_rps_enable() that is prevented
4284912d041SBen Widawsky 	 * by holding struct_mutex for the duration of the write.
4294912d041SBen Widawsky 	 */
4304912d041SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
4313b8d8d91SJesse Barnes }
4323b8d8d91SJesse Barnes 
433e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
434e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
435e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
436e7b4c6b1SDaniel Vetter {
437e7b4c6b1SDaniel Vetter 
438e7b4c6b1SDaniel Vetter 	if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
439e7b4c6b1SDaniel Vetter 		      GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
440e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
441e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BSD_USER_INTERRUPT)
442e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
443e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
444e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
445e7b4c6b1SDaniel Vetter 
446e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
447e7b4c6b1SDaniel Vetter 		      GT_GEN6_BSD_CS_ERROR_INTERRUPT |
448e7b4c6b1SDaniel Vetter 		      GT_RENDER_CS_ERROR_INTERRUPT)) {
449e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
450e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
451e7b4c6b1SDaniel Vetter 	}
452e7b4c6b1SDaniel Vetter }
453e7b4c6b1SDaniel Vetter 
4547e231dbeSJesse Barnes static irqreturn_t valleyview_irq_handler(DRM_IRQ_ARGS)
4557e231dbeSJesse Barnes {
4567e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
4577e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4587e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
4597e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
4607e231dbeSJesse Barnes 	unsigned long irqflags;
4617e231dbeSJesse Barnes 	int pipe;
4627e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
4637e231dbeSJesse Barnes 	u32 vblank_status;
4647e231dbeSJesse Barnes 	int vblank = 0;
4657e231dbeSJesse Barnes 	bool blc_event;
4667e231dbeSJesse Barnes 
4677e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
4687e231dbeSJesse Barnes 
4697e231dbeSJesse Barnes 	vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS |
4707e231dbeSJesse Barnes 		PIPE_VBLANK_INTERRUPT_STATUS;
4717e231dbeSJesse Barnes 
4727e231dbeSJesse Barnes 	while (true) {
4737e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
4747e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
4757e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
4767e231dbeSJesse Barnes 
4777e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
4787e231dbeSJesse Barnes 			goto out;
4797e231dbeSJesse Barnes 
4807e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
4817e231dbeSJesse Barnes 
482e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
4837e231dbeSJesse Barnes 
4847e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4857e231dbeSJesse Barnes 		for_each_pipe(pipe) {
4867e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
4877e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
4887e231dbeSJesse Barnes 
4897e231dbeSJesse Barnes 			/*
4907e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
4917e231dbeSJesse Barnes 			 */
4927e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
4937e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
4947e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
4957e231dbeSJesse Barnes 							 pipe_name(pipe));
4967e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
4977e231dbeSJesse Barnes 			}
4987e231dbeSJesse Barnes 		}
4997e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5007e231dbeSJesse Barnes 
5017e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
5027e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
5037e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
5047e231dbeSJesse Barnes 
5057e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
5067e231dbeSJesse Barnes 					 hotplug_status);
5077e231dbeSJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
5087e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
5097e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
5107e231dbeSJesse Barnes 
5117e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
5127e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
5137e231dbeSJesse Barnes 		}
5147e231dbeSJesse Barnes 
5157e231dbeSJesse Barnes 
5167e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT) {
5177e231dbeSJesse Barnes 			drm_handle_vblank(dev, 0);
5187e231dbeSJesse Barnes 			vblank++;
5197e231dbeSJesse Barnes 			if (!dev_priv->flip_pending_is_done) {
5207e231dbeSJesse Barnes 				intel_finish_page_flip(dev, 0);
5217e231dbeSJesse Barnes 			}
5227e231dbeSJesse Barnes 		}
5237e231dbeSJesse Barnes 
5247e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT) {
5257e231dbeSJesse Barnes 			drm_handle_vblank(dev, 1);
5267e231dbeSJesse Barnes 			vblank++;
5277e231dbeSJesse Barnes 			if (!dev_priv->flip_pending_is_done) {
5287e231dbeSJesse Barnes 				intel_finish_page_flip(dev, 0);
5297e231dbeSJesse Barnes 			}
5307e231dbeSJesse Barnes 		}
5317e231dbeSJesse Barnes 
5327e231dbeSJesse Barnes 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
5337e231dbeSJesse Barnes 			blc_event = true;
5347e231dbeSJesse Barnes 
5357e231dbeSJesse Barnes 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS) {
5367e231dbeSJesse Barnes 			unsigned long flags;
5377e231dbeSJesse Barnes 			spin_lock_irqsave(&dev_priv->rps_lock, flags);
5387e231dbeSJesse Barnes 			WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
5397e231dbeSJesse Barnes 			dev_priv->pm_iir |= pm_iir;
5407e231dbeSJesse Barnes 			I915_WRITE(GEN6_PMIMR, dev_priv->pm_iir);
5417e231dbeSJesse Barnes 			POSTING_READ(GEN6_PMIMR);
5427e231dbeSJesse Barnes 			spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
5437e231dbeSJesse Barnes 			queue_work(dev_priv->wq, &dev_priv->rps_work);
5447e231dbeSJesse Barnes 		}
5457e231dbeSJesse Barnes 
5467e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
5477e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
5487e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
5497e231dbeSJesse Barnes 	}
5507e231dbeSJesse Barnes 
5517e231dbeSJesse Barnes out:
5527e231dbeSJesse Barnes 	return ret;
5537e231dbeSJesse Barnes }
5547e231dbeSJesse Barnes 
555776ad806SJesse Barnes static void pch_irq_handler(struct drm_device *dev)
556776ad806SJesse Barnes {
557776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
558776ad806SJesse Barnes 	u32 pch_iir;
5599db4a9c7SJesse Barnes 	int pipe;
560776ad806SJesse Barnes 
561776ad806SJesse Barnes 	pch_iir = I915_READ(SDEIIR);
562776ad806SJesse Barnes 
563776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
564776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
565776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
566776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
567776ad806SJesse Barnes 
568776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
569776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
570776ad806SJesse Barnes 
571776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
572776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
573776ad806SJesse Barnes 
574776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
575776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
576776ad806SJesse Barnes 
577776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
578776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
579776ad806SJesse Barnes 
5809db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
5819db4a9c7SJesse Barnes 		for_each_pipe(pipe)
5829db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
5839db4a9c7SJesse Barnes 					 pipe_name(pipe),
5849db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
585776ad806SJesse Barnes 
586776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
587776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
588776ad806SJesse Barnes 
589776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
590776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
591776ad806SJesse Barnes 
592776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
593776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
594776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
595776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
596776ad806SJesse Barnes }
597776ad806SJesse Barnes 
598f71d4af4SJesse Barnes static irqreturn_t ivybridge_irq_handler(DRM_IRQ_ARGS)
599b1f14ad0SJesse Barnes {
600b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
601b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
602b1f14ad0SJesse Barnes 	int ret = IRQ_NONE;
603b1f14ad0SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
604b1f14ad0SJesse Barnes 	struct drm_i915_master_private *master_priv;
605b1f14ad0SJesse Barnes 
606b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
607b1f14ad0SJesse Barnes 
608b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
609b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
610b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
611b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
612b1f14ad0SJesse Barnes 
613b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
614b1f14ad0SJesse Barnes 	gt_iir = I915_READ(GTIIR);
615b1f14ad0SJesse Barnes 	pch_iir = I915_READ(SDEIIR);
616b1f14ad0SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
617b1f14ad0SJesse Barnes 
618b1f14ad0SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 && pm_iir == 0)
619b1f14ad0SJesse Barnes 		goto done;
620b1f14ad0SJesse Barnes 
621b1f14ad0SJesse Barnes 	ret = IRQ_HANDLED;
622b1f14ad0SJesse Barnes 
623b1f14ad0SJesse Barnes 	if (dev->primary->master) {
624b1f14ad0SJesse Barnes 		master_priv = dev->primary->master->driver_priv;
625b1f14ad0SJesse Barnes 		if (master_priv->sarea_priv)
626b1f14ad0SJesse Barnes 			master_priv->sarea_priv->last_dispatch =
627b1f14ad0SJesse Barnes 				READ_BREADCRUMB(dev_priv);
628b1f14ad0SJesse Barnes 	}
629b1f14ad0SJesse Barnes 
630e7b4c6b1SDaniel Vetter 	snb_gt_irq_handler(dev, dev_priv, gt_iir);
631b1f14ad0SJesse Barnes 
632b1f14ad0SJesse Barnes 	if (de_iir & DE_GSE_IVB)
633b1f14ad0SJesse Barnes 		intel_opregion_gse_intr(dev);
634b1f14ad0SJesse Barnes 
635b1f14ad0SJesse Barnes 	if (de_iir & DE_PLANEA_FLIP_DONE_IVB) {
636b1f14ad0SJesse Barnes 		intel_prepare_page_flip(dev, 0);
637b1f14ad0SJesse Barnes 		intel_finish_page_flip_plane(dev, 0);
638b1f14ad0SJesse Barnes 	}
639b1f14ad0SJesse Barnes 
640b1f14ad0SJesse Barnes 	if (de_iir & DE_PLANEB_FLIP_DONE_IVB) {
641b1f14ad0SJesse Barnes 		intel_prepare_page_flip(dev, 1);
642b1f14ad0SJesse Barnes 		intel_finish_page_flip_plane(dev, 1);
643b1f14ad0SJesse Barnes 	}
644b1f14ad0SJesse Barnes 
645b1f14ad0SJesse Barnes 	if (de_iir & DE_PIPEA_VBLANK_IVB)
646b1f14ad0SJesse Barnes 		drm_handle_vblank(dev, 0);
647b1f14ad0SJesse Barnes 
648f6b07f45SDan Carpenter 	if (de_iir & DE_PIPEB_VBLANK_IVB)
649b1f14ad0SJesse Barnes 		drm_handle_vblank(dev, 1);
650b1f14ad0SJesse Barnes 
651b1f14ad0SJesse Barnes 	/* check event from PCH */
652b1f14ad0SJesse Barnes 	if (de_iir & DE_PCH_EVENT_IVB) {
653b1f14ad0SJesse Barnes 		if (pch_iir & SDE_HOTPLUG_MASK_CPT)
654b1f14ad0SJesse Barnes 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
655b1f14ad0SJesse Barnes 		pch_irq_handler(dev);
656b1f14ad0SJesse Barnes 	}
657b1f14ad0SJesse Barnes 
658b1f14ad0SJesse Barnes 	if (pm_iir & GEN6_PM_DEFERRED_EVENTS) {
659b1f14ad0SJesse Barnes 		unsigned long flags;
660b1f14ad0SJesse Barnes 		spin_lock_irqsave(&dev_priv->rps_lock, flags);
661b1f14ad0SJesse Barnes 		WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
662b1f14ad0SJesse Barnes 		dev_priv->pm_iir |= pm_iir;
6634fb066abSDaniel Vetter 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_iir);
6644fb066abSDaniel Vetter 		POSTING_READ(GEN6_PMIMR);
665b1f14ad0SJesse Barnes 		spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
666b1f14ad0SJesse Barnes 		queue_work(dev_priv->wq, &dev_priv->rps_work);
667b1f14ad0SJesse Barnes 	}
668b1f14ad0SJesse Barnes 
669b1f14ad0SJesse Barnes 	/* should clear PCH hotplug event before clear CPU irq */
670b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, pch_iir);
671b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, gt_iir);
672b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, de_iir);
673b1f14ad0SJesse Barnes 	I915_WRITE(GEN6_PMIIR, pm_iir);
674b1f14ad0SJesse Barnes 
675b1f14ad0SJesse Barnes done:
676b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
677b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
678b1f14ad0SJesse Barnes 
679b1f14ad0SJesse Barnes 	return ret;
680b1f14ad0SJesse Barnes }
681b1f14ad0SJesse Barnes 
682e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
683e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
684e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
685e7b4c6b1SDaniel Vetter {
686e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
687e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
688e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
689e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
690e7b4c6b1SDaniel Vetter }
691e7b4c6b1SDaniel Vetter 
692f71d4af4SJesse Barnes static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
693036a4a7dSZhenyu Wang {
6944697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
695036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
696036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
6973b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
6982d7b8366SYuanhan Liu 	u32 hotplug_mask;
699036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
700881f47b6SXiang, Haihao 
7014697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
7024697995bSJesse Barnes 
7032d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
7042d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
7052d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
7063143a2bfSChris Wilson 	POSTING_READ(DEIER);
7072d109a84SZou, Nanhai 
708036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
709036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
710c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
7113b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
712036a4a7dSZhenyu Wang 
7133b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
7143b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
715c7c85101SZou Nan hai 		goto done;
716036a4a7dSZhenyu Wang 
7172d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
7182d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
7192d7b8366SYuanhan Liu 	else
7202d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
7212d7b8366SYuanhan Liu 
722036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
723036a4a7dSZhenyu Wang 
724036a4a7dSZhenyu Wang 	if (dev->primary->master) {
725036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
726036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
727036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
728036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
729036a4a7dSZhenyu Wang 	}
730036a4a7dSZhenyu Wang 
731e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
732e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
733e7b4c6b1SDaniel Vetter 	else
734e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
735036a4a7dSZhenyu Wang 
73601c66889SZhao Yakui 	if (de_iir & DE_GSE)
7373b617967SChris Wilson 		intel_opregion_gse_intr(dev);
73801c66889SZhao Yakui 
739f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
740013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
7412bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
742013d5aa2SJesse Barnes 	}
743013d5aa2SJesse Barnes 
744f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
745f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
7462bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
747013d5aa2SJesse Barnes 	}
748c062df61SLi Peng 
749f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
750f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
751f072d2e7SZhenyu Wang 
752f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
753f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
754f072d2e7SZhenyu Wang 
755c650156aSZhenyu Wang 	/* check event from PCH */
756776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
757776ad806SJesse Barnes 		if (pch_iir & hotplug_mask)
758c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
759776ad806SJesse Barnes 		pch_irq_handler(dev);
760776ad806SJesse Barnes 	}
761c650156aSZhenyu Wang 
762f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
7637648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
764f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
765f97108d1SJesse Barnes 	}
766f97108d1SJesse Barnes 
7674912d041SBen Widawsky 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS) {
7684912d041SBen Widawsky 		/*
7694912d041SBen Widawsky 		 * IIR bits should never already be set because IMR should
7704912d041SBen Widawsky 		 * prevent an interrupt from being shown in IIR. The warning
7714912d041SBen Widawsky 		 * displays a case where we've unsafely cleared
7724912d041SBen Widawsky 		 * dev_priv->pm_iir. Although missing an interrupt of the same
7734912d041SBen Widawsky 		 * type is not a problem, it displays a problem in the logic.
7744912d041SBen Widawsky 		 *
7754912d041SBen Widawsky 		 * The mask bit in IMR is cleared by rps_work.
7764912d041SBen Widawsky 		 */
7774912d041SBen Widawsky 		unsigned long flags;
7784912d041SBen Widawsky 		spin_lock_irqsave(&dev_priv->rps_lock, flags);
7794912d041SBen Widawsky 		WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
7804912d041SBen Widawsky 		dev_priv->pm_iir |= pm_iir;
7814fb066abSDaniel Vetter 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_iir);
7824fb066abSDaniel Vetter 		POSTING_READ(GEN6_PMIMR);
7834912d041SBen Widawsky 		spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
7844912d041SBen Widawsky 		queue_work(dev_priv->wq, &dev_priv->rps_work);
7854912d041SBen Widawsky 	}
7863b8d8d91SJesse Barnes 
787c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
788c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
789c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
790c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
7914912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
792036a4a7dSZhenyu Wang 
793c7c85101SZou Nan hai done:
7942d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
7953143a2bfSChris Wilson 	POSTING_READ(DEIER);
7962d109a84SZou, Nanhai 
797036a4a7dSZhenyu Wang 	return ret;
798036a4a7dSZhenyu Wang }
799036a4a7dSZhenyu Wang 
8008a905236SJesse Barnes /**
8018a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
8028a905236SJesse Barnes  * @work: work struct
8038a905236SJesse Barnes  *
8048a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
8058a905236SJesse Barnes  * was detected.
8068a905236SJesse Barnes  */
8078a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
8088a905236SJesse Barnes {
8098a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
8108a905236SJesse Barnes 						    error_work);
8118a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
812f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
813f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
814f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
8158a905236SJesse Barnes 
816f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
8178a905236SJesse Barnes 
818ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
81944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
820f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
821f803aa55SChris Wilson 		if (!i915_reset(dev, GRDOM_RENDER)) {
822ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
823f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
824f316a42cSBen Gamari 		}
82530dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
826f316a42cSBen Gamari 	}
8278a905236SJesse Barnes }
8288a905236SJesse Barnes 
8293bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
8309df30794SChris Wilson static struct drm_i915_error_object *
831bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
83205394f39SChris Wilson 			 struct drm_i915_gem_object *src)
8339df30794SChris Wilson {
8349df30794SChris Wilson 	struct drm_i915_error_object *dst;
8359df30794SChris Wilson 	int page, page_count;
836e56660ddSChris Wilson 	u32 reloc_offset;
8379df30794SChris Wilson 
83805394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
8399df30794SChris Wilson 		return NULL;
8409df30794SChris Wilson 
84105394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
8429df30794SChris Wilson 
8439df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof(u32 *), GFP_ATOMIC);
8449df30794SChris Wilson 	if (dst == NULL)
8459df30794SChris Wilson 		return NULL;
8469df30794SChris Wilson 
84705394f39SChris Wilson 	reloc_offset = src->gtt_offset;
8489df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
849788885aeSAndrew Morton 		unsigned long flags;
850e56660ddSChris Wilson 		void *d;
851788885aeSAndrew Morton 
852e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
8539df30794SChris Wilson 		if (d == NULL)
8549df30794SChris Wilson 			goto unwind;
855e56660ddSChris Wilson 
856788885aeSAndrew Morton 		local_irq_save(flags);
85774898d7eSDaniel Vetter 		if (reloc_offset < dev_priv->mm.gtt_mappable_end &&
85874898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
859172975aaSChris Wilson 			void __iomem *s;
860172975aaSChris Wilson 
861172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
862172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
863172975aaSChris Wilson 			 * captures what the GPU read.
864172975aaSChris Wilson 			 */
865172975aaSChris Wilson 
866e56660ddSChris Wilson 			s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
8673e4d3af5SPeter Zijlstra 						     reloc_offset);
868e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
8693e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
870172975aaSChris Wilson 		} else {
871172975aaSChris Wilson 			void *s;
872172975aaSChris Wilson 
873172975aaSChris Wilson 			drm_clflush_pages(&src->pages[page], 1);
874172975aaSChris Wilson 
875172975aaSChris Wilson 			s = kmap_atomic(src->pages[page]);
876172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
877172975aaSChris Wilson 			kunmap_atomic(s);
878172975aaSChris Wilson 
879172975aaSChris Wilson 			drm_clflush_pages(&src->pages[page], 1);
880172975aaSChris Wilson 		}
881788885aeSAndrew Morton 		local_irq_restore(flags);
882e56660ddSChris Wilson 
8839df30794SChris Wilson 		dst->pages[page] = d;
884e56660ddSChris Wilson 
885e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
8869df30794SChris Wilson 	}
8879df30794SChris Wilson 	dst->page_count = page_count;
88805394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
8899df30794SChris Wilson 
8909df30794SChris Wilson 	return dst;
8919df30794SChris Wilson 
8929df30794SChris Wilson unwind:
8939df30794SChris Wilson 	while (page--)
8949df30794SChris Wilson 		kfree(dst->pages[page]);
8959df30794SChris Wilson 	kfree(dst);
8969df30794SChris Wilson 	return NULL;
8979df30794SChris Wilson }
8989df30794SChris Wilson 
8999df30794SChris Wilson static void
9009df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
9019df30794SChris Wilson {
9029df30794SChris Wilson 	int page;
9039df30794SChris Wilson 
9049df30794SChris Wilson 	if (obj == NULL)
9059df30794SChris Wilson 		return;
9069df30794SChris Wilson 
9079df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
9089df30794SChris Wilson 		kfree(obj->pages[page]);
9099df30794SChris Wilson 
9109df30794SChris Wilson 	kfree(obj);
9119df30794SChris Wilson }
9129df30794SChris Wilson 
9139df30794SChris Wilson static void
9149df30794SChris Wilson i915_error_state_free(struct drm_device *dev,
9159df30794SChris Wilson 		      struct drm_i915_error_state *error)
9169df30794SChris Wilson {
917e2f973d5SChris Wilson 	int i;
918e2f973d5SChris Wilson 
91952d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
92052d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
92152d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
92252d39a21SChris Wilson 		kfree(error->ring[i].requests);
92352d39a21SChris Wilson 	}
924e2f973d5SChris Wilson 
9259df30794SChris Wilson 	kfree(error->active_bo);
9266ef3d427SChris Wilson 	kfree(error->overlay);
9279df30794SChris Wilson 	kfree(error);
9289df30794SChris Wilson }
9299df30794SChris Wilson 
930c724e8a9SChris Wilson static u32 capture_bo_list(struct drm_i915_error_buffer *err,
931c724e8a9SChris Wilson 			   int count,
932c724e8a9SChris Wilson 			   struct list_head *head)
933c724e8a9SChris Wilson {
934c724e8a9SChris Wilson 	struct drm_i915_gem_object *obj;
935c724e8a9SChris Wilson 	int i = 0;
936c724e8a9SChris Wilson 
937c724e8a9SChris Wilson 	list_for_each_entry(obj, head, mm_list) {
938c724e8a9SChris Wilson 		err->size = obj->base.size;
939c724e8a9SChris Wilson 		err->name = obj->base.name;
940c724e8a9SChris Wilson 		err->seqno = obj->last_rendering_seqno;
941c724e8a9SChris Wilson 		err->gtt_offset = obj->gtt_offset;
942c724e8a9SChris Wilson 		err->read_domains = obj->base.read_domains;
943c724e8a9SChris Wilson 		err->write_domain = obj->base.write_domain;
944c724e8a9SChris Wilson 		err->fence_reg = obj->fence_reg;
945c724e8a9SChris Wilson 		err->pinned = 0;
946c724e8a9SChris Wilson 		if (obj->pin_count > 0)
947c724e8a9SChris Wilson 			err->pinned = 1;
948c724e8a9SChris Wilson 		if (obj->user_pin_count > 0)
949c724e8a9SChris Wilson 			err->pinned = -1;
950c724e8a9SChris Wilson 		err->tiling = obj->tiling_mode;
951c724e8a9SChris Wilson 		err->dirty = obj->dirty;
952c724e8a9SChris Wilson 		err->purgeable = obj->madv != I915_MADV_WILLNEED;
95396154f2fSDaniel Vetter 		err->ring = obj->ring ? obj->ring->id : -1;
95493dfb40cSChris Wilson 		err->cache_level = obj->cache_level;
955c724e8a9SChris Wilson 
956c724e8a9SChris Wilson 		if (++i == count)
957c724e8a9SChris Wilson 			break;
958c724e8a9SChris Wilson 
959c724e8a9SChris Wilson 		err++;
960c724e8a9SChris Wilson 	}
961c724e8a9SChris Wilson 
962c724e8a9SChris Wilson 	return i;
963c724e8a9SChris Wilson }
964c724e8a9SChris Wilson 
965748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
966748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
967748ebc60SChris Wilson {
968748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
969748ebc60SChris Wilson 	int i;
970748ebc60SChris Wilson 
971748ebc60SChris Wilson 	/* Fences */
972748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
973775d17b6SDaniel Vetter 	case 7:
974748ebc60SChris Wilson 	case 6:
975748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
976748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
977748ebc60SChris Wilson 		break;
978748ebc60SChris Wilson 	case 5:
979748ebc60SChris Wilson 	case 4:
980748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
981748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
982748ebc60SChris Wilson 		break;
983748ebc60SChris Wilson 	case 3:
984748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
985748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
986748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
987748ebc60SChris Wilson 	case 2:
988748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
989748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
990748ebc60SChris Wilson 		break;
991748ebc60SChris Wilson 
992748ebc60SChris Wilson 	}
993748ebc60SChris Wilson }
994748ebc60SChris Wilson 
995bcfb2e28SChris Wilson static struct drm_i915_error_object *
996bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
997bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
998bcfb2e28SChris Wilson {
999bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
1000bcfb2e28SChris Wilson 	u32 seqno;
1001bcfb2e28SChris Wilson 
1002bcfb2e28SChris Wilson 	if (!ring->get_seqno)
1003bcfb2e28SChris Wilson 		return NULL;
1004bcfb2e28SChris Wilson 
1005bcfb2e28SChris Wilson 	seqno = ring->get_seqno(ring);
1006bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1007bcfb2e28SChris Wilson 		if (obj->ring != ring)
1008bcfb2e28SChris Wilson 			continue;
1009bcfb2e28SChris Wilson 
1010c37d9a5dSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
1011bcfb2e28SChris Wilson 			continue;
1012bcfb2e28SChris Wilson 
1013bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1014bcfb2e28SChris Wilson 			continue;
1015bcfb2e28SChris Wilson 
1016bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1017bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1018bcfb2e28SChris Wilson 		 */
1019bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1020bcfb2e28SChris Wilson 	}
1021bcfb2e28SChris Wilson 
1022bcfb2e28SChris Wilson 	return NULL;
1023bcfb2e28SChris Wilson }
1024bcfb2e28SChris Wilson 
1025d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1026d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1027d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1028d27b1e0eSDaniel Vetter {
1029d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1030d27b1e0eSDaniel Vetter 
103133f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
103233f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
10337e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
10347e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
10357e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
10367e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
103733f3f518SDaniel Vetter 	}
1038c1cd90edSDaniel Vetter 
1039d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
10409d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1041d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1042d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1043d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1044c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1045d27b1e0eSDaniel Vetter 		if (ring->id == RCS) {
1046d27b1e0eSDaniel Vetter 			error->instdone1 = I915_READ(INSTDONE1);
1047d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1048d27b1e0eSDaniel Vetter 		}
1049d27b1e0eSDaniel Vetter 	} else {
10509d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1051d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1052d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1053d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1054d27b1e0eSDaniel Vetter 	}
1055d27b1e0eSDaniel Vetter 
1056c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1057d27b1e0eSDaniel Vetter 	error->seqno[ring->id] = ring->get_seqno(ring);
1058d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1059c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1060c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
10617e3b8737SDaniel Vetter 
10627e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
10637e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1064d27b1e0eSDaniel Vetter }
1065d27b1e0eSDaniel Vetter 
106652d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
106752d39a21SChris Wilson 				  struct drm_i915_error_state *error)
106852d39a21SChris Wilson {
106952d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
107052d39a21SChris Wilson 	struct drm_i915_gem_request *request;
107152d39a21SChris Wilson 	int i, count;
107252d39a21SChris Wilson 
107352d39a21SChris Wilson 	for (i = 0; i < I915_NUM_RINGS; i++) {
107452d39a21SChris Wilson 		struct intel_ring_buffer *ring = &dev_priv->ring[i];
107552d39a21SChris Wilson 
107652d39a21SChris Wilson 		if (ring->obj == NULL)
107752d39a21SChris Wilson 			continue;
107852d39a21SChris Wilson 
107952d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
108052d39a21SChris Wilson 
108152d39a21SChris Wilson 		error->ring[i].batchbuffer =
108252d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
108352d39a21SChris Wilson 
108452d39a21SChris Wilson 		error->ring[i].ringbuffer =
108552d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
108652d39a21SChris Wilson 
108752d39a21SChris Wilson 		count = 0;
108852d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
108952d39a21SChris Wilson 			count++;
109052d39a21SChris Wilson 
109152d39a21SChris Wilson 		error->ring[i].num_requests = count;
109252d39a21SChris Wilson 		error->ring[i].requests =
109352d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
109452d39a21SChris Wilson 				GFP_ATOMIC);
109552d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
109652d39a21SChris Wilson 			error->ring[i].num_requests = 0;
109752d39a21SChris Wilson 			continue;
109852d39a21SChris Wilson 		}
109952d39a21SChris Wilson 
110052d39a21SChris Wilson 		count = 0;
110152d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
110252d39a21SChris Wilson 			struct drm_i915_error_request *erq;
110352d39a21SChris Wilson 
110452d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
110552d39a21SChris Wilson 			erq->seqno = request->seqno;
110652d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1107ee4f42b1SChris Wilson 			erq->tail = request->tail;
110852d39a21SChris Wilson 		}
110952d39a21SChris Wilson 	}
111052d39a21SChris Wilson }
111152d39a21SChris Wilson 
11128a905236SJesse Barnes /**
11138a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
11148a905236SJesse Barnes  * @dev: drm device
11158a905236SJesse Barnes  *
11168a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
11178a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
11188a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
11198a905236SJesse Barnes  * to pick up.
11208a905236SJesse Barnes  */
112163eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
112263eeaf38SJesse Barnes {
112363eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
112405394f39SChris Wilson 	struct drm_i915_gem_object *obj;
112563eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
112663eeaf38SJesse Barnes 	unsigned long flags;
11279db4a9c7SJesse Barnes 	int i, pipe;
112863eeaf38SJesse Barnes 
112963eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
11309df30794SChris Wilson 	error = dev_priv->first_error;
11319df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
11329df30794SChris Wilson 	if (error)
11339df30794SChris Wilson 		return;
113463eeaf38SJesse Barnes 
11359db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
113633f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
113763eeaf38SJesse Barnes 	if (!error) {
11389df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
11399df30794SChris Wilson 		return;
114063eeaf38SJesse Barnes 	}
114163eeaf38SJesse Barnes 
1142b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
1143b6f7833bSChris Wilson 		 dev->primary->index);
11442fa772f3SChris Wilson 
114563eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
114663eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
11479db4a9c7SJesse Barnes 	for_each_pipe(pipe)
11489db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1149d27b1e0eSDaniel Vetter 
115033f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1151f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
115233f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
115333f3f518SDaniel Vetter 	}
1154add354ddSChris Wilson 
1155748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
115652d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
11579df30794SChris Wilson 
1158c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
11599df30794SChris Wilson 	error->active_bo = NULL;
1160c724e8a9SChris Wilson 	error->pinned_bo = NULL;
11619df30794SChris Wilson 
1162bcfb2e28SChris Wilson 	i = 0;
1163bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1164bcfb2e28SChris Wilson 		i++;
1165bcfb2e28SChris Wilson 	error->active_bo_count = i;
116605394f39SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
1167bcfb2e28SChris Wilson 		i++;
1168bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1169c724e8a9SChris Wilson 
11708e934dbfSChris Wilson 	error->active_bo = NULL;
11718e934dbfSChris Wilson 	error->pinned_bo = NULL;
1172bcfb2e28SChris Wilson 	if (i) {
1173bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
11749df30794SChris Wilson 					   GFP_ATOMIC);
1175c724e8a9SChris Wilson 		if (error->active_bo)
1176c724e8a9SChris Wilson 			error->pinned_bo =
1177c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
11789df30794SChris Wilson 	}
1179c724e8a9SChris Wilson 
1180c724e8a9SChris Wilson 	if (error->active_bo)
1181c724e8a9SChris Wilson 		error->active_bo_count =
1182c724e8a9SChris Wilson 			capture_bo_list(error->active_bo,
1183c724e8a9SChris Wilson 					error->active_bo_count,
1184c724e8a9SChris Wilson 					&dev_priv->mm.active_list);
1185c724e8a9SChris Wilson 
1186c724e8a9SChris Wilson 	if (error->pinned_bo)
1187c724e8a9SChris Wilson 		error->pinned_bo_count =
1188c724e8a9SChris Wilson 			capture_bo_list(error->pinned_bo,
1189c724e8a9SChris Wilson 					error->pinned_bo_count,
1190c724e8a9SChris Wilson 					&dev_priv->mm.pinned_list);
119163eeaf38SJesse Barnes 
11928a905236SJesse Barnes 	do_gettimeofday(&error->time);
11938a905236SJesse Barnes 
11946ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1195c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
11966ef3d427SChris Wilson 
11979df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
11989df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
119963eeaf38SJesse Barnes 		dev_priv->first_error = error;
12009df30794SChris Wilson 		error = NULL;
12019df30794SChris Wilson 	}
120263eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
12039df30794SChris Wilson 
12049df30794SChris Wilson 	if (error)
12059df30794SChris Wilson 		i915_error_state_free(dev, error);
12069df30794SChris Wilson }
12079df30794SChris Wilson 
12089df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
12099df30794SChris Wilson {
12109df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
12119df30794SChris Wilson 	struct drm_i915_error_state *error;
12126dc0e816SBen Widawsky 	unsigned long flags;
12139df30794SChris Wilson 
12146dc0e816SBen Widawsky 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12159df30794SChris Wilson 	error = dev_priv->first_error;
12169df30794SChris Wilson 	dev_priv->first_error = NULL;
12176dc0e816SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
12189df30794SChris Wilson 
12199df30794SChris Wilson 	if (error)
12209df30794SChris Wilson 		i915_error_state_free(dev, error);
122163eeaf38SJesse Barnes }
12223bd3c932SChris Wilson #else
12233bd3c932SChris Wilson #define i915_capture_error_state(x)
12243bd3c932SChris Wilson #endif
122563eeaf38SJesse Barnes 
122635aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1227c0e09200SDave Airlie {
12288a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
122963eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
12309db4a9c7SJesse Barnes 	int pipe;
123163eeaf38SJesse Barnes 
123235aed2e6SChris Wilson 	if (!eir)
123335aed2e6SChris Wilson 		return;
123463eeaf38SJesse Barnes 
1235a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
12368a905236SJesse Barnes 
12378a905236SJesse Barnes 	if (IS_G4X(dev)) {
12388a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
12398a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
12408a905236SJesse Barnes 
1241a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1242a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1243a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n",
12448a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
1245a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1246a70491ccSJoe Perches 			pr_err("  INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1247a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
12488a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
12493143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
12508a905236SJesse Barnes 		}
12518a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
12528a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1253a70491ccSJoe Perches 			pr_err("page table error\n");
1254a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
12558a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
12563143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
12578a905236SJesse Barnes 		}
12588a905236SJesse Barnes 	}
12598a905236SJesse Barnes 
1260a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
126163eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
126263eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1263a70491ccSJoe Perches 			pr_err("page table error\n");
1264a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
126563eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
12663143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
126763eeaf38SJesse Barnes 		}
12688a905236SJesse Barnes 	}
12698a905236SJesse Barnes 
127063eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1271a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
12729db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1273a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
12749db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
127563eeaf38SJesse Barnes 		/* pipestat has already been acked */
127663eeaf38SJesse Barnes 	}
127763eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1278a70491ccSJoe Perches 		pr_err("instruction error\n");
1279a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1280a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
128163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
128263eeaf38SJesse Barnes 
1283a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1284a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1285a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n", I915_READ(INSTDONE));
1286a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
128763eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
12883143a2bfSChris Wilson 			POSTING_READ(IPEIR);
128963eeaf38SJesse Barnes 		} else {
129063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
129163eeaf38SJesse Barnes 
1292a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1293a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1294a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n",
129563eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
1296a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1297a70491ccSJoe Perches 			pr_err("  INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1298a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
129963eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
13003143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
130163eeaf38SJesse Barnes 		}
130263eeaf38SJesse Barnes 	}
130363eeaf38SJesse Barnes 
130463eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
13053143a2bfSChris Wilson 	POSTING_READ(EIR);
130663eeaf38SJesse Barnes 	eir = I915_READ(EIR);
130763eeaf38SJesse Barnes 	if (eir) {
130863eeaf38SJesse Barnes 		/*
130963eeaf38SJesse Barnes 		 * some errors might have become stuck,
131063eeaf38SJesse Barnes 		 * mask them.
131163eeaf38SJesse Barnes 		 */
131263eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
131363eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
131463eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
131563eeaf38SJesse Barnes 	}
131635aed2e6SChris Wilson }
131735aed2e6SChris Wilson 
131835aed2e6SChris Wilson /**
131935aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
132035aed2e6SChris Wilson  * @dev: drm device
132135aed2e6SChris Wilson  *
132235aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
132335aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
132435aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
132535aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
132635aed2e6SChris Wilson  * of a ring dump etc.).
132735aed2e6SChris Wilson  */
1328527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
132935aed2e6SChris Wilson {
133035aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
133135aed2e6SChris Wilson 
133235aed2e6SChris Wilson 	i915_capture_error_state(dev);
133335aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
13348a905236SJesse Barnes 
1335ba1234d1SBen Gamari 	if (wedged) {
133630dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1337ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1338ba1234d1SBen Gamari 
133911ed50ecSBen Gamari 		/*
134011ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
134111ed50ecSBen Gamari 		 */
13421ec14ad3SChris Wilson 		wake_up_all(&dev_priv->ring[RCS].irq_queue);
1343f787a5f5SChris Wilson 		if (HAS_BSD(dev))
13441ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[VCS].irq_queue);
1345549f7365SChris Wilson 		if (HAS_BLT(dev))
13461ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[BCS].irq_queue);
134711ed50ecSBen Gamari 	}
134811ed50ecSBen Gamari 
13499c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
13508a905236SJesse Barnes }
13518a905236SJesse Barnes 
13524e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
13534e5359cdSSimon Farnsworth {
13544e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
13554e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13564e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
135705394f39SChris Wilson 	struct drm_i915_gem_object *obj;
13584e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
13594e5359cdSSimon Farnsworth 	unsigned long flags;
13604e5359cdSSimon Farnsworth 	bool stall_detected;
13614e5359cdSSimon Farnsworth 
13624e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
13634e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
13644e5359cdSSimon Farnsworth 		return;
13654e5359cdSSimon Farnsworth 
13664e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
13674e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
13684e5359cdSSimon Farnsworth 
13694e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
13704e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
13714e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
13724e5359cdSSimon Farnsworth 		return;
13734e5359cdSSimon Farnsworth 	}
13744e5359cdSSimon Farnsworth 
13754e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
137605394f39SChris Wilson 	obj = work->pending_flip_obj;
1377a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
13789db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
137905394f39SChris Wilson 		stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
13804e5359cdSSimon Farnsworth 	} else {
13819db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
138205394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
138301f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
13844e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
13854e5359cdSSimon Farnsworth 	}
13864e5359cdSSimon Farnsworth 
13874e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
13884e5359cdSSimon Farnsworth 
13894e5359cdSSimon Farnsworth 	if (stall_detected) {
13904e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
13914e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
13924e5359cdSSimon Farnsworth 	}
13934e5359cdSSimon Farnsworth }
13944e5359cdSSimon Farnsworth 
1395f71d4af4SJesse Barnes static irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
13968a905236SJesse Barnes {
13978a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
13988a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13998a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
14008a905236SJesse Barnes 	u32 iir, new_iir;
14019db4a9c7SJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
14028a905236SJesse Barnes 	u32 vblank_status;
14038a905236SJesse Barnes 	int vblank = 0;
14048a905236SJesse Barnes 	unsigned long irqflags;
14058a905236SJesse Barnes 	int irq_received;
14069db4a9c7SJesse Barnes 	int ret = IRQ_NONE, pipe;
14079db4a9c7SJesse Barnes 	bool blc_event = false;
14088a905236SJesse Barnes 
14098a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
14108a905236SJesse Barnes 
14118a905236SJesse Barnes 	iir = I915_READ(IIR);
14128a905236SJesse Barnes 
1413a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
1414d874bcffSJesse Barnes 		vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
1415e25e6601SJesse Barnes 	else
1416d874bcffSJesse Barnes 		vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
14178a905236SJesse Barnes 
14188a905236SJesse Barnes 	for (;;) {
14198a905236SJesse Barnes 		irq_received = iir != 0;
14208a905236SJesse Barnes 
14218a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
14228a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
14238a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
14248a905236SJesse Barnes 		 * interrupts (for non-MSI).
14258a905236SJesse Barnes 		 */
14261ec14ad3SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
14278a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
1428ba1234d1SBen Gamari 			i915_handle_error(dev, false);
14298a905236SJesse Barnes 
14309db4a9c7SJesse Barnes 		for_each_pipe(pipe) {
14319db4a9c7SJesse Barnes 			int reg = PIPESTAT(pipe);
14329db4a9c7SJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
14339db4a9c7SJesse Barnes 
14348a905236SJesse Barnes 			/*
14359db4a9c7SJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
14368a905236SJesse Barnes 			 */
14379db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
14389db4a9c7SJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
14399db4a9c7SJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
14409db4a9c7SJesse Barnes 							 pipe_name(pipe));
14419db4a9c7SJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
14428a905236SJesse Barnes 				irq_received = 1;
14438a905236SJesse Barnes 			}
14448a905236SJesse Barnes 		}
14451ec14ad3SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
14468a905236SJesse Barnes 
14478a905236SJesse Barnes 		if (!irq_received)
14488a905236SJesse Barnes 			break;
14498a905236SJesse Barnes 
14508a905236SJesse Barnes 		ret = IRQ_HANDLED;
14518a905236SJesse Barnes 
14528a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
14538a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
14548a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
14558a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
14568a905236SJesse Barnes 
145744d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
14588a905236SJesse Barnes 				  hotplug_status);
14598a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
14609c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
14619c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
14628a905236SJesse Barnes 
14638a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
14648a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
146563eeaf38SJesse Barnes 		}
146663eeaf38SJesse Barnes 
1467673a394bSEric Anholt 		I915_WRITE(IIR, iir);
1468cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
14697c463586SKeith Packard 
14707c1c2871SDave Airlie 		if (dev->primary->master) {
14717c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
14727c1c2871SDave Airlie 			if (master_priv->sarea_priv)
14737c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
1474c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
14757c1c2871SDave Airlie 		}
14760a3e67a4SJesse Barnes 
1477549f7365SChris Wilson 		if (iir & I915_USER_INTERRUPT)
14781ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
14791ec14ad3SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
14801ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
1481d1b851fcSZou Nan hai 
14821afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
14836b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
14841afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
14851afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 0);
14861afe3e9dSJesse Barnes 		}
14876b95a207SKristian Høgsberg 
14881afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
148970565d00SJesse Barnes 			intel_prepare_page_flip(dev, 1);
14901afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
14911afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 1);
14921afe3e9dSJesse Barnes 		}
14936b95a207SKristian Høgsberg 
14949db4a9c7SJesse Barnes 		for_each_pipe(pipe) {
14959db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & vblank_status &&
14969db4a9c7SJesse Barnes 			    drm_handle_vblank(dev, pipe)) {
14977c463586SKeith Packard 				vblank++;
14984e5359cdSSimon Farnsworth 				if (!dev_priv->flip_pending_is_done) {
14999db4a9c7SJesse Barnes 					i915_pageflip_stall_check(dev, pipe);
15009db4a9c7SJesse Barnes 					intel_finish_page_flip(dev, pipe);
15017c463586SKeith Packard 				}
15024e5359cdSSimon Farnsworth 			}
15037c463586SKeith Packard 
15049db4a9c7SJesse Barnes 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
15059db4a9c7SJesse Barnes 				blc_event = true;
15064e5359cdSSimon Farnsworth 		}
15077c463586SKeith Packard 
15089db4a9c7SJesse Barnes 
15099db4a9c7SJesse Barnes 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
15103b617967SChris Wilson 			intel_opregion_asle_intr(dev);
15110a3e67a4SJesse Barnes 
1512cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
1513cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
1514cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
1515cdfbc41fSEric Anholt 		 * we would never get another interrupt.
1516cdfbc41fSEric Anholt 		 *
1517cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
1518cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
1519cdfbc41fSEric Anholt 		 * another one.
1520cdfbc41fSEric Anholt 		 *
1521cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
1522cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
1523cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
1524cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
1525cdfbc41fSEric Anholt 		 * stray interrupts.
1526cdfbc41fSEric Anholt 		 */
1527cdfbc41fSEric Anholt 		iir = new_iir;
152805eff845SKeith Packard 	}
1529cdfbc41fSEric Anholt 
153005eff845SKeith Packard 	return ret;
1531c0e09200SDave Airlie }
1532c0e09200SDave Airlie 
1533c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
1534c0e09200SDave Airlie {
1535c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
15367c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1537c0e09200SDave Airlie 
1538c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
1539c0e09200SDave Airlie 
154044d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
1541c0e09200SDave Airlie 
1542c99b058fSKristian Høgsberg 	dev_priv->counter++;
1543c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
1544c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
15457c1c2871SDave Airlie 	if (master_priv->sarea_priv)
15467c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
1547c0e09200SDave Airlie 
1548e1f99ce6SChris Wilson 	if (BEGIN_LP_RING(4) == 0) {
1549585fb111SJesse Barnes 		OUT_RING(MI_STORE_DWORD_INDEX);
15500baf823aSKeith Packard 		OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1551c0e09200SDave Airlie 		OUT_RING(dev_priv->counter);
1552585fb111SJesse Barnes 		OUT_RING(MI_USER_INTERRUPT);
1553c0e09200SDave Airlie 		ADVANCE_LP_RING();
1554e1f99ce6SChris Wilson 	}
1555c0e09200SDave Airlie 
1556c0e09200SDave Airlie 	return dev_priv->counter;
1557c0e09200SDave Airlie }
1558c0e09200SDave Airlie 
1559c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
1560c0e09200SDave Airlie {
1561c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
15627c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1563c0e09200SDave Airlie 	int ret = 0;
15641ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
1565c0e09200SDave Airlie 
156644d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
1567c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
1568c0e09200SDave Airlie 
1569ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
15707c1c2871SDave Airlie 		if (master_priv->sarea_priv)
15717c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
1572c0e09200SDave Airlie 		return 0;
1573ed4cb414SEric Anholt 	}
1574c0e09200SDave Airlie 
15757c1c2871SDave Airlie 	if (master_priv->sarea_priv)
15767c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1577c0e09200SDave Airlie 
1578b13c2b96SChris Wilson 	if (ring->irq_get(ring)) {
15791ec14ad3SChris Wilson 		DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1580c0e09200SDave Airlie 			    READ_BREADCRUMB(dev_priv) >= irq_nr);
15811ec14ad3SChris Wilson 		ring->irq_put(ring);
15825a9a8d1aSChris Wilson 	} else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
15835a9a8d1aSChris Wilson 		ret = -EBUSY;
1584c0e09200SDave Airlie 
1585c0e09200SDave Airlie 	if (ret == -EBUSY) {
1586c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
1587c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1588c0e09200SDave Airlie 	}
1589c0e09200SDave Airlie 
1590c0e09200SDave Airlie 	return ret;
1591c0e09200SDave Airlie }
1592c0e09200SDave Airlie 
1593c0e09200SDave Airlie /* Needs the lock as it touches the ring.
1594c0e09200SDave Airlie  */
1595c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
1596c0e09200SDave Airlie 			 struct drm_file *file_priv)
1597c0e09200SDave Airlie {
1598c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1599c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
1600c0e09200SDave Airlie 	int result;
1601c0e09200SDave Airlie 
16021ec14ad3SChris Wilson 	if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
1603c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1604c0e09200SDave Airlie 		return -EINVAL;
1605c0e09200SDave Airlie 	}
1606299eb93cSEric Anholt 
1607299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1608299eb93cSEric Anholt 
1609546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
1610c0e09200SDave Airlie 	result = i915_emit_irq(dev);
1611546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
1612c0e09200SDave Airlie 
1613c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
1614c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
1615c0e09200SDave Airlie 		return -EFAULT;
1616c0e09200SDave Airlie 	}
1617c0e09200SDave Airlie 
1618c0e09200SDave Airlie 	return 0;
1619c0e09200SDave Airlie }
1620c0e09200SDave Airlie 
1621c0e09200SDave Airlie /* Doesn't need the hardware lock.
1622c0e09200SDave Airlie  */
1623c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
1624c0e09200SDave Airlie 			 struct drm_file *file_priv)
1625c0e09200SDave Airlie {
1626c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1627c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
1628c0e09200SDave Airlie 
1629c0e09200SDave Airlie 	if (!dev_priv) {
1630c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1631c0e09200SDave Airlie 		return -EINVAL;
1632c0e09200SDave Airlie 	}
1633c0e09200SDave Airlie 
1634c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
1635c0e09200SDave Airlie }
1636c0e09200SDave Airlie 
163742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
163842f52ef8SKeith Packard  * we use as a pipe index
163942f52ef8SKeith Packard  */
1640f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
16410a3e67a4SJesse Barnes {
16420a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1643e9d21d7fSKeith Packard 	unsigned long irqflags;
164471e0ffa5SJesse Barnes 
16455eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
164671e0ffa5SJesse Barnes 		return -EINVAL;
16470a3e67a4SJesse Barnes 
16481ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1649f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
16507c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
16517c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
16520a3e67a4SJesse Barnes 	else
16537c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
16547c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
16558692d00eSChris Wilson 
16568692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
16578692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
16588692d00eSChris Wilson 		I915_WRITE(INSTPM, INSTPM_AGPBUSY_DIS << 16);
16591ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
16608692d00eSChris Wilson 
16610a3e67a4SJesse Barnes 	return 0;
16620a3e67a4SJesse Barnes }
16630a3e67a4SJesse Barnes 
1664f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1665f796cf8fSJesse Barnes {
1666f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1667f796cf8fSJesse Barnes 	unsigned long irqflags;
1668f796cf8fSJesse Barnes 
1669f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1670f796cf8fSJesse Barnes 		return -EINVAL;
1671f796cf8fSJesse Barnes 
1672f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1673f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1674f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1675f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1676f796cf8fSJesse Barnes 
1677f796cf8fSJesse Barnes 	return 0;
1678f796cf8fSJesse Barnes }
1679f796cf8fSJesse Barnes 
1680f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1681b1f14ad0SJesse Barnes {
1682b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1683b1f14ad0SJesse Barnes 	unsigned long irqflags;
1684b1f14ad0SJesse Barnes 
1685b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1686b1f14ad0SJesse Barnes 		return -EINVAL;
1687b1f14ad0SJesse Barnes 
1688b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1689b1f14ad0SJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1690b1f14ad0SJesse Barnes 				    DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1691b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1692b1f14ad0SJesse Barnes 
1693b1f14ad0SJesse Barnes 	return 0;
1694b1f14ad0SJesse Barnes }
1695b1f14ad0SJesse Barnes 
16967e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
16977e231dbeSJesse Barnes {
16987e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16997e231dbeSJesse Barnes 	unsigned long irqflags;
17007e231dbeSJesse Barnes 	u32 dpfl, imr;
17017e231dbeSJesse Barnes 
17027e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
17037e231dbeSJesse Barnes 		return -EINVAL;
17047e231dbeSJesse Barnes 
17057e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17067e231dbeSJesse Barnes 	dpfl = I915_READ(VLV_DPFLIPSTAT);
17077e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
17087e231dbeSJesse Barnes 	if (pipe == 0) {
17097e231dbeSJesse Barnes 		dpfl |= PIPEA_VBLANK_INT_EN;
17107e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
17117e231dbeSJesse Barnes 	} else {
17127e231dbeSJesse Barnes 		dpfl |= PIPEA_VBLANK_INT_EN;
17137e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
17147e231dbeSJesse Barnes 	}
17157e231dbeSJesse Barnes 	I915_WRITE(VLV_DPFLIPSTAT, dpfl);
17167e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
17177e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17187e231dbeSJesse Barnes 
17197e231dbeSJesse Barnes 	return 0;
17207e231dbeSJesse Barnes }
17217e231dbeSJesse Barnes 
172242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
172342f52ef8SKeith Packard  * we use as a pipe index
172442f52ef8SKeith Packard  */
1725f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
17260a3e67a4SJesse Barnes {
17270a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1728e9d21d7fSKeith Packard 	unsigned long irqflags;
17290a3e67a4SJesse Barnes 
17301ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17318692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
17328692d00eSChris Wilson 		I915_WRITE(INSTPM,
17338692d00eSChris Wilson 			   INSTPM_AGPBUSY_DIS << 16 | INSTPM_AGPBUSY_DIS);
17348692d00eSChris Wilson 
17357c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
17367c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
17377c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
17381ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17390a3e67a4SJesse Barnes }
17400a3e67a4SJesse Barnes 
1741f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1742f796cf8fSJesse Barnes {
1743f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1744f796cf8fSJesse Barnes 	unsigned long irqflags;
1745f796cf8fSJesse Barnes 
1746f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1747f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1748f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1749f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1750f796cf8fSJesse Barnes }
1751f796cf8fSJesse Barnes 
1752f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1753b1f14ad0SJesse Barnes {
1754b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1755b1f14ad0SJesse Barnes 	unsigned long irqflags;
1756b1f14ad0SJesse Barnes 
1757b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1758b1f14ad0SJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1759b1f14ad0SJesse Barnes 				     DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1760b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1761b1f14ad0SJesse Barnes }
1762b1f14ad0SJesse Barnes 
17637e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
17647e231dbeSJesse Barnes {
17657e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
17667e231dbeSJesse Barnes 	unsigned long irqflags;
17677e231dbeSJesse Barnes 	u32 dpfl, imr;
17687e231dbeSJesse Barnes 
17697e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17707e231dbeSJesse Barnes 	dpfl = I915_READ(VLV_DPFLIPSTAT);
17717e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
17727e231dbeSJesse Barnes 	if (pipe == 0) {
17737e231dbeSJesse Barnes 		dpfl &= ~PIPEA_VBLANK_INT_EN;
17747e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
17757e231dbeSJesse Barnes 	} else {
17767e231dbeSJesse Barnes 		dpfl &= ~PIPEB_VBLANK_INT_EN;
17777e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
17787e231dbeSJesse Barnes 	}
17797e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
17807e231dbeSJesse Barnes 	I915_WRITE(VLV_DPFLIPSTAT, dpfl);
17817e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
17827e231dbeSJesse Barnes }
17837e231dbeSJesse Barnes 
17847e231dbeSJesse Barnes 
1785c0e09200SDave Airlie /* Set the vblank monitor pipe
1786c0e09200SDave Airlie  */
1787c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1788c0e09200SDave Airlie 			 struct drm_file *file_priv)
1789c0e09200SDave Airlie {
1790c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1791c0e09200SDave Airlie 
1792c0e09200SDave Airlie 	if (!dev_priv) {
1793c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1794c0e09200SDave Airlie 		return -EINVAL;
1795c0e09200SDave Airlie 	}
1796c0e09200SDave Airlie 
1797c0e09200SDave Airlie 	return 0;
1798c0e09200SDave Airlie }
1799c0e09200SDave Airlie 
1800c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1801c0e09200SDave Airlie 			 struct drm_file *file_priv)
1802c0e09200SDave Airlie {
1803c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1804c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
1805c0e09200SDave Airlie 
1806c0e09200SDave Airlie 	if (!dev_priv) {
1807c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1808c0e09200SDave Airlie 		return -EINVAL;
1809c0e09200SDave Airlie 	}
1810c0e09200SDave Airlie 
18110a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1812c0e09200SDave Airlie 
1813c0e09200SDave Airlie 	return 0;
1814c0e09200SDave Airlie }
1815c0e09200SDave Airlie 
1816c0e09200SDave Airlie /**
1817c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
1818c0e09200SDave Airlie  */
1819c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
1820c0e09200SDave Airlie 		     struct drm_file *file_priv)
1821c0e09200SDave Airlie {
1822bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1823bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1824bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1825bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1826bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1827bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1828bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1829bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1830bd95e0a4SEric Anholt 	 *
1831bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1832bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1833bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1834bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
18350a3e67a4SJesse Barnes 	 */
1836c0e09200SDave Airlie 	return -EINVAL;
1837c0e09200SDave Airlie }
1838c0e09200SDave Airlie 
1839893eead0SChris Wilson static u32
1840893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1841852835f3SZou Nan hai {
1842893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1843893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1844893eead0SChris Wilson }
1845893eead0SChris Wilson 
1846893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1847893eead0SChris Wilson {
1848893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1849893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1850893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
1851b2223497SChris Wilson 		if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
1852893eead0SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1853893eead0SChris Wilson 				  ring->name,
1854b2223497SChris Wilson 				  ring->waiting_seqno,
1855893eead0SChris Wilson 				  ring->get_seqno(ring));
1856893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1857893eead0SChris Wilson 			*err = true;
1858893eead0SChris Wilson 		}
1859893eead0SChris Wilson 		return true;
1860893eead0SChris Wilson 	}
1861893eead0SChris Wilson 	return false;
1862f65d9421SBen Gamari }
1863f65d9421SBen Gamari 
18641ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
18651ec14ad3SChris Wilson {
18661ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
18671ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
18681ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
18691ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
18701ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
18711ec14ad3SChris Wilson 			  ring->name);
18721ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
18731ec14ad3SChris Wilson 		return true;
18741ec14ad3SChris Wilson 	}
18751ec14ad3SChris Wilson 	return false;
18761ec14ad3SChris Wilson }
18771ec14ad3SChris Wilson 
1878*d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
1879*d1e61e7fSChris Wilson {
1880*d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1881*d1e61e7fSChris Wilson 
1882*d1e61e7fSChris Wilson 	if (dev_priv->hangcheck_count++ > 1) {
1883*d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1884*d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
1885*d1e61e7fSChris Wilson 
1886*d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
1887*d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
1888*d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
1889*d1e61e7fSChris Wilson 			 * and break the hang. This should work on
1890*d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
1891*d1e61e7fSChris Wilson 			 */
1892*d1e61e7fSChris Wilson 			if (kick_ring(&dev_priv->ring[RCS]))
1893*d1e61e7fSChris Wilson 				return false;
1894*d1e61e7fSChris Wilson 
1895*d1e61e7fSChris Wilson 			if (HAS_BSD(dev) && kick_ring(&dev_priv->ring[VCS]))
1896*d1e61e7fSChris Wilson 				return false;
1897*d1e61e7fSChris Wilson 
1898*d1e61e7fSChris Wilson 			if (HAS_BLT(dev) && kick_ring(&dev_priv->ring[BCS]))
1899*d1e61e7fSChris Wilson 				return false;
1900*d1e61e7fSChris Wilson 		}
1901*d1e61e7fSChris Wilson 
1902*d1e61e7fSChris Wilson 		return true;
1903*d1e61e7fSChris Wilson 	}
1904*d1e61e7fSChris Wilson 
1905*d1e61e7fSChris Wilson 	return false;
1906*d1e61e7fSChris Wilson }
1907*d1e61e7fSChris Wilson 
1908f65d9421SBen Gamari /**
1909f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1910f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1911f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1912f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1913f65d9421SBen Gamari  */
1914f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1915f65d9421SBen Gamari {
1916f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1917f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1918097354ebSDaniel Vetter 	uint32_t acthd, instdone, instdone1, acthd_bsd, acthd_blt;
1919893eead0SChris Wilson 	bool err = false;
1920893eead0SChris Wilson 
19213e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
19223e0dc6b0SBen Widawsky 		return;
19233e0dc6b0SBen Widawsky 
1924893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
19251ec14ad3SChris Wilson 	if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
19261ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
19271ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
1928*d1e61e7fSChris Wilson 		if (err) {
1929*d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
1930*d1e61e7fSChris Wilson 				return;
1931*d1e61e7fSChris Wilson 
1932893eead0SChris Wilson 			goto repeat;
1933*d1e61e7fSChris Wilson 		}
1934*d1e61e7fSChris Wilson 
1935*d1e61e7fSChris Wilson 		dev_priv->hangcheck_count = 0;
1936893eead0SChris Wilson 		return;
1937893eead0SChris Wilson 	}
1938f65d9421SBen Gamari 
1939a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1940cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1941cbb465e7SChris Wilson 		instdone1 = 0;
1942cbb465e7SChris Wilson 	} else {
1943cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1944cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1945cbb465e7SChris Wilson 	}
1946097354ebSDaniel Vetter 	acthd = intel_ring_get_active_head(&dev_priv->ring[RCS]);
1947097354ebSDaniel Vetter 	acthd_bsd = HAS_BSD(dev) ?
1948097354ebSDaniel Vetter 		intel_ring_get_active_head(&dev_priv->ring[VCS]) : 0;
1949097354ebSDaniel Vetter 	acthd_blt = HAS_BLT(dev) ?
1950097354ebSDaniel Vetter 		intel_ring_get_active_head(&dev_priv->ring[BCS]) : 0;
1951f65d9421SBen Gamari 
1952cbb465e7SChris Wilson 	if (dev_priv->last_acthd == acthd &&
1953097354ebSDaniel Vetter 	    dev_priv->last_acthd_bsd == acthd_bsd &&
1954097354ebSDaniel Vetter 	    dev_priv->last_acthd_blt == acthd_blt &&
1955cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1956cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1957*d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
1958f65d9421SBen Gamari 			return;
1959cbb465e7SChris Wilson 	} else {
1960cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1961cbb465e7SChris Wilson 
1962cbb465e7SChris Wilson 		dev_priv->last_acthd = acthd;
1963097354ebSDaniel Vetter 		dev_priv->last_acthd_bsd = acthd_bsd;
1964097354ebSDaniel Vetter 		dev_priv->last_acthd_blt = acthd_blt;
1965cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1966cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1967cbb465e7SChris Wilson 	}
1968f65d9421SBen Gamari 
1969893eead0SChris Wilson repeat:
1970f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1971b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1972b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1973f65d9421SBen Gamari }
1974f65d9421SBen Gamari 
1975c0e09200SDave Airlie /* drm_dma.h hooks
1976c0e09200SDave Airlie */
1977f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
1978036a4a7dSZhenyu Wang {
1979036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1980036a4a7dSZhenyu Wang 
19814697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
19824697995bSJesse Barnes 
19834697995bSJesse Barnes 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
19844697995bSJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
19859e3c256dSJesse Barnes 	if (IS_GEN6(dev) || IS_IVYBRIDGE(dev))
19869e3c256dSJesse Barnes 		INIT_WORK(&dev_priv->rps_work, gen6_pm_rps_work);
19874697995bSJesse Barnes 
1988036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1989bdfcdb63SDaniel Vetter 
1990036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1991036a4a7dSZhenyu Wang 
1992036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1993036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
19943143a2bfSChris Wilson 	POSTING_READ(DEIER);
1995036a4a7dSZhenyu Wang 
1996036a4a7dSZhenyu Wang 	/* and GT */
1997036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1998036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
19993143a2bfSChris Wilson 	POSTING_READ(GTIER);
2000c650156aSZhenyu Wang 
2001c650156aSZhenyu Wang 	/* south display irq */
2002c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
2003c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
20043143a2bfSChris Wilson 	POSTING_READ(SDEIER);
2005036a4a7dSZhenyu Wang }
2006036a4a7dSZhenyu Wang 
20077e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
20087e231dbeSJesse Barnes {
20097e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20107e231dbeSJesse Barnes 	int pipe;
20117e231dbeSJesse Barnes 
20127e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
20137e231dbeSJesse Barnes 
20147e231dbeSJesse Barnes 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
20157e231dbeSJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
20167e231dbeSJesse Barnes 
20177e231dbeSJesse Barnes 	/* VLV magic */
20187e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
20197e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
20207e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
20217e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
20227e231dbeSJesse Barnes 
20237e231dbeSJesse Barnes 	/* and GT */
20247e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
20257e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
20267e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
20277e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
20287e231dbeSJesse Barnes 	POSTING_READ(GTIER);
20297e231dbeSJesse Barnes 
20307e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
20317e231dbeSJesse Barnes 
20327e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
20337e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
20347e231dbeSJesse Barnes 	for_each_pipe(pipe)
20357e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
20367e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20377e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
20387e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
20397e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
20407e231dbeSJesse Barnes }
20417e231dbeSJesse Barnes 
20427fe0b973SKeith Packard /*
20437fe0b973SKeith Packard  * Enable digital hotplug on the PCH, and configure the DP short pulse
20447fe0b973SKeith Packard  * duration to 2ms (which is the minimum in the Display Port spec)
20457fe0b973SKeith Packard  *
20467fe0b973SKeith Packard  * This register is the same on all known PCH chips.
20477fe0b973SKeith Packard  */
20487fe0b973SKeith Packard 
20497fe0b973SKeith Packard static void ironlake_enable_pch_hotplug(struct drm_device *dev)
20507fe0b973SKeith Packard {
20517fe0b973SKeith Packard 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20527fe0b973SKeith Packard 	u32	hotplug;
20537fe0b973SKeith Packard 
20547fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
20557fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
20567fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
20577fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
20587fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
20597fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
20607fe0b973SKeith Packard }
20617fe0b973SKeith Packard 
2062f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2063036a4a7dSZhenyu Wang {
2064036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2065036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
2066013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2067013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
20681ec14ad3SChris Wilson 	u32 render_irqs;
20692d7b8366SYuanhan Liu 	u32 hotplug_mask;
2070036a4a7dSZhenyu Wang 
20714697995bSJesse Barnes 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
20724697995bSJesse Barnes 	if (HAS_BSD(dev))
20734697995bSJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
20744697995bSJesse Barnes 	if (HAS_BLT(dev))
20754697995bSJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
20764697995bSJesse Barnes 
20774697995bSJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
20781ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2079036a4a7dSZhenyu Wang 
2080036a4a7dSZhenyu Wang 	/* should always can generate irq */
2081036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
20821ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
20831ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
20843143a2bfSChris Wilson 	POSTING_READ(DEIER);
2085036a4a7dSZhenyu Wang 
20861ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
2087036a4a7dSZhenyu Wang 
2088036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
20891ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2090881f47b6SXiang, Haihao 
20911ec14ad3SChris Wilson 	if (IS_GEN6(dev))
20921ec14ad3SChris Wilson 		render_irqs =
20931ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
2094e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
2095e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
20961ec14ad3SChris Wilson 	else
20971ec14ad3SChris Wilson 		render_irqs =
209888f23b8fSChris Wilson 			GT_USER_INTERRUPT |
2099c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
21001ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
21011ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
21023143a2bfSChris Wilson 	POSTING_READ(GTIER);
2103036a4a7dSZhenyu Wang 
21042d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
21059035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
21069035a97aSChris Wilson 				SDE_PORTB_HOTPLUG_CPT |
21079035a97aSChris Wilson 				SDE_PORTC_HOTPLUG_CPT |
21089035a97aSChris Wilson 				SDE_PORTD_HOTPLUG_CPT);
21092d7b8366SYuanhan Liu 	} else {
21109035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG |
21119035a97aSChris Wilson 				SDE_PORTB_HOTPLUG |
21129035a97aSChris Wilson 				SDE_PORTC_HOTPLUG |
21139035a97aSChris Wilson 				SDE_PORTD_HOTPLUG |
21149035a97aSChris Wilson 				SDE_AUX_MASK);
21152d7b8366SYuanhan Liu 	}
21162d7b8366SYuanhan Liu 
21171ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
2118c650156aSZhenyu Wang 
2119c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
21201ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
21211ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
21223143a2bfSChris Wilson 	POSTING_READ(SDEIER);
2123c650156aSZhenyu Wang 
21247fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
21257fe0b973SKeith Packard 
2126f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
2127f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
2128f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
2129f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
2130f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2131f97108d1SJesse Barnes 	}
2132f97108d1SJesse Barnes 
2133036a4a7dSZhenyu Wang 	return 0;
2134036a4a7dSZhenyu Wang }
2135036a4a7dSZhenyu Wang 
2136f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
2137b1f14ad0SJesse Barnes {
2138b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2139b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
2140b1f14ad0SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
2141b1f14ad0SJesse Barnes 		DE_PCH_EVENT_IVB | DE_PLANEA_FLIP_DONE_IVB |
2142b1f14ad0SJesse Barnes 		DE_PLANEB_FLIP_DONE_IVB;
2143b1f14ad0SJesse Barnes 	u32 render_irqs;
2144b1f14ad0SJesse Barnes 	u32 hotplug_mask;
2145b1f14ad0SJesse Barnes 
2146b1f14ad0SJesse Barnes 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
2147b1f14ad0SJesse Barnes 	if (HAS_BSD(dev))
2148b1f14ad0SJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
2149b1f14ad0SJesse Barnes 	if (HAS_BLT(dev))
2150b1f14ad0SJesse Barnes 		DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
2151b1f14ad0SJesse Barnes 
2152b1f14ad0SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
2153b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
2154b1f14ad0SJesse Barnes 
2155b1f14ad0SJesse Barnes 	/* should always can generate irq */
2156b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2157b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2158b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK_IVB |
2159b1f14ad0SJesse Barnes 		   DE_PIPEB_VBLANK_IVB);
2160b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2161b1f14ad0SJesse Barnes 
2162b1f14ad0SJesse Barnes 	dev_priv->gt_irq_mask = ~0;
2163b1f14ad0SJesse Barnes 
2164b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2165b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2166b1f14ad0SJesse Barnes 
2167e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
2168e2a1e2f0SBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT;
2169b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
2170b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2171b1f14ad0SJesse Barnes 
2172b1f14ad0SJesse Barnes 	hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
2173b1f14ad0SJesse Barnes 			SDE_PORTB_HOTPLUG_CPT |
2174b1f14ad0SJesse Barnes 			SDE_PORTC_HOTPLUG_CPT |
2175b1f14ad0SJesse Barnes 			SDE_PORTD_HOTPLUG_CPT);
2176b1f14ad0SJesse Barnes 	dev_priv->pch_irq_mask = ~hotplug_mask;
2177b1f14ad0SJesse Barnes 
2178b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2179b1f14ad0SJesse Barnes 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
2180b1f14ad0SJesse Barnes 	I915_WRITE(SDEIER, hotplug_mask);
2181b1f14ad0SJesse Barnes 	POSTING_READ(SDEIER);
2182b1f14ad0SJesse Barnes 
21837fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
21847fe0b973SKeith Packard 
2185b1f14ad0SJesse Barnes 	return 0;
2186b1f14ad0SJesse Barnes }
2187b1f14ad0SJesse Barnes 
21887e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
21897e231dbeSJesse Barnes {
21907e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21917e231dbeSJesse Barnes 	u32 render_irqs;
21927e231dbeSJesse Barnes 	u32 enable_mask;
21937e231dbeSJesse Barnes 	u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
21947e231dbeSJesse Barnes 	u16 msid;
21957e231dbeSJesse Barnes 
21967e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
21977e231dbeSJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
21987e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
21997e231dbeSJesse Barnes 
22007e231dbeSJesse Barnes 	dev_priv->irq_mask = ~enable_mask;
22017e231dbeSJesse Barnes 
22027e231dbeSJesse Barnes 
22037e231dbeSJesse Barnes 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
22047e231dbeSJesse Barnes 	DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
22057e231dbeSJesse Barnes 	DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
22067e231dbeSJesse Barnes 
22077e231dbeSJesse Barnes 	dev_priv->pipestat[0] = 0;
22087e231dbeSJesse Barnes 	dev_priv->pipestat[1] = 0;
22097e231dbeSJesse Barnes 
22107e231dbeSJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
22117e231dbeSJesse Barnes 
22127e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
22137e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
22147e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
22157e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
22167e231dbeSJesse Barnes 	msid |= (1<<14);
22177e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
22187e231dbeSJesse Barnes 
22197e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
22207e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
22217e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
22227e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
22237e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
22247e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
22257e231dbeSJesse Barnes 
22267e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
22277e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
22287e231dbeSJesse Barnes 
22297e231dbeSJesse Barnes 	render_irqs = GT_GEN6_BLT_FLUSHDW_NOTIFY_INTERRUPT |
22307e231dbeSJesse Barnes 		GT_GEN6_BLT_CS_ERROR_INTERRUPT |
2231e2a1e2f0SBen Widawsky 		GT_GEN6_BLT_USER_INTERRUPT |
22327e231dbeSJesse Barnes 		GT_GEN6_BSD_USER_INTERRUPT |
22337e231dbeSJesse Barnes 		GT_GEN6_BSD_CS_ERROR_INTERRUPT |
22347e231dbeSJesse Barnes 		GT_GEN7_L3_PARITY_ERROR_INTERRUPT |
22357e231dbeSJesse Barnes 		GT_PIPE_NOTIFY |
22367e231dbeSJesse Barnes 		GT_RENDER_CS_ERROR_INTERRUPT |
22377e231dbeSJesse Barnes 		GT_SYNC_STATUS |
22387e231dbeSJesse Barnes 		GT_USER_INTERRUPT;
22397e231dbeSJesse Barnes 
22407e231dbeSJesse Barnes 	dev_priv->gt_irq_mask = ~render_irqs;
22417e231dbeSJesse Barnes 
22427e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
22437e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
22447e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0);
22457e231dbeSJesse Barnes 	I915_WRITE(GTIER, render_irqs);
22467e231dbeSJesse Barnes 	POSTING_READ(GTIER);
22477e231dbeSJesse Barnes 
22487e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
22497e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
22507e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
22517e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
22527e231dbeSJesse Barnes #endif
22537e231dbeSJesse Barnes 
22547e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
22557e231dbeSJesse Barnes #if 0 /* FIXME: check register definitions; some have moved */
22567e231dbeSJesse Barnes 	/* Note HDMI and DP share bits */
22577e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
22587e231dbeSJesse Barnes 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
22597e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
22607e231dbeSJesse Barnes 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
22617e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
22627e231dbeSJesse Barnes 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
22637e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
22647e231dbeSJesse Barnes 		hotplug_en |= SDVOC_HOTPLUG_INT_EN;
22657e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
22667e231dbeSJesse Barnes 		hotplug_en |= SDVOB_HOTPLUG_INT_EN;
22677e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
22687e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_INT_EN;
22697e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
22707e231dbeSJesse Barnes 	}
22717e231dbeSJesse Barnes #endif
22727e231dbeSJesse Barnes 
22737e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
22747e231dbeSJesse Barnes 
22757e231dbeSJesse Barnes 	return 0;
22767e231dbeSJesse Barnes }
22777e231dbeSJesse Barnes 
2278f71d4af4SJesse Barnes static void i915_driver_irq_preinstall(struct drm_device * dev)
2279c0e09200SDave Airlie {
2280c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22819db4a9c7SJesse Barnes 	int pipe;
2282c0e09200SDave Airlie 
228379e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
228479e53945SJesse Barnes 
2285036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
22868a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
2287036a4a7dSZhenyu Wang 
22885ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
22895ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
22905ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
22915ca58282SJesse Barnes 	}
22925ca58282SJesse Barnes 
22930a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
22949db4a9c7SJesse Barnes 	for_each_pipe(pipe)
22959db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0);
22960a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
2297ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
22983143a2bfSChris Wilson 	POSTING_READ(IER);
2299c0e09200SDave Airlie }
2300c0e09200SDave Airlie 
2301b01f2c3aSJesse Barnes /*
2302b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
2303b01f2c3aSJesse Barnes  * enabled correctly.
2304b01f2c3aSJesse Barnes  */
2305f71d4af4SJesse Barnes static int i915_driver_irq_postinstall(struct drm_device *dev)
2306c0e09200SDave Airlie {
2307c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23085ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
230963eeaf38SJesse Barnes 	u32 error_mask;
23100a3e67a4SJesse Barnes 
23110a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
2312ed4cb414SEric Anholt 
23137c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
23141ec14ad3SChris Wilson 	dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
23158ee1c3dbSMatthew Garrett 
23167c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
23177c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
23187c463586SKeith Packard 
23195ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
2320c496fa1fSAdam Jackson 		/* Enable in IER... */
2321c496fa1fSAdam Jackson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2322c496fa1fSAdam Jackson 		/* and unmask in IMR */
23231ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2324c496fa1fSAdam Jackson 	}
2325c496fa1fSAdam Jackson 
2326c496fa1fSAdam Jackson 	/*
2327c496fa1fSAdam Jackson 	 * Enable some error detection, note the instruction error mask
2328c496fa1fSAdam Jackson 	 * bit is reserved, so we leave it masked.
2329c496fa1fSAdam Jackson 	 */
2330c496fa1fSAdam Jackson 	if (IS_G4X(dev)) {
2331c496fa1fSAdam Jackson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2332c496fa1fSAdam Jackson 			       GM45_ERROR_MEM_PRIV |
2333c496fa1fSAdam Jackson 			       GM45_ERROR_CP_PRIV |
2334c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
2335c496fa1fSAdam Jackson 	} else {
2336c496fa1fSAdam Jackson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2337c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
2338c496fa1fSAdam Jackson 	}
2339c496fa1fSAdam Jackson 	I915_WRITE(EMR, error_mask);
2340c496fa1fSAdam Jackson 
23411ec14ad3SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2342c496fa1fSAdam Jackson 	I915_WRITE(IER, enable_mask);
23433143a2bfSChris Wilson 	POSTING_READ(IER);
2344c496fa1fSAdam Jackson 
2345c496fa1fSAdam Jackson 	if (I915_HAS_HOTPLUG(dev)) {
23465ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
23475ca58282SJesse Barnes 
2348b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
2349b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2350b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2351b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2352b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2353b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2354b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
2355b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
2356b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2357b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
2358b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
23592d1c9752SAndy Lutomirski 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2360b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
23612d1c9752SAndy Lutomirski 
23622d1c9752SAndy Lutomirski 			/* Programming the CRT detection parameters tends
23632d1c9752SAndy Lutomirski 			   to generate a spurious hotplug event about three
23642d1c9752SAndy Lutomirski 			   seconds later.  So just do it once.
23652d1c9752SAndy Lutomirski 			*/
23662d1c9752SAndy Lutomirski 			if (IS_G4X(dev))
23672d1c9752SAndy Lutomirski 				hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
23682d1c9752SAndy Lutomirski 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
23692d1c9752SAndy Lutomirski 		}
23702d1c9752SAndy Lutomirski 
2371b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
2372b01f2c3aSJesse Barnes 
23735ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
23745ca58282SJesse Barnes 	}
23755ca58282SJesse Barnes 
23763b617967SChris Wilson 	intel_opregion_enable_asle(dev);
23770a3e67a4SJesse Barnes 
23780a3e67a4SJesse Barnes 	return 0;
2379c0e09200SDave Airlie }
2380c0e09200SDave Airlie 
23817e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
23827e231dbeSJesse Barnes {
23837e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23847e231dbeSJesse Barnes 	int pipe;
23857e231dbeSJesse Barnes 
23867e231dbeSJesse Barnes 	if (!dev_priv)
23877e231dbeSJesse Barnes 		return;
23887e231dbeSJesse Barnes 
23897e231dbeSJesse Barnes 	dev_priv->vblank_pipe = 0;
23907e231dbeSJesse Barnes 
23917e231dbeSJesse Barnes 	for_each_pipe(pipe)
23927e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
23937e231dbeSJesse Barnes 
23947e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
23957e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
23967e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
23977e231dbeSJesse Barnes 	for_each_pipe(pipe)
23987e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
23997e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
24007e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
24017e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
24027e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
24037e231dbeSJesse Barnes }
24047e231dbeSJesse Barnes 
2405f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2406036a4a7dSZhenyu Wang {
2407036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24084697995bSJesse Barnes 
24094697995bSJesse Barnes 	if (!dev_priv)
24104697995bSJesse Barnes 		return;
24114697995bSJesse Barnes 
24124697995bSJesse Barnes 	dev_priv->vblank_pipe = 0;
24134697995bSJesse Barnes 
2414036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2415036a4a7dSZhenyu Wang 
2416036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2417036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2418036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2419036a4a7dSZhenyu Wang 
2420036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2421036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2422036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2423192aac1fSKeith Packard 
2424192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2425192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2426192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2427036a4a7dSZhenyu Wang }
2428036a4a7dSZhenyu Wang 
2429f71d4af4SJesse Barnes static void i915_driver_irq_uninstall(struct drm_device * dev)
2430c0e09200SDave Airlie {
2431c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24329db4a9c7SJesse Barnes 	int pipe;
2433c0e09200SDave Airlie 
2434c0e09200SDave Airlie 	if (!dev_priv)
2435c0e09200SDave Airlie 		return;
2436c0e09200SDave Airlie 
24370a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
24380a3e67a4SJesse Barnes 
24395ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
24405ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
24415ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
24425ca58282SJesse Barnes 	}
24435ca58282SJesse Barnes 
24440a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
24459db4a9c7SJesse Barnes 	for_each_pipe(pipe)
24469db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0);
24470a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
2448ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
2449c0e09200SDave Airlie 
24509db4a9c7SJesse Barnes 	for_each_pipe(pipe)
24519db4a9c7SJesse Barnes 		I915_WRITE(PIPESTAT(pipe),
24529db4a9c7SJesse Barnes 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
24537c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
2454c0e09200SDave Airlie }
2455f71d4af4SJesse Barnes 
2456f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2457f71d4af4SJesse Barnes {
2458f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2459f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
24607e231dbeSJesse Barnes 	if (IS_G4X(dev) || IS_GEN5(dev) || IS_GEN6(dev) || IS_IVYBRIDGE(dev) ||
24617e231dbeSJesse Barnes 	    IS_VALLEYVIEW(dev)) {
2462f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2463f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2464f71d4af4SJesse Barnes 	}
2465f71d4af4SJesse Barnes 
2466c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2467f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2468c3613de9SKeith Packard 	else
2469c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2470f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2471f71d4af4SJesse Barnes 
24727e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
24737e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
24747e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
24757e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
24767e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
24777e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
24787e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
24797e231dbeSJesse Barnes 	} else if (IS_IVYBRIDGE(dev)) {
2480f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2481f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2482f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2483f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2484f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2485f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2486f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
2487f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2488f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2489f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2490f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2491f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2492f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2493f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
2494f71d4af4SJesse Barnes 	} else {
2495f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = i915_driver_irq_preinstall;
2496f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = i915_driver_irq_postinstall;
2497f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = i915_driver_irq_uninstall;
2498f71d4af4SJesse Barnes 		dev->driver->irq_handler = i915_driver_irq_handler;
2499f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
2500f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
2501f71d4af4SJesse Barnes 	}
2502f71d4af4SJesse Barnes }
2503