xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision bbba0a9712be1c8e970fc66fc3becad0ce809e4f)
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 
4279e53945SJesse Barnes #define I915_PIPE_VBLANK_STATUS	(PIPE_START_VBLANK_INTERRUPT_STATUS |\
4379e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_STATUS)
4479e53945SJesse Barnes 
4579e53945SJesse Barnes #define I915_PIPE_VBLANK_ENABLE	(PIPE_START_VBLANK_INTERRUPT_ENABLE |\
4679e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_ENABLE)
4779e53945SJesse Barnes 
4879e53945SJesse Barnes #define DRM_I915_VBLANK_PIPE_ALL	(DRM_I915_VBLANK_PIPE_A | \
4979e53945SJesse Barnes 					 DRM_I915_VBLANK_PIPE_B)
5079e53945SJesse Barnes 
51036a4a7dSZhenyu Wang /* For display hotplug interrupt */
52995b6762SChris Wilson static void
53f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
54036a4a7dSZhenyu Wang {
551ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
561ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
571ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
583143a2bfSChris Wilson 		POSTING_READ(DEIMR);
59036a4a7dSZhenyu Wang 	}
60036a4a7dSZhenyu Wang }
61036a4a7dSZhenyu Wang 
62036a4a7dSZhenyu Wang static inline void
63f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
64036a4a7dSZhenyu Wang {
651ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
661ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
671ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
683143a2bfSChris Wilson 		POSTING_READ(DEIMR);
69036a4a7dSZhenyu Wang 	}
70036a4a7dSZhenyu Wang }
71036a4a7dSZhenyu Wang 
727c463586SKeith Packard void
737c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
747c463586SKeith Packard {
757c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
769db4a9c7SJesse Barnes 		u32 reg = PIPESTAT(pipe);
777c463586SKeith Packard 
787c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
797c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
807c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
813143a2bfSChris Wilson 		POSTING_READ(reg);
827c463586SKeith Packard 	}
837c463586SKeith Packard }
847c463586SKeith Packard 
857c463586SKeith Packard void
867c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
877c463586SKeith Packard {
887c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
899db4a9c7SJesse Barnes 		u32 reg = PIPESTAT(pipe);
907c463586SKeith Packard 
917c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
927c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
933143a2bfSChris Wilson 		POSTING_READ(reg);
947c463586SKeith Packard 	}
957c463586SKeith Packard }
967c463586SKeith Packard 
97c0e09200SDave Airlie /**
9801c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
9901c66889SZhao Yakui  */
10001c66889SZhao Yakui void intel_enable_asle(struct drm_device *dev)
10101c66889SZhao Yakui {
1021ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1031ec14ad3SChris Wilson 	unsigned long irqflags;
1041ec14ad3SChris Wilson 
1057e231dbeSJesse Barnes 	/* FIXME: opregion/asle for VLV */
1067e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev))
1077e231dbeSJesse Barnes 		return;
1087e231dbeSJesse Barnes 
1091ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
11001c66889SZhao Yakui 
111c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
112f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
113edcb49caSZhao Yakui 	else {
11401c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
115d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
116a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen >= 4)
117edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
118d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
119edcb49caSZhao Yakui 	}
1201ec14ad3SChris Wilson 
1211ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
12201c66889SZhao Yakui }
12301c66889SZhao Yakui 
12401c66889SZhao Yakui /**
1250a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1260a3e67a4SJesse Barnes  * @dev: DRM device
1270a3e67a4SJesse Barnes  * @pipe: pipe to check
1280a3e67a4SJesse Barnes  *
1290a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1300a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1310a3e67a4SJesse Barnes  * before reading such registers if unsure.
1320a3e67a4SJesse Barnes  */
1330a3e67a4SJesse Barnes static int
1340a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1350a3e67a4SJesse Barnes {
1360a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1375eddb70bSChris Wilson 	return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
1380a3e67a4SJesse Barnes }
1390a3e67a4SJesse Barnes 
14042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
14142f52ef8SKeith Packard  * we use as a pipe index
14242f52ef8SKeith Packard  */
143f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1440a3e67a4SJesse Barnes {
1450a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1460a3e67a4SJesse Barnes 	unsigned long high_frame;
1470a3e67a4SJesse Barnes 	unsigned long low_frame;
1485eddb70bSChris Wilson 	u32 high1, high2, low;
1490a3e67a4SJesse Barnes 
1500a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
15144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
1529db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
1530a3e67a4SJesse Barnes 		return 0;
1540a3e67a4SJesse Barnes 	}
1550a3e67a4SJesse Barnes 
1569db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
1579db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
1585eddb70bSChris Wilson 
1590a3e67a4SJesse Barnes 	/*
1600a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
1610a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
1620a3e67a4SJesse Barnes 	 * register.
1630a3e67a4SJesse Barnes 	 */
1640a3e67a4SJesse Barnes 	do {
1655eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1665eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
1675eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
1680a3e67a4SJesse Barnes 	} while (high1 != high2);
1690a3e67a4SJesse Barnes 
1705eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
1715eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
1725eddb70bSChris Wilson 	return (high1 << 8) | low;
1730a3e67a4SJesse Barnes }
1740a3e67a4SJesse Barnes 
175f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
1769880b7a5SJesse Barnes {
1779880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1789db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
1799880b7a5SJesse Barnes 
1809880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
18144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
1829db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
1839880b7a5SJesse Barnes 		return 0;
1849880b7a5SJesse Barnes 	}
1859880b7a5SJesse Barnes 
1869880b7a5SJesse Barnes 	return I915_READ(reg);
1879880b7a5SJesse Barnes }
1889880b7a5SJesse Barnes 
189f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
1900af7e4dfSMario Kleiner 			     int *vpos, int *hpos)
1910af7e4dfSMario Kleiner {
1920af7e4dfSMario Kleiner 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1930af7e4dfSMario Kleiner 	u32 vbl = 0, position = 0;
1940af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
1950af7e4dfSMario Kleiner 	bool in_vbl = true;
1960af7e4dfSMario Kleiner 	int ret = 0;
1970af7e4dfSMario Kleiner 
1980af7e4dfSMario Kleiner 	if (!i915_pipe_enabled(dev, pipe)) {
1990af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
2009db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
2010af7e4dfSMario Kleiner 		return 0;
2020af7e4dfSMario Kleiner 	}
2030af7e4dfSMario Kleiner 
2040af7e4dfSMario Kleiner 	/* Get vtotal. */
2050af7e4dfSMario Kleiner 	vtotal = 1 + ((I915_READ(VTOTAL(pipe)) >> 16) & 0x1fff);
2060af7e4dfSMario Kleiner 
2070af7e4dfSMario Kleiner 	if (INTEL_INFO(dev)->gen >= 4) {
2080af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
2090af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
2100af7e4dfSMario Kleiner 		 */
2110af7e4dfSMario Kleiner 		position = I915_READ(PIPEDSL(pipe));
2120af7e4dfSMario Kleiner 
2130af7e4dfSMario Kleiner 		/* Decode into vertical scanout position. Don't have
2140af7e4dfSMario Kleiner 		 * horizontal scanout position.
2150af7e4dfSMario Kleiner 		 */
2160af7e4dfSMario Kleiner 		*vpos = position & 0x1fff;
2170af7e4dfSMario Kleiner 		*hpos = 0;
2180af7e4dfSMario Kleiner 	} else {
2190af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
2200af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
2210af7e4dfSMario Kleiner 		 * scanout position.
2220af7e4dfSMario Kleiner 		 */
2230af7e4dfSMario Kleiner 		position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
2240af7e4dfSMario Kleiner 
2250af7e4dfSMario Kleiner 		htotal = 1 + ((I915_READ(HTOTAL(pipe)) >> 16) & 0x1fff);
2260af7e4dfSMario Kleiner 		*vpos = position / htotal;
2270af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
2280af7e4dfSMario Kleiner 	}
2290af7e4dfSMario Kleiner 
2300af7e4dfSMario Kleiner 	/* Query vblank area. */
2310af7e4dfSMario Kleiner 	vbl = I915_READ(VBLANK(pipe));
2320af7e4dfSMario Kleiner 
2330af7e4dfSMario Kleiner 	/* Test position against vblank region. */
2340af7e4dfSMario Kleiner 	vbl_start = vbl & 0x1fff;
2350af7e4dfSMario Kleiner 	vbl_end = (vbl >> 16) & 0x1fff;
2360af7e4dfSMario Kleiner 
2370af7e4dfSMario Kleiner 	if ((*vpos < vbl_start) || (*vpos > vbl_end))
2380af7e4dfSMario Kleiner 		in_vbl = false;
2390af7e4dfSMario Kleiner 
2400af7e4dfSMario Kleiner 	/* Inside "upper part" of vblank area? Apply corrective offset: */
2410af7e4dfSMario Kleiner 	if (in_vbl && (*vpos >= vbl_start))
2420af7e4dfSMario Kleiner 		*vpos = *vpos - vtotal;
2430af7e4dfSMario Kleiner 
2440af7e4dfSMario Kleiner 	/* Readouts valid? */
2450af7e4dfSMario Kleiner 	if (vbl > 0)
2460af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
2470af7e4dfSMario Kleiner 
2480af7e4dfSMario Kleiner 	/* In vblank? */
2490af7e4dfSMario Kleiner 	if (in_vbl)
2500af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
2510af7e4dfSMario Kleiner 
2520af7e4dfSMario Kleiner 	return ret;
2530af7e4dfSMario Kleiner }
2540af7e4dfSMario Kleiner 
255f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
2560af7e4dfSMario Kleiner 			      int *max_error,
2570af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
2580af7e4dfSMario Kleiner 			      unsigned flags)
2590af7e4dfSMario Kleiner {
2604041b853SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
2614041b853SChris Wilson 	struct drm_crtc *crtc;
2620af7e4dfSMario Kleiner 
2634041b853SChris Wilson 	if (pipe < 0 || pipe >= dev_priv->num_pipe) {
2644041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2650af7e4dfSMario Kleiner 		return -EINVAL;
2660af7e4dfSMario Kleiner 	}
2670af7e4dfSMario Kleiner 
2680af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
2694041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
2704041b853SChris Wilson 	if (crtc == NULL) {
2714041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
2724041b853SChris Wilson 		return -EINVAL;
2734041b853SChris Wilson 	}
2744041b853SChris Wilson 
2754041b853SChris Wilson 	if (!crtc->enabled) {
2764041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
2774041b853SChris Wilson 		return -EBUSY;
2784041b853SChris Wilson 	}
2790af7e4dfSMario Kleiner 
2800af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
2814041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
2824041b853SChris Wilson 						     vblank_time, flags,
2834041b853SChris Wilson 						     crtc);
2840af7e4dfSMario Kleiner }
2850af7e4dfSMario Kleiner 
2865ca58282SJesse Barnes /*
2875ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
2885ca58282SJesse Barnes  */
2895ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
2905ca58282SJesse Barnes {
2915ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
2925ca58282SJesse Barnes 						    hotplug_work);
2935ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
294c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
2954ef69c7aSChris Wilson 	struct intel_encoder *encoder;
2965ca58282SJesse Barnes 
297a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
298e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
299e67189abSJesse Barnes 
3004ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
3014ef69c7aSChris Wilson 		if (encoder->hot_plug)
3024ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
303c31c4ba3SKeith Packard 
30440ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
30540ee3381SKeith Packard 
3065ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
307eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
3085ca58282SJesse Barnes }
3095ca58282SJesse Barnes 
310f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
311f97108d1SJesse Barnes {
312f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
313b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
314f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
315f97108d1SJesse Barnes 
3167648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
317b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
318b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
319f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
320f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
321f97108d1SJesse Barnes 
322f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
323b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
324f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
325f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
326f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
327f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
328b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
329f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
330f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
331f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
332f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
333f97108d1SJesse Barnes 	}
334f97108d1SJesse Barnes 
3357648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
336f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
337f97108d1SJesse Barnes 
338f97108d1SJesse Barnes 	return;
339f97108d1SJesse Barnes }
340f97108d1SJesse Barnes 
341549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
342549f7365SChris Wilson 			struct intel_ring_buffer *ring)
343549f7365SChris Wilson {
344549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
345475553deSChris Wilson 	u32 seqno;
3469862e600SChris Wilson 
347475553deSChris Wilson 	if (ring->obj == NULL)
348475553deSChris Wilson 		return;
349475553deSChris Wilson 
350475553deSChris Wilson 	seqno = ring->get_seqno(ring);
351db53a302SChris Wilson 	trace_i915_gem_request_complete(ring, seqno);
3529862e600SChris Wilson 
3539862e600SChris Wilson 	ring->irq_seqno = seqno;
354549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
3553e0dc6b0SBen Widawsky 	if (i915_enable_hangcheck) {
356549f7365SChris Wilson 		dev_priv->hangcheck_count = 0;
357549f7365SChris Wilson 		mod_timer(&dev_priv->hangcheck_timer,
3583e0dc6b0SBen Widawsky 			  jiffies +
3593e0dc6b0SBen Widawsky 			  msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
3603e0dc6b0SBen Widawsky 	}
361549f7365SChris Wilson }
362549f7365SChris Wilson 
3634912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
3643b8d8d91SJesse Barnes {
3654912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3664912d041SBen Widawsky 						    rps_work);
3673b8d8d91SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
3684912d041SBen Widawsky 	u32 pm_iir, pm_imr;
3693b8d8d91SJesse Barnes 
3704912d041SBen Widawsky 	spin_lock_irq(&dev_priv->rps_lock);
3714912d041SBen Widawsky 	pm_iir = dev_priv->pm_iir;
3724912d041SBen Widawsky 	dev_priv->pm_iir = 0;
3734912d041SBen Widawsky 	pm_imr = I915_READ(GEN6_PMIMR);
374a9e2641dSDaniel Vetter 	I915_WRITE(GEN6_PMIMR, 0);
3754912d041SBen Widawsky 	spin_unlock_irq(&dev_priv->rps_lock);
3764912d041SBen Widawsky 
3773b8d8d91SJesse Barnes 	if (!pm_iir)
3783b8d8d91SJesse Barnes 		return;
3793b8d8d91SJesse Barnes 
3804912d041SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
3813b8d8d91SJesse Barnes 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
3823b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
3833b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
3843b8d8d91SJesse Barnes 		if (new_delay > dev_priv->max_delay)
3853b8d8d91SJesse Barnes 			new_delay = dev_priv->max_delay;
3863b8d8d91SJesse Barnes 	} else if (pm_iir & (GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT)) {
3874912d041SBen Widawsky 		gen6_gt_force_wake_get(dev_priv);
3883b8d8d91SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
3893b8d8d91SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
3903b8d8d91SJesse Barnes 		if (new_delay < dev_priv->min_delay) {
3913b8d8d91SJesse Barnes 			new_delay = dev_priv->min_delay;
3923b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3933b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) |
3943b8d8d91SJesse Barnes 				   ((new_delay << 16) & 0x3f0000));
3953b8d8d91SJesse Barnes 		} else {
3963b8d8d91SJesse Barnes 			/* Make sure we continue to get down interrupts
3973b8d8d91SJesse Barnes 			 * until we hit the minimum frequency */
3983b8d8d91SJesse Barnes 			I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3993b8d8d91SJesse Barnes 				   I915_READ(GEN6_RP_INTERRUPT_LIMITS) & ~0x3f0000);
4003b8d8d91SJesse Barnes 		}
4014912d041SBen Widawsky 		gen6_gt_force_wake_put(dev_priv);
4023b8d8d91SJesse Barnes 	}
4033b8d8d91SJesse Barnes 
4044912d041SBen Widawsky 	gen6_set_rps(dev_priv->dev, new_delay);
4053b8d8d91SJesse Barnes 	dev_priv->cur_delay = new_delay;
4063b8d8d91SJesse Barnes 
4074912d041SBen Widawsky 	/*
4084912d041SBen Widawsky 	 * rps_lock not held here because clearing is non-destructive. There is
4094912d041SBen Widawsky 	 * an *extremely* unlikely race with gen6_rps_enable() that is prevented
4104912d041SBen Widawsky 	 * by holding struct_mutex for the duration of the write.
4114912d041SBen Widawsky 	 */
4124912d041SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
4133b8d8d91SJesse Barnes }
4143b8d8d91SJesse Barnes 
415e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
416e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
417e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
418e7b4c6b1SDaniel Vetter {
419e7b4c6b1SDaniel Vetter 
420e7b4c6b1SDaniel Vetter 	if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
421e7b4c6b1SDaniel Vetter 		      GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
422e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
423e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BSD_USER_INTERRUPT)
424e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
425e7b4c6b1SDaniel Vetter 	if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
426e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
427e7b4c6b1SDaniel Vetter 
428e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
429e7b4c6b1SDaniel Vetter 		      GT_GEN6_BSD_CS_ERROR_INTERRUPT |
430e7b4c6b1SDaniel Vetter 		      GT_RENDER_CS_ERROR_INTERRUPT)) {
431e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
432e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
433e7b4c6b1SDaniel Vetter 	}
434e7b4c6b1SDaniel Vetter }
435e7b4c6b1SDaniel Vetter 
436fc6826d1SChris Wilson static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
437fc6826d1SChris Wilson 				u32 pm_iir)
438fc6826d1SChris Wilson {
439fc6826d1SChris Wilson 	unsigned long flags;
440fc6826d1SChris Wilson 
441fc6826d1SChris Wilson 	/*
442fc6826d1SChris Wilson 	 * IIR bits should never already be set because IMR should
443fc6826d1SChris Wilson 	 * prevent an interrupt from being shown in IIR. The warning
444fc6826d1SChris Wilson 	 * displays a case where we've unsafely cleared
445fc6826d1SChris Wilson 	 * dev_priv->pm_iir. Although missing an interrupt of the same
446fc6826d1SChris Wilson 	 * type is not a problem, it displays a problem in the logic.
447fc6826d1SChris Wilson 	 *
448fc6826d1SChris Wilson 	 * The mask bit in IMR is cleared by rps_work.
449fc6826d1SChris Wilson 	 */
450fc6826d1SChris Wilson 
451fc6826d1SChris Wilson 	spin_lock_irqsave(&dev_priv->rps_lock, flags);
452fc6826d1SChris Wilson 	WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
453fc6826d1SChris Wilson 	dev_priv->pm_iir |= pm_iir;
454fc6826d1SChris Wilson 	I915_WRITE(GEN6_PMIMR, dev_priv->pm_iir);
455fc6826d1SChris Wilson 	POSTING_READ(GEN6_PMIMR);
456fc6826d1SChris Wilson 	spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
457fc6826d1SChris Wilson 
458fc6826d1SChris Wilson 	queue_work(dev_priv->wq, &dev_priv->rps_work);
459fc6826d1SChris Wilson }
460fc6826d1SChris Wilson 
4617e231dbeSJesse Barnes static irqreturn_t valleyview_irq_handler(DRM_IRQ_ARGS)
4627e231dbeSJesse Barnes {
4637e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
4647e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
4657e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
4667e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
4677e231dbeSJesse Barnes 	unsigned long irqflags;
4687e231dbeSJesse Barnes 	int pipe;
4697e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
4707e231dbeSJesse Barnes 	u32 vblank_status;
4717e231dbeSJesse Barnes 	int vblank = 0;
4727e231dbeSJesse Barnes 	bool blc_event;
4737e231dbeSJesse Barnes 
4747e231dbeSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
4757e231dbeSJesse Barnes 
4767e231dbeSJesse Barnes 	vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS |
4777e231dbeSJesse Barnes 		PIPE_VBLANK_INTERRUPT_STATUS;
4787e231dbeSJesse Barnes 
4797e231dbeSJesse Barnes 	while (true) {
4807e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
4817e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
4827e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
4837e231dbeSJesse Barnes 
4847e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
4857e231dbeSJesse Barnes 			goto out;
4867e231dbeSJesse Barnes 
4877e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
4887e231dbeSJesse Barnes 
489e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
4907e231dbeSJesse Barnes 
4917e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4927e231dbeSJesse Barnes 		for_each_pipe(pipe) {
4937e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
4947e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
4957e231dbeSJesse Barnes 
4967e231dbeSJesse Barnes 			/*
4977e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
4987e231dbeSJesse Barnes 			 */
4997e231dbeSJesse Barnes 			if (pipe_stats[pipe] & 0x8000ffff) {
5007e231dbeSJesse Barnes 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
5017e231dbeSJesse Barnes 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
5027e231dbeSJesse Barnes 							 pipe_name(pipe));
5037e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
5047e231dbeSJesse Barnes 			}
5057e231dbeSJesse Barnes 		}
5067e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5077e231dbeSJesse Barnes 
5087e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
5097e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
5107e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
5117e231dbeSJesse Barnes 
5127e231dbeSJesse Barnes 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
5137e231dbeSJesse Barnes 					 hotplug_status);
5147e231dbeSJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
5157e231dbeSJesse Barnes 				queue_work(dev_priv->wq,
5167e231dbeSJesse Barnes 					   &dev_priv->hotplug_work);
5177e231dbeSJesse Barnes 
5187e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
5197e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
5207e231dbeSJesse Barnes 		}
5217e231dbeSJesse Barnes 
5227e231dbeSJesse Barnes 
5237e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT) {
5247e231dbeSJesse Barnes 			drm_handle_vblank(dev, 0);
5257e231dbeSJesse Barnes 			vblank++;
5267e231dbeSJesse Barnes 			intel_finish_page_flip(dev, 0);
5277e231dbeSJesse Barnes 		}
5287e231dbeSJesse Barnes 
5297e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT) {
5307e231dbeSJesse Barnes 			drm_handle_vblank(dev, 1);
5317e231dbeSJesse Barnes 			vblank++;
5327e231dbeSJesse Barnes 			intel_finish_page_flip(dev, 0);
5337e231dbeSJesse Barnes 		}
5347e231dbeSJesse Barnes 
5357e231dbeSJesse Barnes 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
5367e231dbeSJesse Barnes 			blc_event = true;
5377e231dbeSJesse Barnes 
538fc6826d1SChris Wilson 		if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
539fc6826d1SChris Wilson 			gen6_queue_rps_work(dev_priv, pm_iir);
5407e231dbeSJesse Barnes 
5417e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
5427e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
5437e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
5447e231dbeSJesse Barnes 	}
5457e231dbeSJesse Barnes 
5467e231dbeSJesse Barnes out:
5477e231dbeSJesse Barnes 	return ret;
5487e231dbeSJesse Barnes }
5497e231dbeSJesse Barnes 
550776ad806SJesse Barnes static void pch_irq_handler(struct drm_device *dev)
551776ad806SJesse Barnes {
552776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
553776ad806SJesse Barnes 	u32 pch_iir;
5549db4a9c7SJesse Barnes 	int pipe;
555776ad806SJesse Barnes 
556776ad806SJesse Barnes 	pch_iir = I915_READ(SDEIIR);
557776ad806SJesse Barnes 
558776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_POWER_MASK)
559776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
560776ad806SJesse Barnes 				 (pch_iir & SDE_AUDIO_POWER_MASK) >>
561776ad806SJesse Barnes 				 SDE_AUDIO_POWER_SHIFT);
562776ad806SJesse Barnes 
563776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
564776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
565776ad806SJesse Barnes 
566776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
567776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
568776ad806SJesse Barnes 
569776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
570776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
571776ad806SJesse Barnes 
572776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
573776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
574776ad806SJesse Barnes 
5759db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
5769db4a9c7SJesse Barnes 		for_each_pipe(pipe)
5779db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
5789db4a9c7SJesse Barnes 					 pipe_name(pipe),
5799db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
580776ad806SJesse Barnes 
581776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
582776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
583776ad806SJesse Barnes 
584776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
585776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
586776ad806SJesse Barnes 
587776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
588776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
589776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
590776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
591776ad806SJesse Barnes }
592776ad806SJesse Barnes 
593f71d4af4SJesse Barnes static irqreturn_t ivybridge_irq_handler(DRM_IRQ_ARGS)
594b1f14ad0SJesse Barnes {
595b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
596b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
597b1f14ad0SJesse Barnes 	int ret = IRQ_NONE;
598b1f14ad0SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
599b1f14ad0SJesse Barnes 	struct drm_i915_master_private *master_priv;
600b1f14ad0SJesse Barnes 
601b1f14ad0SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
602b1f14ad0SJesse Barnes 
603b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
604b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
605b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
606b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
607b1f14ad0SJesse Barnes 
608b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
609b1f14ad0SJesse Barnes 	gt_iir = I915_READ(GTIIR);
610b1f14ad0SJesse Barnes 	pch_iir = I915_READ(SDEIIR);
611b1f14ad0SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
612b1f14ad0SJesse Barnes 
613b1f14ad0SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 && pm_iir == 0)
614b1f14ad0SJesse Barnes 		goto done;
615b1f14ad0SJesse Barnes 
616b1f14ad0SJesse Barnes 	ret = IRQ_HANDLED;
617b1f14ad0SJesse Barnes 
618b1f14ad0SJesse Barnes 	if (dev->primary->master) {
619b1f14ad0SJesse Barnes 		master_priv = dev->primary->master->driver_priv;
620b1f14ad0SJesse Barnes 		if (master_priv->sarea_priv)
621b1f14ad0SJesse Barnes 			master_priv->sarea_priv->last_dispatch =
622b1f14ad0SJesse Barnes 				READ_BREADCRUMB(dev_priv);
623b1f14ad0SJesse Barnes 	}
624b1f14ad0SJesse Barnes 
625e7b4c6b1SDaniel Vetter 	snb_gt_irq_handler(dev, dev_priv, gt_iir);
626b1f14ad0SJesse Barnes 
627b1f14ad0SJesse Barnes 	if (de_iir & DE_GSE_IVB)
628b1f14ad0SJesse Barnes 		intel_opregion_gse_intr(dev);
629b1f14ad0SJesse Barnes 
630b1f14ad0SJesse Barnes 	if (de_iir & DE_PLANEA_FLIP_DONE_IVB) {
631b1f14ad0SJesse Barnes 		intel_prepare_page_flip(dev, 0);
632b1f14ad0SJesse Barnes 		intel_finish_page_flip_plane(dev, 0);
633b1f14ad0SJesse Barnes 	}
634b1f14ad0SJesse Barnes 
635b1f14ad0SJesse Barnes 	if (de_iir & DE_PLANEB_FLIP_DONE_IVB) {
636b1f14ad0SJesse Barnes 		intel_prepare_page_flip(dev, 1);
637b1f14ad0SJesse Barnes 		intel_finish_page_flip_plane(dev, 1);
638b1f14ad0SJesse Barnes 	}
639b1f14ad0SJesse Barnes 
640b1f14ad0SJesse Barnes 	if (de_iir & DE_PIPEA_VBLANK_IVB)
641b1f14ad0SJesse Barnes 		drm_handle_vblank(dev, 0);
642b1f14ad0SJesse Barnes 
643f6b07f45SDan Carpenter 	if (de_iir & DE_PIPEB_VBLANK_IVB)
644b1f14ad0SJesse Barnes 		drm_handle_vblank(dev, 1);
645b1f14ad0SJesse Barnes 
646b1f14ad0SJesse Barnes 	/* check event from PCH */
647b1f14ad0SJesse Barnes 	if (de_iir & DE_PCH_EVENT_IVB) {
648b1f14ad0SJesse Barnes 		if (pch_iir & SDE_HOTPLUG_MASK_CPT)
649b1f14ad0SJesse Barnes 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
650b1f14ad0SJesse Barnes 		pch_irq_handler(dev);
651b1f14ad0SJesse Barnes 	}
652b1f14ad0SJesse Barnes 
653fc6826d1SChris Wilson 	if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
654fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
655b1f14ad0SJesse Barnes 
656b1f14ad0SJesse Barnes 	/* should clear PCH hotplug event before clear CPU irq */
657b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, pch_iir);
658b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, gt_iir);
659b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, de_iir);
660b1f14ad0SJesse Barnes 	I915_WRITE(GEN6_PMIIR, pm_iir);
661b1f14ad0SJesse Barnes 
662b1f14ad0SJesse Barnes done:
663b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
664b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
665b1f14ad0SJesse Barnes 
666b1f14ad0SJesse Barnes 	return ret;
667b1f14ad0SJesse Barnes }
668b1f14ad0SJesse Barnes 
669e7b4c6b1SDaniel Vetter static void ilk_gt_irq_handler(struct drm_device *dev,
670e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
671e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
672e7b4c6b1SDaniel Vetter {
673e7b4c6b1SDaniel Vetter 	if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
674e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
675e7b4c6b1SDaniel Vetter 	if (gt_iir & GT_BSD_USER_INTERRUPT)
676e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
677e7b4c6b1SDaniel Vetter }
678e7b4c6b1SDaniel Vetter 
679f71d4af4SJesse Barnes static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
680036a4a7dSZhenyu Wang {
6814697995bSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
682036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
683036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
6843b8d8d91SJesse Barnes 	u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
6852d7b8366SYuanhan Liu 	u32 hotplug_mask;
686036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
687881f47b6SXiang, Haihao 
6884697995bSJesse Barnes 	atomic_inc(&dev_priv->irq_received);
6894697995bSJesse Barnes 
6902d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
6912d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
6922d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
6933143a2bfSChris Wilson 	POSTING_READ(DEIER);
6942d109a84SZou, Nanhai 
695036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
696036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
697c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
6983b8d8d91SJesse Barnes 	pm_iir = I915_READ(GEN6_PMIIR);
699036a4a7dSZhenyu Wang 
7003b8d8d91SJesse Barnes 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
7013b8d8d91SJesse Barnes 	    (!IS_GEN6(dev) || pm_iir == 0))
702c7c85101SZou Nan hai 		goto done;
703036a4a7dSZhenyu Wang 
7042d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
7052d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
7062d7b8366SYuanhan Liu 	else
7072d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
7082d7b8366SYuanhan Liu 
709036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
710036a4a7dSZhenyu Wang 
711036a4a7dSZhenyu Wang 	if (dev->primary->master) {
712036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
713036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
714036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
715036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
716036a4a7dSZhenyu Wang 	}
717036a4a7dSZhenyu Wang 
718e7b4c6b1SDaniel Vetter 	if (IS_GEN5(dev))
719e7b4c6b1SDaniel Vetter 		ilk_gt_irq_handler(dev, dev_priv, gt_iir);
720e7b4c6b1SDaniel Vetter 	else
721e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
722036a4a7dSZhenyu Wang 
72301c66889SZhao Yakui 	if (de_iir & DE_GSE)
7243b617967SChris Wilson 		intel_opregion_gse_intr(dev);
72501c66889SZhao Yakui 
726f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
727013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
7282bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
729013d5aa2SJesse Barnes 	}
730013d5aa2SJesse Barnes 
731f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
732f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
7332bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
734013d5aa2SJesse Barnes 	}
735c062df61SLi Peng 
736f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
737f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
738f072d2e7SZhenyu Wang 
739f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
740f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
741f072d2e7SZhenyu Wang 
742c650156aSZhenyu Wang 	/* check event from PCH */
743776ad806SJesse Barnes 	if (de_iir & DE_PCH_EVENT) {
744776ad806SJesse Barnes 		if (pch_iir & hotplug_mask)
745c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
746776ad806SJesse Barnes 		pch_irq_handler(dev);
747776ad806SJesse Barnes 	}
748c650156aSZhenyu Wang 
749f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
7507648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
751f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
752f97108d1SJesse Barnes 	}
753f97108d1SJesse Barnes 
754fc6826d1SChris Wilson 	if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
755fc6826d1SChris Wilson 		gen6_queue_rps_work(dev_priv, pm_iir);
7563b8d8d91SJesse Barnes 
757c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
758c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
759c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
760c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
7614912d041SBen Widawsky 	I915_WRITE(GEN6_PMIIR, pm_iir);
762036a4a7dSZhenyu Wang 
763c7c85101SZou Nan hai done:
7642d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
7653143a2bfSChris Wilson 	POSTING_READ(DEIER);
7662d109a84SZou, Nanhai 
767036a4a7dSZhenyu Wang 	return ret;
768036a4a7dSZhenyu Wang }
769036a4a7dSZhenyu Wang 
7708a905236SJesse Barnes /**
7718a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
7728a905236SJesse Barnes  * @work: work struct
7738a905236SJesse Barnes  *
7748a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
7758a905236SJesse Barnes  * was detected.
7768a905236SJesse Barnes  */
7778a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
7788a905236SJesse Barnes {
7798a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
7808a905236SJesse Barnes 						    error_work);
7818a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
782f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
783f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
784f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
7858a905236SJesse Barnes 
786f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
7878a905236SJesse Barnes 
788ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
78944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
790f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
791f803aa55SChris Wilson 		if (!i915_reset(dev, GRDOM_RENDER)) {
792ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
793f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
794f316a42cSBen Gamari 		}
79530dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
796f316a42cSBen Gamari 	}
7978a905236SJesse Barnes }
7988a905236SJesse Barnes 
7993bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
8009df30794SChris Wilson static struct drm_i915_error_object *
801bcfb2e28SChris Wilson i915_error_object_create(struct drm_i915_private *dev_priv,
80205394f39SChris Wilson 			 struct drm_i915_gem_object *src)
8039df30794SChris Wilson {
8049df30794SChris Wilson 	struct drm_i915_error_object *dst;
8059df30794SChris Wilson 	int page, page_count;
806e56660ddSChris Wilson 	u32 reloc_offset;
8079df30794SChris Wilson 
80805394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
8099df30794SChris Wilson 		return NULL;
8109df30794SChris Wilson 
81105394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
8129df30794SChris Wilson 
8139df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof(u32 *), GFP_ATOMIC);
8149df30794SChris Wilson 	if (dst == NULL)
8159df30794SChris Wilson 		return NULL;
8169df30794SChris Wilson 
81705394f39SChris Wilson 	reloc_offset = src->gtt_offset;
8189df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
819788885aeSAndrew Morton 		unsigned long flags;
820e56660ddSChris Wilson 		void *d;
821788885aeSAndrew Morton 
822e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
8239df30794SChris Wilson 		if (d == NULL)
8249df30794SChris Wilson 			goto unwind;
825e56660ddSChris Wilson 
826788885aeSAndrew Morton 		local_irq_save(flags);
82774898d7eSDaniel Vetter 		if (reloc_offset < dev_priv->mm.gtt_mappable_end &&
82874898d7eSDaniel Vetter 		    src->has_global_gtt_mapping) {
829172975aaSChris Wilson 			void __iomem *s;
830172975aaSChris Wilson 
831172975aaSChris Wilson 			/* Simply ignore tiling or any overlapping fence.
832172975aaSChris Wilson 			 * It's part of the error state, and this hopefully
833172975aaSChris Wilson 			 * captures what the GPU read.
834172975aaSChris Wilson 			 */
835172975aaSChris Wilson 
836e56660ddSChris Wilson 			s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
8373e4d3af5SPeter Zijlstra 						     reloc_offset);
838e56660ddSChris Wilson 			memcpy_fromio(d, s, PAGE_SIZE);
8393e4d3af5SPeter Zijlstra 			io_mapping_unmap_atomic(s);
840172975aaSChris Wilson 		} else {
841172975aaSChris Wilson 			void *s;
842172975aaSChris Wilson 
843172975aaSChris Wilson 			drm_clflush_pages(&src->pages[page], 1);
844172975aaSChris Wilson 
845172975aaSChris Wilson 			s = kmap_atomic(src->pages[page]);
846172975aaSChris Wilson 			memcpy(d, s, PAGE_SIZE);
847172975aaSChris Wilson 			kunmap_atomic(s);
848172975aaSChris Wilson 
849172975aaSChris Wilson 			drm_clflush_pages(&src->pages[page], 1);
850172975aaSChris Wilson 		}
851788885aeSAndrew Morton 		local_irq_restore(flags);
852e56660ddSChris Wilson 
8539df30794SChris Wilson 		dst->pages[page] = d;
854e56660ddSChris Wilson 
855e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
8569df30794SChris Wilson 	}
8579df30794SChris Wilson 	dst->page_count = page_count;
85805394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
8599df30794SChris Wilson 
8609df30794SChris Wilson 	return dst;
8619df30794SChris Wilson 
8629df30794SChris Wilson unwind:
8639df30794SChris Wilson 	while (page--)
8649df30794SChris Wilson 		kfree(dst->pages[page]);
8659df30794SChris Wilson 	kfree(dst);
8669df30794SChris Wilson 	return NULL;
8679df30794SChris Wilson }
8689df30794SChris Wilson 
8699df30794SChris Wilson static void
8709df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
8719df30794SChris Wilson {
8729df30794SChris Wilson 	int page;
8739df30794SChris Wilson 
8749df30794SChris Wilson 	if (obj == NULL)
8759df30794SChris Wilson 		return;
8769df30794SChris Wilson 
8779df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
8789df30794SChris Wilson 		kfree(obj->pages[page]);
8799df30794SChris Wilson 
8809df30794SChris Wilson 	kfree(obj);
8819df30794SChris Wilson }
8829df30794SChris Wilson 
8839df30794SChris Wilson static void
8849df30794SChris Wilson i915_error_state_free(struct drm_device *dev,
8859df30794SChris Wilson 		      struct drm_i915_error_state *error)
8869df30794SChris Wilson {
887e2f973d5SChris Wilson 	int i;
888e2f973d5SChris Wilson 
88952d39a21SChris Wilson 	for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
89052d39a21SChris Wilson 		i915_error_object_free(error->ring[i].batchbuffer);
89152d39a21SChris Wilson 		i915_error_object_free(error->ring[i].ringbuffer);
89252d39a21SChris Wilson 		kfree(error->ring[i].requests);
89352d39a21SChris Wilson 	}
894e2f973d5SChris Wilson 
8959df30794SChris Wilson 	kfree(error->active_bo);
8966ef3d427SChris Wilson 	kfree(error->overlay);
8979df30794SChris Wilson 	kfree(error);
8989df30794SChris Wilson }
8991b50247aSChris Wilson static void capture_bo(struct drm_i915_error_buffer *err,
9001b50247aSChris Wilson 		       struct drm_i915_gem_object *obj)
901c724e8a9SChris Wilson {
902c724e8a9SChris Wilson 	err->size = obj->base.size;
903c724e8a9SChris Wilson 	err->name = obj->base.name;
904c724e8a9SChris Wilson 	err->seqno = obj->last_rendering_seqno;
905c724e8a9SChris Wilson 	err->gtt_offset = obj->gtt_offset;
906c724e8a9SChris Wilson 	err->read_domains = obj->base.read_domains;
907c724e8a9SChris Wilson 	err->write_domain = obj->base.write_domain;
908c724e8a9SChris Wilson 	err->fence_reg = obj->fence_reg;
909c724e8a9SChris Wilson 	err->pinned = 0;
910c724e8a9SChris Wilson 	if (obj->pin_count > 0)
911c724e8a9SChris Wilson 		err->pinned = 1;
912c724e8a9SChris Wilson 	if (obj->user_pin_count > 0)
913c724e8a9SChris Wilson 		err->pinned = -1;
914c724e8a9SChris Wilson 	err->tiling = obj->tiling_mode;
915c724e8a9SChris Wilson 	err->dirty = obj->dirty;
916c724e8a9SChris Wilson 	err->purgeable = obj->madv != I915_MADV_WILLNEED;
91796154f2fSDaniel Vetter 	err->ring = obj->ring ? obj->ring->id : -1;
91893dfb40cSChris Wilson 	err->cache_level = obj->cache_level;
9191b50247aSChris Wilson }
920c724e8a9SChris Wilson 
9211b50247aSChris Wilson static u32 capture_active_bo(struct drm_i915_error_buffer *err,
9221b50247aSChris Wilson 			     int count, struct list_head *head)
9231b50247aSChris Wilson {
9241b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
9251b50247aSChris Wilson 	int i = 0;
9261b50247aSChris Wilson 
9271b50247aSChris Wilson 	list_for_each_entry(obj, head, mm_list) {
9281b50247aSChris Wilson 		capture_bo(err++, obj);
929c724e8a9SChris Wilson 		if (++i == count)
930c724e8a9SChris Wilson 			break;
9311b50247aSChris Wilson 	}
932c724e8a9SChris Wilson 
9331b50247aSChris Wilson 	return i;
9341b50247aSChris Wilson }
9351b50247aSChris Wilson 
9361b50247aSChris Wilson static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
9371b50247aSChris Wilson 			     int count, struct list_head *head)
9381b50247aSChris Wilson {
9391b50247aSChris Wilson 	struct drm_i915_gem_object *obj;
9401b50247aSChris Wilson 	int i = 0;
9411b50247aSChris Wilson 
9421b50247aSChris Wilson 	list_for_each_entry(obj, head, gtt_list) {
9431b50247aSChris Wilson 		if (obj->pin_count == 0)
9441b50247aSChris Wilson 			continue;
9451b50247aSChris Wilson 
9461b50247aSChris Wilson 		capture_bo(err++, obj);
9471b50247aSChris Wilson 		if (++i == count)
9481b50247aSChris Wilson 			break;
949c724e8a9SChris Wilson 	}
950c724e8a9SChris Wilson 
951c724e8a9SChris Wilson 	return i;
952c724e8a9SChris Wilson }
953c724e8a9SChris Wilson 
954748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
955748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
956748ebc60SChris Wilson {
957748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
958748ebc60SChris Wilson 	int i;
959748ebc60SChris Wilson 
960748ebc60SChris Wilson 	/* Fences */
961748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
962775d17b6SDaniel Vetter 	case 7:
963748ebc60SChris Wilson 	case 6:
964748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
965748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
966748ebc60SChris Wilson 		break;
967748ebc60SChris Wilson 	case 5:
968748ebc60SChris Wilson 	case 4:
969748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
970748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
971748ebc60SChris Wilson 		break;
972748ebc60SChris Wilson 	case 3:
973748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
974748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
975748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
976748ebc60SChris Wilson 	case 2:
977748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
978748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
979748ebc60SChris Wilson 		break;
980748ebc60SChris Wilson 
981748ebc60SChris Wilson 	}
982748ebc60SChris Wilson }
983748ebc60SChris Wilson 
984bcfb2e28SChris Wilson static struct drm_i915_error_object *
985bcfb2e28SChris Wilson i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
986bcfb2e28SChris Wilson 			     struct intel_ring_buffer *ring)
987bcfb2e28SChris Wilson {
988bcfb2e28SChris Wilson 	struct drm_i915_gem_object *obj;
989bcfb2e28SChris Wilson 	u32 seqno;
990bcfb2e28SChris Wilson 
991bcfb2e28SChris Wilson 	if (!ring->get_seqno)
992bcfb2e28SChris Wilson 		return NULL;
993bcfb2e28SChris Wilson 
994bcfb2e28SChris Wilson 	seqno = ring->get_seqno(ring);
995bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
996bcfb2e28SChris Wilson 		if (obj->ring != ring)
997bcfb2e28SChris Wilson 			continue;
998bcfb2e28SChris Wilson 
999c37d9a5dSChris Wilson 		if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
1000bcfb2e28SChris Wilson 			continue;
1001bcfb2e28SChris Wilson 
1002bcfb2e28SChris Wilson 		if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1003bcfb2e28SChris Wilson 			continue;
1004bcfb2e28SChris Wilson 
1005bcfb2e28SChris Wilson 		/* We need to copy these to an anonymous buffer as the simplest
1006bcfb2e28SChris Wilson 		 * method to avoid being overwritten by userspace.
1007bcfb2e28SChris Wilson 		 */
1008bcfb2e28SChris Wilson 		return i915_error_object_create(dev_priv, obj);
1009bcfb2e28SChris Wilson 	}
1010bcfb2e28SChris Wilson 
1011bcfb2e28SChris Wilson 	return NULL;
1012bcfb2e28SChris Wilson }
1013bcfb2e28SChris Wilson 
1014d27b1e0eSDaniel Vetter static void i915_record_ring_state(struct drm_device *dev,
1015d27b1e0eSDaniel Vetter 				   struct drm_i915_error_state *error,
1016d27b1e0eSDaniel Vetter 				   struct intel_ring_buffer *ring)
1017d27b1e0eSDaniel Vetter {
1018d27b1e0eSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1019d27b1e0eSDaniel Vetter 
102033f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
102133f3f518SDaniel Vetter 		error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
10227e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][0]
10237e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_0(ring->mmio_base));
10247e3b8737SDaniel Vetter 		error->semaphore_mboxes[ring->id][1]
10257e3b8737SDaniel Vetter 			= I915_READ(RING_SYNC_1(ring->mmio_base));
102633f3f518SDaniel Vetter 	}
1027c1cd90edSDaniel Vetter 
1028d27b1e0eSDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 4) {
10299d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
1030d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1031d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1032d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
1033c1cd90edSDaniel Vetter 		error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
1034d27b1e0eSDaniel Vetter 		if (ring->id == RCS) {
1035d27b1e0eSDaniel Vetter 			error->instdone1 = I915_READ(INSTDONE1);
1036d27b1e0eSDaniel Vetter 			error->bbaddr = I915_READ64(BB_ADDR);
1037d27b1e0eSDaniel Vetter 		}
1038d27b1e0eSDaniel Vetter 	} else {
10399d2f41faSDaniel Vetter 		error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
1040d27b1e0eSDaniel Vetter 		error->ipeir[ring->id] = I915_READ(IPEIR);
1041d27b1e0eSDaniel Vetter 		error->ipehr[ring->id] = I915_READ(IPEHR);
1042d27b1e0eSDaniel Vetter 		error->instdone[ring->id] = I915_READ(INSTDONE);
1043d27b1e0eSDaniel Vetter 	}
1044d27b1e0eSDaniel Vetter 
1045c1cd90edSDaniel Vetter 	error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
1046d27b1e0eSDaniel Vetter 	error->seqno[ring->id] = ring->get_seqno(ring);
1047d27b1e0eSDaniel Vetter 	error->acthd[ring->id] = intel_ring_get_active_head(ring);
1048c1cd90edSDaniel Vetter 	error->head[ring->id] = I915_READ_HEAD(ring);
1049c1cd90edSDaniel Vetter 	error->tail[ring->id] = I915_READ_TAIL(ring);
10507e3b8737SDaniel Vetter 
10517e3b8737SDaniel Vetter 	error->cpu_ring_head[ring->id] = ring->head;
10527e3b8737SDaniel Vetter 	error->cpu_ring_tail[ring->id] = ring->tail;
1053d27b1e0eSDaniel Vetter }
1054d27b1e0eSDaniel Vetter 
105552d39a21SChris Wilson static void i915_gem_record_rings(struct drm_device *dev,
105652d39a21SChris Wilson 				  struct drm_i915_error_state *error)
105752d39a21SChris Wilson {
105852d39a21SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
105952d39a21SChris Wilson 	struct drm_i915_gem_request *request;
106052d39a21SChris Wilson 	int i, count;
106152d39a21SChris Wilson 
106252d39a21SChris Wilson 	for (i = 0; i < I915_NUM_RINGS; i++) {
106352d39a21SChris Wilson 		struct intel_ring_buffer *ring = &dev_priv->ring[i];
106452d39a21SChris Wilson 
106552d39a21SChris Wilson 		if (ring->obj == NULL)
106652d39a21SChris Wilson 			continue;
106752d39a21SChris Wilson 
106852d39a21SChris Wilson 		i915_record_ring_state(dev, error, ring);
106952d39a21SChris Wilson 
107052d39a21SChris Wilson 		error->ring[i].batchbuffer =
107152d39a21SChris Wilson 			i915_error_first_batchbuffer(dev_priv, ring);
107252d39a21SChris Wilson 
107352d39a21SChris Wilson 		error->ring[i].ringbuffer =
107452d39a21SChris Wilson 			i915_error_object_create(dev_priv, ring->obj);
107552d39a21SChris Wilson 
107652d39a21SChris Wilson 		count = 0;
107752d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list)
107852d39a21SChris Wilson 			count++;
107952d39a21SChris Wilson 
108052d39a21SChris Wilson 		error->ring[i].num_requests = count;
108152d39a21SChris Wilson 		error->ring[i].requests =
108252d39a21SChris Wilson 			kmalloc(count*sizeof(struct drm_i915_error_request),
108352d39a21SChris Wilson 				GFP_ATOMIC);
108452d39a21SChris Wilson 		if (error->ring[i].requests == NULL) {
108552d39a21SChris Wilson 			error->ring[i].num_requests = 0;
108652d39a21SChris Wilson 			continue;
108752d39a21SChris Wilson 		}
108852d39a21SChris Wilson 
108952d39a21SChris Wilson 		count = 0;
109052d39a21SChris Wilson 		list_for_each_entry(request, &ring->request_list, list) {
109152d39a21SChris Wilson 			struct drm_i915_error_request *erq;
109252d39a21SChris Wilson 
109352d39a21SChris Wilson 			erq = &error->ring[i].requests[count++];
109452d39a21SChris Wilson 			erq->seqno = request->seqno;
109552d39a21SChris Wilson 			erq->jiffies = request->emitted_jiffies;
1096ee4f42b1SChris Wilson 			erq->tail = request->tail;
109752d39a21SChris Wilson 		}
109852d39a21SChris Wilson 	}
109952d39a21SChris Wilson }
110052d39a21SChris Wilson 
11018a905236SJesse Barnes /**
11028a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
11038a905236SJesse Barnes  * @dev: drm device
11048a905236SJesse Barnes  *
11058a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
11068a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
11078a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
11088a905236SJesse Barnes  * to pick up.
11098a905236SJesse Barnes  */
111063eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
111163eeaf38SJesse Barnes {
111263eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
111305394f39SChris Wilson 	struct drm_i915_gem_object *obj;
111463eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
111563eeaf38SJesse Barnes 	unsigned long flags;
11169db4a9c7SJesse Barnes 	int i, pipe;
111763eeaf38SJesse Barnes 
111863eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
11199df30794SChris Wilson 	error = dev_priv->first_error;
11209df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
11219df30794SChris Wilson 	if (error)
11229df30794SChris Wilson 		return;
112363eeaf38SJesse Barnes 
11249db4a9c7SJesse Barnes 	/* Account for pipe specific data like PIPE*STAT */
112533f3f518SDaniel Vetter 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
112663eeaf38SJesse Barnes 	if (!error) {
11279df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
11289df30794SChris Wilson 		return;
112963eeaf38SJesse Barnes 	}
113063eeaf38SJesse Barnes 
1131b6f7833bSChris Wilson 	DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
1132b6f7833bSChris Wilson 		 dev->primary->index);
11332fa772f3SChris Wilson 
113463eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
113563eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
11369db4a9c7SJesse Barnes 	for_each_pipe(pipe)
11379db4a9c7SJesse Barnes 		error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
1138d27b1e0eSDaniel Vetter 
113933f3f518SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
1140f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
114133f3f518SDaniel Vetter 		error->done_reg = I915_READ(DONE_REG);
114233f3f518SDaniel Vetter 	}
1143add354ddSChris Wilson 
1144748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
114552d39a21SChris Wilson 	i915_gem_record_rings(dev, error);
11469df30794SChris Wilson 
1147c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
11489df30794SChris Wilson 	error->active_bo = NULL;
1149c724e8a9SChris Wilson 	error->pinned_bo = NULL;
11509df30794SChris Wilson 
1151bcfb2e28SChris Wilson 	i = 0;
1152bcfb2e28SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1153bcfb2e28SChris Wilson 		i++;
1154bcfb2e28SChris Wilson 	error->active_bo_count = i;
11551b50247aSChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list)
11561b50247aSChris Wilson 		if (obj->pin_count)
1157bcfb2e28SChris Wilson 			i++;
1158bcfb2e28SChris Wilson 	error->pinned_bo_count = i - error->active_bo_count;
1159c724e8a9SChris Wilson 
11608e934dbfSChris Wilson 	error->active_bo = NULL;
11618e934dbfSChris Wilson 	error->pinned_bo = NULL;
1162bcfb2e28SChris Wilson 	if (i) {
1163bcfb2e28SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
11649df30794SChris Wilson 					   GFP_ATOMIC);
1165c724e8a9SChris Wilson 		if (error->active_bo)
1166c724e8a9SChris Wilson 			error->pinned_bo =
1167c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
11689df30794SChris Wilson 	}
1169c724e8a9SChris Wilson 
1170c724e8a9SChris Wilson 	if (error->active_bo)
1171c724e8a9SChris Wilson 		error->active_bo_count =
11721b50247aSChris Wilson 			capture_active_bo(error->active_bo,
1173c724e8a9SChris Wilson 					  error->active_bo_count,
1174c724e8a9SChris Wilson 					  &dev_priv->mm.active_list);
1175c724e8a9SChris Wilson 
1176c724e8a9SChris Wilson 	if (error->pinned_bo)
1177c724e8a9SChris Wilson 		error->pinned_bo_count =
11781b50247aSChris Wilson 			capture_pinned_bo(error->pinned_bo,
1179c724e8a9SChris Wilson 					  error->pinned_bo_count,
11801b50247aSChris Wilson 					  &dev_priv->mm.gtt_list);
118163eeaf38SJesse Barnes 
11828a905236SJesse Barnes 	do_gettimeofday(&error->time);
11838a905236SJesse Barnes 
11846ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
1185c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
11866ef3d427SChris Wilson 
11879df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
11889df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
118963eeaf38SJesse Barnes 		dev_priv->first_error = error;
11909df30794SChris Wilson 		error = NULL;
11919df30794SChris Wilson 	}
119263eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
11939df30794SChris Wilson 
11949df30794SChris Wilson 	if (error)
11959df30794SChris Wilson 		i915_error_state_free(dev, error);
11969df30794SChris Wilson }
11979df30794SChris Wilson 
11989df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
11999df30794SChris Wilson {
12009df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
12019df30794SChris Wilson 	struct drm_i915_error_state *error;
12026dc0e816SBen Widawsky 	unsigned long flags;
12039df30794SChris Wilson 
12046dc0e816SBen Widawsky 	spin_lock_irqsave(&dev_priv->error_lock, flags);
12059df30794SChris Wilson 	error = dev_priv->first_error;
12069df30794SChris Wilson 	dev_priv->first_error = NULL;
12076dc0e816SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
12089df30794SChris Wilson 
12099df30794SChris Wilson 	if (error)
12109df30794SChris Wilson 		i915_error_state_free(dev, error);
121163eeaf38SJesse Barnes }
12123bd3c932SChris Wilson #else
12133bd3c932SChris Wilson #define i915_capture_error_state(x)
12143bd3c932SChris Wilson #endif
121563eeaf38SJesse Barnes 
121635aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
1217c0e09200SDave Airlie {
12188a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
121963eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
12209db4a9c7SJesse Barnes 	int pipe;
122163eeaf38SJesse Barnes 
122235aed2e6SChris Wilson 	if (!eir)
122335aed2e6SChris Wilson 		return;
122463eeaf38SJesse Barnes 
1225a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
12268a905236SJesse Barnes 
12278a905236SJesse Barnes 	if (IS_G4X(dev)) {
12288a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
12298a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
12308a905236SJesse Barnes 
1231a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1232a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1233a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n",
12348a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
1235a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1236a70491ccSJoe Perches 			pr_err("  INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1237a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
12388a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
12393143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
12408a905236SJesse Barnes 		}
12418a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
12428a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
1243a70491ccSJoe Perches 			pr_err("page table error\n");
1244a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
12458a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
12463143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
12478a905236SJesse Barnes 		}
12488a905236SJesse Barnes 	}
12498a905236SJesse Barnes 
1250a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
125163eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
125263eeaf38SJesse 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);
125563eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
12563143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
125763eeaf38SJesse Barnes 		}
12588a905236SJesse Barnes 	}
12598a905236SJesse Barnes 
126063eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
1261a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
12629db4a9c7SJesse Barnes 		for_each_pipe(pipe)
1263a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
12649db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
126563eeaf38SJesse Barnes 		/* pipestat has already been acked */
126663eeaf38SJesse Barnes 	}
126763eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
1268a70491ccSJoe Perches 		pr_err("instruction error\n");
1269a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
1270a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
127163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
127263eeaf38SJesse Barnes 
1273a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
1274a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
1275a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n", I915_READ(INSTDONE));
1276a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
127763eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
12783143a2bfSChris Wilson 			POSTING_READ(IPEIR);
127963eeaf38SJesse Barnes 		} else {
128063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
128163eeaf38SJesse Barnes 
1282a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1283a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1284a70491ccSJoe Perches 			pr_err("  INSTDONE: 0x%08x\n",
128563eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
1286a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
1287a70491ccSJoe Perches 			pr_err("  INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1288a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
128963eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
12903143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
129163eeaf38SJesse Barnes 		}
129263eeaf38SJesse Barnes 	}
129363eeaf38SJesse Barnes 
129463eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
12953143a2bfSChris Wilson 	POSTING_READ(EIR);
129663eeaf38SJesse Barnes 	eir = I915_READ(EIR);
129763eeaf38SJesse Barnes 	if (eir) {
129863eeaf38SJesse Barnes 		/*
129963eeaf38SJesse Barnes 		 * some errors might have become stuck,
130063eeaf38SJesse Barnes 		 * mask them.
130163eeaf38SJesse Barnes 		 */
130263eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
130363eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
130463eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
130563eeaf38SJesse Barnes 	}
130635aed2e6SChris Wilson }
130735aed2e6SChris Wilson 
130835aed2e6SChris Wilson /**
130935aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
131035aed2e6SChris Wilson  * @dev: drm device
131135aed2e6SChris Wilson  *
131235aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
131335aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
131435aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
131535aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
131635aed2e6SChris Wilson  * of a ring dump etc.).
131735aed2e6SChris Wilson  */
1318527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
131935aed2e6SChris Wilson {
132035aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
132135aed2e6SChris Wilson 
132235aed2e6SChris Wilson 	i915_capture_error_state(dev);
132335aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
13248a905236SJesse Barnes 
1325ba1234d1SBen Gamari 	if (wedged) {
132630dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
1327ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
1328ba1234d1SBen Gamari 
132911ed50ecSBen Gamari 		/*
133011ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
133111ed50ecSBen Gamari 		 */
13321ec14ad3SChris Wilson 		wake_up_all(&dev_priv->ring[RCS].irq_queue);
1333f787a5f5SChris Wilson 		if (HAS_BSD(dev))
13341ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[VCS].irq_queue);
1335549f7365SChris Wilson 		if (HAS_BLT(dev))
13361ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[BCS].irq_queue);
133711ed50ecSBen Gamari 	}
133811ed50ecSBen Gamari 
13399c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
13408a905236SJesse Barnes }
13418a905236SJesse Barnes 
13424e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
13434e5359cdSSimon Farnsworth {
13444e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
13454e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13464e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
134705394f39SChris Wilson 	struct drm_i915_gem_object *obj;
13484e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
13494e5359cdSSimon Farnsworth 	unsigned long flags;
13504e5359cdSSimon Farnsworth 	bool stall_detected;
13514e5359cdSSimon Farnsworth 
13524e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
13534e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
13544e5359cdSSimon Farnsworth 		return;
13554e5359cdSSimon Farnsworth 
13564e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
13574e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
13584e5359cdSSimon Farnsworth 
13594e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
13604e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
13614e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
13624e5359cdSSimon Farnsworth 		return;
13634e5359cdSSimon Farnsworth 	}
13644e5359cdSSimon Farnsworth 
13654e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
136605394f39SChris Wilson 	obj = work->pending_flip_obj;
1367a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
13689db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
1369446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1370446f2545SArmin Reese 					obj->gtt_offset;
13714e5359cdSSimon Farnsworth 	} else {
13729db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
137305394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
137401f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
13754e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
13764e5359cdSSimon Farnsworth 	}
13774e5359cdSSimon Farnsworth 
13784e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
13794e5359cdSSimon Farnsworth 
13804e5359cdSSimon Farnsworth 	if (stall_detected) {
13814e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
13824e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
13834e5359cdSSimon Farnsworth 	}
13844e5359cdSSimon Farnsworth }
13854e5359cdSSimon Farnsworth 
1386c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
1387c0e09200SDave Airlie {
1388c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
13897c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1390c0e09200SDave Airlie 
1391c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
1392c0e09200SDave Airlie 
139344d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
1394c0e09200SDave Airlie 
1395c99b058fSKristian Høgsberg 	dev_priv->counter++;
1396c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
1397c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
13987c1c2871SDave Airlie 	if (master_priv->sarea_priv)
13997c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
1400c0e09200SDave Airlie 
1401e1f99ce6SChris Wilson 	if (BEGIN_LP_RING(4) == 0) {
1402585fb111SJesse Barnes 		OUT_RING(MI_STORE_DWORD_INDEX);
14030baf823aSKeith Packard 		OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1404c0e09200SDave Airlie 		OUT_RING(dev_priv->counter);
1405585fb111SJesse Barnes 		OUT_RING(MI_USER_INTERRUPT);
1406c0e09200SDave Airlie 		ADVANCE_LP_RING();
1407e1f99ce6SChris Wilson 	}
1408c0e09200SDave Airlie 
1409c0e09200SDave Airlie 	return dev_priv->counter;
1410c0e09200SDave Airlie }
1411c0e09200SDave Airlie 
1412c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
1413c0e09200SDave Airlie {
1414c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
14157c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1416c0e09200SDave Airlie 	int ret = 0;
14171ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
1418c0e09200SDave Airlie 
141944d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
1420c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
1421c0e09200SDave Airlie 
1422ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
14237c1c2871SDave Airlie 		if (master_priv->sarea_priv)
14247c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
1425c0e09200SDave Airlie 		return 0;
1426ed4cb414SEric Anholt 	}
1427c0e09200SDave Airlie 
14287c1c2871SDave Airlie 	if (master_priv->sarea_priv)
14297c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1430c0e09200SDave Airlie 
1431b13c2b96SChris Wilson 	if (ring->irq_get(ring)) {
14321ec14ad3SChris Wilson 		DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1433c0e09200SDave Airlie 			    READ_BREADCRUMB(dev_priv) >= irq_nr);
14341ec14ad3SChris Wilson 		ring->irq_put(ring);
14355a9a8d1aSChris Wilson 	} else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
14365a9a8d1aSChris Wilson 		ret = -EBUSY;
1437c0e09200SDave Airlie 
1438c0e09200SDave Airlie 	if (ret == -EBUSY) {
1439c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
1440c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1441c0e09200SDave Airlie 	}
1442c0e09200SDave Airlie 
1443c0e09200SDave Airlie 	return ret;
1444c0e09200SDave Airlie }
1445c0e09200SDave Airlie 
1446c0e09200SDave Airlie /* Needs the lock as it touches the ring.
1447c0e09200SDave Airlie  */
1448c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
1449c0e09200SDave Airlie 			 struct drm_file *file_priv)
1450c0e09200SDave Airlie {
1451c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1452c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
1453c0e09200SDave Airlie 	int result;
1454c0e09200SDave Airlie 
1455cd9d4e9fSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET))
1456cd9d4e9fSDaniel Vetter 		return -ENODEV;
1457cd9d4e9fSDaniel Vetter 
14581ec14ad3SChris Wilson 	if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
1459c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1460c0e09200SDave Airlie 		return -EINVAL;
1461c0e09200SDave Airlie 	}
1462299eb93cSEric Anholt 
1463299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1464299eb93cSEric Anholt 
1465546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
1466c0e09200SDave Airlie 	result = i915_emit_irq(dev);
1467546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
1468c0e09200SDave Airlie 
1469c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
1470c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
1471c0e09200SDave Airlie 		return -EFAULT;
1472c0e09200SDave Airlie 	}
1473c0e09200SDave Airlie 
1474c0e09200SDave Airlie 	return 0;
1475c0e09200SDave Airlie }
1476c0e09200SDave Airlie 
1477c0e09200SDave Airlie /* Doesn't need the hardware lock.
1478c0e09200SDave Airlie  */
1479c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
1480c0e09200SDave Airlie 			 struct drm_file *file_priv)
1481c0e09200SDave Airlie {
1482c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1483c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
1484c0e09200SDave Airlie 
1485cd9d4e9fSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET))
1486cd9d4e9fSDaniel Vetter 		return -ENODEV;
1487cd9d4e9fSDaniel Vetter 
1488c0e09200SDave Airlie 	if (!dev_priv) {
1489c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1490c0e09200SDave Airlie 		return -EINVAL;
1491c0e09200SDave Airlie 	}
1492c0e09200SDave Airlie 
1493c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
1494c0e09200SDave Airlie }
1495c0e09200SDave Airlie 
149642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
149742f52ef8SKeith Packard  * we use as a pipe index
149842f52ef8SKeith Packard  */
1499f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
15000a3e67a4SJesse Barnes {
15010a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1502e9d21d7fSKeith Packard 	unsigned long irqflags;
150371e0ffa5SJesse Barnes 
15045eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
150571e0ffa5SJesse Barnes 		return -EINVAL;
15060a3e67a4SJesse Barnes 
15071ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1508f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
15097c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
15107c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
15110a3e67a4SJesse Barnes 	else
15127c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
15137c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
15148692d00eSChris Wilson 
15158692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
15168692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15176b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
15181ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15198692d00eSChris Wilson 
15200a3e67a4SJesse Barnes 	return 0;
15210a3e67a4SJesse Barnes }
15220a3e67a4SJesse Barnes 
1523f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1524f796cf8fSJesse Barnes {
1525f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1526f796cf8fSJesse Barnes 	unsigned long irqflags;
1527f796cf8fSJesse Barnes 
1528f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1529f796cf8fSJesse Barnes 		return -EINVAL;
1530f796cf8fSJesse Barnes 
1531f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1532f796cf8fSJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1533f796cf8fSJesse Barnes 				    DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1534f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1535f796cf8fSJesse Barnes 
1536f796cf8fSJesse Barnes 	return 0;
1537f796cf8fSJesse Barnes }
1538f796cf8fSJesse Barnes 
1539f71d4af4SJesse Barnes static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1540b1f14ad0SJesse Barnes {
1541b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1542b1f14ad0SJesse Barnes 	unsigned long irqflags;
1543b1f14ad0SJesse Barnes 
1544b1f14ad0SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
1545b1f14ad0SJesse Barnes 		return -EINVAL;
1546b1f14ad0SJesse Barnes 
1547b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1548b1f14ad0SJesse Barnes 	ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1549b1f14ad0SJesse Barnes 				    DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1550b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1551b1f14ad0SJesse Barnes 
1552b1f14ad0SJesse Barnes 	return 0;
1553b1f14ad0SJesse Barnes }
1554b1f14ad0SJesse Barnes 
15557e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
15567e231dbeSJesse Barnes {
15577e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
15587e231dbeSJesse Barnes 	unsigned long irqflags;
15597e231dbeSJesse Barnes 	u32 dpfl, imr;
15607e231dbeSJesse Barnes 
15617e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
15627e231dbeSJesse Barnes 		return -EINVAL;
15637e231dbeSJesse Barnes 
15647e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15657e231dbeSJesse Barnes 	dpfl = I915_READ(VLV_DPFLIPSTAT);
15667e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
15677e231dbeSJesse Barnes 	if (pipe == 0) {
15687e231dbeSJesse Barnes 		dpfl |= PIPEA_VBLANK_INT_EN;
15697e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
15707e231dbeSJesse Barnes 	} else {
15717e231dbeSJesse Barnes 		dpfl |= PIPEA_VBLANK_INT_EN;
15727e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
15737e231dbeSJesse Barnes 	}
15747e231dbeSJesse Barnes 	I915_WRITE(VLV_DPFLIPSTAT, dpfl);
15757e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
15767e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15777e231dbeSJesse Barnes 
15787e231dbeSJesse Barnes 	return 0;
15797e231dbeSJesse Barnes }
15807e231dbeSJesse Barnes 
158142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
158242f52ef8SKeith Packard  * we use as a pipe index
158342f52ef8SKeith Packard  */
1584f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
15850a3e67a4SJesse Barnes {
15860a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1587e9d21d7fSKeith Packard 	unsigned long irqflags;
15880a3e67a4SJesse Barnes 
15891ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15908692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
15916b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
15928692d00eSChris Wilson 
15937c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
15947c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
15957c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
15961ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15970a3e67a4SJesse Barnes }
15980a3e67a4SJesse Barnes 
1599f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1600f796cf8fSJesse Barnes {
1601f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1602f796cf8fSJesse Barnes 	unsigned long irqflags;
1603f796cf8fSJesse Barnes 
1604f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1605f796cf8fSJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1606f796cf8fSJesse Barnes 				     DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1607f796cf8fSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1608f796cf8fSJesse Barnes }
1609f796cf8fSJesse Barnes 
1610f71d4af4SJesse Barnes static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1611b1f14ad0SJesse Barnes {
1612b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1613b1f14ad0SJesse Barnes 	unsigned long irqflags;
1614b1f14ad0SJesse Barnes 
1615b1f14ad0SJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1616b1f14ad0SJesse Barnes 	ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1617b1f14ad0SJesse Barnes 				     DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1618b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1619b1f14ad0SJesse Barnes }
1620b1f14ad0SJesse Barnes 
16217e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
16227e231dbeSJesse Barnes {
16237e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16247e231dbeSJesse Barnes 	unsigned long irqflags;
16257e231dbeSJesse Barnes 	u32 dpfl, imr;
16267e231dbeSJesse Barnes 
16277e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
16287e231dbeSJesse Barnes 	dpfl = I915_READ(VLV_DPFLIPSTAT);
16297e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
16307e231dbeSJesse Barnes 	if (pipe == 0) {
16317e231dbeSJesse Barnes 		dpfl &= ~PIPEA_VBLANK_INT_EN;
16327e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
16337e231dbeSJesse Barnes 	} else {
16347e231dbeSJesse Barnes 		dpfl &= ~PIPEB_VBLANK_INT_EN;
16357e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
16367e231dbeSJesse Barnes 	}
16377e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
16387e231dbeSJesse Barnes 	I915_WRITE(VLV_DPFLIPSTAT, dpfl);
16397e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
16407e231dbeSJesse Barnes }
16417e231dbeSJesse Barnes 
16427e231dbeSJesse Barnes 
1643c0e09200SDave Airlie /* Set the vblank monitor pipe
1644c0e09200SDave Airlie  */
1645c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1646c0e09200SDave Airlie 			 struct drm_file *file_priv)
1647c0e09200SDave Airlie {
1648c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1649c0e09200SDave Airlie 
1650cd9d4e9fSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET))
1651cd9d4e9fSDaniel Vetter 		return -ENODEV;
1652cd9d4e9fSDaniel Vetter 
1653c0e09200SDave Airlie 	if (!dev_priv) {
1654c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1655c0e09200SDave Airlie 		return -EINVAL;
1656c0e09200SDave Airlie 	}
1657c0e09200SDave Airlie 
1658c0e09200SDave Airlie 	return 0;
1659c0e09200SDave Airlie }
1660c0e09200SDave Airlie 
1661c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1662c0e09200SDave Airlie 			 struct drm_file *file_priv)
1663c0e09200SDave Airlie {
1664c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1665c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
1666c0e09200SDave Airlie 
1667cd9d4e9fSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET))
1668cd9d4e9fSDaniel Vetter 		return -ENODEV;
1669cd9d4e9fSDaniel Vetter 
1670c0e09200SDave Airlie 	if (!dev_priv) {
1671c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1672c0e09200SDave Airlie 		return -EINVAL;
1673c0e09200SDave Airlie 	}
1674c0e09200SDave Airlie 
16750a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1676c0e09200SDave Airlie 
1677c0e09200SDave Airlie 	return 0;
1678c0e09200SDave Airlie }
1679c0e09200SDave Airlie 
1680c0e09200SDave Airlie /**
1681c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
1682c0e09200SDave Airlie  */
1683c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
1684c0e09200SDave Airlie 		     struct drm_file *file_priv)
1685c0e09200SDave Airlie {
1686bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1687bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1688bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1689bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1690bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1691bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1692bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1693bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1694bd95e0a4SEric Anholt 	 *
1695bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1696bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1697bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1698bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
16990a3e67a4SJesse Barnes 	 */
1700c0e09200SDave Airlie 	return -EINVAL;
1701c0e09200SDave Airlie }
1702c0e09200SDave Airlie 
1703893eead0SChris Wilson static u32
1704893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1705852835f3SZou Nan hai {
1706893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1707893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1708893eead0SChris Wilson }
1709893eead0SChris Wilson 
1710893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1711893eead0SChris Wilson {
1712893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1713893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1714893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
1715b2223497SChris Wilson 		if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
1716893eead0SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1717893eead0SChris Wilson 				  ring->name,
1718b2223497SChris Wilson 				  ring->waiting_seqno,
1719893eead0SChris Wilson 				  ring->get_seqno(ring));
1720893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1721893eead0SChris Wilson 			*err = true;
1722893eead0SChris Wilson 		}
1723893eead0SChris Wilson 		return true;
1724893eead0SChris Wilson 	}
1725893eead0SChris Wilson 	return false;
1726f65d9421SBen Gamari }
1727f65d9421SBen Gamari 
17281ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
17291ec14ad3SChris Wilson {
17301ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
17311ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
17321ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
17331ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
17341ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
17351ec14ad3SChris Wilson 			  ring->name);
17361ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
17371ec14ad3SChris Wilson 		return true;
17381ec14ad3SChris Wilson 	}
17391ec14ad3SChris Wilson 	return false;
17401ec14ad3SChris Wilson }
17411ec14ad3SChris Wilson 
1742d1e61e7fSChris Wilson static bool i915_hangcheck_hung(struct drm_device *dev)
1743d1e61e7fSChris Wilson {
1744d1e61e7fSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1745d1e61e7fSChris Wilson 
1746d1e61e7fSChris Wilson 	if (dev_priv->hangcheck_count++ > 1) {
1747d1e61e7fSChris Wilson 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1748d1e61e7fSChris Wilson 		i915_handle_error(dev, true);
1749d1e61e7fSChris Wilson 
1750d1e61e7fSChris Wilson 		if (!IS_GEN2(dev)) {
1751d1e61e7fSChris Wilson 			/* Is the chip hanging on a WAIT_FOR_EVENT?
1752d1e61e7fSChris Wilson 			 * If so we can simply poke the RB_WAIT bit
1753d1e61e7fSChris Wilson 			 * and break the hang. This should work on
1754d1e61e7fSChris Wilson 			 * all but the second generation chipsets.
1755d1e61e7fSChris Wilson 			 */
1756d1e61e7fSChris Wilson 			if (kick_ring(&dev_priv->ring[RCS]))
1757d1e61e7fSChris Wilson 				return false;
1758d1e61e7fSChris Wilson 
1759d1e61e7fSChris Wilson 			if (HAS_BSD(dev) && kick_ring(&dev_priv->ring[VCS]))
1760d1e61e7fSChris Wilson 				return false;
1761d1e61e7fSChris Wilson 
1762d1e61e7fSChris Wilson 			if (HAS_BLT(dev) && kick_ring(&dev_priv->ring[BCS]))
1763d1e61e7fSChris Wilson 				return false;
1764d1e61e7fSChris Wilson 		}
1765d1e61e7fSChris Wilson 
1766d1e61e7fSChris Wilson 		return true;
1767d1e61e7fSChris Wilson 	}
1768d1e61e7fSChris Wilson 
1769d1e61e7fSChris Wilson 	return false;
1770d1e61e7fSChris Wilson }
1771d1e61e7fSChris Wilson 
1772f65d9421SBen Gamari /**
1773f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1774f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1775f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1776f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1777f65d9421SBen Gamari  */
1778f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1779f65d9421SBen Gamari {
1780f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1781f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1782097354ebSDaniel Vetter 	uint32_t acthd, instdone, instdone1, acthd_bsd, acthd_blt;
1783893eead0SChris Wilson 	bool err = false;
1784893eead0SChris Wilson 
17853e0dc6b0SBen Widawsky 	if (!i915_enable_hangcheck)
17863e0dc6b0SBen Widawsky 		return;
17873e0dc6b0SBen Widawsky 
1788893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
17891ec14ad3SChris Wilson 	if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
17901ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
17911ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
1792d1e61e7fSChris Wilson 		if (err) {
1793d1e61e7fSChris Wilson 			if (i915_hangcheck_hung(dev))
1794d1e61e7fSChris Wilson 				return;
1795d1e61e7fSChris Wilson 
1796893eead0SChris Wilson 			goto repeat;
1797d1e61e7fSChris Wilson 		}
1798d1e61e7fSChris Wilson 
1799d1e61e7fSChris Wilson 		dev_priv->hangcheck_count = 0;
1800893eead0SChris Wilson 		return;
1801893eead0SChris Wilson 	}
1802f65d9421SBen Gamari 
1803a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1804cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1805cbb465e7SChris Wilson 		instdone1 = 0;
1806cbb465e7SChris Wilson 	} else {
1807cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1808cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1809cbb465e7SChris Wilson 	}
1810097354ebSDaniel Vetter 	acthd = intel_ring_get_active_head(&dev_priv->ring[RCS]);
1811097354ebSDaniel Vetter 	acthd_bsd = HAS_BSD(dev) ?
1812097354ebSDaniel Vetter 		intel_ring_get_active_head(&dev_priv->ring[VCS]) : 0;
1813097354ebSDaniel Vetter 	acthd_blt = HAS_BLT(dev) ?
1814097354ebSDaniel Vetter 		intel_ring_get_active_head(&dev_priv->ring[BCS]) : 0;
1815f65d9421SBen Gamari 
1816cbb465e7SChris Wilson 	if (dev_priv->last_acthd == acthd &&
1817097354ebSDaniel Vetter 	    dev_priv->last_acthd_bsd == acthd_bsd &&
1818097354ebSDaniel Vetter 	    dev_priv->last_acthd_blt == acthd_blt &&
1819cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1820cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1821d1e61e7fSChris Wilson 		if (i915_hangcheck_hung(dev))
1822f65d9421SBen Gamari 			return;
1823cbb465e7SChris Wilson 	} else {
1824cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1825cbb465e7SChris Wilson 
1826cbb465e7SChris Wilson 		dev_priv->last_acthd = acthd;
1827097354ebSDaniel Vetter 		dev_priv->last_acthd_bsd = acthd_bsd;
1828097354ebSDaniel Vetter 		dev_priv->last_acthd_blt = acthd_blt;
1829cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1830cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1831cbb465e7SChris Wilson 	}
1832f65d9421SBen Gamari 
1833893eead0SChris Wilson repeat:
1834f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1835b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1836b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1837f65d9421SBen Gamari }
1838f65d9421SBen Gamari 
1839c0e09200SDave Airlie /* drm_dma.h hooks
1840c0e09200SDave Airlie */
1841f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
1842036a4a7dSZhenyu Wang {
1843036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1844036a4a7dSZhenyu Wang 
18454697995bSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
18464697995bSJesse Barnes 
18474697995bSJesse Barnes 
1848036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1849bdfcdb63SDaniel Vetter 
1850036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1851036a4a7dSZhenyu Wang 
1852036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1853036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
18543143a2bfSChris Wilson 	POSTING_READ(DEIER);
1855036a4a7dSZhenyu Wang 
1856036a4a7dSZhenyu Wang 	/* and GT */
1857036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1858036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
18593143a2bfSChris Wilson 	POSTING_READ(GTIER);
1860c650156aSZhenyu Wang 
1861c650156aSZhenyu Wang 	/* south display irq */
1862c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1863c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
18643143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1865036a4a7dSZhenyu Wang }
1866036a4a7dSZhenyu Wang 
18677e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
18687e231dbeSJesse Barnes {
18697e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
18707e231dbeSJesse Barnes 	int pipe;
18717e231dbeSJesse Barnes 
18727e231dbeSJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
18737e231dbeSJesse Barnes 
18747e231dbeSJesse Barnes 	/* VLV magic */
18757e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
18767e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
18777e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
18787e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
18797e231dbeSJesse Barnes 
18807e231dbeSJesse Barnes 	/* and GT */
18817e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
18827e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
18837e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0xffffffff);
18847e231dbeSJesse Barnes 	I915_WRITE(GTIER, 0x0);
18857e231dbeSJesse Barnes 	POSTING_READ(GTIER);
18867e231dbeSJesse Barnes 
18877e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
18887e231dbeSJesse Barnes 
18897e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
18907e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
18917e231dbeSJesse Barnes 	for_each_pipe(pipe)
18927e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
18937e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
18947e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
18957e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
18967e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
18977e231dbeSJesse Barnes }
18987e231dbeSJesse Barnes 
18997fe0b973SKeith Packard /*
19007fe0b973SKeith Packard  * Enable digital hotplug on the PCH, and configure the DP short pulse
19017fe0b973SKeith Packard  * duration to 2ms (which is the minimum in the Display Port spec)
19027fe0b973SKeith Packard  *
19037fe0b973SKeith Packard  * This register is the same on all known PCH chips.
19047fe0b973SKeith Packard  */
19057fe0b973SKeith Packard 
19067fe0b973SKeith Packard static void ironlake_enable_pch_hotplug(struct drm_device *dev)
19077fe0b973SKeith Packard {
19087fe0b973SKeith Packard 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
19097fe0b973SKeith Packard 	u32	hotplug;
19107fe0b973SKeith Packard 
19117fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
19127fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
19137fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
19147fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
19157fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
19167fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
19177fe0b973SKeith Packard }
19187fe0b973SKeith Packard 
1919f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
1920036a4a7dSZhenyu Wang {
1921036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1922036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1923013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1924013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
19251ec14ad3SChris Wilson 	u32 render_irqs;
19262d7b8366SYuanhan Liu 	u32 hotplug_mask;
1927036a4a7dSZhenyu Wang 
19284697995bSJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
19291ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1930036a4a7dSZhenyu Wang 
1931036a4a7dSZhenyu Wang 	/* should always can generate irq */
1932036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
19331ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
19341ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
19353143a2bfSChris Wilson 	POSTING_READ(DEIER);
1936036a4a7dSZhenyu Wang 
19371ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1938036a4a7dSZhenyu Wang 
1939036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
19401ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1941881f47b6SXiang, Haihao 
19421ec14ad3SChris Wilson 	if (IS_GEN6(dev))
19431ec14ad3SChris Wilson 		render_irqs =
19441ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
1945e2a1e2f0SBen Widawsky 			GEN6_BSD_USER_INTERRUPT |
1946e2a1e2f0SBen Widawsky 			GEN6_BLITTER_USER_INTERRUPT;
19471ec14ad3SChris Wilson 	else
19481ec14ad3SChris Wilson 		render_irqs =
194988f23b8fSChris Wilson 			GT_USER_INTERRUPT |
1950c6df541cSChris Wilson 			GT_PIPE_NOTIFY |
19511ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
19521ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
19533143a2bfSChris Wilson 	POSTING_READ(GTIER);
1954036a4a7dSZhenyu Wang 
19552d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
19569035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
19579035a97aSChris Wilson 				SDE_PORTB_HOTPLUG_CPT |
19589035a97aSChris Wilson 				SDE_PORTC_HOTPLUG_CPT |
19599035a97aSChris Wilson 				SDE_PORTD_HOTPLUG_CPT);
19602d7b8366SYuanhan Liu 	} else {
19619035a97aSChris Wilson 		hotplug_mask = (SDE_CRT_HOTPLUG |
19629035a97aSChris Wilson 				SDE_PORTB_HOTPLUG |
19639035a97aSChris Wilson 				SDE_PORTC_HOTPLUG |
19649035a97aSChris Wilson 				SDE_PORTD_HOTPLUG |
19659035a97aSChris Wilson 				SDE_AUX_MASK);
19662d7b8366SYuanhan Liu 	}
19672d7b8366SYuanhan Liu 
19681ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1969c650156aSZhenyu Wang 
1970c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
19711ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
19721ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
19733143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1974c650156aSZhenyu Wang 
19757fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
19767fe0b973SKeith Packard 
1977f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1978f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1979f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1980f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1981f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1982f97108d1SJesse Barnes 	}
1983f97108d1SJesse Barnes 
1984036a4a7dSZhenyu Wang 	return 0;
1985036a4a7dSZhenyu Wang }
1986036a4a7dSZhenyu Wang 
1987f71d4af4SJesse Barnes static int ivybridge_irq_postinstall(struct drm_device *dev)
1988b1f14ad0SJesse Barnes {
1989b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1990b1f14ad0SJesse Barnes 	/* enable kind of interrupts always enabled */
1991b1f14ad0SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
1992b1f14ad0SJesse Barnes 		DE_PCH_EVENT_IVB | DE_PLANEA_FLIP_DONE_IVB |
1993b1f14ad0SJesse Barnes 		DE_PLANEB_FLIP_DONE_IVB;
1994b1f14ad0SJesse Barnes 	u32 render_irqs;
1995b1f14ad0SJesse Barnes 	u32 hotplug_mask;
1996b1f14ad0SJesse Barnes 
1997b1f14ad0SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1998b1f14ad0SJesse Barnes 	dev_priv->irq_mask = ~display_mask;
1999b1f14ad0SJesse Barnes 
2000b1f14ad0SJesse Barnes 	/* should always can generate irq */
2001b1f14ad0SJesse Barnes 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2002b1f14ad0SJesse Barnes 	I915_WRITE(DEIMR, dev_priv->irq_mask);
2003b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK_IVB |
2004b1f14ad0SJesse Barnes 		   DE_PIPEB_VBLANK_IVB);
2005b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2006b1f14ad0SJesse Barnes 
2007b1f14ad0SJesse Barnes 	dev_priv->gt_irq_mask = ~0;
2008b1f14ad0SJesse Barnes 
2009b1f14ad0SJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2010b1f14ad0SJesse Barnes 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2011b1f14ad0SJesse Barnes 
2012e2a1e2f0SBen Widawsky 	render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
2013e2a1e2f0SBen Widawsky 		GEN6_BLITTER_USER_INTERRUPT;
2014b1f14ad0SJesse Barnes 	I915_WRITE(GTIER, render_irqs);
2015b1f14ad0SJesse Barnes 	POSTING_READ(GTIER);
2016b1f14ad0SJesse Barnes 
2017b1f14ad0SJesse Barnes 	hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
2018b1f14ad0SJesse Barnes 			SDE_PORTB_HOTPLUG_CPT |
2019b1f14ad0SJesse Barnes 			SDE_PORTC_HOTPLUG_CPT |
2020b1f14ad0SJesse Barnes 			SDE_PORTD_HOTPLUG_CPT);
2021b1f14ad0SJesse Barnes 	dev_priv->pch_irq_mask = ~hotplug_mask;
2022b1f14ad0SJesse Barnes 
2023b1f14ad0SJesse Barnes 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2024b1f14ad0SJesse Barnes 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
2025b1f14ad0SJesse Barnes 	I915_WRITE(SDEIER, hotplug_mask);
2026b1f14ad0SJesse Barnes 	POSTING_READ(SDEIER);
2027b1f14ad0SJesse Barnes 
20287fe0b973SKeith Packard 	ironlake_enable_pch_hotplug(dev);
20297fe0b973SKeith Packard 
2030b1f14ad0SJesse Barnes 	return 0;
2031b1f14ad0SJesse Barnes }
2032b1f14ad0SJesse Barnes 
20337e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
20347e231dbeSJesse Barnes {
20357e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
20367e231dbeSJesse Barnes 	u32 render_irqs;
20377e231dbeSJesse Barnes 	u32 enable_mask;
20387e231dbeSJesse Barnes 	u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
20397e231dbeSJesse Barnes 	u16 msid;
20407e231dbeSJesse Barnes 
20417e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
20427e231dbeSJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
20437e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
20447e231dbeSJesse Barnes 
20457e231dbeSJesse Barnes 	dev_priv->irq_mask = ~enable_mask;
20467e231dbeSJesse Barnes 
20477e231dbeSJesse Barnes 	dev_priv->pipestat[0] = 0;
20487e231dbeSJesse Barnes 	dev_priv->pipestat[1] = 0;
20497e231dbeSJesse Barnes 
20507e231dbeSJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
20517e231dbeSJesse Barnes 
20527e231dbeSJesse Barnes 	/* Hack for broken MSIs on VLV */
20537e231dbeSJesse Barnes 	pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
20547e231dbeSJesse Barnes 	pci_read_config_word(dev->pdev, 0x98, &msid);
20557e231dbeSJesse Barnes 	msid &= 0xff; /* mask out delivery bits */
20567e231dbeSJesse Barnes 	msid |= (1<<14);
20577e231dbeSJesse Barnes 	pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
20587e231dbeSJesse Barnes 
20597e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
20607e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
20617e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20627e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
20637e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
20647e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
20657e231dbeSJesse Barnes 
20667e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20677e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
20687e231dbeSJesse Barnes 
20697e231dbeSJesse Barnes 	render_irqs = GT_GEN6_BLT_FLUSHDW_NOTIFY_INTERRUPT |
20707e231dbeSJesse Barnes 		GT_GEN6_BLT_CS_ERROR_INTERRUPT |
2071e2a1e2f0SBen Widawsky 		GT_GEN6_BLT_USER_INTERRUPT |
20727e231dbeSJesse Barnes 		GT_GEN6_BSD_USER_INTERRUPT |
20737e231dbeSJesse Barnes 		GT_GEN6_BSD_CS_ERROR_INTERRUPT |
20747e231dbeSJesse Barnes 		GT_GEN7_L3_PARITY_ERROR_INTERRUPT |
20757e231dbeSJesse Barnes 		GT_PIPE_NOTIFY |
20767e231dbeSJesse Barnes 		GT_RENDER_CS_ERROR_INTERRUPT |
20777e231dbeSJesse Barnes 		GT_SYNC_STATUS |
20787e231dbeSJesse Barnes 		GT_USER_INTERRUPT;
20797e231dbeSJesse Barnes 
20807e231dbeSJesse Barnes 	dev_priv->gt_irq_mask = ~render_irqs;
20817e231dbeSJesse Barnes 
20827e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
20837e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
20847e231dbeSJesse Barnes 	I915_WRITE(GTIMR, 0);
20857e231dbeSJesse Barnes 	I915_WRITE(GTIER, render_irqs);
20867e231dbeSJesse Barnes 	POSTING_READ(GTIER);
20877e231dbeSJesse Barnes 
20887e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
20897e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
20907e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
20917e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
20927e231dbeSJesse Barnes #endif
20937e231dbeSJesse Barnes 
20947e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
20957e231dbeSJesse Barnes #if 0 /* FIXME: check register definitions; some have moved */
20967e231dbeSJesse Barnes 	/* Note HDMI and DP share bits */
20977e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
20987e231dbeSJesse Barnes 		hotplug_en |= HDMIB_HOTPLUG_INT_EN;
20997e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
21007e231dbeSJesse Barnes 		hotplug_en |= HDMIC_HOTPLUG_INT_EN;
21017e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
21027e231dbeSJesse Barnes 		hotplug_en |= HDMID_HOTPLUG_INT_EN;
21037e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
21047e231dbeSJesse Barnes 		hotplug_en |= SDVOC_HOTPLUG_INT_EN;
21057e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
21067e231dbeSJesse Barnes 		hotplug_en |= SDVOB_HOTPLUG_INT_EN;
21077e231dbeSJesse Barnes 	if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
21087e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_INT_EN;
21097e231dbeSJesse Barnes 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
21107e231dbeSJesse Barnes 	}
21117e231dbeSJesse Barnes #endif
21127e231dbeSJesse Barnes 
21137e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
21147e231dbeSJesse Barnes 
21157e231dbeSJesse Barnes 	return 0;
21167e231dbeSJesse Barnes }
21177e231dbeSJesse Barnes 
21187e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
21197e231dbeSJesse Barnes {
21207e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21217e231dbeSJesse Barnes 	int pipe;
21227e231dbeSJesse Barnes 
21237e231dbeSJesse Barnes 	if (!dev_priv)
21247e231dbeSJesse Barnes 		return;
21257e231dbeSJesse Barnes 
21267e231dbeSJesse Barnes 	dev_priv->vblank_pipe = 0;
21277e231dbeSJesse Barnes 
21287e231dbeSJesse Barnes 	for_each_pipe(pipe)
21297e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
21307e231dbeSJesse Barnes 
21317e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
21327e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
21337e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
21347e231dbeSJesse Barnes 	for_each_pipe(pipe)
21357e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
21367e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
21377e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
21387e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
21397e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
21407e231dbeSJesse Barnes }
21417e231dbeSJesse Barnes 
2142f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
2143036a4a7dSZhenyu Wang {
2144036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
21454697995bSJesse Barnes 
21464697995bSJesse Barnes 	if (!dev_priv)
21474697995bSJesse Barnes 		return;
21484697995bSJesse Barnes 
21494697995bSJesse Barnes 	dev_priv->vblank_pipe = 0;
21504697995bSJesse Barnes 
2151036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
2152036a4a7dSZhenyu Wang 
2153036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2154036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
2155036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
2156036a4a7dSZhenyu Wang 
2157036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
2158036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
2159036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2160192aac1fSKeith Packard 
2161192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
2162192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
2163192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2164036a4a7dSZhenyu Wang }
2165036a4a7dSZhenyu Wang 
2166c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
2167c2798b19SChris Wilson {
2168c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2169c2798b19SChris Wilson 	int pipe;
2170c2798b19SChris Wilson 
2171c2798b19SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2172c2798b19SChris Wilson 
2173c2798b19SChris Wilson 	for_each_pipe(pipe)
2174c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2175c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2176c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2177c2798b19SChris Wilson 	POSTING_READ16(IER);
2178c2798b19SChris Wilson }
2179c2798b19SChris Wilson 
2180c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
2181c2798b19SChris Wilson {
2182c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2183c2798b19SChris Wilson 
2184c2798b19SChris Wilson 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
2185c2798b19SChris Wilson 
2186c2798b19SChris Wilson 	dev_priv->pipestat[0] = 0;
2187c2798b19SChris Wilson 	dev_priv->pipestat[1] = 0;
2188c2798b19SChris Wilson 
2189c2798b19SChris Wilson 	I915_WRITE16(EMR,
2190c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2191c2798b19SChris Wilson 
2192c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
2193c2798b19SChris Wilson 	dev_priv->irq_mask =
2194c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2195c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2196c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2197c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2198c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2199c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
2200c2798b19SChris Wilson 
2201c2798b19SChris Wilson 	I915_WRITE16(IER,
2202c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2203c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2204c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2205c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
2206c2798b19SChris Wilson 	POSTING_READ16(IER);
2207c2798b19SChris Wilson 
2208c2798b19SChris Wilson 	return 0;
2209c2798b19SChris Wilson }
2210c2798b19SChris Wilson 
2211c2798b19SChris Wilson static irqreturn_t i8xx_irq_handler(DRM_IRQ_ARGS)
2212c2798b19SChris Wilson {
2213c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2214c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2215c2798b19SChris Wilson 	struct drm_i915_master_private *master_priv;
2216c2798b19SChris Wilson 	u16 iir, new_iir;
2217c2798b19SChris Wilson 	u32 pipe_stats[2];
2218c2798b19SChris Wilson 	unsigned long irqflags;
2219c2798b19SChris Wilson 	int irq_received;
2220c2798b19SChris Wilson 	int pipe;
2221c2798b19SChris Wilson 	u16 flip_mask =
2222c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2223c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2224c2798b19SChris Wilson 
2225c2798b19SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2226c2798b19SChris Wilson 
2227c2798b19SChris Wilson 	iir = I915_READ16(IIR);
2228c2798b19SChris Wilson 	if (iir == 0)
2229c2798b19SChris Wilson 		return IRQ_NONE;
2230c2798b19SChris Wilson 
2231c2798b19SChris Wilson 	while (iir & ~flip_mask) {
2232c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2233c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2234c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2235c2798b19SChris Wilson 		 * interrupts (for non-MSI).
2236c2798b19SChris Wilson 		 */
2237c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2238c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2239c2798b19SChris Wilson 			i915_handle_error(dev, false);
2240c2798b19SChris Wilson 
2241c2798b19SChris Wilson 		for_each_pipe(pipe) {
2242c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
2243c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2244c2798b19SChris Wilson 
2245c2798b19SChris Wilson 			/*
2246c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2247c2798b19SChris Wilson 			 */
2248c2798b19SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2249c2798b19SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2250c2798b19SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2251c2798b19SChris Wilson 							 pipe_name(pipe));
2252c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2253c2798b19SChris Wilson 				irq_received = 1;
2254c2798b19SChris Wilson 			}
2255c2798b19SChris Wilson 		}
2256c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2257c2798b19SChris Wilson 
2258c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
2259c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
2260c2798b19SChris Wilson 
2261c2798b19SChris Wilson 		if (dev->primary->master) {
2262c2798b19SChris Wilson 			master_priv = dev->primary->master->driver_priv;
2263c2798b19SChris Wilson 			if (master_priv->sarea_priv)
2264c2798b19SChris Wilson 				master_priv->sarea_priv->last_dispatch =
2265c2798b19SChris Wilson 					READ_BREADCRUMB(dev_priv);
2266c2798b19SChris Wilson 		}
2267c2798b19SChris Wilson 
2268c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2269c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2270c2798b19SChris Wilson 
2271c2798b19SChris Wilson 		if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
2272c2798b19SChris Wilson 		    drm_handle_vblank(dev, 0)) {
2273c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
2274c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 0);
2275c2798b19SChris Wilson 				intel_finish_page_flip(dev, 0);
2276c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT;
2277c2798b19SChris Wilson 			}
2278c2798b19SChris Wilson 		}
2279c2798b19SChris Wilson 
2280c2798b19SChris Wilson 		if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
2281c2798b19SChris Wilson 		    drm_handle_vblank(dev, 1)) {
2282c2798b19SChris Wilson 			if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
2283c2798b19SChris Wilson 				intel_prepare_page_flip(dev, 1);
2284c2798b19SChris Wilson 				intel_finish_page_flip(dev, 1);
2285c2798b19SChris Wilson 				flip_mask &= ~I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2286c2798b19SChris Wilson 			}
2287c2798b19SChris Wilson 		}
2288c2798b19SChris Wilson 
2289c2798b19SChris Wilson 		iir = new_iir;
2290c2798b19SChris Wilson 	}
2291c2798b19SChris Wilson 
2292c2798b19SChris Wilson 	return IRQ_HANDLED;
2293c2798b19SChris Wilson }
2294c2798b19SChris Wilson 
2295c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
2296c2798b19SChris Wilson {
2297c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2298c2798b19SChris Wilson 	int pipe;
2299c2798b19SChris Wilson 
2300c2798b19SChris Wilson 	dev_priv->vblank_pipe = 0;
2301c2798b19SChris Wilson 
2302c2798b19SChris Wilson 	for_each_pipe(pipe) {
2303c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
2304c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2305c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2306c2798b19SChris Wilson 	}
2307c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
2308c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
2309c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
2310c2798b19SChris Wilson }
2311c2798b19SChris Wilson 
2312a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
2313a266c7d5SChris Wilson {
2314a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2315a266c7d5SChris Wilson 	int pipe;
2316a266c7d5SChris Wilson 
2317a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2318a266c7d5SChris Wilson 
2319a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2320a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2321a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2322a266c7d5SChris Wilson 	}
2323a266c7d5SChris Wilson 
232400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
2325a266c7d5SChris Wilson 	for_each_pipe(pipe)
2326a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2327a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2328a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2329a266c7d5SChris Wilson 	POSTING_READ(IER);
2330a266c7d5SChris Wilson }
2331a266c7d5SChris Wilson 
2332a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
2333a266c7d5SChris Wilson {
2334a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
233538bde180SChris Wilson 	u32 enable_mask;
2336a266c7d5SChris Wilson 
2337a266c7d5SChris Wilson 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
2338a266c7d5SChris Wilson 
2339a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2340a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2341a266c7d5SChris Wilson 
234238bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
234338bde180SChris Wilson 
234438bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
234538bde180SChris Wilson 	dev_priv->irq_mask =
234638bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
234738bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
234838bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
234938bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
235038bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
235138bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
235238bde180SChris Wilson 
235338bde180SChris Wilson 	enable_mask =
235438bde180SChris Wilson 		I915_ASLE_INTERRUPT |
235538bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
235638bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
235738bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
235838bde180SChris Wilson 		I915_USER_INTERRUPT;
235938bde180SChris Wilson 
2360a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2361a266c7d5SChris Wilson 		/* Enable in IER... */
2362a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2363a266c7d5SChris Wilson 		/* and unmask in IMR */
2364a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2365a266c7d5SChris Wilson 	}
2366a266c7d5SChris Wilson 
2367a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2368a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2369a266c7d5SChris Wilson 	POSTING_READ(IER);
2370a266c7d5SChris Wilson 
2371a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2372a266c7d5SChris Wilson 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2373a266c7d5SChris Wilson 
2374a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2375a266c7d5SChris Wilson 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2376a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2377a266c7d5SChris Wilson 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2378a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2379a266c7d5SChris Wilson 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
2380a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
2381a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2382a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
2383a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2384a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2385a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_INT_EN;
2386a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2387a266c7d5SChris Wilson 		}
2388a266c7d5SChris Wilson 
2389a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2390a266c7d5SChris Wilson 
2391a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2392a266c7d5SChris Wilson 	}
2393a266c7d5SChris Wilson 
2394a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2395a266c7d5SChris Wilson 
2396a266c7d5SChris Wilson 	return 0;
2397a266c7d5SChris Wilson }
2398a266c7d5SChris Wilson 
2399a266c7d5SChris Wilson static irqreturn_t i915_irq_handler(DRM_IRQ_ARGS)
2400a266c7d5SChris Wilson {
2401a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2402a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2403a266c7d5SChris Wilson 	struct drm_i915_master_private *master_priv;
24048291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2405a266c7d5SChris Wilson 	unsigned long irqflags;
240638bde180SChris Wilson 	u32 flip_mask =
240738bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
240838bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
240938bde180SChris Wilson 	u32 flip[2] = {
241038bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT,
241138bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT
241238bde180SChris Wilson 	};
241338bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
2414a266c7d5SChris Wilson 
2415a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2416a266c7d5SChris Wilson 
2417a266c7d5SChris Wilson 	iir = I915_READ(IIR);
241838bde180SChris Wilson 	do {
241938bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
24208291ee90SChris Wilson 		bool blc_event = false;
2421a266c7d5SChris Wilson 
2422a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2423a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2424a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2425a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2426a266c7d5SChris Wilson 		 */
2427a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2428a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2429a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2430a266c7d5SChris Wilson 
2431a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2432a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2433a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2434a266c7d5SChris Wilson 
243538bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
2436a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2437a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2438a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2439a266c7d5SChris Wilson 							 pipe_name(pipe));
2440a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
244138bde180SChris Wilson 				irq_received = true;
2442a266c7d5SChris Wilson 			}
2443a266c7d5SChris Wilson 		}
2444a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2445a266c7d5SChris Wilson 
2446a266c7d5SChris Wilson 		if (!irq_received)
2447a266c7d5SChris Wilson 			break;
2448a266c7d5SChris Wilson 
2449a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2450a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2451a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2452a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2453a266c7d5SChris Wilson 
2454a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2455a266c7d5SChris Wilson 				  hotplug_status);
2456a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2457a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2458a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2459a266c7d5SChris Wilson 
2460a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
246138bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
2462a266c7d5SChris Wilson 		}
2463a266c7d5SChris Wilson 
246438bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
2465a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2466a266c7d5SChris Wilson 
2467a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2468a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2469a266c7d5SChris Wilson 
2470a266c7d5SChris Wilson 		for_each_pipe(pipe) {
247138bde180SChris Wilson 			int plane = pipe;
247238bde180SChris Wilson 			if (IS_MOBILE(dev))
247338bde180SChris Wilson 				plane = !plane;
24748291ee90SChris Wilson 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
2475a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
247638bde180SChris Wilson 				if (iir & flip[plane]) {
247738bde180SChris Wilson 					intel_prepare_page_flip(dev, plane);
2478a266c7d5SChris Wilson 					intel_finish_page_flip(dev, pipe);
247938bde180SChris Wilson 					flip_mask &= ~flip[plane];
248038bde180SChris Wilson 				}
2481a266c7d5SChris Wilson 			}
2482a266c7d5SChris Wilson 
2483a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2484a266c7d5SChris Wilson 				blc_event = true;
2485a266c7d5SChris Wilson 		}
2486a266c7d5SChris Wilson 
2487a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2488a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2489a266c7d5SChris Wilson 
2490a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2491a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2492a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2493a266c7d5SChris Wilson 		 * we would never get another interrupt.
2494a266c7d5SChris Wilson 		 *
2495a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2496a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2497a266c7d5SChris Wilson 		 * another one.
2498a266c7d5SChris Wilson 		 *
2499a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2500a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2501a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2502a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2503a266c7d5SChris Wilson 		 * stray interrupts.
2504a266c7d5SChris Wilson 		 */
250538bde180SChris Wilson 		ret = IRQ_HANDLED;
2506a266c7d5SChris Wilson 		iir = new_iir;
250738bde180SChris Wilson 	} while (iir & ~flip_mask);
2508a266c7d5SChris Wilson 
25098291ee90SChris Wilson 	if (dev->primary->master) {
25108291ee90SChris Wilson 		master_priv = dev->primary->master->driver_priv;
25118291ee90SChris Wilson 		if (master_priv->sarea_priv)
25128291ee90SChris Wilson 			master_priv->sarea_priv->last_dispatch =
25138291ee90SChris Wilson 				READ_BREADCRUMB(dev_priv);
25148291ee90SChris Wilson 	}
25158291ee90SChris Wilson 
2516a266c7d5SChris Wilson 	return ret;
2517a266c7d5SChris Wilson }
2518a266c7d5SChris Wilson 
2519a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
2520a266c7d5SChris Wilson {
2521a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2522a266c7d5SChris Wilson 	int pipe;
2523a266c7d5SChris Wilson 
2524a266c7d5SChris Wilson 	dev_priv->vblank_pipe = 0;
2525a266c7d5SChris Wilson 
2526a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2527a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2528a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2529a266c7d5SChris Wilson 	}
2530a266c7d5SChris Wilson 
253100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
253255b39755SChris Wilson 	for_each_pipe(pipe) {
253355b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
2534a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
253555b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
253655b39755SChris Wilson 	}
2537a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2538a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2539a266c7d5SChris Wilson 
2540a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2541a266c7d5SChris Wilson }
2542a266c7d5SChris Wilson 
2543a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
2544a266c7d5SChris Wilson {
2545a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2546a266c7d5SChris Wilson 	int pipe;
2547a266c7d5SChris Wilson 
2548a266c7d5SChris Wilson 	atomic_set(&dev_priv->irq_received, 0);
2549a266c7d5SChris Wilson 
2550a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2551a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2552a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2553a266c7d5SChris Wilson 	}
2554a266c7d5SChris Wilson 
2555a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
2556a266c7d5SChris Wilson 	for_each_pipe(pipe)
2557a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2558a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2559a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2560a266c7d5SChris Wilson 	POSTING_READ(IER);
2561a266c7d5SChris Wilson }
2562a266c7d5SChris Wilson 
2563a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
2564a266c7d5SChris Wilson {
2565a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2566*bbba0a97SChris Wilson 	u32 enable_mask;
2567a266c7d5SChris Wilson 	u32 error_mask;
2568a266c7d5SChris Wilson 
2569a266c7d5SChris Wilson 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
2570a266c7d5SChris Wilson 
2571a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
2572*bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2573*bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2574*bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2575*bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2576*bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2577*bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2578*bbba0a97SChris Wilson 
2579*bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
2580*bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
2581*bbba0a97SChris Wilson 
2582*bbba0a97SChris Wilson 	if (IS_G4X(dev))
2583*bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
2584a266c7d5SChris Wilson 
2585a266c7d5SChris Wilson 	dev_priv->pipestat[0] = 0;
2586a266c7d5SChris Wilson 	dev_priv->pipestat[1] = 0;
2587a266c7d5SChris Wilson 
2588a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2589a266c7d5SChris Wilson 		/* Enable in IER... */
2590a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2591a266c7d5SChris Wilson 		/* and unmask in IMR */
2592a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2593a266c7d5SChris Wilson 	}
2594a266c7d5SChris Wilson 
2595a266c7d5SChris Wilson 	/*
2596a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
2597a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
2598a266c7d5SChris Wilson 	 */
2599a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
2600a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
2601a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
2602a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
2603a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2604a266c7d5SChris Wilson 	} else {
2605a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
2606a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
2607a266c7d5SChris Wilson 	}
2608a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
2609a266c7d5SChris Wilson 
2610a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
2611a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
2612a266c7d5SChris Wilson 	POSTING_READ(IER);
2613a266c7d5SChris Wilson 
2614a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2615a266c7d5SChris Wilson 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2616a266c7d5SChris Wilson 
2617a266c7d5SChris Wilson 		/* Note HDMI and DP share bits */
2618a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2619a266c7d5SChris Wilson 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2620a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2621a266c7d5SChris Wilson 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2622a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2623a266c7d5SChris Wilson 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
2624a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
2625a266c7d5SChris Wilson 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2626a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
2627a266c7d5SChris Wilson 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2628a266c7d5SChris Wilson 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2629a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_INT_EN;
2630a266c7d5SChris Wilson 
2631a266c7d5SChris Wilson 			/* Programming the CRT detection parameters tends
2632a266c7d5SChris Wilson 			   to generate a spurious hotplug event about three
2633a266c7d5SChris Wilson 			   seconds later.  So just do it once.
2634a266c7d5SChris Wilson 			*/
2635a266c7d5SChris Wilson 			if (IS_G4X(dev))
2636a266c7d5SChris Wilson 				hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2637a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2638a266c7d5SChris Wilson 		}
2639a266c7d5SChris Wilson 
2640a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
2641a266c7d5SChris Wilson 
2642a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2643a266c7d5SChris Wilson 	}
2644a266c7d5SChris Wilson 
2645a266c7d5SChris Wilson 	intel_opregion_enable_asle(dev);
2646a266c7d5SChris Wilson 
2647a266c7d5SChris Wilson 	return 0;
2648a266c7d5SChris Wilson }
2649a266c7d5SChris Wilson 
2650a266c7d5SChris Wilson static irqreturn_t i965_irq_handler(DRM_IRQ_ARGS)
2651a266c7d5SChris Wilson {
2652a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
2653a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2654a266c7d5SChris Wilson 	struct drm_i915_master_private *master_priv;
2655a266c7d5SChris Wilson 	u32 iir, new_iir;
2656a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
2657a266c7d5SChris Wilson 	unsigned long irqflags;
2658a266c7d5SChris Wilson 	int irq_received;
2659a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
2660a266c7d5SChris Wilson 
2661a266c7d5SChris Wilson 	atomic_inc(&dev_priv->irq_received);
2662a266c7d5SChris Wilson 
2663a266c7d5SChris Wilson 	iir = I915_READ(IIR);
2664a266c7d5SChris Wilson 
2665a266c7d5SChris Wilson 	for (;;) {
26662c8ba29fSChris Wilson 		bool blc_event = false;
26672c8ba29fSChris Wilson 
2668a266c7d5SChris Wilson 		irq_received = iir != 0;
2669a266c7d5SChris Wilson 
2670a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
2671a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
2672a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
2673a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
2674a266c7d5SChris Wilson 		 */
2675a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2676a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2677a266c7d5SChris Wilson 			i915_handle_error(dev, false);
2678a266c7d5SChris Wilson 
2679a266c7d5SChris Wilson 		for_each_pipe(pipe) {
2680a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
2681a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
2682a266c7d5SChris Wilson 
2683a266c7d5SChris Wilson 			/*
2684a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
2685a266c7d5SChris Wilson 			 */
2686a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
2687a266c7d5SChris Wilson 				if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2688a266c7d5SChris Wilson 					DRM_DEBUG_DRIVER("pipe %c underrun\n",
2689a266c7d5SChris Wilson 							 pipe_name(pipe));
2690a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
2691a266c7d5SChris Wilson 				irq_received = 1;
2692a266c7d5SChris Wilson 			}
2693a266c7d5SChris Wilson 		}
2694a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2695a266c7d5SChris Wilson 
2696a266c7d5SChris Wilson 		if (!irq_received)
2697a266c7d5SChris Wilson 			break;
2698a266c7d5SChris Wilson 
2699a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
2700a266c7d5SChris Wilson 
2701a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
2702a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
2703a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2704a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2705a266c7d5SChris Wilson 
2706a266c7d5SChris Wilson 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2707a266c7d5SChris Wilson 				  hotplug_status);
2708a266c7d5SChris Wilson 			if (hotplug_status & dev_priv->hotplug_supported_mask)
2709a266c7d5SChris Wilson 				queue_work(dev_priv->wq,
2710a266c7d5SChris Wilson 					   &dev_priv->hotplug_work);
2711a266c7d5SChris Wilson 
2712a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2713a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
2714a266c7d5SChris Wilson 		}
2715a266c7d5SChris Wilson 
2716a266c7d5SChris Wilson 		I915_WRITE(IIR, iir);
2717a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
2718a266c7d5SChris Wilson 
2719a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
2720a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
2721a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
2722a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
2723a266c7d5SChris Wilson 
27244f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
2725a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 0);
2726a266c7d5SChris Wilson 
27274f7d1e79SChris Wilson 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
2728a266c7d5SChris Wilson 			intel_prepare_page_flip(dev, 1);
2729a266c7d5SChris Wilson 
2730a266c7d5SChris Wilson 		for_each_pipe(pipe) {
27312c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
2732a266c7d5SChris Wilson 			    drm_handle_vblank(dev, pipe)) {
2733a266c7d5SChris Wilson 				i915_pageflip_stall_check(dev, pipe);
2734a266c7d5SChris Wilson 				intel_finish_page_flip(dev, pipe);
2735a266c7d5SChris Wilson 			}
2736a266c7d5SChris Wilson 
2737a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2738a266c7d5SChris Wilson 				blc_event = true;
2739a266c7d5SChris Wilson 		}
2740a266c7d5SChris Wilson 
2741a266c7d5SChris Wilson 
2742a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
2743a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
2744a266c7d5SChris Wilson 
2745a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
2746a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
2747a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
2748a266c7d5SChris Wilson 		 * we would never get another interrupt.
2749a266c7d5SChris Wilson 		 *
2750a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
2751a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
2752a266c7d5SChris Wilson 		 * another one.
2753a266c7d5SChris Wilson 		 *
2754a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
2755a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
2756a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
2757a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
2758a266c7d5SChris Wilson 		 * stray interrupts.
2759a266c7d5SChris Wilson 		 */
2760a266c7d5SChris Wilson 		iir = new_iir;
2761a266c7d5SChris Wilson 	}
2762a266c7d5SChris Wilson 
27632c8ba29fSChris Wilson 	if (dev->primary->master) {
27642c8ba29fSChris Wilson 		master_priv = dev->primary->master->driver_priv;
27652c8ba29fSChris Wilson 		if (master_priv->sarea_priv)
27662c8ba29fSChris Wilson 			master_priv->sarea_priv->last_dispatch =
27672c8ba29fSChris Wilson 				READ_BREADCRUMB(dev_priv);
27682c8ba29fSChris Wilson 	}
27692c8ba29fSChris Wilson 
2770a266c7d5SChris Wilson 	return ret;
2771a266c7d5SChris Wilson }
2772a266c7d5SChris Wilson 
2773a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
2774a266c7d5SChris Wilson {
2775a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2776a266c7d5SChris Wilson 	int pipe;
2777a266c7d5SChris Wilson 
2778a266c7d5SChris Wilson 	if (!dev_priv)
2779a266c7d5SChris Wilson 		return;
2780a266c7d5SChris Wilson 
2781a266c7d5SChris Wilson 	dev_priv->vblank_pipe = 0;
2782a266c7d5SChris Wilson 
2783a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
2784a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
2785a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2786a266c7d5SChris Wilson 	}
2787a266c7d5SChris Wilson 
2788a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
2789a266c7d5SChris Wilson 	for_each_pipe(pipe)
2790a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
2791a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
2792a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
2793a266c7d5SChris Wilson 
2794a266c7d5SChris Wilson 	for_each_pipe(pipe)
2795a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
2796a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2797a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
2798a266c7d5SChris Wilson }
2799a266c7d5SChris Wilson 
2800f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
2801f71d4af4SJesse Barnes {
28028b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28038b2e326dSChris Wilson 
28048b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
28058b2e326dSChris Wilson 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
28068b2e326dSChris Wilson 	INIT_WORK(&dev_priv->rps_work, gen6_pm_rps_work);
28078b2e326dSChris Wilson 
2808f71d4af4SJesse Barnes 	dev->driver->get_vblank_counter = i915_get_vblank_counter;
2809f71d4af4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
28107e231dbeSJesse Barnes 	if (IS_G4X(dev) || IS_GEN5(dev) || IS_GEN6(dev) || IS_IVYBRIDGE(dev) ||
28117e231dbeSJesse Barnes 	    IS_VALLEYVIEW(dev)) {
2812f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2813f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2814f71d4af4SJesse Barnes 	}
2815f71d4af4SJesse Barnes 
2816c3613de9SKeith Packard 	if (drm_core_check_feature(dev, DRIVER_MODESET))
2817f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2818c3613de9SKeith Packard 	else
2819c3613de9SKeith Packard 		dev->driver->get_vblank_timestamp = NULL;
2820f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2821f71d4af4SJesse Barnes 
28227e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
28237e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
28247e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
28257e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
28267e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
28277e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
28287e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
28297e231dbeSJesse Barnes 	} else if (IS_IVYBRIDGE(dev)) {
2830f71d4af4SJesse Barnes 		/* Share pre & uninstall handlers with ILK/SNB */
2831f71d4af4SJesse Barnes 		dev->driver->irq_handler = ivybridge_irq_handler;
2832f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2833f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2834f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2835f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ivybridge_enable_vblank;
2836f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ivybridge_disable_vblank;
2837f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
2838f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
2839f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
2840f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
2841f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
2842f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
2843f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
2844f71d4af4SJesse Barnes 	} else {
2845c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
2846c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
2847c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
2848c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
2849c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
2850a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
28514f7d1e79SChris Wilson 			/* IIR "flip pending" means done if this bit is set */
28524f7d1e79SChris Wilson 			I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
28534f7d1e79SChris Wilson 
2854a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
2855a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
2856a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
2857a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
2858c2798b19SChris Wilson 		} else {
2859a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
2860a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
2861a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
2862a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
2863c2798b19SChris Wilson 		}
2864f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
2865f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
2866f71d4af4SJesse Barnes 	}
2867f71d4af4SJesse Barnes }
2868