xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision f316a42cc49eca73b33d85feb6177e32431747ff)
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"
3479e53945SJesse Barnes #include "intel_drv.h"
35c0e09200SDave Airlie 
36c0e09200SDave Airlie #define MAX_NOPID ((u32)~0)
37c0e09200SDave Airlie 
387c463586SKeith Packard /**
397c463586SKeith Packard  * Interrupts that are always left unmasked.
407c463586SKeith Packard  *
417c463586SKeith Packard  * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
427c463586SKeith Packard  * we leave them always unmasked in IMR and then control enabling them through
437c463586SKeith Packard  * PIPESTAT alone.
447c463586SKeith Packard  */
457c463586SKeith Packard #define I915_INTERRUPT_ENABLE_FIX (I915_ASLE_INTERRUPT |		 \
460a3e67a4SJesse Barnes 				   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
4763eeaf38SJesse Barnes 				   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT | \
4863eeaf38SJesse Barnes 				   I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
49ed4cb414SEric Anholt 
507c463586SKeith Packard /** Interrupts that we mask and unmask at runtime. */
517c463586SKeith Packard #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
527c463586SKeith Packard 
5379e53945SJesse Barnes #define I915_PIPE_VBLANK_STATUS	(PIPE_START_VBLANK_INTERRUPT_STATUS |\
5479e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_STATUS)
5579e53945SJesse Barnes 
5679e53945SJesse Barnes #define I915_PIPE_VBLANK_ENABLE	(PIPE_START_VBLANK_INTERRUPT_ENABLE |\
5779e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_ENABLE)
5879e53945SJesse Barnes 
5979e53945SJesse Barnes #define DRM_I915_VBLANK_PIPE_ALL	(DRM_I915_VBLANK_PIPE_A | \
6079e53945SJesse Barnes 					 DRM_I915_VBLANK_PIPE_B)
6179e53945SJesse Barnes 
628ee1c3dbSMatthew Garrett void
63036a4a7dSZhenyu Wang igdng_enable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
64036a4a7dSZhenyu Wang {
65036a4a7dSZhenyu Wang 	if ((dev_priv->gt_irq_mask_reg & mask) != 0) {
66036a4a7dSZhenyu Wang 		dev_priv->gt_irq_mask_reg &= ~mask;
67036a4a7dSZhenyu Wang 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
68036a4a7dSZhenyu Wang 		(void) I915_READ(GTIMR);
69036a4a7dSZhenyu Wang 	}
70036a4a7dSZhenyu Wang }
71036a4a7dSZhenyu Wang 
72036a4a7dSZhenyu Wang static inline void
73036a4a7dSZhenyu Wang igdng_disable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
74036a4a7dSZhenyu Wang {
75036a4a7dSZhenyu Wang 	if ((dev_priv->gt_irq_mask_reg & mask) != mask) {
76036a4a7dSZhenyu Wang 		dev_priv->gt_irq_mask_reg |= mask;
77036a4a7dSZhenyu Wang 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
78036a4a7dSZhenyu Wang 		(void) I915_READ(GTIMR);
79036a4a7dSZhenyu Wang 	}
80036a4a7dSZhenyu Wang }
81036a4a7dSZhenyu Wang 
82036a4a7dSZhenyu Wang /* For display hotplug interrupt */
83036a4a7dSZhenyu Wang void
84036a4a7dSZhenyu Wang igdng_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
85036a4a7dSZhenyu Wang {
86036a4a7dSZhenyu Wang 	if ((dev_priv->irq_mask_reg & mask) != 0) {
87036a4a7dSZhenyu Wang 		dev_priv->irq_mask_reg &= ~mask;
88036a4a7dSZhenyu Wang 		I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
89036a4a7dSZhenyu Wang 		(void) I915_READ(DEIMR);
90036a4a7dSZhenyu Wang 	}
91036a4a7dSZhenyu Wang }
92036a4a7dSZhenyu Wang 
93036a4a7dSZhenyu Wang static inline void
94036a4a7dSZhenyu Wang igdng_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
95036a4a7dSZhenyu Wang {
96036a4a7dSZhenyu Wang 	if ((dev_priv->irq_mask_reg & mask) != mask) {
97036a4a7dSZhenyu Wang 		dev_priv->irq_mask_reg |= mask;
98036a4a7dSZhenyu Wang 		I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
99036a4a7dSZhenyu Wang 		(void) I915_READ(DEIMR);
100036a4a7dSZhenyu Wang 	}
101036a4a7dSZhenyu Wang }
102036a4a7dSZhenyu Wang 
103036a4a7dSZhenyu Wang void
104ed4cb414SEric Anholt i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
105ed4cb414SEric Anholt {
106ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != 0) {
107ed4cb414SEric Anholt 		dev_priv->irq_mask_reg &= ~mask;
108ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
109ed4cb414SEric Anholt 		(void) I915_READ(IMR);
110ed4cb414SEric Anholt 	}
111ed4cb414SEric Anholt }
112ed4cb414SEric Anholt 
113ed4cb414SEric Anholt static inline void
114ed4cb414SEric Anholt i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
115ed4cb414SEric Anholt {
116ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != mask) {
117ed4cb414SEric Anholt 		dev_priv->irq_mask_reg |= mask;
118ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
119ed4cb414SEric Anholt 		(void) I915_READ(IMR);
120ed4cb414SEric Anholt 	}
121ed4cb414SEric Anholt }
122ed4cb414SEric Anholt 
1237c463586SKeith Packard static inline u32
1247c463586SKeith Packard i915_pipestat(int pipe)
1257c463586SKeith Packard {
1267c463586SKeith Packard 	if (pipe == 0)
1277c463586SKeith Packard 		return PIPEASTAT;
1287c463586SKeith Packard 	if (pipe == 1)
1297c463586SKeith Packard 		return PIPEBSTAT;
1309c84ba4eSAndrew Morton 	BUG();
1317c463586SKeith Packard }
1327c463586SKeith Packard 
1337c463586SKeith Packard void
1347c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1357c463586SKeith Packard {
1367c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
1377c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1387c463586SKeith Packard 
1397c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
1407c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
1417c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
1427c463586SKeith Packard 		(void) I915_READ(reg);
1437c463586SKeith Packard 	}
1447c463586SKeith Packard }
1457c463586SKeith Packard 
1467c463586SKeith Packard void
1477c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1487c463586SKeith Packard {
1497c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
1507c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1517c463586SKeith Packard 
1527c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
1537c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
1547c463586SKeith Packard 		(void) I915_READ(reg);
1557c463586SKeith Packard 	}
1567c463586SKeith Packard }
1577c463586SKeith Packard 
158c0e09200SDave Airlie /**
1590a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1600a3e67a4SJesse Barnes  * @dev: DRM device
1610a3e67a4SJesse Barnes  * @pipe: pipe to check
1620a3e67a4SJesse Barnes  *
1630a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1640a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1650a3e67a4SJesse Barnes  * before reading such registers if unsure.
1660a3e67a4SJesse Barnes  */
1670a3e67a4SJesse Barnes static int
1680a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1690a3e67a4SJesse Barnes {
1700a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1710a3e67a4SJesse Barnes 	unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
1720a3e67a4SJesse Barnes 
1730a3e67a4SJesse Barnes 	if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
1740a3e67a4SJesse Barnes 		return 1;
1750a3e67a4SJesse Barnes 
1760a3e67a4SJesse Barnes 	return 0;
1770a3e67a4SJesse Barnes }
1780a3e67a4SJesse Barnes 
17942f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
18042f52ef8SKeith Packard  * we use as a pipe index
18142f52ef8SKeith Packard  */
18242f52ef8SKeith Packard u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1830a3e67a4SJesse Barnes {
1840a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1850a3e67a4SJesse Barnes 	unsigned long high_frame;
1860a3e67a4SJesse Barnes 	unsigned long low_frame;
1870a3e67a4SJesse Barnes 	u32 high1, high2, low, count;
1880a3e67a4SJesse Barnes 
1890a3e67a4SJesse Barnes 	high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
1900a3e67a4SJesse Barnes 	low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
1910a3e67a4SJesse Barnes 
1920a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
1936cb504c2SFrans Pop 		DRM_DEBUG("trying to get vblank count for disabled pipe %d\n", pipe);
1940a3e67a4SJesse Barnes 		return 0;
1950a3e67a4SJesse Barnes 	}
1960a3e67a4SJesse Barnes 
1970a3e67a4SJesse Barnes 	/*
1980a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
1990a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
2000a3e67a4SJesse Barnes 	 * register.
2010a3e67a4SJesse Barnes 	 */
2020a3e67a4SJesse Barnes 	do {
2030a3e67a4SJesse Barnes 		high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
2040a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
2050a3e67a4SJesse Barnes 		low =  ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
2060a3e67a4SJesse Barnes 			PIPE_FRAME_LOW_SHIFT);
2070a3e67a4SJesse Barnes 		high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
2080a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
2090a3e67a4SJesse Barnes 	} while (high1 != high2);
2100a3e67a4SJesse Barnes 
2110a3e67a4SJesse Barnes 	count = (high1 << 8) | low;
2120a3e67a4SJesse Barnes 
2130a3e67a4SJesse Barnes 	return count;
2140a3e67a4SJesse Barnes }
2150a3e67a4SJesse Barnes 
2169880b7a5SJesse Barnes u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
2179880b7a5SJesse Barnes {
2189880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2199880b7a5SJesse Barnes 	int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
2209880b7a5SJesse Barnes 
2219880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
2226cb504c2SFrans Pop 		DRM_DEBUG("trying to get vblank count for disabled pipe %d\n", pipe);
2239880b7a5SJesse Barnes 		return 0;
2249880b7a5SJesse Barnes 	}
2259880b7a5SJesse Barnes 
2269880b7a5SJesse Barnes 	return I915_READ(reg);
2279880b7a5SJesse Barnes }
2289880b7a5SJesse Barnes 
2295ca58282SJesse Barnes /*
2305ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
2315ca58282SJesse Barnes  */
2325ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
2335ca58282SJesse Barnes {
2345ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
2355ca58282SJesse Barnes 						    hotplug_work);
2365ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
237c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
238c31c4ba3SKeith Packard 	struct drm_connector *connector;
2395ca58282SJesse Barnes 
240c31c4ba3SKeith Packard 	if (mode_config->num_connector) {
241c31c4ba3SKeith Packard 		list_for_each_entry(connector, &mode_config->connector_list, head) {
242c31c4ba3SKeith Packard 			struct intel_output *intel_output = to_intel_output(connector);
243c31c4ba3SKeith Packard 
244c31c4ba3SKeith Packard 			if (intel_output->hot_plug)
245c31c4ba3SKeith Packard 				(*intel_output->hot_plug) (intel_output);
246c31c4ba3SKeith Packard 		}
247c31c4ba3SKeith Packard 	}
2485ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
2495ca58282SJesse Barnes 	drm_sysfs_hotplug_event(dev);
2505ca58282SJesse Barnes }
2515ca58282SJesse Barnes 
252036a4a7dSZhenyu Wang irqreturn_t igdng_irq_handler(struct drm_device *dev)
253036a4a7dSZhenyu Wang {
254036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
255036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
256036a4a7dSZhenyu Wang 	u32 de_iir, gt_iir;
257036a4a7dSZhenyu Wang 	u32 new_de_iir, new_gt_iir;
258036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
259036a4a7dSZhenyu Wang 
260036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
261036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
262036a4a7dSZhenyu Wang 
263036a4a7dSZhenyu Wang 	for (;;) {
264036a4a7dSZhenyu Wang 		if (de_iir == 0 && gt_iir == 0)
265036a4a7dSZhenyu Wang 			break;
266036a4a7dSZhenyu Wang 
267036a4a7dSZhenyu Wang 		ret = IRQ_HANDLED;
268036a4a7dSZhenyu Wang 
269036a4a7dSZhenyu Wang 		I915_WRITE(DEIIR, de_iir);
270036a4a7dSZhenyu Wang 		new_de_iir = I915_READ(DEIIR);
271036a4a7dSZhenyu Wang 		I915_WRITE(GTIIR, gt_iir);
272036a4a7dSZhenyu Wang 		new_gt_iir = I915_READ(GTIIR);
273036a4a7dSZhenyu Wang 
274036a4a7dSZhenyu Wang 		if (dev->primary->master) {
275036a4a7dSZhenyu Wang 			master_priv = dev->primary->master->driver_priv;
276036a4a7dSZhenyu Wang 			if (master_priv->sarea_priv)
277036a4a7dSZhenyu Wang 				master_priv->sarea_priv->last_dispatch =
278036a4a7dSZhenyu Wang 					READ_BREADCRUMB(dev_priv);
279036a4a7dSZhenyu Wang 		}
280036a4a7dSZhenyu Wang 
281036a4a7dSZhenyu Wang 		if (gt_iir & GT_USER_INTERRUPT) {
282036a4a7dSZhenyu Wang 			dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
283036a4a7dSZhenyu Wang 			DRM_WAKEUP(&dev_priv->irq_queue);
284036a4a7dSZhenyu Wang 		}
285036a4a7dSZhenyu Wang 
286036a4a7dSZhenyu Wang 		de_iir = new_de_iir;
287036a4a7dSZhenyu Wang 		gt_iir = new_gt_iir;
288036a4a7dSZhenyu Wang 	}
289036a4a7dSZhenyu Wang 
290036a4a7dSZhenyu Wang 	return ret;
291036a4a7dSZhenyu Wang }
292036a4a7dSZhenyu Wang 
2938a905236SJesse Barnes /**
2948a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
2958a905236SJesse Barnes  * @work: work struct
2968a905236SJesse Barnes  *
2978a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
2988a905236SJesse Barnes  * was detected.
2998a905236SJesse Barnes  */
3008a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
3018a905236SJesse Barnes {
3028a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3038a905236SJesse Barnes 						    error_work);
3048a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
305*f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
306*f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
307*f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
3088a905236SJesse Barnes 
3098a905236SJesse Barnes 	DRM_DEBUG("generating error event\n");
310*f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
3118a905236SJesse Barnes 
312*f316a42cSBen Gamari 	if (dev_priv->mm.wedged) {
313*f316a42cSBen Gamari 		if (IS_I965G(dev)) {
314*f316a42cSBen Gamari 			DRM_DEBUG("resetting chip\n");
315*f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
316*f316a42cSBen Gamari 			if (!i965_reset(dev, GDRST_RENDER)) {
317*f316a42cSBen Gamari 				dev_priv->mm.wedged = 0;
318*f316a42cSBen Gamari 				kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
319*f316a42cSBen Gamari 			}
320*f316a42cSBen Gamari 		} else {
321*f316a42cSBen Gamari 			printk("reboot required\n");
322*f316a42cSBen Gamari 		}
323*f316a42cSBen Gamari 	}
3248a905236SJesse Barnes }
3258a905236SJesse Barnes 
3268a905236SJesse Barnes /**
3278a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
3288a905236SJesse Barnes  * @dev: drm device
3298a905236SJesse Barnes  *
3308a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
3318a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
3328a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
3338a905236SJesse Barnes  * to pick up.
3348a905236SJesse Barnes  */
33563eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
33663eeaf38SJesse Barnes {
33763eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
33863eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
33963eeaf38SJesse Barnes 	unsigned long flags;
34063eeaf38SJesse Barnes 
34163eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
34263eeaf38SJesse Barnes 	if (dev_priv->first_error)
34363eeaf38SJesse Barnes 		goto out;
34463eeaf38SJesse Barnes 
34563eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
34663eeaf38SJesse Barnes 	if (!error) {
34763eeaf38SJesse Barnes 		DRM_DEBUG("out ot memory, not capturing error state\n");
34863eeaf38SJesse Barnes 		goto out;
34963eeaf38SJesse Barnes 	}
35063eeaf38SJesse Barnes 
35163eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
35263eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
35363eeaf38SJesse Barnes 	error->pipeastat = I915_READ(PIPEASTAT);
35463eeaf38SJesse Barnes 	error->pipebstat = I915_READ(PIPEBSTAT);
35563eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
35663eeaf38SJesse Barnes 	if (!IS_I965G(dev)) {
35763eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR);
35863eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR);
35963eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE);
36063eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD);
36163eeaf38SJesse Barnes 	} else {
36263eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
36363eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
36463eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
36563eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
36663eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
36763eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
36863eeaf38SJesse Barnes 	}
36963eeaf38SJesse Barnes 
3708a905236SJesse Barnes 	do_gettimeofday(&error->time);
3718a905236SJesse Barnes 
37263eeaf38SJesse Barnes 	dev_priv->first_error = error;
37363eeaf38SJesse Barnes 
37463eeaf38SJesse Barnes out:
37563eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
37663eeaf38SJesse Barnes }
37763eeaf38SJesse Barnes 
3788a905236SJesse Barnes /**
3798a905236SJesse Barnes  * i915_handle_error - handle an error interrupt
3808a905236SJesse Barnes  * @dev: drm device
3818a905236SJesse Barnes  *
3828a905236SJesse Barnes  * Do some basic checking of regsiter state at error interrupt time and
3838a905236SJesse Barnes  * dump it to the syslog.  Also call i915_capture_error_state() to make
3848a905236SJesse Barnes  * sure we get a record and make it available in debugfs.  Fire a uevent
3858a905236SJesse Barnes  * so userspace knows something bad happened (should trigger collection
3868a905236SJesse Barnes  * of a ring dump etc.).
3878a905236SJesse Barnes  */
3888a905236SJesse Barnes static void i915_handle_error(struct drm_device *dev)
389c0e09200SDave Airlie {
3908a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
39163eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
3928a905236SJesse Barnes 	u32 pipea_stats = I915_READ(PIPEASTAT);
3938a905236SJesse Barnes 	u32 pipeb_stats = I915_READ(PIPEBSTAT);
39463eeaf38SJesse Barnes 
39563eeaf38SJesse Barnes 	i915_capture_error_state(dev);
39663eeaf38SJesse Barnes 
39763eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
39863eeaf38SJesse Barnes 	       eir);
3998a905236SJesse Barnes 
4008a905236SJesse Barnes 	if (IS_G4X(dev)) {
4018a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
4028a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
4038a905236SJesse Barnes 
4048a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
4058a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
4068a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
4078a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
4088a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
4098a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
4108a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
4118a905236SJesse Barnes 			       I915_READ(INSTPS));
4128a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
4138a905236SJesse Barnes 			       I915_READ(INSTDONE1));
4148a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
4158a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
4168a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
4178a905236SJesse Barnes 			(void)I915_READ(IPEIR_I965);
4188a905236SJesse Barnes 		}
4198a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
4208a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
4218a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
4228a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
4238a905236SJesse Barnes 			       pgtbl_err);
4248a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
4258a905236SJesse Barnes 			(void)I915_READ(PGTBL_ER);
4268a905236SJesse Barnes 		}
4278a905236SJesse Barnes 	}
4288a905236SJesse Barnes 
4298a905236SJesse Barnes 	if (IS_I9XX(dev)) {
43063eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
43163eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
43263eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
43363eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
43463eeaf38SJesse Barnes 			       pgtbl_err);
43563eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
43663eeaf38SJesse Barnes 			(void)I915_READ(PGTBL_ER);
43763eeaf38SJesse Barnes 		}
4388a905236SJesse Barnes 	}
4398a905236SJesse Barnes 
44063eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
44163eeaf38SJesse Barnes 		printk(KERN_ERR "memory refresh error\n");
44263eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
44363eeaf38SJesse Barnes 		       pipea_stats);
44463eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
44563eeaf38SJesse Barnes 		       pipeb_stats);
44663eeaf38SJesse Barnes 		/* pipestat has already been acked */
44763eeaf38SJesse Barnes 	}
44863eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
44963eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
45063eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
45163eeaf38SJesse Barnes 		       I915_READ(INSTPM));
45263eeaf38SJesse Barnes 		if (!IS_I965G(dev)) {
45363eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
45463eeaf38SJesse Barnes 
45563eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
45663eeaf38SJesse Barnes 			       I915_READ(IPEIR));
45763eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
45863eeaf38SJesse Barnes 			       I915_READ(IPEHR));
45963eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
46063eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
46163eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
46263eeaf38SJesse Barnes 			       I915_READ(ACTHD));
46363eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
46463eeaf38SJesse Barnes 			(void)I915_READ(IPEIR);
46563eeaf38SJesse Barnes 		} else {
46663eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
46763eeaf38SJesse Barnes 
46863eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
46963eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
47063eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
47163eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
47263eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
47363eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
47463eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
47563eeaf38SJesse Barnes 			       I915_READ(INSTPS));
47663eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
47763eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
47863eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
47963eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
48063eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
48163eeaf38SJesse Barnes 			(void)I915_READ(IPEIR_I965);
48263eeaf38SJesse Barnes 		}
48363eeaf38SJesse Barnes 	}
48463eeaf38SJesse Barnes 
48563eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
48663eeaf38SJesse Barnes 	(void)I915_READ(EIR);
48763eeaf38SJesse Barnes 	eir = I915_READ(EIR);
48863eeaf38SJesse Barnes 	if (eir) {
48963eeaf38SJesse Barnes 		/*
49063eeaf38SJesse Barnes 		 * some errors might have become stuck,
49163eeaf38SJesse Barnes 		 * mask them.
49263eeaf38SJesse Barnes 		 */
49363eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
49463eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
49563eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
49663eeaf38SJesse Barnes 	}
4978a905236SJesse Barnes 
49811ed50ecSBen Gamari 	if (dev_priv->mm.wedged) {
49911ed50ecSBen Gamari 		/*
50011ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
50111ed50ecSBen Gamari 		 */
50211ed50ecSBen Gamari 		printk("i915: Waking up sleeping processes\n");
50311ed50ecSBen Gamari 		DRM_WAKEUP(&dev_priv->irq_queue);
50411ed50ecSBen Gamari 	}
50511ed50ecSBen Gamari 
5069c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
5078a905236SJesse Barnes }
5088a905236SJesse Barnes 
5098a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
5108a905236SJesse Barnes {
5118a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
5128a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5138a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
5148a905236SJesse Barnes 	u32 iir, new_iir;
5158a905236SJesse Barnes 	u32 pipea_stats, pipeb_stats;
5168a905236SJesse Barnes 	u32 vblank_status;
5178a905236SJesse Barnes 	u32 vblank_enable;
5188a905236SJesse Barnes 	int vblank = 0;
5198a905236SJesse Barnes 	unsigned long irqflags;
5208a905236SJesse Barnes 	int irq_received;
5218a905236SJesse Barnes 	int ret = IRQ_NONE;
5228a905236SJesse Barnes 
5238a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5248a905236SJesse Barnes 
5258a905236SJesse Barnes 	if (IS_IGDNG(dev))
5268a905236SJesse Barnes 		return igdng_irq_handler(dev);
5278a905236SJesse Barnes 
5288a905236SJesse Barnes 	iir = I915_READ(IIR);
5298a905236SJesse Barnes 
5308a905236SJesse Barnes 	if (IS_I965G(dev)) {
5318a905236SJesse Barnes 		vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
5328a905236SJesse Barnes 		vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
5338a905236SJesse Barnes 	} else {
5348a905236SJesse Barnes 		vblank_status = I915_VBLANK_INTERRUPT_STATUS;
5358a905236SJesse Barnes 		vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
5368a905236SJesse Barnes 	}
5378a905236SJesse Barnes 
5388a905236SJesse Barnes 	for (;;) {
5398a905236SJesse Barnes 		irq_received = iir != 0;
5408a905236SJesse Barnes 
5418a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
5428a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
5438a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
5448a905236SJesse Barnes 		 * interrupts (for non-MSI).
5458a905236SJesse Barnes 		 */
5468a905236SJesse Barnes 		spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
5478a905236SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
5488a905236SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
5498a905236SJesse Barnes 
5508a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
5518a905236SJesse Barnes 			i915_handle_error(dev);
5528a905236SJesse Barnes 
5538a905236SJesse Barnes 		/*
5548a905236SJesse Barnes 		 * Clear the PIPE(A|B)STAT regs before the IIR
5558a905236SJesse Barnes 		 */
5568a905236SJesse Barnes 		if (pipea_stats & 0x8000ffff) {
5578a905236SJesse Barnes 			if (pipea_stats &  PIPE_FIFO_UNDERRUN_STATUS)
5588a905236SJesse Barnes 				DRM_DEBUG("pipe a underrun\n");
5598a905236SJesse Barnes 			I915_WRITE(PIPEASTAT, pipea_stats);
5608a905236SJesse Barnes 			irq_received = 1;
5618a905236SJesse Barnes 		}
5628a905236SJesse Barnes 
5638a905236SJesse Barnes 		if (pipeb_stats & 0x8000ffff) {
5648a905236SJesse Barnes 			if (pipeb_stats &  PIPE_FIFO_UNDERRUN_STATUS)
5658a905236SJesse Barnes 				DRM_DEBUG("pipe b underrun\n");
5668a905236SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
5678a905236SJesse Barnes 			irq_received = 1;
5688a905236SJesse Barnes 		}
5698a905236SJesse Barnes 		spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
5708a905236SJesse Barnes 
5718a905236SJesse Barnes 		if (!irq_received)
5728a905236SJesse Barnes 			break;
5738a905236SJesse Barnes 
5748a905236SJesse Barnes 		ret = IRQ_HANDLED;
5758a905236SJesse Barnes 
5768a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
5778a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
5788a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
5798a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
5808a905236SJesse Barnes 
5818a905236SJesse Barnes 			DRM_DEBUG("hotplug event received, stat 0x%08x\n",
5828a905236SJesse Barnes 				  hotplug_status);
5838a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
5849c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
5859c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
5868a905236SJesse Barnes 
5878a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
5888a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
58904302965SShaohua Li 
59004302965SShaohua Li 			/* EOS interrupts occurs */
59104302965SShaohua Li 			if (IS_IGD(dev) &&
59204302965SShaohua Li 				(hotplug_status & CRT_EOS_INT_STATUS)) {
59304302965SShaohua Li 				u32 temp;
59404302965SShaohua Li 
59504302965SShaohua Li 				DRM_DEBUG("EOS interrupt occurs\n");
59604302965SShaohua Li 				/* status is already cleared */
59704302965SShaohua Li 				temp = I915_READ(ADPA);
59804302965SShaohua Li 				temp &= ~ADPA_DAC_ENABLE;
59904302965SShaohua Li 				I915_WRITE(ADPA, temp);
60004302965SShaohua Li 
60104302965SShaohua Li 				temp = I915_READ(PORT_HOTPLUG_EN);
60204302965SShaohua Li 				temp &= ~CRT_EOS_INT_EN;
60304302965SShaohua Li 				I915_WRITE(PORT_HOTPLUG_EN, temp);
60404302965SShaohua Li 
60504302965SShaohua Li 				temp = I915_READ(PORT_HOTPLUG_STAT);
60604302965SShaohua Li 				if (temp & CRT_EOS_INT_STATUS)
60704302965SShaohua Li 					I915_WRITE(PORT_HOTPLUG_STAT,
60804302965SShaohua Li 						CRT_EOS_INT_STATUS);
60904302965SShaohua Li 			}
61063eeaf38SJesse Barnes 		}
61163eeaf38SJesse Barnes 
612673a394bSEric Anholt 		I915_WRITE(IIR, iir);
613cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
6147c463586SKeith Packard 
6157c1c2871SDave Airlie 		if (dev->primary->master) {
6167c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
6177c1c2871SDave Airlie 			if (master_priv->sarea_priv)
6187c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
619c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
6207c1c2871SDave Airlie 		}
6210a3e67a4SJesse Barnes 
622673a394bSEric Anholt 		if (iir & I915_USER_INTERRUPT) {
623673a394bSEric Anholt 			dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
624673a394bSEric Anholt 			DRM_WAKEUP(&dev_priv->irq_queue);
625f65d9421SBen Gamari 			dev_priv->hangcheck_count = 0;
626f65d9421SBen Gamari 			mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
627673a394bSEric Anholt 		}
628673a394bSEric Anholt 
62905eff845SKeith Packard 		if (pipea_stats & vblank_status) {
6307c463586SKeith Packard 			vblank++;
6317c463586SKeith Packard 			drm_handle_vblank(dev, 0);
6327c463586SKeith Packard 		}
6337c463586SKeith Packard 
63405eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
6357c463586SKeith Packard 			vblank++;
6367c463586SKeith Packard 			drm_handle_vblank(dev, 1);
6377c463586SKeith Packard 		}
6387c463586SKeith Packard 
6397c463586SKeith Packard 		if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
6407c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
641673a394bSEric Anholt 			opregion_asle_intr(dev);
6420a3e67a4SJesse Barnes 
643cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
644cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
645cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
646cdfbc41fSEric Anholt 		 * we would never get another interrupt.
647cdfbc41fSEric Anholt 		 *
648cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
649cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
650cdfbc41fSEric Anholt 		 * another one.
651cdfbc41fSEric Anholt 		 *
652cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
653cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
654cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
655cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
656cdfbc41fSEric Anholt 		 * stray interrupts.
657cdfbc41fSEric Anholt 		 */
658cdfbc41fSEric Anholt 		iir = new_iir;
65905eff845SKeith Packard 	}
660cdfbc41fSEric Anholt 
66105eff845SKeith Packard 	return ret;
662c0e09200SDave Airlie }
663c0e09200SDave Airlie 
664c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
665c0e09200SDave Airlie {
666c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
6677c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
668c0e09200SDave Airlie 	RING_LOCALS;
669c0e09200SDave Airlie 
670c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
671c0e09200SDave Airlie 
672c0e09200SDave Airlie 	DRM_DEBUG("\n");
673c0e09200SDave Airlie 
674c99b058fSKristian Høgsberg 	dev_priv->counter++;
675c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
676c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
6777c1c2871SDave Airlie 	if (master_priv->sarea_priv)
6787c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
679c0e09200SDave Airlie 
6800baf823aSKeith Packard 	BEGIN_LP_RING(4);
681585fb111SJesse Barnes 	OUT_RING(MI_STORE_DWORD_INDEX);
6820baf823aSKeith Packard 	OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
683c0e09200SDave Airlie 	OUT_RING(dev_priv->counter);
684585fb111SJesse Barnes 	OUT_RING(MI_USER_INTERRUPT);
685c0e09200SDave Airlie 	ADVANCE_LP_RING();
686c0e09200SDave Airlie 
687c0e09200SDave Airlie 	return dev_priv->counter;
688c0e09200SDave Airlie }
689c0e09200SDave Airlie 
690673a394bSEric Anholt void i915_user_irq_get(struct drm_device *dev)
691ed4cb414SEric Anholt {
692ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
693e9d21d7fSKeith Packard 	unsigned long irqflags;
694ed4cb414SEric Anholt 
695e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
696036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
697036a4a7dSZhenyu Wang 		if (IS_IGDNG(dev))
698036a4a7dSZhenyu Wang 			igdng_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
699036a4a7dSZhenyu Wang 		else
700ed4cb414SEric Anholt 			i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
701036a4a7dSZhenyu Wang 	}
702e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
703ed4cb414SEric Anholt }
704ed4cb414SEric Anholt 
7050a3e67a4SJesse Barnes void i915_user_irq_put(struct drm_device *dev)
706ed4cb414SEric Anholt {
707ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
708e9d21d7fSKeith Packard 	unsigned long irqflags;
709ed4cb414SEric Anholt 
710e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
711ed4cb414SEric Anholt 	BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
712036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
713036a4a7dSZhenyu Wang 		if (IS_IGDNG(dev))
714036a4a7dSZhenyu Wang 			igdng_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
715036a4a7dSZhenyu Wang 		else
716ed4cb414SEric Anholt 			i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
717036a4a7dSZhenyu Wang 	}
718e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
719ed4cb414SEric Anholt }
720ed4cb414SEric Anholt 
721c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
722c0e09200SDave Airlie {
723c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7247c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
725c0e09200SDave Airlie 	int ret = 0;
726c0e09200SDave Airlie 
727c0e09200SDave Airlie 	DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
728c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
729c0e09200SDave Airlie 
730ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
7317c1c2871SDave Airlie 		if (master_priv->sarea_priv)
7327c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
733c0e09200SDave Airlie 		return 0;
734ed4cb414SEric Anholt 	}
735c0e09200SDave Airlie 
7367c1c2871SDave Airlie 	if (master_priv->sarea_priv)
7377c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
738c0e09200SDave Airlie 
739ed4cb414SEric Anholt 	i915_user_irq_get(dev);
740c0e09200SDave Airlie 	DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
741c0e09200SDave Airlie 		    READ_BREADCRUMB(dev_priv) >= irq_nr);
742ed4cb414SEric Anholt 	i915_user_irq_put(dev);
743c0e09200SDave Airlie 
744c0e09200SDave Airlie 	if (ret == -EBUSY) {
745c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
746c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
747c0e09200SDave Airlie 	}
748c0e09200SDave Airlie 
749c0e09200SDave Airlie 	return ret;
750c0e09200SDave Airlie }
751c0e09200SDave Airlie 
752c0e09200SDave Airlie /* Needs the lock as it touches the ring.
753c0e09200SDave Airlie  */
754c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
755c0e09200SDave Airlie 			 struct drm_file *file_priv)
756c0e09200SDave Airlie {
757c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
758c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
759c0e09200SDave Airlie 	int result;
760c0e09200SDave Airlie 
76107f4f8bfSEric Anholt 	if (!dev_priv || !dev_priv->ring.virtual_start) {
762c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
763c0e09200SDave Airlie 		return -EINVAL;
764c0e09200SDave Airlie 	}
765299eb93cSEric Anholt 
766299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
767299eb93cSEric Anholt 
768546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
769c0e09200SDave Airlie 	result = i915_emit_irq(dev);
770546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
771c0e09200SDave Airlie 
772c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
773c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
774c0e09200SDave Airlie 		return -EFAULT;
775c0e09200SDave Airlie 	}
776c0e09200SDave Airlie 
777c0e09200SDave Airlie 	return 0;
778c0e09200SDave Airlie }
779c0e09200SDave Airlie 
780c0e09200SDave Airlie /* Doesn't need the hardware lock.
781c0e09200SDave Airlie  */
782c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
783c0e09200SDave Airlie 			 struct drm_file *file_priv)
784c0e09200SDave Airlie {
785c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
786c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
787c0e09200SDave Airlie 
788c0e09200SDave Airlie 	if (!dev_priv) {
789c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
790c0e09200SDave Airlie 		return -EINVAL;
791c0e09200SDave Airlie 	}
792c0e09200SDave Airlie 
793c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
794c0e09200SDave Airlie }
795c0e09200SDave Airlie 
79642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
79742f52ef8SKeith Packard  * we use as a pipe index
79842f52ef8SKeith Packard  */
79942f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
8000a3e67a4SJesse Barnes {
8010a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
802e9d21d7fSKeith Packard 	unsigned long irqflags;
80371e0ffa5SJesse Barnes 	int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
80471e0ffa5SJesse Barnes 	u32 pipeconf;
80571e0ffa5SJesse Barnes 
80671e0ffa5SJesse Barnes 	pipeconf = I915_READ(pipeconf_reg);
80771e0ffa5SJesse Barnes 	if (!(pipeconf & PIPEACONF_ENABLE))
80871e0ffa5SJesse Barnes 		return -EINVAL;
8090a3e67a4SJesse Barnes 
810036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev))
811036a4a7dSZhenyu Wang 		return 0;
812036a4a7dSZhenyu Wang 
813e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
8140a3e67a4SJesse Barnes 	if (IS_I965G(dev))
8157c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
8167c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
8170a3e67a4SJesse Barnes 	else
8187c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
8197c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
820e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
8210a3e67a4SJesse Barnes 	return 0;
8220a3e67a4SJesse Barnes }
8230a3e67a4SJesse Barnes 
82442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
82542f52ef8SKeith Packard  * we use as a pipe index
82642f52ef8SKeith Packard  */
82742f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
8280a3e67a4SJesse Barnes {
8290a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
830e9d21d7fSKeith Packard 	unsigned long irqflags;
8310a3e67a4SJesse Barnes 
832036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev))
833036a4a7dSZhenyu Wang 		return;
834036a4a7dSZhenyu Wang 
835e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
8367c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
8377c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
8387c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
839e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
8400a3e67a4SJesse Barnes }
8410a3e67a4SJesse Barnes 
84279e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev)
84379e53945SJesse Barnes {
84479e53945SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
845e170b030SZhenyu Wang 
846e170b030SZhenyu Wang 	if (!IS_IGDNG(dev))
84779e53945SJesse Barnes 		opregion_enable_asle(dev);
84879e53945SJesse Barnes 	dev_priv->irq_enabled = 1;
84979e53945SJesse Barnes }
85079e53945SJesse Barnes 
85179e53945SJesse Barnes 
852c0e09200SDave Airlie /* Set the vblank monitor pipe
853c0e09200SDave Airlie  */
854c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
855c0e09200SDave Airlie 			 struct drm_file *file_priv)
856c0e09200SDave Airlie {
857c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
858c0e09200SDave Airlie 
859c0e09200SDave Airlie 	if (!dev_priv) {
860c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
861c0e09200SDave Airlie 		return -EINVAL;
862c0e09200SDave Airlie 	}
863c0e09200SDave Airlie 
864c0e09200SDave Airlie 	return 0;
865c0e09200SDave Airlie }
866c0e09200SDave Airlie 
867c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
868c0e09200SDave Airlie 			 struct drm_file *file_priv)
869c0e09200SDave Airlie {
870c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
871c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
872c0e09200SDave Airlie 
873c0e09200SDave Airlie 	if (!dev_priv) {
874c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
875c0e09200SDave Airlie 		return -EINVAL;
876c0e09200SDave Airlie 	}
877c0e09200SDave Airlie 
8780a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
879c0e09200SDave Airlie 
880c0e09200SDave Airlie 	return 0;
881c0e09200SDave Airlie }
882c0e09200SDave Airlie 
883c0e09200SDave Airlie /**
884c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
885c0e09200SDave Airlie  */
886c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
887c0e09200SDave Airlie 		     struct drm_file *file_priv)
888c0e09200SDave Airlie {
889bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
890bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
891bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
892bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
893bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
894bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
895bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
896bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
897bd95e0a4SEric Anholt 	 *
898bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
899bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
900bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
901bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
9020a3e67a4SJesse Barnes 	 */
903c0e09200SDave Airlie 	return -EINVAL;
904c0e09200SDave Airlie }
905c0e09200SDave Airlie 
906f65d9421SBen Gamari struct drm_i915_gem_request *i915_get_tail_request(struct drm_device *dev) {
907f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
908f65d9421SBen Gamari 	return list_entry(dev_priv->mm.request_list.prev, struct drm_i915_gem_request, list);
909f65d9421SBen Gamari }
910f65d9421SBen Gamari 
911f65d9421SBen Gamari /**
912f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
913f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
914f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
915f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
916f65d9421SBen Gamari  */
917f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
918f65d9421SBen Gamari {
919f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
920f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
921f65d9421SBen Gamari 	uint32_t acthd;
922f65d9421SBen Gamari 
923f65d9421SBen Gamari 	if (!IS_I965G(dev))
924f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
925f65d9421SBen Gamari 	else
926f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
927f65d9421SBen Gamari 
928f65d9421SBen Gamari 	/* If all work is done then ACTHD clearly hasn't advanced. */
929f65d9421SBen Gamari 	if (list_empty(&dev_priv->mm.request_list) ||
930f65d9421SBen Gamari 		       i915_seqno_passed(i915_get_gem_seqno(dev), i915_get_tail_request(dev)->seqno)) {
931f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
932f65d9421SBen Gamari 		return;
933f65d9421SBen Gamari 	}
934f65d9421SBen Gamari 
935f65d9421SBen Gamari 	if (dev_priv->last_acthd == acthd && dev_priv->hangcheck_count > 0) {
936f65d9421SBen Gamari 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
937f65d9421SBen Gamari 		dev_priv->mm.wedged = true; /* Hopefully this is atomic */
938f65d9421SBen Gamari 		i915_handle_error(dev);
939f65d9421SBen Gamari 		return;
940f65d9421SBen Gamari 	}
941f65d9421SBen Gamari 
942f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
943f65d9421SBen Gamari 	mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
944f65d9421SBen Gamari 
945f65d9421SBen Gamari 	if (acthd != dev_priv->last_acthd)
946f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
947f65d9421SBen Gamari 	else
948f65d9421SBen Gamari 		dev_priv->hangcheck_count++;
949f65d9421SBen Gamari 
950f65d9421SBen Gamari 	dev_priv->last_acthd = acthd;
951f65d9421SBen Gamari }
952f65d9421SBen Gamari 
953c0e09200SDave Airlie /* drm_dma.h hooks
954c0e09200SDave Airlie */
955036a4a7dSZhenyu Wang static void igdng_irq_preinstall(struct drm_device *dev)
956036a4a7dSZhenyu Wang {
957036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
958036a4a7dSZhenyu Wang 
959036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
960036a4a7dSZhenyu Wang 
961036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
962036a4a7dSZhenyu Wang 
963036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
964036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
965036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
966036a4a7dSZhenyu Wang 
967036a4a7dSZhenyu Wang 	/* and GT */
968036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
969036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
970036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
971036a4a7dSZhenyu Wang }
972036a4a7dSZhenyu Wang 
973036a4a7dSZhenyu Wang static int igdng_irq_postinstall(struct drm_device *dev)
974036a4a7dSZhenyu Wang {
975036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
976036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
977036a4a7dSZhenyu Wang 	u32 display_mask = DE_MASTER_IRQ_CONTROL /*| DE_PCH_EVENT */;
978036a4a7dSZhenyu Wang 	u32 render_mask = GT_USER_INTERRUPT;
979036a4a7dSZhenyu Wang 
980036a4a7dSZhenyu Wang 	dev_priv->irq_mask_reg = ~display_mask;
981036a4a7dSZhenyu Wang 	dev_priv->de_irq_enable_reg = display_mask;
982036a4a7dSZhenyu Wang 
983036a4a7dSZhenyu Wang 	/* should always can generate irq */
984036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
985036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
986036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
987036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
988036a4a7dSZhenyu Wang 
989036a4a7dSZhenyu Wang 	/* user interrupt should be enabled, but masked initial */
990036a4a7dSZhenyu Wang 	dev_priv->gt_irq_mask_reg = 0xffffffff;
991036a4a7dSZhenyu Wang 	dev_priv->gt_irq_enable_reg = render_mask;
992036a4a7dSZhenyu Wang 
993036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
994036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
995036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
996036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
997036a4a7dSZhenyu Wang 
998036a4a7dSZhenyu Wang 	return 0;
999036a4a7dSZhenyu Wang }
1000036a4a7dSZhenyu Wang 
1001c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1002c0e09200SDave Airlie {
1003c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1004c0e09200SDave Airlie 
100579e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
100679e53945SJesse Barnes 
1007036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
10088a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1009036a4a7dSZhenyu Wang 
1010036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev)) {
1011036a4a7dSZhenyu Wang 		igdng_irq_preinstall(dev);
1012036a4a7dSZhenyu Wang 		return;
1013036a4a7dSZhenyu Wang 	}
1014036a4a7dSZhenyu Wang 
10155ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
10165ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
10175ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
10185ca58282SJesse Barnes 	}
10195ca58282SJesse Barnes 
10200a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
10217c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
10227c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
10230a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1024ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
10257c463586SKeith Packard 	(void) I915_READ(IER);
1026c0e09200SDave Airlie }
1027c0e09200SDave Airlie 
10280a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1029c0e09200SDave Airlie {
1030c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10315ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
103263eeaf38SJesse Barnes 	u32 error_mask;
10330a3e67a4SJesse Barnes 
1034036a4a7dSZhenyu Wang 	DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
1035036a4a7dSZhenyu Wang 
10360a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1037ed4cb414SEric Anholt 
1038036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev))
1039036a4a7dSZhenyu Wang 		return igdng_irq_postinstall(dev);
1040036a4a7dSZhenyu Wang 
10417c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
10427c463586SKeith Packard 	dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
10438ee1c3dbSMatthew Garrett 
10447c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
10457c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
10467c463586SKeith Packard 
10475ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
10485ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
10495ca58282SJesse Barnes 
10505ca58282SJesse Barnes 		/* Leave other bits alone */
10515ca58282SJesse Barnes 		hotplug_en |= HOTPLUG_EN_MASK;
10525ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
10535ca58282SJesse Barnes 
10545ca58282SJesse Barnes 		dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
10555ca58282SJesse Barnes 			TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
10565ca58282SJesse Barnes 			SDVOB_HOTPLUG_INT_STATUS;
10575ca58282SJesse Barnes 		if (IS_G4X(dev)) {
10585ca58282SJesse Barnes 			dev_priv->hotplug_supported_mask |=
10595ca58282SJesse Barnes 				HDMIB_HOTPLUG_INT_STATUS |
10605ca58282SJesse Barnes 				HDMIC_HOTPLUG_INT_STATUS |
10615ca58282SJesse Barnes 				HDMID_HOTPLUG_INT_STATUS;
10625ca58282SJesse Barnes 		}
10635ca58282SJesse Barnes 		/* Enable in IER... */
10645ca58282SJesse Barnes 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
10655ca58282SJesse Barnes 		/* and unmask in IMR */
10665ca58282SJesse Barnes 		i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
10675ca58282SJesse Barnes 	}
10685ca58282SJesse Barnes 
106963eeaf38SJesse Barnes 	/*
107063eeaf38SJesse Barnes 	 * Enable some error detection, note the instruction error mask
107163eeaf38SJesse Barnes 	 * bit is reserved, so we leave it masked.
107263eeaf38SJesse Barnes 	 */
107363eeaf38SJesse Barnes 	if (IS_G4X(dev)) {
107463eeaf38SJesse Barnes 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
107563eeaf38SJesse Barnes 			       GM45_ERROR_MEM_PRIV |
107663eeaf38SJesse Barnes 			       GM45_ERROR_CP_PRIV |
107763eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
107863eeaf38SJesse Barnes 	} else {
107963eeaf38SJesse Barnes 		error_mask = ~(I915_ERROR_PAGE_TABLE |
108063eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
108163eeaf38SJesse Barnes 	}
108263eeaf38SJesse Barnes 	I915_WRITE(EMR, error_mask);
108363eeaf38SJesse Barnes 
10847c463586SKeith Packard 	/* Disable pipe interrupt enables, clear pending pipe status */
10857c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
10867c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
10877c463586SKeith Packard 	/* Clear pending interrupt status */
10887c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
10897c463586SKeith Packard 
10905ca58282SJesse Barnes 	I915_WRITE(IER, enable_mask);
10917c463586SKeith Packard 	I915_WRITE(IMR, dev_priv->irq_mask_reg);
1092ed4cb414SEric Anholt 	(void) I915_READ(IER);
1093ed4cb414SEric Anholt 
10948ee1c3dbSMatthew Garrett 	opregion_enable_asle(dev);
10950a3e67a4SJesse Barnes 
10960a3e67a4SJesse Barnes 	return 0;
1097c0e09200SDave Airlie }
1098c0e09200SDave Airlie 
1099036a4a7dSZhenyu Wang static void igdng_irq_uninstall(struct drm_device *dev)
1100036a4a7dSZhenyu Wang {
1101036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1102036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1103036a4a7dSZhenyu Wang 
1104036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1105036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1106036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1107036a4a7dSZhenyu Wang 
1108036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1109036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1110036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1111036a4a7dSZhenyu Wang }
1112036a4a7dSZhenyu Wang 
1113c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1114c0e09200SDave Airlie {
1115c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1116c0e09200SDave Airlie 
1117c0e09200SDave Airlie 	if (!dev_priv)
1118c0e09200SDave Airlie 		return;
1119c0e09200SDave Airlie 
11200a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
11210a3e67a4SJesse Barnes 
1122036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev)) {
1123036a4a7dSZhenyu Wang 		igdng_irq_uninstall(dev);
1124036a4a7dSZhenyu Wang 		return;
1125036a4a7dSZhenyu Wang 	}
1126036a4a7dSZhenyu Wang 
11275ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
11285ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
11295ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
11305ca58282SJesse Barnes 	}
11315ca58282SJesse Barnes 
11320a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
11337c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
11347c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
11350a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1136ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1137c0e09200SDave Airlie 
11387c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
11397c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
11407c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1141c0e09200SDave Airlie }
1142