blob: 01fb65097977bac769132b30c9874dd7197853d1 [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
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700433static irqreturn_t valleyview_irq_handler(DRM_IRQ_ARGS)
434{
435 struct drm_device *dev = (struct drm_device *) arg;
436 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
437 u32 iir, gt_iir, pm_iir;
438 irqreturn_t ret = IRQ_NONE;
439 unsigned long irqflags;
440 int pipe;
441 u32 pipe_stats[I915_MAX_PIPES];
442 u32 vblank_status;
443 int vblank = 0;
444 bool blc_event;
445
446 atomic_inc(&dev_priv->irq_received);
447
448 vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS |
449 PIPE_VBLANK_INTERRUPT_STATUS;
450
451 while (true) {
452 iir = I915_READ(VLV_IIR);
453 gt_iir = I915_READ(GTIIR);
454 pm_iir = I915_READ(GEN6_PMIIR);
455
456 if (gt_iir == 0 && pm_iir == 0 && iir == 0)
457 goto out;
458
459 ret = IRQ_HANDLED;
460
461 if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
462 notify_ring(dev, &dev_priv->ring[RCS]);
463 if (gt_iir & GT_GEN6_BSD_USER_INTERRUPT)
464 notify_ring(dev, &dev_priv->ring[VCS]);
465 if (gt_iir & GT_BLT_USER_INTERRUPT)
466 notify_ring(dev, &dev_priv->ring[BCS]);
467
468 if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
469 GT_GEN6_BSD_CS_ERROR_INTERRUPT |
470 GT_RENDER_CS_ERROR_INTERRUPT)) {
471 DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
472 i915_handle_error(dev, false);
473 }
474
475 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
476 for_each_pipe(pipe) {
477 int reg = PIPESTAT(pipe);
478 pipe_stats[pipe] = I915_READ(reg);
479
480 /*
481 * Clear the PIPE*STAT regs before the IIR
482 */
483 if (pipe_stats[pipe] & 0x8000ffff) {
484 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
485 DRM_DEBUG_DRIVER("pipe %c underrun\n",
486 pipe_name(pipe));
487 I915_WRITE(reg, pipe_stats[pipe]);
488 }
489 }
490 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
491
492 /* Consume port. Then clear IIR or we'll miss events */
493 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
494 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
495
496 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
497 hotplug_status);
498 if (hotplug_status & dev_priv->hotplug_supported_mask)
499 queue_work(dev_priv->wq,
500 &dev_priv->hotplug_work);
501
502 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
503 I915_READ(PORT_HOTPLUG_STAT);
504 }
505
506
507 if (iir & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT) {
508 drm_handle_vblank(dev, 0);
509 vblank++;
510 if (!dev_priv->flip_pending_is_done) {
511 intel_finish_page_flip(dev, 0);
512 }
513 }
514
515 if (iir & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT) {
516 drm_handle_vblank(dev, 1);
517 vblank++;
518 if (!dev_priv->flip_pending_is_done) {
519 intel_finish_page_flip(dev, 0);
520 }
521 }
522
523 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
524 blc_event = true;
525
526 if (pm_iir & GEN6_PM_DEFERRED_EVENTS) {
527 unsigned long flags;
528 spin_lock_irqsave(&dev_priv->rps_lock, flags);
529 WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
530 dev_priv->pm_iir |= pm_iir;
531 I915_WRITE(GEN6_PMIMR, dev_priv->pm_iir);
532 POSTING_READ(GEN6_PMIMR);
533 spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
534 queue_work(dev_priv->wq, &dev_priv->rps_work);
535 }
536
537 I915_WRITE(GTIIR, gt_iir);
538 I915_WRITE(GEN6_PMIIR, pm_iir);
539 I915_WRITE(VLV_IIR, iir);
540 }
541
542out:
543 return ret;
544}
545
Jesse Barnes776ad802011-01-04 15:09:39 -0800546static void pch_irq_handler(struct drm_device *dev)
547{
548 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
549 u32 pch_iir;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800550 int pipe;
Jesse Barnes776ad802011-01-04 15:09:39 -0800551
552 pch_iir = I915_READ(SDEIIR);
553
554 if (pch_iir & SDE_AUDIO_POWER_MASK)
555 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
556 (pch_iir & SDE_AUDIO_POWER_MASK) >>
557 SDE_AUDIO_POWER_SHIFT);
558
559 if (pch_iir & SDE_GMBUS)
560 DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
561
562 if (pch_iir & SDE_AUDIO_HDCP_MASK)
563 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
564
565 if (pch_iir & SDE_AUDIO_TRANS_MASK)
566 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
567
568 if (pch_iir & SDE_POISON)
569 DRM_ERROR("PCH poison interrupt\n");
570
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800571 if (pch_iir & SDE_FDI_MASK)
572 for_each_pipe(pipe)
573 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
574 pipe_name(pipe),
575 I915_READ(FDI_RX_IIR(pipe)));
Jesse Barnes776ad802011-01-04 15:09:39 -0800576
577 if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
578 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
579
580 if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
581 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
582
583 if (pch_iir & SDE_TRANSB_FIFO_UNDER)
584 DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
585 if (pch_iir & SDE_TRANSA_FIFO_UNDER)
586 DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
587}
588
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700589static irqreturn_t ivybridge_irq_handler(DRM_IRQ_ARGS)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700590{
591 struct drm_device *dev = (struct drm_device *) arg;
592 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
593 int ret = IRQ_NONE;
594 u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
595 struct drm_i915_master_private *master_priv;
596
597 atomic_inc(&dev_priv->irq_received);
598
599 /* disable master interrupt before clearing iir */
600 de_ier = I915_READ(DEIER);
601 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
602 POSTING_READ(DEIER);
603
604 de_iir = I915_READ(DEIIR);
605 gt_iir = I915_READ(GTIIR);
606 pch_iir = I915_READ(SDEIIR);
607 pm_iir = I915_READ(GEN6_PMIIR);
608
609 if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 && pm_iir == 0)
610 goto done;
611
612 ret = IRQ_HANDLED;
613
614 if (dev->primary->master) {
615 master_priv = dev->primary->master->driver_priv;
616 if (master_priv->sarea_priv)
617 master_priv->sarea_priv->last_dispatch =
618 READ_BREADCRUMB(dev_priv);
619 }
620
621 if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
622 notify_ring(dev, &dev_priv->ring[RCS]);
623 if (gt_iir & GT_GEN6_BSD_USER_INTERRUPT)
624 notify_ring(dev, &dev_priv->ring[VCS]);
625 if (gt_iir & GT_BLT_USER_INTERRUPT)
626 notify_ring(dev, &dev_priv->ring[BCS]);
627
628 if (de_iir & DE_GSE_IVB)
629 intel_opregion_gse_intr(dev);
630
631 if (de_iir & DE_PLANEA_FLIP_DONE_IVB) {
632 intel_prepare_page_flip(dev, 0);
633 intel_finish_page_flip_plane(dev, 0);
634 }
635
636 if (de_iir & DE_PLANEB_FLIP_DONE_IVB) {
637 intel_prepare_page_flip(dev, 1);
638 intel_finish_page_flip_plane(dev, 1);
639 }
640
641 if (de_iir & DE_PIPEA_VBLANK_IVB)
642 drm_handle_vblank(dev, 0);
643
Dan Carpenterf6b07f42011-05-25 12:56:56 +0300644 if (de_iir & DE_PIPEB_VBLANK_IVB)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700645 drm_handle_vblank(dev, 1);
646
647 /* check event from PCH */
648 if (de_iir & DE_PCH_EVENT_IVB) {
649 if (pch_iir & SDE_HOTPLUG_MASK_CPT)
650 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
651 pch_irq_handler(dev);
652 }
653
654 if (pm_iir & GEN6_PM_DEFERRED_EVENTS) {
655 unsigned long flags;
656 spin_lock_irqsave(&dev_priv->rps_lock, flags);
657 WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700658 dev_priv->pm_iir |= pm_iir;
Daniel Vetter4fb066a2011-09-08 14:00:20 +0200659 I915_WRITE(GEN6_PMIMR, dev_priv->pm_iir);
660 POSTING_READ(GEN6_PMIMR);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700661 spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
662 queue_work(dev_priv->wq, &dev_priv->rps_work);
663 }
664
665 /* should clear PCH hotplug event before clear CPU irq */
666 I915_WRITE(SDEIIR, pch_iir);
667 I915_WRITE(GTIIR, gt_iir);
668 I915_WRITE(DEIIR, de_iir);
669 I915_WRITE(GEN6_PMIIR, pm_iir);
670
671done:
672 I915_WRITE(DEIER, de_ier);
673 POSTING_READ(DEIER);
674
675 return ret;
676}
677
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700678static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800679{
Jesse Barnes46979952011-04-07 13:53:55 -0700680 struct drm_device *dev = (struct drm_device *) arg;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800681 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
682 int ret = IRQ_NONE;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800683 u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
Yuanhan Liu2d7b8362010-10-08 10:21:06 +0100684 u32 hotplug_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800685 struct drm_i915_master_private *master_priv;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100686 u32 bsd_usr_interrupt = GT_BSD_USER_INTERRUPT;
687
Jesse Barnes46979952011-04-07 13:53:55 -0700688 atomic_inc(&dev_priv->irq_received);
689
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100690 if (IS_GEN6(dev))
691 bsd_usr_interrupt = GT_GEN6_BSD_USER_INTERRUPT;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800692
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000693 /* disable master interrupt before clearing iir */
694 de_ier = I915_READ(DEIER);
695 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000696 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000697
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800698 de_iir = I915_READ(DEIIR);
699 gt_iir = I915_READ(GTIIR);
Zhenyu Wangc6501562009-11-03 18:57:21 +0000700 pch_iir = I915_READ(SDEIIR);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800701 pm_iir = I915_READ(GEN6_PMIIR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800702
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800703 if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
704 (!IS_GEN6(dev) || pm_iir == 0))
Zou Nan haic7c85102010-01-15 10:29:06 +0800705 goto done;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800706
Yuanhan Liu2d7b8362010-10-08 10:21:06 +0100707 if (HAS_PCH_CPT(dev))
708 hotplug_mask = SDE_HOTPLUG_MASK_CPT;
709 else
710 hotplug_mask = SDE_HOTPLUG_MASK;
711
Zou Nan haic7c85102010-01-15 10:29:06 +0800712 ret = IRQ_HANDLED;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800713
Zou Nan haic7c85102010-01-15 10:29:06 +0800714 if (dev->primary->master) {
715 master_priv = dev->primary->master->driver_priv;
716 if (master_priv->sarea_priv)
717 master_priv->sarea_priv->last_dispatch =
718 READ_BREADCRUMB(dev_priv);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800719 }
720
Chris Wilsonc6df5412010-12-15 09:56:50 +0000721 if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000722 notify_ring(dev, &dev_priv->ring[RCS]);
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100723 if (gt_iir & bsd_usr_interrupt)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000724 notify_ring(dev, &dev_priv->ring[VCS]);
725 if (gt_iir & GT_BLT_USER_INTERRUPT)
726 notify_ring(dev, &dev_priv->ring[BCS]);
Zou Nan haic7c85102010-01-15 10:29:06 +0800727
728 if (de_iir & DE_GSE)
Chris Wilson3b617962010-08-24 09:02:58 +0100729 intel_opregion_gse_intr(dev);
Zou Nan haic7c85102010-01-15 10:29:06 +0800730
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800731 if (de_iir & DE_PLANEA_FLIP_DONE) {
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800732 intel_prepare_page_flip(dev, 0);
Chris Wilson2bbda382010-09-02 17:59:39 +0100733 intel_finish_page_flip_plane(dev, 0);
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800734 }
735
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800736 if (de_iir & DE_PLANEB_FLIP_DONE) {
737 intel_prepare_page_flip(dev, 1);
Chris Wilson2bbda382010-09-02 17:59:39 +0100738 intel_finish_page_flip_plane(dev, 1);
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800739 }
Li Pengc062df62010-01-23 00:12:58 +0800740
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800741 if (de_iir & DE_PIPEA_VBLANK)
742 drm_handle_vblank(dev, 0);
743
744 if (de_iir & DE_PIPEB_VBLANK)
745 drm_handle_vblank(dev, 1);
746
Zou Nan haic7c85102010-01-15 10:29:06 +0800747 /* check event from PCH */
Jesse Barnes776ad802011-01-04 15:09:39 -0800748 if (de_iir & DE_PCH_EVENT) {
749 if (pch_iir & hotplug_mask)
750 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
751 pch_irq_handler(dev);
752 }
Zou Nan haic7c85102010-01-15 10:29:06 +0800753
Jesse Barnesf97108d2010-01-29 11:27:07 -0800754 if (de_iir & DE_PCU_EVENT) {
Jesse Barnes7648fa92010-05-20 14:28:11 -0700755 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
Jesse Barnesf97108d2010-01-29 11:27:07 -0800756 i915_handle_rps_change(dev);
757 }
758
Ben Widawsky4912d042011-04-25 11:25:20 -0700759 if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS) {
760 /*
761 * IIR bits should never already be set because IMR should
762 * prevent an interrupt from being shown in IIR. The warning
763 * displays a case where we've unsafely cleared
764 * dev_priv->pm_iir. Although missing an interrupt of the same
765 * type is not a problem, it displays a problem in the logic.
766 *
767 * The mask bit in IMR is cleared by rps_work.
768 */
769 unsigned long flags;
770 spin_lock_irqsave(&dev_priv->rps_lock, flags);
771 WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
Ben Widawsky4912d042011-04-25 11:25:20 -0700772 dev_priv->pm_iir |= pm_iir;
Daniel Vetter4fb066a2011-09-08 14:00:20 +0200773 I915_WRITE(GEN6_PMIMR, dev_priv->pm_iir);
774 POSTING_READ(GEN6_PMIMR);
Ben Widawsky4912d042011-04-25 11:25:20 -0700775 spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
776 queue_work(dev_priv->wq, &dev_priv->rps_work);
777 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800778
Zou Nan haic7c85102010-01-15 10:29:06 +0800779 /* should clear PCH hotplug event before clear CPU irq */
780 I915_WRITE(SDEIIR, pch_iir);
781 I915_WRITE(GTIIR, gt_iir);
782 I915_WRITE(DEIIR, de_iir);
Ben Widawsky4912d042011-04-25 11:25:20 -0700783 I915_WRITE(GEN6_PMIIR, pm_iir);
Zou Nan haic7c85102010-01-15 10:29:06 +0800784
785done:
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000786 I915_WRITE(DEIER, de_ier);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000787 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000788
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800789 return ret;
790}
791
Jesse Barnes8a905232009-07-11 16:48:03 -0400792/**
793 * i915_error_work_func - do process context error handling work
794 * @work: work struct
795 *
796 * Fire an error uevent so userspace can see that a hang or error
797 * was detected.
798 */
799static void i915_error_work_func(struct work_struct *work)
800{
801 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
802 error_work);
803 struct drm_device *dev = dev_priv->dev;
Ben Gamarif316a422009-09-14 17:48:46 -0400804 char *error_event[] = { "ERROR=1", NULL };
805 char *reset_event[] = { "RESET=1", NULL };
806 char *reset_done_event[] = { "ERROR=0", NULL };
Jesse Barnes8a905232009-07-11 16:48:03 -0400807
Ben Gamarif316a422009-09-14 17:48:46 -0400808 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -0400809
Ben Gamariba1234d2009-09-14 17:48:47 -0400810 if (atomic_read(&dev_priv->mm.wedged)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +0100811 DRM_DEBUG_DRIVER("resetting chip\n");
812 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
813 if (!i915_reset(dev, GRDOM_RENDER)) {
814 atomic_set(&dev_priv->mm.wedged, 0);
815 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
Ben Gamarif316a422009-09-14 17:48:46 -0400816 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100817 complete_all(&dev_priv->error_completion);
Ben Gamarif316a422009-09-14 17:48:46 -0400818 }
Jesse Barnes8a905232009-07-11 16:48:03 -0400819}
820
Chris Wilson3bd3c932010-08-19 08:19:30 +0100821#ifdef CONFIG_DEBUG_FS
Chris Wilson9df30792010-02-18 10:24:56 +0000822static struct drm_i915_error_object *
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000823i915_error_object_create(struct drm_i915_private *dev_priv,
Chris Wilson05394f32010-11-08 19:18:58 +0000824 struct drm_i915_gem_object *src)
Chris Wilson9df30792010-02-18 10:24:56 +0000825{
826 struct drm_i915_error_object *dst;
Chris Wilson9df30792010-02-18 10:24:56 +0000827 int page, page_count;
Chris Wilsone56660d2010-08-07 11:01:26 +0100828 u32 reloc_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000829
Chris Wilson05394f32010-11-08 19:18:58 +0000830 if (src == NULL || src->pages == NULL)
Chris Wilson9df30792010-02-18 10:24:56 +0000831 return NULL;
832
Chris Wilson05394f32010-11-08 19:18:58 +0000833 page_count = src->base.size / PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +0000834
Akshay Joshi0206e352011-08-16 15:34:10 -0400835 dst = kmalloc(sizeof(*dst) + page_count * sizeof(u32 *), GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +0000836 if (dst == NULL)
837 return NULL;
838
Chris Wilson05394f32010-11-08 19:18:58 +0000839 reloc_offset = src->gtt_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000840 for (page = 0; page < page_count; page++) {
Andrew Morton788885a2010-05-11 14:07:05 -0700841 unsigned long flags;
Chris Wilsone56660d2010-08-07 11:01:26 +0100842 void *d;
Andrew Morton788885a2010-05-11 14:07:05 -0700843
Chris Wilsone56660d2010-08-07 11:01:26 +0100844 d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +0000845 if (d == NULL)
846 goto unwind;
Chris Wilsone56660d2010-08-07 11:01:26 +0100847
Andrew Morton788885a2010-05-11 14:07:05 -0700848 local_irq_save(flags);
Daniel Vetter74898d72012-02-15 23:50:22 +0100849 if (reloc_offset < dev_priv->mm.gtt_mappable_end &&
850 src->has_global_gtt_mapping) {
Chris Wilson172975aa2011-12-14 13:57:25 +0100851 void __iomem *s;
852
853 /* Simply ignore tiling or any overlapping fence.
854 * It's part of the error state, and this hopefully
855 * captures what the GPU read.
856 */
857
858 s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
859 reloc_offset);
860 memcpy_fromio(d, s, PAGE_SIZE);
861 io_mapping_unmap_atomic(s);
862 } else {
863 void *s;
864
865 drm_clflush_pages(&src->pages[page], 1);
866
867 s = kmap_atomic(src->pages[page]);
868 memcpy(d, s, PAGE_SIZE);
869 kunmap_atomic(s);
870
871 drm_clflush_pages(&src->pages[page], 1);
872 }
Andrew Morton788885a2010-05-11 14:07:05 -0700873 local_irq_restore(flags);
Chris Wilsone56660d2010-08-07 11:01:26 +0100874
Chris Wilson9df30792010-02-18 10:24:56 +0000875 dst->pages[page] = d;
Chris Wilsone56660d2010-08-07 11:01:26 +0100876
877 reloc_offset += PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +0000878 }
879 dst->page_count = page_count;
Chris Wilson05394f32010-11-08 19:18:58 +0000880 dst->gtt_offset = src->gtt_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000881
882 return dst;
883
884unwind:
885 while (page--)
886 kfree(dst->pages[page]);
887 kfree(dst);
888 return NULL;
889}
890
891static void
892i915_error_object_free(struct drm_i915_error_object *obj)
893{
894 int page;
895
896 if (obj == NULL)
897 return;
898
899 for (page = 0; page < obj->page_count; page++)
900 kfree(obj->pages[page]);
901
902 kfree(obj);
903}
904
905static void
906i915_error_state_free(struct drm_device *dev,
907 struct drm_i915_error_state *error)
908{
Chris Wilsone2f973d2011-01-27 19:15:11 +0000909 int i;
910
Chris Wilson52d39a22012-02-15 11:25:37 +0000911 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
912 i915_error_object_free(error->ring[i].batchbuffer);
913 i915_error_object_free(error->ring[i].ringbuffer);
914 kfree(error->ring[i].requests);
915 }
Chris Wilsone2f973d2011-01-27 19:15:11 +0000916
Chris Wilson9df30792010-02-18 10:24:56 +0000917 kfree(error->active_bo);
Chris Wilson6ef3d422010-08-04 20:26:07 +0100918 kfree(error->overlay);
Chris Wilson9df30792010-02-18 10:24:56 +0000919 kfree(error);
920}
921
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000922static u32 capture_bo_list(struct drm_i915_error_buffer *err,
923 int count,
924 struct list_head *head)
925{
926 struct drm_i915_gem_object *obj;
927 int i = 0;
928
929 list_for_each_entry(obj, head, mm_list) {
930 err->size = obj->base.size;
931 err->name = obj->base.name;
932 err->seqno = obj->last_rendering_seqno;
933 err->gtt_offset = obj->gtt_offset;
934 err->read_domains = obj->base.read_domains;
935 err->write_domain = obj->base.write_domain;
936 err->fence_reg = obj->fence_reg;
937 err->pinned = 0;
938 if (obj->pin_count > 0)
939 err->pinned = 1;
940 if (obj->user_pin_count > 0)
941 err->pinned = -1;
942 err->tiling = obj->tiling_mode;
943 err->dirty = obj->dirty;
944 err->purgeable = obj->madv != I915_MADV_WILLNEED;
Daniel Vetter96154f22011-12-14 13:57:00 +0100945 err->ring = obj->ring ? obj->ring->id : -1;
Chris Wilson93dfb402011-03-29 16:59:50 -0700946 err->cache_level = obj->cache_level;
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000947
948 if (++i == count)
949 break;
950
951 err++;
952 }
953
954 return i;
955}
956
Chris Wilson748ebc62010-10-24 10:28:47 +0100957static void i915_gem_record_fences(struct drm_device *dev,
958 struct drm_i915_error_state *error)
959{
960 struct drm_i915_private *dev_priv = dev->dev_private;
961 int i;
962
963 /* Fences */
964 switch (INTEL_INFO(dev)->gen) {
Daniel Vetter775d17b2011-10-09 21:52:01 +0200965 case 7:
Chris Wilson748ebc62010-10-24 10:28:47 +0100966 case 6:
967 for (i = 0; i < 16; i++)
968 error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
969 break;
970 case 5:
971 case 4:
972 for (i = 0; i < 16; i++)
973 error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
974 break;
975 case 3:
976 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
977 for (i = 0; i < 8; i++)
978 error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
979 case 2:
980 for (i = 0; i < 8; i++)
981 error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
982 break;
983
984 }
985}
986
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000987static struct drm_i915_error_object *
988i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
989 struct intel_ring_buffer *ring)
990{
991 struct drm_i915_gem_object *obj;
992 u32 seqno;
993
994 if (!ring->get_seqno)
995 return NULL;
996
997 seqno = ring->get_seqno(ring);
998 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
999 if (obj->ring != ring)
1000 continue;
1001
Chris Wilsonc37d9a52011-01-12 20:33:01 +00001002 if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001003 continue;
1004
1005 if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1006 continue;
1007
1008 /* We need to copy these to an anonymous buffer as the simplest
1009 * method to avoid being overwritten by userspace.
1010 */
1011 return i915_error_object_create(dev_priv, obj);
1012 }
1013
1014 return NULL;
1015}
1016
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001017static void i915_record_ring_state(struct drm_device *dev,
1018 struct drm_i915_error_state *error,
1019 struct intel_ring_buffer *ring)
1020{
1021 struct drm_i915_private *dev_priv = dev->dev_private;
1022
Daniel Vetter33f3f512011-12-14 13:57:39 +01001023 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001024 error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
Daniel Vetter33f3f512011-12-14 13:57:39 +01001025 error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
Daniel Vetter7e3b8732012-02-01 22:26:45 +01001026 error->semaphore_mboxes[ring->id][0]
1027 = I915_READ(RING_SYNC_0(ring->mmio_base));
1028 error->semaphore_mboxes[ring->id][1]
1029 = I915_READ(RING_SYNC_1(ring->mmio_base));
Daniel Vetter33f3f512011-12-14 13:57:39 +01001030 }
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001031
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001032 if (INTEL_INFO(dev)->gen >= 4) {
1033 error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1034 error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1035 error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001036 error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001037 if (ring->id == RCS) {
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001038 error->instdone1 = I915_READ(INSTDONE1);
1039 error->bbaddr = I915_READ64(BB_ADDR);
1040 }
1041 } else {
1042 error->ipeir[ring->id] = I915_READ(IPEIR);
1043 error->ipehr[ring->id] = I915_READ(IPEHR);
1044 error->instdone[ring->id] = I915_READ(INSTDONE);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001045 }
1046
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001047 error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001048 error->seqno[ring->id] = ring->get_seqno(ring);
1049 error->acthd[ring->id] = intel_ring_get_active_head(ring);
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001050 error->head[ring->id] = I915_READ_HEAD(ring);
1051 error->tail[ring->id] = I915_READ_TAIL(ring);
Daniel Vetter7e3b8732012-02-01 22:26:45 +01001052
1053 error->cpu_ring_head[ring->id] = ring->head;
1054 error->cpu_ring_tail[ring->id] = ring->tail;
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001055}
1056
Chris Wilson52d39a22012-02-15 11:25:37 +00001057static void i915_gem_record_rings(struct drm_device *dev,
1058 struct drm_i915_error_state *error)
1059{
1060 struct drm_i915_private *dev_priv = dev->dev_private;
1061 struct drm_i915_gem_request *request;
1062 int i, count;
1063
1064 for (i = 0; i < I915_NUM_RINGS; i++) {
1065 struct intel_ring_buffer *ring = &dev_priv->ring[i];
1066
1067 if (ring->obj == NULL)
1068 continue;
1069
1070 i915_record_ring_state(dev, error, ring);
1071
1072 error->ring[i].batchbuffer =
1073 i915_error_first_batchbuffer(dev_priv, ring);
1074
1075 error->ring[i].ringbuffer =
1076 i915_error_object_create(dev_priv, ring->obj);
1077
1078 count = 0;
1079 list_for_each_entry(request, &ring->request_list, list)
1080 count++;
1081
1082 error->ring[i].num_requests = count;
1083 error->ring[i].requests =
1084 kmalloc(count*sizeof(struct drm_i915_error_request),
1085 GFP_ATOMIC);
1086 if (error->ring[i].requests == NULL) {
1087 error->ring[i].num_requests = 0;
1088 continue;
1089 }
1090
1091 count = 0;
1092 list_for_each_entry(request, &ring->request_list, list) {
1093 struct drm_i915_error_request *erq;
1094
1095 erq = &error->ring[i].requests[count++];
1096 erq->seqno = request->seqno;
1097 erq->jiffies = request->emitted_jiffies;
Chris Wilsonee4f42b2012-02-15 11:25:38 +00001098 erq->tail = request->tail;
Chris Wilson52d39a22012-02-15 11:25:37 +00001099 }
1100 }
1101}
1102
Jesse Barnes8a905232009-07-11 16:48:03 -04001103/**
1104 * i915_capture_error_state - capture an error record for later analysis
1105 * @dev: drm device
1106 *
1107 * Should be called when an error is detected (either a hang or an error
1108 * interrupt) to capture error state from the time of the error. Fills
1109 * out a structure which becomes available in debugfs for user level tools
1110 * to pick up.
1111 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001112static void i915_capture_error_state(struct drm_device *dev)
1113{
1114 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001115 struct drm_i915_gem_object *obj;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001116 struct drm_i915_error_state *error;
1117 unsigned long flags;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001118 int i, pipe;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001119
1120 spin_lock_irqsave(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001121 error = dev_priv->first_error;
1122 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
1123 if (error)
1124 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001125
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001126 /* Account for pipe specific data like PIPE*STAT */
Daniel Vetter33f3f512011-12-14 13:57:39 +01001127 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001128 if (!error) {
Chris Wilson9df30792010-02-18 10:24:56 +00001129 DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
1130 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001131 }
1132
Chris Wilsonb6f78332011-02-01 14:15:55 +00001133 DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
1134 dev->primary->index);
Chris Wilson2fa772f2010-10-01 13:23:27 +01001135
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001136 error->eir = I915_READ(EIR);
1137 error->pgtbl_er = I915_READ(PGTBL_ER);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001138 for_each_pipe(pipe)
1139 error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001140
Daniel Vetter33f3f512011-12-14 13:57:39 +01001141 if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilsonf4068392010-10-27 20:36:41 +01001142 error->error = I915_READ(ERROR_GEN6);
Daniel Vetter33f3f512011-12-14 13:57:39 +01001143 error->done_reg = I915_READ(DONE_REG);
1144 }
Chris Wilsonadd354d2010-10-29 19:00:51 +01001145
Chris Wilson748ebc62010-10-24 10:28:47 +01001146 i915_gem_record_fences(dev, error);
Chris Wilson52d39a22012-02-15 11:25:37 +00001147 i915_gem_record_rings(dev, error);
Chris Wilson9df30792010-02-18 10:24:56 +00001148
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001149 /* Record buffers on the active and pinned lists. */
Chris Wilson9df30792010-02-18 10:24:56 +00001150 error->active_bo = NULL;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001151 error->pinned_bo = NULL;
Chris Wilson9df30792010-02-18 10:24:56 +00001152
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001153 i = 0;
1154 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1155 i++;
1156 error->active_bo_count = i;
Chris Wilson05394f32010-11-08 19:18:58 +00001157 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001158 i++;
1159 error->pinned_bo_count = i - error->active_bo_count;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001160
Chris Wilson8e934db2011-01-24 12:34:00 +00001161 error->active_bo = NULL;
1162 error->pinned_bo = NULL;
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001163 if (i) {
1164 error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
Chris Wilson9df30792010-02-18 10:24:56 +00001165 GFP_ATOMIC);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001166 if (error->active_bo)
1167 error->pinned_bo =
1168 error->active_bo + error->active_bo_count;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001169 }
1170
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001171 if (error->active_bo)
1172 error->active_bo_count =
1173 capture_bo_list(error->active_bo,
1174 error->active_bo_count,
1175 &dev_priv->mm.active_list);
1176
1177 if (error->pinned_bo)
1178 error->pinned_bo_count =
1179 capture_bo_list(error->pinned_bo,
1180 error->pinned_bo_count,
1181 &dev_priv->mm.pinned_list);
1182
Jesse Barnes8a905232009-07-11 16:48:03 -04001183 do_gettimeofday(&error->time);
1184
Chris Wilson6ef3d422010-08-04 20:26:07 +01001185 error->overlay = intel_overlay_capture_error_state(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00001186 error->display = intel_display_capture_error_state(dev);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001187
Chris Wilson9df30792010-02-18 10:24:56 +00001188 spin_lock_irqsave(&dev_priv->error_lock, flags);
1189 if (dev_priv->first_error == NULL) {
1190 dev_priv->first_error = error;
1191 error = NULL;
1192 }
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001193 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001194
1195 if (error)
1196 i915_error_state_free(dev, error);
1197}
1198
1199void i915_destroy_error_state(struct drm_device *dev)
1200{
1201 struct drm_i915_private *dev_priv = dev->dev_private;
1202 struct drm_i915_error_state *error;
Ben Widawsky6dc0e812012-01-23 15:30:02 -08001203 unsigned long flags;
Chris Wilson9df30792010-02-18 10:24:56 +00001204
Ben Widawsky6dc0e812012-01-23 15:30:02 -08001205 spin_lock_irqsave(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001206 error = dev_priv->first_error;
1207 dev_priv->first_error = NULL;
Ben Widawsky6dc0e812012-01-23 15:30:02 -08001208 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001209
1210 if (error)
1211 i915_error_state_free(dev, error);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001212}
Chris Wilson3bd3c932010-08-19 08:19:30 +01001213#else
1214#define i915_capture_error_state(x)
1215#endif
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001216
Chris Wilson35aed2e2010-05-27 13:18:12 +01001217static void i915_report_and_clear_eir(struct drm_device *dev)
Jesse Barnes8a905232009-07-11 16:48:03 -04001218{
1219 struct drm_i915_private *dev_priv = dev->dev_private;
1220 u32 eir = I915_READ(EIR);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001221 int pipe;
Jesse Barnes8a905232009-07-11 16:48:03 -04001222
Chris Wilson35aed2e2010-05-27 13:18:12 +01001223 if (!eir)
1224 return;
Jesse Barnes8a905232009-07-11 16:48:03 -04001225
Joe Perchesa70491c2012-03-18 13:00:11 -07001226 pr_err("render error detected, EIR: 0x%08x\n", eir);
Jesse Barnes8a905232009-07-11 16:48:03 -04001227
1228 if (IS_G4X(dev)) {
1229 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
1230 u32 ipeir = I915_READ(IPEIR_I965);
1231
Joe Perchesa70491c2012-03-18 13:00:11 -07001232 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1233 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1234 pr_err(" INSTDONE: 0x%08x\n",
Jesse Barnes8a905232009-07-11 16:48:03 -04001235 I915_READ(INSTDONE_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07001236 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
1237 pr_err(" INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1238 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04001239 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001240 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001241 }
1242 if (eir & GM45_ERROR_PAGE_TABLE) {
1243 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07001244 pr_err("page table error\n");
1245 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04001246 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001247 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04001248 }
1249 }
1250
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001251 if (!IS_GEN2(dev)) {
Jesse Barnes8a905232009-07-11 16:48:03 -04001252 if (eir & I915_ERROR_PAGE_TABLE) {
1253 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07001254 pr_err("page table error\n");
1255 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04001256 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001257 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04001258 }
1259 }
1260
1261 if (eir & I915_ERROR_MEMORY_REFRESH) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001262 pr_err("memory refresh error:\n");
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001263 for_each_pipe(pipe)
Joe Perchesa70491c2012-03-18 13:00:11 -07001264 pr_err("pipe %c stat: 0x%08x\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001265 pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
Jesse Barnes8a905232009-07-11 16:48:03 -04001266 /* pipestat has already been acked */
1267 }
1268 if (eir & I915_ERROR_INSTRUCTION) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001269 pr_err("instruction error\n");
1270 pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001271 if (INTEL_INFO(dev)->gen < 4) {
Jesse Barnes8a905232009-07-11 16:48:03 -04001272 u32 ipeir = I915_READ(IPEIR);
1273
Joe Perchesa70491c2012-03-18 13:00:11 -07001274 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
1275 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
1276 pr_err(" INSTDONE: 0x%08x\n", I915_READ(INSTDONE));
1277 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
Jesse Barnes8a905232009-07-11 16:48:03 -04001278 I915_WRITE(IPEIR, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001279 POSTING_READ(IPEIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001280 } else {
1281 u32 ipeir = I915_READ(IPEIR_I965);
1282
Joe Perchesa70491c2012-03-18 13:00:11 -07001283 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1284 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1285 pr_err(" INSTDONE: 0x%08x\n",
Jesse Barnes8a905232009-07-11 16:48:03 -04001286 I915_READ(INSTDONE_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07001287 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
1288 pr_err(" INSTDONE1: 0x%08x\n", I915_READ(INSTDONE1));
1289 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04001290 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001291 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001292 }
1293 }
1294
1295 I915_WRITE(EIR, eir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001296 POSTING_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001297 eir = I915_READ(EIR);
1298 if (eir) {
1299 /*
1300 * some errors might have become stuck,
1301 * mask them.
1302 */
1303 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
1304 I915_WRITE(EMR, I915_READ(EMR) | eir);
1305 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
1306 }
Chris Wilson35aed2e2010-05-27 13:18:12 +01001307}
1308
1309/**
1310 * i915_handle_error - handle an error interrupt
1311 * @dev: drm device
1312 *
1313 * Do some basic checking of regsiter state at error interrupt time and
1314 * dump it to the syslog. Also call i915_capture_error_state() to make
1315 * sure we get a record and make it available in debugfs. Fire a uevent
1316 * so userspace knows something bad happened (should trigger collection
1317 * of a ring dump etc.).
1318 */
Chris Wilson527f9e92010-11-11 01:16:58 +00001319void i915_handle_error(struct drm_device *dev, bool wedged)
Chris Wilson35aed2e2010-05-27 13:18:12 +01001320{
1321 struct drm_i915_private *dev_priv = dev->dev_private;
1322
1323 i915_capture_error_state(dev);
1324 i915_report_and_clear_eir(dev);
Jesse Barnes8a905232009-07-11 16:48:03 -04001325
Ben Gamariba1234d2009-09-14 17:48:47 -04001326 if (wedged) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001327 INIT_COMPLETION(dev_priv->error_completion);
Ben Gamariba1234d2009-09-14 17:48:47 -04001328 atomic_set(&dev_priv->mm.wedged, 1);
1329
Ben Gamari11ed50e2009-09-14 17:48:45 -04001330 /*
1331 * Wakeup waiting processes so they don't hang
1332 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001333 wake_up_all(&dev_priv->ring[RCS].irq_queue);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001334 if (HAS_BSD(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001335 wake_up_all(&dev_priv->ring[VCS].irq_queue);
Chris Wilson549f7362010-10-19 11:19:32 +01001336 if (HAS_BLT(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001337 wake_up_all(&dev_priv->ring[BCS].irq_queue);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001338 }
1339
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001340 queue_work(dev_priv->wq, &dev_priv->error_work);
Jesse Barnes8a905232009-07-11 16:48:03 -04001341}
1342
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001343static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
1344{
1345 drm_i915_private_t *dev_priv = dev->dev_private;
1346 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00001348 struct drm_i915_gem_object *obj;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001349 struct intel_unpin_work *work;
1350 unsigned long flags;
1351 bool stall_detected;
1352
1353 /* Ignore early vblank irqs */
1354 if (intel_crtc == NULL)
1355 return;
1356
1357 spin_lock_irqsave(&dev->event_lock, flags);
1358 work = intel_crtc->unpin_work;
1359
1360 if (work == NULL || work->pending || !work->enable_stall_check) {
1361 /* Either the pending flip IRQ arrived, or we're too early. Don't check */
1362 spin_unlock_irqrestore(&dev->event_lock, flags);
1363 return;
1364 }
1365
1366 /* Potential stall - if we see that the flip has happened, assume a missed interrupt */
Chris Wilson05394f32010-11-08 19:18:58 +00001367 obj = work->pending_flip_obj;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001368 if (INTEL_INFO(dev)->gen >= 4) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001369 int dspsurf = DSPSURF(intel_crtc->plane);
Chris Wilson05394f32010-11-08 19:18:58 +00001370 stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001371 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001372 int dspaddr = DSPADDR(intel_crtc->plane);
Chris Wilson05394f32010-11-08 19:18:58 +00001373 stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001374 crtc->y * crtc->fb->pitches[0] +
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001375 crtc->x * crtc->fb->bits_per_pixel/8);
1376 }
1377
1378 spin_unlock_irqrestore(&dev->event_lock, flags);
1379
1380 if (stall_detected) {
1381 DRM_DEBUG_DRIVER("Pageflip stall detected\n");
1382 intel_prepare_page_flip(dev, intel_crtc->plane);
1383 }
1384}
1385
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001386static irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001388 struct drm_device *dev = (struct drm_device *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001390 struct drm_i915_master_private *master_priv;
Eric Anholtcdfbc412008-11-04 15:50:30 -08001391 u32 iir, new_iir;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001392 u32 pipe_stats[I915_MAX_PIPES];
Keith Packard05eff842008-11-19 14:03:05 -08001393 u32 vblank_status;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001394 int vblank = 0;
Keith Packard7c463582008-11-04 02:03:27 -08001395 unsigned long irqflags;
Keith Packard05eff842008-11-19 14:03:05 -08001396 int irq_received;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001397 int ret = IRQ_NONE, pipe;
1398 bool blc_event = false;
Dave Airlieaf6061a2008-05-07 12:15:39 +10001399
Eric Anholt630681d2008-10-06 15:14:12 -07001400 atomic_inc(&dev_priv->irq_received);
1401
Eric Anholted4cb412008-07-29 12:10:39 -07001402 iir = I915_READ(IIR);
Dave Airlieaf6061a2008-05-07 12:15:39 +10001403
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001404 if (INTEL_INFO(dev)->gen >= 4)
Jesse Barnesd874bcf2010-06-30 13:16:00 -07001405 vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
Jesse Barnese25e6602010-06-30 13:15:19 -07001406 else
Jesse Barnesd874bcf2010-06-30 13:16:00 -07001407 vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Keith Packard05eff842008-11-19 14:03:05 -08001409 for (;;) {
1410 irq_received = iir != 0;
1411
1412 /* Can't rely on pipestat interrupt bit in iir as it might
1413 * have been cleared after the pipestat interrupt was received.
1414 * It doesn't set the bit in iir again, but it still produces
1415 * interrupts (for non-MSI).
1416 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001417 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes8a905232009-07-11 16:48:03 -04001418 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Ben Gamariba1234d2009-09-14 17:48:47 -04001419 i915_handle_error(dev, false);
Jesse Barnes8a905232009-07-11 16:48:03 -04001420
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001421 for_each_pipe(pipe) {
1422 int reg = PIPESTAT(pipe);
1423 pipe_stats[pipe] = I915_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -08001424
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001425 /*
1426 * Clear the PIPE*STAT regs before the IIR
1427 */
1428 if (pipe_stats[pipe] & 0x8000ffff) {
1429 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1430 DRM_DEBUG_DRIVER("pipe %c underrun\n",
1431 pipe_name(pipe));
1432 I915_WRITE(reg, pipe_stats[pipe]);
1433 irq_received = 1;
1434 }
Eric Anholtcdfbc412008-11-04 15:50:30 -08001435 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001436 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Keith Packard05eff842008-11-19 14:03:05 -08001437
1438 if (!irq_received)
1439 break;
1440
1441 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Jesse Barnes5ca58282009-03-31 14:11:15 -07001443 /* Consume port. Then clear IIR or we'll miss events */
1444 if ((I915_HAS_HOTPLUG(dev)) &&
1445 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
1446 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1447
Zhao Yakui44d98a62009-10-09 11:39:40 +08001448 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
Jesse Barnes5ca58282009-03-31 14:11:15 -07001449 hotplug_status);
1450 if (hotplug_status & dev_priv->hotplug_supported_mask)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001451 queue_work(dev_priv->wq,
1452 &dev_priv->hotplug_work);
Jesse Barnes5ca58282009-03-31 14:11:15 -07001453
1454 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
1455 I915_READ(PORT_HOTPLUG_STAT);
1456 }
1457
Eric Anholtcdfbc412008-11-04 15:50:30 -08001458 I915_WRITE(IIR, iir);
1459 new_iir = I915_READ(IIR); /* Flush posted writes */
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001460
Dave Airlie7c1c2872008-11-28 14:22:24 +10001461 if (dev->primary->master) {
1462 master_priv = dev->primary->master->driver_priv;
1463 if (master_priv->sarea_priv)
1464 master_priv->sarea_priv->last_dispatch =
1465 READ_BREADCRUMB(dev_priv);
1466 }
Keith Packard7c463582008-11-04 02:03:27 -08001467
Chris Wilson549f7362010-10-19 11:19:32 +01001468 if (iir & I915_USER_INTERRUPT)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001469 notify_ring(dev, &dev_priv->ring[RCS]);
1470 if (iir & I915_BSD_USER_INTERRUPT)
1471 notify_ring(dev, &dev_priv->ring[VCS]);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001472
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001473 if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001474 intel_prepare_page_flip(dev, 0);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001475 if (dev_priv->flip_pending_is_done)
1476 intel_finish_page_flip_plane(dev, 0);
1477 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001478
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001479 if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
Jesse Barnes70565d02010-07-01 04:45:43 -07001480 intel_prepare_page_flip(dev, 1);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001481 if (dev_priv->flip_pending_is_done)
1482 intel_finish_page_flip_plane(dev, 1);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001483 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001484
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001485 for_each_pipe(pipe) {
1486 if (pipe_stats[pipe] & vblank_status &&
1487 drm_handle_vblank(dev, pipe)) {
1488 vblank++;
1489 if (!dev_priv->flip_pending_is_done) {
1490 i915_pageflip_stall_check(dev, pipe);
1491 intel_finish_page_flip(dev, pipe);
1492 }
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001493 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001494
1495 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1496 blc_event = true;
Eric Anholtcdfbc412008-11-04 15:50:30 -08001497 }
Eric Anholt673a3942008-07-30 12:06:12 -07001498
Keith Packard7c463582008-11-04 02:03:27 -08001499
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001500 if (blc_event || (iir & I915_ASLE_INTERRUPT))
Chris Wilson3b617962010-08-24 09:02:58 +01001501 intel_opregion_asle_intr(dev);
Keith Packard7c463582008-11-04 02:03:27 -08001502
Eric Anholtcdfbc412008-11-04 15:50:30 -08001503 /* With MSI, interrupts are only generated when iir
1504 * transitions from zero to nonzero. If another bit got
1505 * set while we were handling the existing iir bits, then
1506 * we would never get another interrupt.
1507 *
1508 * This is fine on non-MSI as well, as if we hit this path
1509 * we avoid exiting the interrupt handler only to generate
1510 * another one.
1511 *
1512 * Note that for MSI this could cause a stray interrupt report
1513 * if an interrupt landed in the time between writing IIR and
1514 * the posting read. This should be rare enough to never
1515 * trigger the 99% of 100,000 interrupts test for disabling
1516 * stray interrupts.
1517 */
1518 iir = new_iir;
Keith Packard05eff842008-11-19 14:03:05 -08001519 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001520
Keith Packard05eff842008-11-19 14:03:05 -08001521 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
Dave Airlieaf6061a2008-05-07 12:15:39 +10001524static int i915_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001527 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 i915_kernel_lost_context(dev);
1530
Zhao Yakui44d98a62009-10-09 11:39:40 +08001531 DRM_DEBUG_DRIVER("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Kristian Høgsbergc99b0582008-08-20 11:20:13 -04001533 dev_priv->counter++;
Alan Hourihanec29b6692006-08-12 16:29:24 +10001534 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -04001535 dev_priv->counter = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001536 if (master_priv->sarea_priv)
1537 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Alan Hourihanec29b6692006-08-12 16:29:24 +10001538
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001539 if (BEGIN_LP_RING(4) == 0) {
1540 OUT_RING(MI_STORE_DWORD_INDEX);
1541 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1542 OUT_RING(dev_priv->counter);
1543 OUT_RING(MI_USER_INTERRUPT);
1544 ADVANCE_LP_RING();
1545 }
Dave Airliebc5f4522007-11-05 12:50:58 +10001546
Alan Hourihanec29b6692006-08-12 16:29:24 +10001547 return dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Dave Airlie84b1fd12007-07-11 15:53:27 +10001550static int i915_wait_irq(struct drm_device * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001553 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int ret = 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001555 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Zhao Yakui44d98a62009-10-09 11:39:40 +08001557 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 READ_BREADCRUMB(dev_priv));
1559
Eric Anholted4cb412008-07-29 12:10:39 -07001560 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
Dave Airlie7c1c2872008-11-28 14:22:24 +10001561 if (master_priv->sarea_priv)
1562 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Dave Airlie7c1c2872008-11-28 14:22:24 +10001566 if (master_priv->sarea_priv)
1567 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001569 if (ring->irq_get(ring)) {
1570 DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1571 READ_BREADCRUMB(dev_priv) >= irq_nr);
1572 ring->irq_put(ring);
Chris Wilson5a9a8d12011-01-23 13:03:24 +00001573 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
1574 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Eric Anholt20caafa2007-08-25 19:22:43 +10001576 if (ret == -EBUSY) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001577 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1579 }
1580
Dave Airlieaf6061a2008-05-07 12:15:39 +10001581 return ret;
1582}
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584/* Needs the lock as it touches the ring.
1585 */
Eric Anholtc153f452007-09-03 12:06:45 +10001586int i915_irq_emit(struct drm_device *dev, void *data,
1587 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001590 drm_i915_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int result;
1592
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001593 if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001594 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001595 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Eric Anholt299eb932009-02-24 22:14:12 -08001597
1598 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1599
Eric Anholt546b0972008-09-01 16:45:29 -07001600 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 result = i915_emit_irq(dev);
Eric Anholt546b0972008-09-01 16:45:29 -07001602 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Eric Anholtc153f452007-09-03 12:06:45 +10001604 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001606 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608
1609 return 0;
1610}
1611
1612/* Doesn't need the hardware lock.
1613 */
Eric Anholtc153f452007-09-03 12:06:45 +10001614int i915_irq_wait(struct drm_device *dev, void *data,
1615 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001618 drm_i915_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001621 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001622 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624
Eric Anholtc153f452007-09-03 12:06:45 +10001625 return i915_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
Keith Packard42f52ef2008-10-18 19:39:29 -07001628/* Called from drm generic code, passed 'crtc' which
1629 * we use as a pipe index
1630 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001631static int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001632{
1633 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001634 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001635
Chris Wilson5eddb702010-09-11 13:48:45 +01001636 if (!i915_pipe_enabled(dev, pipe))
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001637 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001638
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001639 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07001640 if (INTEL_INFO(dev)->gen >= 4)
Keith Packard7c463582008-11-04 02:03:27 -08001641 i915_enable_pipestat(dev_priv, pipe,
1642 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -07001643 else
Keith Packard7c463582008-11-04 02:03:27 -08001644 i915_enable_pipestat(dev_priv, pipe,
1645 PIPE_VBLANK_INTERRUPT_ENABLE);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001646
1647 /* maintain vblank delivery even in deep C-states */
1648 if (dev_priv->info->gen == 3)
1649 I915_WRITE(INSTPM, INSTPM_AGPBUSY_DIS << 16);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001650 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001651
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001652 return 0;
1653}
1654
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001655static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07001656{
1657 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1658 unsigned long irqflags;
1659
1660 if (!i915_pipe_enabled(dev, pipe))
1661 return -EINVAL;
1662
1663 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1664 ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
Akshay Joshi0206e352011-08-16 15:34:10 -04001665 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
Jesse Barnesf796cf82011-04-07 13:58:17 -07001666 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1667
1668 return 0;
1669}
1670
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001671static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001672{
1673 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1674 unsigned long irqflags;
1675
1676 if (!i915_pipe_enabled(dev, pipe))
1677 return -EINVAL;
1678
1679 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1680 ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1681 DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1682 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1683
1684 return 0;
1685}
1686
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001687static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
1688{
1689 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1690 unsigned long irqflags;
1691 u32 dpfl, imr;
1692
1693 if (!i915_pipe_enabled(dev, pipe))
1694 return -EINVAL;
1695
1696 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1697 dpfl = I915_READ(VLV_DPFLIPSTAT);
1698 imr = I915_READ(VLV_IMR);
1699 if (pipe == 0) {
1700 dpfl |= PIPEA_VBLANK_INT_EN;
1701 imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
1702 } else {
1703 dpfl |= PIPEA_VBLANK_INT_EN;
1704 imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
1705 }
1706 I915_WRITE(VLV_DPFLIPSTAT, dpfl);
1707 I915_WRITE(VLV_IMR, imr);
1708 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1709
1710 return 0;
1711}
1712
Keith Packard42f52ef2008-10-18 19:39:29 -07001713/* Called from drm generic code, passed 'crtc' which
1714 * we use as a pipe index
1715 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001716static void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001717{
1718 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001719 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001720
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001721 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001722 if (dev_priv->info->gen == 3)
1723 I915_WRITE(INSTPM,
1724 INSTPM_AGPBUSY_DIS << 16 | INSTPM_AGPBUSY_DIS);
1725
Jesse Barnesf796cf82011-04-07 13:58:17 -07001726 i915_disable_pipestat(dev_priv, pipe,
1727 PIPE_VBLANK_INTERRUPT_ENABLE |
1728 PIPE_START_VBLANK_INTERRUPT_ENABLE);
1729 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1730}
1731
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001732static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07001733{
1734 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1735 unsigned long irqflags;
1736
1737 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1738 ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
Akshay Joshi0206e352011-08-16 15:34:10 -04001739 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001740 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001741}
1742
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001743static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001744{
1745 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1746 unsigned long irqflags;
1747
1748 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1749 ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1750 DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1751 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1752}
1753
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001754static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
1755{
1756 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1757 unsigned long irqflags;
1758 u32 dpfl, imr;
1759
1760 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1761 dpfl = I915_READ(VLV_DPFLIPSTAT);
1762 imr = I915_READ(VLV_IMR);
1763 if (pipe == 0) {
1764 dpfl &= ~PIPEA_VBLANK_INT_EN;
1765 imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
1766 } else {
1767 dpfl &= ~PIPEB_VBLANK_INT_EN;
1768 imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
1769 }
1770 I915_WRITE(VLV_IMR, imr);
1771 I915_WRITE(VLV_DPFLIPSTAT, dpfl);
1772 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1773}
1774
1775
Dave Airlie702880f2006-06-24 17:07:34 +10001776/* Set the vblank monitor pipe
1777 */
Eric Anholtc153f452007-09-03 12:06:45 +10001778int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1779 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +10001780{
Dave Airlie702880f2006-06-24 17:07:34 +10001781 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie702880f2006-06-24 17:07:34 +10001782
1783 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001784 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001785 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +10001786 }
1787
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +10001788 return 0;
Dave Airlie702880f2006-06-24 17:07:34 +10001789}
1790
Eric Anholtc153f452007-09-03 12:06:45 +10001791int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1792 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +10001793{
Dave Airlie702880f2006-06-24 17:07:34 +10001794 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001795 drm_i915_vblank_pipe_t *pipe = data;
Dave Airlie702880f2006-06-24 17:07:34 +10001796
1797 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001798 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001799 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +10001800 }
1801
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001802 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Eric Anholtc153f452007-09-03 12:06:45 +10001803
Dave Airlie702880f2006-06-24 17:07:34 +10001804 return 0;
1805}
1806
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +10001807/**
1808 * Schedule buffer swap at given vertical blank.
1809 */
Eric Anholtc153f452007-09-03 12:06:45 +10001810int i915_vblank_swap(struct drm_device *dev, void *data,
1811 struct drm_file *file_priv)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +10001812{
Eric Anholtbd95e0a2008-11-04 12:01:24 -08001813 /* The delayed swap mechanism was fundamentally racy, and has been
1814 * removed. The model was that the client requested a delayed flip/swap
1815 * from the kernel, then waited for vblank before continuing to perform
1816 * rendering. The problem was that the kernel might wake the client
1817 * up before it dispatched the vblank swap (since the lock has to be
1818 * held while touching the ringbuffer), in which case the client would
1819 * clear and start the next frame before the swap occurred, and
1820 * flicker would occur in addition to likely missing the vblank.
1821 *
1822 * In the absence of this ioctl, userland falls back to a correct path
1823 * of waiting for a vblank, then dispatching the swap on its own.
1824 * Context switching to userland and back is plenty fast enough for
1825 * meeting the requirements of vblank swapping.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001826 */
Eric Anholtbd95e0a2008-11-04 12:01:24 -08001827 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +10001828}
1829
Chris Wilson893eead2010-10-27 14:44:35 +01001830static u32
1831ring_last_seqno(struct intel_ring_buffer *ring)
Zou Nan hai852835f2010-05-21 09:08:56 +08001832{
Chris Wilson893eead2010-10-27 14:44:35 +01001833 return list_entry(ring->request_list.prev,
1834 struct drm_i915_gem_request, list)->seqno;
1835}
1836
1837static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1838{
1839 if (list_empty(&ring->request_list) ||
1840 i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1841 /* Issue a wake-up to catch stuck h/w. */
Chris Wilsonb2223492010-10-27 15:27:33 +01001842 if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
Chris Wilson893eead2010-10-27 14:44:35 +01001843 DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1844 ring->name,
Chris Wilsonb2223492010-10-27 15:27:33 +01001845 ring->waiting_seqno,
Chris Wilson893eead2010-10-27 14:44:35 +01001846 ring->get_seqno(ring));
1847 wake_up_all(&ring->irq_queue);
1848 *err = true;
1849 }
1850 return true;
1851 }
1852 return false;
Ben Gamarif65d9422009-09-14 17:48:44 -04001853}
1854
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001855static bool kick_ring(struct intel_ring_buffer *ring)
1856{
1857 struct drm_device *dev = ring->dev;
1858 struct drm_i915_private *dev_priv = dev->dev_private;
1859 u32 tmp = I915_READ_CTL(ring);
1860 if (tmp & RING_WAIT) {
1861 DRM_ERROR("Kicking stuck wait on %s\n",
1862 ring->name);
1863 I915_WRITE_CTL(ring, tmp);
1864 return true;
1865 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001866 return false;
1867}
1868
Ben Gamarif65d9422009-09-14 17:48:44 -04001869/**
1870 * This is called when the chip hasn't reported back with completed
1871 * batchbuffers in a long time. The first time this is called we simply record
1872 * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1873 * again, we assume the chip is wedged and try to fix it.
1874 */
1875void i915_hangcheck_elapsed(unsigned long data)
1876{
1877 struct drm_device *dev = (struct drm_device *)data;
1878 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter097354e2011-11-27 18:58:17 +01001879 uint32_t acthd, instdone, instdone1, acthd_bsd, acthd_blt;
Chris Wilson893eead2010-10-27 14:44:35 +01001880 bool err = false;
1881
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001882 if (!i915_enable_hangcheck)
1883 return;
1884
Chris Wilson893eead2010-10-27 14:44:35 +01001885 /* If all work is done then ACTHD clearly hasn't advanced. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001886 if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
1887 i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
1888 i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
Chris Wilson893eead2010-10-27 14:44:35 +01001889 dev_priv->hangcheck_count = 0;
1890 if (err)
1891 goto repeat;
1892 return;
1893 }
Eric Anholtb9201c12010-01-08 14:25:16 -08001894
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001895 if (INTEL_INFO(dev)->gen < 4) {
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001896 instdone = I915_READ(INSTDONE);
1897 instdone1 = 0;
1898 } else {
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001899 instdone = I915_READ(INSTDONE_I965);
1900 instdone1 = I915_READ(INSTDONE1);
1901 }
Daniel Vetter097354e2011-11-27 18:58:17 +01001902 acthd = intel_ring_get_active_head(&dev_priv->ring[RCS]);
1903 acthd_bsd = HAS_BSD(dev) ?
1904 intel_ring_get_active_head(&dev_priv->ring[VCS]) : 0;
1905 acthd_blt = HAS_BLT(dev) ?
1906 intel_ring_get_active_head(&dev_priv->ring[BCS]) : 0;
Ben Gamarif65d9422009-09-14 17:48:44 -04001907
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001908 if (dev_priv->last_acthd == acthd &&
Daniel Vetter097354e2011-11-27 18:58:17 +01001909 dev_priv->last_acthd_bsd == acthd_bsd &&
1910 dev_priv->last_acthd_blt == acthd_blt &&
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001911 dev_priv->last_instdone == instdone &&
1912 dev_priv->last_instdone1 == instdone1) {
1913 if (dev_priv->hangcheck_count++ > 1) {
1914 DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
Daniel Vetter653d7be2011-12-14 13:57:21 +01001915 i915_handle_error(dev, true);
Chris Wilson8c80b592010-08-08 20:38:12 +01001916
1917 if (!IS_GEN2(dev)) {
1918 /* Is the chip hanging on a WAIT_FOR_EVENT?
1919 * If so we can simply poke the RB_WAIT bit
1920 * and break the hang. This should work on
1921 * all but the second generation chipsets.
1922 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001923 if (kick_ring(&dev_priv->ring[RCS]))
Chris Wilson893eead2010-10-27 14:44:35 +01001924 goto repeat;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001925
1926 if (HAS_BSD(dev) &&
1927 kick_ring(&dev_priv->ring[VCS]))
1928 goto repeat;
1929
1930 if (HAS_BLT(dev) &&
1931 kick_ring(&dev_priv->ring[BCS]))
1932 goto repeat;
Chris Wilson8c80b592010-08-08 20:38:12 +01001933 }
1934
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001935 return;
1936 }
1937 } else {
1938 dev_priv->hangcheck_count = 0;
1939
1940 dev_priv->last_acthd = acthd;
Daniel Vetter097354e2011-11-27 18:58:17 +01001941 dev_priv->last_acthd_bsd = acthd_bsd;
1942 dev_priv->last_acthd_blt = acthd_blt;
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001943 dev_priv->last_instdone = instdone;
1944 dev_priv->last_instdone1 = instdone1;
1945 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001946
Chris Wilson893eead2010-10-27 14:44:35 +01001947repeat:
Ben Gamarif65d9422009-09-14 17:48:44 -04001948 /* Reset timer case chip hangs without another request being added */
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001949 mod_timer(&dev_priv->hangcheck_timer,
1950 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001951}
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953/* drm_dma.h hooks
1954*/
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001955static void ironlake_irq_preinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001956{
1957 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1958
Jesse Barnes46979952011-04-07 13:53:55 -07001959 atomic_set(&dev_priv->irq_received, 0);
1960
1961 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
1962 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
Jesse Barnes9e3c2562011-05-18 13:51:43 -07001963 if (IS_GEN6(dev) || IS_IVYBRIDGE(dev))
1964 INIT_WORK(&dev_priv->rps_work, gen6_pm_rps_work);
Jesse Barnes46979952011-04-07 13:53:55 -07001965
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001966 I915_WRITE(HWSTAM, 0xeffe);
Daniel Vetterbdfcdb62012-01-05 01:05:26 +01001967
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001968 /* XXX hotplug from PCH */
1969
1970 I915_WRITE(DEIMR, 0xffffffff);
1971 I915_WRITE(DEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001972 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001973
1974 /* and GT */
1975 I915_WRITE(GTIMR, 0xffffffff);
1976 I915_WRITE(GTIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001977 POSTING_READ(GTIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00001978
1979 /* south display irq */
1980 I915_WRITE(SDEIMR, 0xffffffff);
1981 I915_WRITE(SDEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001982 POSTING_READ(SDEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001983}
1984
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001985static void valleyview_irq_preinstall(struct drm_device *dev)
1986{
1987 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1988 int pipe;
1989
1990 atomic_set(&dev_priv->irq_received, 0);
1991
1992 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
1993 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
1994
1995 /* VLV magic */
1996 I915_WRITE(VLV_IMR, 0);
1997 I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
1998 I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
1999 I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
2000
2001 if (IS_GEN6(dev) || IS_GEN7(dev)) {
2002 /* Workaround stalls observed on Sandy Bridge GPUs by
2003 * making the blitter command streamer generate a
2004 * write to the Hardware Status Page for
2005 * MI_USER_INTERRUPT. This appears to serialize the
2006 * previous seqno write out before the interrupt
2007 * happens.
2008 */
2009 I915_WRITE(GEN6_BLITTER_HWSTAM, ~GEN6_BLITTER_USER_INTERRUPT);
2010 I915_WRITE(GEN6_BSD_HWSTAM, ~GEN6_BSD_USER_INTERRUPT);
2011 }
2012
2013 /* and GT */
2014 I915_WRITE(GTIIR, I915_READ(GTIIR));
2015 I915_WRITE(GTIIR, I915_READ(GTIIR));
2016 I915_WRITE(GTIMR, 0xffffffff);
2017 I915_WRITE(GTIER, 0x0);
2018 POSTING_READ(GTIER);
2019
2020 I915_WRITE(DPINVGTT, 0xff);
2021
2022 I915_WRITE(PORT_HOTPLUG_EN, 0);
2023 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2024 for_each_pipe(pipe)
2025 I915_WRITE(PIPESTAT(pipe), 0xffff);
2026 I915_WRITE(VLV_IIR, 0xffffffff);
2027 I915_WRITE(VLV_IMR, 0xffffffff);
2028 I915_WRITE(VLV_IER, 0x0);
2029 POSTING_READ(VLV_IER);
2030}
2031
Keith Packard7fe0b972011-09-19 13:31:02 -07002032/*
2033 * Enable digital hotplug on the PCH, and configure the DP short pulse
2034 * duration to 2ms (which is the minimum in the Display Port spec)
2035 *
2036 * This register is the same on all known PCH chips.
2037 */
2038
2039static void ironlake_enable_pch_hotplug(struct drm_device *dev)
2040{
2041 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2042 u32 hotplug;
2043
2044 hotplug = I915_READ(PCH_PORT_HOTPLUG);
2045 hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
2046 hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
2047 hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
2048 hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
2049 I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
2050}
2051
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002052static int ironlake_irq_postinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002053{
2054 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2055 /* enable kind of interrupts always enabled */
Jesse Barnes013d5aa2010-01-29 11:18:31 -08002056 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2057 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002058 u32 render_irqs;
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01002059 u32 hotplug_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002060
Jesse Barnes46979952011-04-07 13:53:55 -07002061 DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
2062 if (HAS_BSD(dev))
2063 DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
2064 if (HAS_BLT(dev))
2065 DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
2066
2067 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002068 dev_priv->irq_mask = ~display_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002069
2070 /* should always can generate irq */
2071 I915_WRITE(DEIIR, I915_READ(DEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002072 I915_WRITE(DEIMR, dev_priv->irq_mask);
2073 I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002074 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002075
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002076 dev_priv->gt_irq_mask = ~0;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002077
2078 I915_WRITE(GTIIR, I915_READ(GTIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002079 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002080
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002081 if (IS_GEN6(dev))
2082 render_irqs =
2083 GT_USER_INTERRUPT |
2084 GT_GEN6_BSD_USER_INTERRUPT |
2085 GT_BLT_USER_INTERRUPT;
2086 else
2087 render_irqs =
Chris Wilson88f23b82010-12-05 15:08:31 +00002088 GT_USER_INTERRUPT |
Chris Wilsonc6df5412010-12-15 09:56:50 +00002089 GT_PIPE_NOTIFY |
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002090 GT_BSD_USER_INTERRUPT;
2091 I915_WRITE(GTIER, render_irqs);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002092 POSTING_READ(GTIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002093
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01002094 if (HAS_PCH_CPT(dev)) {
Chris Wilson9035a972011-02-16 09:36:05 +00002095 hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
2096 SDE_PORTB_HOTPLUG_CPT |
2097 SDE_PORTC_HOTPLUG_CPT |
2098 SDE_PORTD_HOTPLUG_CPT);
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01002099 } else {
Chris Wilson9035a972011-02-16 09:36:05 +00002100 hotplug_mask = (SDE_CRT_HOTPLUG |
2101 SDE_PORTB_HOTPLUG |
2102 SDE_PORTC_HOTPLUG |
2103 SDE_PORTD_HOTPLUG |
2104 SDE_AUX_MASK);
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01002105 }
2106
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002107 dev_priv->pch_irq_mask = ~hotplug_mask;
Zhenyu Wangc6501562009-11-03 18:57:21 +00002108
2109 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002110 I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
2111 I915_WRITE(SDEIER, hotplug_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002112 POSTING_READ(SDEIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00002113
Keith Packard7fe0b972011-09-19 13:31:02 -07002114 ironlake_enable_pch_hotplug(dev);
2115
Jesse Barnesf97108d2010-01-29 11:27:07 -08002116 if (IS_IRONLAKE_M(dev)) {
2117 /* Clear & enable PCU event interrupts */
2118 I915_WRITE(DEIIR, DE_PCU_EVENT);
2119 I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
2120 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2121 }
2122
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002123 return 0;
2124}
2125
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002126static int ivybridge_irq_postinstall(struct drm_device *dev)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002127{
2128 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2129 /* enable kind of interrupts always enabled */
2130 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
2131 DE_PCH_EVENT_IVB | DE_PLANEA_FLIP_DONE_IVB |
2132 DE_PLANEB_FLIP_DONE_IVB;
2133 u32 render_irqs;
2134 u32 hotplug_mask;
2135
2136 DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
2137 if (HAS_BSD(dev))
2138 DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
2139 if (HAS_BLT(dev))
2140 DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
2141
2142 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
2143 dev_priv->irq_mask = ~display_mask;
2144
2145 /* should always can generate irq */
2146 I915_WRITE(DEIIR, I915_READ(DEIIR));
2147 I915_WRITE(DEIMR, dev_priv->irq_mask);
2148 I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK_IVB |
2149 DE_PIPEB_VBLANK_IVB);
2150 POSTING_READ(DEIER);
2151
2152 dev_priv->gt_irq_mask = ~0;
2153
2154 I915_WRITE(GTIIR, I915_READ(GTIIR));
2155 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2156
2157 render_irqs = GT_USER_INTERRUPT | GT_GEN6_BSD_USER_INTERRUPT |
2158 GT_BLT_USER_INTERRUPT;
2159 I915_WRITE(GTIER, render_irqs);
2160 POSTING_READ(GTIER);
2161
2162 hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
2163 SDE_PORTB_HOTPLUG_CPT |
2164 SDE_PORTC_HOTPLUG_CPT |
2165 SDE_PORTD_HOTPLUG_CPT);
2166 dev_priv->pch_irq_mask = ~hotplug_mask;
2167
2168 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2169 I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
2170 I915_WRITE(SDEIER, hotplug_mask);
2171 POSTING_READ(SDEIER);
2172
Keith Packard7fe0b972011-09-19 13:31:02 -07002173 ironlake_enable_pch_hotplug(dev);
2174
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002175 return 0;
2176}
2177
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002178static int valleyview_irq_postinstall(struct drm_device *dev)
2179{
2180 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2181 u32 render_irqs;
2182 u32 enable_mask;
2183 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2184 u16 msid;
2185
2186 enable_mask = I915_DISPLAY_PORT_INTERRUPT;
2187 enable_mask |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
2188 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
2189
2190 dev_priv->irq_mask = ~enable_mask;
2191
2192
2193 DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
2194 DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
2195 DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
2196
2197 dev_priv->pipestat[0] = 0;
2198 dev_priv->pipestat[1] = 0;
2199
2200 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
2201
2202 /* Hack for broken MSIs on VLV */
2203 pci_write_config_dword(dev_priv->dev->pdev, 0x94, 0xfee00000);
2204 pci_read_config_word(dev->pdev, 0x98, &msid);
2205 msid &= 0xff; /* mask out delivery bits */
2206 msid |= (1<<14);
2207 pci_write_config_word(dev_priv->dev->pdev, 0x98, msid);
2208
2209 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
2210 I915_WRITE(VLV_IER, enable_mask);
2211 I915_WRITE(VLV_IIR, 0xffffffff);
2212 I915_WRITE(PIPESTAT(0), 0xffff);
2213 I915_WRITE(PIPESTAT(1), 0xffff);
2214 POSTING_READ(VLV_IER);
2215
2216 I915_WRITE(VLV_IIR, 0xffffffff);
2217 I915_WRITE(VLV_IIR, 0xffffffff);
2218
2219 render_irqs = GT_GEN6_BLT_FLUSHDW_NOTIFY_INTERRUPT |
2220 GT_GEN6_BLT_CS_ERROR_INTERRUPT |
2221 GT_BLT_USER_INTERRUPT |
2222 GT_GEN6_BSD_USER_INTERRUPT |
2223 GT_GEN6_BSD_CS_ERROR_INTERRUPT |
2224 GT_GEN7_L3_PARITY_ERROR_INTERRUPT |
2225 GT_PIPE_NOTIFY |
2226 GT_RENDER_CS_ERROR_INTERRUPT |
2227 GT_SYNC_STATUS |
2228 GT_USER_INTERRUPT;
2229
2230 dev_priv->gt_irq_mask = ~render_irqs;
2231
2232 I915_WRITE(GTIIR, I915_READ(GTIIR));
2233 I915_WRITE(GTIIR, I915_READ(GTIIR));
2234 I915_WRITE(GTIMR, 0);
2235 I915_WRITE(GTIER, render_irqs);
2236 POSTING_READ(GTIER);
2237
2238 /* ack & enable invalid PTE error interrupts */
2239#if 0 /* FIXME: add support to irq handler for checking these bits */
2240 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
2241 I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
2242#endif
2243
2244 I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
2245#if 0 /* FIXME: check register definitions; some have moved */
2246 /* Note HDMI and DP share bits */
2247 if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2248 hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2249 if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2250 hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2251 if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2252 hotplug_en |= HDMID_HOTPLUG_INT_EN;
2253 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
2254 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2255 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
2256 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
2257 if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
2258 hotplug_en |= CRT_HOTPLUG_INT_EN;
2259 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2260 }
2261#endif
2262
2263 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2264
2265 return 0;
2266}
2267
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002268static void i915_driver_irq_preinstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002271 int pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Jesse Barnes79e53942008-11-07 14:24:08 -08002273 atomic_set(&dev_priv->irq_received, 0);
2274
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002275 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Jesse Barnes8a905232009-07-11 16:48:03 -04002276 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002277
Jesse Barnes5ca58282009-03-31 14:11:15 -07002278 if (I915_HAS_HOTPLUG(dev)) {
2279 I915_WRITE(PORT_HOTPLUG_EN, 0);
2280 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2281 }
2282
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002283 I915_WRITE(HWSTAM, 0xeffe);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002284 for_each_pipe(pipe)
2285 I915_WRITE(PIPESTAT(pipe), 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002286 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07002287 I915_WRITE(IER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002288 POSTING_READ(IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002291/*
2292 * Must be called after intel_modeset_init or hotplug interrupts won't be
2293 * enabled correctly.
2294 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002295static int i915_driver_irq_postinstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
2297 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes5ca58282009-03-31 14:11:15 -07002298 u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07002299 u32 error_mask;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002300
2301 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002302
Keith Packard7c463582008-11-04 02:03:27 -08002303 /* Unmask the interrupts that we always want on. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002304 dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01002305
Keith Packard7c463582008-11-04 02:03:27 -08002306 dev_priv->pipestat[0] = 0;
2307 dev_priv->pipestat[1] = 0;
2308
Jesse Barnes5ca58282009-03-31 14:11:15 -07002309 if (I915_HAS_HOTPLUG(dev)) {
Adam Jacksonc496fa12010-05-27 17:26:45 -04002310 /* Enable in IER... */
2311 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2312 /* and unmask in IMR */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002313 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
Adam Jacksonc496fa12010-05-27 17:26:45 -04002314 }
2315
2316 /*
2317 * Enable some error detection, note the instruction error mask
2318 * bit is reserved, so we leave it masked.
2319 */
2320 if (IS_G4X(dev)) {
2321 error_mask = ~(GM45_ERROR_PAGE_TABLE |
2322 GM45_ERROR_MEM_PRIV |
2323 GM45_ERROR_CP_PRIV |
2324 I915_ERROR_MEMORY_REFRESH);
2325 } else {
2326 error_mask = ~(I915_ERROR_PAGE_TABLE |
2327 I915_ERROR_MEMORY_REFRESH);
2328 }
2329 I915_WRITE(EMR, error_mask);
2330
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002331 I915_WRITE(IMR, dev_priv->irq_mask);
Adam Jacksonc496fa12010-05-27 17:26:45 -04002332 I915_WRITE(IER, enable_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002333 POSTING_READ(IER);
Adam Jacksonc496fa12010-05-27 17:26:45 -04002334
2335 if (I915_HAS_HOTPLUG(dev)) {
Jesse Barnes5ca58282009-03-31 14:11:15 -07002336 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2337
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002338 /* Note HDMI and DP share bits */
2339 if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
2340 hotplug_en |= HDMIB_HOTPLUG_INT_EN;
2341 if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
2342 hotplug_en |= HDMIC_HOTPLUG_INT_EN;
2343 if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
2344 hotplug_en |= HDMID_HOTPLUG_INT_EN;
2345 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
2346 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
2347 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
2348 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
Andy Lutomirski2d1c9752010-06-12 05:21:18 -04002349 if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002350 hotplug_en |= CRT_HOTPLUG_INT_EN;
Andy Lutomirski2d1c9752010-06-12 05:21:18 -04002351
2352 /* Programming the CRT detection parameters tends
2353 to generate a spurious hotplug event about three
2354 seconds later. So just do it once.
2355 */
2356 if (IS_G4X(dev))
2357 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2358 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2359 }
2360
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002361 /* Ignore TV since it's buggy */
2362
Jesse Barnes5ca58282009-03-31 14:11:15 -07002363 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Jesse Barnes5ca58282009-03-31 14:11:15 -07002364 }
2365
Chris Wilson3b617962010-08-24 09:02:58 +01002366 intel_opregion_enable_asle(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002367
2368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002371static void valleyview_irq_uninstall(struct drm_device *dev)
2372{
2373 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2374 int pipe;
2375
2376 if (!dev_priv)
2377 return;
2378
2379 dev_priv->vblank_pipe = 0;
2380
2381 for_each_pipe(pipe)
2382 I915_WRITE(PIPESTAT(pipe), 0xffff);
2383
2384 I915_WRITE(HWSTAM, 0xffffffff);
2385 I915_WRITE(PORT_HOTPLUG_EN, 0);
2386 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2387 for_each_pipe(pipe)
2388 I915_WRITE(PIPESTAT(pipe), 0xffff);
2389 I915_WRITE(VLV_IIR, 0xffffffff);
2390 I915_WRITE(VLV_IMR, 0xffffffff);
2391 I915_WRITE(VLV_IER, 0x0);
2392 POSTING_READ(VLV_IER);
2393}
2394
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002395static void ironlake_irq_uninstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002396{
2397 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes46979952011-04-07 13:53:55 -07002398
2399 if (!dev_priv)
2400 return;
2401
2402 dev_priv->vblank_pipe = 0;
2403
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002404 I915_WRITE(HWSTAM, 0xffffffff);
2405
2406 I915_WRITE(DEIMR, 0xffffffff);
2407 I915_WRITE(DEIER, 0x0);
2408 I915_WRITE(DEIIR, I915_READ(DEIIR));
2409
2410 I915_WRITE(GTIMR, 0xffffffff);
2411 I915_WRITE(GTIER, 0x0);
2412 I915_WRITE(GTIIR, I915_READ(GTIIR));
Keith Packard192aac1f2011-09-20 10:12:44 -07002413
2414 I915_WRITE(SDEIMR, 0xffffffff);
2415 I915_WRITE(SDEIER, 0x0);
2416 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002417}
2418
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002419static void i915_driver_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
2421 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002422 int pipe;
Dave Airlie91e37382006-02-18 15:17:04 +11002423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (!dev_priv)
2425 return;
2426
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002427 dev_priv->vblank_pipe = 0;
2428
Jesse Barnes5ca58282009-03-31 14:11:15 -07002429 if (I915_HAS_HOTPLUG(dev)) {
2430 I915_WRITE(PORT_HOTPLUG_EN, 0);
2431 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2432 }
2433
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002434 I915_WRITE(HWSTAM, 0xffffffff);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002435 for_each_pipe(pipe)
2436 I915_WRITE(PIPESTAT(pipe), 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002437 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07002438 I915_WRITE(IER, 0x0);
Dave Airlie91e37382006-02-18 15:17:04 +11002439
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002440 for_each_pipe(pipe)
2441 I915_WRITE(PIPESTAT(pipe),
2442 I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
Keith Packard7c463582008-11-04 02:03:27 -08002443 I915_WRITE(IIR, I915_READ(IIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002445
2446void intel_irq_init(struct drm_device *dev)
2447{
2448 dev->driver->get_vblank_counter = i915_get_vblank_counter;
2449 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002450 if (IS_G4X(dev) || IS_GEN5(dev) || IS_GEN6(dev) || IS_IVYBRIDGE(dev) ||
2451 IS_VALLEYVIEW(dev)) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002452 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2453 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2454 }
2455
Keith Packardc3613de2011-08-12 17:05:54 -07002456 if (drm_core_check_feature(dev, DRIVER_MODESET))
2457 dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2458 else
2459 dev->driver->get_vblank_timestamp = NULL;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002460 dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2461
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002462 if (IS_VALLEYVIEW(dev)) {
2463 dev->driver->irq_handler = valleyview_irq_handler;
2464 dev->driver->irq_preinstall = valleyview_irq_preinstall;
2465 dev->driver->irq_postinstall = valleyview_irq_postinstall;
2466 dev->driver->irq_uninstall = valleyview_irq_uninstall;
2467 dev->driver->enable_vblank = valleyview_enable_vblank;
2468 dev->driver->disable_vblank = valleyview_disable_vblank;
2469 } else if (IS_IVYBRIDGE(dev)) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002470 /* Share pre & uninstall handlers with ILK/SNB */
2471 dev->driver->irq_handler = ivybridge_irq_handler;
2472 dev->driver->irq_preinstall = ironlake_irq_preinstall;
2473 dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2474 dev->driver->irq_uninstall = ironlake_irq_uninstall;
2475 dev->driver->enable_vblank = ivybridge_enable_vblank;
2476 dev->driver->disable_vblank = ivybridge_disable_vblank;
2477 } else if (HAS_PCH_SPLIT(dev)) {
2478 dev->driver->irq_handler = ironlake_irq_handler;
2479 dev->driver->irq_preinstall = ironlake_irq_preinstall;
2480 dev->driver->irq_postinstall = ironlake_irq_postinstall;
2481 dev->driver->irq_uninstall = ironlake_irq_uninstall;
2482 dev->driver->enable_vblank = ironlake_enable_vblank;
2483 dev->driver->disable_vblank = ironlake_disable_vblank;
2484 } else {
2485 dev->driver->irq_preinstall = i915_driver_irq_preinstall;
2486 dev->driver->irq_postinstall = i915_driver_irq_postinstall;
2487 dev->driver->irq_uninstall = i915_driver_irq_uninstall;
2488 dev->driver->irq_handler = i915_driver_irq_handler;
2489 dev->driver->enable_vblank = i915_enable_vblank;
2490 dev->driver->disable_vblank = i915_disable_vblank;
2491 }
2492}