blob: 5fc178e1e515b36306fd017e1e52ce2d3b444ec5 [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>
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/drmP.h>
34#include <drm/i915_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010036#include "i915_trace.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080037#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Zhenyu Wang036a4a72009-06-08 14:40:19 +080039/* For display hotplug interrupt */
Chris Wilson995b6762010-08-20 13:23:26 +010040static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050041ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080042{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000043 if ((dev_priv->irq_mask & mask) != 0) {
44 dev_priv->irq_mask &= ~mask;
45 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000046 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080047 }
48}
49
50static inline void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050051ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080052{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000053 if ((dev_priv->irq_mask & mask) != mask) {
54 dev_priv->irq_mask |= mask;
55 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000056 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080057 }
58}
59
Keith Packard7c463582008-11-04 02:03:27 -080060void
61i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
62{
Ville Syrjälä46c06a32013-02-20 21:16:18 +020063 u32 reg = PIPESTAT(pipe);
64 u32 pipestat = I915_READ(reg) & 0x7fff0000;
Keith Packard7c463582008-11-04 02:03:27 -080065
Ville Syrjälä46c06a32013-02-20 21:16:18 +020066 if ((pipestat & mask) == mask)
67 return;
68
69 /* Enable the interrupt, clear any pending status */
70 pipestat |= mask | (mask >> 16);
71 I915_WRITE(reg, pipestat);
72 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -080073}
74
75void
76i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
77{
Ville Syrjälä46c06a32013-02-20 21:16:18 +020078 u32 reg = PIPESTAT(pipe);
79 u32 pipestat = I915_READ(reg) & 0x7fff0000;
Keith Packard7c463582008-11-04 02:03:27 -080080
Ville Syrjälä46c06a32013-02-20 21:16:18 +020081 if ((pipestat & mask) == 0)
82 return;
83
84 pipestat &= ~mask;
85 I915_WRITE(reg, pipestat);
86 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -080087}
88
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100089/**
Zhao Yakui01c66882009-10-28 05:10:00 +000090 * intel_enable_asle - enable ASLE interrupt for OpRegion
91 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +000092void intel_enable_asle(struct drm_device *dev)
Zhao Yakui01c66882009-10-28 05:10:00 +000093{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000094 drm_i915_private_t *dev_priv = dev->dev_private;
95 unsigned long irqflags;
96
Jesse Barnes7e231dbe2012-03-28 13:39:38 -070097 /* FIXME: opregion/asle for VLV */
98 if (IS_VALLEYVIEW(dev))
99 return;
100
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000101 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000102
Eric Anholtc619eed2010-01-28 16:45:52 -0800103 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500104 ironlake_enable_display_irq(dev_priv, DE_GSE);
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800105 else {
Zhao Yakui01c66882009-10-28 05:10:00 +0000106 i915_enable_pipestat(dev_priv, 1,
Jesse Barnesd874bcf2010-06-30 13:16:00 -0700107 PIPE_LEGACY_BLC_EVENT_ENABLE);
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100108 if (INTEL_INFO(dev)->gen >= 4)
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800109 i915_enable_pipestat(dev_priv, 0,
Jesse Barnesd874bcf2010-06-30 13:16:00 -0700110 PIPE_LEGACY_BLC_EVENT_ENABLE);
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800111 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000112
113 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000114}
115
116/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700117 * i915_pipe_enabled - check if a pipe is enabled
118 * @dev: DRM device
119 * @pipe: pipe to check
120 *
121 * Reading certain registers when the pipe is disabled can hang the chip.
122 * Use this routine to make sure the PLL is running and the pipe is active
123 * before reading such registers if unsure.
124 */
125static int
126i915_pipe_enabled(struct drm_device *dev, int pipe)
127{
128 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200129 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
130 pipe);
131
132 return I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_ENABLE;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700133}
134
Keith Packard42f52ef2008-10-18 19:39:29 -0700135/* Called from drm generic code, passed a 'crtc', which
136 * we use as a pipe index
137 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700138static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700139{
140 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
141 unsigned long high_frame;
142 unsigned long low_frame;
Chris Wilson5eddb702010-09-11 13:48:45 +0100143 u32 high1, high2, low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700144
145 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800146 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800147 "pipe %c\n", pipe_name(pipe));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700148 return 0;
149 }
150
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800151 high_frame = PIPEFRAME(pipe);
152 low_frame = PIPEFRAMEPIXEL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +0100153
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700154 /*
155 * High & low register fields aren't synchronized, so make sure
156 * we get a low value that's stable across two reads of the high
157 * register.
158 */
159 do {
Chris Wilson5eddb702010-09-11 13:48:45 +0100160 high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
161 low = I915_READ(low_frame) & PIPE_FRAME_LOW_MASK;
162 high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700163 } while (high1 != high2);
164
Chris Wilson5eddb702010-09-11 13:48:45 +0100165 high1 >>= PIPE_FRAME_HIGH_SHIFT;
166 low >>= PIPE_FRAME_LOW_SHIFT;
167 return (high1 << 8) | low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700168}
169
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700170static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800171{
172 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800173 int reg = PIPE_FRMCOUNT_GM45(pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800174
175 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800176 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800177 "pipe %c\n", pipe_name(pipe));
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800178 return 0;
179 }
180
181 return I915_READ(reg);
182}
183
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700184static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100185 int *vpos, int *hpos)
186{
187 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
188 u32 vbl = 0, position = 0;
189 int vbl_start, vbl_end, htotal, vtotal;
190 bool in_vbl = true;
191 int ret = 0;
Paulo Zanonife2b8f92012-10-23 18:30:02 -0200192 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
193 pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100194
195 if (!i915_pipe_enabled(dev, pipe)) {
196 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800197 "pipe %c\n", pipe_name(pipe));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100198 return 0;
199 }
200
201 /* Get vtotal. */
Paulo Zanonife2b8f92012-10-23 18:30:02 -0200202 vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100203
204 if (INTEL_INFO(dev)->gen >= 4) {
205 /* No obvious pixelcount register. Only query vertical
206 * scanout position from Display scan line register.
207 */
208 position = I915_READ(PIPEDSL(pipe));
209
210 /* Decode into vertical scanout position. Don't have
211 * horizontal scanout position.
212 */
213 *vpos = position & 0x1fff;
214 *hpos = 0;
215 } else {
216 /* Have access to pixelcount since start of frame.
217 * We can split this into vertical and horizontal
218 * scanout position.
219 */
220 position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
221
Paulo Zanonife2b8f92012-10-23 18:30:02 -0200222 htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100223 *vpos = position / htotal;
224 *hpos = position - (*vpos * htotal);
225 }
226
227 /* Query vblank area. */
Paulo Zanonife2b8f92012-10-23 18:30:02 -0200228 vbl = I915_READ(VBLANK(cpu_transcoder));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100229
230 /* Test position against vblank region. */
231 vbl_start = vbl & 0x1fff;
232 vbl_end = (vbl >> 16) & 0x1fff;
233
234 if ((*vpos < vbl_start) || (*vpos > vbl_end))
235 in_vbl = false;
236
237 /* Inside "upper part" of vblank area? Apply corrective offset: */
238 if (in_vbl && (*vpos >= vbl_start))
239 *vpos = *vpos - vtotal;
240
241 /* Readouts valid? */
242 if (vbl > 0)
243 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
244
245 /* In vblank? */
246 if (in_vbl)
247 ret |= DRM_SCANOUTPOS_INVBL;
248
249 return ret;
250}
251
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700252static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100253 int *max_error,
254 struct timeval *vblank_time,
255 unsigned flags)
256{
Chris Wilson4041b852011-01-22 10:07:56 +0000257 struct drm_crtc *crtc;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100258
Ben Widawsky7eb552a2013-03-13 14:05:41 -0700259 if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
Chris Wilson4041b852011-01-22 10:07:56 +0000260 DRM_ERROR("Invalid crtc %d\n", pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100261 return -EINVAL;
262 }
263
264 /* Get drm_crtc to timestamp: */
Chris Wilson4041b852011-01-22 10:07:56 +0000265 crtc = intel_get_crtc_for_pipe(dev, pipe);
266 if (crtc == NULL) {
267 DRM_ERROR("Invalid crtc %d\n", pipe);
268 return -EINVAL;
269 }
270
271 if (!crtc->enabled) {
272 DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
273 return -EBUSY;
274 }
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100275
276 /* Helper routine in DRM core does all the work: */
Chris Wilson4041b852011-01-22 10:07:56 +0000277 return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
278 vblank_time, flags,
279 crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100280}
281
Jesse Barnes5ca58282009-03-31 14:11:15 -0700282/*
283 * Handle hotplug events outside the interrupt handler proper.
284 */
285static void i915_hotplug_work_func(struct work_struct *work)
286{
287 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
288 hotplug_work);
289 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700290 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100291 struct intel_encoder *encoder;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700292
Daniel Vetter52d7ece2012-12-01 21:03:22 +0100293 /* HPD irq before everything is fully set up. */
294 if (!dev_priv->enable_hotplug_processing)
295 return;
296
Keith Packarda65e34c2011-07-25 10:04:56 -0700297 mutex_lock(&mode_config->mutex);
Jesse Barnese67189ab2011-02-11 14:44:51 -0800298 DRM_DEBUG_KMS("running encoder hotplug functions\n");
299
Chris Wilson4ef69c72010-09-09 15:14:28 +0100300 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
301 if (encoder->hot_plug)
302 encoder->hot_plug(encoder);
303
Keith Packard40ee3382011-07-28 15:31:19 -0700304 mutex_unlock(&mode_config->mutex);
305
Jesse Barnes5ca58282009-03-31 14:11:15 -0700306 /* Just fire off a uevent and let userspace tell us what to do */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000307 drm_helper_hpd_irq_event(dev);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700308}
309
Daniel Vetter73edd18f2012-08-08 23:35:37 +0200310static void ironlake_handle_rps_change(struct drm_device *dev)
Jesse Barnesf97108d2010-01-29 11:27:07 -0800311{
312 drm_i915_private_t *dev_priv = dev->dev_private;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000313 u32 busy_up, busy_down, max_avg, min_avg;
Daniel Vetter92703882012-08-09 16:46:01 +0200314 u8 new_delay;
315 unsigned long flags;
316
317 spin_lock_irqsave(&mchdev_lock, flags);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800318
Daniel Vetter73edd18f2012-08-08 23:35:37 +0200319 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
320
Daniel Vetter20e4d402012-08-08 23:35:39 +0200321 new_delay = dev_priv->ips.cur_delay;
Daniel Vetter92703882012-08-09 16:46:01 +0200322
Jesse Barnes7648fa92010-05-20 14:28:11 -0700323 I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000324 busy_up = I915_READ(RCPREVBSYTUPAVG);
325 busy_down = I915_READ(RCPREVBSYTDNAVG);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800326 max_avg = I915_READ(RCBMAXAVG);
327 min_avg = I915_READ(RCBMINAVG);
328
329 /* Handle RCS change request from hw */
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000330 if (busy_up > max_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200331 if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
332 new_delay = dev_priv->ips.cur_delay - 1;
333 if (new_delay < dev_priv->ips.max_delay)
334 new_delay = dev_priv->ips.max_delay;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000335 } else if (busy_down < min_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200336 if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
337 new_delay = dev_priv->ips.cur_delay + 1;
338 if (new_delay > dev_priv->ips.min_delay)
339 new_delay = dev_priv->ips.min_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800340 }
341
Jesse Barnes7648fa92010-05-20 14:28:11 -0700342 if (ironlake_set_drps(dev, new_delay))
Daniel Vetter20e4d402012-08-08 23:35:39 +0200343 dev_priv->ips.cur_delay = new_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800344
Daniel Vetter92703882012-08-09 16:46:01 +0200345 spin_unlock_irqrestore(&mchdev_lock, flags);
346
Jesse Barnesf97108d2010-01-29 11:27:07 -0800347 return;
348}
349
Chris Wilson549f7362010-10-19 11:19:32 +0100350static void notify_ring(struct drm_device *dev,
351 struct intel_ring_buffer *ring)
352{
353 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9862e602011-01-04 22:22:17 +0000354
Chris Wilson475553d2011-01-20 09:52:56 +0000355 if (ring->obj == NULL)
356 return;
357
Chris Wilsonb2eadbc2012-08-09 10:58:30 +0100358 trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
Chris Wilson9862e602011-01-04 22:22:17 +0000359
Chris Wilson549f7362010-10-19 11:19:32 +0100360 wake_up_all(&ring->irq_queue);
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -0700361 if (i915_enable_hangcheck) {
Daniel Vetter99584db2012-11-14 17:14:04 +0100362 dev_priv->gpu_error.hangcheck_count = 0;
363 mod_timer(&dev_priv->gpu_error.hangcheck_timer,
Chris Wilsoncecc21f2012-10-05 17:02:56 +0100364 round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -0700365 }
Chris Wilson549f7362010-10-19 11:19:32 +0100366}
367
Ben Widawsky4912d042011-04-25 11:25:20 -0700368static void gen6_pm_rps_work(struct work_struct *work)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800369{
Ben Widawsky4912d042011-04-25 11:25:20 -0700370 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200371 rps.work);
Ben Widawsky4912d042011-04-25 11:25:20 -0700372 u32 pm_iir, pm_imr;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100373 u8 new_delay;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800374
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200375 spin_lock_irq(&dev_priv->rps.lock);
376 pm_iir = dev_priv->rps.pm_iir;
377 dev_priv->rps.pm_iir = 0;
Ben Widawsky4912d042011-04-25 11:25:20 -0700378 pm_imr = I915_READ(GEN6_PMIMR);
Daniel Vettera9e26412011-09-08 14:00:21 +0200379 I915_WRITE(GEN6_PMIMR, 0);
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200380 spin_unlock_irq(&dev_priv->rps.lock);
Ben Widawsky4912d042011-04-25 11:25:20 -0700381
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100382 if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800383 return;
384
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700385 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100386
387 if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200388 new_delay = dev_priv->rps.cur_delay + 1;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100389 else
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200390 new_delay = dev_priv->rps.cur_delay - 1;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800391
Ben Widawsky79249632012-09-07 19:43:42 -0700392 /* sysfs frequency interfaces may have snuck in while servicing the
393 * interrupt
394 */
395 if (!(new_delay > dev_priv->rps.max_delay ||
396 new_delay < dev_priv->rps.min_delay)) {
397 gen6_set_rps(dev_priv->dev, new_delay);
398 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800399
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700400 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800401}
402
Ben Widawskye3689192012-05-25 16:56:22 -0700403
404/**
405 * ivybridge_parity_work - Workqueue called when a parity error interrupt
406 * occurred.
407 * @work: workqueue struct
408 *
409 * Doesn't actually do anything except notify userspace. As a consequence of
410 * this event, userspace should try to remap the bad rows since statistically
411 * it is likely the same row is more likely to go bad again.
412 */
413static void ivybridge_parity_work(struct work_struct *work)
414{
415 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
Daniel Vettera4da4fa2012-11-02 19:55:07 +0100416 l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -0700417 u32 error_status, row, bank, subbank;
418 char *parity_event[5];
419 uint32_t misccpctl;
420 unsigned long flags;
421
422 /* We must turn off DOP level clock gating to access the L3 registers.
423 * In order to prevent a get/put style interface, acquire struct mutex
424 * any time we access those registers.
425 */
426 mutex_lock(&dev_priv->dev->struct_mutex);
427
428 misccpctl = I915_READ(GEN7_MISCCPCTL);
429 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
430 POSTING_READ(GEN7_MISCCPCTL);
431
432 error_status = I915_READ(GEN7_L3CDERRST1);
433 row = GEN7_PARITY_ERROR_ROW(error_status);
434 bank = GEN7_PARITY_ERROR_BANK(error_status);
435 subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
436
437 I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
438 GEN7_L3CDERRST1_ENABLE);
439 POSTING_READ(GEN7_L3CDERRST1);
440
441 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
442
443 spin_lock_irqsave(&dev_priv->irq_lock, flags);
444 dev_priv->gt_irq_mask &= ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
445 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
446 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
447
448 mutex_unlock(&dev_priv->dev->struct_mutex);
449
450 parity_event[0] = "L3_PARITY_ERROR=1";
451 parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
452 parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
453 parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
454 parity_event[4] = NULL;
455
456 kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
457 KOBJ_CHANGE, parity_event);
458
459 DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
460 row, bank, subbank);
461
462 kfree(parity_event[3]);
463 kfree(parity_event[2]);
464 kfree(parity_event[1]);
465}
466
Daniel Vetterd2ba8472012-05-31 14:57:41 +0200467static void ivybridge_handle_parity_error(struct drm_device *dev)
Ben Widawskye3689192012-05-25 16:56:22 -0700468{
469 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
470 unsigned long flags;
471
Ben Widawskye1ef7cc2012-07-24 20:47:31 -0700472 if (!HAS_L3_GPU_CACHE(dev))
Ben Widawskye3689192012-05-25 16:56:22 -0700473 return;
474
475 spin_lock_irqsave(&dev_priv->irq_lock, flags);
476 dev_priv->gt_irq_mask |= GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
477 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
478 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
479
Daniel Vettera4da4fa2012-11-02 19:55:07 +0100480 queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -0700481}
482
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200483static void snb_gt_irq_handler(struct drm_device *dev,
484 struct drm_i915_private *dev_priv,
485 u32 gt_iir)
486{
487
488 if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
489 GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
490 notify_ring(dev, &dev_priv->ring[RCS]);
491 if (gt_iir & GEN6_BSD_USER_INTERRUPT)
492 notify_ring(dev, &dev_priv->ring[VCS]);
493 if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
494 notify_ring(dev, &dev_priv->ring[BCS]);
495
496 if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
497 GT_GEN6_BSD_CS_ERROR_INTERRUPT |
498 GT_RENDER_CS_ERROR_INTERRUPT)) {
499 DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
500 i915_handle_error(dev, false);
501 }
Ben Widawskye3689192012-05-25 16:56:22 -0700502
503 if (gt_iir & GT_GEN7_L3_PARITY_ERROR_INTERRUPT)
504 ivybridge_handle_parity_error(dev);
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200505}
506
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100507static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
508 u32 pm_iir)
509{
510 unsigned long flags;
511
512 /*
513 * IIR bits should never already be set because IMR should
514 * prevent an interrupt from being shown in IIR. The warning
515 * displays a case where we've unsafely cleared
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200516 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100517 * type is not a problem, it displays a problem in the logic.
518 *
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200519 * The mask bit in IMR is cleared by dev_priv->rps.work.
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100520 */
521
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200522 spin_lock_irqsave(&dev_priv->rps.lock, flags);
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200523 dev_priv->rps.pm_iir |= pm_iir;
524 I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100525 POSTING_READ(GEN6_PMIMR);
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200526 spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100527
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200528 queue_work(dev_priv->wq, &dev_priv->rps.work);
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100529}
530
Daniel Vetter515ac2b2012-12-01 13:53:44 +0100531static void gmbus_irq_handler(struct drm_device *dev)
532{
Daniel Vetter28c70f12012-12-01 13:53:45 +0100533 struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
534
Daniel Vetter28c70f12012-12-01 13:53:45 +0100535 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetter515ac2b2012-12-01 13:53:44 +0100536}
537
Daniel Vetterce99c252012-12-01 13:53:47 +0100538static void dp_aux_irq_handler(struct drm_device *dev)
539{
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100540 struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
541
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100542 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetterce99c252012-12-01 13:53:47 +0100543}
544
Daniel Vetterff1f5252012-10-02 15:10:55 +0200545static irqreturn_t valleyview_irq_handler(int irq, void *arg)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700546{
547 struct drm_device *dev = (struct drm_device *) arg;
548 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
549 u32 iir, gt_iir, pm_iir;
550 irqreturn_t ret = IRQ_NONE;
551 unsigned long irqflags;
552 int pipe;
553 u32 pipe_stats[I915_MAX_PIPES];
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700554
555 atomic_inc(&dev_priv->irq_received);
556
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700557 while (true) {
558 iir = I915_READ(VLV_IIR);
559 gt_iir = I915_READ(GTIIR);
560 pm_iir = I915_READ(GEN6_PMIIR);
561
562 if (gt_iir == 0 && pm_iir == 0 && iir == 0)
563 goto out;
564
565 ret = IRQ_HANDLED;
566
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200567 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700568
569 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
570 for_each_pipe(pipe) {
571 int reg = PIPESTAT(pipe);
572 pipe_stats[pipe] = I915_READ(reg);
573
574 /*
575 * Clear the PIPE*STAT regs before the IIR
576 */
577 if (pipe_stats[pipe] & 0x8000ffff) {
578 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
579 DRM_DEBUG_DRIVER("pipe %c underrun\n",
580 pipe_name(pipe));
581 I915_WRITE(reg, pipe_stats[pipe]);
582 }
583 }
584 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
585
Jesse Barnes31acc7f2012-06-20 10:53:11 -0700586 for_each_pipe(pipe) {
587 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
588 drm_handle_vblank(dev, pipe);
589
590 if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
591 intel_prepare_page_flip(dev, pipe);
592 intel_finish_page_flip(dev, pipe);
593 }
594 }
595
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700596 /* Consume port. Then clear IIR or we'll miss events */
597 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
598 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
599
600 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
601 hotplug_status);
602 if (hotplug_status & dev_priv->hotplug_supported_mask)
603 queue_work(dev_priv->wq,
604 &dev_priv->hotplug_work);
605
606 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
607 I915_READ(PORT_HOTPLUG_STAT);
608 }
609
Daniel Vetter515ac2b2012-12-01 13:53:44 +0100610 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
611 gmbus_irq_handler(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700612
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100613 if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
614 gen6_queue_rps_work(dev_priv, pm_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700615
616 I915_WRITE(GTIIR, gt_iir);
617 I915_WRITE(GEN6_PMIIR, pm_iir);
618 I915_WRITE(VLV_IIR, iir);
619 }
620
621out:
622 return ret;
623}
624
Adam Jackson23e81d62012-06-06 15:45:44 -0400625static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
Jesse Barnes776ad802011-01-04 15:09:39 -0800626{
627 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800628 int pipe;
Jesse Barnes776ad802011-01-04 15:09:39 -0800629
Daniel Vetter76e43832012-10-12 20:14:05 +0200630 if (pch_iir & SDE_HOTPLUG_MASK)
631 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
632
Jesse Barnes776ad802011-01-04 15:09:39 -0800633 if (pch_iir & SDE_AUDIO_POWER_MASK)
634 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
635 (pch_iir & SDE_AUDIO_POWER_MASK) >>
636 SDE_AUDIO_POWER_SHIFT);
637
Daniel Vetterce99c252012-12-01 13:53:47 +0100638 if (pch_iir & SDE_AUX_MASK)
639 dp_aux_irq_handler(dev);
640
Jesse Barnes776ad802011-01-04 15:09:39 -0800641 if (pch_iir & SDE_GMBUS)
Daniel Vetter515ac2b2012-12-01 13:53:44 +0100642 gmbus_irq_handler(dev);
Jesse Barnes776ad802011-01-04 15:09:39 -0800643
644 if (pch_iir & SDE_AUDIO_HDCP_MASK)
645 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
646
647 if (pch_iir & SDE_AUDIO_TRANS_MASK)
648 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
649
650 if (pch_iir & SDE_POISON)
651 DRM_ERROR("PCH poison interrupt\n");
652
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800653 if (pch_iir & SDE_FDI_MASK)
654 for_each_pipe(pipe)
655 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
656 pipe_name(pipe),
657 I915_READ(FDI_RX_IIR(pipe)));
Jesse Barnes776ad802011-01-04 15:09:39 -0800658
659 if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
660 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
661
662 if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
663 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
664
665 if (pch_iir & SDE_TRANSB_FIFO_UNDER)
666 DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
667 if (pch_iir & SDE_TRANSA_FIFO_UNDER)
668 DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
669}
670
Adam Jackson23e81d62012-06-06 15:45:44 -0400671static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
672{
673 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
674 int pipe;
675
Daniel Vetter76e43832012-10-12 20:14:05 +0200676 if (pch_iir & SDE_HOTPLUG_MASK_CPT)
677 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
678
Adam Jackson23e81d62012-06-06 15:45:44 -0400679 if (pch_iir & SDE_AUDIO_POWER_MASK_CPT)
680 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
681 (pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
682 SDE_AUDIO_POWER_SHIFT_CPT);
683
684 if (pch_iir & SDE_AUX_MASK_CPT)
Daniel Vetterce99c252012-12-01 13:53:47 +0100685 dp_aux_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -0400686
687 if (pch_iir & SDE_GMBUS_CPT)
Daniel Vetter515ac2b2012-12-01 13:53:44 +0100688 gmbus_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -0400689
690 if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
691 DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
692
693 if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
694 DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
695
696 if (pch_iir & SDE_FDI_MASK_CPT)
697 for_each_pipe(pipe)
698 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
699 pipe_name(pipe),
700 I915_READ(FDI_RX_IIR(pipe)));
701}
702
Daniel Vetterff1f5252012-10-02 15:10:55 +0200703static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700704{
705 struct drm_device *dev = (struct drm_device *) arg;
706 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Paulo Zanoni44498ae2013-02-22 17:05:28 -0300707 u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
Chris Wilson0e434062012-05-09 21:45:44 +0100708 irqreturn_t ret = IRQ_NONE;
709 int i;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700710
711 atomic_inc(&dev_priv->irq_received);
712
713 /* disable master interrupt before clearing iir */
714 de_ier = I915_READ(DEIER);
715 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Chris Wilson0e434062012-05-09 21:45:44 +0100716
Paulo Zanoni44498ae2013-02-22 17:05:28 -0300717 /* Disable south interrupts. We'll only write to SDEIIR once, so further
718 * interrupts will will be stored on its back queue, and then we'll be
719 * able to process them after we restore SDEIER (as soon as we restore
720 * it, we'll get an interrupt if SDEIIR still has something to process
721 * due to its back queue). */
722 sde_ier = I915_READ(SDEIER);
723 I915_WRITE(SDEIER, 0);
724 POSTING_READ(SDEIER);
725
Chris Wilson0e434062012-05-09 21:45:44 +0100726 gt_iir = I915_READ(GTIIR);
727 if (gt_iir) {
728 snb_gt_irq_handler(dev, dev_priv, gt_iir);
729 I915_WRITE(GTIIR, gt_iir);
730 ret = IRQ_HANDLED;
731 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700732
733 de_iir = I915_READ(DEIIR);
Chris Wilson0e434062012-05-09 21:45:44 +0100734 if (de_iir) {
Daniel Vetterce99c252012-12-01 13:53:47 +0100735 if (de_iir & DE_AUX_CHANNEL_A_IVB)
736 dp_aux_irq_handler(dev);
737
Chris Wilson0e434062012-05-09 21:45:44 +0100738 if (de_iir & DE_GSE_IVB)
739 intel_opregion_gse_intr(dev);
740
741 for (i = 0; i < 3; i++) {
Daniel Vetter74d44442012-10-02 17:54:35 +0200742 if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
743 drm_handle_vblank(dev, i);
Chris Wilson0e434062012-05-09 21:45:44 +0100744 if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
745 intel_prepare_page_flip(dev, i);
746 intel_finish_page_flip_plane(dev, i);
747 }
Chris Wilson0e434062012-05-09 21:45:44 +0100748 }
749
750 /* check event from PCH */
751 if (de_iir & DE_PCH_EVENT_IVB) {
752 u32 pch_iir = I915_READ(SDEIIR);
753
Adam Jackson23e81d62012-06-06 15:45:44 -0400754 cpt_irq_handler(dev, pch_iir);
Chris Wilson0e434062012-05-09 21:45:44 +0100755
756 /* clear PCH hotplug event before clear CPU irq */
757 I915_WRITE(SDEIIR, pch_iir);
758 }
759
760 I915_WRITE(DEIIR, de_iir);
761 ret = IRQ_HANDLED;
762 }
763
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700764 pm_iir = I915_READ(GEN6_PMIIR);
Chris Wilson0e434062012-05-09 21:45:44 +0100765 if (pm_iir) {
766 if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
767 gen6_queue_rps_work(dev_priv, pm_iir);
768 I915_WRITE(GEN6_PMIIR, pm_iir);
769 ret = IRQ_HANDLED;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700770 }
771
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700772 I915_WRITE(DEIER, de_ier);
773 POSTING_READ(DEIER);
Paulo Zanoni44498ae2013-02-22 17:05:28 -0300774 I915_WRITE(SDEIER, sde_ier);
775 POSTING_READ(SDEIER);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700776
777 return ret;
778}
779
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200780static void ilk_gt_irq_handler(struct drm_device *dev,
781 struct drm_i915_private *dev_priv,
782 u32 gt_iir)
783{
784 if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
785 notify_ring(dev, &dev_priv->ring[RCS]);
786 if (gt_iir & GT_BSD_USER_INTERRUPT)
787 notify_ring(dev, &dev_priv->ring[VCS]);
788}
789
Daniel Vetterff1f5252012-10-02 15:10:55 +0200790static irqreturn_t ironlake_irq_handler(int irq, void *arg)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800791{
Jesse Barnes46979952011-04-07 13:53:55 -0700792 struct drm_device *dev = (struct drm_device *) arg;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800793 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
794 int ret = IRQ_NONE;
Paulo Zanoni44498ae2013-02-22 17:05:28 -0300795 u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100796
Jesse Barnes46979952011-04-07 13:53:55 -0700797 atomic_inc(&dev_priv->irq_received);
798
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000799 /* disable master interrupt before clearing iir */
800 de_ier = I915_READ(DEIER);
801 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000802 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000803
Paulo Zanoni44498ae2013-02-22 17:05:28 -0300804 /* Disable south interrupts. We'll only write to SDEIIR once, so further
805 * interrupts will will be stored on its back queue, and then we'll be
806 * able to process them after we restore SDEIER (as soon as we restore
807 * it, we'll get an interrupt if SDEIIR still has something to process
808 * due to its back queue). */
809 sde_ier = I915_READ(SDEIER);
810 I915_WRITE(SDEIER, 0);
811 POSTING_READ(SDEIER);
812
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800813 de_iir = I915_READ(DEIIR);
814 gt_iir = I915_READ(GTIIR);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800815 pm_iir = I915_READ(GEN6_PMIIR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800816
Daniel Vetteracd15b62012-11-30 11:24:50 +0100817 if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
Zou Nan haic7c85102010-01-15 10:29:06 +0800818 goto done;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800819
Zou Nan haic7c85102010-01-15 10:29:06 +0800820 ret = IRQ_HANDLED;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800821
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200822 if (IS_GEN5(dev))
823 ilk_gt_irq_handler(dev, dev_priv, gt_iir);
824 else
825 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Zou Nan haic7c85102010-01-15 10:29:06 +0800826
Daniel Vetterce99c252012-12-01 13:53:47 +0100827 if (de_iir & DE_AUX_CHANNEL_A)
828 dp_aux_irq_handler(dev);
829
Zou Nan haic7c85102010-01-15 10:29:06 +0800830 if (de_iir & DE_GSE)
Chris Wilson3b617962010-08-24 09:02:58 +0100831 intel_opregion_gse_intr(dev);
Zou Nan haic7c85102010-01-15 10:29:06 +0800832
Daniel Vetter74d44442012-10-02 17:54:35 +0200833 if (de_iir & DE_PIPEA_VBLANK)
834 drm_handle_vblank(dev, 0);
835
836 if (de_iir & DE_PIPEB_VBLANK)
837 drm_handle_vblank(dev, 1);
838
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800839 if (de_iir & DE_PLANEA_FLIP_DONE) {
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800840 intel_prepare_page_flip(dev, 0);
Chris Wilson2bbda382010-09-02 17:59:39 +0100841 intel_finish_page_flip_plane(dev, 0);
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800842 }
843
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800844 if (de_iir & DE_PLANEB_FLIP_DONE) {
845 intel_prepare_page_flip(dev, 1);
Chris Wilson2bbda382010-09-02 17:59:39 +0100846 intel_finish_page_flip_plane(dev, 1);
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800847 }
Li Pengc062df62010-01-23 00:12:58 +0800848
Zou Nan haic7c85102010-01-15 10:29:06 +0800849 /* check event from PCH */
Jesse Barnes776ad802011-01-04 15:09:39 -0800850 if (de_iir & DE_PCH_EVENT) {
Daniel Vetteracd15b62012-11-30 11:24:50 +0100851 u32 pch_iir = I915_READ(SDEIIR);
852
Adam Jackson23e81d62012-06-06 15:45:44 -0400853 if (HAS_PCH_CPT(dev))
854 cpt_irq_handler(dev, pch_iir);
855 else
856 ibx_irq_handler(dev, pch_iir);
Daniel Vetteracd15b62012-11-30 11:24:50 +0100857
858 /* should clear PCH hotplug event before clear CPU irq */
859 I915_WRITE(SDEIIR, pch_iir);
Jesse Barnes776ad802011-01-04 15:09:39 -0800860 }
Zou Nan haic7c85102010-01-15 10:29:06 +0800861
Daniel Vetter73edd18f2012-08-08 23:35:37 +0200862 if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
863 ironlake_handle_rps_change(dev);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800864
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100865 if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
866 gen6_queue_rps_work(dev_priv, pm_iir);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800867
Zou Nan haic7c85102010-01-15 10:29:06 +0800868 I915_WRITE(GTIIR, gt_iir);
869 I915_WRITE(DEIIR, de_iir);
Ben Widawsky4912d042011-04-25 11:25:20 -0700870 I915_WRITE(GEN6_PMIIR, pm_iir);
Zou Nan haic7c85102010-01-15 10:29:06 +0800871
872done:
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000873 I915_WRITE(DEIER, de_ier);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000874 POSTING_READ(DEIER);
Paulo Zanoni44498ae2013-02-22 17:05:28 -0300875 I915_WRITE(SDEIER, sde_ier);
876 POSTING_READ(SDEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000877
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800878 return ret;
879}
880
Jesse Barnes8a905232009-07-11 16:48:03 -0400881/**
882 * i915_error_work_func - do process context error handling work
883 * @work: work struct
884 *
885 * Fire an error uevent so userspace can see that a hang or error
886 * was detected.
887 */
888static void i915_error_work_func(struct work_struct *work)
889{
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100890 struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
891 work);
892 drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
893 gpu_error);
Jesse Barnes8a905232009-07-11 16:48:03 -0400894 struct drm_device *dev = dev_priv->dev;
Daniel Vetterf69061b2012-12-06 09:01:42 +0100895 struct intel_ring_buffer *ring;
Ben Gamarif316a422009-09-14 17:48:46 -0400896 char *error_event[] = { "ERROR=1", NULL };
897 char *reset_event[] = { "RESET=1", NULL };
898 char *reset_done_event[] = { "ERROR=0", NULL };
Daniel Vetterf69061b2012-12-06 09:01:42 +0100899 int i, ret;
Jesse Barnes8a905232009-07-11 16:48:03 -0400900
Ben Gamarif316a422009-09-14 17:48:46 -0400901 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -0400902
Daniel Vetter7db0ba22012-12-06 16:23:37 +0100903 /*
904 * Note that there's only one work item which does gpu resets, so we
905 * need not worry about concurrent gpu resets potentially incrementing
906 * error->reset_counter twice. We only need to take care of another
907 * racing irq/hangcheck declaring the gpu dead for a second time. A
908 * quick check for that is good enough: schedule_work ensures the
909 * correct ordering between hang detection and this work item, and since
910 * the reset in-progress bit is only ever set by code outside of this
911 * work we don't need to worry about any other races.
912 */
913 if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +0100914 DRM_DEBUG_DRIVER("resetting chip\n");
Daniel Vetter7db0ba22012-12-06 16:23:37 +0100915 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
916 reset_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100917
Daniel Vetterf69061b2012-12-06 09:01:42 +0100918 ret = i915_reset(dev);
919
920 if (ret == 0) {
921 /*
922 * After all the gem state is reset, increment the reset
923 * counter and wake up everyone waiting for the reset to
924 * complete.
925 *
926 * Since unlock operations are a one-sided barrier only,
927 * we need to insert a barrier here to order any seqno
928 * updates before
929 * the counter increment.
930 */
931 smp_mb__before_atomic_inc();
932 atomic_inc(&dev_priv->gpu_error.reset_counter);
933
934 kobject_uevent_env(&dev->primary->kdev.kobj,
935 KOBJ_CHANGE, reset_done_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100936 } else {
937 atomic_set(&error->reset_counter, I915_WEDGED);
Ben Gamarif316a422009-09-14 17:48:46 -0400938 }
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100939
Daniel Vetterf69061b2012-12-06 09:01:42 +0100940 for_each_ring(ring, dev_priv, i)
941 wake_up_all(&ring->irq_queue);
942
Ville Syrjälä96a02912013-02-18 19:08:49 +0200943 intel_display_handle_reset(dev);
944
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100945 wake_up_all(&dev_priv->gpu_error.reset_queue);
Ben Gamarif316a422009-09-14 17:48:46 -0400946 }
Jesse Barnes8a905232009-07-11 16:48:03 -0400947}
948
Daniel Vetter85f9e502012-08-31 21:42:26 +0200949/* NB: please notice the memset */
950static void i915_get_extra_instdone(struct drm_device *dev,
951 uint32_t *instdone)
952{
953 struct drm_i915_private *dev_priv = dev->dev_private;
954 memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
955
956 switch(INTEL_INFO(dev)->gen) {
957 case 2:
958 case 3:
959 instdone[0] = I915_READ(INSTDONE);
960 break;
961 case 4:
962 case 5:
963 case 6:
964 instdone[0] = I915_READ(INSTDONE_I965);
965 instdone[1] = I915_READ(INSTDONE1);
966 break;
967 default:
968 WARN_ONCE(1, "Unsupported platform\n");
969 case 7:
970 instdone[0] = I915_READ(GEN7_INSTDONE_1);
971 instdone[1] = I915_READ(GEN7_SC_INSTDONE);
972 instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
973 instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
974 break;
975 }
976}
977
Chris Wilson3bd3c932010-08-19 08:19:30 +0100978#ifdef CONFIG_DEBUG_FS
Chris Wilson9df30792010-02-18 10:24:56 +0000979static struct drm_i915_error_object *
Ben Widawskyd0d045e2013-02-24 18:10:00 -0800980i915_error_object_create_sized(struct drm_i915_private *dev_priv,
981 struct drm_i915_gem_object *src,
982 const int num_pages)
Chris Wilson9df30792010-02-18 10:24:56 +0000983{
984 struct drm_i915_error_object *dst;
Ben Widawskyd0d045e2013-02-24 18:10:00 -0800985 int i;
Chris Wilsone56660d2010-08-07 11:01:26 +0100986 u32 reloc_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000987
Chris Wilson05394f32010-11-08 19:18:58 +0000988 if (src == NULL || src->pages == NULL)
Chris Wilson9df30792010-02-18 10:24:56 +0000989 return NULL;
990
Ben Widawskyd0d045e2013-02-24 18:10:00 -0800991 dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +0000992 if (dst == NULL)
993 return NULL;
994
Chris Wilson05394f32010-11-08 19:18:58 +0000995 reloc_offset = src->gtt_offset;
Ben Widawskyd0d045e2013-02-24 18:10:00 -0800996 for (i = 0; i < num_pages; i++) {
Andrew Morton788885a2010-05-11 14:07:05 -0700997 unsigned long flags;
Chris Wilsone56660d2010-08-07 11:01:26 +0100998 void *d;
Andrew Morton788885a2010-05-11 14:07:05 -0700999
Chris Wilsone56660d2010-08-07 11:01:26 +01001000 d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +00001001 if (d == NULL)
1002 goto unwind;
Chris Wilsone56660d2010-08-07 11:01:26 +01001003
Andrew Morton788885a2010-05-11 14:07:05 -07001004 local_irq_save(flags);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001005 if (reloc_offset < dev_priv->gtt.mappable_end &&
Daniel Vetter74898d72012-02-15 23:50:22 +01001006 src->has_global_gtt_mapping) {
Chris Wilson172975aa2011-12-14 13:57:25 +01001007 void __iomem *s;
1008
1009 /* Simply ignore tiling or any overlapping fence.
1010 * It's part of the error state, and this hopefully
1011 * captures what the GPU read.
1012 */
1013
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001014 s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
Chris Wilson172975aa2011-12-14 13:57:25 +01001015 reloc_offset);
1016 memcpy_fromio(d, s, PAGE_SIZE);
1017 io_mapping_unmap_atomic(s);
Chris Wilson960e3562012-11-15 11:32:23 +00001018 } else if (src->stolen) {
1019 unsigned long offset;
1020
1021 offset = dev_priv->mm.stolen_base;
1022 offset += src->stolen->start;
1023 offset += i << PAGE_SHIFT;
1024
Daniel Vetter1a240d42012-11-29 22:18:51 +01001025 memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
Chris Wilson172975aa2011-12-14 13:57:25 +01001026 } else {
Chris Wilson9da3da62012-06-01 15:20:22 +01001027 struct page *page;
Chris Wilson172975aa2011-12-14 13:57:25 +01001028 void *s;
1029
Chris Wilson9da3da62012-06-01 15:20:22 +01001030 page = i915_gem_object_get_page(src, i);
Chris Wilson172975aa2011-12-14 13:57:25 +01001031
Chris Wilson9da3da62012-06-01 15:20:22 +01001032 drm_clflush_pages(&page, 1);
1033
1034 s = kmap_atomic(page);
Chris Wilson172975aa2011-12-14 13:57:25 +01001035 memcpy(d, s, PAGE_SIZE);
1036 kunmap_atomic(s);
1037
Chris Wilson9da3da62012-06-01 15:20:22 +01001038 drm_clflush_pages(&page, 1);
Chris Wilson172975aa2011-12-14 13:57:25 +01001039 }
Andrew Morton788885a2010-05-11 14:07:05 -07001040 local_irq_restore(flags);
Chris Wilsone56660d2010-08-07 11:01:26 +01001041
Chris Wilson9da3da62012-06-01 15:20:22 +01001042 dst->pages[i] = d;
Chris Wilsone56660d2010-08-07 11:01:26 +01001043
1044 reloc_offset += PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +00001045 }
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001046 dst->page_count = num_pages;
Chris Wilson05394f32010-11-08 19:18:58 +00001047 dst->gtt_offset = src->gtt_offset;
Chris Wilson9df30792010-02-18 10:24:56 +00001048
1049 return dst;
1050
1051unwind:
Chris Wilson9da3da62012-06-01 15:20:22 +01001052 while (i--)
1053 kfree(dst->pages[i]);
Chris Wilson9df30792010-02-18 10:24:56 +00001054 kfree(dst);
1055 return NULL;
1056}
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001057#define i915_error_object_create(dev_priv, src) \
1058 i915_error_object_create_sized((dev_priv), (src), \
1059 (src)->base.size>>PAGE_SHIFT)
Chris Wilson9df30792010-02-18 10:24:56 +00001060
1061static void
1062i915_error_object_free(struct drm_i915_error_object *obj)
1063{
1064 int page;
1065
1066 if (obj == NULL)
1067 return;
1068
1069 for (page = 0; page < obj->page_count; page++)
1070 kfree(obj->pages[page]);
1071
1072 kfree(obj);
1073}
1074
Daniel Vetter742cbee2012-04-27 15:17:39 +02001075void
1076i915_error_state_free(struct kref *error_ref)
Chris Wilson9df30792010-02-18 10:24:56 +00001077{
Daniel Vetter742cbee2012-04-27 15:17:39 +02001078 struct drm_i915_error_state *error = container_of(error_ref,
1079 typeof(*error), ref);
Chris Wilsone2f973d2011-01-27 19:15:11 +00001080 int i;
1081
Chris Wilson52d39a22012-02-15 11:25:37 +00001082 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
1083 i915_error_object_free(error->ring[i].batchbuffer);
1084 i915_error_object_free(error->ring[i].ringbuffer);
1085 kfree(error->ring[i].requests);
1086 }
Chris Wilsone2f973d2011-01-27 19:15:11 +00001087
Chris Wilson9df30792010-02-18 10:24:56 +00001088 kfree(error->active_bo);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001089 kfree(error->overlay);
Chris Wilson9df30792010-02-18 10:24:56 +00001090 kfree(error);
1091}
Chris Wilson1b502472012-04-24 15:47:30 +01001092static void capture_bo(struct drm_i915_error_buffer *err,
1093 struct drm_i915_gem_object *obj)
1094{
1095 err->size = obj->base.size;
1096 err->name = obj->base.name;
Chris Wilson0201f1e2012-07-20 12:41:01 +01001097 err->rseqno = obj->last_read_seqno;
1098 err->wseqno = obj->last_write_seqno;
Chris Wilson1b502472012-04-24 15:47:30 +01001099 err->gtt_offset = obj->gtt_offset;
1100 err->read_domains = obj->base.read_domains;
1101 err->write_domain = obj->base.write_domain;
1102 err->fence_reg = obj->fence_reg;
1103 err->pinned = 0;
1104 if (obj->pin_count > 0)
1105 err->pinned = 1;
1106 if (obj->user_pin_count > 0)
1107 err->pinned = -1;
1108 err->tiling = obj->tiling_mode;
1109 err->dirty = obj->dirty;
1110 err->purgeable = obj->madv != I915_MADV_WILLNEED;
1111 err->ring = obj->ring ? obj->ring->id : -1;
1112 err->cache_level = obj->cache_level;
1113}
Chris Wilson9df30792010-02-18 10:24:56 +00001114
Chris Wilson1b502472012-04-24 15:47:30 +01001115static u32 capture_active_bo(struct drm_i915_error_buffer *err,
1116 int count, struct list_head *head)
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001117{
1118 struct drm_i915_gem_object *obj;
1119 int i = 0;
1120
1121 list_for_each_entry(obj, head, mm_list) {
Chris Wilson1b502472012-04-24 15:47:30 +01001122 capture_bo(err++, obj);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001123 if (++i == count)
1124 break;
Chris Wilson1b502472012-04-24 15:47:30 +01001125 }
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001126
Chris Wilson1b502472012-04-24 15:47:30 +01001127 return i;
1128}
1129
1130static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
1131 int count, struct list_head *head)
1132{
1133 struct drm_i915_gem_object *obj;
1134 int i = 0;
1135
1136 list_for_each_entry(obj, head, gtt_list) {
1137 if (obj->pin_count == 0)
1138 continue;
1139
1140 capture_bo(err++, obj);
1141 if (++i == count)
1142 break;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001143 }
1144
1145 return i;
1146}
1147
Chris Wilson748ebc62010-10-24 10:28:47 +01001148static void i915_gem_record_fences(struct drm_device *dev,
1149 struct drm_i915_error_state *error)
1150{
1151 struct drm_i915_private *dev_priv = dev->dev_private;
1152 int i;
1153
1154 /* Fences */
1155 switch (INTEL_INFO(dev)->gen) {
Daniel Vetter775d17b2011-10-09 21:52:01 +02001156 case 7:
Chris Wilson748ebc62010-10-24 10:28:47 +01001157 case 6:
1158 for (i = 0; i < 16; i++)
1159 error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1160 break;
1161 case 5:
1162 case 4:
1163 for (i = 0; i < 16; i++)
1164 error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1165 break;
1166 case 3:
1167 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1168 for (i = 0; i < 8; i++)
1169 error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1170 case 2:
1171 for (i = 0; i < 8; i++)
1172 error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1173 break;
1174
Ben Widawsky7dbf9d62012-12-18 10:31:22 -08001175 default:
1176 BUG();
Chris Wilson748ebc62010-10-24 10:28:47 +01001177 }
1178}
1179
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001180static struct drm_i915_error_object *
1181i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1182 struct intel_ring_buffer *ring)
1183{
1184 struct drm_i915_gem_object *obj;
1185 u32 seqno;
1186
1187 if (!ring->get_seqno)
1188 return NULL;
1189
Daniel Vetterb45305f2012-12-17 16:21:27 +01001190 if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1191 u32 acthd = I915_READ(ACTHD);
1192
1193 if (WARN_ON(ring->id != RCS))
1194 return NULL;
1195
1196 obj = ring->private;
1197 if (acthd >= obj->gtt_offset &&
1198 acthd < obj->gtt_offset + obj->base.size)
1199 return i915_error_object_create(dev_priv, obj);
1200 }
1201
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01001202 seqno = ring->get_seqno(ring, false);
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001203 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1204 if (obj->ring != ring)
1205 continue;
1206
Chris Wilson0201f1e2012-07-20 12:41:01 +01001207 if (i915_seqno_passed(seqno, obj->last_read_seqno))
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001208 continue;
1209
1210 if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1211 continue;
1212
1213 /* We need to copy these to an anonymous buffer as the simplest
1214 * method to avoid being overwritten by userspace.
1215 */
1216 return i915_error_object_create(dev_priv, obj);
1217 }
1218
1219 return NULL;
1220}
1221
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001222static void i915_record_ring_state(struct drm_device *dev,
1223 struct drm_i915_error_state *error,
1224 struct intel_ring_buffer *ring)
1225{
1226 struct drm_i915_private *dev_priv = dev->dev_private;
1227
Daniel Vetter33f3f512011-12-14 13:57:39 +01001228 if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilson12f55812012-07-05 17:14:01 +01001229 error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
Daniel Vetter33f3f512011-12-14 13:57:39 +01001230 error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
Daniel Vetter7e3b8732012-02-01 22:26:45 +01001231 error->semaphore_mboxes[ring->id][0]
1232 = I915_READ(RING_SYNC_0(ring->mmio_base));
1233 error->semaphore_mboxes[ring->id][1]
1234 = I915_READ(RING_SYNC_1(ring->mmio_base));
Chris Wilsondf2b23d2012-11-27 17:06:54 +00001235 error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1236 error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
Daniel Vetter33f3f512011-12-14 13:57:39 +01001237 }
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001238
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001239 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter9d2f41f2012-04-02 21:41:45 +02001240 error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001241 error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1242 error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1243 error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001244 error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
Ben Widawsky050ee912012-08-22 11:32:15 -07001245 if (ring->id == RCS)
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001246 error->bbaddr = I915_READ64(BB_ADDR);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001247 } else {
Daniel Vetter9d2f41f2012-04-02 21:41:45 +02001248 error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001249 error->ipeir[ring->id] = I915_READ(IPEIR);
1250 error->ipehr[ring->id] = I915_READ(IPEHR);
1251 error->instdone[ring->id] = I915_READ(INSTDONE);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001252 }
1253
Ben Widawsky9574b3f2012-04-26 16:03:01 -07001254 error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001255 error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01001256 error->seqno[ring->id] = ring->get_seqno(ring, false);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001257 error->acthd[ring->id] = intel_ring_get_active_head(ring);
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001258 error->head[ring->id] = I915_READ_HEAD(ring);
1259 error->tail[ring->id] = I915_READ_TAIL(ring);
Chris Wilson0f3b6842013-01-15 12:05:55 +00001260 error->ctl[ring->id] = I915_READ_CTL(ring);
Daniel Vetter7e3b8732012-02-01 22:26:45 +01001261
1262 error->cpu_ring_head[ring->id] = ring->head;
1263 error->cpu_ring_tail[ring->id] = ring->tail;
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001264}
1265
Ben Widawsky8c123e52013-03-04 17:00:29 -08001266
1267static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
1268 struct drm_i915_error_state *error,
1269 struct drm_i915_error_ring *ering)
1270{
1271 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1272 struct drm_i915_gem_object *obj;
1273
1274 /* Currently render ring is the only HW context user */
1275 if (ring->id != RCS || !error->ccid)
1276 return;
1277
1278 list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) {
1279 if ((error->ccid & PAGE_MASK) == obj->gtt_offset) {
1280 ering->ctx = i915_error_object_create_sized(dev_priv,
1281 obj, 1);
1282 }
1283 }
1284}
1285
Chris Wilson52d39a22012-02-15 11:25:37 +00001286static void i915_gem_record_rings(struct drm_device *dev,
1287 struct drm_i915_error_state *error)
1288{
1289 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01001290 struct intel_ring_buffer *ring;
Chris Wilson52d39a22012-02-15 11:25:37 +00001291 struct drm_i915_gem_request *request;
1292 int i, count;
1293
Chris Wilsonb4519512012-05-11 14:29:30 +01001294 for_each_ring(ring, dev_priv, i) {
Chris Wilson52d39a22012-02-15 11:25:37 +00001295 i915_record_ring_state(dev, error, ring);
1296
1297 error->ring[i].batchbuffer =
1298 i915_error_first_batchbuffer(dev_priv, ring);
1299
1300 error->ring[i].ringbuffer =
1301 i915_error_object_create(dev_priv, ring->obj);
1302
Ben Widawsky8c123e52013-03-04 17:00:29 -08001303
1304 i915_gem_record_active_context(ring, error, &error->ring[i]);
1305
Chris Wilson52d39a22012-02-15 11:25:37 +00001306 count = 0;
1307 list_for_each_entry(request, &ring->request_list, list)
1308 count++;
1309
1310 error->ring[i].num_requests = count;
1311 error->ring[i].requests =
1312 kmalloc(count*sizeof(struct drm_i915_error_request),
1313 GFP_ATOMIC);
1314 if (error->ring[i].requests == NULL) {
1315 error->ring[i].num_requests = 0;
1316 continue;
1317 }
1318
1319 count = 0;
1320 list_for_each_entry(request, &ring->request_list, list) {
1321 struct drm_i915_error_request *erq;
1322
1323 erq = &error->ring[i].requests[count++];
1324 erq->seqno = request->seqno;
1325 erq->jiffies = request->emitted_jiffies;
Chris Wilsonee4f42b2012-02-15 11:25:38 +00001326 erq->tail = request->tail;
Chris Wilson52d39a22012-02-15 11:25:37 +00001327 }
1328 }
1329}
1330
Jesse Barnes8a905232009-07-11 16:48:03 -04001331/**
1332 * i915_capture_error_state - capture an error record for later analysis
1333 * @dev: drm device
1334 *
1335 * Should be called when an error is detected (either a hang or an error
1336 * interrupt) to capture error state from the time of the error. Fills
1337 * out a structure which becomes available in debugfs for user level tools
1338 * to pick up.
1339 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001340static void i915_capture_error_state(struct drm_device *dev)
1341{
1342 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001343 struct drm_i915_gem_object *obj;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001344 struct drm_i915_error_state *error;
1345 unsigned long flags;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001346 int i, pipe;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001347
Daniel Vetter99584db2012-11-14 17:14:04 +01001348 spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
1349 error = dev_priv->gpu_error.first_error;
1350 spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001351 if (error)
1352 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001353
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001354 /* Account for pipe specific data like PIPE*STAT */
Daniel Vetter33f3f512011-12-14 13:57:39 +01001355 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001356 if (!error) {
Chris Wilson9df30792010-02-18 10:24:56 +00001357 DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
1358 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001359 }
1360
Paulo Zanoni5d83d292013-03-06 20:03:22 -03001361 DRM_INFO("capturing error event; look for more information in "
Ben Widawsky2f86f192013-01-28 15:32:15 -08001362 "/sys/kernel/debug/dri/%d/i915_error_state\n",
Chris Wilsonb6f78332011-02-01 14:15:55 +00001363 dev->primary->index);
Chris Wilson2fa772f2010-10-01 13:23:27 +01001364
Daniel Vetter742cbee2012-04-27 15:17:39 +02001365 kref_init(&error->ref);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001366 error->eir = I915_READ(EIR);
1367 error->pgtbl_er = I915_READ(PGTBL_ER);
Ben Widawsky211816e2013-02-24 18:10:01 -08001368 if (HAS_HW_CONTEXTS(dev))
1369 error->ccid = I915_READ(CCID);
Ben Widawskybe998e22012-04-26 16:03:00 -07001370
1371 if (HAS_PCH_SPLIT(dev))
1372 error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1373 else if (IS_VALLEYVIEW(dev))
1374 error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1375 else if (IS_GEN2(dev))
1376 error->ier = I915_READ16(IER);
1377 else
1378 error->ier = I915_READ(IER);
1379
Chris Wilson0f3b6842013-01-15 12:05:55 +00001380 if (INTEL_INFO(dev)->gen >= 6)
1381 error->derrmr = I915_READ(DERRMR);
1382
1383 if (IS_VALLEYVIEW(dev))
1384 error->forcewake = I915_READ(FORCEWAKE_VLV);
1385 else if (INTEL_INFO(dev)->gen >= 7)
1386 error->forcewake = I915_READ(FORCEWAKE_MT);
1387 else if (INTEL_INFO(dev)->gen == 6)
1388 error->forcewake = I915_READ(FORCEWAKE);
1389
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001390 for_each_pipe(pipe)
1391 error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001392
Daniel Vetter33f3f512011-12-14 13:57:39 +01001393 if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilsonf4068392010-10-27 20:36:41 +01001394 error->error = I915_READ(ERROR_GEN6);
Daniel Vetter33f3f512011-12-14 13:57:39 +01001395 error->done_reg = I915_READ(DONE_REG);
1396 }
Chris Wilsonadd354d2010-10-29 19:00:51 +01001397
Ben Widawsky71e172e2012-08-20 16:15:13 -07001398 if (INTEL_INFO(dev)->gen == 7)
1399 error->err_int = I915_READ(GEN7_ERR_INT);
1400
Ben Widawsky050ee912012-08-22 11:32:15 -07001401 i915_get_extra_instdone(dev, error->extra_instdone);
1402
Chris Wilson748ebc62010-10-24 10:28:47 +01001403 i915_gem_record_fences(dev, error);
Chris Wilson52d39a22012-02-15 11:25:37 +00001404 i915_gem_record_rings(dev, error);
Chris Wilson9df30792010-02-18 10:24:56 +00001405
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001406 /* Record buffers on the active and pinned lists. */
Chris Wilson9df30792010-02-18 10:24:56 +00001407 error->active_bo = NULL;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001408 error->pinned_bo = NULL;
Chris Wilson9df30792010-02-18 10:24:56 +00001409
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001410 i = 0;
1411 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1412 i++;
1413 error->active_bo_count = i;
Chris Wilson6c085a72012-08-20 11:40:46 +02001414 list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
Chris Wilson1b502472012-04-24 15:47:30 +01001415 if (obj->pin_count)
1416 i++;
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001417 error->pinned_bo_count = i - error->active_bo_count;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001418
Chris Wilson8e934db2011-01-24 12:34:00 +00001419 error->active_bo = NULL;
1420 error->pinned_bo = NULL;
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001421 if (i) {
1422 error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
Chris Wilson9df30792010-02-18 10:24:56 +00001423 GFP_ATOMIC);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001424 if (error->active_bo)
1425 error->pinned_bo =
1426 error->active_bo + error->active_bo_count;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001427 }
1428
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001429 if (error->active_bo)
1430 error->active_bo_count =
Chris Wilson1b502472012-04-24 15:47:30 +01001431 capture_active_bo(error->active_bo,
1432 error->active_bo_count,
1433 &dev_priv->mm.active_list);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001434
1435 if (error->pinned_bo)
1436 error->pinned_bo_count =
Chris Wilson1b502472012-04-24 15:47:30 +01001437 capture_pinned_bo(error->pinned_bo,
1438 error->pinned_bo_count,
Chris Wilson6c085a72012-08-20 11:40:46 +02001439 &dev_priv->mm.bound_list);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001440
Jesse Barnes8a905232009-07-11 16:48:03 -04001441 do_gettimeofday(&error->time);
1442
Chris Wilson6ef3d422010-08-04 20:26:07 +01001443 error->overlay = intel_overlay_capture_error_state(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00001444 error->display = intel_display_capture_error_state(dev);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001445
Daniel Vetter99584db2012-11-14 17:14:04 +01001446 spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
1447 if (dev_priv->gpu_error.first_error == NULL) {
1448 dev_priv->gpu_error.first_error = error;
Chris Wilson9df30792010-02-18 10:24:56 +00001449 error = NULL;
1450 }
Daniel Vetter99584db2012-11-14 17:14:04 +01001451 spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001452
1453 if (error)
Daniel Vetter742cbee2012-04-27 15:17:39 +02001454 i915_error_state_free(&error->ref);
Chris Wilson9df30792010-02-18 10:24:56 +00001455}
1456
1457void i915_destroy_error_state(struct drm_device *dev)
1458{
1459 struct drm_i915_private *dev_priv = dev->dev_private;
1460 struct drm_i915_error_state *error;
Ben Widawsky6dc0e812012-01-23 15:30:02 -08001461 unsigned long flags;
Chris Wilson9df30792010-02-18 10:24:56 +00001462
Daniel Vetter99584db2012-11-14 17:14:04 +01001463 spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
1464 error = dev_priv->gpu_error.first_error;
1465 dev_priv->gpu_error.first_error = NULL;
1466 spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001467
1468 if (error)
Daniel Vetter742cbee2012-04-27 15:17:39 +02001469 kref_put(&error->ref, i915_error_state_free);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001470}
Chris Wilson3bd3c932010-08-19 08:19:30 +01001471#else
1472#define i915_capture_error_state(x)
1473#endif
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001474
Chris Wilson35aed2e2010-05-27 13:18:12 +01001475static void i915_report_and_clear_eir(struct drm_device *dev)
Jesse Barnes8a905232009-07-11 16:48:03 -04001476{
1477 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybd9854f2012-08-23 15:18:09 -07001478 uint32_t instdone[I915_NUM_INSTDONE_REG];
Jesse Barnes8a905232009-07-11 16:48:03 -04001479 u32 eir = I915_READ(EIR);
Ben Widawsky050ee912012-08-22 11:32:15 -07001480 int pipe, i;
Jesse Barnes8a905232009-07-11 16:48:03 -04001481
Chris Wilson35aed2e2010-05-27 13:18:12 +01001482 if (!eir)
1483 return;
Jesse Barnes8a905232009-07-11 16:48:03 -04001484
Joe Perchesa70491c2012-03-18 13:00:11 -07001485 pr_err("render error detected, EIR: 0x%08x\n", eir);
Jesse Barnes8a905232009-07-11 16:48:03 -04001486
Ben Widawskybd9854f2012-08-23 15:18:09 -07001487 i915_get_extra_instdone(dev, instdone);
1488
Jesse Barnes8a905232009-07-11 16:48:03 -04001489 if (IS_G4X(dev)) {
1490 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
1491 u32 ipeir = I915_READ(IPEIR_I965);
1492
Joe Perchesa70491c2012-03-18 13:00:11 -07001493 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1494 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Ben Widawsky050ee912012-08-22 11:32:15 -07001495 for (i = 0; i < ARRAY_SIZE(instdone); i++)
1496 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Joe Perchesa70491c2012-03-18 13:00:11 -07001497 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07001498 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04001499 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001500 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001501 }
1502 if (eir & GM45_ERROR_PAGE_TABLE) {
1503 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07001504 pr_err("page table error\n");
1505 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04001506 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001507 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04001508 }
1509 }
1510
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001511 if (!IS_GEN2(dev)) {
Jesse Barnes8a905232009-07-11 16:48:03 -04001512 if (eir & I915_ERROR_PAGE_TABLE) {
1513 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07001514 pr_err("page table error\n");
1515 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04001516 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001517 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04001518 }
1519 }
1520
1521 if (eir & I915_ERROR_MEMORY_REFRESH) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001522 pr_err("memory refresh error:\n");
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001523 for_each_pipe(pipe)
Joe Perchesa70491c2012-03-18 13:00:11 -07001524 pr_err("pipe %c stat: 0x%08x\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001525 pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
Jesse Barnes8a905232009-07-11 16:48:03 -04001526 /* pipestat has already been acked */
1527 }
1528 if (eir & I915_ERROR_INSTRUCTION) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001529 pr_err("instruction error\n");
1530 pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
Ben Widawsky050ee912012-08-22 11:32:15 -07001531 for (i = 0; i < ARRAY_SIZE(instdone); i++)
1532 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001533 if (INTEL_INFO(dev)->gen < 4) {
Jesse Barnes8a905232009-07-11 16:48:03 -04001534 u32 ipeir = I915_READ(IPEIR);
1535
Joe Perchesa70491c2012-03-18 13:00:11 -07001536 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
1537 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
Joe Perchesa70491c2012-03-18 13:00:11 -07001538 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
Jesse Barnes8a905232009-07-11 16:48:03 -04001539 I915_WRITE(IPEIR, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001540 POSTING_READ(IPEIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001541 } else {
1542 u32 ipeir = I915_READ(IPEIR_I965);
1543
Joe Perchesa70491c2012-03-18 13:00:11 -07001544 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1545 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07001546 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07001547 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04001548 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001549 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001550 }
1551 }
1552
1553 I915_WRITE(EIR, eir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001554 POSTING_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001555 eir = I915_READ(EIR);
1556 if (eir) {
1557 /*
1558 * some errors might have become stuck,
1559 * mask them.
1560 */
1561 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
1562 I915_WRITE(EMR, I915_READ(EMR) | eir);
1563 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
1564 }
Chris Wilson35aed2e2010-05-27 13:18:12 +01001565}
1566
1567/**
1568 * i915_handle_error - handle an error interrupt
1569 * @dev: drm device
1570 *
1571 * Do some basic checking of regsiter state at error interrupt time and
1572 * dump it to the syslog. Also call i915_capture_error_state() to make
1573 * sure we get a record and make it available in debugfs. Fire a uevent
1574 * so userspace knows something bad happened (should trigger collection
1575 * of a ring dump etc.).
1576 */
Chris Wilson527f9e92010-11-11 01:16:58 +00001577void i915_handle_error(struct drm_device *dev, bool wedged)
Chris Wilson35aed2e2010-05-27 13:18:12 +01001578{
1579 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01001580 struct intel_ring_buffer *ring;
1581 int i;
Chris Wilson35aed2e2010-05-27 13:18:12 +01001582
1583 i915_capture_error_state(dev);
1584 i915_report_and_clear_eir(dev);
Jesse Barnes8a905232009-07-11 16:48:03 -04001585
Ben Gamariba1234d2009-09-14 17:48:47 -04001586 if (wedged) {
Daniel Vetterf69061b2012-12-06 09:01:42 +01001587 atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1588 &dev_priv->gpu_error.reset_counter);
Ben Gamariba1234d2009-09-14 17:48:47 -04001589
Ben Gamari11ed50e2009-09-14 17:48:45 -04001590 /*
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001591 * Wakeup waiting processes so that the reset work item
1592 * doesn't deadlock trying to grab various locks.
Ben Gamari11ed50e2009-09-14 17:48:45 -04001593 */
Chris Wilsonb4519512012-05-11 14:29:30 +01001594 for_each_ring(ring, dev_priv, i)
1595 wake_up_all(&ring->irq_queue);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001596 }
1597
Daniel Vetter99584db2012-11-14 17:14:04 +01001598 queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
Jesse Barnes8a905232009-07-11 16:48:03 -04001599}
1600
Ville Syrjälä21ad8332013-02-19 15:16:39 +02001601static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001602{
1603 drm_i915_private_t *dev_priv = dev->dev_private;
1604 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1605 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00001606 struct drm_i915_gem_object *obj;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001607 struct intel_unpin_work *work;
1608 unsigned long flags;
1609 bool stall_detected;
1610
1611 /* Ignore early vblank irqs */
1612 if (intel_crtc == NULL)
1613 return;
1614
1615 spin_lock_irqsave(&dev->event_lock, flags);
1616 work = intel_crtc->unpin_work;
1617
Chris Wilsone7d841c2012-12-03 11:36:30 +00001618 if (work == NULL ||
1619 atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1620 !work->enable_stall_check) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001621 /* Either the pending flip IRQ arrived, or we're too early. Don't check */
1622 spin_unlock_irqrestore(&dev->event_lock, flags);
1623 return;
1624 }
1625
1626 /* Potential stall - if we see that the flip has happened, assume a missed interrupt */
Chris Wilson05394f32010-11-08 19:18:58 +00001627 obj = work->pending_flip_obj;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001628 if (INTEL_INFO(dev)->gen >= 4) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001629 int dspsurf = DSPSURF(intel_crtc->plane);
Armin Reese446f2542012-03-30 16:20:16 -07001630 stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1631 obj->gtt_offset;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001632 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001633 int dspaddr = DSPADDR(intel_crtc->plane);
Chris Wilson05394f32010-11-08 19:18:58 +00001634 stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001635 crtc->y * crtc->fb->pitches[0] +
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001636 crtc->x * crtc->fb->bits_per_pixel/8);
1637 }
1638
1639 spin_unlock_irqrestore(&dev->event_lock, flags);
1640
1641 if (stall_detected) {
1642 DRM_DEBUG_DRIVER("Pageflip stall detected\n");
1643 intel_prepare_page_flip(dev, intel_crtc->plane);
1644 }
1645}
1646
Keith Packard42f52ef2008-10-18 19:39:29 -07001647/* Called from drm generic code, passed 'crtc' which
1648 * we use as a pipe index
1649 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001650static int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001651{
1652 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001653 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001654
Chris Wilson5eddb702010-09-11 13:48:45 +01001655 if (!i915_pipe_enabled(dev, pipe))
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001656 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001657
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001658 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07001659 if (INTEL_INFO(dev)->gen >= 4)
Keith Packard7c463582008-11-04 02:03:27 -08001660 i915_enable_pipestat(dev_priv, pipe,
1661 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -07001662 else
Keith Packard7c463582008-11-04 02:03:27 -08001663 i915_enable_pipestat(dev_priv, pipe,
1664 PIPE_VBLANK_INTERRUPT_ENABLE);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001665
1666 /* maintain vblank delivery even in deep C-states */
1667 if (dev_priv->info->gen == 3)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001668 I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001669 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001670
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001671 return 0;
1672}
1673
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001674static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07001675{
1676 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1677 unsigned long irqflags;
1678
1679 if (!i915_pipe_enabled(dev, pipe))
1680 return -EINVAL;
1681
1682 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1683 ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
Akshay Joshi0206e352011-08-16 15:34:10 -04001684 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
Jesse Barnesf796cf82011-04-07 13:58:17 -07001685 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1686
1687 return 0;
1688}
1689
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001690static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001691{
1692 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1693 unsigned long irqflags;
1694
1695 if (!i915_pipe_enabled(dev, pipe))
1696 return -EINVAL;
1697
1698 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilsonb615b572012-05-02 09:52:12 +01001699 ironlake_enable_display_irq(dev_priv,
1700 DE_PIPEA_VBLANK_IVB << (5 * pipe));
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001701 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1702
1703 return 0;
1704}
1705
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001706static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
1707{
1708 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1709 unsigned long irqflags;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001710 u32 imr;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001711
1712 if (!i915_pipe_enabled(dev, pipe))
1713 return -EINVAL;
1714
1715 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001716 imr = I915_READ(VLV_IMR);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001717 if (pipe == 0)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001718 imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001719 else
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001720 imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001721 I915_WRITE(VLV_IMR, imr);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001722 i915_enable_pipestat(dev_priv, pipe,
1723 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001724 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1725
1726 return 0;
1727}
1728
Keith Packard42f52ef2008-10-18 19:39:29 -07001729/* Called from drm generic code, passed 'crtc' which
1730 * we use as a pipe index
1731 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001732static void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001733{
1734 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001735 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001736
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001737 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001738 if (dev_priv->info->gen == 3)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001739 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
Chris Wilson8692d00e2011-02-05 10:08:21 +00001740
Jesse Barnesf796cf82011-04-07 13:58:17 -07001741 i915_disable_pipestat(dev_priv, pipe,
1742 PIPE_VBLANK_INTERRUPT_ENABLE |
1743 PIPE_START_VBLANK_INTERRUPT_ENABLE);
1744 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1745}
1746
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001747static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07001748{
1749 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1750 unsigned long irqflags;
1751
1752 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1753 ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
Akshay Joshi0206e352011-08-16 15:34:10 -04001754 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001755 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001756}
1757
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001758static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001759{
1760 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1761 unsigned long irqflags;
1762
1763 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilsonb615b572012-05-02 09:52:12 +01001764 ironlake_disable_display_irq(dev_priv,
1765 DE_PIPEA_VBLANK_IVB << (pipe * 5));
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001766 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1767}
1768
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001769static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
1770{
1771 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1772 unsigned long irqflags;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001773 u32 imr;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001774
1775 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001776 i915_disable_pipestat(dev_priv, pipe,
1777 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001778 imr = I915_READ(VLV_IMR);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001779 if (pipe == 0)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001780 imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001781 else
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001782 imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001783 I915_WRITE(VLV_IMR, imr);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001784 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1785}
1786
Chris Wilson893eead2010-10-27 14:44:35 +01001787static u32
1788ring_last_seqno(struct intel_ring_buffer *ring)
Zou Nan hai852835f2010-05-21 09:08:56 +08001789{
Chris Wilson893eead2010-10-27 14:44:35 +01001790 return list_entry(ring->request_list.prev,
1791 struct drm_i915_gem_request, list)->seqno;
1792}
1793
1794static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1795{
1796 if (list_empty(&ring->request_list) ||
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01001797 i915_seqno_passed(ring->get_seqno(ring, false),
1798 ring_last_seqno(ring))) {
Chris Wilson893eead2010-10-27 14:44:35 +01001799 /* Issue a wake-up to catch stuck h/w. */
Ben Widawsky9574b3f2012-04-26 16:03:01 -07001800 if (waitqueue_active(&ring->irq_queue)) {
1801 DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
1802 ring->name);
Chris Wilson893eead2010-10-27 14:44:35 +01001803 wake_up_all(&ring->irq_queue);
1804 *err = true;
1805 }
1806 return true;
1807 }
1808 return false;
Ben Gamarif65d9422009-09-14 17:48:44 -04001809}
1810
Chris Wilsona24a11e2013-03-14 17:52:05 +02001811static bool semaphore_passed(struct intel_ring_buffer *ring)
1812{
1813 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1814 u32 acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
1815 struct intel_ring_buffer *signaller;
1816 u32 cmd, ipehr, acthd_min;
1817
1818 ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
1819 if ((ipehr & ~(0x3 << 16)) !=
1820 (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
1821 return false;
1822
1823 /* ACTHD is likely pointing to the dword after the actual command,
1824 * so scan backwards until we find the MBOX.
1825 */
1826 acthd_min = max((int)acthd - 3 * 4, 0);
1827 do {
1828 cmd = ioread32(ring->virtual_start + acthd);
1829 if (cmd == ipehr)
1830 break;
1831
1832 acthd -= 4;
1833 if (acthd < acthd_min)
1834 return false;
1835 } while (1);
1836
1837 signaller = &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
1838 return i915_seqno_passed(signaller->get_seqno(signaller, false),
1839 ioread32(ring->virtual_start+acthd+4)+1);
1840}
1841
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001842static bool kick_ring(struct intel_ring_buffer *ring)
1843{
1844 struct drm_device *dev = ring->dev;
1845 struct drm_i915_private *dev_priv = dev->dev_private;
1846 u32 tmp = I915_READ_CTL(ring);
1847 if (tmp & RING_WAIT) {
1848 DRM_ERROR("Kicking stuck wait on %s\n",
1849 ring->name);
1850 I915_WRITE_CTL(ring, tmp);
1851 return true;
1852 }
Chris Wilsona24a11e2013-03-14 17:52:05 +02001853
1854 if (INTEL_INFO(dev)->gen >= 6 &&
1855 tmp & RING_WAIT_SEMAPHORE &&
1856 semaphore_passed(ring)) {
1857 DRM_ERROR("Kicking stuck semaphore on %s\n",
1858 ring->name);
1859 I915_WRITE_CTL(ring, tmp);
1860 return true;
1861 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001862 return false;
1863}
1864
Chris Wilsond1e61e72012-04-10 17:00:41 +01001865static bool i915_hangcheck_hung(struct drm_device *dev)
1866{
1867 drm_i915_private_t *dev_priv = dev->dev_private;
1868
Daniel Vetter99584db2012-11-14 17:14:04 +01001869 if (dev_priv->gpu_error.hangcheck_count++ > 1) {
Chris Wilsonb4519512012-05-11 14:29:30 +01001870 bool hung = true;
1871
Chris Wilsond1e61e72012-04-10 17:00:41 +01001872 DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
1873 i915_handle_error(dev, true);
1874
1875 if (!IS_GEN2(dev)) {
Chris Wilsonb4519512012-05-11 14:29:30 +01001876 struct intel_ring_buffer *ring;
1877 int i;
1878
Chris Wilsond1e61e72012-04-10 17:00:41 +01001879 /* Is the chip hanging on a WAIT_FOR_EVENT?
1880 * If so we can simply poke the RB_WAIT bit
1881 * and break the hang. This should work on
1882 * all but the second generation chipsets.
1883 */
Chris Wilsonb4519512012-05-11 14:29:30 +01001884 for_each_ring(ring, dev_priv, i)
1885 hung &= !kick_ring(ring);
Chris Wilsond1e61e72012-04-10 17:00:41 +01001886 }
1887
Chris Wilsonb4519512012-05-11 14:29:30 +01001888 return hung;
Chris Wilsond1e61e72012-04-10 17:00:41 +01001889 }
1890
1891 return false;
1892}
1893
Ben Gamarif65d9422009-09-14 17:48:44 -04001894/**
1895 * This is called when the chip hasn't reported back with completed
1896 * batchbuffers in a long time. The first time this is called we simply record
1897 * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1898 * again, we assume the chip is wedged and try to fix it.
1899 */
1900void i915_hangcheck_elapsed(unsigned long data)
1901{
1902 struct drm_device *dev = (struct drm_device *)data;
1903 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawskybd9854f2012-08-23 15:18:09 -07001904 uint32_t acthd[I915_NUM_RINGS], instdone[I915_NUM_INSTDONE_REG];
Chris Wilsonb4519512012-05-11 14:29:30 +01001905 struct intel_ring_buffer *ring;
1906 bool err = false, idle;
1907 int i;
Chris Wilson893eead2010-10-27 14:44:35 +01001908
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001909 if (!i915_enable_hangcheck)
1910 return;
1911
Chris Wilsonb4519512012-05-11 14:29:30 +01001912 memset(acthd, 0, sizeof(acthd));
1913 idle = true;
1914 for_each_ring(ring, dev_priv, i) {
1915 idle &= i915_hangcheck_ring_idle(ring, &err);
1916 acthd[i] = intel_ring_get_active_head(ring);
1917 }
1918
Chris Wilson893eead2010-10-27 14:44:35 +01001919 /* If all work is done then ACTHD clearly hasn't advanced. */
Chris Wilsonb4519512012-05-11 14:29:30 +01001920 if (idle) {
Chris Wilsond1e61e72012-04-10 17:00:41 +01001921 if (err) {
1922 if (i915_hangcheck_hung(dev))
1923 return;
1924
Chris Wilson893eead2010-10-27 14:44:35 +01001925 goto repeat;
Chris Wilsond1e61e72012-04-10 17:00:41 +01001926 }
1927
Daniel Vetter99584db2012-11-14 17:14:04 +01001928 dev_priv->gpu_error.hangcheck_count = 0;
Chris Wilson893eead2010-10-27 14:44:35 +01001929 return;
1930 }
Eric Anholtb9201c12010-01-08 14:25:16 -08001931
Ben Widawskybd9854f2012-08-23 15:18:09 -07001932 i915_get_extra_instdone(dev, instdone);
Daniel Vetter99584db2012-11-14 17:14:04 +01001933 if (memcmp(dev_priv->gpu_error.last_acthd, acthd,
1934 sizeof(acthd)) == 0 &&
1935 memcmp(dev_priv->gpu_error.prev_instdone, instdone,
1936 sizeof(instdone)) == 0) {
Chris Wilsond1e61e72012-04-10 17:00:41 +01001937 if (i915_hangcheck_hung(dev))
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001938 return;
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001939 } else {
Daniel Vetter99584db2012-11-14 17:14:04 +01001940 dev_priv->gpu_error.hangcheck_count = 0;
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001941
Daniel Vetter99584db2012-11-14 17:14:04 +01001942 memcpy(dev_priv->gpu_error.last_acthd, acthd,
1943 sizeof(acthd));
1944 memcpy(dev_priv->gpu_error.prev_instdone, instdone,
1945 sizeof(instdone));
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001946 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001947
Chris Wilson893eead2010-10-27 14:44:35 +01001948repeat:
Ben Gamarif65d9422009-09-14 17:48:44 -04001949 /* Reset timer case chip hangs without another request being added */
Daniel Vetter99584db2012-11-14 17:14:04 +01001950 mod_timer(&dev_priv->gpu_error.hangcheck_timer,
Chris Wilsoncecc21f2012-10-05 17:02:56 +01001951 round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
Ben Gamarif65d9422009-09-14 17:48:44 -04001952}
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954/* drm_dma.h hooks
1955*/
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001956static void ironlake_irq_preinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001957{
1958 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1959
Jesse Barnes46979952011-04-07 13:53:55 -07001960 atomic_set(&dev_priv->irq_received, 0);
1961
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001962 I915_WRITE(HWSTAM, 0xeffe);
Daniel Vetterbdfcdb62012-01-05 01:05:26 +01001963
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001964 /* XXX hotplug from PCH */
1965
1966 I915_WRITE(DEIMR, 0xffffffff);
1967 I915_WRITE(DEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001968 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001969
1970 /* and GT */
1971 I915_WRITE(GTIMR, 0xffffffff);
1972 I915_WRITE(GTIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001973 POSTING_READ(GTIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00001974
1975 /* south display irq */
1976 I915_WRITE(SDEIMR, 0xffffffff);
1977 I915_WRITE(SDEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001978 POSTING_READ(SDEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001979}
1980
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001981static void valleyview_irq_preinstall(struct drm_device *dev)
1982{
1983 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1984 int pipe;
1985
1986 atomic_set(&dev_priv->irq_received, 0);
1987
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001988 /* VLV magic */
1989 I915_WRITE(VLV_IMR, 0);
1990 I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
1991 I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
1992 I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
1993
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001994 /* and GT */
1995 I915_WRITE(GTIIR, I915_READ(GTIIR));
1996 I915_WRITE(GTIIR, I915_READ(GTIIR));
1997 I915_WRITE(GTIMR, 0xffffffff);
1998 I915_WRITE(GTIER, 0x0);
1999 POSTING_READ(GTIER);
2000
2001 I915_WRITE(DPINVGTT, 0xff);
2002
2003 I915_WRITE(PORT_HOTPLUG_EN, 0);
2004 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2005 for_each_pipe(pipe)
2006 I915_WRITE(PIPESTAT(pipe), 0xffff);
2007 I915_WRITE(VLV_IIR, 0xffffffff);
2008 I915_WRITE(VLV_IMR, 0xffffffff);
2009 I915_WRITE(VLV_IER, 0x0);
2010 POSTING_READ(VLV_IER);
2011}
2012
Keith Packard7fe0b972011-09-19 13:31:02 -07002013/*
2014 * Enable digital hotplug on the PCH, and configure the DP short pulse
2015 * duration to 2ms (which is the minimum in the Display Port spec)
2016 *
2017 * This register is the same on all known PCH chips.
2018 */
2019
Paulo Zanonid46da432013-02-08 17:35:15 -02002020static void ibx_enable_hotplug(struct drm_device *dev)
Keith Packard7fe0b972011-09-19 13:31:02 -07002021{
2022 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2023 u32 hotplug;
2024
2025 hotplug = I915_READ(PCH_PORT_HOTPLUG);
2026 hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
2027 hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
2028 hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
2029 hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
2030 I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
2031}
2032
Paulo Zanonid46da432013-02-08 17:35:15 -02002033static void ibx_irq_postinstall(struct drm_device *dev)
2034{
2035 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2036 u32 mask;
2037
2038 if (HAS_PCH_IBX(dev))
2039 mask = SDE_HOTPLUG_MASK |
2040 SDE_GMBUS |
2041 SDE_AUX_MASK;
2042 else
2043 mask = SDE_HOTPLUG_MASK_CPT |
2044 SDE_GMBUS_CPT |
2045 SDE_AUX_MASK_CPT;
2046
2047 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2048 I915_WRITE(SDEIMR, ~mask);
2049 I915_WRITE(SDEIER, mask);
2050 POSTING_READ(SDEIER);
2051
2052 ibx_enable_hotplug(dev);
2053}
2054
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002055static int ironlake_irq_postinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002056{
2057 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2058 /* enable kind of interrupts always enabled */
Jesse Barnes013d5aa2010-01-29 11:18:31 -08002059 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
Daniel Vetterce99c252012-12-01 13:53:47 +01002060 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
2061 DE_AUX_CHANNEL_A;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002062 u32 render_irqs;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002063
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002064 dev_priv->irq_mask = ~display_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002065
2066 /* should always can generate irq */
2067 I915_WRITE(DEIIR, I915_READ(DEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002068 I915_WRITE(DEIMR, dev_priv->irq_mask);
2069 I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002070 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002071
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002072 dev_priv->gt_irq_mask = ~0;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002073
2074 I915_WRITE(GTIIR, I915_READ(GTIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002075 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002076
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002077 if (IS_GEN6(dev))
2078 render_irqs =
2079 GT_USER_INTERRUPT |
Ben Widawskye2a1e2f2012-03-29 19:11:26 -07002080 GEN6_BSD_USER_INTERRUPT |
2081 GEN6_BLITTER_USER_INTERRUPT;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002082 else
2083 render_irqs =
Chris Wilson88f23b82010-12-05 15:08:31 +00002084 GT_USER_INTERRUPT |
Chris Wilsonc6df5412010-12-15 09:56:50 +00002085 GT_PIPE_NOTIFY |
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002086 GT_BSD_USER_INTERRUPT;
2087 I915_WRITE(GTIER, render_irqs);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002088 POSTING_READ(GTIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002089
Paulo Zanonid46da432013-02-08 17:35:15 -02002090 ibx_irq_postinstall(dev);
Keith Packard7fe0b972011-09-19 13:31:02 -07002091
Jesse Barnesf97108d2010-01-29 11:27:07 -08002092 if (IS_IRONLAKE_M(dev)) {
2093 /* Clear & enable PCU event interrupts */
2094 I915_WRITE(DEIIR, DE_PCU_EVENT);
2095 I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
2096 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2097 }
2098
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002099 return 0;
2100}
2101
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002102static int ivybridge_irq_postinstall(struct drm_device *dev)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002103{
2104 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2105 /* enable kind of interrupts always enabled */
Chris Wilsonb615b572012-05-02 09:52:12 +01002106 u32 display_mask =
2107 DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2108 DE_PLANEC_FLIP_DONE_IVB |
2109 DE_PLANEB_FLIP_DONE_IVB |
Daniel Vetterce99c252012-12-01 13:53:47 +01002110 DE_PLANEA_FLIP_DONE_IVB |
2111 DE_AUX_CHANNEL_A_IVB;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002112 u32 render_irqs;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002113
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002114 dev_priv->irq_mask = ~display_mask;
2115
2116 /* should always can generate irq */
2117 I915_WRITE(DEIIR, I915_READ(DEIIR));
2118 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilsonb615b572012-05-02 09:52:12 +01002119 I915_WRITE(DEIER,
2120 display_mask |
2121 DE_PIPEC_VBLANK_IVB |
2122 DE_PIPEB_VBLANK_IVB |
2123 DE_PIPEA_VBLANK_IVB);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002124 POSTING_READ(DEIER);
2125
Ben Widawsky15b9f802012-05-25 16:56:23 -07002126 dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002127
2128 I915_WRITE(GTIIR, I915_READ(GTIIR));
2129 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2130
Ben Widawskye2a1e2f2012-03-29 19:11:26 -07002131 render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
Ben Widawsky15b9f802012-05-25 16:56:23 -07002132 GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002133 I915_WRITE(GTIER, render_irqs);
2134 POSTING_READ(GTIER);
2135
Paulo Zanonid46da432013-02-08 17:35:15 -02002136 ibx_irq_postinstall(dev);
Keith Packard7fe0b972011-09-19 13:31:02 -07002137
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002138 return 0;
2139}
2140
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002141static int valleyview_irq_postinstall(struct drm_device *dev)
2142{
2143 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002144 u32 enable_mask;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002145 u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
Jesse Barnes3bcedbe2012-09-19 13:29:01 -07002146 u32 render_irqs;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002147 u16 msid;
2148
2149 enable_mask = I915_DISPLAY_PORT_INTERRUPT;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002150 enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2151 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
2152 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002153 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
2154
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002155 /*
2156 *Leave vblank interrupts masked initially. enable/disable will
2157 * toggle them based on usage.
2158 */
2159 dev_priv->irq_mask = (~enable_mask) |
2160 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
2161 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002162
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002163 /* Hack for broken MSIs on VLV */
2164 pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
2165 pci_read_config_word(dev->pdev, 0x98, &msid);
2166 msid &= 0xff; /* mask out delivery bits */
2167 msid |= (1<<14);
2168 pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
2169
Daniel Vetter20afbda2012-12-11 14:05:07 +01002170 I915_WRITE(PORT_HOTPLUG_EN, 0);
2171 POSTING_READ(PORT_HOTPLUG_EN);
2172
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002173 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
2174 I915_WRITE(VLV_IER, enable_mask);
2175 I915_WRITE(VLV_IIR, 0xffffffff);
2176 I915_WRITE(PIPESTAT(0), 0xffff);
2177 I915_WRITE(PIPESTAT(1), 0xffff);
2178 POSTING_READ(VLV_IER);
2179
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002180 i915_enable_pipestat(dev_priv, 0, pipestat_enable);
Daniel Vetter515ac2b2012-12-01 13:53:44 +01002181 i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002182 i915_enable_pipestat(dev_priv, 1, pipestat_enable);
2183
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002184 I915_WRITE(VLV_IIR, 0xffffffff);
2185 I915_WRITE(VLV_IIR, 0xffffffff);
2186
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002187 I915_WRITE(GTIIR, I915_READ(GTIIR));
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002188 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Jesse Barnes3bcedbe2012-09-19 13:29:01 -07002189
2190 render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
2191 GEN6_BLITTER_USER_INTERRUPT;
2192 I915_WRITE(GTIER, render_irqs);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002193 POSTING_READ(GTIER);
2194
2195 /* ack & enable invalid PTE error interrupts */
2196#if 0 /* FIXME: add support to irq handler for checking these bits */
2197 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
2198 I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
2199#endif
2200
2201 I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
Daniel Vetter20afbda2012-12-11 14:05:07 +01002202
2203 return 0;
2204}
2205
2206static void valleyview_hpd_irq_setup(struct drm_device *dev)
2207{
2208 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2209 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2210
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002211 /* Note HDMI and DP share bits */
Daniel Vetter26739f12013-02-07 12:42:32 +01002212 if (dev_priv->hotplug_supported_mask & PORTB_HOTPLUG_INT_STATUS)
2213 hotplug_en |= PORTB_HOTPLUG_INT_EN;
2214 if (dev_priv->hotplug_supported_mask & PORTC_HOTPLUG_INT_STATUS)
2215 hotplug_en |= PORTC_HOTPLUG_INT_EN;
2216 if (dev_priv->hotplug_supported_mask & PORTD_HOTPLUG_INT_STATUS)
2217 hotplug_en |= PORTD_HOTPLUG_INT_EN;
Vijay Purushothamanae33cdcf2012-09-27 19:13:02 +05302218 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002219 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
Vijay Purushothamanae33cdcf2012-09-27 19:13:02 +05302220 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002221 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2222 if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2223 hotplug_en |= CRT_HOTPLUG_INT_EN;
2224 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2225 }
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002226
2227 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002228}
2229
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002230static void valleyview_irq_uninstall(struct drm_device *dev)
2231{
2232 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2233 int pipe;
2234
2235 if (!dev_priv)
2236 return;
2237
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002238 for_each_pipe(pipe)
2239 I915_WRITE(PIPESTAT(pipe), 0xffff);
2240
2241 I915_WRITE(HWSTAM, 0xffffffff);
2242 I915_WRITE(PORT_HOTPLUG_EN, 0);
2243 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2244 for_each_pipe(pipe)
2245 I915_WRITE(PIPESTAT(pipe), 0xffff);
2246 I915_WRITE(VLV_IIR, 0xffffffff);
2247 I915_WRITE(VLV_IMR, 0xffffffff);
2248 I915_WRITE(VLV_IER, 0x0);
2249 POSTING_READ(VLV_IER);
2250}
2251
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002252static void ironlake_irq_uninstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002253{
2254 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes46979952011-04-07 13:53:55 -07002255
2256 if (!dev_priv)
2257 return;
2258
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002259 I915_WRITE(HWSTAM, 0xffffffff);
2260
2261 I915_WRITE(DEIMR, 0xffffffff);
2262 I915_WRITE(DEIER, 0x0);
2263 I915_WRITE(DEIIR, I915_READ(DEIIR));
2264
2265 I915_WRITE(GTIMR, 0xffffffff);
2266 I915_WRITE(GTIER, 0x0);
2267 I915_WRITE(GTIIR, I915_READ(GTIIR));
Keith Packard192aac1f2011-09-20 10:12:44 -07002268
2269 I915_WRITE(SDEIMR, 0xffffffff);
2270 I915_WRITE(SDEIER, 0x0);
2271 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002272}
2273
Chris Wilsonc2798b12012-04-22 21:13:57 +01002274static void i8xx_irq_preinstall(struct drm_device * dev)
2275{
2276 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2277 int pipe;
2278
2279 atomic_set(&dev_priv->irq_received, 0);
2280
2281 for_each_pipe(pipe)
2282 I915_WRITE(PIPESTAT(pipe), 0);
2283 I915_WRITE16(IMR, 0xffff);
2284 I915_WRITE16(IER, 0x0);
2285 POSTING_READ16(IER);
2286}
2287
2288static int i8xx_irq_postinstall(struct drm_device *dev)
2289{
2290 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2291
Chris Wilsonc2798b12012-04-22 21:13:57 +01002292 I915_WRITE16(EMR,
2293 ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2294
2295 /* Unmask the interrupts that we always want on. */
2296 dev_priv->irq_mask =
2297 ~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2298 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2299 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2300 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2301 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2302 I915_WRITE16(IMR, dev_priv->irq_mask);
2303
2304 I915_WRITE16(IER,
2305 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2306 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2307 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2308 I915_USER_INTERRUPT);
2309 POSTING_READ16(IER);
2310
2311 return 0;
2312}
2313
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002314/*
2315 * Returns true when a page flip has completed.
2316 */
2317static bool i8xx_handle_vblank(struct drm_device *dev,
2318 int pipe, u16 iir)
2319{
2320 drm_i915_private_t *dev_priv = dev->dev_private;
2321 u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
2322
2323 if (!drm_handle_vblank(dev, pipe))
2324 return false;
2325
2326 if ((iir & flip_pending) == 0)
2327 return false;
2328
2329 intel_prepare_page_flip(dev, pipe);
2330
2331 /* We detect FlipDone by looking for the change in PendingFlip from '1'
2332 * to '0' on the following vblank, i.e. IIR has the Pendingflip
2333 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
2334 * the flip is completed (no longer pending). Since this doesn't raise
2335 * an interrupt per se, we watch for the change at vblank.
2336 */
2337 if (I915_READ16(ISR) & flip_pending)
2338 return false;
2339
2340 intel_finish_page_flip(dev, pipe);
2341
2342 return true;
2343}
2344
Daniel Vetterff1f5252012-10-02 15:10:55 +02002345static irqreturn_t i8xx_irq_handler(int irq, void *arg)
Chris Wilsonc2798b12012-04-22 21:13:57 +01002346{
2347 struct drm_device *dev = (struct drm_device *) arg;
2348 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002349 u16 iir, new_iir;
2350 u32 pipe_stats[2];
2351 unsigned long irqflags;
2352 int irq_received;
2353 int pipe;
2354 u16 flip_mask =
2355 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2356 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2357
2358 atomic_inc(&dev_priv->irq_received);
2359
2360 iir = I915_READ16(IIR);
2361 if (iir == 0)
2362 return IRQ_NONE;
2363
2364 while (iir & ~flip_mask) {
2365 /* Can't rely on pipestat interrupt bit in iir as it might
2366 * have been cleared after the pipestat interrupt was received.
2367 * It doesn't set the bit in iir again, but it still produces
2368 * interrupts (for non-MSI).
2369 */
2370 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2371 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2372 i915_handle_error(dev, false);
2373
2374 for_each_pipe(pipe) {
2375 int reg = PIPESTAT(pipe);
2376 pipe_stats[pipe] = I915_READ(reg);
2377
2378 /*
2379 * Clear the PIPE*STAT regs before the IIR
2380 */
2381 if (pipe_stats[pipe] & 0x8000ffff) {
2382 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2383 DRM_DEBUG_DRIVER("pipe %c underrun\n",
2384 pipe_name(pipe));
2385 I915_WRITE(reg, pipe_stats[pipe]);
2386 irq_received = 1;
2387 }
2388 }
2389 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2390
2391 I915_WRITE16(IIR, iir & ~flip_mask);
2392 new_iir = I915_READ16(IIR); /* Flush posted writes */
2393
Daniel Vetterd05c6172012-04-26 23:28:09 +02002394 i915_update_dri1_breadcrumb(dev);
Chris Wilsonc2798b12012-04-22 21:13:57 +01002395
2396 if (iir & I915_USER_INTERRUPT)
2397 notify_ring(dev, &dev_priv->ring[RCS]);
2398
2399 if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002400 i8xx_handle_vblank(dev, 0, iir))
2401 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
Chris Wilsonc2798b12012-04-22 21:13:57 +01002402
2403 if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002404 i8xx_handle_vblank(dev, 1, iir))
2405 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
Chris Wilsonc2798b12012-04-22 21:13:57 +01002406
2407 iir = new_iir;
2408 }
2409
2410 return IRQ_HANDLED;
2411}
2412
2413static void i8xx_irq_uninstall(struct drm_device * dev)
2414{
2415 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2416 int pipe;
2417
Chris Wilsonc2798b12012-04-22 21:13:57 +01002418 for_each_pipe(pipe) {
2419 /* Clear enable bits; then clear status bits */
2420 I915_WRITE(PIPESTAT(pipe), 0);
2421 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2422 }
2423 I915_WRITE16(IMR, 0xffff);
2424 I915_WRITE16(IER, 0x0);
2425 I915_WRITE16(IIR, I915_READ16(IIR));
2426}
2427
Chris Wilsona266c7d2012-04-24 22:59:44 +01002428static void i915_irq_preinstall(struct drm_device * dev)
2429{
2430 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2431 int pipe;
2432
2433 atomic_set(&dev_priv->irq_received, 0);
2434
2435 if (I915_HAS_HOTPLUG(dev)) {
2436 I915_WRITE(PORT_HOTPLUG_EN, 0);
2437 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2438 }
2439
Chris Wilson00d98eb2012-04-24 22:59:48 +01002440 I915_WRITE16(HWSTAM, 0xeffe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002441 for_each_pipe(pipe)
2442 I915_WRITE(PIPESTAT(pipe), 0);
2443 I915_WRITE(IMR, 0xffffffff);
2444 I915_WRITE(IER, 0x0);
2445 POSTING_READ(IER);
2446}
2447
2448static int i915_irq_postinstall(struct drm_device *dev)
2449{
2450 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson38bde182012-04-24 22:59:50 +01002451 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002452
Chris Wilson38bde182012-04-24 22:59:50 +01002453 I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2454
2455 /* Unmask the interrupts that we always want on. */
2456 dev_priv->irq_mask =
2457 ~(I915_ASLE_INTERRUPT |
2458 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2459 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2460 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2461 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2462 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2463
2464 enable_mask =
2465 I915_ASLE_INTERRUPT |
2466 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2467 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2468 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2469 I915_USER_INTERRUPT;
2470
Chris Wilsona266c7d2012-04-24 22:59:44 +01002471 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetter20afbda2012-12-11 14:05:07 +01002472 I915_WRITE(PORT_HOTPLUG_EN, 0);
2473 POSTING_READ(PORT_HOTPLUG_EN);
2474
Chris Wilsona266c7d2012-04-24 22:59:44 +01002475 /* Enable in IER... */
2476 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2477 /* and unmask in IMR */
2478 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2479 }
2480
Chris Wilsona266c7d2012-04-24 22:59:44 +01002481 I915_WRITE(IMR, dev_priv->irq_mask);
2482 I915_WRITE(IER, enable_mask);
2483 POSTING_READ(IER);
2484
Daniel Vetter20afbda2012-12-11 14:05:07 +01002485 intel_opregion_enable_asle(dev);
2486
2487 return 0;
2488}
2489
2490static void i915_hpd_irq_setup(struct drm_device *dev)
2491{
2492 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2493 u32 hotplug_en;
2494
Chris Wilsona266c7d2012-04-24 22:59:44 +01002495 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetter20afbda2012-12-11 14:05:07 +01002496 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002497
Daniel Vetter26739f12013-02-07 12:42:32 +01002498 if (dev_priv->hotplug_supported_mask & PORTB_HOTPLUG_INT_STATUS)
2499 hotplug_en |= PORTB_HOTPLUG_INT_EN;
2500 if (dev_priv->hotplug_supported_mask & PORTC_HOTPLUG_INT_STATUS)
2501 hotplug_en |= PORTC_HOTPLUG_INT_EN;
2502 if (dev_priv->hotplug_supported_mask & PORTD_HOTPLUG_INT_STATUS)
2503 hotplug_en |= PORTD_HOTPLUG_INT_EN;
Chris Wilson084b6122012-05-11 18:01:33 +01002504 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I915)
Chris Wilsona266c7d2012-04-24 22:59:44 +01002505 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
Chris Wilson084b6122012-05-11 18:01:33 +01002506 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I915)
Chris Wilsona266c7d2012-04-24 22:59:44 +01002507 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2508 if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2509 hotplug_en |= CRT_HOTPLUG_INT_EN;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002510 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2511 }
2512
2513 /* Ignore TV since it's buggy */
2514
2515 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2516 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01002517}
2518
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002519/*
2520 * Returns true when a page flip has completed.
2521 */
2522static bool i915_handle_vblank(struct drm_device *dev,
2523 int plane, int pipe, u32 iir)
2524{
2525 drm_i915_private_t *dev_priv = dev->dev_private;
2526 u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
2527
2528 if (!drm_handle_vblank(dev, pipe))
2529 return false;
2530
2531 if ((iir & flip_pending) == 0)
2532 return false;
2533
2534 intel_prepare_page_flip(dev, plane);
2535
2536 /* We detect FlipDone by looking for the change in PendingFlip from '1'
2537 * to '0' on the following vblank, i.e. IIR has the Pendingflip
2538 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
2539 * the flip is completed (no longer pending). Since this doesn't raise
2540 * an interrupt per se, we watch for the change at vblank.
2541 */
2542 if (I915_READ(ISR) & flip_pending)
2543 return false;
2544
2545 intel_finish_page_flip(dev, pipe);
2546
2547 return true;
2548}
2549
Daniel Vetterff1f5252012-10-02 15:10:55 +02002550static irqreturn_t i915_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01002551{
2552 struct drm_device *dev = (struct drm_device *) arg;
2553 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson8291ee92012-04-24 22:59:47 +01002554 u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01002555 unsigned long irqflags;
Chris Wilson38bde182012-04-24 22:59:50 +01002556 u32 flip_mask =
2557 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2558 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilson38bde182012-04-24 22:59:50 +01002559 int pipe, ret = IRQ_NONE;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002560
2561 atomic_inc(&dev_priv->irq_received);
2562
2563 iir = I915_READ(IIR);
Chris Wilson38bde182012-04-24 22:59:50 +01002564 do {
2565 bool irq_received = (iir & ~flip_mask) != 0;
Chris Wilson8291ee92012-04-24 22:59:47 +01002566 bool blc_event = false;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002567
2568 /* Can't rely on pipestat interrupt bit in iir as it might
2569 * have been cleared after the pipestat interrupt was received.
2570 * It doesn't set the bit in iir again, but it still produces
2571 * interrupts (for non-MSI).
2572 */
2573 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2574 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2575 i915_handle_error(dev, false);
2576
2577 for_each_pipe(pipe) {
2578 int reg = PIPESTAT(pipe);
2579 pipe_stats[pipe] = I915_READ(reg);
2580
Chris Wilson38bde182012-04-24 22:59:50 +01002581 /* Clear the PIPE*STAT regs before the IIR */
Chris Wilsona266c7d2012-04-24 22:59:44 +01002582 if (pipe_stats[pipe] & 0x8000ffff) {
2583 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2584 DRM_DEBUG_DRIVER("pipe %c underrun\n",
2585 pipe_name(pipe));
2586 I915_WRITE(reg, pipe_stats[pipe]);
Chris Wilson38bde182012-04-24 22:59:50 +01002587 irq_received = true;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002588 }
2589 }
2590 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2591
2592 if (!irq_received)
2593 break;
2594
Chris Wilsona266c7d2012-04-24 22:59:44 +01002595 /* Consume port. Then clear IIR or we'll miss events */
2596 if ((I915_HAS_HOTPLUG(dev)) &&
2597 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2598 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2599
2600 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2601 hotplug_status);
2602 if (hotplug_status & dev_priv->hotplug_supported_mask)
2603 queue_work(dev_priv->wq,
2604 &dev_priv->hotplug_work);
2605
2606 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
Chris Wilson38bde182012-04-24 22:59:50 +01002607 POSTING_READ(PORT_HOTPLUG_STAT);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002608 }
2609
Chris Wilson38bde182012-04-24 22:59:50 +01002610 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002611 new_iir = I915_READ(IIR); /* Flush posted writes */
2612
Chris Wilsona266c7d2012-04-24 22:59:44 +01002613 if (iir & I915_USER_INTERRUPT)
2614 notify_ring(dev, &dev_priv->ring[RCS]);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002615
Chris Wilsona266c7d2012-04-24 22:59:44 +01002616 for_each_pipe(pipe) {
Chris Wilson38bde182012-04-24 22:59:50 +01002617 int plane = pipe;
2618 if (IS_MOBILE(dev))
2619 plane = !plane;
Ville Syrjälä5e2032d2013-02-19 15:16:38 +02002620
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002621 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
2622 i915_handle_vblank(dev, plane, pipe, iir))
2623 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002624
2625 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2626 blc_event = true;
2627 }
2628
Chris Wilsona266c7d2012-04-24 22:59:44 +01002629 if (blc_event || (iir & I915_ASLE_INTERRUPT))
2630 intel_opregion_asle_intr(dev);
2631
2632 /* With MSI, interrupts are only generated when iir
2633 * transitions from zero to nonzero. If another bit got
2634 * set while we were handling the existing iir bits, then
2635 * we would never get another interrupt.
2636 *
2637 * This is fine on non-MSI as well, as if we hit this path
2638 * we avoid exiting the interrupt handler only to generate
2639 * another one.
2640 *
2641 * Note that for MSI this could cause a stray interrupt report
2642 * if an interrupt landed in the time between writing IIR and
2643 * the posting read. This should be rare enough to never
2644 * trigger the 99% of 100,000 interrupts test for disabling
2645 * stray interrupts.
2646 */
Chris Wilson38bde182012-04-24 22:59:50 +01002647 ret = IRQ_HANDLED;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002648 iir = new_iir;
Chris Wilson38bde182012-04-24 22:59:50 +01002649 } while (iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002650
Daniel Vetterd05c6172012-04-26 23:28:09 +02002651 i915_update_dri1_breadcrumb(dev);
Chris Wilson8291ee92012-04-24 22:59:47 +01002652
Chris Wilsona266c7d2012-04-24 22:59:44 +01002653 return ret;
2654}
2655
2656static void i915_irq_uninstall(struct drm_device * dev)
2657{
2658 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2659 int pipe;
2660
Chris Wilsona266c7d2012-04-24 22:59:44 +01002661 if (I915_HAS_HOTPLUG(dev)) {
2662 I915_WRITE(PORT_HOTPLUG_EN, 0);
2663 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2664 }
2665
Chris Wilson00d98eb2012-04-24 22:59:48 +01002666 I915_WRITE16(HWSTAM, 0xffff);
Chris Wilson55b39752012-04-24 22:59:49 +01002667 for_each_pipe(pipe) {
2668 /* Clear enable bits; then clear status bits */
Chris Wilsona266c7d2012-04-24 22:59:44 +01002669 I915_WRITE(PIPESTAT(pipe), 0);
Chris Wilson55b39752012-04-24 22:59:49 +01002670 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2671 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01002672 I915_WRITE(IMR, 0xffffffff);
2673 I915_WRITE(IER, 0x0);
2674
Chris Wilsona266c7d2012-04-24 22:59:44 +01002675 I915_WRITE(IIR, I915_READ(IIR));
2676}
2677
2678static void i965_irq_preinstall(struct drm_device * dev)
2679{
2680 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2681 int pipe;
2682
2683 atomic_set(&dev_priv->irq_received, 0);
2684
Chris Wilsonadca4732012-05-11 18:01:31 +01002685 I915_WRITE(PORT_HOTPLUG_EN, 0);
2686 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01002687
2688 I915_WRITE(HWSTAM, 0xeffe);
2689 for_each_pipe(pipe)
2690 I915_WRITE(PIPESTAT(pipe), 0);
2691 I915_WRITE(IMR, 0xffffffff);
2692 I915_WRITE(IER, 0x0);
2693 POSTING_READ(IER);
2694}
2695
2696static int i965_irq_postinstall(struct drm_device *dev)
2697{
2698 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsonbbba0a92012-04-24 22:59:51 +01002699 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002700 u32 error_mask;
2701
Chris Wilsona266c7d2012-04-24 22:59:44 +01002702 /* Unmask the interrupts that we always want on. */
Chris Wilsonbbba0a92012-04-24 22:59:51 +01002703 dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
Chris Wilsonadca4732012-05-11 18:01:31 +01002704 I915_DISPLAY_PORT_INTERRUPT |
Chris Wilsonbbba0a92012-04-24 22:59:51 +01002705 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2706 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2707 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2708 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2709 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2710
2711 enable_mask = ~dev_priv->irq_mask;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02002712 enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2713 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
Chris Wilsonbbba0a92012-04-24 22:59:51 +01002714 enable_mask |= I915_USER_INTERRUPT;
2715
2716 if (IS_G4X(dev))
2717 enable_mask |= I915_BSD_USER_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002718
Daniel Vetter515ac2b2012-12-01 13:53:44 +01002719 i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002720
Chris Wilsona266c7d2012-04-24 22:59:44 +01002721 /*
2722 * Enable some error detection, note the instruction error mask
2723 * bit is reserved, so we leave it masked.
2724 */
2725 if (IS_G4X(dev)) {
2726 error_mask = ~(GM45_ERROR_PAGE_TABLE |
2727 GM45_ERROR_MEM_PRIV |
2728 GM45_ERROR_CP_PRIV |
2729 I915_ERROR_MEMORY_REFRESH);
2730 } else {
2731 error_mask = ~(I915_ERROR_PAGE_TABLE |
2732 I915_ERROR_MEMORY_REFRESH);
2733 }
2734 I915_WRITE(EMR, error_mask);
2735
2736 I915_WRITE(IMR, dev_priv->irq_mask);
2737 I915_WRITE(IER, enable_mask);
2738 POSTING_READ(IER);
2739
Daniel Vetter20afbda2012-12-11 14:05:07 +01002740 I915_WRITE(PORT_HOTPLUG_EN, 0);
2741 POSTING_READ(PORT_HOTPLUG_EN);
2742
2743 intel_opregion_enable_asle(dev);
2744
2745 return 0;
2746}
2747
2748static void i965_hpd_irq_setup(struct drm_device *dev)
2749{
2750 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2751 u32 hotplug_en;
2752
Chris Wilsonadca4732012-05-11 18:01:31 +01002753 /* Note HDMI and DP share hotplug bits */
2754 hotplug_en = 0;
Daniel Vetter26739f12013-02-07 12:42:32 +01002755 if (dev_priv->hotplug_supported_mask & PORTB_HOTPLUG_INT_STATUS)
2756 hotplug_en |= PORTB_HOTPLUG_INT_EN;
2757 if (dev_priv->hotplug_supported_mask & PORTC_HOTPLUG_INT_STATUS)
2758 hotplug_en |= PORTC_HOTPLUG_INT_EN;
2759 if (dev_priv->hotplug_supported_mask & PORTD_HOTPLUG_INT_STATUS)
2760 hotplug_en |= PORTD_HOTPLUG_INT_EN;
Chris Wilson084b6122012-05-11 18:01:33 +01002761 if (IS_G4X(dev)) {
2762 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_G4X)
2763 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2764 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_G4X)
2765 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2766 } else {
2767 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS_I965)
2768 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2769 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS_I965)
2770 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2771 }
Chris Wilsonadca4732012-05-11 18:01:31 +01002772 if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2773 hotplug_en |= CRT_HOTPLUG_INT_EN;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002774
Chris Wilsonadca4732012-05-11 18:01:31 +01002775 /* Programming the CRT detection parameters tends
2776 to generate a spurious hotplug event about three
2777 seconds later. So just do it once.
2778 */
2779 if (IS_G4X(dev))
2780 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2781 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002782 }
2783
Chris Wilsonadca4732012-05-11 18:01:31 +01002784 /* Ignore TV since it's buggy */
2785
2786 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002787}
2788
Daniel Vetterff1f5252012-10-02 15:10:55 +02002789static irqreturn_t i965_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01002790{
2791 struct drm_device *dev = (struct drm_device *) arg;
2792 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002793 u32 iir, new_iir;
2794 u32 pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01002795 unsigned long irqflags;
2796 int irq_received;
2797 int ret = IRQ_NONE, pipe;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02002798 u32 flip_mask =
2799 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2800 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002801
2802 atomic_inc(&dev_priv->irq_received);
2803
2804 iir = I915_READ(IIR);
2805
Chris Wilsona266c7d2012-04-24 22:59:44 +01002806 for (;;) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01002807 bool blc_event = false;
2808
Ville Syrjälä21ad8332013-02-19 15:16:39 +02002809 irq_received = (iir & ~flip_mask) != 0;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002810
2811 /* Can't rely on pipestat interrupt bit in iir as it might
2812 * have been cleared after the pipestat interrupt was received.
2813 * It doesn't set the bit in iir again, but it still produces
2814 * interrupts (for non-MSI).
2815 */
2816 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2817 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2818 i915_handle_error(dev, false);
2819
2820 for_each_pipe(pipe) {
2821 int reg = PIPESTAT(pipe);
2822 pipe_stats[pipe] = I915_READ(reg);
2823
2824 /*
2825 * Clear the PIPE*STAT regs before the IIR
2826 */
2827 if (pipe_stats[pipe] & 0x8000ffff) {
2828 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2829 DRM_DEBUG_DRIVER("pipe %c underrun\n",
2830 pipe_name(pipe));
2831 I915_WRITE(reg, pipe_stats[pipe]);
2832 irq_received = 1;
2833 }
2834 }
2835 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2836
2837 if (!irq_received)
2838 break;
2839
2840 ret = IRQ_HANDLED;
2841
2842 /* Consume port. Then clear IIR or we'll miss events */
Chris Wilsonadca4732012-05-11 18:01:31 +01002843 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01002844 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2845
2846 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2847 hotplug_status);
2848 if (hotplug_status & dev_priv->hotplug_supported_mask)
2849 queue_work(dev_priv->wq,
2850 &dev_priv->hotplug_work);
2851
2852 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2853 I915_READ(PORT_HOTPLUG_STAT);
2854 }
2855
Ville Syrjälä21ad8332013-02-19 15:16:39 +02002856 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002857 new_iir = I915_READ(IIR); /* Flush posted writes */
2858
Chris Wilsona266c7d2012-04-24 22:59:44 +01002859 if (iir & I915_USER_INTERRUPT)
2860 notify_ring(dev, &dev_priv->ring[RCS]);
2861 if (iir & I915_BSD_USER_INTERRUPT)
2862 notify_ring(dev, &dev_priv->ring[VCS]);
2863
Chris Wilsona266c7d2012-04-24 22:59:44 +01002864 for_each_pipe(pipe) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01002865 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002866 i915_handle_vblank(dev, pipe, pipe, iir))
2867 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002868
2869 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2870 blc_event = true;
2871 }
2872
2873
2874 if (blc_event || (iir & I915_ASLE_INTERRUPT))
2875 intel_opregion_asle_intr(dev);
2876
Daniel Vetter515ac2b2012-12-01 13:53:44 +01002877 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
2878 gmbus_irq_handler(dev);
2879
Chris Wilsona266c7d2012-04-24 22:59:44 +01002880 /* With MSI, interrupts are only generated when iir
2881 * transitions from zero to nonzero. If another bit got
2882 * set while we were handling the existing iir bits, then
2883 * we would never get another interrupt.
2884 *
2885 * This is fine on non-MSI as well, as if we hit this path
2886 * we avoid exiting the interrupt handler only to generate
2887 * another one.
2888 *
2889 * Note that for MSI this could cause a stray interrupt report
2890 * if an interrupt landed in the time between writing IIR and
2891 * the posting read. This should be rare enough to never
2892 * trigger the 99% of 100,000 interrupts test for disabling
2893 * stray interrupts.
2894 */
2895 iir = new_iir;
2896 }
2897
Daniel Vetterd05c6172012-04-26 23:28:09 +02002898 i915_update_dri1_breadcrumb(dev);
Chris Wilson2c8ba292012-04-24 22:59:46 +01002899
Chris Wilsona266c7d2012-04-24 22:59:44 +01002900 return ret;
2901}
2902
2903static void i965_irq_uninstall(struct drm_device * dev)
2904{
2905 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2906 int pipe;
2907
2908 if (!dev_priv)
2909 return;
2910
Chris Wilsonadca4732012-05-11 18:01:31 +01002911 I915_WRITE(PORT_HOTPLUG_EN, 0);
2912 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01002913
2914 I915_WRITE(HWSTAM, 0xffffffff);
2915 for_each_pipe(pipe)
2916 I915_WRITE(PIPESTAT(pipe), 0);
2917 I915_WRITE(IMR, 0xffffffff);
2918 I915_WRITE(IER, 0x0);
2919
2920 for_each_pipe(pipe)
2921 I915_WRITE(PIPESTAT(pipe),
2922 I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
2923 I915_WRITE(IIR, I915_READ(IIR));
2924}
2925
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002926void intel_irq_init(struct drm_device *dev)
2927{
Chris Wilson8b2e3262012-04-24 22:59:41 +01002928 struct drm_i915_private *dev_priv = dev->dev_private;
2929
2930 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Daniel Vetter99584db2012-11-14 17:14:04 +01002931 INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002932 INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
Daniel Vettera4da4fa2012-11-02 19:55:07 +01002933 INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
Chris Wilson8b2e3262012-04-24 22:59:41 +01002934
Daniel Vetter99584db2012-11-14 17:14:04 +01002935 setup_timer(&dev_priv->gpu_error.hangcheck_timer,
2936 i915_hangcheck_elapsed,
Daniel Vetter61bac782012-12-01 21:03:21 +01002937 (unsigned long) dev);
2938
Tomas Janousek97a19a22012-12-08 13:48:13 +01002939 pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01002940
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002941 dev->driver->get_vblank_counter = i915_get_vblank_counter;
2942 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Eugeni Dodonov7d4e1462012-05-09 15:37:09 -03002943 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002944 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2945 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2946 }
2947
Keith Packardc3613de2011-08-12 17:05:54 -07002948 if (drm_core_check_feature(dev, DRIVER_MODESET))
2949 dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2950 else
2951 dev->driver->get_vblank_timestamp = NULL;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002952 dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2953
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002954 if (IS_VALLEYVIEW(dev)) {
2955 dev->driver->irq_handler = valleyview_irq_handler;
2956 dev->driver->irq_preinstall = valleyview_irq_preinstall;
2957 dev->driver->irq_postinstall = valleyview_irq_postinstall;
2958 dev->driver->irq_uninstall = valleyview_irq_uninstall;
2959 dev->driver->enable_vblank = valleyview_enable_vblank;
2960 dev->driver->disable_vblank = valleyview_disable_vblank;
Daniel Vetter20afbda2012-12-11 14:05:07 +01002961 dev_priv->display.hpd_irq_setup = valleyview_hpd_irq_setup;
Daniel Vetter4a06e202012-12-01 13:53:40 +01002962 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002963 /* Share pre & uninstall handlers with ILK/SNB */
2964 dev->driver->irq_handler = ivybridge_irq_handler;
2965 dev->driver->irq_preinstall = ironlake_irq_preinstall;
2966 dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2967 dev->driver->irq_uninstall = ironlake_irq_uninstall;
2968 dev->driver->enable_vblank = ivybridge_enable_vblank;
2969 dev->driver->disable_vblank = ivybridge_disable_vblank;
2970 } else if (HAS_PCH_SPLIT(dev)) {
2971 dev->driver->irq_handler = ironlake_irq_handler;
2972 dev->driver->irq_preinstall = ironlake_irq_preinstall;
2973 dev->driver->irq_postinstall = ironlake_irq_postinstall;
2974 dev->driver->irq_uninstall = ironlake_irq_uninstall;
2975 dev->driver->enable_vblank = ironlake_enable_vblank;
2976 dev->driver->disable_vblank = ironlake_disable_vblank;
2977 } else {
Chris Wilsonc2798b12012-04-22 21:13:57 +01002978 if (INTEL_INFO(dev)->gen == 2) {
2979 dev->driver->irq_preinstall = i8xx_irq_preinstall;
2980 dev->driver->irq_postinstall = i8xx_irq_postinstall;
2981 dev->driver->irq_handler = i8xx_irq_handler;
2982 dev->driver->irq_uninstall = i8xx_irq_uninstall;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002983 } else if (INTEL_INFO(dev)->gen == 3) {
2984 dev->driver->irq_preinstall = i915_irq_preinstall;
2985 dev->driver->irq_postinstall = i915_irq_postinstall;
2986 dev->driver->irq_uninstall = i915_irq_uninstall;
2987 dev->driver->irq_handler = i915_irq_handler;
Daniel Vetter20afbda2012-12-11 14:05:07 +01002988 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002989 } else {
Chris Wilsona266c7d2012-04-24 22:59:44 +01002990 dev->driver->irq_preinstall = i965_irq_preinstall;
2991 dev->driver->irq_postinstall = i965_irq_postinstall;
2992 dev->driver->irq_uninstall = i965_irq_uninstall;
2993 dev->driver->irq_handler = i965_irq_handler;
Daniel Vetter20afbda2012-12-11 14:05:07 +01002994 dev_priv->display.hpd_irq_setup = i965_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002995 }
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002996 dev->driver->enable_vblank = i915_enable_vblank;
2997 dev->driver->disable_vblank = i915_disable_vblank;
2998 }
2999}
Daniel Vetter20afbda2012-12-11 14:05:07 +01003000
3001void intel_hpd_init(struct drm_device *dev)
3002{
3003 struct drm_i915_private *dev_priv = dev->dev_private;
3004
3005 if (dev_priv->display.hpd_irq_setup)
3006 dev_priv->display.hpd_irq_setup(dev);
3007}