xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 2d109a845dd3074885db726892c629ab73dd0ed8)
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  */
467c463586SKeith Packard #define I915_INTERRUPT_ENABLE_FIX (I915_ASLE_INTERRUPT |		 \
470a3e67a4SJesse Barnes 				   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
4863eeaf38SJesse Barnes 				   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT | \
4963eeaf38SJesse Barnes 				   I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
50ed4cb414SEric Anholt 
517c463586SKeith Packard /** Interrupts that we mask and unmask at runtime. */
527c463586SKeith Packard #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
537c463586SKeith Packard 
5479e53945SJesse Barnes #define I915_PIPE_VBLANK_STATUS	(PIPE_START_VBLANK_INTERRUPT_STATUS |\
5579e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_STATUS)
5679e53945SJesse Barnes 
5779e53945SJesse Barnes #define I915_PIPE_VBLANK_ENABLE	(PIPE_START_VBLANK_INTERRUPT_ENABLE |\
5879e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_ENABLE)
5979e53945SJesse Barnes 
6079e53945SJesse Barnes #define DRM_I915_VBLANK_PIPE_ALL	(DRM_I915_VBLANK_PIPE_A | \
6179e53945SJesse Barnes 					 DRM_I915_VBLANK_PIPE_B)
6279e53945SJesse Barnes 
638ee1c3dbSMatthew Garrett void
64036a4a7dSZhenyu Wang igdng_enable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
65036a4a7dSZhenyu Wang {
66036a4a7dSZhenyu Wang 	if ((dev_priv->gt_irq_mask_reg & mask) != 0) {
67036a4a7dSZhenyu Wang 		dev_priv->gt_irq_mask_reg &= ~mask;
68036a4a7dSZhenyu Wang 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
69036a4a7dSZhenyu Wang 		(void) I915_READ(GTIMR);
70036a4a7dSZhenyu Wang 	}
71036a4a7dSZhenyu Wang }
72036a4a7dSZhenyu Wang 
73036a4a7dSZhenyu Wang static inline void
74036a4a7dSZhenyu Wang igdng_disable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
75036a4a7dSZhenyu Wang {
76036a4a7dSZhenyu Wang 	if ((dev_priv->gt_irq_mask_reg & mask) != mask) {
77036a4a7dSZhenyu Wang 		dev_priv->gt_irq_mask_reg |= mask;
78036a4a7dSZhenyu Wang 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
79036a4a7dSZhenyu Wang 		(void) I915_READ(GTIMR);
80036a4a7dSZhenyu Wang 	}
81036a4a7dSZhenyu Wang }
82036a4a7dSZhenyu Wang 
83036a4a7dSZhenyu Wang /* For display hotplug interrupt */
84036a4a7dSZhenyu Wang void
85036a4a7dSZhenyu Wang igdng_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
86036a4a7dSZhenyu Wang {
87036a4a7dSZhenyu Wang 	if ((dev_priv->irq_mask_reg & mask) != 0) {
88036a4a7dSZhenyu Wang 		dev_priv->irq_mask_reg &= ~mask;
89036a4a7dSZhenyu Wang 		I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
90036a4a7dSZhenyu Wang 		(void) I915_READ(DEIMR);
91036a4a7dSZhenyu Wang 	}
92036a4a7dSZhenyu Wang }
93036a4a7dSZhenyu Wang 
94036a4a7dSZhenyu Wang static inline void
95036a4a7dSZhenyu Wang igdng_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
96036a4a7dSZhenyu Wang {
97036a4a7dSZhenyu Wang 	if ((dev_priv->irq_mask_reg & mask) != mask) {
98036a4a7dSZhenyu Wang 		dev_priv->irq_mask_reg |= mask;
99036a4a7dSZhenyu Wang 		I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
100036a4a7dSZhenyu Wang 		(void) I915_READ(DEIMR);
101036a4a7dSZhenyu Wang 	}
102036a4a7dSZhenyu Wang }
103036a4a7dSZhenyu Wang 
104036a4a7dSZhenyu Wang void
105ed4cb414SEric Anholt i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
106ed4cb414SEric Anholt {
107ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != 0) {
108ed4cb414SEric Anholt 		dev_priv->irq_mask_reg &= ~mask;
109ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
110ed4cb414SEric Anholt 		(void) I915_READ(IMR);
111ed4cb414SEric Anholt 	}
112ed4cb414SEric Anholt }
113ed4cb414SEric Anholt 
114ed4cb414SEric Anholt static inline void
115ed4cb414SEric Anholt i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
116ed4cb414SEric Anholt {
117ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != mask) {
118ed4cb414SEric Anholt 		dev_priv->irq_mask_reg |= mask;
119ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
120ed4cb414SEric Anholt 		(void) I915_READ(IMR);
121ed4cb414SEric Anholt 	}
122ed4cb414SEric Anholt }
123ed4cb414SEric Anholt 
1247c463586SKeith Packard static inline u32
1257c463586SKeith Packard i915_pipestat(int pipe)
1267c463586SKeith Packard {
1277c463586SKeith Packard 	if (pipe == 0)
1287c463586SKeith Packard 		return PIPEASTAT;
1297c463586SKeith Packard 	if (pipe == 1)
1307c463586SKeith Packard 		return PIPEBSTAT;
1319c84ba4eSAndrew Morton 	BUG();
1327c463586SKeith Packard }
1337c463586SKeith Packard 
1347c463586SKeith Packard void
1357c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1367c463586SKeith Packard {
1377c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
1387c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1397c463586SKeith Packard 
1407c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
1417c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
1427c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
1437c463586SKeith Packard 		(void) I915_READ(reg);
1447c463586SKeith Packard 	}
1457c463586SKeith Packard }
1467c463586SKeith Packard 
1477c463586SKeith Packard void
1487c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1497c463586SKeith Packard {
1507c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
1517c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1527c463586SKeith Packard 
1537c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
1547c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
1557c463586SKeith Packard 		(void) I915_READ(reg);
1567c463586SKeith Packard 	}
1577c463586SKeith Packard }
1587c463586SKeith Packard 
159c0e09200SDave Airlie /**
1600a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1610a3e67a4SJesse Barnes  * @dev: DRM device
1620a3e67a4SJesse Barnes  * @pipe: pipe to check
1630a3e67a4SJesse Barnes  *
1640a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1650a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1660a3e67a4SJesse Barnes  * before reading such registers if unsure.
1670a3e67a4SJesse Barnes  */
1680a3e67a4SJesse Barnes static int
1690a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1700a3e67a4SJesse Barnes {
1710a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1720a3e67a4SJesse Barnes 	unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
1730a3e67a4SJesse Barnes 
1740a3e67a4SJesse Barnes 	if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
1750a3e67a4SJesse Barnes 		return 1;
1760a3e67a4SJesse Barnes 
1770a3e67a4SJesse Barnes 	return 0;
1780a3e67a4SJesse Barnes }
1790a3e67a4SJesse Barnes 
18042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
18142f52ef8SKeith Packard  * we use as a pipe index
18242f52ef8SKeith Packard  */
18342f52ef8SKeith Packard u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1840a3e67a4SJesse Barnes {
1850a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1860a3e67a4SJesse Barnes 	unsigned long high_frame;
1870a3e67a4SJesse Barnes 	unsigned long low_frame;
1880a3e67a4SJesse Barnes 	u32 high1, high2, low, count;
1890a3e67a4SJesse Barnes 
1900a3e67a4SJesse Barnes 	high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
1910a3e67a4SJesse Barnes 	low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
1920a3e67a4SJesse Barnes 
1930a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
1946cb504c2SFrans Pop 		DRM_DEBUG("trying to get vblank count for disabled pipe %d\n", pipe);
1950a3e67a4SJesse Barnes 		return 0;
1960a3e67a4SJesse Barnes 	}
1970a3e67a4SJesse Barnes 
1980a3e67a4SJesse Barnes 	/*
1990a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
2000a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
2010a3e67a4SJesse Barnes 	 * register.
2020a3e67a4SJesse Barnes 	 */
2030a3e67a4SJesse Barnes 	do {
2040a3e67a4SJesse Barnes 		high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
2050a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
2060a3e67a4SJesse Barnes 		low =  ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
2070a3e67a4SJesse Barnes 			PIPE_FRAME_LOW_SHIFT);
2080a3e67a4SJesse Barnes 		high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
2090a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
2100a3e67a4SJesse Barnes 	} while (high1 != high2);
2110a3e67a4SJesse Barnes 
2120a3e67a4SJesse Barnes 	count = (high1 << 8) | low;
2130a3e67a4SJesse Barnes 
2140a3e67a4SJesse Barnes 	return count;
2150a3e67a4SJesse Barnes }
2160a3e67a4SJesse Barnes 
2179880b7a5SJesse Barnes u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
2189880b7a5SJesse Barnes {
2199880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2209880b7a5SJesse Barnes 	int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
2219880b7a5SJesse Barnes 
2229880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
2236cb504c2SFrans Pop 		DRM_DEBUG("trying to get vblank count for disabled pipe %d\n", pipe);
2249880b7a5SJesse Barnes 		return 0;
2259880b7a5SJesse Barnes 	}
2269880b7a5SJesse Barnes 
2279880b7a5SJesse Barnes 	return I915_READ(reg);
2289880b7a5SJesse Barnes }
2299880b7a5SJesse Barnes 
2305ca58282SJesse Barnes /*
2315ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
2325ca58282SJesse Barnes  */
2335ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
2345ca58282SJesse Barnes {
2355ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
2365ca58282SJesse Barnes 						    hotplug_work);
2375ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
238c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
239c31c4ba3SKeith Packard 	struct drm_connector *connector;
2405ca58282SJesse Barnes 
241c31c4ba3SKeith Packard 	if (mode_config->num_connector) {
242c31c4ba3SKeith Packard 		list_for_each_entry(connector, &mode_config->connector_list, head) {
243c31c4ba3SKeith Packard 			struct intel_output *intel_output = to_intel_output(connector);
244c31c4ba3SKeith Packard 
245c31c4ba3SKeith Packard 			if (intel_output->hot_plug)
246c31c4ba3SKeith Packard 				(*intel_output->hot_plug) (intel_output);
247c31c4ba3SKeith Packard 		}
248c31c4ba3SKeith Packard 	}
2495ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
2505ca58282SJesse Barnes 	drm_sysfs_hotplug_event(dev);
2515ca58282SJesse Barnes }
2525ca58282SJesse Barnes 
253036a4a7dSZhenyu Wang irqreturn_t igdng_irq_handler(struct drm_device *dev)
254036a4a7dSZhenyu Wang {
255036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
256036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
257*2d109a84SZou, Nanhai 	u32 de_iir, gt_iir, de_ier;
258036a4a7dSZhenyu Wang 	u32 new_de_iir, new_gt_iir;
259036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
260036a4a7dSZhenyu Wang 
261*2d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
262*2d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
263*2d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
264*2d109a84SZou, Nanhai 	(void)I915_READ(DEIER);
265*2d109a84SZou, Nanhai 
266036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
267036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
268036a4a7dSZhenyu Wang 
269036a4a7dSZhenyu Wang 	for (;;) {
270036a4a7dSZhenyu Wang 		if (de_iir == 0 && gt_iir == 0)
271036a4a7dSZhenyu Wang 			break;
272036a4a7dSZhenyu Wang 
273036a4a7dSZhenyu Wang 		ret = IRQ_HANDLED;
274036a4a7dSZhenyu Wang 
275036a4a7dSZhenyu Wang 		I915_WRITE(DEIIR, de_iir);
276036a4a7dSZhenyu Wang 		new_de_iir = I915_READ(DEIIR);
277036a4a7dSZhenyu Wang 		I915_WRITE(GTIIR, gt_iir);
278036a4a7dSZhenyu Wang 		new_gt_iir = I915_READ(GTIIR);
279036a4a7dSZhenyu Wang 
280036a4a7dSZhenyu Wang 		if (dev->primary->master) {
281036a4a7dSZhenyu Wang 			master_priv = dev->primary->master->driver_priv;
282036a4a7dSZhenyu Wang 			if (master_priv->sarea_priv)
283036a4a7dSZhenyu Wang 				master_priv->sarea_priv->last_dispatch =
284036a4a7dSZhenyu Wang 					READ_BREADCRUMB(dev_priv);
285036a4a7dSZhenyu Wang 		}
286036a4a7dSZhenyu Wang 
287036a4a7dSZhenyu Wang 		if (gt_iir & GT_USER_INTERRUPT) {
2881c5d22f7SChris Wilson 			u32 seqno = i915_get_gem_seqno(dev);
2891c5d22f7SChris Wilson 			dev_priv->mm.irq_gem_seqno = seqno;
2901c5d22f7SChris Wilson 			trace_i915_gem_request_complete(dev, seqno);
291036a4a7dSZhenyu Wang 			DRM_WAKEUP(&dev_priv->irq_queue);
292036a4a7dSZhenyu Wang 		}
293036a4a7dSZhenyu Wang 
294036a4a7dSZhenyu Wang 		de_iir = new_de_iir;
295036a4a7dSZhenyu Wang 		gt_iir = new_gt_iir;
296036a4a7dSZhenyu Wang 	}
297036a4a7dSZhenyu Wang 
298*2d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
299*2d109a84SZou, Nanhai 	(void)I915_READ(DEIER);
300*2d109a84SZou, Nanhai 
301036a4a7dSZhenyu Wang 	return ret;
302036a4a7dSZhenyu Wang }
303036a4a7dSZhenyu Wang 
3048a905236SJesse Barnes /**
3058a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
3068a905236SJesse Barnes  * @work: work struct
3078a905236SJesse Barnes  *
3088a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
3098a905236SJesse Barnes  * was detected.
3108a905236SJesse Barnes  */
3118a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
3128a905236SJesse Barnes {
3138a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3148a905236SJesse Barnes 						    error_work);
3158a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
316f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
317f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
318f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
3198a905236SJesse Barnes 
3208a905236SJesse Barnes 	DRM_DEBUG("generating error event\n");
321f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
3228a905236SJesse Barnes 
323ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
324f316a42cSBen Gamari 		if (IS_I965G(dev)) {
325f316a42cSBen Gamari 			DRM_DEBUG("resetting chip\n");
326f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
327f316a42cSBen Gamari 			if (!i965_reset(dev, GDRST_RENDER)) {
328ba1234d1SBen Gamari 				atomic_set(&dev_priv->mm.wedged, 0);
329f316a42cSBen Gamari 				kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
330f316a42cSBen Gamari 			}
331f316a42cSBen Gamari 		} else {
332f316a42cSBen Gamari 			printk("reboot required\n");
333f316a42cSBen Gamari 		}
334f316a42cSBen Gamari 	}
3358a905236SJesse Barnes }
3368a905236SJesse Barnes 
3378a905236SJesse Barnes /**
3388a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
3398a905236SJesse Barnes  * @dev: drm device
3408a905236SJesse Barnes  *
3418a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
3428a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
3438a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
3448a905236SJesse Barnes  * to pick up.
3458a905236SJesse Barnes  */
34663eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
34763eeaf38SJesse Barnes {
34863eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
34963eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
35063eeaf38SJesse Barnes 	unsigned long flags;
35163eeaf38SJesse Barnes 
35263eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
35363eeaf38SJesse Barnes 	if (dev_priv->first_error)
35463eeaf38SJesse Barnes 		goto out;
35563eeaf38SJesse Barnes 
35663eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
35763eeaf38SJesse Barnes 	if (!error) {
35863eeaf38SJesse Barnes 		DRM_DEBUG("out ot memory, not capturing error state\n");
35963eeaf38SJesse Barnes 		goto out;
36063eeaf38SJesse Barnes 	}
36163eeaf38SJesse Barnes 
36263eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
36363eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
36463eeaf38SJesse Barnes 	error->pipeastat = I915_READ(PIPEASTAT);
36563eeaf38SJesse Barnes 	error->pipebstat = I915_READ(PIPEBSTAT);
36663eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
36763eeaf38SJesse Barnes 	if (!IS_I965G(dev)) {
36863eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR);
36963eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR);
37063eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE);
37163eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD);
37263eeaf38SJesse Barnes 	} else {
37363eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
37463eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
37563eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
37663eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
37763eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
37863eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
37963eeaf38SJesse Barnes 	}
38063eeaf38SJesse Barnes 
3818a905236SJesse Barnes 	do_gettimeofday(&error->time);
3828a905236SJesse Barnes 
38363eeaf38SJesse Barnes 	dev_priv->first_error = error;
38463eeaf38SJesse Barnes 
38563eeaf38SJesse Barnes out:
38663eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
38763eeaf38SJesse Barnes }
38863eeaf38SJesse Barnes 
3898a905236SJesse Barnes /**
3908a905236SJesse Barnes  * i915_handle_error - handle an error interrupt
3918a905236SJesse Barnes  * @dev: drm device
3928a905236SJesse Barnes  *
3938a905236SJesse Barnes  * Do some basic checking of regsiter state at error interrupt time and
3948a905236SJesse Barnes  * dump it to the syslog.  Also call i915_capture_error_state() to make
3958a905236SJesse Barnes  * sure we get a record and make it available in debugfs.  Fire a uevent
3968a905236SJesse Barnes  * so userspace knows something bad happened (should trigger collection
3978a905236SJesse Barnes  * of a ring dump etc.).
3988a905236SJesse Barnes  */
399ba1234d1SBen Gamari static void i915_handle_error(struct drm_device *dev, bool wedged)
400c0e09200SDave Airlie {
4018a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
40263eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
4038a905236SJesse Barnes 	u32 pipea_stats = I915_READ(PIPEASTAT);
4048a905236SJesse Barnes 	u32 pipeb_stats = I915_READ(PIPEBSTAT);
40563eeaf38SJesse Barnes 
40663eeaf38SJesse Barnes 	i915_capture_error_state(dev);
40763eeaf38SJesse Barnes 
40863eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
40963eeaf38SJesse Barnes 	       eir);
4108a905236SJesse Barnes 
4118a905236SJesse Barnes 	if (IS_G4X(dev)) {
4128a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
4138a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
4148a905236SJesse Barnes 
4158a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
4168a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
4178a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
4188a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
4198a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
4208a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
4218a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
4228a905236SJesse Barnes 			       I915_READ(INSTPS));
4238a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
4248a905236SJesse Barnes 			       I915_READ(INSTDONE1));
4258a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
4268a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
4278a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
4288a905236SJesse Barnes 			(void)I915_READ(IPEIR_I965);
4298a905236SJesse Barnes 		}
4308a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
4318a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
4328a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
4338a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
4348a905236SJesse Barnes 			       pgtbl_err);
4358a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
4368a905236SJesse Barnes 			(void)I915_READ(PGTBL_ER);
4378a905236SJesse Barnes 		}
4388a905236SJesse Barnes 	}
4398a905236SJesse Barnes 
4408a905236SJesse Barnes 	if (IS_I9XX(dev)) {
44163eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
44263eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
44363eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
44463eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
44563eeaf38SJesse Barnes 			       pgtbl_err);
44663eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
44763eeaf38SJesse Barnes 			(void)I915_READ(PGTBL_ER);
44863eeaf38SJesse Barnes 		}
4498a905236SJesse Barnes 	}
4508a905236SJesse Barnes 
45163eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
45263eeaf38SJesse Barnes 		printk(KERN_ERR "memory refresh error\n");
45363eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
45463eeaf38SJesse Barnes 		       pipea_stats);
45563eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
45663eeaf38SJesse Barnes 		       pipeb_stats);
45763eeaf38SJesse Barnes 		/* pipestat has already been acked */
45863eeaf38SJesse Barnes 	}
45963eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
46063eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
46163eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
46263eeaf38SJesse Barnes 		       I915_READ(INSTPM));
46363eeaf38SJesse Barnes 		if (!IS_I965G(dev)) {
46463eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
46563eeaf38SJesse Barnes 
46663eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
46763eeaf38SJesse Barnes 			       I915_READ(IPEIR));
46863eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
46963eeaf38SJesse Barnes 			       I915_READ(IPEHR));
47063eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
47163eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
47263eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
47363eeaf38SJesse Barnes 			       I915_READ(ACTHD));
47463eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
47563eeaf38SJesse Barnes 			(void)I915_READ(IPEIR);
47663eeaf38SJesse Barnes 		} else {
47763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
47863eeaf38SJesse Barnes 
47963eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
48063eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
48163eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
48263eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
48363eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
48463eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
48563eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
48663eeaf38SJesse Barnes 			       I915_READ(INSTPS));
48763eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
48863eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
48963eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
49063eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
49163eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
49263eeaf38SJesse Barnes 			(void)I915_READ(IPEIR_I965);
49363eeaf38SJesse Barnes 		}
49463eeaf38SJesse Barnes 	}
49563eeaf38SJesse Barnes 
49663eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
49763eeaf38SJesse Barnes 	(void)I915_READ(EIR);
49863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
49963eeaf38SJesse Barnes 	if (eir) {
50063eeaf38SJesse Barnes 		/*
50163eeaf38SJesse Barnes 		 * some errors might have become stuck,
50263eeaf38SJesse Barnes 		 * mask them.
50363eeaf38SJesse Barnes 		 */
50463eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
50563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
50663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
50763eeaf38SJesse Barnes 	}
5088a905236SJesse Barnes 
509ba1234d1SBen Gamari 	if (wedged) {
510ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
511ba1234d1SBen Gamari 
51211ed50ecSBen Gamari 		/*
51311ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
51411ed50ecSBen Gamari 		 */
51511ed50ecSBen Gamari 		printk("i915: Waking up sleeping processes\n");
51611ed50ecSBen Gamari 		DRM_WAKEUP(&dev_priv->irq_queue);
51711ed50ecSBen Gamari 	}
51811ed50ecSBen Gamari 
5199c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
5208a905236SJesse Barnes }
5218a905236SJesse Barnes 
5228a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
5238a905236SJesse Barnes {
5248a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
5258a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5268a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
5278a905236SJesse Barnes 	u32 iir, new_iir;
5288a905236SJesse Barnes 	u32 pipea_stats, pipeb_stats;
5298a905236SJesse Barnes 	u32 vblank_status;
5308a905236SJesse Barnes 	u32 vblank_enable;
5318a905236SJesse Barnes 	int vblank = 0;
5328a905236SJesse Barnes 	unsigned long irqflags;
5338a905236SJesse Barnes 	int irq_received;
5348a905236SJesse Barnes 	int ret = IRQ_NONE;
5358a905236SJesse Barnes 
5368a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5378a905236SJesse Barnes 
5388a905236SJesse Barnes 	if (IS_IGDNG(dev))
5398a905236SJesse Barnes 		return igdng_irq_handler(dev);
5408a905236SJesse Barnes 
5418a905236SJesse Barnes 	iir = I915_READ(IIR);
5428a905236SJesse Barnes 
5438a905236SJesse Barnes 	if (IS_I965G(dev)) {
5448a905236SJesse Barnes 		vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
5458a905236SJesse Barnes 		vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
5468a905236SJesse Barnes 	} else {
5478a905236SJesse Barnes 		vblank_status = I915_VBLANK_INTERRUPT_STATUS;
5488a905236SJesse Barnes 		vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
5498a905236SJesse Barnes 	}
5508a905236SJesse Barnes 
5518a905236SJesse Barnes 	for (;;) {
5528a905236SJesse Barnes 		irq_received = iir != 0;
5538a905236SJesse Barnes 
5548a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
5558a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
5568a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
5578a905236SJesse Barnes 		 * interrupts (for non-MSI).
5588a905236SJesse Barnes 		 */
5598a905236SJesse Barnes 		spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
5608a905236SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
5618a905236SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
5628a905236SJesse Barnes 
5638a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
564ba1234d1SBen Gamari 			i915_handle_error(dev, false);
5658a905236SJesse Barnes 
5668a905236SJesse Barnes 		/*
5678a905236SJesse Barnes 		 * Clear the PIPE(A|B)STAT regs before the IIR
5688a905236SJesse Barnes 		 */
5698a905236SJesse Barnes 		if (pipea_stats & 0x8000ffff) {
5708a905236SJesse Barnes 			if (pipea_stats &  PIPE_FIFO_UNDERRUN_STATUS)
5718a905236SJesse Barnes 				DRM_DEBUG("pipe a underrun\n");
5728a905236SJesse Barnes 			I915_WRITE(PIPEASTAT, pipea_stats);
5738a905236SJesse Barnes 			irq_received = 1;
5748a905236SJesse Barnes 		}
5758a905236SJesse Barnes 
5768a905236SJesse Barnes 		if (pipeb_stats & 0x8000ffff) {
5778a905236SJesse Barnes 			if (pipeb_stats &  PIPE_FIFO_UNDERRUN_STATUS)
5788a905236SJesse Barnes 				DRM_DEBUG("pipe b underrun\n");
5798a905236SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
5808a905236SJesse Barnes 			irq_received = 1;
5818a905236SJesse Barnes 		}
5828a905236SJesse Barnes 		spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
5838a905236SJesse Barnes 
5848a905236SJesse Barnes 		if (!irq_received)
5858a905236SJesse Barnes 			break;
5868a905236SJesse Barnes 
5878a905236SJesse Barnes 		ret = IRQ_HANDLED;
5888a905236SJesse Barnes 
5898a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
5908a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
5918a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
5928a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
5938a905236SJesse Barnes 
5948a905236SJesse Barnes 			DRM_DEBUG("hotplug event received, stat 0x%08x\n",
5958a905236SJesse Barnes 				  hotplug_status);
5968a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
5979c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
5989c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
5998a905236SJesse Barnes 
6008a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
6018a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
60204302965SShaohua Li 
60304302965SShaohua Li 			/* EOS interrupts occurs */
60404302965SShaohua Li 			if (IS_IGD(dev) &&
60504302965SShaohua Li 				(hotplug_status & CRT_EOS_INT_STATUS)) {
60604302965SShaohua Li 				u32 temp;
60704302965SShaohua Li 
60804302965SShaohua Li 				DRM_DEBUG("EOS interrupt occurs\n");
60904302965SShaohua Li 				/* status is already cleared */
61004302965SShaohua Li 				temp = I915_READ(ADPA);
61104302965SShaohua Li 				temp &= ~ADPA_DAC_ENABLE;
61204302965SShaohua Li 				I915_WRITE(ADPA, temp);
61304302965SShaohua Li 
61404302965SShaohua Li 				temp = I915_READ(PORT_HOTPLUG_EN);
61504302965SShaohua Li 				temp &= ~CRT_EOS_INT_EN;
61604302965SShaohua Li 				I915_WRITE(PORT_HOTPLUG_EN, temp);
61704302965SShaohua Li 
61804302965SShaohua Li 				temp = I915_READ(PORT_HOTPLUG_STAT);
61904302965SShaohua Li 				if (temp & CRT_EOS_INT_STATUS)
62004302965SShaohua Li 					I915_WRITE(PORT_HOTPLUG_STAT,
62104302965SShaohua Li 						CRT_EOS_INT_STATUS);
62204302965SShaohua Li 			}
62363eeaf38SJesse Barnes 		}
62463eeaf38SJesse Barnes 
625673a394bSEric Anholt 		I915_WRITE(IIR, iir);
626cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
6277c463586SKeith Packard 
6287c1c2871SDave Airlie 		if (dev->primary->master) {
6297c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
6307c1c2871SDave Airlie 			if (master_priv->sarea_priv)
6317c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
632c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
6337c1c2871SDave Airlie 		}
6340a3e67a4SJesse Barnes 
635673a394bSEric Anholt 		if (iir & I915_USER_INTERRUPT) {
6361c5d22f7SChris Wilson 			u32 seqno = i915_get_gem_seqno(dev);
6371c5d22f7SChris Wilson 			dev_priv->mm.irq_gem_seqno = seqno;
6381c5d22f7SChris Wilson 			trace_i915_gem_request_complete(dev, seqno);
639673a394bSEric Anholt 			DRM_WAKEUP(&dev_priv->irq_queue);
640f65d9421SBen Gamari 			dev_priv->hangcheck_count = 0;
641f65d9421SBen Gamari 			mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
642673a394bSEric Anholt 		}
643673a394bSEric Anholt 
64405eff845SKeith Packard 		if (pipea_stats & vblank_status) {
6457c463586SKeith Packard 			vblank++;
6467c463586SKeith Packard 			drm_handle_vblank(dev, 0);
6477c463586SKeith Packard 		}
6487c463586SKeith Packard 
64905eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
6507c463586SKeith Packard 			vblank++;
6517c463586SKeith Packard 			drm_handle_vblank(dev, 1);
6527c463586SKeith Packard 		}
6537c463586SKeith Packard 
6547c463586SKeith Packard 		if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
6557c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
656673a394bSEric Anholt 			opregion_asle_intr(dev);
6570a3e67a4SJesse Barnes 
658cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
659cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
660cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
661cdfbc41fSEric Anholt 		 * we would never get another interrupt.
662cdfbc41fSEric Anholt 		 *
663cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
664cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
665cdfbc41fSEric Anholt 		 * another one.
666cdfbc41fSEric Anholt 		 *
667cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
668cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
669cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
670cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
671cdfbc41fSEric Anholt 		 * stray interrupts.
672cdfbc41fSEric Anholt 		 */
673cdfbc41fSEric Anholt 		iir = new_iir;
67405eff845SKeith Packard 	}
675cdfbc41fSEric Anholt 
67605eff845SKeith Packard 	return ret;
677c0e09200SDave Airlie }
678c0e09200SDave Airlie 
679c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
680c0e09200SDave Airlie {
681c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
6827c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
683c0e09200SDave Airlie 	RING_LOCALS;
684c0e09200SDave Airlie 
685c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
686c0e09200SDave Airlie 
687c0e09200SDave Airlie 	DRM_DEBUG("\n");
688c0e09200SDave Airlie 
689c99b058fSKristian Høgsberg 	dev_priv->counter++;
690c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
691c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
6927c1c2871SDave Airlie 	if (master_priv->sarea_priv)
6937c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
694c0e09200SDave Airlie 
6950baf823aSKeith Packard 	BEGIN_LP_RING(4);
696585fb111SJesse Barnes 	OUT_RING(MI_STORE_DWORD_INDEX);
6970baf823aSKeith Packard 	OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
698c0e09200SDave Airlie 	OUT_RING(dev_priv->counter);
699585fb111SJesse Barnes 	OUT_RING(MI_USER_INTERRUPT);
700c0e09200SDave Airlie 	ADVANCE_LP_RING();
701c0e09200SDave Airlie 
702c0e09200SDave Airlie 	return dev_priv->counter;
703c0e09200SDave Airlie }
704c0e09200SDave Airlie 
705673a394bSEric Anholt void i915_user_irq_get(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);
711036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
712036a4a7dSZhenyu Wang 		if (IS_IGDNG(dev))
713036a4a7dSZhenyu Wang 			igdng_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
714036a4a7dSZhenyu Wang 		else
715ed4cb414SEric Anholt 			i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
716036a4a7dSZhenyu Wang 	}
717e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
718ed4cb414SEric Anholt }
719ed4cb414SEric Anholt 
7200a3e67a4SJesse Barnes void i915_user_irq_put(struct drm_device *dev)
721ed4cb414SEric Anholt {
722ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
723e9d21d7fSKeith Packard 	unsigned long irqflags;
724ed4cb414SEric Anholt 
725e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
726ed4cb414SEric Anholt 	BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
727036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
728036a4a7dSZhenyu Wang 		if (IS_IGDNG(dev))
729036a4a7dSZhenyu Wang 			igdng_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
730036a4a7dSZhenyu Wang 		else
731ed4cb414SEric Anholt 			i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
732036a4a7dSZhenyu Wang 	}
733e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
734ed4cb414SEric Anholt }
735ed4cb414SEric Anholt 
7369d34e5dbSChris Wilson void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
7379d34e5dbSChris Wilson {
7389d34e5dbSChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7399d34e5dbSChris Wilson 
7409d34e5dbSChris Wilson 	if (dev_priv->trace_irq_seqno == 0)
7419d34e5dbSChris Wilson 		i915_user_irq_get(dev);
7429d34e5dbSChris Wilson 
7439d34e5dbSChris Wilson 	dev_priv->trace_irq_seqno = seqno;
7449d34e5dbSChris Wilson }
7459d34e5dbSChris Wilson 
746c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
747c0e09200SDave Airlie {
748c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7497c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
750c0e09200SDave Airlie 	int ret = 0;
751c0e09200SDave Airlie 
752c0e09200SDave Airlie 	DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
753c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
754c0e09200SDave Airlie 
755ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
7567c1c2871SDave Airlie 		if (master_priv->sarea_priv)
7577c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
758c0e09200SDave Airlie 		return 0;
759ed4cb414SEric Anholt 	}
760c0e09200SDave Airlie 
7617c1c2871SDave Airlie 	if (master_priv->sarea_priv)
7627c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
763c0e09200SDave Airlie 
764ed4cb414SEric Anholt 	i915_user_irq_get(dev);
765c0e09200SDave Airlie 	DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
766c0e09200SDave Airlie 		    READ_BREADCRUMB(dev_priv) >= irq_nr);
767ed4cb414SEric Anholt 	i915_user_irq_put(dev);
768c0e09200SDave Airlie 
769c0e09200SDave Airlie 	if (ret == -EBUSY) {
770c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
771c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
772c0e09200SDave Airlie 	}
773c0e09200SDave Airlie 
774c0e09200SDave Airlie 	return ret;
775c0e09200SDave Airlie }
776c0e09200SDave Airlie 
777c0e09200SDave Airlie /* Needs the lock as it touches the ring.
778c0e09200SDave Airlie  */
779c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
780c0e09200SDave Airlie 			 struct drm_file *file_priv)
781c0e09200SDave Airlie {
782c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
783c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
784c0e09200SDave Airlie 	int result;
785c0e09200SDave Airlie 
78607f4f8bfSEric Anholt 	if (!dev_priv || !dev_priv->ring.virtual_start) {
787c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
788c0e09200SDave Airlie 		return -EINVAL;
789c0e09200SDave Airlie 	}
790299eb93cSEric Anholt 
791299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
792299eb93cSEric Anholt 
793546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
794c0e09200SDave Airlie 	result = i915_emit_irq(dev);
795546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
796c0e09200SDave Airlie 
797c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
798c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
799c0e09200SDave Airlie 		return -EFAULT;
800c0e09200SDave Airlie 	}
801c0e09200SDave Airlie 
802c0e09200SDave Airlie 	return 0;
803c0e09200SDave Airlie }
804c0e09200SDave Airlie 
805c0e09200SDave Airlie /* Doesn't need the hardware lock.
806c0e09200SDave Airlie  */
807c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
808c0e09200SDave Airlie 			 struct drm_file *file_priv)
809c0e09200SDave Airlie {
810c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
811c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
812c0e09200SDave Airlie 
813c0e09200SDave Airlie 	if (!dev_priv) {
814c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
815c0e09200SDave Airlie 		return -EINVAL;
816c0e09200SDave Airlie 	}
817c0e09200SDave Airlie 
818c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
819c0e09200SDave Airlie }
820c0e09200SDave Airlie 
82142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
82242f52ef8SKeith Packard  * we use as a pipe index
82342f52ef8SKeith Packard  */
82442f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
8250a3e67a4SJesse Barnes {
8260a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
827e9d21d7fSKeith Packard 	unsigned long irqflags;
82871e0ffa5SJesse Barnes 	int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
82971e0ffa5SJesse Barnes 	u32 pipeconf;
83071e0ffa5SJesse Barnes 
83171e0ffa5SJesse Barnes 	pipeconf = I915_READ(pipeconf_reg);
83271e0ffa5SJesse Barnes 	if (!(pipeconf & PIPEACONF_ENABLE))
83371e0ffa5SJesse Barnes 		return -EINVAL;
8340a3e67a4SJesse Barnes 
835036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev))
836036a4a7dSZhenyu Wang 		return 0;
837036a4a7dSZhenyu Wang 
838e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
8390a3e67a4SJesse Barnes 	if (IS_I965G(dev))
8407c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
8417c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
8420a3e67a4SJesse Barnes 	else
8437c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
8447c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
845e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
8460a3e67a4SJesse Barnes 	return 0;
8470a3e67a4SJesse Barnes }
8480a3e67a4SJesse Barnes 
84942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
85042f52ef8SKeith Packard  * we use as a pipe index
85142f52ef8SKeith Packard  */
85242f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
8530a3e67a4SJesse Barnes {
8540a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
855e9d21d7fSKeith Packard 	unsigned long irqflags;
8560a3e67a4SJesse Barnes 
857036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev))
858036a4a7dSZhenyu Wang 		return;
859036a4a7dSZhenyu Wang 
860e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
8617c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
8627c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
8637c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
864e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
8650a3e67a4SJesse Barnes }
8660a3e67a4SJesse Barnes 
86779e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev)
86879e53945SJesse Barnes {
86979e53945SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
870e170b030SZhenyu Wang 
871e170b030SZhenyu Wang 	if (!IS_IGDNG(dev))
87279e53945SJesse Barnes 		opregion_enable_asle(dev);
87379e53945SJesse Barnes 	dev_priv->irq_enabled = 1;
87479e53945SJesse Barnes }
87579e53945SJesse Barnes 
87679e53945SJesse Barnes 
877c0e09200SDave Airlie /* Set the vblank monitor pipe
878c0e09200SDave Airlie  */
879c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
880c0e09200SDave Airlie 			 struct drm_file *file_priv)
881c0e09200SDave Airlie {
882c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
883c0e09200SDave Airlie 
884c0e09200SDave Airlie 	if (!dev_priv) {
885c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
886c0e09200SDave Airlie 		return -EINVAL;
887c0e09200SDave Airlie 	}
888c0e09200SDave Airlie 
889c0e09200SDave Airlie 	return 0;
890c0e09200SDave Airlie }
891c0e09200SDave Airlie 
892c0e09200SDave Airlie int i915_vblank_pipe_get(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 	drm_i915_vblank_pipe_t *pipe = data;
897c0e09200SDave Airlie 
898c0e09200SDave Airlie 	if (!dev_priv) {
899c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
900c0e09200SDave Airlie 		return -EINVAL;
901c0e09200SDave Airlie 	}
902c0e09200SDave Airlie 
9030a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
904c0e09200SDave Airlie 
905c0e09200SDave Airlie 	return 0;
906c0e09200SDave Airlie }
907c0e09200SDave Airlie 
908c0e09200SDave Airlie /**
909c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
910c0e09200SDave Airlie  */
911c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
912c0e09200SDave Airlie 		     struct drm_file *file_priv)
913c0e09200SDave Airlie {
914bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
915bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
916bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
917bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
918bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
919bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
920bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
921bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
922bd95e0a4SEric Anholt 	 *
923bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
924bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
925bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
926bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
9270a3e67a4SJesse Barnes 	 */
928c0e09200SDave Airlie 	return -EINVAL;
929c0e09200SDave Airlie }
930c0e09200SDave Airlie 
931f65d9421SBen Gamari struct drm_i915_gem_request *i915_get_tail_request(struct drm_device *dev) {
932f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
933f65d9421SBen Gamari 	return list_entry(dev_priv->mm.request_list.prev, struct drm_i915_gem_request, list);
934f65d9421SBen Gamari }
935f65d9421SBen Gamari 
936f65d9421SBen Gamari /**
937f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
938f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
939f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
940f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
941f65d9421SBen Gamari  */
942f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
943f65d9421SBen Gamari {
944f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
945f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
946f65d9421SBen Gamari 	uint32_t acthd;
947f65d9421SBen Gamari 
948f65d9421SBen Gamari 	if (!IS_I965G(dev))
949f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
950f65d9421SBen Gamari 	else
951f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
952f65d9421SBen Gamari 
953f65d9421SBen Gamari 	/* If all work is done then ACTHD clearly hasn't advanced. */
954f65d9421SBen Gamari 	if (list_empty(&dev_priv->mm.request_list) ||
955f65d9421SBen Gamari 		       i915_seqno_passed(i915_get_gem_seqno(dev), i915_get_tail_request(dev)->seqno)) {
956f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
957f65d9421SBen Gamari 		return;
958f65d9421SBen Gamari 	}
959f65d9421SBen Gamari 
960f65d9421SBen Gamari 	if (dev_priv->last_acthd == acthd && dev_priv->hangcheck_count > 0) {
961f65d9421SBen Gamari 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
962ba1234d1SBen Gamari 		i915_handle_error(dev, true);
963f65d9421SBen Gamari 		return;
964f65d9421SBen Gamari 	}
965f65d9421SBen Gamari 
966f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
967f65d9421SBen Gamari 	mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
968f65d9421SBen Gamari 
969f65d9421SBen Gamari 	if (acthd != dev_priv->last_acthd)
970f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
971f65d9421SBen Gamari 	else
972f65d9421SBen Gamari 		dev_priv->hangcheck_count++;
973f65d9421SBen Gamari 
974f65d9421SBen Gamari 	dev_priv->last_acthd = acthd;
975f65d9421SBen Gamari }
976f65d9421SBen Gamari 
977c0e09200SDave Airlie /* drm_dma.h hooks
978c0e09200SDave Airlie */
979036a4a7dSZhenyu Wang static void igdng_irq_preinstall(struct drm_device *dev)
980036a4a7dSZhenyu Wang {
981036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
982036a4a7dSZhenyu Wang 
983036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
984036a4a7dSZhenyu Wang 
985036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
986036a4a7dSZhenyu Wang 
987036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
988036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
989036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
990036a4a7dSZhenyu Wang 
991036a4a7dSZhenyu Wang 	/* and GT */
992036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
993036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
994036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
995036a4a7dSZhenyu Wang }
996036a4a7dSZhenyu Wang 
997036a4a7dSZhenyu Wang static int igdng_irq_postinstall(struct drm_device *dev)
998036a4a7dSZhenyu Wang {
999036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1000036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1001036a4a7dSZhenyu Wang 	u32 display_mask = DE_MASTER_IRQ_CONTROL /*| DE_PCH_EVENT */;
1002036a4a7dSZhenyu Wang 	u32 render_mask = GT_USER_INTERRUPT;
1003036a4a7dSZhenyu Wang 
1004036a4a7dSZhenyu Wang 	dev_priv->irq_mask_reg = ~display_mask;
1005036a4a7dSZhenyu Wang 	dev_priv->de_irq_enable_reg = display_mask;
1006036a4a7dSZhenyu Wang 
1007036a4a7dSZhenyu Wang 	/* should always can generate irq */
1008036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1009036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
1010036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
1011036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
1012036a4a7dSZhenyu Wang 
1013036a4a7dSZhenyu Wang 	/* user interrupt should be enabled, but masked initial */
1014036a4a7dSZhenyu Wang 	dev_priv->gt_irq_mask_reg = 0xffffffff;
1015036a4a7dSZhenyu Wang 	dev_priv->gt_irq_enable_reg = render_mask;
1016036a4a7dSZhenyu Wang 
1017036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1018036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
1019036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
1020036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
1021036a4a7dSZhenyu Wang 
1022036a4a7dSZhenyu Wang 	return 0;
1023036a4a7dSZhenyu Wang }
1024036a4a7dSZhenyu Wang 
1025c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1026c0e09200SDave Airlie {
1027c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1028c0e09200SDave Airlie 
102979e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
103079e53945SJesse Barnes 
1031036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
10328a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1033036a4a7dSZhenyu Wang 
1034036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev)) {
1035036a4a7dSZhenyu Wang 		igdng_irq_preinstall(dev);
1036036a4a7dSZhenyu Wang 		return;
1037036a4a7dSZhenyu Wang 	}
1038036a4a7dSZhenyu Wang 
10395ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
10405ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
10415ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
10425ca58282SJesse Barnes 	}
10435ca58282SJesse Barnes 
10440a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
10457c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
10467c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
10470a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1048ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
10497c463586SKeith Packard 	(void) I915_READ(IER);
1050c0e09200SDave Airlie }
1051c0e09200SDave Airlie 
10520a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1053c0e09200SDave Airlie {
1054c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10555ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
105663eeaf38SJesse Barnes 	u32 error_mask;
10570a3e67a4SJesse Barnes 
1058036a4a7dSZhenyu Wang 	DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
1059036a4a7dSZhenyu Wang 
10600a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1061ed4cb414SEric Anholt 
1062036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev))
1063036a4a7dSZhenyu Wang 		return igdng_irq_postinstall(dev);
1064036a4a7dSZhenyu Wang 
10657c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
10667c463586SKeith Packard 	dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
10678ee1c3dbSMatthew Garrett 
10687c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
10697c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
10707c463586SKeith Packard 
10715ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
10725ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
10735ca58282SJesse Barnes 
10745ca58282SJesse Barnes 		/* Leave other bits alone */
10755ca58282SJesse Barnes 		hotplug_en |= HOTPLUG_EN_MASK;
10765ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
10775ca58282SJesse Barnes 
10785ca58282SJesse Barnes 		dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
10795ca58282SJesse Barnes 			TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
10805ca58282SJesse Barnes 			SDVOB_HOTPLUG_INT_STATUS;
10815ca58282SJesse Barnes 		if (IS_G4X(dev)) {
10825ca58282SJesse Barnes 			dev_priv->hotplug_supported_mask |=
10835ca58282SJesse Barnes 				HDMIB_HOTPLUG_INT_STATUS |
10845ca58282SJesse Barnes 				HDMIC_HOTPLUG_INT_STATUS |
10855ca58282SJesse Barnes 				HDMID_HOTPLUG_INT_STATUS;
10865ca58282SJesse Barnes 		}
10875ca58282SJesse Barnes 		/* Enable in IER... */
10885ca58282SJesse Barnes 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
10895ca58282SJesse Barnes 		/* and unmask in IMR */
10905ca58282SJesse Barnes 		i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
10915ca58282SJesse Barnes 	}
10925ca58282SJesse Barnes 
109363eeaf38SJesse Barnes 	/*
109463eeaf38SJesse Barnes 	 * Enable some error detection, note the instruction error mask
109563eeaf38SJesse Barnes 	 * bit is reserved, so we leave it masked.
109663eeaf38SJesse Barnes 	 */
109763eeaf38SJesse Barnes 	if (IS_G4X(dev)) {
109863eeaf38SJesse Barnes 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
109963eeaf38SJesse Barnes 			       GM45_ERROR_MEM_PRIV |
110063eeaf38SJesse Barnes 			       GM45_ERROR_CP_PRIV |
110163eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
110263eeaf38SJesse Barnes 	} else {
110363eeaf38SJesse Barnes 		error_mask = ~(I915_ERROR_PAGE_TABLE |
110463eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
110563eeaf38SJesse Barnes 	}
110663eeaf38SJesse Barnes 	I915_WRITE(EMR, error_mask);
110763eeaf38SJesse Barnes 
11087c463586SKeith Packard 	/* Disable pipe interrupt enables, clear pending pipe status */
11097c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
11107c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
11117c463586SKeith Packard 	/* Clear pending interrupt status */
11127c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
11137c463586SKeith Packard 
11145ca58282SJesse Barnes 	I915_WRITE(IER, enable_mask);
11157c463586SKeith Packard 	I915_WRITE(IMR, dev_priv->irq_mask_reg);
1116ed4cb414SEric Anholt 	(void) I915_READ(IER);
1117ed4cb414SEric Anholt 
11188ee1c3dbSMatthew Garrett 	opregion_enable_asle(dev);
11190a3e67a4SJesse Barnes 
11200a3e67a4SJesse Barnes 	return 0;
1121c0e09200SDave Airlie }
1122c0e09200SDave Airlie 
1123036a4a7dSZhenyu Wang static void igdng_irq_uninstall(struct drm_device *dev)
1124036a4a7dSZhenyu Wang {
1125036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1126036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1127036a4a7dSZhenyu Wang 
1128036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1129036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1130036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1131036a4a7dSZhenyu Wang 
1132036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1133036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1134036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1135036a4a7dSZhenyu Wang }
1136036a4a7dSZhenyu Wang 
1137c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1138c0e09200SDave Airlie {
1139c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1140c0e09200SDave Airlie 
1141c0e09200SDave Airlie 	if (!dev_priv)
1142c0e09200SDave Airlie 		return;
1143c0e09200SDave Airlie 
11440a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
11450a3e67a4SJesse Barnes 
1146036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev)) {
1147036a4a7dSZhenyu Wang 		igdng_irq_uninstall(dev);
1148036a4a7dSZhenyu Wang 		return;
1149036a4a7dSZhenyu Wang 	}
1150036a4a7dSZhenyu Wang 
11515ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
11525ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
11535ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
11545ca58282SJesse Barnes 	}
11555ca58282SJesse Barnes 
11560a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
11577c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
11587c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
11590a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1160ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1161c0e09200SDave Airlie 
11627c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
11637c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
11647c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1165c0e09200SDave Airlie }
1166