blob: 0887581fa650f89fcd1d7d5803fb0bdb319b3580 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "drmP.h"
31#include "drm.h"
32#include "i915_drm.h"
33#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MAX_NOPID ((u32)~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Keith Packard7c463582008-11-04 02:03:27 -080039/**
40 * Interrupts that are always left unmasked.
41 *
42 * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
43 * we leave them always unmasked in IMR and then control enabling them through
44 * PIPESTAT alone.
45 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -070046#define I915_INTERRUPT_ENABLE_FIX (I915_ASLE_INTERRUPT | \
47 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
48 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT | \
49 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Keith Packard7c463582008-11-04 02:03:27 -080050
51/** Interrupts that we mask and unmask at runtime. */
52#define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
53
Jesse Barnes79e53942008-11-07 14:24:08 -080054#define I915_PIPE_VBLANK_STATUS (PIPE_START_VBLANK_INTERRUPT_STATUS |\
55 PIPE_VBLANK_INTERRUPT_STATUS)
56
57#define I915_PIPE_VBLANK_ENABLE (PIPE_START_VBLANK_INTERRUPT_ENABLE |\
58 PIPE_VBLANK_INTERRUPT_ENABLE)
59
60#define DRM_I915_VBLANK_PIPE_ALL (DRM_I915_VBLANK_PIPE_A | \
61 DRM_I915_VBLANK_PIPE_B)
62
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +010063void
Zhenyu Wang036a4a72009-06-08 14:40:19 +080064igdng_enable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
65{
66 if ((dev_priv->gt_irq_mask_reg & mask) != 0) {
67 dev_priv->gt_irq_mask_reg &= ~mask;
68 I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
69 (void) I915_READ(GTIMR);
70 }
71}
72
73static inline void
74igdng_disable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
75{
76 if ((dev_priv->gt_irq_mask_reg & mask) != mask) {
77 dev_priv->gt_irq_mask_reg |= mask;
78 I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
79 (void) I915_READ(GTIMR);
80 }
81}
82
83/* For display hotplug interrupt */
84void
85igdng_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
86{
87 if ((dev_priv->irq_mask_reg & mask) != 0) {
88 dev_priv->irq_mask_reg &= ~mask;
89 I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
90 (void) I915_READ(DEIMR);
91 }
92}
93
94static inline void
95igdng_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
96{
97 if ((dev_priv->irq_mask_reg & mask) != mask) {
98 dev_priv->irq_mask_reg |= mask;
99 I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
100 (void) I915_READ(DEIMR);
101 }
102}
103
104void
Eric Anholted4cb412008-07-29 12:10:39 -0700105i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
106{
107 if ((dev_priv->irq_mask_reg & mask) != 0) {
108 dev_priv->irq_mask_reg &= ~mask;
109 I915_WRITE(IMR, dev_priv->irq_mask_reg);
110 (void) I915_READ(IMR);
111 }
112}
113
114static inline void
115i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
116{
117 if ((dev_priv->irq_mask_reg & mask) != mask) {
118 dev_priv->irq_mask_reg |= mask;
119 I915_WRITE(IMR, dev_priv->irq_mask_reg);
120 (void) I915_READ(IMR);
121 }
122}
123
Keith Packard7c463582008-11-04 02:03:27 -0800124static inline u32
125i915_pipestat(int pipe)
126{
127 if (pipe == 0)
128 return PIPEASTAT;
129 if (pipe == 1)
130 return PIPEBSTAT;
Andrew Morton9c84ba42008-12-01 13:14:08 -0800131 BUG();
Keith Packard7c463582008-11-04 02:03:27 -0800132}
133
134void
135i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
136{
137 if ((dev_priv->pipestat[pipe] & mask) != mask) {
138 u32 reg = i915_pipestat(pipe);
139
140 dev_priv->pipestat[pipe] |= mask;
141 /* Enable the interrupt, clear any pending status */
142 I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
143 (void) I915_READ(reg);
144 }
145}
146
147void
148i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
149{
150 if ((dev_priv->pipestat[pipe] & mask) != 0) {
151 u32 reg = i915_pipestat(pipe);
152
153 dev_priv->pipestat[pipe] &= ~mask;
154 I915_WRITE(reg, dev_priv->pipestat[pipe]);
155 (void) I915_READ(reg);
156 }
157}
158
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000159/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700160 * i915_pipe_enabled - check if a pipe is enabled
161 * @dev: DRM device
162 * @pipe: pipe to check
163 *
164 * Reading certain registers when the pipe is disabled can hang the chip.
165 * Use this routine to make sure the PLL is running and the pipe is active
166 * before reading such registers if unsure.
167 */
168static int
169i915_pipe_enabled(struct drm_device *dev, int pipe)
170{
171 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
172 unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
173
174 if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
175 return 1;
176
177 return 0;
178}
179
Keith Packard42f52ef2008-10-18 19:39:29 -0700180/* Called from drm generic code, passed a 'crtc', which
181 * we use as a pipe index
182 */
183u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700184{
185 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
186 unsigned long high_frame;
187 unsigned long low_frame;
188 u32 high1, high2, low, count;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700189
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700190 high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
191 low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
192
193 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800194 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
195 "pipe %d\n", pipe);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700196 return 0;
197 }
198
199 /*
200 * High & low register fields aren't synchronized, so make sure
201 * we get a low value that's stable across two reads of the high
202 * register.
203 */
204 do {
205 high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
206 PIPE_FRAME_HIGH_SHIFT);
207 low = ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
208 PIPE_FRAME_LOW_SHIFT);
209 high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
210 PIPE_FRAME_HIGH_SHIFT);
211 } while (high1 != high2);
212
213 count = (high1 << 8) | low;
214
215 return count;
216}
217
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800218u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
219{
220 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
221 int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
222
223 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800224 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
225 "pipe %d\n", pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800226 return 0;
227 }
228
229 return I915_READ(reg);
230}
231
Jesse Barnes5ca58282009-03-31 14:11:15 -0700232/*
233 * Handle hotplug events outside the interrupt handler proper.
234 */
235static void i915_hotplug_work_func(struct work_struct *work)
236{
237 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
238 hotplug_work);
239 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700240 struct drm_mode_config *mode_config = &dev->mode_config;
241 struct drm_connector *connector;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700242
Keith Packardc31c4ba2009-05-06 11:48:58 -0700243 if (mode_config->num_connector) {
244 list_for_each_entry(connector, &mode_config->connector_list, head) {
245 struct intel_output *intel_output = to_intel_output(connector);
246
247 if (intel_output->hot_plug)
248 (*intel_output->hot_plug) (intel_output);
249 }
250 }
Jesse Barnes5ca58282009-03-31 14:11:15 -0700251 /* Just fire off a uevent and let userspace tell us what to do */
252 drm_sysfs_hotplug_event(dev);
253}
254
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800255irqreturn_t igdng_irq_handler(struct drm_device *dev)
256{
257 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
258 int ret = IRQ_NONE;
259 u32 de_iir, gt_iir;
260 u32 new_de_iir, new_gt_iir;
261 struct drm_i915_master_private *master_priv;
262
263 de_iir = I915_READ(DEIIR);
264 gt_iir = I915_READ(GTIIR);
265
266 for (;;) {
267 if (de_iir == 0 && gt_iir == 0)
268 break;
269
270 ret = IRQ_HANDLED;
271
272 I915_WRITE(DEIIR, de_iir);
273 new_de_iir = I915_READ(DEIIR);
274 I915_WRITE(GTIIR, gt_iir);
275 new_gt_iir = I915_READ(GTIIR);
276
277 if (dev->primary->master) {
278 master_priv = dev->primary->master->driver_priv;
279 if (master_priv->sarea_priv)
280 master_priv->sarea_priv->last_dispatch =
281 READ_BREADCRUMB(dev_priv);
282 }
283
284 if (gt_iir & GT_USER_INTERRUPT) {
Chris Wilson1c5d22f2009-08-25 11:15:50 +0100285 u32 seqno = i915_get_gem_seqno(dev);
286 dev_priv->mm.irq_gem_seqno = seqno;
287 trace_i915_gem_request_complete(dev, seqno);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800288 DRM_WAKEUP(&dev_priv->irq_queue);
289 }
290
291 de_iir = new_de_iir;
292 gt_iir = new_gt_iir;
293 }
294
295 return ret;
296}
297
Jesse Barnes8a905232009-07-11 16:48:03 -0400298/**
299 * i915_error_work_func - do process context error handling work
300 * @work: work struct
301 *
302 * Fire an error uevent so userspace can see that a hang or error
303 * was detected.
304 */
305static void i915_error_work_func(struct work_struct *work)
306{
307 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
308 error_work);
309 struct drm_device *dev = dev_priv->dev;
Ben Gamarif316a422009-09-14 17:48:46 -0400310 char *error_event[] = { "ERROR=1", NULL };
311 char *reset_event[] = { "RESET=1", NULL };
312 char *reset_done_event[] = { "ERROR=0", NULL };
Jesse Barnes8a905232009-07-11 16:48:03 -0400313
Zhao Yakui44d98a62009-10-09 11:39:40 +0800314 DRM_DEBUG_DRIVER("generating error event\n");
Ben Gamarif316a422009-09-14 17:48:46 -0400315 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -0400316
Ben Gamariba1234d2009-09-14 17:48:47 -0400317 if (atomic_read(&dev_priv->mm.wedged)) {
Ben Gamarif316a422009-09-14 17:48:46 -0400318 if (IS_I965G(dev)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800319 DRM_DEBUG_DRIVER("resetting chip\n");
Ben Gamarif316a422009-09-14 17:48:46 -0400320 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
321 if (!i965_reset(dev, GDRST_RENDER)) {
Ben Gamariba1234d2009-09-14 17:48:47 -0400322 atomic_set(&dev_priv->mm.wedged, 0);
Ben Gamarif316a422009-09-14 17:48:46 -0400323 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
324 }
325 } else {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800326 DRM_DEBUG_DRIVER("reboot required\n");
Ben Gamarif316a422009-09-14 17:48:46 -0400327 }
328 }
Jesse Barnes8a905232009-07-11 16:48:03 -0400329}
330
331/**
332 * i915_capture_error_state - capture an error record for later analysis
333 * @dev: drm device
334 *
335 * Should be called when an error is detected (either a hang or an error
336 * interrupt) to capture error state from the time of the error. Fills
337 * out a structure which becomes available in debugfs for user level tools
338 * to pick up.
339 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700340static void i915_capture_error_state(struct drm_device *dev)
341{
342 struct drm_i915_private *dev_priv = dev->dev_private;
343 struct drm_i915_error_state *error;
344 unsigned long flags;
345
346 spin_lock_irqsave(&dev_priv->error_lock, flags);
347 if (dev_priv->first_error)
348 goto out;
349
350 error = kmalloc(sizeof(*error), GFP_ATOMIC);
351 if (!error) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800352 DRM_DEBUG_DRIVER("out ot memory, not capturing error state\n");
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700353 goto out;
354 }
355
356 error->eir = I915_READ(EIR);
357 error->pgtbl_er = I915_READ(PGTBL_ER);
358 error->pipeastat = I915_READ(PIPEASTAT);
359 error->pipebstat = I915_READ(PIPEBSTAT);
360 error->instpm = I915_READ(INSTPM);
361 if (!IS_I965G(dev)) {
362 error->ipeir = I915_READ(IPEIR);
363 error->ipehr = I915_READ(IPEHR);
364 error->instdone = I915_READ(INSTDONE);
365 error->acthd = I915_READ(ACTHD);
366 } else {
367 error->ipeir = I915_READ(IPEIR_I965);
368 error->ipehr = I915_READ(IPEHR_I965);
369 error->instdone = I915_READ(INSTDONE_I965);
370 error->instps = I915_READ(INSTPS);
371 error->instdone1 = I915_READ(INSTDONE1);
372 error->acthd = I915_READ(ACTHD_I965);
373 }
374
Jesse Barnes8a905232009-07-11 16:48:03 -0400375 do_gettimeofday(&error->time);
376
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700377 dev_priv->first_error = error;
378
379out:
380 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
381}
382
Jesse Barnes8a905232009-07-11 16:48:03 -0400383/**
384 * i915_handle_error - handle an error interrupt
385 * @dev: drm device
386 *
387 * Do some basic checking of regsiter state at error interrupt time and
388 * dump it to the syslog. Also call i915_capture_error_state() to make
389 * sure we get a record and make it available in debugfs. Fire a uevent
390 * so userspace knows something bad happened (should trigger collection
391 * of a ring dump etc.).
392 */
Ben Gamariba1234d2009-09-14 17:48:47 -0400393static void i915_handle_error(struct drm_device *dev, bool wedged)
Jesse Barnes8a905232009-07-11 16:48:03 -0400394{
395 struct drm_i915_private *dev_priv = dev->dev_private;
396 u32 eir = I915_READ(EIR);
397 u32 pipea_stats = I915_READ(PIPEASTAT);
398 u32 pipeb_stats = I915_READ(PIPEBSTAT);
399
400 i915_capture_error_state(dev);
401
402 printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
403 eir);
404
405 if (IS_G4X(dev)) {
406 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
407 u32 ipeir = I915_READ(IPEIR_I965);
408
409 printk(KERN_ERR " IPEIR: 0x%08x\n",
410 I915_READ(IPEIR_I965));
411 printk(KERN_ERR " IPEHR: 0x%08x\n",
412 I915_READ(IPEHR_I965));
413 printk(KERN_ERR " INSTDONE: 0x%08x\n",
414 I915_READ(INSTDONE_I965));
415 printk(KERN_ERR " INSTPS: 0x%08x\n",
416 I915_READ(INSTPS));
417 printk(KERN_ERR " INSTDONE1: 0x%08x\n",
418 I915_READ(INSTDONE1));
419 printk(KERN_ERR " ACTHD: 0x%08x\n",
420 I915_READ(ACTHD_I965));
421 I915_WRITE(IPEIR_I965, ipeir);
422 (void)I915_READ(IPEIR_I965);
423 }
424 if (eir & GM45_ERROR_PAGE_TABLE) {
425 u32 pgtbl_err = I915_READ(PGTBL_ER);
426 printk(KERN_ERR "page table error\n");
427 printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
428 pgtbl_err);
429 I915_WRITE(PGTBL_ER, pgtbl_err);
430 (void)I915_READ(PGTBL_ER);
431 }
432 }
433
434 if (IS_I9XX(dev)) {
435 if (eir & I915_ERROR_PAGE_TABLE) {
436 u32 pgtbl_err = I915_READ(PGTBL_ER);
437 printk(KERN_ERR "page table error\n");
438 printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
439 pgtbl_err);
440 I915_WRITE(PGTBL_ER, pgtbl_err);
441 (void)I915_READ(PGTBL_ER);
442 }
443 }
444
445 if (eir & I915_ERROR_MEMORY_REFRESH) {
446 printk(KERN_ERR "memory refresh error\n");
447 printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
448 pipea_stats);
449 printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
450 pipeb_stats);
451 /* pipestat has already been acked */
452 }
453 if (eir & I915_ERROR_INSTRUCTION) {
454 printk(KERN_ERR "instruction error\n");
455 printk(KERN_ERR " INSTPM: 0x%08x\n",
456 I915_READ(INSTPM));
457 if (!IS_I965G(dev)) {
458 u32 ipeir = I915_READ(IPEIR);
459
460 printk(KERN_ERR " IPEIR: 0x%08x\n",
461 I915_READ(IPEIR));
462 printk(KERN_ERR " IPEHR: 0x%08x\n",
463 I915_READ(IPEHR));
464 printk(KERN_ERR " INSTDONE: 0x%08x\n",
465 I915_READ(INSTDONE));
466 printk(KERN_ERR " ACTHD: 0x%08x\n",
467 I915_READ(ACTHD));
468 I915_WRITE(IPEIR, ipeir);
469 (void)I915_READ(IPEIR);
470 } else {
471 u32 ipeir = I915_READ(IPEIR_I965);
472
473 printk(KERN_ERR " IPEIR: 0x%08x\n",
474 I915_READ(IPEIR_I965));
475 printk(KERN_ERR " IPEHR: 0x%08x\n",
476 I915_READ(IPEHR_I965));
477 printk(KERN_ERR " INSTDONE: 0x%08x\n",
478 I915_READ(INSTDONE_I965));
479 printk(KERN_ERR " INSTPS: 0x%08x\n",
480 I915_READ(INSTPS));
481 printk(KERN_ERR " INSTDONE1: 0x%08x\n",
482 I915_READ(INSTDONE1));
483 printk(KERN_ERR " ACTHD: 0x%08x\n",
484 I915_READ(ACTHD_I965));
485 I915_WRITE(IPEIR_I965, ipeir);
486 (void)I915_READ(IPEIR_I965);
487 }
488 }
489
490 I915_WRITE(EIR, eir);
491 (void)I915_READ(EIR);
492 eir = I915_READ(EIR);
493 if (eir) {
494 /*
495 * some errors might have become stuck,
496 * mask them.
497 */
498 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
499 I915_WRITE(EMR, I915_READ(EMR) | eir);
500 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
501 }
502
Ben Gamariba1234d2009-09-14 17:48:47 -0400503 if (wedged) {
504 atomic_set(&dev_priv->mm.wedged, 1);
505
Ben Gamari11ed50e2009-09-14 17:48:45 -0400506 /*
507 * Wakeup waiting processes so they don't hang
508 */
509 printk("i915: Waking up sleeping processes\n");
510 DRM_WAKEUP(&dev_priv->irq_queue);
511 }
512
Eric Anholt9c9fe1f2009-08-03 16:09:16 -0700513 queue_work(dev_priv->wq, &dev_priv->error_work);
Jesse Barnes8a905232009-07-11 16:48:03 -0400514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
517{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000518 struct drm_device *dev = (struct drm_device *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000520 struct drm_i915_master_private *master_priv;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800521 u32 iir, new_iir;
522 u32 pipea_stats, pipeb_stats;
Keith Packard05eff842008-11-19 14:03:05 -0800523 u32 vblank_status;
524 u32 vblank_enable;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700525 int vblank = 0;
Keith Packard7c463582008-11-04 02:03:27 -0800526 unsigned long irqflags;
Keith Packard05eff842008-11-19 14:03:05 -0800527 int irq_received;
528 int ret = IRQ_NONE;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000529
Eric Anholt630681d2008-10-06 15:14:12 -0700530 atomic_inc(&dev_priv->irq_received);
531
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800532 if (IS_IGDNG(dev))
533 return igdng_irq_handler(dev);
534
Eric Anholted4cb412008-07-29 12:10:39 -0700535 iir = I915_READ(IIR);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000536
Keith Packard05eff842008-11-19 14:03:05 -0800537 if (IS_I965G(dev)) {
538 vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
539 vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
540 } else {
541 vblank_status = I915_VBLANK_INTERRUPT_STATUS;
542 vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Keith Packard05eff842008-11-19 14:03:05 -0800545 for (;;) {
546 irq_received = iir != 0;
547
548 /* Can't rely on pipestat interrupt bit in iir as it might
549 * have been cleared after the pipestat interrupt was received.
550 * It doesn't set the bit in iir again, but it still produces
551 * interrupts (for non-MSI).
552 */
553 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
554 pipea_stats = I915_READ(PIPEASTAT);
555 pipeb_stats = I915_READ(PIPEBSTAT);
Jesse Barnes79e53942008-11-07 14:24:08 -0800556
Jesse Barnes8a905232009-07-11 16:48:03 -0400557 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Ben Gamariba1234d2009-09-14 17:48:47 -0400558 i915_handle_error(dev, false);
Jesse Barnes8a905232009-07-11 16:48:03 -0400559
Eric Anholtcdfbc412008-11-04 15:50:30 -0800560 /*
561 * Clear the PIPE(A|B)STAT regs before the IIR
562 */
Keith Packard05eff842008-11-19 14:03:05 -0800563 if (pipea_stats & 0x8000ffff) {
Shaohua Li7662c8b2009-06-26 11:23:55 +0800564 if (pipea_stats & PIPE_FIFO_UNDERRUN_STATUS)
Zhao Yakui44d98a62009-10-09 11:39:40 +0800565 DRM_DEBUG_DRIVER("pipe a underrun\n");
Eric Anholtcdfbc412008-11-04 15:50:30 -0800566 I915_WRITE(PIPEASTAT, pipea_stats);
Keith Packard05eff842008-11-19 14:03:05 -0800567 irq_received = 1;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800568 }
Keith Packard7c463582008-11-04 02:03:27 -0800569
Keith Packard05eff842008-11-19 14:03:05 -0800570 if (pipeb_stats & 0x8000ffff) {
Shaohua Li7662c8b2009-06-26 11:23:55 +0800571 if (pipeb_stats & PIPE_FIFO_UNDERRUN_STATUS)
Zhao Yakui44d98a62009-10-09 11:39:40 +0800572 DRM_DEBUG_DRIVER("pipe b underrun\n");
Eric Anholtcdfbc412008-11-04 15:50:30 -0800573 I915_WRITE(PIPEBSTAT, pipeb_stats);
Keith Packard05eff842008-11-19 14:03:05 -0800574 irq_received = 1;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800575 }
Keith Packard05eff842008-11-19 14:03:05 -0800576 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
577
578 if (!irq_received)
579 break;
580
581 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Jesse Barnes5ca58282009-03-31 14:11:15 -0700583 /* Consume port. Then clear IIR or we'll miss events */
584 if ((I915_HAS_HOTPLUG(dev)) &&
585 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
586 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
587
Zhao Yakui44d98a62009-10-09 11:39:40 +0800588 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
Jesse Barnes5ca58282009-03-31 14:11:15 -0700589 hotplug_status);
590 if (hotplug_status & dev_priv->hotplug_supported_mask)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -0700591 queue_work(dev_priv->wq,
592 &dev_priv->hotplug_work);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700593
594 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
595 I915_READ(PORT_HOTPLUG_STAT);
Shaohua Li04302962009-08-24 10:25:23 +0800596
597 /* EOS interrupts occurs */
598 if (IS_IGD(dev) &&
599 (hotplug_status & CRT_EOS_INT_STATUS)) {
600 u32 temp;
601
Zhao Yakui44d98a62009-10-09 11:39:40 +0800602 DRM_DEBUG_DRIVER("EOS interrupt occurs\n");
Shaohua Li04302962009-08-24 10:25:23 +0800603 /* status is already cleared */
604 temp = I915_READ(ADPA);
605 temp &= ~ADPA_DAC_ENABLE;
606 I915_WRITE(ADPA, temp);
607
608 temp = I915_READ(PORT_HOTPLUG_EN);
609 temp &= ~CRT_EOS_INT_EN;
610 I915_WRITE(PORT_HOTPLUG_EN, temp);
611
612 temp = I915_READ(PORT_HOTPLUG_STAT);
613 if (temp & CRT_EOS_INT_STATUS)
614 I915_WRITE(PORT_HOTPLUG_STAT,
615 CRT_EOS_INT_STATUS);
616 }
Jesse Barnes5ca58282009-03-31 14:11:15 -0700617 }
618
Eric Anholtcdfbc412008-11-04 15:50:30 -0800619 I915_WRITE(IIR, iir);
620 new_iir = I915_READ(IIR); /* Flush posted writes */
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100621
Dave Airlie7c1c2872008-11-28 14:22:24 +1000622 if (dev->primary->master) {
623 master_priv = dev->primary->master->driver_priv;
624 if (master_priv->sarea_priv)
625 master_priv->sarea_priv->last_dispatch =
626 READ_BREADCRUMB(dev_priv);
627 }
Keith Packard7c463582008-11-04 02:03:27 -0800628
Eric Anholtcdfbc412008-11-04 15:50:30 -0800629 if (iir & I915_USER_INTERRUPT) {
Chris Wilson1c5d22f2009-08-25 11:15:50 +0100630 u32 seqno = i915_get_gem_seqno(dev);
631 dev_priv->mm.irq_gem_seqno = seqno;
632 trace_i915_gem_request_complete(dev, seqno);
Eric Anholtcdfbc412008-11-04 15:50:30 -0800633 DRM_WAKEUP(&dev_priv->irq_queue);
Ben Gamarif65d9422009-09-14 17:48:44 -0400634 dev_priv->hangcheck_count = 0;
635 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
Eric Anholtcdfbc412008-11-04 15:50:30 -0800636 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700637
Keith Packard05eff842008-11-19 14:03:05 -0800638 if (pipea_stats & vblank_status) {
Eric Anholtcdfbc412008-11-04 15:50:30 -0800639 vblank++;
640 drm_handle_vblank(dev, 0);
641 }
Eric Anholt673a3942008-07-30 12:06:12 -0700642
Keith Packard05eff842008-11-19 14:03:05 -0800643 if (pipeb_stats & vblank_status) {
Eric Anholtcdfbc412008-11-04 15:50:30 -0800644 vblank++;
645 drm_handle_vblank(dev, 1);
646 }
Keith Packard7c463582008-11-04 02:03:27 -0800647
Eric Anholtcdfbc412008-11-04 15:50:30 -0800648 if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
649 (iir & I915_ASLE_INTERRUPT))
650 opregion_asle_intr(dev);
Keith Packard7c463582008-11-04 02:03:27 -0800651
Eric Anholtcdfbc412008-11-04 15:50:30 -0800652 /* With MSI, interrupts are only generated when iir
653 * transitions from zero to nonzero. If another bit got
654 * set while we were handling the existing iir bits, then
655 * we would never get another interrupt.
656 *
657 * This is fine on non-MSI as well, as if we hit this path
658 * we avoid exiting the interrupt handler only to generate
659 * another one.
660 *
661 * Note that for MSI this could cause a stray interrupt report
662 * if an interrupt landed in the time between writing IIR and
663 * the posting read. This should be rare enough to never
664 * trigger the 99% of 100,000 interrupts test for disabling
665 * stray interrupts.
666 */
667 iir = new_iir;
Keith Packard05eff842008-11-19 14:03:05 -0800668 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700669
Keith Packard05eff842008-11-19 14:03:05 -0800670 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Dave Airlieaf6061a2008-05-07 12:15:39 +1000673static int i915_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000676 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 RING_LOCALS;
678
679 i915_kernel_lost_context(dev);
680
Zhao Yakui44d98a62009-10-09 11:39:40 +0800681 DRM_DEBUG_DRIVER("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400683 dev_priv->counter++;
Alan Hourihanec29b6692006-08-12 16:29:24 +1000684 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400685 dev_priv->counter = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000686 if (master_priv->sarea_priv)
687 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Alan Hourihanec29b6692006-08-12 16:29:24 +1000688
Keith Packard0baf8232008-11-08 11:44:14 +1000689 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700690 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000691 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000692 OUT_RING(dev_priv->counter);
Jesse Barnes585fb112008-07-29 11:54:06 -0700693 OUT_RING(MI_USER_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 ADVANCE_LP_RING();
Dave Airliebc5f4522007-11-05 12:50:58 +1000695
Alan Hourihanec29b6692006-08-12 16:29:24 +1000696 return dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Eric Anholt673a3942008-07-30 12:06:12 -0700699void i915_user_irq_get(struct drm_device *dev)
Eric Anholted4cb412008-07-29 12:10:39 -0700700{
701 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700702 unsigned long irqflags;
Eric Anholted4cb412008-07-29 12:10:39 -0700703
Keith Packarde9d21d72008-10-16 11:31:38 -0700704 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800705 if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
706 if (IS_IGDNG(dev))
707 igdng_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
708 else
709 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
710 }
Keith Packarde9d21d72008-10-16 11:31:38 -0700711 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700712}
713
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700714void i915_user_irq_put(struct drm_device *dev)
Eric Anholted4cb412008-07-29 12:10:39 -0700715{
716 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700717 unsigned long irqflags;
Eric Anholted4cb412008-07-29 12:10:39 -0700718
Keith Packarde9d21d72008-10-16 11:31:38 -0700719 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700720 BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800721 if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
722 if (IS_IGDNG(dev))
723 igdng_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
724 else
725 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
726 }
Keith Packarde9d21d72008-10-16 11:31:38 -0700727 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700728}
729
Chris Wilson9d34e5d2009-09-24 05:26:06 +0100730void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
731{
732 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
733
734 if (dev_priv->trace_irq_seqno == 0)
735 i915_user_irq_get(dev);
736
737 dev_priv->trace_irq_seqno = seqno;
738}
739
Dave Airlie84b1fd12007-07-11 15:53:27 +1000740static int i915_wait_irq(struct drm_device * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000743 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int ret = 0;
745
Zhao Yakui44d98a62009-10-09 11:39:40 +0800746 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 READ_BREADCRUMB(dev_priv));
748
Eric Anholted4cb412008-07-29 12:10:39 -0700749 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000750 if (master_priv->sarea_priv)
751 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return 0;
Eric Anholted4cb412008-07-29 12:10:39 -0700753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Dave Airlie7c1c2872008-11-28 14:22:24 +1000755 if (master_priv->sarea_priv)
756 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Eric Anholted4cb412008-07-29 12:10:39 -0700758 i915_user_irq_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
760 READ_BREADCRUMB(dev_priv) >= irq_nr);
Eric Anholted4cb412008-07-29 12:10:39 -0700761 i915_user_irq_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Eric Anholt20caafa2007-08-25 19:22:43 +1000763 if (ret == -EBUSY) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000764 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
766 }
767
Dave Airlieaf6061a2008-05-07 12:15:39 +1000768 return ret;
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771/* Needs the lock as it touches the ring.
772 */
Eric Anholtc153f452007-09-03 12:06:45 +1000773int i915_irq_emit(struct drm_device *dev, void *data,
774 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000777 drm_i915_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int result;
779
Eric Anholt07f4f8b2009-04-16 13:46:12 -0700780 if (!dev_priv || !dev_priv->ring.virtual_start) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000781 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000782 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Eric Anholt299eb932009-02-24 22:14:12 -0800784
785 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
786
Eric Anholt546b0972008-09-01 16:45:29 -0700787 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 result = i915_emit_irq(dev);
Eric Anholt546b0972008-09-01 16:45:29 -0700789 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Eric Anholtc153f452007-09-03 12:06:45 +1000791 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000793 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
796 return 0;
797}
798
799/* Doesn't need the hardware lock.
800 */
Eric Anholtc153f452007-09-03 12:06:45 +1000801int i915_irq_wait(struct drm_device *dev, void *data,
802 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000805 drm_i915_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000808 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000809 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
Eric Anholtc153f452007-09-03 12:06:45 +1000812 return i915_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
Keith Packard42f52ef2008-10-18 19:39:29 -0700815/* Called from drm generic code, passed 'crtc' which
816 * we use as a pipe index
817 */
818int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700819{
820 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700821 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -0800822 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
823 u32 pipeconf;
824
825 pipeconf = I915_READ(pipeconf_reg);
826 if (!(pipeconf & PIPEACONF_ENABLE))
827 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700828
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800829 if (IS_IGDNG(dev))
830 return 0;
831
Keith Packarde9d21d72008-10-16 11:31:38 -0700832 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Keith Packarde9d21d72008-10-16 11:31:38 -0700833 if (IS_I965G(dev))
Keith Packard7c463582008-11-04 02:03:27 -0800834 i915_enable_pipestat(dev_priv, pipe,
835 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700836 else
Keith Packard7c463582008-11-04 02:03:27 -0800837 i915_enable_pipestat(dev_priv, pipe,
838 PIPE_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700839 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700840 return 0;
841}
842
Keith Packard42f52ef2008-10-18 19:39:29 -0700843/* Called from drm generic code, passed 'crtc' which
844 * we use as a pipe index
845 */
846void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700847{
848 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700849 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700850
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800851 if (IS_IGDNG(dev))
852 return;
853
Keith Packarde9d21d72008-10-16 11:31:38 -0700854 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Keith Packard7c463582008-11-04 02:03:27 -0800855 i915_disable_pipestat(dev_priv, pipe,
856 PIPE_VBLANK_INTERRUPT_ENABLE |
857 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700858 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700859}
860
Jesse Barnes79e53942008-11-07 14:24:08 -0800861void i915_enable_interrupt (struct drm_device *dev)
862{
863 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wange170b032009-06-05 15:38:40 +0800864
865 if (!IS_IGDNG(dev))
866 opregion_enable_asle(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800867 dev_priv->irq_enabled = 1;
868}
869
870
Dave Airlie702880f2006-06-24 17:07:34 +1000871/* Set the vblank monitor pipe
872 */
Eric Anholtc153f452007-09-03 12:06:45 +1000873int i915_vblank_pipe_set(struct drm_device *dev, void *data,
874 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000875{
Dave Airlie702880f2006-06-24 17:07:34 +1000876 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie702880f2006-06-24 17:07:34 +1000877
878 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000879 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000880 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000881 }
882
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +1000883 return 0;
Dave Airlie702880f2006-06-24 17:07:34 +1000884}
885
Eric Anholtc153f452007-09-03 12:06:45 +1000886int i915_vblank_pipe_get(struct drm_device *dev, void *data,
887 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000888{
Dave Airlie702880f2006-06-24 17:07:34 +1000889 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000890 drm_i915_vblank_pipe_t *pipe = data;
Dave Airlie702880f2006-06-24 17:07:34 +1000891
892 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000893 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000894 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000895 }
896
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700897 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Eric Anholtc153f452007-09-03 12:06:45 +1000898
Dave Airlie702880f2006-06-24 17:07:34 +1000899 return 0;
900}
901
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000902/**
903 * Schedule buffer swap at given vertical blank.
904 */
Eric Anholtc153f452007-09-03 12:06:45 +1000905int i915_vblank_swap(struct drm_device *dev, void *data,
906 struct drm_file *file_priv)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000907{
Eric Anholtbd95e0a2008-11-04 12:01:24 -0800908 /* The delayed swap mechanism was fundamentally racy, and has been
909 * removed. The model was that the client requested a delayed flip/swap
910 * from the kernel, then waited for vblank before continuing to perform
911 * rendering. The problem was that the kernel might wake the client
912 * up before it dispatched the vblank swap (since the lock has to be
913 * held while touching the ringbuffer), in which case the client would
914 * clear and start the next frame before the swap occurred, and
915 * flicker would occur in addition to likely missing the vblank.
916 *
917 * In the absence of this ioctl, userland falls back to a correct path
918 * of waiting for a vblank, then dispatching the swap on its own.
919 * Context switching to userland and back is plenty fast enough for
920 * meeting the requirements of vblank swapping.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700921 */
Eric Anholtbd95e0a2008-11-04 12:01:24 -0800922 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000923}
924
Ben Gamarif65d9422009-09-14 17:48:44 -0400925struct drm_i915_gem_request *i915_get_tail_request(struct drm_device *dev) {
926 drm_i915_private_t *dev_priv = dev->dev_private;
927 return list_entry(dev_priv->mm.request_list.prev, struct drm_i915_gem_request, list);
928}
929
930/**
931 * This is called when the chip hasn't reported back with completed
932 * batchbuffers in a long time. The first time this is called we simply record
933 * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
934 * again, we assume the chip is wedged and try to fix it.
935 */
936void i915_hangcheck_elapsed(unsigned long data)
937{
938 struct drm_device *dev = (struct drm_device *)data;
939 drm_i915_private_t *dev_priv = dev->dev_private;
940 uint32_t acthd;
941
942 if (!IS_I965G(dev))
943 acthd = I915_READ(ACTHD);
944 else
945 acthd = I915_READ(ACTHD_I965);
946
947 /* If all work is done then ACTHD clearly hasn't advanced. */
948 if (list_empty(&dev_priv->mm.request_list) ||
949 i915_seqno_passed(i915_get_gem_seqno(dev), i915_get_tail_request(dev)->seqno)) {
950 dev_priv->hangcheck_count = 0;
951 return;
952 }
953
954 if (dev_priv->last_acthd == acthd && dev_priv->hangcheck_count > 0) {
955 DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
Ben Gamariba1234d2009-09-14 17:48:47 -0400956 i915_handle_error(dev, true);
Ben Gamarif65d9422009-09-14 17:48:44 -0400957 return;
958 }
959
960 /* Reset timer case chip hangs without another request being added */
961 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
962
963 if (acthd != dev_priv->last_acthd)
964 dev_priv->hangcheck_count = 0;
965 else
966 dev_priv->hangcheck_count++;
967
968 dev_priv->last_acthd = acthd;
969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/* drm_dma.h hooks
972*/
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800973static void igdng_irq_preinstall(struct drm_device *dev)
974{
975 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
976
977 I915_WRITE(HWSTAM, 0xeffe);
978
979 /* XXX hotplug from PCH */
980
981 I915_WRITE(DEIMR, 0xffffffff);
982 I915_WRITE(DEIER, 0x0);
983 (void) I915_READ(DEIER);
984
985 /* and GT */
986 I915_WRITE(GTIMR, 0xffffffff);
987 I915_WRITE(GTIER, 0x0);
988 (void) I915_READ(GTIER);
989}
990
991static int igdng_irq_postinstall(struct drm_device *dev)
992{
993 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
994 /* enable kind of interrupts always enabled */
995 u32 display_mask = DE_MASTER_IRQ_CONTROL /*| DE_PCH_EVENT */;
996 u32 render_mask = GT_USER_INTERRUPT;
997
998 dev_priv->irq_mask_reg = ~display_mask;
999 dev_priv->de_irq_enable_reg = display_mask;
1000
1001 /* should always can generate irq */
1002 I915_WRITE(DEIIR, I915_READ(DEIIR));
1003 I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
1004 I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
1005 (void) I915_READ(DEIER);
1006
1007 /* user interrupt should be enabled, but masked initial */
1008 dev_priv->gt_irq_mask_reg = 0xffffffff;
1009 dev_priv->gt_irq_enable_reg = render_mask;
1010
1011 I915_WRITE(GTIIR, I915_READ(GTIIR));
1012 I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
1013 I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
1014 (void) I915_READ(GTIER);
1015
1016 return 0;
1017}
1018
Dave Airlie84b1fd12007-07-11 15:53:27 +10001019void i915_driver_irq_preinstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1022
Jesse Barnes79e53942008-11-07 14:24:08 -08001023 atomic_set(&dev_priv->irq_received, 0);
1024
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001025 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Jesse Barnes8a905232009-07-11 16:48:03 -04001026 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001027
1028 if (IS_IGDNG(dev)) {
1029 igdng_irq_preinstall(dev);
1030 return;
1031 }
1032
Jesse Barnes5ca58282009-03-31 14:11:15 -07001033 if (I915_HAS_HOTPLUG(dev)) {
1034 I915_WRITE(PORT_HOTPLUG_EN, 0);
1035 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1036 }
1037
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001038 I915_WRITE(HWSTAM, 0xeffe);
Keith Packard7c463582008-11-04 02:03:27 -08001039 I915_WRITE(PIPEASTAT, 0);
1040 I915_WRITE(PIPEBSTAT, 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001041 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07001042 I915_WRITE(IER, 0x0);
Keith Packard7c463582008-11-04 02:03:27 -08001043 (void) I915_READ(IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001046int i915_driver_irq_postinstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes5ca58282009-03-31 14:11:15 -07001049 u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001050 u32 error_mask;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001051
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001052 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
1053
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001054 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001055
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001056 if (IS_IGDNG(dev))
1057 return igdng_irq_postinstall(dev);
1058
Keith Packard7c463582008-11-04 02:03:27 -08001059 /* Unmask the interrupts that we always want on. */
1060 dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001061
Keith Packard7c463582008-11-04 02:03:27 -08001062 dev_priv->pipestat[0] = 0;
1063 dev_priv->pipestat[1] = 0;
1064
Jesse Barnes5ca58282009-03-31 14:11:15 -07001065 if (I915_HAS_HOTPLUG(dev)) {
1066 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
1067
1068 /* Leave other bits alone */
1069 hotplug_en |= HOTPLUG_EN_MASK;
1070 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
1071
1072 dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
1073 TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
1074 SDVOB_HOTPLUG_INT_STATUS;
1075 if (IS_G4X(dev)) {
1076 dev_priv->hotplug_supported_mask |=
1077 HDMIB_HOTPLUG_INT_STATUS |
1078 HDMIC_HOTPLUG_INT_STATUS |
1079 HDMID_HOTPLUG_INT_STATUS;
1080 }
1081 /* Enable in IER... */
1082 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1083 /* and unmask in IMR */
1084 i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
1085 }
1086
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001087 /*
1088 * Enable some error detection, note the instruction error mask
1089 * bit is reserved, so we leave it masked.
1090 */
1091 if (IS_G4X(dev)) {
1092 error_mask = ~(GM45_ERROR_PAGE_TABLE |
1093 GM45_ERROR_MEM_PRIV |
1094 GM45_ERROR_CP_PRIV |
1095 I915_ERROR_MEMORY_REFRESH);
1096 } else {
1097 error_mask = ~(I915_ERROR_PAGE_TABLE |
1098 I915_ERROR_MEMORY_REFRESH);
1099 }
1100 I915_WRITE(EMR, error_mask);
1101
Keith Packard7c463582008-11-04 02:03:27 -08001102 /* Disable pipe interrupt enables, clear pending pipe status */
1103 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
1104 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
1105 /* Clear pending interrupt status */
1106 I915_WRITE(IIR, I915_READ(IIR));
1107
Jesse Barnes5ca58282009-03-31 14:11:15 -07001108 I915_WRITE(IER, enable_mask);
Keith Packard7c463582008-11-04 02:03:27 -08001109 I915_WRITE(IMR, dev_priv->irq_mask_reg);
Eric Anholted4cb412008-07-29 12:10:39 -07001110 (void) I915_READ(IER);
1111
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001112 opregion_enable_asle(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001113
1114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001117static void igdng_irq_uninstall(struct drm_device *dev)
1118{
1119 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1120 I915_WRITE(HWSTAM, 0xffffffff);
1121
1122 I915_WRITE(DEIMR, 0xffffffff);
1123 I915_WRITE(DEIER, 0x0);
1124 I915_WRITE(DEIIR, I915_READ(DEIIR));
1125
1126 I915_WRITE(GTIMR, 0xffffffff);
1127 I915_WRITE(GTIER, 0x0);
1128 I915_WRITE(GTIIR, I915_READ(GTIIR));
1129}
1130
Dave Airlie84b1fd12007-07-11 15:53:27 +10001131void i915_driver_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie91e37382006-02-18 15:17:04 +11001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (!dev_priv)
1136 return;
1137
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001138 dev_priv->vblank_pipe = 0;
1139
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001140 if (IS_IGDNG(dev)) {
1141 igdng_irq_uninstall(dev);
1142 return;
1143 }
1144
Jesse Barnes5ca58282009-03-31 14:11:15 -07001145 if (I915_HAS_HOTPLUG(dev)) {
1146 I915_WRITE(PORT_HOTPLUG_EN, 0);
1147 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1148 }
1149
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001150 I915_WRITE(HWSTAM, 0xffffffff);
Keith Packard7c463582008-11-04 02:03:27 -08001151 I915_WRITE(PIPEASTAT, 0);
1152 I915_WRITE(PIPEBSTAT, 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001153 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07001154 I915_WRITE(IER, 0x0);
Dave Airlie91e37382006-02-18 15:17:04 +11001155
Keith Packard7c463582008-11-04 02:03:27 -08001156 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
1157 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
1158 I915_WRITE(IIR, I915_READ(IIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}