xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 1c5d22f76dc721f3acb7a3dadc657a221e487fb7)
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"
34*1c5d22f7SChris 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;
257036a4a7dSZhenyu Wang 	u32 de_iir, gt_iir;
258036a4a7dSZhenyu Wang 	u32 new_de_iir, new_gt_iir;
259036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
260036a4a7dSZhenyu Wang 
261036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
262036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
263036a4a7dSZhenyu Wang 
264036a4a7dSZhenyu Wang 	for (;;) {
265036a4a7dSZhenyu Wang 		if (de_iir == 0 && gt_iir == 0)
266036a4a7dSZhenyu Wang 			break;
267036a4a7dSZhenyu Wang 
268036a4a7dSZhenyu Wang 		ret = IRQ_HANDLED;
269036a4a7dSZhenyu Wang 
270036a4a7dSZhenyu Wang 		I915_WRITE(DEIIR, de_iir);
271036a4a7dSZhenyu Wang 		new_de_iir = I915_READ(DEIIR);
272036a4a7dSZhenyu Wang 		I915_WRITE(GTIIR, gt_iir);
273036a4a7dSZhenyu Wang 		new_gt_iir = I915_READ(GTIIR);
274036a4a7dSZhenyu Wang 
275036a4a7dSZhenyu Wang 		if (dev->primary->master) {
276036a4a7dSZhenyu Wang 			master_priv = dev->primary->master->driver_priv;
277036a4a7dSZhenyu Wang 			if (master_priv->sarea_priv)
278036a4a7dSZhenyu Wang 				master_priv->sarea_priv->last_dispatch =
279036a4a7dSZhenyu Wang 					READ_BREADCRUMB(dev_priv);
280036a4a7dSZhenyu Wang 		}
281036a4a7dSZhenyu Wang 
282036a4a7dSZhenyu Wang 		if (gt_iir & GT_USER_INTERRUPT) {
283*1c5d22f7SChris Wilson 			u32 seqno = i915_get_gem_seqno(dev);
284*1c5d22f7SChris Wilson 			dev_priv->mm.irq_gem_seqno = seqno;
285*1c5d22f7SChris Wilson 			trace_i915_gem_request_complete(dev, seqno);
286036a4a7dSZhenyu Wang 			DRM_WAKEUP(&dev_priv->irq_queue);
287036a4a7dSZhenyu Wang 		}
288036a4a7dSZhenyu Wang 
289036a4a7dSZhenyu Wang 		de_iir = new_de_iir;
290036a4a7dSZhenyu Wang 		gt_iir = new_gt_iir;
291036a4a7dSZhenyu Wang 	}
292036a4a7dSZhenyu Wang 
293036a4a7dSZhenyu Wang 	return ret;
294036a4a7dSZhenyu Wang }
295036a4a7dSZhenyu Wang 
2968a905236SJesse Barnes /**
2978a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
2988a905236SJesse Barnes  * @work: work struct
2998a905236SJesse Barnes  *
3008a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
3018a905236SJesse Barnes  * was detected.
3028a905236SJesse Barnes  */
3038a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
3048a905236SJesse Barnes {
3058a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3068a905236SJesse Barnes 						    error_work);
3078a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
308f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
309f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
310f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
3118a905236SJesse Barnes 
3128a905236SJesse Barnes 	DRM_DEBUG("generating error event\n");
313f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
3148a905236SJesse Barnes 
315ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
316f316a42cSBen Gamari 		if (IS_I965G(dev)) {
317f316a42cSBen Gamari 			DRM_DEBUG("resetting chip\n");
318f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
319f316a42cSBen Gamari 			if (!i965_reset(dev, GDRST_RENDER)) {
320ba1234d1SBen Gamari 				atomic_set(&dev_priv->mm.wedged, 0);
321f316a42cSBen Gamari 				kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
322f316a42cSBen Gamari 			}
323f316a42cSBen Gamari 		} else {
324f316a42cSBen Gamari 			printk("reboot required\n");
325f316a42cSBen Gamari 		}
326f316a42cSBen Gamari 	}
3278a905236SJesse Barnes }
3288a905236SJesse Barnes 
3298a905236SJesse Barnes /**
3308a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
3318a905236SJesse Barnes  * @dev: drm device
3328a905236SJesse Barnes  *
3338a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
3348a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
3358a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
3368a905236SJesse Barnes  * to pick up.
3378a905236SJesse Barnes  */
33863eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
33963eeaf38SJesse Barnes {
34063eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
34163eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
34263eeaf38SJesse Barnes 	unsigned long flags;
34363eeaf38SJesse Barnes 
34463eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
34563eeaf38SJesse Barnes 	if (dev_priv->first_error)
34663eeaf38SJesse Barnes 		goto out;
34763eeaf38SJesse Barnes 
34863eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
34963eeaf38SJesse Barnes 	if (!error) {
35063eeaf38SJesse Barnes 		DRM_DEBUG("out ot memory, not capturing error state\n");
35163eeaf38SJesse Barnes 		goto out;
35263eeaf38SJesse Barnes 	}
35363eeaf38SJesse Barnes 
35463eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
35563eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
35663eeaf38SJesse Barnes 	error->pipeastat = I915_READ(PIPEASTAT);
35763eeaf38SJesse Barnes 	error->pipebstat = I915_READ(PIPEBSTAT);
35863eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
35963eeaf38SJesse Barnes 	if (!IS_I965G(dev)) {
36063eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR);
36163eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR);
36263eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE);
36363eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD);
36463eeaf38SJesse Barnes 	} else {
36563eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
36663eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
36763eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
36863eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
36963eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
37063eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
37163eeaf38SJesse Barnes 	}
37263eeaf38SJesse Barnes 
3738a905236SJesse Barnes 	do_gettimeofday(&error->time);
3748a905236SJesse Barnes 
37563eeaf38SJesse Barnes 	dev_priv->first_error = error;
37663eeaf38SJesse Barnes 
37763eeaf38SJesse Barnes out:
37863eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
37963eeaf38SJesse Barnes }
38063eeaf38SJesse Barnes 
3818a905236SJesse Barnes /**
3828a905236SJesse Barnes  * i915_handle_error - handle an error interrupt
3838a905236SJesse Barnes  * @dev: drm device
3848a905236SJesse Barnes  *
3858a905236SJesse Barnes  * Do some basic checking of regsiter state at error interrupt time and
3868a905236SJesse Barnes  * dump it to the syslog.  Also call i915_capture_error_state() to make
3878a905236SJesse Barnes  * sure we get a record and make it available in debugfs.  Fire a uevent
3888a905236SJesse Barnes  * so userspace knows something bad happened (should trigger collection
3898a905236SJesse Barnes  * of a ring dump etc.).
3908a905236SJesse Barnes  */
391ba1234d1SBen Gamari static void i915_handle_error(struct drm_device *dev, bool wedged)
392c0e09200SDave Airlie {
3938a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
39463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
3958a905236SJesse Barnes 	u32 pipea_stats = I915_READ(PIPEASTAT);
3968a905236SJesse Barnes 	u32 pipeb_stats = I915_READ(PIPEBSTAT);
39763eeaf38SJesse Barnes 
39863eeaf38SJesse Barnes 	i915_capture_error_state(dev);
39963eeaf38SJesse Barnes 
40063eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
40163eeaf38SJesse Barnes 	       eir);
4028a905236SJesse Barnes 
4038a905236SJesse Barnes 	if (IS_G4X(dev)) {
4048a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
4058a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
4068a905236SJesse Barnes 
4078a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
4088a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
4098a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
4108a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
4118a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
4128a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
4138a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
4148a905236SJesse Barnes 			       I915_READ(INSTPS));
4158a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
4168a905236SJesse Barnes 			       I915_READ(INSTDONE1));
4178a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
4188a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
4198a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
4208a905236SJesse Barnes 			(void)I915_READ(IPEIR_I965);
4218a905236SJesse Barnes 		}
4228a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
4238a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
4248a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
4258a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
4268a905236SJesse Barnes 			       pgtbl_err);
4278a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
4288a905236SJesse Barnes 			(void)I915_READ(PGTBL_ER);
4298a905236SJesse Barnes 		}
4308a905236SJesse Barnes 	}
4318a905236SJesse Barnes 
4328a905236SJesse Barnes 	if (IS_I9XX(dev)) {
43363eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
43463eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
43563eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
43663eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
43763eeaf38SJesse Barnes 			       pgtbl_err);
43863eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
43963eeaf38SJesse Barnes 			(void)I915_READ(PGTBL_ER);
44063eeaf38SJesse Barnes 		}
4418a905236SJesse Barnes 	}
4428a905236SJesse Barnes 
44363eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
44463eeaf38SJesse Barnes 		printk(KERN_ERR "memory refresh error\n");
44563eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
44663eeaf38SJesse Barnes 		       pipea_stats);
44763eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
44863eeaf38SJesse Barnes 		       pipeb_stats);
44963eeaf38SJesse Barnes 		/* pipestat has already been acked */
45063eeaf38SJesse Barnes 	}
45163eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
45263eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
45363eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
45463eeaf38SJesse Barnes 		       I915_READ(INSTPM));
45563eeaf38SJesse Barnes 		if (!IS_I965G(dev)) {
45663eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
45763eeaf38SJesse Barnes 
45863eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
45963eeaf38SJesse Barnes 			       I915_READ(IPEIR));
46063eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
46163eeaf38SJesse Barnes 			       I915_READ(IPEHR));
46263eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
46363eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
46463eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
46563eeaf38SJesse Barnes 			       I915_READ(ACTHD));
46663eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
46763eeaf38SJesse Barnes 			(void)I915_READ(IPEIR);
46863eeaf38SJesse Barnes 		} else {
46963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
47063eeaf38SJesse Barnes 
47163eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
47263eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
47363eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
47463eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
47563eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
47663eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
47763eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
47863eeaf38SJesse Barnes 			       I915_READ(INSTPS));
47963eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
48063eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
48163eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
48263eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
48363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
48463eeaf38SJesse Barnes 			(void)I915_READ(IPEIR_I965);
48563eeaf38SJesse Barnes 		}
48663eeaf38SJesse Barnes 	}
48763eeaf38SJesse Barnes 
48863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
48963eeaf38SJesse Barnes 	(void)I915_READ(EIR);
49063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
49163eeaf38SJesse Barnes 	if (eir) {
49263eeaf38SJesse Barnes 		/*
49363eeaf38SJesse Barnes 		 * some errors might have become stuck,
49463eeaf38SJesse Barnes 		 * mask them.
49563eeaf38SJesse Barnes 		 */
49663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
49763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
49863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
49963eeaf38SJesse Barnes 	}
5008a905236SJesse Barnes 
501ba1234d1SBen Gamari 	if (wedged) {
502ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
503ba1234d1SBen Gamari 
50411ed50ecSBen Gamari 		/*
50511ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
50611ed50ecSBen Gamari 		 */
50711ed50ecSBen Gamari 		printk("i915: Waking up sleeping processes\n");
50811ed50ecSBen Gamari 		DRM_WAKEUP(&dev_priv->irq_queue);
50911ed50ecSBen Gamari 	}
51011ed50ecSBen Gamari 
5119c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
5128a905236SJesse Barnes }
5138a905236SJesse Barnes 
5148a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
5158a905236SJesse Barnes {
5168a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
5178a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5188a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
5198a905236SJesse Barnes 	u32 iir, new_iir;
5208a905236SJesse Barnes 	u32 pipea_stats, pipeb_stats;
5218a905236SJesse Barnes 	u32 vblank_status;
5228a905236SJesse Barnes 	u32 vblank_enable;
5238a905236SJesse Barnes 	int vblank = 0;
5248a905236SJesse Barnes 	unsigned long irqflags;
5258a905236SJesse Barnes 	int irq_received;
5268a905236SJesse Barnes 	int ret = IRQ_NONE;
5278a905236SJesse Barnes 
5288a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5298a905236SJesse Barnes 
5308a905236SJesse Barnes 	if (IS_IGDNG(dev))
5318a905236SJesse Barnes 		return igdng_irq_handler(dev);
5328a905236SJesse Barnes 
5338a905236SJesse Barnes 	iir = I915_READ(IIR);
5348a905236SJesse Barnes 
5358a905236SJesse Barnes 	if (IS_I965G(dev)) {
5368a905236SJesse Barnes 		vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
5378a905236SJesse Barnes 		vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
5388a905236SJesse Barnes 	} else {
5398a905236SJesse Barnes 		vblank_status = I915_VBLANK_INTERRUPT_STATUS;
5408a905236SJesse Barnes 		vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
5418a905236SJesse Barnes 	}
5428a905236SJesse Barnes 
5438a905236SJesse Barnes 	for (;;) {
5448a905236SJesse Barnes 		irq_received = iir != 0;
5458a905236SJesse Barnes 
5468a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
5478a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
5488a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
5498a905236SJesse Barnes 		 * interrupts (for non-MSI).
5508a905236SJesse Barnes 		 */
5518a905236SJesse Barnes 		spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
5528a905236SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
5538a905236SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
5548a905236SJesse Barnes 
5558a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
556ba1234d1SBen Gamari 			i915_handle_error(dev, false);
5578a905236SJesse Barnes 
5588a905236SJesse Barnes 		/*
5598a905236SJesse Barnes 		 * Clear the PIPE(A|B)STAT regs before the IIR
5608a905236SJesse Barnes 		 */
5618a905236SJesse Barnes 		if (pipea_stats & 0x8000ffff) {
5628a905236SJesse Barnes 			if (pipea_stats &  PIPE_FIFO_UNDERRUN_STATUS)
5638a905236SJesse Barnes 				DRM_DEBUG("pipe a underrun\n");
5648a905236SJesse Barnes 			I915_WRITE(PIPEASTAT, pipea_stats);
5658a905236SJesse Barnes 			irq_received = 1;
5668a905236SJesse Barnes 		}
5678a905236SJesse Barnes 
5688a905236SJesse Barnes 		if (pipeb_stats & 0x8000ffff) {
5698a905236SJesse Barnes 			if (pipeb_stats &  PIPE_FIFO_UNDERRUN_STATUS)
5708a905236SJesse Barnes 				DRM_DEBUG("pipe b underrun\n");
5718a905236SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
5728a905236SJesse Barnes 			irq_received = 1;
5738a905236SJesse Barnes 		}
5748a905236SJesse Barnes 		spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
5758a905236SJesse Barnes 
5768a905236SJesse Barnes 		if (!irq_received)
5778a905236SJesse Barnes 			break;
5788a905236SJesse Barnes 
5798a905236SJesse Barnes 		ret = IRQ_HANDLED;
5808a905236SJesse Barnes 
5818a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
5828a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
5838a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
5848a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
5858a905236SJesse Barnes 
5868a905236SJesse Barnes 			DRM_DEBUG("hotplug event received, stat 0x%08x\n",
5878a905236SJesse Barnes 				  hotplug_status);
5888a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
5899c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
5909c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
5918a905236SJesse Barnes 
5928a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
5938a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
59404302965SShaohua Li 
59504302965SShaohua Li 			/* EOS interrupts occurs */
59604302965SShaohua Li 			if (IS_IGD(dev) &&
59704302965SShaohua Li 				(hotplug_status & CRT_EOS_INT_STATUS)) {
59804302965SShaohua Li 				u32 temp;
59904302965SShaohua Li 
60004302965SShaohua Li 				DRM_DEBUG("EOS interrupt occurs\n");
60104302965SShaohua Li 				/* status is already cleared */
60204302965SShaohua Li 				temp = I915_READ(ADPA);
60304302965SShaohua Li 				temp &= ~ADPA_DAC_ENABLE;
60404302965SShaohua Li 				I915_WRITE(ADPA, temp);
60504302965SShaohua Li 
60604302965SShaohua Li 				temp = I915_READ(PORT_HOTPLUG_EN);
60704302965SShaohua Li 				temp &= ~CRT_EOS_INT_EN;
60804302965SShaohua Li 				I915_WRITE(PORT_HOTPLUG_EN, temp);
60904302965SShaohua Li 
61004302965SShaohua Li 				temp = I915_READ(PORT_HOTPLUG_STAT);
61104302965SShaohua Li 				if (temp & CRT_EOS_INT_STATUS)
61204302965SShaohua Li 					I915_WRITE(PORT_HOTPLUG_STAT,
61304302965SShaohua Li 						CRT_EOS_INT_STATUS);
61404302965SShaohua Li 			}
61563eeaf38SJesse Barnes 		}
61663eeaf38SJesse Barnes 
617673a394bSEric Anholt 		I915_WRITE(IIR, iir);
618cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
6197c463586SKeith Packard 
6207c1c2871SDave Airlie 		if (dev->primary->master) {
6217c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
6227c1c2871SDave Airlie 			if (master_priv->sarea_priv)
6237c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
624c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
6257c1c2871SDave Airlie 		}
6260a3e67a4SJesse Barnes 
627673a394bSEric Anholt 		if (iir & I915_USER_INTERRUPT) {
628*1c5d22f7SChris Wilson 			u32 seqno = i915_get_gem_seqno(dev);
629*1c5d22f7SChris Wilson 			dev_priv->mm.irq_gem_seqno = seqno;
630*1c5d22f7SChris Wilson 			trace_i915_gem_request_complete(dev, seqno);
631673a394bSEric Anholt 			DRM_WAKEUP(&dev_priv->irq_queue);
632f65d9421SBen Gamari 			dev_priv->hangcheck_count = 0;
633f65d9421SBen Gamari 			mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
634673a394bSEric Anholt 		}
635673a394bSEric Anholt 
63605eff845SKeith Packard 		if (pipea_stats & vblank_status) {
6377c463586SKeith Packard 			vblank++;
6387c463586SKeith Packard 			drm_handle_vblank(dev, 0);
6397c463586SKeith Packard 		}
6407c463586SKeith Packard 
64105eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
6427c463586SKeith Packard 			vblank++;
6437c463586SKeith Packard 			drm_handle_vblank(dev, 1);
6447c463586SKeith Packard 		}
6457c463586SKeith Packard 
6467c463586SKeith Packard 		if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
6477c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
648673a394bSEric Anholt 			opregion_asle_intr(dev);
6490a3e67a4SJesse Barnes 
650cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
651cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
652cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
653cdfbc41fSEric Anholt 		 * we would never get another interrupt.
654cdfbc41fSEric Anholt 		 *
655cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
656cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
657cdfbc41fSEric Anholt 		 * another one.
658cdfbc41fSEric Anholt 		 *
659cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
660cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
661cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
662cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
663cdfbc41fSEric Anholt 		 * stray interrupts.
664cdfbc41fSEric Anholt 		 */
665cdfbc41fSEric Anholt 		iir = new_iir;
66605eff845SKeith Packard 	}
667cdfbc41fSEric Anholt 
66805eff845SKeith Packard 	return ret;
669c0e09200SDave Airlie }
670c0e09200SDave Airlie 
671c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
672c0e09200SDave Airlie {
673c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
6747c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
675c0e09200SDave Airlie 	RING_LOCALS;
676c0e09200SDave Airlie 
677c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
678c0e09200SDave Airlie 
679c0e09200SDave Airlie 	DRM_DEBUG("\n");
680c0e09200SDave Airlie 
681c99b058fSKristian Høgsberg 	dev_priv->counter++;
682c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
683c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
6847c1c2871SDave Airlie 	if (master_priv->sarea_priv)
6857c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
686c0e09200SDave Airlie 
6870baf823aSKeith Packard 	BEGIN_LP_RING(4);
688585fb111SJesse Barnes 	OUT_RING(MI_STORE_DWORD_INDEX);
6890baf823aSKeith Packard 	OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
690c0e09200SDave Airlie 	OUT_RING(dev_priv->counter);
691585fb111SJesse Barnes 	OUT_RING(MI_USER_INTERRUPT);
692c0e09200SDave Airlie 	ADVANCE_LP_RING();
693c0e09200SDave Airlie 
694c0e09200SDave Airlie 	return dev_priv->counter;
695c0e09200SDave Airlie }
696c0e09200SDave Airlie 
697673a394bSEric Anholt void i915_user_irq_get(struct drm_device *dev)
698ed4cb414SEric Anholt {
699ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
700e9d21d7fSKeith Packard 	unsigned long irqflags;
701ed4cb414SEric Anholt 
702e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
703036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
704036a4a7dSZhenyu Wang 		if (IS_IGDNG(dev))
705036a4a7dSZhenyu Wang 			igdng_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
706036a4a7dSZhenyu Wang 		else
707ed4cb414SEric Anholt 			i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
708036a4a7dSZhenyu Wang 	}
709e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
710ed4cb414SEric Anholt }
711ed4cb414SEric Anholt 
7120a3e67a4SJesse Barnes void i915_user_irq_put(struct drm_device *dev)
713ed4cb414SEric Anholt {
714ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
715e9d21d7fSKeith Packard 	unsigned long irqflags;
716ed4cb414SEric Anholt 
717e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
718ed4cb414SEric Anholt 	BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
719036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
720036a4a7dSZhenyu Wang 		if (IS_IGDNG(dev))
721036a4a7dSZhenyu Wang 			igdng_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
722036a4a7dSZhenyu Wang 		else
723ed4cb414SEric Anholt 			i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
724036a4a7dSZhenyu Wang 	}
725e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
726ed4cb414SEric Anholt }
727ed4cb414SEric Anholt 
728c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
729c0e09200SDave Airlie {
730c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7317c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
732c0e09200SDave Airlie 	int ret = 0;
733c0e09200SDave Airlie 
734c0e09200SDave Airlie 	DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
735c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
736c0e09200SDave Airlie 
737ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
7387c1c2871SDave Airlie 		if (master_priv->sarea_priv)
7397c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
740c0e09200SDave Airlie 		return 0;
741ed4cb414SEric Anholt 	}
742c0e09200SDave Airlie 
7437c1c2871SDave Airlie 	if (master_priv->sarea_priv)
7447c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
745c0e09200SDave Airlie 
746ed4cb414SEric Anholt 	i915_user_irq_get(dev);
747c0e09200SDave Airlie 	DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
748c0e09200SDave Airlie 		    READ_BREADCRUMB(dev_priv) >= irq_nr);
749ed4cb414SEric Anholt 	i915_user_irq_put(dev);
750c0e09200SDave Airlie 
751c0e09200SDave Airlie 	if (ret == -EBUSY) {
752c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
753c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
754c0e09200SDave Airlie 	}
755c0e09200SDave Airlie 
756c0e09200SDave Airlie 	return ret;
757c0e09200SDave Airlie }
758c0e09200SDave Airlie 
759c0e09200SDave Airlie /* Needs the lock as it touches the ring.
760c0e09200SDave Airlie  */
761c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
762c0e09200SDave Airlie 			 struct drm_file *file_priv)
763c0e09200SDave Airlie {
764c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
765c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
766c0e09200SDave Airlie 	int result;
767c0e09200SDave Airlie 
76807f4f8bfSEric Anholt 	if (!dev_priv || !dev_priv->ring.virtual_start) {
769c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
770c0e09200SDave Airlie 		return -EINVAL;
771c0e09200SDave Airlie 	}
772299eb93cSEric Anholt 
773299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
774299eb93cSEric Anholt 
775546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
776c0e09200SDave Airlie 	result = i915_emit_irq(dev);
777546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
778c0e09200SDave Airlie 
779c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
780c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
781c0e09200SDave Airlie 		return -EFAULT;
782c0e09200SDave Airlie 	}
783c0e09200SDave Airlie 
784c0e09200SDave Airlie 	return 0;
785c0e09200SDave Airlie }
786c0e09200SDave Airlie 
787c0e09200SDave Airlie /* Doesn't need the hardware lock.
788c0e09200SDave Airlie  */
789c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
790c0e09200SDave Airlie 			 struct drm_file *file_priv)
791c0e09200SDave Airlie {
792c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
793c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
794c0e09200SDave Airlie 
795c0e09200SDave Airlie 	if (!dev_priv) {
796c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
797c0e09200SDave Airlie 		return -EINVAL;
798c0e09200SDave Airlie 	}
799c0e09200SDave Airlie 
800c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
801c0e09200SDave Airlie }
802c0e09200SDave Airlie 
80342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
80442f52ef8SKeith Packard  * we use as a pipe index
80542f52ef8SKeith Packard  */
80642f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
8070a3e67a4SJesse Barnes {
8080a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
809e9d21d7fSKeith Packard 	unsigned long irqflags;
81071e0ffa5SJesse Barnes 	int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
81171e0ffa5SJesse Barnes 	u32 pipeconf;
81271e0ffa5SJesse Barnes 
81371e0ffa5SJesse Barnes 	pipeconf = I915_READ(pipeconf_reg);
81471e0ffa5SJesse Barnes 	if (!(pipeconf & PIPEACONF_ENABLE))
81571e0ffa5SJesse Barnes 		return -EINVAL;
8160a3e67a4SJesse Barnes 
817036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev))
818036a4a7dSZhenyu Wang 		return 0;
819036a4a7dSZhenyu Wang 
820e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
8210a3e67a4SJesse Barnes 	if (IS_I965G(dev))
8227c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
8237c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
8240a3e67a4SJesse Barnes 	else
8257c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
8267c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
827e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
8280a3e67a4SJesse Barnes 	return 0;
8290a3e67a4SJesse Barnes }
8300a3e67a4SJesse Barnes 
83142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
83242f52ef8SKeith Packard  * we use as a pipe index
83342f52ef8SKeith Packard  */
83442f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
8350a3e67a4SJesse Barnes {
8360a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
837e9d21d7fSKeith Packard 	unsigned long irqflags;
8380a3e67a4SJesse Barnes 
839036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev))
840036a4a7dSZhenyu Wang 		return;
841036a4a7dSZhenyu Wang 
842e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
8437c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
8447c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
8457c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
846e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
8470a3e67a4SJesse Barnes }
8480a3e67a4SJesse Barnes 
84979e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev)
85079e53945SJesse Barnes {
85179e53945SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
852e170b030SZhenyu Wang 
853e170b030SZhenyu Wang 	if (!IS_IGDNG(dev))
85479e53945SJesse Barnes 		opregion_enable_asle(dev);
85579e53945SJesse Barnes 	dev_priv->irq_enabled = 1;
85679e53945SJesse Barnes }
85779e53945SJesse Barnes 
85879e53945SJesse Barnes 
859c0e09200SDave Airlie /* Set the vblank monitor pipe
860c0e09200SDave Airlie  */
861c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
862c0e09200SDave Airlie 			 struct drm_file *file_priv)
863c0e09200SDave Airlie {
864c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
865c0e09200SDave Airlie 
866c0e09200SDave Airlie 	if (!dev_priv) {
867c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
868c0e09200SDave Airlie 		return -EINVAL;
869c0e09200SDave Airlie 	}
870c0e09200SDave Airlie 
871c0e09200SDave Airlie 	return 0;
872c0e09200SDave Airlie }
873c0e09200SDave Airlie 
874c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
875c0e09200SDave Airlie 			 struct drm_file *file_priv)
876c0e09200SDave Airlie {
877c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
878c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
879c0e09200SDave Airlie 
880c0e09200SDave Airlie 	if (!dev_priv) {
881c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
882c0e09200SDave Airlie 		return -EINVAL;
883c0e09200SDave Airlie 	}
884c0e09200SDave Airlie 
8850a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
886c0e09200SDave Airlie 
887c0e09200SDave Airlie 	return 0;
888c0e09200SDave Airlie }
889c0e09200SDave Airlie 
890c0e09200SDave Airlie /**
891c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
892c0e09200SDave Airlie  */
893c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
894c0e09200SDave Airlie 		     struct drm_file *file_priv)
895c0e09200SDave Airlie {
896bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
897bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
898bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
899bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
900bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
901bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
902bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
903bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
904bd95e0a4SEric Anholt 	 *
905bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
906bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
907bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
908bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
9090a3e67a4SJesse Barnes 	 */
910c0e09200SDave Airlie 	return -EINVAL;
911c0e09200SDave Airlie }
912c0e09200SDave Airlie 
913f65d9421SBen Gamari struct drm_i915_gem_request *i915_get_tail_request(struct drm_device *dev) {
914f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
915f65d9421SBen Gamari 	return list_entry(dev_priv->mm.request_list.prev, struct drm_i915_gem_request, list);
916f65d9421SBen Gamari }
917f65d9421SBen Gamari 
918f65d9421SBen Gamari /**
919f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
920f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
921f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
922f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
923f65d9421SBen Gamari  */
924f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
925f65d9421SBen Gamari {
926f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
927f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
928f65d9421SBen Gamari 	uint32_t acthd;
929f65d9421SBen Gamari 
930f65d9421SBen Gamari 	if (!IS_I965G(dev))
931f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
932f65d9421SBen Gamari 	else
933f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
934f65d9421SBen Gamari 
935f65d9421SBen Gamari 	/* If all work is done then ACTHD clearly hasn't advanced. */
936f65d9421SBen Gamari 	if (list_empty(&dev_priv->mm.request_list) ||
937f65d9421SBen Gamari 		       i915_seqno_passed(i915_get_gem_seqno(dev), i915_get_tail_request(dev)->seqno)) {
938f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
939f65d9421SBen Gamari 		return;
940f65d9421SBen Gamari 	}
941f65d9421SBen Gamari 
942f65d9421SBen Gamari 	if (dev_priv->last_acthd == acthd && dev_priv->hangcheck_count > 0) {
943f65d9421SBen Gamari 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
944ba1234d1SBen Gamari 		i915_handle_error(dev, true);
945f65d9421SBen Gamari 		return;
946f65d9421SBen Gamari 	}
947f65d9421SBen Gamari 
948f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
949f65d9421SBen Gamari 	mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
950f65d9421SBen Gamari 
951f65d9421SBen Gamari 	if (acthd != dev_priv->last_acthd)
952f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
953f65d9421SBen Gamari 	else
954f65d9421SBen Gamari 		dev_priv->hangcheck_count++;
955f65d9421SBen Gamari 
956f65d9421SBen Gamari 	dev_priv->last_acthd = acthd;
957f65d9421SBen Gamari }
958f65d9421SBen Gamari 
959c0e09200SDave Airlie /* drm_dma.h hooks
960c0e09200SDave Airlie */
961036a4a7dSZhenyu Wang static void igdng_irq_preinstall(struct drm_device *dev)
962036a4a7dSZhenyu Wang {
963036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
964036a4a7dSZhenyu Wang 
965036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
966036a4a7dSZhenyu Wang 
967036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
968036a4a7dSZhenyu Wang 
969036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
970036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
971036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
972036a4a7dSZhenyu Wang 
973036a4a7dSZhenyu Wang 	/* and GT */
974036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
975036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
976036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
977036a4a7dSZhenyu Wang }
978036a4a7dSZhenyu Wang 
979036a4a7dSZhenyu Wang static int igdng_irq_postinstall(struct drm_device *dev)
980036a4a7dSZhenyu Wang {
981036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
982036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
983036a4a7dSZhenyu Wang 	u32 display_mask = DE_MASTER_IRQ_CONTROL /*| DE_PCH_EVENT */;
984036a4a7dSZhenyu Wang 	u32 render_mask = GT_USER_INTERRUPT;
985036a4a7dSZhenyu Wang 
986036a4a7dSZhenyu Wang 	dev_priv->irq_mask_reg = ~display_mask;
987036a4a7dSZhenyu Wang 	dev_priv->de_irq_enable_reg = display_mask;
988036a4a7dSZhenyu Wang 
989036a4a7dSZhenyu Wang 	/* should always can generate irq */
990036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
991036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
992036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
993036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
994036a4a7dSZhenyu Wang 
995036a4a7dSZhenyu Wang 	/* user interrupt should be enabled, but masked initial */
996036a4a7dSZhenyu Wang 	dev_priv->gt_irq_mask_reg = 0xffffffff;
997036a4a7dSZhenyu Wang 	dev_priv->gt_irq_enable_reg = render_mask;
998036a4a7dSZhenyu Wang 
999036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1000036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
1001036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
1002036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
1003036a4a7dSZhenyu Wang 
1004036a4a7dSZhenyu Wang 	return 0;
1005036a4a7dSZhenyu Wang }
1006036a4a7dSZhenyu Wang 
1007c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1008c0e09200SDave Airlie {
1009c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1010c0e09200SDave Airlie 
101179e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
101279e53945SJesse Barnes 
1013036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
10148a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1015036a4a7dSZhenyu Wang 
1016036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev)) {
1017036a4a7dSZhenyu Wang 		igdng_irq_preinstall(dev);
1018036a4a7dSZhenyu Wang 		return;
1019036a4a7dSZhenyu Wang 	}
1020036a4a7dSZhenyu Wang 
10215ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
10225ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
10235ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
10245ca58282SJesse Barnes 	}
10255ca58282SJesse Barnes 
10260a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
10277c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
10287c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
10290a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1030ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
10317c463586SKeith Packard 	(void) I915_READ(IER);
1032c0e09200SDave Airlie }
1033c0e09200SDave Airlie 
10340a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1035c0e09200SDave Airlie {
1036c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
10375ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
103863eeaf38SJesse Barnes 	u32 error_mask;
10390a3e67a4SJesse Barnes 
1040036a4a7dSZhenyu Wang 	DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
1041036a4a7dSZhenyu Wang 
10420a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1043ed4cb414SEric Anholt 
1044036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev))
1045036a4a7dSZhenyu Wang 		return igdng_irq_postinstall(dev);
1046036a4a7dSZhenyu Wang 
10477c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
10487c463586SKeith Packard 	dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
10498ee1c3dbSMatthew Garrett 
10507c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
10517c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
10527c463586SKeith Packard 
10535ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
10545ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
10555ca58282SJesse Barnes 
10565ca58282SJesse Barnes 		/* Leave other bits alone */
10575ca58282SJesse Barnes 		hotplug_en |= HOTPLUG_EN_MASK;
10585ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
10595ca58282SJesse Barnes 
10605ca58282SJesse Barnes 		dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
10615ca58282SJesse Barnes 			TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
10625ca58282SJesse Barnes 			SDVOB_HOTPLUG_INT_STATUS;
10635ca58282SJesse Barnes 		if (IS_G4X(dev)) {
10645ca58282SJesse Barnes 			dev_priv->hotplug_supported_mask |=
10655ca58282SJesse Barnes 				HDMIB_HOTPLUG_INT_STATUS |
10665ca58282SJesse Barnes 				HDMIC_HOTPLUG_INT_STATUS |
10675ca58282SJesse Barnes 				HDMID_HOTPLUG_INT_STATUS;
10685ca58282SJesse Barnes 		}
10695ca58282SJesse Barnes 		/* Enable in IER... */
10705ca58282SJesse Barnes 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
10715ca58282SJesse Barnes 		/* and unmask in IMR */
10725ca58282SJesse Barnes 		i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
10735ca58282SJesse Barnes 	}
10745ca58282SJesse Barnes 
107563eeaf38SJesse Barnes 	/*
107663eeaf38SJesse Barnes 	 * Enable some error detection, note the instruction error mask
107763eeaf38SJesse Barnes 	 * bit is reserved, so we leave it masked.
107863eeaf38SJesse Barnes 	 */
107963eeaf38SJesse Barnes 	if (IS_G4X(dev)) {
108063eeaf38SJesse Barnes 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
108163eeaf38SJesse Barnes 			       GM45_ERROR_MEM_PRIV |
108263eeaf38SJesse Barnes 			       GM45_ERROR_CP_PRIV |
108363eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
108463eeaf38SJesse Barnes 	} else {
108563eeaf38SJesse Barnes 		error_mask = ~(I915_ERROR_PAGE_TABLE |
108663eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
108763eeaf38SJesse Barnes 	}
108863eeaf38SJesse Barnes 	I915_WRITE(EMR, error_mask);
108963eeaf38SJesse Barnes 
10907c463586SKeith Packard 	/* Disable pipe interrupt enables, clear pending pipe status */
10917c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
10927c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
10937c463586SKeith Packard 	/* Clear pending interrupt status */
10947c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
10957c463586SKeith Packard 
10965ca58282SJesse Barnes 	I915_WRITE(IER, enable_mask);
10977c463586SKeith Packard 	I915_WRITE(IMR, dev_priv->irq_mask_reg);
1098ed4cb414SEric Anholt 	(void) I915_READ(IER);
1099ed4cb414SEric Anholt 
11008ee1c3dbSMatthew Garrett 	opregion_enable_asle(dev);
11010a3e67a4SJesse Barnes 
11020a3e67a4SJesse Barnes 	return 0;
1103c0e09200SDave Airlie }
1104c0e09200SDave Airlie 
1105036a4a7dSZhenyu Wang static void igdng_irq_uninstall(struct drm_device *dev)
1106036a4a7dSZhenyu Wang {
1107036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1108036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1109036a4a7dSZhenyu Wang 
1110036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1111036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1112036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1113036a4a7dSZhenyu Wang 
1114036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1115036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1116036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1117036a4a7dSZhenyu Wang }
1118036a4a7dSZhenyu Wang 
1119c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1120c0e09200SDave Airlie {
1121c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1122c0e09200SDave Airlie 
1123c0e09200SDave Airlie 	if (!dev_priv)
1124c0e09200SDave Airlie 		return;
1125c0e09200SDave Airlie 
11260a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
11270a3e67a4SJesse Barnes 
1128036a4a7dSZhenyu Wang 	if (IS_IGDNG(dev)) {
1129036a4a7dSZhenyu Wang 		igdng_irq_uninstall(dev);
1130036a4a7dSZhenyu Wang 		return;
1131036a4a7dSZhenyu Wang 	}
1132036a4a7dSZhenyu Wang 
11335ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
11345ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
11355ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
11365ca58282SJesse Barnes 	}
11375ca58282SJesse Barnes 
11380a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
11397c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
11407c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
11410a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1142ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1143c0e09200SDave Airlie 
11447c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
11457c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
11467c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1147c0e09200SDave Airlie }
1148