xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision eb1f8e4f3be898df808e2dfc131099f5831d491d)
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. */
567c463586SKeith Packard #define I915_INTERRUPT_ENABLE_VAR (I915_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 
77036a4a7dSZhenyu Wang static inline 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 */
88036a4a7dSZhenyu Wang 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 
118ed4cb414SEric Anholt static inline 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,
17401c66889SZhao Yakui 				     I915_LEGACY_BLC_EVENT_ENABLE);
175edcb49caSZhao Yakui 		if (IS_I965G(dev))
176edcb49caSZhao Yakui 			i915_enable_pipestat(dev_priv, 0,
177edcb49caSZhao Yakui 					     I915_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;
2635bf4c9c4SZhenyu Wang 	struct drm_encoder *encoder;
2645ca58282SJesse Barnes 
2655bf4c9c4SZhenyu Wang 	if (mode_config->num_encoder) {
2665bf4c9c4SZhenyu Wang 		list_for_each_entry(encoder, &mode_config->encoder_list, head) {
2675bf4c9c4SZhenyu Wang 			struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
268c31c4ba3SKeith Packard 
26921d40d37SEric Anholt 			if (intel_encoder->hot_plug)
27021d40d37SEric Anholt 				(*intel_encoder->hot_plug) (intel_encoder);
271c31c4ba3SKeith Packard 		}
272c31c4ba3SKeith Packard 	}
2735ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
274*eb1f8e4fSDave Airlie 	drm_helper_hpd_irq_event(dev);
2755ca58282SJesse Barnes }
2765ca58282SJesse Barnes 
277f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
278f97108d1SJesse Barnes {
279f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
280b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
281f97108d1SJesse Barnes 	u16 rgvswctl;
282f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
283f97108d1SJesse Barnes 
284f97108d1SJesse Barnes 	I915_WRITE(MEMINTRSTS, I915_READ(MEMINTRSTS) & ~MEMINT_EVAL_CHG);
285b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
286b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
287f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
288f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
289f97108d1SJesse Barnes 
290f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
291b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
292f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->max_delay)
293f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay - 1;
294f97108d1SJesse Barnes 		if (new_delay < dev_priv->max_delay)
295f97108d1SJesse Barnes 			new_delay = dev_priv->max_delay;
296b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
297f97108d1SJesse Barnes 		if (dev_priv->cur_delay != dev_priv->min_delay)
298f97108d1SJesse Barnes 			new_delay = dev_priv->cur_delay + 1;
299f97108d1SJesse Barnes 		if (new_delay > dev_priv->min_delay)
300f97108d1SJesse Barnes 			new_delay = dev_priv->min_delay;
301f97108d1SJesse Barnes 	}
302f97108d1SJesse Barnes 
303f97108d1SJesse Barnes 	DRM_DEBUG("rps change requested: %d -> %d\n",
304f97108d1SJesse Barnes 		  dev_priv->cur_delay, new_delay);
305f97108d1SJesse Barnes 
306f97108d1SJesse Barnes 	rgvswctl = I915_READ(MEMSWCTL);
307f97108d1SJesse Barnes 	if (rgvswctl & MEMCTL_CMD_STS) {
308b5b72e89SMatthew Garrett 		DRM_ERROR("gpu busy, RCS change rejected\n");
309b5b72e89SMatthew Garrett 		return; /* still busy with another command */
310f97108d1SJesse Barnes 	}
311f97108d1SJesse Barnes 
312f97108d1SJesse Barnes 	/* Program the new state */
313f97108d1SJesse Barnes 	rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
314f97108d1SJesse Barnes 		(new_delay << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
315f97108d1SJesse Barnes 	I915_WRITE(MEMSWCTL, rgvswctl);
316f97108d1SJesse Barnes 	POSTING_READ(MEMSWCTL);
317f97108d1SJesse Barnes 
318f97108d1SJesse Barnes 	rgvswctl |= MEMCTL_CMD_STS;
319f97108d1SJesse Barnes 	I915_WRITE(MEMSWCTL, rgvswctl);
320f97108d1SJesse Barnes 
321f97108d1SJesse Barnes 	dev_priv->cur_delay = new_delay;
322f97108d1SJesse Barnes 
323f97108d1SJesse Barnes 	DRM_DEBUG("rps changed\n");
324f97108d1SJesse Barnes 
325f97108d1SJesse Barnes 	return;
326f97108d1SJesse Barnes }
327f97108d1SJesse Barnes 
328f2b115e6SAdam Jackson irqreturn_t ironlake_irq_handler(struct drm_device *dev)
329036a4a7dSZhenyu Wang {
330036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
331036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
3323ff99164SDave Airlie 	u32 de_iir, gt_iir, de_ier, pch_iir;
333036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
334036a4a7dSZhenyu Wang 
3352d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
3362d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
3372d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
3382d109a84SZou, Nanhai 	(void)I915_READ(DEIER);
3392d109a84SZou, Nanhai 
340036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
341036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
342c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
343036a4a7dSZhenyu Wang 
344c650156aSZhenyu Wang 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0)
345c7c85101SZou Nan hai 		goto done;
346036a4a7dSZhenyu Wang 
347036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
348036a4a7dSZhenyu Wang 
349036a4a7dSZhenyu Wang 	if (dev->primary->master) {
350036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
351036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
352036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
353036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
354036a4a7dSZhenyu Wang 	}
355036a4a7dSZhenyu Wang 
356036a4a7dSZhenyu Wang 	if (gt_iir & GT_USER_INTERRUPT) {
3571c5d22f7SChris Wilson 		u32 seqno = i915_get_gem_seqno(dev);
3581c5d22f7SChris Wilson 		dev_priv->mm.irq_gem_seqno = seqno;
3591c5d22f7SChris Wilson 		trace_i915_gem_request_complete(dev, seqno);
360036a4a7dSZhenyu Wang 		DRM_WAKEUP(&dev_priv->irq_queue);
361c566ec49SZhenyu Wang 		dev_priv->hangcheck_count = 0;
362c566ec49SZhenyu Wang 		mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
363036a4a7dSZhenyu Wang 	}
364036a4a7dSZhenyu Wang 
36501c66889SZhao Yakui 	if (de_iir & DE_GSE)
36601c66889SZhao Yakui 		ironlake_opregion_gse_intr(dev);
36701c66889SZhao Yakui 
368f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
369013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
370013d5aa2SJesse Barnes 		intel_finish_page_flip(dev, 0);
371013d5aa2SJesse Barnes 	}
372013d5aa2SJesse Barnes 
373f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
374f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
375013d5aa2SJesse Barnes 		intel_finish_page_flip(dev, 1);
376013d5aa2SJesse Barnes 	}
377c062df61SLi Peng 
378f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
379f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
380f072d2e7SZhenyu Wang 
381f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
382f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
383f072d2e7SZhenyu Wang 
384c650156aSZhenyu Wang 	/* check event from PCH */
385c650156aSZhenyu Wang 	if ((de_iir & DE_PCH_EVENT) &&
386c650156aSZhenyu Wang 	    (pch_iir & SDE_HOTPLUG_MASK)) {
387c650156aSZhenyu Wang 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
388c650156aSZhenyu Wang 	}
389c650156aSZhenyu Wang 
390f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
391f97108d1SJesse Barnes 		I915_WRITE(MEMINTRSTS, I915_READ(MEMINTRSTS));
392f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
393f97108d1SJesse Barnes 	}
394f97108d1SJesse Barnes 
395c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
396c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
397c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
398c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
399036a4a7dSZhenyu Wang 
400c7c85101SZou Nan hai done:
4012d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
4022d109a84SZou, Nanhai 	(void)I915_READ(DEIER);
4032d109a84SZou, Nanhai 
404036a4a7dSZhenyu Wang 	return ret;
405036a4a7dSZhenyu Wang }
406036a4a7dSZhenyu Wang 
4078a905236SJesse Barnes /**
4088a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
4098a905236SJesse Barnes  * @work: work struct
4108a905236SJesse Barnes  *
4118a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
4128a905236SJesse Barnes  * was detected.
4138a905236SJesse Barnes  */
4148a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
4158a905236SJesse Barnes {
4168a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
4178a905236SJesse Barnes 						    error_work);
4188a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
419f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
420f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
421f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
4228a905236SJesse Barnes 
42344d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("generating error event\n");
424f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
4258a905236SJesse Barnes 
426ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
427f316a42cSBen Gamari 		if (IS_I965G(dev)) {
42844d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("resetting chip\n");
429f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
430f316a42cSBen Gamari 			if (!i965_reset(dev, GDRST_RENDER)) {
431ba1234d1SBen Gamari 				atomic_set(&dev_priv->mm.wedged, 0);
432f316a42cSBen Gamari 				kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
433f316a42cSBen Gamari 			}
434f316a42cSBen Gamari 		} else {
43544d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("reboot required\n");
436f316a42cSBen Gamari 		}
437f316a42cSBen Gamari 	}
4388a905236SJesse Barnes }
4398a905236SJesse Barnes 
4409df30794SChris Wilson static struct drm_i915_error_object *
4419df30794SChris Wilson i915_error_object_create(struct drm_device *dev,
4429df30794SChris Wilson 			 struct drm_gem_object *src)
4439df30794SChris Wilson {
4449df30794SChris Wilson 	struct drm_i915_error_object *dst;
4459df30794SChris Wilson 	struct drm_i915_gem_object *src_priv;
4469df30794SChris Wilson 	int page, page_count;
4479df30794SChris Wilson 
4489df30794SChris Wilson 	if (src == NULL)
4499df30794SChris Wilson 		return NULL;
4509df30794SChris Wilson 
45123010e43SDaniel Vetter 	src_priv = to_intel_bo(src);
4529df30794SChris Wilson 	if (src_priv->pages == NULL)
4539df30794SChris Wilson 		return NULL;
4549df30794SChris Wilson 
4559df30794SChris Wilson 	page_count = src->size / PAGE_SIZE;
4569df30794SChris Wilson 
4579df30794SChris Wilson 	dst = kmalloc(sizeof(*dst) + page_count * sizeof (u32 *), GFP_ATOMIC);
4589df30794SChris Wilson 	if (dst == NULL)
4599df30794SChris Wilson 		return NULL;
4609df30794SChris Wilson 
4619df30794SChris Wilson 	for (page = 0; page < page_count; page++) {
4629df30794SChris Wilson 		void *s, *d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
4639df30794SChris Wilson 		if (d == NULL)
4649df30794SChris Wilson 			goto unwind;
4659df30794SChris Wilson 		s = kmap_atomic(src_priv->pages[page], KM_USER0);
4669df30794SChris Wilson 		memcpy(d, s, PAGE_SIZE);
4679df30794SChris Wilson 		kunmap_atomic(s, KM_USER0);
4689df30794SChris Wilson 		dst->pages[page] = d;
4699df30794SChris Wilson 	}
4709df30794SChris Wilson 	dst->page_count = page_count;
4719df30794SChris Wilson 	dst->gtt_offset = src_priv->gtt_offset;
4729df30794SChris Wilson 
4739df30794SChris Wilson 	return dst;
4749df30794SChris Wilson 
4759df30794SChris Wilson unwind:
4769df30794SChris Wilson 	while (page--)
4779df30794SChris Wilson 		kfree(dst->pages[page]);
4789df30794SChris Wilson 	kfree(dst);
4799df30794SChris Wilson 	return NULL;
4809df30794SChris Wilson }
4819df30794SChris Wilson 
4829df30794SChris Wilson static void
4839df30794SChris Wilson i915_error_object_free(struct drm_i915_error_object *obj)
4849df30794SChris Wilson {
4859df30794SChris Wilson 	int page;
4869df30794SChris Wilson 
4879df30794SChris Wilson 	if (obj == NULL)
4889df30794SChris Wilson 		return;
4899df30794SChris Wilson 
4909df30794SChris Wilson 	for (page = 0; page < obj->page_count; page++)
4919df30794SChris Wilson 		kfree(obj->pages[page]);
4929df30794SChris Wilson 
4939df30794SChris Wilson 	kfree(obj);
4949df30794SChris Wilson }
4959df30794SChris Wilson 
4969df30794SChris Wilson static void
4979df30794SChris Wilson i915_error_state_free(struct drm_device *dev,
4989df30794SChris Wilson 		      struct drm_i915_error_state *error)
4999df30794SChris Wilson {
5009df30794SChris Wilson 	i915_error_object_free(error->batchbuffer[0]);
5019df30794SChris Wilson 	i915_error_object_free(error->batchbuffer[1]);
5029df30794SChris Wilson 	i915_error_object_free(error->ringbuffer);
5039df30794SChris Wilson 	kfree(error->active_bo);
5049df30794SChris Wilson 	kfree(error);
5059df30794SChris Wilson }
5069df30794SChris Wilson 
5079df30794SChris Wilson static u32
5089df30794SChris Wilson i915_get_bbaddr(struct drm_device *dev, u32 *ring)
5099df30794SChris Wilson {
5109df30794SChris Wilson 	u32 cmd;
5119df30794SChris Wilson 
5129df30794SChris Wilson 	if (IS_I830(dev) || IS_845G(dev))
5139df30794SChris Wilson 		cmd = MI_BATCH_BUFFER;
5149df30794SChris Wilson 	else if (IS_I965G(dev))
5159df30794SChris Wilson 		cmd = (MI_BATCH_BUFFER_START | (2 << 6) |
5169df30794SChris Wilson 		       MI_BATCH_NON_SECURE_I965);
5179df30794SChris Wilson 	else
5189df30794SChris Wilson 		cmd = (MI_BATCH_BUFFER_START | (2 << 6));
5199df30794SChris Wilson 
5209df30794SChris Wilson 	return ring[0] == cmd ? ring[1] : 0;
5219df30794SChris Wilson }
5229df30794SChris Wilson 
5239df30794SChris Wilson static u32
5249df30794SChris Wilson i915_ringbuffer_last_batch(struct drm_device *dev)
5259df30794SChris Wilson {
5269df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
5279df30794SChris Wilson 	u32 head, bbaddr;
5289df30794SChris Wilson 	u32 *ring;
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 	 */
5339df30794SChris Wilson 	bbaddr = 0;
5349df30794SChris Wilson 	head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
5359df30794SChris Wilson 	ring = (u32 *)(dev_priv->ring.virtual_start + head);
5369df30794SChris Wilson 
5379df30794SChris Wilson 	while (--ring >= (u32 *)dev_priv->ring.virtual_start) {
5389df30794SChris Wilson 		bbaddr = i915_get_bbaddr(dev, ring);
5399df30794SChris Wilson 		if (bbaddr)
5409df30794SChris Wilson 			break;
5419df30794SChris Wilson 	}
5429df30794SChris Wilson 
5439df30794SChris Wilson 	if (bbaddr == 0) {
5449df30794SChris Wilson 		ring = (u32 *)(dev_priv->ring.virtual_start + dev_priv->ring.Size);
5459df30794SChris Wilson 		while (--ring >= (u32 *)dev_priv->ring.virtual_start) {
5469df30794SChris Wilson 			bbaddr = i915_get_bbaddr(dev, ring);
5479df30794SChris Wilson 			if (bbaddr)
5489df30794SChris Wilson 				break;
5499df30794SChris Wilson 		}
5509df30794SChris Wilson 	}
5519df30794SChris Wilson 
5529df30794SChris Wilson 	return bbaddr;
5539df30794SChris Wilson }
5549df30794SChris Wilson 
5558a905236SJesse Barnes /**
5568a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
5578a905236SJesse Barnes  * @dev: drm device
5588a905236SJesse Barnes  *
5598a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
5608a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
5618a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
5628a905236SJesse Barnes  * to pick up.
5638a905236SJesse Barnes  */
56463eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
56563eeaf38SJesse Barnes {
56663eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
5679df30794SChris Wilson 	struct drm_i915_gem_object *obj_priv;
56863eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
5699df30794SChris Wilson 	struct drm_gem_object *batchbuffer[2];
57063eeaf38SJesse Barnes 	unsigned long flags;
5719df30794SChris Wilson 	u32 bbaddr;
5729df30794SChris Wilson 	int count;
57363eeaf38SJesse Barnes 
57463eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
5759df30794SChris Wilson 	error = dev_priv->first_error;
5769df30794SChris Wilson 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
5779df30794SChris Wilson 	if (error)
5789df30794SChris Wilson 		return;
57963eeaf38SJesse Barnes 
58063eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
58163eeaf38SJesse Barnes 	if (!error) {
5829df30794SChris Wilson 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
5839df30794SChris Wilson 		return;
58463eeaf38SJesse Barnes 	}
58563eeaf38SJesse Barnes 
5869df30794SChris Wilson 	error->seqno = i915_get_gem_seqno(dev);
58763eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
58863eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
58963eeaf38SJesse Barnes 	error->pipeastat = I915_READ(PIPEASTAT);
59063eeaf38SJesse Barnes 	error->pipebstat = I915_READ(PIPEBSTAT);
59163eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
59263eeaf38SJesse Barnes 	if (!IS_I965G(dev)) {
59363eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR);
59463eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR);
59563eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE);
59663eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD);
5979df30794SChris Wilson 		error->bbaddr = 0;
59863eeaf38SJesse Barnes 	} else {
59963eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
60063eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
60163eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
60263eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
60363eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
60463eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
6059df30794SChris Wilson 		error->bbaddr = I915_READ64(BB_ADDR);
6069df30794SChris Wilson 	}
6079df30794SChris Wilson 
6089df30794SChris Wilson 	bbaddr = i915_ringbuffer_last_batch(dev);
6099df30794SChris Wilson 
6109df30794SChris Wilson 	/* Grab the current batchbuffer, most likely to have crashed. */
6119df30794SChris Wilson 	batchbuffer[0] = NULL;
6129df30794SChris Wilson 	batchbuffer[1] = NULL;
6139df30794SChris Wilson 	count = 0;
6149df30794SChris Wilson 	list_for_each_entry(obj_priv, &dev_priv->mm.active_list, list) {
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 &&
6249df30794SChris Wilson 		    error->acthd < obj_priv->gtt_offset + obj->size &&
6259df30794SChris Wilson 		    batchbuffer[0] != obj)
6269df30794SChris Wilson 			batchbuffer[1] = obj;
6279df30794SChris Wilson 
6289df30794SChris Wilson 		count++;
6299df30794SChris Wilson 	}
6309df30794SChris Wilson 
6319df30794SChris Wilson 	/* We need to copy these to an anonymous buffer as the simplest
6329df30794SChris Wilson 	 * method to avoid being overwritten by userpace.
6339df30794SChris Wilson 	 */
6349df30794SChris Wilson 	error->batchbuffer[0] = i915_error_object_create(dev, batchbuffer[0]);
6359df30794SChris Wilson 	error->batchbuffer[1] = i915_error_object_create(dev, batchbuffer[1]);
6369df30794SChris Wilson 
6379df30794SChris Wilson 	/* Record the ringbuffer */
6389df30794SChris Wilson 	error->ringbuffer = i915_error_object_create(dev, dev_priv->ring.ring_obj);
6399df30794SChris Wilson 
6409df30794SChris Wilson 	/* Record buffers on the active list. */
6419df30794SChris Wilson 	error->active_bo = NULL;
6429df30794SChris Wilson 	error->active_bo_count = 0;
6439df30794SChris Wilson 
6449df30794SChris Wilson 	if (count)
6459df30794SChris Wilson 		error->active_bo = kmalloc(sizeof(*error->active_bo)*count,
6469df30794SChris Wilson 					   GFP_ATOMIC);
6479df30794SChris Wilson 
6489df30794SChris Wilson 	if (error->active_bo) {
6499df30794SChris Wilson 		int i = 0;
6509df30794SChris Wilson 		list_for_each_entry(obj_priv, &dev_priv->mm.active_list, list) {
651a8089e84SDaniel Vetter 			struct drm_gem_object *obj = &obj_priv->base;
6529df30794SChris Wilson 
6539df30794SChris Wilson 			error->active_bo[i].size = obj->size;
6549df30794SChris Wilson 			error->active_bo[i].name = obj->name;
6559df30794SChris Wilson 			error->active_bo[i].seqno = obj_priv->last_rendering_seqno;
6569df30794SChris Wilson 			error->active_bo[i].gtt_offset = obj_priv->gtt_offset;
6579df30794SChris Wilson 			error->active_bo[i].read_domains = obj->read_domains;
6589df30794SChris Wilson 			error->active_bo[i].write_domain = obj->write_domain;
6599df30794SChris Wilson 			error->active_bo[i].fence_reg = obj_priv->fence_reg;
6609df30794SChris Wilson 			error->active_bo[i].pinned = 0;
6619df30794SChris Wilson 			if (obj_priv->pin_count > 0)
6629df30794SChris Wilson 				error->active_bo[i].pinned = 1;
6639df30794SChris Wilson 			if (obj_priv->user_pin_count > 0)
6649df30794SChris Wilson 				error->active_bo[i].pinned = -1;
6659df30794SChris Wilson 			error->active_bo[i].tiling = obj_priv->tiling_mode;
6669df30794SChris Wilson 			error->active_bo[i].dirty = obj_priv->dirty;
6679df30794SChris Wilson 			error->active_bo[i].purgeable = obj_priv->madv != I915_MADV_WILLNEED;
6689df30794SChris Wilson 
6699df30794SChris Wilson 			if (++i == count)
6709df30794SChris Wilson 				break;
6719df30794SChris Wilson 		}
6729df30794SChris Wilson 		error->active_bo_count = i;
67363eeaf38SJesse Barnes 	}
67463eeaf38SJesse Barnes 
6758a905236SJesse Barnes 	do_gettimeofday(&error->time);
6768a905236SJesse Barnes 
6779df30794SChris Wilson 	spin_lock_irqsave(&dev_priv->error_lock, flags);
6789df30794SChris Wilson 	if (dev_priv->first_error == NULL) {
67963eeaf38SJesse Barnes 		dev_priv->first_error = error;
6809df30794SChris Wilson 		error = NULL;
6819df30794SChris Wilson 	}
68263eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
6839df30794SChris Wilson 
6849df30794SChris Wilson 	if (error)
6859df30794SChris Wilson 		i915_error_state_free(dev, error);
6869df30794SChris Wilson }
6879df30794SChris Wilson 
6889df30794SChris Wilson void i915_destroy_error_state(struct drm_device *dev)
6899df30794SChris Wilson {
6909df30794SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
6919df30794SChris Wilson 	struct drm_i915_error_state *error;
6929df30794SChris Wilson 
6939df30794SChris Wilson 	spin_lock(&dev_priv->error_lock);
6949df30794SChris Wilson 	error = dev_priv->first_error;
6959df30794SChris Wilson 	dev_priv->first_error = NULL;
6969df30794SChris Wilson 	spin_unlock(&dev_priv->error_lock);
6979df30794SChris Wilson 
6989df30794SChris Wilson 	if (error)
6999df30794SChris Wilson 		i915_error_state_free(dev, error);
70063eeaf38SJesse Barnes }
70163eeaf38SJesse Barnes 
7028a905236SJesse Barnes /**
7038a905236SJesse Barnes  * i915_handle_error - handle an error interrupt
7048a905236SJesse Barnes  * @dev: drm device
7058a905236SJesse Barnes  *
7068a905236SJesse Barnes  * Do some basic checking of regsiter state at error interrupt time and
7078a905236SJesse Barnes  * dump it to the syslog.  Also call i915_capture_error_state() to make
7088a905236SJesse Barnes  * sure we get a record and make it available in debugfs.  Fire a uevent
7098a905236SJesse Barnes  * so userspace knows something bad happened (should trigger collection
7108a905236SJesse Barnes  * of a ring dump etc.).
7118a905236SJesse Barnes  */
712ba1234d1SBen Gamari static void i915_handle_error(struct drm_device *dev, bool wedged)
713c0e09200SDave Airlie {
7148a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
71563eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
7168a905236SJesse Barnes 	u32 pipea_stats = I915_READ(PIPEASTAT);
7178a905236SJesse Barnes 	u32 pipeb_stats = I915_READ(PIPEBSTAT);
71863eeaf38SJesse Barnes 
71963eeaf38SJesse Barnes 	i915_capture_error_state(dev);
72063eeaf38SJesse Barnes 
72163eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
72263eeaf38SJesse Barnes 	       eir);
7238a905236SJesse Barnes 
7248a905236SJesse Barnes 	if (IS_G4X(dev)) {
7258a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
7268a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
7278a905236SJesse Barnes 
7288a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
7298a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
7308a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
7318a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
7328a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
7338a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
7348a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
7358a905236SJesse Barnes 			       I915_READ(INSTPS));
7368a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
7378a905236SJesse Barnes 			       I915_READ(INSTDONE1));
7388a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
7398a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
7408a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
7418a905236SJesse Barnes 			(void)I915_READ(IPEIR_I965);
7428a905236SJesse Barnes 		}
7438a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
7448a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
7458a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
7468a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
7478a905236SJesse Barnes 			       pgtbl_err);
7488a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
7498a905236SJesse Barnes 			(void)I915_READ(PGTBL_ER);
7508a905236SJesse Barnes 		}
7518a905236SJesse Barnes 	}
7528a905236SJesse Barnes 
7538a905236SJesse Barnes 	if (IS_I9XX(dev)) {
75463eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
75563eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
75663eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
75763eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
75863eeaf38SJesse Barnes 			       pgtbl_err);
75963eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
76063eeaf38SJesse Barnes 			(void)I915_READ(PGTBL_ER);
76163eeaf38SJesse Barnes 		}
7628a905236SJesse Barnes 	}
7638a905236SJesse Barnes 
76463eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
76563eeaf38SJesse Barnes 		printk(KERN_ERR "memory refresh error\n");
76663eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
76763eeaf38SJesse Barnes 		       pipea_stats);
76863eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
76963eeaf38SJesse Barnes 		       pipeb_stats);
77063eeaf38SJesse Barnes 		/* pipestat has already been acked */
77163eeaf38SJesse Barnes 	}
77263eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
77363eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
77463eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
77563eeaf38SJesse Barnes 		       I915_READ(INSTPM));
77663eeaf38SJesse Barnes 		if (!IS_I965G(dev)) {
77763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
77863eeaf38SJesse Barnes 
77963eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
78063eeaf38SJesse Barnes 			       I915_READ(IPEIR));
78163eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
78263eeaf38SJesse Barnes 			       I915_READ(IPEHR));
78363eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
78463eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
78563eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
78663eeaf38SJesse Barnes 			       I915_READ(ACTHD));
78763eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
78863eeaf38SJesse Barnes 			(void)I915_READ(IPEIR);
78963eeaf38SJesse Barnes 		} else {
79063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
79163eeaf38SJesse Barnes 
79263eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
79363eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
79463eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
79563eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
79663eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
79763eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
79863eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
79963eeaf38SJesse Barnes 			       I915_READ(INSTPS));
80063eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
80163eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
80263eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
80363eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
80463eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
80563eeaf38SJesse Barnes 			(void)I915_READ(IPEIR_I965);
80663eeaf38SJesse Barnes 		}
80763eeaf38SJesse Barnes 	}
80863eeaf38SJesse Barnes 
80963eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
81063eeaf38SJesse Barnes 	(void)I915_READ(EIR);
81163eeaf38SJesse Barnes 	eir = I915_READ(EIR);
81263eeaf38SJesse Barnes 	if (eir) {
81363eeaf38SJesse Barnes 		/*
81463eeaf38SJesse Barnes 		 * some errors might have become stuck,
81563eeaf38SJesse Barnes 		 * mask them.
81663eeaf38SJesse Barnes 		 */
81763eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
81863eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
81963eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
82063eeaf38SJesse Barnes 	}
8218a905236SJesse Barnes 
822ba1234d1SBen Gamari 	if (wedged) {
823ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
824ba1234d1SBen Gamari 
82511ed50ecSBen Gamari 		/*
82611ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
82711ed50ecSBen Gamari 		 */
82811ed50ecSBen Gamari 		DRM_WAKEUP(&dev_priv->irq_queue);
82911ed50ecSBen Gamari 	}
83011ed50ecSBen Gamari 
8319c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
8328a905236SJesse Barnes }
8338a905236SJesse Barnes 
8348a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
8358a905236SJesse Barnes {
8368a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
8378a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
8388a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
8398a905236SJesse Barnes 	u32 iir, new_iir;
8408a905236SJesse Barnes 	u32 pipea_stats, pipeb_stats;
8418a905236SJesse Barnes 	u32 vblank_status;
8428a905236SJesse Barnes 	u32 vblank_enable;
8438a905236SJesse Barnes 	int vblank = 0;
8448a905236SJesse Barnes 	unsigned long irqflags;
8458a905236SJesse Barnes 	int irq_received;
8468a905236SJesse Barnes 	int ret = IRQ_NONE;
8478a905236SJesse Barnes 
8488a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
8498a905236SJesse Barnes 
850bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
851f2b115e6SAdam Jackson 		return ironlake_irq_handler(dev);
8528a905236SJesse Barnes 
8538a905236SJesse Barnes 	iir = I915_READ(IIR);
8548a905236SJesse Barnes 
8558a905236SJesse Barnes 	if (IS_I965G(dev)) {
8568a905236SJesse Barnes 		vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
8578a905236SJesse Barnes 		vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
8588a905236SJesse Barnes 	} else {
8598a905236SJesse Barnes 		vblank_status = I915_VBLANK_INTERRUPT_STATUS;
8608a905236SJesse Barnes 		vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
8618a905236SJesse Barnes 	}
8628a905236SJesse Barnes 
8638a905236SJesse Barnes 	for (;;) {
8648a905236SJesse Barnes 		irq_received = iir != 0;
8658a905236SJesse Barnes 
8668a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
8678a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
8688a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
8698a905236SJesse Barnes 		 * interrupts (for non-MSI).
8708a905236SJesse Barnes 		 */
8718a905236SJesse Barnes 		spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
8728a905236SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
8738a905236SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
8748a905236SJesse Barnes 
8758a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
876ba1234d1SBen Gamari 			i915_handle_error(dev, false);
8778a905236SJesse Barnes 
8788a905236SJesse Barnes 		/*
8798a905236SJesse Barnes 		 * Clear the PIPE(A|B)STAT regs before the IIR
8808a905236SJesse Barnes 		 */
8818a905236SJesse Barnes 		if (pipea_stats & 0x8000ffff) {
8828a905236SJesse Barnes 			if (pipea_stats &  PIPE_FIFO_UNDERRUN_STATUS)
88344d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe a underrun\n");
8848a905236SJesse Barnes 			I915_WRITE(PIPEASTAT, pipea_stats);
8858a905236SJesse Barnes 			irq_received = 1;
8868a905236SJesse Barnes 		}
8878a905236SJesse Barnes 
8888a905236SJesse Barnes 		if (pipeb_stats & 0x8000ffff) {
8898a905236SJesse Barnes 			if (pipeb_stats &  PIPE_FIFO_UNDERRUN_STATUS)
89044d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe b underrun\n");
8918a905236SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
8928a905236SJesse Barnes 			irq_received = 1;
8938a905236SJesse Barnes 		}
8948a905236SJesse Barnes 		spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
8958a905236SJesse Barnes 
8968a905236SJesse Barnes 		if (!irq_received)
8978a905236SJesse Barnes 			break;
8988a905236SJesse Barnes 
8998a905236SJesse Barnes 		ret = IRQ_HANDLED;
9008a905236SJesse Barnes 
9018a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
9028a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
9038a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
9048a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
9058a905236SJesse Barnes 
90644d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
9078a905236SJesse Barnes 				  hotplug_status);
9088a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
9099c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
9109c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
9118a905236SJesse Barnes 
9128a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
9138a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
91463eeaf38SJesse Barnes 		}
91563eeaf38SJesse Barnes 
916673a394bSEric Anholt 		I915_WRITE(IIR, iir);
917cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
9187c463586SKeith Packard 
9197c1c2871SDave Airlie 		if (dev->primary->master) {
9207c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
9217c1c2871SDave Airlie 			if (master_priv->sarea_priv)
9227c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
923c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
9247c1c2871SDave Airlie 		}
9250a3e67a4SJesse Barnes 
926673a394bSEric Anholt 		if (iir & I915_USER_INTERRUPT) {
9271c5d22f7SChris Wilson 			u32 seqno = i915_get_gem_seqno(dev);
9281c5d22f7SChris Wilson 			dev_priv->mm.irq_gem_seqno = seqno;
9291c5d22f7SChris Wilson 			trace_i915_gem_request_complete(dev, seqno);
930673a394bSEric Anholt 			DRM_WAKEUP(&dev_priv->irq_queue);
931f65d9421SBen Gamari 			dev_priv->hangcheck_count = 0;
932f65d9421SBen Gamari 			mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
933673a394bSEric Anholt 		}
934673a394bSEric Anholt 
9356b95a207SKristian Høgsberg 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
9366b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
9376b95a207SKristian Høgsberg 
9386b95a207SKristian Høgsberg 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
9396b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 1);
9406b95a207SKristian Høgsberg 
94105eff845SKeith Packard 		if (pipea_stats & vblank_status) {
9427c463586SKeith Packard 			vblank++;
9437c463586SKeith Packard 			drm_handle_vblank(dev, 0);
9446b95a207SKristian Høgsberg 			intel_finish_page_flip(dev, 0);
9457c463586SKeith Packard 		}
9467c463586SKeith Packard 
94705eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
9487c463586SKeith Packard 			vblank++;
9497c463586SKeith Packard 			drm_handle_vblank(dev, 1);
9506b95a207SKristian Høgsberg 			intel_finish_page_flip(dev, 1);
9517c463586SKeith Packard 		}
9527c463586SKeith Packard 
953edcb49caSZhao Yakui 		if ((pipea_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
954edcb49caSZhao Yakui 		    (pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
9557c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
956673a394bSEric Anholt 			opregion_asle_intr(dev);
9570a3e67a4SJesse Barnes 
958cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
959cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
960cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
961cdfbc41fSEric Anholt 		 * we would never get another interrupt.
962cdfbc41fSEric Anholt 		 *
963cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
964cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
965cdfbc41fSEric Anholt 		 * another one.
966cdfbc41fSEric Anholt 		 *
967cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
968cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
969cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
970cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
971cdfbc41fSEric Anholt 		 * stray interrupts.
972cdfbc41fSEric Anholt 		 */
973cdfbc41fSEric Anholt 		iir = new_iir;
97405eff845SKeith Packard 	}
975cdfbc41fSEric Anholt 
97605eff845SKeith Packard 	return ret;
977c0e09200SDave Airlie }
978c0e09200SDave Airlie 
979c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
980c0e09200SDave Airlie {
981c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
9827c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
983c0e09200SDave Airlie 	RING_LOCALS;
984c0e09200SDave Airlie 
985c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
986c0e09200SDave Airlie 
98744d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
988c0e09200SDave Airlie 
989c99b058fSKristian Høgsberg 	dev_priv->counter++;
990c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
991c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
9927c1c2871SDave Airlie 	if (master_priv->sarea_priv)
9937c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
994c0e09200SDave Airlie 
9950baf823aSKeith Packard 	BEGIN_LP_RING(4);
996585fb111SJesse Barnes 	OUT_RING(MI_STORE_DWORD_INDEX);
9970baf823aSKeith Packard 	OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
998c0e09200SDave Airlie 	OUT_RING(dev_priv->counter);
999585fb111SJesse Barnes 	OUT_RING(MI_USER_INTERRUPT);
1000c0e09200SDave Airlie 	ADVANCE_LP_RING();
1001c0e09200SDave Airlie 
1002c0e09200SDave Airlie 	return dev_priv->counter;
1003c0e09200SDave Airlie }
1004c0e09200SDave Airlie 
1005673a394bSEric Anholt void i915_user_irq_get(struct drm_device *dev)
1006ed4cb414SEric Anholt {
1007ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1008e9d21d7fSKeith Packard 	unsigned long irqflags;
1009ed4cb414SEric Anholt 
1010e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
1011036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
1012bad720ffSEric Anholt 		if (HAS_PCH_SPLIT(dev))
1013f2b115e6SAdam Jackson 			ironlake_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
1014036a4a7dSZhenyu Wang 		else
1015ed4cb414SEric Anholt 			i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
1016036a4a7dSZhenyu Wang 	}
1017e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
1018ed4cb414SEric Anholt }
1019ed4cb414SEric Anholt 
10200a3e67a4SJesse Barnes void i915_user_irq_put(struct drm_device *dev)
1021ed4cb414SEric Anholt {
1022ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1023e9d21d7fSKeith Packard 	unsigned long irqflags;
1024ed4cb414SEric Anholt 
1025e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
1026ed4cb414SEric Anholt 	BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
1027036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
1028bad720ffSEric Anholt 		if (HAS_PCH_SPLIT(dev))
1029f2b115e6SAdam Jackson 			ironlake_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
1030036a4a7dSZhenyu Wang 		else
1031ed4cb414SEric Anholt 			i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
1032036a4a7dSZhenyu Wang 	}
1033e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
1034ed4cb414SEric Anholt }
1035ed4cb414SEric Anholt 
10369d34e5dbSChris Wilson void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
10379d34e5dbSChris Wilson {
10389d34e5dbSChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10399d34e5dbSChris Wilson 
10409d34e5dbSChris Wilson 	if (dev_priv->trace_irq_seqno == 0)
10419d34e5dbSChris Wilson 		i915_user_irq_get(dev);
10429d34e5dbSChris Wilson 
10439d34e5dbSChris Wilson 	dev_priv->trace_irq_seqno = seqno;
10449d34e5dbSChris Wilson }
10459d34e5dbSChris Wilson 
1046c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
1047c0e09200SDave Airlie {
1048c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10497c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1050c0e09200SDave Airlie 	int ret = 0;
1051c0e09200SDave Airlie 
105244d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
1053c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
1054c0e09200SDave Airlie 
1055ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
10567c1c2871SDave Airlie 		if (master_priv->sarea_priv)
10577c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
1058c0e09200SDave Airlie 		return 0;
1059ed4cb414SEric Anholt 	}
1060c0e09200SDave Airlie 
10617c1c2871SDave Airlie 	if (master_priv->sarea_priv)
10627c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1063c0e09200SDave Airlie 
1064ed4cb414SEric Anholt 	i915_user_irq_get(dev);
1065c0e09200SDave Airlie 	DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
1066c0e09200SDave Airlie 		    READ_BREADCRUMB(dev_priv) >= irq_nr);
1067ed4cb414SEric Anholt 	i915_user_irq_put(dev);
1068c0e09200SDave Airlie 
1069c0e09200SDave Airlie 	if (ret == -EBUSY) {
1070c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
1071c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1072c0e09200SDave Airlie 	}
1073c0e09200SDave Airlie 
1074c0e09200SDave Airlie 	return ret;
1075c0e09200SDave Airlie }
1076c0e09200SDave Airlie 
1077c0e09200SDave Airlie /* Needs the lock as it touches the ring.
1078c0e09200SDave Airlie  */
1079c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
1080c0e09200SDave Airlie 			 struct drm_file *file_priv)
1081c0e09200SDave Airlie {
1082c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1083c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
1084c0e09200SDave Airlie 	int result;
1085c0e09200SDave Airlie 
108607f4f8bfSEric Anholt 	if (!dev_priv || !dev_priv->ring.virtual_start) {
1087c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1088c0e09200SDave Airlie 		return -EINVAL;
1089c0e09200SDave Airlie 	}
1090299eb93cSEric Anholt 
1091299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1092299eb93cSEric Anholt 
1093546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
1094c0e09200SDave Airlie 	result = i915_emit_irq(dev);
1095546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
1096c0e09200SDave Airlie 
1097c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
1098c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
1099c0e09200SDave Airlie 		return -EFAULT;
1100c0e09200SDave Airlie 	}
1101c0e09200SDave Airlie 
1102c0e09200SDave Airlie 	return 0;
1103c0e09200SDave Airlie }
1104c0e09200SDave Airlie 
1105c0e09200SDave Airlie /* Doesn't need the hardware lock.
1106c0e09200SDave Airlie  */
1107c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
1108c0e09200SDave Airlie 			 struct drm_file *file_priv)
1109c0e09200SDave Airlie {
1110c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1111c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
1112c0e09200SDave Airlie 
1113c0e09200SDave Airlie 	if (!dev_priv) {
1114c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1115c0e09200SDave Airlie 		return -EINVAL;
1116c0e09200SDave Airlie 	}
1117c0e09200SDave Airlie 
1118c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
1119c0e09200SDave Airlie }
1120c0e09200SDave Airlie 
112142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
112242f52ef8SKeith Packard  * we use as a pipe index
112342f52ef8SKeith Packard  */
112442f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
11250a3e67a4SJesse Barnes {
11260a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1127e9d21d7fSKeith Packard 	unsigned long irqflags;
112871e0ffa5SJesse Barnes 	int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
112971e0ffa5SJesse Barnes 	u32 pipeconf;
113071e0ffa5SJesse Barnes 
113171e0ffa5SJesse Barnes 	pipeconf = I915_READ(pipeconf_reg);
113271e0ffa5SJesse Barnes 	if (!(pipeconf & PIPEACONF_ENABLE))
113371e0ffa5SJesse Barnes 		return -EINVAL;
11340a3e67a4SJesse Barnes 
1135e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
1136bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1137c062df61SLi Peng 		ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1138c062df61SLi Peng 					    DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1139c062df61SLi Peng 	else if (IS_I965G(dev))
11407c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
11417c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
11420a3e67a4SJesse Barnes 	else
11437c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
11447c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
1145e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
11460a3e67a4SJesse Barnes 	return 0;
11470a3e67a4SJesse Barnes }
11480a3e67a4SJesse Barnes 
114942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
115042f52ef8SKeith Packard  * we use as a pipe index
115142f52ef8SKeith Packard  */
115242f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
11530a3e67a4SJesse Barnes {
11540a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1155e9d21d7fSKeith Packard 	unsigned long irqflags;
11560a3e67a4SJesse Barnes 
1157e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
1158bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1159c062df61SLi Peng 		ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1160c062df61SLi Peng 					     DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1161c062df61SLi Peng 	else
11627c463586SKeith Packard 		i915_disable_pipestat(dev_priv, pipe,
11637c463586SKeith Packard 				      PIPE_VBLANK_INTERRUPT_ENABLE |
11647c463586SKeith Packard 				      PIPE_START_VBLANK_INTERRUPT_ENABLE);
1165e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
11660a3e67a4SJesse Barnes }
11670a3e67a4SJesse Barnes 
116879e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev)
116979e53945SJesse Barnes {
117079e53945SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
1171e170b030SZhenyu Wang 
1172bad720ffSEric Anholt 	if (!HAS_PCH_SPLIT(dev))
117379e53945SJesse Barnes 		opregion_enable_asle(dev);
117479e53945SJesse Barnes 	dev_priv->irq_enabled = 1;
117579e53945SJesse Barnes }
117679e53945SJesse Barnes 
117779e53945SJesse Barnes 
1178c0e09200SDave Airlie /* Set the vblank monitor pipe
1179c0e09200SDave Airlie  */
1180c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1181c0e09200SDave Airlie 			 struct drm_file *file_priv)
1182c0e09200SDave Airlie {
1183c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1184c0e09200SDave Airlie 
1185c0e09200SDave Airlie 	if (!dev_priv) {
1186c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1187c0e09200SDave Airlie 		return -EINVAL;
1188c0e09200SDave Airlie 	}
1189c0e09200SDave Airlie 
1190c0e09200SDave Airlie 	return 0;
1191c0e09200SDave Airlie }
1192c0e09200SDave Airlie 
1193c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1194c0e09200SDave Airlie 			 struct drm_file *file_priv)
1195c0e09200SDave Airlie {
1196c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
1197c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
1198c0e09200SDave Airlie 
1199c0e09200SDave Airlie 	if (!dev_priv) {
1200c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
1201c0e09200SDave Airlie 		return -EINVAL;
1202c0e09200SDave Airlie 	}
1203c0e09200SDave Airlie 
12040a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1205c0e09200SDave Airlie 
1206c0e09200SDave Airlie 	return 0;
1207c0e09200SDave Airlie }
1208c0e09200SDave Airlie 
1209c0e09200SDave Airlie /**
1210c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
1211c0e09200SDave Airlie  */
1212c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
1213c0e09200SDave Airlie 		     struct drm_file *file_priv)
1214c0e09200SDave Airlie {
1215bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1216bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1217bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1218bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1219bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1220bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1221bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1222bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1223bd95e0a4SEric Anholt 	 *
1224bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1225bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1226bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1227bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
12280a3e67a4SJesse Barnes 	 */
1229c0e09200SDave Airlie 	return -EINVAL;
1230c0e09200SDave Airlie }
1231c0e09200SDave Airlie 
1232f65d9421SBen Gamari struct drm_i915_gem_request *i915_get_tail_request(struct drm_device *dev) {
1233f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1234f65d9421SBen Gamari 	return list_entry(dev_priv->mm.request_list.prev, struct drm_i915_gem_request, list);
1235f65d9421SBen Gamari }
1236f65d9421SBen Gamari 
1237f65d9421SBen Gamari /**
1238f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1239f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1240f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1241f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1242f65d9421SBen Gamari  */
1243f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1244f65d9421SBen Gamari {
1245f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1246f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1247f65d9421SBen Gamari 	uint32_t acthd;
1248f65d9421SBen Gamari 
1249b9201c14SEric Anholt 	/* No reset support on this chip yet. */
1250b9201c14SEric Anholt 	if (IS_GEN6(dev))
1251b9201c14SEric Anholt 		return;
1252b9201c14SEric Anholt 
1253f65d9421SBen Gamari 	if (!IS_I965G(dev))
1254f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
1255f65d9421SBen Gamari 	else
1256f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
1257f65d9421SBen Gamari 
1258f65d9421SBen Gamari 	/* If all work is done then ACTHD clearly hasn't advanced. */
1259f65d9421SBen Gamari 	if (list_empty(&dev_priv->mm.request_list) ||
1260f65d9421SBen Gamari 		       i915_seqno_passed(i915_get_gem_seqno(dev), i915_get_tail_request(dev)->seqno)) {
1261f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
1262f65d9421SBen Gamari 		return;
1263f65d9421SBen Gamari 	}
1264f65d9421SBen Gamari 
1265f65d9421SBen Gamari 	if (dev_priv->last_acthd == acthd && dev_priv->hangcheck_count > 0) {
1266f65d9421SBen Gamari 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1267ba1234d1SBen Gamari 		i915_handle_error(dev, true);
1268f65d9421SBen Gamari 		return;
1269f65d9421SBen Gamari 	}
1270f65d9421SBen Gamari 
1271f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1272f65d9421SBen Gamari 	mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1273f65d9421SBen Gamari 
1274f65d9421SBen Gamari 	if (acthd != dev_priv->last_acthd)
1275f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
1276f65d9421SBen Gamari 	else
1277f65d9421SBen Gamari 		dev_priv->hangcheck_count++;
1278f65d9421SBen Gamari 
1279f65d9421SBen Gamari 	dev_priv->last_acthd = acthd;
1280f65d9421SBen Gamari }
1281f65d9421SBen Gamari 
1282c0e09200SDave Airlie /* drm_dma.h hooks
1283c0e09200SDave Airlie */
1284f2b115e6SAdam Jackson static void ironlake_irq_preinstall(struct drm_device *dev)
1285036a4a7dSZhenyu Wang {
1286036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1287036a4a7dSZhenyu Wang 
1288036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1289036a4a7dSZhenyu Wang 
1290036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1291036a4a7dSZhenyu Wang 
1292036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1293036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1294036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
1295036a4a7dSZhenyu Wang 
1296036a4a7dSZhenyu Wang 	/* and GT */
1297036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1298036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1299036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
1300c650156aSZhenyu Wang 
1301c650156aSZhenyu Wang 	/* south display irq */
1302c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1303c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
1304c650156aSZhenyu Wang 	(void) I915_READ(SDEIER);
1305036a4a7dSZhenyu Wang }
1306036a4a7dSZhenyu Wang 
1307f2b115e6SAdam Jackson static int ironlake_irq_postinstall(struct drm_device *dev)
1308036a4a7dSZhenyu Wang {
1309036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1310036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1311013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1312013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
1313036a4a7dSZhenyu Wang 	u32 render_mask = GT_USER_INTERRUPT;
1314c650156aSZhenyu Wang 	u32 hotplug_mask = SDE_CRT_HOTPLUG | SDE_PORTB_HOTPLUG |
1315c650156aSZhenyu Wang 			   SDE_PORTC_HOTPLUG | SDE_PORTD_HOTPLUG;
1316036a4a7dSZhenyu Wang 
1317036a4a7dSZhenyu Wang 	dev_priv->irq_mask_reg = ~display_mask;
1318643ced9bSLi Peng 	dev_priv->de_irq_enable_reg = display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK;
1319036a4a7dSZhenyu Wang 
1320036a4a7dSZhenyu Wang 	/* should always can generate irq */
1321036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1322036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
1323036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
1324036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
1325036a4a7dSZhenyu Wang 
1326036a4a7dSZhenyu Wang 	/* user interrupt should be enabled, but masked initial */
1327036a4a7dSZhenyu Wang 	dev_priv->gt_irq_mask_reg = 0xffffffff;
1328036a4a7dSZhenyu Wang 	dev_priv->gt_irq_enable_reg = render_mask;
1329036a4a7dSZhenyu Wang 
1330036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1331036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
1332036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
1333036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
1334036a4a7dSZhenyu Wang 
1335c650156aSZhenyu Wang 	dev_priv->pch_irq_mask_reg = ~hotplug_mask;
1336c650156aSZhenyu Wang 	dev_priv->pch_irq_enable_reg = hotplug_mask;
1337c650156aSZhenyu Wang 
1338c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1339c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask_reg);
1340c650156aSZhenyu Wang 	I915_WRITE(SDEIER, dev_priv->pch_irq_enable_reg);
1341c650156aSZhenyu Wang 	(void) I915_READ(SDEIER);
1342c650156aSZhenyu Wang 
1343f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1344f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1345f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1346f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1347f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1348f97108d1SJesse Barnes 	}
1349f97108d1SJesse Barnes 
1350036a4a7dSZhenyu Wang 	return 0;
1351036a4a7dSZhenyu Wang }
1352036a4a7dSZhenyu Wang 
1353c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1354c0e09200SDave Airlie {
1355c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1356c0e09200SDave Airlie 
135779e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
135879e53945SJesse Barnes 
1359036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
13608a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1361036a4a7dSZhenyu Wang 
1362bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1363f2b115e6SAdam Jackson 		ironlake_irq_preinstall(dev);
1364036a4a7dSZhenyu Wang 		return;
1365036a4a7dSZhenyu Wang 	}
1366036a4a7dSZhenyu Wang 
13675ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
13685ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
13695ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
13705ca58282SJesse Barnes 	}
13715ca58282SJesse Barnes 
13720a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
13737c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
13747c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
13750a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1376ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
13777c463586SKeith Packard 	(void) I915_READ(IER);
1378c0e09200SDave Airlie }
1379c0e09200SDave Airlie 
1380b01f2c3aSJesse Barnes /*
1381b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
1382b01f2c3aSJesse Barnes  * enabled correctly.
1383b01f2c3aSJesse Barnes  */
13840a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1385c0e09200SDave Airlie {
1386c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
13875ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
138863eeaf38SJesse Barnes 	u32 error_mask;
13890a3e67a4SJesse Barnes 
1390036a4a7dSZhenyu Wang 	DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
1391036a4a7dSZhenyu Wang 
13920a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1393ed4cb414SEric Anholt 
1394bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev))
1395f2b115e6SAdam Jackson 		return ironlake_irq_postinstall(dev);
1396036a4a7dSZhenyu Wang 
13977c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
13987c463586SKeith Packard 	dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
13998ee1c3dbSMatthew Garrett 
14007c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
14017c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
14027c463586SKeith Packard 
14035ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
14045ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
14055ca58282SJesse Barnes 
1406b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
1407b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1408b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1409b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1410b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1411b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1412b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
1413b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1414b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1415b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1416b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
1417b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS)
1418b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
1419b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
1420b01f2c3aSJesse Barnes 
14215ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
14225ca58282SJesse Barnes 
14235ca58282SJesse Barnes 		/* Enable in IER... */
14245ca58282SJesse Barnes 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
14255ca58282SJesse Barnes 		/* and unmask in IMR */
14265ca58282SJesse Barnes 		i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
14275ca58282SJesse Barnes 	}
14285ca58282SJesse Barnes 
142963eeaf38SJesse Barnes 	/*
143063eeaf38SJesse Barnes 	 * Enable some error detection, note the instruction error mask
143163eeaf38SJesse Barnes 	 * bit is reserved, so we leave it masked.
143263eeaf38SJesse Barnes 	 */
143363eeaf38SJesse Barnes 	if (IS_G4X(dev)) {
143463eeaf38SJesse Barnes 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
143563eeaf38SJesse Barnes 			       GM45_ERROR_MEM_PRIV |
143663eeaf38SJesse Barnes 			       GM45_ERROR_CP_PRIV |
143763eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
143863eeaf38SJesse Barnes 	} else {
143963eeaf38SJesse Barnes 		error_mask = ~(I915_ERROR_PAGE_TABLE |
144063eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
144163eeaf38SJesse Barnes 	}
144263eeaf38SJesse Barnes 	I915_WRITE(EMR, error_mask);
144363eeaf38SJesse Barnes 
14447c463586SKeith Packard 	/* Disable pipe interrupt enables, clear pending pipe status */
14457c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
14467c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
14477c463586SKeith Packard 	/* Clear pending interrupt status */
14487c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
14497c463586SKeith Packard 
14505ca58282SJesse Barnes 	I915_WRITE(IER, enable_mask);
14517c463586SKeith Packard 	I915_WRITE(IMR, dev_priv->irq_mask_reg);
1452ed4cb414SEric Anholt 	(void) I915_READ(IER);
1453ed4cb414SEric Anholt 
14548ee1c3dbSMatthew Garrett 	opregion_enable_asle(dev);
14550a3e67a4SJesse Barnes 
14560a3e67a4SJesse Barnes 	return 0;
1457c0e09200SDave Airlie }
1458c0e09200SDave Airlie 
1459f2b115e6SAdam Jackson static void ironlake_irq_uninstall(struct drm_device *dev)
1460036a4a7dSZhenyu Wang {
1461036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1462036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1463036a4a7dSZhenyu Wang 
1464036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1465036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1466036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1467036a4a7dSZhenyu Wang 
1468036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1469036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1470036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1471036a4a7dSZhenyu Wang }
1472036a4a7dSZhenyu Wang 
1473c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1474c0e09200SDave Airlie {
1475c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1476c0e09200SDave Airlie 
1477c0e09200SDave Airlie 	if (!dev_priv)
1478c0e09200SDave Airlie 		return;
1479c0e09200SDave Airlie 
14800a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
14810a3e67a4SJesse Barnes 
1482bad720ffSEric Anholt 	if (HAS_PCH_SPLIT(dev)) {
1483f2b115e6SAdam Jackson 		ironlake_irq_uninstall(dev);
1484036a4a7dSZhenyu Wang 		return;
1485036a4a7dSZhenyu Wang 	}
1486036a4a7dSZhenyu Wang 
14875ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
14885ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
14895ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
14905ca58282SJesse Barnes 	}
14915ca58282SJesse Barnes 
14920a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
14937c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
14947c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
14950a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1496ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1497c0e09200SDave Airlie 
14987c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
14997c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
15007c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1501c0e09200SDave Airlie }
1502