blob: adf983f01dda262ba6b83c48dc174b5f35563a05 [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
Jesse Barnes63eeaf32009-06-18 16:56:52 -070029#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "drmP.h"
32#include "drm.h"
33#include "i915_drm.h"
34#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010035#include "i915_trace.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define MAX_NOPID ((u32)~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Keith Packard7c463582008-11-04 02:03:27 -080040/**
41 * Interrupts that are always left unmasked.
42 *
43 * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
44 * we leave them always unmasked in IMR and then control enabling them through
45 * PIPESTAT alone.
46 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050047#define I915_INTERRUPT_ENABLE_FIX \
48 (I915_ASLE_INTERRUPT | \
49 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
50 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT | \
51 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT | \
52 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT | \
53 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Keith Packard7c463582008-11-04 02:03:27 -080054
55/** Interrupts that we mask and unmask at runtime. */
Zou Nan haid1b851f2010-05-21 09:08:57 +080056#define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT | I915_BSD_USER_INTERRUPT)
Keith Packard7c463582008-11-04 02:03:27 -080057
Jesse Barnes79e53942008-11-07 14:24:08 -080058#define I915_PIPE_VBLANK_STATUS (PIPE_START_VBLANK_INTERRUPT_STATUS |\
59 PIPE_VBLANK_INTERRUPT_STATUS)
60
61#define I915_PIPE_VBLANK_ENABLE (PIPE_START_VBLANK_INTERRUPT_ENABLE |\
62 PIPE_VBLANK_INTERRUPT_ENABLE)
63
64#define DRM_I915_VBLANK_PIPE_ALL (DRM_I915_VBLANK_PIPE_A | \
65 DRM_I915_VBLANK_PIPE_B)
66
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +010067void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050068ironlake_enable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080069{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000070 if ((dev_priv->gt_irq_mask & mask) != 0) {
71 dev_priv->gt_irq_mask &= ~mask;
72 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000073 POSTING_READ(GTIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080074 }
75}
76
Eric Anholt62fdfea2010-05-21 13:26:39 -070077void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050078ironlake_disable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080079{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000080 if ((dev_priv->gt_irq_mask & mask) != mask) {
81 dev_priv->gt_irq_mask |= mask;
82 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000083 POSTING_READ(GTIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080084 }
85}
86
87/* For display hotplug interrupt */
Chris Wilson995b67622010-08-20 13:23:26 +010088static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050089ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080090{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000091 if ((dev_priv->irq_mask & mask) != 0) {
92 dev_priv->irq_mask &= ~mask;
93 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000094 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080095 }
96}
97
98static inline void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050099ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800100{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000101 if ((dev_priv->irq_mask & mask) != mask) {
102 dev_priv->irq_mask |= mask;
103 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000104 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800105 }
106}
107
108void
Eric Anholted4cb412008-07-29 12:10:39 -0700109i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
110{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000111 if ((dev_priv->irq_mask & mask) != 0) {
112 dev_priv->irq_mask &= ~mask;
113 I915_WRITE(IMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000114 POSTING_READ(IMR);
Eric Anholted4cb412008-07-29 12:10:39 -0700115 }
116}
117
Eric Anholt62fdfea2010-05-21 13:26:39 -0700118void
Eric Anholted4cb412008-07-29 12:10:39 -0700119i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
120{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000121 if ((dev_priv->irq_mask & mask) != mask) {
122 dev_priv->irq_mask |= mask;
123 I915_WRITE(IMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000124 POSTING_READ(IMR);
Eric Anholted4cb412008-07-29 12:10:39 -0700125 }
126}
127
Keith Packard7c463582008-11-04 02:03:27 -0800128static inline u32
129i915_pipestat(int pipe)
130{
131 if (pipe == 0)
132 return PIPEASTAT;
133 if (pipe == 1)
134 return PIPEBSTAT;
Andrew Morton9c84ba42008-12-01 13:14:08 -0800135 BUG();
Keith Packard7c463582008-11-04 02:03:27 -0800136}
137
138void
139i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
140{
141 if ((dev_priv->pipestat[pipe] & mask) != mask) {
142 u32 reg = i915_pipestat(pipe);
143
144 dev_priv->pipestat[pipe] |= mask;
145 /* Enable the interrupt, clear any pending status */
146 I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
Chris Wilson3143a2b2010-11-16 15:55:10 +0000147 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800148 }
149}
150
151void
152i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
153{
154 if ((dev_priv->pipestat[pipe] & mask) != 0) {
155 u32 reg = i915_pipestat(pipe);
156
157 dev_priv->pipestat[pipe] &= ~mask;
158 I915_WRITE(reg, dev_priv->pipestat[pipe]);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000159 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800160 }
161}
162
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000163/**
Zhao Yakui01c66882009-10-28 05:10:00 +0000164 * intel_enable_asle - enable ASLE interrupt for OpRegion
165 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000166void intel_enable_asle(struct drm_device *dev)
Zhao Yakui01c66882009-10-28 05:10:00 +0000167{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000168 drm_i915_private_t *dev_priv = dev->dev_private;
169 unsigned long irqflags;
170
171 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000172
Eric Anholtc619eed2010-01-28 16:45:52 -0800173 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500174 ironlake_enable_display_irq(dev_priv, DE_GSE);
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800175 else {
Zhao Yakui01c66882009-10-28 05:10:00 +0000176 i915_enable_pipestat(dev_priv, 1,
Jesse Barnesd874bcf2010-06-30 13:16:00 -0700177 PIPE_LEGACY_BLC_EVENT_ENABLE);
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100178 if (INTEL_INFO(dev)->gen >= 4)
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800179 i915_enable_pipestat(dev_priv, 0,
Jesse Barnesd874bcf2010-06-30 13:16:00 -0700180 PIPE_LEGACY_BLC_EVENT_ENABLE);
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800181 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000182
183 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000184}
185
186/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700187 * i915_pipe_enabled - check if a pipe is enabled
188 * @dev: DRM device
189 * @pipe: pipe to check
190 *
191 * Reading certain registers when the pipe is disabled can hang the chip.
192 * Use this routine to make sure the PLL is running and the pipe is active
193 * before reading such registers if unsure.
194 */
195static int
196i915_pipe_enabled(struct drm_device *dev, int pipe)
197{
198 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson5eddb702010-09-11 13:48:45 +0100199 return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700200}
201
Keith Packard42f52ef2008-10-18 19:39:29 -0700202/* Called from drm generic code, passed a 'crtc', which
203 * we use as a pipe index
204 */
205u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700206{
207 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
208 unsigned long high_frame;
209 unsigned long low_frame;
Chris Wilson5eddb702010-09-11 13:48:45 +0100210 u32 high1, high2, low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700211
212 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800213 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
214 "pipe %d\n", pipe);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700215 return 0;
216 }
217
Chris Wilson5eddb702010-09-11 13:48:45 +0100218 high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
219 low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
220
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700221 /*
222 * High & low register fields aren't synchronized, so make sure
223 * we get a low value that's stable across two reads of the high
224 * register.
225 */
226 do {
Chris Wilson5eddb702010-09-11 13:48:45 +0100227 high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
228 low = I915_READ(low_frame) & PIPE_FRAME_LOW_MASK;
229 high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700230 } while (high1 != high2);
231
Chris Wilson5eddb702010-09-11 13:48:45 +0100232 high1 >>= PIPE_FRAME_HIGH_SHIFT;
233 low >>= PIPE_FRAME_LOW_SHIFT;
234 return (high1 << 8) | low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700235}
236
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800237u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
238{
239 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
240 int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
241
242 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800243 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
244 "pipe %d\n", pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800245 return 0;
246 }
247
248 return I915_READ(reg);
249}
250
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100251int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
252 int *vpos, int *hpos)
253{
254 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
255 u32 vbl = 0, position = 0;
256 int vbl_start, vbl_end, htotal, vtotal;
257 bool in_vbl = true;
258 int ret = 0;
259
260 if (!i915_pipe_enabled(dev, pipe)) {
261 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
262 "pipe %d\n", pipe);
263 return 0;
264 }
265
266 /* Get vtotal. */
267 vtotal = 1 + ((I915_READ(VTOTAL(pipe)) >> 16) & 0x1fff);
268
269 if (INTEL_INFO(dev)->gen >= 4) {
270 /* No obvious pixelcount register. Only query vertical
271 * scanout position from Display scan line register.
272 */
273 position = I915_READ(PIPEDSL(pipe));
274
275 /* Decode into vertical scanout position. Don't have
276 * horizontal scanout position.
277 */
278 *vpos = position & 0x1fff;
279 *hpos = 0;
280 } else {
281 /* Have access to pixelcount since start of frame.
282 * We can split this into vertical and horizontal
283 * scanout position.
284 */
285 position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
286
287 htotal = 1 + ((I915_READ(HTOTAL(pipe)) >> 16) & 0x1fff);
288 *vpos = position / htotal;
289 *hpos = position - (*vpos * htotal);
290 }
291
292 /* Query vblank area. */
293 vbl = I915_READ(VBLANK(pipe));
294
295 /* Test position against vblank region. */
296 vbl_start = vbl & 0x1fff;
297 vbl_end = (vbl >> 16) & 0x1fff;
298
299 if ((*vpos < vbl_start) || (*vpos > vbl_end))
300 in_vbl = false;
301
302 /* Inside "upper part" of vblank area? Apply corrective offset: */
303 if (in_vbl && (*vpos >= vbl_start))
304 *vpos = *vpos - vtotal;
305
306 /* Readouts valid? */
307 if (vbl > 0)
308 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
309
310 /* In vblank? */
311 if (in_vbl)
312 ret |= DRM_SCANOUTPOS_INVBL;
313
314 return ret;
315}
316
317int i915_get_vblank_timestamp(struct drm_device *dev, int crtc,
318 int *max_error,
319 struct timeval *vblank_time,
320 unsigned flags)
321{
322 struct drm_crtc *drmcrtc;
323
324 if (crtc < 0 || crtc >= dev->num_crtcs) {
325 DRM_ERROR("Invalid crtc %d\n", crtc);
326 return -EINVAL;
327 }
328
329 /* Get drm_crtc to timestamp: */
330 drmcrtc = intel_get_crtc_for_pipe(dev, crtc);
331
332 /* Helper routine in DRM core does all the work: */
333 return drm_calc_vbltimestamp_from_scanoutpos(dev, crtc, max_error,
334 vblank_time, flags, drmcrtc);
335}
336
Jesse Barnes5ca58282009-03-31 14:11:15 -0700337/*
338 * Handle hotplug events outside the interrupt handler proper.
339 */
340static void i915_hotplug_work_func(struct work_struct *work)
341{
342 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
343 hotplug_work);
344 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700345 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100346 struct intel_encoder *encoder;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700347
Chris Wilson4ef69c72010-09-09 15:14:28 +0100348 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
349 if (encoder->hot_plug)
350 encoder->hot_plug(encoder);
351
Jesse Barnes5ca58282009-03-31 14:11:15 -0700352 /* Just fire off a uevent and let userspace tell us what to do */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000353 drm_helper_hpd_irq_event(dev);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700354}
355
Jesse Barnesf97108d2010-01-29 11:27:07 -0800356static void i915_handle_rps_change(struct drm_device *dev)
357{
358 drm_i915_private_t *dev_priv = dev->dev_private;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000359 u32 busy_up, busy_down, max_avg, min_avg;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800360 u8 new_delay = dev_priv->cur_delay;
361
Jesse Barnes7648fa92010-05-20 14:28:11 -0700362 I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000363 busy_up = I915_READ(RCPREVBSYTUPAVG);
364 busy_down = I915_READ(RCPREVBSYTDNAVG);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800365 max_avg = I915_READ(RCBMAXAVG);
366 min_avg = I915_READ(RCBMINAVG);
367
368 /* Handle RCS change request from hw */
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000369 if (busy_up > max_avg) {
Jesse Barnesf97108d2010-01-29 11:27:07 -0800370 if (dev_priv->cur_delay != dev_priv->max_delay)
371 new_delay = dev_priv->cur_delay - 1;
372 if (new_delay < dev_priv->max_delay)
373 new_delay = dev_priv->max_delay;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000374 } else if (busy_down < min_avg) {
Jesse Barnesf97108d2010-01-29 11:27:07 -0800375 if (dev_priv->cur_delay != dev_priv->min_delay)
376 new_delay = dev_priv->cur_delay + 1;
377 if (new_delay > dev_priv->min_delay)
378 new_delay = dev_priv->min_delay;
379 }
380
Jesse Barnes7648fa92010-05-20 14:28:11 -0700381 if (ironlake_set_drps(dev, new_delay))
382 dev_priv->cur_delay = new_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800383
384 return;
385}
386
Chris Wilson549f7362010-10-19 11:19:32 +0100387static void notify_ring(struct drm_device *dev,
388 struct intel_ring_buffer *ring)
389{
390 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100391 u32 seqno = ring->get_seqno(ring);
Chris Wilsonb2223492010-10-27 15:27:33 +0100392 ring->irq_seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +0100393 trace_i915_gem_request_complete(dev, seqno);
394 wake_up_all(&ring->irq_queue);
395 dev_priv->hangcheck_count = 0;
396 mod_timer(&dev_priv->hangcheck_timer,
397 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
398}
399
Chris Wilson995b67622010-08-20 13:23:26 +0100400static irqreturn_t ironlake_irq_handler(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800401{
402 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
403 int ret = IRQ_NONE;
Dave Airlie3ff99162009-12-08 14:03:47 +1000404 u32 de_iir, gt_iir, de_ier, pch_iir;
Yuanhan Liu2d7b8362010-10-08 10:21:06 +0100405 u32 hotplug_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800406 struct drm_i915_master_private *master_priv;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100407 u32 bsd_usr_interrupt = GT_BSD_USER_INTERRUPT;
408
409 if (IS_GEN6(dev))
410 bsd_usr_interrupt = GT_GEN6_BSD_USER_INTERRUPT;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800411
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000412 /* disable master interrupt before clearing iir */
413 de_ier = I915_READ(DEIER);
414 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000415 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000416
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800417 de_iir = I915_READ(DEIIR);
418 gt_iir = I915_READ(GTIIR);
Zhenyu Wangc6501562009-11-03 18:57:21 +0000419 pch_iir = I915_READ(SDEIIR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800420
Zou Nan haic7c85102010-01-15 10:29:06 +0800421 if (de_iir == 0 && gt_iir == 0 && pch_iir == 0)
422 goto done;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800423
Yuanhan Liu2d7b8362010-10-08 10:21:06 +0100424 if (HAS_PCH_CPT(dev))
425 hotplug_mask = SDE_HOTPLUG_MASK_CPT;
426 else
427 hotplug_mask = SDE_HOTPLUG_MASK;
428
Zou Nan haic7c85102010-01-15 10:29:06 +0800429 ret = IRQ_HANDLED;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800430
Zou Nan haic7c85102010-01-15 10:29:06 +0800431 if (dev->primary->master) {
432 master_priv = dev->primary->master->driver_priv;
433 if (master_priv->sarea_priv)
434 master_priv->sarea_priv->last_dispatch =
435 READ_BREADCRUMB(dev_priv);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800436 }
437
Chris Wilsonc6df5412010-12-15 09:56:50 +0000438 if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000439 notify_ring(dev, &dev_priv->ring[RCS]);
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100440 if (gt_iir & bsd_usr_interrupt)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000441 notify_ring(dev, &dev_priv->ring[VCS]);
442 if (gt_iir & GT_BLT_USER_INTERRUPT)
443 notify_ring(dev, &dev_priv->ring[BCS]);
Zou Nan haic7c85102010-01-15 10:29:06 +0800444
445 if (de_iir & DE_GSE)
Chris Wilson3b617962010-08-24 09:02:58 +0100446 intel_opregion_gse_intr(dev);
Zou Nan haic7c85102010-01-15 10:29:06 +0800447
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800448 if (de_iir & DE_PLANEA_FLIP_DONE) {
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800449 intel_prepare_page_flip(dev, 0);
Chris Wilson2bbda382010-09-02 17:59:39 +0100450 intel_finish_page_flip_plane(dev, 0);
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800451 }
452
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800453 if (de_iir & DE_PLANEB_FLIP_DONE) {
454 intel_prepare_page_flip(dev, 1);
Chris Wilson2bbda382010-09-02 17:59:39 +0100455 intel_finish_page_flip_plane(dev, 1);
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800456 }
Li Pengc062df62010-01-23 00:12:58 +0800457
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800458 if (de_iir & DE_PIPEA_VBLANK)
459 drm_handle_vblank(dev, 0);
460
461 if (de_iir & DE_PIPEB_VBLANK)
462 drm_handle_vblank(dev, 1);
463
Zou Nan haic7c85102010-01-15 10:29:06 +0800464 /* check event from PCH */
Yuanhan Liu2d7b8362010-10-08 10:21:06 +0100465 if ((de_iir & DE_PCH_EVENT) && (pch_iir & hotplug_mask))
Zou Nan haic7c85102010-01-15 10:29:06 +0800466 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
Zou Nan haic7c85102010-01-15 10:29:06 +0800467
Jesse Barnesf97108d2010-01-29 11:27:07 -0800468 if (de_iir & DE_PCU_EVENT) {
Jesse Barnes7648fa92010-05-20 14:28:11 -0700469 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
Jesse Barnesf97108d2010-01-29 11:27:07 -0800470 i915_handle_rps_change(dev);
471 }
472
Zou Nan haic7c85102010-01-15 10:29:06 +0800473 /* should clear PCH hotplug event before clear CPU irq */
474 I915_WRITE(SDEIIR, pch_iir);
475 I915_WRITE(GTIIR, gt_iir);
476 I915_WRITE(DEIIR, de_iir);
477
478done:
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000479 I915_WRITE(DEIER, de_ier);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000480 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000481
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800482 return ret;
483}
484
Jesse Barnes8a905232009-07-11 16:48:03 -0400485/**
486 * i915_error_work_func - do process context error handling work
487 * @work: work struct
488 *
489 * Fire an error uevent so userspace can see that a hang or error
490 * was detected.
491 */
492static void i915_error_work_func(struct work_struct *work)
493{
494 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
495 error_work);
496 struct drm_device *dev = dev_priv->dev;
Ben Gamarif316a422009-09-14 17:48:46 -0400497 char *error_event[] = { "ERROR=1", NULL };
498 char *reset_event[] = { "RESET=1", NULL };
499 char *reset_done_event[] = { "ERROR=0", NULL };
Jesse Barnes8a905232009-07-11 16:48:03 -0400500
Ben Gamarif316a422009-09-14 17:48:46 -0400501 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -0400502
Ben Gamariba1234d2009-09-14 17:48:47 -0400503 if (atomic_read(&dev_priv->mm.wedged)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +0100504 DRM_DEBUG_DRIVER("resetting chip\n");
505 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
506 if (!i915_reset(dev, GRDOM_RENDER)) {
507 atomic_set(&dev_priv->mm.wedged, 0);
508 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
Ben Gamarif316a422009-09-14 17:48:46 -0400509 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100510 complete_all(&dev_priv->error_completion);
Ben Gamarif316a422009-09-14 17:48:46 -0400511 }
Jesse Barnes8a905232009-07-11 16:48:03 -0400512}
513
Chris Wilson3bd3c932010-08-19 08:19:30 +0100514#ifdef CONFIG_DEBUG_FS
Chris Wilson9df30792010-02-18 10:24:56 +0000515static struct drm_i915_error_object *
516i915_error_object_create(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +0000517 struct drm_i915_gem_object *src)
Chris Wilson9df30792010-02-18 10:24:56 +0000518{
Chris Wilsone56660d2010-08-07 11:01:26 +0100519 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson9df30792010-02-18 10:24:56 +0000520 struct drm_i915_error_object *dst;
Chris Wilson9df30792010-02-18 10:24:56 +0000521 int page, page_count;
Chris Wilsone56660d2010-08-07 11:01:26 +0100522 u32 reloc_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000523
Chris Wilson05394f32010-11-08 19:18:58 +0000524 if (src == NULL || src->pages == NULL)
Chris Wilson9df30792010-02-18 10:24:56 +0000525 return NULL;
526
Chris Wilson05394f32010-11-08 19:18:58 +0000527 page_count = src->base.size / PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +0000528
529 dst = kmalloc(sizeof(*dst) + page_count * sizeof (u32 *), GFP_ATOMIC);
530 if (dst == NULL)
531 return NULL;
532
Chris Wilson05394f32010-11-08 19:18:58 +0000533 reloc_offset = src->gtt_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000534 for (page = 0; page < page_count; page++) {
Andrew Morton788885a2010-05-11 14:07:05 -0700535 unsigned long flags;
Chris Wilsone56660d2010-08-07 11:01:26 +0100536 void __iomem *s;
537 void *d;
Andrew Morton788885a2010-05-11 14:07:05 -0700538
Chris Wilsone56660d2010-08-07 11:01:26 +0100539 d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +0000540 if (d == NULL)
541 goto unwind;
Chris Wilsone56660d2010-08-07 11:01:26 +0100542
Andrew Morton788885a2010-05-11 14:07:05 -0700543 local_irq_save(flags);
Chris Wilsone56660d2010-08-07 11:01:26 +0100544 s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700545 reloc_offset);
Chris Wilsone56660d2010-08-07 11:01:26 +0100546 memcpy_fromio(d, s, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700547 io_mapping_unmap_atomic(s);
Andrew Morton788885a2010-05-11 14:07:05 -0700548 local_irq_restore(flags);
Chris Wilsone56660d2010-08-07 11:01:26 +0100549
Chris Wilson9df30792010-02-18 10:24:56 +0000550 dst->pages[page] = d;
Chris Wilsone56660d2010-08-07 11:01:26 +0100551
552 reloc_offset += PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +0000553 }
554 dst->page_count = page_count;
Chris Wilson05394f32010-11-08 19:18:58 +0000555 dst->gtt_offset = src->gtt_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000556
557 return dst;
558
559unwind:
560 while (page--)
561 kfree(dst->pages[page]);
562 kfree(dst);
563 return NULL;
564}
565
566static void
567i915_error_object_free(struct drm_i915_error_object *obj)
568{
569 int page;
570
571 if (obj == NULL)
572 return;
573
574 for (page = 0; page < obj->page_count; page++)
575 kfree(obj->pages[page]);
576
577 kfree(obj);
578}
579
580static void
581i915_error_state_free(struct drm_device *dev,
582 struct drm_i915_error_state *error)
583{
584 i915_error_object_free(error->batchbuffer[0]);
585 i915_error_object_free(error->batchbuffer[1]);
586 i915_error_object_free(error->ringbuffer);
587 kfree(error->active_bo);
Chris Wilson6ef3d422010-08-04 20:26:07 +0100588 kfree(error->overlay);
Chris Wilson9df30792010-02-18 10:24:56 +0000589 kfree(error);
590}
591
592static u32
593i915_get_bbaddr(struct drm_device *dev, u32 *ring)
594{
595 u32 cmd;
596
597 if (IS_I830(dev) || IS_845G(dev))
598 cmd = MI_BATCH_BUFFER;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100599 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson9df30792010-02-18 10:24:56 +0000600 cmd = (MI_BATCH_BUFFER_START | (2 << 6) |
601 MI_BATCH_NON_SECURE_I965);
602 else
603 cmd = (MI_BATCH_BUFFER_START | (2 << 6));
604
605 return ring[0] == cmd ? ring[1] : 0;
606}
607
608static u32
Chris Wilson8168bd42010-11-11 17:54:52 +0000609i915_ringbuffer_last_batch(struct drm_device *dev,
610 struct intel_ring_buffer *ring)
Chris Wilson9df30792010-02-18 10:24:56 +0000611{
612 struct drm_i915_private *dev_priv = dev->dev_private;
613 u32 head, bbaddr;
Chris Wilson8168bd42010-11-11 17:54:52 +0000614 u32 *val;
Chris Wilson9df30792010-02-18 10:24:56 +0000615
616 /* Locate the current position in the ringbuffer and walk back
617 * to find the most recently dispatched batch buffer.
618 */
Chris Wilson8168bd42010-11-11 17:54:52 +0000619 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Chris Wilson9df30792010-02-18 10:24:56 +0000620
Chris Wilsonab5793a2010-11-22 13:24:13 +0000621 val = (u32 *)(ring->virtual_start + head);
Chris Wilson8168bd42010-11-11 17:54:52 +0000622 while (--val >= (u32 *)ring->virtual_start) {
623 bbaddr = i915_get_bbaddr(dev, val);
Chris Wilson9df30792010-02-18 10:24:56 +0000624 if (bbaddr)
Chris Wilsonab5793a2010-11-22 13:24:13 +0000625 return bbaddr;
Chris Wilson9df30792010-02-18 10:24:56 +0000626 }
627
Chris Wilsonab5793a2010-11-22 13:24:13 +0000628 val = (u32 *)(ring->virtual_start + ring->size);
629 while (--val >= (u32 *)ring->virtual_start) {
630 bbaddr = i915_get_bbaddr(dev, val);
631 if (bbaddr)
632 return bbaddr;
Chris Wilson9df30792010-02-18 10:24:56 +0000633 }
634
Chris Wilsonab5793a2010-11-22 13:24:13 +0000635 return 0;
Chris Wilson9df30792010-02-18 10:24:56 +0000636}
637
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000638static u32 capture_bo_list(struct drm_i915_error_buffer *err,
639 int count,
640 struct list_head *head)
641{
642 struct drm_i915_gem_object *obj;
643 int i = 0;
644
645 list_for_each_entry(obj, head, mm_list) {
646 err->size = obj->base.size;
647 err->name = obj->base.name;
648 err->seqno = obj->last_rendering_seqno;
649 err->gtt_offset = obj->gtt_offset;
650 err->read_domains = obj->base.read_domains;
651 err->write_domain = obj->base.write_domain;
652 err->fence_reg = obj->fence_reg;
653 err->pinned = 0;
654 if (obj->pin_count > 0)
655 err->pinned = 1;
656 if (obj->user_pin_count > 0)
657 err->pinned = -1;
658 err->tiling = obj->tiling_mode;
659 err->dirty = obj->dirty;
660 err->purgeable = obj->madv != I915_MADV_WILLNEED;
Chris Wilson36850922010-11-23 08:49:38 +0000661 err->ring = obj->ring ? obj->ring->id : 0;
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000662
663 if (++i == count)
664 break;
665
666 err++;
667 }
668
669 return i;
670}
671
Chris Wilson748ebc62010-10-24 10:28:47 +0100672static void i915_gem_record_fences(struct drm_device *dev,
673 struct drm_i915_error_state *error)
674{
675 struct drm_i915_private *dev_priv = dev->dev_private;
676 int i;
677
678 /* Fences */
679 switch (INTEL_INFO(dev)->gen) {
680 case 6:
681 for (i = 0; i < 16; i++)
682 error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
683 break;
684 case 5:
685 case 4:
686 for (i = 0; i < 16; i++)
687 error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
688 break;
689 case 3:
690 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
691 for (i = 0; i < 8; i++)
692 error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
693 case 2:
694 for (i = 0; i < 8; i++)
695 error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
696 break;
697
698 }
699}
700
Jesse Barnes8a905232009-07-11 16:48:03 -0400701/**
702 * i915_capture_error_state - capture an error record for later analysis
703 * @dev: drm device
704 *
705 * Should be called when an error is detected (either a hang or an error
706 * interrupt) to capture error state from the time of the error. Fills
707 * out a structure which becomes available in debugfs for user level tools
708 * to pick up.
709 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700710static void i915_capture_error_state(struct drm_device *dev)
711{
712 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000713 struct drm_i915_gem_object *obj;
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700714 struct drm_i915_error_state *error;
Chris Wilson05394f32010-11-08 19:18:58 +0000715 struct drm_i915_gem_object *batchbuffer[2];
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700716 unsigned long flags;
Chris Wilson9df30792010-02-18 10:24:56 +0000717 u32 bbaddr;
718 int count;
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700719
720 spin_lock_irqsave(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +0000721 error = dev_priv->first_error;
722 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
723 if (error)
724 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700725
726 error = kmalloc(sizeof(*error), GFP_ATOMIC);
727 if (!error) {
Chris Wilson9df30792010-02-18 10:24:56 +0000728 DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
729 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700730 }
731
Chris Wilson2fa772f32010-10-01 13:23:27 +0100732 DRM_DEBUG_DRIVER("generating error event\n");
733
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000734 error->seqno = dev_priv->ring[RCS].get_seqno(&dev_priv->ring[RCS]);
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700735 error->eir = I915_READ(EIR);
736 error->pgtbl_er = I915_READ(PGTBL_ER);
737 error->pipeastat = I915_READ(PIPEASTAT);
738 error->pipebstat = I915_READ(PIPEBSTAT);
739 error->instpm = I915_READ(INSTPM);
Chris Wilsonf4068392010-10-27 20:36:41 +0100740 error->error = 0;
741 if (INTEL_INFO(dev)->gen >= 6) {
742 error->error = I915_READ(ERROR_GEN6);
Chris Wilsonadd354d2010-10-29 19:00:51 +0100743
Chris Wilson1d8f38f2010-10-29 19:00:51 +0100744 error->bcs_acthd = I915_READ(BCS_ACTHD);
745 error->bcs_ipehr = I915_READ(BCS_IPEHR);
746 error->bcs_ipeir = I915_READ(BCS_IPEIR);
747 error->bcs_instdone = I915_READ(BCS_INSTDONE);
748 error->bcs_seqno = 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000749 if (dev_priv->ring[BCS].get_seqno)
750 error->bcs_seqno = dev_priv->ring[BCS].get_seqno(&dev_priv->ring[BCS]);
Chris Wilsonadd354d2010-10-29 19:00:51 +0100751
752 error->vcs_acthd = I915_READ(VCS_ACTHD);
753 error->vcs_ipehr = I915_READ(VCS_IPEHR);
754 error->vcs_ipeir = I915_READ(VCS_IPEIR);
755 error->vcs_instdone = I915_READ(VCS_INSTDONE);
756 error->vcs_seqno = 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000757 if (dev_priv->ring[VCS].get_seqno)
758 error->vcs_seqno = dev_priv->ring[VCS].get_seqno(&dev_priv->ring[VCS]);
Chris Wilsonf4068392010-10-27 20:36:41 +0100759 }
760 if (INTEL_INFO(dev)->gen >= 4) {
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700761 error->ipeir = I915_READ(IPEIR_I965);
762 error->ipehr = I915_READ(IPEHR_I965);
763 error->instdone = I915_READ(INSTDONE_I965);
764 error->instps = I915_READ(INSTPS);
765 error->instdone1 = I915_READ(INSTDONE1);
766 error->acthd = I915_READ(ACTHD_I965);
Chris Wilson9df30792010-02-18 10:24:56 +0000767 error->bbaddr = I915_READ64(BB_ADDR);
Chris Wilsonf4068392010-10-27 20:36:41 +0100768 } else {
769 error->ipeir = I915_READ(IPEIR);
770 error->ipehr = I915_READ(IPEHR);
771 error->instdone = I915_READ(INSTDONE);
772 error->acthd = I915_READ(ACTHD);
773 error->bbaddr = 0;
Chris Wilson9df30792010-02-18 10:24:56 +0000774 }
Chris Wilson748ebc62010-10-24 10:28:47 +0100775 i915_gem_record_fences(dev, error);
Chris Wilson9df30792010-02-18 10:24:56 +0000776
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000777 bbaddr = i915_ringbuffer_last_batch(dev, &dev_priv->ring[RCS]);
Chris Wilson9df30792010-02-18 10:24:56 +0000778
779 /* Grab the current batchbuffer, most likely to have crashed. */
780 batchbuffer[0] = NULL;
781 batchbuffer[1] = NULL;
782 count = 0;
Chris Wilson05394f32010-11-08 19:18:58 +0000783 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
Chris Wilson9df30792010-02-18 10:24:56 +0000784 if (batchbuffer[0] == NULL &&
Chris Wilson05394f32010-11-08 19:18:58 +0000785 bbaddr >= obj->gtt_offset &&
786 bbaddr < obj->gtt_offset + obj->base.size)
Chris Wilson9df30792010-02-18 10:24:56 +0000787 batchbuffer[0] = obj;
788
789 if (batchbuffer[1] == NULL &&
Chris Wilson05394f32010-11-08 19:18:58 +0000790 error->acthd >= obj->gtt_offset &&
791 error->acthd < obj->gtt_offset + obj->base.size)
Chris Wilson9df30792010-02-18 10:24:56 +0000792 batchbuffer[1] = obj;
793
794 count++;
795 }
Chris Wilsone56660d2010-08-07 11:01:26 +0100796 /* Scan the other lists for completeness for those bizarre errors. */
797 if (batchbuffer[0] == NULL || batchbuffer[1] == NULL) {
Chris Wilson05394f32010-11-08 19:18:58 +0000798 list_for_each_entry(obj, &dev_priv->mm.flushing_list, mm_list) {
Chris Wilsone56660d2010-08-07 11:01:26 +0100799 if (batchbuffer[0] == NULL &&
Chris Wilson05394f32010-11-08 19:18:58 +0000800 bbaddr >= obj->gtt_offset &&
801 bbaddr < obj->gtt_offset + obj->base.size)
Chris Wilsone56660d2010-08-07 11:01:26 +0100802 batchbuffer[0] = obj;
803
804 if (batchbuffer[1] == NULL &&
Chris Wilson05394f32010-11-08 19:18:58 +0000805 error->acthd >= obj->gtt_offset &&
806 error->acthd < obj->gtt_offset + obj->base.size)
Chris Wilsone56660d2010-08-07 11:01:26 +0100807 batchbuffer[1] = obj;
808
809 if (batchbuffer[0] && batchbuffer[1])
810 break;
811 }
812 }
813 if (batchbuffer[0] == NULL || batchbuffer[1] == NULL) {
Chris Wilson05394f32010-11-08 19:18:58 +0000814 list_for_each_entry(obj, &dev_priv->mm.inactive_list, mm_list) {
Chris Wilsone56660d2010-08-07 11:01:26 +0100815 if (batchbuffer[0] == NULL &&
Chris Wilson05394f32010-11-08 19:18:58 +0000816 bbaddr >= obj->gtt_offset &&
817 bbaddr < obj->gtt_offset + obj->base.size)
Chris Wilsone56660d2010-08-07 11:01:26 +0100818 batchbuffer[0] = obj;
819
820 if (batchbuffer[1] == NULL &&
Chris Wilson05394f32010-11-08 19:18:58 +0000821 error->acthd >= obj->gtt_offset &&
822 error->acthd < obj->gtt_offset + obj->base.size)
Chris Wilsone56660d2010-08-07 11:01:26 +0100823 batchbuffer[1] = obj;
824
825 if (batchbuffer[0] && batchbuffer[1])
826 break;
827 }
828 }
Chris Wilson9df30792010-02-18 10:24:56 +0000829
830 /* We need to copy these to an anonymous buffer as the simplest
Andrea Gelmini139d3632010-10-15 17:14:33 +0200831 * method to avoid being overwritten by userspace.
Chris Wilson9df30792010-02-18 10:24:56 +0000832 */
833 error->batchbuffer[0] = i915_error_object_create(dev, batchbuffer[0]);
Chris Wilsone56660d2010-08-07 11:01:26 +0100834 if (batchbuffer[1] != batchbuffer[0])
835 error->batchbuffer[1] = i915_error_object_create(dev, batchbuffer[1]);
836 else
837 error->batchbuffer[1] = NULL;
Chris Wilson9df30792010-02-18 10:24:56 +0000838
839 /* Record the ringbuffer */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800840 error->ringbuffer = i915_error_object_create(dev,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000841 dev_priv->ring[RCS].obj);
Chris Wilson9df30792010-02-18 10:24:56 +0000842
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000843 /* Record buffers on the active and pinned lists. */
Chris Wilson9df30792010-02-18 10:24:56 +0000844 error->active_bo = NULL;
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000845 error->pinned_bo = NULL;
Chris Wilson9df30792010-02-18 10:24:56 +0000846
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000847 error->active_bo_count = count;
Chris Wilson05394f32010-11-08 19:18:58 +0000848 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000849 count++;
850 error->pinned_bo_count = count - error->active_bo_count;
851
852 if (count) {
Chris Wilson9df30792010-02-18 10:24:56 +0000853 error->active_bo = kmalloc(sizeof(*error->active_bo)*count,
854 GFP_ATOMIC);
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000855 if (error->active_bo)
856 error->pinned_bo =
857 error->active_bo + error->active_bo_count;
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700858 }
859
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000860 if (error->active_bo)
861 error->active_bo_count =
862 capture_bo_list(error->active_bo,
863 error->active_bo_count,
864 &dev_priv->mm.active_list);
865
866 if (error->pinned_bo)
867 error->pinned_bo_count =
868 capture_bo_list(error->pinned_bo,
869 error->pinned_bo_count,
870 &dev_priv->mm.pinned_list);
871
Jesse Barnes8a905232009-07-11 16:48:03 -0400872 do_gettimeofday(&error->time);
873
Chris Wilson6ef3d422010-08-04 20:26:07 +0100874 error->overlay = intel_overlay_capture_error_state(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +0000875 error->display = intel_display_capture_error_state(dev);
Chris Wilson6ef3d422010-08-04 20:26:07 +0100876
Chris Wilson9df30792010-02-18 10:24:56 +0000877 spin_lock_irqsave(&dev_priv->error_lock, flags);
878 if (dev_priv->first_error == NULL) {
879 dev_priv->first_error = error;
880 error = NULL;
881 }
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700882 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +0000883
884 if (error)
885 i915_error_state_free(dev, error);
886}
887
888void i915_destroy_error_state(struct drm_device *dev)
889{
890 struct drm_i915_private *dev_priv = dev->dev_private;
891 struct drm_i915_error_state *error;
892
893 spin_lock(&dev_priv->error_lock);
894 error = dev_priv->first_error;
895 dev_priv->first_error = NULL;
896 spin_unlock(&dev_priv->error_lock);
897
898 if (error)
899 i915_error_state_free(dev, error);
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700900}
Chris Wilson3bd3c932010-08-19 08:19:30 +0100901#else
902#define i915_capture_error_state(x)
903#endif
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700904
Chris Wilson35aed2e2010-05-27 13:18:12 +0100905static void i915_report_and_clear_eir(struct drm_device *dev)
Jesse Barnes8a905232009-07-11 16:48:03 -0400906{
907 struct drm_i915_private *dev_priv = dev->dev_private;
908 u32 eir = I915_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -0400909
Chris Wilson35aed2e2010-05-27 13:18:12 +0100910 if (!eir)
911 return;
Jesse Barnes8a905232009-07-11 16:48:03 -0400912
913 printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
914 eir);
915
916 if (IS_G4X(dev)) {
917 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
918 u32 ipeir = I915_READ(IPEIR_I965);
919
920 printk(KERN_ERR " IPEIR: 0x%08x\n",
921 I915_READ(IPEIR_I965));
922 printk(KERN_ERR " IPEHR: 0x%08x\n",
923 I915_READ(IPEHR_I965));
924 printk(KERN_ERR " INSTDONE: 0x%08x\n",
925 I915_READ(INSTDONE_I965));
926 printk(KERN_ERR " INSTPS: 0x%08x\n",
927 I915_READ(INSTPS));
928 printk(KERN_ERR " INSTDONE1: 0x%08x\n",
929 I915_READ(INSTDONE1));
930 printk(KERN_ERR " ACTHD: 0x%08x\n",
931 I915_READ(ACTHD_I965));
932 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000933 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -0400934 }
935 if (eir & GM45_ERROR_PAGE_TABLE) {
936 u32 pgtbl_err = I915_READ(PGTBL_ER);
937 printk(KERN_ERR "page table error\n");
938 printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
939 pgtbl_err);
940 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000941 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -0400942 }
943 }
944
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100945 if (!IS_GEN2(dev)) {
Jesse Barnes8a905232009-07-11 16:48:03 -0400946 if (eir & I915_ERROR_PAGE_TABLE) {
947 u32 pgtbl_err = I915_READ(PGTBL_ER);
948 printk(KERN_ERR "page table error\n");
949 printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
950 pgtbl_err);
951 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000952 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -0400953 }
954 }
955
956 if (eir & I915_ERROR_MEMORY_REFRESH) {
Chris Wilson35aed2e2010-05-27 13:18:12 +0100957 u32 pipea_stats = I915_READ(PIPEASTAT);
958 u32 pipeb_stats = I915_READ(PIPEBSTAT);
959
Jesse Barnes8a905232009-07-11 16:48:03 -0400960 printk(KERN_ERR "memory refresh error\n");
961 printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
962 pipea_stats);
963 printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
964 pipeb_stats);
965 /* pipestat has already been acked */
966 }
967 if (eir & I915_ERROR_INSTRUCTION) {
968 printk(KERN_ERR "instruction error\n");
969 printk(KERN_ERR " INSTPM: 0x%08x\n",
970 I915_READ(INSTPM));
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100971 if (INTEL_INFO(dev)->gen < 4) {
Jesse Barnes8a905232009-07-11 16:48:03 -0400972 u32 ipeir = I915_READ(IPEIR);
973
974 printk(KERN_ERR " IPEIR: 0x%08x\n",
975 I915_READ(IPEIR));
976 printk(KERN_ERR " IPEHR: 0x%08x\n",
977 I915_READ(IPEHR));
978 printk(KERN_ERR " INSTDONE: 0x%08x\n",
979 I915_READ(INSTDONE));
980 printk(KERN_ERR " ACTHD: 0x%08x\n",
981 I915_READ(ACTHD));
982 I915_WRITE(IPEIR, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000983 POSTING_READ(IPEIR);
Jesse Barnes8a905232009-07-11 16:48:03 -0400984 } else {
985 u32 ipeir = I915_READ(IPEIR_I965);
986
987 printk(KERN_ERR " IPEIR: 0x%08x\n",
988 I915_READ(IPEIR_I965));
989 printk(KERN_ERR " IPEHR: 0x%08x\n",
990 I915_READ(IPEHR_I965));
991 printk(KERN_ERR " INSTDONE: 0x%08x\n",
992 I915_READ(INSTDONE_I965));
993 printk(KERN_ERR " INSTPS: 0x%08x\n",
994 I915_READ(INSTPS));
995 printk(KERN_ERR " INSTDONE1: 0x%08x\n",
996 I915_READ(INSTDONE1));
997 printk(KERN_ERR " ACTHD: 0x%08x\n",
998 I915_READ(ACTHD_I965));
999 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001000 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001001 }
1002 }
1003
1004 I915_WRITE(EIR, eir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001005 POSTING_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001006 eir = I915_READ(EIR);
1007 if (eir) {
1008 /*
1009 * some errors might have become stuck,
1010 * mask them.
1011 */
1012 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
1013 I915_WRITE(EMR, I915_READ(EMR) | eir);
1014 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
1015 }
Chris Wilson35aed2e2010-05-27 13:18:12 +01001016}
1017
1018/**
1019 * i915_handle_error - handle an error interrupt
1020 * @dev: drm device
1021 *
1022 * Do some basic checking of regsiter state at error interrupt time and
1023 * dump it to the syslog. Also call i915_capture_error_state() to make
1024 * sure we get a record and make it available in debugfs. Fire a uevent
1025 * so userspace knows something bad happened (should trigger collection
1026 * of a ring dump etc.).
1027 */
Chris Wilson527f9e92010-11-11 01:16:58 +00001028void i915_handle_error(struct drm_device *dev, bool wedged)
Chris Wilson35aed2e2010-05-27 13:18:12 +01001029{
1030 struct drm_i915_private *dev_priv = dev->dev_private;
1031
1032 i915_capture_error_state(dev);
1033 i915_report_and_clear_eir(dev);
Jesse Barnes8a905232009-07-11 16:48:03 -04001034
Ben Gamariba1234d2009-09-14 17:48:47 -04001035 if (wedged) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001036 INIT_COMPLETION(dev_priv->error_completion);
Ben Gamariba1234d2009-09-14 17:48:47 -04001037 atomic_set(&dev_priv->mm.wedged, 1);
1038
Ben Gamari11ed50e2009-09-14 17:48:45 -04001039 /*
1040 * Wakeup waiting processes so they don't hang
1041 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001042 wake_up_all(&dev_priv->ring[RCS].irq_queue);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001043 if (HAS_BSD(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001044 wake_up_all(&dev_priv->ring[VCS].irq_queue);
Chris Wilson549f7362010-10-19 11:19:32 +01001045 if (HAS_BLT(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001046 wake_up_all(&dev_priv->ring[BCS].irq_queue);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001047 }
1048
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001049 queue_work(dev_priv->wq, &dev_priv->error_work);
Jesse Barnes8a905232009-07-11 16:48:03 -04001050}
1051
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001052static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
1053{
1054 drm_i915_private_t *dev_priv = dev->dev_private;
1055 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1056 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00001057 struct drm_i915_gem_object *obj;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001058 struct intel_unpin_work *work;
1059 unsigned long flags;
1060 bool stall_detected;
1061
1062 /* Ignore early vblank irqs */
1063 if (intel_crtc == NULL)
1064 return;
1065
1066 spin_lock_irqsave(&dev->event_lock, flags);
1067 work = intel_crtc->unpin_work;
1068
1069 if (work == NULL || work->pending || !work->enable_stall_check) {
1070 /* Either the pending flip IRQ arrived, or we're too early. Don't check */
1071 spin_unlock_irqrestore(&dev->event_lock, flags);
1072 return;
1073 }
1074
1075 /* Potential stall - if we see that the flip has happened, assume a missed interrupt */
Chris Wilson05394f32010-11-08 19:18:58 +00001076 obj = work->pending_flip_obj;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001077 if (INTEL_INFO(dev)->gen >= 4) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001078 int dspsurf = intel_crtc->plane == 0 ? DSPASURF : DSPBSURF;
Chris Wilson05394f32010-11-08 19:18:58 +00001079 stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001080 } else {
1081 int dspaddr = intel_crtc->plane == 0 ? DSPAADDR : DSPBADDR;
Chris Wilson05394f32010-11-08 19:18:58 +00001082 stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001083 crtc->y * crtc->fb->pitch +
1084 crtc->x * crtc->fb->bits_per_pixel/8);
1085 }
1086
1087 spin_unlock_irqrestore(&dev->event_lock, flags);
1088
1089 if (stall_detected) {
1090 DRM_DEBUG_DRIVER("Pageflip stall detected\n");
1091 intel_prepare_page_flip(dev, intel_crtc->plane);
1092 }
1093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
1096{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001097 struct drm_device *dev = (struct drm_device *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001099 struct drm_i915_master_private *master_priv;
Eric Anholtcdfbc412008-11-04 15:50:30 -08001100 u32 iir, new_iir;
1101 u32 pipea_stats, pipeb_stats;
Keith Packard05eff842008-11-19 14:03:05 -08001102 u32 vblank_status;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001103 int vblank = 0;
Keith Packard7c463582008-11-04 02:03:27 -08001104 unsigned long irqflags;
Keith Packard05eff842008-11-19 14:03:05 -08001105 int irq_received;
1106 int ret = IRQ_NONE;
Dave Airlieaf6061a2008-05-07 12:15:39 +10001107
Eric Anholt630681d2008-10-06 15:14:12 -07001108 atomic_inc(&dev_priv->irq_received);
1109
Eric Anholtbad720f2009-10-22 16:11:14 -07001110 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001111 return ironlake_irq_handler(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001112
Eric Anholted4cb412008-07-29 12:10:39 -07001113 iir = I915_READ(IIR);
Dave Airlieaf6061a2008-05-07 12:15:39 +10001114
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001115 if (INTEL_INFO(dev)->gen >= 4)
Jesse Barnesd874bcf2010-06-30 13:16:00 -07001116 vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
Jesse Barnese25e6602010-06-30 13:15:19 -07001117 else
Jesse Barnesd874bcf2010-06-30 13:16:00 -07001118 vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Keith Packard05eff842008-11-19 14:03:05 -08001120 for (;;) {
1121 irq_received = iir != 0;
1122
1123 /* Can't rely on pipestat interrupt bit in iir as it might
1124 * have been cleared after the pipestat interrupt was received.
1125 * It doesn't set the bit in iir again, but it still produces
1126 * interrupts (for non-MSI).
1127 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001128 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Keith Packard05eff842008-11-19 14:03:05 -08001129 pipea_stats = I915_READ(PIPEASTAT);
1130 pipeb_stats = I915_READ(PIPEBSTAT);
Jesse Barnes79e53942008-11-07 14:24:08 -08001131
Jesse Barnes8a905232009-07-11 16:48:03 -04001132 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Ben Gamariba1234d2009-09-14 17:48:47 -04001133 i915_handle_error(dev, false);
Jesse Barnes8a905232009-07-11 16:48:03 -04001134
Eric Anholtcdfbc412008-11-04 15:50:30 -08001135 /*
1136 * Clear the PIPE(A|B)STAT regs before the IIR
1137 */
Keith Packard05eff842008-11-19 14:03:05 -08001138 if (pipea_stats & 0x8000ffff) {
Shaohua Li7662c8b2009-06-26 11:23:55 +08001139 if (pipea_stats & PIPE_FIFO_UNDERRUN_STATUS)
Zhao Yakui44d98a62009-10-09 11:39:40 +08001140 DRM_DEBUG_DRIVER("pipe a underrun\n");
Eric Anholtcdfbc412008-11-04 15:50:30 -08001141 I915_WRITE(PIPEASTAT, pipea_stats);
Keith Packard05eff842008-11-19 14:03:05 -08001142 irq_received = 1;
Eric Anholtcdfbc412008-11-04 15:50:30 -08001143 }
Keith Packard7c463582008-11-04 02:03:27 -08001144
Keith Packard05eff842008-11-19 14:03:05 -08001145 if (pipeb_stats & 0x8000ffff) {
Shaohua Li7662c8b2009-06-26 11:23:55 +08001146 if (pipeb_stats & PIPE_FIFO_UNDERRUN_STATUS)
Zhao Yakui44d98a62009-10-09 11:39:40 +08001147 DRM_DEBUG_DRIVER("pipe b underrun\n");
Eric Anholtcdfbc412008-11-04 15:50:30 -08001148 I915_WRITE(PIPEBSTAT, pipeb_stats);
Keith Packard05eff842008-11-19 14:03:05 -08001149 irq_received = 1;
Eric Anholtcdfbc412008-11-04 15:50:30 -08001150 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001151 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Keith Packard05eff842008-11-19 14:03:05 -08001152
1153 if (!irq_received)
1154 break;
1155
1156 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Jesse Barnes5ca58282009-03-31 14:11:15 -07001158 /* Consume port. Then clear IIR or we'll miss events */
1159 if ((I915_HAS_HOTPLUG(dev)) &&
1160 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
1161 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1162
Zhao Yakui44d98a62009-10-09 11:39:40 +08001163 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
Jesse Barnes5ca58282009-03-31 14:11:15 -07001164 hotplug_status);
1165 if (hotplug_status & dev_priv->hotplug_supported_mask)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001166 queue_work(dev_priv->wq,
1167 &dev_priv->hotplug_work);
Jesse Barnes5ca58282009-03-31 14:11:15 -07001168
1169 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
1170 I915_READ(PORT_HOTPLUG_STAT);
1171 }
1172
Eric Anholtcdfbc412008-11-04 15:50:30 -08001173 I915_WRITE(IIR, iir);
1174 new_iir = I915_READ(IIR); /* Flush posted writes */
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001175
Dave Airlie7c1c2872008-11-28 14:22:24 +10001176 if (dev->primary->master) {
1177 master_priv = dev->primary->master->driver_priv;
1178 if (master_priv->sarea_priv)
1179 master_priv->sarea_priv->last_dispatch =
1180 READ_BREADCRUMB(dev_priv);
1181 }
Keith Packard7c463582008-11-04 02:03:27 -08001182
Chris Wilson549f7362010-10-19 11:19:32 +01001183 if (iir & I915_USER_INTERRUPT)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001184 notify_ring(dev, &dev_priv->ring[RCS]);
1185 if (iir & I915_BSD_USER_INTERRUPT)
1186 notify_ring(dev, &dev_priv->ring[VCS]);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001187
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001188 if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001189 intel_prepare_page_flip(dev, 0);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001190 if (dev_priv->flip_pending_is_done)
1191 intel_finish_page_flip_plane(dev, 0);
1192 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001193
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001194 if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
Jesse Barnes70565d02010-07-01 04:45:43 -07001195 intel_prepare_page_flip(dev, 1);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001196 if (dev_priv->flip_pending_is_done)
1197 intel_finish_page_flip_plane(dev, 1);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001198 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001199
Keith Packard05eff842008-11-19 14:03:05 -08001200 if (pipea_stats & vblank_status) {
Eric Anholtcdfbc412008-11-04 15:50:30 -08001201 vblank++;
1202 drm_handle_vblank(dev, 0);
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001203 if (!dev_priv->flip_pending_is_done) {
1204 i915_pageflip_stall_check(dev, 0);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001205 intel_finish_page_flip(dev, 0);
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001206 }
Eric Anholtcdfbc412008-11-04 15:50:30 -08001207 }
Eric Anholt673a3942008-07-30 12:06:12 -07001208
Keith Packard05eff842008-11-19 14:03:05 -08001209 if (pipeb_stats & vblank_status) {
Eric Anholtcdfbc412008-11-04 15:50:30 -08001210 vblank++;
1211 drm_handle_vblank(dev, 1);
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001212 if (!dev_priv->flip_pending_is_done) {
1213 i915_pageflip_stall_check(dev, 1);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001214 intel_finish_page_flip(dev, 1);
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001215 }
Eric Anholtcdfbc412008-11-04 15:50:30 -08001216 }
Keith Packard7c463582008-11-04 02:03:27 -08001217
Jesse Barnesd874bcf2010-06-30 13:16:00 -07001218 if ((pipea_stats & PIPE_LEGACY_BLC_EVENT_STATUS) ||
1219 (pipeb_stats & PIPE_LEGACY_BLC_EVENT_STATUS) ||
Eric Anholtcdfbc412008-11-04 15:50:30 -08001220 (iir & I915_ASLE_INTERRUPT))
Chris Wilson3b617962010-08-24 09:02:58 +01001221 intel_opregion_asle_intr(dev);
Keith Packard7c463582008-11-04 02:03:27 -08001222
Eric Anholtcdfbc412008-11-04 15:50:30 -08001223 /* With MSI, interrupts are only generated when iir
1224 * transitions from zero to nonzero. If another bit got
1225 * set while we were handling the existing iir bits, then
1226 * we would never get another interrupt.
1227 *
1228 * This is fine on non-MSI as well, as if we hit this path
1229 * we avoid exiting the interrupt handler only to generate
1230 * another one.
1231 *
1232 * Note that for MSI this could cause a stray interrupt report
1233 * if an interrupt landed in the time between writing IIR and
1234 * the posting read. This should be rare enough to never
1235 * trigger the 99% of 100,000 interrupts test for disabling
1236 * stray interrupts.
1237 */
1238 iir = new_iir;
Keith Packard05eff842008-11-19 14:03:05 -08001239 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001240
Keith Packard05eff842008-11-19 14:03:05 -08001241 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Dave Airlieaf6061a2008-05-07 12:15:39 +10001244static int i915_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001247 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 i915_kernel_lost_context(dev);
1250
Zhao Yakui44d98a62009-10-09 11:39:40 +08001251 DRM_DEBUG_DRIVER("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Kristian Høgsbergc99b0582008-08-20 11:20:13 -04001253 dev_priv->counter++;
Alan Hourihanec29b6692006-08-12 16:29:24 +10001254 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -04001255 dev_priv->counter = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001256 if (master_priv->sarea_priv)
1257 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Alan Hourihanec29b6692006-08-12 16:29:24 +10001258
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001259 if (BEGIN_LP_RING(4) == 0) {
1260 OUT_RING(MI_STORE_DWORD_INDEX);
1261 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1262 OUT_RING(dev_priv->counter);
1263 OUT_RING(MI_USER_INTERRUPT);
1264 ADVANCE_LP_RING();
1265 }
Dave Airliebc5f4522007-11-05 12:50:58 +10001266
Alan Hourihanec29b6692006-08-12 16:29:24 +10001267 return dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001270void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
1271{
1272 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001273 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001274
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001275 if (dev_priv->trace_irq_seqno == 0 &&
1276 ring->irq_get(ring))
1277 dev_priv->trace_irq_seqno = seqno;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001278}
1279
Dave Airlie84b1fd12007-07-11 15:53:27 +10001280static int i915_wait_irq(struct drm_device * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001283 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int ret = 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001285 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Zhao Yakui44d98a62009-10-09 11:39:40 +08001287 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 READ_BREADCRUMB(dev_priv));
1289
Eric Anholted4cb412008-07-29 12:10:39 -07001290 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
Dave Airlie7c1c2872008-11-28 14:22:24 +10001291 if (master_priv->sarea_priv)
1292 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Dave Airlie7c1c2872008-11-28 14:22:24 +10001296 if (master_priv->sarea_priv)
1297 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001299 ret = -ENODEV;
1300 if (ring->irq_get(ring)) {
1301 DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1302 READ_BREADCRUMB(dev_priv) >= irq_nr);
1303 ring->irq_put(ring);
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Eric Anholt20caafa2007-08-25 19:22:43 +10001306 if (ret == -EBUSY) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001307 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1309 }
1310
Dave Airlieaf6061a2008-05-07 12:15:39 +10001311 return ret;
1312}
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314/* Needs the lock as it touches the ring.
1315 */
Eric Anholtc153f452007-09-03 12:06:45 +10001316int i915_irq_emit(struct drm_device *dev, void *data,
1317 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001320 drm_i915_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 int result;
1322
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001323 if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001324 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001325 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
Eric Anholt299eb932009-02-24 22:14:12 -08001327
1328 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1329
Eric Anholt546b0972008-09-01 16:45:29 -07001330 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 result = i915_emit_irq(dev);
Eric Anholt546b0972008-09-01 16:45:29 -07001332 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Eric Anholtc153f452007-09-03 12:06:45 +10001334 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001336 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
1339 return 0;
1340}
1341
1342/* Doesn't need the hardware lock.
1343 */
Eric Anholtc153f452007-09-03 12:06:45 +10001344int i915_irq_wait(struct drm_device *dev, void *data,
1345 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001348 drm_i915_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001351 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001352 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354
Eric Anholtc153f452007-09-03 12:06:45 +10001355 return i915_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Keith Packard42f52ef2008-10-18 19:39:29 -07001358/* Called from drm generic code, passed 'crtc' which
1359 * we use as a pipe index
1360 */
1361int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001362{
1363 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001364 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001365
Chris Wilson5eddb702010-09-11 13:48:45 +01001366 if (!i915_pipe_enabled(dev, pipe))
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001367 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001368
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001369 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Eric Anholtbad720f2009-10-22 16:11:14 -07001370 if (HAS_PCH_SPLIT(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001371 ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
Li Pengc062df62010-01-23 00:12:58 +08001372 DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001373 else if (INTEL_INFO(dev)->gen >= 4)
Keith Packard7c463582008-11-04 02:03:27 -08001374 i915_enable_pipestat(dev_priv, pipe,
1375 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -07001376 else
Keith Packard7c463582008-11-04 02:03:27 -08001377 i915_enable_pipestat(dev_priv, pipe,
1378 PIPE_VBLANK_INTERRUPT_ENABLE);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001379 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001380 return 0;
1381}
1382
Keith Packard42f52ef2008-10-18 19:39:29 -07001383/* Called from drm generic code, passed 'crtc' which
1384 * we use as a pipe index
1385 */
1386void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001387{
1388 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001389 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001390
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001391 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Eric Anholtbad720f2009-10-22 16:11:14 -07001392 if (HAS_PCH_SPLIT(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001393 ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
Li Pengc062df62010-01-23 00:12:58 +08001394 DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1395 else
1396 i915_disable_pipestat(dev_priv, pipe,
1397 PIPE_VBLANK_INTERRUPT_ENABLE |
1398 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001399 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001400}
1401
Jesse Barnes79e53942008-11-07 14:24:08 -08001402void i915_enable_interrupt (struct drm_device *dev)
1403{
1404 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wange170b032009-06-05 15:38:40 +08001405
Eric Anholtbad720f2009-10-22 16:11:14 -07001406 if (!HAS_PCH_SPLIT(dev))
Chris Wilson3b617962010-08-24 09:02:58 +01001407 intel_opregion_enable_asle(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001408 dev_priv->irq_enabled = 1;
1409}
1410
1411
Dave Airlie702880f2006-06-24 17:07:34 +10001412/* Set the vblank monitor pipe
1413 */
Eric Anholtc153f452007-09-03 12:06:45 +10001414int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1415 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +10001416{
Dave Airlie702880f2006-06-24 17:07:34 +10001417 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie702880f2006-06-24 17:07:34 +10001418
1419 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001420 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001421 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +10001422 }
1423
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +10001424 return 0;
Dave Airlie702880f2006-06-24 17:07:34 +10001425}
1426
Eric Anholtc153f452007-09-03 12:06:45 +10001427int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1428 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +10001429{
Dave Airlie702880f2006-06-24 17:07:34 +10001430 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001431 drm_i915_vblank_pipe_t *pipe = data;
Dave Airlie702880f2006-06-24 17:07:34 +10001432
1433 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001434 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001435 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +10001436 }
1437
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001438 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Eric Anholtc153f452007-09-03 12:06:45 +10001439
Dave Airlie702880f2006-06-24 17:07:34 +10001440 return 0;
1441}
1442
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +10001443/**
1444 * Schedule buffer swap at given vertical blank.
1445 */
Eric Anholtc153f452007-09-03 12:06:45 +10001446int i915_vblank_swap(struct drm_device *dev, void *data,
1447 struct drm_file *file_priv)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +10001448{
Eric Anholtbd95e0a2008-11-04 12:01:24 -08001449 /* The delayed swap mechanism was fundamentally racy, and has been
1450 * removed. The model was that the client requested a delayed flip/swap
1451 * from the kernel, then waited for vblank before continuing to perform
1452 * rendering. The problem was that the kernel might wake the client
1453 * up before it dispatched the vblank swap (since the lock has to be
1454 * held while touching the ringbuffer), in which case the client would
1455 * clear and start the next frame before the swap occurred, and
1456 * flicker would occur in addition to likely missing the vblank.
1457 *
1458 * In the absence of this ioctl, userland falls back to a correct path
1459 * of waiting for a vblank, then dispatching the swap on its own.
1460 * Context switching to userland and back is plenty fast enough for
1461 * meeting the requirements of vblank swapping.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001462 */
Eric Anholtbd95e0a2008-11-04 12:01:24 -08001463 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +10001464}
1465
Chris Wilson893eead2010-10-27 14:44:35 +01001466static u32
1467ring_last_seqno(struct intel_ring_buffer *ring)
Zou Nan hai852835f2010-05-21 09:08:56 +08001468{
Chris Wilson893eead2010-10-27 14:44:35 +01001469 return list_entry(ring->request_list.prev,
1470 struct drm_i915_gem_request, list)->seqno;
1471}
1472
1473static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1474{
1475 if (list_empty(&ring->request_list) ||
1476 i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1477 /* Issue a wake-up to catch stuck h/w. */
Chris Wilsonb2223492010-10-27 15:27:33 +01001478 if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
Chris Wilson893eead2010-10-27 14:44:35 +01001479 DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1480 ring->name,
Chris Wilsonb2223492010-10-27 15:27:33 +01001481 ring->waiting_seqno,
Chris Wilson893eead2010-10-27 14:44:35 +01001482 ring->get_seqno(ring));
1483 wake_up_all(&ring->irq_queue);
1484 *err = true;
1485 }
1486 return true;
1487 }
1488 return false;
Ben Gamarif65d9422009-09-14 17:48:44 -04001489}
1490
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001491static bool kick_ring(struct intel_ring_buffer *ring)
1492{
1493 struct drm_device *dev = ring->dev;
1494 struct drm_i915_private *dev_priv = dev->dev_private;
1495 u32 tmp = I915_READ_CTL(ring);
1496 if (tmp & RING_WAIT) {
1497 DRM_ERROR("Kicking stuck wait on %s\n",
1498 ring->name);
1499 I915_WRITE_CTL(ring, tmp);
1500 return true;
1501 }
1502 if (IS_GEN6(dev) &&
1503 (tmp & RING_WAIT_SEMAPHORE)) {
1504 DRM_ERROR("Kicking stuck semaphore on %s\n",
1505 ring->name);
1506 I915_WRITE_CTL(ring, tmp);
1507 return true;
1508 }
1509 return false;
1510}
1511
Ben Gamarif65d9422009-09-14 17:48:44 -04001512/**
1513 * This is called when the chip hasn't reported back with completed
1514 * batchbuffers in a long time. The first time this is called we simply record
1515 * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1516 * again, we assume the chip is wedged and try to fix it.
1517 */
1518void i915_hangcheck_elapsed(unsigned long data)
1519{
1520 struct drm_device *dev = (struct drm_device *)data;
1521 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001522 uint32_t acthd, instdone, instdone1;
Chris Wilson893eead2010-10-27 14:44:35 +01001523 bool err = false;
1524
1525 /* If all work is done then ACTHD clearly hasn't advanced. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001526 if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
1527 i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
1528 i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
Chris Wilson893eead2010-10-27 14:44:35 +01001529 dev_priv->hangcheck_count = 0;
1530 if (err)
1531 goto repeat;
1532 return;
1533 }
Eric Anholtb9201c12010-01-08 14:25:16 -08001534
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001535 if (INTEL_INFO(dev)->gen < 4) {
Ben Gamarif65d9422009-09-14 17:48:44 -04001536 acthd = I915_READ(ACTHD);
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001537 instdone = I915_READ(INSTDONE);
1538 instdone1 = 0;
1539 } else {
Ben Gamarif65d9422009-09-14 17:48:44 -04001540 acthd = I915_READ(ACTHD_I965);
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001541 instdone = I915_READ(INSTDONE_I965);
1542 instdone1 = I915_READ(INSTDONE1);
1543 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001544
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001545 if (dev_priv->last_acthd == acthd &&
1546 dev_priv->last_instdone == instdone &&
1547 dev_priv->last_instdone1 == instdone1) {
1548 if (dev_priv->hangcheck_count++ > 1) {
1549 DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
Chris Wilson8c80b592010-08-08 20:38:12 +01001550
1551 if (!IS_GEN2(dev)) {
1552 /* Is the chip hanging on a WAIT_FOR_EVENT?
1553 * If so we can simply poke the RB_WAIT bit
1554 * and break the hang. This should work on
1555 * all but the second generation chipsets.
1556 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001557
1558 if (kick_ring(&dev_priv->ring[RCS]))
Chris Wilson893eead2010-10-27 14:44:35 +01001559 goto repeat;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001560
1561 if (HAS_BSD(dev) &&
1562 kick_ring(&dev_priv->ring[VCS]))
1563 goto repeat;
1564
1565 if (HAS_BLT(dev) &&
1566 kick_ring(&dev_priv->ring[BCS]))
1567 goto repeat;
Chris Wilson8c80b592010-08-08 20:38:12 +01001568 }
1569
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001570 i915_handle_error(dev, true);
1571 return;
1572 }
1573 } else {
1574 dev_priv->hangcheck_count = 0;
1575
1576 dev_priv->last_acthd = acthd;
1577 dev_priv->last_instdone = instdone;
1578 dev_priv->last_instdone1 = instdone1;
1579 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001580
Chris Wilson893eead2010-10-27 14:44:35 +01001581repeat:
Ben Gamarif65d9422009-09-14 17:48:44 -04001582 /* Reset timer case chip hangs without another request being added */
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001583 mod_timer(&dev_priv->hangcheck_timer,
1584 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001585}
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587/* drm_dma.h hooks
1588*/
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001589static void ironlake_irq_preinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001590{
1591 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1592
1593 I915_WRITE(HWSTAM, 0xeffe);
1594
1595 /* XXX hotplug from PCH */
1596
1597 I915_WRITE(DEIMR, 0xffffffff);
1598 I915_WRITE(DEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001599 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001600
1601 /* and GT */
1602 I915_WRITE(GTIMR, 0xffffffff);
1603 I915_WRITE(GTIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001604 POSTING_READ(GTIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00001605
1606 /* south display irq */
1607 I915_WRITE(SDEIMR, 0xffffffff);
1608 I915_WRITE(SDEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001609 POSTING_READ(SDEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001610}
1611
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001612static int ironlake_irq_postinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001613{
1614 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1615 /* enable kind of interrupts always enabled */
Jesse Barnes013d5aa2010-01-29 11:18:31 -08001616 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1617 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001618 u32 render_irqs;
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01001619 u32 hotplug_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001620
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001621 dev_priv->irq_mask = ~display_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001622
1623 /* should always can generate irq */
1624 I915_WRITE(DEIIR, I915_READ(DEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001625 I915_WRITE(DEIMR, dev_priv->irq_mask);
1626 I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001627 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001628
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001629 dev_priv->gt_irq_mask = ~0;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001630
1631 I915_WRITE(GTIIR, I915_READ(GTIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001632 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001633 if (IS_GEN6(dev)) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001634 I915_WRITE(GEN6_RENDER_IMR, ~GEN6_RENDER_USER_INTERRUPT);
1635 I915_WRITE(GEN6_BSD_IMR, ~GEN6_BSD_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001636 I915_WRITE(GEN6_BLITTER_IMR, ~GEN6_BLITTER_USER_INTERRUPT);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001637 }
1638
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001639 if (IS_GEN6(dev))
1640 render_irqs =
1641 GT_USER_INTERRUPT |
1642 GT_GEN6_BSD_USER_INTERRUPT |
1643 GT_BLT_USER_INTERRUPT;
1644 else
1645 render_irqs =
Chris Wilson88f23b82010-12-05 15:08:31 +00001646 GT_USER_INTERRUPT |
Chris Wilsonc6df5412010-12-15 09:56:50 +00001647 GT_PIPE_NOTIFY |
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001648 GT_BSD_USER_INTERRUPT;
1649 I915_WRITE(GTIER, render_irqs);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001650 POSTING_READ(GTIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001651
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01001652 if (HAS_PCH_CPT(dev)) {
1653 hotplug_mask = SDE_CRT_HOTPLUG_CPT | SDE_PORTB_HOTPLUG_CPT |
1654 SDE_PORTC_HOTPLUG_CPT | SDE_PORTD_HOTPLUG_CPT ;
1655 } else {
1656 hotplug_mask = SDE_CRT_HOTPLUG | SDE_PORTB_HOTPLUG |
1657 SDE_PORTC_HOTPLUG | SDE_PORTD_HOTPLUG;
1658 }
1659
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001660 dev_priv->pch_irq_mask = ~hotplug_mask;
Zhenyu Wangc6501562009-11-03 18:57:21 +00001661
1662 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001663 I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1664 I915_WRITE(SDEIER, hotplug_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001665 POSTING_READ(SDEIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00001666
Jesse Barnesf97108d2010-01-29 11:27:07 -08001667 if (IS_IRONLAKE_M(dev)) {
1668 /* Clear & enable PCU event interrupts */
1669 I915_WRITE(DEIIR, DE_PCU_EVENT);
1670 I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1671 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1672 }
1673
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001674 return 0;
1675}
1676
Dave Airlie84b1fd12007-07-11 15:53:27 +10001677void i915_driver_irq_preinstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1680
Jesse Barnes79e53942008-11-07 14:24:08 -08001681 atomic_set(&dev_priv->irq_received, 0);
1682
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001683 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Jesse Barnes8a905232009-07-11 16:48:03 -04001684 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001685
Eric Anholtbad720f2009-10-22 16:11:14 -07001686 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001687 ironlake_irq_preinstall(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001688 return;
1689 }
1690
Jesse Barnes5ca58282009-03-31 14:11:15 -07001691 if (I915_HAS_HOTPLUG(dev)) {
1692 I915_WRITE(PORT_HOTPLUG_EN, 0);
1693 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1694 }
1695
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001696 I915_WRITE(HWSTAM, 0xeffe);
Keith Packard7c463582008-11-04 02:03:27 -08001697 I915_WRITE(PIPEASTAT, 0);
1698 I915_WRITE(PIPEBSTAT, 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001699 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07001700 I915_WRITE(IER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001701 POSTING_READ(IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001704/*
1705 * Must be called after intel_modeset_init or hotplug interrupts won't be
1706 * enabled correctly.
1707 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001708int i915_driver_irq_postinstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes5ca58282009-03-31 14:11:15 -07001711 u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001712 u32 error_mask;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001713
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001714 DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001715 if (HAS_BSD(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001716 DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
Chris Wilson549f7362010-10-19 11:19:32 +01001717 if (HAS_BLT(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001718 DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001719
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001720 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001721
Eric Anholtbad720f2009-10-22 16:11:14 -07001722 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001723 return ironlake_irq_postinstall(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001724
Keith Packard7c463582008-11-04 02:03:27 -08001725 /* Unmask the interrupts that we always want on. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001726 dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001727
Keith Packard7c463582008-11-04 02:03:27 -08001728 dev_priv->pipestat[0] = 0;
1729 dev_priv->pipestat[1] = 0;
1730
Jesse Barnes5ca58282009-03-31 14:11:15 -07001731 if (I915_HAS_HOTPLUG(dev)) {
Adam Jacksonc496fa12010-05-27 17:26:45 -04001732 /* Enable in IER... */
1733 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1734 /* and unmask in IMR */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001735 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
Adam Jacksonc496fa12010-05-27 17:26:45 -04001736 }
1737
1738 /*
1739 * Enable some error detection, note the instruction error mask
1740 * bit is reserved, so we leave it masked.
1741 */
1742 if (IS_G4X(dev)) {
1743 error_mask = ~(GM45_ERROR_PAGE_TABLE |
1744 GM45_ERROR_MEM_PRIV |
1745 GM45_ERROR_CP_PRIV |
1746 I915_ERROR_MEMORY_REFRESH);
1747 } else {
1748 error_mask = ~(I915_ERROR_PAGE_TABLE |
1749 I915_ERROR_MEMORY_REFRESH);
1750 }
1751 I915_WRITE(EMR, error_mask);
1752
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001753 I915_WRITE(IMR, dev_priv->irq_mask);
Adam Jacksonc496fa12010-05-27 17:26:45 -04001754 I915_WRITE(IER, enable_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001755 POSTING_READ(IER);
Adam Jacksonc496fa12010-05-27 17:26:45 -04001756
1757 if (I915_HAS_HOTPLUG(dev)) {
Jesse Barnes5ca58282009-03-31 14:11:15 -07001758 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
1759
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001760 /* Note HDMI and DP share bits */
1761 if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1762 hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1763 if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1764 hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1765 if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1766 hotplug_en |= HDMID_HOTPLUG_INT_EN;
1767 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1768 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1769 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1770 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
Andy Lutomirski2d1c9752010-06-12 05:21:18 -04001771 if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001772 hotplug_en |= CRT_HOTPLUG_INT_EN;
Andy Lutomirski2d1c9752010-06-12 05:21:18 -04001773
1774 /* Programming the CRT detection parameters tends
1775 to generate a spurious hotplug event about three
1776 seconds later. So just do it once.
1777 */
1778 if (IS_G4X(dev))
1779 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
1780 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
1781 }
1782
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001783 /* Ignore TV since it's buggy */
1784
Jesse Barnes5ca58282009-03-31 14:11:15 -07001785 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Jesse Barnes5ca58282009-03-31 14:11:15 -07001786 }
1787
Chris Wilson3b617962010-08-24 09:02:58 +01001788 intel_opregion_enable_asle(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001789
1790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001793static void ironlake_irq_uninstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001794{
1795 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1796 I915_WRITE(HWSTAM, 0xffffffff);
1797
1798 I915_WRITE(DEIMR, 0xffffffff);
1799 I915_WRITE(DEIER, 0x0);
1800 I915_WRITE(DEIIR, I915_READ(DEIIR));
1801
1802 I915_WRITE(GTIMR, 0xffffffff);
1803 I915_WRITE(GTIER, 0x0);
1804 I915_WRITE(GTIIR, I915_READ(GTIIR));
1805}
1806
Dave Airlie84b1fd12007-07-11 15:53:27 +10001807void i915_driver_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie91e37382006-02-18 15:17:04 +11001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (!dev_priv)
1812 return;
1813
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001814 dev_priv->vblank_pipe = 0;
1815
Eric Anholtbad720f2009-10-22 16:11:14 -07001816 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001817 ironlake_irq_uninstall(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001818 return;
1819 }
1820
Jesse Barnes5ca58282009-03-31 14:11:15 -07001821 if (I915_HAS_HOTPLUG(dev)) {
1822 I915_WRITE(PORT_HOTPLUG_EN, 0);
1823 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1824 }
1825
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001826 I915_WRITE(HWSTAM, 0xffffffff);
Keith Packard7c463582008-11-04 02:03:27 -08001827 I915_WRITE(PIPEASTAT, 0);
1828 I915_WRITE(PIPEBSTAT, 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001829 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07001830 I915_WRITE(IER, 0x0);
Dave Airlie91e37382006-02-18 15:17:04 +11001831
Keith Packard7c463582008-11-04 02:03:27 -08001832 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
1833 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
1834 I915_WRITE(IIR, I915_READ(IIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}