xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 88f23b8fa3e6357c423af24ec31c661fc12f884b)
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 
2963eeaf38SJesse Barnes #include <linux/sysrq.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31c0e09200SDave Airlie #include "drmP.h"
32c0e09200SDave Airlie #include "drm.h"
33c0e09200SDave Airlie #include "i915_drm.h"
34c0e09200SDave Airlie #include "i915_drv.h"
351c5d22f7SChris Wilson #include "i915_trace.h"
3679e53945SJesse Barnes #include "intel_drv.h"
37c0e09200SDave Airlie 
38c0e09200SDave Airlie #define MAX_NOPID ((u32)~0)
39c0e09200SDave Airlie 
407c463586SKeith Packard /**
417c463586SKeith Packard  * Interrupts that are always left unmasked.
427c463586SKeith Packard  *
437c463586SKeith Packard  * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
447c463586SKeith Packard  * we leave them always unmasked in IMR and then control enabling them through
457c463586SKeith Packard  * PIPESTAT alone.
467c463586SKeith Packard  */
476b95a207SKristian Høgsberg #define I915_INTERRUPT_ENABLE_FIX			\
486b95a207SKristian Høgsberg 	(I915_ASLE_INTERRUPT |				\
490a3e67a4SJesse Barnes 	 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |		\
5063eeaf38SJesse Barnes 	 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |		\
516b95a207SKristian Høgsberg 	 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |	\
526b95a207SKristian Høgsberg 	 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |	\
5363eeaf38SJesse Barnes 	 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
54ed4cb414SEric Anholt 
557c463586SKeith Packard /** Interrupts that we mask and unmask at runtime. */
56d1b851fcSZou Nan hai #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT | I915_BSD_USER_INTERRUPT)
577c463586SKeith Packard 
5879e53945SJesse Barnes #define I915_PIPE_VBLANK_STATUS	(PIPE_START_VBLANK_INTERRUPT_STATUS |\
5979e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_STATUS)
6079e53945SJesse Barnes 
6179e53945SJesse Barnes #define I915_PIPE_VBLANK_ENABLE	(PIPE_START_VBLANK_INTERRUPT_ENABLE |\
6279e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_ENABLE)
6379e53945SJesse Barnes 
6479e53945SJesse Barnes #define DRM_I915_VBLANK_PIPE_ALL	(DRM_I915_VBLANK_PIPE_A | \
6579e53945SJesse Barnes 					 DRM_I915_VBLANK_PIPE_B)
6679e53945SJesse Barnes 
678ee1c3dbSMatthew Garrett void
68f2b115e6SAdam Jackson ironlake_enable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
69036a4a7dSZhenyu Wang {
701ec14ad3SChris Wilson 	if ((dev_priv->gt_irq_mask & mask) != 0) {
711ec14ad3SChris Wilson 		dev_priv->gt_irq_mask &= ~mask;
721ec14ad3SChris Wilson 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
733143a2bfSChris Wilson 		POSTING_READ(GTIMR);
74036a4a7dSZhenyu Wang 	}
75036a4a7dSZhenyu Wang }
76036a4a7dSZhenyu Wang 
7762fdfeafSEric Anholt void
78f2b115e6SAdam Jackson ironlake_disable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
79036a4a7dSZhenyu Wang {
801ec14ad3SChris Wilson 	if ((dev_priv->gt_irq_mask & mask) != mask) {
811ec14ad3SChris Wilson 		dev_priv->gt_irq_mask |= mask;
821ec14ad3SChris Wilson 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
833143a2bfSChris Wilson 		POSTING_READ(GTIMR);
84036a4a7dSZhenyu Wang 	}
85036a4a7dSZhenyu Wang }
86036a4a7dSZhenyu Wang 
87036a4a7dSZhenyu Wang /* For display hotplug interrupt */
88995b6762SChris Wilson static void
89f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
90036a4a7dSZhenyu Wang {
911ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
921ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
931ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
943143a2bfSChris Wilson 		POSTING_READ(DEIMR);
95036a4a7dSZhenyu Wang 	}
96036a4a7dSZhenyu Wang }
97036a4a7dSZhenyu Wang 
98036a4a7dSZhenyu Wang static inline void
99f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
100036a4a7dSZhenyu Wang {
1011ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1021ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1031ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1043143a2bfSChris Wilson 		POSTING_READ(DEIMR);
105036a4a7dSZhenyu Wang 	}
106036a4a7dSZhenyu Wang }
107036a4a7dSZhenyu Wang 
108036a4a7dSZhenyu Wang void
109ed4cb414SEric Anholt i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
110ed4cb414SEric Anholt {
1111ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
1121ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
1131ec14ad3SChris Wilson 		I915_WRITE(IMR, dev_priv->irq_mask);
1143143a2bfSChris Wilson 		POSTING_READ(IMR);
115ed4cb414SEric Anholt 	}
116ed4cb414SEric Anholt }
117ed4cb414SEric Anholt 
11862fdfeafSEric Anholt void
119ed4cb414SEric Anholt i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
120ed4cb414SEric Anholt {
1211ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1221ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1231ec14ad3SChris Wilson 		I915_WRITE(IMR, dev_priv->irq_mask);
1243143a2bfSChris Wilson 		POSTING_READ(IMR);
125ed4cb414SEric Anholt 	}
126ed4cb414SEric Anholt }
127ed4cb414SEric Anholt 
1287c463586SKeith Packard static inline u32
1297c463586SKeith Packard i915_pipestat(int pipe)
1307c463586SKeith Packard {
1317c463586SKeith Packard 	if (pipe == 0)
1327c463586SKeith Packard 		return PIPEASTAT;
1337c463586SKeith Packard 	if (pipe == 1)
1347c463586SKeith Packard 		return PIPEBSTAT;
1359c84ba4eSAndrew Morton 	BUG();
1367c463586SKeith Packard }
1377c463586SKeith Packard 
1387c463586SKeith Packard void
1397c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1407c463586SKeith Packard {
1417c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
1427c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1437c463586SKeith Packard 
1447c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
1457c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
1467c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
1473143a2bfSChris Wilson 		POSTING_READ(reg);
1487c463586SKeith Packard 	}
1497c463586SKeith Packard }
1507c463586SKeith Packard 
1517c463586SKeith Packard void
1527c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1537c463586SKeith Packard {
1547c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
1557c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1567c463586SKeith Packard 
1577c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
1587c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
1593143a2bfSChris Wilson 		POSTING_READ(reg);
1607c463586SKeith Packard 	}
1617c463586SKeith Packard }
1627c463586SKeith Packard 
163c0e09200SDave Airlie /**
16401c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
16501c66889SZhao Yakui  */
16601c66889SZhao Yakui void intel_enable_asle(struct drm_device *dev)
16701c66889SZhao Yakui {
1681ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
1691ec14ad3SChris Wilson 	unsigned long irqflags;
1701ec14ad3SChris Wilson 
1711ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
17201c66889SZhao Yakui 
173c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
174f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
175edcb49caSZhao Yakui 	else {
17601c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
177d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
178a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen >= 4)
179edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
180d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
181edcb49caSZhao Yakui 	}
1821ec14ad3SChris Wilson 
1831ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
18401c66889SZhao Yakui }
18501c66889SZhao Yakui 
18601c66889SZhao Yakui /**
1870a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1880a3e67a4SJesse Barnes  * @dev: DRM device
1890a3e67a4SJesse Barnes  * @pipe: pipe to check
1900a3e67a4SJesse Barnes  *
1910a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1920a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1930a3e67a4SJesse Barnes  * before reading such registers if unsure.
1940a3e67a4SJesse Barnes  */
1950a3e67a4SJesse Barnes static int
1960a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1970a3e67a4SJesse Barnes {
1980a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1995eddb70bSChris Wilson 	return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
2000a3e67a4SJesse Barnes }
2010a3e67a4SJesse Barnes 
20242f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
20342f52ef8SKeith Packard  * we use as a pipe index
20442f52ef8SKeith Packard  */
20542f52ef8SKeith Packard u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
2060a3e67a4SJesse Barnes {
2070a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2080a3e67a4SJesse Barnes 	unsigned long high_frame;
2090a3e67a4SJesse Barnes 	unsigned long low_frame;
2105eddb70bSChris Wilson 	u32 high1, high2, low;
2110a3e67a4SJesse Barnes 
2120a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
21344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
21444d98a61SZhao Yakui 				"pipe %d\n", pipe);
2150a3e67a4SJesse Barnes 		return 0;
2160a3e67a4SJesse Barnes 	}
2170a3e67a4SJesse Barnes 
2185eddb70bSChris Wilson 	high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
2195eddb70bSChris Wilson 	low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
2205eddb70bSChris Wilson 
2210a3e67a4SJesse Barnes 	/*
2220a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
2230a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
2240a3e67a4SJesse Barnes 	 * register.
2250a3e67a4SJesse Barnes 	 */
2260a3e67a4SJesse Barnes 	do {
2275eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
2285eddb70bSChris Wilson 		low   = I915_READ(low_frame)  & PIPE_FRAME_LOW_MASK;
2295eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
2300a3e67a4SJesse Barnes 	} while (high1 != high2);
2310a3e67a4SJesse Barnes 
2325eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
2335eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
2345eddb70bSChris Wilson 	return (high1 << 8) | low;
2350a3e67a4SJesse Barnes }
2360a3e67a4SJesse Barnes 
2379880b7a5SJesse Barnes u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
2389880b7a5SJesse Barnes {
2399880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2409880b7a5SJesse Barnes 	int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
2419880b7a5SJesse Barnes 
2429880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
24344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
24444d98a61SZhao Yakui 					"pipe %d\n", pipe);
2459880b7a5SJesse Barnes 		return 0;
2469880b7a5SJesse Barnes 	}
2479880b7a5SJesse Barnes 
2489880b7a5SJesse Barnes 	return I915_READ(reg);
2499880b7a5SJesse Barnes }
2509880b7a5SJesse Barnes 
2515ca58282SJesse Barnes /*
2525ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
2535ca58282SJesse Barnes  */
2545ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
2555ca58282SJesse Barnes {
2565ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
2575ca58282SJesse Barnes 						    hotplug_work);
2585ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
259c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
2604ef69c7aSChris Wilson 	struct intel_encoder *encoder;
2615ca58282SJesse Barnes 
2624ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
2634ef69c7aSChris Wilson 		if (encoder->hot_plug)
2644ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
265c31c4ba3SKeith Packard 
2665ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
267eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
2685ca58282SJesse Barnes }
2695ca58282SJesse Barnes 
270f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
271f97108d1SJesse Barnes {
272f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
273b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
274f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
275f97108d1SJesse Barnes 
2767648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
277b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
278b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
279f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
280f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
281f97108d1SJesse Barnes 
282f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
283b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
284f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
285f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
286f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
287f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
288b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
289f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
290f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
291f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
292f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
293f97108d1SJesse Barnes 	}
294f97108d1SJesse Barnes 
2957648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
296f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
297f97108d1SJesse Barnes 
298f97108d1SJesse Barnes 	return;
299f97108d1SJesse Barnes }
300f97108d1SJesse Barnes 
301549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
302549f7365SChris Wilson 			struct intel_ring_buffer *ring)
303549f7365SChris Wilson {
304549f7365SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
30578501eacSChris Wilson 	u32 seqno = ring->get_seqno(ring);
306b2223497SChris Wilson 	ring->irq_seqno = seqno;
307549f7365SChris Wilson 	trace_i915_gem_request_complete(dev, seqno);
308549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
309549f7365SChris Wilson 	dev_priv->hangcheck_count = 0;
310549f7365SChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
311549f7365SChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
312549f7365SChris Wilson }
313549f7365SChris Wilson 
314995b6762SChris Wilson static irqreturn_t ironlake_irq_handler(struct drm_device *dev)
315036a4a7dSZhenyu Wang {
316036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
317036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
3183ff99164SDave Airlie 	u32 de_iir, gt_iir, de_ier, pch_iir;
3192d7b8366SYuanhan Liu 	u32 hotplug_mask;
320036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
321881f47b6SXiang, Haihao 	u32 bsd_usr_interrupt = GT_BSD_USER_INTERRUPT;
322881f47b6SXiang, Haihao 
323881f47b6SXiang, Haihao 	if (IS_GEN6(dev))
324881f47b6SXiang, Haihao 		bsd_usr_interrupt = GT_GEN6_BSD_USER_INTERRUPT;
325036a4a7dSZhenyu Wang 
3262d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
3272d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
3282d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
3293143a2bfSChris Wilson 	POSTING_READ(DEIER);
3302d109a84SZou, Nanhai 
331036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
332036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
333c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
334036a4a7dSZhenyu Wang 
335c650156aSZhenyu Wang 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0)
336c7c85101SZou Nan hai 		goto done;
337036a4a7dSZhenyu Wang 
3382d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev))
3392d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK_CPT;
3402d7b8366SYuanhan Liu 	else
3412d7b8366SYuanhan Liu 		hotplug_mask = SDE_HOTPLUG_MASK;
3422d7b8366SYuanhan Liu 
343036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
344036a4a7dSZhenyu Wang 
345036a4a7dSZhenyu Wang 	if (dev->primary->master) {
346036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
347036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
348036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
349036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
350036a4a7dSZhenyu Wang 	}
351036a4a7dSZhenyu Wang 
352*88f23b8fSChris Wilson 	if (gt_iir & GT_USER_INTERRUPT)
3531ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[RCS]);
354881f47b6SXiang, Haihao 	if (gt_iir & bsd_usr_interrupt)
3551ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[VCS]);
3561ec14ad3SChris Wilson 	if (gt_iir & GT_BLT_USER_INTERRUPT)
3571ec14ad3SChris Wilson 		notify_ring(dev, &dev_priv->ring[BCS]);
358036a4a7dSZhenyu Wang 
35901c66889SZhao Yakui 	if (de_iir & DE_GSE)
3603b617967SChris Wilson 		intel_opregion_gse_intr(dev);
36101c66889SZhao Yakui 
362f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
363013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
3642bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
365013d5aa2SJesse Barnes 	}
366013d5aa2SJesse Barnes 
367f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
368f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
3692bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
370013d5aa2SJesse Barnes 	}
371c062df61SLi Peng 
372f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
373f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
374f072d2e7SZhenyu Wang 
375f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
376f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
377f072d2e7SZhenyu Wang 
378c650156aSZhenyu Wang 	/* check event from PCH */
3792d7b8366SYuanhan Liu 	if ((de_iir & DE_PCH_EVENT) && (pch_iir & hotplug_mask))
380c650156aSZhenyu Wang 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
381c650156aSZhenyu Wang 
382f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
3837648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
384f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
385f97108d1SJesse Barnes 	}
386f97108d1SJesse Barnes 
387c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
388c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
389c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
390c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
391036a4a7dSZhenyu Wang 
392c7c85101SZou Nan hai done:
3932d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
3943143a2bfSChris Wilson 	POSTING_READ(DEIER);
3952d109a84SZou, Nanhai 
396036a4a7dSZhenyu Wang 	return ret;
397036a4a7dSZhenyu Wang }
398036a4a7dSZhenyu Wang 
3998a905236SJesse Barnes /**
4008a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
4018a905236SJesse Barnes  * @work: work struct
4028a905236SJesse Barnes  *
4038a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
4048a905236SJesse Barnes  * was detected.
4058a905236SJesse Barnes  */
4068a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
4078a905236SJesse Barnes {
4088a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
4098a905236SJesse Barnes 						    error_work);
4108a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
411f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
412f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
413f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
4148a905236SJesse Barnes 
415f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
4168a905236SJesse Barnes 
417ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
41844d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
419f316a42cSBen Gamari 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
420f803aa55SChris Wilson 		if (!i915_reset(dev, GRDOM_RENDER)) {
421ba1234d1SBen Gamari 			atomic_set(&dev_priv->mm.wedged, 0);
422f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
423f316a42cSBen Gamari 		}
42430dbf0c0SChris Wilson 		complete_all(&dev_priv->error_completion);
425f316a42cSBen Gamari 	}
4268a905236SJesse Barnes }
4278a905236SJesse Barnes 
4283bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
4299df30794SChris Wilson static struct drm_i915_error_object *
4309df30794SChris Wilson i915_error_object_create(struct drm_device *dev,
43105394f39SChris Wilson 			 struct drm_i915_gem_object *src)
4329df30794SChris Wilson {
433e56660ddSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
4349df30794SChris Wilson 	struct drm_i915_error_object *dst;
4359df30794SChris Wilson 	int page, page_count;
436e56660ddSChris Wilson 	u32 reloc_offset;
4379df30794SChris Wilson 
43805394f39SChris Wilson 	if (src == NULL || src->pages == NULL)
4399df30794SChris Wilson 		return NULL;
4409df30794SChris Wilson 
44105394f39SChris Wilson 	page_count = src->base.size / PAGE_SIZE;
4429df30794SChris Wilson 
4439df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof (u32 *), GFP_ATOMIC);
4449df30794SChris Wilson 	if (dst == NULL)
4459df30794SChris Wilson 		return NULL;
4469df30794SChris Wilson 
44705394f39SChris Wilson 	reloc_offset = src->gtt_offset;
4489df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
449788885aeSAndrew Morton 		unsigned long flags;
450e56660ddSChris Wilson 		void __iomem *s;
451e56660ddSChris Wilson 		void *d;
452788885aeSAndrew Morton 
453e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
4549df30794SChris Wilson 		if (d == NULL)
4559df30794SChris Wilson 			goto unwind;
456e56660ddSChris Wilson 
457788885aeSAndrew Morton 		local_irq_save(flags);
458e56660ddSChris Wilson 		s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
4593e4d3af5SPeter Zijlstra 					     reloc_offset);
460e56660ddSChris Wilson 		memcpy_fromio(d, s, PAGE_SIZE);
4613e4d3af5SPeter Zijlstra 		io_mapping_unmap_atomic(s);
462788885aeSAndrew Morton 		local_irq_restore(flags);
463e56660ddSChris Wilson 
4649df30794SChris Wilson 		dst->pages[page] = d;
465e56660ddSChris Wilson 
466e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
4679df30794SChris Wilson 	}
4689df30794SChris Wilson 	dst->page_count = page_count;
46905394f39SChris Wilson 	dst->gtt_offset = src->gtt_offset;
4709df30794SChris Wilson 
4719df30794SChris Wilson 	return dst;
4729df30794SChris Wilson 
4739df30794SChris Wilson unwind:
4749df30794SChris Wilson 	while (page--)
4759df30794SChris Wilson 		kfree(dst->pages[page]);
4769df30794SChris Wilson 	kfree(dst);
4779df30794SChris Wilson 	return NULL;
4789df30794SChris Wilson }
4799df30794SChris Wilson 
4809df30794SChris Wilson static void
4819df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
4829df30794SChris Wilson {
4839df30794SChris Wilson 	int page;
4849df30794SChris Wilson 
4859df30794SChris Wilson 	if (obj == NULL)
4869df30794SChris Wilson 		return;
4879df30794SChris Wilson 
4889df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
4899df30794SChris Wilson 		kfree(obj->pages[page]);
4909df30794SChris Wilson 
4919df30794SChris Wilson 	kfree(obj);
4929df30794SChris Wilson }
4939df30794SChris Wilson 
4949df30794SChris Wilson static void
4959df30794SChris Wilson i915_error_state_free(struct drm_device *dev,
4969df30794SChris Wilson 		      struct drm_i915_error_state *error)
4979df30794SChris Wilson {
4989df30794SChris Wilson 	i915_error_object_free(error->batchbuffer[0]);
4999df30794SChris Wilson 	i915_error_object_free(error->batchbuffer[1]);
5009df30794SChris Wilson 	i915_error_object_free(error->ringbuffer);
5019df30794SChris Wilson 	kfree(error->active_bo);
5026ef3d427SChris Wilson 	kfree(error->overlay);
5039df30794SChris Wilson 	kfree(error);
5049df30794SChris Wilson }
5059df30794SChris Wilson 
5069df30794SChris Wilson static u32
5079df30794SChris Wilson i915_get_bbaddr(struct drm_device *dev, u32 *ring)
5089df30794SChris Wilson {
5099df30794SChris Wilson 	u32 cmd;
5109df30794SChris Wilson 
5119df30794SChris Wilson 	if (IS_I830(dev) || IS_845G(dev))
5129df30794SChris Wilson 		cmd = MI_BATCH_BUFFER;
513a6c45cf0SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 4)
5149df30794SChris Wilson 		cmd = (MI_BATCH_BUFFER_START | (2 << 6) |
5159df30794SChris Wilson 		       MI_BATCH_NON_SECURE_I965);
5169df30794SChris Wilson 	else
5179df30794SChris Wilson 		cmd = (MI_BATCH_BUFFER_START | (2 << 6));
5189df30794SChris Wilson 
5199df30794SChris Wilson 	return ring[0] == cmd ? ring[1] : 0;
5209df30794SChris Wilson }
5219df30794SChris Wilson 
5229df30794SChris Wilson static u32
5238168bd48SChris Wilson i915_ringbuffer_last_batch(struct drm_device *dev,
5248168bd48SChris Wilson 			   struct intel_ring_buffer *ring)
5259df30794SChris Wilson {
5269df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
5279df30794SChris Wilson 	u32 head, bbaddr;
5288168bd48SChris Wilson 	u32 *val;
5299df30794SChris Wilson 
5309df30794SChris Wilson 	/* Locate the current position in the ringbuffer and walk back
5319df30794SChris Wilson 	 * to find the most recently dispatched batch buffer.
5329df30794SChris Wilson 	 */
5338168bd48SChris Wilson 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
5349df30794SChris Wilson 
535ab5793adSChris Wilson 	val = (u32 *)(ring->virtual_start + head);
5368168bd48SChris Wilson 	while (--val >= (u32 *)ring->virtual_start) {
5378168bd48SChris Wilson 		bbaddr = i915_get_bbaddr(dev, val);
5389df30794SChris Wilson 		if (bbaddr)
539ab5793adSChris Wilson 			return bbaddr;
5409df30794SChris Wilson 	}
5419df30794SChris Wilson 
5428168bd48SChris Wilson 	val = (u32 *)(ring->virtual_start + ring->size);
5438168bd48SChris Wilson 	while (--val >= (u32 *)ring->virtual_start) {
5448168bd48SChris Wilson 		bbaddr = i915_get_bbaddr(dev, val);
5459df30794SChris Wilson 		if (bbaddr)
546ab5793adSChris Wilson 			return bbaddr;
5479df30794SChris Wilson 	}
5489df30794SChris Wilson 
549ab5793adSChris Wilson 	return 0;
5509df30794SChris Wilson }
5519df30794SChris Wilson 
552c724e8a9SChris Wilson static u32 capture_bo_list(struct drm_i915_error_buffer *err,
553c724e8a9SChris Wilson 			   int count,
554c724e8a9SChris Wilson 			   struct list_head *head)
555c724e8a9SChris Wilson {
556c724e8a9SChris Wilson 	struct drm_i915_gem_object *obj;
557c724e8a9SChris Wilson 	int i = 0;
558c724e8a9SChris Wilson 
559c724e8a9SChris Wilson 	list_for_each_entry(obj, head, mm_list) {
560c724e8a9SChris Wilson 		err->size = obj->base.size;
561c724e8a9SChris Wilson 		err->name = obj->base.name;
562c724e8a9SChris Wilson 		err->seqno = obj->last_rendering_seqno;
563c724e8a9SChris Wilson 		err->gtt_offset = obj->gtt_offset;
564c724e8a9SChris Wilson 		err->read_domains = obj->base.read_domains;
565c724e8a9SChris Wilson 		err->write_domain = obj->base.write_domain;
566c724e8a9SChris Wilson 		err->fence_reg = obj->fence_reg;
567c724e8a9SChris Wilson 		err->pinned = 0;
568c724e8a9SChris Wilson 		if (obj->pin_count > 0)
569c724e8a9SChris Wilson 			err->pinned = 1;
570c724e8a9SChris Wilson 		if (obj->user_pin_count > 0)
571c724e8a9SChris Wilson 			err->pinned = -1;
572c724e8a9SChris Wilson 		err->tiling = obj->tiling_mode;
573c724e8a9SChris Wilson 		err->dirty = obj->dirty;
574c724e8a9SChris Wilson 		err->purgeable = obj->madv != I915_MADV_WILLNEED;
5753685092bSChris Wilson 		err->ring = obj->ring ? obj->ring->id : 0;
576c724e8a9SChris Wilson 
577c724e8a9SChris Wilson 		if (++i == count)
578c724e8a9SChris Wilson 			break;
579c724e8a9SChris Wilson 
580c724e8a9SChris Wilson 		err++;
581c724e8a9SChris Wilson 	}
582c724e8a9SChris Wilson 
583c724e8a9SChris Wilson 	return i;
584c724e8a9SChris Wilson }
585c724e8a9SChris Wilson 
586748ebc60SChris Wilson static void i915_gem_record_fences(struct drm_device *dev,
587748ebc60SChris Wilson 				   struct drm_i915_error_state *error)
588748ebc60SChris Wilson {
589748ebc60SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
590748ebc60SChris Wilson 	int i;
591748ebc60SChris Wilson 
592748ebc60SChris Wilson 	/* Fences */
593748ebc60SChris Wilson 	switch (INTEL_INFO(dev)->gen) {
594748ebc60SChris Wilson 	case 6:
595748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
596748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
597748ebc60SChris Wilson 		break;
598748ebc60SChris Wilson 	case 5:
599748ebc60SChris Wilson 	case 4:
600748ebc60SChris Wilson 		for (i = 0; i < 16; i++)
601748ebc60SChris Wilson 			error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
602748ebc60SChris Wilson 		break;
603748ebc60SChris Wilson 	case 3:
604748ebc60SChris Wilson 		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
605748ebc60SChris Wilson 			for (i = 0; i < 8; i++)
606748ebc60SChris Wilson 				error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
607748ebc60SChris Wilson 	case 2:
608748ebc60SChris Wilson 		for (i = 0; i < 8; i++)
609748ebc60SChris Wilson 			error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
610748ebc60SChris Wilson 		break;
611748ebc60SChris Wilson 
612748ebc60SChris Wilson 	}
613748ebc60SChris Wilson }
614748ebc60SChris Wilson 
6158a905236SJesse Barnes /**
6168a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
6178a905236SJesse Barnes  * @dev: drm device
6188a905236SJesse Barnes  *
6198a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
6208a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
6218a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
6228a905236SJesse Barnes  * to pick up.
6238a905236SJesse Barnes  */
62463eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
62563eeaf38SJesse Barnes {
62663eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
62705394f39SChris Wilson 	struct drm_i915_gem_object *obj;
62863eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
62905394f39SChris Wilson 	struct drm_i915_gem_object *batchbuffer[2];
63063eeaf38SJesse Barnes 	unsigned long flags;
6319df30794SChris Wilson 	u32 bbaddr;
6329df30794SChris Wilson 	int count;
63363eeaf38SJesse Barnes 
63463eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
6359df30794SChris Wilson 	error = dev_priv->first_error;
6369df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
6379df30794SChris Wilson 	if (error)
6389df30794SChris Wilson 		return;
63963eeaf38SJesse Barnes 
64063eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
64163eeaf38SJesse Barnes 	if (!error) {
6429df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
6439df30794SChris Wilson 		return;
64463eeaf38SJesse Barnes 	}
64563eeaf38SJesse Barnes 
6462fa772f3SChris Wilson 	DRM_DEBUG_DRIVER("generating error event\n");
6472fa772f3SChris Wilson 
6481ec14ad3SChris Wilson 	error->seqno = dev_priv->ring[RCS].get_seqno(&dev_priv->ring[RCS]);
64963eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
65063eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
65163eeaf38SJesse Barnes 	error->pipeastat = I915_READ(PIPEASTAT);
65263eeaf38SJesse Barnes 	error->pipebstat = I915_READ(PIPEBSTAT);
65363eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
654f406839fSChris Wilson 	error->error = 0;
655f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 6) {
656f406839fSChris Wilson 		error->error = I915_READ(ERROR_GEN6);
657add354ddSChris Wilson 
6581d8f38f4SChris Wilson 		error->bcs_acthd = I915_READ(BCS_ACTHD);
6591d8f38f4SChris Wilson 		error->bcs_ipehr = I915_READ(BCS_IPEHR);
6601d8f38f4SChris Wilson 		error->bcs_ipeir = I915_READ(BCS_IPEIR);
6611d8f38f4SChris Wilson 		error->bcs_instdone = I915_READ(BCS_INSTDONE);
6621d8f38f4SChris Wilson 		error->bcs_seqno = 0;
6631ec14ad3SChris Wilson 		if (dev_priv->ring[BCS].get_seqno)
6641ec14ad3SChris Wilson 			error->bcs_seqno = dev_priv->ring[BCS].get_seqno(&dev_priv->ring[BCS]);
665add354ddSChris Wilson 
666add354ddSChris Wilson 		error->vcs_acthd = I915_READ(VCS_ACTHD);
667add354ddSChris Wilson 		error->vcs_ipehr = I915_READ(VCS_IPEHR);
668add354ddSChris Wilson 		error->vcs_ipeir = I915_READ(VCS_IPEIR);
669add354ddSChris Wilson 		error->vcs_instdone = I915_READ(VCS_INSTDONE);
670add354ddSChris Wilson 		error->vcs_seqno = 0;
6711ec14ad3SChris Wilson 		if (dev_priv->ring[VCS].get_seqno)
6721ec14ad3SChris Wilson 			error->vcs_seqno = dev_priv->ring[VCS].get_seqno(&dev_priv->ring[VCS]);
673f406839fSChris Wilson 	}
674f406839fSChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
67563eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
67663eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
67763eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
67863eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
67963eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
68063eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
6819df30794SChris Wilson 		error->bbaddr = I915_READ64(BB_ADDR);
682f406839fSChris Wilson 	} else {
683f406839fSChris Wilson 		error->ipeir = I915_READ(IPEIR);
684f406839fSChris Wilson 		error->ipehr = I915_READ(IPEHR);
685f406839fSChris Wilson 		error->instdone = I915_READ(INSTDONE);
686f406839fSChris Wilson 		error->acthd = I915_READ(ACTHD);
687f406839fSChris Wilson 		error->bbaddr = 0;
6889df30794SChris Wilson 	}
689748ebc60SChris Wilson 	i915_gem_record_fences(dev, error);
6909df30794SChris Wilson 
6911ec14ad3SChris Wilson 	bbaddr = i915_ringbuffer_last_batch(dev, &dev_priv->ring[RCS]);
6929df30794SChris Wilson 
6939df30794SChris Wilson 	/* Grab the current batchbuffer, most likely to have crashed. */
6949df30794SChris Wilson 	batchbuffer[0] = NULL;
6959df30794SChris Wilson 	batchbuffer[1] = NULL;
6969df30794SChris Wilson 	count = 0;
69705394f39SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
6989df30794SChris Wilson 		if (batchbuffer[0] == NULL &&
69905394f39SChris Wilson 		    bbaddr >= obj->gtt_offset &&
70005394f39SChris Wilson 		    bbaddr < obj->gtt_offset + obj->base.size)
7019df30794SChris Wilson 			batchbuffer[0] = obj;
7029df30794SChris Wilson 
7039df30794SChris Wilson 		if (batchbuffer[1] == NULL &&
70405394f39SChris Wilson 		    error->acthd >= obj->gtt_offset &&
70505394f39SChris Wilson 		    error->acthd < obj->gtt_offset + obj->base.size)
7069df30794SChris Wilson 			batchbuffer[1] = obj;
7079df30794SChris Wilson 
7089df30794SChris Wilson 		count++;
7099df30794SChris Wilson 	}
710e56660ddSChris Wilson 	/* Scan the other lists for completeness for those bizarre errors. */
711e56660ddSChris Wilson 	if (batchbuffer[0] == NULL || batchbuffer[1] == NULL) {
71205394f39SChris Wilson 		list_for_each_entry(obj, &dev_priv->mm.flushing_list, mm_list) {
713e56660ddSChris Wilson 			if (batchbuffer[0] == NULL &&
71405394f39SChris Wilson 			    bbaddr >= obj->gtt_offset &&
71505394f39SChris Wilson 			    bbaddr < obj->gtt_offset + obj->base.size)
716e56660ddSChris Wilson 				batchbuffer[0] = obj;
717e56660ddSChris Wilson 
718e56660ddSChris Wilson 			if (batchbuffer[1] == NULL &&
71905394f39SChris Wilson 			    error->acthd >= obj->gtt_offset &&
72005394f39SChris Wilson 			    error->acthd < obj->gtt_offset + obj->base.size)
721e56660ddSChris Wilson 				batchbuffer[1] = obj;
722e56660ddSChris Wilson 
723e56660ddSChris Wilson 			if (batchbuffer[0] && batchbuffer[1])
724e56660ddSChris Wilson 				break;
725e56660ddSChris Wilson 		}
726e56660ddSChris Wilson 	}
727e56660ddSChris Wilson 	if (batchbuffer[0] == NULL || batchbuffer[1] == NULL) {
72805394f39SChris Wilson 		list_for_each_entry(obj, &dev_priv->mm.inactive_list, mm_list) {
729e56660ddSChris Wilson 			if (batchbuffer[0] == NULL &&
73005394f39SChris Wilson 			    bbaddr >= obj->gtt_offset &&
73105394f39SChris Wilson 			    bbaddr < obj->gtt_offset + obj->base.size)
732e56660ddSChris Wilson 				batchbuffer[0] = obj;
733e56660ddSChris Wilson 
734e56660ddSChris Wilson 			if (batchbuffer[1] == NULL &&
73505394f39SChris Wilson 			    error->acthd >= obj->gtt_offset &&
73605394f39SChris Wilson 			    error->acthd < obj->gtt_offset + obj->base.size)
737e56660ddSChris Wilson 				batchbuffer[1] = obj;
738e56660ddSChris Wilson 
739e56660ddSChris Wilson 			if (batchbuffer[0] && batchbuffer[1])
740e56660ddSChris Wilson 				break;
741e56660ddSChris Wilson 		}
742e56660ddSChris Wilson 	}
7439df30794SChris Wilson 
7449df30794SChris Wilson 	/* We need to copy these to an anonymous buffer as the simplest
745139d363bSAndrea Gelmini 	 * method to avoid being overwritten by userspace.
7469df30794SChris Wilson 	 */
7479df30794SChris Wilson 	error->batchbuffer[0] = i915_error_object_create(dev, batchbuffer[0]);
748e56660ddSChris Wilson 	if (batchbuffer[1] != batchbuffer[0])
7499df30794SChris Wilson 		error->batchbuffer[1] = i915_error_object_create(dev, batchbuffer[1]);
750e56660ddSChris Wilson 	else
751e56660ddSChris Wilson 		error->batchbuffer[1] = NULL;
7529df30794SChris Wilson 
7539df30794SChris Wilson 	/* Record the ringbuffer */
7548187a2b7SZou Nan hai 	error->ringbuffer = i915_error_object_create(dev,
7551ec14ad3SChris Wilson 						     dev_priv->ring[RCS].obj);
7569df30794SChris Wilson 
757c724e8a9SChris Wilson 	/* Record buffers on the active and pinned lists. */
7589df30794SChris Wilson 	error->active_bo = NULL;
759c724e8a9SChris Wilson 	error->pinned_bo = NULL;
7609df30794SChris Wilson 
761c724e8a9SChris Wilson 	error->active_bo_count = count;
76205394f39SChris Wilson 	list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
763c724e8a9SChris Wilson 		count++;
764c724e8a9SChris Wilson 	error->pinned_bo_count = count - error->active_bo_count;
765c724e8a9SChris Wilson 
766c724e8a9SChris Wilson 	if (count) {
7679df30794SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*count,
7689df30794SChris Wilson 					   GFP_ATOMIC);
769c724e8a9SChris Wilson 		if (error->active_bo)
770c724e8a9SChris Wilson 			error->pinned_bo =
771c724e8a9SChris Wilson 				error->active_bo + error->active_bo_count;
7729df30794SChris Wilson 	}
773c724e8a9SChris Wilson 
774c724e8a9SChris Wilson 	if (error->active_bo)
775c724e8a9SChris Wilson 		error->active_bo_count =
776c724e8a9SChris Wilson 			capture_bo_list(error->active_bo,
777c724e8a9SChris Wilson 					error->active_bo_count,
778c724e8a9SChris Wilson 					&dev_priv->mm.active_list);
779c724e8a9SChris Wilson 
780c724e8a9SChris Wilson 	if (error->pinned_bo)
781c724e8a9SChris Wilson 		error->pinned_bo_count =
782c724e8a9SChris Wilson 			capture_bo_list(error->pinned_bo,
783c724e8a9SChris Wilson 					error->pinned_bo_count,
784c724e8a9SChris Wilson 					&dev_priv->mm.pinned_list);
78563eeaf38SJesse Barnes 
7868a905236SJesse Barnes 	do_gettimeofday(&error->time);
7878a905236SJesse Barnes 
7886ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
789c4a1d9e4SChris Wilson 	error->display = intel_display_capture_error_state(dev);
7906ef3d427SChris Wilson 
7919df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
7929df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
79363eeaf38SJesse Barnes 		dev_priv->first_error = error;
7949df30794SChris Wilson 		error = NULL;
7959df30794SChris Wilson 	}
79663eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
7979df30794SChris Wilson 
7989df30794SChris Wilson 	if (error)
7999df30794SChris Wilson 		i915_error_state_free(dev, error);
8009df30794SChris Wilson }
8019df30794SChris Wilson 
8029df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
8039df30794SChris Wilson {
8049df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
8059df30794SChris Wilson 	struct drm_i915_error_state *error;
8069df30794SChris Wilson 
8079df30794SChris Wilson 	spin_lock(&dev_priv->error_lock);
8089df30794SChris Wilson 	error = dev_priv->first_error;
8099df30794SChris Wilson 	dev_priv->first_error = NULL;
8109df30794SChris Wilson 	spin_unlock(&dev_priv->error_lock);
8119df30794SChris Wilson 
8129df30794SChris Wilson 	if (error)
8139df30794SChris Wilson 		i915_error_state_free(dev, error);
81463eeaf38SJesse Barnes }
8153bd3c932SChris Wilson #else
8163bd3c932SChris Wilson #define i915_capture_error_state(x)
8173bd3c932SChris Wilson #endif
81863eeaf38SJesse Barnes 
81935aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
820c0e09200SDave Airlie {
8218a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
82263eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
82363eeaf38SJesse Barnes 
82435aed2e6SChris Wilson 	if (!eir)
82535aed2e6SChris Wilson 		return;
82663eeaf38SJesse Barnes 
82763eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
82863eeaf38SJesse Barnes 	       eir);
8298a905236SJesse Barnes 
8308a905236SJesse Barnes 	if (IS_G4X(dev)) {
8318a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
8328a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
8338a905236SJesse Barnes 
8348a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
8358a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
8368a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
8378a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
8388a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
8398a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
8408a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
8418a905236SJesse Barnes 			       I915_READ(INSTPS));
8428a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
8438a905236SJesse Barnes 			       I915_READ(INSTDONE1));
8448a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
8458a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
8468a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
8473143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
8488a905236SJesse Barnes 		}
8498a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
8508a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
8518a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
8528a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
8538a905236SJesse Barnes 			       pgtbl_err);
8548a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
8553143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
8568a905236SJesse Barnes 		}
8578a905236SJesse Barnes 	}
8588a905236SJesse Barnes 
859a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
86063eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
86163eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
86263eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
86363eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
86463eeaf38SJesse Barnes 			       pgtbl_err);
86563eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
8663143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
86763eeaf38SJesse Barnes 		}
8688a905236SJesse Barnes 	}
8698a905236SJesse Barnes 
87063eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
87135aed2e6SChris Wilson 		u32 pipea_stats = I915_READ(PIPEASTAT);
87235aed2e6SChris Wilson 		u32 pipeb_stats = I915_READ(PIPEBSTAT);
87335aed2e6SChris Wilson 
87463eeaf38SJesse Barnes 		printk(KERN_ERR "memory refresh error\n");
87563eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
87663eeaf38SJesse Barnes 		       pipea_stats);
87763eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
87863eeaf38SJesse Barnes 		       pipeb_stats);
87963eeaf38SJesse Barnes 		/* pipestat has already been acked */
88063eeaf38SJesse Barnes 	}
88163eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
88263eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
88363eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
88463eeaf38SJesse Barnes 		       I915_READ(INSTPM));
885a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
88663eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
88763eeaf38SJesse Barnes 
88863eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
88963eeaf38SJesse Barnes 			       I915_READ(IPEIR));
89063eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
89163eeaf38SJesse Barnes 			       I915_READ(IPEHR));
89263eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
89363eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
89463eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
89563eeaf38SJesse Barnes 			       I915_READ(ACTHD));
89663eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
8973143a2bfSChris Wilson 			POSTING_READ(IPEIR);
89863eeaf38SJesse Barnes 		} else {
89963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
90063eeaf38SJesse Barnes 
90163eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
90263eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
90363eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
90463eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
90563eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
90663eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
90763eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
90863eeaf38SJesse Barnes 			       I915_READ(INSTPS));
90963eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
91063eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
91163eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
91263eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
91363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
9143143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
91563eeaf38SJesse Barnes 		}
91663eeaf38SJesse Barnes 	}
91763eeaf38SJesse Barnes 
91863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
9193143a2bfSChris Wilson 	POSTING_READ(EIR);
92063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
92163eeaf38SJesse Barnes 	if (eir) {
92263eeaf38SJesse Barnes 		/*
92363eeaf38SJesse Barnes 		 * some errors might have become stuck,
92463eeaf38SJesse Barnes 		 * mask them.
92563eeaf38SJesse Barnes 		 */
92663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
92763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
92863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
92963eeaf38SJesse Barnes 	}
93035aed2e6SChris Wilson }
93135aed2e6SChris Wilson 
93235aed2e6SChris Wilson /**
93335aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
93435aed2e6SChris Wilson  * @dev: drm device
93535aed2e6SChris Wilson  *
93635aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
93735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
93835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
93935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
94035aed2e6SChris Wilson  * of a ring dump etc.).
94135aed2e6SChris Wilson  */
942527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
94335aed2e6SChris Wilson {
94435aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
94535aed2e6SChris Wilson 
94635aed2e6SChris Wilson 	i915_capture_error_state(dev);
94735aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
9488a905236SJesse Barnes 
949ba1234d1SBen Gamari 	if (wedged) {
95030dbf0c0SChris Wilson 		INIT_COMPLETION(dev_priv->error_completion);
951ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
952ba1234d1SBen Gamari 
95311ed50ecSBen Gamari 		/*
95411ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
95511ed50ecSBen Gamari 		 */
9561ec14ad3SChris Wilson 		wake_up_all(&dev_priv->ring[RCS].irq_queue);
957f787a5f5SChris Wilson 		if (HAS_BSD(dev))
9581ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[VCS].irq_queue);
959549f7365SChris Wilson 		if (HAS_BLT(dev))
9601ec14ad3SChris Wilson 			wake_up_all(&dev_priv->ring[BCS].irq_queue);
96111ed50ecSBen Gamari 	}
96211ed50ecSBen Gamari 
9639c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
9648a905236SJesse Barnes }
9658a905236SJesse Barnes 
9664e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
9674e5359cdSSimon Farnsworth {
9684e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
9694e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9704e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
97105394f39SChris Wilson 	struct drm_i915_gem_object *obj;
9724e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
9734e5359cdSSimon Farnsworth 	unsigned long flags;
9744e5359cdSSimon Farnsworth 	bool stall_detected;
9754e5359cdSSimon Farnsworth 
9764e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
9774e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
9784e5359cdSSimon Farnsworth 		return;
9794e5359cdSSimon Farnsworth 
9804e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
9814e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
9824e5359cdSSimon Farnsworth 
9834e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
9844e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
9854e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
9864e5359cdSSimon Farnsworth 		return;
9874e5359cdSSimon Farnsworth 	}
9884e5359cdSSimon Farnsworth 
9894e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
99005394f39SChris Wilson 	obj = work->pending_flip_obj;
991a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
9924e5359cdSSimon Farnsworth 		int dspsurf = intel_crtc->plane == 0 ? DSPASURF : DSPBSURF;
99305394f39SChris Wilson 		stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
9944e5359cdSSimon Farnsworth 	} else {
9954e5359cdSSimon Farnsworth 		int dspaddr = intel_crtc->plane == 0 ? DSPAADDR : DSPBADDR;
99605394f39SChris Wilson 		stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
9974e5359cdSSimon Farnsworth 							crtc->y * crtc->fb->pitch +
9984e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
9994e5359cdSSimon Farnsworth 	}
10004e5359cdSSimon Farnsworth 
10014e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
10024e5359cdSSimon Farnsworth 
10034e5359cdSSimon Farnsworth 	if (stall_detected) {
10044e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
10054e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
10064e5359cdSSimon Farnsworth 	}
10074e5359cdSSimon Farnsworth }
10084e5359cdSSimon Farnsworth 
10098a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
10108a905236SJesse Barnes {
10118a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
10128a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10138a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
10148a905236SJesse Barnes 	u32 iir, new_iir;
10158a905236SJesse Barnes 	u32 pipea_stats, pipeb_stats;
10168a905236SJesse Barnes 	u32 vblank_status;
10178a905236SJesse Barnes 	int vblank = 0;
10188a905236SJesse Barnes 	unsigned long irqflags;
10198a905236SJesse Barnes 	int irq_received;
10208a905236SJesse Barnes 	int ret = IRQ_NONE;
10218a905236SJesse Barnes 
10228a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
10238a905236SJesse Barnes 
1024bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1025f2b115e6SAdam Jackson 		return ironlake_irq_handler(dev);
10268a905236SJesse Barnes 
10278a905236SJesse Barnes 	iir = I915_READ(IIR);
10288a905236SJesse Barnes 
1029a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
1030d874bcffSJesse Barnes 		vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
1031e25e6601SJesse Barnes 	else
1032d874bcffSJesse Barnes 		vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
10338a905236SJesse Barnes 
10348a905236SJesse Barnes 	for (;;) {
10358a905236SJesse Barnes 		irq_received = iir != 0;
10368a905236SJesse Barnes 
10378a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
10388a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
10398a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
10408a905236SJesse Barnes 		 * interrupts (for non-MSI).
10418a905236SJesse Barnes 		 */
10421ec14ad3SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
10438a905236SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
10448a905236SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
10458a905236SJesse Barnes 
10468a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
1047ba1234d1SBen Gamari 			i915_handle_error(dev, false);
10488a905236SJesse Barnes 
10498a905236SJesse Barnes 		/*
10508a905236SJesse Barnes 		 * Clear the PIPE(A|B)STAT regs before the IIR
10518a905236SJesse Barnes 		 */
10528a905236SJesse Barnes 		if (pipea_stats & 0x8000ffff) {
10538a905236SJesse Barnes 			if (pipea_stats &  PIPE_FIFO_UNDERRUN_STATUS)
105444d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe a underrun\n");
10558a905236SJesse Barnes 			I915_WRITE(PIPEASTAT, pipea_stats);
10568a905236SJesse Barnes 			irq_received = 1;
10578a905236SJesse Barnes 		}
10588a905236SJesse Barnes 
10598a905236SJesse Barnes 		if (pipeb_stats & 0x8000ffff) {
10608a905236SJesse Barnes 			if (pipeb_stats &  PIPE_FIFO_UNDERRUN_STATUS)
106144d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe b underrun\n");
10628a905236SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
10638a905236SJesse Barnes 			irq_received = 1;
10648a905236SJesse Barnes 		}
10651ec14ad3SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
10668a905236SJesse Barnes 
10678a905236SJesse Barnes 		if (!irq_received)
10688a905236SJesse Barnes 			break;
10698a905236SJesse Barnes 
10708a905236SJesse Barnes 		ret = IRQ_HANDLED;
10718a905236SJesse Barnes 
10728a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
10738a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
10748a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
10758a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
10768a905236SJesse Barnes 
107744d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
10788a905236SJesse Barnes 				  hotplug_status);
10798a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
10809c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
10819c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
10828a905236SJesse Barnes 
10838a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
10848a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
108563eeaf38SJesse Barnes 		}
108663eeaf38SJesse Barnes 
1087673a394bSEric Anholt 		I915_WRITE(IIR, iir);
1088cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
10897c463586SKeith Packard 
10907c1c2871SDave Airlie 		if (dev->primary->master) {
10917c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
10927c1c2871SDave Airlie 			if (master_priv->sarea_priv)
10937c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
1094c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
10957c1c2871SDave Airlie 		}
10960a3e67a4SJesse Barnes 
1097549f7365SChris Wilson 		if (iir & I915_USER_INTERRUPT)
10981ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
10991ec14ad3SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
11001ec14ad3SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
1101d1b851fcSZou Nan hai 
11021afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
11036b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
11041afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
11051afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 0);
11061afe3e9dSJesse Barnes 		}
11076b95a207SKristian Høgsberg 
11081afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
110970565d00SJesse Barnes 			intel_prepare_page_flip(dev, 1);
11101afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
11111afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 1);
11121afe3e9dSJesse Barnes 		}
11136b95a207SKristian Høgsberg 
111405eff845SKeith Packard 		if (pipea_stats & vblank_status) {
11157c463586SKeith Packard 			vblank++;
11167c463586SKeith Packard 			drm_handle_vblank(dev, 0);
11174e5359cdSSimon Farnsworth 			if (!dev_priv->flip_pending_is_done) {
11184e5359cdSSimon Farnsworth 				i915_pageflip_stall_check(dev, 0);
11196b95a207SKristian Høgsberg 				intel_finish_page_flip(dev, 0);
11207c463586SKeith Packard 			}
11214e5359cdSSimon Farnsworth 		}
11227c463586SKeith Packard 
112305eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
11247c463586SKeith Packard 			vblank++;
11257c463586SKeith Packard 			drm_handle_vblank(dev, 1);
11264e5359cdSSimon Farnsworth 			if (!dev_priv->flip_pending_is_done) {
11274e5359cdSSimon Farnsworth 				i915_pageflip_stall_check(dev, 1);
11286b95a207SKristian Høgsberg 				intel_finish_page_flip(dev, 1);
11297c463586SKeith Packard 			}
11304e5359cdSSimon Farnsworth 		}
11317c463586SKeith Packard 
1132d874bcffSJesse Barnes 		if ((pipea_stats & PIPE_LEGACY_BLC_EVENT_STATUS) ||
1133d874bcffSJesse Barnes 		    (pipeb_stats & PIPE_LEGACY_BLC_EVENT_STATUS) ||
11347c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
11353b617967SChris Wilson 			intel_opregion_asle_intr(dev);
11360a3e67a4SJesse Barnes 
1137cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
1138cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
1139cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
1140cdfbc41fSEric Anholt 		 * we would never get another interrupt.
1141cdfbc41fSEric Anholt 		 *
1142cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
1143cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
1144cdfbc41fSEric Anholt 		 * another one.
1145cdfbc41fSEric Anholt 		 *
1146cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
1147cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
1148cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
1149cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
1150cdfbc41fSEric Anholt 		 * stray interrupts.
1151cdfbc41fSEric Anholt 		 */
1152cdfbc41fSEric Anholt 		iir = new_iir;
115305eff845SKeith Packard 	}
1154cdfbc41fSEric Anholt 
115505eff845SKeith Packard 	return ret;
1156c0e09200SDave Airlie }
1157c0e09200SDave Airlie 
1158c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
1159c0e09200SDave Airlie {
1160c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
11617c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1162c0e09200SDave Airlie 
1163c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
1164c0e09200SDave Airlie 
116544d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
1166c0e09200SDave Airlie 
1167c99b058fSKristian Høgsberg 	dev_priv->counter++;
1168c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
1169c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
11707c1c2871SDave Airlie 	if (master_priv->sarea_priv)
11717c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
1172c0e09200SDave Airlie 
1173e1f99ce6SChris Wilson 	if (BEGIN_LP_RING(4) == 0) {
1174585fb111SJesse Barnes 		OUT_RING(MI_STORE_DWORD_INDEX);
11750baf823aSKeith Packard 		OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1176c0e09200SDave Airlie 		OUT_RING(dev_priv->counter);
1177585fb111SJesse Barnes 		OUT_RING(MI_USER_INTERRUPT);
1178c0e09200SDave Airlie 		ADVANCE_LP_RING();
1179e1f99ce6SChris Wilson 	}
1180c0e09200SDave Airlie 
1181c0e09200SDave Airlie 	return dev_priv->counter;
1182c0e09200SDave Airlie }
1183c0e09200SDave Airlie 
11849d34e5dbSChris Wilson void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
11859d34e5dbSChris Wilson {
11869d34e5dbSChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11871ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
11889d34e5dbSChris Wilson 
11899d34e5dbSChris Wilson 	if (dev_priv->trace_irq_seqno == 0)
11901ec14ad3SChris Wilson 		ring->irq_get(ring);
11919d34e5dbSChris Wilson 
11929d34e5dbSChris Wilson 	dev_priv->trace_irq_seqno = seqno;
11939d34e5dbSChris Wilson }
11949d34e5dbSChris Wilson 
1195c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
1196c0e09200SDave Airlie {
1197c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11987c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1199c0e09200SDave Airlie 	int ret = 0;
12001ec14ad3SChris Wilson 	struct intel_ring_buffer *ring = LP_RING(dev_priv);
1201c0e09200SDave Airlie 
120244d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
1203c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
1204c0e09200SDave Airlie 
1205ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
12067c1c2871SDave Airlie 		if (master_priv->sarea_priv)
12077c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
1208c0e09200SDave Airlie 		return 0;
1209ed4cb414SEric Anholt 	}
1210c0e09200SDave Airlie 
12117c1c2871SDave Airlie 	if (master_priv->sarea_priv)
12127c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1213c0e09200SDave Airlie 
12141ec14ad3SChris Wilson 	ring->irq_get(ring);
12151ec14ad3SChris Wilson 	DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1216c0e09200SDave Airlie 		    READ_BREADCRUMB(dev_priv) >= irq_nr);
12171ec14ad3SChris Wilson 	ring->irq_put(ring);
1218c0e09200SDave Airlie 
1219c0e09200SDave Airlie 	if (ret == -EBUSY) {
1220c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
1221c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1222c0e09200SDave Airlie 	}
1223c0e09200SDave Airlie 
1224c0e09200SDave Airlie 	return ret;
1225c0e09200SDave Airlie }
1226c0e09200SDave Airlie 
1227c0e09200SDave Airlie /* Needs the lock as it touches the ring.
1228c0e09200SDave Airlie  */
1229c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
1230c0e09200SDave Airlie 			 struct drm_file *file_priv)
1231c0e09200SDave Airlie {
1232c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1233c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
1234c0e09200SDave Airlie 	int result;
1235c0e09200SDave Airlie 
12361ec14ad3SChris Wilson 	if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
1237c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1238c0e09200SDave Airlie 		return -EINVAL;
1239c0e09200SDave Airlie 	}
1240299eb93cSEric Anholt 
1241299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1242299eb93cSEric Anholt 
1243546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
1244c0e09200SDave Airlie 	result = i915_emit_irq(dev);
1245546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
1246c0e09200SDave Airlie 
1247c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
1248c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
1249c0e09200SDave Airlie 		return -EFAULT;
1250c0e09200SDave Airlie 	}
1251c0e09200SDave Airlie 
1252c0e09200SDave Airlie 	return 0;
1253c0e09200SDave Airlie }
1254c0e09200SDave Airlie 
1255c0e09200SDave Airlie /* Doesn't need the hardware lock.
1256c0e09200SDave Airlie  */
1257c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
1258c0e09200SDave Airlie 			 struct drm_file *file_priv)
1259c0e09200SDave Airlie {
1260c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1261c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
1262c0e09200SDave Airlie 
1263c0e09200SDave Airlie 	if (!dev_priv) {
1264c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1265c0e09200SDave Airlie 		return -EINVAL;
1266c0e09200SDave Airlie 	}
1267c0e09200SDave Airlie 
1268c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
1269c0e09200SDave Airlie }
1270c0e09200SDave Airlie 
127142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
127242f52ef8SKeith Packard  * we use as a pipe index
127342f52ef8SKeith Packard  */
127442f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
12750a3e67a4SJesse Barnes {
12760a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1277e9d21d7fSKeith Packard 	unsigned long irqflags;
127871e0ffa5SJesse Barnes 
12795eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
128071e0ffa5SJesse Barnes 		return -EINVAL;
12810a3e67a4SJesse Barnes 
12821ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1283bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1284c062df61SLi Peng 		ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1285c062df61SLi Peng 					    DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1286a6c45cf0SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 4)
12877c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
12887c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
12890a3e67a4SJesse Barnes 	else
12907c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
12917c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
12921ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
12930a3e67a4SJesse Barnes 	return 0;
12940a3e67a4SJesse Barnes }
12950a3e67a4SJesse Barnes 
129642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
129742f52ef8SKeith Packard  * we use as a pipe index
129842f52ef8SKeith Packard  */
129942f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
13000a3e67a4SJesse Barnes {
13010a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1302e9d21d7fSKeith Packard 	unsigned long irqflags;
13030a3e67a4SJesse Barnes 
13041ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1305bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1306c062df61SLi Peng 		ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1307c062df61SLi Peng 					     DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1308c062df61SLi Peng 	else
13097c463586SKeith Packard 		i915_disable_pipestat(dev_priv, pipe,
13107c463586SKeith Packard 				      PIPE_VBLANK_INTERRUPT_ENABLE |
13117c463586SKeith Packard 				      PIPE_START_VBLANK_INTERRUPT_ENABLE);
13121ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
13130a3e67a4SJesse Barnes }
13140a3e67a4SJesse Barnes 
131579e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev)
131679e53945SJesse Barnes {
131779e53945SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1318e170b030SZhenyu Wang 
1319bad720ffSEric Anholt 	if (!HAS_PCH_SPLIT(dev))
13203b617967SChris Wilson 		intel_opregion_enable_asle(dev);
132179e53945SJesse Barnes 	dev_priv->irq_enabled = 1;
132279e53945SJesse Barnes }
132379e53945SJesse Barnes 
132479e53945SJesse Barnes 
1325c0e09200SDave Airlie /* Set the vblank monitor pipe
1326c0e09200SDave Airlie  */
1327c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1328c0e09200SDave Airlie 			 struct drm_file *file_priv)
1329c0e09200SDave Airlie {
1330c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1331c0e09200SDave Airlie 
1332c0e09200SDave Airlie 	if (!dev_priv) {
1333c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1334c0e09200SDave Airlie 		return -EINVAL;
1335c0e09200SDave Airlie 	}
1336c0e09200SDave Airlie 
1337c0e09200SDave Airlie 	return 0;
1338c0e09200SDave Airlie }
1339c0e09200SDave Airlie 
1340c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1341c0e09200SDave Airlie 			 struct drm_file *file_priv)
1342c0e09200SDave Airlie {
1343c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1344c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
1345c0e09200SDave Airlie 
1346c0e09200SDave Airlie 	if (!dev_priv) {
1347c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1348c0e09200SDave Airlie 		return -EINVAL;
1349c0e09200SDave Airlie 	}
1350c0e09200SDave Airlie 
13510a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1352c0e09200SDave Airlie 
1353c0e09200SDave Airlie 	return 0;
1354c0e09200SDave Airlie }
1355c0e09200SDave Airlie 
1356c0e09200SDave Airlie /**
1357c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
1358c0e09200SDave Airlie  */
1359c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
1360c0e09200SDave Airlie 		     struct drm_file *file_priv)
1361c0e09200SDave Airlie {
1362bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1363bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1364bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1365bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1366bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1367bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1368bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1369bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1370bd95e0a4SEric Anholt 	 *
1371bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1372bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1373bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1374bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
13750a3e67a4SJesse Barnes 	 */
1376c0e09200SDave Airlie 	return -EINVAL;
1377c0e09200SDave Airlie }
1378c0e09200SDave Airlie 
1379893eead0SChris Wilson static u32
1380893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
1381852835f3SZou Nan hai {
1382893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
1383893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
1384893eead0SChris Wilson }
1385893eead0SChris Wilson 
1386893eead0SChris Wilson static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1387893eead0SChris Wilson {
1388893eead0SChris Wilson 	if (list_empty(&ring->request_list) ||
1389893eead0SChris Wilson 	    i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1390893eead0SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
1391b2223497SChris Wilson 		if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
1392893eead0SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1393893eead0SChris Wilson 				  ring->name,
1394b2223497SChris Wilson 				  ring->waiting_seqno,
1395893eead0SChris Wilson 				  ring->get_seqno(ring));
1396893eead0SChris Wilson 			wake_up_all(&ring->irq_queue);
1397893eead0SChris Wilson 			*err = true;
1398893eead0SChris Wilson 		}
1399893eead0SChris Wilson 		return true;
1400893eead0SChris Wilson 	}
1401893eead0SChris Wilson 	return false;
1402f65d9421SBen Gamari }
1403f65d9421SBen Gamari 
14041ec14ad3SChris Wilson static bool kick_ring(struct intel_ring_buffer *ring)
14051ec14ad3SChris Wilson {
14061ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
14071ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
14081ec14ad3SChris Wilson 	u32 tmp = I915_READ_CTL(ring);
14091ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
14101ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
14111ec14ad3SChris Wilson 			  ring->name);
14121ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
14131ec14ad3SChris Wilson 		return true;
14141ec14ad3SChris Wilson 	}
14151ec14ad3SChris Wilson 	if (IS_GEN6(dev) &&
14161ec14ad3SChris Wilson 	    (tmp & RING_WAIT_SEMAPHORE)) {
14171ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck semaphore on %s\n",
14181ec14ad3SChris Wilson 			  ring->name);
14191ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
14201ec14ad3SChris Wilson 		return true;
14211ec14ad3SChris Wilson 	}
14221ec14ad3SChris Wilson 	return false;
14231ec14ad3SChris Wilson }
14241ec14ad3SChris Wilson 
1425f65d9421SBen Gamari /**
1426f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1427f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1428f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1429f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1430f65d9421SBen Gamari  */
1431f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1432f65d9421SBen Gamari {
1433f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1434f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1435cbb465e7SChris Wilson 	uint32_t acthd, instdone, instdone1;
1436893eead0SChris Wilson 	bool err = false;
1437893eead0SChris Wilson 
1438893eead0SChris Wilson 	/* If all work is done then ACTHD clearly hasn't advanced. */
14391ec14ad3SChris Wilson 	if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
14401ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
14411ec14ad3SChris Wilson 	    i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
1442893eead0SChris Wilson 		dev_priv->hangcheck_count = 0;
1443893eead0SChris Wilson 		if (err)
1444893eead0SChris Wilson 			goto repeat;
1445893eead0SChris Wilson 		return;
1446893eead0SChris Wilson 	}
1447f65d9421SBen Gamari 
1448a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen < 4) {
1449f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
1450cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1451cbb465e7SChris Wilson 		instdone1 = 0;
1452cbb465e7SChris Wilson 	} else {
1453f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
1454cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1455cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1456cbb465e7SChris Wilson 	}
1457f65d9421SBen Gamari 
1458cbb465e7SChris Wilson 	if (dev_priv->last_acthd == acthd &&
1459cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1460cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1461cbb465e7SChris Wilson 		if (dev_priv->hangcheck_count++ > 1) {
1462f65d9421SBen Gamari 			DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
14638c80b59bSChris Wilson 
14648c80b59bSChris Wilson 			if (!IS_GEN2(dev)) {
14658c80b59bSChris Wilson 				/* Is the chip hanging on a WAIT_FOR_EVENT?
14668c80b59bSChris Wilson 				 * If so we can simply poke the RB_WAIT bit
14678c80b59bSChris Wilson 				 * and break the hang. This should work on
14688c80b59bSChris Wilson 				 * all but the second generation chipsets.
14698c80b59bSChris Wilson 				 */
14701ec14ad3SChris Wilson 
14711ec14ad3SChris Wilson 				if (kick_ring(&dev_priv->ring[RCS]))
1472893eead0SChris Wilson 					goto repeat;
14731ec14ad3SChris Wilson 
14741ec14ad3SChris Wilson 				if (HAS_BSD(dev) &&
14751ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[VCS]))
14761ec14ad3SChris Wilson 					goto repeat;
14771ec14ad3SChris Wilson 
14781ec14ad3SChris Wilson 				if (HAS_BLT(dev) &&
14791ec14ad3SChris Wilson 				    kick_ring(&dev_priv->ring[BCS]))
14801ec14ad3SChris Wilson 					goto repeat;
14818c80b59bSChris Wilson 			}
14828c80b59bSChris Wilson 
1483ba1234d1SBen Gamari 			i915_handle_error(dev, true);
1484f65d9421SBen Gamari 			return;
1485f65d9421SBen Gamari 		}
1486cbb465e7SChris Wilson 	} else {
1487cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1488cbb465e7SChris Wilson 
1489cbb465e7SChris Wilson 		dev_priv->last_acthd = acthd;
1490cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1491cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1492cbb465e7SChris Wilson 	}
1493f65d9421SBen Gamari 
1494893eead0SChris Wilson repeat:
1495f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1496b3b079dbSChris Wilson 	mod_timer(&dev_priv->hangcheck_timer,
1497b3b079dbSChris Wilson 		  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1498f65d9421SBen Gamari }
1499f65d9421SBen Gamari 
1500c0e09200SDave Airlie /* drm_dma.h hooks
1501c0e09200SDave Airlie */
1502f2b115e6SAdam Jackson static void ironlake_irq_preinstall(struct drm_device *dev)
1503036a4a7dSZhenyu Wang {
1504036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1505036a4a7dSZhenyu Wang 
1506036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1507036a4a7dSZhenyu Wang 
1508036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1509036a4a7dSZhenyu Wang 
1510036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1511036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
15123143a2bfSChris Wilson 	POSTING_READ(DEIER);
1513036a4a7dSZhenyu Wang 
1514036a4a7dSZhenyu Wang 	/* and GT */
1515036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1516036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
15173143a2bfSChris Wilson 	POSTING_READ(GTIER);
1518c650156aSZhenyu Wang 
1519c650156aSZhenyu Wang 	/* south display irq */
1520c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1521c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
15223143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1523036a4a7dSZhenyu Wang }
1524036a4a7dSZhenyu Wang 
1525f2b115e6SAdam Jackson static int ironlake_irq_postinstall(struct drm_device *dev)
1526036a4a7dSZhenyu Wang {
1527036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1528036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1529013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1530013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
15311ec14ad3SChris Wilson 	u32 render_irqs;
15322d7b8366SYuanhan Liu 	u32 hotplug_mask;
1533036a4a7dSZhenyu Wang 
15341ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
1535036a4a7dSZhenyu Wang 
1536036a4a7dSZhenyu Wang 	/* should always can generate irq */
1537036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
15381ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
15391ec14ad3SChris Wilson 	I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
15403143a2bfSChris Wilson 	POSTING_READ(DEIER);
1541036a4a7dSZhenyu Wang 
15421ec14ad3SChris Wilson 	dev_priv->gt_irq_mask = ~0;
1543036a4a7dSZhenyu Wang 
1544036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
15451ec14ad3SChris Wilson 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1546881f47b6SXiang, Haihao 	if (IS_GEN6(dev)) {
15471ec14ad3SChris Wilson 		I915_WRITE(GEN6_RENDER_IMR, ~GEN6_RENDER_USER_INTERRUPT);
15481ec14ad3SChris Wilson 		I915_WRITE(GEN6_BSD_IMR, ~GEN6_BSD_USER_INTERRUPT);
1549549f7365SChris Wilson 		I915_WRITE(GEN6_BLITTER_IMR, ~GEN6_BLITTER_USER_INTERRUPT);
1550881f47b6SXiang, Haihao 	}
1551881f47b6SXiang, Haihao 
15521ec14ad3SChris Wilson 	if (IS_GEN6(dev))
15531ec14ad3SChris Wilson 		render_irqs =
15541ec14ad3SChris Wilson 			GT_USER_INTERRUPT |
15551ec14ad3SChris Wilson 			GT_GEN6_BSD_USER_INTERRUPT |
15561ec14ad3SChris Wilson 			GT_BLT_USER_INTERRUPT;
15571ec14ad3SChris Wilson 	else
15581ec14ad3SChris Wilson 		render_irqs =
1559*88f23b8fSChris Wilson 			GT_USER_INTERRUPT |
15601ec14ad3SChris Wilson 			GT_BSD_USER_INTERRUPT;
15611ec14ad3SChris Wilson 	I915_WRITE(GTIER, render_irqs);
15623143a2bfSChris Wilson 	POSTING_READ(GTIER);
1563036a4a7dSZhenyu Wang 
15642d7b8366SYuanhan Liu 	if (HAS_PCH_CPT(dev)) {
15652d7b8366SYuanhan Liu 		hotplug_mask = SDE_CRT_HOTPLUG_CPT | SDE_PORTB_HOTPLUG_CPT  |
15662d7b8366SYuanhan Liu 			       SDE_PORTC_HOTPLUG_CPT | SDE_PORTD_HOTPLUG_CPT ;
15672d7b8366SYuanhan Liu 	} else {
15682d7b8366SYuanhan Liu 		hotplug_mask = SDE_CRT_HOTPLUG | SDE_PORTB_HOTPLUG |
15692d7b8366SYuanhan Liu 			       SDE_PORTC_HOTPLUG | SDE_PORTD_HOTPLUG;
15702d7b8366SYuanhan Liu 	}
15712d7b8366SYuanhan Liu 
15721ec14ad3SChris Wilson 	dev_priv->pch_irq_mask = ~hotplug_mask;
1573c650156aSZhenyu Wang 
1574c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
15751ec14ad3SChris Wilson 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
15761ec14ad3SChris Wilson 	I915_WRITE(SDEIER, hotplug_mask);
15773143a2bfSChris Wilson 	POSTING_READ(SDEIER);
1578c650156aSZhenyu Wang 
1579f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1580f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1581f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1582f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1583f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1584f97108d1SJesse Barnes 	}
1585f97108d1SJesse Barnes 
1586036a4a7dSZhenyu Wang 	return 0;
1587036a4a7dSZhenyu Wang }
1588036a4a7dSZhenyu Wang 
1589c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1590c0e09200SDave Airlie {
1591c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1592c0e09200SDave Airlie 
159379e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
159479e53945SJesse Barnes 
1595036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
15968a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1597036a4a7dSZhenyu Wang 
1598bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1599f2b115e6SAdam Jackson 		ironlake_irq_preinstall(dev);
1600036a4a7dSZhenyu Wang 		return;
1601036a4a7dSZhenyu Wang 	}
1602036a4a7dSZhenyu Wang 
16035ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
16045ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
16055ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
16065ca58282SJesse Barnes 	}
16075ca58282SJesse Barnes 
16080a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
16097c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
16107c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
16110a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1612ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
16133143a2bfSChris Wilson 	POSTING_READ(IER);
1614c0e09200SDave Airlie }
1615c0e09200SDave Airlie 
1616b01f2c3aSJesse Barnes /*
1617b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
1618b01f2c3aSJesse Barnes  * enabled correctly.
1619b01f2c3aSJesse Barnes  */
16200a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1621c0e09200SDave Airlie {
1622c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16235ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
162463eeaf38SJesse Barnes 	u32 error_mask;
16250a3e67a4SJesse Barnes 
16261ec14ad3SChris Wilson 	DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
1627d1b851fcSZou Nan hai 	if (HAS_BSD(dev))
16281ec14ad3SChris Wilson 		DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
1629549f7365SChris Wilson 	if (HAS_BLT(dev))
16301ec14ad3SChris Wilson 		DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
1631d1b851fcSZou Nan hai 
16320a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1633ed4cb414SEric Anholt 
1634bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1635f2b115e6SAdam Jackson 		return ironlake_irq_postinstall(dev);
1636036a4a7dSZhenyu Wang 
16377c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
16381ec14ad3SChris Wilson 	dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
16398ee1c3dbSMatthew Garrett 
16407c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
16417c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
16427c463586SKeith Packard 
16435ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
1644c496fa1fSAdam Jackson 		/* Enable in IER... */
1645c496fa1fSAdam Jackson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1646c496fa1fSAdam Jackson 		/* and unmask in IMR */
16471ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
1648c496fa1fSAdam Jackson 	}
1649c496fa1fSAdam Jackson 
1650c496fa1fSAdam Jackson 	/*
1651c496fa1fSAdam Jackson 	 * Enable some error detection, note the instruction error mask
1652c496fa1fSAdam Jackson 	 * bit is reserved, so we leave it masked.
1653c496fa1fSAdam Jackson 	 */
1654c496fa1fSAdam Jackson 	if (IS_G4X(dev)) {
1655c496fa1fSAdam Jackson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
1656c496fa1fSAdam Jackson 			       GM45_ERROR_MEM_PRIV |
1657c496fa1fSAdam Jackson 			       GM45_ERROR_CP_PRIV |
1658c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1659c496fa1fSAdam Jackson 	} else {
1660c496fa1fSAdam Jackson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
1661c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1662c496fa1fSAdam Jackson 	}
1663c496fa1fSAdam Jackson 	I915_WRITE(EMR, error_mask);
1664c496fa1fSAdam Jackson 
16651ec14ad3SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
1666c496fa1fSAdam Jackson 	I915_WRITE(IER, enable_mask);
16673143a2bfSChris Wilson 	POSTING_READ(IER);
1668c496fa1fSAdam Jackson 
1669c496fa1fSAdam Jackson 	if (I915_HAS_HOTPLUG(dev)) {
16705ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
16715ca58282SJesse Barnes 
1672b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
1673b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1674b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1675b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1676b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1677b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1678b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
1679b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1680b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1681b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1682b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
16832d1c9752SAndy Lutomirski 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
1684b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
16852d1c9752SAndy Lutomirski 
16862d1c9752SAndy Lutomirski 			/* Programming the CRT detection parameters tends
16872d1c9752SAndy Lutomirski 			   to generate a spurious hotplug event about three
16882d1c9752SAndy Lutomirski 			   seconds later.  So just do it once.
16892d1c9752SAndy Lutomirski 			*/
16902d1c9752SAndy Lutomirski 			if (IS_G4X(dev))
16912d1c9752SAndy Lutomirski 				hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
16922d1c9752SAndy Lutomirski 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
16932d1c9752SAndy Lutomirski 		}
16942d1c9752SAndy Lutomirski 
1695b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
1696b01f2c3aSJesse Barnes 
16975ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
16985ca58282SJesse Barnes 	}
16995ca58282SJesse Barnes 
17003b617967SChris Wilson 	intel_opregion_enable_asle(dev);
17010a3e67a4SJesse Barnes 
17020a3e67a4SJesse Barnes 	return 0;
1703c0e09200SDave Airlie }
1704c0e09200SDave Airlie 
1705f2b115e6SAdam Jackson static void ironlake_irq_uninstall(struct drm_device *dev)
1706036a4a7dSZhenyu Wang {
1707036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1708036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1709036a4a7dSZhenyu Wang 
1710036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1711036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1712036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1713036a4a7dSZhenyu Wang 
1714036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1715036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1716036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1717036a4a7dSZhenyu Wang }
1718036a4a7dSZhenyu Wang 
1719c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1720c0e09200SDave Airlie {
1721c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1722c0e09200SDave Airlie 
1723c0e09200SDave Airlie 	if (!dev_priv)
1724c0e09200SDave Airlie 		return;
1725c0e09200SDave Airlie 
17260a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
17270a3e67a4SJesse Barnes 
1728bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1729f2b115e6SAdam Jackson 		ironlake_irq_uninstall(dev);
1730036a4a7dSZhenyu Wang 		return;
1731036a4a7dSZhenyu Wang 	}
1732036a4a7dSZhenyu Wang 
17335ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
17345ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
17355ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
17365ca58282SJesse Barnes 	}
17375ca58282SJesse Barnes 
17380a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
17397c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
17407c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
17410a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1742ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1743c0e09200SDave Airlie 
17447c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
17457c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
17467c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1747c0e09200SDave Airlie }
1748