xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision f2b115e69d46344ae7afcaad5823496d2a0d8650)
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
67*f2b115e6SAdam 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
77*f2b115e6SAdam 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
88*f2b115e6SAdam 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
98*f2b115e6SAdam 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 
169*f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev))
170*f2b115e6SAdam 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 
272*f2b115e6SAdam Jackson irqreturn_t ironlake_irq_handler(struct drm_device *dev)
273036a4a7dSZhenyu Wang {
274036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
275036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
276c650156aSZhenyu Wang 	u32 de_iir, gt_iir, pch_iir;
277c650156aSZhenyu Wang 	u32 new_de_iir, new_gt_iir, new_pch_iir;
278036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
279036a4a7dSZhenyu Wang 
280036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
281036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
282c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
283036a4a7dSZhenyu Wang 
284036a4a7dSZhenyu Wang 	for (;;) {
285c650156aSZhenyu Wang 		if (de_iir == 0 && gt_iir == 0 && pch_iir == 0)
286036a4a7dSZhenyu Wang 			break;
287036a4a7dSZhenyu Wang 
288036a4a7dSZhenyu Wang 		ret = IRQ_HANDLED;
289036a4a7dSZhenyu Wang 
290c650156aSZhenyu Wang 		/* should clear PCH hotplug event before clear CPU irq */
291c650156aSZhenyu Wang 		I915_WRITE(SDEIIR, pch_iir);
292c650156aSZhenyu Wang 		new_pch_iir = I915_READ(SDEIIR);
293c650156aSZhenyu Wang 
294036a4a7dSZhenyu Wang 		I915_WRITE(DEIIR, de_iir);
295036a4a7dSZhenyu Wang 		new_de_iir = I915_READ(DEIIR);
296036a4a7dSZhenyu Wang 		I915_WRITE(GTIIR, gt_iir);
297036a4a7dSZhenyu Wang 		new_gt_iir = I915_READ(GTIIR);
298036a4a7dSZhenyu Wang 
299036a4a7dSZhenyu Wang 		if (dev->primary->master) {
300036a4a7dSZhenyu Wang 			master_priv = dev->primary->master->driver_priv;
301036a4a7dSZhenyu Wang 			if (master_priv->sarea_priv)
302036a4a7dSZhenyu Wang 				master_priv->sarea_priv->last_dispatch =
303036a4a7dSZhenyu Wang 					READ_BREADCRUMB(dev_priv);
304036a4a7dSZhenyu Wang 		}
305036a4a7dSZhenyu Wang 
306036a4a7dSZhenyu Wang 		if (gt_iir & GT_USER_INTERRUPT) {
3071c5d22f7SChris Wilson 			u32 seqno = i915_get_gem_seqno(dev);
3081c5d22f7SChris Wilson 			dev_priv->mm.irq_gem_seqno = seqno;
3091c5d22f7SChris Wilson 			trace_i915_gem_request_complete(dev, seqno);
310036a4a7dSZhenyu Wang 			DRM_WAKEUP(&dev_priv->irq_queue);
311036a4a7dSZhenyu Wang 		}
312036a4a7dSZhenyu Wang 
31301c66889SZhao Yakui 		if (de_iir & DE_GSE)
31401c66889SZhao Yakui 			ironlake_opregion_gse_intr(dev);
31501c66889SZhao Yakui 
316c650156aSZhenyu Wang 		/* check event from PCH */
317c650156aSZhenyu Wang 		if ((de_iir & DE_PCH_EVENT) &&
318c650156aSZhenyu Wang 			(pch_iir & SDE_HOTPLUG_MASK)) {
319c650156aSZhenyu Wang 			queue_work(dev_priv->wq, &dev_priv->hotplug_work);
320c650156aSZhenyu Wang 		}
321c650156aSZhenyu Wang 
322036a4a7dSZhenyu Wang 		de_iir = new_de_iir;
323036a4a7dSZhenyu Wang 		gt_iir = new_gt_iir;
324c650156aSZhenyu Wang 		pch_iir = new_pch_iir;
325036a4a7dSZhenyu Wang 	}
326036a4a7dSZhenyu Wang 
327036a4a7dSZhenyu Wang 	return ret;
328036a4a7dSZhenyu Wang }
329036a4a7dSZhenyu Wang 
3308a905236SJesse Barnes /**
3318a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
3328a905236SJesse Barnes  * @work: work struct
3338a905236SJesse Barnes  *
3348a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
3358a905236SJesse Barnes  * was detected.
3368a905236SJesse Barnes  */
3378a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
3388a905236SJesse Barnes {
3398a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3408a905236SJesse Barnes 						    error_work);
3418a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
342f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
343f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
344f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
3458a905236SJesse Barnes 
34644d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("generating error event\n");
347f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
3488a905236SJesse Barnes 
349ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
350f316a42cSBen Gamari 		if (IS_I965G(dev)) {
35144d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("resetting chip\n");
352f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
353f316a42cSBen Gamari 			if (!i965_reset(dev, GDRST_RENDER)) {
354ba1234d1SBen Gamari 				atomic_set(&dev_priv->mm.wedged, 0);
355f316a42cSBen Gamari 				kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
356f316a42cSBen Gamari 			}
357f316a42cSBen Gamari 		} else {
35844d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("reboot required\n");
359f316a42cSBen Gamari 		}
360f316a42cSBen Gamari 	}
3618a905236SJesse Barnes }
3628a905236SJesse Barnes 
3638a905236SJesse Barnes /**
3648a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
3658a905236SJesse Barnes  * @dev: drm device
3668a905236SJesse Barnes  *
3678a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
3688a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
3698a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
3708a905236SJesse Barnes  * to pick up.
3718a905236SJesse Barnes  */
37263eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
37363eeaf38SJesse Barnes {
37463eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
37563eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
37663eeaf38SJesse Barnes 	unsigned long flags;
37763eeaf38SJesse Barnes 
37863eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
37963eeaf38SJesse Barnes 	if (dev_priv->first_error)
38063eeaf38SJesse Barnes 		goto out;
38163eeaf38SJesse Barnes 
38263eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
38363eeaf38SJesse Barnes 	if (!error) {
38444d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("out ot memory, not capturing error state\n");
38563eeaf38SJesse Barnes 		goto out;
38663eeaf38SJesse Barnes 	}
38763eeaf38SJesse Barnes 
38863eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
38963eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
39063eeaf38SJesse Barnes 	error->pipeastat = I915_READ(PIPEASTAT);
39163eeaf38SJesse Barnes 	error->pipebstat = I915_READ(PIPEBSTAT);
39263eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
39363eeaf38SJesse Barnes 	if (!IS_I965G(dev)) {
39463eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR);
39563eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR);
39663eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE);
39763eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD);
39863eeaf38SJesse Barnes 	} else {
39963eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
40063eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
40163eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
40263eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
40363eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
40463eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
40563eeaf38SJesse Barnes 	}
40663eeaf38SJesse Barnes 
4078a905236SJesse Barnes 	do_gettimeofday(&error->time);
4088a905236SJesse Barnes 
40963eeaf38SJesse Barnes 	dev_priv->first_error = error;
41063eeaf38SJesse Barnes 
41163eeaf38SJesse Barnes out:
41263eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
41363eeaf38SJesse Barnes }
41463eeaf38SJesse Barnes 
4158a905236SJesse Barnes /**
4168a905236SJesse Barnes  * i915_handle_error - handle an error interrupt
4178a905236SJesse Barnes  * @dev: drm device
4188a905236SJesse Barnes  *
4198a905236SJesse Barnes  * Do some basic checking of regsiter state at error interrupt time and
4208a905236SJesse Barnes  * dump it to the syslog.  Also call i915_capture_error_state() to make
4218a905236SJesse Barnes  * sure we get a record and make it available in debugfs.  Fire a uevent
4228a905236SJesse Barnes  * so userspace knows something bad happened (should trigger collection
4238a905236SJesse Barnes  * of a ring dump etc.).
4248a905236SJesse Barnes  */
425ba1234d1SBen Gamari static void i915_handle_error(struct drm_device *dev, bool wedged)
426c0e09200SDave Airlie {
4278a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
42863eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
4298a905236SJesse Barnes 	u32 pipea_stats = I915_READ(PIPEASTAT);
4308a905236SJesse Barnes 	u32 pipeb_stats = I915_READ(PIPEBSTAT);
43163eeaf38SJesse Barnes 
43263eeaf38SJesse Barnes 	i915_capture_error_state(dev);
43363eeaf38SJesse Barnes 
43463eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
43563eeaf38SJesse Barnes 	       eir);
4368a905236SJesse Barnes 
4378a905236SJesse Barnes 	if (IS_G4X(dev)) {
4388a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
4398a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
4408a905236SJesse Barnes 
4418a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
4428a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
4438a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
4448a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
4458a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
4468a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
4478a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
4488a905236SJesse Barnes 			       I915_READ(INSTPS));
4498a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
4508a905236SJesse Barnes 			       I915_READ(INSTDONE1));
4518a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
4528a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
4538a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
4548a905236SJesse Barnes 			(void)I915_READ(IPEIR_I965);
4558a905236SJesse Barnes 		}
4568a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
4578a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
4588a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
4598a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
4608a905236SJesse Barnes 			       pgtbl_err);
4618a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
4628a905236SJesse Barnes 			(void)I915_READ(PGTBL_ER);
4638a905236SJesse Barnes 		}
4648a905236SJesse Barnes 	}
4658a905236SJesse Barnes 
4668a905236SJesse Barnes 	if (IS_I9XX(dev)) {
46763eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
46863eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
46963eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
47063eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
47163eeaf38SJesse Barnes 			       pgtbl_err);
47263eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
47363eeaf38SJesse Barnes 			(void)I915_READ(PGTBL_ER);
47463eeaf38SJesse Barnes 		}
4758a905236SJesse Barnes 	}
4768a905236SJesse Barnes 
47763eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
47863eeaf38SJesse Barnes 		printk(KERN_ERR "memory refresh error\n");
47963eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
48063eeaf38SJesse Barnes 		       pipea_stats);
48163eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
48263eeaf38SJesse Barnes 		       pipeb_stats);
48363eeaf38SJesse Barnes 		/* pipestat has already been acked */
48463eeaf38SJesse Barnes 	}
48563eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
48663eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
48763eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
48863eeaf38SJesse Barnes 		       I915_READ(INSTPM));
48963eeaf38SJesse Barnes 		if (!IS_I965G(dev)) {
49063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
49163eeaf38SJesse Barnes 
49263eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
49363eeaf38SJesse Barnes 			       I915_READ(IPEIR));
49463eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
49563eeaf38SJesse Barnes 			       I915_READ(IPEHR));
49663eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
49763eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
49863eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
49963eeaf38SJesse Barnes 			       I915_READ(ACTHD));
50063eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
50163eeaf38SJesse Barnes 			(void)I915_READ(IPEIR);
50263eeaf38SJesse Barnes 		} else {
50363eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
50463eeaf38SJesse Barnes 
50563eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
50663eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
50763eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
50863eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
50963eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
51063eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
51163eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
51263eeaf38SJesse Barnes 			       I915_READ(INSTPS));
51363eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
51463eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
51563eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
51663eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
51763eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
51863eeaf38SJesse Barnes 			(void)I915_READ(IPEIR_I965);
51963eeaf38SJesse Barnes 		}
52063eeaf38SJesse Barnes 	}
52163eeaf38SJesse Barnes 
52263eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
52363eeaf38SJesse Barnes 	(void)I915_READ(EIR);
52463eeaf38SJesse Barnes 	eir = I915_READ(EIR);
52563eeaf38SJesse Barnes 	if (eir) {
52663eeaf38SJesse Barnes 		/*
52763eeaf38SJesse Barnes 		 * some errors might have become stuck,
52863eeaf38SJesse Barnes 		 * mask them.
52963eeaf38SJesse Barnes 		 */
53063eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
53163eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
53263eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
53363eeaf38SJesse Barnes 	}
5348a905236SJesse Barnes 
535ba1234d1SBen Gamari 	if (wedged) {
536ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
537ba1234d1SBen Gamari 
53811ed50ecSBen Gamari 		/*
53911ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
54011ed50ecSBen Gamari 		 */
54111ed50ecSBen Gamari 		printk("i915: Waking up sleeping processes\n");
54211ed50ecSBen Gamari 		DRM_WAKEUP(&dev_priv->irq_queue);
54311ed50ecSBen Gamari 	}
54411ed50ecSBen Gamari 
5459c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
5468a905236SJesse Barnes }
5478a905236SJesse Barnes 
5488a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
5498a905236SJesse Barnes {
5508a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
5518a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5528a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
5538a905236SJesse Barnes 	u32 iir, new_iir;
5548a905236SJesse Barnes 	u32 pipea_stats, pipeb_stats;
5558a905236SJesse Barnes 	u32 vblank_status;
5568a905236SJesse Barnes 	u32 vblank_enable;
5578a905236SJesse Barnes 	int vblank = 0;
5588a905236SJesse Barnes 	unsigned long irqflags;
5598a905236SJesse Barnes 	int irq_received;
5608a905236SJesse Barnes 	int ret = IRQ_NONE;
5618a905236SJesse Barnes 
5628a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5638a905236SJesse Barnes 
564*f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev))
565*f2b115e6SAdam Jackson 		return ironlake_irq_handler(dev);
5668a905236SJesse Barnes 
5678a905236SJesse Barnes 	iir = I915_READ(IIR);
5688a905236SJesse Barnes 
5698a905236SJesse Barnes 	if (IS_I965G(dev)) {
5708a905236SJesse Barnes 		vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
5718a905236SJesse Barnes 		vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
5728a905236SJesse Barnes 	} else {
5738a905236SJesse Barnes 		vblank_status = I915_VBLANK_INTERRUPT_STATUS;
5748a905236SJesse Barnes 		vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
5758a905236SJesse Barnes 	}
5768a905236SJesse Barnes 
5778a905236SJesse Barnes 	for (;;) {
5788a905236SJesse Barnes 		irq_received = iir != 0;
5798a905236SJesse Barnes 
5808a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
5818a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
5828a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
5838a905236SJesse Barnes 		 * interrupts (for non-MSI).
5848a905236SJesse Barnes 		 */
5858a905236SJesse Barnes 		spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
5868a905236SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
5878a905236SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
5888a905236SJesse Barnes 
5898a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
590ba1234d1SBen Gamari 			i915_handle_error(dev, false);
5918a905236SJesse Barnes 
5928a905236SJesse Barnes 		/*
5938a905236SJesse Barnes 		 * Clear the PIPE(A|B)STAT regs before the IIR
5948a905236SJesse Barnes 		 */
5958a905236SJesse Barnes 		if (pipea_stats & 0x8000ffff) {
5968a905236SJesse Barnes 			if (pipea_stats &  PIPE_FIFO_UNDERRUN_STATUS)
59744d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe a underrun\n");
5988a905236SJesse Barnes 			I915_WRITE(PIPEASTAT, pipea_stats);
5998a905236SJesse Barnes 			irq_received = 1;
6008a905236SJesse Barnes 		}
6018a905236SJesse Barnes 
6028a905236SJesse Barnes 		if (pipeb_stats & 0x8000ffff) {
6038a905236SJesse Barnes 			if (pipeb_stats &  PIPE_FIFO_UNDERRUN_STATUS)
60444d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe b underrun\n");
6058a905236SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
6068a905236SJesse Barnes 			irq_received = 1;
6078a905236SJesse Barnes 		}
6088a905236SJesse Barnes 		spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
6098a905236SJesse Barnes 
6108a905236SJesse Barnes 		if (!irq_received)
6118a905236SJesse Barnes 			break;
6128a905236SJesse Barnes 
6138a905236SJesse Barnes 		ret = IRQ_HANDLED;
6148a905236SJesse Barnes 
6158a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
6168a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
6178a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
6188a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
6198a905236SJesse Barnes 
62044d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
6218a905236SJesse Barnes 				  hotplug_status);
6228a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
6239c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
6249c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
6258a905236SJesse Barnes 
6268a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
6278a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
62863eeaf38SJesse Barnes 		}
62963eeaf38SJesse Barnes 
630673a394bSEric Anholt 		I915_WRITE(IIR, iir);
631cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
6327c463586SKeith Packard 
6337c1c2871SDave Airlie 		if (dev->primary->master) {
6347c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
6357c1c2871SDave Airlie 			if (master_priv->sarea_priv)
6367c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
637c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
6387c1c2871SDave Airlie 		}
6390a3e67a4SJesse Barnes 
640673a394bSEric Anholt 		if (iir & I915_USER_INTERRUPT) {
6411c5d22f7SChris Wilson 			u32 seqno = i915_get_gem_seqno(dev);
6421c5d22f7SChris Wilson 			dev_priv->mm.irq_gem_seqno = seqno;
6431c5d22f7SChris Wilson 			trace_i915_gem_request_complete(dev, seqno);
644673a394bSEric Anholt 			DRM_WAKEUP(&dev_priv->irq_queue);
645f65d9421SBen Gamari 			dev_priv->hangcheck_count = 0;
646f65d9421SBen Gamari 			mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
647673a394bSEric Anholt 		}
648673a394bSEric Anholt 
6496b95a207SKristian Høgsberg 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
6506b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
6516b95a207SKristian Høgsberg 
6526b95a207SKristian Høgsberg 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
6536b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 1);
6546b95a207SKristian Høgsberg 
65505eff845SKeith Packard 		if (pipea_stats & vblank_status) {
6567c463586SKeith Packard 			vblank++;
6577c463586SKeith Packard 			drm_handle_vblank(dev, 0);
6586b95a207SKristian Høgsberg 			intel_finish_page_flip(dev, 0);
6597c463586SKeith Packard 		}
6607c463586SKeith Packard 
66105eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
6627c463586SKeith Packard 			vblank++;
6637c463586SKeith Packard 			drm_handle_vblank(dev, 1);
6646b95a207SKristian Høgsberg 			intel_finish_page_flip(dev, 1);
6657c463586SKeith Packard 		}
6667c463586SKeith Packard 
6677c463586SKeith Packard 		if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
6687c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
669673a394bSEric Anholt 			opregion_asle_intr(dev);
6700a3e67a4SJesse Barnes 
671cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
672cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
673cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
674cdfbc41fSEric Anholt 		 * we would never get another interrupt.
675cdfbc41fSEric Anholt 		 *
676cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
677cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
678cdfbc41fSEric Anholt 		 * another one.
679cdfbc41fSEric Anholt 		 *
680cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
681cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
682cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
683cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
684cdfbc41fSEric Anholt 		 * stray interrupts.
685cdfbc41fSEric Anholt 		 */
686cdfbc41fSEric Anholt 		iir = new_iir;
68705eff845SKeith Packard 	}
688cdfbc41fSEric Anholt 
68905eff845SKeith Packard 	return ret;
690c0e09200SDave Airlie }
691c0e09200SDave Airlie 
692c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
693c0e09200SDave Airlie {
694c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
6957c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
696c0e09200SDave Airlie 	RING_LOCALS;
697c0e09200SDave Airlie 
698c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
699c0e09200SDave Airlie 
70044d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
701c0e09200SDave Airlie 
702c99b058fSKristian Høgsberg 	dev_priv->counter++;
703c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
704c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
7057c1c2871SDave Airlie 	if (master_priv->sarea_priv)
7067c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
707c0e09200SDave Airlie 
7080baf823aSKeith Packard 	BEGIN_LP_RING(4);
709585fb111SJesse Barnes 	OUT_RING(MI_STORE_DWORD_INDEX);
7100baf823aSKeith Packard 	OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
711c0e09200SDave Airlie 	OUT_RING(dev_priv->counter);
712585fb111SJesse Barnes 	OUT_RING(MI_USER_INTERRUPT);
713c0e09200SDave Airlie 	ADVANCE_LP_RING();
714c0e09200SDave Airlie 
715c0e09200SDave Airlie 	return dev_priv->counter;
716c0e09200SDave Airlie }
717c0e09200SDave Airlie 
718673a394bSEric Anholt void i915_user_irq_get(struct drm_device *dev)
719ed4cb414SEric Anholt {
720ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
721e9d21d7fSKeith Packard 	unsigned long irqflags;
722ed4cb414SEric Anholt 
723e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
724036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
725*f2b115e6SAdam Jackson 		if (IS_IRONLAKE(dev))
726*f2b115e6SAdam Jackson 			ironlake_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
727036a4a7dSZhenyu Wang 		else
728ed4cb414SEric Anholt 			i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
729036a4a7dSZhenyu Wang 	}
730e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
731ed4cb414SEric Anholt }
732ed4cb414SEric Anholt 
7330a3e67a4SJesse Barnes void i915_user_irq_put(struct drm_device *dev)
734ed4cb414SEric Anholt {
735ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
736e9d21d7fSKeith Packard 	unsigned long irqflags;
737ed4cb414SEric Anholt 
738e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
739ed4cb414SEric Anholt 	BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
740036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
741*f2b115e6SAdam Jackson 		if (IS_IRONLAKE(dev))
742*f2b115e6SAdam Jackson 			ironlake_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
743036a4a7dSZhenyu Wang 		else
744ed4cb414SEric Anholt 			i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
745036a4a7dSZhenyu Wang 	}
746e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
747ed4cb414SEric Anholt }
748ed4cb414SEric Anholt 
7499d34e5dbSChris Wilson void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
7509d34e5dbSChris Wilson {
7519d34e5dbSChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7529d34e5dbSChris Wilson 
7539d34e5dbSChris Wilson 	if (dev_priv->trace_irq_seqno == 0)
7549d34e5dbSChris Wilson 		i915_user_irq_get(dev);
7559d34e5dbSChris Wilson 
7569d34e5dbSChris Wilson 	dev_priv->trace_irq_seqno = seqno;
7579d34e5dbSChris Wilson }
7589d34e5dbSChris Wilson 
759c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
760c0e09200SDave Airlie {
761c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7627c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
763c0e09200SDave Airlie 	int ret = 0;
764c0e09200SDave Airlie 
76544d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
766c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
767c0e09200SDave Airlie 
768ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
7697c1c2871SDave Airlie 		if (master_priv->sarea_priv)
7707c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
771c0e09200SDave Airlie 		return 0;
772ed4cb414SEric Anholt 	}
773c0e09200SDave Airlie 
7747c1c2871SDave Airlie 	if (master_priv->sarea_priv)
7757c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
776c0e09200SDave Airlie 
777ed4cb414SEric Anholt 	i915_user_irq_get(dev);
778c0e09200SDave Airlie 	DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
779c0e09200SDave Airlie 		    READ_BREADCRUMB(dev_priv) >= irq_nr);
780ed4cb414SEric Anholt 	i915_user_irq_put(dev);
781c0e09200SDave Airlie 
782c0e09200SDave Airlie 	if (ret == -EBUSY) {
783c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
784c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
785c0e09200SDave Airlie 	}
786c0e09200SDave Airlie 
787c0e09200SDave Airlie 	return ret;
788c0e09200SDave Airlie }
789c0e09200SDave Airlie 
790c0e09200SDave Airlie /* Needs the lock as it touches the ring.
791c0e09200SDave Airlie  */
792c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
793c0e09200SDave Airlie 			 struct drm_file *file_priv)
794c0e09200SDave Airlie {
795c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
796c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
797c0e09200SDave Airlie 	int result;
798c0e09200SDave Airlie 
79907f4f8bfSEric Anholt 	if (!dev_priv || !dev_priv->ring.virtual_start) {
800c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
801c0e09200SDave Airlie 		return -EINVAL;
802c0e09200SDave Airlie 	}
803299eb93cSEric Anholt 
804299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
805299eb93cSEric Anholt 
806546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
807c0e09200SDave Airlie 	result = i915_emit_irq(dev);
808546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
809c0e09200SDave Airlie 
810c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
811c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
812c0e09200SDave Airlie 		return -EFAULT;
813c0e09200SDave Airlie 	}
814c0e09200SDave Airlie 
815c0e09200SDave Airlie 	return 0;
816c0e09200SDave Airlie }
817c0e09200SDave Airlie 
818c0e09200SDave Airlie /* Doesn't need the hardware lock.
819c0e09200SDave Airlie  */
820c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
821c0e09200SDave Airlie 			 struct drm_file *file_priv)
822c0e09200SDave Airlie {
823c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
824c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
825c0e09200SDave Airlie 
826c0e09200SDave Airlie 	if (!dev_priv) {
827c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
828c0e09200SDave Airlie 		return -EINVAL;
829c0e09200SDave Airlie 	}
830c0e09200SDave Airlie 
831c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
832c0e09200SDave Airlie }
833c0e09200SDave Airlie 
83442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
83542f52ef8SKeith Packard  * we use as a pipe index
83642f52ef8SKeith Packard  */
83742f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
8380a3e67a4SJesse Barnes {
8390a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
840e9d21d7fSKeith Packard 	unsigned long irqflags;
84171e0ffa5SJesse Barnes 	int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
84271e0ffa5SJesse Barnes 	u32 pipeconf;
84371e0ffa5SJesse Barnes 
84471e0ffa5SJesse Barnes 	pipeconf = I915_READ(pipeconf_reg);
84571e0ffa5SJesse Barnes 	if (!(pipeconf & PIPEACONF_ENABLE))
84671e0ffa5SJesse Barnes 		return -EINVAL;
8470a3e67a4SJesse Barnes 
848*f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev))
849036a4a7dSZhenyu Wang 		return 0;
850036a4a7dSZhenyu Wang 
851e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
8520a3e67a4SJesse Barnes 	if (IS_I965G(dev))
8537c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
8547c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
8550a3e67a4SJesse Barnes 	else
8567c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
8577c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
858e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
8590a3e67a4SJesse Barnes 	return 0;
8600a3e67a4SJesse Barnes }
8610a3e67a4SJesse Barnes 
86242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
86342f52ef8SKeith Packard  * we use as a pipe index
86442f52ef8SKeith Packard  */
86542f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
8660a3e67a4SJesse Barnes {
8670a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
868e9d21d7fSKeith Packard 	unsigned long irqflags;
8690a3e67a4SJesse Barnes 
870*f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev))
871036a4a7dSZhenyu Wang 		return;
872036a4a7dSZhenyu Wang 
873e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
8747c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
8757c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
8767c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
877e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
8780a3e67a4SJesse Barnes }
8790a3e67a4SJesse Barnes 
88079e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev)
88179e53945SJesse Barnes {
88279e53945SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
883e170b030SZhenyu Wang 
884*f2b115e6SAdam Jackson 	if (!IS_IRONLAKE(dev))
88579e53945SJesse Barnes 		opregion_enable_asle(dev);
88679e53945SJesse Barnes 	dev_priv->irq_enabled = 1;
88779e53945SJesse Barnes }
88879e53945SJesse Barnes 
88979e53945SJesse Barnes 
890c0e09200SDave Airlie /* Set the vblank monitor pipe
891c0e09200SDave Airlie  */
892c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
893c0e09200SDave Airlie 			 struct drm_file *file_priv)
894c0e09200SDave Airlie {
895c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
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 0;
903c0e09200SDave Airlie }
904c0e09200SDave Airlie 
905c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
906c0e09200SDave Airlie 			 struct drm_file *file_priv)
907c0e09200SDave Airlie {
908c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
909c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
910c0e09200SDave Airlie 
911c0e09200SDave Airlie 	if (!dev_priv) {
912c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
913c0e09200SDave Airlie 		return -EINVAL;
914c0e09200SDave Airlie 	}
915c0e09200SDave Airlie 
9160a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
917c0e09200SDave Airlie 
918c0e09200SDave Airlie 	return 0;
919c0e09200SDave Airlie }
920c0e09200SDave Airlie 
921c0e09200SDave Airlie /**
922c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
923c0e09200SDave Airlie  */
924c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
925c0e09200SDave Airlie 		     struct drm_file *file_priv)
926c0e09200SDave Airlie {
927bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
928bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
929bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
930bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
931bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
932bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
933bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
934bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
935bd95e0a4SEric Anholt 	 *
936bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
937bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
938bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
939bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
9400a3e67a4SJesse Barnes 	 */
941c0e09200SDave Airlie 	return -EINVAL;
942c0e09200SDave Airlie }
943c0e09200SDave Airlie 
944f65d9421SBen Gamari struct drm_i915_gem_request *i915_get_tail_request(struct drm_device *dev) {
945f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
946f65d9421SBen Gamari 	return list_entry(dev_priv->mm.request_list.prev, struct drm_i915_gem_request, list);
947f65d9421SBen Gamari }
948f65d9421SBen Gamari 
949f65d9421SBen Gamari /**
950f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
951f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
952f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
953f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
954f65d9421SBen Gamari  */
955f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
956f65d9421SBen Gamari {
957f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
958f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
959f65d9421SBen Gamari 	uint32_t acthd;
960f65d9421SBen Gamari 
961f65d9421SBen Gamari 	if (!IS_I965G(dev))
962f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
963f65d9421SBen Gamari 	else
964f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
965f65d9421SBen Gamari 
966f65d9421SBen Gamari 	/* If all work is done then ACTHD clearly hasn't advanced. */
967f65d9421SBen Gamari 	if (list_empty(&dev_priv->mm.request_list) ||
968f65d9421SBen Gamari 		       i915_seqno_passed(i915_get_gem_seqno(dev), i915_get_tail_request(dev)->seqno)) {
969f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
970f65d9421SBen Gamari 		return;
971f65d9421SBen Gamari 	}
972f65d9421SBen Gamari 
973f65d9421SBen Gamari 	if (dev_priv->last_acthd == acthd && dev_priv->hangcheck_count > 0) {
974f65d9421SBen Gamari 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
975ba1234d1SBen Gamari 		i915_handle_error(dev, true);
976f65d9421SBen Gamari 		return;
977f65d9421SBen Gamari 	}
978f65d9421SBen Gamari 
979f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
980f65d9421SBen Gamari 	mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
981f65d9421SBen Gamari 
982f65d9421SBen Gamari 	if (acthd != dev_priv->last_acthd)
983f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
984f65d9421SBen Gamari 	else
985f65d9421SBen Gamari 		dev_priv->hangcheck_count++;
986f65d9421SBen Gamari 
987f65d9421SBen Gamari 	dev_priv->last_acthd = acthd;
988f65d9421SBen Gamari }
989f65d9421SBen Gamari 
990c0e09200SDave Airlie /* drm_dma.h hooks
991c0e09200SDave Airlie */
992*f2b115e6SAdam Jackson static void ironlake_irq_preinstall(struct drm_device *dev)
993036a4a7dSZhenyu Wang {
994036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
995036a4a7dSZhenyu Wang 
996036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
997036a4a7dSZhenyu Wang 
998036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
999036a4a7dSZhenyu Wang 
1000036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1001036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1002036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
1003036a4a7dSZhenyu Wang 
1004036a4a7dSZhenyu Wang 	/* and GT */
1005036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1006036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1007036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
1008c650156aSZhenyu Wang 
1009c650156aSZhenyu Wang 	/* south display irq */
1010c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1011c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
1012c650156aSZhenyu Wang 	(void) I915_READ(SDEIER);
1013036a4a7dSZhenyu Wang }
1014036a4a7dSZhenyu Wang 
1015*f2b115e6SAdam Jackson static int ironlake_irq_postinstall(struct drm_device *dev)
1016036a4a7dSZhenyu Wang {
1017036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1018036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1019c650156aSZhenyu Wang 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT;
1020036a4a7dSZhenyu Wang 	u32 render_mask = GT_USER_INTERRUPT;
1021c650156aSZhenyu Wang 	u32 hotplug_mask = SDE_CRT_HOTPLUG | SDE_PORTB_HOTPLUG |
1022c650156aSZhenyu Wang 			   SDE_PORTC_HOTPLUG | SDE_PORTD_HOTPLUG;
1023036a4a7dSZhenyu Wang 
1024036a4a7dSZhenyu Wang 	dev_priv->irq_mask_reg = ~display_mask;
1025036a4a7dSZhenyu Wang 	dev_priv->de_irq_enable_reg = display_mask;
1026036a4a7dSZhenyu Wang 
1027036a4a7dSZhenyu Wang 	/* should always can generate irq */
1028036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1029036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
1030036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
1031036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
1032036a4a7dSZhenyu Wang 
1033036a4a7dSZhenyu Wang 	/* user interrupt should be enabled, but masked initial */
1034036a4a7dSZhenyu Wang 	dev_priv->gt_irq_mask_reg = 0xffffffff;
1035036a4a7dSZhenyu Wang 	dev_priv->gt_irq_enable_reg = render_mask;
1036036a4a7dSZhenyu Wang 
1037036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1038036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
1039036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
1040036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
1041036a4a7dSZhenyu Wang 
1042c650156aSZhenyu Wang 	dev_priv->pch_irq_mask_reg = ~hotplug_mask;
1043c650156aSZhenyu Wang 	dev_priv->pch_irq_enable_reg = hotplug_mask;
1044c650156aSZhenyu Wang 
1045c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1046c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask_reg);
1047c650156aSZhenyu Wang 	I915_WRITE(SDEIER, dev_priv->pch_irq_enable_reg);
1048c650156aSZhenyu Wang 	(void) I915_READ(SDEIER);
1049c650156aSZhenyu Wang 
1050036a4a7dSZhenyu Wang 	return 0;
1051036a4a7dSZhenyu Wang }
1052036a4a7dSZhenyu Wang 
1053c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1054c0e09200SDave Airlie {
1055c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1056c0e09200SDave Airlie 
105779e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
105879e53945SJesse Barnes 
1059036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
10608a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1061036a4a7dSZhenyu Wang 
1062*f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev)) {
1063*f2b115e6SAdam Jackson 		ironlake_irq_preinstall(dev);
1064036a4a7dSZhenyu Wang 		return;
1065036a4a7dSZhenyu Wang 	}
1066036a4a7dSZhenyu Wang 
10675ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
10685ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
10695ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
10705ca58282SJesse Barnes 	}
10715ca58282SJesse Barnes 
10720a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
10737c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
10747c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
10750a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1076ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
10777c463586SKeith Packard 	(void) I915_READ(IER);
1078c0e09200SDave Airlie }
1079c0e09200SDave Airlie 
10800a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1081c0e09200SDave Airlie {
1082c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10835ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
108463eeaf38SJesse Barnes 	u32 error_mask;
10850a3e67a4SJesse Barnes 
1086036a4a7dSZhenyu Wang 	DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
1087036a4a7dSZhenyu Wang 
10880a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1089ed4cb414SEric Anholt 
1090*f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev))
1091*f2b115e6SAdam Jackson 		return ironlake_irq_postinstall(dev);
1092036a4a7dSZhenyu Wang 
10937c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
10947c463586SKeith Packard 	dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
10958ee1c3dbSMatthew Garrett 
10967c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
10977c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
10987c463586SKeith Packard 
10995ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
11005ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
11015ca58282SJesse Barnes 
11025ca58282SJesse Barnes 		/* Leave other bits alone */
11035ca58282SJesse Barnes 		hotplug_en |= HOTPLUG_EN_MASK;
11045ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
11055ca58282SJesse Barnes 
11065ca58282SJesse Barnes 		dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
11075ca58282SJesse Barnes 			TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
11085ca58282SJesse Barnes 			SDVOB_HOTPLUG_INT_STATUS;
11095ca58282SJesse Barnes 		if (IS_G4X(dev)) {
11105ca58282SJesse Barnes 			dev_priv->hotplug_supported_mask |=
11115ca58282SJesse Barnes 				HDMIB_HOTPLUG_INT_STATUS |
11125ca58282SJesse Barnes 				HDMIC_HOTPLUG_INT_STATUS |
11135ca58282SJesse Barnes 				HDMID_HOTPLUG_INT_STATUS;
11145ca58282SJesse Barnes 		}
11155ca58282SJesse Barnes 		/* Enable in IER... */
11165ca58282SJesse Barnes 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
11175ca58282SJesse Barnes 		/* and unmask in IMR */
11185ca58282SJesse Barnes 		i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
11195ca58282SJesse Barnes 	}
11205ca58282SJesse Barnes 
112163eeaf38SJesse Barnes 	/*
112263eeaf38SJesse Barnes 	 * Enable some error detection, note the instruction error mask
112363eeaf38SJesse Barnes 	 * bit is reserved, so we leave it masked.
112463eeaf38SJesse Barnes 	 */
112563eeaf38SJesse Barnes 	if (IS_G4X(dev)) {
112663eeaf38SJesse Barnes 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
112763eeaf38SJesse Barnes 			       GM45_ERROR_MEM_PRIV |
112863eeaf38SJesse Barnes 			       GM45_ERROR_CP_PRIV |
112963eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
113063eeaf38SJesse Barnes 	} else {
113163eeaf38SJesse Barnes 		error_mask = ~(I915_ERROR_PAGE_TABLE |
113263eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
113363eeaf38SJesse Barnes 	}
113463eeaf38SJesse Barnes 	I915_WRITE(EMR, error_mask);
113563eeaf38SJesse Barnes 
11367c463586SKeith Packard 	/* Disable pipe interrupt enables, clear pending pipe status */
11377c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
11387c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
11397c463586SKeith Packard 	/* Clear pending interrupt status */
11407c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
11417c463586SKeith Packard 
11425ca58282SJesse Barnes 	I915_WRITE(IER, enable_mask);
11437c463586SKeith Packard 	I915_WRITE(IMR, dev_priv->irq_mask_reg);
1144ed4cb414SEric Anholt 	(void) I915_READ(IER);
1145ed4cb414SEric Anholt 
11468ee1c3dbSMatthew Garrett 	opregion_enable_asle(dev);
11470a3e67a4SJesse Barnes 
11480a3e67a4SJesse Barnes 	return 0;
1149c0e09200SDave Airlie }
1150c0e09200SDave Airlie 
1151*f2b115e6SAdam Jackson static void ironlake_irq_uninstall(struct drm_device *dev)
1152036a4a7dSZhenyu Wang {
1153036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1154036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1155036a4a7dSZhenyu Wang 
1156036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1157036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1158036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1159036a4a7dSZhenyu Wang 
1160036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1161036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1162036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1163036a4a7dSZhenyu Wang }
1164036a4a7dSZhenyu Wang 
1165c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1166c0e09200SDave Airlie {
1167c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1168c0e09200SDave Airlie 
1169c0e09200SDave Airlie 	if (!dev_priv)
1170c0e09200SDave Airlie 		return;
1171c0e09200SDave Airlie 
11720a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
11730a3e67a4SJesse Barnes 
1174*f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev)) {
1175*f2b115e6SAdam Jackson 		ironlake_irq_uninstall(dev);
1176036a4a7dSZhenyu Wang 		return;
1177036a4a7dSZhenyu Wang 	}
1178036a4a7dSZhenyu Wang 
11795ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
11805ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
11815ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
11825ca58282SJesse Barnes 	}
11835ca58282SJesse Barnes 
11840a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
11857c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
11867c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
11870a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1188ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1189c0e09200SDave Airlie 
11907c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
11917c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
11927c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1193c0e09200SDave Airlie }
1194