xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 4ef69c7a64b78d477d1666eba258ca049e8bac91)
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 {
70036a4a7dSZhenyu Wang 	if ((dev_priv->gt_irq_mask_reg & mask) != 0) {
71036a4a7dSZhenyu Wang 		dev_priv->gt_irq_mask_reg &= ~mask;
72036a4a7dSZhenyu Wang 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
73036a4a7dSZhenyu Wang 		(void) I915_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 {
80036a4a7dSZhenyu Wang 	if ((dev_priv->gt_irq_mask_reg & mask) != mask) {
81036a4a7dSZhenyu Wang 		dev_priv->gt_irq_mask_reg |= mask;
82036a4a7dSZhenyu Wang 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
83036a4a7dSZhenyu Wang 		(void) I915_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 {
91036a4a7dSZhenyu Wang 	if ((dev_priv->irq_mask_reg & mask) != 0) {
92036a4a7dSZhenyu Wang 		dev_priv->irq_mask_reg &= ~mask;
93036a4a7dSZhenyu Wang 		I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
94036a4a7dSZhenyu Wang 		(void) I915_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 {
101036a4a7dSZhenyu Wang 	if ((dev_priv->irq_mask_reg & mask) != mask) {
102036a4a7dSZhenyu Wang 		dev_priv->irq_mask_reg |= mask;
103036a4a7dSZhenyu Wang 		I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
104036a4a7dSZhenyu Wang 		(void) I915_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 {
111ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != 0) {
112ed4cb414SEric Anholt 		dev_priv->irq_mask_reg &= ~mask;
113ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
114ed4cb414SEric Anholt 		(void) I915_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 {
121ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != mask) {
122ed4cb414SEric Anholt 		dev_priv->irq_mask_reg |= mask;
123ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
124ed4cb414SEric Anholt 		(void) I915_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));
1477c463586SKeith Packard 		(void) I915_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]);
1597c463586SKeith Packard 		(void) I915_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 {
16801c66889SZhao Yakui 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16901c66889SZhao Yakui 
170c619eed4SEric Anholt 	if (HAS_PCH_SPLIT(dev))
171f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
172edcb49caSZhao Yakui 	else {
17301c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
174d874bcffSJesse Barnes 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
175edcb49caSZhao Yakui 		if (IS_I965G(dev))
176edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
177d874bcffSJesse Barnes 					     PIPE_LEGACY_BLC_EVENT_ENABLE);
178edcb49caSZhao Yakui 	}
17901c66889SZhao Yakui }
18001c66889SZhao Yakui 
18101c66889SZhao Yakui /**
1820a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1830a3e67a4SJesse Barnes  * @dev: DRM device
1840a3e67a4SJesse Barnes  * @pipe: pipe to check
1850a3e67a4SJesse Barnes  *
1860a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1870a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1880a3e67a4SJesse Barnes  * before reading such registers if unsure.
1890a3e67a4SJesse Barnes  */
1900a3e67a4SJesse Barnes static int
1910a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1920a3e67a4SJesse Barnes {
1930a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1940a3e67a4SJesse Barnes 	unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
1950a3e67a4SJesse Barnes 
1960a3e67a4SJesse Barnes 	if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
1970a3e67a4SJesse Barnes 		return 1;
1980a3e67a4SJesse Barnes 
1990a3e67a4SJesse Barnes 	return 0;
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;
2100a3e67a4SJesse Barnes 	u32 high1, high2, low, count;
2110a3e67a4SJesse Barnes 
2120a3e67a4SJesse Barnes 	high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
2130a3e67a4SJesse Barnes 	low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
2140a3e67a4SJesse Barnes 
2150a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
21644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
21744d98a61SZhao Yakui 				"pipe %d\n", pipe);
2180a3e67a4SJesse Barnes 		return 0;
2190a3e67a4SJesse Barnes 	}
2200a3e67a4SJesse Barnes 
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 {
2270a3e67a4SJesse Barnes 		high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
2280a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
2290a3e67a4SJesse Barnes 		low =  ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
2300a3e67a4SJesse Barnes 			PIPE_FRAME_LOW_SHIFT);
2310a3e67a4SJesse Barnes 		high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
2320a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
2330a3e67a4SJesse Barnes 	} while (high1 != high2);
2340a3e67a4SJesse Barnes 
2350a3e67a4SJesse Barnes 	count = (high1 << 8) | low;
2360a3e67a4SJesse Barnes 
2370a3e67a4SJesse Barnes 	return count;
2380a3e67a4SJesse Barnes }
2390a3e67a4SJesse Barnes 
2409880b7a5SJesse Barnes u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
2419880b7a5SJesse Barnes {
2429880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2439880b7a5SJesse Barnes 	int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
2449880b7a5SJesse Barnes 
2459880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
24644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
24744d98a61SZhao Yakui 					"pipe %d\n", pipe);
2489880b7a5SJesse Barnes 		return 0;
2499880b7a5SJesse Barnes 	}
2509880b7a5SJesse Barnes 
2519880b7a5SJesse Barnes 	return I915_READ(reg);
2529880b7a5SJesse Barnes }
2539880b7a5SJesse Barnes 
2545ca58282SJesse Barnes /*
2555ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
2565ca58282SJesse Barnes  */
2575ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
2585ca58282SJesse Barnes {
2595ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
2605ca58282SJesse Barnes 						    hotplug_work);
2615ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
262c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
263*4ef69c7aSChris Wilson 	struct intel_encoder *encoder;
2645ca58282SJesse Barnes 
265*4ef69c7aSChris Wilson 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
266*4ef69c7aSChris Wilson 		if (encoder->hot_plug)
267*4ef69c7aSChris Wilson 			encoder->hot_plug(encoder);
268c31c4ba3SKeith Packard 
2695ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
270eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
2715ca58282SJesse Barnes }
2725ca58282SJesse Barnes 
273f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
274f97108d1SJesse Barnes {
275f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
276b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
277f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
278f97108d1SJesse Barnes 
2797648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
280b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
281b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
282f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
283f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
284f97108d1SJesse Barnes 
285f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
286b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
287f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
288f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
289f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
290f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
291b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
292f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
293f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
294f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
295f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
296f97108d1SJesse Barnes 	}
297f97108d1SJesse Barnes 
2987648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
299f97108d1SJesse Barnes 		dev_priv->cur_delay = new_delay;
300f97108d1SJesse Barnes 
301f97108d1SJesse Barnes 	return;
302f97108d1SJesse Barnes }
303f97108d1SJesse Barnes 
304995b6762SChris Wilson static irqreturn_t ironlake_irq_handler(struct drm_device *dev)
305036a4a7dSZhenyu Wang {
306036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
307036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
3083ff99164SDave Airlie 	u32 de_iir, gt_iir, de_ier, pch_iir;
309036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
310852835f3SZou Nan hai 	struct intel_ring_buffer *render_ring = &dev_priv->render_ring;
311036a4a7dSZhenyu Wang 
3122d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
3132d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
3142d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
3152d109a84SZou, Nanhai 	(void)I915_READ(DEIER);
3162d109a84SZou, Nanhai 
317036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
318036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
319c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
320036a4a7dSZhenyu Wang 
321c650156aSZhenyu Wang 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0)
322c7c85101SZou Nan hai 		goto done;
323036a4a7dSZhenyu Wang 
324036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
325036a4a7dSZhenyu Wang 
326036a4a7dSZhenyu Wang 	if (dev->primary->master) {
327036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
328036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
329036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
330036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
331036a4a7dSZhenyu Wang 	}
332036a4a7dSZhenyu Wang 
333e552eb70SJesse Barnes 	if (gt_iir & GT_PIPE_NOTIFY) {
334852835f3SZou Nan hai 		u32 seqno = render_ring->get_gem_seqno(dev, render_ring);
335852835f3SZou Nan hai 		render_ring->irq_gem_seqno = seqno;
3361c5d22f7SChris Wilson 		trace_i915_gem_request_complete(dev, seqno);
337852835f3SZou Nan hai 		DRM_WAKEUP(&dev_priv->render_ring.irq_queue);
338c566ec49SZhenyu Wang 		dev_priv->hangcheck_count = 0;
339c566ec49SZhenyu Wang 		mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
340036a4a7dSZhenyu Wang 	}
341d1b851fcSZou Nan hai 	if (gt_iir & GT_BSD_USER_INTERRUPT)
342d1b851fcSZou Nan hai 		DRM_WAKEUP(&dev_priv->bsd_ring.irq_queue);
343d1b851fcSZou Nan hai 
344036a4a7dSZhenyu Wang 
34501c66889SZhao Yakui 	if (de_iir & DE_GSE)
3463b617967SChris Wilson 		intel_opregion_gse_intr(dev);
34701c66889SZhao Yakui 
348f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
349013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
3502bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 0);
351013d5aa2SJesse Barnes 	}
352013d5aa2SJesse Barnes 
353f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
354f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
3552bbda389SChris Wilson 		intel_finish_page_flip_plane(dev, 1);
356013d5aa2SJesse Barnes 	}
357c062df61SLi Peng 
358f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
359f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
360f072d2e7SZhenyu Wang 
361f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
362f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
363f072d2e7SZhenyu Wang 
364c650156aSZhenyu Wang 	/* check event from PCH */
365c650156aSZhenyu Wang 	if ((de_iir & DE_PCH_EVENT) &&
366c650156aSZhenyu Wang 	    (pch_iir & SDE_HOTPLUG_MASK)) {
367c650156aSZhenyu Wang 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
368c650156aSZhenyu Wang 	}
369c650156aSZhenyu Wang 
370f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
3717648fa99SJesse Barnes 		I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
372f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
373f97108d1SJesse Barnes 	}
374f97108d1SJesse Barnes 
375c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
376c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
377c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
378c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
379036a4a7dSZhenyu Wang 
380c7c85101SZou Nan hai done:
3812d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
3822d109a84SZou, Nanhai 	(void)I915_READ(DEIER);
3832d109a84SZou, Nanhai 
384036a4a7dSZhenyu Wang 	return ret;
385036a4a7dSZhenyu Wang }
386036a4a7dSZhenyu Wang 
3878a905236SJesse Barnes /**
3888a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
3898a905236SJesse Barnes  * @work: work struct
3908a905236SJesse Barnes  *
3918a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
3928a905236SJesse Barnes  * was detected.
3938a905236SJesse Barnes  */
3948a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
3958a905236SJesse Barnes {
3968a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3978a905236SJesse Barnes 						    error_work);
3988a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
399f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
400f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
401f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
4028a905236SJesse Barnes 
40344d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("generating error event\n");
404f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
4058a905236SJesse Barnes 
406ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
407f316a42cSBen Gamari 		if (IS_I965G(dev)) {
40844d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("resetting chip\n");
409f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
410f316a42cSBen Gamari 			if (!i965_reset(dev, GDRST_RENDER)) {
411ba1234d1SBen Gamari 				atomic_set(&dev_priv->mm.wedged, 0);
412f316a42cSBen Gamari 				kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
413f316a42cSBen Gamari 			}
414f316a42cSBen Gamari 		} else {
41544d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("reboot required\n");
416f316a42cSBen Gamari 		}
417f316a42cSBen Gamari 	}
4188a905236SJesse Barnes }
4198a905236SJesse Barnes 
4203bd3c932SChris Wilson #ifdef CONFIG_DEBUG_FS
4219df30794SChris Wilson static struct drm_i915_error_object *
4229df30794SChris Wilson i915_error_object_create(struct drm_device *dev,
4239df30794SChris Wilson 			 struct drm_gem_object *src)
4249df30794SChris Wilson {
425e56660ddSChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
4269df30794SChris Wilson 	struct drm_i915_error_object *dst;
4279df30794SChris Wilson 	struct drm_i915_gem_object *src_priv;
4289df30794SChris Wilson 	int page, page_count;
429e56660ddSChris Wilson 	u32 reloc_offset;
4309df30794SChris Wilson 
4319df30794SChris Wilson 	if (src == NULL)
4329df30794SChris Wilson 		return NULL;
4339df30794SChris Wilson 
43423010e43SDaniel Vetter 	src_priv = to_intel_bo(src);
4359df30794SChris Wilson 	if (src_priv->pages == NULL)
4369df30794SChris Wilson 		return NULL;
4379df30794SChris Wilson 
4389df30794SChris Wilson 	page_count = src->size / PAGE_SIZE;
4399df30794SChris Wilson 
4409df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof (u32 *), GFP_ATOMIC);
4419df30794SChris Wilson 	if (dst == NULL)
4429df30794SChris Wilson 		return NULL;
4439df30794SChris Wilson 
444e56660ddSChris Wilson 	reloc_offset = src_priv->gtt_offset;
4459df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
446788885aeSAndrew Morton 		unsigned long flags;
447e56660ddSChris Wilson 		void __iomem *s;
448e56660ddSChris Wilson 		void *d;
449788885aeSAndrew Morton 
450e56660ddSChris Wilson 		d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
4519df30794SChris Wilson 		if (d == NULL)
4529df30794SChris Wilson 			goto unwind;
453e56660ddSChris Wilson 
454788885aeSAndrew Morton 		local_irq_save(flags);
455e56660ddSChris Wilson 		s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
456e56660ddSChris Wilson 					     reloc_offset,
457e56660ddSChris Wilson 					     KM_IRQ0);
458e56660ddSChris Wilson 		memcpy_fromio(d, s, PAGE_SIZE);
459e56660ddSChris Wilson 		io_mapping_unmap_atomic(s, KM_IRQ0);
460788885aeSAndrew Morton 		local_irq_restore(flags);
461e56660ddSChris Wilson 
4629df30794SChris Wilson 		dst->pages[page] = d;
463e56660ddSChris Wilson 
464e56660ddSChris Wilson 		reloc_offset += PAGE_SIZE;
4659df30794SChris Wilson 	}
4669df30794SChris Wilson 	dst->page_count = page_count;
4679df30794SChris Wilson 	dst->gtt_offset = src_priv->gtt_offset;
4689df30794SChris Wilson 
4699df30794SChris Wilson 	return dst;
4709df30794SChris Wilson 
4719df30794SChris Wilson unwind:
4729df30794SChris Wilson 	while (page--)
4739df30794SChris Wilson 		kfree(dst->pages[page]);
4749df30794SChris Wilson 	kfree(dst);
4759df30794SChris Wilson 	return NULL;
4769df30794SChris Wilson }
4779df30794SChris Wilson 
4789df30794SChris Wilson static void
4799df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
4809df30794SChris Wilson {
4819df30794SChris Wilson 	int page;
4829df30794SChris Wilson 
4839df30794SChris Wilson 	if (obj == NULL)
4849df30794SChris Wilson 		return;
4859df30794SChris Wilson 
4869df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
4879df30794SChris Wilson 		kfree(obj->pages[page]);
4889df30794SChris Wilson 
4899df30794SChris Wilson 	kfree(obj);
4909df30794SChris Wilson }
4919df30794SChris Wilson 
4929df30794SChris Wilson static void
4939df30794SChris Wilson i915_error_state_free(struct drm_device *dev,
4949df30794SChris Wilson 		      struct drm_i915_error_state *error)
4959df30794SChris Wilson {
4969df30794SChris Wilson 	i915_error_object_free(error->batchbuffer[0]);
4979df30794SChris Wilson 	i915_error_object_free(error->batchbuffer[1]);
4989df30794SChris Wilson 	i915_error_object_free(error->ringbuffer);
4999df30794SChris Wilson 	kfree(error->active_bo);
5006ef3d427SChris Wilson 	kfree(error->overlay);
5019df30794SChris Wilson 	kfree(error);
5029df30794SChris Wilson }
5039df30794SChris Wilson 
5049df30794SChris Wilson static u32
5059df30794SChris Wilson i915_get_bbaddr(struct drm_device *dev, u32 *ring)
5069df30794SChris Wilson {
5079df30794SChris Wilson 	u32 cmd;
5089df30794SChris Wilson 
5099df30794SChris Wilson 	if (IS_I830(dev) || IS_845G(dev))
5109df30794SChris Wilson 		cmd = MI_BATCH_BUFFER;
5119df30794SChris Wilson 	else if (IS_I965G(dev))
5129df30794SChris Wilson 		cmd = (MI_BATCH_BUFFER_START | (2 << 6) |
5139df30794SChris Wilson 		       MI_BATCH_NON_SECURE_I965);
5149df30794SChris Wilson 	else
5159df30794SChris Wilson 		cmd = (MI_BATCH_BUFFER_START | (2 << 6));
5169df30794SChris Wilson 
5179df30794SChris Wilson 	return ring[0] == cmd ? ring[1] : 0;
5189df30794SChris Wilson }
5199df30794SChris Wilson 
5209df30794SChris Wilson static u32
5219df30794SChris Wilson i915_ringbuffer_last_batch(struct drm_device *dev)
5229df30794SChris Wilson {
5239df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
5249df30794SChris Wilson 	u32 head, bbaddr;
5259df30794SChris Wilson 	u32 *ring;
5269df30794SChris Wilson 
5279df30794SChris Wilson 	/* Locate the current position in the ringbuffer and walk back
5289df30794SChris Wilson 	 * to find the most recently dispatched batch buffer.
5299df30794SChris Wilson 	 */
5309df30794SChris Wilson 	bbaddr = 0;
5319df30794SChris Wilson 	head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
532d3301d86SEric Anholt 	ring = (u32 *)(dev_priv->render_ring.virtual_start + head);
5339df30794SChris Wilson 
534d3301d86SEric Anholt 	while (--ring >= (u32 *)dev_priv->render_ring.virtual_start) {
5359df30794SChris Wilson 		bbaddr = i915_get_bbaddr(dev, ring);
5369df30794SChris Wilson 		if (bbaddr)
5379df30794SChris Wilson 			break;
5389df30794SChris Wilson 	}
5399df30794SChris Wilson 
5409df30794SChris Wilson 	if (bbaddr == 0) {
5418187a2b7SZou Nan hai 		ring = (u32 *)(dev_priv->render_ring.virtual_start
5428187a2b7SZou Nan hai 				+ dev_priv->render_ring.size);
543d3301d86SEric Anholt 		while (--ring >= (u32 *)dev_priv->render_ring.virtual_start) {
5449df30794SChris Wilson 			bbaddr = i915_get_bbaddr(dev, ring);
5459df30794SChris Wilson 			if (bbaddr)
5469df30794SChris Wilson 				break;
5479df30794SChris Wilson 		}
5489df30794SChris Wilson 	}
5499df30794SChris Wilson 
5509df30794SChris Wilson 	return bbaddr;
5519df30794SChris Wilson }
5529df30794SChris Wilson 
5538a905236SJesse Barnes /**
5548a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
5558a905236SJesse Barnes  * @dev: drm device
5568a905236SJesse Barnes  *
5578a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
5588a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
5598a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
5608a905236SJesse Barnes  * to pick up.
5618a905236SJesse Barnes  */
56263eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
56363eeaf38SJesse Barnes {
56463eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
5659df30794SChris Wilson 	struct drm_i915_gem_object *obj_priv;
56663eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
5679df30794SChris Wilson 	struct drm_gem_object *batchbuffer[2];
56863eeaf38SJesse Barnes 	unsigned long flags;
5699df30794SChris Wilson 	u32 bbaddr;
5709df30794SChris Wilson 	int count;
57163eeaf38SJesse Barnes 
57263eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
5739df30794SChris Wilson 	error = dev_priv->first_error;
5749df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
5759df30794SChris Wilson 	if (error)
5769df30794SChris Wilson 		return;
57763eeaf38SJesse Barnes 
57863eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
57963eeaf38SJesse Barnes 	if (!error) {
5809df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
5819df30794SChris Wilson 		return;
58263eeaf38SJesse Barnes 	}
58363eeaf38SJesse Barnes 
584852835f3SZou Nan hai 	error->seqno = i915_get_gem_seqno(dev, &dev_priv->render_ring);
58563eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
58663eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
58763eeaf38SJesse Barnes 	error->pipeastat = I915_READ(PIPEASTAT);
58863eeaf38SJesse Barnes 	error->pipebstat = I915_READ(PIPEBSTAT);
58963eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
59063eeaf38SJesse Barnes 	if (!IS_I965G(dev)) {
59163eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR);
59263eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR);
59363eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE);
59463eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD);
5959df30794SChris Wilson 		error->bbaddr = 0;
59663eeaf38SJesse Barnes 	} else {
59763eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
59863eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
59963eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
60063eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
60163eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
60263eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
6039df30794SChris Wilson 		error->bbaddr = I915_READ64(BB_ADDR);
6049df30794SChris Wilson 	}
6059df30794SChris Wilson 
6069df30794SChris Wilson 	bbaddr = i915_ringbuffer_last_batch(dev);
6079df30794SChris Wilson 
6089df30794SChris Wilson 	/* Grab the current batchbuffer, most likely to have crashed. */
6099df30794SChris Wilson 	batchbuffer[0] = NULL;
6109df30794SChris Wilson 	batchbuffer[1] = NULL;
6119df30794SChris Wilson 	count = 0;
612852835f3SZou Nan hai 	list_for_each_entry(obj_priv,
613852835f3SZou Nan hai 			&dev_priv->render_ring.active_list, list) {
614852835f3SZou Nan hai 
615a8089e84SDaniel Vetter 		struct drm_gem_object *obj = &obj_priv->base;
6169df30794SChris Wilson 
6179df30794SChris Wilson 		if (batchbuffer[0] == NULL &&
6189df30794SChris Wilson 		    bbaddr >= obj_priv->gtt_offset &&
6199df30794SChris Wilson 		    bbaddr < obj_priv->gtt_offset + obj->size)
6209df30794SChris Wilson 			batchbuffer[0] = obj;
6219df30794SChris Wilson 
6229df30794SChris Wilson 		if (batchbuffer[1] == NULL &&
6239df30794SChris Wilson 		    error->acthd >= obj_priv->gtt_offset &&
624e56660ddSChris Wilson 		    error->acthd < obj_priv->gtt_offset + obj->size)
6259df30794SChris Wilson 			batchbuffer[1] = obj;
6269df30794SChris Wilson 
6279df30794SChris Wilson 		count++;
6289df30794SChris Wilson 	}
629e56660ddSChris Wilson 	/* Scan the other lists for completeness for those bizarre errors. */
630e56660ddSChris Wilson 	if (batchbuffer[0] == NULL || batchbuffer[1] == NULL) {
631e56660ddSChris Wilson 		list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
632e56660ddSChris Wilson 			struct drm_gem_object *obj = &obj_priv->base;
633e56660ddSChris Wilson 
634e56660ddSChris Wilson 			if (batchbuffer[0] == NULL &&
635e56660ddSChris Wilson 			    bbaddr >= obj_priv->gtt_offset &&
636e56660ddSChris Wilson 			    bbaddr < obj_priv->gtt_offset + obj->size)
637e56660ddSChris Wilson 				batchbuffer[0] = obj;
638e56660ddSChris Wilson 
639e56660ddSChris Wilson 			if (batchbuffer[1] == NULL &&
640e56660ddSChris Wilson 			    error->acthd >= obj_priv->gtt_offset &&
641e56660ddSChris Wilson 			    error->acthd < obj_priv->gtt_offset + obj->size)
642e56660ddSChris Wilson 				batchbuffer[1] = obj;
643e56660ddSChris Wilson 
644e56660ddSChris Wilson 			if (batchbuffer[0] && batchbuffer[1])
645e56660ddSChris Wilson 				break;
646e56660ddSChris Wilson 		}
647e56660ddSChris Wilson 	}
648e56660ddSChris Wilson 	if (batchbuffer[0] == NULL || batchbuffer[1] == NULL) {
649e56660ddSChris Wilson 		list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
650e56660ddSChris Wilson 			struct drm_gem_object *obj = &obj_priv->base;
651e56660ddSChris Wilson 
652e56660ddSChris Wilson 			if (batchbuffer[0] == NULL &&
653e56660ddSChris Wilson 			    bbaddr >= obj_priv->gtt_offset &&
654e56660ddSChris Wilson 			    bbaddr < obj_priv->gtt_offset + obj->size)
655e56660ddSChris Wilson 				batchbuffer[0] = obj;
656e56660ddSChris Wilson 
657e56660ddSChris Wilson 			if (batchbuffer[1] == NULL &&
658e56660ddSChris Wilson 			    error->acthd >= obj_priv->gtt_offset &&
659e56660ddSChris Wilson 			    error->acthd < obj_priv->gtt_offset + obj->size)
660e56660ddSChris Wilson 				batchbuffer[1] = obj;
661e56660ddSChris Wilson 
662e56660ddSChris Wilson 			if (batchbuffer[0] && batchbuffer[1])
663e56660ddSChris Wilson 				break;
664e56660ddSChris Wilson 		}
665e56660ddSChris Wilson 	}
6669df30794SChris Wilson 
6679df30794SChris Wilson 	/* We need to copy these to an anonymous buffer as the simplest
6689df30794SChris Wilson 	 * method to avoid being overwritten by userpace.
6699df30794SChris Wilson 	 */
6709df30794SChris Wilson 	error->batchbuffer[0] = i915_error_object_create(dev, batchbuffer[0]);
671e56660ddSChris Wilson 	if (batchbuffer[1] != batchbuffer[0])
6729df30794SChris Wilson 		error->batchbuffer[1] = i915_error_object_create(dev, batchbuffer[1]);
673e56660ddSChris Wilson 	else
674e56660ddSChris Wilson 		error->batchbuffer[1] = NULL;
6759df30794SChris Wilson 
6769df30794SChris Wilson 	/* Record the ringbuffer */
6778187a2b7SZou Nan hai 	error->ringbuffer = i915_error_object_create(dev,
6788187a2b7SZou Nan hai 			dev_priv->render_ring.gem_object);
6799df30794SChris Wilson 
6809df30794SChris Wilson 	/* Record buffers on the active list. */
6819df30794SChris Wilson 	error->active_bo = NULL;
6829df30794SChris Wilson 	error->active_bo_count = 0;
6839df30794SChris Wilson 
6849df30794SChris Wilson 	if (count)
6859df30794SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*count,
6869df30794SChris Wilson 					   GFP_ATOMIC);
6879df30794SChris Wilson 
6889df30794SChris Wilson 	if (error->active_bo) {
6899df30794SChris Wilson 		int i = 0;
690852835f3SZou Nan hai 		list_for_each_entry(obj_priv,
691852835f3SZou Nan hai 				&dev_priv->render_ring.active_list, list) {
692a8089e84SDaniel Vetter 			struct drm_gem_object *obj = &obj_priv->base;
6939df30794SChris Wilson 
6949df30794SChris Wilson 			error->active_bo[i].size = obj->size;
6959df30794SChris Wilson 			error->active_bo[i].name = obj->name;
6969df30794SChris Wilson 			error->active_bo[i].seqno = obj_priv->last_rendering_seqno;
6979df30794SChris Wilson 			error->active_bo[i].gtt_offset = obj_priv->gtt_offset;
6989df30794SChris Wilson 			error->active_bo[i].read_domains = obj->read_domains;
6999df30794SChris Wilson 			error->active_bo[i].write_domain = obj->write_domain;
7009df30794SChris Wilson 			error->active_bo[i].fence_reg = obj_priv->fence_reg;
7019df30794SChris Wilson 			error->active_bo[i].pinned = 0;
7029df30794SChris Wilson 			if (obj_priv->pin_count > 0)
7039df30794SChris Wilson 				error->active_bo[i].pinned = 1;
7049df30794SChris Wilson 			if (obj_priv->user_pin_count > 0)
7059df30794SChris Wilson 				error->active_bo[i].pinned = -1;
7069df30794SChris Wilson 			error->active_bo[i].tiling = obj_priv->tiling_mode;
7079df30794SChris Wilson 			error->active_bo[i].dirty = obj_priv->dirty;
7089df30794SChris Wilson 			error->active_bo[i].purgeable = obj_priv->madv != I915_MADV_WILLNEED;
7099df30794SChris Wilson 
7109df30794SChris Wilson 			if (++i == count)
7119df30794SChris Wilson 				break;
7129df30794SChris Wilson 		}
7139df30794SChris Wilson 		error->active_bo_count = i;
71463eeaf38SJesse Barnes 	}
71563eeaf38SJesse Barnes 
7168a905236SJesse Barnes 	do_gettimeofday(&error->time);
7178a905236SJesse Barnes 
7186ef3d427SChris Wilson 	error->overlay = intel_overlay_capture_error_state(dev);
7196ef3d427SChris Wilson 
7209df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
7219df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
72263eeaf38SJesse Barnes 		dev_priv->first_error = error;
7239df30794SChris Wilson 		error = NULL;
7249df30794SChris Wilson 	}
72563eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
7269df30794SChris Wilson 
7279df30794SChris Wilson 	if (error)
7289df30794SChris Wilson 		i915_error_state_free(dev, error);
7299df30794SChris Wilson }
7309df30794SChris Wilson 
7319df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
7329df30794SChris Wilson {
7339df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
7349df30794SChris Wilson 	struct drm_i915_error_state *error;
7359df30794SChris Wilson 
7369df30794SChris Wilson 	spin_lock(&dev_priv->error_lock);
7379df30794SChris Wilson 	error = dev_priv->first_error;
7389df30794SChris Wilson 	dev_priv->first_error = NULL;
7399df30794SChris Wilson 	spin_unlock(&dev_priv->error_lock);
7409df30794SChris Wilson 
7419df30794SChris Wilson 	if (error)
7429df30794SChris Wilson 		i915_error_state_free(dev, error);
74363eeaf38SJesse Barnes }
7443bd3c932SChris Wilson #else
7453bd3c932SChris Wilson #define i915_capture_error_state(x)
7463bd3c932SChris Wilson #endif
74763eeaf38SJesse Barnes 
74835aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
749c0e09200SDave Airlie {
7508a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
75163eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
75263eeaf38SJesse Barnes 
75335aed2e6SChris Wilson 	if (!eir)
75435aed2e6SChris Wilson 		return;
75563eeaf38SJesse Barnes 
75663eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
75763eeaf38SJesse Barnes 	       eir);
7588a905236SJesse Barnes 
7598a905236SJesse Barnes 	if (IS_G4X(dev)) {
7608a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
7618a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
7628a905236SJesse Barnes 
7638a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
7648a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
7658a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
7668a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
7678a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
7688a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
7698a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
7708a905236SJesse Barnes 			       I915_READ(INSTPS));
7718a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
7728a905236SJesse Barnes 			       I915_READ(INSTDONE1));
7738a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
7748a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
7758a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
7768a905236SJesse Barnes 			(void)I915_READ(IPEIR_I965);
7778a905236SJesse Barnes 		}
7788a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
7798a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
7808a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
7818a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
7828a905236SJesse Barnes 			       pgtbl_err);
7838a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
7848a905236SJesse Barnes 			(void)I915_READ(PGTBL_ER);
7858a905236SJesse Barnes 		}
7868a905236SJesse Barnes 	}
7878a905236SJesse Barnes 
7888a905236SJesse Barnes 	if (IS_I9XX(dev)) {
78963eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
79063eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
79163eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
79263eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
79363eeaf38SJesse Barnes 			       pgtbl_err);
79463eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
79563eeaf38SJesse Barnes 			(void)I915_READ(PGTBL_ER);
79663eeaf38SJesse Barnes 		}
7978a905236SJesse Barnes 	}
7988a905236SJesse Barnes 
79963eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
80035aed2e6SChris Wilson 		u32 pipea_stats = I915_READ(PIPEASTAT);
80135aed2e6SChris Wilson 		u32 pipeb_stats = I915_READ(PIPEBSTAT);
80235aed2e6SChris Wilson 
80363eeaf38SJesse Barnes 		printk(KERN_ERR "memory refresh error\n");
80463eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
80563eeaf38SJesse Barnes 		       pipea_stats);
80663eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
80763eeaf38SJesse Barnes 		       pipeb_stats);
80863eeaf38SJesse Barnes 		/* pipestat has already been acked */
80963eeaf38SJesse Barnes 	}
81063eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
81163eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
81263eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
81363eeaf38SJesse Barnes 		       I915_READ(INSTPM));
81463eeaf38SJesse Barnes 		if (!IS_I965G(dev)) {
81563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
81663eeaf38SJesse Barnes 
81763eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
81863eeaf38SJesse Barnes 			       I915_READ(IPEIR));
81963eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
82063eeaf38SJesse Barnes 			       I915_READ(IPEHR));
82163eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
82263eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
82363eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
82463eeaf38SJesse Barnes 			       I915_READ(ACTHD));
82563eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
82663eeaf38SJesse Barnes 			(void)I915_READ(IPEIR);
82763eeaf38SJesse Barnes 		} else {
82863eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
82963eeaf38SJesse Barnes 
83063eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
83163eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
83263eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
83363eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
83463eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
83563eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
83663eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
83763eeaf38SJesse Barnes 			       I915_READ(INSTPS));
83863eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
83963eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
84063eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
84163eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
84263eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
84363eeaf38SJesse Barnes 			(void)I915_READ(IPEIR_I965);
84463eeaf38SJesse Barnes 		}
84563eeaf38SJesse Barnes 	}
84663eeaf38SJesse Barnes 
84763eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
84863eeaf38SJesse Barnes 	(void)I915_READ(EIR);
84963eeaf38SJesse Barnes 	eir = I915_READ(EIR);
85063eeaf38SJesse Barnes 	if (eir) {
85163eeaf38SJesse Barnes 		/*
85263eeaf38SJesse Barnes 		 * some errors might have become stuck,
85363eeaf38SJesse Barnes 		 * mask them.
85463eeaf38SJesse Barnes 		 */
85563eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
85663eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
85763eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
85863eeaf38SJesse Barnes 	}
85935aed2e6SChris Wilson }
86035aed2e6SChris Wilson 
86135aed2e6SChris Wilson /**
86235aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
86335aed2e6SChris Wilson  * @dev: drm device
86435aed2e6SChris Wilson  *
86535aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
86635aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
86735aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
86835aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
86935aed2e6SChris Wilson  * of a ring dump etc.).
87035aed2e6SChris Wilson  */
87135aed2e6SChris Wilson static void i915_handle_error(struct drm_device *dev, bool wedged)
87235aed2e6SChris Wilson {
87335aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
87435aed2e6SChris Wilson 
87535aed2e6SChris Wilson 	i915_capture_error_state(dev);
87635aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
8778a905236SJesse Barnes 
878ba1234d1SBen Gamari 	if (wedged) {
879ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
880ba1234d1SBen Gamari 
88111ed50ecSBen Gamari 		/*
88211ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
88311ed50ecSBen Gamari 		 */
884852835f3SZou Nan hai 		DRM_WAKEUP(&dev_priv->render_ring.irq_queue);
88511ed50ecSBen Gamari 	}
88611ed50ecSBen Gamari 
8879c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
8888a905236SJesse Barnes }
8898a905236SJesse Barnes 
8904e5359cdSSimon Farnsworth static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
8914e5359cdSSimon Farnsworth {
8924e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
8934e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8944e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8954e5359cdSSimon Farnsworth 	struct drm_i915_gem_object *obj_priv;
8964e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
8974e5359cdSSimon Farnsworth 	unsigned long flags;
8984e5359cdSSimon Farnsworth 	bool stall_detected;
8994e5359cdSSimon Farnsworth 
9004e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
9014e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
9024e5359cdSSimon Farnsworth 		return;
9034e5359cdSSimon Farnsworth 
9044e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
9054e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
9064e5359cdSSimon Farnsworth 
9074e5359cdSSimon Farnsworth 	if (work == NULL || work->pending || !work->enable_stall_check) {
9084e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
9094e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
9104e5359cdSSimon Farnsworth 		return;
9114e5359cdSSimon Farnsworth 	}
9124e5359cdSSimon Farnsworth 
9134e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
9144e5359cdSSimon Farnsworth 	obj_priv = to_intel_bo(work->pending_flip_obj);
9154e5359cdSSimon Farnsworth 	if(IS_I965G(dev)) {
9164e5359cdSSimon Farnsworth 		int dspsurf = intel_crtc->plane == 0 ? DSPASURF : DSPBSURF;
9174e5359cdSSimon Farnsworth 		stall_detected = I915_READ(dspsurf) == obj_priv->gtt_offset;
9184e5359cdSSimon Farnsworth 	} else {
9194e5359cdSSimon Farnsworth 		int dspaddr = intel_crtc->plane == 0 ? DSPAADDR : DSPBADDR;
9204e5359cdSSimon Farnsworth 		stall_detected = I915_READ(dspaddr) == (obj_priv->gtt_offset +
9214e5359cdSSimon Farnsworth 							crtc->y * crtc->fb->pitch +
9224e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
9234e5359cdSSimon Farnsworth 	}
9244e5359cdSSimon Farnsworth 
9254e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
9264e5359cdSSimon Farnsworth 
9274e5359cdSSimon Farnsworth 	if (stall_detected) {
9284e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
9294e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
9304e5359cdSSimon Farnsworth 	}
9314e5359cdSSimon Farnsworth }
9324e5359cdSSimon Farnsworth 
9338a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
9348a905236SJesse Barnes {
9358a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
9368a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
9378a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
9388a905236SJesse Barnes 	u32 iir, new_iir;
9398a905236SJesse Barnes 	u32 pipea_stats, pipeb_stats;
9408a905236SJesse Barnes 	u32 vblank_status;
9418a905236SJesse Barnes 	int vblank = 0;
9428a905236SJesse Barnes 	unsigned long irqflags;
9438a905236SJesse Barnes 	int irq_received;
9448a905236SJesse Barnes 	int ret = IRQ_NONE;
945852835f3SZou Nan hai 	struct intel_ring_buffer *render_ring = &dev_priv->render_ring;
9468a905236SJesse Barnes 
9478a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
9488a905236SJesse Barnes 
949bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
950f2b115e6SAdam Jackson 		return ironlake_irq_handler(dev);
9518a905236SJesse Barnes 
9528a905236SJesse Barnes 	iir = I915_READ(IIR);
9538a905236SJesse Barnes 
954e25e6601SJesse Barnes 	if (IS_I965G(dev))
955d874bcffSJesse Barnes 		vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
956e25e6601SJesse Barnes 	else
957d874bcffSJesse Barnes 		vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
9588a905236SJesse Barnes 
9598a905236SJesse Barnes 	for (;;) {
9608a905236SJesse Barnes 		irq_received = iir != 0;
9618a905236SJesse Barnes 
9628a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
9638a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
9648a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
9658a905236SJesse Barnes 		 * interrupts (for non-MSI).
9668a905236SJesse Barnes 		 */
9678a905236SJesse Barnes 		spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
9688a905236SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
9698a905236SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
9708a905236SJesse Barnes 
9718a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
972ba1234d1SBen Gamari 			i915_handle_error(dev, false);
9738a905236SJesse Barnes 
9748a905236SJesse Barnes 		/*
9758a905236SJesse Barnes 		 * Clear the PIPE(A|B)STAT regs before the IIR
9768a905236SJesse Barnes 		 */
9778a905236SJesse Barnes 		if (pipea_stats & 0x8000ffff) {
9788a905236SJesse Barnes 			if (pipea_stats &  PIPE_FIFO_UNDERRUN_STATUS)
97944d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe a underrun\n");
9808a905236SJesse Barnes 			I915_WRITE(PIPEASTAT, pipea_stats);
9818a905236SJesse Barnes 			irq_received = 1;
9828a905236SJesse Barnes 		}
9838a905236SJesse Barnes 
9848a905236SJesse Barnes 		if (pipeb_stats & 0x8000ffff) {
9858a905236SJesse Barnes 			if (pipeb_stats &  PIPE_FIFO_UNDERRUN_STATUS)
98644d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe b underrun\n");
9878a905236SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
9888a905236SJesse Barnes 			irq_received = 1;
9898a905236SJesse Barnes 		}
9908a905236SJesse Barnes 		spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
9918a905236SJesse Barnes 
9928a905236SJesse Barnes 		if (!irq_received)
9938a905236SJesse Barnes 			break;
9948a905236SJesse Barnes 
9958a905236SJesse Barnes 		ret = IRQ_HANDLED;
9968a905236SJesse Barnes 
9978a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
9988a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
9998a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
10008a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
10018a905236SJesse Barnes 
100244d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
10038a905236SJesse Barnes 				  hotplug_status);
10048a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
10059c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
10069c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
10078a905236SJesse Barnes 
10088a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
10098a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
101063eeaf38SJesse Barnes 		}
101163eeaf38SJesse Barnes 
1012673a394bSEric Anholt 		I915_WRITE(IIR, iir);
1013cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
10147c463586SKeith Packard 
10157c1c2871SDave Airlie 		if (dev->primary->master) {
10167c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
10177c1c2871SDave Airlie 			if (master_priv->sarea_priv)
10187c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
1019c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
10207c1c2871SDave Airlie 		}
10210a3e67a4SJesse Barnes 
1022673a394bSEric Anholt 		if (iir & I915_USER_INTERRUPT) {
1023852835f3SZou Nan hai 			u32 seqno =
1024852835f3SZou Nan hai 				render_ring->get_gem_seqno(dev, render_ring);
1025852835f3SZou Nan hai 			render_ring->irq_gem_seqno = seqno;
10261c5d22f7SChris Wilson 			trace_i915_gem_request_complete(dev, seqno);
1027852835f3SZou Nan hai 			DRM_WAKEUP(&dev_priv->render_ring.irq_queue);
1028f65d9421SBen Gamari 			dev_priv->hangcheck_count = 0;
1029f65d9421SBen Gamari 			mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1030673a394bSEric Anholt 		}
1031673a394bSEric Anholt 
1032d1b851fcSZou Nan hai 		if (HAS_BSD(dev) && (iir & I915_BSD_USER_INTERRUPT))
1033d1b851fcSZou Nan hai 			DRM_WAKEUP(&dev_priv->bsd_ring.irq_queue);
1034d1b851fcSZou Nan hai 
10351afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
10366b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
10371afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
10381afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 0);
10391afe3e9dSJesse Barnes 		}
10406b95a207SKristian Høgsberg 
10411afe3e9dSJesse Barnes 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
104270565d00SJesse Barnes 			intel_prepare_page_flip(dev, 1);
10431afe3e9dSJesse Barnes 			if (dev_priv->flip_pending_is_done)
10441afe3e9dSJesse Barnes 				intel_finish_page_flip_plane(dev, 1);
10451afe3e9dSJesse Barnes 		}
10466b95a207SKristian Høgsberg 
104705eff845SKeith Packard 		if (pipea_stats & vblank_status) {
10487c463586SKeith Packard 			vblank++;
10497c463586SKeith Packard 			drm_handle_vblank(dev, 0);
10504e5359cdSSimon Farnsworth 			if (!dev_priv->flip_pending_is_done) {
10514e5359cdSSimon Farnsworth 				i915_pageflip_stall_check(dev, 0);
10526b95a207SKristian Høgsberg 				intel_finish_page_flip(dev, 0);
10537c463586SKeith Packard 			}
10544e5359cdSSimon Farnsworth 		}
10557c463586SKeith Packard 
105605eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
10577c463586SKeith Packard 			vblank++;
10587c463586SKeith Packard 			drm_handle_vblank(dev, 1);
10594e5359cdSSimon Farnsworth 			if (!dev_priv->flip_pending_is_done) {
10604e5359cdSSimon Farnsworth 				i915_pageflip_stall_check(dev, 1);
10616b95a207SKristian Høgsberg 				intel_finish_page_flip(dev, 1);
10627c463586SKeith Packard 			}
10634e5359cdSSimon Farnsworth 		}
10647c463586SKeith Packard 
1065d874bcffSJesse Barnes 		if ((pipea_stats & PIPE_LEGACY_BLC_EVENT_STATUS) ||
1066d874bcffSJesse Barnes 		    (pipeb_stats & PIPE_LEGACY_BLC_EVENT_STATUS) ||
10677c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
10683b617967SChris Wilson 			intel_opregion_asle_intr(dev);
10690a3e67a4SJesse Barnes 
1070cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
1071cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
1072cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
1073cdfbc41fSEric Anholt 		 * we would never get another interrupt.
1074cdfbc41fSEric Anholt 		 *
1075cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
1076cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
1077cdfbc41fSEric Anholt 		 * another one.
1078cdfbc41fSEric Anholt 		 *
1079cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
1080cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
1081cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
1082cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
1083cdfbc41fSEric Anholt 		 * stray interrupts.
1084cdfbc41fSEric Anholt 		 */
1085cdfbc41fSEric Anholt 		iir = new_iir;
108605eff845SKeith Packard 	}
1087cdfbc41fSEric Anholt 
108805eff845SKeith Packard 	return ret;
1089c0e09200SDave Airlie }
1090c0e09200SDave Airlie 
1091c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
1092c0e09200SDave Airlie {
1093c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
10947c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1095c0e09200SDave Airlie 
1096c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
1097c0e09200SDave Airlie 
109844d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
1099c0e09200SDave Airlie 
1100c99b058fSKristian Høgsberg 	dev_priv->counter++;
1101c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
1102c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
11037c1c2871SDave Airlie 	if (master_priv->sarea_priv)
11047c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
1105c0e09200SDave Airlie 
11060baf823aSKeith Packard 	BEGIN_LP_RING(4);
1107585fb111SJesse Barnes 	OUT_RING(MI_STORE_DWORD_INDEX);
11080baf823aSKeith Packard 	OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1109c0e09200SDave Airlie 	OUT_RING(dev_priv->counter);
1110585fb111SJesse Barnes 	OUT_RING(MI_USER_INTERRUPT);
1111c0e09200SDave Airlie 	ADVANCE_LP_RING();
1112c0e09200SDave Airlie 
1113c0e09200SDave Airlie 	return dev_priv->counter;
1114c0e09200SDave Airlie }
1115c0e09200SDave Airlie 
11169d34e5dbSChris Wilson void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
11179d34e5dbSChris Wilson {
11189d34e5dbSChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11198187a2b7SZou Nan hai 	struct intel_ring_buffer *render_ring = &dev_priv->render_ring;
11209d34e5dbSChris Wilson 
11219d34e5dbSChris Wilson 	if (dev_priv->trace_irq_seqno == 0)
11228187a2b7SZou Nan hai 		render_ring->user_irq_get(dev, render_ring);
11239d34e5dbSChris Wilson 
11249d34e5dbSChris Wilson 	dev_priv->trace_irq_seqno = seqno;
11259d34e5dbSChris Wilson }
11269d34e5dbSChris Wilson 
1127c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
1128c0e09200SDave Airlie {
1129c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11307c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1131c0e09200SDave Airlie 	int ret = 0;
11328187a2b7SZou Nan hai 	struct intel_ring_buffer *render_ring = &dev_priv->render_ring;
1133c0e09200SDave Airlie 
113444d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
1135c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
1136c0e09200SDave Airlie 
1137ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
11387c1c2871SDave Airlie 		if (master_priv->sarea_priv)
11397c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
1140c0e09200SDave Airlie 		return 0;
1141ed4cb414SEric Anholt 	}
1142c0e09200SDave Airlie 
11437c1c2871SDave Airlie 	if (master_priv->sarea_priv)
11447c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1145c0e09200SDave Airlie 
11468187a2b7SZou Nan hai 	render_ring->user_irq_get(dev, render_ring);
1147852835f3SZou Nan hai 	DRM_WAIT_ON(ret, dev_priv->render_ring.irq_queue, 3 * DRM_HZ,
1148c0e09200SDave Airlie 		    READ_BREADCRUMB(dev_priv) >= irq_nr);
11498187a2b7SZou Nan hai 	render_ring->user_irq_put(dev, render_ring);
1150c0e09200SDave Airlie 
1151c0e09200SDave Airlie 	if (ret == -EBUSY) {
1152c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
1153c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1154c0e09200SDave Airlie 	}
1155c0e09200SDave Airlie 
1156c0e09200SDave Airlie 	return ret;
1157c0e09200SDave Airlie }
1158c0e09200SDave Airlie 
1159c0e09200SDave Airlie /* Needs the lock as it touches the ring.
1160c0e09200SDave Airlie  */
1161c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
1162c0e09200SDave Airlie 			 struct drm_file *file_priv)
1163c0e09200SDave Airlie {
1164c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1165c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
1166c0e09200SDave Airlie 	int result;
1167c0e09200SDave Airlie 
1168d3301d86SEric Anholt 	if (!dev_priv || !dev_priv->render_ring.virtual_start) {
1169c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1170c0e09200SDave Airlie 		return -EINVAL;
1171c0e09200SDave Airlie 	}
1172299eb93cSEric Anholt 
1173299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1174299eb93cSEric Anholt 
1175546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
1176c0e09200SDave Airlie 	result = i915_emit_irq(dev);
1177546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
1178c0e09200SDave Airlie 
1179c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
1180c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
1181c0e09200SDave Airlie 		return -EFAULT;
1182c0e09200SDave Airlie 	}
1183c0e09200SDave Airlie 
1184c0e09200SDave Airlie 	return 0;
1185c0e09200SDave Airlie }
1186c0e09200SDave Airlie 
1187c0e09200SDave Airlie /* Doesn't need the hardware lock.
1188c0e09200SDave Airlie  */
1189c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
1190c0e09200SDave Airlie 			 struct drm_file *file_priv)
1191c0e09200SDave Airlie {
1192c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1193c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
1194c0e09200SDave Airlie 
1195c0e09200SDave Airlie 	if (!dev_priv) {
1196c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1197c0e09200SDave Airlie 		return -EINVAL;
1198c0e09200SDave Airlie 	}
1199c0e09200SDave Airlie 
1200c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
1201c0e09200SDave Airlie }
1202c0e09200SDave Airlie 
120342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
120442f52ef8SKeith Packard  * we use as a pipe index
120542f52ef8SKeith Packard  */
120642f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
12070a3e67a4SJesse Barnes {
12080a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1209e9d21d7fSKeith Packard 	unsigned long irqflags;
121071e0ffa5SJesse Barnes 	int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
121171e0ffa5SJesse Barnes 	u32 pipeconf;
121271e0ffa5SJesse Barnes 
121371e0ffa5SJesse Barnes 	pipeconf = I915_READ(pipeconf_reg);
121471e0ffa5SJesse Barnes 	if (!(pipeconf & PIPEACONF_ENABLE))
121571e0ffa5SJesse Barnes 		return -EINVAL;
12160a3e67a4SJesse Barnes 
1217e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
1218bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1219c062df61SLi Peng 		ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1220c062df61SLi Peng 					    DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1221c062df61SLi Peng 	else if (IS_I965G(dev))
12227c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
12237c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
12240a3e67a4SJesse Barnes 	else
12257c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
12267c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
1227e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
12280a3e67a4SJesse Barnes 	return 0;
12290a3e67a4SJesse Barnes }
12300a3e67a4SJesse Barnes 
123142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
123242f52ef8SKeith Packard  * we use as a pipe index
123342f52ef8SKeith Packard  */
123442f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
12350a3e67a4SJesse Barnes {
12360a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1237e9d21d7fSKeith Packard 	unsigned long irqflags;
12380a3e67a4SJesse Barnes 
1239e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
1240bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1241c062df61SLi Peng 		ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1242c062df61SLi Peng 					     DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1243c062df61SLi Peng 	else
12447c463586SKeith Packard 		i915_disable_pipestat(dev_priv, pipe,
12457c463586SKeith Packard 				      PIPE_VBLANK_INTERRUPT_ENABLE |
12467c463586SKeith Packard 				      PIPE_START_VBLANK_INTERRUPT_ENABLE);
1247e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
12480a3e67a4SJesse Barnes }
12490a3e67a4SJesse Barnes 
125079e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev)
125179e53945SJesse Barnes {
125279e53945SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1253e170b030SZhenyu Wang 
1254bad720ffSEric Anholt 	if (!HAS_PCH_SPLIT(dev))
12553b617967SChris Wilson 		intel_opregion_enable_asle(dev);
125679e53945SJesse Barnes 	dev_priv->irq_enabled = 1;
125779e53945SJesse Barnes }
125879e53945SJesse Barnes 
125979e53945SJesse Barnes 
1260c0e09200SDave Airlie /* Set the vblank monitor pipe
1261c0e09200SDave Airlie  */
1262c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1263c0e09200SDave Airlie 			 struct drm_file *file_priv)
1264c0e09200SDave Airlie {
1265c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1266c0e09200SDave Airlie 
1267c0e09200SDave Airlie 	if (!dev_priv) {
1268c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1269c0e09200SDave Airlie 		return -EINVAL;
1270c0e09200SDave Airlie 	}
1271c0e09200SDave Airlie 
1272c0e09200SDave Airlie 	return 0;
1273c0e09200SDave Airlie }
1274c0e09200SDave Airlie 
1275c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1276c0e09200SDave Airlie 			 struct drm_file *file_priv)
1277c0e09200SDave Airlie {
1278c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1279c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
1280c0e09200SDave Airlie 
1281c0e09200SDave Airlie 	if (!dev_priv) {
1282c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1283c0e09200SDave Airlie 		return -EINVAL;
1284c0e09200SDave Airlie 	}
1285c0e09200SDave Airlie 
12860a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1287c0e09200SDave Airlie 
1288c0e09200SDave Airlie 	return 0;
1289c0e09200SDave Airlie }
1290c0e09200SDave Airlie 
1291c0e09200SDave Airlie /**
1292c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
1293c0e09200SDave Airlie  */
1294c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
1295c0e09200SDave Airlie 		     struct drm_file *file_priv)
1296c0e09200SDave Airlie {
1297bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1298bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1299bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1300bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1301bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1302bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1303bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1304bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1305bd95e0a4SEric Anholt 	 *
1306bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1307bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1308bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1309bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
13100a3e67a4SJesse Barnes 	 */
1311c0e09200SDave Airlie 	return -EINVAL;
1312c0e09200SDave Airlie }
1313c0e09200SDave Airlie 
1314995b6762SChris Wilson static struct drm_i915_gem_request *
1315852835f3SZou Nan hai i915_get_tail_request(struct drm_device *dev)
1316852835f3SZou Nan hai {
1317f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1318852835f3SZou Nan hai 	return list_entry(dev_priv->render_ring.request_list.prev,
1319852835f3SZou Nan hai 			struct drm_i915_gem_request, list);
1320f65d9421SBen Gamari }
1321f65d9421SBen Gamari 
1322f65d9421SBen Gamari /**
1323f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1324f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1325f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1326f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1327f65d9421SBen Gamari  */
1328f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1329f65d9421SBen Gamari {
1330f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1331f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1332cbb465e7SChris Wilson 	uint32_t acthd, instdone, instdone1;
1333f65d9421SBen Gamari 
1334b9201c14SEric Anholt 	/* No reset support on this chip yet. */
1335b9201c14SEric Anholt 	if (IS_GEN6(dev))
1336b9201c14SEric Anholt 		return;
1337b9201c14SEric Anholt 
1338cbb465e7SChris Wilson 	if (!IS_I965G(dev)) {
1339f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
1340cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE);
1341cbb465e7SChris Wilson 		instdone1 = 0;
1342cbb465e7SChris Wilson 	} else {
1343f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
1344cbb465e7SChris Wilson 		instdone = I915_READ(INSTDONE_I965);
1345cbb465e7SChris Wilson 		instdone1 = I915_READ(INSTDONE1);
1346cbb465e7SChris Wilson 	}
1347f65d9421SBen Gamari 
1348f65d9421SBen Gamari 	/* If all work is done then ACTHD clearly hasn't advanced. */
1349852835f3SZou Nan hai 	if (list_empty(&dev_priv->render_ring.request_list) ||
1350852835f3SZou Nan hai 		i915_seqno_passed(i915_get_gem_seqno(dev,
1351852835f3SZou Nan hai 				&dev_priv->render_ring),
1352852835f3SZou Nan hai 			i915_get_tail_request(dev)->seqno)) {
1353f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
1354e78d73b1SChris Wilson 
1355e78d73b1SChris Wilson 		/* Issue a wake-up to catch stuck h/w. */
1356e78d73b1SChris Wilson 		if (dev_priv->render_ring.waiting_gem_seqno |
1357e78d73b1SChris Wilson 		    dev_priv->bsd_ring.waiting_gem_seqno) {
1358e78d73b1SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... GPU idle, missed IRQ.\n");
1359e78d73b1SChris Wilson 			if (dev_priv->render_ring.waiting_gem_seqno)
1360e78d73b1SChris Wilson 				DRM_WAKEUP(&dev_priv->render_ring.irq_queue);
1361e78d73b1SChris Wilson 			if (dev_priv->bsd_ring.waiting_gem_seqno)
1362e78d73b1SChris Wilson 				DRM_WAKEUP(&dev_priv->bsd_ring.irq_queue);
1363e78d73b1SChris Wilson 		}
1364f65d9421SBen Gamari 		return;
1365f65d9421SBen Gamari 	}
1366f65d9421SBen Gamari 
1367cbb465e7SChris Wilson 	if (dev_priv->last_acthd == acthd &&
1368cbb465e7SChris Wilson 	    dev_priv->last_instdone == instdone &&
1369cbb465e7SChris Wilson 	    dev_priv->last_instdone1 == instdone1) {
1370cbb465e7SChris Wilson 		if (dev_priv->hangcheck_count++ > 1) {
1371f65d9421SBen Gamari 			DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
13728c80b59bSChris Wilson 
13738c80b59bSChris Wilson 			if (!IS_GEN2(dev)) {
13748c80b59bSChris Wilson 				/* Is the chip hanging on a WAIT_FOR_EVENT?
13758c80b59bSChris Wilson 				 * If so we can simply poke the RB_WAIT bit
13768c80b59bSChris Wilson 				 * and break the hang. This should work on
13778c80b59bSChris Wilson 				 * all but the second generation chipsets.
13788c80b59bSChris Wilson 				 */
13798c80b59bSChris Wilson 				u32 tmp = I915_READ(PRB0_CTL);
13808c80b59bSChris Wilson 				if (tmp & RING_WAIT) {
13818c80b59bSChris Wilson 					I915_WRITE(PRB0_CTL, tmp);
13828c80b59bSChris Wilson 					POSTING_READ(PRB0_CTL);
13838c80b59bSChris Wilson 					goto out;
13848c80b59bSChris Wilson 				}
13858c80b59bSChris Wilson 			}
13868c80b59bSChris Wilson 
1387ba1234d1SBen Gamari 			i915_handle_error(dev, true);
1388f65d9421SBen Gamari 			return;
1389f65d9421SBen Gamari 		}
1390cbb465e7SChris Wilson 	} else {
1391cbb465e7SChris Wilson 		dev_priv->hangcheck_count = 0;
1392cbb465e7SChris Wilson 
1393cbb465e7SChris Wilson 		dev_priv->last_acthd = acthd;
1394cbb465e7SChris Wilson 		dev_priv->last_instdone = instdone;
1395cbb465e7SChris Wilson 		dev_priv->last_instdone1 = instdone1;
1396cbb465e7SChris Wilson 	}
1397f65d9421SBen Gamari 
13988c80b59bSChris Wilson out:
1399f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1400f65d9421SBen Gamari 	mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1401f65d9421SBen Gamari }
1402f65d9421SBen Gamari 
1403c0e09200SDave Airlie /* drm_dma.h hooks
1404c0e09200SDave Airlie */
1405f2b115e6SAdam Jackson static void ironlake_irq_preinstall(struct drm_device *dev)
1406036a4a7dSZhenyu Wang {
1407036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1408036a4a7dSZhenyu Wang 
1409036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1410036a4a7dSZhenyu Wang 
1411036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1412036a4a7dSZhenyu Wang 
1413036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1414036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1415036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
1416036a4a7dSZhenyu Wang 
1417036a4a7dSZhenyu Wang 	/* and GT */
1418036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1419036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1420036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
1421c650156aSZhenyu Wang 
1422c650156aSZhenyu Wang 	/* south display irq */
1423c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1424c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
1425c650156aSZhenyu Wang 	(void) I915_READ(SDEIER);
1426036a4a7dSZhenyu Wang }
1427036a4a7dSZhenyu Wang 
1428f2b115e6SAdam Jackson static int ironlake_irq_postinstall(struct drm_device *dev)
1429036a4a7dSZhenyu Wang {
1430036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1431036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1432013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1433013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
1434d1b851fcSZou Nan hai 	u32 render_mask = GT_PIPE_NOTIFY | GT_BSD_USER_INTERRUPT;
1435c650156aSZhenyu Wang 	u32 hotplug_mask = SDE_CRT_HOTPLUG | SDE_PORTB_HOTPLUG |
1436c650156aSZhenyu Wang 			   SDE_PORTC_HOTPLUG | SDE_PORTD_HOTPLUG;
1437036a4a7dSZhenyu Wang 
1438036a4a7dSZhenyu Wang 	dev_priv->irq_mask_reg = ~display_mask;
1439643ced9bSLi Peng 	dev_priv->de_irq_enable_reg = display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK;
1440036a4a7dSZhenyu Wang 
1441036a4a7dSZhenyu Wang 	/* should always can generate irq */
1442036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1443036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
1444036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
1445036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
1446036a4a7dSZhenyu Wang 
14473fdef020SZhenyu Wang 	/* Gen6 only needs render pipe_control now */
14483fdef020SZhenyu Wang 	if (IS_GEN6(dev))
14493fdef020SZhenyu Wang 		render_mask = GT_PIPE_NOTIFY;
14503fdef020SZhenyu Wang 
1451852835f3SZou Nan hai 	dev_priv->gt_irq_mask_reg = ~render_mask;
1452036a4a7dSZhenyu Wang 	dev_priv->gt_irq_enable_reg = render_mask;
1453036a4a7dSZhenyu Wang 
1454036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1455036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
14563fdef020SZhenyu Wang 	if (IS_GEN6(dev))
14573fdef020SZhenyu Wang 		I915_WRITE(GEN6_RENDER_IMR, ~GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT);
1458036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
1459036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
1460036a4a7dSZhenyu Wang 
1461c650156aSZhenyu Wang 	dev_priv->pch_irq_mask_reg = ~hotplug_mask;
1462c650156aSZhenyu Wang 	dev_priv->pch_irq_enable_reg = hotplug_mask;
1463c650156aSZhenyu Wang 
1464c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1465c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask_reg);
1466c650156aSZhenyu Wang 	I915_WRITE(SDEIER, dev_priv->pch_irq_enable_reg);
1467c650156aSZhenyu Wang 	(void) I915_READ(SDEIER);
1468c650156aSZhenyu Wang 
1469f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1470f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1471f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1472f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1473f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1474f97108d1SJesse Barnes 	}
1475f97108d1SJesse Barnes 
1476036a4a7dSZhenyu Wang 	return 0;
1477036a4a7dSZhenyu Wang }
1478036a4a7dSZhenyu Wang 
1479c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1480c0e09200SDave Airlie {
1481c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1482c0e09200SDave Airlie 
148379e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
148479e53945SJesse Barnes 
1485036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
14868a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1487036a4a7dSZhenyu Wang 
1488bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1489f2b115e6SAdam Jackson 		ironlake_irq_preinstall(dev);
1490036a4a7dSZhenyu Wang 		return;
1491036a4a7dSZhenyu Wang 	}
1492036a4a7dSZhenyu Wang 
14935ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
14945ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
14955ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
14965ca58282SJesse Barnes 	}
14975ca58282SJesse Barnes 
14980a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
14997c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
15007c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
15010a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1502ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
15037c463586SKeith Packard 	(void) I915_READ(IER);
1504c0e09200SDave Airlie }
1505c0e09200SDave Airlie 
1506b01f2c3aSJesse Barnes /*
1507b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
1508b01f2c3aSJesse Barnes  * enabled correctly.
1509b01f2c3aSJesse Barnes  */
15100a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1511c0e09200SDave Airlie {
1512c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
15135ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
151463eeaf38SJesse Barnes 	u32 error_mask;
15150a3e67a4SJesse Barnes 
1516852835f3SZou Nan hai 	DRM_INIT_WAITQUEUE(&dev_priv->render_ring.irq_queue);
1517036a4a7dSZhenyu Wang 
1518d1b851fcSZou Nan hai 	if (HAS_BSD(dev))
1519d1b851fcSZou Nan hai 		DRM_INIT_WAITQUEUE(&dev_priv->bsd_ring.irq_queue);
1520d1b851fcSZou Nan hai 
15210a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1522ed4cb414SEric Anholt 
1523bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1524f2b115e6SAdam Jackson 		return ironlake_irq_postinstall(dev);
1525036a4a7dSZhenyu Wang 
15267c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
15277c463586SKeith Packard 	dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
15288ee1c3dbSMatthew Garrett 
15297c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
15307c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
15317c463586SKeith Packard 
15325ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
1533c496fa1fSAdam Jackson 		/* Enable in IER... */
1534c496fa1fSAdam Jackson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1535c496fa1fSAdam Jackson 		/* and unmask in IMR */
1536c496fa1fSAdam Jackson 		dev_priv->irq_mask_reg &= ~I915_DISPLAY_PORT_INTERRUPT;
1537c496fa1fSAdam Jackson 	}
1538c496fa1fSAdam Jackson 
1539c496fa1fSAdam Jackson 	/*
1540c496fa1fSAdam Jackson 	 * Enable some error detection, note the instruction error mask
1541c496fa1fSAdam Jackson 	 * bit is reserved, so we leave it masked.
1542c496fa1fSAdam Jackson 	 */
1543c496fa1fSAdam Jackson 	if (IS_G4X(dev)) {
1544c496fa1fSAdam Jackson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
1545c496fa1fSAdam Jackson 			       GM45_ERROR_MEM_PRIV |
1546c496fa1fSAdam Jackson 			       GM45_ERROR_CP_PRIV |
1547c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1548c496fa1fSAdam Jackson 	} else {
1549c496fa1fSAdam Jackson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
1550c496fa1fSAdam Jackson 			       I915_ERROR_MEMORY_REFRESH);
1551c496fa1fSAdam Jackson 	}
1552c496fa1fSAdam Jackson 	I915_WRITE(EMR, error_mask);
1553c496fa1fSAdam Jackson 
1554c496fa1fSAdam Jackson 	I915_WRITE(IMR, dev_priv->irq_mask_reg);
1555c496fa1fSAdam Jackson 	I915_WRITE(IER, enable_mask);
1556c496fa1fSAdam Jackson 	(void) I915_READ(IER);
1557c496fa1fSAdam Jackson 
1558c496fa1fSAdam Jackson 	if (I915_HAS_HOTPLUG(dev)) {
15595ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
15605ca58282SJesse Barnes 
1561b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
1562b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1563b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1564b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1565b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1566b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1567b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
1568b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1569b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1570b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1571b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
15722d1c9752SAndy Lutomirski 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
1573b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
15742d1c9752SAndy Lutomirski 
15752d1c9752SAndy Lutomirski 			/* Programming the CRT detection parameters tends
15762d1c9752SAndy Lutomirski 			   to generate a spurious hotplug event about three
15772d1c9752SAndy Lutomirski 			   seconds later.  So just do it once.
15782d1c9752SAndy Lutomirski 			*/
15792d1c9752SAndy Lutomirski 			if (IS_G4X(dev))
15802d1c9752SAndy Lutomirski 				hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
15812d1c9752SAndy Lutomirski 			hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
15822d1c9752SAndy Lutomirski 		}
15832d1c9752SAndy Lutomirski 
1584b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
1585b01f2c3aSJesse Barnes 
15865ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
15875ca58282SJesse Barnes 	}
15885ca58282SJesse Barnes 
15893b617967SChris Wilson 	intel_opregion_enable_asle(dev);
15900a3e67a4SJesse Barnes 
15910a3e67a4SJesse Barnes 	return 0;
1592c0e09200SDave Airlie }
1593c0e09200SDave Airlie 
1594f2b115e6SAdam Jackson static void ironlake_irq_uninstall(struct drm_device *dev)
1595036a4a7dSZhenyu Wang {
1596036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1597036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1598036a4a7dSZhenyu Wang 
1599036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1600036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1601036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1602036a4a7dSZhenyu Wang 
1603036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1604036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1605036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1606036a4a7dSZhenyu Wang }
1607036a4a7dSZhenyu Wang 
1608c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1609c0e09200SDave Airlie {
1610c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1611c0e09200SDave Airlie 
1612c0e09200SDave Airlie 	if (!dev_priv)
1613c0e09200SDave Airlie 		return;
1614c0e09200SDave Airlie 
16150a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
16160a3e67a4SJesse Barnes 
1617bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1618f2b115e6SAdam Jackson 		ironlake_irq_uninstall(dev);
1619036a4a7dSZhenyu Wang 		return;
1620036a4a7dSZhenyu Wang 	}
1621036a4a7dSZhenyu Wang 
16225ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
16235ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
16245ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
16255ca58282SJesse Barnes 	}
16265ca58282SJesse Barnes 
16270a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
16287c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
16297c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
16300a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1631ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1632c0e09200SDave Airlie 
16337c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
16347c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
16357c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1636c0e09200SDave Airlie }
1637