blob: 6d76ee54278b2eabbb54987c074662d55d0a544c [file] [log] [blame]
Dave Airlie0d6aa602006-01-02 20:14:23 +11001/* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 */
Dave Airlie0d6aa602006-01-02 20:14:23 +11003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
Dave Airliebc54fd12005-06-23 22:46:46 +10006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
Dave Airlie0d6aa602006-01-02 20:14:23 +110027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Joe Perchesa70491c2012-03-18 13:00:11 -070029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Jesse Barnes63eeaf32009-06-18 16:56:52 -070031#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "drmP.h"
34#include "drm.h"
35#include "i915_drm.h"
36#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010037#include "i915_trace.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define MAX_NOPID ((u32)~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Keith Packard7c463582008-11-04 02:03:27 -080042/**
43 * Interrupts that are always left unmasked.
44 *
45 * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
46 * we leave them always unmasked in IMR and then control enabling them through
47 * PIPESTAT alone.
48 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050049#define I915_INTERRUPT_ENABLE_FIX \
50 (I915_ASLE_INTERRUPT | \
51 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
52 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT | \
53 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT | \
54 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT | \
55 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Keith Packard7c463582008-11-04 02:03:27 -080056
57/** Interrupts that we mask and unmask at runtime. */
Zou Nan haid1b851f2010-05-21 09:08:57 +080058#define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT | I915_BSD_USER_INTERRUPT)
Keith Packard7c463582008-11-04 02:03:27 -080059
Jesse Barnes79e53942008-11-07 14:24:08 -080060#define I915_PIPE_VBLANK_STATUS (PIPE_START_VBLANK_INTERRUPT_STATUS |\
61 PIPE_VBLANK_INTERRUPT_STATUS)
62
63#define I915_PIPE_VBLANK_ENABLE (PIPE_START_VBLANK_INTERRUPT_ENABLE |\
64 PIPE_VBLANK_INTERRUPT_ENABLE)
65
66#define DRM_I915_VBLANK_PIPE_ALL (DRM_I915_VBLANK_PIPE_A | \
67 DRM_I915_VBLANK_PIPE_B)
68
Zhenyu Wang036a4a72009-06-08 14:40:19 +080069/* For display hotplug interrupt */
Chris Wilson995b6762010-08-20 13:23:26 +010070static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050071ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080072{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000073 if ((dev_priv->irq_mask & mask) != 0) {
74 dev_priv->irq_mask &= ~mask;
75 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000076 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080077 }
78}
79
80static inline void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050081ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080082{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000083 if ((dev_priv->irq_mask & mask) != mask) {
84 dev_priv->irq_mask |= mask;
85 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000086 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080087 }
88}
89
Keith Packard7c463582008-11-04 02:03:27 -080090void
91i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
92{
93 if ((dev_priv->pipestat[pipe] & mask) != mask) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -080094 u32 reg = PIPESTAT(pipe);
Keith Packard7c463582008-11-04 02:03:27 -080095
96 dev_priv->pipestat[pipe] |= mask;
97 /* Enable the interrupt, clear any pending status */
98 I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
Chris Wilson3143a2b2010-11-16 15:55:10 +000099 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800100 }
101}
102
103void
104i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
105{
106 if ((dev_priv->pipestat[pipe] & mask) != 0) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800107 u32 reg = PIPESTAT(pipe);
Keith Packard7c463582008-11-04 02:03:27 -0800108
109 dev_priv->pipestat[pipe] &= ~mask;
110 I915_WRITE(reg, dev_priv->pipestat[pipe]);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000111 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800112 }
113}
114
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000115/**
Zhao Yakui01c66882009-10-28 05:10:00 +0000116 * intel_enable_asle - enable ASLE interrupt for OpRegion
117 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000118void intel_enable_asle(struct drm_device *dev)
Zhao Yakui01c66882009-10-28 05:10:00 +0000119{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000120 drm_i915_private_t *dev_priv = dev->dev_private;
121 unsigned long irqflags;
122
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700123 /* FIXME: opregion/asle for VLV */
124 if (IS_VALLEYVIEW(dev))
125 return;
126
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000127 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000128
Eric Anholtc619eed2010-01-28 16:45:52 -0800129 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500130 ironlake_enable_display_irq(dev_priv, DE_GSE);
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800131 else {
Zhao Yakui01c66882009-10-28 05:10:00 +0000132 i915_enable_pipestat(dev_priv, 1,
Jesse Barnesd874bcf2010-06-30 13:16:00 -0700133 PIPE_LEGACY_BLC_EVENT_ENABLE);
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100134 if (INTEL_INFO(dev)->gen >= 4)
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800135 i915_enable_pipestat(dev_priv, 0,
Jesse Barnesd874bcf2010-06-30 13:16:00 -0700136 PIPE_LEGACY_BLC_EVENT_ENABLE);
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800137 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000138
139 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000140}
141
142/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700143 * i915_pipe_enabled - check if a pipe is enabled
144 * @dev: DRM device
145 * @pipe: pipe to check
146 *
147 * Reading certain registers when the pipe is disabled can hang the chip.
148 * Use this routine to make sure the PLL is running and the pipe is active
149 * before reading such registers if unsure.
150 */
151static int
152i915_pipe_enabled(struct drm_device *dev, int pipe)
153{
154 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson5eddb702010-09-11 13:48:45 +0100155 return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700156}
157
Keith Packard42f52ef2008-10-18 19:39:29 -0700158/* Called from drm generic code, passed a 'crtc', which
159 * we use as a pipe index
160 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700161static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700162{
163 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
164 unsigned long high_frame;
165 unsigned long low_frame;
Chris Wilson5eddb702010-09-11 13:48:45 +0100166 u32 high1, high2, low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700167
168 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800169 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800170 "pipe %c\n", pipe_name(pipe));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700171 return 0;
172 }
173
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800174 high_frame = PIPEFRAME(pipe);
175 low_frame = PIPEFRAMEPIXEL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +0100176
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700177 /*
178 * High & low register fields aren't synchronized, so make sure
179 * we get a low value that's stable across two reads of the high
180 * register.
181 */
182 do {
Chris Wilson5eddb702010-09-11 13:48:45 +0100183 high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
184 low = I915_READ(low_frame) & PIPE_FRAME_LOW_MASK;
185 high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700186 } while (high1 != high2);
187
Chris Wilson5eddb702010-09-11 13:48:45 +0100188 high1 >>= PIPE_FRAME_HIGH_SHIFT;
189 low >>= PIPE_FRAME_LOW_SHIFT;
190 return (high1 << 8) | low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700191}
192
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700193static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800194{
195 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800196 int reg = PIPE_FRMCOUNT_GM45(pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800197
198 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800199 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800200 "pipe %c\n", pipe_name(pipe));
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800201 return 0;
202 }
203
204 return I915_READ(reg);
205}
206
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700207static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100208 int *vpos, int *hpos)
209{
210 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
211 u32 vbl = 0, position = 0;
212 int vbl_start, vbl_end, htotal, vtotal;
213 bool in_vbl = true;
214 int ret = 0;
215
216 if (!i915_pipe_enabled(dev, pipe)) {
217 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800218 "pipe %c\n", pipe_name(pipe));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100219 return 0;
220 }
221
222 /* Get vtotal. */
223 vtotal = 1 + ((I915_READ(VTOTAL(pipe)) >> 16) & 0x1fff);
224
225 if (INTEL_INFO(dev)->gen >= 4) {
226 /* No obvious pixelcount register. Only query vertical
227 * scanout position from Display scan line register.
228 */
229 position = I915_READ(PIPEDSL(pipe));
230
231 /* Decode into vertical scanout position. Don't have
232 * horizontal scanout position.
233 */
234 *vpos = position & 0x1fff;
235 *hpos = 0;
236 } else {
237 /* Have access to pixelcount since start of frame.
238 * We can split this into vertical and horizontal
239 * scanout position.
240 */
241 position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
242
243 htotal = 1 + ((I915_READ(HTOTAL(pipe)) >> 16) & 0x1fff);
244 *vpos = position / htotal;
245 *hpos = position - (*vpos * htotal);
246 }
247
248 /* Query vblank area. */
249 vbl = I915_READ(VBLANK(pipe));
250
251 /* Test position against vblank region. */
252 vbl_start = vbl & 0x1fff;
253 vbl_end = (vbl >> 16) & 0x1fff;
254
255 if ((*vpos < vbl_start) || (*vpos > vbl_end))
256 in_vbl = false;
257
258 /* Inside "upper part" of vblank area? Apply corrective offset: */
259 if (in_vbl && (*vpos >= vbl_start))
260 *vpos = *vpos - vtotal;
261
262 /* Readouts valid? */
263 if (vbl > 0)
264 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
265
266 /* In vblank? */
267 if (in_vbl)
268 ret |= DRM_SCANOUTPOS_INVBL;
269
270 return ret;
271}
272
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700273static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100274 int *max_error,
275 struct timeval *vblank_time,
276 unsigned flags)
277{
Chris Wilson4041b852011-01-22 10:07:56 +0000278 struct drm_i915_private *dev_priv = dev->dev_private;
279 struct drm_crtc *crtc;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100280
Chris Wilson4041b852011-01-22 10:07:56 +0000281 if (pipe < 0 || pipe >= dev_priv->num_pipe) {
282 DRM_ERROR("Invalid crtc %d\n", pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100283 return -EINVAL;
284 }
285
286 /* Get drm_crtc to timestamp: */
Chris Wilson4041b852011-01-22 10:07:56 +0000287 crtc = intel_get_crtc_for_pipe(dev, pipe);
288 if (crtc == NULL) {
289 DRM_ERROR("Invalid crtc %d\n", pipe);
290 return -EINVAL;
291 }
292
293 if (!crtc->enabled) {
294 DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
295 return -EBUSY;
296 }
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100297
298 /* Helper routine in DRM core does all the work: */
Chris Wilson4041b852011-01-22 10:07:56 +0000299 return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
300 vblank_time, flags,
301 crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100302}
303
Jesse Barnes5ca58282009-03-31 14:11:15 -0700304/*
305 * Handle hotplug events outside the interrupt handler proper.
306 */
307static void i915_hotplug_work_func(struct work_struct *work)
308{
309 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
310 hotplug_work);
311 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700312 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100313 struct intel_encoder *encoder;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700314
Keith Packarda65e34c2011-07-25 10:04:56 -0700315 mutex_lock(&mode_config->mutex);
Jesse Barnese67189ab2011-02-11 14:44:51 -0800316 DRM_DEBUG_KMS("running encoder hotplug functions\n");
317
Chris Wilson4ef69c72010-09-09 15:14:28 +0100318 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
319 if (encoder->hot_plug)
320 encoder->hot_plug(encoder);
321
Keith Packard40ee3382011-07-28 15:31:19 -0700322 mutex_unlock(&mode_config->mutex);
323
Jesse Barnes5ca58282009-03-31 14:11:15 -0700324 /* Just fire off a uevent and let userspace tell us what to do */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000325 drm_helper_hpd_irq_event(dev);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700326}
327
Jesse Barnesf97108d2010-01-29 11:27:07 -0800328static void i915_handle_rps_change(struct drm_device *dev)
329{
330 drm_i915_private_t *dev_priv = dev->dev_private;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000331 u32 busy_up, busy_down, max_avg, min_avg;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800332 u8 new_delay = dev_priv->cur_delay;
333
Jesse Barnes7648fa92010-05-20 14:28:11 -0700334 I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000335 busy_up = I915_READ(RCPREVBSYTUPAVG);
336 busy_down = I915_READ(RCPREVBSYTDNAVG);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800337 max_avg = I915_READ(RCBMAXAVG);
338 min_avg = I915_READ(RCBMINAVG);
339
340 /* Handle RCS change request from hw */
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000341 if (busy_up > max_avg) {
Jesse Barnesf97108d2010-01-29 11:27:07 -0800342 if (dev_priv->cur_delay != dev_priv->max_delay)
343 new_delay = dev_priv->cur_delay - 1;
344 if (new_delay < dev_priv->max_delay)
345 new_delay = dev_priv->max_delay;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000346 } else if (busy_down < min_avg) {
Jesse Barnesf97108d2010-01-29 11:27:07 -0800347 if (dev_priv->cur_delay != dev_priv->min_delay)
348 new_delay = dev_priv->cur_delay + 1;
349 if (new_delay > dev_priv->min_delay)
350 new_delay = dev_priv->min_delay;
351 }
352
Jesse Barnes7648fa92010-05-20 14:28:11 -0700353 if (ironlake_set_drps(dev, new_delay))
354 dev_priv->cur_delay = new_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800355
356 return;
357}
358
Chris Wilson549f7362010-10-19 11:19:32 +0100359static void notify_ring(struct drm_device *dev,
360 struct intel_ring_buffer *ring)
361{
362 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson475553d2011-01-20 09:52:56 +0000363 u32 seqno;
Chris Wilson9862e602011-01-04 22:22:17 +0000364
Chris Wilson475553d2011-01-20 09:52:56 +0000365 if (ring->obj == NULL)
366 return;
367
368 seqno = ring->get_seqno(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +0000369 trace_i915_gem_request_complete(ring, seqno);
Chris Wilson9862e602011-01-04 22:22:17 +0000370
371 ring->irq_seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +0100372 wake_up_all(&ring->irq_queue);
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -0700373 if (i915_enable_hangcheck) {
374 dev_priv->hangcheck_count = 0;
375 mod_timer(&dev_priv->hangcheck_timer,
376 jiffies +
377 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
378 }
Chris Wilson549f7362010-10-19 11:19:32 +0100379}
380
Ben Widawsky4912d042011-04-25 11:25:20 -0700381static void gen6_pm_rps_work(struct work_struct *work)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800382{
Ben Widawsky4912d042011-04-25 11:25:20 -0700383 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
384 rps_work);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800385 u8 new_delay = dev_priv->cur_delay;
Ben Widawsky4912d042011-04-25 11:25:20 -0700386 u32 pm_iir, pm_imr;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800387
Ben Widawsky4912d042011-04-25 11:25:20 -0700388 spin_lock_irq(&dev_priv->rps_lock);
389 pm_iir = dev_priv->pm_iir;
390 dev_priv->pm_iir = 0;
391 pm_imr = I915_READ(GEN6_PMIMR);
Daniel Vettera9e26412011-09-08 14:00:21 +0200392 I915_WRITE(GEN6_PMIMR, 0);
Ben Widawsky4912d042011-04-25 11:25:20 -0700393 spin_unlock_irq(&dev_priv->rps_lock);
394
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800395 if (!pm_iir)
396 return;
397
Ben Widawsky4912d042011-04-25 11:25:20 -0700398 mutex_lock(&dev_priv->dev->struct_mutex);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800399 if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
400 if (dev_priv->cur_delay != dev_priv->max_delay)
401 new_delay = dev_priv->cur_delay + 1;
402 if (new_delay > dev_priv->max_delay)
403 new_delay = dev_priv->max_delay;
404 } else if (pm_iir & (GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT)) {
Ben Widawsky4912d042011-04-25 11:25:20 -0700405 gen6_gt_force_wake_get(dev_priv);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800406 if (dev_priv->cur_delay != dev_priv->min_delay)
407 new_delay = dev_priv->cur_delay - 1;
408 if (new_delay < dev_priv->min_delay) {
409 new_delay = dev_priv->min_delay;
410 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
411 I915_READ(GEN6_RP_INTERRUPT_LIMITS) |
412 ((new_delay << 16) & 0x3f0000));
413 } else {
414 /* Make sure we continue to get down interrupts
415 * until we hit the minimum frequency */
416 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
417 I915_READ(GEN6_RP_INTERRUPT_LIMITS) & ~0x3f0000);
418 }
Ben Widawsky4912d042011-04-25 11:25:20 -0700419 gen6_gt_force_wake_put(dev_priv);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800420 }
421
Ben Widawsky4912d042011-04-25 11:25:20 -0700422 gen6_set_rps(dev_priv->dev, new_delay);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800423 dev_priv->cur_delay = new_delay;
424
Ben Widawsky4912d042011-04-25 11:25:20 -0700425 /*
426 * rps_lock not held here because clearing is non-destructive. There is
427 * an *extremely* unlikely race with gen6_rps_enable() that is prevented
428 * by holding struct_mutex for the duration of the write.
429 */
Ben Widawsky4912d042011-04-25 11:25:20 -0700430 mutex_unlock(&dev_priv->dev->struct_mutex);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800431}
432
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200433static void snb_gt_irq_handler(struct drm_device *dev,
434 struct drm_i915_private *dev_priv,
435 u32 gt_iir)
436{
437
438 if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
439 GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
440 notify_ring(dev, &dev_priv->ring[RCS]);
441 if (gt_iir & GEN6_BSD_USER_INTERRUPT)
442 notify_ring(dev, &dev_priv->ring[VCS]);
443 if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
444 notify_ring(dev, &dev_priv->ring[BCS]);
445
446 if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
447 GT_GEN6_BSD_CS_ERROR_INTERRUPT |
448 GT_RENDER_CS_ERROR_INTERRUPT)) {
449 DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
450 i915_handle_error(dev, false);
451 }
452}
453
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700454static irqreturn_t valleyview_irq_handler(DRM_IRQ_ARGS)
455{
456 struct drm_device *dev = (struct drm_device *) arg;
457 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
458 u32 iir, gt_iir, pm_iir;
459 irqreturn_t ret = IRQ_NONE;
460 unsigned long irqflags;
461 int pipe;
462 u32 pipe_stats[I915_MAX_PIPES];
463 u32 vblank_status;
464 int vblank = 0;
465 bool blc_event;
466
467 atomic_inc(&dev_priv->irq_received);
468
469 vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS |
470 PIPE_VBLANK_INTERRUPT_STATUS;
471
472 while (true) {
473 iir = I915_READ(VLV_IIR);
474 gt_iir = I915_READ(GTIIR);
475 pm_iir = I915_READ(GEN6_PMIIR);
476
477 if (gt_iir == 0 && pm_iir == 0 && iir == 0)
478 goto out;
479
480 ret = IRQ_HANDLED;
481
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200482 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700483
484 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
485 for_each_pipe(pipe) {
486 int reg = PIPESTAT(pipe);
487 pipe_stats[pipe] = I915_READ(reg);
488
489 /*
490 * Clear the PIPE*STAT regs before the IIR
491 */
492 if (pipe_stats[pipe] & 0x8000ffff) {
493 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
494 DRM_DEBUG_DRIVER("pipe %c underrun\n",
495 pipe_name(pipe));
496 I915_WRITE(reg, pipe_stats[pipe]);
497 }
498 }
499 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
500
501 /* Consume port. Then clear IIR or we'll miss events */
502 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
503 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
504
505 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
506 hotplug_status);
507 if (hotplug_status & dev_priv->hotplug_supported_mask)
508 queue_work(dev_priv->wq,
509 &dev_priv->hotplug_work);
510
511 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
512 I915_READ(PORT_HOTPLUG_STAT);
513 }
514
515
516 if (iir & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT) {
517 drm_handle_vblank(dev, 0);
518 vblank++;
519 if (!dev_priv->flip_pending_is_done) {
520 intel_finish_page_flip(dev, 0);
521 }
522 }
523
524 if (iir & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT) {
525 drm_handle_vblank(dev, 1);
526 vblank++;
527 if (!dev_priv->flip_pending_is_done) {
528 intel_finish_page_flip(dev, 0);
529 }
530 }
531
532 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
533 blc_event = true;
534
535 if (pm_iir & GEN6_PM_DEFERRED_EVENTS) {
536 unsigned long flags;
537 spin_lock_irqsave(&dev_priv->rps_lock, flags);
538 WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
539 dev_priv->pm_iir |= pm_iir;
540 I915_WRITE(GEN6_PMIMR, dev_priv->pm_iir);
541 POSTING_READ(GEN6_PMIMR);
542 spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
543 queue_work(dev_priv->wq, &dev_priv->rps_work);
544 }
545
546 I915_WRITE(GTIIR, gt_iir);
547 I915_WRITE(GEN6_PMIIR, pm_iir);
548 I915_WRITE(VLV_IIR, iir);
549 }
550
551out:
552 return ret;
553}
554
Jesse Barnes776ad802011-01-04 15:09:39 -0800555static void pch_irq_handler(struct drm_device *dev)
556{
557 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
558 u32 pch_iir;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800559 int pipe;
Jesse Barnes776ad802011-01-04 15:09:39 -0800560
561 pch_iir = I915_READ(SDEIIR);
562
563 if (pch_iir & SDE_AUDIO_POWER_MASK)
564 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
565 (pch_iir & SDE_AUDIO_POWER_MASK) >>
566 SDE_AUDIO_POWER_SHIFT);
567
568 if (pch_iir & SDE_GMBUS)
569 DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
570
571 if (pch_iir & SDE_AUDIO_HDCP_MASK)
572 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
573
574 if (pch_iir & SDE_AUDIO_TRANS_MASK)
575 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
576
577 if (pch_iir & SDE_POISON)
578 DRM_ERROR("PCH poison interrupt\n");
579
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800580 if (pch_iir & SDE_FDI_MASK)
581 for_each_pipe(pipe)
582 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
583 pipe_name(pipe),
584 I915_READ(FDI_RX_IIR(pipe)));
Jesse Barnes776ad802011-01-04 15:09:39 -0800585
586 if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
587 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
588
589 if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
590 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
591
592 if (pch_iir & SDE_TRANSB_FIFO_UNDER)
593 DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
594 if (pch_iir & SDE_TRANSA_FIFO_UNDER)
595 DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
596}
597
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700598static irqreturn_t ivybridge_irq_handler(DRM_IRQ_ARGS)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700599{
600 struct drm_device *dev = (struct drm_device *) arg;
601 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
602 int ret = IRQ_NONE;
603 u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
604 struct drm_i915_master_private *master_priv;
605
606 atomic_inc(&dev_priv->irq_received);
607
608 /* disable master interrupt before clearing iir */
609 de_ier = I915_READ(DEIER);
610 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
611 POSTING_READ(DEIER);
612
613 de_iir = I915_READ(DEIIR);
614 gt_iir = I915_READ(GTIIR);
615 pch_iir = I915_READ(SDEIIR);
616 pm_iir = I915_READ(GEN6_PMIIR);
617
618 if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 && pm_iir == 0)
619 goto done;
620
621 ret = IRQ_HANDLED;
622
623 if (dev->primary->master) {
624 master_priv = dev->primary->master->driver_priv;
625 if (master_priv->sarea_priv)
626 master_priv->sarea_priv->last_dispatch =
627 READ_BREADCRUMB(dev_priv);
628 }
629
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200630 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700631
632 if (de_iir & DE_GSE_IVB)
633 intel_opregion_gse_intr(dev);
634
635 if (de_iir & DE_PLANEA_FLIP_DONE_IVB) {
636 intel_prepare_page_flip(dev, 0);
637 intel_finish_page_flip_plane(dev, 0);
638 }
639
640 if (de_iir & DE_PLANEB_FLIP_DONE_IVB) {
641 intel_prepare_page_flip(dev, 1);
642 intel_finish_page_flip_plane(dev, 1);
643 }
644
645 if (de_iir & DE_PIPEA_VBLANK_IVB)
646 drm_handle_vblank(dev, 0);
647
Dan Carpenterf6b07f42011-05-25 12:56:56 +0300648 if (de_iir & DE_PIPEB_VBLANK_IVB)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700649 drm_handle_vblank(dev, 1);
650
651 /* check event from PCH */
652 if (de_iir & DE_PCH_EVENT_IVB) {
653 if (pch_iir & SDE_HOTPLUG_MASK_CPT)
654 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
655 pch_irq_handler(dev);
656 }
657
658 if (pm_iir & GEN6_PM_DEFERRED_EVENTS) {
659 unsigned long flags;
660 spin_lock_irqsave(&dev_priv->rps_lock, flags);
661 WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700662 dev_priv->pm_iir |= pm_iir;
Daniel Vetter4fb066a2011-09-08 14:00:20 +0200663 I915_WRITE(GEN6_PMIMR, dev_priv->pm_iir);
664 POSTING_READ(GEN6_PMIMR);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700665 spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
666 queue_work(dev_priv->wq, &dev_priv->rps_work);
667 }
668
669 /* should clear PCH hotplug event before clear CPU irq */
670 I915_WRITE(SDEIIR, pch_iir);
671 I915_WRITE(GTIIR, gt_iir);
672 I915_WRITE(DEIIR, de_iir);
673 I915_WRITE(GEN6_PMIIR, pm_iir);
674
675done:
676 I915_WRITE(DEIER, de_ier);
677 POSTING_READ(DEIER);
678
679 return ret;
680}
681
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200682static void ilk_gt_irq_handler(struct drm_device *dev,
683 struct drm_i915_private *dev_priv,
684 u32 gt_iir)
685{
686 if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
687 notify_ring(dev, &dev_priv->ring[RCS]);
688 if (gt_iir & GT_BSD_USER_INTERRUPT)
689 notify_ring(dev, &dev_priv->ring[VCS]);
690}
691
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700692static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800693{
Jesse Barnes46979952011-04-07 13:53:55 -0700694 struct drm_device *dev = (struct drm_device *) arg;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800695 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
696 int ret = IRQ_NONE;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800697 u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
Yuanhan Liu2d7b8362010-10-08 10:21:06 +0100698 u32 hotplug_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800699 struct drm_i915_master_private *master_priv;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100700
Jesse Barnes46979952011-04-07 13:53:55 -0700701 atomic_inc(&dev_priv->irq_received);
702
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000703 /* disable master interrupt before clearing iir */
704 de_ier = I915_READ(DEIER);
705 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000706 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000707
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800708 de_iir = I915_READ(DEIIR);
709 gt_iir = I915_READ(GTIIR);
Zhenyu Wangc6501562009-11-03 18:57:21 +0000710 pch_iir = I915_READ(SDEIIR);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800711 pm_iir = I915_READ(GEN6_PMIIR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800712
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800713 if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
714 (!IS_GEN6(dev) || pm_iir == 0))
Zou Nan haic7c85102010-01-15 10:29:06 +0800715 goto done;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800716
Yuanhan Liu2d7b8362010-10-08 10:21:06 +0100717 if (HAS_PCH_CPT(dev))
718 hotplug_mask = SDE_HOTPLUG_MASK_CPT;
719 else
720 hotplug_mask = SDE_HOTPLUG_MASK;
721
Zou Nan haic7c85102010-01-15 10:29:06 +0800722 ret = IRQ_HANDLED;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800723
Zou Nan haic7c85102010-01-15 10:29:06 +0800724 if (dev->primary->master) {
725 master_priv = dev->primary->master->driver_priv;
726 if (master_priv->sarea_priv)
727 master_priv->sarea_priv->last_dispatch =
728 READ_BREADCRUMB(dev_priv);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800729 }
730
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200731 if (IS_GEN5(dev))
732 ilk_gt_irq_handler(dev, dev_priv, gt_iir);
733 else
734 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Zou Nan haic7c85102010-01-15 10:29:06 +0800735
736 if (de_iir & DE_GSE)
Chris Wilson3b617962010-08-24 09:02:58 +0100737 intel_opregion_gse_intr(dev);
Zou Nan haic7c85102010-01-15 10:29:06 +0800738
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800739 if (de_iir & DE_PLANEA_FLIP_DONE) {
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800740 intel_prepare_page_flip(dev, 0);
Chris Wilson2bbda382010-09-02 17:59:39 +0100741 intel_finish_page_flip_plane(dev, 0);
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800742 }
743
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800744 if (de_iir & DE_PLANEB_FLIP_DONE) {
745 intel_prepare_page_flip(dev, 1);
Chris Wilson2bbda382010-09-02 17:59:39 +0100746 intel_finish_page_flip_plane(dev, 1);
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800747 }
Li Pengc062df62010-01-23 00:12:58 +0800748
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800749 if (de_iir & DE_PIPEA_VBLANK)
750 drm_handle_vblank(dev, 0);
751
752 if (de_iir & DE_PIPEB_VBLANK)
753 drm_handle_vblank(dev, 1);
754
Zou Nan haic7c85102010-01-15 10:29:06 +0800755 /* check event from PCH */
Jesse Barnes776ad802011-01-04 15:09:39 -0800756 if (de_iir & DE_PCH_EVENT) {
757 if (pch_iir & hotplug_mask)
758 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
759 pch_irq_handler(dev);
760 }
Zou Nan haic7c85102010-01-15 10:29:06 +0800761
Jesse Barnesf97108d2010-01-29 11:27:07 -0800762 if (de_iir & DE_PCU_EVENT) {
Jesse Barnes7648fa92010-05-20 14:28:11 -0700763 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
Jesse Barnesf97108d2010-01-29 11:27:07 -0800764 i915_handle_rps_change(dev);
765 }
766
Ben Widawsky4912d042011-04-25 11:25:20 -0700767 if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS) {
768 /*
769 * IIR bits should never already be set because IMR should
770 * prevent an interrupt from being shown in IIR. The warning
771 * displays a case where we've unsafely cleared
772 * dev_priv->pm_iir. Although missing an interrupt of the same
773 * type is not a problem, it displays a problem in the logic.
774 *
775 * The mask bit in IMR is cleared by rps_work.
776 */
777 unsigned long flags;
778 spin_lock_irqsave(&dev_priv->rps_lock, flags);
779 WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
Ben Widawsky4912d042011-04-25 11:25:20 -0700780 dev_priv->pm_iir |= pm_iir;
Daniel Vetter4fb066a2011-09-08 14:00:20 +0200781 I915_WRITE(GEN6_PMIMR, dev_priv->pm_iir);
782 POSTING_READ(GEN6_PMIMR);
Ben Widawsky4912d042011-04-25 11:25:20 -0700783 spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
784 queue_work(dev_priv->wq, &dev_priv->rps_work);
785 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800786
Zou Nan haic7c85102010-01-15 10:29:06 +0800787 /* should clear PCH hotplug event before clear CPU irq */
788 I915_WRITE(SDEIIR, pch_iir);
789 I915_WRITE(GTIIR, gt_iir);
790 I915_WRITE(DEIIR, de_iir);
Ben Widawsky4912d042011-04-25 11:25:20 -0700791 I915_WRITE(GEN6_PMIIR, pm_iir);
Zou Nan haic7c85102010-01-15 10:29:06 +0800792
793done:
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000794 I915_WRITE(DEIER, de_ier);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000795 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000796
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800797 return ret;
798}
799
Jesse Barnes8a905232009-07-11 16:48:03 -0400800/**
801 * i915_error_work_func - do process context error handling work
802 * @work: work struct
803 *
804 * Fire an error uevent so userspace can see that a hang or error
805 * was detected.
806 */
807static void i915_error_work_func(struct work_struct *work)
808{
809 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
810 error_work);
811 struct drm_device *dev = dev_priv->dev;
Ben Gamarif316a422009-09-14 17:48:46 -0400812 char *error_event[] = { "ERROR=1", NULL };
813 char *reset_event[] = { "RESET=1", NULL };
814 char *reset_done_event[] = { "ERROR=0", NULL };
Jesse Barnes8a905232009-07-11 16:48:03 -0400815
Ben Gamarif316a422009-09-14 17:48:46 -0400816 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -0400817
Ben Gamariba1234d2009-09-14 17:48:47 -0400818 if (atomic_read(&dev_priv->mm.wedged)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +0100819 DRM_DEBUG_DRIVER("resetting chip\n");
820 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
821 if (!i915_reset(dev, GRDOM_RENDER)) {
822 atomic_set(&dev_priv->mm.wedged, 0);
823 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
Ben Gamarif316a422009-09-14 17:48:46 -0400824 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100825 complete_all(&dev_priv->error_completion);
Ben Gamarif316a422009-09-14 17:48:46 -0400826 }
Jesse Barnes8a905232009-07-11 16:48:03 -0400827}
828
Chris Wilson3bd3c932010-08-19 08:19:30 +0100829#ifdef CONFIG_DEBUG_FS
Chris Wilson9df30792010-02-18 10:24:56 +0000830static struct drm_i915_error_object *
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000831i915_error_object_create(struct drm_i915_private *dev_priv,
Chris Wilson05394f32010-11-08 19:18:58 +0000832 struct drm_i915_gem_object *src)
Chris Wilson9df30792010-02-18 10:24:56 +0000833{
834 struct drm_i915_error_object *dst;
Chris Wilson9df30792010-02-18 10:24:56 +0000835 int page, page_count;
Chris Wilsone56660d2010-08-07 11:01:26 +0100836 u32 reloc_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000837
Chris Wilson05394f32010-11-08 19:18:58 +0000838 if (src == NULL || src->pages == NULL)
Chris Wilson9df30792010-02-18 10:24:56 +0000839 return NULL;
840
Chris Wilson05394f32010-11-08 19:18:58 +0000841 page_count = src->base.size / PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +0000842
Akshay Joshi0206e352011-08-16 15:34:10 -0400843 dst = kmalloc(sizeof(*dst) + page_count * sizeof(u32 *), GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +0000844 if (dst == NULL)
845 return NULL;
846
Chris Wilson05394f32010-11-08 19:18:58 +0000847 reloc_offset = src->gtt_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000848 for (page = 0; page < page_count; page++) {
Andrew Morton788885a2010-05-11 14:07:05 -0700849 unsigned long flags;
Chris Wilsone56660d2010-08-07 11:01:26 +0100850 void *d;
Andrew Morton788885a2010-05-11 14:07:05 -0700851
Chris Wilsone56660d2010-08-07 11:01:26 +0100852 d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +0000853 if (d == NULL)
854 goto unwind;
Chris Wilsone56660d2010-08-07 11:01:26 +0100855
Andrew Morton788885a2010-05-11 14:07:05 -0700856 local_irq_save(flags);
Daniel Vetter74898d72012-02-15 23:50:22 +0100857 if (reloc_offset < dev_priv->mm.gtt_mappable_end &&
858 src->has_global_gtt_mapping) {
Chris Wilson172975aa2011-12-14 13:57:25 +0100859 void __iomem *s;
860
861 /* Simply ignore tiling or any overlapping fence.
862 * It's part of the error state, and this hopefully
863 * captures what the GPU read.
864 */
865
866 s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
867 reloc_offset);
868 memcpy_fromio(d, s, PAGE_SIZE);
869 io_mapping_unmap_atomic(s);
870 } else {
871 void *s;
872
873 drm_clflush_pages(&src->pages[page], 1);
874
875 s = kmap_atomic(src->pages[page]);
876 memcpy(d, s, PAGE_SIZE);
877 kunmap_atomic(s);
878
879 drm_clflush_pages(&src->pages[page], 1);
880 }
Andrew Morton788885a2010-05-11 14:07:05 -0700881 local_irq_restore(flags);
Chris Wilsone56660d2010-08-07 11:01:26 +0100882
Chris Wilson9df30792010-02-18 10:24:56 +0000883 dst->pages[page] = d;
Chris Wilsone56660d2010-08-07 11:01:26 +0100884
885 reloc_offset += PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +0000886 }
887 dst->page_count = page_count;
Chris Wilson05394f32010-11-08 19:18:58 +0000888 dst->gtt_offset = src->gtt_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000889
890 return dst;
891
892unwind:
893 while (page--)
894 kfree(dst->pages[page]);
895 kfree(dst);
896 return NULL;
897}
898
899static void
900i915_error_object_free(struct drm_i915_error_object *obj)
901{
902 int page;
903
904 if (obj == NULL)
905 return;
906
907 for (page = 0; page < obj->page_count; page++)
908 kfree(obj->pages[page]);
909
910 kfree(obj);
911}
912
913static void
914i915_error_state_free(struct drm_device *dev,
915 struct drm_i915_error_state *error)
916{
Chris Wilsone2f973d2011-01-27 19:15:11 +0000917 int i;
918
Chris Wilson52d39a22012-02-15 11:25:37 +0000919 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
920 i915_error_object_free(error->ring[i].batchbuffer);
921 i915_error_object_free(error->ring[i].ringbuffer);
922 kfree(error->ring[i].requests);
923 }
Chris Wilsone2f973d2011-01-27 19:15:11 +0000924
Chris Wilson9df30792010-02-18 10:24:56 +0000925 kfree(error->active_bo);
Chris Wilson6ef3d422010-08-04 20:26:07 +0100926 kfree(error->overlay);
Chris Wilson9df30792010-02-18 10:24:56 +0000927 kfree(error);
928}
929
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000930static u32 capture_bo_list(struct drm_i915_error_buffer *err,
931 int count,
932 struct list_head *head)
933{
934 struct drm_i915_gem_object *obj;
935 int i = 0;
936
937 list_for_each_entry(obj, head, mm_list) {
938 err->size = obj->base.size;
939 err->name = obj->base.name;
940 err->seqno = obj->last_rendering_seqno;
941 err->gtt_offset = obj->gtt_offset;
942 err->read_domains = obj->base.read_domains;
943 err->write_domain = obj->base.write_domain;
944 err->fence_reg = obj->fence_reg;
945 err->pinned = 0;
946 if (obj->pin_count > 0)
947 err->pinned = 1;
948 if (obj->user_pin_count > 0)
949 err->pinned = -1;
950 err->tiling = obj->tiling_mode;
951 err->dirty = obj->dirty;
952 err->purgeable = obj->madv != I915_MADV_WILLNEED;
Daniel Vetter96154f22011-12-14 13:57:00 +0100953 err->ring = obj->ring ? obj->ring->id : -1;
Chris Wilson93dfb402011-03-29 16:59:50 -0700954 err->cache_level = obj->cache_level;
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000955
956 if (++i == count)
957 break;
958
959 err++;
960 }
961
962 return i;
963}
964
Chris Wilson748ebc62010-10-24 10:28:47 +0100965static void i915_gem_record_fences(struct drm_device *dev,
966 struct drm_i915_error_state *error)
967{
968 struct drm_i915_private *dev_priv = dev->dev_private;
969 int i;
970
971 /* Fences */
972 switch (INTEL_INFO(dev)->gen) {
Daniel Vetter775d17b2011-10-09 21:52:01 +0200973 case 7:
Chris Wilson748ebc62010-10-24 10:28:47 +0100974 case 6:
975 for (i = 0; i < 16; i++)
976 error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
977 break;
978 case 5:
979 case 4:
980 for (i = 0; i < 16; i++)
981 error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
982 break;
983 case 3:
984 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
985 for (i = 0; i < 8; i++)
986 error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
987 case 2:
988 for (i = 0; i < 8; i++)
989 error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
990 break;
991
992 }
993}
994
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000995static struct drm_i915_error_object *
996i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
997 struct intel_ring_buffer *ring)
998{
999 struct drm_i915_gem_object *obj;
1000 u32 seqno;
1001
1002 if (!ring->get_seqno)
1003 return NULL;
1004
1005 seqno = ring->get_seqno(ring);
1006 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1007 if (obj->ring != ring)
1008 continue;
1009
Chris Wilsonc37d9a52011-01-12 20:33:01 +00001010 if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001011 continue;
1012
1013 if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1014 continue;
1015
1016 /* We need to copy these to an anonymous buffer as the simplest
1017 * method to avoid being overwritten by userspace.
1018 */
1019 return i915_error_object_create(dev_priv, obj);
1020 }
1021
1022 return NULL;
1023}
1024
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001025static void i915_record_ring_state(struct drm_device *dev,
1026 struct drm_i915_error_state *error,
1027 struct intel_ring_buffer *ring)
1028{
1029 struct drm_i915_private *dev_priv = dev->dev_private;
1030
Daniel Vetter33f3f512011-12-14 13:57:39 +01001031 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001032 error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
Daniel Vetter33f3f512011-12-14 13:57:39 +01001033 error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
Daniel Vetter7e3b8732012-02-01 22:26:45 +01001034 error->semaphore_mboxes[ring->id][0]
1035 = I915_READ(RING_SYNC_0(ring->mmio_base));
1036 error->semaphore_mboxes[ring->id][1]
1037 = I915_READ(RING_SYNC_1(ring->mmio_base));
Daniel Vetter33f3f512011-12-14 13:57:39 +01001038 }
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001039
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001040 if (INTEL_INFO(dev)->gen >= 4) {
1041 error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1042 error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1043 error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001044 error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001045 if (ring->id == RCS) {
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001046 error->instdone1 = I915_READ(INSTDONE1);
1047 error->bbaddr = I915_READ64(BB_ADDR);
1048 }
1049 } else {
1050 error->ipeir[ring->id] = I915_READ(IPEIR);
1051 error->ipehr[ring->id] = I915_READ(IPEHR);
1052 error->instdone[ring->id] = I915_READ(INSTDONE);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001053 }
1054
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001055 error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001056 error->seqno[ring->id] = ring->get_seqno(ring);
1057 error->acthd[ring->id] = intel_ring_get_active_head(ring);
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001058 error->head[ring->id] = I915_READ_HEAD(ring);
1059 error->tail[ring->id] = I915_READ_TAIL(ring);
Daniel Vetter7e3b8732012-02-01 22:26:45 +01001060
1061 error->cpu_ring_head[ring->id] = ring->head;
1062 error->cpu_ring_tail[ring->id] = ring->tail;
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001063}
1064
Chris Wilson52d39a22012-02-15 11:25:37 +00001065static void i915_gem_record_rings(struct drm_device *dev,
1066 struct drm_i915_error_state *error)
1067{
1068 struct drm_i915_private *dev_priv = dev->dev_private;
1069 struct drm_i915_gem_request *request;
1070 int i, count;
1071
1072 for (i = 0; i < I915_NUM_RINGS; i++) {
1073 struct intel_ring_buffer *ring = &dev_priv->ring[i];
1074
1075 if (ring->obj == NULL)
1076 continue;
1077
1078 i915_record_ring_state(dev, error, ring);
1079
1080 error->ring[i].batchbuffer =
1081 i915_error_first_batchbuffer(dev_priv, ring);
1082
1083 error->ring[i].ringbuffer =
1084 i915_error_object_create(dev_priv, ring->obj);
1085
1086 count = 0;
1087 list_for_each_entry(request, &ring->request_list, list)
1088 count++;
1089
1090 error->ring[i].num_requests = count;
1091 error->ring[i].requests =
1092 kmalloc(count*sizeof(struct drm_i915_error_request),
1093 GFP_ATOMIC);
1094 if (error->ring[i].requests == NULL) {
1095 error->ring[i].num_requests = 0;
1096 continue;
1097 }
1098
1099 count = 0;
1100 list_for_each_entry(request, &ring->request_list, list) {
1101 struct drm_i915_error_request *erq;
1102
1103 erq = &error->ring[i].requests[count++];
1104 erq->seqno = request->seqno;
1105 erq->jiffies = request->emitted_jiffies;
Chris Wilsonee4f42b2012-02-15 11:25:38 +00001106 erq->tail = request->tail;
Chris Wilson52d39a22012-02-15 11:25:37 +00001107 }
1108 }
1109}
1110
Jesse Barnes8a905232009-07-11 16:48:03 -04001111/**
1112 * i915_capture_error_state - capture an error record for later analysis
1113 * @dev: drm device
1114 *
1115 * Should be called when an error is detected (either a hang or an error
1116 * interrupt) to capture error state from the time of the error. Fills
1117 * out a structure which becomes available in debugfs for user level tools
1118 * to pick up.
1119 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001120static void i915_capture_error_state(struct drm_device *dev)
1121{
1122 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001123 struct drm_i915_gem_object *obj;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001124 struct drm_i915_error_state *error;
1125 unsigned long flags;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001126 int i, pipe;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001127
1128 spin_lock_irqsave(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001129 error = dev_priv->first_error;
1130 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
1131 if (error)
1132 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001133
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001134 /* Account for pipe specific data like PIPE*STAT */
Daniel Vetter33f3f512011-12-14 13:57:39 +01001135 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001136 if (!error) {
Chris Wilson9df30792010-02-18 10:24:56 +00001137 DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
1138 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001139 }
1140
Chris Wilsonb6f78332011-02-01 14:15:55 +00001141 DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
1142 dev->primary->index);
Chris Wilson2fa772f2010-10-01 13:23:27 +01001143
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001144 error->eir = I915_READ(EIR);
1145 error->pgtbl_er = I915_READ(PGTBL_ER);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001146 for_each_pipe(pipe)
1147 error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001148
Daniel Vetter33f3f512011-12-14 13:57:39 +01001149 if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilsonf4068392010-10-27 20:36:41 +01001150 error->error = I915_READ(ERROR_GEN6);
Daniel Vetter33f3f512011-12-14 13:57:39 +01001151 error->done_reg = I915_READ(DONE_REG);
1152 }
Chris Wilsonadd354d2010-10-29 19:00:51 +01001153
Chris Wilson748ebc62010-10-24 10:28:47 +01001154 i915_gem_record_fences(dev, error);
Chris Wilson52d39a22012-02-15 11:25:37 +00001155 i915_gem_record_rings(dev, error);
Chris Wilson9df30792010-02-18 10:24:56 +00001156
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001157 /* Record buffers on the active and pinned lists. */
Chris Wilson9df30792010-02-18 10:24:56 +00001158 error->active_bo = NULL;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001159 error->pinned_bo = NULL;
Chris Wilson9df30792010-02-18 10:24:56 +00001160
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001161 i = 0;
1162 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1163 i++;
1164 error->active_bo_count = i;
Chris Wilson05394f32010-11-08 19:18:58 +00001165 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001166 i++;
1167 error->pinned_bo_count = i - error->active_bo_count;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001168
Chris Wilson8e934db2011-01-24 12:34:00 +00001169 error->active_bo = NULL;
1170 error->pinned_bo = NULL;
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001171 if (i) {
1172 error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
Chris Wilson9df30792010-02-18 10:24:56 +00001173 GFP_ATOMIC);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001174 if (error->active_bo)
1175 error->pinned_bo =
1176 error->active_bo + error->active_bo_count;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001177 }
1178
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001179 if (error->active_bo)
1180 error->active_bo_count =
1181 capture_bo_list(error->active_bo,
1182 error->active_bo_count,
1183 &dev_priv->mm.active_list);
1184
1185 if (error->pinned_bo)
1186 error->pinned_bo_count =
1187 capture_bo_list(error->pinned_bo,
1188 error->pinned_bo_count,
1189 &dev_priv->mm.pinned_list);
1190
Jesse Barnes8a905232009-07-11 16:48:03 -04001191 do_gettimeofday(&error->time);
1192
Chris Wilson6ef3d422010-08-04 20:26:07 +01001193 error->overlay = intel_overlay_capture_error_state(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00001194 error->display = intel_display_capture_error_state(dev);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001195
Chris Wilson9df30792010-02-18 10:24:56 +00001196 spin_lock_irqsave(&dev_priv->error_lock, flags);
1197 if (dev_priv->first_error == NULL) {
1198 dev_priv->first_error = error;
1199 error = NULL;
1200 }
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001201 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001202
1203 if (error)
1204 i915_error_state_free(dev, error);
1205}
1206
1207void i915_destroy_error_state(struct drm_device *dev)
1208{
1209 struct drm_i915_private *dev_priv = dev->dev_private;
1210 struct drm_i915_error_state *error;
Ben Widawsky6dc0e812012-01-23 15:30:02 -08001211 unsigned long flags;
Chris Wilson9df30792010-02-18 10:24:56 +00001212
Ben Widawsky6dc0e812012-01-23 15:30:02 -08001213 spin_lock_irqsave(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001214 error = dev_priv->first_error;
1215 dev_priv->first_error = NULL;
Ben Widawsky6dc0e812012-01-23 15:30:02 -08001216 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001217
1218 if (error)
1219 i915_error_state_free(dev, error);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001220}
Chris Wilson3bd3c932010-08-19 08:19:30 +01001221#else
1222#define i915_capture_error_state(x)
1223#endif
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001224
Chris Wilson35aed2e2010-05-27 13:18:12 +01001225static void i915_report_and_clear_eir(struct drm_device *dev)
Jesse Barnes8a905232009-07-11 16:48:03 -04001226{
1227 struct drm_i915_private *dev_priv = dev->dev_private;
1228 u32 eir = I915_READ(EIR);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001229 int pipe;
Jesse Barnes8a905232009-07-11 16:48:03 -04001230
Chris Wilson35aed2e2010-05-27 13:18:12 +01001231 if (!eir)
1232 return;
Jesse Barnes8a905232009-07-11 16:48:03 -04001233
Joe Perchesa70491c2012-03-18 13:00:11 -07001234 pr_err("render error detected, EIR: 0x%08x\n", eir);
Jesse Barnes8a905232009-07-11 16:48:03 -04001235
1236 if (IS_G4X(dev)) {
1237 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
1238 u32 ipeir = I915_READ(IPEIR_I965);
1239
Joe Perchesa70491c2012-03-18 13:00:11 -07001240 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1241 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1242 pr_err(" INSTDONE: 0x%08x\n",
Jesse Barnes8a905232009-07-11 16:48:03 -04001243 I915_READ(INSTDONE_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07001244 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
1245 pr_err(" INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1246 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04001247 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001248 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001249 }
1250 if (eir & GM45_ERROR_PAGE_TABLE) {
1251 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07001252 pr_err("page table error\n");
1253 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04001254 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001255 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04001256 }
1257 }
1258
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001259 if (!IS_GEN2(dev)) {
Jesse Barnes8a905232009-07-11 16:48:03 -04001260 if (eir & I915_ERROR_PAGE_TABLE) {
1261 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07001262 pr_err("page table error\n");
1263 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04001264 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001265 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04001266 }
1267 }
1268
1269 if (eir & I915_ERROR_MEMORY_REFRESH) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001270 pr_err("memory refresh error:\n");
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001271 for_each_pipe(pipe)
Joe Perchesa70491c2012-03-18 13:00:11 -07001272 pr_err("pipe %c stat: 0x%08x\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001273 pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
Jesse Barnes8a905232009-07-11 16:48:03 -04001274 /* pipestat has already been acked */
1275 }
1276 if (eir & I915_ERROR_INSTRUCTION) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001277 pr_err("instruction error\n");
1278 pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001279 if (INTEL_INFO(dev)->gen < 4) {
Jesse Barnes8a905232009-07-11 16:48:03 -04001280 u32 ipeir = I915_READ(IPEIR);
1281
Joe Perchesa70491c2012-03-18 13:00:11 -07001282 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
1283 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
1284 pr_err(" INSTDONE: 0x%08x\n", I915_READ(INSTDONE));
1285 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
Jesse Barnes8a905232009-07-11 16:48:03 -04001286 I915_WRITE(IPEIR, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001287 POSTING_READ(IPEIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001288 } else {
1289 u32 ipeir = I915_READ(IPEIR_I965);
1290
Joe Perchesa70491c2012-03-18 13:00:11 -07001291 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1292 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1293 pr_err(" INSTDONE: 0x%08x\n",
Jesse Barnes8a905232009-07-11 16:48:03 -04001294 I915_READ(INSTDONE_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07001295 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
1296 pr_err(" INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1297 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04001298 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001299 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001300 }
1301 }
1302
1303 I915_WRITE(EIR, eir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001304 POSTING_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001305 eir = I915_READ(EIR);
1306 if (eir) {
1307 /*
1308 * some errors might have become stuck,
1309 * mask them.
1310 */
1311 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
1312 I915_WRITE(EMR, I915_READ(EMR) | eir);
1313 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
1314 }
Chris Wilson35aed2e2010-05-27 13:18:12 +01001315}
1316
1317/**
1318 * i915_handle_error - handle an error interrupt
1319 * @dev: drm device
1320 *
1321 * Do some basic checking of regsiter state at error interrupt time and
1322 * dump it to the syslog. Also call i915_capture_error_state() to make
1323 * sure we get a record and make it available in debugfs. Fire a uevent
1324 * so userspace knows something bad happened (should trigger collection
1325 * of a ring dump etc.).
1326 */
Chris Wilson527f9e92010-11-11 01:16:58 +00001327void i915_handle_error(struct drm_device *dev, bool wedged)
Chris Wilson35aed2e2010-05-27 13:18:12 +01001328{
1329 struct drm_i915_private *dev_priv = dev->dev_private;
1330
1331 i915_capture_error_state(dev);
1332 i915_report_and_clear_eir(dev);
Jesse Barnes8a905232009-07-11 16:48:03 -04001333
Ben Gamariba1234d2009-09-14 17:48:47 -04001334 if (wedged) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001335 INIT_COMPLETION(dev_priv->error_completion);
Ben Gamariba1234d2009-09-14 17:48:47 -04001336 atomic_set(&dev_priv->mm.wedged, 1);
1337
Ben Gamari11ed50e2009-09-14 17:48:45 -04001338 /*
1339 * Wakeup waiting processes so they don't hang
1340 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001341 wake_up_all(&dev_priv->ring[RCS].irq_queue);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001342 if (HAS_BSD(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001343 wake_up_all(&dev_priv->ring[VCS].irq_queue);
Chris Wilson549f7362010-10-19 11:19:32 +01001344 if (HAS_BLT(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001345 wake_up_all(&dev_priv->ring[BCS].irq_queue);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001346 }
1347
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001348 queue_work(dev_priv->wq, &dev_priv->error_work);
Jesse Barnes8a905232009-07-11 16:48:03 -04001349}
1350
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001351static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
1352{
1353 drm_i915_private_t *dev_priv = dev->dev_private;
1354 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1355 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00001356 struct drm_i915_gem_object *obj;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001357 struct intel_unpin_work *work;
1358 unsigned long flags;
1359 bool stall_detected;
1360
1361 /* Ignore early vblank irqs */
1362 if (intel_crtc == NULL)
1363 return;
1364
1365 spin_lock_irqsave(&dev->event_lock, flags);
1366 work = intel_crtc->unpin_work;
1367
1368 if (work == NULL || work->pending || !work->enable_stall_check) {
1369 /* Either the pending flip IRQ arrived, or we're too early. Don't check */
1370 spin_unlock_irqrestore(&dev->event_lock, flags);
1371 return;
1372 }
1373
1374 /* Potential stall - if we see that the flip has happened, assume a missed interrupt */
Chris Wilson05394f32010-11-08 19:18:58 +00001375 obj = work->pending_flip_obj;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001376 if (INTEL_INFO(dev)->gen >= 4) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001377 int dspsurf = DSPSURF(intel_crtc->plane);
Chris Wilson05394f32010-11-08 19:18:58 +00001378 stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001379 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001380 int dspaddr = DSPADDR(intel_crtc->plane);
Chris Wilson05394f32010-11-08 19:18:58 +00001381 stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001382 crtc->y * crtc->fb->pitches[0] +
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001383 crtc->x * crtc->fb->bits_per_pixel/8);
1384 }
1385
1386 spin_unlock_irqrestore(&dev->event_lock, flags);
1387
1388 if (stall_detected) {
1389 DRM_DEBUG_DRIVER("Pageflip stall detected\n");
1390 intel_prepare_page_flip(dev, intel_crtc->plane);
1391 }
1392}
1393
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001394static irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001396 struct drm_device *dev = (struct drm_device *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001398 struct drm_i915_master_private *master_priv;
Eric Anholtcdfbc412008-11-04 15:50:30 -08001399 u32 iir, new_iir;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001400 u32 pipe_stats[I915_MAX_PIPES];
Keith Packard05eff842008-11-19 14:03:05 -08001401 u32 vblank_status;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001402 int vblank = 0;
Keith Packard7c463582008-11-04 02:03:27 -08001403 unsigned long irqflags;
Keith Packard05eff842008-11-19 14:03:05 -08001404 int irq_received;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001405 int ret = IRQ_NONE, pipe;
1406 bool blc_event = false;
Dave Airlieaf6061a2008-05-07 12:15:39 +10001407
Eric Anholt630681d2008-10-06 15:14:12 -07001408 atomic_inc(&dev_priv->irq_received);
1409
Eric Anholted4cb412008-07-29 12:10:39 -07001410 iir = I915_READ(IIR);
Dave Airlieaf6061a2008-05-07 12:15:39 +10001411
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001412 if (INTEL_INFO(dev)->gen >= 4)
Jesse Barnesd874bcf2010-06-30 13:16:00 -07001413 vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
Jesse Barnese25e6602010-06-30 13:15:19 -07001414 else
Jesse Barnesd874bcf2010-06-30 13:16:00 -07001415 vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Keith Packard05eff842008-11-19 14:03:05 -08001417 for (;;) {
1418 irq_received = iir != 0;
1419
1420 /* Can't rely on pipestat interrupt bit in iir as it might
1421 * have been cleared after the pipestat interrupt was received.
1422 * It doesn't set the bit in iir again, but it still produces
1423 * interrupts (for non-MSI).
1424 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001425 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes8a905232009-07-11 16:48:03 -04001426 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Ben Gamariba1234d2009-09-14 17:48:47 -04001427 i915_handle_error(dev, false);
Jesse Barnes8a905232009-07-11 16:48:03 -04001428
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001429 for_each_pipe(pipe) {
1430 int reg = PIPESTAT(pipe);
1431 pipe_stats[pipe] = I915_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -08001432
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001433 /*
1434 * Clear the PIPE*STAT regs before the IIR
1435 */
1436 if (pipe_stats[pipe] & 0x8000ffff) {
1437 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1438 DRM_DEBUG_DRIVER("pipe %c underrun\n",
1439 pipe_name(pipe));
1440 I915_WRITE(reg, pipe_stats[pipe]);
1441 irq_received = 1;
1442 }
Eric Anholtcdfbc412008-11-04 15:50:30 -08001443 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001444 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Keith Packard05eff842008-11-19 14:03:05 -08001445
1446 if (!irq_received)
1447 break;
1448
1449 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Jesse Barnes5ca58282009-03-31 14:11:15 -07001451 /* Consume port. Then clear IIR or we'll miss events */
1452 if ((I915_HAS_HOTPLUG(dev)) &&
1453 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
1454 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1455
Zhao Yakui44d98a62009-10-09 11:39:40 +08001456 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
Jesse Barnes5ca58282009-03-31 14:11:15 -07001457 hotplug_status);
1458 if (hotplug_status & dev_priv->hotplug_supported_mask)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001459 queue_work(dev_priv->wq,
1460 &dev_priv->hotplug_work);
Jesse Barnes5ca58282009-03-31 14:11:15 -07001461
1462 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
1463 I915_READ(PORT_HOTPLUG_STAT);
1464 }
1465
Eric Anholtcdfbc412008-11-04 15:50:30 -08001466 I915_WRITE(IIR, iir);
1467 new_iir = I915_READ(IIR); /* Flush posted writes */
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001468
Dave Airlie7c1c2872008-11-28 14:22:24 +10001469 if (dev->primary->master) {
1470 master_priv = dev->primary->master->driver_priv;
1471 if (master_priv->sarea_priv)
1472 master_priv->sarea_priv->last_dispatch =
1473 READ_BREADCRUMB(dev_priv);
1474 }
Keith Packard7c463582008-11-04 02:03:27 -08001475
Chris Wilson549f7362010-10-19 11:19:32 +01001476 if (iir & I915_USER_INTERRUPT)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001477 notify_ring(dev, &dev_priv->ring[RCS]);
1478 if (iir & I915_BSD_USER_INTERRUPT)
1479 notify_ring(dev, &dev_priv->ring[VCS]);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001480
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001481 if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001482 intel_prepare_page_flip(dev, 0);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001483 if (dev_priv->flip_pending_is_done)
1484 intel_finish_page_flip_plane(dev, 0);
1485 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001486
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001487 if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
Jesse Barnes70565d02010-07-01 04:45:43 -07001488 intel_prepare_page_flip(dev, 1);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001489 if (dev_priv->flip_pending_is_done)
1490 intel_finish_page_flip_plane(dev, 1);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001491 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001492
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001493 for_each_pipe(pipe) {
1494 if (pipe_stats[pipe] & vblank_status &&
1495 drm_handle_vblank(dev, pipe)) {
1496 vblank++;
1497 if (!dev_priv->flip_pending_is_done) {
1498 i915_pageflip_stall_check(dev, pipe);
1499 intel_finish_page_flip(dev, pipe);
1500 }
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001501 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001502
1503 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1504 blc_event = true;
Eric Anholtcdfbc412008-11-04 15:50:30 -08001505 }
Eric Anholt673a3942008-07-30 12:06:12 -07001506
Keith Packard7c463582008-11-04 02:03:27 -08001507
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001508 if (blc_event || (iir & I915_ASLE_INTERRUPT))
Chris Wilson3b617962010-08-24 09:02:58 +01001509 intel_opregion_asle_intr(dev);
Keith Packard7c463582008-11-04 02:03:27 -08001510
Eric Anholtcdfbc412008-11-04 15:50:30 -08001511 /* With MSI, interrupts are only generated when iir
1512 * transitions from zero to nonzero. If another bit got
1513 * set while we were handling the existing iir bits, then
1514 * we would never get another interrupt.
1515 *
1516 * This is fine on non-MSI as well, as if we hit this path
1517 * we avoid exiting the interrupt handler only to generate
1518 * another one.
1519 *
1520 * Note that for MSI this could cause a stray interrupt report
1521 * if an interrupt landed in the time between writing IIR and
1522 * the posting read. This should be rare enough to never
1523 * trigger the 99% of 100,000 interrupts test for disabling
1524 * stray interrupts.
1525 */
1526 iir = new_iir;
Keith Packard05eff842008-11-19 14:03:05 -08001527 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001528
Keith Packard05eff842008-11-19 14:03:05 -08001529 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Dave Airlieaf6061a2008-05-07 12:15:39 +10001532static int i915_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001535 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 i915_kernel_lost_context(dev);
1538
Zhao Yakui44d98a62009-10-09 11:39:40 +08001539 DRM_DEBUG_DRIVER("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Kristian Høgsbergc99b0582008-08-20 11:20:13 -04001541 dev_priv->counter++;
Alan Hourihanec29b6692006-08-12 16:29:24 +10001542 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -04001543 dev_priv->counter = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001544 if (master_priv->sarea_priv)
1545 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Alan Hourihanec29b6692006-08-12 16:29:24 +10001546
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001547 if (BEGIN_LP_RING(4) == 0) {
1548 OUT_RING(MI_STORE_DWORD_INDEX);
1549 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1550 OUT_RING(dev_priv->counter);
1551 OUT_RING(MI_USER_INTERRUPT);
1552 ADVANCE_LP_RING();
1553 }
Dave Airliebc5f4522007-11-05 12:50:58 +10001554
Alan Hourihanec29b6692006-08-12 16:29:24 +10001555 return dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Dave Airlie84b1fd12007-07-11 15:53:27 +10001558static int i915_wait_irq(struct drm_device * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001561 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int ret = 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001563 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Zhao Yakui44d98a62009-10-09 11:39:40 +08001565 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 READ_BREADCRUMB(dev_priv));
1567
Eric Anholted4cb412008-07-29 12:10:39 -07001568 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
Dave Airlie7c1c2872008-11-28 14:22:24 +10001569 if (master_priv->sarea_priv)
1570 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Dave Airlie7c1c2872008-11-28 14:22:24 +10001574 if (master_priv->sarea_priv)
1575 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001577 if (ring->irq_get(ring)) {
1578 DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1579 READ_BREADCRUMB(dev_priv) >= irq_nr);
1580 ring->irq_put(ring);
Chris Wilson5a9a8d12011-01-23 13:03:24 +00001581 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
1582 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Eric Anholt20caafa2007-08-25 19:22:43 +10001584 if (ret == -EBUSY) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001585 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1587 }
1588
Dave Airlieaf6061a2008-05-07 12:15:39 +10001589 return ret;
1590}
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592/* Needs the lock as it touches the ring.
1593 */
Eric Anholtc153f452007-09-03 12:06:45 +10001594int i915_irq_emit(struct drm_device *dev, void *data,
1595 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001598 drm_i915_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 int result;
1600
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001601 if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001602 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001603 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Eric Anholt299eb932009-02-24 22:14:12 -08001605
1606 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1607
Eric Anholt546b0972008-09-01 16:45:29 -07001608 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 result = i915_emit_irq(dev);
Eric Anholt546b0972008-09-01 16:45:29 -07001610 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Eric Anholtc153f452007-09-03 12:06:45 +10001612 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001614 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616
1617 return 0;
1618}
1619
1620/* Doesn't need the hardware lock.
1621 */
Eric Anholtc153f452007-09-03 12:06:45 +10001622int i915_irq_wait(struct drm_device *dev, void *data,
1623 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001626 drm_i915_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001629 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001630 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632
Eric Anholtc153f452007-09-03 12:06:45 +10001633 return i915_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Keith Packard42f52ef2008-10-18 19:39:29 -07001636/* Called from drm generic code, passed 'crtc' which
1637 * we use as a pipe index
1638 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001639static int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001640{
1641 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001642 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001643
Chris Wilson5eddb702010-09-11 13:48:45 +01001644 if (!i915_pipe_enabled(dev, pipe))
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001645 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001646
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001647 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07001648 if (INTEL_INFO(dev)->gen >= 4)
Keith Packard7c463582008-11-04 02:03:27 -08001649 i915_enable_pipestat(dev_priv, pipe,
1650 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -07001651 else
Keith Packard7c463582008-11-04 02:03:27 -08001652 i915_enable_pipestat(dev_priv, pipe,
1653 PIPE_VBLANK_INTERRUPT_ENABLE);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001654
1655 /* maintain vblank delivery even in deep C-states */
1656 if (dev_priv->info->gen == 3)
1657 I915_WRITE(INSTPM, INSTPM_AGPBUSY_DIS << 16);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001658 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001659
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001660 return 0;
1661}
1662
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001663static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07001664{
1665 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1666 unsigned long irqflags;
1667
1668 if (!i915_pipe_enabled(dev, pipe))
1669 return -EINVAL;
1670
1671 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1672 ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
Akshay Joshi0206e352011-08-16 15:34:10 -04001673 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
Jesse Barnesf796cf82011-04-07 13:58:17 -07001674 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1675
1676 return 0;
1677}
1678
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001679static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001680{
1681 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1682 unsigned long irqflags;
1683
1684 if (!i915_pipe_enabled(dev, pipe))
1685 return -EINVAL;
1686
1687 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1688 ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1689 DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1690 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1691
1692 return 0;
1693}
1694
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001695static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
1696{
1697 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1698 unsigned long irqflags;
1699 u32 dpfl, imr;
1700
1701 if (!i915_pipe_enabled(dev, pipe))
1702 return -EINVAL;
1703
1704 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1705 dpfl = I915_READ(VLV_DPFLIPSTAT);
1706 imr = I915_READ(VLV_IMR);
1707 if (pipe == 0) {
1708 dpfl |= PIPEA_VBLANK_INT_EN;
1709 imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
1710 } else {
1711 dpfl |= PIPEA_VBLANK_INT_EN;
1712 imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
1713 }
1714 I915_WRITE(VLV_DPFLIPSTAT, dpfl);
1715 I915_WRITE(VLV_IMR, imr);
1716 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1717
1718 return 0;
1719}
1720
Keith Packard42f52ef2008-10-18 19:39:29 -07001721/* Called from drm generic code, passed 'crtc' which
1722 * we use as a pipe index
1723 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001724static void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001725{
1726 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001727 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001728
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001729 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001730 if (dev_priv->info->gen == 3)
1731 I915_WRITE(INSTPM,
1732 INSTPM_AGPBUSY_DIS << 16 | INSTPM_AGPBUSY_DIS);
1733
Jesse Barnesf796cf82011-04-07 13:58:17 -07001734 i915_disable_pipestat(dev_priv, pipe,
1735 PIPE_VBLANK_INTERRUPT_ENABLE |
1736 PIPE_START_VBLANK_INTERRUPT_ENABLE);
1737 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1738}
1739
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001740static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07001741{
1742 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1743 unsigned long irqflags;
1744
1745 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1746 ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
Akshay Joshi0206e352011-08-16 15:34:10 -04001747 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001748 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001749}
1750
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001751static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001752{
1753 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1754 unsigned long irqflags;
1755
1756 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1757 ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1758 DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1759 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1760}
1761
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001762static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
1763{
1764 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1765 unsigned long irqflags;
1766 u32 dpfl, imr;
1767
1768 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1769 dpfl = I915_READ(VLV_DPFLIPSTAT);
1770 imr = I915_READ(VLV_IMR);
1771 if (pipe == 0) {
1772 dpfl &= ~PIPEA_VBLANK_INT_EN;
1773 imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
1774 } else {
1775 dpfl &= ~PIPEB_VBLANK_INT_EN;
1776 imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
1777 }
1778 I915_WRITE(VLV_IMR, imr);
1779 I915_WRITE(VLV_DPFLIPSTAT, dpfl);
1780 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1781}
1782
1783
Dave Airlie702880f2006-06-24 17:07:34 +10001784/* Set the vblank monitor pipe
1785 */
Eric Anholtc153f452007-09-03 12:06:45 +10001786int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1787 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +10001788{
Dave Airlie702880f2006-06-24 17:07:34 +10001789 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie702880f2006-06-24 17:07:34 +10001790
1791 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001792 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001793 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +10001794 }
1795
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +10001796 return 0;
Dave Airlie702880f2006-06-24 17:07:34 +10001797}
1798
Eric Anholtc153f452007-09-03 12:06:45 +10001799int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1800 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +10001801{
Dave Airlie702880f2006-06-24 17:07:34 +10001802 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001803 drm_i915_vblank_pipe_t *pipe = data;
Dave Airlie702880f2006-06-24 17:07:34 +10001804
1805 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001806 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001807 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +10001808 }
1809
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001810 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Eric Anholtc153f452007-09-03 12:06:45 +10001811
Dave Airlie702880f2006-06-24 17:07:34 +10001812 return 0;
1813}
1814
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +10001815/**
1816 * Schedule buffer swap at given vertical blank.
1817 */
Eric Anholtc153f452007-09-03 12:06:45 +10001818int i915_vblank_swap(struct drm_device *dev, void *data,
1819 struct drm_file *file_priv)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +10001820{
Eric Anholtbd95e0a2008-11-04 12:01:24 -08001821 /* The delayed swap mechanism was fundamentally racy, and has been
1822 * removed. The model was that the client requested a delayed flip/swap
1823 * from the kernel, then waited for vblank before continuing to perform
1824 * rendering. The problem was that the kernel might wake the client
1825 * up before it dispatched the vblank swap (since the lock has to be
1826 * held while touching the ringbuffer), in which case the client would
1827 * clear and start the next frame before the swap occurred, and
1828 * flicker would occur in addition to likely missing the vblank.
1829 *
1830 * In the absence of this ioctl, userland falls back to a correct path
1831 * of waiting for a vblank, then dispatching the swap on its own.
1832 * Context switching to userland and back is plenty fast enough for
1833 * meeting the requirements of vblank swapping.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001834 */
Eric Anholtbd95e0a2008-11-04 12:01:24 -08001835 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +10001836}
1837
Chris Wilson893eead2010-10-27 14:44:35 +01001838static u32
1839ring_last_seqno(struct intel_ring_buffer *ring)
Zou Nan hai852835f2010-05-21 09:08:56 +08001840{
Chris Wilson893eead2010-10-27 14:44:35 +01001841 return list_entry(ring->request_list.prev,
1842 struct drm_i915_gem_request, list)->seqno;
1843}
1844
1845static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1846{
1847 if (list_empty(&ring->request_list) ||
1848 i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1849 /* Issue a wake-up to catch stuck h/w. */
Chris Wilsonb2223492010-10-27 15:27:33 +01001850 if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
Chris Wilson893eead2010-10-27 14:44:35 +01001851 DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1852 ring->name,
Chris Wilsonb2223492010-10-27 15:27:33 +01001853 ring->waiting_seqno,
Chris Wilson893eead2010-10-27 14:44:35 +01001854 ring->get_seqno(ring));
1855 wake_up_all(&ring->irq_queue);
1856 *err = true;
1857 }
1858 return true;
1859 }
1860 return false;
Ben Gamarif65d9422009-09-14 17:48:44 -04001861}
1862
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001863static bool kick_ring(struct intel_ring_buffer *ring)
1864{
1865 struct drm_device *dev = ring->dev;
1866 struct drm_i915_private *dev_priv = dev->dev_private;
1867 u32 tmp = I915_READ_CTL(ring);
1868 if (tmp & RING_WAIT) {
1869 DRM_ERROR("Kicking stuck wait on %s\n",
1870 ring->name);
1871 I915_WRITE_CTL(ring, tmp);
1872 return true;
1873 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001874 return false;
1875}
1876
Ben Gamarif65d9422009-09-14 17:48:44 -04001877/**
1878 * This is called when the chip hasn't reported back with completed
1879 * batchbuffers in a long time. The first time this is called we simply record
1880 * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1881 * again, we assume the chip is wedged and try to fix it.
1882 */
1883void i915_hangcheck_elapsed(unsigned long data)
1884{
1885 struct drm_device *dev = (struct drm_device *)data;
1886 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter097354e2011-11-27 18:58:17 +01001887 uint32_t acthd, instdone, instdone1, acthd_bsd, acthd_blt;
Chris Wilson893eead2010-10-27 14:44:35 +01001888 bool err = false;
1889
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001890 if (!i915_enable_hangcheck)
1891 return;
1892
Chris Wilson893eead2010-10-27 14:44:35 +01001893 /* If all work is done then ACTHD clearly hasn't advanced. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001894 if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
1895 i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
1896 i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
Chris Wilson893eead2010-10-27 14:44:35 +01001897 dev_priv->hangcheck_count = 0;
1898 if (err)
1899 goto repeat;
1900 return;
1901 }
Eric Anholtb9201c12010-01-08 14:25:16 -08001902
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001903 if (INTEL_INFO(dev)->gen < 4) {
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001904 instdone = I915_READ(INSTDONE);
1905 instdone1 = 0;
1906 } else {
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001907 instdone = I915_READ(INSTDONE_I965);
1908 instdone1 = I915_READ(INSTDONE1);
1909 }
Daniel Vetter097354e2011-11-27 18:58:17 +01001910 acthd = intel_ring_get_active_head(&dev_priv->ring[RCS]);
1911 acthd_bsd = HAS_BSD(dev) ?
1912 intel_ring_get_active_head(&dev_priv->ring[VCS]) : 0;
1913 acthd_blt = HAS_BLT(dev) ?
1914 intel_ring_get_active_head(&dev_priv->ring[BCS]) : 0;
Ben Gamarif65d9422009-09-14 17:48:44 -04001915
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001916 if (dev_priv->last_acthd == acthd &&
Daniel Vetter097354e2011-11-27 18:58:17 +01001917 dev_priv->last_acthd_bsd == acthd_bsd &&
1918 dev_priv->last_acthd_blt == acthd_blt &&
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001919 dev_priv->last_instdone == instdone &&
1920 dev_priv->last_instdone1 == instdone1) {
1921 if (dev_priv->hangcheck_count++ > 1) {
1922 DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
Daniel Vetter653d7be2011-12-14 13:57:21 +01001923 i915_handle_error(dev, true);
Chris Wilson8c80b592010-08-08 20:38:12 +01001924
1925 if (!IS_GEN2(dev)) {
1926 /* Is the chip hanging on a WAIT_FOR_EVENT?
1927 * If so we can simply poke the RB_WAIT bit
1928 * and break the hang. This should work on
1929 * all but the second generation chipsets.
1930 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001931 if (kick_ring(&dev_priv->ring[RCS]))
Chris Wilson893eead2010-10-27 14:44:35 +01001932 goto repeat;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001933
1934 if (HAS_BSD(dev) &&
1935 kick_ring(&dev_priv->ring[VCS]))
1936 goto repeat;
1937
1938 if (HAS_BLT(dev) &&
1939 kick_ring(&dev_priv->ring[BCS]))
1940 goto repeat;
Chris Wilson8c80b592010-08-08 20:38:12 +01001941 }
1942
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001943 return;
1944 }
1945 } else {
1946 dev_priv->hangcheck_count = 0;
1947
1948 dev_priv->last_acthd = acthd;
Daniel Vetter097354e2011-11-27 18:58:17 +01001949 dev_priv->last_acthd_bsd = acthd_bsd;
1950 dev_priv->last_acthd_blt = acthd_blt;
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001951 dev_priv->last_instdone = instdone;
1952 dev_priv->last_instdone1 = instdone1;
1953 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001954
Chris Wilson893eead2010-10-27 14:44:35 +01001955repeat:
Ben Gamarif65d9422009-09-14 17:48:44 -04001956 /* Reset timer case chip hangs without another request being added */
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001957 mod_timer(&dev_priv->hangcheck_timer,
1958 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001959}
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961/* drm_dma.h hooks
1962*/
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001963static void ironlake_irq_preinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001964{
1965 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1966
Jesse Barnes46979952011-04-07 13:53:55 -07001967 atomic_set(&dev_priv->irq_received, 0);
1968
1969 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
1970 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
Jesse Barnes9e3c2562011-05-18 13:51:43 -07001971 if (IS_GEN6(dev) || IS_IVYBRIDGE(dev))
1972 INIT_WORK(&dev_priv->rps_work, gen6_pm_rps_work);
Jesse Barnes46979952011-04-07 13:53:55 -07001973
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001974 I915_WRITE(HWSTAM, 0xeffe);
Daniel Vetterbdfcdb62012-01-05 01:05:26 +01001975
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001976 /* XXX hotplug from PCH */
1977
1978 I915_WRITE(DEIMR, 0xffffffff);
1979 I915_WRITE(DEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001980 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001981
1982 /* and GT */
1983 I915_WRITE(GTIMR, 0xffffffff);
1984 I915_WRITE(GTIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001985 POSTING_READ(GTIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00001986
1987 /* south display irq */
1988 I915_WRITE(SDEIMR, 0xffffffff);
1989 I915_WRITE(SDEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001990 POSTING_READ(SDEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001991}
1992
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001993static void valleyview_irq_preinstall(struct drm_device *dev)
1994{
1995 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1996 int pipe;
1997
1998 atomic_set(&dev_priv->irq_received, 0);
1999
2000 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
2001 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
2002
2003 /* VLV magic */
2004 I915_WRITE(VLV_IMR, 0);
2005 I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
2006 I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
2007 I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
2008
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002009 /* and GT */
2010 I915_WRITE(GTIIR, I915_READ(GTIIR));
2011 I915_WRITE(GTIIR, I915_READ(GTIIR));
2012 I915_WRITE(GTIMR, 0xffffffff);
2013 I915_WRITE(GTIER, 0x0);
2014 POSTING_READ(GTIER);
2015
2016 I915_WRITE(DPINVGTT, 0xff);
2017
2018 I915_WRITE(PORT_HOTPLUG_EN, 0);
2019 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2020 for_each_pipe(pipe)
2021 I915_WRITE(PIPESTAT(pipe), 0xffff);
2022 I915_WRITE(VLV_IIR, 0xffffffff);
2023 I915_WRITE(VLV_IMR, 0xffffffff);
2024 I915_WRITE(VLV_IER, 0x0);
2025 POSTING_READ(VLV_IER);
2026}
2027
Keith Packard7fe0b972011-09-19 13:31:02 -07002028/*
2029 * Enable digital hotplug on the PCH, and configure the DP short pulse
2030 * duration to 2ms (which is the minimum in the Display Port spec)
2031 *
2032 * This register is the same on all known PCH chips.
2033 */
2034
2035static void ironlake_enable_pch_hotplug(struct drm_device *dev)
2036{
2037 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2038 u32 hotplug;
2039
2040 hotplug = I915_READ(PCH_PORT_HOTPLUG);
2041 hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
2042 hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
2043 hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
2044 hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
2045 I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
2046}
2047
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002048static int ironlake_irq_postinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002049{
2050 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2051 /* enable kind of interrupts always enabled */
Jesse Barnes013d5aa2010-01-29 11:18:31 -08002052 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2053 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002054 u32 render_irqs;
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01002055 u32 hotplug_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002056
Jesse Barnes46979952011-04-07 13:53:55 -07002057 DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
2058 if (HAS_BSD(dev))
2059 DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
2060 if (HAS_BLT(dev))
2061 DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
2062
2063 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
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
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01002090 if (HAS_PCH_CPT(dev)) {
Chris Wilson9035a972011-02-16 09:36:05 +00002091 hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
2092 SDE_PORTB_HOTPLUG_CPT |
2093 SDE_PORTC_HOTPLUG_CPT |
2094 SDE_PORTD_HOTPLUG_CPT);
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01002095 } else {
Chris Wilson9035a972011-02-16 09:36:05 +00002096 hotplug_mask = (SDE_CRT_HOTPLUG |
2097 SDE_PORTB_HOTPLUG |
2098 SDE_PORTC_HOTPLUG |
2099 SDE_PORTD_HOTPLUG |
2100 SDE_AUX_MASK);
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01002101 }
2102
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002103 dev_priv->pch_irq_mask = ~hotplug_mask;
Zhenyu Wangc6501562009-11-03 18:57:21 +00002104
2105 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002106 I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
2107 I915_WRITE(SDEIER, hotplug_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002108 POSTING_READ(SDEIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00002109
Keith Packard7fe0b972011-09-19 13:31:02 -07002110 ironlake_enable_pch_hotplug(dev);
2111
Jesse Barnesf97108d2010-01-29 11:27:07 -08002112 if (IS_IRONLAKE_M(dev)) {
2113 /* Clear & enable PCU event interrupts */
2114 I915_WRITE(DEIIR, DE_PCU_EVENT);
2115 I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
2116 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2117 }
2118
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002119 return 0;
2120}
2121
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002122static int ivybridge_irq_postinstall(struct drm_device *dev)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002123{
2124 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2125 /* enable kind of interrupts always enabled */
2126 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
2127 DE_PCH_EVENT_IVB | DE_PLANEA_FLIP_DONE_IVB |
2128 DE_PLANEB_FLIP_DONE_IVB;
2129 u32 render_irqs;
2130 u32 hotplug_mask;
2131
2132 DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
2133 if (HAS_BSD(dev))
2134 DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
2135 if (HAS_BLT(dev))
2136 DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
2137
2138 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
2139 dev_priv->irq_mask = ~display_mask;
2140
2141 /* should always can generate irq */
2142 I915_WRITE(DEIIR, I915_READ(DEIIR));
2143 I915_WRITE(DEIMR, dev_priv->irq_mask);
2144 I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK_IVB |
2145 DE_PIPEB_VBLANK_IVB);
2146 POSTING_READ(DEIER);
2147
2148 dev_priv->gt_irq_mask = ~0;
2149
2150 I915_WRITE(GTIIR, I915_READ(GTIIR));
2151 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2152
Ben Widawskye2a1e2f2012-03-29 19:11:26 -07002153 render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
2154 GEN6_BLITTER_USER_INTERRUPT;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002155 I915_WRITE(GTIER, render_irqs);
2156 POSTING_READ(GTIER);
2157
2158 hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
2159 SDE_PORTB_HOTPLUG_CPT |
2160 SDE_PORTC_HOTPLUG_CPT |
2161 SDE_PORTD_HOTPLUG_CPT);
2162 dev_priv->pch_irq_mask = ~hotplug_mask;
2163
2164 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2165 I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
2166 I915_WRITE(SDEIER, hotplug_mask);
2167 POSTING_READ(SDEIER);
2168
Keith Packard7fe0b972011-09-19 13:31:02 -07002169 ironlake_enable_pch_hotplug(dev);
2170
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002171 return 0;
2172}
2173
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002174static int valleyview_irq_postinstall(struct drm_device *dev)
2175{
2176 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2177 u32 render_irqs;
2178 u32 enable_mask;
2179 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2180 u16 msid;
2181
2182 enable_mask = I915_DISPLAY_PORT_INTERRUPT;
2183 enable_mask |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
2184 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
2185
2186 dev_priv->irq_mask = ~enable_mask;
2187
2188
2189 DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
2190 DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
2191 DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
2192
2193 dev_priv->pipestat[0] = 0;
2194 dev_priv->pipestat[1] = 0;
2195
2196 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
2197
2198 /* Hack for broken MSIs on VLV */
2199 pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
2200 pci_read_config_word(dev->pdev, 0x98, &msid);
2201 msid &= 0xff; /* mask out delivery bits */
2202 msid |= (1<<14);
2203 pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
2204
2205 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
2206 I915_WRITE(VLV_IER, enable_mask);
2207 I915_WRITE(VLV_IIR, 0xffffffff);
2208 I915_WRITE(PIPESTAT(0), 0xffff);
2209 I915_WRITE(PIPESTAT(1), 0xffff);
2210 POSTING_READ(VLV_IER);
2211
2212 I915_WRITE(VLV_IIR, 0xffffffff);
2213 I915_WRITE(VLV_IIR, 0xffffffff);
2214
2215 render_irqs = GT_GEN6_BLT_FLUSHDW_NOTIFY_INTERRUPT |
2216 GT_GEN6_BLT_CS_ERROR_INTERRUPT |
Ben Widawskye2a1e2f2012-03-29 19:11:26 -07002217 GT_GEN6_BLT_USER_INTERRUPT |
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002218 GT_GEN6_BSD_USER_INTERRUPT |
2219 GT_GEN6_BSD_CS_ERROR_INTERRUPT |
2220 GT_GEN7_L3_PARITY_ERROR_INTERRUPT |
2221 GT_PIPE_NOTIFY |
2222 GT_RENDER_CS_ERROR_INTERRUPT |
2223 GT_SYNC_STATUS |
2224 GT_USER_INTERRUPT;
2225
2226 dev_priv->gt_irq_mask = ~render_irqs;
2227
2228 I915_WRITE(GTIIR, I915_READ(GTIIR));
2229 I915_WRITE(GTIIR, I915_READ(GTIIR));
2230 I915_WRITE(GTIMR, 0);
2231 I915_WRITE(GTIER, render_irqs);
2232 POSTING_READ(GTIER);
2233
2234 /* ack & enable invalid PTE error interrupts */
2235#if 0 /* FIXME: add support to irq handler for checking these bits */
2236 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
2237 I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
2238#endif
2239
2240 I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
2241#if 0 /* FIXME: check register definitions; some have moved */
2242 /* Note HDMI and DP share bits */
2243 if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2244 hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2245 if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2246 hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2247 if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2248 hotplug_en |= HDMID_HOTPLUG_INT_EN;
2249 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
2250 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2251 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
2252 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2253 if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2254 hotplug_en |= CRT_HOTPLUG_INT_EN;
2255 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2256 }
2257#endif
2258
2259 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2260
2261 return 0;
2262}
2263
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002264static void i915_driver_irq_preinstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002267 int pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Jesse Barnes79e53942008-11-07 14:24:08 -08002269 atomic_set(&dev_priv->irq_received, 0);
2270
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002271 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Jesse Barnes8a905232009-07-11 16:48:03 -04002272 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002273
Jesse Barnes5ca58282009-03-31 14:11:15 -07002274 if (I915_HAS_HOTPLUG(dev)) {
2275 I915_WRITE(PORT_HOTPLUG_EN, 0);
2276 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2277 }
2278
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002279 I915_WRITE(HWSTAM, 0xeffe);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002280 for_each_pipe(pipe)
2281 I915_WRITE(PIPESTAT(pipe), 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002282 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07002283 I915_WRITE(IER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002284 POSTING_READ(IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002287/*
2288 * Must be called after intel_modeset_init or hotplug interrupts won't be
2289 * enabled correctly.
2290 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002291static int i915_driver_irq_postinstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
2293 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes5ca58282009-03-31 14:11:15 -07002294 u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07002295 u32 error_mask;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002296
2297 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002298
Keith Packard7c463582008-11-04 02:03:27 -08002299 /* Unmask the interrupts that we always want on. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002300 dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01002301
Keith Packard7c463582008-11-04 02:03:27 -08002302 dev_priv->pipestat[0] = 0;
2303 dev_priv->pipestat[1] = 0;
2304
Jesse Barnes5ca58282009-03-31 14:11:15 -07002305 if (I915_HAS_HOTPLUG(dev)) {
Adam Jacksonc496fa12010-05-27 17:26:45 -04002306 /* Enable in IER... */
2307 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2308 /* and unmask in IMR */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002309 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
Adam Jacksonc496fa12010-05-27 17:26:45 -04002310 }
2311
2312 /*
2313 * Enable some error detection, note the instruction error mask
2314 * bit is reserved, so we leave it masked.
2315 */
2316 if (IS_G4X(dev)) {
2317 error_mask = ~(GM45_ERROR_PAGE_TABLE |
2318 GM45_ERROR_MEM_PRIV |
2319 GM45_ERROR_CP_PRIV |
2320 I915_ERROR_MEMORY_REFRESH);
2321 } else {
2322 error_mask = ~(I915_ERROR_PAGE_TABLE |
2323 I915_ERROR_MEMORY_REFRESH);
2324 }
2325 I915_WRITE(EMR, error_mask);
2326
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002327 I915_WRITE(IMR, dev_priv->irq_mask);
Adam Jacksonc496fa12010-05-27 17:26:45 -04002328 I915_WRITE(IER, enable_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002329 POSTING_READ(IER);
Adam Jacksonc496fa12010-05-27 17:26:45 -04002330
2331 if (I915_HAS_HOTPLUG(dev)) {
Jesse Barnes5ca58282009-03-31 14:11:15 -07002332 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2333
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002334 /* Note HDMI and DP share bits */
2335 if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2336 hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2337 if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2338 hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2339 if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2340 hotplug_en |= HDMID_HOTPLUG_INT_EN;
2341 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
2342 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2343 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
2344 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
Andy Lutomirski2d1c9752010-06-12 05:21:18 -04002345 if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002346 hotplug_en |= CRT_HOTPLUG_INT_EN;
Andy Lutomirski2d1c9752010-06-12 05:21:18 -04002347
2348 /* Programming the CRT detection parameters tends
2349 to generate a spurious hotplug event about three
2350 seconds later. So just do it once.
2351 */
2352 if (IS_G4X(dev))
2353 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2354 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2355 }
2356
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002357 /* Ignore TV since it's buggy */
2358
Jesse Barnes5ca58282009-03-31 14:11:15 -07002359 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Jesse Barnes5ca58282009-03-31 14:11:15 -07002360 }
2361
Chris Wilson3b617962010-08-24 09:02:58 +01002362 intel_opregion_enable_asle(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002363
2364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002367static void valleyview_irq_uninstall(struct drm_device *dev)
2368{
2369 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2370 int pipe;
2371
2372 if (!dev_priv)
2373 return;
2374
2375 dev_priv->vblank_pipe = 0;
2376
2377 for_each_pipe(pipe)
2378 I915_WRITE(PIPESTAT(pipe), 0xffff);
2379
2380 I915_WRITE(HWSTAM, 0xffffffff);
2381 I915_WRITE(PORT_HOTPLUG_EN, 0);
2382 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2383 for_each_pipe(pipe)
2384 I915_WRITE(PIPESTAT(pipe), 0xffff);
2385 I915_WRITE(VLV_IIR, 0xffffffff);
2386 I915_WRITE(VLV_IMR, 0xffffffff);
2387 I915_WRITE(VLV_IER, 0x0);
2388 POSTING_READ(VLV_IER);
2389}
2390
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002391static void ironlake_irq_uninstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002392{
2393 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes46979952011-04-07 13:53:55 -07002394
2395 if (!dev_priv)
2396 return;
2397
2398 dev_priv->vblank_pipe = 0;
2399
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002400 I915_WRITE(HWSTAM, 0xffffffff);
2401
2402 I915_WRITE(DEIMR, 0xffffffff);
2403 I915_WRITE(DEIER, 0x0);
2404 I915_WRITE(DEIIR, I915_READ(DEIIR));
2405
2406 I915_WRITE(GTIMR, 0xffffffff);
2407 I915_WRITE(GTIER, 0x0);
2408 I915_WRITE(GTIIR, I915_READ(GTIIR));
Keith Packard192aac1f2011-09-20 10:12:44 -07002409
2410 I915_WRITE(SDEIMR, 0xffffffff);
2411 I915_WRITE(SDEIER, 0x0);
2412 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002413}
2414
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002415static void i915_driver_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
2417 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002418 int pipe;
Dave Airlie91e37382006-02-18 15:17:04 +11002419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (!dev_priv)
2421 return;
2422
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002423 dev_priv->vblank_pipe = 0;
2424
Jesse Barnes5ca58282009-03-31 14:11:15 -07002425 if (I915_HAS_HOTPLUG(dev)) {
2426 I915_WRITE(PORT_HOTPLUG_EN, 0);
2427 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2428 }
2429
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002430 I915_WRITE(HWSTAM, 0xffffffff);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002431 for_each_pipe(pipe)
2432 I915_WRITE(PIPESTAT(pipe), 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002433 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07002434 I915_WRITE(IER, 0x0);
Dave Airlie91e37382006-02-18 15:17:04 +11002435
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002436 for_each_pipe(pipe)
2437 I915_WRITE(PIPESTAT(pipe),
2438 I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
Keith Packard7c463582008-11-04 02:03:27 -08002439 I915_WRITE(IIR, I915_READ(IIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002441
2442void intel_irq_init(struct drm_device *dev)
2443{
2444 dev->driver->get_vblank_counter = i915_get_vblank_counter;
2445 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002446 if (IS_G4X(dev) || IS_GEN5(dev) || IS_GEN6(dev) || IS_IVYBRIDGE(dev) ||
2447 IS_VALLEYVIEW(dev)) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002448 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2449 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2450 }
2451
Keith Packardc3613de2011-08-12 17:05:54 -07002452 if (drm_core_check_feature(dev, DRIVER_MODESET))
2453 dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2454 else
2455 dev->driver->get_vblank_timestamp = NULL;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002456 dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2457
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002458 if (IS_VALLEYVIEW(dev)) {
2459 dev->driver->irq_handler = valleyview_irq_handler;
2460 dev->driver->irq_preinstall = valleyview_irq_preinstall;
2461 dev->driver->irq_postinstall = valleyview_irq_postinstall;
2462 dev->driver->irq_uninstall = valleyview_irq_uninstall;
2463 dev->driver->enable_vblank = valleyview_enable_vblank;
2464 dev->driver->disable_vblank = valleyview_disable_vblank;
2465 } else if (IS_IVYBRIDGE(dev)) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002466 /* Share pre & uninstall handlers with ILK/SNB */
2467 dev->driver->irq_handler = ivybridge_irq_handler;
2468 dev->driver->irq_preinstall = ironlake_irq_preinstall;
2469 dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2470 dev->driver->irq_uninstall = ironlake_irq_uninstall;
2471 dev->driver->enable_vblank = ivybridge_enable_vblank;
2472 dev->driver->disable_vblank = ivybridge_disable_vblank;
2473 } else if (HAS_PCH_SPLIT(dev)) {
2474 dev->driver->irq_handler = ironlake_irq_handler;
2475 dev->driver->irq_preinstall = ironlake_irq_preinstall;
2476 dev->driver->irq_postinstall = ironlake_irq_postinstall;
2477 dev->driver->irq_uninstall = ironlake_irq_uninstall;
2478 dev->driver->enable_vblank = ironlake_enable_vblank;
2479 dev->driver->disable_vblank = ironlake_disable_vblank;
2480 } else {
2481 dev->driver->irq_preinstall = i915_driver_irq_preinstall;
2482 dev->driver->irq_postinstall = i915_driver_irq_postinstall;
2483 dev->driver->irq_uninstall = i915_driver_irq_uninstall;
2484 dev->driver->irq_handler = i915_driver_irq_handler;
2485 dev->driver->enable_vblank = i915_enable_vblank;
2486 dev->driver->disable_vblank = i915_disable_vblank;
2487 }
2488}