xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision b5b72e891a5a6056c849ef8eaf259f126090f88b)
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>
30c0e09200SDave Airlie #include "drmP.h"
31c0e09200SDave Airlie #include "drm.h"
32c0e09200SDave Airlie #include "i915_drm.h"
33c0e09200SDave Airlie #include "i915_drv.h"
341c5d22f7SChris Wilson #include "i915_trace.h"
3579e53945SJesse Barnes #include "intel_drv.h"
36c0e09200SDave Airlie 
37c0e09200SDave Airlie #define MAX_NOPID ((u32)~0)
38c0e09200SDave Airlie 
397c463586SKeith Packard /**
407c463586SKeith Packard  * Interrupts that are always left unmasked.
417c463586SKeith Packard  *
427c463586SKeith Packard  * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
437c463586SKeith Packard  * we leave them always unmasked in IMR and then control enabling them through
447c463586SKeith Packard  * PIPESTAT alone.
457c463586SKeith Packard  */
466b95a207SKristian Høgsberg #define I915_INTERRUPT_ENABLE_FIX			\
476b95a207SKristian Høgsberg 	(I915_ASLE_INTERRUPT |				\
480a3e67a4SJesse Barnes 	 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |		\
4963eeaf38SJesse Barnes 	 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |		\
506b95a207SKristian Høgsberg 	 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |	\
516b95a207SKristian Høgsberg 	 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |	\
5263eeaf38SJesse Barnes 	 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
53ed4cb414SEric Anholt 
547c463586SKeith Packard /** Interrupts that we mask and unmask at runtime. */
557c463586SKeith Packard #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
567c463586SKeith Packard 
5779e53945SJesse Barnes #define I915_PIPE_VBLANK_STATUS	(PIPE_START_VBLANK_INTERRUPT_STATUS |\
5879e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_STATUS)
5979e53945SJesse Barnes 
6079e53945SJesse Barnes #define I915_PIPE_VBLANK_ENABLE	(PIPE_START_VBLANK_INTERRUPT_ENABLE |\
6179e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_ENABLE)
6279e53945SJesse Barnes 
6379e53945SJesse Barnes #define DRM_I915_VBLANK_PIPE_ALL	(DRM_I915_VBLANK_PIPE_A | \
6479e53945SJesse Barnes 					 DRM_I915_VBLANK_PIPE_B)
6579e53945SJesse Barnes 
668ee1c3dbSMatthew Garrett void
67f2b115e6SAdam Jackson ironlake_enable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
68036a4a7dSZhenyu Wang {
69036a4a7dSZhenyu Wang 	if ((dev_priv->gt_irq_mask_reg & mask) != 0) {
70036a4a7dSZhenyu Wang 		dev_priv->gt_irq_mask_reg &= ~mask;
71036a4a7dSZhenyu Wang 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
72036a4a7dSZhenyu Wang 		(void) I915_READ(GTIMR);
73036a4a7dSZhenyu Wang 	}
74036a4a7dSZhenyu Wang }
75036a4a7dSZhenyu Wang 
76036a4a7dSZhenyu Wang static inline void
77f2b115e6SAdam Jackson ironlake_disable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
78036a4a7dSZhenyu Wang {
79036a4a7dSZhenyu Wang 	if ((dev_priv->gt_irq_mask_reg & mask) != mask) {
80036a4a7dSZhenyu Wang 		dev_priv->gt_irq_mask_reg |= mask;
81036a4a7dSZhenyu Wang 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
82036a4a7dSZhenyu Wang 		(void) I915_READ(GTIMR);
83036a4a7dSZhenyu Wang 	}
84036a4a7dSZhenyu Wang }
85036a4a7dSZhenyu Wang 
86036a4a7dSZhenyu Wang /* For display hotplug interrupt */
87036a4a7dSZhenyu Wang void
88f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
89036a4a7dSZhenyu Wang {
90036a4a7dSZhenyu Wang 	if ((dev_priv->irq_mask_reg & mask) != 0) {
91036a4a7dSZhenyu Wang 		dev_priv->irq_mask_reg &= ~mask;
92036a4a7dSZhenyu Wang 		I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
93036a4a7dSZhenyu Wang 		(void) I915_READ(DEIMR);
94036a4a7dSZhenyu Wang 	}
95036a4a7dSZhenyu Wang }
96036a4a7dSZhenyu Wang 
97036a4a7dSZhenyu Wang static inline void
98f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
99036a4a7dSZhenyu Wang {
100036a4a7dSZhenyu Wang 	if ((dev_priv->irq_mask_reg & mask) != mask) {
101036a4a7dSZhenyu Wang 		dev_priv->irq_mask_reg |= mask;
102036a4a7dSZhenyu Wang 		I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
103036a4a7dSZhenyu Wang 		(void) I915_READ(DEIMR);
104036a4a7dSZhenyu Wang 	}
105036a4a7dSZhenyu Wang }
106036a4a7dSZhenyu Wang 
107036a4a7dSZhenyu Wang void
108ed4cb414SEric Anholt i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
109ed4cb414SEric Anholt {
110ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != 0) {
111ed4cb414SEric Anholt 		dev_priv->irq_mask_reg &= ~mask;
112ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
113ed4cb414SEric Anholt 		(void) I915_READ(IMR);
114ed4cb414SEric Anholt 	}
115ed4cb414SEric Anholt }
116ed4cb414SEric Anholt 
117ed4cb414SEric Anholt static inline void
118ed4cb414SEric Anholt i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
119ed4cb414SEric Anholt {
120ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != mask) {
121ed4cb414SEric Anholt 		dev_priv->irq_mask_reg |= mask;
122ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
123ed4cb414SEric Anholt 		(void) I915_READ(IMR);
124ed4cb414SEric Anholt 	}
125ed4cb414SEric Anholt }
126ed4cb414SEric Anholt 
1277c463586SKeith Packard static inline u32
1287c463586SKeith Packard i915_pipestat(int pipe)
1297c463586SKeith Packard {
1307c463586SKeith Packard 	if (pipe == 0)
1317c463586SKeith Packard 		return PIPEASTAT;
1327c463586SKeith Packard 	if (pipe == 1)
1337c463586SKeith Packard 		return PIPEBSTAT;
1349c84ba4eSAndrew Morton 	BUG();
1357c463586SKeith Packard }
1367c463586SKeith Packard 
1377c463586SKeith Packard void
1387c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1397c463586SKeith Packard {
1407c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
1417c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1427c463586SKeith Packard 
1437c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
1447c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
1457c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
1467c463586SKeith Packard 		(void) I915_READ(reg);
1477c463586SKeith Packard 	}
1487c463586SKeith Packard }
1497c463586SKeith Packard 
1507c463586SKeith Packard void
1517c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1527c463586SKeith Packard {
1537c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
1547c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1557c463586SKeith Packard 
1567c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
1577c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
1587c463586SKeith Packard 		(void) I915_READ(reg);
1597c463586SKeith Packard 	}
1607c463586SKeith Packard }
1617c463586SKeith Packard 
162c0e09200SDave Airlie /**
16301c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
16401c66889SZhao Yakui  */
16501c66889SZhao Yakui void intel_enable_asle (struct drm_device *dev)
16601c66889SZhao Yakui {
16701c66889SZhao Yakui 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16801c66889SZhao Yakui 
169f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev))
170f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
17101c66889SZhao Yakui 	else
17201c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
17301c66889SZhao Yakui 				     I915_LEGACY_BLC_EVENT_ENABLE);
17401c66889SZhao Yakui }
17501c66889SZhao Yakui 
17601c66889SZhao Yakui /**
1770a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1780a3e67a4SJesse Barnes  * @dev: DRM device
1790a3e67a4SJesse Barnes  * @pipe: pipe to check
1800a3e67a4SJesse Barnes  *
1810a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1820a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1830a3e67a4SJesse Barnes  * before reading such registers if unsure.
1840a3e67a4SJesse Barnes  */
1850a3e67a4SJesse Barnes static int
1860a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1870a3e67a4SJesse Barnes {
1880a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1890a3e67a4SJesse Barnes 	unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
1900a3e67a4SJesse Barnes 
1910a3e67a4SJesse Barnes 	if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
1920a3e67a4SJesse Barnes 		return 1;
1930a3e67a4SJesse Barnes 
1940a3e67a4SJesse Barnes 	return 0;
1950a3e67a4SJesse Barnes }
1960a3e67a4SJesse Barnes 
19742f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
19842f52ef8SKeith Packard  * we use as a pipe index
19942f52ef8SKeith Packard  */
20042f52ef8SKeith Packard u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
2010a3e67a4SJesse Barnes {
2020a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2030a3e67a4SJesse Barnes 	unsigned long high_frame;
2040a3e67a4SJesse Barnes 	unsigned long low_frame;
2050a3e67a4SJesse Barnes 	u32 high1, high2, low, count;
2060a3e67a4SJesse Barnes 
2070a3e67a4SJesse Barnes 	high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
2080a3e67a4SJesse Barnes 	low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
2090a3e67a4SJesse Barnes 
2100a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
21144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
21244d98a61SZhao Yakui 				"pipe %d\n", pipe);
2130a3e67a4SJesse Barnes 		return 0;
2140a3e67a4SJesse Barnes 	}
2150a3e67a4SJesse Barnes 
2160a3e67a4SJesse Barnes 	/*
2170a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
2180a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
2190a3e67a4SJesse Barnes 	 * register.
2200a3e67a4SJesse Barnes 	 */
2210a3e67a4SJesse Barnes 	do {
2220a3e67a4SJesse Barnes 		high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
2230a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
2240a3e67a4SJesse Barnes 		low =  ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
2250a3e67a4SJesse Barnes 			PIPE_FRAME_LOW_SHIFT);
2260a3e67a4SJesse Barnes 		high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
2270a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
2280a3e67a4SJesse Barnes 	} while (high1 != high2);
2290a3e67a4SJesse Barnes 
2300a3e67a4SJesse Barnes 	count = (high1 << 8) | low;
2310a3e67a4SJesse Barnes 
2320a3e67a4SJesse Barnes 	return count;
2330a3e67a4SJesse Barnes }
2340a3e67a4SJesse Barnes 
2359880b7a5SJesse Barnes u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
2369880b7a5SJesse Barnes {
2379880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2389880b7a5SJesse Barnes 	int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
2399880b7a5SJesse Barnes 
2409880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
24144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
24244d98a61SZhao Yakui 					"pipe %d\n", pipe);
2439880b7a5SJesse Barnes 		return 0;
2449880b7a5SJesse Barnes 	}
2459880b7a5SJesse Barnes 
2469880b7a5SJesse Barnes 	return I915_READ(reg);
2479880b7a5SJesse Barnes }
2489880b7a5SJesse Barnes 
2495ca58282SJesse Barnes /*
2505ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
2515ca58282SJesse Barnes  */
2525ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
2535ca58282SJesse Barnes {
2545ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
2555ca58282SJesse Barnes 						    hotplug_work);
2565ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
257c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
258c31c4ba3SKeith Packard 	struct drm_connector *connector;
2595ca58282SJesse Barnes 
260c31c4ba3SKeith Packard 	if (mode_config->num_connector) {
261c31c4ba3SKeith Packard 		list_for_each_entry(connector, &mode_config->connector_list, head) {
262c31c4ba3SKeith Packard 			struct intel_output *intel_output = to_intel_output(connector);
263c31c4ba3SKeith Packard 
264c31c4ba3SKeith Packard 			if (intel_output->hot_plug)
265c31c4ba3SKeith Packard 				(*intel_output->hot_plug) (intel_output);
266c31c4ba3SKeith Packard 		}
267c31c4ba3SKeith Packard 	}
2685ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
2695ca58282SJesse Barnes 	drm_sysfs_hotplug_event(dev);
2705ca58282SJesse Barnes }
2715ca58282SJesse Barnes 
272f97108d1SJesse Barnes static void i915_handle_rps_change(struct drm_device *dev)
273f97108d1SJesse Barnes {
274f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
275*b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
276f97108d1SJesse Barnes 	u16 rgvswctl;
277f97108d1SJesse Barnes 	u8 new_delay = dev_priv->cur_delay;
278f97108d1SJesse Barnes 
279f97108d1SJesse Barnes 	I915_WRITE(MEMINTRSTS, I915_READ(MEMINTRSTS) & ~MEMINT_EVAL_CHG);
280*b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
281*b5b72e89SMatthew 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 */
286*b5b72e89SMatthew 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;
291*b5b72e89SMatthew 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 
298f97108d1SJesse Barnes 	DRM_DEBUG("rps change requested: %d -> %d\n",
299f97108d1SJesse Barnes 		  dev_priv->cur_delay, new_delay);
300f97108d1SJesse Barnes 
301f97108d1SJesse Barnes 	rgvswctl = I915_READ(MEMSWCTL);
302f97108d1SJesse Barnes 	if (rgvswctl & MEMCTL_CMD_STS) {
303*b5b72e89SMatthew Garrett 		DRM_ERROR("gpu busy, RCS change rejected\n");
304*b5b72e89SMatthew Garrett 		return; /* still busy with another command */
305f97108d1SJesse Barnes 	}
306f97108d1SJesse Barnes 
307f97108d1SJesse Barnes 	/* Program the new state */
308f97108d1SJesse Barnes 	rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
309f97108d1SJesse Barnes 		(new_delay << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
310f97108d1SJesse Barnes 	I915_WRITE(MEMSWCTL, rgvswctl);
311f97108d1SJesse Barnes 	POSTING_READ(MEMSWCTL);
312f97108d1SJesse Barnes 
313f97108d1SJesse Barnes 	rgvswctl |= MEMCTL_CMD_STS;
314f97108d1SJesse Barnes 	I915_WRITE(MEMSWCTL, rgvswctl);
315f97108d1SJesse Barnes 
316f97108d1SJesse Barnes 	dev_priv->cur_delay = new_delay;
317f97108d1SJesse Barnes 
318f97108d1SJesse Barnes 	DRM_DEBUG("rps changed\n");
319f97108d1SJesse Barnes 
320f97108d1SJesse Barnes 	return;
321f97108d1SJesse Barnes }
322f97108d1SJesse Barnes 
323f2b115e6SAdam Jackson irqreturn_t ironlake_irq_handler(struct drm_device *dev)
324036a4a7dSZhenyu Wang {
325036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
326036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
3273ff99164SDave Airlie 	u32 de_iir, gt_iir, de_ier, pch_iir;
328036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
329036a4a7dSZhenyu Wang 
3302d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
3312d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
3322d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
3332d109a84SZou, Nanhai 	(void)I915_READ(DEIER);
3342d109a84SZou, Nanhai 
335036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
336036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
337c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
338036a4a7dSZhenyu Wang 
339c650156aSZhenyu Wang 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0)
340c7c85101SZou Nan hai 		goto done;
341036a4a7dSZhenyu Wang 
342036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
343036a4a7dSZhenyu Wang 
344036a4a7dSZhenyu Wang 	if (dev->primary->master) {
345036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
346036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
347036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
348036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
349036a4a7dSZhenyu Wang 	}
350036a4a7dSZhenyu Wang 
351036a4a7dSZhenyu Wang 	if (gt_iir & GT_USER_INTERRUPT) {
3521c5d22f7SChris Wilson 		u32 seqno = i915_get_gem_seqno(dev);
3531c5d22f7SChris Wilson 		dev_priv->mm.irq_gem_seqno = seqno;
3541c5d22f7SChris Wilson 		trace_i915_gem_request_complete(dev, seqno);
355036a4a7dSZhenyu Wang 		DRM_WAKEUP(&dev_priv->irq_queue);
356c566ec49SZhenyu Wang 		dev_priv->hangcheck_count = 0;
357c566ec49SZhenyu Wang 		mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
358036a4a7dSZhenyu Wang 	}
359036a4a7dSZhenyu Wang 
36001c66889SZhao Yakui 	if (de_iir & DE_GSE)
36101c66889SZhao Yakui 		ironlake_opregion_gse_intr(dev);
36201c66889SZhao Yakui 
363f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEA_FLIP_DONE) {
364013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
365013d5aa2SJesse Barnes 		intel_finish_page_flip(dev, 0);
366013d5aa2SJesse Barnes 	}
367013d5aa2SJesse Barnes 
368f072d2e7SZhenyu Wang 	if (de_iir & DE_PLANEB_FLIP_DONE) {
369f072d2e7SZhenyu Wang 		intel_prepare_page_flip(dev, 1);
370013d5aa2SJesse Barnes 		intel_finish_page_flip(dev, 1);
371013d5aa2SJesse Barnes 	}
372c062df61SLi Peng 
373f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEA_VBLANK)
374f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 0);
375f072d2e7SZhenyu Wang 
376f072d2e7SZhenyu Wang 	if (de_iir & DE_PIPEB_VBLANK)
377f072d2e7SZhenyu Wang 		drm_handle_vblank(dev, 1);
378f072d2e7SZhenyu Wang 
379c650156aSZhenyu Wang 	/* check event from PCH */
380c650156aSZhenyu Wang 	if ((de_iir & DE_PCH_EVENT) &&
381c650156aSZhenyu Wang 	    (pch_iir & SDE_HOTPLUG_MASK)) {
382c650156aSZhenyu Wang 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
383c650156aSZhenyu Wang 	}
384c650156aSZhenyu Wang 
385f97108d1SJesse Barnes 	if (de_iir & DE_PCU_EVENT) {
386f97108d1SJesse Barnes 		I915_WRITE(MEMINTRSTS, I915_READ(MEMINTRSTS));
387f97108d1SJesse Barnes 		i915_handle_rps_change(dev);
388f97108d1SJesse Barnes 	}
389f97108d1SJesse Barnes 
390c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
391c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
392c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
393c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
394036a4a7dSZhenyu Wang 
395c7c85101SZou Nan hai done:
3962d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
3972d109a84SZou, Nanhai 	(void)I915_READ(DEIER);
3982d109a84SZou, Nanhai 
399036a4a7dSZhenyu Wang 	return ret;
400036a4a7dSZhenyu Wang }
401036a4a7dSZhenyu Wang 
4028a905236SJesse Barnes /**
4038a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
4048a905236SJesse Barnes  * @work: work struct
4058a905236SJesse Barnes  *
4068a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
4078a905236SJesse Barnes  * was detected.
4088a905236SJesse Barnes  */
4098a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
4108a905236SJesse Barnes {
4118a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
4128a905236SJesse Barnes 						    error_work);
4138a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
414f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
415f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
416f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
4178a905236SJesse Barnes 
41844d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("generating error event\n");
419f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
4208a905236SJesse Barnes 
421ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
422f316a42cSBen Gamari 		if (IS_I965G(dev)) {
42344d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("resetting chip\n");
424f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
425f316a42cSBen Gamari 			if (!i965_reset(dev, GDRST_RENDER)) {
426ba1234d1SBen Gamari 				atomic_set(&dev_priv->mm.wedged, 0);
427f316a42cSBen Gamari 				kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
428f316a42cSBen Gamari 			}
429f316a42cSBen Gamari 		} else {
43044d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("reboot required\n");
431f316a42cSBen Gamari 		}
432f316a42cSBen Gamari 	}
4338a905236SJesse Barnes }
4348a905236SJesse Barnes 
4358a905236SJesse Barnes /**
4368a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
4378a905236SJesse Barnes  * @dev: drm device
4388a905236SJesse Barnes  *
4398a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
4408a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
4418a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
4428a905236SJesse Barnes  * to pick up.
4438a905236SJesse Barnes  */
44463eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
44563eeaf38SJesse Barnes {
44663eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
44763eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
44863eeaf38SJesse Barnes 	unsigned long flags;
44963eeaf38SJesse Barnes 
45063eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
45163eeaf38SJesse Barnes 	if (dev_priv->first_error)
45263eeaf38SJesse Barnes 		goto out;
45363eeaf38SJesse Barnes 
45463eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
45563eeaf38SJesse Barnes 	if (!error) {
45644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("out ot memory, not capturing error state\n");
45763eeaf38SJesse Barnes 		goto out;
45863eeaf38SJesse Barnes 	}
45963eeaf38SJesse Barnes 
46063eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
46163eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
46263eeaf38SJesse Barnes 	error->pipeastat = I915_READ(PIPEASTAT);
46363eeaf38SJesse Barnes 	error->pipebstat = I915_READ(PIPEBSTAT);
46463eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
46563eeaf38SJesse Barnes 	if (!IS_I965G(dev)) {
46663eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR);
46763eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR);
46863eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE);
46963eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD);
47063eeaf38SJesse Barnes 	} else {
47163eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
47263eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
47363eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
47463eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
47563eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
47663eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
47763eeaf38SJesse Barnes 	}
47863eeaf38SJesse Barnes 
4798a905236SJesse Barnes 	do_gettimeofday(&error->time);
4808a905236SJesse Barnes 
48163eeaf38SJesse Barnes 	dev_priv->first_error = error;
48263eeaf38SJesse Barnes 
48363eeaf38SJesse Barnes out:
48463eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
48563eeaf38SJesse Barnes }
48663eeaf38SJesse Barnes 
4878a905236SJesse Barnes /**
4888a905236SJesse Barnes  * i915_handle_error - handle an error interrupt
4898a905236SJesse Barnes  * @dev: drm device
4908a905236SJesse Barnes  *
4918a905236SJesse Barnes  * Do some basic checking of regsiter state at error interrupt time and
4928a905236SJesse Barnes  * dump it to the syslog.  Also call i915_capture_error_state() to make
4938a905236SJesse Barnes  * sure we get a record and make it available in debugfs.  Fire a uevent
4948a905236SJesse Barnes  * so userspace knows something bad happened (should trigger collection
4958a905236SJesse Barnes  * of a ring dump etc.).
4968a905236SJesse Barnes  */
497ba1234d1SBen Gamari static void i915_handle_error(struct drm_device *dev, bool wedged)
498c0e09200SDave Airlie {
4998a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
50063eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
5018a905236SJesse Barnes 	u32 pipea_stats = I915_READ(PIPEASTAT);
5028a905236SJesse Barnes 	u32 pipeb_stats = I915_READ(PIPEBSTAT);
50363eeaf38SJesse Barnes 
50463eeaf38SJesse Barnes 	i915_capture_error_state(dev);
50563eeaf38SJesse Barnes 
50663eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
50763eeaf38SJesse Barnes 	       eir);
5088a905236SJesse Barnes 
5098a905236SJesse Barnes 	if (IS_G4X(dev)) {
5108a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
5118a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
5128a905236SJesse Barnes 
5138a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
5148a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
5158a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
5168a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
5178a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
5188a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
5198a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
5208a905236SJesse Barnes 			       I915_READ(INSTPS));
5218a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
5228a905236SJesse Barnes 			       I915_READ(INSTDONE1));
5238a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
5248a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
5258a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
5268a905236SJesse Barnes 			(void)I915_READ(IPEIR_I965);
5278a905236SJesse Barnes 		}
5288a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
5298a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
5308a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
5318a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
5328a905236SJesse Barnes 			       pgtbl_err);
5338a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
5348a905236SJesse Barnes 			(void)I915_READ(PGTBL_ER);
5358a905236SJesse Barnes 		}
5368a905236SJesse Barnes 	}
5378a905236SJesse Barnes 
5388a905236SJesse Barnes 	if (IS_I9XX(dev)) {
53963eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
54063eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
54163eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
54263eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
54363eeaf38SJesse Barnes 			       pgtbl_err);
54463eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
54563eeaf38SJesse Barnes 			(void)I915_READ(PGTBL_ER);
54663eeaf38SJesse Barnes 		}
5478a905236SJesse Barnes 	}
5488a905236SJesse Barnes 
54963eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
55063eeaf38SJesse Barnes 		printk(KERN_ERR "memory refresh error\n");
55163eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
55263eeaf38SJesse Barnes 		       pipea_stats);
55363eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
55463eeaf38SJesse Barnes 		       pipeb_stats);
55563eeaf38SJesse Barnes 		/* pipestat has already been acked */
55663eeaf38SJesse Barnes 	}
55763eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
55863eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
55963eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
56063eeaf38SJesse Barnes 		       I915_READ(INSTPM));
56163eeaf38SJesse Barnes 		if (!IS_I965G(dev)) {
56263eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
56363eeaf38SJesse Barnes 
56463eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
56563eeaf38SJesse Barnes 			       I915_READ(IPEIR));
56663eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
56763eeaf38SJesse Barnes 			       I915_READ(IPEHR));
56863eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
56963eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
57063eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
57163eeaf38SJesse Barnes 			       I915_READ(ACTHD));
57263eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
57363eeaf38SJesse Barnes 			(void)I915_READ(IPEIR);
57463eeaf38SJesse Barnes 		} else {
57563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
57663eeaf38SJesse Barnes 
57763eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
57863eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
57963eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
58063eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
58163eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
58263eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
58363eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
58463eeaf38SJesse Barnes 			       I915_READ(INSTPS));
58563eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
58663eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
58763eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
58863eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
58963eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
59063eeaf38SJesse Barnes 			(void)I915_READ(IPEIR_I965);
59163eeaf38SJesse Barnes 		}
59263eeaf38SJesse Barnes 	}
59363eeaf38SJesse Barnes 
59463eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
59563eeaf38SJesse Barnes 	(void)I915_READ(EIR);
59663eeaf38SJesse Barnes 	eir = I915_READ(EIR);
59763eeaf38SJesse Barnes 	if (eir) {
59863eeaf38SJesse Barnes 		/*
59963eeaf38SJesse Barnes 		 * some errors might have become stuck,
60063eeaf38SJesse Barnes 		 * mask them.
60163eeaf38SJesse Barnes 		 */
60263eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
60363eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
60463eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
60563eeaf38SJesse Barnes 	}
6068a905236SJesse Barnes 
607ba1234d1SBen Gamari 	if (wedged) {
608ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
609ba1234d1SBen Gamari 
61011ed50ecSBen Gamari 		/*
61111ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
61211ed50ecSBen Gamari 		 */
61311ed50ecSBen Gamari 		DRM_WAKEUP(&dev_priv->irq_queue);
61411ed50ecSBen Gamari 	}
61511ed50ecSBen Gamari 
6169c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
6178a905236SJesse Barnes }
6188a905236SJesse Barnes 
6198a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
6208a905236SJesse Barnes {
6218a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
6228a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6238a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
6248a905236SJesse Barnes 	u32 iir, new_iir;
6258a905236SJesse Barnes 	u32 pipea_stats, pipeb_stats;
6268a905236SJesse Barnes 	u32 vblank_status;
6278a905236SJesse Barnes 	u32 vblank_enable;
6288a905236SJesse Barnes 	int vblank = 0;
6298a905236SJesse Barnes 	unsigned long irqflags;
6308a905236SJesse Barnes 	int irq_received;
6318a905236SJesse Barnes 	int ret = IRQ_NONE;
6328a905236SJesse Barnes 
6338a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
6348a905236SJesse Barnes 
635f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev))
636f2b115e6SAdam Jackson 		return ironlake_irq_handler(dev);
6378a905236SJesse Barnes 
6388a905236SJesse Barnes 	iir = I915_READ(IIR);
6398a905236SJesse Barnes 
6408a905236SJesse Barnes 	if (IS_I965G(dev)) {
6418a905236SJesse Barnes 		vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
6428a905236SJesse Barnes 		vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
6438a905236SJesse Barnes 	} else {
6448a905236SJesse Barnes 		vblank_status = I915_VBLANK_INTERRUPT_STATUS;
6458a905236SJesse Barnes 		vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
6468a905236SJesse Barnes 	}
6478a905236SJesse Barnes 
6488a905236SJesse Barnes 	for (;;) {
6498a905236SJesse Barnes 		irq_received = iir != 0;
6508a905236SJesse Barnes 
6518a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
6528a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
6538a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
6548a905236SJesse Barnes 		 * interrupts (for non-MSI).
6558a905236SJesse Barnes 		 */
6568a905236SJesse Barnes 		spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
6578a905236SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
6588a905236SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
6598a905236SJesse Barnes 
6608a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
661ba1234d1SBen Gamari 			i915_handle_error(dev, false);
6628a905236SJesse Barnes 
6638a905236SJesse Barnes 		/*
6648a905236SJesse Barnes 		 * Clear the PIPE(A|B)STAT regs before the IIR
6658a905236SJesse Barnes 		 */
6668a905236SJesse Barnes 		if (pipea_stats & 0x8000ffff) {
6678a905236SJesse Barnes 			if (pipea_stats &  PIPE_FIFO_UNDERRUN_STATUS)
66844d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe a underrun\n");
6698a905236SJesse Barnes 			I915_WRITE(PIPEASTAT, pipea_stats);
6708a905236SJesse Barnes 			irq_received = 1;
6718a905236SJesse Barnes 		}
6728a905236SJesse Barnes 
6738a905236SJesse Barnes 		if (pipeb_stats & 0x8000ffff) {
6748a905236SJesse Barnes 			if (pipeb_stats &  PIPE_FIFO_UNDERRUN_STATUS)
67544d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe b underrun\n");
6768a905236SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
6778a905236SJesse Barnes 			irq_received = 1;
6788a905236SJesse Barnes 		}
6798a905236SJesse Barnes 		spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
6808a905236SJesse Barnes 
6818a905236SJesse Barnes 		if (!irq_received)
6828a905236SJesse Barnes 			break;
6838a905236SJesse Barnes 
6848a905236SJesse Barnes 		ret = IRQ_HANDLED;
6858a905236SJesse Barnes 
6868a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
6878a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
6888a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
6898a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
6908a905236SJesse Barnes 
69144d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
6928a905236SJesse Barnes 				  hotplug_status);
6938a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
6949c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
6959c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
6968a905236SJesse Barnes 
6978a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
6988a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
69963eeaf38SJesse Barnes 		}
70063eeaf38SJesse Barnes 
701673a394bSEric Anholt 		I915_WRITE(IIR, iir);
702cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
7037c463586SKeith Packard 
7047c1c2871SDave Airlie 		if (dev->primary->master) {
7057c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
7067c1c2871SDave Airlie 			if (master_priv->sarea_priv)
7077c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
708c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
7097c1c2871SDave Airlie 		}
7100a3e67a4SJesse Barnes 
711673a394bSEric Anholt 		if (iir & I915_USER_INTERRUPT) {
7121c5d22f7SChris Wilson 			u32 seqno = i915_get_gem_seqno(dev);
7131c5d22f7SChris Wilson 			dev_priv->mm.irq_gem_seqno = seqno;
7141c5d22f7SChris Wilson 			trace_i915_gem_request_complete(dev, seqno);
715673a394bSEric Anholt 			DRM_WAKEUP(&dev_priv->irq_queue);
716f65d9421SBen Gamari 			dev_priv->hangcheck_count = 0;
717f65d9421SBen Gamari 			mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
718673a394bSEric Anholt 		}
719673a394bSEric Anholt 
7206b95a207SKristian Høgsberg 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
7216b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
7226b95a207SKristian Høgsberg 
7236b95a207SKristian Høgsberg 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
7246b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 1);
7256b95a207SKristian Høgsberg 
72605eff845SKeith Packard 		if (pipea_stats & vblank_status) {
7277c463586SKeith Packard 			vblank++;
7287c463586SKeith Packard 			drm_handle_vblank(dev, 0);
7296b95a207SKristian Høgsberg 			intel_finish_page_flip(dev, 0);
7307c463586SKeith Packard 		}
7317c463586SKeith Packard 
73205eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
7337c463586SKeith Packard 			vblank++;
7347c463586SKeith Packard 			drm_handle_vblank(dev, 1);
7356b95a207SKristian Høgsberg 			intel_finish_page_flip(dev, 1);
7367c463586SKeith Packard 		}
7377c463586SKeith Packard 
7387c463586SKeith Packard 		if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
7397c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
740673a394bSEric Anholt 			opregion_asle_intr(dev);
7410a3e67a4SJesse Barnes 
742cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
743cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
744cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
745cdfbc41fSEric Anholt 		 * we would never get another interrupt.
746cdfbc41fSEric Anholt 		 *
747cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
748cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
749cdfbc41fSEric Anholt 		 * another one.
750cdfbc41fSEric Anholt 		 *
751cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
752cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
753cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
754cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
755cdfbc41fSEric Anholt 		 * stray interrupts.
756cdfbc41fSEric Anholt 		 */
757cdfbc41fSEric Anholt 		iir = new_iir;
75805eff845SKeith Packard 	}
759cdfbc41fSEric Anholt 
76005eff845SKeith Packard 	return ret;
761c0e09200SDave Airlie }
762c0e09200SDave Airlie 
763c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
764c0e09200SDave Airlie {
765c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
7667c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
767c0e09200SDave Airlie 	RING_LOCALS;
768c0e09200SDave Airlie 
769c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
770c0e09200SDave Airlie 
77144d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
772c0e09200SDave Airlie 
773c99b058fSKristian Høgsberg 	dev_priv->counter++;
774c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
775c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
7767c1c2871SDave Airlie 	if (master_priv->sarea_priv)
7777c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
778c0e09200SDave Airlie 
7790baf823aSKeith Packard 	BEGIN_LP_RING(4);
780585fb111SJesse Barnes 	OUT_RING(MI_STORE_DWORD_INDEX);
7810baf823aSKeith Packard 	OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
782c0e09200SDave Airlie 	OUT_RING(dev_priv->counter);
783585fb111SJesse Barnes 	OUT_RING(MI_USER_INTERRUPT);
784c0e09200SDave Airlie 	ADVANCE_LP_RING();
785c0e09200SDave Airlie 
786c0e09200SDave Airlie 	return dev_priv->counter;
787c0e09200SDave Airlie }
788c0e09200SDave Airlie 
789673a394bSEric Anholt void i915_user_irq_get(struct drm_device *dev)
790ed4cb414SEric Anholt {
791ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
792e9d21d7fSKeith Packard 	unsigned long irqflags;
793ed4cb414SEric Anholt 
794e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
795036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
796f2b115e6SAdam Jackson 		if (IS_IRONLAKE(dev))
797f2b115e6SAdam Jackson 			ironlake_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
798036a4a7dSZhenyu Wang 		else
799ed4cb414SEric Anholt 			i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
800036a4a7dSZhenyu Wang 	}
801e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
802ed4cb414SEric Anholt }
803ed4cb414SEric Anholt 
8040a3e67a4SJesse Barnes void i915_user_irq_put(struct drm_device *dev)
805ed4cb414SEric Anholt {
806ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
807e9d21d7fSKeith Packard 	unsigned long irqflags;
808ed4cb414SEric Anholt 
809e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
810ed4cb414SEric Anholt 	BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
811036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
812f2b115e6SAdam Jackson 		if (IS_IRONLAKE(dev))
813f2b115e6SAdam Jackson 			ironlake_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
814036a4a7dSZhenyu Wang 		else
815ed4cb414SEric Anholt 			i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
816036a4a7dSZhenyu Wang 	}
817e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
818ed4cb414SEric Anholt }
819ed4cb414SEric Anholt 
8209d34e5dbSChris Wilson void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
8219d34e5dbSChris Wilson {
8229d34e5dbSChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
8239d34e5dbSChris Wilson 
8249d34e5dbSChris Wilson 	if (dev_priv->trace_irq_seqno == 0)
8259d34e5dbSChris Wilson 		i915_user_irq_get(dev);
8269d34e5dbSChris Wilson 
8279d34e5dbSChris Wilson 	dev_priv->trace_irq_seqno = seqno;
8289d34e5dbSChris Wilson }
8299d34e5dbSChris Wilson 
830c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
831c0e09200SDave Airlie {
832c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
8337c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
834c0e09200SDave Airlie 	int ret = 0;
835c0e09200SDave Airlie 
83644d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
837c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
838c0e09200SDave Airlie 
839ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
8407c1c2871SDave Airlie 		if (master_priv->sarea_priv)
8417c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
842c0e09200SDave Airlie 		return 0;
843ed4cb414SEric Anholt 	}
844c0e09200SDave Airlie 
8457c1c2871SDave Airlie 	if (master_priv->sarea_priv)
8467c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
847c0e09200SDave Airlie 
848ed4cb414SEric Anholt 	i915_user_irq_get(dev);
849c0e09200SDave Airlie 	DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
850c0e09200SDave Airlie 		    READ_BREADCRUMB(dev_priv) >= irq_nr);
851ed4cb414SEric Anholt 	i915_user_irq_put(dev);
852c0e09200SDave Airlie 
853c0e09200SDave Airlie 	if (ret == -EBUSY) {
854c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
855c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
856c0e09200SDave Airlie 	}
857c0e09200SDave Airlie 
858c0e09200SDave Airlie 	return ret;
859c0e09200SDave Airlie }
860c0e09200SDave Airlie 
861c0e09200SDave Airlie /* Needs the lock as it touches the ring.
862c0e09200SDave Airlie  */
863c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
864c0e09200SDave Airlie 			 struct drm_file *file_priv)
865c0e09200SDave Airlie {
866c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
867c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
868c0e09200SDave Airlie 	int result;
869c0e09200SDave Airlie 
87007f4f8bfSEric Anholt 	if (!dev_priv || !dev_priv->ring.virtual_start) {
871c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
872c0e09200SDave Airlie 		return -EINVAL;
873c0e09200SDave Airlie 	}
874299eb93cSEric Anholt 
875299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
876299eb93cSEric Anholt 
877546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
878c0e09200SDave Airlie 	result = i915_emit_irq(dev);
879546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
880c0e09200SDave Airlie 
881c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
882c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
883c0e09200SDave Airlie 		return -EFAULT;
884c0e09200SDave Airlie 	}
885c0e09200SDave Airlie 
886c0e09200SDave Airlie 	return 0;
887c0e09200SDave Airlie }
888c0e09200SDave Airlie 
889c0e09200SDave Airlie /* Doesn't need the hardware lock.
890c0e09200SDave Airlie  */
891c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
892c0e09200SDave Airlie 			 struct drm_file *file_priv)
893c0e09200SDave Airlie {
894c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
895c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
896c0e09200SDave Airlie 
897c0e09200SDave Airlie 	if (!dev_priv) {
898c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
899c0e09200SDave Airlie 		return -EINVAL;
900c0e09200SDave Airlie 	}
901c0e09200SDave Airlie 
902c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
903c0e09200SDave Airlie }
904c0e09200SDave Airlie 
90542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
90642f52ef8SKeith Packard  * we use as a pipe index
90742f52ef8SKeith Packard  */
90842f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
9090a3e67a4SJesse Barnes {
9100a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
911e9d21d7fSKeith Packard 	unsigned long irqflags;
91271e0ffa5SJesse Barnes 	int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
91371e0ffa5SJesse Barnes 	u32 pipeconf;
91471e0ffa5SJesse Barnes 
91571e0ffa5SJesse Barnes 	pipeconf = I915_READ(pipeconf_reg);
91671e0ffa5SJesse Barnes 	if (!(pipeconf & PIPEACONF_ENABLE))
91771e0ffa5SJesse Barnes 		return -EINVAL;
9180a3e67a4SJesse Barnes 
919e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
920c062df61SLi Peng 	if (IS_IRONLAKE(dev))
921c062df61SLi Peng 		ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
922c062df61SLi Peng 					    DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
923c062df61SLi Peng 	else if (IS_I965G(dev))
9247c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
9257c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
9260a3e67a4SJesse Barnes 	else
9277c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
9287c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
929e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
9300a3e67a4SJesse Barnes 	return 0;
9310a3e67a4SJesse Barnes }
9320a3e67a4SJesse Barnes 
93342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
93442f52ef8SKeith Packard  * we use as a pipe index
93542f52ef8SKeith Packard  */
93642f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
9370a3e67a4SJesse Barnes {
9380a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
939e9d21d7fSKeith Packard 	unsigned long irqflags;
9400a3e67a4SJesse Barnes 
941e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
942c062df61SLi Peng 	if (IS_IRONLAKE(dev))
943c062df61SLi Peng 		ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
944c062df61SLi Peng 					     DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
945c062df61SLi Peng 	else
9467c463586SKeith Packard 		i915_disable_pipestat(dev_priv, pipe,
9477c463586SKeith Packard 				      PIPE_VBLANK_INTERRUPT_ENABLE |
9487c463586SKeith Packard 				      PIPE_START_VBLANK_INTERRUPT_ENABLE);
949e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
9500a3e67a4SJesse Barnes }
9510a3e67a4SJesse Barnes 
95279e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev)
95379e53945SJesse Barnes {
95479e53945SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
955e170b030SZhenyu Wang 
956f2b115e6SAdam Jackson 	if (!IS_IRONLAKE(dev))
95779e53945SJesse Barnes 		opregion_enable_asle(dev);
95879e53945SJesse Barnes 	dev_priv->irq_enabled = 1;
95979e53945SJesse Barnes }
96079e53945SJesse Barnes 
96179e53945SJesse Barnes 
962c0e09200SDave Airlie /* Set the vblank monitor pipe
963c0e09200SDave Airlie  */
964c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
965c0e09200SDave Airlie 			 struct drm_file *file_priv)
966c0e09200SDave Airlie {
967c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
968c0e09200SDave Airlie 
969c0e09200SDave Airlie 	if (!dev_priv) {
970c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
971c0e09200SDave Airlie 		return -EINVAL;
972c0e09200SDave Airlie 	}
973c0e09200SDave Airlie 
974c0e09200SDave Airlie 	return 0;
975c0e09200SDave Airlie }
976c0e09200SDave Airlie 
977c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
978c0e09200SDave Airlie 			 struct drm_file *file_priv)
979c0e09200SDave Airlie {
980c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
981c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
982c0e09200SDave Airlie 
983c0e09200SDave Airlie 	if (!dev_priv) {
984c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
985c0e09200SDave Airlie 		return -EINVAL;
986c0e09200SDave Airlie 	}
987c0e09200SDave Airlie 
9880a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
989c0e09200SDave Airlie 
990c0e09200SDave Airlie 	return 0;
991c0e09200SDave Airlie }
992c0e09200SDave Airlie 
993c0e09200SDave Airlie /**
994c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
995c0e09200SDave Airlie  */
996c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
997c0e09200SDave Airlie 		     struct drm_file *file_priv)
998c0e09200SDave Airlie {
999bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
1000bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
1001bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
1002bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
1003bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
1004bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
1005bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
1006bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
1007bd95e0a4SEric Anholt 	 *
1008bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
1009bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
1010bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
1011bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
10120a3e67a4SJesse Barnes 	 */
1013c0e09200SDave Airlie 	return -EINVAL;
1014c0e09200SDave Airlie }
1015c0e09200SDave Airlie 
1016f65d9421SBen Gamari struct drm_i915_gem_request *i915_get_tail_request(struct drm_device *dev) {
1017f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1018f65d9421SBen Gamari 	return list_entry(dev_priv->mm.request_list.prev, struct drm_i915_gem_request, list);
1019f65d9421SBen Gamari }
1020f65d9421SBen Gamari 
1021f65d9421SBen Gamari /**
1022f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
1023f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
1024f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1025f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
1026f65d9421SBen Gamari  */
1027f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
1028f65d9421SBen Gamari {
1029f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
1030f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
1031f65d9421SBen Gamari 	uint32_t acthd;
1032f65d9421SBen Gamari 
1033f65d9421SBen Gamari 	if (!IS_I965G(dev))
1034f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
1035f65d9421SBen Gamari 	else
1036f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
1037f65d9421SBen Gamari 
1038f65d9421SBen Gamari 	/* If all work is done then ACTHD clearly hasn't advanced. */
1039f65d9421SBen Gamari 	if (list_empty(&dev_priv->mm.request_list) ||
1040f65d9421SBen Gamari 		       i915_seqno_passed(i915_get_gem_seqno(dev), i915_get_tail_request(dev)->seqno)) {
1041f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
1042f65d9421SBen Gamari 		return;
1043f65d9421SBen Gamari 	}
1044f65d9421SBen Gamari 
1045f65d9421SBen Gamari 	if (dev_priv->last_acthd == acthd && dev_priv->hangcheck_count > 0) {
1046f65d9421SBen Gamari 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1047ba1234d1SBen Gamari 		i915_handle_error(dev, true);
1048f65d9421SBen Gamari 		return;
1049f65d9421SBen Gamari 	}
1050f65d9421SBen Gamari 
1051f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
1052f65d9421SBen Gamari 	mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1053f65d9421SBen Gamari 
1054f65d9421SBen Gamari 	if (acthd != dev_priv->last_acthd)
1055f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
1056f65d9421SBen Gamari 	else
1057f65d9421SBen Gamari 		dev_priv->hangcheck_count++;
1058f65d9421SBen Gamari 
1059f65d9421SBen Gamari 	dev_priv->last_acthd = acthd;
1060f65d9421SBen Gamari }
1061f65d9421SBen Gamari 
1062c0e09200SDave Airlie /* drm_dma.h hooks
1063c0e09200SDave Airlie */
1064f2b115e6SAdam Jackson static void ironlake_irq_preinstall(struct drm_device *dev)
1065036a4a7dSZhenyu Wang {
1066036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1067036a4a7dSZhenyu Wang 
1068036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1069036a4a7dSZhenyu Wang 
1070036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1071036a4a7dSZhenyu Wang 
1072036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1073036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1074036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
1075036a4a7dSZhenyu Wang 
1076036a4a7dSZhenyu Wang 	/* and GT */
1077036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1078036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1079036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
1080c650156aSZhenyu Wang 
1081c650156aSZhenyu Wang 	/* south display irq */
1082c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1083c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
1084c650156aSZhenyu Wang 	(void) I915_READ(SDEIER);
1085036a4a7dSZhenyu Wang }
1086036a4a7dSZhenyu Wang 
1087f2b115e6SAdam Jackson static int ironlake_irq_postinstall(struct drm_device *dev)
1088036a4a7dSZhenyu Wang {
1089036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1090036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1091013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1092013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
1093036a4a7dSZhenyu Wang 	u32 render_mask = GT_USER_INTERRUPT;
1094c650156aSZhenyu Wang 	u32 hotplug_mask = SDE_CRT_HOTPLUG | SDE_PORTB_HOTPLUG |
1095c650156aSZhenyu Wang 			   SDE_PORTC_HOTPLUG | SDE_PORTD_HOTPLUG;
1096036a4a7dSZhenyu Wang 
1097036a4a7dSZhenyu Wang 	dev_priv->irq_mask_reg = ~display_mask;
1098643ced9bSLi Peng 	dev_priv->de_irq_enable_reg = display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK;
1099036a4a7dSZhenyu Wang 
1100036a4a7dSZhenyu Wang 	/* should always can generate irq */
1101036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1102036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
1103036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
1104036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
1105036a4a7dSZhenyu Wang 
1106036a4a7dSZhenyu Wang 	/* user interrupt should be enabled, but masked initial */
1107036a4a7dSZhenyu Wang 	dev_priv->gt_irq_mask_reg = 0xffffffff;
1108036a4a7dSZhenyu Wang 	dev_priv->gt_irq_enable_reg = render_mask;
1109036a4a7dSZhenyu Wang 
1110036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1111036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
1112036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
1113036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
1114036a4a7dSZhenyu Wang 
1115c650156aSZhenyu Wang 	dev_priv->pch_irq_mask_reg = ~hotplug_mask;
1116c650156aSZhenyu Wang 	dev_priv->pch_irq_enable_reg = hotplug_mask;
1117c650156aSZhenyu Wang 
1118c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1119c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask_reg);
1120c650156aSZhenyu Wang 	I915_WRITE(SDEIER, dev_priv->pch_irq_enable_reg);
1121c650156aSZhenyu Wang 	(void) I915_READ(SDEIER);
1122c650156aSZhenyu Wang 
1123f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
1124f97108d1SJesse Barnes 		/* Clear & enable PCU event interrupts */
1125f97108d1SJesse Barnes 		I915_WRITE(DEIIR, DE_PCU_EVENT);
1126f97108d1SJesse Barnes 		I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1127f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1128f97108d1SJesse Barnes 	}
1129f97108d1SJesse Barnes 
1130036a4a7dSZhenyu Wang 	return 0;
1131036a4a7dSZhenyu Wang }
1132036a4a7dSZhenyu Wang 
1133c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1134c0e09200SDave Airlie {
1135c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1136c0e09200SDave Airlie 
113779e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
113879e53945SJesse Barnes 
1139036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
11408a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1141036a4a7dSZhenyu Wang 
1142f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev)) {
1143f2b115e6SAdam Jackson 		ironlake_irq_preinstall(dev);
1144036a4a7dSZhenyu Wang 		return;
1145036a4a7dSZhenyu Wang 	}
1146036a4a7dSZhenyu Wang 
11475ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
11485ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
11495ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
11505ca58282SJesse Barnes 	}
11515ca58282SJesse Barnes 
11520a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
11537c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
11547c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
11550a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1156ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
11577c463586SKeith Packard 	(void) I915_READ(IER);
1158c0e09200SDave Airlie }
1159c0e09200SDave Airlie 
1160b01f2c3aSJesse Barnes /*
1161b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
1162b01f2c3aSJesse Barnes  * enabled correctly.
1163b01f2c3aSJesse Barnes  */
11640a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1165c0e09200SDave Airlie {
1166c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11675ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
116863eeaf38SJesse Barnes 	u32 error_mask;
11690a3e67a4SJesse Barnes 
1170036a4a7dSZhenyu Wang 	DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
1171036a4a7dSZhenyu Wang 
11720a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1173ed4cb414SEric Anholt 
1174f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev))
1175f2b115e6SAdam Jackson 		return ironlake_irq_postinstall(dev);
1176036a4a7dSZhenyu Wang 
11777c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
11787c463586SKeith Packard 	dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
11798ee1c3dbSMatthew Garrett 
11807c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
11817c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
11827c463586SKeith Packard 
11835ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
11845ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
11855ca58282SJesse Barnes 
1186b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
1187b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1188b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1189b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1190b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1191b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1192b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
1193b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1194b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1195b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1196b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
1197b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS)
1198b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
1199b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
1200b01f2c3aSJesse Barnes 
12015ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
12025ca58282SJesse Barnes 
12035ca58282SJesse Barnes 		/* Enable in IER... */
12045ca58282SJesse Barnes 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
12055ca58282SJesse Barnes 		/* and unmask in IMR */
12065ca58282SJesse Barnes 		i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
12075ca58282SJesse Barnes 	}
12085ca58282SJesse Barnes 
120963eeaf38SJesse Barnes 	/*
121063eeaf38SJesse Barnes 	 * Enable some error detection, note the instruction error mask
121163eeaf38SJesse Barnes 	 * bit is reserved, so we leave it masked.
121263eeaf38SJesse Barnes 	 */
121363eeaf38SJesse Barnes 	if (IS_G4X(dev)) {
121463eeaf38SJesse Barnes 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
121563eeaf38SJesse Barnes 			       GM45_ERROR_MEM_PRIV |
121663eeaf38SJesse Barnes 			       GM45_ERROR_CP_PRIV |
121763eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
121863eeaf38SJesse Barnes 	} else {
121963eeaf38SJesse Barnes 		error_mask = ~(I915_ERROR_PAGE_TABLE |
122063eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
122163eeaf38SJesse Barnes 	}
122263eeaf38SJesse Barnes 	I915_WRITE(EMR, error_mask);
122363eeaf38SJesse Barnes 
12247c463586SKeith Packard 	/* Disable pipe interrupt enables, clear pending pipe status */
12257c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
12267c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
12277c463586SKeith Packard 	/* Clear pending interrupt status */
12287c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
12297c463586SKeith Packard 
12305ca58282SJesse Barnes 	I915_WRITE(IER, enable_mask);
12317c463586SKeith Packard 	I915_WRITE(IMR, dev_priv->irq_mask_reg);
1232ed4cb414SEric Anholt 	(void) I915_READ(IER);
1233ed4cb414SEric Anholt 
12348ee1c3dbSMatthew Garrett 	opregion_enable_asle(dev);
12350a3e67a4SJesse Barnes 
12360a3e67a4SJesse Barnes 	return 0;
1237c0e09200SDave Airlie }
1238c0e09200SDave Airlie 
1239f2b115e6SAdam Jackson static void ironlake_irq_uninstall(struct drm_device *dev)
1240036a4a7dSZhenyu Wang {
1241036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1242036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1243036a4a7dSZhenyu Wang 
1244036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1245036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1246036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1247036a4a7dSZhenyu Wang 
1248036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1249036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1250036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1251036a4a7dSZhenyu Wang }
1252036a4a7dSZhenyu Wang 
1253c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1254c0e09200SDave Airlie {
1255c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1256c0e09200SDave Airlie 
1257c0e09200SDave Airlie 	if (!dev_priv)
1258c0e09200SDave Airlie 		return;
1259c0e09200SDave Airlie 
12600a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
12610a3e67a4SJesse Barnes 
1262f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev)) {
1263f2b115e6SAdam Jackson 		ironlake_irq_uninstall(dev);
1264036a4a7dSZhenyu Wang 		return;
1265036a4a7dSZhenyu Wang 	}
1266036a4a7dSZhenyu Wang 
12675ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
12685ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
12695ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
12705ca58282SJesse Barnes 	}
12715ca58282SJesse Barnes 
12720a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
12737c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
12747c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
12750a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1276ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1277c0e09200SDave Airlie 
12787c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
12797c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
12807c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1281c0e09200SDave Airlie }
1282