blob: ce337be4bbcd3a256817965ce76eb0eb7d959c75 [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/**
Zhao Yakui01c66882009-10-28 05:10:00 +0000160 * intel_enable_asle - enable ASLE interrupt for OpRegion
161 */
162void intel_enable_asle (struct drm_device *dev)
163{
164 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
165
166 if (IS_IGDNG(dev))
167 igdng_enable_display_irq(dev_priv, DE_GSE);
168 else
169 i915_enable_pipestat(dev_priv, 1,
170 I915_LEGACY_BLC_EVENT_ENABLE);
171}
172
173/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700174 * i915_pipe_enabled - check if a pipe is enabled
175 * @dev: DRM device
176 * @pipe: pipe to check
177 *
178 * Reading certain registers when the pipe is disabled can hang the chip.
179 * Use this routine to make sure the PLL is running and the pipe is active
180 * before reading such registers if unsure.
181 */
182static int
183i915_pipe_enabled(struct drm_device *dev, int pipe)
184{
185 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
186 unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
187
188 if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
189 return 1;
190
191 return 0;
192}
193
Keith Packard42f52ef2008-10-18 19:39:29 -0700194/* Called from drm generic code, passed a 'crtc', which
195 * we use as a pipe index
196 */
197u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700198{
199 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
200 unsigned long high_frame;
201 unsigned long low_frame;
202 u32 high1, high2, low, count;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700203
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700204 high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
205 low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
206
207 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800208 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
209 "pipe %d\n", pipe);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700210 return 0;
211 }
212
213 /*
214 * High & low register fields aren't synchronized, so make sure
215 * we get a low value that's stable across two reads of the high
216 * register.
217 */
218 do {
219 high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
220 PIPE_FRAME_HIGH_SHIFT);
221 low = ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
222 PIPE_FRAME_LOW_SHIFT);
223 high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
224 PIPE_FRAME_HIGH_SHIFT);
225 } while (high1 != high2);
226
227 count = (high1 << 8) | low;
228
229 return count;
230}
231
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800232u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
233{
234 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
235 int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
236
237 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800238 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
239 "pipe %d\n", pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800240 return 0;
241 }
242
243 return I915_READ(reg);
244}
245
Jesse Barnes5ca58282009-03-31 14:11:15 -0700246/*
247 * Handle hotplug events outside the interrupt handler proper.
248 */
249static void i915_hotplug_work_func(struct work_struct *work)
250{
251 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
252 hotplug_work);
253 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700254 struct drm_mode_config *mode_config = &dev->mode_config;
255 struct drm_connector *connector;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700256
Keith Packardc31c4ba2009-05-06 11:48:58 -0700257 if (mode_config->num_connector) {
258 list_for_each_entry(connector, &mode_config->connector_list, head) {
259 struct intel_output *intel_output = to_intel_output(connector);
260
261 if (intel_output->hot_plug)
262 (*intel_output->hot_plug) (intel_output);
263 }
264 }
Jesse Barnes5ca58282009-03-31 14:11:15 -0700265 /* Just fire off a uevent and let userspace tell us what to do */
266 drm_sysfs_hotplug_event(dev);
267}
268
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800269irqreturn_t igdng_irq_handler(struct drm_device *dev)
270{
271 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
272 int ret = IRQ_NONE;
273 u32 de_iir, gt_iir;
274 u32 new_de_iir, new_gt_iir;
275 struct drm_i915_master_private *master_priv;
276
277 de_iir = I915_READ(DEIIR);
278 gt_iir = I915_READ(GTIIR);
279
280 for (;;) {
281 if (de_iir == 0 && gt_iir == 0)
282 break;
283
284 ret = IRQ_HANDLED;
285
286 I915_WRITE(DEIIR, de_iir);
287 new_de_iir = I915_READ(DEIIR);
288 I915_WRITE(GTIIR, gt_iir);
289 new_gt_iir = I915_READ(GTIIR);
290
291 if (dev->primary->master) {
292 master_priv = dev->primary->master->driver_priv;
293 if (master_priv->sarea_priv)
294 master_priv->sarea_priv->last_dispatch =
295 READ_BREADCRUMB(dev_priv);
296 }
297
298 if (gt_iir & GT_USER_INTERRUPT) {
Chris Wilson1c5d22f2009-08-25 11:15:50 +0100299 u32 seqno = i915_get_gem_seqno(dev);
300 dev_priv->mm.irq_gem_seqno = seqno;
301 trace_i915_gem_request_complete(dev, seqno);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800302 DRM_WAKEUP(&dev_priv->irq_queue);
303 }
304
Zhao Yakui01c66882009-10-28 05:10:00 +0000305 if (de_iir & DE_GSE)
306 ironlake_opregion_gse_intr(dev);
307
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800308 de_iir = new_de_iir;
309 gt_iir = new_gt_iir;
310 }
311
312 return ret;
313}
314
Jesse Barnes8a905232009-07-11 16:48:03 -0400315/**
316 * i915_error_work_func - do process context error handling work
317 * @work: work struct
318 *
319 * Fire an error uevent so userspace can see that a hang or error
320 * was detected.
321 */
322static void i915_error_work_func(struct work_struct *work)
323{
324 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
325 error_work);
326 struct drm_device *dev = dev_priv->dev;
Ben Gamarif316a422009-09-14 17:48:46 -0400327 char *error_event[] = { "ERROR=1", NULL };
328 char *reset_event[] = { "RESET=1", NULL };
329 char *reset_done_event[] = { "ERROR=0", NULL };
Jesse Barnes8a905232009-07-11 16:48:03 -0400330
Zhao Yakui44d98a62009-10-09 11:39:40 +0800331 DRM_DEBUG_DRIVER("generating error event\n");
Ben Gamarif316a422009-09-14 17:48:46 -0400332 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -0400333
Ben Gamariba1234d2009-09-14 17:48:47 -0400334 if (atomic_read(&dev_priv->mm.wedged)) {
Ben Gamarif316a422009-09-14 17:48:46 -0400335 if (IS_I965G(dev)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800336 DRM_DEBUG_DRIVER("resetting chip\n");
Ben Gamarif316a422009-09-14 17:48:46 -0400337 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
338 if (!i965_reset(dev, GDRST_RENDER)) {
Ben Gamariba1234d2009-09-14 17:48:47 -0400339 atomic_set(&dev_priv->mm.wedged, 0);
Ben Gamarif316a422009-09-14 17:48:46 -0400340 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
341 }
342 } else {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800343 DRM_DEBUG_DRIVER("reboot required\n");
Ben Gamarif316a422009-09-14 17:48:46 -0400344 }
345 }
Jesse Barnes8a905232009-07-11 16:48:03 -0400346}
347
348/**
349 * i915_capture_error_state - capture an error record for later analysis
350 * @dev: drm device
351 *
352 * Should be called when an error is detected (either a hang or an error
353 * interrupt) to capture error state from the time of the error. Fills
354 * out a structure which becomes available in debugfs for user level tools
355 * to pick up.
356 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700357static void i915_capture_error_state(struct drm_device *dev)
358{
359 struct drm_i915_private *dev_priv = dev->dev_private;
360 struct drm_i915_error_state *error;
361 unsigned long flags;
362
363 spin_lock_irqsave(&dev_priv->error_lock, flags);
364 if (dev_priv->first_error)
365 goto out;
366
367 error = kmalloc(sizeof(*error), GFP_ATOMIC);
368 if (!error) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800369 DRM_DEBUG_DRIVER("out ot memory, not capturing error state\n");
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700370 goto out;
371 }
372
373 error->eir = I915_READ(EIR);
374 error->pgtbl_er = I915_READ(PGTBL_ER);
375 error->pipeastat = I915_READ(PIPEASTAT);
376 error->pipebstat = I915_READ(PIPEBSTAT);
377 error->instpm = I915_READ(INSTPM);
378 if (!IS_I965G(dev)) {
379 error->ipeir = I915_READ(IPEIR);
380 error->ipehr = I915_READ(IPEHR);
381 error->instdone = I915_READ(INSTDONE);
382 error->acthd = I915_READ(ACTHD);
383 } else {
384 error->ipeir = I915_READ(IPEIR_I965);
385 error->ipehr = I915_READ(IPEHR_I965);
386 error->instdone = I915_READ(INSTDONE_I965);
387 error->instps = I915_READ(INSTPS);
388 error->instdone1 = I915_READ(INSTDONE1);
389 error->acthd = I915_READ(ACTHD_I965);
390 }
391
Jesse Barnes8a905232009-07-11 16:48:03 -0400392 do_gettimeofday(&error->time);
393
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700394 dev_priv->first_error = error;
395
396out:
397 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
398}
399
Jesse Barnes8a905232009-07-11 16:48:03 -0400400/**
401 * i915_handle_error - handle an error interrupt
402 * @dev: drm device
403 *
404 * Do some basic checking of regsiter state at error interrupt time and
405 * dump it to the syslog. Also call i915_capture_error_state() to make
406 * sure we get a record and make it available in debugfs. Fire a uevent
407 * so userspace knows something bad happened (should trigger collection
408 * of a ring dump etc.).
409 */
Ben Gamariba1234d2009-09-14 17:48:47 -0400410static void i915_handle_error(struct drm_device *dev, bool wedged)
Jesse Barnes8a905232009-07-11 16:48:03 -0400411{
412 struct drm_i915_private *dev_priv = dev->dev_private;
413 u32 eir = I915_READ(EIR);
414 u32 pipea_stats = I915_READ(PIPEASTAT);
415 u32 pipeb_stats = I915_READ(PIPEBSTAT);
416
417 i915_capture_error_state(dev);
418
419 printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
420 eir);
421
422 if (IS_G4X(dev)) {
423 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
424 u32 ipeir = I915_READ(IPEIR_I965);
425
426 printk(KERN_ERR " IPEIR: 0x%08x\n",
427 I915_READ(IPEIR_I965));
428 printk(KERN_ERR " IPEHR: 0x%08x\n",
429 I915_READ(IPEHR_I965));
430 printk(KERN_ERR " INSTDONE: 0x%08x\n",
431 I915_READ(INSTDONE_I965));
432 printk(KERN_ERR " INSTPS: 0x%08x\n",
433 I915_READ(INSTPS));
434 printk(KERN_ERR " INSTDONE1: 0x%08x\n",
435 I915_READ(INSTDONE1));
436 printk(KERN_ERR " ACTHD: 0x%08x\n",
437 I915_READ(ACTHD_I965));
438 I915_WRITE(IPEIR_I965, ipeir);
439 (void)I915_READ(IPEIR_I965);
440 }
441 if (eir & GM45_ERROR_PAGE_TABLE) {
442 u32 pgtbl_err = I915_READ(PGTBL_ER);
443 printk(KERN_ERR "page table error\n");
444 printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
445 pgtbl_err);
446 I915_WRITE(PGTBL_ER, pgtbl_err);
447 (void)I915_READ(PGTBL_ER);
448 }
449 }
450
451 if (IS_I9XX(dev)) {
452 if (eir & I915_ERROR_PAGE_TABLE) {
453 u32 pgtbl_err = I915_READ(PGTBL_ER);
454 printk(KERN_ERR "page table error\n");
455 printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
456 pgtbl_err);
457 I915_WRITE(PGTBL_ER, pgtbl_err);
458 (void)I915_READ(PGTBL_ER);
459 }
460 }
461
462 if (eir & I915_ERROR_MEMORY_REFRESH) {
463 printk(KERN_ERR "memory refresh error\n");
464 printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
465 pipea_stats);
466 printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
467 pipeb_stats);
468 /* pipestat has already been acked */
469 }
470 if (eir & I915_ERROR_INSTRUCTION) {
471 printk(KERN_ERR "instruction error\n");
472 printk(KERN_ERR " INSTPM: 0x%08x\n",
473 I915_READ(INSTPM));
474 if (!IS_I965G(dev)) {
475 u32 ipeir = I915_READ(IPEIR);
476
477 printk(KERN_ERR " IPEIR: 0x%08x\n",
478 I915_READ(IPEIR));
479 printk(KERN_ERR " IPEHR: 0x%08x\n",
480 I915_READ(IPEHR));
481 printk(KERN_ERR " INSTDONE: 0x%08x\n",
482 I915_READ(INSTDONE));
483 printk(KERN_ERR " ACTHD: 0x%08x\n",
484 I915_READ(ACTHD));
485 I915_WRITE(IPEIR, ipeir);
486 (void)I915_READ(IPEIR);
487 } else {
488 u32 ipeir = I915_READ(IPEIR_I965);
489
490 printk(KERN_ERR " IPEIR: 0x%08x\n",
491 I915_READ(IPEIR_I965));
492 printk(KERN_ERR " IPEHR: 0x%08x\n",
493 I915_READ(IPEHR_I965));
494 printk(KERN_ERR " INSTDONE: 0x%08x\n",
495 I915_READ(INSTDONE_I965));
496 printk(KERN_ERR " INSTPS: 0x%08x\n",
497 I915_READ(INSTPS));
498 printk(KERN_ERR " INSTDONE1: 0x%08x\n",
499 I915_READ(INSTDONE1));
500 printk(KERN_ERR " ACTHD: 0x%08x\n",
501 I915_READ(ACTHD_I965));
502 I915_WRITE(IPEIR_I965, ipeir);
503 (void)I915_READ(IPEIR_I965);
504 }
505 }
506
507 I915_WRITE(EIR, eir);
508 (void)I915_READ(EIR);
509 eir = I915_READ(EIR);
510 if (eir) {
511 /*
512 * some errors might have become stuck,
513 * mask them.
514 */
515 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
516 I915_WRITE(EMR, I915_READ(EMR) | eir);
517 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
518 }
519
Ben Gamariba1234d2009-09-14 17:48:47 -0400520 if (wedged) {
521 atomic_set(&dev_priv->mm.wedged, 1);
522
Ben Gamari11ed50e2009-09-14 17:48:45 -0400523 /*
524 * Wakeup waiting processes so they don't hang
525 */
526 printk("i915: Waking up sleeping processes\n");
527 DRM_WAKEUP(&dev_priv->irq_queue);
528 }
529
Eric Anholt9c9fe1f2009-08-03 16:09:16 -0700530 queue_work(dev_priv->wq, &dev_priv->error_work);
Jesse Barnes8a905232009-07-11 16:48:03 -0400531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
534{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000535 struct drm_device *dev = (struct drm_device *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000537 struct drm_i915_master_private *master_priv;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800538 u32 iir, new_iir;
539 u32 pipea_stats, pipeb_stats;
Keith Packard05eff842008-11-19 14:03:05 -0800540 u32 vblank_status;
541 u32 vblank_enable;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700542 int vblank = 0;
Keith Packard7c463582008-11-04 02:03:27 -0800543 unsigned long irqflags;
Keith Packard05eff842008-11-19 14:03:05 -0800544 int irq_received;
545 int ret = IRQ_NONE;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000546
Eric Anholt630681d2008-10-06 15:14:12 -0700547 atomic_inc(&dev_priv->irq_received);
548
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800549 if (IS_IGDNG(dev))
550 return igdng_irq_handler(dev);
551
Eric Anholted4cb412008-07-29 12:10:39 -0700552 iir = I915_READ(IIR);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000553
Keith Packard05eff842008-11-19 14:03:05 -0800554 if (IS_I965G(dev)) {
555 vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
556 vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
557 } else {
558 vblank_status = I915_VBLANK_INTERRUPT_STATUS;
559 vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Keith Packard05eff842008-11-19 14:03:05 -0800562 for (;;) {
563 irq_received = iir != 0;
564
565 /* Can't rely on pipestat interrupt bit in iir as it might
566 * have been cleared after the pipestat interrupt was received.
567 * It doesn't set the bit in iir again, but it still produces
568 * interrupts (for non-MSI).
569 */
570 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
571 pipea_stats = I915_READ(PIPEASTAT);
572 pipeb_stats = I915_READ(PIPEBSTAT);
Jesse Barnes79e53942008-11-07 14:24:08 -0800573
Jesse Barnes8a905232009-07-11 16:48:03 -0400574 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Ben Gamariba1234d2009-09-14 17:48:47 -0400575 i915_handle_error(dev, false);
Jesse Barnes8a905232009-07-11 16:48:03 -0400576
Eric Anholtcdfbc412008-11-04 15:50:30 -0800577 /*
578 * Clear the PIPE(A|B)STAT regs before the IIR
579 */
Keith Packard05eff842008-11-19 14:03:05 -0800580 if (pipea_stats & 0x8000ffff) {
Shaohua Li7662c8b2009-06-26 11:23:55 +0800581 if (pipea_stats & PIPE_FIFO_UNDERRUN_STATUS)
Zhao Yakui44d98a62009-10-09 11:39:40 +0800582 DRM_DEBUG_DRIVER("pipe a underrun\n");
Eric Anholtcdfbc412008-11-04 15:50:30 -0800583 I915_WRITE(PIPEASTAT, pipea_stats);
Keith Packard05eff842008-11-19 14:03:05 -0800584 irq_received = 1;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800585 }
Keith Packard7c463582008-11-04 02:03:27 -0800586
Keith Packard05eff842008-11-19 14:03:05 -0800587 if (pipeb_stats & 0x8000ffff) {
Shaohua Li7662c8b2009-06-26 11:23:55 +0800588 if (pipeb_stats & PIPE_FIFO_UNDERRUN_STATUS)
Zhao Yakui44d98a62009-10-09 11:39:40 +0800589 DRM_DEBUG_DRIVER("pipe b underrun\n");
Eric Anholtcdfbc412008-11-04 15:50:30 -0800590 I915_WRITE(PIPEBSTAT, pipeb_stats);
Keith Packard05eff842008-11-19 14:03:05 -0800591 irq_received = 1;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800592 }
Keith Packard05eff842008-11-19 14:03:05 -0800593 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
594
595 if (!irq_received)
596 break;
597
598 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Jesse Barnes5ca58282009-03-31 14:11:15 -0700600 /* Consume port. Then clear IIR or we'll miss events */
601 if ((I915_HAS_HOTPLUG(dev)) &&
602 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
603 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
604
Zhao Yakui44d98a62009-10-09 11:39:40 +0800605 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
Jesse Barnes5ca58282009-03-31 14:11:15 -0700606 hotplug_status);
607 if (hotplug_status & dev_priv->hotplug_supported_mask)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -0700608 queue_work(dev_priv->wq,
609 &dev_priv->hotplug_work);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700610
611 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
612 I915_READ(PORT_HOTPLUG_STAT);
Shaohua Li04302962009-08-24 10:25:23 +0800613
614 /* EOS interrupts occurs */
615 if (IS_IGD(dev) &&
616 (hotplug_status & CRT_EOS_INT_STATUS)) {
617 u32 temp;
618
Zhao Yakui44d98a62009-10-09 11:39:40 +0800619 DRM_DEBUG_DRIVER("EOS interrupt occurs\n");
Shaohua Li04302962009-08-24 10:25:23 +0800620 /* status is already cleared */
621 temp = I915_READ(ADPA);
622 temp &= ~ADPA_DAC_ENABLE;
623 I915_WRITE(ADPA, temp);
624
625 temp = I915_READ(PORT_HOTPLUG_EN);
626 temp &= ~CRT_EOS_INT_EN;
627 I915_WRITE(PORT_HOTPLUG_EN, temp);
628
629 temp = I915_READ(PORT_HOTPLUG_STAT);
630 if (temp & CRT_EOS_INT_STATUS)
631 I915_WRITE(PORT_HOTPLUG_STAT,
632 CRT_EOS_INT_STATUS);
633 }
Jesse Barnes5ca58282009-03-31 14:11:15 -0700634 }
635
Eric Anholtcdfbc412008-11-04 15:50:30 -0800636 I915_WRITE(IIR, iir);
637 new_iir = I915_READ(IIR); /* Flush posted writes */
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100638
Dave Airlie7c1c2872008-11-28 14:22:24 +1000639 if (dev->primary->master) {
640 master_priv = dev->primary->master->driver_priv;
641 if (master_priv->sarea_priv)
642 master_priv->sarea_priv->last_dispatch =
643 READ_BREADCRUMB(dev_priv);
644 }
Keith Packard7c463582008-11-04 02:03:27 -0800645
Eric Anholtcdfbc412008-11-04 15:50:30 -0800646 if (iir & I915_USER_INTERRUPT) {
Chris Wilson1c5d22f2009-08-25 11:15:50 +0100647 u32 seqno = i915_get_gem_seqno(dev);
648 dev_priv->mm.irq_gem_seqno = seqno;
649 trace_i915_gem_request_complete(dev, seqno);
Eric Anholtcdfbc412008-11-04 15:50:30 -0800650 DRM_WAKEUP(&dev_priv->irq_queue);
Ben Gamarif65d9422009-09-14 17:48:44 -0400651 dev_priv->hangcheck_count = 0;
652 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
Eric Anholtcdfbc412008-11-04 15:50:30 -0800653 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700654
Keith Packard05eff842008-11-19 14:03:05 -0800655 if (pipea_stats & vblank_status) {
Eric Anholtcdfbc412008-11-04 15:50:30 -0800656 vblank++;
657 drm_handle_vblank(dev, 0);
658 }
Eric Anholt673a3942008-07-30 12:06:12 -0700659
Keith Packard05eff842008-11-19 14:03:05 -0800660 if (pipeb_stats & vblank_status) {
Eric Anholtcdfbc412008-11-04 15:50:30 -0800661 vblank++;
662 drm_handle_vblank(dev, 1);
663 }
Keith Packard7c463582008-11-04 02:03:27 -0800664
Eric Anholtcdfbc412008-11-04 15:50:30 -0800665 if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
666 (iir & I915_ASLE_INTERRUPT))
667 opregion_asle_intr(dev);
Keith Packard7c463582008-11-04 02:03:27 -0800668
Eric Anholtcdfbc412008-11-04 15:50:30 -0800669 /* With MSI, interrupts are only generated when iir
670 * transitions from zero to nonzero. If another bit got
671 * set while we were handling the existing iir bits, then
672 * we would never get another interrupt.
673 *
674 * This is fine on non-MSI as well, as if we hit this path
675 * we avoid exiting the interrupt handler only to generate
676 * another one.
677 *
678 * Note that for MSI this could cause a stray interrupt report
679 * if an interrupt landed in the time between writing IIR and
680 * the posting read. This should be rare enough to never
681 * trigger the 99% of 100,000 interrupts test for disabling
682 * stray interrupts.
683 */
684 iir = new_iir;
Keith Packard05eff842008-11-19 14:03:05 -0800685 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700686
Keith Packard05eff842008-11-19 14:03:05 -0800687 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Dave Airlieaf6061a2008-05-07 12:15:39 +1000690static int i915_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000693 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 RING_LOCALS;
695
696 i915_kernel_lost_context(dev);
697
Zhao Yakui44d98a62009-10-09 11:39:40 +0800698 DRM_DEBUG_DRIVER("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400700 dev_priv->counter++;
Alan Hourihanec29b6692006-08-12 16:29:24 +1000701 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400702 dev_priv->counter = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000703 if (master_priv->sarea_priv)
704 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Alan Hourihanec29b6692006-08-12 16:29:24 +1000705
Keith Packard0baf8232008-11-08 11:44:14 +1000706 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700707 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000708 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000709 OUT_RING(dev_priv->counter);
Jesse Barnes585fb112008-07-29 11:54:06 -0700710 OUT_RING(MI_USER_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 ADVANCE_LP_RING();
Dave Airliebc5f4522007-11-05 12:50:58 +1000712
Alan Hourihanec29b6692006-08-12 16:29:24 +1000713 return dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Eric Anholt673a3942008-07-30 12:06:12 -0700716void i915_user_irq_get(struct drm_device *dev)
Eric Anholted4cb412008-07-29 12:10:39 -0700717{
718 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700719 unsigned long irqflags;
Eric Anholted4cb412008-07-29 12:10:39 -0700720
Keith Packarde9d21d72008-10-16 11:31:38 -0700721 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800722 if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
723 if (IS_IGDNG(dev))
724 igdng_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
725 else
726 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
727 }
Keith Packarde9d21d72008-10-16 11:31:38 -0700728 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700729}
730
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700731void i915_user_irq_put(struct drm_device *dev)
Eric Anholted4cb412008-07-29 12:10:39 -0700732{
733 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700734 unsigned long irqflags;
Eric Anholted4cb412008-07-29 12:10:39 -0700735
Keith Packarde9d21d72008-10-16 11:31:38 -0700736 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700737 BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800738 if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
739 if (IS_IGDNG(dev))
740 igdng_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
741 else
742 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
743 }
Keith Packarde9d21d72008-10-16 11:31:38 -0700744 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700745}
746
Chris Wilson9d34e5d2009-09-24 05:26:06 +0100747void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
748{
749 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
750
751 if (dev_priv->trace_irq_seqno == 0)
752 i915_user_irq_get(dev);
753
754 dev_priv->trace_irq_seqno = seqno;
755}
756
Dave Airlie84b1fd12007-07-11 15:53:27 +1000757static int i915_wait_irq(struct drm_device * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000760 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int ret = 0;
762
Zhao Yakui44d98a62009-10-09 11:39:40 +0800763 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 READ_BREADCRUMB(dev_priv));
765
Eric Anholted4cb412008-07-29 12:10:39 -0700766 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000767 if (master_priv->sarea_priv)
768 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return 0;
Eric Anholted4cb412008-07-29 12:10:39 -0700770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Dave Airlie7c1c2872008-11-28 14:22:24 +1000772 if (master_priv->sarea_priv)
773 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Eric Anholted4cb412008-07-29 12:10:39 -0700775 i915_user_irq_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
777 READ_BREADCRUMB(dev_priv) >= irq_nr);
Eric Anholted4cb412008-07-29 12:10:39 -0700778 i915_user_irq_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Eric Anholt20caafa2007-08-25 19:22:43 +1000780 if (ret == -EBUSY) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000781 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
783 }
784
Dave Airlieaf6061a2008-05-07 12:15:39 +1000785 return ret;
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/* Needs the lock as it touches the ring.
789 */
Eric Anholtc153f452007-09-03 12:06:45 +1000790int i915_irq_emit(struct drm_device *dev, void *data,
791 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000794 drm_i915_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int result;
796
Eric Anholt07f4f8b2009-04-16 13:46:12 -0700797 if (!dev_priv || !dev_priv->ring.virtual_start) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000798 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000799 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Eric Anholt299eb932009-02-24 22:14:12 -0800801
802 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
803
Eric Anholt546b0972008-09-01 16:45:29 -0700804 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 result = i915_emit_irq(dev);
Eric Anholt546b0972008-09-01 16:45:29 -0700806 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Eric Anholtc153f452007-09-03 12:06:45 +1000808 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000810 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
813 return 0;
814}
815
816/* Doesn't need the hardware lock.
817 */
Eric Anholtc153f452007-09-03 12:06:45 +1000818int i915_irq_wait(struct drm_device *dev, void *data,
819 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000822 drm_i915_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000825 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000826 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
Eric Anholtc153f452007-09-03 12:06:45 +1000829 return i915_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Keith Packard42f52ef2008-10-18 19:39:29 -0700832/* Called from drm generic code, passed 'crtc' which
833 * we use as a pipe index
834 */
835int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700836{
837 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700838 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -0800839 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
840 u32 pipeconf;
841
842 pipeconf = I915_READ(pipeconf_reg);
843 if (!(pipeconf & PIPEACONF_ENABLE))
844 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700845
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800846 if (IS_IGDNG(dev))
847 return 0;
848
Keith Packarde9d21d72008-10-16 11:31:38 -0700849 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Keith Packarde9d21d72008-10-16 11:31:38 -0700850 if (IS_I965G(dev))
Keith Packard7c463582008-11-04 02:03:27 -0800851 i915_enable_pipestat(dev_priv, pipe,
852 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700853 else
Keith Packard7c463582008-11-04 02:03:27 -0800854 i915_enable_pipestat(dev_priv, pipe,
855 PIPE_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700856 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700857 return 0;
858}
859
Keith Packard42f52ef2008-10-18 19:39:29 -0700860/* Called from drm generic code, passed 'crtc' which
861 * we use as a pipe index
862 */
863void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700864{
865 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700866 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700867
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800868 if (IS_IGDNG(dev))
869 return;
870
Keith Packarde9d21d72008-10-16 11:31:38 -0700871 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Keith Packard7c463582008-11-04 02:03:27 -0800872 i915_disable_pipestat(dev_priv, pipe,
873 PIPE_VBLANK_INTERRUPT_ENABLE |
874 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700875 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700876}
877
Jesse Barnes79e53942008-11-07 14:24:08 -0800878void i915_enable_interrupt (struct drm_device *dev)
879{
880 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wange170b032009-06-05 15:38:40 +0800881
882 if (!IS_IGDNG(dev))
883 opregion_enable_asle(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800884 dev_priv->irq_enabled = 1;
885}
886
887
Dave Airlie702880f2006-06-24 17:07:34 +1000888/* Set the vblank monitor pipe
889 */
Eric Anholtc153f452007-09-03 12:06:45 +1000890int i915_vblank_pipe_set(struct drm_device *dev, void *data,
891 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000892{
Dave Airlie702880f2006-06-24 17:07:34 +1000893 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie702880f2006-06-24 17:07:34 +1000894
895 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000896 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000897 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000898 }
899
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +1000900 return 0;
Dave Airlie702880f2006-06-24 17:07:34 +1000901}
902
Eric Anholtc153f452007-09-03 12:06:45 +1000903int i915_vblank_pipe_get(struct drm_device *dev, void *data,
904 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000905{
Dave Airlie702880f2006-06-24 17:07:34 +1000906 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000907 drm_i915_vblank_pipe_t *pipe = data;
Dave Airlie702880f2006-06-24 17:07:34 +1000908
909 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000910 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000911 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000912 }
913
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700914 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Eric Anholtc153f452007-09-03 12:06:45 +1000915
Dave Airlie702880f2006-06-24 17:07:34 +1000916 return 0;
917}
918
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000919/**
920 * Schedule buffer swap at given vertical blank.
921 */
Eric Anholtc153f452007-09-03 12:06:45 +1000922int i915_vblank_swap(struct drm_device *dev, void *data,
923 struct drm_file *file_priv)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000924{
Eric Anholtbd95e0a2008-11-04 12:01:24 -0800925 /* The delayed swap mechanism was fundamentally racy, and has been
926 * removed. The model was that the client requested a delayed flip/swap
927 * from the kernel, then waited for vblank before continuing to perform
928 * rendering. The problem was that the kernel might wake the client
929 * up before it dispatched the vblank swap (since the lock has to be
930 * held while touching the ringbuffer), in which case the client would
931 * clear and start the next frame before the swap occurred, and
932 * flicker would occur in addition to likely missing the vblank.
933 *
934 * In the absence of this ioctl, userland falls back to a correct path
935 * of waiting for a vblank, then dispatching the swap on its own.
936 * Context switching to userland and back is plenty fast enough for
937 * meeting the requirements of vblank swapping.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700938 */
Eric Anholtbd95e0a2008-11-04 12:01:24 -0800939 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000940}
941
Ben Gamarif65d9422009-09-14 17:48:44 -0400942struct drm_i915_gem_request *i915_get_tail_request(struct drm_device *dev) {
943 drm_i915_private_t *dev_priv = dev->dev_private;
944 return list_entry(dev_priv->mm.request_list.prev, struct drm_i915_gem_request, list);
945}
946
947/**
948 * This is called when the chip hasn't reported back with completed
949 * batchbuffers in a long time. The first time this is called we simply record
950 * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
951 * again, we assume the chip is wedged and try to fix it.
952 */
953void i915_hangcheck_elapsed(unsigned long data)
954{
955 struct drm_device *dev = (struct drm_device *)data;
956 drm_i915_private_t *dev_priv = dev->dev_private;
957 uint32_t acthd;
958
959 if (!IS_I965G(dev))
960 acthd = I915_READ(ACTHD);
961 else
962 acthd = I915_READ(ACTHD_I965);
963
964 /* If all work is done then ACTHD clearly hasn't advanced. */
965 if (list_empty(&dev_priv->mm.request_list) ||
966 i915_seqno_passed(i915_get_gem_seqno(dev), i915_get_tail_request(dev)->seqno)) {
967 dev_priv->hangcheck_count = 0;
968 return;
969 }
970
971 if (dev_priv->last_acthd == acthd && dev_priv->hangcheck_count > 0) {
972 DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
Ben Gamariba1234d2009-09-14 17:48:47 -0400973 i915_handle_error(dev, true);
Ben Gamarif65d9422009-09-14 17:48:44 -0400974 return;
975 }
976
977 /* Reset timer case chip hangs without another request being added */
978 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
979
980 if (acthd != dev_priv->last_acthd)
981 dev_priv->hangcheck_count = 0;
982 else
983 dev_priv->hangcheck_count++;
984
985 dev_priv->last_acthd = acthd;
986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/* drm_dma.h hooks
989*/
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800990static void igdng_irq_preinstall(struct drm_device *dev)
991{
992 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
993
994 I915_WRITE(HWSTAM, 0xeffe);
995
996 /* XXX hotplug from PCH */
997
998 I915_WRITE(DEIMR, 0xffffffff);
999 I915_WRITE(DEIER, 0x0);
1000 (void) I915_READ(DEIER);
1001
1002 /* and GT */
1003 I915_WRITE(GTIMR, 0xffffffff);
1004 I915_WRITE(GTIER, 0x0);
1005 (void) I915_READ(GTIER);
1006}
1007
1008static int igdng_irq_postinstall(struct drm_device *dev)
1009{
1010 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1011 /* enable kind of interrupts always enabled */
Zhao Yakui01c66882009-10-28 05:10:00 +00001012 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE /*| DE_PCH_EVENT */;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001013 u32 render_mask = GT_USER_INTERRUPT;
1014
1015 dev_priv->irq_mask_reg = ~display_mask;
1016 dev_priv->de_irq_enable_reg = display_mask;
1017
1018 /* should always can generate irq */
1019 I915_WRITE(DEIIR, I915_READ(DEIIR));
1020 I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
1021 I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
1022 (void) I915_READ(DEIER);
1023
1024 /* user interrupt should be enabled, but masked initial */
1025 dev_priv->gt_irq_mask_reg = 0xffffffff;
1026 dev_priv->gt_irq_enable_reg = render_mask;
1027
1028 I915_WRITE(GTIIR, I915_READ(GTIIR));
1029 I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
1030 I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
1031 (void) I915_READ(GTIER);
1032
1033 return 0;
1034}
1035
Dave Airlie84b1fd12007-07-11 15:53:27 +10001036void i915_driver_irq_preinstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1039
Jesse Barnes79e53942008-11-07 14:24:08 -08001040 atomic_set(&dev_priv->irq_received, 0);
1041
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001042 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Jesse Barnes8a905232009-07-11 16:48:03 -04001043 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001044
1045 if (IS_IGDNG(dev)) {
1046 igdng_irq_preinstall(dev);
1047 return;
1048 }
1049
Jesse Barnes5ca58282009-03-31 14:11:15 -07001050 if (I915_HAS_HOTPLUG(dev)) {
1051 I915_WRITE(PORT_HOTPLUG_EN, 0);
1052 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1053 }
1054
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001055 I915_WRITE(HWSTAM, 0xeffe);
Keith Packard7c463582008-11-04 02:03:27 -08001056 I915_WRITE(PIPEASTAT, 0);
1057 I915_WRITE(PIPEBSTAT, 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001058 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07001059 I915_WRITE(IER, 0x0);
Keith Packard7c463582008-11-04 02:03:27 -08001060 (void) I915_READ(IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001063int i915_driver_irq_postinstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes5ca58282009-03-31 14:11:15 -07001066 u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001067 u32 error_mask;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001068
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001069 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
1070
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001071 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001072
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001073 if (IS_IGDNG(dev))
1074 return igdng_irq_postinstall(dev);
1075
Keith Packard7c463582008-11-04 02:03:27 -08001076 /* Unmask the interrupts that we always want on. */
1077 dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001078
Keith Packard7c463582008-11-04 02:03:27 -08001079 dev_priv->pipestat[0] = 0;
1080 dev_priv->pipestat[1] = 0;
1081
Jesse Barnes5ca58282009-03-31 14:11:15 -07001082 if (I915_HAS_HOTPLUG(dev)) {
1083 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
1084
1085 /* Leave other bits alone */
1086 hotplug_en |= HOTPLUG_EN_MASK;
1087 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
1088
1089 dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
1090 TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
1091 SDVOB_HOTPLUG_INT_STATUS;
1092 if (IS_G4X(dev)) {
1093 dev_priv->hotplug_supported_mask |=
1094 HDMIB_HOTPLUG_INT_STATUS |
1095 HDMIC_HOTPLUG_INT_STATUS |
1096 HDMID_HOTPLUG_INT_STATUS;
1097 }
1098 /* Enable in IER... */
1099 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1100 /* and unmask in IMR */
1101 i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
1102 }
1103
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001104 /*
1105 * Enable some error detection, note the instruction error mask
1106 * bit is reserved, so we leave it masked.
1107 */
1108 if (IS_G4X(dev)) {
1109 error_mask = ~(GM45_ERROR_PAGE_TABLE |
1110 GM45_ERROR_MEM_PRIV |
1111 GM45_ERROR_CP_PRIV |
1112 I915_ERROR_MEMORY_REFRESH);
1113 } else {
1114 error_mask = ~(I915_ERROR_PAGE_TABLE |
1115 I915_ERROR_MEMORY_REFRESH);
1116 }
1117 I915_WRITE(EMR, error_mask);
1118
Keith Packard7c463582008-11-04 02:03:27 -08001119 /* Disable pipe interrupt enables, clear pending pipe status */
1120 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
1121 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
1122 /* Clear pending interrupt status */
1123 I915_WRITE(IIR, I915_READ(IIR));
1124
Jesse Barnes5ca58282009-03-31 14:11:15 -07001125 I915_WRITE(IER, enable_mask);
Keith Packard7c463582008-11-04 02:03:27 -08001126 I915_WRITE(IMR, dev_priv->irq_mask_reg);
Eric Anholted4cb412008-07-29 12:10:39 -07001127 (void) I915_READ(IER);
1128
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001129 opregion_enable_asle(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001130
1131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001134static void igdng_irq_uninstall(struct drm_device *dev)
1135{
1136 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1137 I915_WRITE(HWSTAM, 0xffffffff);
1138
1139 I915_WRITE(DEIMR, 0xffffffff);
1140 I915_WRITE(DEIER, 0x0);
1141 I915_WRITE(DEIIR, I915_READ(DEIIR));
1142
1143 I915_WRITE(GTIMR, 0xffffffff);
1144 I915_WRITE(GTIER, 0x0);
1145 I915_WRITE(GTIIR, I915_READ(GTIIR));
1146}
1147
Dave Airlie84b1fd12007-07-11 15:53:27 +10001148void i915_driver_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie91e37382006-02-18 15:17:04 +11001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (!dev_priv)
1153 return;
1154
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001155 dev_priv->vblank_pipe = 0;
1156
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001157 if (IS_IGDNG(dev)) {
1158 igdng_irq_uninstall(dev);
1159 return;
1160 }
1161
Jesse Barnes5ca58282009-03-31 14:11:15 -07001162 if (I915_HAS_HOTPLUG(dev)) {
1163 I915_WRITE(PORT_HOTPLUG_EN, 0);
1164 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1165 }
1166
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001167 I915_WRITE(HWSTAM, 0xffffffff);
Keith Packard7c463582008-11-04 02:03:27 -08001168 I915_WRITE(PIPEASTAT, 0);
1169 I915_WRITE(PIPEBSTAT, 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001170 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07001171 I915_WRITE(IER, 0x0);
Dave Airlie91e37382006-02-18 15:17:04 +11001172
Keith Packard7c463582008-11-04 02:03:27 -08001173 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
1174 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
1175 I915_WRITE(IIR, I915_READ(IIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}