blob: aa7fd82aa6eb343afa3fdb4b7088104f311f00d9 [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)) {
Frans Pop6cb504c2009-08-09 12:25:29 +1000194 DRM_DEBUG("trying to get vblank count for disabled pipe %d\n", pipe);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700195 return 0;
196 }
197
198 /*
199 * High & low register fields aren't synchronized, so make sure
200 * we get a low value that's stable across two reads of the high
201 * register.
202 */
203 do {
204 high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
205 PIPE_FRAME_HIGH_SHIFT);
206 low = ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
207 PIPE_FRAME_LOW_SHIFT);
208 high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
209 PIPE_FRAME_HIGH_SHIFT);
210 } while (high1 != high2);
211
212 count = (high1 << 8) | low;
213
214 return count;
215}
216
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800217u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
218{
219 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
220 int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
221
222 if (!i915_pipe_enabled(dev, pipe)) {
Frans Pop6cb504c2009-08-09 12:25:29 +1000223 DRM_DEBUG("trying to get vblank count for disabled pipe %d\n", pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800224 return 0;
225 }
226
227 return I915_READ(reg);
228}
229
Jesse Barnes5ca58282009-03-31 14:11:15 -0700230/*
231 * Handle hotplug events outside the interrupt handler proper.
232 */
233static void i915_hotplug_work_func(struct work_struct *work)
234{
235 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
236 hotplug_work);
237 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700238 struct drm_mode_config *mode_config = &dev->mode_config;
239 struct drm_connector *connector;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700240
Keith Packardc31c4ba2009-05-06 11:48:58 -0700241 if (mode_config->num_connector) {
242 list_for_each_entry(connector, &mode_config->connector_list, head) {
243 struct intel_output *intel_output = to_intel_output(connector);
244
245 if (intel_output->hot_plug)
246 (*intel_output->hot_plug) (intel_output);
247 }
248 }
Jesse Barnes5ca58282009-03-31 14:11:15 -0700249 /* Just fire off a uevent and let userspace tell us what to do */
250 drm_sysfs_hotplug_event(dev);
251}
252
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800253irqreturn_t igdng_irq_handler(struct drm_device *dev)
254{
255 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
256 int ret = IRQ_NONE;
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000257 u32 de_iir, gt_iir, de_ier;
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800258 u32 new_de_iir, new_gt_iir;
259 struct drm_i915_master_private *master_priv;
260
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000261 /* disable master interrupt before clearing iir */
262 de_ier = I915_READ(DEIER);
263 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
264 (void)I915_READ(DEIER);
265
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800266 de_iir = I915_READ(DEIIR);
267 gt_iir = I915_READ(GTIIR);
268
269 for (;;) {
270 if (de_iir == 0 && gt_iir == 0)
271 break;
272
273 ret = IRQ_HANDLED;
274
275 I915_WRITE(DEIIR, de_iir);
276 new_de_iir = I915_READ(DEIIR);
277 I915_WRITE(GTIIR, gt_iir);
278 new_gt_iir = I915_READ(GTIIR);
279
280 if (dev->primary->master) {
281 master_priv = dev->primary->master->driver_priv;
282 if (master_priv->sarea_priv)
283 master_priv->sarea_priv->last_dispatch =
284 READ_BREADCRUMB(dev_priv);
285 }
286
287 if (gt_iir & GT_USER_INTERRUPT) {
Chris Wilson1c5d22f2009-08-25 11:15:50 +0100288 u32 seqno = i915_get_gem_seqno(dev);
289 dev_priv->mm.irq_gem_seqno = seqno;
290 trace_i915_gem_request_complete(dev, seqno);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800291 DRM_WAKEUP(&dev_priv->irq_queue);
292 }
293
294 de_iir = new_de_iir;
295 gt_iir = new_gt_iir;
296 }
297
Zou, Nanhai2d109a82009-11-06 02:13:01 +0000298 I915_WRITE(DEIER, de_ier);
299 (void)I915_READ(DEIER);
300
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800301 return ret;
302}
303
Jesse Barnes8a905232009-07-11 16:48:03 -0400304/**
305 * i915_error_work_func - do process context error handling work
306 * @work: work struct
307 *
308 * Fire an error uevent so userspace can see that a hang or error
309 * was detected.
310 */
311static void i915_error_work_func(struct work_struct *work)
312{
313 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
314 error_work);
315 struct drm_device *dev = dev_priv->dev;
Ben Gamarif316a422009-09-14 17:48:46 -0400316 char *error_event[] = { "ERROR=1", NULL };
317 char *reset_event[] = { "RESET=1", NULL };
318 char *reset_done_event[] = { "ERROR=0", NULL };
Jesse Barnes8a905232009-07-11 16:48:03 -0400319
320 DRM_DEBUG("generating error event\n");
Ben Gamarif316a422009-09-14 17:48:46 -0400321 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -0400322
Ben Gamariba1234d2009-09-14 17:48:47 -0400323 if (atomic_read(&dev_priv->mm.wedged)) {
Ben Gamarif316a422009-09-14 17:48:46 -0400324 if (IS_I965G(dev)) {
325 DRM_DEBUG("resetting chip\n");
326 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
327 if (!i965_reset(dev, GDRST_RENDER)) {
Ben Gamariba1234d2009-09-14 17:48:47 -0400328 atomic_set(&dev_priv->mm.wedged, 0);
Ben Gamarif316a422009-09-14 17:48:46 -0400329 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
330 }
331 } else {
332 printk("reboot required\n");
333 }
334 }
Jesse Barnes8a905232009-07-11 16:48:03 -0400335}
336
337/**
338 * i915_capture_error_state - capture an error record for later analysis
339 * @dev: drm device
340 *
341 * Should be called when an error is detected (either a hang or an error
342 * interrupt) to capture error state from the time of the error. Fills
343 * out a structure which becomes available in debugfs for user level tools
344 * to pick up.
345 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700346static void i915_capture_error_state(struct drm_device *dev)
347{
348 struct drm_i915_private *dev_priv = dev->dev_private;
349 struct drm_i915_error_state *error;
350 unsigned long flags;
351
352 spin_lock_irqsave(&dev_priv->error_lock, flags);
353 if (dev_priv->first_error)
354 goto out;
355
356 error = kmalloc(sizeof(*error), GFP_ATOMIC);
357 if (!error) {
358 DRM_DEBUG("out ot memory, not capturing error state\n");
359 goto out;
360 }
361
362 error->eir = I915_READ(EIR);
363 error->pgtbl_er = I915_READ(PGTBL_ER);
364 error->pipeastat = I915_READ(PIPEASTAT);
365 error->pipebstat = I915_READ(PIPEBSTAT);
366 error->instpm = I915_READ(INSTPM);
367 if (!IS_I965G(dev)) {
368 error->ipeir = I915_READ(IPEIR);
369 error->ipehr = I915_READ(IPEHR);
370 error->instdone = I915_READ(INSTDONE);
371 error->acthd = I915_READ(ACTHD);
372 } else {
373 error->ipeir = I915_READ(IPEIR_I965);
374 error->ipehr = I915_READ(IPEHR_I965);
375 error->instdone = I915_READ(INSTDONE_I965);
376 error->instps = I915_READ(INSTPS);
377 error->instdone1 = I915_READ(INSTDONE1);
378 error->acthd = I915_READ(ACTHD_I965);
379 }
380
Jesse Barnes8a905232009-07-11 16:48:03 -0400381 do_gettimeofday(&error->time);
382
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700383 dev_priv->first_error = error;
384
385out:
386 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
387}
388
Jesse Barnes8a905232009-07-11 16:48:03 -0400389/**
390 * i915_handle_error - handle an error interrupt
391 * @dev: drm device
392 *
393 * Do some basic checking of regsiter state at error interrupt time and
394 * dump it to the syslog. Also call i915_capture_error_state() to make
395 * sure we get a record and make it available in debugfs. Fire a uevent
396 * so userspace knows something bad happened (should trigger collection
397 * of a ring dump etc.).
398 */
Ben Gamariba1234d2009-09-14 17:48:47 -0400399static void i915_handle_error(struct drm_device *dev, bool wedged)
Jesse Barnes8a905232009-07-11 16:48:03 -0400400{
401 struct drm_i915_private *dev_priv = dev->dev_private;
402 u32 eir = I915_READ(EIR);
403 u32 pipea_stats = I915_READ(PIPEASTAT);
404 u32 pipeb_stats = I915_READ(PIPEBSTAT);
405
406 i915_capture_error_state(dev);
407
408 printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
409 eir);
410
411 if (IS_G4X(dev)) {
412 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
413 u32 ipeir = I915_READ(IPEIR_I965);
414
415 printk(KERN_ERR " IPEIR: 0x%08x\n",
416 I915_READ(IPEIR_I965));
417 printk(KERN_ERR " IPEHR: 0x%08x\n",
418 I915_READ(IPEHR_I965));
419 printk(KERN_ERR " INSTDONE: 0x%08x\n",
420 I915_READ(INSTDONE_I965));
421 printk(KERN_ERR " INSTPS: 0x%08x\n",
422 I915_READ(INSTPS));
423 printk(KERN_ERR " INSTDONE1: 0x%08x\n",
424 I915_READ(INSTDONE1));
425 printk(KERN_ERR " ACTHD: 0x%08x\n",
426 I915_READ(ACTHD_I965));
427 I915_WRITE(IPEIR_I965, ipeir);
428 (void)I915_READ(IPEIR_I965);
429 }
430 if (eir & GM45_ERROR_PAGE_TABLE) {
431 u32 pgtbl_err = I915_READ(PGTBL_ER);
432 printk(KERN_ERR "page table error\n");
433 printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
434 pgtbl_err);
435 I915_WRITE(PGTBL_ER, pgtbl_err);
436 (void)I915_READ(PGTBL_ER);
437 }
438 }
439
440 if (IS_I9XX(dev)) {
441 if (eir & I915_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 (eir & I915_ERROR_MEMORY_REFRESH) {
452 printk(KERN_ERR "memory refresh error\n");
453 printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
454 pipea_stats);
455 printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
456 pipeb_stats);
457 /* pipestat has already been acked */
458 }
459 if (eir & I915_ERROR_INSTRUCTION) {
460 printk(KERN_ERR "instruction error\n");
461 printk(KERN_ERR " INSTPM: 0x%08x\n",
462 I915_READ(INSTPM));
463 if (!IS_I965G(dev)) {
464 u32 ipeir = I915_READ(IPEIR);
465
466 printk(KERN_ERR " IPEIR: 0x%08x\n",
467 I915_READ(IPEIR));
468 printk(KERN_ERR " IPEHR: 0x%08x\n",
469 I915_READ(IPEHR));
470 printk(KERN_ERR " INSTDONE: 0x%08x\n",
471 I915_READ(INSTDONE));
472 printk(KERN_ERR " ACTHD: 0x%08x\n",
473 I915_READ(ACTHD));
474 I915_WRITE(IPEIR, ipeir);
475 (void)I915_READ(IPEIR);
476 } else {
477 u32 ipeir = I915_READ(IPEIR_I965);
478
479 printk(KERN_ERR " IPEIR: 0x%08x\n",
480 I915_READ(IPEIR_I965));
481 printk(KERN_ERR " IPEHR: 0x%08x\n",
482 I915_READ(IPEHR_I965));
483 printk(KERN_ERR " INSTDONE: 0x%08x\n",
484 I915_READ(INSTDONE_I965));
485 printk(KERN_ERR " INSTPS: 0x%08x\n",
486 I915_READ(INSTPS));
487 printk(KERN_ERR " INSTDONE1: 0x%08x\n",
488 I915_READ(INSTDONE1));
489 printk(KERN_ERR " ACTHD: 0x%08x\n",
490 I915_READ(ACTHD_I965));
491 I915_WRITE(IPEIR_I965, ipeir);
492 (void)I915_READ(IPEIR_I965);
493 }
494 }
495
496 I915_WRITE(EIR, eir);
497 (void)I915_READ(EIR);
498 eir = I915_READ(EIR);
499 if (eir) {
500 /*
501 * some errors might have become stuck,
502 * mask them.
503 */
504 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
505 I915_WRITE(EMR, I915_READ(EMR) | eir);
506 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
507 }
508
Ben Gamariba1234d2009-09-14 17:48:47 -0400509 if (wedged) {
510 atomic_set(&dev_priv->mm.wedged, 1);
511
Ben Gamari11ed50e2009-09-14 17:48:45 -0400512 /*
513 * Wakeup waiting processes so they don't hang
514 */
515 printk("i915: Waking up sleeping processes\n");
516 DRM_WAKEUP(&dev_priv->irq_queue);
517 }
518
Eric Anholt9c9fe1f2009-08-03 16:09:16 -0700519 queue_work(dev_priv->wq, &dev_priv->error_work);
Jesse Barnes8a905232009-07-11 16:48:03 -0400520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
523{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000524 struct drm_device *dev = (struct drm_device *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000526 struct drm_i915_master_private *master_priv;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800527 u32 iir, new_iir;
528 u32 pipea_stats, pipeb_stats;
Keith Packard05eff842008-11-19 14:03:05 -0800529 u32 vblank_status;
530 u32 vblank_enable;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700531 int vblank = 0;
Keith Packard7c463582008-11-04 02:03:27 -0800532 unsigned long irqflags;
Keith Packard05eff842008-11-19 14:03:05 -0800533 int irq_received;
534 int ret = IRQ_NONE;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000535
Eric Anholt630681d2008-10-06 15:14:12 -0700536 atomic_inc(&dev_priv->irq_received);
537
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800538 if (IS_IGDNG(dev))
539 return igdng_irq_handler(dev);
540
Eric Anholted4cb412008-07-29 12:10:39 -0700541 iir = I915_READ(IIR);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000542
Keith Packard05eff842008-11-19 14:03:05 -0800543 if (IS_I965G(dev)) {
544 vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
545 vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
546 } else {
547 vblank_status = I915_VBLANK_INTERRUPT_STATUS;
548 vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Keith Packard05eff842008-11-19 14:03:05 -0800551 for (;;) {
552 irq_received = iir != 0;
553
554 /* Can't rely on pipestat interrupt bit in iir as it might
555 * have been cleared after the pipestat interrupt was received.
556 * It doesn't set the bit in iir again, but it still produces
557 * interrupts (for non-MSI).
558 */
559 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
560 pipea_stats = I915_READ(PIPEASTAT);
561 pipeb_stats = I915_READ(PIPEBSTAT);
Jesse Barnes79e53942008-11-07 14:24:08 -0800562
Jesse Barnes8a905232009-07-11 16:48:03 -0400563 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Ben Gamariba1234d2009-09-14 17:48:47 -0400564 i915_handle_error(dev, false);
Jesse Barnes8a905232009-07-11 16:48:03 -0400565
Eric Anholtcdfbc412008-11-04 15:50:30 -0800566 /*
567 * Clear the PIPE(A|B)STAT regs before the IIR
568 */
Keith Packard05eff842008-11-19 14:03:05 -0800569 if (pipea_stats & 0x8000ffff) {
Shaohua Li7662c8b2009-06-26 11:23:55 +0800570 if (pipea_stats & PIPE_FIFO_UNDERRUN_STATUS)
571 DRM_DEBUG("pipe a underrun\n");
Eric Anholtcdfbc412008-11-04 15:50:30 -0800572 I915_WRITE(PIPEASTAT, pipea_stats);
Keith Packard05eff842008-11-19 14:03:05 -0800573 irq_received = 1;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800574 }
Keith Packard7c463582008-11-04 02:03:27 -0800575
Keith Packard05eff842008-11-19 14:03:05 -0800576 if (pipeb_stats & 0x8000ffff) {
Shaohua Li7662c8b2009-06-26 11:23:55 +0800577 if (pipeb_stats & PIPE_FIFO_UNDERRUN_STATUS)
578 DRM_DEBUG("pipe b underrun\n");
Eric Anholtcdfbc412008-11-04 15:50:30 -0800579 I915_WRITE(PIPEBSTAT, pipeb_stats);
Keith Packard05eff842008-11-19 14:03:05 -0800580 irq_received = 1;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800581 }
Keith Packard05eff842008-11-19 14:03:05 -0800582 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
583
584 if (!irq_received)
585 break;
586
587 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Jesse Barnes5ca58282009-03-31 14:11:15 -0700589 /* Consume port. Then clear IIR or we'll miss events */
590 if ((I915_HAS_HOTPLUG(dev)) &&
591 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
592 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
593
594 DRM_DEBUG("hotplug event received, stat 0x%08x\n",
595 hotplug_status);
596 if (hotplug_status & dev_priv->hotplug_supported_mask)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -0700597 queue_work(dev_priv->wq,
598 &dev_priv->hotplug_work);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700599
600 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
601 I915_READ(PORT_HOTPLUG_STAT);
Shaohua Li04302962009-08-24 10:25:23 +0800602
603 /* EOS interrupts occurs */
604 if (IS_IGD(dev) &&
605 (hotplug_status & CRT_EOS_INT_STATUS)) {
606 u32 temp;
607
608 DRM_DEBUG("EOS interrupt occurs\n");
609 /* status is already cleared */
610 temp = I915_READ(ADPA);
611 temp &= ~ADPA_DAC_ENABLE;
612 I915_WRITE(ADPA, temp);
613
614 temp = I915_READ(PORT_HOTPLUG_EN);
615 temp &= ~CRT_EOS_INT_EN;
616 I915_WRITE(PORT_HOTPLUG_EN, temp);
617
618 temp = I915_READ(PORT_HOTPLUG_STAT);
619 if (temp & CRT_EOS_INT_STATUS)
620 I915_WRITE(PORT_HOTPLUG_STAT,
621 CRT_EOS_INT_STATUS);
622 }
Jesse Barnes5ca58282009-03-31 14:11:15 -0700623 }
624
Eric Anholtcdfbc412008-11-04 15:50:30 -0800625 I915_WRITE(IIR, iir);
626 new_iir = I915_READ(IIR); /* Flush posted writes */
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100627
Dave Airlie7c1c2872008-11-28 14:22:24 +1000628 if (dev->primary->master) {
629 master_priv = dev->primary->master->driver_priv;
630 if (master_priv->sarea_priv)
631 master_priv->sarea_priv->last_dispatch =
632 READ_BREADCRUMB(dev_priv);
633 }
Keith Packard7c463582008-11-04 02:03:27 -0800634
Eric Anholtcdfbc412008-11-04 15:50:30 -0800635 if (iir & I915_USER_INTERRUPT) {
Chris Wilson1c5d22f2009-08-25 11:15:50 +0100636 u32 seqno = i915_get_gem_seqno(dev);
637 dev_priv->mm.irq_gem_seqno = seqno;
638 trace_i915_gem_request_complete(dev, seqno);
Eric Anholtcdfbc412008-11-04 15:50:30 -0800639 DRM_WAKEUP(&dev_priv->irq_queue);
Ben Gamarif65d9422009-09-14 17:48:44 -0400640 dev_priv->hangcheck_count = 0;
641 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
Eric Anholtcdfbc412008-11-04 15:50:30 -0800642 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700643
Keith Packard05eff842008-11-19 14:03:05 -0800644 if (pipea_stats & vblank_status) {
Eric Anholtcdfbc412008-11-04 15:50:30 -0800645 vblank++;
646 drm_handle_vblank(dev, 0);
647 }
Eric Anholt673a3942008-07-30 12:06:12 -0700648
Keith Packard05eff842008-11-19 14:03:05 -0800649 if (pipeb_stats & vblank_status) {
Eric Anholtcdfbc412008-11-04 15:50:30 -0800650 vblank++;
651 drm_handle_vblank(dev, 1);
652 }
Keith Packard7c463582008-11-04 02:03:27 -0800653
Eric Anholtcdfbc412008-11-04 15:50:30 -0800654 if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
655 (iir & I915_ASLE_INTERRUPT))
656 opregion_asle_intr(dev);
Keith Packard7c463582008-11-04 02:03:27 -0800657
Eric Anholtcdfbc412008-11-04 15:50:30 -0800658 /* With MSI, interrupts are only generated when iir
659 * transitions from zero to nonzero. If another bit got
660 * set while we were handling the existing iir bits, then
661 * we would never get another interrupt.
662 *
663 * This is fine on non-MSI as well, as if we hit this path
664 * we avoid exiting the interrupt handler only to generate
665 * another one.
666 *
667 * Note that for MSI this could cause a stray interrupt report
668 * if an interrupt landed in the time between writing IIR and
669 * the posting read. This should be rare enough to never
670 * trigger the 99% of 100,000 interrupts test for disabling
671 * stray interrupts.
672 */
673 iir = new_iir;
Keith Packard05eff842008-11-19 14:03:05 -0800674 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700675
Keith Packard05eff842008-11-19 14:03:05 -0800676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Dave Airlieaf6061a2008-05-07 12:15:39 +1000679static int i915_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000682 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 RING_LOCALS;
684
685 i915_kernel_lost_context(dev);
686
Márton Németh3e684ea2008-01-24 15:58:57 +1000687 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400689 dev_priv->counter++;
Alan Hourihanec29b6692006-08-12 16:29:24 +1000690 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400691 dev_priv->counter = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000692 if (master_priv->sarea_priv)
693 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Alan Hourihanec29b6692006-08-12 16:29:24 +1000694
Keith Packard0baf8232008-11-08 11:44:14 +1000695 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700696 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000697 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000698 OUT_RING(dev_priv->counter);
Jesse Barnes585fb112008-07-29 11:54:06 -0700699 OUT_RING(MI_USER_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 ADVANCE_LP_RING();
Dave Airliebc5f4522007-11-05 12:50:58 +1000701
Alan Hourihanec29b6692006-08-12 16:29:24 +1000702 return dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Eric Anholt673a3942008-07-30 12:06:12 -0700705void i915_user_irq_get(struct drm_device *dev)
Eric Anholted4cb412008-07-29 12:10:39 -0700706{
707 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700708 unsigned long irqflags;
Eric Anholted4cb412008-07-29 12:10:39 -0700709
Keith Packarde9d21d72008-10-16 11:31:38 -0700710 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800711 if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
712 if (IS_IGDNG(dev))
713 igdng_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
714 else
715 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
716 }
Keith Packarde9d21d72008-10-16 11:31:38 -0700717 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700718}
719
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700720void i915_user_irq_put(struct drm_device *dev)
Eric Anholted4cb412008-07-29 12:10:39 -0700721{
722 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700723 unsigned long irqflags;
Eric Anholted4cb412008-07-29 12:10:39 -0700724
Keith Packarde9d21d72008-10-16 11:31:38 -0700725 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700726 BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800727 if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
728 if (IS_IGDNG(dev))
729 igdng_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
730 else
731 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
732 }
Keith Packarde9d21d72008-10-16 11:31:38 -0700733 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700734}
735
Chris Wilson9d34e5d2009-09-24 05:26:06 +0100736void i915_trace_irq_get(struct drm_device *dev, u32 seqno)
737{
738 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
739
740 if (dev_priv->trace_irq_seqno == 0)
741 i915_user_irq_get(dev);
742
743 dev_priv->trace_irq_seqno = seqno;
744}
745
Dave Airlie84b1fd12007-07-11 15:53:27 +1000746static int i915_wait_irq(struct drm_device * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000749 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 int ret = 0;
751
Márton Németh3e684ea2008-01-24 15:58:57 +1000752 DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 READ_BREADCRUMB(dev_priv));
754
Eric Anholted4cb412008-07-29 12:10:39 -0700755 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000756 if (master_priv->sarea_priv)
757 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
Eric Anholted4cb412008-07-29 12:10:39 -0700759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Dave Airlie7c1c2872008-11-28 14:22:24 +1000761 if (master_priv->sarea_priv)
762 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Eric Anholted4cb412008-07-29 12:10:39 -0700764 i915_user_irq_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
766 READ_BREADCRUMB(dev_priv) >= irq_nr);
Eric Anholted4cb412008-07-29 12:10:39 -0700767 i915_user_irq_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Eric Anholt20caafa2007-08-25 19:22:43 +1000769 if (ret == -EBUSY) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000770 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
772 }
773
Dave Airlieaf6061a2008-05-07 12:15:39 +1000774 return ret;
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777/* Needs the lock as it touches the ring.
778 */
Eric Anholtc153f452007-09-03 12:06:45 +1000779int i915_irq_emit(struct drm_device *dev, void *data,
780 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000783 drm_i915_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int result;
785
Eric Anholt07f4f8b2009-04-16 13:46:12 -0700786 if (!dev_priv || !dev_priv->ring.virtual_start) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000787 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000788 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Eric Anholt299eb932009-02-24 22:14:12 -0800790
791 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
792
Eric Anholt546b0972008-09-01 16:45:29 -0700793 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 result = i915_emit_irq(dev);
Eric Anholt546b0972008-09-01 16:45:29 -0700795 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Eric Anholtc153f452007-09-03 12:06:45 +1000797 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000799 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
802 return 0;
803}
804
805/* Doesn't need the hardware lock.
806 */
Eric Anholtc153f452007-09-03 12:06:45 +1000807int i915_irq_wait(struct drm_device *dev, void *data,
808 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000811 drm_i915_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000814 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000815 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817
Eric Anholtc153f452007-09-03 12:06:45 +1000818 return i915_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Keith Packard42f52ef2008-10-18 19:39:29 -0700821/* Called from drm generic code, passed 'crtc' which
822 * we use as a pipe index
823 */
824int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700825{
826 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700827 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -0800828 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
829 u32 pipeconf;
830
831 pipeconf = I915_READ(pipeconf_reg);
832 if (!(pipeconf & PIPEACONF_ENABLE))
833 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700834
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800835 if (IS_IGDNG(dev))
836 return 0;
837
Keith Packarde9d21d72008-10-16 11:31:38 -0700838 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Keith Packarde9d21d72008-10-16 11:31:38 -0700839 if (IS_I965G(dev))
Keith Packard7c463582008-11-04 02:03:27 -0800840 i915_enable_pipestat(dev_priv, pipe,
841 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700842 else
Keith Packard7c463582008-11-04 02:03:27 -0800843 i915_enable_pipestat(dev_priv, pipe,
844 PIPE_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700845 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700846 return 0;
847}
848
Keith Packard42f52ef2008-10-18 19:39:29 -0700849/* Called from drm generic code, passed 'crtc' which
850 * we use as a pipe index
851 */
852void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700853{
854 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700855 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700856
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800857 if (IS_IGDNG(dev))
858 return;
859
Keith Packarde9d21d72008-10-16 11:31:38 -0700860 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Keith Packard7c463582008-11-04 02:03:27 -0800861 i915_disable_pipestat(dev_priv, pipe,
862 PIPE_VBLANK_INTERRUPT_ENABLE |
863 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700864 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700865}
866
Jesse Barnes79e53942008-11-07 14:24:08 -0800867void i915_enable_interrupt (struct drm_device *dev)
868{
869 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wange170b032009-06-05 15:38:40 +0800870
871 if (!IS_IGDNG(dev))
872 opregion_enable_asle(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800873 dev_priv->irq_enabled = 1;
874}
875
876
Dave Airlie702880f2006-06-24 17:07:34 +1000877/* Set the vblank monitor pipe
878 */
Eric Anholtc153f452007-09-03 12:06:45 +1000879int i915_vblank_pipe_set(struct drm_device *dev, void *data,
880 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000881{
Dave Airlie702880f2006-06-24 17:07:34 +1000882 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie702880f2006-06-24 17:07:34 +1000883
884 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000885 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000886 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000887 }
888
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +1000889 return 0;
Dave Airlie702880f2006-06-24 17:07:34 +1000890}
891
Eric Anholtc153f452007-09-03 12:06:45 +1000892int i915_vblank_pipe_get(struct drm_device *dev, void *data,
893 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000894{
Dave Airlie702880f2006-06-24 17:07:34 +1000895 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000896 drm_i915_vblank_pipe_t *pipe = data;
Dave Airlie702880f2006-06-24 17:07:34 +1000897
898 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000899 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000900 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000901 }
902
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700903 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Eric Anholtc153f452007-09-03 12:06:45 +1000904
Dave Airlie702880f2006-06-24 17:07:34 +1000905 return 0;
906}
907
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000908/**
909 * Schedule buffer swap at given vertical blank.
910 */
Eric Anholtc153f452007-09-03 12:06:45 +1000911int i915_vblank_swap(struct drm_device *dev, void *data,
912 struct drm_file *file_priv)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000913{
Eric Anholtbd95e0a2008-11-04 12:01:24 -0800914 /* The delayed swap mechanism was fundamentally racy, and has been
915 * removed. The model was that the client requested a delayed flip/swap
916 * from the kernel, then waited for vblank before continuing to perform
917 * rendering. The problem was that the kernel might wake the client
918 * up before it dispatched the vblank swap (since the lock has to be
919 * held while touching the ringbuffer), in which case the client would
920 * clear and start the next frame before the swap occurred, and
921 * flicker would occur in addition to likely missing the vblank.
922 *
923 * In the absence of this ioctl, userland falls back to a correct path
924 * of waiting for a vblank, then dispatching the swap on its own.
925 * Context switching to userland and back is plenty fast enough for
926 * meeting the requirements of vblank swapping.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700927 */
Eric Anholtbd95e0a2008-11-04 12:01:24 -0800928 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000929}
930
Ben Gamarif65d9422009-09-14 17:48:44 -0400931struct drm_i915_gem_request *i915_get_tail_request(struct drm_device *dev) {
932 drm_i915_private_t *dev_priv = dev->dev_private;
933 return list_entry(dev_priv->mm.request_list.prev, struct drm_i915_gem_request, list);
934}
935
936/**
937 * This is called when the chip hasn't reported back with completed
938 * batchbuffers in a long time. The first time this is called we simply record
939 * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
940 * again, we assume the chip is wedged and try to fix it.
941 */
942void i915_hangcheck_elapsed(unsigned long data)
943{
944 struct drm_device *dev = (struct drm_device *)data;
945 drm_i915_private_t *dev_priv = dev->dev_private;
946 uint32_t acthd;
947
948 if (!IS_I965G(dev))
949 acthd = I915_READ(ACTHD);
950 else
951 acthd = I915_READ(ACTHD_I965);
952
953 /* If all work is done then ACTHD clearly hasn't advanced. */
954 if (list_empty(&dev_priv->mm.request_list) ||
955 i915_seqno_passed(i915_get_gem_seqno(dev), i915_get_tail_request(dev)->seqno)) {
956 dev_priv->hangcheck_count = 0;
957 return;
958 }
959
960 if (dev_priv->last_acthd == acthd && dev_priv->hangcheck_count > 0) {
961 DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
Ben Gamariba1234d2009-09-14 17:48:47 -0400962 i915_handle_error(dev, true);
Ben Gamarif65d9422009-09-14 17:48:44 -0400963 return;
964 }
965
966 /* Reset timer case chip hangs without another request being added */
967 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
968
969 if (acthd != dev_priv->last_acthd)
970 dev_priv->hangcheck_count = 0;
971 else
972 dev_priv->hangcheck_count++;
973
974 dev_priv->last_acthd = acthd;
975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977/* drm_dma.h hooks
978*/
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800979static void igdng_irq_preinstall(struct drm_device *dev)
980{
981 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
982
983 I915_WRITE(HWSTAM, 0xeffe);
984
985 /* XXX hotplug from PCH */
986
987 I915_WRITE(DEIMR, 0xffffffff);
988 I915_WRITE(DEIER, 0x0);
989 (void) I915_READ(DEIER);
990
991 /* and GT */
992 I915_WRITE(GTIMR, 0xffffffff);
993 I915_WRITE(GTIER, 0x0);
994 (void) I915_READ(GTIER);
995}
996
997static int igdng_irq_postinstall(struct drm_device *dev)
998{
999 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1000 /* enable kind of interrupts always enabled */
1001 u32 display_mask = DE_MASTER_IRQ_CONTROL /*| DE_PCH_EVENT */;
1002 u32 render_mask = GT_USER_INTERRUPT;
1003
1004 dev_priv->irq_mask_reg = ~display_mask;
1005 dev_priv->de_irq_enable_reg = display_mask;
1006
1007 /* should always can generate irq */
1008 I915_WRITE(DEIIR, I915_READ(DEIIR));
1009 I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
1010 I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
1011 (void) I915_READ(DEIER);
1012
1013 /* user interrupt should be enabled, but masked initial */
1014 dev_priv->gt_irq_mask_reg = 0xffffffff;
1015 dev_priv->gt_irq_enable_reg = render_mask;
1016
1017 I915_WRITE(GTIIR, I915_READ(GTIIR));
1018 I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
1019 I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
1020 (void) I915_READ(GTIER);
1021
1022 return 0;
1023}
1024
Dave Airlie84b1fd12007-07-11 15:53:27 +10001025void i915_driver_irq_preinstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1028
Jesse Barnes79e53942008-11-07 14:24:08 -08001029 atomic_set(&dev_priv->irq_received, 0);
1030
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001031 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Jesse Barnes8a905232009-07-11 16:48:03 -04001032 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001033
1034 if (IS_IGDNG(dev)) {
1035 igdng_irq_preinstall(dev);
1036 return;
1037 }
1038
Jesse Barnes5ca58282009-03-31 14:11:15 -07001039 if (I915_HAS_HOTPLUG(dev)) {
1040 I915_WRITE(PORT_HOTPLUG_EN, 0);
1041 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1042 }
1043
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001044 I915_WRITE(HWSTAM, 0xeffe);
Keith Packard7c463582008-11-04 02:03:27 -08001045 I915_WRITE(PIPEASTAT, 0);
1046 I915_WRITE(PIPEBSTAT, 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001047 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07001048 I915_WRITE(IER, 0x0);
Keith Packard7c463582008-11-04 02:03:27 -08001049 (void) I915_READ(IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001052int i915_driver_irq_postinstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes5ca58282009-03-31 14:11:15 -07001055 u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001056 u32 error_mask;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001057
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001058 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
1059
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001060 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001061
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001062 if (IS_IGDNG(dev))
1063 return igdng_irq_postinstall(dev);
1064
Keith Packard7c463582008-11-04 02:03:27 -08001065 /* Unmask the interrupts that we always want on. */
1066 dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001067
Keith Packard7c463582008-11-04 02:03:27 -08001068 dev_priv->pipestat[0] = 0;
1069 dev_priv->pipestat[1] = 0;
1070
Jesse Barnes5ca58282009-03-31 14:11:15 -07001071 if (I915_HAS_HOTPLUG(dev)) {
1072 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
1073
1074 /* Leave other bits alone */
1075 hotplug_en |= HOTPLUG_EN_MASK;
1076 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
1077
1078 dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
1079 TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
1080 SDVOB_HOTPLUG_INT_STATUS;
1081 if (IS_G4X(dev)) {
1082 dev_priv->hotplug_supported_mask |=
1083 HDMIB_HOTPLUG_INT_STATUS |
1084 HDMIC_HOTPLUG_INT_STATUS |
1085 HDMID_HOTPLUG_INT_STATUS;
1086 }
1087 /* Enable in IER... */
1088 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1089 /* and unmask in IMR */
1090 i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
1091 }
1092
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001093 /*
1094 * Enable some error detection, note the instruction error mask
1095 * bit is reserved, so we leave it masked.
1096 */
1097 if (IS_G4X(dev)) {
1098 error_mask = ~(GM45_ERROR_PAGE_TABLE |
1099 GM45_ERROR_MEM_PRIV |
1100 GM45_ERROR_CP_PRIV |
1101 I915_ERROR_MEMORY_REFRESH);
1102 } else {
1103 error_mask = ~(I915_ERROR_PAGE_TABLE |
1104 I915_ERROR_MEMORY_REFRESH);
1105 }
1106 I915_WRITE(EMR, error_mask);
1107
Keith Packard7c463582008-11-04 02:03:27 -08001108 /* Disable pipe interrupt enables, clear pending pipe status */
1109 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
1110 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
1111 /* Clear pending interrupt status */
1112 I915_WRITE(IIR, I915_READ(IIR));
1113
Jesse Barnes5ca58282009-03-31 14:11:15 -07001114 I915_WRITE(IER, enable_mask);
Keith Packard7c463582008-11-04 02:03:27 -08001115 I915_WRITE(IMR, dev_priv->irq_mask_reg);
Eric Anholted4cb412008-07-29 12:10:39 -07001116 (void) I915_READ(IER);
1117
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001118 opregion_enable_asle(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001119
1120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001123static void igdng_irq_uninstall(struct drm_device *dev)
1124{
1125 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1126 I915_WRITE(HWSTAM, 0xffffffff);
1127
1128 I915_WRITE(DEIMR, 0xffffffff);
1129 I915_WRITE(DEIER, 0x0);
1130 I915_WRITE(DEIIR, I915_READ(DEIIR));
1131
1132 I915_WRITE(GTIMR, 0xffffffff);
1133 I915_WRITE(GTIER, 0x0);
1134 I915_WRITE(GTIIR, I915_READ(GTIIR));
1135}
1136
Dave Airlie84b1fd12007-07-11 15:53:27 +10001137void i915_driver_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie91e37382006-02-18 15:17:04 +11001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!dev_priv)
1142 return;
1143
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001144 dev_priv->vblank_pipe = 0;
1145
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001146 if (IS_IGDNG(dev)) {
1147 igdng_irq_uninstall(dev);
1148 return;
1149 }
1150
Jesse Barnes5ca58282009-03-31 14:11:15 -07001151 if (I915_HAS_HOTPLUG(dev)) {
1152 I915_WRITE(PORT_HOTPLUG_EN, 0);
1153 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1154 }
1155
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001156 I915_WRITE(HWSTAM, 0xffffffff);
Keith Packard7c463582008-11-04 02:03:27 -08001157 I915_WRITE(PIPEASTAT, 0);
1158 I915_WRITE(PIPEBSTAT, 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001159 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07001160 I915_WRITE(IER, 0x0);
Dave Airlie91e37382006-02-18 15:17:04 +11001161
Keith Packard7c463582008-11-04 02:03:27 -08001162 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
1163 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
1164 I915_WRITE(IIR, I915_READ(IIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}