blob: 1417660a93ec00a0a8a24cc797acc7b7754db063 [file] [log] [blame]
Dave Airlie0d6aa602006-01-02 20:14:23 +11001/* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 */
Dave Airlie0d6aa602006-01-02 20:14:23 +11003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
Dave Airliebc54fd12005-06-23 22:46:46 +10006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
Dave Airlie0d6aa602006-01-02 20:14:23 +110027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Joe Perchesa70491c2012-03-18 13:00:11 -070029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Jesse Barnes63eeaf32009-06-18 16:56:52 -070031#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "drmP.h"
34#include "drm.h"
35#include "i915_drm.h"
36#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010037#include "i915_trace.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Zhenyu Wang036a4a72009-06-08 14:40:19 +080040/* For display hotplug interrupt */
Chris Wilson995b6762010-08-20 13:23:26 +010041static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050042ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080043{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000044 if ((dev_priv->irq_mask & mask) != 0) {
45 dev_priv->irq_mask &= ~mask;
46 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000047 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080048 }
49}
50
51static inline void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050052ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080053{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000054 if ((dev_priv->irq_mask & mask) != mask) {
55 dev_priv->irq_mask |= mask;
56 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000057 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080058 }
59}
60
Keith Packard7c463582008-11-04 02:03:27 -080061void
62i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
63{
64 if ((dev_priv->pipestat[pipe] & mask) != mask) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -080065 u32 reg = PIPESTAT(pipe);
Keith Packard7c463582008-11-04 02:03:27 -080066
67 dev_priv->pipestat[pipe] |= mask;
68 /* Enable the interrupt, clear any pending status */
69 I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
Chris Wilson3143a2b2010-11-16 15:55:10 +000070 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -080071 }
72}
73
74void
75i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
76{
77 if ((dev_priv->pipestat[pipe] & mask) != 0) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -080078 u32 reg = PIPESTAT(pipe);
Keith Packard7c463582008-11-04 02:03:27 -080079
80 dev_priv->pipestat[pipe] &= ~mask;
81 I915_WRITE(reg, dev_priv->pipestat[pipe]);
Chris Wilson3143a2b2010-11-16 15:55:10 +000082 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -080083 }
84}
85
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100086/**
Zhao Yakui01c66882009-10-28 05:10:00 +000087 * intel_enable_asle - enable ASLE interrupt for OpRegion
88 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +000089void intel_enable_asle(struct drm_device *dev)
Zhao Yakui01c66882009-10-28 05:10:00 +000090{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000091 drm_i915_private_t *dev_priv = dev->dev_private;
92 unsigned long irqflags;
93
Jesse Barnes7e231dbe2012-03-28 13:39:38 -070094 /* FIXME: opregion/asle for VLV */
95 if (IS_VALLEYVIEW(dev))
96 return;
97
Chris Wilson1ec14ad2010-12-04 11:30:53 +000098 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +000099
Eric Anholtc619eed2010-01-28 16:45:52 -0800100 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500101 ironlake_enable_display_irq(dev_priv, DE_GSE);
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800102 else {
Zhao Yakui01c66882009-10-28 05:10:00 +0000103 i915_enable_pipestat(dev_priv, 1,
Jesse Barnesd874bcf2010-06-30 13:16:00 -0700104 PIPE_LEGACY_BLC_EVENT_ENABLE);
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100105 if (INTEL_INFO(dev)->gen >= 4)
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800106 i915_enable_pipestat(dev_priv, 0,
Jesse Barnesd874bcf2010-06-30 13:16:00 -0700107 PIPE_LEGACY_BLC_EVENT_ENABLE);
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800108 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000109
110 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000111}
112
113/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700114 * i915_pipe_enabled - check if a pipe is enabled
115 * @dev: DRM device
116 * @pipe: pipe to check
117 *
118 * Reading certain registers when the pipe is disabled can hang the chip.
119 * Use this routine to make sure the PLL is running and the pipe is active
120 * before reading such registers if unsure.
121 */
122static int
123i915_pipe_enabled(struct drm_device *dev, int pipe)
124{
125 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson5eddb702010-09-11 13:48:45 +0100126 return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700127}
128
Keith Packard42f52ef2008-10-18 19:39:29 -0700129/* Called from drm generic code, passed a 'crtc', which
130 * we use as a pipe index
131 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700132static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700133{
134 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
135 unsigned long high_frame;
136 unsigned long low_frame;
Chris Wilson5eddb702010-09-11 13:48:45 +0100137 u32 high1, high2, low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700138
139 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800140 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800141 "pipe %c\n", pipe_name(pipe));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700142 return 0;
143 }
144
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800145 high_frame = PIPEFRAME(pipe);
146 low_frame = PIPEFRAMEPIXEL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +0100147
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700148 /*
149 * High & low register fields aren't synchronized, so make sure
150 * we get a low value that's stable across two reads of the high
151 * register.
152 */
153 do {
Chris Wilson5eddb702010-09-11 13:48:45 +0100154 high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
155 low = I915_READ(low_frame) & PIPE_FRAME_LOW_MASK;
156 high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700157 } while (high1 != high2);
158
Chris Wilson5eddb702010-09-11 13:48:45 +0100159 high1 >>= PIPE_FRAME_HIGH_SHIFT;
160 low >>= PIPE_FRAME_LOW_SHIFT;
161 return (high1 << 8) | low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700162}
163
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700164static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800165{
166 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800167 int reg = PIPE_FRMCOUNT_GM45(pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800168
169 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800170 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800171 "pipe %c\n", pipe_name(pipe));
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800172 return 0;
173 }
174
175 return I915_READ(reg);
176}
177
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700178static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100179 int *vpos, int *hpos)
180{
181 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
182 u32 vbl = 0, position = 0;
183 int vbl_start, vbl_end, htotal, vtotal;
184 bool in_vbl = true;
185 int ret = 0;
186
187 if (!i915_pipe_enabled(dev, pipe)) {
188 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800189 "pipe %c\n", pipe_name(pipe));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100190 return 0;
191 }
192
193 /* Get vtotal. */
194 vtotal = 1 + ((I915_READ(VTOTAL(pipe)) >> 16) & 0x1fff);
195
196 if (INTEL_INFO(dev)->gen >= 4) {
197 /* No obvious pixelcount register. Only query vertical
198 * scanout position from Display scan line register.
199 */
200 position = I915_READ(PIPEDSL(pipe));
201
202 /* Decode into vertical scanout position. Don't have
203 * horizontal scanout position.
204 */
205 *vpos = position & 0x1fff;
206 *hpos = 0;
207 } else {
208 /* Have access to pixelcount since start of frame.
209 * We can split this into vertical and horizontal
210 * scanout position.
211 */
212 position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
213
214 htotal = 1 + ((I915_READ(HTOTAL(pipe)) >> 16) & 0x1fff);
215 *vpos = position / htotal;
216 *hpos = position - (*vpos * htotal);
217 }
218
219 /* Query vblank area. */
220 vbl = I915_READ(VBLANK(pipe));
221
222 /* Test position against vblank region. */
223 vbl_start = vbl & 0x1fff;
224 vbl_end = (vbl >> 16) & 0x1fff;
225
226 if ((*vpos < vbl_start) || (*vpos > vbl_end))
227 in_vbl = false;
228
229 /* Inside "upper part" of vblank area? Apply corrective offset: */
230 if (in_vbl && (*vpos >= vbl_start))
231 *vpos = *vpos - vtotal;
232
233 /* Readouts valid? */
234 if (vbl > 0)
235 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
236
237 /* In vblank? */
238 if (in_vbl)
239 ret |= DRM_SCANOUTPOS_INVBL;
240
241 return ret;
242}
243
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700244static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100245 int *max_error,
246 struct timeval *vblank_time,
247 unsigned flags)
248{
Chris Wilson4041b852011-01-22 10:07:56 +0000249 struct drm_i915_private *dev_priv = dev->dev_private;
250 struct drm_crtc *crtc;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100251
Chris Wilson4041b852011-01-22 10:07:56 +0000252 if (pipe < 0 || pipe >= dev_priv->num_pipe) {
253 DRM_ERROR("Invalid crtc %d\n", pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100254 return -EINVAL;
255 }
256
257 /* Get drm_crtc to timestamp: */
Chris Wilson4041b852011-01-22 10:07:56 +0000258 crtc = intel_get_crtc_for_pipe(dev, pipe);
259 if (crtc == NULL) {
260 DRM_ERROR("Invalid crtc %d\n", pipe);
261 return -EINVAL;
262 }
263
264 if (!crtc->enabled) {
265 DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
266 return -EBUSY;
267 }
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100268
269 /* Helper routine in DRM core does all the work: */
Chris Wilson4041b852011-01-22 10:07:56 +0000270 return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
271 vblank_time, flags,
272 crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100273}
274
Jesse Barnes5ca58282009-03-31 14:11:15 -0700275/*
276 * Handle hotplug events outside the interrupt handler proper.
277 */
278static void i915_hotplug_work_func(struct work_struct *work)
279{
280 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
281 hotplug_work);
282 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700283 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100284 struct intel_encoder *encoder;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700285
Keith Packarda65e34c2011-07-25 10:04:56 -0700286 mutex_lock(&mode_config->mutex);
Jesse Barnese67189ab2011-02-11 14:44:51 -0800287 DRM_DEBUG_KMS("running encoder hotplug functions\n");
288
Chris Wilson4ef69c72010-09-09 15:14:28 +0100289 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
290 if (encoder->hot_plug)
291 encoder->hot_plug(encoder);
292
Keith Packard40ee3382011-07-28 15:31:19 -0700293 mutex_unlock(&mode_config->mutex);
294
Jesse Barnes5ca58282009-03-31 14:11:15 -0700295 /* Just fire off a uevent and let userspace tell us what to do */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000296 drm_helper_hpd_irq_event(dev);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700297}
298
Jesse Barnesf97108d2010-01-29 11:27:07 -0800299static void i915_handle_rps_change(struct drm_device *dev)
300{
301 drm_i915_private_t *dev_priv = dev->dev_private;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000302 u32 busy_up, busy_down, max_avg, min_avg;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800303 u8 new_delay = dev_priv->cur_delay;
304
Jesse Barnes7648fa92010-05-20 14:28:11 -0700305 I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000306 busy_up = I915_READ(RCPREVBSYTUPAVG);
307 busy_down = I915_READ(RCPREVBSYTDNAVG);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800308 max_avg = I915_READ(RCBMAXAVG);
309 min_avg = I915_READ(RCBMINAVG);
310
311 /* Handle RCS change request from hw */
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000312 if (busy_up > max_avg) {
Jesse Barnesf97108d2010-01-29 11:27:07 -0800313 if (dev_priv->cur_delay != dev_priv->max_delay)
314 new_delay = dev_priv->cur_delay - 1;
315 if (new_delay < dev_priv->max_delay)
316 new_delay = dev_priv->max_delay;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000317 } else if (busy_down < min_avg) {
Jesse Barnesf97108d2010-01-29 11:27:07 -0800318 if (dev_priv->cur_delay != dev_priv->min_delay)
319 new_delay = dev_priv->cur_delay + 1;
320 if (new_delay > dev_priv->min_delay)
321 new_delay = dev_priv->min_delay;
322 }
323
Jesse Barnes7648fa92010-05-20 14:28:11 -0700324 if (ironlake_set_drps(dev, new_delay))
325 dev_priv->cur_delay = new_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800326
327 return;
328}
329
Chris Wilson549f7362010-10-19 11:19:32 +0100330static void notify_ring(struct drm_device *dev,
331 struct intel_ring_buffer *ring)
332{
333 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9862e602011-01-04 22:22:17 +0000334
Chris Wilson475553d2011-01-20 09:52:56 +0000335 if (ring->obj == NULL)
336 return;
337
Chris Wilson6d171cb2012-04-28 09:00:03 +0100338 trace_i915_gem_request_complete(ring, ring->get_seqno(ring));
Chris Wilson9862e602011-01-04 22:22:17 +0000339
Chris Wilson549f7362010-10-19 11:19:32 +0100340 wake_up_all(&ring->irq_queue);
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -0700341 if (i915_enable_hangcheck) {
342 dev_priv->hangcheck_count = 0;
343 mod_timer(&dev_priv->hangcheck_timer,
344 jiffies +
345 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
346 }
Chris Wilson549f7362010-10-19 11:19:32 +0100347}
348
Ben Widawsky4912d042011-04-25 11:25:20 -0700349static void gen6_pm_rps_work(struct work_struct *work)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800350{
Ben Widawsky4912d042011-04-25 11:25:20 -0700351 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
352 rps_work);
Ben Widawsky4912d042011-04-25 11:25:20 -0700353 u32 pm_iir, pm_imr;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100354 u8 new_delay;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800355
Ben Widawsky4912d042011-04-25 11:25:20 -0700356 spin_lock_irq(&dev_priv->rps_lock);
357 pm_iir = dev_priv->pm_iir;
358 dev_priv->pm_iir = 0;
359 pm_imr = I915_READ(GEN6_PMIMR);
Daniel Vettera9e26412011-09-08 14:00:21 +0200360 I915_WRITE(GEN6_PMIMR, 0);
Ben Widawsky4912d042011-04-25 11:25:20 -0700361 spin_unlock_irq(&dev_priv->rps_lock);
362
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100363 if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800364 return;
365
Ben Widawsky4912d042011-04-25 11:25:20 -0700366 mutex_lock(&dev_priv->dev->struct_mutex);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100367
368 if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
369 new_delay = dev_priv->cur_delay + 1;
370 else
371 new_delay = dev_priv->cur_delay - 1;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800372
Ben Widawsky4912d042011-04-25 11:25:20 -0700373 gen6_set_rps(dev_priv->dev, new_delay);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800374
Ben Widawsky4912d042011-04-25 11:25:20 -0700375 mutex_unlock(&dev_priv->dev->struct_mutex);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800376}
377
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200378static void snb_gt_irq_handler(struct drm_device *dev,
379 struct drm_i915_private *dev_priv,
380 u32 gt_iir)
381{
382
383 if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
384 GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
385 notify_ring(dev, &dev_priv->ring[RCS]);
386 if (gt_iir & GEN6_BSD_USER_INTERRUPT)
387 notify_ring(dev, &dev_priv->ring[VCS]);
388 if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
389 notify_ring(dev, &dev_priv->ring[BCS]);
390
391 if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
392 GT_GEN6_BSD_CS_ERROR_INTERRUPT |
393 GT_RENDER_CS_ERROR_INTERRUPT)) {
394 DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
395 i915_handle_error(dev, false);
396 }
397}
398
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100399static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
400 u32 pm_iir)
401{
402 unsigned long flags;
403
404 /*
405 * IIR bits should never already be set because IMR should
406 * prevent an interrupt from being shown in IIR. The warning
407 * displays a case where we've unsafely cleared
408 * dev_priv->pm_iir. Although missing an interrupt of the same
409 * type is not a problem, it displays a problem in the logic.
410 *
411 * The mask bit in IMR is cleared by rps_work.
412 */
413
414 spin_lock_irqsave(&dev_priv->rps_lock, flags);
415 WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
416 dev_priv->pm_iir |= pm_iir;
417 I915_WRITE(GEN6_PMIMR, dev_priv->pm_iir);
418 POSTING_READ(GEN6_PMIMR);
419 spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
420
421 queue_work(dev_priv->wq, &dev_priv->rps_work);
422}
423
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700424static irqreturn_t valleyview_irq_handler(DRM_IRQ_ARGS)
425{
426 struct drm_device *dev = (struct drm_device *) arg;
427 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
428 u32 iir, gt_iir, pm_iir;
429 irqreturn_t ret = IRQ_NONE;
430 unsigned long irqflags;
431 int pipe;
432 u32 pipe_stats[I915_MAX_PIPES];
433 u32 vblank_status;
434 int vblank = 0;
435 bool blc_event;
436
437 atomic_inc(&dev_priv->irq_received);
438
439 vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS |
440 PIPE_VBLANK_INTERRUPT_STATUS;
441
442 while (true) {
443 iir = I915_READ(VLV_IIR);
444 gt_iir = I915_READ(GTIIR);
445 pm_iir = I915_READ(GEN6_PMIIR);
446
447 if (gt_iir == 0 && pm_iir == 0 && iir == 0)
448 goto out;
449
450 ret = IRQ_HANDLED;
451
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200452 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700453
454 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
455 for_each_pipe(pipe) {
456 int reg = PIPESTAT(pipe);
457 pipe_stats[pipe] = I915_READ(reg);
458
459 /*
460 * Clear the PIPE*STAT regs before the IIR
461 */
462 if (pipe_stats[pipe] & 0x8000ffff) {
463 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
464 DRM_DEBUG_DRIVER("pipe %c underrun\n",
465 pipe_name(pipe));
466 I915_WRITE(reg, pipe_stats[pipe]);
467 }
468 }
469 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
470
471 /* Consume port. Then clear IIR or we'll miss events */
472 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
473 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
474
475 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
476 hotplug_status);
477 if (hotplug_status & dev_priv->hotplug_supported_mask)
478 queue_work(dev_priv->wq,
479 &dev_priv->hotplug_work);
480
481 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
482 I915_READ(PORT_HOTPLUG_STAT);
483 }
484
485
486 if (iir & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT) {
487 drm_handle_vblank(dev, 0);
488 vblank++;
Chris Wilsone0f608d2012-04-24 22:59:43 +0100489 intel_finish_page_flip(dev, 0);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700490 }
491
492 if (iir & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT) {
493 drm_handle_vblank(dev, 1);
494 vblank++;
Chris Wilsone0f608d2012-04-24 22:59:43 +0100495 intel_finish_page_flip(dev, 0);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700496 }
497
498 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
499 blc_event = true;
500
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100501 if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
502 gen6_queue_rps_work(dev_priv, pm_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700503
504 I915_WRITE(GTIIR, gt_iir);
505 I915_WRITE(GEN6_PMIIR, pm_iir);
506 I915_WRITE(VLV_IIR, iir);
507 }
508
509out:
510 return ret;
511}
512
Chris Wilson9adab8b2012-05-09 21:45:43 +0100513static void pch_irq_handler(struct drm_device *dev, u32 pch_iir)
Jesse Barnes776ad802011-01-04 15:09:39 -0800514{
515 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800516 int pipe;
Jesse Barnes776ad802011-01-04 15:09:39 -0800517
Jesse Barnes776ad802011-01-04 15:09:39 -0800518 if (pch_iir & SDE_AUDIO_POWER_MASK)
519 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
520 (pch_iir & SDE_AUDIO_POWER_MASK) >>
521 SDE_AUDIO_POWER_SHIFT);
522
523 if (pch_iir & SDE_GMBUS)
524 DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
525
526 if (pch_iir & SDE_AUDIO_HDCP_MASK)
527 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
528
529 if (pch_iir & SDE_AUDIO_TRANS_MASK)
530 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
531
532 if (pch_iir & SDE_POISON)
533 DRM_ERROR("PCH poison interrupt\n");
534
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800535 if (pch_iir & SDE_FDI_MASK)
536 for_each_pipe(pipe)
537 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
538 pipe_name(pipe),
539 I915_READ(FDI_RX_IIR(pipe)));
Jesse Barnes776ad802011-01-04 15:09:39 -0800540
541 if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
542 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
543
544 if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
545 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
546
547 if (pch_iir & SDE_TRANSB_FIFO_UNDER)
548 DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
549 if (pch_iir & SDE_TRANSA_FIFO_UNDER)
550 DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
551}
552
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700553static irqreturn_t ivybridge_irq_handler(DRM_IRQ_ARGS)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700554{
555 struct drm_device *dev = (struct drm_device *) arg;
556 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson0e434062012-05-09 21:45:44 +0100557 u32 de_iir, gt_iir, de_ier, pm_iir;
558 irqreturn_t ret = IRQ_NONE;
559 int i;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700560
561 atomic_inc(&dev_priv->irq_received);
562
563 /* disable master interrupt before clearing iir */
564 de_ier = I915_READ(DEIER);
565 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Chris Wilson0e434062012-05-09 21:45:44 +0100566
567 gt_iir = I915_READ(GTIIR);
568 if (gt_iir) {
569 snb_gt_irq_handler(dev, dev_priv, gt_iir);
570 I915_WRITE(GTIIR, gt_iir);
571 ret = IRQ_HANDLED;
572 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700573
574 de_iir = I915_READ(DEIIR);
Chris Wilson0e434062012-05-09 21:45:44 +0100575 if (de_iir) {
576 if (de_iir & DE_GSE_IVB)
577 intel_opregion_gse_intr(dev);
578
579 for (i = 0; i < 3; i++) {
580 if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
581 intel_prepare_page_flip(dev, i);
582 intel_finish_page_flip_plane(dev, i);
583 }
584 if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
585 drm_handle_vblank(dev, i);
586 }
587
588 /* check event from PCH */
589 if (de_iir & DE_PCH_EVENT_IVB) {
590 u32 pch_iir = I915_READ(SDEIIR);
591
592 if (pch_iir & SDE_HOTPLUG_MASK_CPT)
593 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
594 pch_irq_handler(dev, pch_iir);
595
596 /* clear PCH hotplug event before clear CPU irq */
597 I915_WRITE(SDEIIR, pch_iir);
598 }
599
600 I915_WRITE(DEIIR, de_iir);
601 ret = IRQ_HANDLED;
602 }
603
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700604 pm_iir = I915_READ(GEN6_PMIIR);
Chris Wilson0e434062012-05-09 21:45:44 +0100605 if (pm_iir) {
606 if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
607 gen6_queue_rps_work(dev_priv, pm_iir);
608 I915_WRITE(GEN6_PMIIR, pm_iir);
609 ret = IRQ_HANDLED;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700610 }
611
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700612 I915_WRITE(DEIER, de_ier);
613 POSTING_READ(DEIER);
614
615 return ret;
616}
617
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200618static void ilk_gt_irq_handler(struct drm_device *dev,
619 struct drm_i915_private *dev_priv,
620 u32 gt_iir)
621{
622 if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
623 notify_ring(dev, &dev_priv->ring[RCS]);
624 if (gt_iir & GT_BSD_USER_INTERRUPT)
625 notify_ring(dev, &dev_priv->ring[VCS]);
626}
627
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700628static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800629{
Jesse Barnes46979952011-04-07 13:53:55 -0700630 struct drm_device *dev = (struct drm_device *) arg;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800631 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
632 int ret = IRQ_NONE;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800633 u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
Yuanhan Liu2d7b8362010-10-08 10:21:06 +0100634 u32 hotplug_mask;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100635
Jesse Barnes46979952011-04-07 13:53:55 -0700636 atomic_inc(&dev_priv->irq_received);
637
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000638 /* disable master interrupt before clearing iir */
639 de_ier = I915_READ(DEIER);
640 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000641 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000642
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800643 de_iir = I915_READ(DEIIR);
644 gt_iir = I915_READ(GTIIR);
Zhenyu Wangc6501562009-11-03 18:57:21 +0000645 pch_iir = I915_READ(SDEIIR);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800646 pm_iir = I915_READ(GEN6_PMIIR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800647
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800648 if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
649 (!IS_GEN6(dev) || pm_iir == 0))
Zou Nan haic7c85102010-01-15 10:29:06 +0800650 goto done;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800651
Yuanhan Liu2d7b8362010-10-08 10:21:06 +0100652 if (HAS_PCH_CPT(dev))
653 hotplug_mask = SDE_HOTPLUG_MASK_CPT;
654 else
655 hotplug_mask = SDE_HOTPLUG_MASK;
656
Zou Nan haic7c85102010-01-15 10:29:06 +0800657 ret = IRQ_HANDLED;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800658
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200659 if (IS_GEN5(dev))
660 ilk_gt_irq_handler(dev, dev_priv, gt_iir);
661 else
662 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Zou Nan haic7c85102010-01-15 10:29:06 +0800663
664 if (de_iir & DE_GSE)
Chris Wilson3b617962010-08-24 09:02:58 +0100665 intel_opregion_gse_intr(dev);
Zou Nan haic7c85102010-01-15 10:29:06 +0800666
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800667 if (de_iir & DE_PLANEA_FLIP_DONE) {
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800668 intel_prepare_page_flip(dev, 0);
Chris Wilson2bbda382010-09-02 17:59:39 +0100669 intel_finish_page_flip_plane(dev, 0);
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800670 }
671
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800672 if (de_iir & DE_PLANEB_FLIP_DONE) {
673 intel_prepare_page_flip(dev, 1);
Chris Wilson2bbda382010-09-02 17:59:39 +0100674 intel_finish_page_flip_plane(dev, 1);
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800675 }
Li Pengc062df62010-01-23 00:12:58 +0800676
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800677 if (de_iir & DE_PIPEA_VBLANK)
678 drm_handle_vblank(dev, 0);
679
680 if (de_iir & DE_PIPEB_VBLANK)
681 drm_handle_vblank(dev, 1);
682
Zou Nan haic7c85102010-01-15 10:29:06 +0800683 /* check event from PCH */
Jesse Barnes776ad802011-01-04 15:09:39 -0800684 if (de_iir & DE_PCH_EVENT) {
685 if (pch_iir & hotplug_mask)
686 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
Chris Wilson9adab8b2012-05-09 21:45:43 +0100687 pch_irq_handler(dev, pch_iir);
Jesse Barnes776ad802011-01-04 15:09:39 -0800688 }
Zou Nan haic7c85102010-01-15 10:29:06 +0800689
Jesse Barnesf97108d2010-01-29 11:27:07 -0800690 if (de_iir & DE_PCU_EVENT) {
Jesse Barnes7648fa92010-05-20 14:28:11 -0700691 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
Jesse Barnesf97108d2010-01-29 11:27:07 -0800692 i915_handle_rps_change(dev);
693 }
694
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100695 if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
696 gen6_queue_rps_work(dev_priv, pm_iir);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800697
Zou Nan haic7c85102010-01-15 10:29:06 +0800698 /* should clear PCH hotplug event before clear CPU irq */
699 I915_WRITE(SDEIIR, pch_iir);
700 I915_WRITE(GTIIR, gt_iir);
701 I915_WRITE(DEIIR, de_iir);
Ben Widawsky4912d042011-04-25 11:25:20 -0700702 I915_WRITE(GEN6_PMIIR, pm_iir);
Zou Nan haic7c85102010-01-15 10:29:06 +0800703
704done:
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000705 I915_WRITE(DEIER, de_ier);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000706 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000707
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800708 return ret;
709}
710
Jesse Barnes8a905232009-07-11 16:48:03 -0400711/**
712 * i915_error_work_func - do process context error handling work
713 * @work: work struct
714 *
715 * Fire an error uevent so userspace can see that a hang or error
716 * was detected.
717 */
718static void i915_error_work_func(struct work_struct *work)
719{
720 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
721 error_work);
722 struct drm_device *dev = dev_priv->dev;
Ben Gamarif316a422009-09-14 17:48:46 -0400723 char *error_event[] = { "ERROR=1", NULL };
724 char *reset_event[] = { "RESET=1", NULL };
725 char *reset_done_event[] = { "ERROR=0", NULL };
Jesse Barnes8a905232009-07-11 16:48:03 -0400726
Ben Gamarif316a422009-09-14 17:48:46 -0400727 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -0400728
Ben Gamariba1234d2009-09-14 17:48:47 -0400729 if (atomic_read(&dev_priv->mm.wedged)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +0100730 DRM_DEBUG_DRIVER("resetting chip\n");
731 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
Daniel Vetterd4b8bb22012-04-27 15:17:44 +0200732 if (!i915_reset(dev)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +0100733 atomic_set(&dev_priv->mm.wedged, 0);
734 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
Ben Gamarif316a422009-09-14 17:48:46 -0400735 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100736 complete_all(&dev_priv->error_completion);
Ben Gamarif316a422009-09-14 17:48:46 -0400737 }
Jesse Barnes8a905232009-07-11 16:48:03 -0400738}
739
Chris Wilson3bd3c932010-08-19 08:19:30 +0100740#ifdef CONFIG_DEBUG_FS
Chris Wilson9df30792010-02-18 10:24:56 +0000741static struct drm_i915_error_object *
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000742i915_error_object_create(struct drm_i915_private *dev_priv,
Chris Wilson05394f32010-11-08 19:18:58 +0000743 struct drm_i915_gem_object *src)
Chris Wilson9df30792010-02-18 10:24:56 +0000744{
745 struct drm_i915_error_object *dst;
Chris Wilson9df30792010-02-18 10:24:56 +0000746 int page, page_count;
Chris Wilsone56660d2010-08-07 11:01:26 +0100747 u32 reloc_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000748
Chris Wilson05394f32010-11-08 19:18:58 +0000749 if (src == NULL || src->pages == NULL)
Chris Wilson9df30792010-02-18 10:24:56 +0000750 return NULL;
751
Chris Wilson05394f32010-11-08 19:18:58 +0000752 page_count = src->base.size / PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +0000753
Akshay Joshi0206e352011-08-16 15:34:10 -0400754 dst = kmalloc(sizeof(*dst) + page_count * sizeof(u32 *), GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +0000755 if (dst == NULL)
756 return NULL;
757
Chris Wilson05394f32010-11-08 19:18:58 +0000758 reloc_offset = src->gtt_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000759 for (page = 0; page < page_count; page++) {
Andrew Morton788885a2010-05-11 14:07:05 -0700760 unsigned long flags;
Chris Wilsone56660d2010-08-07 11:01:26 +0100761 void *d;
Andrew Morton788885a2010-05-11 14:07:05 -0700762
Chris Wilsone56660d2010-08-07 11:01:26 +0100763 d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +0000764 if (d == NULL)
765 goto unwind;
Chris Wilsone56660d2010-08-07 11:01:26 +0100766
Andrew Morton788885a2010-05-11 14:07:05 -0700767 local_irq_save(flags);
Daniel Vetter74898d72012-02-15 23:50:22 +0100768 if (reloc_offset < dev_priv->mm.gtt_mappable_end &&
769 src->has_global_gtt_mapping) {
Chris Wilson172975aa2011-12-14 13:57:25 +0100770 void __iomem *s;
771
772 /* Simply ignore tiling or any overlapping fence.
773 * It's part of the error state, and this hopefully
774 * captures what the GPU read.
775 */
776
777 s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
778 reloc_offset);
779 memcpy_fromio(d, s, PAGE_SIZE);
780 io_mapping_unmap_atomic(s);
781 } else {
782 void *s;
783
784 drm_clflush_pages(&src->pages[page], 1);
785
786 s = kmap_atomic(src->pages[page]);
787 memcpy(d, s, PAGE_SIZE);
788 kunmap_atomic(s);
789
790 drm_clflush_pages(&src->pages[page], 1);
791 }
Andrew Morton788885a2010-05-11 14:07:05 -0700792 local_irq_restore(flags);
Chris Wilsone56660d2010-08-07 11:01:26 +0100793
Chris Wilson9df30792010-02-18 10:24:56 +0000794 dst->pages[page] = d;
Chris Wilsone56660d2010-08-07 11:01:26 +0100795
796 reloc_offset += PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +0000797 }
798 dst->page_count = page_count;
Chris Wilson05394f32010-11-08 19:18:58 +0000799 dst->gtt_offset = src->gtt_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000800
801 return dst;
802
803unwind:
804 while (page--)
805 kfree(dst->pages[page]);
806 kfree(dst);
807 return NULL;
808}
809
810static void
811i915_error_object_free(struct drm_i915_error_object *obj)
812{
813 int page;
814
815 if (obj == NULL)
816 return;
817
818 for (page = 0; page < obj->page_count; page++)
819 kfree(obj->pages[page]);
820
821 kfree(obj);
822}
823
Daniel Vetter742cbee2012-04-27 15:17:39 +0200824void
825i915_error_state_free(struct kref *error_ref)
Chris Wilson9df30792010-02-18 10:24:56 +0000826{
Daniel Vetter742cbee2012-04-27 15:17:39 +0200827 struct drm_i915_error_state *error = container_of(error_ref,
828 typeof(*error), ref);
Chris Wilsone2f973d2011-01-27 19:15:11 +0000829 int i;
830
Chris Wilson52d39a22012-02-15 11:25:37 +0000831 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
832 i915_error_object_free(error->ring[i].batchbuffer);
833 i915_error_object_free(error->ring[i].ringbuffer);
834 kfree(error->ring[i].requests);
835 }
Chris Wilsone2f973d2011-01-27 19:15:11 +0000836
Chris Wilson9df30792010-02-18 10:24:56 +0000837 kfree(error->active_bo);
Chris Wilson6ef3d422010-08-04 20:26:07 +0100838 kfree(error->overlay);
Chris Wilson9df30792010-02-18 10:24:56 +0000839 kfree(error);
840}
Chris Wilson1b502472012-04-24 15:47:30 +0100841static void capture_bo(struct drm_i915_error_buffer *err,
842 struct drm_i915_gem_object *obj)
843{
844 err->size = obj->base.size;
845 err->name = obj->base.name;
846 err->seqno = obj->last_rendering_seqno;
847 err->gtt_offset = obj->gtt_offset;
848 err->read_domains = obj->base.read_domains;
849 err->write_domain = obj->base.write_domain;
850 err->fence_reg = obj->fence_reg;
851 err->pinned = 0;
852 if (obj->pin_count > 0)
853 err->pinned = 1;
854 if (obj->user_pin_count > 0)
855 err->pinned = -1;
856 err->tiling = obj->tiling_mode;
857 err->dirty = obj->dirty;
858 err->purgeable = obj->madv != I915_MADV_WILLNEED;
859 err->ring = obj->ring ? obj->ring->id : -1;
860 err->cache_level = obj->cache_level;
861}
Chris Wilson9df30792010-02-18 10:24:56 +0000862
Chris Wilson1b502472012-04-24 15:47:30 +0100863static u32 capture_active_bo(struct drm_i915_error_buffer *err,
864 int count, struct list_head *head)
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000865{
866 struct drm_i915_gem_object *obj;
867 int i = 0;
868
869 list_for_each_entry(obj, head, mm_list) {
Chris Wilson1b502472012-04-24 15:47:30 +0100870 capture_bo(err++, obj);
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000871 if (++i == count)
872 break;
Chris Wilson1b502472012-04-24 15:47:30 +0100873 }
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000874
Chris Wilson1b502472012-04-24 15:47:30 +0100875 return i;
876}
877
878static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
879 int count, struct list_head *head)
880{
881 struct drm_i915_gem_object *obj;
882 int i = 0;
883
884 list_for_each_entry(obj, head, gtt_list) {
885 if (obj->pin_count == 0)
886 continue;
887
888 capture_bo(err++, obj);
889 if (++i == count)
890 break;
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000891 }
892
893 return i;
894}
895
Chris Wilson748ebc62010-10-24 10:28:47 +0100896static void i915_gem_record_fences(struct drm_device *dev,
897 struct drm_i915_error_state *error)
898{
899 struct drm_i915_private *dev_priv = dev->dev_private;
900 int i;
901
902 /* Fences */
903 switch (INTEL_INFO(dev)->gen) {
Daniel Vetter775d17b2011-10-09 21:52:01 +0200904 case 7:
Chris Wilson748ebc62010-10-24 10:28:47 +0100905 case 6:
906 for (i = 0; i < 16; i++)
907 error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
908 break;
909 case 5:
910 case 4:
911 for (i = 0; i < 16; i++)
912 error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
913 break;
914 case 3:
915 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
916 for (i = 0; i < 8; i++)
917 error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
918 case 2:
919 for (i = 0; i < 8; i++)
920 error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
921 break;
922
923 }
924}
925
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000926static struct drm_i915_error_object *
927i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
928 struct intel_ring_buffer *ring)
929{
930 struct drm_i915_gem_object *obj;
931 u32 seqno;
932
933 if (!ring->get_seqno)
934 return NULL;
935
936 seqno = ring->get_seqno(ring);
937 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
938 if (obj->ring != ring)
939 continue;
940
Chris Wilsonc37d9a52011-01-12 20:33:01 +0000941 if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000942 continue;
943
944 if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
945 continue;
946
947 /* We need to copy these to an anonymous buffer as the simplest
948 * method to avoid being overwritten by userspace.
949 */
950 return i915_error_object_create(dev_priv, obj);
951 }
952
953 return NULL;
954}
955
Daniel Vetterd27b1e02011-12-14 13:57:01 +0100956static void i915_record_ring_state(struct drm_device *dev,
957 struct drm_i915_error_state *error,
958 struct intel_ring_buffer *ring)
959{
960 struct drm_i915_private *dev_priv = dev->dev_private;
961
Daniel Vetter33f3f512011-12-14 13:57:39 +0100962 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter33f3f512011-12-14 13:57:39 +0100963 error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
Daniel Vetter7e3b8732012-02-01 22:26:45 +0100964 error->semaphore_mboxes[ring->id][0]
965 = I915_READ(RING_SYNC_0(ring->mmio_base));
966 error->semaphore_mboxes[ring->id][1]
967 = I915_READ(RING_SYNC_1(ring->mmio_base));
Daniel Vetter33f3f512011-12-14 13:57:39 +0100968 }
Daniel Vetterc1cd90e2011-12-14 13:57:02 +0100969
Daniel Vetterd27b1e02011-12-14 13:57:01 +0100970 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter9d2f41f2012-04-02 21:41:45 +0200971 error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
Daniel Vetterd27b1e02011-12-14 13:57:01 +0100972 error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
973 error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
974 error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
Daniel Vetterc1cd90e2011-12-14 13:57:02 +0100975 error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
Daniel Vetterd27b1e02011-12-14 13:57:01 +0100976 if (ring->id == RCS) {
Daniel Vetterd27b1e02011-12-14 13:57:01 +0100977 error->instdone1 = I915_READ(INSTDONE1);
978 error->bbaddr = I915_READ64(BB_ADDR);
979 }
980 } else {
Daniel Vetter9d2f41f2012-04-02 21:41:45 +0200981 error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
Daniel Vetterd27b1e02011-12-14 13:57:01 +0100982 error->ipeir[ring->id] = I915_READ(IPEIR);
983 error->ipehr[ring->id] = I915_READ(IPEHR);
984 error->instdone[ring->id] = I915_READ(INSTDONE);
Daniel Vetterd27b1e02011-12-14 13:57:01 +0100985 }
986
Ben Widawsky9574b3f2012-04-26 16:03:01 -0700987 error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
Daniel Vetterc1cd90e2011-12-14 13:57:02 +0100988 error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
Daniel Vetterd27b1e02011-12-14 13:57:01 +0100989 error->seqno[ring->id] = ring->get_seqno(ring);
990 error->acthd[ring->id] = intel_ring_get_active_head(ring);
Daniel Vetterc1cd90e2011-12-14 13:57:02 +0100991 error->head[ring->id] = I915_READ_HEAD(ring);
992 error->tail[ring->id] = I915_READ_TAIL(ring);
Daniel Vetter7e3b8732012-02-01 22:26:45 +0100993
994 error->cpu_ring_head[ring->id] = ring->head;
995 error->cpu_ring_tail[ring->id] = ring->tail;
Daniel Vetterd27b1e02011-12-14 13:57:01 +0100996}
997
Chris Wilson52d39a22012-02-15 11:25:37 +0000998static void i915_gem_record_rings(struct drm_device *dev,
999 struct drm_i915_error_state *error)
1000{
1001 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01001002 struct intel_ring_buffer *ring;
Chris Wilson52d39a22012-02-15 11:25:37 +00001003 struct drm_i915_gem_request *request;
1004 int i, count;
1005
Chris Wilsonb4519512012-05-11 14:29:30 +01001006 for_each_ring(ring, dev_priv, i) {
Chris Wilson52d39a22012-02-15 11:25:37 +00001007 i915_record_ring_state(dev, error, ring);
1008
1009 error->ring[i].batchbuffer =
1010 i915_error_first_batchbuffer(dev_priv, ring);
1011
1012 error->ring[i].ringbuffer =
1013 i915_error_object_create(dev_priv, ring->obj);
1014
1015 count = 0;
1016 list_for_each_entry(request, &ring->request_list, list)
1017 count++;
1018
1019 error->ring[i].num_requests = count;
1020 error->ring[i].requests =
1021 kmalloc(count*sizeof(struct drm_i915_error_request),
1022 GFP_ATOMIC);
1023 if (error->ring[i].requests == NULL) {
1024 error->ring[i].num_requests = 0;
1025 continue;
1026 }
1027
1028 count = 0;
1029 list_for_each_entry(request, &ring->request_list, list) {
1030 struct drm_i915_error_request *erq;
1031
1032 erq = &error->ring[i].requests[count++];
1033 erq->seqno = request->seqno;
1034 erq->jiffies = request->emitted_jiffies;
Chris Wilsonee4f42b2012-02-15 11:25:38 +00001035 erq->tail = request->tail;
Chris Wilson52d39a22012-02-15 11:25:37 +00001036 }
1037 }
1038}
1039
Jesse Barnes8a905232009-07-11 16:48:03 -04001040/**
1041 * i915_capture_error_state - capture an error record for later analysis
1042 * @dev: drm device
1043 *
1044 * Should be called when an error is detected (either a hang or an error
1045 * interrupt) to capture error state from the time of the error. Fills
1046 * out a structure which becomes available in debugfs for user level tools
1047 * to pick up.
1048 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001049static void i915_capture_error_state(struct drm_device *dev)
1050{
1051 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001052 struct drm_i915_gem_object *obj;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001053 struct drm_i915_error_state *error;
1054 unsigned long flags;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001055 int i, pipe;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001056
1057 spin_lock_irqsave(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001058 error = dev_priv->first_error;
1059 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
1060 if (error)
1061 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001062
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001063 /* Account for pipe specific data like PIPE*STAT */
Daniel Vetter33f3f512011-12-14 13:57:39 +01001064 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001065 if (!error) {
Chris Wilson9df30792010-02-18 10:24:56 +00001066 DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
1067 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001068 }
1069
Chris Wilsonb6f78332011-02-01 14:15:55 +00001070 DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
1071 dev->primary->index);
Chris Wilson2fa772f2010-10-01 13:23:27 +01001072
Daniel Vetter742cbee2012-04-27 15:17:39 +02001073 kref_init(&error->ref);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001074 error->eir = I915_READ(EIR);
1075 error->pgtbl_er = I915_READ(PGTBL_ER);
Ben Widawskybe998e22012-04-26 16:03:00 -07001076
1077 if (HAS_PCH_SPLIT(dev))
1078 error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1079 else if (IS_VALLEYVIEW(dev))
1080 error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1081 else if (IS_GEN2(dev))
1082 error->ier = I915_READ16(IER);
1083 else
1084 error->ier = I915_READ(IER);
1085
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001086 for_each_pipe(pipe)
1087 error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001088
Daniel Vetter33f3f512011-12-14 13:57:39 +01001089 if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilsonf4068392010-10-27 20:36:41 +01001090 error->error = I915_READ(ERROR_GEN6);
Daniel Vetter33f3f512011-12-14 13:57:39 +01001091 error->done_reg = I915_READ(DONE_REG);
1092 }
Chris Wilsonadd354d2010-10-29 19:00:51 +01001093
Chris Wilson748ebc62010-10-24 10:28:47 +01001094 i915_gem_record_fences(dev, error);
Chris Wilson52d39a22012-02-15 11:25:37 +00001095 i915_gem_record_rings(dev, error);
Chris Wilson9df30792010-02-18 10:24:56 +00001096
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001097 /* Record buffers on the active and pinned lists. */
Chris Wilson9df30792010-02-18 10:24:56 +00001098 error->active_bo = NULL;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001099 error->pinned_bo = NULL;
Chris Wilson9df30792010-02-18 10:24:56 +00001100
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001101 i = 0;
1102 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1103 i++;
1104 error->active_bo_count = i;
Chris Wilson1b502472012-04-24 15:47:30 +01001105 list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list)
1106 if (obj->pin_count)
1107 i++;
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001108 error->pinned_bo_count = i - error->active_bo_count;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001109
Chris Wilson8e934db2011-01-24 12:34:00 +00001110 error->active_bo = NULL;
1111 error->pinned_bo = NULL;
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001112 if (i) {
1113 error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
Chris Wilson9df30792010-02-18 10:24:56 +00001114 GFP_ATOMIC);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001115 if (error->active_bo)
1116 error->pinned_bo =
1117 error->active_bo + error->active_bo_count;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001118 }
1119
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001120 if (error->active_bo)
1121 error->active_bo_count =
Chris Wilson1b502472012-04-24 15:47:30 +01001122 capture_active_bo(error->active_bo,
1123 error->active_bo_count,
1124 &dev_priv->mm.active_list);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001125
1126 if (error->pinned_bo)
1127 error->pinned_bo_count =
Chris Wilson1b502472012-04-24 15:47:30 +01001128 capture_pinned_bo(error->pinned_bo,
1129 error->pinned_bo_count,
1130 &dev_priv->mm.gtt_list);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001131
Jesse Barnes8a905232009-07-11 16:48:03 -04001132 do_gettimeofday(&error->time);
1133
Chris Wilson6ef3d422010-08-04 20:26:07 +01001134 error->overlay = intel_overlay_capture_error_state(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00001135 error->display = intel_display_capture_error_state(dev);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001136
Chris Wilson9df30792010-02-18 10:24:56 +00001137 spin_lock_irqsave(&dev_priv->error_lock, flags);
1138 if (dev_priv->first_error == NULL) {
1139 dev_priv->first_error = error;
1140 error = NULL;
1141 }
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001142 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001143
1144 if (error)
Daniel Vetter742cbee2012-04-27 15:17:39 +02001145 i915_error_state_free(&error->ref);
Chris Wilson9df30792010-02-18 10:24:56 +00001146}
1147
1148void i915_destroy_error_state(struct drm_device *dev)
1149{
1150 struct drm_i915_private *dev_priv = dev->dev_private;
1151 struct drm_i915_error_state *error;
Ben Widawsky6dc0e812012-01-23 15:30:02 -08001152 unsigned long flags;
Chris Wilson9df30792010-02-18 10:24:56 +00001153
Ben Widawsky6dc0e812012-01-23 15:30:02 -08001154 spin_lock_irqsave(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001155 error = dev_priv->first_error;
1156 dev_priv->first_error = NULL;
Ben Widawsky6dc0e812012-01-23 15:30:02 -08001157 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001158
1159 if (error)
Daniel Vetter742cbee2012-04-27 15:17:39 +02001160 kref_put(&error->ref, i915_error_state_free);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001161}
Chris Wilson3bd3c932010-08-19 08:19:30 +01001162#else
1163#define i915_capture_error_state(x)
1164#endif
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001165
Chris Wilson35aed2e2010-05-27 13:18:12 +01001166static void i915_report_and_clear_eir(struct drm_device *dev)
Jesse Barnes8a905232009-07-11 16:48:03 -04001167{
1168 struct drm_i915_private *dev_priv = dev->dev_private;
1169 u32 eir = I915_READ(EIR);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001170 int pipe;
Jesse Barnes8a905232009-07-11 16:48:03 -04001171
Chris Wilson35aed2e2010-05-27 13:18:12 +01001172 if (!eir)
1173 return;
Jesse Barnes8a905232009-07-11 16:48:03 -04001174
Joe Perchesa70491c2012-03-18 13:00:11 -07001175 pr_err("render error detected, EIR: 0x%08x\n", eir);
Jesse Barnes8a905232009-07-11 16:48:03 -04001176
1177 if (IS_G4X(dev)) {
1178 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
1179 u32 ipeir = I915_READ(IPEIR_I965);
1180
Joe Perchesa70491c2012-03-18 13:00:11 -07001181 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1182 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1183 pr_err(" INSTDONE: 0x%08x\n",
Jesse Barnes8a905232009-07-11 16:48:03 -04001184 I915_READ(INSTDONE_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07001185 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
1186 pr_err(" INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1187 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04001188 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001189 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001190 }
1191 if (eir & GM45_ERROR_PAGE_TABLE) {
1192 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07001193 pr_err("page table error\n");
1194 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04001195 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001196 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04001197 }
1198 }
1199
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001200 if (!IS_GEN2(dev)) {
Jesse Barnes8a905232009-07-11 16:48:03 -04001201 if (eir & I915_ERROR_PAGE_TABLE) {
1202 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07001203 pr_err("page table error\n");
1204 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04001205 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001206 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04001207 }
1208 }
1209
1210 if (eir & I915_ERROR_MEMORY_REFRESH) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001211 pr_err("memory refresh error:\n");
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001212 for_each_pipe(pipe)
Joe Perchesa70491c2012-03-18 13:00:11 -07001213 pr_err("pipe %c stat: 0x%08x\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001214 pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
Jesse Barnes8a905232009-07-11 16:48:03 -04001215 /* pipestat has already been acked */
1216 }
1217 if (eir & I915_ERROR_INSTRUCTION) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001218 pr_err("instruction error\n");
1219 pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001220 if (INTEL_INFO(dev)->gen < 4) {
Jesse Barnes8a905232009-07-11 16:48:03 -04001221 u32 ipeir = I915_READ(IPEIR);
1222
Joe Perchesa70491c2012-03-18 13:00:11 -07001223 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
1224 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
1225 pr_err(" INSTDONE: 0x%08x\n", I915_READ(INSTDONE));
1226 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
Jesse Barnes8a905232009-07-11 16:48:03 -04001227 I915_WRITE(IPEIR, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001228 POSTING_READ(IPEIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001229 } else {
1230 u32 ipeir = I915_READ(IPEIR_I965);
1231
Joe Perchesa70491c2012-03-18 13:00:11 -07001232 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1233 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1234 pr_err(" INSTDONE: 0x%08x\n",
Jesse Barnes8a905232009-07-11 16:48:03 -04001235 I915_READ(INSTDONE_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07001236 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
1237 pr_err(" INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1238 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04001239 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001240 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001241 }
1242 }
1243
1244 I915_WRITE(EIR, eir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001245 POSTING_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001246 eir = I915_READ(EIR);
1247 if (eir) {
1248 /*
1249 * some errors might have become stuck,
1250 * mask them.
1251 */
1252 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
1253 I915_WRITE(EMR, I915_READ(EMR) | eir);
1254 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
1255 }
Chris Wilson35aed2e2010-05-27 13:18:12 +01001256}
1257
1258/**
1259 * i915_handle_error - handle an error interrupt
1260 * @dev: drm device
1261 *
1262 * Do some basic checking of regsiter state at error interrupt time and
1263 * dump it to the syslog. Also call i915_capture_error_state() to make
1264 * sure we get a record and make it available in debugfs. Fire a uevent
1265 * so userspace knows something bad happened (should trigger collection
1266 * of a ring dump etc.).
1267 */
Chris Wilson527f9e92010-11-11 01:16:58 +00001268void i915_handle_error(struct drm_device *dev, bool wedged)
Chris Wilson35aed2e2010-05-27 13:18:12 +01001269{
1270 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01001271 struct intel_ring_buffer *ring;
1272 int i;
Chris Wilson35aed2e2010-05-27 13:18:12 +01001273
1274 i915_capture_error_state(dev);
1275 i915_report_and_clear_eir(dev);
Jesse Barnes8a905232009-07-11 16:48:03 -04001276
Ben Gamariba1234d2009-09-14 17:48:47 -04001277 if (wedged) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001278 INIT_COMPLETION(dev_priv->error_completion);
Ben Gamariba1234d2009-09-14 17:48:47 -04001279 atomic_set(&dev_priv->mm.wedged, 1);
1280
Ben Gamari11ed50e2009-09-14 17:48:45 -04001281 /*
1282 * Wakeup waiting processes so they don't hang
1283 */
Chris Wilsonb4519512012-05-11 14:29:30 +01001284 for_each_ring(ring, dev_priv, i)
1285 wake_up_all(&ring->irq_queue);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001286 }
1287
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001288 queue_work(dev_priv->wq, &dev_priv->error_work);
Jesse Barnes8a905232009-07-11 16:48:03 -04001289}
1290
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001291static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
1292{
1293 drm_i915_private_t *dev_priv = dev->dev_private;
1294 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00001296 struct drm_i915_gem_object *obj;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001297 struct intel_unpin_work *work;
1298 unsigned long flags;
1299 bool stall_detected;
1300
1301 /* Ignore early vblank irqs */
1302 if (intel_crtc == NULL)
1303 return;
1304
1305 spin_lock_irqsave(&dev->event_lock, flags);
1306 work = intel_crtc->unpin_work;
1307
1308 if (work == NULL || work->pending || !work->enable_stall_check) {
1309 /* Either the pending flip IRQ arrived, or we're too early. Don't check */
1310 spin_unlock_irqrestore(&dev->event_lock, flags);
1311 return;
1312 }
1313
1314 /* Potential stall - if we see that the flip has happened, assume a missed interrupt */
Chris Wilson05394f32010-11-08 19:18:58 +00001315 obj = work->pending_flip_obj;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001316 if (INTEL_INFO(dev)->gen >= 4) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001317 int dspsurf = DSPSURF(intel_crtc->plane);
Armin Reese446f2542012-03-30 16:20:16 -07001318 stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1319 obj->gtt_offset;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001320 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001321 int dspaddr = DSPADDR(intel_crtc->plane);
Chris Wilson05394f32010-11-08 19:18:58 +00001322 stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001323 crtc->y * crtc->fb->pitches[0] +
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001324 crtc->x * crtc->fb->bits_per_pixel/8);
1325 }
1326
1327 spin_unlock_irqrestore(&dev->event_lock, flags);
1328
1329 if (stall_detected) {
1330 DRM_DEBUG_DRIVER("Pageflip stall detected\n");
1331 intel_prepare_page_flip(dev, intel_crtc->plane);
1332 }
1333}
1334
Keith Packard42f52ef2008-10-18 19:39:29 -07001335/* Called from drm generic code, passed 'crtc' which
1336 * we use as a pipe index
1337 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001338static int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001339{
1340 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001341 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001342
Chris Wilson5eddb702010-09-11 13:48:45 +01001343 if (!i915_pipe_enabled(dev, pipe))
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001344 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001345
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001346 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07001347 if (INTEL_INFO(dev)->gen >= 4)
Keith Packard7c463582008-11-04 02:03:27 -08001348 i915_enable_pipestat(dev_priv, pipe,
1349 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -07001350 else
Keith Packard7c463582008-11-04 02:03:27 -08001351 i915_enable_pipestat(dev_priv, pipe,
1352 PIPE_VBLANK_INTERRUPT_ENABLE);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001353
1354 /* maintain vblank delivery even in deep C-states */
1355 if (dev_priv->info->gen == 3)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001356 I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001357 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001358
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001359 return 0;
1360}
1361
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001362static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07001363{
1364 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1365 unsigned long irqflags;
1366
1367 if (!i915_pipe_enabled(dev, pipe))
1368 return -EINVAL;
1369
1370 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1371 ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
Akshay Joshi0206e352011-08-16 15:34:10 -04001372 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
Jesse Barnesf796cf82011-04-07 13:58:17 -07001373 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1374
1375 return 0;
1376}
1377
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001378static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001379{
1380 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1381 unsigned long irqflags;
1382
1383 if (!i915_pipe_enabled(dev, pipe))
1384 return -EINVAL;
1385
1386 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilsonb615b572012-05-02 09:52:12 +01001387 ironlake_enable_display_irq(dev_priv,
1388 DE_PIPEA_VBLANK_IVB << (5 * pipe));
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001389 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1390
1391 return 0;
1392}
1393
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001394static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
1395{
1396 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1397 unsigned long irqflags;
1398 u32 dpfl, imr;
1399
1400 if (!i915_pipe_enabled(dev, pipe))
1401 return -EINVAL;
1402
1403 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1404 dpfl = I915_READ(VLV_DPFLIPSTAT);
1405 imr = I915_READ(VLV_IMR);
1406 if (pipe == 0) {
1407 dpfl |= PIPEA_VBLANK_INT_EN;
1408 imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
1409 } else {
1410 dpfl |= PIPEA_VBLANK_INT_EN;
1411 imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
1412 }
1413 I915_WRITE(VLV_DPFLIPSTAT, dpfl);
1414 I915_WRITE(VLV_IMR, imr);
1415 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1416
1417 return 0;
1418}
1419
Keith Packard42f52ef2008-10-18 19:39:29 -07001420/* Called from drm generic code, passed 'crtc' which
1421 * we use as a pipe index
1422 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001423static void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001424{
1425 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001426 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001427
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001428 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001429 if (dev_priv->info->gen == 3)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001430 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
Chris Wilson8692d00e2011-02-05 10:08:21 +00001431
Jesse Barnesf796cf82011-04-07 13:58:17 -07001432 i915_disable_pipestat(dev_priv, pipe,
1433 PIPE_VBLANK_INTERRUPT_ENABLE |
1434 PIPE_START_VBLANK_INTERRUPT_ENABLE);
1435 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1436}
1437
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001438static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07001439{
1440 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1441 unsigned long irqflags;
1442
1443 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1444 ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
Akshay Joshi0206e352011-08-16 15:34:10 -04001445 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001446 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001447}
1448
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001449static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001450{
1451 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1452 unsigned long irqflags;
1453
1454 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilsonb615b572012-05-02 09:52:12 +01001455 ironlake_disable_display_irq(dev_priv,
1456 DE_PIPEA_VBLANK_IVB << (pipe * 5));
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001457 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1458}
1459
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001460static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
1461{
1462 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1463 unsigned long irqflags;
1464 u32 dpfl, imr;
1465
1466 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1467 dpfl = I915_READ(VLV_DPFLIPSTAT);
1468 imr = I915_READ(VLV_IMR);
1469 if (pipe == 0) {
1470 dpfl &= ~PIPEA_VBLANK_INT_EN;
1471 imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
1472 } else {
1473 dpfl &= ~PIPEB_VBLANK_INT_EN;
1474 imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
1475 }
1476 I915_WRITE(VLV_IMR, imr);
1477 I915_WRITE(VLV_DPFLIPSTAT, dpfl);
1478 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1479}
1480
Chris Wilson893eead2010-10-27 14:44:35 +01001481static u32
1482ring_last_seqno(struct intel_ring_buffer *ring)
Zou Nan hai852835f2010-05-21 09:08:56 +08001483{
Chris Wilson893eead2010-10-27 14:44:35 +01001484 return list_entry(ring->request_list.prev,
1485 struct drm_i915_gem_request, list)->seqno;
1486}
1487
1488static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1489{
1490 if (list_empty(&ring->request_list) ||
1491 i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1492 /* Issue a wake-up to catch stuck h/w. */
Ben Widawsky9574b3f2012-04-26 16:03:01 -07001493 if (waitqueue_active(&ring->irq_queue)) {
1494 DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
1495 ring->name);
Chris Wilson893eead2010-10-27 14:44:35 +01001496 wake_up_all(&ring->irq_queue);
1497 *err = true;
1498 }
1499 return true;
1500 }
1501 return false;
Ben Gamarif65d9422009-09-14 17:48:44 -04001502}
1503
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001504static bool kick_ring(struct intel_ring_buffer *ring)
1505{
1506 struct drm_device *dev = ring->dev;
1507 struct drm_i915_private *dev_priv = dev->dev_private;
1508 u32 tmp = I915_READ_CTL(ring);
1509 if (tmp & RING_WAIT) {
1510 DRM_ERROR("Kicking stuck wait on %s\n",
1511 ring->name);
1512 I915_WRITE_CTL(ring, tmp);
1513 return true;
1514 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001515 return false;
1516}
1517
Chris Wilsond1e61e72012-04-10 17:00:41 +01001518static bool i915_hangcheck_hung(struct drm_device *dev)
1519{
1520 drm_i915_private_t *dev_priv = dev->dev_private;
1521
1522 if (dev_priv->hangcheck_count++ > 1) {
Chris Wilsonb4519512012-05-11 14:29:30 +01001523 bool hung = true;
1524
Chris Wilsond1e61e72012-04-10 17:00:41 +01001525 DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1526 i915_handle_error(dev, true);
1527
1528 if (!IS_GEN2(dev)) {
Chris Wilsonb4519512012-05-11 14:29:30 +01001529 struct intel_ring_buffer *ring;
1530 int i;
1531
Chris Wilsond1e61e72012-04-10 17:00:41 +01001532 /* Is the chip hanging on a WAIT_FOR_EVENT?
1533 * If so we can simply poke the RB_WAIT bit
1534 * and break the hang. This should work on
1535 * all but the second generation chipsets.
1536 */
Chris Wilsonb4519512012-05-11 14:29:30 +01001537 for_each_ring(ring, dev_priv, i)
1538 hung &= !kick_ring(ring);
Chris Wilsond1e61e72012-04-10 17:00:41 +01001539 }
1540
Chris Wilsonb4519512012-05-11 14:29:30 +01001541 return hung;
Chris Wilsond1e61e72012-04-10 17:00:41 +01001542 }
1543
1544 return false;
1545}
1546
Ben Gamarif65d9422009-09-14 17:48:44 -04001547/**
1548 * This is called when the chip hasn't reported back with completed
1549 * batchbuffers in a long time. The first time this is called we simply record
1550 * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1551 * again, we assume the chip is wedged and try to fix it.
1552 */
1553void i915_hangcheck_elapsed(unsigned long data)
1554{
1555 struct drm_device *dev = (struct drm_device *)data;
1556 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01001557 uint32_t acthd[I915_NUM_RINGS], instdone, instdone1;
1558 struct intel_ring_buffer *ring;
1559 bool err = false, idle;
1560 int i;
Chris Wilson893eead2010-10-27 14:44:35 +01001561
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001562 if (!i915_enable_hangcheck)
1563 return;
1564
Chris Wilsonb4519512012-05-11 14:29:30 +01001565 memset(acthd, 0, sizeof(acthd));
1566 idle = true;
1567 for_each_ring(ring, dev_priv, i) {
1568 idle &= i915_hangcheck_ring_idle(ring, &err);
1569 acthd[i] = intel_ring_get_active_head(ring);
1570 }
1571
Chris Wilson893eead2010-10-27 14:44:35 +01001572 /* If all work is done then ACTHD clearly hasn't advanced. */
Chris Wilsonb4519512012-05-11 14:29:30 +01001573 if (idle) {
Chris Wilsond1e61e72012-04-10 17:00:41 +01001574 if (err) {
1575 if (i915_hangcheck_hung(dev))
1576 return;
1577
Chris Wilson893eead2010-10-27 14:44:35 +01001578 goto repeat;
Chris Wilsond1e61e72012-04-10 17:00:41 +01001579 }
1580
1581 dev_priv->hangcheck_count = 0;
Chris Wilson893eead2010-10-27 14:44:35 +01001582 return;
1583 }
Eric Anholtb9201c12010-01-08 14:25:16 -08001584
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001585 if (INTEL_INFO(dev)->gen < 4) {
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001586 instdone = I915_READ(INSTDONE);
1587 instdone1 = 0;
1588 } else {
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001589 instdone = I915_READ(INSTDONE_I965);
1590 instdone1 = I915_READ(INSTDONE1);
1591 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001592
Chris Wilsonb4519512012-05-11 14:29:30 +01001593 if (memcmp(dev_priv->last_acthd, acthd, sizeof(acthd)) == 0 &&
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001594 dev_priv->last_instdone == instdone &&
1595 dev_priv->last_instdone1 == instdone1) {
Chris Wilsond1e61e72012-04-10 17:00:41 +01001596 if (i915_hangcheck_hung(dev))
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001597 return;
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001598 } else {
1599 dev_priv->hangcheck_count = 0;
1600
Chris Wilsonb4519512012-05-11 14:29:30 +01001601 memcpy(dev_priv->last_acthd, acthd, sizeof(acthd));
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001602 dev_priv->last_instdone = instdone;
1603 dev_priv->last_instdone1 = instdone1;
1604 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001605
Chris Wilson893eead2010-10-27 14:44:35 +01001606repeat:
Ben Gamarif65d9422009-09-14 17:48:44 -04001607 /* Reset timer case chip hangs without another request being added */
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001608 mod_timer(&dev_priv->hangcheck_timer,
1609 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612/* drm_dma.h hooks
1613*/
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001614static void ironlake_irq_preinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001615{
1616 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1617
Jesse Barnes46979952011-04-07 13:53:55 -07001618 atomic_set(&dev_priv->irq_received, 0);
1619
Jesse Barnes46979952011-04-07 13:53:55 -07001620
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001621 I915_WRITE(HWSTAM, 0xeffe);
Daniel Vetterbdfcdb62012-01-05 01:05:26 +01001622
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001623 /* XXX hotplug from PCH */
1624
1625 I915_WRITE(DEIMR, 0xffffffff);
1626 I915_WRITE(DEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001627 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001628
1629 /* and GT */
1630 I915_WRITE(GTIMR, 0xffffffff);
1631 I915_WRITE(GTIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001632 POSTING_READ(GTIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00001633
1634 /* south display irq */
1635 I915_WRITE(SDEIMR, 0xffffffff);
1636 I915_WRITE(SDEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001637 POSTING_READ(SDEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001638}
1639
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001640static void valleyview_irq_preinstall(struct drm_device *dev)
1641{
1642 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1643 int pipe;
1644
1645 atomic_set(&dev_priv->irq_received, 0);
1646
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001647 /* VLV magic */
1648 I915_WRITE(VLV_IMR, 0);
1649 I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
1650 I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
1651 I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
1652
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001653 /* and GT */
1654 I915_WRITE(GTIIR, I915_READ(GTIIR));
1655 I915_WRITE(GTIIR, I915_READ(GTIIR));
1656 I915_WRITE(GTIMR, 0xffffffff);
1657 I915_WRITE(GTIER, 0x0);
1658 POSTING_READ(GTIER);
1659
1660 I915_WRITE(DPINVGTT, 0xff);
1661
1662 I915_WRITE(PORT_HOTPLUG_EN, 0);
1663 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1664 for_each_pipe(pipe)
1665 I915_WRITE(PIPESTAT(pipe), 0xffff);
1666 I915_WRITE(VLV_IIR, 0xffffffff);
1667 I915_WRITE(VLV_IMR, 0xffffffff);
1668 I915_WRITE(VLV_IER, 0x0);
1669 POSTING_READ(VLV_IER);
1670}
1671
Keith Packard7fe0b972011-09-19 13:31:02 -07001672/*
1673 * Enable digital hotplug on the PCH, and configure the DP short pulse
1674 * duration to 2ms (which is the minimum in the Display Port spec)
1675 *
1676 * This register is the same on all known PCH chips.
1677 */
1678
1679static void ironlake_enable_pch_hotplug(struct drm_device *dev)
1680{
1681 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1682 u32 hotplug;
1683
1684 hotplug = I915_READ(PCH_PORT_HOTPLUG);
1685 hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
1686 hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
1687 hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
1688 hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
1689 I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
1690}
1691
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001692static int ironlake_irq_postinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001693{
1694 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1695 /* enable kind of interrupts always enabled */
Jesse Barnes013d5aa2010-01-29 11:18:31 -08001696 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1697 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001698 u32 render_irqs;
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01001699 u32 hotplug_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001700
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001701 dev_priv->irq_mask = ~display_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001702
1703 /* should always can generate irq */
1704 I915_WRITE(DEIIR, I915_READ(DEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001705 I915_WRITE(DEIMR, dev_priv->irq_mask);
1706 I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001707 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001708
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001709 dev_priv->gt_irq_mask = ~0;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001710
1711 I915_WRITE(GTIIR, I915_READ(GTIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001712 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001713
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001714 if (IS_GEN6(dev))
1715 render_irqs =
1716 GT_USER_INTERRUPT |
Ben Widawskye2a1e2f2012-03-29 19:11:26 -07001717 GEN6_BSD_USER_INTERRUPT |
1718 GEN6_BLITTER_USER_INTERRUPT;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001719 else
1720 render_irqs =
Chris Wilson88f23b82010-12-05 15:08:31 +00001721 GT_USER_INTERRUPT |
Chris Wilsonc6df5412010-12-15 09:56:50 +00001722 GT_PIPE_NOTIFY |
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001723 GT_BSD_USER_INTERRUPT;
1724 I915_WRITE(GTIER, render_irqs);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001725 POSTING_READ(GTIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001726
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01001727 if (HAS_PCH_CPT(dev)) {
Chris Wilson9035a972011-02-16 09:36:05 +00001728 hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
1729 SDE_PORTB_HOTPLUG_CPT |
1730 SDE_PORTC_HOTPLUG_CPT |
1731 SDE_PORTD_HOTPLUG_CPT);
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01001732 } else {
Chris Wilson9035a972011-02-16 09:36:05 +00001733 hotplug_mask = (SDE_CRT_HOTPLUG |
1734 SDE_PORTB_HOTPLUG |
1735 SDE_PORTC_HOTPLUG |
1736 SDE_PORTD_HOTPLUG |
1737 SDE_AUX_MASK);
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01001738 }
1739
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001740 dev_priv->pch_irq_mask = ~hotplug_mask;
Zhenyu Wangc6501562009-11-03 18:57:21 +00001741
1742 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001743 I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1744 I915_WRITE(SDEIER, hotplug_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001745 POSTING_READ(SDEIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00001746
Keith Packard7fe0b972011-09-19 13:31:02 -07001747 ironlake_enable_pch_hotplug(dev);
1748
Jesse Barnesf97108d2010-01-29 11:27:07 -08001749 if (IS_IRONLAKE_M(dev)) {
1750 /* Clear & enable PCU event interrupts */
1751 I915_WRITE(DEIIR, DE_PCU_EVENT);
1752 I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1753 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1754 }
1755
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001756 return 0;
1757}
1758
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001759static int ivybridge_irq_postinstall(struct drm_device *dev)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001760{
1761 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1762 /* enable kind of interrupts always enabled */
Chris Wilsonb615b572012-05-02 09:52:12 +01001763 u32 display_mask =
1764 DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
1765 DE_PLANEC_FLIP_DONE_IVB |
1766 DE_PLANEB_FLIP_DONE_IVB |
1767 DE_PLANEA_FLIP_DONE_IVB;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001768 u32 render_irqs;
1769 u32 hotplug_mask;
1770
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001771 dev_priv->irq_mask = ~display_mask;
1772
1773 /* should always can generate irq */
1774 I915_WRITE(DEIIR, I915_READ(DEIIR));
1775 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilsonb615b572012-05-02 09:52:12 +01001776 I915_WRITE(DEIER,
1777 display_mask |
1778 DE_PIPEC_VBLANK_IVB |
1779 DE_PIPEB_VBLANK_IVB |
1780 DE_PIPEA_VBLANK_IVB);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001781 POSTING_READ(DEIER);
1782
1783 dev_priv->gt_irq_mask = ~0;
1784
1785 I915_WRITE(GTIIR, I915_READ(GTIIR));
1786 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1787
Ben Widawskye2a1e2f2012-03-29 19:11:26 -07001788 render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
1789 GEN6_BLITTER_USER_INTERRUPT;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001790 I915_WRITE(GTIER, render_irqs);
1791 POSTING_READ(GTIER);
1792
1793 hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
1794 SDE_PORTB_HOTPLUG_CPT |
1795 SDE_PORTC_HOTPLUG_CPT |
1796 SDE_PORTD_HOTPLUG_CPT);
1797 dev_priv->pch_irq_mask = ~hotplug_mask;
1798
1799 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1800 I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1801 I915_WRITE(SDEIER, hotplug_mask);
1802 POSTING_READ(SDEIER);
1803
Keith Packard7fe0b972011-09-19 13:31:02 -07001804 ironlake_enable_pch_hotplug(dev);
1805
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001806 return 0;
1807}
1808
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001809static int valleyview_irq_postinstall(struct drm_device *dev)
1810{
1811 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1812 u32 render_irqs;
1813 u32 enable_mask;
1814 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
1815 u16 msid;
1816
1817 enable_mask = I915_DISPLAY_PORT_INTERRUPT;
1818 enable_mask |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
1819 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
1820
1821 dev_priv->irq_mask = ~enable_mask;
1822
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001823 dev_priv->pipestat[0] = 0;
1824 dev_priv->pipestat[1] = 0;
1825
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001826 /* Hack for broken MSIs on VLV */
1827 pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
1828 pci_read_config_word(dev->pdev, 0x98, &msid);
1829 msid &= 0xff; /* mask out delivery bits */
1830 msid |= (1<<14);
1831 pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
1832
1833 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
1834 I915_WRITE(VLV_IER, enable_mask);
1835 I915_WRITE(VLV_IIR, 0xffffffff);
1836 I915_WRITE(PIPESTAT(0), 0xffff);
1837 I915_WRITE(PIPESTAT(1), 0xffff);
1838 POSTING_READ(VLV_IER);
1839
1840 I915_WRITE(VLV_IIR, 0xffffffff);
1841 I915_WRITE(VLV_IIR, 0xffffffff);
1842
1843 render_irqs = GT_GEN6_BLT_FLUSHDW_NOTIFY_INTERRUPT |
1844 GT_GEN6_BLT_CS_ERROR_INTERRUPT |
Ben Widawskye2a1e2f2012-03-29 19:11:26 -07001845 GT_GEN6_BLT_USER_INTERRUPT |
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001846 GT_GEN6_BSD_USER_INTERRUPT |
1847 GT_GEN6_BSD_CS_ERROR_INTERRUPT |
1848 GT_GEN7_L3_PARITY_ERROR_INTERRUPT |
1849 GT_PIPE_NOTIFY |
1850 GT_RENDER_CS_ERROR_INTERRUPT |
1851 GT_SYNC_STATUS |
1852 GT_USER_INTERRUPT;
1853
1854 dev_priv->gt_irq_mask = ~render_irqs;
1855
1856 I915_WRITE(GTIIR, I915_READ(GTIIR));
1857 I915_WRITE(GTIIR, I915_READ(GTIIR));
1858 I915_WRITE(GTIMR, 0);
1859 I915_WRITE(GTIER, render_irqs);
1860 POSTING_READ(GTIER);
1861
1862 /* ack & enable invalid PTE error interrupts */
1863#if 0 /* FIXME: add support to irq handler for checking these bits */
1864 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
1865 I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
1866#endif
1867
1868 I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
1869#if 0 /* FIXME: check register definitions; some have moved */
1870 /* Note HDMI and DP share bits */
1871 if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1872 hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1873 if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1874 hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1875 if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1876 hotplug_en |= HDMID_HOTPLUG_INT_EN;
1877 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1878 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1879 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1880 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
1881 if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
1882 hotplug_en |= CRT_HOTPLUG_INT_EN;
1883 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
1884 }
1885#endif
1886
1887 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
1888
1889 return 0;
1890}
1891
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001892static void valleyview_irq_uninstall(struct drm_device *dev)
1893{
1894 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1895 int pipe;
1896
1897 if (!dev_priv)
1898 return;
1899
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001900 for_each_pipe(pipe)
1901 I915_WRITE(PIPESTAT(pipe), 0xffff);
1902
1903 I915_WRITE(HWSTAM, 0xffffffff);
1904 I915_WRITE(PORT_HOTPLUG_EN, 0);
1905 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1906 for_each_pipe(pipe)
1907 I915_WRITE(PIPESTAT(pipe), 0xffff);
1908 I915_WRITE(VLV_IIR, 0xffffffff);
1909 I915_WRITE(VLV_IMR, 0xffffffff);
1910 I915_WRITE(VLV_IER, 0x0);
1911 POSTING_READ(VLV_IER);
1912}
1913
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001914static void ironlake_irq_uninstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001915{
1916 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes46979952011-04-07 13:53:55 -07001917
1918 if (!dev_priv)
1919 return;
1920
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001921 I915_WRITE(HWSTAM, 0xffffffff);
1922
1923 I915_WRITE(DEIMR, 0xffffffff);
1924 I915_WRITE(DEIER, 0x0);
1925 I915_WRITE(DEIIR, I915_READ(DEIIR));
1926
1927 I915_WRITE(GTIMR, 0xffffffff);
1928 I915_WRITE(GTIER, 0x0);
1929 I915_WRITE(GTIIR, I915_READ(GTIIR));
Keith Packard192aac1f2011-09-20 10:12:44 -07001930
1931 I915_WRITE(SDEIMR, 0xffffffff);
1932 I915_WRITE(SDEIER, 0x0);
1933 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001934}
1935
Chris Wilsonc2798b12012-04-22 21:13:57 +01001936static void i8xx_irq_preinstall(struct drm_device * dev)
1937{
1938 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1939 int pipe;
1940
1941 atomic_set(&dev_priv->irq_received, 0);
1942
1943 for_each_pipe(pipe)
1944 I915_WRITE(PIPESTAT(pipe), 0);
1945 I915_WRITE16(IMR, 0xffff);
1946 I915_WRITE16(IER, 0x0);
1947 POSTING_READ16(IER);
1948}
1949
1950static int i8xx_irq_postinstall(struct drm_device *dev)
1951{
1952 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1953
Chris Wilsonc2798b12012-04-22 21:13:57 +01001954 dev_priv->pipestat[0] = 0;
1955 dev_priv->pipestat[1] = 0;
1956
1957 I915_WRITE16(EMR,
1958 ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
1959
1960 /* Unmask the interrupts that we always want on. */
1961 dev_priv->irq_mask =
1962 ~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
1963 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
1964 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
1965 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
1966 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
1967 I915_WRITE16(IMR, dev_priv->irq_mask);
1968
1969 I915_WRITE16(IER,
1970 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
1971 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
1972 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
1973 I915_USER_INTERRUPT);
1974 POSTING_READ16(IER);
1975
1976 return 0;
1977}
1978
1979static irqreturn_t i8xx_irq_handler(DRM_IRQ_ARGS)
1980{
1981 struct drm_device *dev = (struct drm_device *) arg;
1982 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001983 u16 iir, new_iir;
1984 u32 pipe_stats[2];
1985 unsigned long irqflags;
1986 int irq_received;
1987 int pipe;
1988 u16 flip_mask =
1989 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
1990 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
1991
1992 atomic_inc(&dev_priv->irq_received);
1993
1994 iir = I915_READ16(IIR);
1995 if (iir == 0)
1996 return IRQ_NONE;
1997
1998 while (iir & ~flip_mask) {
1999 /* Can't rely on pipestat interrupt bit in iir as it might
2000 * have been cleared after the pipestat interrupt was received.
2001 * It doesn't set the bit in iir again, but it still produces
2002 * interrupts (for non-MSI).
2003 */
2004 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2005 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2006 i915_handle_error(dev, false);
2007
2008 for_each_pipe(pipe) {
2009 int reg = PIPESTAT(pipe);
2010 pipe_stats[pipe] = I915_READ(reg);
2011
2012 /*
2013 * Clear the PIPE*STAT regs before the IIR
2014 */
2015 if (pipe_stats[pipe] & 0x8000ffff) {
2016 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2017 DRM_DEBUG_DRIVER("pipe %c underrun\n",
2018 pipe_name(pipe));
2019 I915_WRITE(reg, pipe_stats[pipe]);
2020 irq_received = 1;
2021 }
2022 }
2023 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2024
2025 I915_WRITE16(IIR, iir & ~flip_mask);
2026 new_iir = I915_READ16(IIR); /* Flush posted writes */
2027
Daniel Vetterd05c6172012-04-26 23:28:09 +02002028 i915_update_dri1_breadcrumb(dev);
Chris Wilsonc2798b12012-04-22 21:13:57 +01002029
2030 if (iir & I915_USER_INTERRUPT)
2031 notify_ring(dev, &dev_priv->ring[RCS]);
2032
2033 if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
2034 drm_handle_vblank(dev, 0)) {
2035 if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
2036 intel_prepare_page_flip(dev, 0);
2037 intel_finish_page_flip(dev, 0);
2038 flip_mask &= ~I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT;
2039 }
2040 }
2041
2042 if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
2043 drm_handle_vblank(dev, 1)) {
2044 if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
2045 intel_prepare_page_flip(dev, 1);
2046 intel_finish_page_flip(dev, 1);
2047 flip_mask &= ~I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2048 }
2049 }
2050
2051 iir = new_iir;
2052 }
2053
2054 return IRQ_HANDLED;
2055}
2056
2057static void i8xx_irq_uninstall(struct drm_device * dev)
2058{
2059 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2060 int pipe;
2061
Chris Wilsonc2798b12012-04-22 21:13:57 +01002062 for_each_pipe(pipe) {
2063 /* Clear enable bits; then clear status bits */
2064 I915_WRITE(PIPESTAT(pipe), 0);
2065 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2066 }
2067 I915_WRITE16(IMR, 0xffff);
2068 I915_WRITE16(IER, 0x0);
2069 I915_WRITE16(IIR, I915_READ16(IIR));
2070}
2071
Chris Wilsona266c7d2012-04-24 22:59:44 +01002072static void i915_irq_preinstall(struct drm_device * dev)
2073{
2074 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2075 int pipe;
2076
2077 atomic_set(&dev_priv->irq_received, 0);
2078
2079 if (I915_HAS_HOTPLUG(dev)) {
2080 I915_WRITE(PORT_HOTPLUG_EN, 0);
2081 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2082 }
2083
Chris Wilson00d98eb2012-04-24 22:59:48 +01002084 I915_WRITE16(HWSTAM, 0xeffe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002085 for_each_pipe(pipe)
2086 I915_WRITE(PIPESTAT(pipe), 0);
2087 I915_WRITE(IMR, 0xffffffff);
2088 I915_WRITE(IER, 0x0);
2089 POSTING_READ(IER);
2090}
2091
2092static int i915_irq_postinstall(struct drm_device *dev)
2093{
2094 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson38bde182012-04-24 22:59:50 +01002095 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002096
Chris Wilsona266c7d2012-04-24 22:59:44 +01002097 dev_priv->pipestat[0] = 0;
2098 dev_priv->pipestat[1] = 0;
2099
Chris Wilson38bde182012-04-24 22:59:50 +01002100 I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2101
2102 /* Unmask the interrupts that we always want on. */
2103 dev_priv->irq_mask =
2104 ~(I915_ASLE_INTERRUPT |
2105 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2106 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2107 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2108 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2109 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2110
2111 enable_mask =
2112 I915_ASLE_INTERRUPT |
2113 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2114 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2115 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2116 I915_USER_INTERRUPT;
2117
Chris Wilsona266c7d2012-04-24 22:59:44 +01002118 if (I915_HAS_HOTPLUG(dev)) {
2119 /* Enable in IER... */
2120 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2121 /* and unmask in IMR */
2122 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2123 }
2124
Chris Wilsona266c7d2012-04-24 22:59:44 +01002125 I915_WRITE(IMR, dev_priv->irq_mask);
2126 I915_WRITE(IER, enable_mask);
2127 POSTING_READ(IER);
2128
2129 if (I915_HAS_HOTPLUG(dev)) {
2130 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2131
Chris Wilsona266c7d2012-04-24 22:59:44 +01002132 if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2133 hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2134 if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2135 hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2136 if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2137 hotplug_en |= HDMID_HOTPLUG_INT_EN;
2138 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
2139 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2140 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
2141 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2142 if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2143 hotplug_en |= CRT_HOTPLUG_INT_EN;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002144 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2145 }
2146
2147 /* Ignore TV since it's buggy */
2148
2149 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2150 }
2151
2152 intel_opregion_enable_asle(dev);
2153
2154 return 0;
2155}
2156
2157static irqreturn_t i915_irq_handler(DRM_IRQ_ARGS)
2158{
2159 struct drm_device *dev = (struct drm_device *) arg;
2160 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson8291ee92012-04-24 22:59:47 +01002161 u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01002162 unsigned long irqflags;
Chris Wilson38bde182012-04-24 22:59:50 +01002163 u32 flip_mask =
2164 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2165 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2166 u32 flip[2] = {
2167 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT,
2168 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT
2169 };
2170 int pipe, ret = IRQ_NONE;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002171
2172 atomic_inc(&dev_priv->irq_received);
2173
2174 iir = I915_READ(IIR);
Chris Wilson38bde182012-04-24 22:59:50 +01002175 do {
2176 bool irq_received = (iir & ~flip_mask) != 0;
Chris Wilson8291ee92012-04-24 22:59:47 +01002177 bool blc_event = false;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002178
2179 /* Can't rely on pipestat interrupt bit in iir as it might
2180 * have been cleared after the pipestat interrupt was received.
2181 * It doesn't set the bit in iir again, but it still produces
2182 * interrupts (for non-MSI).
2183 */
2184 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2185 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2186 i915_handle_error(dev, false);
2187
2188 for_each_pipe(pipe) {
2189 int reg = PIPESTAT(pipe);
2190 pipe_stats[pipe] = I915_READ(reg);
2191
Chris Wilson38bde182012-04-24 22:59:50 +01002192 /* Clear the PIPE*STAT regs before the IIR */
Chris Wilsona266c7d2012-04-24 22:59:44 +01002193 if (pipe_stats[pipe] & 0x8000ffff) {
2194 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2195 DRM_DEBUG_DRIVER("pipe %c underrun\n",
2196 pipe_name(pipe));
2197 I915_WRITE(reg, pipe_stats[pipe]);
Chris Wilson38bde182012-04-24 22:59:50 +01002198 irq_received = true;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002199 }
2200 }
2201 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2202
2203 if (!irq_received)
2204 break;
2205
Chris Wilsona266c7d2012-04-24 22:59:44 +01002206 /* Consume port. Then clear IIR or we'll miss events */
2207 if ((I915_HAS_HOTPLUG(dev)) &&
2208 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2209 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2210
2211 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2212 hotplug_status);
2213 if (hotplug_status & dev_priv->hotplug_supported_mask)
2214 queue_work(dev_priv->wq,
2215 &dev_priv->hotplug_work);
2216
2217 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
Chris Wilson38bde182012-04-24 22:59:50 +01002218 POSTING_READ(PORT_HOTPLUG_STAT);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002219 }
2220
Chris Wilson38bde182012-04-24 22:59:50 +01002221 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002222 new_iir = I915_READ(IIR); /* Flush posted writes */
2223
Chris Wilsona266c7d2012-04-24 22:59:44 +01002224 if (iir & I915_USER_INTERRUPT)
2225 notify_ring(dev, &dev_priv->ring[RCS]);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002226
Chris Wilsona266c7d2012-04-24 22:59:44 +01002227 for_each_pipe(pipe) {
Chris Wilson38bde182012-04-24 22:59:50 +01002228 int plane = pipe;
2229 if (IS_MOBILE(dev))
2230 plane = !plane;
Chris Wilson8291ee92012-04-24 22:59:47 +01002231 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
Chris Wilsona266c7d2012-04-24 22:59:44 +01002232 drm_handle_vblank(dev, pipe)) {
Chris Wilson38bde182012-04-24 22:59:50 +01002233 if (iir & flip[plane]) {
2234 intel_prepare_page_flip(dev, plane);
2235 intel_finish_page_flip(dev, pipe);
2236 flip_mask &= ~flip[plane];
2237 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01002238 }
2239
2240 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2241 blc_event = true;
2242 }
2243
Chris Wilsona266c7d2012-04-24 22:59:44 +01002244 if (blc_event || (iir & I915_ASLE_INTERRUPT))
2245 intel_opregion_asle_intr(dev);
2246
2247 /* With MSI, interrupts are only generated when iir
2248 * transitions from zero to nonzero. If another bit got
2249 * set while we were handling the existing iir bits, then
2250 * we would never get another interrupt.
2251 *
2252 * This is fine on non-MSI as well, as if we hit this path
2253 * we avoid exiting the interrupt handler only to generate
2254 * another one.
2255 *
2256 * Note that for MSI this could cause a stray interrupt report
2257 * if an interrupt landed in the time between writing IIR and
2258 * the posting read. This should be rare enough to never
2259 * trigger the 99% of 100,000 interrupts test for disabling
2260 * stray interrupts.
2261 */
Chris Wilson38bde182012-04-24 22:59:50 +01002262 ret = IRQ_HANDLED;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002263 iir = new_iir;
Chris Wilson38bde182012-04-24 22:59:50 +01002264 } while (iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002265
Daniel Vetterd05c6172012-04-26 23:28:09 +02002266 i915_update_dri1_breadcrumb(dev);
Chris Wilson8291ee92012-04-24 22:59:47 +01002267
Chris Wilsona266c7d2012-04-24 22:59:44 +01002268 return ret;
2269}
2270
2271static void i915_irq_uninstall(struct drm_device * dev)
2272{
2273 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2274 int pipe;
2275
Chris Wilsona266c7d2012-04-24 22:59:44 +01002276 if (I915_HAS_HOTPLUG(dev)) {
2277 I915_WRITE(PORT_HOTPLUG_EN, 0);
2278 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2279 }
2280
Chris Wilson00d98eb2012-04-24 22:59:48 +01002281 I915_WRITE16(HWSTAM, 0xffff);
Chris Wilson55b39752012-04-24 22:59:49 +01002282 for_each_pipe(pipe) {
2283 /* Clear enable bits; then clear status bits */
Chris Wilsona266c7d2012-04-24 22:59:44 +01002284 I915_WRITE(PIPESTAT(pipe), 0);
Chris Wilson55b39752012-04-24 22:59:49 +01002285 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2286 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01002287 I915_WRITE(IMR, 0xffffffff);
2288 I915_WRITE(IER, 0x0);
2289
Chris Wilsona266c7d2012-04-24 22:59:44 +01002290 I915_WRITE(IIR, I915_READ(IIR));
2291}
2292
2293static void i965_irq_preinstall(struct drm_device * dev)
2294{
2295 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2296 int pipe;
2297
2298 atomic_set(&dev_priv->irq_received, 0);
2299
2300 if (I915_HAS_HOTPLUG(dev)) {
2301 I915_WRITE(PORT_HOTPLUG_EN, 0);
2302 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2303 }
2304
2305 I915_WRITE(HWSTAM, 0xeffe);
2306 for_each_pipe(pipe)
2307 I915_WRITE(PIPESTAT(pipe), 0);
2308 I915_WRITE(IMR, 0xffffffff);
2309 I915_WRITE(IER, 0x0);
2310 POSTING_READ(IER);
2311}
2312
2313static int i965_irq_postinstall(struct drm_device *dev)
2314{
2315 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsonbbba0a92012-04-24 22:59:51 +01002316 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002317 u32 error_mask;
2318
Chris Wilsona266c7d2012-04-24 22:59:44 +01002319 /* Unmask the interrupts that we always want on. */
Chris Wilsonbbba0a92012-04-24 22:59:51 +01002320 dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2321 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2322 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2323 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2324 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2325 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2326
2327 enable_mask = ~dev_priv->irq_mask;
2328 enable_mask |= I915_USER_INTERRUPT;
2329
2330 if (IS_G4X(dev))
2331 enable_mask |= I915_BSD_USER_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002332
2333 dev_priv->pipestat[0] = 0;
2334 dev_priv->pipestat[1] = 0;
2335
2336 if (I915_HAS_HOTPLUG(dev)) {
2337 /* Enable in IER... */
2338 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2339 /* and unmask in IMR */
2340 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2341 }
2342
2343 /*
2344 * Enable some error detection, note the instruction error mask
2345 * bit is reserved, so we leave it masked.
2346 */
2347 if (IS_G4X(dev)) {
2348 error_mask = ~(GM45_ERROR_PAGE_TABLE |
2349 GM45_ERROR_MEM_PRIV |
2350 GM45_ERROR_CP_PRIV |
2351 I915_ERROR_MEMORY_REFRESH);
2352 } else {
2353 error_mask = ~(I915_ERROR_PAGE_TABLE |
2354 I915_ERROR_MEMORY_REFRESH);
2355 }
2356 I915_WRITE(EMR, error_mask);
2357
2358 I915_WRITE(IMR, dev_priv->irq_mask);
2359 I915_WRITE(IER, enable_mask);
2360 POSTING_READ(IER);
2361
2362 if (I915_HAS_HOTPLUG(dev)) {
2363 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2364
2365 /* Note HDMI and DP share bits */
2366 if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2367 hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2368 if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2369 hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2370 if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2371 hotplug_en |= HDMID_HOTPLUG_INT_EN;
2372 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
2373 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2374 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
2375 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2376 if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2377 hotplug_en |= CRT_HOTPLUG_INT_EN;
2378
2379 /* Programming the CRT detection parameters tends
2380 to generate a spurious hotplug event about three
2381 seconds later. So just do it once.
2382 */
2383 if (IS_G4X(dev))
2384 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2385 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2386 }
2387
2388 /* Ignore TV since it's buggy */
2389
2390 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2391 }
2392
2393 intel_opregion_enable_asle(dev);
2394
2395 return 0;
2396}
2397
2398static irqreturn_t i965_irq_handler(DRM_IRQ_ARGS)
2399{
2400 struct drm_device *dev = (struct drm_device *) arg;
2401 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002402 u32 iir, new_iir;
2403 u32 pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01002404 unsigned long irqflags;
2405 int irq_received;
2406 int ret = IRQ_NONE, pipe;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002407
2408 atomic_inc(&dev_priv->irq_received);
2409
2410 iir = I915_READ(IIR);
2411
Chris Wilsona266c7d2012-04-24 22:59:44 +01002412 for (;;) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01002413 bool blc_event = false;
2414
Chris Wilsona266c7d2012-04-24 22:59:44 +01002415 irq_received = iir != 0;
2416
2417 /* Can't rely on pipestat interrupt bit in iir as it might
2418 * have been cleared after the pipestat interrupt was received.
2419 * It doesn't set the bit in iir again, but it still produces
2420 * interrupts (for non-MSI).
2421 */
2422 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2423 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2424 i915_handle_error(dev, false);
2425
2426 for_each_pipe(pipe) {
2427 int reg = PIPESTAT(pipe);
2428 pipe_stats[pipe] = I915_READ(reg);
2429
2430 /*
2431 * Clear the PIPE*STAT regs before the IIR
2432 */
2433 if (pipe_stats[pipe] & 0x8000ffff) {
2434 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2435 DRM_DEBUG_DRIVER("pipe %c underrun\n",
2436 pipe_name(pipe));
2437 I915_WRITE(reg, pipe_stats[pipe]);
2438 irq_received = 1;
2439 }
2440 }
2441 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2442
2443 if (!irq_received)
2444 break;
2445
2446 ret = IRQ_HANDLED;
2447
2448 /* Consume port. Then clear IIR or we'll miss events */
2449 if ((I915_HAS_HOTPLUG(dev)) &&
2450 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2451 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2452
2453 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2454 hotplug_status);
2455 if (hotplug_status & dev_priv->hotplug_supported_mask)
2456 queue_work(dev_priv->wq,
2457 &dev_priv->hotplug_work);
2458
2459 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2460 I915_READ(PORT_HOTPLUG_STAT);
2461 }
2462
2463 I915_WRITE(IIR, iir);
2464 new_iir = I915_READ(IIR); /* Flush posted writes */
2465
Chris Wilsona266c7d2012-04-24 22:59:44 +01002466 if (iir & I915_USER_INTERRUPT)
2467 notify_ring(dev, &dev_priv->ring[RCS]);
2468 if (iir & I915_BSD_USER_INTERRUPT)
2469 notify_ring(dev, &dev_priv->ring[VCS]);
2470
Chris Wilson4f7d1e72012-04-24 22:59:45 +01002471 if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT)
Chris Wilsona266c7d2012-04-24 22:59:44 +01002472 intel_prepare_page_flip(dev, 0);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002473
Chris Wilson4f7d1e72012-04-24 22:59:45 +01002474 if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT)
Chris Wilsona266c7d2012-04-24 22:59:44 +01002475 intel_prepare_page_flip(dev, 1);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002476
2477 for_each_pipe(pipe) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01002478 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
Chris Wilsona266c7d2012-04-24 22:59:44 +01002479 drm_handle_vblank(dev, pipe)) {
Chris Wilson4f7d1e72012-04-24 22:59:45 +01002480 i915_pageflip_stall_check(dev, pipe);
2481 intel_finish_page_flip(dev, pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002482 }
2483
2484 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2485 blc_event = true;
2486 }
2487
2488
2489 if (blc_event || (iir & I915_ASLE_INTERRUPT))
2490 intel_opregion_asle_intr(dev);
2491
2492 /* With MSI, interrupts are only generated when iir
2493 * transitions from zero to nonzero. If another bit got
2494 * set while we were handling the existing iir bits, then
2495 * we would never get another interrupt.
2496 *
2497 * This is fine on non-MSI as well, as if we hit this path
2498 * we avoid exiting the interrupt handler only to generate
2499 * another one.
2500 *
2501 * Note that for MSI this could cause a stray interrupt report
2502 * if an interrupt landed in the time between writing IIR and
2503 * the posting read. This should be rare enough to never
2504 * trigger the 99% of 100,000 interrupts test for disabling
2505 * stray interrupts.
2506 */
2507 iir = new_iir;
2508 }
2509
Daniel Vetterd05c6172012-04-26 23:28:09 +02002510 i915_update_dri1_breadcrumb(dev);
Chris Wilson2c8ba292012-04-24 22:59:46 +01002511
Chris Wilsona266c7d2012-04-24 22:59:44 +01002512 return ret;
2513}
2514
2515static void i965_irq_uninstall(struct drm_device * dev)
2516{
2517 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2518 int pipe;
2519
2520 if (!dev_priv)
2521 return;
2522
Chris Wilsona266c7d2012-04-24 22:59:44 +01002523 if (I915_HAS_HOTPLUG(dev)) {
2524 I915_WRITE(PORT_HOTPLUG_EN, 0);
2525 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2526 }
2527
2528 I915_WRITE(HWSTAM, 0xffffffff);
2529 for_each_pipe(pipe)
2530 I915_WRITE(PIPESTAT(pipe), 0);
2531 I915_WRITE(IMR, 0xffffffff);
2532 I915_WRITE(IER, 0x0);
2533
2534 for_each_pipe(pipe)
2535 I915_WRITE(PIPESTAT(pipe),
2536 I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2537 I915_WRITE(IIR, I915_READ(IIR));
2538}
2539
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002540void intel_irq_init(struct drm_device *dev)
2541{
Chris Wilson8b2e3262012-04-24 22:59:41 +01002542 struct drm_i915_private *dev_priv = dev->dev_private;
2543
2544 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
2545 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
2546 INIT_WORK(&dev_priv->rps_work, gen6_pm_rps_work);
2547
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002548 dev->driver->get_vblank_counter = i915_get_vblank_counter;
2549 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Eugeni Dodonov7d4e1462012-05-09 15:37:09 -03002550 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002551 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2552 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2553 }
2554
Keith Packardc3613de2011-08-12 17:05:54 -07002555 if (drm_core_check_feature(dev, DRIVER_MODESET))
2556 dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2557 else
2558 dev->driver->get_vblank_timestamp = NULL;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002559 dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2560
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002561 if (IS_VALLEYVIEW(dev)) {
2562 dev->driver->irq_handler = valleyview_irq_handler;
2563 dev->driver->irq_preinstall = valleyview_irq_preinstall;
2564 dev->driver->irq_postinstall = valleyview_irq_postinstall;
2565 dev->driver->irq_uninstall = valleyview_irq_uninstall;
2566 dev->driver->enable_vblank = valleyview_enable_vblank;
2567 dev->driver->disable_vblank = valleyview_disable_vblank;
2568 } else if (IS_IVYBRIDGE(dev)) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002569 /* Share pre & uninstall handlers with ILK/SNB */
2570 dev->driver->irq_handler = ivybridge_irq_handler;
2571 dev->driver->irq_preinstall = ironlake_irq_preinstall;
2572 dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2573 dev->driver->irq_uninstall = ironlake_irq_uninstall;
2574 dev->driver->enable_vblank = ivybridge_enable_vblank;
2575 dev->driver->disable_vblank = ivybridge_disable_vblank;
Eugeni Dodonov7d4e1462012-05-09 15:37:09 -03002576 } else if (IS_HASWELL(dev)) {
2577 /* Share interrupts handling with IVB */
2578 dev->driver->irq_handler = ivybridge_irq_handler;
2579 dev->driver->irq_preinstall = ironlake_irq_preinstall;
2580 dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2581 dev->driver->irq_uninstall = ironlake_irq_uninstall;
2582 dev->driver->enable_vblank = ivybridge_enable_vblank;
2583 dev->driver->disable_vblank = ivybridge_disable_vblank;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002584 } else if (HAS_PCH_SPLIT(dev)) {
2585 dev->driver->irq_handler = ironlake_irq_handler;
2586 dev->driver->irq_preinstall = ironlake_irq_preinstall;
2587 dev->driver->irq_postinstall = ironlake_irq_postinstall;
2588 dev->driver->irq_uninstall = ironlake_irq_uninstall;
2589 dev->driver->enable_vblank = ironlake_enable_vblank;
2590 dev->driver->disable_vblank = ironlake_disable_vblank;
2591 } else {
Chris Wilsonc2798b12012-04-22 21:13:57 +01002592 if (INTEL_INFO(dev)->gen == 2) {
2593 dev->driver->irq_preinstall = i8xx_irq_preinstall;
2594 dev->driver->irq_postinstall = i8xx_irq_postinstall;
2595 dev->driver->irq_handler = i8xx_irq_handler;
2596 dev->driver->irq_uninstall = i8xx_irq_uninstall;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002597 } else if (INTEL_INFO(dev)->gen == 3) {
Chris Wilson4f7d1e72012-04-24 22:59:45 +01002598 /* IIR "flip pending" means done if this bit is set */
2599 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
2600
Chris Wilsona266c7d2012-04-24 22:59:44 +01002601 dev->driver->irq_preinstall = i915_irq_preinstall;
2602 dev->driver->irq_postinstall = i915_irq_postinstall;
2603 dev->driver->irq_uninstall = i915_irq_uninstall;
2604 dev->driver->irq_handler = i915_irq_handler;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002605 } else {
Chris Wilsona266c7d2012-04-24 22:59:44 +01002606 dev->driver->irq_preinstall = i965_irq_preinstall;
2607 dev->driver->irq_postinstall = i965_irq_postinstall;
2608 dev->driver->irq_uninstall = i965_irq_uninstall;
2609 dev->driver->irq_handler = i965_irq_handler;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002610 }
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002611 dev->driver->enable_vblank = i915_enable_vblank;
2612 dev->driver->disable_vblank = i915_disable_vblank;
2613 }
2614}