xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 013d5aa2bbb2ceacba7a0dad7f2a0eb20133323f)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
2963eeaf38SJesse Barnes #include <linux/sysrq.h>
30c0e09200SDave Airlie #include "drmP.h"
31c0e09200SDave Airlie #include "drm.h"
32c0e09200SDave Airlie #include "i915_drm.h"
33c0e09200SDave Airlie #include "i915_drv.h"
341c5d22f7SChris Wilson #include "i915_trace.h"
3579e53945SJesse Barnes #include "intel_drv.h"
36c0e09200SDave Airlie 
37c0e09200SDave Airlie #define MAX_NOPID ((u32)~0)
38c0e09200SDave Airlie 
397c463586SKeith Packard /**
407c463586SKeith Packard  * Interrupts that are always left unmasked.
417c463586SKeith Packard  *
427c463586SKeith Packard  * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
437c463586SKeith Packard  * we leave them always unmasked in IMR and then control enabling them through
447c463586SKeith Packard  * PIPESTAT alone.
457c463586SKeith Packard  */
466b95a207SKristian Høgsberg #define I915_INTERRUPT_ENABLE_FIX			\
476b95a207SKristian Høgsberg 	(I915_ASLE_INTERRUPT |				\
480a3e67a4SJesse Barnes 	 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |		\
4963eeaf38SJesse Barnes 	 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |		\
506b95a207SKristian Høgsberg 	 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |	\
516b95a207SKristian Høgsberg 	 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |	\
5263eeaf38SJesse Barnes 	 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
53ed4cb414SEric Anholt 
547c463586SKeith Packard /** Interrupts that we mask and unmask at runtime. */
557c463586SKeith Packard #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
567c463586SKeith Packard 
5779e53945SJesse Barnes #define I915_PIPE_VBLANK_STATUS	(PIPE_START_VBLANK_INTERRUPT_STATUS |\
5879e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_STATUS)
5979e53945SJesse Barnes 
6079e53945SJesse Barnes #define I915_PIPE_VBLANK_ENABLE	(PIPE_START_VBLANK_INTERRUPT_ENABLE |\
6179e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_ENABLE)
6279e53945SJesse Barnes 
6379e53945SJesse Barnes #define DRM_I915_VBLANK_PIPE_ALL	(DRM_I915_VBLANK_PIPE_A | \
6479e53945SJesse Barnes 					 DRM_I915_VBLANK_PIPE_B)
6579e53945SJesse Barnes 
668ee1c3dbSMatthew Garrett void
67f2b115e6SAdam Jackson ironlake_enable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
68036a4a7dSZhenyu Wang {
69036a4a7dSZhenyu Wang 	if ((dev_priv->gt_irq_mask_reg & mask) != 0) {
70036a4a7dSZhenyu Wang 		dev_priv->gt_irq_mask_reg &= ~mask;
71036a4a7dSZhenyu Wang 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
72036a4a7dSZhenyu Wang 		(void) I915_READ(GTIMR);
73036a4a7dSZhenyu Wang 	}
74036a4a7dSZhenyu Wang }
75036a4a7dSZhenyu Wang 
76036a4a7dSZhenyu Wang static inline void
77f2b115e6SAdam Jackson ironlake_disable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
78036a4a7dSZhenyu Wang {
79036a4a7dSZhenyu Wang 	if ((dev_priv->gt_irq_mask_reg & mask) != mask) {
80036a4a7dSZhenyu Wang 		dev_priv->gt_irq_mask_reg |= mask;
81036a4a7dSZhenyu Wang 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
82036a4a7dSZhenyu Wang 		(void) I915_READ(GTIMR);
83036a4a7dSZhenyu Wang 	}
84036a4a7dSZhenyu Wang }
85036a4a7dSZhenyu Wang 
86036a4a7dSZhenyu Wang /* For display hotplug interrupt */
87036a4a7dSZhenyu Wang void
88f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
89036a4a7dSZhenyu Wang {
90036a4a7dSZhenyu Wang 	if ((dev_priv->irq_mask_reg & mask) != 0) {
91036a4a7dSZhenyu Wang 		dev_priv->irq_mask_reg &= ~mask;
92036a4a7dSZhenyu Wang 		I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
93036a4a7dSZhenyu Wang 		(void) I915_READ(DEIMR);
94036a4a7dSZhenyu Wang 	}
95036a4a7dSZhenyu Wang }
96036a4a7dSZhenyu Wang 
97036a4a7dSZhenyu Wang static inline void
98f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
99036a4a7dSZhenyu Wang {
100036a4a7dSZhenyu Wang 	if ((dev_priv->irq_mask_reg & mask) != mask) {
101036a4a7dSZhenyu Wang 		dev_priv->irq_mask_reg |= mask;
102036a4a7dSZhenyu Wang 		I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
103036a4a7dSZhenyu Wang 		(void) I915_READ(DEIMR);
104036a4a7dSZhenyu Wang 	}
105036a4a7dSZhenyu Wang }
106036a4a7dSZhenyu Wang 
107036a4a7dSZhenyu Wang void
108ed4cb414SEric Anholt i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
109ed4cb414SEric Anholt {
110ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != 0) {
111ed4cb414SEric Anholt 		dev_priv->irq_mask_reg &= ~mask;
112ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
113ed4cb414SEric Anholt 		(void) I915_READ(IMR);
114ed4cb414SEric Anholt 	}
115ed4cb414SEric Anholt }
116ed4cb414SEric Anholt 
117ed4cb414SEric Anholt static inline void
118ed4cb414SEric Anholt i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
119ed4cb414SEric Anholt {
120ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != mask) {
121ed4cb414SEric Anholt 		dev_priv->irq_mask_reg |= mask;
122ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
123ed4cb414SEric Anholt 		(void) I915_READ(IMR);
124ed4cb414SEric Anholt 	}
125ed4cb414SEric Anholt }
126ed4cb414SEric Anholt 
1277c463586SKeith Packard static inline u32
1287c463586SKeith Packard i915_pipestat(int pipe)
1297c463586SKeith Packard {
1307c463586SKeith Packard 	if (pipe == 0)
1317c463586SKeith Packard 		return PIPEASTAT;
1327c463586SKeith Packard 	if (pipe == 1)
1337c463586SKeith Packard 		return PIPEBSTAT;
1349c84ba4eSAndrew Morton 	BUG();
1357c463586SKeith Packard }
1367c463586SKeith Packard 
1377c463586SKeith Packard void
1387c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1397c463586SKeith Packard {
1407c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
1417c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1427c463586SKeith Packard 
1437c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
1447c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
1457c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
1467c463586SKeith Packard 		(void) I915_READ(reg);
1477c463586SKeith Packard 	}
1487c463586SKeith Packard }
1497c463586SKeith Packard 
1507c463586SKeith Packard void
1517c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1527c463586SKeith Packard {
1537c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
1547c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1557c463586SKeith Packard 
1567c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
1577c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
1587c463586SKeith Packard 		(void) I915_READ(reg);
1597c463586SKeith Packard 	}
1607c463586SKeith Packard }
1617c463586SKeith Packard 
162c0e09200SDave Airlie /**
16301c66889SZhao Yakui  * intel_enable_asle - enable ASLE interrupt for OpRegion
16401c66889SZhao Yakui  */
16501c66889SZhao Yakui void intel_enable_asle (struct drm_device *dev)
16601c66889SZhao Yakui {
16701c66889SZhao Yakui 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16801c66889SZhao Yakui 
169f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev))
170f2b115e6SAdam Jackson 		ironlake_enable_display_irq(dev_priv, DE_GSE);
17101c66889SZhao Yakui 	else
17201c66889SZhao Yakui 		i915_enable_pipestat(dev_priv, 1,
17301c66889SZhao Yakui 				     I915_LEGACY_BLC_EVENT_ENABLE);
17401c66889SZhao Yakui }
17501c66889SZhao Yakui 
17601c66889SZhao Yakui /**
1770a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1780a3e67a4SJesse Barnes  * @dev: DRM device
1790a3e67a4SJesse Barnes  * @pipe: pipe to check
1800a3e67a4SJesse Barnes  *
1810a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1820a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1830a3e67a4SJesse Barnes  * before reading such registers if unsure.
1840a3e67a4SJesse Barnes  */
1850a3e67a4SJesse Barnes static int
1860a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1870a3e67a4SJesse Barnes {
1880a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1890a3e67a4SJesse Barnes 	unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
1900a3e67a4SJesse Barnes 
1910a3e67a4SJesse Barnes 	if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
1920a3e67a4SJesse Barnes 		return 1;
1930a3e67a4SJesse Barnes 
1940a3e67a4SJesse Barnes 	return 0;
1950a3e67a4SJesse Barnes }
1960a3e67a4SJesse Barnes 
19742f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
19842f52ef8SKeith Packard  * we use as a pipe index
19942f52ef8SKeith Packard  */
20042f52ef8SKeith Packard u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
2010a3e67a4SJesse Barnes {
2020a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2030a3e67a4SJesse Barnes 	unsigned long high_frame;
2040a3e67a4SJesse Barnes 	unsigned long low_frame;
2050a3e67a4SJesse Barnes 	u32 high1, high2, low, count;
2060a3e67a4SJesse Barnes 
2070a3e67a4SJesse Barnes 	high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
2080a3e67a4SJesse Barnes 	low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
2090a3e67a4SJesse Barnes 
2100a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
21144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
21244d98a61SZhao Yakui 				"pipe %d\n", pipe);
2130a3e67a4SJesse Barnes 		return 0;
2140a3e67a4SJesse Barnes 	}
2150a3e67a4SJesse Barnes 
2160a3e67a4SJesse Barnes 	/*
2170a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
2180a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
2190a3e67a4SJesse Barnes 	 * register.
2200a3e67a4SJesse Barnes 	 */
2210a3e67a4SJesse Barnes 	do {
2220a3e67a4SJesse Barnes 		high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
2230a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
2240a3e67a4SJesse Barnes 		low =  ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
2250a3e67a4SJesse Barnes 			PIPE_FRAME_LOW_SHIFT);
2260a3e67a4SJesse Barnes 		high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
2270a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
2280a3e67a4SJesse Barnes 	} while (high1 != high2);
2290a3e67a4SJesse Barnes 
2300a3e67a4SJesse Barnes 	count = (high1 << 8) | low;
2310a3e67a4SJesse Barnes 
2320a3e67a4SJesse Barnes 	return count;
2330a3e67a4SJesse Barnes }
2340a3e67a4SJesse Barnes 
2359880b7a5SJesse Barnes u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
2369880b7a5SJesse Barnes {
2379880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2389880b7a5SJesse Barnes 	int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
2399880b7a5SJesse Barnes 
2409880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
24144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
24244d98a61SZhao Yakui 					"pipe %d\n", pipe);
2439880b7a5SJesse Barnes 		return 0;
2449880b7a5SJesse Barnes 	}
2459880b7a5SJesse Barnes 
2469880b7a5SJesse Barnes 	return I915_READ(reg);
2479880b7a5SJesse Barnes }
2489880b7a5SJesse Barnes 
2495ca58282SJesse Barnes /*
2505ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
2515ca58282SJesse Barnes  */
2525ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
2535ca58282SJesse Barnes {
2545ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
2555ca58282SJesse Barnes 						    hotplug_work);
2565ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
257c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
258c31c4ba3SKeith Packard 	struct drm_connector *connector;
2595ca58282SJesse Barnes 
260c31c4ba3SKeith Packard 	if (mode_config->num_connector) {
261c31c4ba3SKeith Packard 		list_for_each_entry(connector, &mode_config->connector_list, head) {
262c31c4ba3SKeith Packard 			struct intel_output *intel_output = to_intel_output(connector);
263c31c4ba3SKeith Packard 
264c31c4ba3SKeith Packard 			if (intel_output->hot_plug)
265c31c4ba3SKeith Packard 				(*intel_output->hot_plug) (intel_output);
266c31c4ba3SKeith Packard 		}
267c31c4ba3SKeith Packard 	}
2685ca58282SJesse Barnes 	/* Just fire off a uevent and let userspace tell us what to do */
2695ca58282SJesse Barnes 	drm_sysfs_hotplug_event(dev);
2705ca58282SJesse Barnes }
2715ca58282SJesse Barnes 
272f2b115e6SAdam Jackson irqreturn_t ironlake_irq_handler(struct drm_device *dev)
273036a4a7dSZhenyu Wang {
274036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
275036a4a7dSZhenyu Wang 	int ret = IRQ_NONE;
2763ff99164SDave Airlie 	u32 de_iir, gt_iir, de_ier, pch_iir;
277036a4a7dSZhenyu Wang 	struct drm_i915_master_private *master_priv;
278036a4a7dSZhenyu Wang 
2792d109a84SZou, Nanhai 	/* disable master interrupt before clearing iir  */
2802d109a84SZou, Nanhai 	de_ier = I915_READ(DEIER);
2812d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
2822d109a84SZou, Nanhai 	(void)I915_READ(DEIER);
2832d109a84SZou, Nanhai 
284036a4a7dSZhenyu Wang 	de_iir = I915_READ(DEIIR);
285036a4a7dSZhenyu Wang 	gt_iir = I915_READ(GTIIR);
286c650156aSZhenyu Wang 	pch_iir = I915_READ(SDEIIR);
287036a4a7dSZhenyu Wang 
288c650156aSZhenyu Wang 	if (de_iir == 0 && gt_iir == 0 && pch_iir == 0)
289c7c85101SZou Nan hai 		goto done;
290036a4a7dSZhenyu Wang 
291036a4a7dSZhenyu Wang 	ret = IRQ_HANDLED;
292036a4a7dSZhenyu Wang 
293036a4a7dSZhenyu Wang 	if (dev->primary->master) {
294036a4a7dSZhenyu Wang 		master_priv = dev->primary->master->driver_priv;
295036a4a7dSZhenyu Wang 		if (master_priv->sarea_priv)
296036a4a7dSZhenyu Wang 			master_priv->sarea_priv->last_dispatch =
297036a4a7dSZhenyu Wang 				READ_BREADCRUMB(dev_priv);
298036a4a7dSZhenyu Wang 	}
299036a4a7dSZhenyu Wang 
300036a4a7dSZhenyu Wang 	if (gt_iir & GT_USER_INTERRUPT) {
3011c5d22f7SChris Wilson 		u32 seqno = i915_get_gem_seqno(dev);
3021c5d22f7SChris Wilson 		dev_priv->mm.irq_gem_seqno = seqno;
3031c5d22f7SChris Wilson 		trace_i915_gem_request_complete(dev, seqno);
304036a4a7dSZhenyu Wang 		DRM_WAKEUP(&dev_priv->irq_queue);
305c566ec49SZhenyu Wang 		dev_priv->hangcheck_count = 0;
306c566ec49SZhenyu Wang 		mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
307036a4a7dSZhenyu Wang 	}
308036a4a7dSZhenyu Wang 
30901c66889SZhao Yakui 	if (de_iir & DE_GSE)
31001c66889SZhao Yakui 		ironlake_opregion_gse_intr(dev);
31101c66889SZhao Yakui 
312*013d5aa2SJesse Barnes 	if (de_iir & DE_PLANEA_FLIP_DONE)
313*013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 0);
314c062df61SLi Peng 
315*013d5aa2SJesse Barnes 	if (de_iir & DE_PLANEB_FLIP_DONE)
316*013d5aa2SJesse Barnes 		intel_prepare_page_flip(dev, 1);
317*013d5aa2SJesse Barnes 
318*013d5aa2SJesse Barnes 	if (de_iir & DE_PIPEA_VBLANK) {
319*013d5aa2SJesse Barnes 		drm_handle_vblank(dev, 0);
320*013d5aa2SJesse Barnes 		intel_finish_page_flip(dev, 0);
321*013d5aa2SJesse Barnes 	}
322*013d5aa2SJesse Barnes 
323*013d5aa2SJesse Barnes 	if (de_iir & DE_PIPEB_VBLANK) {
324c062df61SLi Peng 		drm_handle_vblank(dev, 1);
325*013d5aa2SJesse Barnes 		intel_finish_page_flip(dev, 1);
326*013d5aa2SJesse Barnes 	}
327c062df61SLi Peng 
328c650156aSZhenyu Wang 	/* check event from PCH */
329c650156aSZhenyu Wang 	if ((de_iir & DE_PCH_EVENT) &&
330c650156aSZhenyu Wang 	    (pch_iir & SDE_HOTPLUG_MASK)) {
331c650156aSZhenyu Wang 		queue_work(dev_priv->wq, &dev_priv->hotplug_work);
332c650156aSZhenyu Wang 	}
333c650156aSZhenyu Wang 
334c7c85101SZou Nan hai 	/* should clear PCH hotplug event before clear CPU irq */
335c7c85101SZou Nan hai 	I915_WRITE(SDEIIR, pch_iir);
336c7c85101SZou Nan hai 	I915_WRITE(GTIIR, gt_iir);
337c7c85101SZou Nan hai 	I915_WRITE(DEIIR, de_iir);
338036a4a7dSZhenyu Wang 
339c7c85101SZou Nan hai done:
3402d109a84SZou, Nanhai 	I915_WRITE(DEIER, de_ier);
3412d109a84SZou, Nanhai 	(void)I915_READ(DEIER);
3422d109a84SZou, Nanhai 
343036a4a7dSZhenyu Wang 	return ret;
344036a4a7dSZhenyu Wang }
345036a4a7dSZhenyu Wang 
3468a905236SJesse Barnes /**
3478a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
3488a905236SJesse Barnes  * @work: work struct
3498a905236SJesse Barnes  *
3508a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
3518a905236SJesse Barnes  * was detected.
3528a905236SJesse Barnes  */
3538a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
3548a905236SJesse Barnes {
3558a905236SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3568a905236SJesse Barnes 						    error_work);
3578a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
358f316a42cSBen Gamari 	char *error_event[] = { "ERROR=1", NULL };
359f316a42cSBen Gamari 	char *reset_event[] = { "RESET=1", NULL };
360f316a42cSBen Gamari 	char *reset_done_event[] = { "ERROR=0", NULL };
3618a905236SJesse Barnes 
36244d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("generating error event\n");
363f316a42cSBen Gamari 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
3648a905236SJesse Barnes 
365ba1234d1SBen Gamari 	if (atomic_read(&dev_priv->mm.wedged)) {
366f316a42cSBen Gamari 		if (IS_I965G(dev)) {
36744d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("resetting chip\n");
368f316a42cSBen Gamari 			kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
369f316a42cSBen Gamari 			if (!i965_reset(dev, GDRST_RENDER)) {
370ba1234d1SBen Gamari 				atomic_set(&dev_priv->mm.wedged, 0);
371f316a42cSBen Gamari 				kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
372f316a42cSBen Gamari 			}
373f316a42cSBen Gamari 		} else {
37444d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("reboot required\n");
375f316a42cSBen Gamari 		}
376f316a42cSBen Gamari 	}
3778a905236SJesse Barnes }
3788a905236SJesse Barnes 
3798a905236SJesse Barnes /**
3808a905236SJesse Barnes  * i915_capture_error_state - capture an error record for later analysis
3818a905236SJesse Barnes  * @dev: drm device
3828a905236SJesse Barnes  *
3838a905236SJesse Barnes  * Should be called when an error is detected (either a hang or an error
3848a905236SJesse Barnes  * interrupt) to capture error state from the time of the error.  Fills
3858a905236SJesse Barnes  * out a structure which becomes available in debugfs for user level tools
3868a905236SJesse Barnes  * to pick up.
3878a905236SJesse Barnes  */
38863eeaf38SJesse Barnes static void i915_capture_error_state(struct drm_device *dev)
38963eeaf38SJesse Barnes {
39063eeaf38SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
39163eeaf38SJesse Barnes 	struct drm_i915_error_state *error;
39263eeaf38SJesse Barnes 	unsigned long flags;
39363eeaf38SJesse Barnes 
39463eeaf38SJesse Barnes 	spin_lock_irqsave(&dev_priv->error_lock, flags);
39563eeaf38SJesse Barnes 	if (dev_priv->first_error)
39663eeaf38SJesse Barnes 		goto out;
39763eeaf38SJesse Barnes 
39863eeaf38SJesse Barnes 	error = kmalloc(sizeof(*error), GFP_ATOMIC);
39963eeaf38SJesse Barnes 	if (!error) {
40044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("out ot memory, not capturing error state\n");
40163eeaf38SJesse Barnes 		goto out;
40263eeaf38SJesse Barnes 	}
40363eeaf38SJesse Barnes 
40463eeaf38SJesse Barnes 	error->eir = I915_READ(EIR);
40563eeaf38SJesse Barnes 	error->pgtbl_er = I915_READ(PGTBL_ER);
40663eeaf38SJesse Barnes 	error->pipeastat = I915_READ(PIPEASTAT);
40763eeaf38SJesse Barnes 	error->pipebstat = I915_READ(PIPEBSTAT);
40863eeaf38SJesse Barnes 	error->instpm = I915_READ(INSTPM);
40963eeaf38SJesse Barnes 	if (!IS_I965G(dev)) {
41063eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR);
41163eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR);
41263eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE);
41363eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD);
41463eeaf38SJesse Barnes 	} else {
41563eeaf38SJesse Barnes 		error->ipeir = I915_READ(IPEIR_I965);
41663eeaf38SJesse Barnes 		error->ipehr = I915_READ(IPEHR_I965);
41763eeaf38SJesse Barnes 		error->instdone = I915_READ(INSTDONE_I965);
41863eeaf38SJesse Barnes 		error->instps = I915_READ(INSTPS);
41963eeaf38SJesse Barnes 		error->instdone1 = I915_READ(INSTDONE1);
42063eeaf38SJesse Barnes 		error->acthd = I915_READ(ACTHD_I965);
42163eeaf38SJesse Barnes 	}
42263eeaf38SJesse Barnes 
4238a905236SJesse Barnes 	do_gettimeofday(&error->time);
4248a905236SJesse Barnes 
42563eeaf38SJesse Barnes 	dev_priv->first_error = error;
42663eeaf38SJesse Barnes 
42763eeaf38SJesse Barnes out:
42863eeaf38SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->error_lock, flags);
42963eeaf38SJesse Barnes }
43063eeaf38SJesse Barnes 
4318a905236SJesse Barnes /**
4328a905236SJesse Barnes  * i915_handle_error - handle an error interrupt
4338a905236SJesse Barnes  * @dev: drm device
4348a905236SJesse Barnes  *
4358a905236SJesse Barnes  * Do some basic checking of regsiter state at error interrupt time and
4368a905236SJesse Barnes  * dump it to the syslog.  Also call i915_capture_error_state() to make
4378a905236SJesse Barnes  * sure we get a record and make it available in debugfs.  Fire a uevent
4388a905236SJesse Barnes  * so userspace knows something bad happened (should trigger collection
4398a905236SJesse Barnes  * of a ring dump etc.).
4408a905236SJesse Barnes  */
441ba1234d1SBen Gamari static void i915_handle_error(struct drm_device *dev, bool wedged)
442c0e09200SDave Airlie {
4438a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
44463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
4458a905236SJesse Barnes 	u32 pipea_stats = I915_READ(PIPEASTAT);
4468a905236SJesse Barnes 	u32 pipeb_stats = I915_READ(PIPEBSTAT);
44763eeaf38SJesse Barnes 
44863eeaf38SJesse Barnes 	i915_capture_error_state(dev);
44963eeaf38SJesse Barnes 
45063eeaf38SJesse Barnes 	printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
45163eeaf38SJesse Barnes 	       eir);
4528a905236SJesse Barnes 
4538a905236SJesse Barnes 	if (IS_G4X(dev)) {
4548a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
4558a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
4568a905236SJesse Barnes 
4578a905236SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
4588a905236SJesse Barnes 			       I915_READ(IPEIR_I965));
4598a905236SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
4608a905236SJesse Barnes 			       I915_READ(IPEHR_I965));
4618a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
4628a905236SJesse Barnes 			       I915_READ(INSTDONE_I965));
4638a905236SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
4648a905236SJesse Barnes 			       I915_READ(INSTPS));
4658a905236SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
4668a905236SJesse Barnes 			       I915_READ(INSTDONE1));
4678a905236SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
4688a905236SJesse Barnes 			       I915_READ(ACTHD_I965));
4698a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
4708a905236SJesse Barnes 			(void)I915_READ(IPEIR_I965);
4718a905236SJesse Barnes 		}
4728a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
4738a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
4748a905236SJesse Barnes 			printk(KERN_ERR "page table error\n");
4758a905236SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
4768a905236SJesse Barnes 			       pgtbl_err);
4778a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
4788a905236SJesse Barnes 			(void)I915_READ(PGTBL_ER);
4798a905236SJesse Barnes 		}
4808a905236SJesse Barnes 	}
4818a905236SJesse Barnes 
4828a905236SJesse Barnes 	if (IS_I9XX(dev)) {
48363eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
48463eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
48563eeaf38SJesse Barnes 			printk(KERN_ERR "page table error\n");
48663eeaf38SJesse Barnes 			printk(KERN_ERR "  PGTBL_ER: 0x%08x\n",
48763eeaf38SJesse Barnes 			       pgtbl_err);
48863eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
48963eeaf38SJesse Barnes 			(void)I915_READ(PGTBL_ER);
49063eeaf38SJesse Barnes 		}
4918a905236SJesse Barnes 	}
4928a905236SJesse Barnes 
49363eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
49463eeaf38SJesse Barnes 		printk(KERN_ERR "memory refresh error\n");
49563eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
49663eeaf38SJesse Barnes 		       pipea_stats);
49763eeaf38SJesse Barnes 		printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
49863eeaf38SJesse Barnes 		       pipeb_stats);
49963eeaf38SJesse Barnes 		/* pipestat has already been acked */
50063eeaf38SJesse Barnes 	}
50163eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
50263eeaf38SJesse Barnes 		printk(KERN_ERR "instruction error\n");
50363eeaf38SJesse Barnes 		printk(KERN_ERR "  INSTPM: 0x%08x\n",
50463eeaf38SJesse Barnes 		       I915_READ(INSTPM));
50563eeaf38SJesse Barnes 		if (!IS_I965G(dev)) {
50663eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
50763eeaf38SJesse Barnes 
50863eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
50963eeaf38SJesse Barnes 			       I915_READ(IPEIR));
51063eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
51163eeaf38SJesse Barnes 			       I915_READ(IPEHR));
51263eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
51363eeaf38SJesse Barnes 			       I915_READ(INSTDONE));
51463eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
51563eeaf38SJesse Barnes 			       I915_READ(ACTHD));
51663eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
51763eeaf38SJesse Barnes 			(void)I915_READ(IPEIR);
51863eeaf38SJesse Barnes 		} else {
51963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
52063eeaf38SJesse Barnes 
52163eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEIR: 0x%08x\n",
52263eeaf38SJesse Barnes 			       I915_READ(IPEIR_I965));
52363eeaf38SJesse Barnes 			printk(KERN_ERR "  IPEHR: 0x%08x\n",
52463eeaf38SJesse Barnes 			       I915_READ(IPEHR_I965));
52563eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE: 0x%08x\n",
52663eeaf38SJesse Barnes 			       I915_READ(INSTDONE_I965));
52763eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTPS: 0x%08x\n",
52863eeaf38SJesse Barnes 			       I915_READ(INSTPS));
52963eeaf38SJesse Barnes 			printk(KERN_ERR "  INSTDONE1: 0x%08x\n",
53063eeaf38SJesse Barnes 			       I915_READ(INSTDONE1));
53163eeaf38SJesse Barnes 			printk(KERN_ERR "  ACTHD: 0x%08x\n",
53263eeaf38SJesse Barnes 			       I915_READ(ACTHD_I965));
53363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
53463eeaf38SJesse Barnes 			(void)I915_READ(IPEIR_I965);
53563eeaf38SJesse Barnes 		}
53663eeaf38SJesse Barnes 	}
53763eeaf38SJesse Barnes 
53863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
53963eeaf38SJesse Barnes 	(void)I915_READ(EIR);
54063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
54163eeaf38SJesse Barnes 	if (eir) {
54263eeaf38SJesse Barnes 		/*
54363eeaf38SJesse Barnes 		 * some errors might have become stuck,
54463eeaf38SJesse Barnes 		 * mask them.
54563eeaf38SJesse Barnes 		 */
54663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
54763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
54863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
54963eeaf38SJesse Barnes 	}
5508a905236SJesse Barnes 
551ba1234d1SBen Gamari 	if (wedged) {
552ba1234d1SBen Gamari 		atomic_set(&dev_priv->mm.wedged, 1);
553ba1234d1SBen Gamari 
55411ed50ecSBen Gamari 		/*
55511ed50ecSBen Gamari 		 * Wakeup waiting processes so they don't hang
55611ed50ecSBen Gamari 		 */
55711ed50ecSBen Gamari 		DRM_WAKEUP(&dev_priv->irq_queue);
55811ed50ecSBen Gamari 	}
55911ed50ecSBen Gamari 
5609c9fe1f8SEric Anholt 	queue_work(dev_priv->wq, &dev_priv->error_work);
5618a905236SJesse Barnes }
5628a905236SJesse Barnes 
5638a905236SJesse Barnes irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
5648a905236SJesse Barnes {
5658a905236SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
5668a905236SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5678a905236SJesse Barnes 	struct drm_i915_master_private *master_priv;
5688a905236SJesse Barnes 	u32 iir, new_iir;
5698a905236SJesse Barnes 	u32 pipea_stats, pipeb_stats;
5708a905236SJesse Barnes 	u32 vblank_status;
5718a905236SJesse Barnes 	u32 vblank_enable;
5728a905236SJesse Barnes 	int vblank = 0;
5738a905236SJesse Barnes 	unsigned long irqflags;
5748a905236SJesse Barnes 	int irq_received;
5758a905236SJesse Barnes 	int ret = IRQ_NONE;
5768a905236SJesse Barnes 
5778a905236SJesse Barnes 	atomic_inc(&dev_priv->irq_received);
5788a905236SJesse Barnes 
579f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev))
580f2b115e6SAdam Jackson 		return ironlake_irq_handler(dev);
5818a905236SJesse Barnes 
5828a905236SJesse Barnes 	iir = I915_READ(IIR);
5838a905236SJesse Barnes 
5848a905236SJesse Barnes 	if (IS_I965G(dev)) {
5858a905236SJesse Barnes 		vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
5868a905236SJesse Barnes 		vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
5878a905236SJesse Barnes 	} else {
5888a905236SJesse Barnes 		vblank_status = I915_VBLANK_INTERRUPT_STATUS;
5898a905236SJesse Barnes 		vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
5908a905236SJesse Barnes 	}
5918a905236SJesse Barnes 
5928a905236SJesse Barnes 	for (;;) {
5938a905236SJesse Barnes 		irq_received = iir != 0;
5948a905236SJesse Barnes 
5958a905236SJesse Barnes 		/* Can't rely on pipestat interrupt bit in iir as it might
5968a905236SJesse Barnes 		 * have been cleared after the pipestat interrupt was received.
5978a905236SJesse Barnes 		 * It doesn't set the bit in iir again, but it still produces
5988a905236SJesse Barnes 		 * interrupts (for non-MSI).
5998a905236SJesse Barnes 		 */
6008a905236SJesse Barnes 		spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
6018a905236SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
6028a905236SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
6038a905236SJesse Barnes 
6048a905236SJesse Barnes 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
605ba1234d1SBen Gamari 			i915_handle_error(dev, false);
6068a905236SJesse Barnes 
6078a905236SJesse Barnes 		/*
6088a905236SJesse Barnes 		 * Clear the PIPE(A|B)STAT regs before the IIR
6098a905236SJesse Barnes 		 */
6108a905236SJesse Barnes 		if (pipea_stats & 0x8000ffff) {
6118a905236SJesse Barnes 			if (pipea_stats &  PIPE_FIFO_UNDERRUN_STATUS)
61244d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe a underrun\n");
6138a905236SJesse Barnes 			I915_WRITE(PIPEASTAT, pipea_stats);
6148a905236SJesse Barnes 			irq_received = 1;
6158a905236SJesse Barnes 		}
6168a905236SJesse Barnes 
6178a905236SJesse Barnes 		if (pipeb_stats & 0x8000ffff) {
6188a905236SJesse Barnes 			if (pipeb_stats &  PIPE_FIFO_UNDERRUN_STATUS)
61944d98a61SZhao Yakui 				DRM_DEBUG_DRIVER("pipe b underrun\n");
6208a905236SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
6218a905236SJesse Barnes 			irq_received = 1;
6228a905236SJesse Barnes 		}
6238a905236SJesse Barnes 		spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
6248a905236SJesse Barnes 
6258a905236SJesse Barnes 		if (!irq_received)
6268a905236SJesse Barnes 			break;
6278a905236SJesse Barnes 
6288a905236SJesse Barnes 		ret = IRQ_HANDLED;
6298a905236SJesse Barnes 
6308a905236SJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
6318a905236SJesse Barnes 		if ((I915_HAS_HOTPLUG(dev)) &&
6328a905236SJesse Barnes 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
6338a905236SJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
6348a905236SJesse Barnes 
63544d98a61SZhao Yakui 			DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
6368a905236SJesse Barnes 				  hotplug_status);
6378a905236SJesse Barnes 			if (hotplug_status & dev_priv->hotplug_supported_mask)
6389c9fe1f8SEric Anholt 				queue_work(dev_priv->wq,
6399c9fe1f8SEric Anholt 					   &dev_priv->hotplug_work);
6408a905236SJesse Barnes 
6418a905236SJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
6428a905236SJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
64363eeaf38SJesse Barnes 		}
64463eeaf38SJesse Barnes 
645673a394bSEric Anholt 		I915_WRITE(IIR, iir);
646cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
6477c463586SKeith Packard 
6487c1c2871SDave Airlie 		if (dev->primary->master) {
6497c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
6507c1c2871SDave Airlie 			if (master_priv->sarea_priv)
6517c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
652c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
6537c1c2871SDave Airlie 		}
6540a3e67a4SJesse Barnes 
655673a394bSEric Anholt 		if (iir & I915_USER_INTERRUPT) {
6561c5d22f7SChris Wilson 			u32 seqno = i915_get_gem_seqno(dev);
6571c5d22f7SChris Wilson 			dev_priv->mm.irq_gem_seqno = seqno;
6581c5d22f7SChris Wilson 			trace_i915_gem_request_complete(dev, seqno);
659673a394bSEric Anholt 			DRM_WAKEUP(&dev_priv->irq_queue);
660f65d9421SBen Gamari 			dev_priv->hangcheck_count = 0;
661f65d9421SBen Gamari 			mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
662673a394bSEric Anholt 		}
663673a394bSEric Anholt 
6646b95a207SKristian Høgsberg 		if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
6656b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 0);
6666b95a207SKristian Høgsberg 
6676b95a207SKristian Høgsberg 		if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
6686b95a207SKristian Høgsberg 			intel_prepare_page_flip(dev, 1);
6696b95a207SKristian Høgsberg 
67005eff845SKeith Packard 		if (pipea_stats & vblank_status) {
6717c463586SKeith Packard 			vblank++;
6727c463586SKeith Packard 			drm_handle_vblank(dev, 0);
6736b95a207SKristian Høgsberg 			intel_finish_page_flip(dev, 0);
6747c463586SKeith Packard 		}
6757c463586SKeith Packard 
67605eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
6777c463586SKeith Packard 			vblank++;
6787c463586SKeith Packard 			drm_handle_vblank(dev, 1);
6796b95a207SKristian Høgsberg 			intel_finish_page_flip(dev, 1);
6807c463586SKeith Packard 		}
6817c463586SKeith Packard 
6827c463586SKeith Packard 		if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
6837c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
684673a394bSEric Anholt 			opregion_asle_intr(dev);
6850a3e67a4SJesse Barnes 
686cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
687cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
688cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
689cdfbc41fSEric Anholt 		 * we would never get another interrupt.
690cdfbc41fSEric Anholt 		 *
691cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
692cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
693cdfbc41fSEric Anholt 		 * another one.
694cdfbc41fSEric Anholt 		 *
695cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
696cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
697cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
698cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
699cdfbc41fSEric Anholt 		 * stray interrupts.
700cdfbc41fSEric Anholt 		 */
701cdfbc41fSEric Anholt 		iir = new_iir;
70205eff845SKeith Packard 	}
703cdfbc41fSEric Anholt 
70405eff845SKeith Packard 	return ret;
705c0e09200SDave Airlie }
706c0e09200SDave Airlie 
707c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
708c0e09200SDave Airlie {
709c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
7107c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
711c0e09200SDave Airlie 	RING_LOCALS;
712c0e09200SDave Airlie 
713c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
714c0e09200SDave Airlie 
71544d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("\n");
716c0e09200SDave Airlie 
717c99b058fSKristian Høgsberg 	dev_priv->counter++;
718c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
719c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
7207c1c2871SDave Airlie 	if (master_priv->sarea_priv)
7217c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
722c0e09200SDave Airlie 
7230baf823aSKeith Packard 	BEGIN_LP_RING(4);
724585fb111SJesse Barnes 	OUT_RING(MI_STORE_DWORD_INDEX);
7250baf823aSKeith Packard 	OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
726c0e09200SDave Airlie 	OUT_RING(dev_priv->counter);
727585fb111SJesse Barnes 	OUT_RING(MI_USER_INTERRUPT);
728c0e09200SDave Airlie 	ADVANCE_LP_RING();
729c0e09200SDave Airlie 
730c0e09200SDave Airlie 	return dev_priv->counter;
731c0e09200SDave Airlie }
732c0e09200SDave Airlie 
733673a394bSEric Anholt void i915_user_irq_get(struct drm_device *dev)
734ed4cb414SEric Anholt {
735ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
736e9d21d7fSKeith Packard 	unsigned long irqflags;
737ed4cb414SEric Anholt 
738e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
739036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
740f2b115e6SAdam Jackson 		if (IS_IRONLAKE(dev))
741f2b115e6SAdam Jackson 			ironlake_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
742036a4a7dSZhenyu Wang 		else
743ed4cb414SEric Anholt 			i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
744036a4a7dSZhenyu Wang 	}
745e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
746ed4cb414SEric Anholt }
747ed4cb414SEric Anholt 
7480a3e67a4SJesse Barnes void i915_user_irq_put(struct drm_device *dev)
749ed4cb414SEric Anholt {
750ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
751e9d21d7fSKeith Packard 	unsigned long irqflags;
752ed4cb414SEric Anholt 
753e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
754ed4cb414SEric Anholt 	BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
755036a4a7dSZhenyu Wang 	if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
756f2b115e6SAdam Jackson 		if (IS_IRONLAKE(dev))
757f2b115e6SAdam Jackson 			ironlake_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
758036a4a7dSZhenyu Wang 		else
759ed4cb414SEric Anholt 			i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
760036a4a7dSZhenyu Wang 	}
761e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
762ed4cb414SEric Anholt }
763ed4cb414SEric Anholt 
7649d34e5dbSChris Wilson void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
7659d34e5dbSChris Wilson {
7669d34e5dbSChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7679d34e5dbSChris Wilson 
7689d34e5dbSChris Wilson 	if (dev_priv->trace_irq_seqno == 0)
7699d34e5dbSChris Wilson 		i915_user_irq_get(dev);
7709d34e5dbSChris Wilson 
7719d34e5dbSChris Wilson 	dev_priv->trace_irq_seqno = seqno;
7729d34e5dbSChris Wilson }
7739d34e5dbSChris Wilson 
774c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
775c0e09200SDave Airlie {
776c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7777c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
778c0e09200SDave Airlie 	int ret = 0;
779c0e09200SDave Airlie 
78044d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
781c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
782c0e09200SDave Airlie 
783ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
7847c1c2871SDave Airlie 		if (master_priv->sarea_priv)
7857c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
786c0e09200SDave Airlie 		return 0;
787ed4cb414SEric Anholt 	}
788c0e09200SDave Airlie 
7897c1c2871SDave Airlie 	if (master_priv->sarea_priv)
7907c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
791c0e09200SDave Airlie 
792ed4cb414SEric Anholt 	i915_user_irq_get(dev);
793c0e09200SDave Airlie 	DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
794c0e09200SDave Airlie 		    READ_BREADCRUMB(dev_priv) >= irq_nr);
795ed4cb414SEric Anholt 	i915_user_irq_put(dev);
796c0e09200SDave Airlie 
797c0e09200SDave Airlie 	if (ret == -EBUSY) {
798c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
799c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
800c0e09200SDave Airlie 	}
801c0e09200SDave Airlie 
802c0e09200SDave Airlie 	return ret;
803c0e09200SDave Airlie }
804c0e09200SDave Airlie 
805c0e09200SDave Airlie /* Needs the lock as it touches the ring.
806c0e09200SDave Airlie  */
807c0e09200SDave Airlie int i915_irq_emit(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_emit_t *emit = data;
812c0e09200SDave Airlie 	int result;
813c0e09200SDave Airlie 
81407f4f8bfSEric Anholt 	if (!dev_priv || !dev_priv->ring.virtual_start) {
815c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
816c0e09200SDave Airlie 		return -EINVAL;
817c0e09200SDave Airlie 	}
818299eb93cSEric Anholt 
819299eb93cSEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
820299eb93cSEric Anholt 
821546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
822c0e09200SDave Airlie 	result = i915_emit_irq(dev);
823546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
824c0e09200SDave Airlie 
825c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
826c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
827c0e09200SDave Airlie 		return -EFAULT;
828c0e09200SDave Airlie 	}
829c0e09200SDave Airlie 
830c0e09200SDave Airlie 	return 0;
831c0e09200SDave Airlie }
832c0e09200SDave Airlie 
833c0e09200SDave Airlie /* Doesn't need the hardware lock.
834c0e09200SDave Airlie  */
835c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
836c0e09200SDave Airlie 			 struct drm_file *file_priv)
837c0e09200SDave Airlie {
838c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
839c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
840c0e09200SDave Airlie 
841c0e09200SDave Airlie 	if (!dev_priv) {
842c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
843c0e09200SDave Airlie 		return -EINVAL;
844c0e09200SDave Airlie 	}
845c0e09200SDave Airlie 
846c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
847c0e09200SDave Airlie }
848c0e09200SDave Airlie 
84942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
85042f52ef8SKeith Packard  * we use as a pipe index
85142f52ef8SKeith Packard  */
85242f52ef8SKeith Packard int i915_enable_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;
85671e0ffa5SJesse Barnes 	int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
85771e0ffa5SJesse Barnes 	u32 pipeconf;
85871e0ffa5SJesse Barnes 
85971e0ffa5SJesse Barnes 	pipeconf = I915_READ(pipeconf_reg);
86071e0ffa5SJesse Barnes 	if (!(pipeconf & PIPEACONF_ENABLE))
86171e0ffa5SJesse Barnes 		return -EINVAL;
8620a3e67a4SJesse Barnes 
863e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
864c062df61SLi Peng 	if (IS_IRONLAKE(dev))
865c062df61SLi Peng 		ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
866c062df61SLi Peng 					    DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
867c062df61SLi Peng 	else if (IS_I965G(dev))
8687c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
8697c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
8700a3e67a4SJesse Barnes 	else
8717c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
8727c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
873e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
8740a3e67a4SJesse Barnes 	return 0;
8750a3e67a4SJesse Barnes }
8760a3e67a4SJesse Barnes 
87742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
87842f52ef8SKeith Packard  * we use as a pipe index
87942f52ef8SKeith Packard  */
88042f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
8810a3e67a4SJesse Barnes {
8820a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
883e9d21d7fSKeith Packard 	unsigned long irqflags;
8840a3e67a4SJesse Barnes 
885e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
886c062df61SLi Peng 	if (IS_IRONLAKE(dev))
887c062df61SLi Peng 		ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
888c062df61SLi Peng 					     DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
889c062df61SLi Peng 	else
8907c463586SKeith Packard 		i915_disable_pipestat(dev_priv, pipe,
8917c463586SKeith Packard 				      PIPE_VBLANK_INTERRUPT_ENABLE |
8927c463586SKeith Packard 				      PIPE_START_VBLANK_INTERRUPT_ENABLE);
893e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
8940a3e67a4SJesse Barnes }
8950a3e67a4SJesse Barnes 
89679e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev)
89779e53945SJesse Barnes {
89879e53945SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
899e170b030SZhenyu Wang 
900f2b115e6SAdam Jackson 	if (!IS_IRONLAKE(dev))
90179e53945SJesse Barnes 		opregion_enable_asle(dev);
90279e53945SJesse Barnes 	dev_priv->irq_enabled = 1;
90379e53945SJesse Barnes }
90479e53945SJesse Barnes 
90579e53945SJesse Barnes 
906c0e09200SDave Airlie /* Set the vblank monitor pipe
907c0e09200SDave Airlie  */
908c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
909c0e09200SDave Airlie 			 struct drm_file *file_priv)
910c0e09200SDave Airlie {
911c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
912c0e09200SDave Airlie 
913c0e09200SDave Airlie 	if (!dev_priv) {
914c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
915c0e09200SDave Airlie 		return -EINVAL;
916c0e09200SDave Airlie 	}
917c0e09200SDave Airlie 
918c0e09200SDave Airlie 	return 0;
919c0e09200SDave Airlie }
920c0e09200SDave Airlie 
921c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
922c0e09200SDave Airlie 			 struct drm_file *file_priv)
923c0e09200SDave Airlie {
924c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
925c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
926c0e09200SDave Airlie 
927c0e09200SDave Airlie 	if (!dev_priv) {
928c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
929c0e09200SDave Airlie 		return -EINVAL;
930c0e09200SDave Airlie 	}
931c0e09200SDave Airlie 
9320a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
933c0e09200SDave Airlie 
934c0e09200SDave Airlie 	return 0;
935c0e09200SDave Airlie }
936c0e09200SDave Airlie 
937c0e09200SDave Airlie /**
938c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
939c0e09200SDave Airlie  */
940c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
941c0e09200SDave Airlie 		     struct drm_file *file_priv)
942c0e09200SDave Airlie {
943bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
944bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
945bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
946bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
947bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
948bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
949bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
950bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
951bd95e0a4SEric Anholt 	 *
952bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
953bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
954bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
955bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
9560a3e67a4SJesse Barnes 	 */
957c0e09200SDave Airlie 	return -EINVAL;
958c0e09200SDave Airlie }
959c0e09200SDave Airlie 
960f65d9421SBen Gamari struct drm_i915_gem_request *i915_get_tail_request(struct drm_device *dev) {
961f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
962f65d9421SBen Gamari 	return list_entry(dev_priv->mm.request_list.prev, struct drm_i915_gem_request, list);
963f65d9421SBen Gamari }
964f65d9421SBen Gamari 
965f65d9421SBen Gamari /**
966f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
967f65d9421SBen Gamari  * batchbuffers in a long time. The first time this is called we simply record
968f65d9421SBen Gamari  * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
969f65d9421SBen Gamari  * again, we assume the chip is wedged and try to fix it.
970f65d9421SBen Gamari  */
971f65d9421SBen Gamari void i915_hangcheck_elapsed(unsigned long data)
972f65d9421SBen Gamari {
973f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
974f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
975f65d9421SBen Gamari 	uint32_t acthd;
976f65d9421SBen Gamari 
977f65d9421SBen Gamari 	if (!IS_I965G(dev))
978f65d9421SBen Gamari 		acthd = I915_READ(ACTHD);
979f65d9421SBen Gamari 	else
980f65d9421SBen Gamari 		acthd = I915_READ(ACTHD_I965);
981f65d9421SBen Gamari 
982f65d9421SBen Gamari 	/* If all work is done then ACTHD clearly hasn't advanced. */
983f65d9421SBen Gamari 	if (list_empty(&dev_priv->mm.request_list) ||
984f65d9421SBen Gamari 		       i915_seqno_passed(i915_get_gem_seqno(dev), i915_get_tail_request(dev)->seqno)) {
985f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
986f65d9421SBen Gamari 		return;
987f65d9421SBen Gamari 	}
988f65d9421SBen Gamari 
989f65d9421SBen Gamari 	if (dev_priv->last_acthd == acthd && dev_priv->hangcheck_count > 0) {
990f65d9421SBen Gamari 		DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
991ba1234d1SBen Gamari 		i915_handle_error(dev, true);
992f65d9421SBen Gamari 		return;
993f65d9421SBen Gamari 	}
994f65d9421SBen Gamari 
995f65d9421SBen Gamari 	/* Reset timer case chip hangs without another request being added */
996f65d9421SBen Gamari 	mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
997f65d9421SBen Gamari 
998f65d9421SBen Gamari 	if (acthd != dev_priv->last_acthd)
999f65d9421SBen Gamari 		dev_priv->hangcheck_count = 0;
1000f65d9421SBen Gamari 	else
1001f65d9421SBen Gamari 		dev_priv->hangcheck_count++;
1002f65d9421SBen Gamari 
1003f65d9421SBen Gamari 	dev_priv->last_acthd = acthd;
1004f65d9421SBen Gamari }
1005f65d9421SBen Gamari 
1006c0e09200SDave Airlie /* drm_dma.h hooks
1007c0e09200SDave Airlie */
1008f2b115e6SAdam Jackson static void ironlake_irq_preinstall(struct drm_device *dev)
1009036a4a7dSZhenyu Wang {
1010036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1011036a4a7dSZhenyu Wang 
1012036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
1013036a4a7dSZhenyu Wang 
1014036a4a7dSZhenyu Wang 	/* XXX hotplug from PCH */
1015036a4a7dSZhenyu Wang 
1016036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1017036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1018036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
1019036a4a7dSZhenyu Wang 
1020036a4a7dSZhenyu Wang 	/* and GT */
1021036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1022036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1023036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
1024c650156aSZhenyu Wang 
1025c650156aSZhenyu Wang 	/* south display irq */
1026c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, 0xffffffff);
1027c650156aSZhenyu Wang 	I915_WRITE(SDEIER, 0x0);
1028c650156aSZhenyu Wang 	(void) I915_READ(SDEIER);
1029036a4a7dSZhenyu Wang }
1030036a4a7dSZhenyu Wang 
1031f2b115e6SAdam Jackson static int ironlake_irq_postinstall(struct drm_device *dev)
1032036a4a7dSZhenyu Wang {
1033036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1034036a4a7dSZhenyu Wang 	/* enable kind of interrupts always enabled */
1035*013d5aa2SJesse Barnes 	u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1036*013d5aa2SJesse Barnes 			   DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
1037036a4a7dSZhenyu Wang 	u32 render_mask = GT_USER_INTERRUPT;
1038c650156aSZhenyu Wang 	u32 hotplug_mask = SDE_CRT_HOTPLUG | SDE_PORTB_HOTPLUG |
1039c650156aSZhenyu Wang 			   SDE_PORTC_HOTPLUG | SDE_PORTD_HOTPLUG;
1040036a4a7dSZhenyu Wang 
1041036a4a7dSZhenyu Wang 	dev_priv->irq_mask_reg = ~display_mask;
1042643ced9bSLi Peng 	dev_priv->de_irq_enable_reg = display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK;
1043036a4a7dSZhenyu Wang 
1044036a4a7dSZhenyu Wang 	/* should always can generate irq */
1045036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1046036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
1047036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
1048036a4a7dSZhenyu Wang 	(void) I915_READ(DEIER);
1049036a4a7dSZhenyu Wang 
1050036a4a7dSZhenyu Wang 	/* user interrupt should be enabled, but masked initial */
1051036a4a7dSZhenyu Wang 	dev_priv->gt_irq_mask_reg = 0xffffffff;
1052036a4a7dSZhenyu Wang 	dev_priv->gt_irq_enable_reg = render_mask;
1053036a4a7dSZhenyu Wang 
1054036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1055036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
1056036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
1057036a4a7dSZhenyu Wang 	(void) I915_READ(GTIER);
1058036a4a7dSZhenyu Wang 
1059c650156aSZhenyu Wang 	dev_priv->pch_irq_mask_reg = ~hotplug_mask;
1060c650156aSZhenyu Wang 	dev_priv->pch_irq_enable_reg = hotplug_mask;
1061c650156aSZhenyu Wang 
1062c650156aSZhenyu Wang 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1063c650156aSZhenyu Wang 	I915_WRITE(SDEIMR, dev_priv->pch_irq_mask_reg);
1064c650156aSZhenyu Wang 	I915_WRITE(SDEIER, dev_priv->pch_irq_enable_reg);
1065c650156aSZhenyu Wang 	(void) I915_READ(SDEIER);
1066c650156aSZhenyu Wang 
1067036a4a7dSZhenyu Wang 	return 0;
1068036a4a7dSZhenyu Wang }
1069036a4a7dSZhenyu Wang 
1070c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
1071c0e09200SDave Airlie {
1072c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1073c0e09200SDave Airlie 
107479e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
107579e53945SJesse Barnes 
1076036a4a7dSZhenyu Wang 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
10778a905236SJesse Barnes 	INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1078036a4a7dSZhenyu Wang 
1079f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev)) {
1080f2b115e6SAdam Jackson 		ironlake_irq_preinstall(dev);
1081036a4a7dSZhenyu Wang 		return;
1082036a4a7dSZhenyu Wang 	}
1083036a4a7dSZhenyu Wang 
10845ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
10855ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
10865ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
10875ca58282SJesse Barnes 	}
10885ca58282SJesse Barnes 
10890a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
10907c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
10917c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
10920a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1093ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
10947c463586SKeith Packard 	(void) I915_READ(IER);
1095c0e09200SDave Airlie }
1096c0e09200SDave Airlie 
1097b01f2c3aSJesse Barnes /*
1098b01f2c3aSJesse Barnes  * Must be called after intel_modeset_init or hotplug interrupts won't be
1099b01f2c3aSJesse Barnes  * enabled correctly.
1100b01f2c3aSJesse Barnes  */
11010a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
1102c0e09200SDave Airlie {
1103c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
11045ca58282SJesse Barnes 	u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
110563eeaf38SJesse Barnes 	u32 error_mask;
11060a3e67a4SJesse Barnes 
1107036a4a7dSZhenyu Wang 	DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
1108036a4a7dSZhenyu Wang 
11090a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1110ed4cb414SEric Anholt 
1111f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev))
1112f2b115e6SAdam Jackson 		return ironlake_irq_postinstall(dev);
1113036a4a7dSZhenyu Wang 
11147c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
11157c463586SKeith Packard 	dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
11168ee1c3dbSMatthew Garrett 
11177c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
11187c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
11197c463586SKeith Packard 
11205ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
11215ca58282SJesse Barnes 		u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
11225ca58282SJesse Barnes 
1123b01f2c3aSJesse Barnes 		/* Note HDMI and DP share bits */
1124b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1125b01f2c3aSJesse Barnes 			hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1126b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1127b01f2c3aSJesse Barnes 			hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1128b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1129b01f2c3aSJesse Barnes 			hotplug_en |= HDMID_HOTPLUG_INT_EN;
1130b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1131b01f2c3aSJesse Barnes 			hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1132b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1133b01f2c3aSJesse Barnes 			hotplug_en |= SDVOB_HOTPLUG_INT_EN;
1134b01f2c3aSJesse Barnes 		if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS)
1135b01f2c3aSJesse Barnes 			hotplug_en |= CRT_HOTPLUG_INT_EN;
1136b01f2c3aSJesse Barnes 		/* Ignore TV since it's buggy */
1137b01f2c3aSJesse Barnes 
11385ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
11395ca58282SJesse Barnes 
11405ca58282SJesse Barnes 		/* Enable in IER... */
11415ca58282SJesse Barnes 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
11425ca58282SJesse Barnes 		/* and unmask in IMR */
11435ca58282SJesse Barnes 		i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
11445ca58282SJesse Barnes 	}
11455ca58282SJesse Barnes 
114663eeaf38SJesse Barnes 	/*
114763eeaf38SJesse Barnes 	 * Enable some error detection, note the instruction error mask
114863eeaf38SJesse Barnes 	 * bit is reserved, so we leave it masked.
114963eeaf38SJesse Barnes 	 */
115063eeaf38SJesse Barnes 	if (IS_G4X(dev)) {
115163eeaf38SJesse Barnes 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
115263eeaf38SJesse Barnes 			       GM45_ERROR_MEM_PRIV |
115363eeaf38SJesse Barnes 			       GM45_ERROR_CP_PRIV |
115463eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
115563eeaf38SJesse Barnes 	} else {
115663eeaf38SJesse Barnes 		error_mask = ~(I915_ERROR_PAGE_TABLE |
115763eeaf38SJesse Barnes 			       I915_ERROR_MEMORY_REFRESH);
115863eeaf38SJesse Barnes 	}
115963eeaf38SJesse Barnes 	I915_WRITE(EMR, error_mask);
116063eeaf38SJesse Barnes 
11617c463586SKeith Packard 	/* Disable pipe interrupt enables, clear pending pipe status */
11627c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
11637c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
11647c463586SKeith Packard 	/* Clear pending interrupt status */
11657c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
11667c463586SKeith Packard 
11675ca58282SJesse Barnes 	I915_WRITE(IER, enable_mask);
11687c463586SKeith Packard 	I915_WRITE(IMR, dev_priv->irq_mask_reg);
1169ed4cb414SEric Anholt 	(void) I915_READ(IER);
1170ed4cb414SEric Anholt 
11718ee1c3dbSMatthew Garrett 	opregion_enable_asle(dev);
11720a3e67a4SJesse Barnes 
11730a3e67a4SJesse Barnes 	return 0;
1174c0e09200SDave Airlie }
1175c0e09200SDave Airlie 
1176f2b115e6SAdam Jackson static void ironlake_irq_uninstall(struct drm_device *dev)
1177036a4a7dSZhenyu Wang {
1178036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1179036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
1180036a4a7dSZhenyu Wang 
1181036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
1182036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
1183036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
1184036a4a7dSZhenyu Wang 
1185036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
1186036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
1187036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
1188036a4a7dSZhenyu Wang }
1189036a4a7dSZhenyu Wang 
1190c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
1191c0e09200SDave Airlie {
1192c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1193c0e09200SDave Airlie 
1194c0e09200SDave Airlie 	if (!dev_priv)
1195c0e09200SDave Airlie 		return;
1196c0e09200SDave Airlie 
11970a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
11980a3e67a4SJesse Barnes 
1199f2b115e6SAdam Jackson 	if (IS_IRONLAKE(dev)) {
1200f2b115e6SAdam Jackson 		ironlake_irq_uninstall(dev);
1201036a4a7dSZhenyu Wang 		return;
1202036a4a7dSZhenyu Wang 	}
1203036a4a7dSZhenyu Wang 
12045ca58282SJesse Barnes 	if (I915_HAS_HOTPLUG(dev)) {
12055ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_EN, 0);
12065ca58282SJesse Barnes 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
12075ca58282SJesse Barnes 	}
12085ca58282SJesse Barnes 
12090a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
12107c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
12117c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
12120a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
1213ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
1214c0e09200SDave Airlie 
12157c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
12167c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
12177c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
1218c0e09200SDave Airlie }
1219