blob: 9b1d669f7d4b72e0464e10337af8774664880ae8 [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
Zhenyu Wang036a4a72009-06-08 14:40:19 +080067/* For display hotplug interrupt */
Chris Wilson995b6762010-08-20 13:23:26 +010068static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050069ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080070{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000071 if ((dev_priv->irq_mask & mask) != 0) {
72 dev_priv->irq_mask &= ~mask;
73 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000074 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080075 }
76}
77
78static inline void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050079ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080080{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000081 if ((dev_priv->irq_mask & mask) != mask) {
82 dev_priv->irq_mask |= mask;
83 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000084 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080085 }
86}
87
Keith Packard7c463582008-11-04 02:03:27 -080088void
89i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
90{
91 if ((dev_priv->pipestat[pipe] & mask) != mask) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -080092 u32 reg = PIPESTAT(pipe);
Keith Packard7c463582008-11-04 02:03:27 -080093
94 dev_priv->pipestat[pipe] |= mask;
95 /* Enable the interrupt, clear any pending status */
96 I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
Chris Wilson3143a2b2010-11-16 15:55:10 +000097 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -080098 }
99}
100
101void
102i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
103{
104 if ((dev_priv->pipestat[pipe] & mask) != 0) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800105 u32 reg = PIPESTAT(pipe);
Keith Packard7c463582008-11-04 02:03:27 -0800106
107 dev_priv->pipestat[pipe] &= ~mask;
108 I915_WRITE(reg, dev_priv->pipestat[pipe]);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000109 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800110 }
111}
112
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000113/**
Zhao Yakui01c66882009-10-28 05:10:00 +0000114 * intel_enable_asle - enable ASLE interrupt for OpRegion
115 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000116void intel_enable_asle(struct drm_device *dev)
Zhao Yakui01c66882009-10-28 05:10:00 +0000117{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000118 drm_i915_private_t *dev_priv = dev->dev_private;
119 unsigned long irqflags;
120
121 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000122
Eric Anholtc619eed2010-01-28 16:45:52 -0800123 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500124 ironlake_enable_display_irq(dev_priv, DE_GSE);
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800125 else {
Zhao Yakui01c66882009-10-28 05:10:00 +0000126 i915_enable_pipestat(dev_priv, 1,
Jesse Barnesd874bcf2010-06-30 13:16:00 -0700127 PIPE_LEGACY_BLC_EVENT_ENABLE);
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100128 if (INTEL_INFO(dev)->gen >= 4)
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800129 i915_enable_pipestat(dev_priv, 0,
Jesse Barnesd874bcf2010-06-30 13:16:00 -0700130 PIPE_LEGACY_BLC_EVENT_ENABLE);
Zhao Yakuiedcb49c2010-04-07 17:11:21 +0800131 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000132
133 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000134}
135
136/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700137 * i915_pipe_enabled - check if a pipe is enabled
138 * @dev: DRM device
139 * @pipe: pipe to check
140 *
141 * Reading certain registers when the pipe is disabled can hang the chip.
142 * Use this routine to make sure the PLL is running and the pipe is active
143 * before reading such registers if unsure.
144 */
145static int
146i915_pipe_enabled(struct drm_device *dev, int pipe)
147{
148 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson5eddb702010-09-11 13:48:45 +0100149 return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700150}
151
Keith Packard42f52ef2008-10-18 19:39:29 -0700152/* Called from drm generic code, passed a 'crtc', which
153 * we use as a pipe index
154 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700155static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700156{
157 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
158 unsigned long high_frame;
159 unsigned long low_frame;
Chris Wilson5eddb702010-09-11 13:48:45 +0100160 u32 high1, high2, low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700161
162 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800163 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800164 "pipe %c\n", pipe_name(pipe));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700165 return 0;
166 }
167
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800168 high_frame = PIPEFRAME(pipe);
169 low_frame = PIPEFRAMEPIXEL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +0100170
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700171 /*
172 * High & low register fields aren't synchronized, so make sure
173 * we get a low value that's stable across two reads of the high
174 * register.
175 */
176 do {
Chris Wilson5eddb702010-09-11 13:48:45 +0100177 high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
178 low = I915_READ(low_frame) & PIPE_FRAME_LOW_MASK;
179 high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700180 } while (high1 != high2);
181
Chris Wilson5eddb702010-09-11 13:48:45 +0100182 high1 >>= PIPE_FRAME_HIGH_SHIFT;
183 low >>= PIPE_FRAME_LOW_SHIFT;
184 return (high1 << 8) | low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700185}
186
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700187static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800188{
189 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800190 int reg = PIPE_FRMCOUNT_GM45(pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800191
192 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800193 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800194 "pipe %c\n", pipe_name(pipe));
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800195 return 0;
196 }
197
198 return I915_READ(reg);
199}
200
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700201static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100202 int *vpos, int *hpos)
203{
204 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
205 u32 vbl = 0, position = 0;
206 int vbl_start, vbl_end, htotal, vtotal;
207 bool in_vbl = true;
208 int ret = 0;
209
210 if (!i915_pipe_enabled(dev, pipe)) {
211 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800212 "pipe %c\n", pipe_name(pipe));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100213 return 0;
214 }
215
216 /* Get vtotal. */
217 vtotal = 1 + ((I915_READ(VTOTAL(pipe)) >> 16) & 0x1fff);
218
219 if (INTEL_INFO(dev)->gen >= 4) {
220 /* No obvious pixelcount register. Only query vertical
221 * scanout position from Display scan line register.
222 */
223 position = I915_READ(PIPEDSL(pipe));
224
225 /* Decode into vertical scanout position. Don't have
226 * horizontal scanout position.
227 */
228 *vpos = position & 0x1fff;
229 *hpos = 0;
230 } else {
231 /* Have access to pixelcount since start of frame.
232 * We can split this into vertical and horizontal
233 * scanout position.
234 */
235 position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
236
237 htotal = 1 + ((I915_READ(HTOTAL(pipe)) >> 16) & 0x1fff);
238 *vpos = position / htotal;
239 *hpos = position - (*vpos * htotal);
240 }
241
242 /* Query vblank area. */
243 vbl = I915_READ(VBLANK(pipe));
244
245 /* Test position against vblank region. */
246 vbl_start = vbl & 0x1fff;
247 vbl_end = (vbl >> 16) & 0x1fff;
248
249 if ((*vpos < vbl_start) || (*vpos > vbl_end))
250 in_vbl = false;
251
252 /* Inside "upper part" of vblank area? Apply corrective offset: */
253 if (in_vbl && (*vpos >= vbl_start))
254 *vpos = *vpos - vtotal;
255
256 /* Readouts valid? */
257 if (vbl > 0)
258 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
259
260 /* In vblank? */
261 if (in_vbl)
262 ret |= DRM_SCANOUTPOS_INVBL;
263
264 return ret;
265}
266
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700267static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100268 int *max_error,
269 struct timeval *vblank_time,
270 unsigned flags)
271{
Chris Wilson4041b852011-01-22 10:07:56 +0000272 struct drm_i915_private *dev_priv = dev->dev_private;
273 struct drm_crtc *crtc;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100274
Chris Wilson4041b852011-01-22 10:07:56 +0000275 if (pipe < 0 || pipe >= dev_priv->num_pipe) {
276 DRM_ERROR("Invalid crtc %d\n", pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100277 return -EINVAL;
278 }
279
280 /* Get drm_crtc to timestamp: */
Chris Wilson4041b852011-01-22 10:07:56 +0000281 crtc = intel_get_crtc_for_pipe(dev, pipe);
282 if (crtc == NULL) {
283 DRM_ERROR("Invalid crtc %d\n", pipe);
284 return -EINVAL;
285 }
286
287 if (!crtc->enabled) {
288 DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
289 return -EBUSY;
290 }
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100291
292 /* Helper routine in DRM core does all the work: */
Chris Wilson4041b852011-01-22 10:07:56 +0000293 return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
294 vblank_time, flags,
295 crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100296}
297
Jesse Barnes5ca58282009-03-31 14:11:15 -0700298/*
299 * Handle hotplug events outside the interrupt handler proper.
300 */
301static void i915_hotplug_work_func(struct work_struct *work)
302{
303 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
304 hotplug_work);
305 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700306 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100307 struct intel_encoder *encoder;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700308
Keith Packarda65e34c2011-07-25 10:04:56 -0700309 mutex_lock(&mode_config->mutex);
Jesse Barnese67189ab2011-02-11 14:44:51 -0800310 DRM_DEBUG_KMS("running encoder hotplug functions\n");
311
Chris Wilson4ef69c72010-09-09 15:14:28 +0100312 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
313 if (encoder->hot_plug)
314 encoder->hot_plug(encoder);
315
Keith Packard40ee3382011-07-28 15:31:19 -0700316 mutex_unlock(&mode_config->mutex);
317
Jesse Barnes5ca58282009-03-31 14:11:15 -0700318 /* Just fire off a uevent and let userspace tell us what to do */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000319 drm_helper_hpd_irq_event(dev);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700320}
321
Jesse Barnesf97108d2010-01-29 11:27:07 -0800322static void i915_handle_rps_change(struct drm_device *dev)
323{
324 drm_i915_private_t *dev_priv = dev->dev_private;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000325 u32 busy_up, busy_down, max_avg, min_avg;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800326 u8 new_delay = dev_priv->cur_delay;
327
Jesse Barnes7648fa92010-05-20 14:28:11 -0700328 I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000329 busy_up = I915_READ(RCPREVBSYTUPAVG);
330 busy_down = I915_READ(RCPREVBSYTDNAVG);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800331 max_avg = I915_READ(RCBMAXAVG);
332 min_avg = I915_READ(RCBMINAVG);
333
334 /* Handle RCS change request from hw */
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000335 if (busy_up > max_avg) {
Jesse Barnesf97108d2010-01-29 11:27:07 -0800336 if (dev_priv->cur_delay != dev_priv->max_delay)
337 new_delay = dev_priv->cur_delay - 1;
338 if (new_delay < dev_priv->max_delay)
339 new_delay = dev_priv->max_delay;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000340 } else if (busy_down < min_avg) {
Jesse Barnesf97108d2010-01-29 11:27:07 -0800341 if (dev_priv->cur_delay != dev_priv->min_delay)
342 new_delay = dev_priv->cur_delay + 1;
343 if (new_delay > dev_priv->min_delay)
344 new_delay = dev_priv->min_delay;
345 }
346
Jesse Barnes7648fa92010-05-20 14:28:11 -0700347 if (ironlake_set_drps(dev, new_delay))
348 dev_priv->cur_delay = new_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800349
350 return;
351}
352
Chris Wilson549f7362010-10-19 11:19:32 +0100353static void notify_ring(struct drm_device *dev,
354 struct intel_ring_buffer *ring)
355{
356 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson475553d2011-01-20 09:52:56 +0000357 u32 seqno;
Chris Wilson9862e602011-01-04 22:22:17 +0000358
Chris Wilson475553d2011-01-20 09:52:56 +0000359 if (ring->obj == NULL)
360 return;
361
362 seqno = ring->get_seqno(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +0000363 trace_i915_gem_request_complete(ring, seqno);
Chris Wilson9862e602011-01-04 22:22:17 +0000364
365 ring->irq_seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +0100366 wake_up_all(&ring->irq_queue);
Chris Wilson9862e602011-01-04 22:22:17 +0000367
Chris Wilson549f7362010-10-19 11:19:32 +0100368 dev_priv->hangcheck_count = 0;
369 mod_timer(&dev_priv->hangcheck_timer,
370 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
371}
372
Ben Widawsky4912d042011-04-25 11:25:20 -0700373static void gen6_pm_rps_work(struct work_struct *work)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800374{
Ben Widawsky4912d042011-04-25 11:25:20 -0700375 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
376 rps_work);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800377 u8 new_delay = dev_priv->cur_delay;
Ben Widawsky4912d042011-04-25 11:25:20 -0700378 u32 pm_iir, pm_imr;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800379
Ben Widawsky4912d042011-04-25 11:25:20 -0700380 spin_lock_irq(&dev_priv->rps_lock);
381 pm_iir = dev_priv->pm_iir;
382 dev_priv->pm_iir = 0;
383 pm_imr = I915_READ(GEN6_PMIMR);
384 spin_unlock_irq(&dev_priv->rps_lock);
385
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800386 if (!pm_iir)
387 return;
388
Ben Widawsky4912d042011-04-25 11:25:20 -0700389 mutex_lock(&dev_priv->dev->struct_mutex);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800390 if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
391 if (dev_priv->cur_delay != dev_priv->max_delay)
392 new_delay = dev_priv->cur_delay + 1;
393 if (new_delay > dev_priv->max_delay)
394 new_delay = dev_priv->max_delay;
395 } else if (pm_iir & (GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT)) {
Ben Widawsky4912d042011-04-25 11:25:20 -0700396 gen6_gt_force_wake_get(dev_priv);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800397 if (dev_priv->cur_delay != dev_priv->min_delay)
398 new_delay = dev_priv->cur_delay - 1;
399 if (new_delay < dev_priv->min_delay) {
400 new_delay = dev_priv->min_delay;
401 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
402 I915_READ(GEN6_RP_INTERRUPT_LIMITS) |
403 ((new_delay << 16) & 0x3f0000));
404 } else {
405 /* Make sure we continue to get down interrupts
406 * until we hit the minimum frequency */
407 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
408 I915_READ(GEN6_RP_INTERRUPT_LIMITS) & ~0x3f0000);
409 }
Ben Widawsky4912d042011-04-25 11:25:20 -0700410 gen6_gt_force_wake_put(dev_priv);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800411 }
412
Ben Widawsky4912d042011-04-25 11:25:20 -0700413 gen6_set_rps(dev_priv->dev, new_delay);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800414 dev_priv->cur_delay = new_delay;
415
Ben Widawsky4912d042011-04-25 11:25:20 -0700416 /*
417 * rps_lock not held here because clearing is non-destructive. There is
418 * an *extremely* unlikely race with gen6_rps_enable() that is prevented
419 * by holding struct_mutex for the duration of the write.
420 */
421 I915_WRITE(GEN6_PMIMR, pm_imr & ~pm_iir);
422 mutex_unlock(&dev_priv->dev->struct_mutex);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800423}
424
Jesse Barnes776ad802011-01-04 15:09:39 -0800425static void pch_irq_handler(struct drm_device *dev)
426{
427 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
428 u32 pch_iir;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800429 int pipe;
Jesse Barnes776ad802011-01-04 15:09:39 -0800430
431 pch_iir = I915_READ(SDEIIR);
432
433 if (pch_iir & SDE_AUDIO_POWER_MASK)
434 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
435 (pch_iir & SDE_AUDIO_POWER_MASK) >>
436 SDE_AUDIO_POWER_SHIFT);
437
438 if (pch_iir & SDE_GMBUS)
439 DRM_DEBUG_DRIVER("PCH GMBUS interrupt\n");
440
441 if (pch_iir & SDE_AUDIO_HDCP_MASK)
442 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
443
444 if (pch_iir & SDE_AUDIO_TRANS_MASK)
445 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
446
447 if (pch_iir & SDE_POISON)
448 DRM_ERROR("PCH poison interrupt\n");
449
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800450 if (pch_iir & SDE_FDI_MASK)
451 for_each_pipe(pipe)
452 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
453 pipe_name(pipe),
454 I915_READ(FDI_RX_IIR(pipe)));
Jesse Barnes776ad802011-01-04 15:09:39 -0800455
456 if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
457 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
458
459 if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
460 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
461
462 if (pch_iir & SDE_TRANSB_FIFO_UNDER)
463 DRM_DEBUG_DRIVER("PCH transcoder B underrun interrupt\n");
464 if (pch_iir & SDE_TRANSA_FIFO_UNDER)
465 DRM_DEBUG_DRIVER("PCH transcoder A underrun interrupt\n");
466}
467
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700468static irqreturn_t ivybridge_irq_handler(DRM_IRQ_ARGS)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700469{
470 struct drm_device *dev = (struct drm_device *) arg;
471 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
472 int ret = IRQ_NONE;
473 u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
474 struct drm_i915_master_private *master_priv;
475
476 atomic_inc(&dev_priv->irq_received);
477
478 /* disable master interrupt before clearing iir */
479 de_ier = I915_READ(DEIER);
480 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
481 POSTING_READ(DEIER);
482
483 de_iir = I915_READ(DEIIR);
484 gt_iir = I915_READ(GTIIR);
485 pch_iir = I915_READ(SDEIIR);
486 pm_iir = I915_READ(GEN6_PMIIR);
487
488 if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 && pm_iir == 0)
489 goto done;
490
491 ret = IRQ_HANDLED;
492
493 if (dev->primary->master) {
494 master_priv = dev->primary->master->driver_priv;
495 if (master_priv->sarea_priv)
496 master_priv->sarea_priv->last_dispatch =
497 READ_BREADCRUMB(dev_priv);
498 }
499
500 if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
501 notify_ring(dev, &dev_priv->ring[RCS]);
502 if (gt_iir & GT_GEN6_BSD_USER_INTERRUPT)
503 notify_ring(dev, &dev_priv->ring[VCS]);
504 if (gt_iir & GT_BLT_USER_INTERRUPT)
505 notify_ring(dev, &dev_priv->ring[BCS]);
506
507 if (de_iir & DE_GSE_IVB)
508 intel_opregion_gse_intr(dev);
509
510 if (de_iir & DE_PLANEA_FLIP_DONE_IVB) {
511 intel_prepare_page_flip(dev, 0);
512 intel_finish_page_flip_plane(dev, 0);
513 }
514
515 if (de_iir & DE_PLANEB_FLIP_DONE_IVB) {
516 intel_prepare_page_flip(dev, 1);
517 intel_finish_page_flip_plane(dev, 1);
518 }
519
520 if (de_iir & DE_PIPEA_VBLANK_IVB)
521 drm_handle_vblank(dev, 0);
522
Dan Carpenterf6b07f42011-05-25 12:56:56 +0300523 if (de_iir & DE_PIPEB_VBLANK_IVB)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -0700524 drm_handle_vblank(dev, 1);
525
526 /* check event from PCH */
527 if (de_iir & DE_PCH_EVENT_IVB) {
528 if (pch_iir & SDE_HOTPLUG_MASK_CPT)
529 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
530 pch_irq_handler(dev);
531 }
532
533 if (pm_iir & GEN6_PM_DEFERRED_EVENTS) {
534 unsigned long flags;
535 spin_lock_irqsave(&dev_priv->rps_lock, flags);
536 WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
537 I915_WRITE(GEN6_PMIMR, pm_iir);
538 dev_priv->pm_iir |= pm_iir;
539 spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
540 queue_work(dev_priv->wq, &dev_priv->rps_work);
541 }
542
543 /* should clear PCH hotplug event before clear CPU irq */
544 I915_WRITE(SDEIIR, pch_iir);
545 I915_WRITE(GTIIR, gt_iir);
546 I915_WRITE(DEIIR, de_iir);
547 I915_WRITE(GEN6_PMIIR, pm_iir);
548
549done:
550 I915_WRITE(DEIER, de_ier);
551 POSTING_READ(DEIER);
552
553 return ret;
554}
555
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700556static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800557{
Jesse Barnes46979952011-04-07 13:53:55 -0700558 struct drm_device *dev = (struct drm_device *) arg;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800559 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
560 int ret = IRQ_NONE;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800561 u32 de_iir, gt_iir, de_ier, pch_iir, pm_iir;
Yuanhan Liu2d7b8362010-10-08 10:21:06 +0100562 u32 hotplug_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800563 struct drm_i915_master_private *master_priv;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100564 u32 bsd_usr_interrupt = GT_BSD_USER_INTERRUPT;
565
Jesse Barnes46979952011-04-07 13:53:55 -0700566 atomic_inc(&dev_priv->irq_received);
567
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100568 if (IS_GEN6(dev))
569 bsd_usr_interrupt = GT_GEN6_BSD_USER_INTERRUPT;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800570
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000571 /* disable master interrupt before clearing iir */
572 de_ier = I915_READ(DEIER);
573 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000574 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000575
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800576 de_iir = I915_READ(DEIIR);
577 gt_iir = I915_READ(GTIIR);
Zhenyu Wangc6501562009-11-03 18:57:21 +0000578 pch_iir = I915_READ(SDEIIR);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800579 pm_iir = I915_READ(GEN6_PMIIR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800580
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800581 if (de_iir == 0 && gt_iir == 0 && pch_iir == 0 &&
582 (!IS_GEN6(dev) || pm_iir == 0))
Zou Nan haic7c85102010-01-15 10:29:06 +0800583 goto done;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800584
Yuanhan Liu2d7b8362010-10-08 10:21:06 +0100585 if (HAS_PCH_CPT(dev))
586 hotplug_mask = SDE_HOTPLUG_MASK_CPT;
587 else
588 hotplug_mask = SDE_HOTPLUG_MASK;
589
Zou Nan haic7c85102010-01-15 10:29:06 +0800590 ret = IRQ_HANDLED;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800591
Zou Nan haic7c85102010-01-15 10:29:06 +0800592 if (dev->primary->master) {
593 master_priv = dev->primary->master->driver_priv;
594 if (master_priv->sarea_priv)
595 master_priv->sarea_priv->last_dispatch =
596 READ_BREADCRUMB(dev_priv);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800597 }
598
Chris Wilsonc6df5412010-12-15 09:56:50 +0000599 if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000600 notify_ring(dev, &dev_priv->ring[RCS]);
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100601 if (gt_iir & bsd_usr_interrupt)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000602 notify_ring(dev, &dev_priv->ring[VCS]);
603 if (gt_iir & GT_BLT_USER_INTERRUPT)
604 notify_ring(dev, &dev_priv->ring[BCS]);
Zou Nan haic7c85102010-01-15 10:29:06 +0800605
606 if (de_iir & DE_GSE)
Chris Wilson3b617962010-08-24 09:02:58 +0100607 intel_opregion_gse_intr(dev);
Zou Nan haic7c85102010-01-15 10:29:06 +0800608
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800609 if (de_iir & DE_PLANEA_FLIP_DONE) {
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800610 intel_prepare_page_flip(dev, 0);
Chris Wilson2bbda382010-09-02 17:59:39 +0100611 intel_finish_page_flip_plane(dev, 0);
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800612 }
613
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800614 if (de_iir & DE_PLANEB_FLIP_DONE) {
615 intel_prepare_page_flip(dev, 1);
Chris Wilson2bbda382010-09-02 17:59:39 +0100616 intel_finish_page_flip_plane(dev, 1);
Jesse Barnes013d5aa2010-01-29 11:18:31 -0800617 }
Li Pengc062df62010-01-23 00:12:58 +0800618
Zhenyu Wangf072d2e2010-02-09 09:46:19 +0800619 if (de_iir & DE_PIPEA_VBLANK)
620 drm_handle_vblank(dev, 0);
621
622 if (de_iir & DE_PIPEB_VBLANK)
623 drm_handle_vblank(dev, 1);
624
Zou Nan haic7c85102010-01-15 10:29:06 +0800625 /* check event from PCH */
Jesse Barnes776ad802011-01-04 15:09:39 -0800626 if (de_iir & DE_PCH_EVENT) {
627 if (pch_iir & hotplug_mask)
628 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
629 pch_irq_handler(dev);
630 }
Zou Nan haic7c85102010-01-15 10:29:06 +0800631
Jesse Barnesf97108d2010-01-29 11:27:07 -0800632 if (de_iir & DE_PCU_EVENT) {
Jesse Barnes7648fa92010-05-20 14:28:11 -0700633 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
Jesse Barnesf97108d2010-01-29 11:27:07 -0800634 i915_handle_rps_change(dev);
635 }
636
Ben Widawsky4912d042011-04-25 11:25:20 -0700637 if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS) {
638 /*
639 * IIR bits should never already be set because IMR should
640 * prevent an interrupt from being shown in IIR. The warning
641 * displays a case where we've unsafely cleared
642 * dev_priv->pm_iir. Although missing an interrupt of the same
643 * type is not a problem, it displays a problem in the logic.
644 *
645 * The mask bit in IMR is cleared by rps_work.
646 */
647 unsigned long flags;
648 spin_lock_irqsave(&dev_priv->rps_lock, flags);
649 WARN(dev_priv->pm_iir & pm_iir, "Missed a PM interrupt\n");
650 I915_WRITE(GEN6_PMIMR, pm_iir);
651 dev_priv->pm_iir |= pm_iir;
652 spin_unlock_irqrestore(&dev_priv->rps_lock, flags);
653 queue_work(dev_priv->wq, &dev_priv->rps_work);
654 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800655
Zou Nan haic7c85102010-01-15 10:29:06 +0800656 /* should clear PCH hotplug event before clear CPU irq */
657 I915_WRITE(SDEIIR, pch_iir);
658 I915_WRITE(GTIIR, gt_iir);
659 I915_WRITE(DEIIR, de_iir);
Ben Widawsky4912d042011-04-25 11:25:20 -0700660 I915_WRITE(GEN6_PMIIR, pm_iir);
Zou Nan haic7c85102010-01-15 10:29:06 +0800661
662done:
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000663 I915_WRITE(DEIER, de_ier);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000664 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000665
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800666 return ret;
667}
668
Jesse Barnes8a905232009-07-11 16:48:03 -0400669/**
670 * i915_error_work_func - do process context error handling work
671 * @work: work struct
672 *
673 * Fire an error uevent so userspace can see that a hang or error
674 * was detected.
675 */
676static void i915_error_work_func(struct work_struct *work)
677{
678 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
679 error_work);
680 struct drm_device *dev = dev_priv->dev;
Ben Gamarif316a422009-09-14 17:48:46 -0400681 char *error_event[] = { "ERROR=1", NULL };
682 char *reset_event[] = { "RESET=1", NULL };
683 char *reset_done_event[] = { "ERROR=0", NULL };
Jesse Barnes8a905232009-07-11 16:48:03 -0400684
Ben Gamarif316a422009-09-14 17:48:46 -0400685 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -0400686
Ben Gamariba1234d2009-09-14 17:48:47 -0400687 if (atomic_read(&dev_priv->mm.wedged)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +0100688 DRM_DEBUG_DRIVER("resetting chip\n");
689 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
690 if (!i915_reset(dev, GRDOM_RENDER)) {
691 atomic_set(&dev_priv->mm.wedged, 0);
692 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
Ben Gamarif316a422009-09-14 17:48:46 -0400693 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100694 complete_all(&dev_priv->error_completion);
Ben Gamarif316a422009-09-14 17:48:46 -0400695 }
Jesse Barnes8a905232009-07-11 16:48:03 -0400696}
697
Chris Wilson3bd3c932010-08-19 08:19:30 +0100698#ifdef CONFIG_DEBUG_FS
Chris Wilson9df30792010-02-18 10:24:56 +0000699static struct drm_i915_error_object *
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000700i915_error_object_create(struct drm_i915_private *dev_priv,
Chris Wilson05394f32010-11-08 19:18:58 +0000701 struct drm_i915_gem_object *src)
Chris Wilson9df30792010-02-18 10:24:56 +0000702{
703 struct drm_i915_error_object *dst;
Chris Wilson9df30792010-02-18 10:24:56 +0000704 int page, page_count;
Chris Wilsone56660d2010-08-07 11:01:26 +0100705 u32 reloc_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000706
Chris Wilson05394f32010-11-08 19:18:58 +0000707 if (src == NULL || src->pages == NULL)
Chris Wilson9df30792010-02-18 10:24:56 +0000708 return NULL;
709
Chris Wilson05394f32010-11-08 19:18:58 +0000710 page_count = src->base.size / PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +0000711
712 dst = kmalloc(sizeof(*dst) + page_count * sizeof (u32 *), GFP_ATOMIC);
713 if (dst == NULL)
714 return NULL;
715
Chris Wilson05394f32010-11-08 19:18:58 +0000716 reloc_offset = src->gtt_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000717 for (page = 0; page < page_count; page++) {
Andrew Morton788885a2010-05-11 14:07:05 -0700718 unsigned long flags;
Chris Wilsone56660d2010-08-07 11:01:26 +0100719 void __iomem *s;
720 void *d;
Andrew Morton788885a2010-05-11 14:07:05 -0700721
Chris Wilsone56660d2010-08-07 11:01:26 +0100722 d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +0000723 if (d == NULL)
724 goto unwind;
Chris Wilsone56660d2010-08-07 11:01:26 +0100725
Andrew Morton788885a2010-05-11 14:07:05 -0700726 local_irq_save(flags);
Chris Wilsone56660d2010-08-07 11:01:26 +0100727 s = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700728 reloc_offset);
Chris Wilsone56660d2010-08-07 11:01:26 +0100729 memcpy_fromio(d, s, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700730 io_mapping_unmap_atomic(s);
Andrew Morton788885a2010-05-11 14:07:05 -0700731 local_irq_restore(flags);
Chris Wilsone56660d2010-08-07 11:01:26 +0100732
Chris Wilson9df30792010-02-18 10:24:56 +0000733 dst->pages[page] = d;
Chris Wilsone56660d2010-08-07 11:01:26 +0100734
735 reloc_offset += PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +0000736 }
737 dst->page_count = page_count;
Chris Wilson05394f32010-11-08 19:18:58 +0000738 dst->gtt_offset = src->gtt_offset;
Chris Wilson9df30792010-02-18 10:24:56 +0000739
740 return dst;
741
742unwind:
743 while (page--)
744 kfree(dst->pages[page]);
745 kfree(dst);
746 return NULL;
747}
748
749static void
750i915_error_object_free(struct drm_i915_error_object *obj)
751{
752 int page;
753
754 if (obj == NULL)
755 return;
756
757 for (page = 0; page < obj->page_count; page++)
758 kfree(obj->pages[page]);
759
760 kfree(obj);
761}
762
763static void
764i915_error_state_free(struct drm_device *dev,
765 struct drm_i915_error_state *error)
766{
Chris Wilsone2f973d2011-01-27 19:15:11 +0000767 int i;
768
769 for (i = 0; i < ARRAY_SIZE(error->batchbuffer); i++)
770 i915_error_object_free(error->batchbuffer[i]);
771
772 for (i = 0; i < ARRAY_SIZE(error->ringbuffer); i++)
773 i915_error_object_free(error->ringbuffer[i]);
774
Chris Wilson9df30792010-02-18 10:24:56 +0000775 kfree(error->active_bo);
Chris Wilson6ef3d422010-08-04 20:26:07 +0100776 kfree(error->overlay);
Chris Wilson9df30792010-02-18 10:24:56 +0000777 kfree(error);
778}
779
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000780static u32 capture_bo_list(struct drm_i915_error_buffer *err,
781 int count,
782 struct list_head *head)
783{
784 struct drm_i915_gem_object *obj;
785 int i = 0;
786
787 list_for_each_entry(obj, head, mm_list) {
788 err->size = obj->base.size;
789 err->name = obj->base.name;
790 err->seqno = obj->last_rendering_seqno;
791 err->gtt_offset = obj->gtt_offset;
792 err->read_domains = obj->base.read_domains;
793 err->write_domain = obj->base.write_domain;
794 err->fence_reg = obj->fence_reg;
795 err->pinned = 0;
796 if (obj->pin_count > 0)
797 err->pinned = 1;
798 if (obj->user_pin_count > 0)
799 err->pinned = -1;
800 err->tiling = obj->tiling_mode;
801 err->dirty = obj->dirty;
802 err->purgeable = obj->madv != I915_MADV_WILLNEED;
Chris Wilson36850922010-11-23 08:49:38 +0000803 err->ring = obj->ring ? obj->ring->id : 0;
Chris Wilson93dfb402011-03-29 16:59:50 -0700804 err->cache_level = obj->cache_level;
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000805
806 if (++i == count)
807 break;
808
809 err++;
810 }
811
812 return i;
813}
814
Chris Wilson748ebc62010-10-24 10:28:47 +0100815static void i915_gem_record_fences(struct drm_device *dev,
816 struct drm_i915_error_state *error)
817{
818 struct drm_i915_private *dev_priv = dev->dev_private;
819 int i;
820
821 /* Fences */
822 switch (INTEL_INFO(dev)->gen) {
823 case 6:
824 for (i = 0; i < 16; i++)
825 error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
826 break;
827 case 5:
828 case 4:
829 for (i = 0; i < 16; i++)
830 error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
831 break;
832 case 3:
833 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
834 for (i = 0; i < 8; i++)
835 error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
836 case 2:
837 for (i = 0; i < 8; i++)
838 error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
839 break;
840
841 }
842}
843
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000844static struct drm_i915_error_object *
845i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
846 struct intel_ring_buffer *ring)
847{
848 struct drm_i915_gem_object *obj;
849 u32 seqno;
850
851 if (!ring->get_seqno)
852 return NULL;
853
854 seqno = ring->get_seqno(ring);
855 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
856 if (obj->ring != ring)
857 continue;
858
Chris Wilsonc37d9a52011-01-12 20:33:01 +0000859 if (i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000860 continue;
861
862 if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
863 continue;
864
865 /* We need to copy these to an anonymous buffer as the simplest
866 * method to avoid being overwritten by userspace.
867 */
868 return i915_error_object_create(dev_priv, obj);
869 }
870
871 return NULL;
872}
873
Jesse Barnes8a905232009-07-11 16:48:03 -0400874/**
875 * i915_capture_error_state - capture an error record for later analysis
876 * @dev: drm device
877 *
878 * Should be called when an error is detected (either a hang or an error
879 * interrupt) to capture error state from the time of the error. Fills
880 * out a structure which becomes available in debugfs for user level tools
881 * to pick up.
882 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700883static void i915_capture_error_state(struct drm_device *dev)
884{
885 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000886 struct drm_i915_gem_object *obj;
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700887 struct drm_i915_error_state *error;
888 unsigned long flags;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800889 int i, pipe;
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700890
891 spin_lock_irqsave(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +0000892 error = dev_priv->first_error;
893 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
894 if (error)
895 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700896
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800897 /* Account for pipe specific data like PIPE*STAT */
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700898 error = kmalloc(sizeof(*error), GFP_ATOMIC);
899 if (!error) {
Chris Wilson9df30792010-02-18 10:24:56 +0000900 DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
901 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700902 }
903
Chris Wilsonb6f78332011-02-01 14:15:55 +0000904 DRM_INFO("capturing error event; look for more information in /debug/dri/%d/i915_error_state\n",
905 dev->primary->index);
Chris Wilson2fa772f2010-10-01 13:23:27 +0100906
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000907 error->seqno = dev_priv->ring[RCS].get_seqno(&dev_priv->ring[RCS]);
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700908 error->eir = I915_READ(EIR);
909 error->pgtbl_er = I915_READ(PGTBL_ER);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800910 for_each_pipe(pipe)
911 error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700912 error->instpm = I915_READ(INSTPM);
Chris Wilsonf4068392010-10-27 20:36:41 +0100913 error->error = 0;
914 if (INTEL_INFO(dev)->gen >= 6) {
915 error->error = I915_READ(ERROR_GEN6);
Chris Wilsonadd354d2010-10-29 19:00:51 +0100916
Chris Wilson1d8f38f2010-10-29 19:00:51 +0100917 error->bcs_acthd = I915_READ(BCS_ACTHD);
918 error->bcs_ipehr = I915_READ(BCS_IPEHR);
919 error->bcs_ipeir = I915_READ(BCS_IPEIR);
920 error->bcs_instdone = I915_READ(BCS_INSTDONE);
921 error->bcs_seqno = 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000922 if (dev_priv->ring[BCS].get_seqno)
923 error->bcs_seqno = dev_priv->ring[BCS].get_seqno(&dev_priv->ring[BCS]);
Chris Wilsonadd354d2010-10-29 19:00:51 +0100924
925 error->vcs_acthd = I915_READ(VCS_ACTHD);
926 error->vcs_ipehr = I915_READ(VCS_IPEHR);
927 error->vcs_ipeir = I915_READ(VCS_IPEIR);
928 error->vcs_instdone = I915_READ(VCS_INSTDONE);
929 error->vcs_seqno = 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000930 if (dev_priv->ring[VCS].get_seqno)
931 error->vcs_seqno = dev_priv->ring[VCS].get_seqno(&dev_priv->ring[VCS]);
Chris Wilsonf4068392010-10-27 20:36:41 +0100932 }
933 if (INTEL_INFO(dev)->gen >= 4) {
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700934 error->ipeir = I915_READ(IPEIR_I965);
935 error->ipehr = I915_READ(IPEHR_I965);
936 error->instdone = I915_READ(INSTDONE_I965);
937 error->instps = I915_READ(INSTPS);
938 error->instdone1 = I915_READ(INSTDONE1);
939 error->acthd = I915_READ(ACTHD_I965);
Chris Wilson9df30792010-02-18 10:24:56 +0000940 error->bbaddr = I915_READ64(BB_ADDR);
Chris Wilsonf4068392010-10-27 20:36:41 +0100941 } else {
942 error->ipeir = I915_READ(IPEIR);
943 error->ipehr = I915_READ(IPEHR);
944 error->instdone = I915_READ(INSTDONE);
945 error->acthd = I915_READ(ACTHD);
946 error->bbaddr = 0;
Chris Wilson9df30792010-02-18 10:24:56 +0000947 }
Chris Wilson748ebc62010-10-24 10:28:47 +0100948 i915_gem_record_fences(dev, error);
Chris Wilson9df30792010-02-18 10:24:56 +0000949
Chris Wilsone2f973d2011-01-27 19:15:11 +0000950 /* Record the active batch and ring buffers */
951 for (i = 0; i < I915_NUM_RINGS; i++) {
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000952 error->batchbuffer[i] =
953 i915_error_first_batchbuffer(dev_priv,
954 &dev_priv->ring[i]);
Chris Wilson9df30792010-02-18 10:24:56 +0000955
Chris Wilsone2f973d2011-01-27 19:15:11 +0000956 error->ringbuffer[i] =
957 i915_error_object_create(dev_priv,
958 dev_priv->ring[i].obj);
959 }
Chris Wilson9df30792010-02-18 10:24:56 +0000960
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000961 /* Record buffers on the active and pinned lists. */
Chris Wilson9df30792010-02-18 10:24:56 +0000962 error->active_bo = NULL;
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000963 error->pinned_bo = NULL;
Chris Wilson9df30792010-02-18 10:24:56 +0000964
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000965 i = 0;
966 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
967 i++;
968 error->active_bo_count = i;
Chris Wilson05394f32010-11-08 19:18:58 +0000969 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000970 i++;
971 error->pinned_bo_count = i - error->active_bo_count;
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000972
Chris Wilson8e934db2011-01-24 12:34:00 +0000973 error->active_bo = NULL;
974 error->pinned_bo = NULL;
Chris Wilsonbcfb2e22011-01-07 21:06:07 +0000975 if (i) {
976 error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
Chris Wilson9df30792010-02-18 10:24:56 +0000977 GFP_ATOMIC);
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000978 if (error->active_bo)
979 error->pinned_bo =
980 error->active_bo + error->active_bo_count;
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700981 }
982
Chris Wilsonc724e8a2010-11-22 08:07:02 +0000983 if (error->active_bo)
984 error->active_bo_count =
985 capture_bo_list(error->active_bo,
986 error->active_bo_count,
987 &dev_priv->mm.active_list);
988
989 if (error->pinned_bo)
990 error->pinned_bo_count =
991 capture_bo_list(error->pinned_bo,
992 error->pinned_bo_count,
993 &dev_priv->mm.pinned_list);
994
Jesse Barnes8a905232009-07-11 16:48:03 -0400995 do_gettimeofday(&error->time);
996
Chris Wilson6ef3d422010-08-04 20:26:07 +0100997 error->overlay = intel_overlay_capture_error_state(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +0000998 error->display = intel_display_capture_error_state(dev);
Chris Wilson6ef3d422010-08-04 20:26:07 +0100999
Chris Wilson9df30792010-02-18 10:24:56 +00001000 spin_lock_irqsave(&dev_priv->error_lock, flags);
1001 if (dev_priv->first_error == NULL) {
1002 dev_priv->first_error = error;
1003 error = NULL;
1004 }
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001005 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001006
1007 if (error)
1008 i915_error_state_free(dev, error);
1009}
1010
1011void i915_destroy_error_state(struct drm_device *dev)
1012{
1013 struct drm_i915_private *dev_priv = dev->dev_private;
1014 struct drm_i915_error_state *error;
1015
1016 spin_lock(&dev_priv->error_lock);
1017 error = dev_priv->first_error;
1018 dev_priv->first_error = NULL;
1019 spin_unlock(&dev_priv->error_lock);
1020
1021 if (error)
1022 i915_error_state_free(dev, error);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001023}
Chris Wilson3bd3c932010-08-19 08:19:30 +01001024#else
1025#define i915_capture_error_state(x)
1026#endif
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001027
Chris Wilson35aed2e2010-05-27 13:18:12 +01001028static void i915_report_and_clear_eir(struct drm_device *dev)
Jesse Barnes8a905232009-07-11 16:48:03 -04001029{
1030 struct drm_i915_private *dev_priv = dev->dev_private;
1031 u32 eir = I915_READ(EIR);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001032 int pipe;
Jesse Barnes8a905232009-07-11 16:48:03 -04001033
Chris Wilson35aed2e2010-05-27 13:18:12 +01001034 if (!eir)
1035 return;
Jesse Barnes8a905232009-07-11 16:48:03 -04001036
1037 printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
1038 eir);
1039
1040 if (IS_G4X(dev)) {
1041 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
1042 u32 ipeir = I915_READ(IPEIR_I965);
1043
1044 printk(KERN_ERR " IPEIR: 0x%08x\n",
1045 I915_READ(IPEIR_I965));
1046 printk(KERN_ERR " IPEHR: 0x%08x\n",
1047 I915_READ(IPEHR_I965));
1048 printk(KERN_ERR " INSTDONE: 0x%08x\n",
1049 I915_READ(INSTDONE_I965));
1050 printk(KERN_ERR " INSTPS: 0x%08x\n",
1051 I915_READ(INSTPS));
1052 printk(KERN_ERR " INSTDONE1: 0x%08x\n",
1053 I915_READ(INSTDONE1));
1054 printk(KERN_ERR " ACTHD: 0x%08x\n",
1055 I915_READ(ACTHD_I965));
1056 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001057 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001058 }
1059 if (eir & GM45_ERROR_PAGE_TABLE) {
1060 u32 pgtbl_err = I915_READ(PGTBL_ER);
1061 printk(KERN_ERR "page table error\n");
1062 printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
1063 pgtbl_err);
1064 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001065 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04001066 }
1067 }
1068
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001069 if (!IS_GEN2(dev)) {
Jesse Barnes8a905232009-07-11 16:48:03 -04001070 if (eir & I915_ERROR_PAGE_TABLE) {
1071 u32 pgtbl_err = I915_READ(PGTBL_ER);
1072 printk(KERN_ERR "page table error\n");
1073 printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
1074 pgtbl_err);
1075 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001076 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04001077 }
1078 }
1079
1080 if (eir & I915_ERROR_MEMORY_REFRESH) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001081 printk(KERN_ERR "memory refresh error:\n");
1082 for_each_pipe(pipe)
1083 printk(KERN_ERR "pipe %c stat: 0x%08x\n",
1084 pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
Jesse Barnes8a905232009-07-11 16:48:03 -04001085 /* pipestat has already been acked */
1086 }
1087 if (eir & I915_ERROR_INSTRUCTION) {
1088 printk(KERN_ERR "instruction error\n");
1089 printk(KERN_ERR " INSTPM: 0x%08x\n",
1090 I915_READ(INSTPM));
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001091 if (INTEL_INFO(dev)->gen < 4) {
Jesse Barnes8a905232009-07-11 16:48:03 -04001092 u32 ipeir = I915_READ(IPEIR);
1093
1094 printk(KERN_ERR " IPEIR: 0x%08x\n",
1095 I915_READ(IPEIR));
1096 printk(KERN_ERR " IPEHR: 0x%08x\n",
1097 I915_READ(IPEHR));
1098 printk(KERN_ERR " INSTDONE: 0x%08x\n",
1099 I915_READ(INSTDONE));
1100 printk(KERN_ERR " ACTHD: 0x%08x\n",
1101 I915_READ(ACTHD));
1102 I915_WRITE(IPEIR, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001103 POSTING_READ(IPEIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001104 } else {
1105 u32 ipeir = I915_READ(IPEIR_I965);
1106
1107 printk(KERN_ERR " IPEIR: 0x%08x\n",
1108 I915_READ(IPEIR_I965));
1109 printk(KERN_ERR " IPEHR: 0x%08x\n",
1110 I915_READ(IPEHR_I965));
1111 printk(KERN_ERR " INSTDONE: 0x%08x\n",
1112 I915_READ(INSTDONE_I965));
1113 printk(KERN_ERR " INSTPS: 0x%08x\n",
1114 I915_READ(INSTPS));
1115 printk(KERN_ERR " INSTDONE1: 0x%08x\n",
1116 I915_READ(INSTDONE1));
1117 printk(KERN_ERR " ACTHD: 0x%08x\n",
1118 I915_READ(ACTHD_I965));
1119 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001120 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001121 }
1122 }
1123
1124 I915_WRITE(EIR, eir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001125 POSTING_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001126 eir = I915_READ(EIR);
1127 if (eir) {
1128 /*
1129 * some errors might have become stuck,
1130 * mask them.
1131 */
1132 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
1133 I915_WRITE(EMR, I915_READ(EMR) | eir);
1134 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
1135 }
Chris Wilson35aed2e2010-05-27 13:18:12 +01001136}
1137
1138/**
1139 * i915_handle_error - handle an error interrupt
1140 * @dev: drm device
1141 *
1142 * Do some basic checking of regsiter state at error interrupt time and
1143 * dump it to the syslog. Also call i915_capture_error_state() to make
1144 * sure we get a record and make it available in debugfs. Fire a uevent
1145 * so userspace knows something bad happened (should trigger collection
1146 * of a ring dump etc.).
1147 */
Chris Wilson527f9e92010-11-11 01:16:58 +00001148void i915_handle_error(struct drm_device *dev, bool wedged)
Chris Wilson35aed2e2010-05-27 13:18:12 +01001149{
1150 struct drm_i915_private *dev_priv = dev->dev_private;
1151
1152 i915_capture_error_state(dev);
1153 i915_report_and_clear_eir(dev);
Jesse Barnes8a905232009-07-11 16:48:03 -04001154
Ben Gamariba1234d2009-09-14 17:48:47 -04001155 if (wedged) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001156 INIT_COMPLETION(dev_priv->error_completion);
Ben Gamariba1234d2009-09-14 17:48:47 -04001157 atomic_set(&dev_priv->mm.wedged, 1);
1158
Ben Gamari11ed50e2009-09-14 17:48:45 -04001159 /*
1160 * Wakeup waiting processes so they don't hang
1161 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001162 wake_up_all(&dev_priv->ring[RCS].irq_queue);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001163 if (HAS_BSD(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001164 wake_up_all(&dev_priv->ring[VCS].irq_queue);
Chris Wilson549f7362010-10-19 11:19:32 +01001165 if (HAS_BLT(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001166 wake_up_all(&dev_priv->ring[BCS].irq_queue);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001167 }
1168
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001169 queue_work(dev_priv->wq, &dev_priv->error_work);
Jesse Barnes8a905232009-07-11 16:48:03 -04001170}
1171
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001172static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
1173{
1174 drm_i915_private_t *dev_priv = dev->dev_private;
1175 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1176 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00001177 struct drm_i915_gem_object *obj;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001178 struct intel_unpin_work *work;
1179 unsigned long flags;
1180 bool stall_detected;
1181
1182 /* Ignore early vblank irqs */
1183 if (intel_crtc == NULL)
1184 return;
1185
1186 spin_lock_irqsave(&dev->event_lock, flags);
1187 work = intel_crtc->unpin_work;
1188
1189 if (work == NULL || work->pending || !work->enable_stall_check) {
1190 /* Either the pending flip IRQ arrived, or we're too early. Don't check */
1191 spin_unlock_irqrestore(&dev->event_lock, flags);
1192 return;
1193 }
1194
1195 /* Potential stall - if we see that the flip has happened, assume a missed interrupt */
Chris Wilson05394f32010-11-08 19:18:58 +00001196 obj = work->pending_flip_obj;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001197 if (INTEL_INFO(dev)->gen >= 4) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001198 int dspsurf = DSPSURF(intel_crtc->plane);
Chris Wilson05394f32010-11-08 19:18:58 +00001199 stall_detected = I915_READ(dspsurf) == obj->gtt_offset;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001200 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001201 int dspaddr = DSPADDR(intel_crtc->plane);
Chris Wilson05394f32010-11-08 19:18:58 +00001202 stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001203 crtc->y * crtc->fb->pitch +
1204 crtc->x * crtc->fb->bits_per_pixel/8);
1205 }
1206
1207 spin_unlock_irqrestore(&dev->event_lock, flags);
1208
1209 if (stall_detected) {
1210 DRM_DEBUG_DRIVER("Pageflip stall detected\n");
1211 intel_prepare_page_flip(dev, intel_crtc->plane);
1212 }
1213}
1214
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001215static irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Dave Airlie84b1fd12007-07-11 15:53:27 +10001217 struct drm_device *dev = (struct drm_device *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001219 struct drm_i915_master_private *master_priv;
Eric Anholtcdfbc412008-11-04 15:50:30 -08001220 u32 iir, new_iir;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001221 u32 pipe_stats[I915_MAX_PIPES];
Keith Packard05eff842008-11-19 14:03:05 -08001222 u32 vblank_status;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001223 int vblank = 0;
Keith Packard7c463582008-11-04 02:03:27 -08001224 unsigned long irqflags;
Keith Packard05eff842008-11-19 14:03:05 -08001225 int irq_received;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001226 int ret = IRQ_NONE, pipe;
1227 bool blc_event = false;
Dave Airlieaf6061a2008-05-07 12:15:39 +10001228
Eric Anholt630681d2008-10-06 15:14:12 -07001229 atomic_inc(&dev_priv->irq_received);
1230
Eric Anholted4cb412008-07-29 12:10:39 -07001231 iir = I915_READ(IIR);
Dave Airlieaf6061a2008-05-07 12:15:39 +10001232
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001233 if (INTEL_INFO(dev)->gen >= 4)
Jesse Barnesd874bcf2010-06-30 13:16:00 -07001234 vblank_status = PIPE_START_VBLANK_INTERRUPT_STATUS;
Jesse Barnese25e6602010-06-30 13:15:19 -07001235 else
Jesse Barnesd874bcf2010-06-30 13:16:00 -07001236 vblank_status = PIPE_VBLANK_INTERRUPT_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Keith Packard05eff842008-11-19 14:03:05 -08001238 for (;;) {
1239 irq_received = iir != 0;
1240
1241 /* Can't rely on pipestat interrupt bit in iir as it might
1242 * have been cleared after the pipestat interrupt was received.
1243 * It doesn't set the bit in iir again, but it still produces
1244 * interrupts (for non-MSI).
1245 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001246 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes8a905232009-07-11 16:48:03 -04001247 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Ben Gamariba1234d2009-09-14 17:48:47 -04001248 i915_handle_error(dev, false);
Jesse Barnes8a905232009-07-11 16:48:03 -04001249
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001250 for_each_pipe(pipe) {
1251 int reg = PIPESTAT(pipe);
1252 pipe_stats[pipe] = I915_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -08001253
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001254 /*
1255 * Clear the PIPE*STAT regs before the IIR
1256 */
1257 if (pipe_stats[pipe] & 0x8000ffff) {
1258 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1259 DRM_DEBUG_DRIVER("pipe %c underrun\n",
1260 pipe_name(pipe));
1261 I915_WRITE(reg, pipe_stats[pipe]);
1262 irq_received = 1;
1263 }
Eric Anholtcdfbc412008-11-04 15:50:30 -08001264 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001265 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Keith Packard05eff842008-11-19 14:03:05 -08001266
1267 if (!irq_received)
1268 break;
1269
1270 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Jesse Barnes5ca58282009-03-31 14:11:15 -07001272 /* Consume port. Then clear IIR or we'll miss events */
1273 if ((I915_HAS_HOTPLUG(dev)) &&
1274 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
1275 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1276
Zhao Yakui44d98a62009-10-09 11:39:40 +08001277 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
Jesse Barnes5ca58282009-03-31 14:11:15 -07001278 hotplug_status);
1279 if (hotplug_status & dev_priv->hotplug_supported_mask)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001280 queue_work(dev_priv->wq,
1281 &dev_priv->hotplug_work);
Jesse Barnes5ca58282009-03-31 14:11:15 -07001282
1283 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
1284 I915_READ(PORT_HOTPLUG_STAT);
1285 }
1286
Eric Anholtcdfbc412008-11-04 15:50:30 -08001287 I915_WRITE(IIR, iir);
1288 new_iir = I915_READ(IIR); /* Flush posted writes */
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001289
Dave Airlie7c1c2872008-11-28 14:22:24 +10001290 if (dev->primary->master) {
1291 master_priv = dev->primary->master->driver_priv;
1292 if (master_priv->sarea_priv)
1293 master_priv->sarea_priv->last_dispatch =
1294 READ_BREADCRUMB(dev_priv);
1295 }
Keith Packard7c463582008-11-04 02:03:27 -08001296
Chris Wilson549f7362010-10-19 11:19:32 +01001297 if (iir & I915_USER_INTERRUPT)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001298 notify_ring(dev, &dev_priv->ring[RCS]);
1299 if (iir & I915_BSD_USER_INTERRUPT)
1300 notify_ring(dev, &dev_priv->ring[VCS]);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001301
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001302 if (iir & I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001303 intel_prepare_page_flip(dev, 0);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001304 if (dev_priv->flip_pending_is_done)
1305 intel_finish_page_flip_plane(dev, 0);
1306 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001307
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001308 if (iir & I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT) {
Jesse Barnes70565d02010-07-01 04:45:43 -07001309 intel_prepare_page_flip(dev, 1);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001310 if (dev_priv->flip_pending_is_done)
1311 intel_finish_page_flip_plane(dev, 1);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001312 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001313
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001314 for_each_pipe(pipe) {
1315 if (pipe_stats[pipe] & vblank_status &&
1316 drm_handle_vblank(dev, pipe)) {
1317 vblank++;
1318 if (!dev_priv->flip_pending_is_done) {
1319 i915_pageflip_stall_check(dev, pipe);
1320 intel_finish_page_flip(dev, pipe);
1321 }
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001322 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001323
1324 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1325 blc_event = true;
Eric Anholtcdfbc412008-11-04 15:50:30 -08001326 }
Eric Anholt673a3942008-07-30 12:06:12 -07001327
Keith Packard7c463582008-11-04 02:03:27 -08001328
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001329 if (blc_event || (iir & I915_ASLE_INTERRUPT))
Chris Wilson3b617962010-08-24 09:02:58 +01001330 intel_opregion_asle_intr(dev);
Keith Packard7c463582008-11-04 02:03:27 -08001331
Eric Anholtcdfbc412008-11-04 15:50:30 -08001332 /* With MSI, interrupts are only generated when iir
1333 * transitions from zero to nonzero. If another bit got
1334 * set while we were handling the existing iir bits, then
1335 * we would never get another interrupt.
1336 *
1337 * This is fine on non-MSI as well, as if we hit this path
1338 * we avoid exiting the interrupt handler only to generate
1339 * another one.
1340 *
1341 * Note that for MSI this could cause a stray interrupt report
1342 * if an interrupt landed in the time between writing IIR and
1343 * the posting read. This should be rare enough to never
1344 * trigger the 99% of 100,000 interrupts test for disabling
1345 * stray interrupts.
1346 */
1347 iir = new_iir;
Keith Packard05eff842008-11-19 14:03:05 -08001348 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001349
Keith Packard05eff842008-11-19 14:03:05 -08001350 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Dave Airlieaf6061a2008-05-07 12:15:39 +10001353static int i915_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001356 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 i915_kernel_lost_context(dev);
1359
Zhao Yakui44d98a62009-10-09 11:39:40 +08001360 DRM_DEBUG_DRIVER("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Kristian Høgsbergc99b0582008-08-20 11:20:13 -04001362 dev_priv->counter++;
Alan Hourihanec29b6692006-08-12 16:29:24 +10001363 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -04001364 dev_priv->counter = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001365 if (master_priv->sarea_priv)
1366 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Alan Hourihanec29b6692006-08-12 16:29:24 +10001367
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001368 if (BEGIN_LP_RING(4) == 0) {
1369 OUT_RING(MI_STORE_DWORD_INDEX);
1370 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1371 OUT_RING(dev_priv->counter);
1372 OUT_RING(MI_USER_INTERRUPT);
1373 ADVANCE_LP_RING();
1374 }
Dave Airliebc5f4522007-11-05 12:50:58 +10001375
Alan Hourihanec29b6692006-08-12 16:29:24 +10001376 return dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Dave Airlie84b1fd12007-07-11 15:53:27 +10001379static int i915_wait_irq(struct drm_device * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001382 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 int ret = 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001384 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Zhao Yakui44d98a62009-10-09 11:39:40 +08001386 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 READ_BREADCRUMB(dev_priv));
1388
Eric Anholted4cb412008-07-29 12:10:39 -07001389 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
Dave Airlie7c1c2872008-11-28 14:22:24 +10001390 if (master_priv->sarea_priv)
1391 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Dave Airlie7c1c2872008-11-28 14:22:24 +10001395 if (master_priv->sarea_priv)
1396 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001398 if (ring->irq_get(ring)) {
1399 DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
1400 READ_BREADCRUMB(dev_priv) >= irq_nr);
1401 ring->irq_put(ring);
Chris Wilson5a9a8d12011-01-23 13:03:24 +00001402 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
1403 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Eric Anholt20caafa2007-08-25 19:22:43 +10001405 if (ret == -EBUSY) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001406 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
1408 }
1409
Dave Airlieaf6061a2008-05-07 12:15:39 +10001410 return ret;
1411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/* Needs the lock as it touches the ring.
1414 */
Eric Anholtc153f452007-09-03 12:06:45 +10001415int i915_irq_emit(struct drm_device *dev, void *data,
1416 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001419 drm_i915_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 int result;
1421
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001422 if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001423 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001424 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Eric Anholt299eb932009-02-24 22:14:12 -08001426
1427 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1428
Eric Anholt546b0972008-09-01 16:45:29 -07001429 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 result = i915_emit_irq(dev);
Eric Anholt546b0972008-09-01 16:45:29 -07001431 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Eric Anholtc153f452007-09-03 12:06:45 +10001433 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001435 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437
1438 return 0;
1439}
1440
1441/* Doesn't need the hardware lock.
1442 */
Eric Anholtc153f452007-09-03 12:06:45 +10001443int i915_irq_wait(struct drm_device *dev, void *data,
1444 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001447 drm_i915_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001450 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001451 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
Eric Anholtc153f452007-09-03 12:06:45 +10001454 return i915_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Keith Packard42f52ef2008-10-18 19:39:29 -07001457/* Called from drm generic code, passed 'crtc' which
1458 * we use as a pipe index
1459 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001460static int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001461{
1462 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001463 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001464
Chris Wilson5eddb702010-09-11 13:48:45 +01001465 if (!i915_pipe_enabled(dev, pipe))
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001466 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001467
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001468 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07001469 if (INTEL_INFO(dev)->gen >= 4)
Keith Packard7c463582008-11-04 02:03:27 -08001470 i915_enable_pipestat(dev_priv, pipe,
1471 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -07001472 else
Keith Packard7c463582008-11-04 02:03:27 -08001473 i915_enable_pipestat(dev_priv, pipe,
1474 PIPE_VBLANK_INTERRUPT_ENABLE);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001475
1476 /* maintain vblank delivery even in deep C-states */
1477 if (dev_priv->info->gen == 3)
1478 I915_WRITE(INSTPM, INSTPM_AGPBUSY_DIS << 16);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001479 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001480
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001481 return 0;
1482}
1483
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001484static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07001485{
1486 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1487 unsigned long irqflags;
1488
1489 if (!i915_pipe_enabled(dev, pipe))
1490 return -EINVAL;
1491
1492 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1493 ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1494 DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
1495 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1496
1497 return 0;
1498}
1499
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001500static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001501{
1502 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1503 unsigned long irqflags;
1504
1505 if (!i915_pipe_enabled(dev, pipe))
1506 return -EINVAL;
1507
1508 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1509 ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1510 DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1511 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1512
1513 return 0;
1514}
1515
Keith Packard42f52ef2008-10-18 19:39:29 -07001516/* Called from drm generic code, passed 'crtc' which
1517 * we use as a pipe index
1518 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001519static void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001520{
1521 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001522 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001523
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001524 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001525 if (dev_priv->info->gen == 3)
1526 I915_WRITE(INSTPM,
1527 INSTPM_AGPBUSY_DIS << 16 | INSTPM_AGPBUSY_DIS);
1528
Jesse Barnesf796cf82011-04-07 13:58:17 -07001529 i915_disable_pipestat(dev_priv, pipe,
1530 PIPE_VBLANK_INTERRUPT_ENABLE |
1531 PIPE_START_VBLANK_INTERRUPT_ENABLE);
1532 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1533}
1534
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001535static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07001536{
1537 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1538 unsigned long irqflags;
1539
1540 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1541 ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1542 DE_PIPEA_VBLANK: DE_PIPEB_VBLANK);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001543 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001544}
1545
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001546static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001547{
1548 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1549 unsigned long irqflags;
1550
1551 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1552 ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1553 DE_PIPEA_VBLANK_IVB : DE_PIPEB_VBLANK_IVB);
1554 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1555}
1556
Dave Airlie702880f2006-06-24 17:07:34 +10001557/* Set the vblank monitor pipe
1558 */
Eric Anholtc153f452007-09-03 12:06:45 +10001559int i915_vblank_pipe_set(struct drm_device *dev, void *data,
1560 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +10001561{
Dave Airlie702880f2006-06-24 17:07:34 +10001562 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie702880f2006-06-24 17:07:34 +10001563
1564 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001565 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001566 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +10001567 }
1568
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +10001569 return 0;
Dave Airlie702880f2006-06-24 17:07:34 +10001570}
1571
Eric Anholtc153f452007-09-03 12:06:45 +10001572int i915_vblank_pipe_get(struct drm_device *dev, void *data,
1573 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +10001574{
Dave Airlie702880f2006-06-24 17:07:34 +10001575 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001576 drm_i915_vblank_pipe_t *pipe = data;
Dave Airlie702880f2006-06-24 17:07:34 +10001577
1578 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001579 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001580 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +10001581 }
1582
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001583 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Eric Anholtc153f452007-09-03 12:06:45 +10001584
Dave Airlie702880f2006-06-24 17:07:34 +10001585 return 0;
1586}
1587
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +10001588/**
1589 * Schedule buffer swap at given vertical blank.
1590 */
Eric Anholtc153f452007-09-03 12:06:45 +10001591int i915_vblank_swap(struct drm_device *dev, void *data,
1592 struct drm_file *file_priv)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +10001593{
Eric Anholtbd95e0a2008-11-04 12:01:24 -08001594 /* The delayed swap mechanism was fundamentally racy, and has been
1595 * removed. The model was that the client requested a delayed flip/swap
1596 * from the kernel, then waited for vblank before continuing to perform
1597 * rendering. The problem was that the kernel might wake the client
1598 * up before it dispatched the vblank swap (since the lock has to be
1599 * held while touching the ringbuffer), in which case the client would
1600 * clear and start the next frame before the swap occurred, and
1601 * flicker would occur in addition to likely missing the vblank.
1602 *
1603 * In the absence of this ioctl, userland falls back to a correct path
1604 * of waiting for a vblank, then dispatching the swap on its own.
1605 * Context switching to userland and back is plenty fast enough for
1606 * meeting the requirements of vblank swapping.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001607 */
Eric Anholtbd95e0a2008-11-04 12:01:24 -08001608 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +10001609}
1610
Chris Wilson893eead2010-10-27 14:44:35 +01001611static u32
1612ring_last_seqno(struct intel_ring_buffer *ring)
Zou Nan hai852835f2010-05-21 09:08:56 +08001613{
Chris Wilson893eead2010-10-27 14:44:35 +01001614 return list_entry(ring->request_list.prev,
1615 struct drm_i915_gem_request, list)->seqno;
1616}
1617
1618static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
1619{
1620 if (list_empty(&ring->request_list) ||
1621 i915_seqno_passed(ring->get_seqno(ring), ring_last_seqno(ring))) {
1622 /* Issue a wake-up to catch stuck h/w. */
Chris Wilsonb2223492010-10-27 15:27:33 +01001623 if (ring->waiting_seqno && waitqueue_active(&ring->irq_queue)) {
Chris Wilson893eead2010-10-27 14:44:35 +01001624 DRM_ERROR("Hangcheck timer elapsed... %s idle [waiting on %d, at %d], missed IRQ?\n",
1625 ring->name,
Chris Wilsonb2223492010-10-27 15:27:33 +01001626 ring->waiting_seqno,
Chris Wilson893eead2010-10-27 14:44:35 +01001627 ring->get_seqno(ring));
1628 wake_up_all(&ring->irq_queue);
1629 *err = true;
1630 }
1631 return true;
1632 }
1633 return false;
Ben Gamarif65d9422009-09-14 17:48:44 -04001634}
1635
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001636static bool kick_ring(struct intel_ring_buffer *ring)
1637{
1638 struct drm_device *dev = ring->dev;
1639 struct drm_i915_private *dev_priv = dev->dev_private;
1640 u32 tmp = I915_READ_CTL(ring);
1641 if (tmp & RING_WAIT) {
1642 DRM_ERROR("Kicking stuck wait on %s\n",
1643 ring->name);
1644 I915_WRITE_CTL(ring, tmp);
1645 return true;
1646 }
1647 if (IS_GEN6(dev) &&
1648 (tmp & RING_WAIT_SEMAPHORE)) {
1649 DRM_ERROR("Kicking stuck semaphore on %s\n",
1650 ring->name);
1651 I915_WRITE_CTL(ring, tmp);
1652 return true;
1653 }
1654 return false;
1655}
1656
Ben Gamarif65d9422009-09-14 17:48:44 -04001657/**
1658 * This is called when the chip hasn't reported back with completed
1659 * batchbuffers in a long time. The first time this is called we simply record
1660 * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
1661 * again, we assume the chip is wedged and try to fix it.
1662 */
1663void i915_hangcheck_elapsed(unsigned long data)
1664{
1665 struct drm_device *dev = (struct drm_device *)data;
1666 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001667 uint32_t acthd, instdone, instdone1;
Chris Wilson893eead2010-10-27 14:44:35 +01001668 bool err = false;
1669
1670 /* If all work is done then ACTHD clearly hasn't advanced. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001671 if (i915_hangcheck_ring_idle(&dev_priv->ring[RCS], &err) &&
1672 i915_hangcheck_ring_idle(&dev_priv->ring[VCS], &err) &&
1673 i915_hangcheck_ring_idle(&dev_priv->ring[BCS], &err)) {
Chris Wilson893eead2010-10-27 14:44:35 +01001674 dev_priv->hangcheck_count = 0;
1675 if (err)
1676 goto repeat;
1677 return;
1678 }
Eric Anholtb9201c12010-01-08 14:25:16 -08001679
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001680 if (INTEL_INFO(dev)->gen < 4) {
Ben Gamarif65d9422009-09-14 17:48:44 -04001681 acthd = I915_READ(ACTHD);
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001682 instdone = I915_READ(INSTDONE);
1683 instdone1 = 0;
1684 } else {
Ben Gamarif65d9422009-09-14 17:48:44 -04001685 acthd = I915_READ(ACTHD_I965);
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001686 instdone = I915_READ(INSTDONE_I965);
1687 instdone1 = I915_READ(INSTDONE1);
1688 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001689
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001690 if (dev_priv->last_acthd == acthd &&
1691 dev_priv->last_instdone == instdone &&
1692 dev_priv->last_instdone1 == instdone1) {
1693 if (dev_priv->hangcheck_count++ > 1) {
1694 DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
Chris Wilson8c80b592010-08-08 20:38:12 +01001695
1696 if (!IS_GEN2(dev)) {
1697 /* Is the chip hanging on a WAIT_FOR_EVENT?
1698 * If so we can simply poke the RB_WAIT bit
1699 * and break the hang. This should work on
1700 * all but the second generation chipsets.
1701 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001702
1703 if (kick_ring(&dev_priv->ring[RCS]))
Chris Wilson893eead2010-10-27 14:44:35 +01001704 goto repeat;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001705
1706 if (HAS_BSD(dev) &&
1707 kick_ring(&dev_priv->ring[VCS]))
1708 goto repeat;
1709
1710 if (HAS_BLT(dev) &&
1711 kick_ring(&dev_priv->ring[BCS]))
1712 goto repeat;
Chris Wilson8c80b592010-08-08 20:38:12 +01001713 }
1714
Chris Wilsoncbb465e2010-06-06 12:16:24 +01001715 i915_handle_error(dev, true);
1716 return;
1717 }
1718 } else {
1719 dev_priv->hangcheck_count = 0;
1720
1721 dev_priv->last_acthd = acthd;
1722 dev_priv->last_instdone = instdone;
1723 dev_priv->last_instdone1 = instdone1;
1724 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001725
Chris Wilson893eead2010-10-27 14:44:35 +01001726repeat:
Ben Gamarif65d9422009-09-14 17:48:44 -04001727 /* Reset timer case chip hangs without another request being added */
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001728 mod_timer(&dev_priv->hangcheck_timer,
1729 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732/* drm_dma.h hooks
1733*/
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001734static void ironlake_irq_preinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001735{
1736 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1737
Jesse Barnes46979952011-04-07 13:53:55 -07001738 atomic_set(&dev_priv->irq_received, 0);
1739
1740 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
1741 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
Jesse Barnes9e3c2562011-05-18 13:51:43 -07001742 if (IS_GEN6(dev) || IS_IVYBRIDGE(dev))
1743 INIT_WORK(&dev_priv->rps_work, gen6_pm_rps_work);
Jesse Barnes46979952011-04-07 13:53:55 -07001744
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001745 I915_WRITE(HWSTAM, 0xeffe);
Jesse Barnes2b1ecb72011-07-01 11:08:56 -07001746 if (IS_GEN6(dev) || IS_GEN7(dev)) {
Daniel J Blueman498e7202011-06-17 11:32:19 -07001747 /* Workaround stalls observed on Sandy Bridge GPUs by
1748 * making the blitter command streamer generate a
1749 * write to the Hardware Status Page for
1750 * MI_USER_INTERRUPT. This appears to serialize the
1751 * previous seqno write out before the interrupt
1752 * happens.
1753 */
1754 I915_WRITE(GEN6_BLITTER_HWSTAM, ~GEN6_BLITTER_USER_INTERRUPT);
Chris Wilsonec6a8902011-06-21 18:37:59 +01001755 I915_WRITE(GEN6_BSD_HWSTAM, ~GEN6_BSD_USER_INTERRUPT);
Daniel J Blueman498e7202011-06-17 11:32:19 -07001756 }
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001757
1758 /* XXX hotplug from PCH */
1759
1760 I915_WRITE(DEIMR, 0xffffffff);
1761 I915_WRITE(DEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001762 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001763
1764 /* and GT */
1765 I915_WRITE(GTIMR, 0xffffffff);
1766 I915_WRITE(GTIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001767 POSTING_READ(GTIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00001768
1769 /* south display irq */
1770 I915_WRITE(SDEIMR, 0xffffffff);
1771 I915_WRITE(SDEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001772 POSTING_READ(SDEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001773}
1774
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001775static int ironlake_irq_postinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001776{
1777 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1778 /* enable kind of interrupts always enabled */
Jesse Barnes013d5aa2010-01-29 11:18:31 -08001779 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
1780 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001781 u32 render_irqs;
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01001782 u32 hotplug_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001783
Jesse Barnes46979952011-04-07 13:53:55 -07001784 DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
1785 if (HAS_BSD(dev))
1786 DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
1787 if (HAS_BLT(dev))
1788 DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
1789
1790 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001791 dev_priv->irq_mask = ~display_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001792
1793 /* should always can generate irq */
1794 I915_WRITE(DEIIR, I915_READ(DEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001795 I915_WRITE(DEIMR, dev_priv->irq_mask);
1796 I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001797 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001798
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001799 dev_priv->gt_irq_mask = ~0;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001800
1801 I915_WRITE(GTIIR, I915_READ(GTIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001802 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001803
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001804 if (IS_GEN6(dev))
1805 render_irqs =
1806 GT_USER_INTERRUPT |
1807 GT_GEN6_BSD_USER_INTERRUPT |
1808 GT_BLT_USER_INTERRUPT;
1809 else
1810 render_irqs =
Chris Wilson88f23b82010-12-05 15:08:31 +00001811 GT_USER_INTERRUPT |
Chris Wilsonc6df5412010-12-15 09:56:50 +00001812 GT_PIPE_NOTIFY |
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001813 GT_BSD_USER_INTERRUPT;
1814 I915_WRITE(GTIER, render_irqs);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001815 POSTING_READ(GTIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001816
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01001817 if (HAS_PCH_CPT(dev)) {
Chris Wilson9035a972011-02-16 09:36:05 +00001818 hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
1819 SDE_PORTB_HOTPLUG_CPT |
1820 SDE_PORTC_HOTPLUG_CPT |
1821 SDE_PORTD_HOTPLUG_CPT);
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01001822 } else {
Chris Wilson9035a972011-02-16 09:36:05 +00001823 hotplug_mask = (SDE_CRT_HOTPLUG |
1824 SDE_PORTB_HOTPLUG |
1825 SDE_PORTC_HOTPLUG |
1826 SDE_PORTD_HOTPLUG |
1827 SDE_AUX_MASK);
Yuanhan Liu2d7b8362010-10-08 10:21:06 +01001828 }
1829
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001830 dev_priv->pch_irq_mask = ~hotplug_mask;
Zhenyu Wangc6501562009-11-03 18:57:21 +00001831
1832 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001833 I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1834 I915_WRITE(SDEIER, hotplug_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001835 POSTING_READ(SDEIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00001836
Jesse Barnesf97108d2010-01-29 11:27:07 -08001837 if (IS_IRONLAKE_M(dev)) {
1838 /* Clear & enable PCU event interrupts */
1839 I915_WRITE(DEIIR, DE_PCU_EVENT);
1840 I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
1841 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
1842 }
1843
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001844 return 0;
1845}
1846
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001847static int ivybridge_irq_postinstall(struct drm_device *dev)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001848{
1849 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1850 /* enable kind of interrupts always enabled */
1851 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
1852 DE_PCH_EVENT_IVB | DE_PLANEA_FLIP_DONE_IVB |
1853 DE_PLANEB_FLIP_DONE_IVB;
1854 u32 render_irqs;
1855 u32 hotplug_mask;
1856
1857 DRM_INIT_WAITQUEUE(&dev_priv->ring[RCS].irq_queue);
1858 if (HAS_BSD(dev))
1859 DRM_INIT_WAITQUEUE(&dev_priv->ring[VCS].irq_queue);
1860 if (HAS_BLT(dev))
1861 DRM_INIT_WAITQUEUE(&dev_priv->ring[BCS].irq_queue);
1862
1863 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
1864 dev_priv->irq_mask = ~display_mask;
1865
1866 /* should always can generate irq */
1867 I915_WRITE(DEIIR, I915_READ(DEIIR));
1868 I915_WRITE(DEIMR, dev_priv->irq_mask);
1869 I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK_IVB |
1870 DE_PIPEB_VBLANK_IVB);
1871 POSTING_READ(DEIER);
1872
1873 dev_priv->gt_irq_mask = ~0;
1874
1875 I915_WRITE(GTIIR, I915_READ(GTIIR));
1876 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
1877
1878 render_irqs = GT_USER_INTERRUPT | GT_GEN6_BSD_USER_INTERRUPT |
1879 GT_BLT_USER_INTERRUPT;
1880 I915_WRITE(GTIER, render_irqs);
1881 POSTING_READ(GTIER);
1882
1883 hotplug_mask = (SDE_CRT_HOTPLUG_CPT |
1884 SDE_PORTB_HOTPLUG_CPT |
1885 SDE_PORTC_HOTPLUG_CPT |
1886 SDE_PORTD_HOTPLUG_CPT);
1887 dev_priv->pch_irq_mask = ~hotplug_mask;
1888
1889 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
1890 I915_WRITE(SDEIMR, dev_priv->pch_irq_mask);
1891 I915_WRITE(SDEIER, hotplug_mask);
1892 POSTING_READ(SDEIER);
1893
1894 return 0;
1895}
1896
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001897static void i915_driver_irq_preinstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
1899 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001900 int pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Jesse Barnes79e53942008-11-07 14:24:08 -08001902 atomic_set(&dev_priv->irq_received, 0);
1903
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001904 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Jesse Barnes8a905232009-07-11 16:48:03 -04001905 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001906
Jesse Barnes5ca58282009-03-31 14:11:15 -07001907 if (I915_HAS_HOTPLUG(dev)) {
1908 I915_WRITE(PORT_HOTPLUG_EN, 0);
1909 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1910 }
1911
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001912 I915_WRITE(HWSTAM, 0xeffe);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001913 for_each_pipe(pipe)
1914 I915_WRITE(PIPESTAT(pipe), 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001915 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07001916 I915_WRITE(IER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001917 POSTING_READ(IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001920/*
1921 * Must be called after intel_modeset_init or hotplug interrupts won't be
1922 * enabled correctly.
1923 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001924static int i915_driver_irq_postinstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes5ca58282009-03-31 14:11:15 -07001927 u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001928 u32 error_mask;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001929
1930 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001931
Keith Packard7c463582008-11-04 02:03:27 -08001932 /* Unmask the interrupts that we always want on. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001933 dev_priv->irq_mask = ~I915_INTERRUPT_ENABLE_FIX;
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001934
Keith Packard7c463582008-11-04 02:03:27 -08001935 dev_priv->pipestat[0] = 0;
1936 dev_priv->pipestat[1] = 0;
1937
Jesse Barnes5ca58282009-03-31 14:11:15 -07001938 if (I915_HAS_HOTPLUG(dev)) {
Adam Jacksonc496fa12010-05-27 17:26:45 -04001939 /* Enable in IER... */
1940 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1941 /* and unmask in IMR */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001942 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
Adam Jacksonc496fa12010-05-27 17:26:45 -04001943 }
1944
1945 /*
1946 * Enable some error detection, note the instruction error mask
1947 * bit is reserved, so we leave it masked.
1948 */
1949 if (IS_G4X(dev)) {
1950 error_mask = ~(GM45_ERROR_PAGE_TABLE |
1951 GM45_ERROR_MEM_PRIV |
1952 GM45_ERROR_CP_PRIV |
1953 I915_ERROR_MEMORY_REFRESH);
1954 } else {
1955 error_mask = ~(I915_ERROR_PAGE_TABLE |
1956 I915_ERROR_MEMORY_REFRESH);
1957 }
1958 I915_WRITE(EMR, error_mask);
1959
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001960 I915_WRITE(IMR, dev_priv->irq_mask);
Adam Jacksonc496fa12010-05-27 17:26:45 -04001961 I915_WRITE(IER, enable_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001962 POSTING_READ(IER);
Adam Jacksonc496fa12010-05-27 17:26:45 -04001963
1964 if (I915_HAS_HOTPLUG(dev)) {
Jesse Barnes5ca58282009-03-31 14:11:15 -07001965 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
1966
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001967 /* Note HDMI and DP share bits */
1968 if (dev_priv->hotplug_supported_mask & HDMIB_HOTPLUG_INT_STATUS)
1969 hotplug_en |= HDMIB_HOTPLUG_INT_EN;
1970 if (dev_priv->hotplug_supported_mask & HDMIC_HOTPLUG_INT_STATUS)
1971 hotplug_en |= HDMIC_HOTPLUG_INT_EN;
1972 if (dev_priv->hotplug_supported_mask & HDMID_HOTPLUG_INT_STATUS)
1973 hotplug_en |= HDMID_HOTPLUG_INT_EN;
1974 if (dev_priv->hotplug_supported_mask & SDVOC_HOTPLUG_INT_STATUS)
1975 hotplug_en |= SDVOC_HOTPLUG_INT_EN;
1976 if (dev_priv->hotplug_supported_mask & SDVOB_HOTPLUG_INT_STATUS)
1977 hotplug_en |= SDVOB_HOTPLUG_INT_EN;
Andy Lutomirski2d1c9752010-06-12 05:21:18 -04001978 if (dev_priv->hotplug_supported_mask & CRT_HOTPLUG_INT_STATUS) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001979 hotplug_en |= CRT_HOTPLUG_INT_EN;
Andy Lutomirski2d1c9752010-06-12 05:21:18 -04001980
1981 /* Programming the CRT detection parameters tends
1982 to generate a spurious hotplug event about three
1983 seconds later. So just do it once.
1984 */
1985 if (IS_G4X(dev))
1986 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
1987 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
1988 }
1989
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001990 /* Ignore TV since it's buggy */
1991
Jesse Barnes5ca58282009-03-31 14:11:15 -07001992 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
Jesse Barnes5ca58282009-03-31 14:11:15 -07001993 }
1994
Chris Wilson3b617962010-08-24 09:02:58 +01001995 intel_opregion_enable_asle(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001996
1997 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002000static void ironlake_irq_uninstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002001{
2002 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes46979952011-04-07 13:53:55 -07002003
2004 if (!dev_priv)
2005 return;
2006
2007 dev_priv->vblank_pipe = 0;
2008
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002009 I915_WRITE(HWSTAM, 0xffffffff);
2010
2011 I915_WRITE(DEIMR, 0xffffffff);
2012 I915_WRITE(DEIER, 0x0);
2013 I915_WRITE(DEIIR, I915_READ(DEIIR));
2014
2015 I915_WRITE(GTIMR, 0xffffffff);
2016 I915_WRITE(GTIER, 0x0);
2017 I915_WRITE(GTIIR, I915_READ(GTIIR));
2018}
2019
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002020static void i915_driver_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002023 int pipe;
Dave Airlie91e37382006-02-18 15:17:04 +11002024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (!dev_priv)
2026 return;
2027
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002028 dev_priv->vblank_pipe = 0;
2029
Jesse Barnes5ca58282009-03-31 14:11:15 -07002030 if (I915_HAS_HOTPLUG(dev)) {
2031 I915_WRITE(PORT_HOTPLUG_EN, 0);
2032 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2033 }
2034
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002035 I915_WRITE(HWSTAM, 0xffffffff);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002036 for_each_pipe(pipe)
2037 I915_WRITE(PIPESTAT(pipe), 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002038 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07002039 I915_WRITE(IER, 0x0);
Dave Airlie91e37382006-02-18 15:17:04 +11002040
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002041 for_each_pipe(pipe)
2042 I915_WRITE(PIPESTAT(pipe),
2043 I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
Keith Packard7c463582008-11-04 02:03:27 -08002044 I915_WRITE(IIR, I915_READ(IIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002046
2047void intel_irq_init(struct drm_device *dev)
2048{
2049 dev->driver->get_vblank_counter = i915_get_vblank_counter;
2050 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
2051 if (IS_G4X(dev) || IS_GEN5(dev) || IS_GEN6(dev) || IS_IVYBRIDGE(dev)) {
2052 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
2053 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
2054 }
2055
2056
2057 dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
2058 dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
2059
2060 if (IS_IVYBRIDGE(dev)) {
2061 /* Share pre & uninstall handlers with ILK/SNB */
2062 dev->driver->irq_handler = ivybridge_irq_handler;
2063 dev->driver->irq_preinstall = ironlake_irq_preinstall;
2064 dev->driver->irq_postinstall = ivybridge_irq_postinstall;
2065 dev->driver->irq_uninstall = ironlake_irq_uninstall;
2066 dev->driver->enable_vblank = ivybridge_enable_vblank;
2067 dev->driver->disable_vblank = ivybridge_disable_vblank;
2068 } else if (HAS_PCH_SPLIT(dev)) {
2069 dev->driver->irq_handler = ironlake_irq_handler;
2070 dev->driver->irq_preinstall = ironlake_irq_preinstall;
2071 dev->driver->irq_postinstall = ironlake_irq_postinstall;
2072 dev->driver->irq_uninstall = ironlake_irq_uninstall;
2073 dev->driver->enable_vblank = ironlake_enable_vblank;
2074 dev->driver->disable_vblank = ironlake_disable_vblank;
2075 } else {
2076 dev->driver->irq_preinstall = i915_driver_irq_preinstall;
2077 dev->driver->irq_postinstall = i915_driver_irq_postinstall;
2078 dev->driver->irq_uninstall = i915_driver_irq_uninstall;
2079 dev->driver->irq_handler = i915_driver_irq_handler;
2080 dev->driver->enable_vblank = i915_enable_vblank;
2081 dev->driver->disable_vblank = i915_disable_vblank;
2082 }
2083}