blob: 8f5276614ce2c2e65b727b3bbf79f8cf0033b783 [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"
Jesse Barnes79e53942008-11-07 14:24:08 -080034#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define MAX_NOPID ((u32)~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Keith Packard7c463582008-11-04 02:03:27 -080038/**
39 * Interrupts that are always left unmasked.
40 *
41 * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
42 * we leave them always unmasked in IMR and then control enabling them through
43 * PIPESTAT alone.
44 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -070045#define I915_INTERRUPT_ENABLE_FIX (I915_ASLE_INTERRUPT | \
46 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
47 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT | \
48 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Keith Packard7c463582008-11-04 02:03:27 -080049
50/** Interrupts that we mask and unmask at runtime. */
51#define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
52
Jesse Barnes79e53942008-11-07 14:24:08 -080053#define I915_PIPE_VBLANK_STATUS (PIPE_START_VBLANK_INTERRUPT_STATUS |\
54 PIPE_VBLANK_INTERRUPT_STATUS)
55
56#define I915_PIPE_VBLANK_ENABLE (PIPE_START_VBLANK_INTERRUPT_ENABLE |\
57 PIPE_VBLANK_INTERRUPT_ENABLE)
58
59#define DRM_I915_VBLANK_PIPE_ALL (DRM_I915_VBLANK_PIPE_A | \
60 DRM_I915_VBLANK_PIPE_B)
61
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +010062void
Zhenyu Wang036a4a72009-06-08 14:40:19 +080063igdng_enable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
64{
65 if ((dev_priv->gt_irq_mask_reg & mask) != 0) {
66 dev_priv->gt_irq_mask_reg &= ~mask;
67 I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
68 (void) I915_READ(GTIMR);
69 }
70}
71
72static inline void
73igdng_disable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
74{
75 if ((dev_priv->gt_irq_mask_reg & mask) != mask) {
76 dev_priv->gt_irq_mask_reg |= mask;
77 I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
78 (void) I915_READ(GTIMR);
79 }
80}
81
82/* For display hotplug interrupt */
83void
84igdng_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
85{
86 if ((dev_priv->irq_mask_reg & mask) != 0) {
87 dev_priv->irq_mask_reg &= ~mask;
88 I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
89 (void) I915_READ(DEIMR);
90 }
91}
92
93static inline void
94igdng_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
95{
96 if ((dev_priv->irq_mask_reg & mask) != mask) {
97 dev_priv->irq_mask_reg |= mask;
98 I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
99 (void) I915_READ(DEIMR);
100 }
101}
102
103void
Eric Anholted4cb412008-07-29 12:10:39 -0700104i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
105{
106 if ((dev_priv->irq_mask_reg & mask) != 0) {
107 dev_priv->irq_mask_reg &= ~mask;
108 I915_WRITE(IMR, dev_priv->irq_mask_reg);
109 (void) I915_READ(IMR);
110 }
111}
112
113static inline void
114i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
115{
116 if ((dev_priv->irq_mask_reg & mask) != mask) {
117 dev_priv->irq_mask_reg |= mask;
118 I915_WRITE(IMR, dev_priv->irq_mask_reg);
119 (void) I915_READ(IMR);
120 }
121}
122
Keith Packard7c463582008-11-04 02:03:27 -0800123static inline u32
124i915_pipestat(int pipe)
125{
126 if (pipe == 0)
127 return PIPEASTAT;
128 if (pipe == 1)
129 return PIPEBSTAT;
Andrew Morton9c84ba42008-12-01 13:14:08 -0800130 BUG();
Keith Packard7c463582008-11-04 02:03:27 -0800131}
132
133void
134i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
135{
136 if ((dev_priv->pipestat[pipe] & mask) != mask) {
137 u32 reg = i915_pipestat(pipe);
138
139 dev_priv->pipestat[pipe] |= mask;
140 /* Enable the interrupt, clear any pending status */
141 I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
142 (void) I915_READ(reg);
143 }
144}
145
146void
147i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
148{
149 if ((dev_priv->pipestat[pipe] & mask) != 0) {
150 u32 reg = i915_pipestat(pipe);
151
152 dev_priv->pipestat[pipe] &= ~mask;
153 I915_WRITE(reg, dev_priv->pipestat[pipe]);
154 (void) I915_READ(reg);
155 }
156}
157
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000158/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700159 * i915_pipe_enabled - check if a pipe is enabled
160 * @dev: DRM device
161 * @pipe: pipe to check
162 *
163 * Reading certain registers when the pipe is disabled can hang the chip.
164 * Use this routine to make sure the PLL is running and the pipe is active
165 * before reading such registers if unsure.
166 */
167static int
168i915_pipe_enabled(struct drm_device *dev, int pipe)
169{
170 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
171 unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
172
173 if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
174 return 1;
175
176 return 0;
177}
178
Keith Packard42f52ef2008-10-18 19:39:29 -0700179/* Called from drm generic code, passed a 'crtc', which
180 * we use as a pipe index
181 */
182u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700183{
184 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
185 unsigned long high_frame;
186 unsigned long low_frame;
187 u32 high1, high2, low, count;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700188
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700189 high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
190 low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
191
192 if (!i915_pipe_enabled(dev, pipe)) {
Frans Pop6cb504c2009-08-09 12:25:29 +1000193 DRM_DEBUG("trying to get vblank count for disabled pipe %d\n", pipe);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700194 return 0;
195 }
196
197 /*
198 * High & low register fields aren't synchronized, so make sure
199 * we get a low value that's stable across two reads of the high
200 * register.
201 */
202 do {
203 high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
204 PIPE_FRAME_HIGH_SHIFT);
205 low = ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
206 PIPE_FRAME_LOW_SHIFT);
207 high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
208 PIPE_FRAME_HIGH_SHIFT);
209 } while (high1 != high2);
210
211 count = (high1 << 8) | low;
212
213 return count;
214}
215
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800216u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
217{
218 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
219 int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
220
221 if (!i915_pipe_enabled(dev, pipe)) {
Frans Pop6cb504c2009-08-09 12:25:29 +1000222 DRM_DEBUG("trying to get vblank count for disabled pipe %d\n", pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800223 return 0;
224 }
225
226 return I915_READ(reg);
227}
228
Jesse Barnes5ca58282009-03-31 14:11:15 -0700229/*
230 * Handle hotplug events outside the interrupt handler proper.
231 */
232static void i915_hotplug_work_func(struct work_struct *work)
233{
234 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
235 hotplug_work);
236 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700237 struct drm_mode_config *mode_config = &dev->mode_config;
238 struct drm_connector *connector;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700239
Keith Packardc31c4ba2009-05-06 11:48:58 -0700240 if (mode_config->num_connector) {
241 list_for_each_entry(connector, &mode_config->connector_list, head) {
242 struct intel_output *intel_output = to_intel_output(connector);
243
244 if (intel_output->hot_plug)
245 (*intel_output->hot_plug) (intel_output);
246 }
247 }
Jesse Barnes5ca58282009-03-31 14:11:15 -0700248 /* Just fire off a uevent and let userspace tell us what to do */
249 drm_sysfs_hotplug_event(dev);
250}
251
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800252irqreturn_t igdng_irq_handler(struct drm_device *dev)
253{
254 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
255 int ret = IRQ_NONE;
256 u32 de_iir, gt_iir;
257 u32 new_de_iir, new_gt_iir;
258 struct drm_i915_master_private *master_priv;
259
260 de_iir = I915_READ(DEIIR);
261 gt_iir = I915_READ(GTIIR);
262
263 for (;;) {
264 if (de_iir == 0 && gt_iir == 0)
265 break;
266
267 ret = IRQ_HANDLED;
268
269 I915_WRITE(DEIIR, de_iir);
270 new_de_iir = I915_READ(DEIIR);
271 I915_WRITE(GTIIR, gt_iir);
272 new_gt_iir = I915_READ(GTIIR);
273
274 if (dev->primary->master) {
275 master_priv = dev->primary->master->driver_priv;
276 if (master_priv->sarea_priv)
277 master_priv->sarea_priv->last_dispatch =
278 READ_BREADCRUMB(dev_priv);
279 }
280
281 if (gt_iir & GT_USER_INTERRUPT) {
282 dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
283 DRM_WAKEUP(&dev_priv->irq_queue);
284 }
285
286 de_iir = new_de_iir;
287 gt_iir = new_gt_iir;
288 }
289
290 return ret;
291}
292
Jesse Barnes8a905232009-07-11 16:48:03 -0400293/**
294 * i915_error_work_func - do process context error handling work
295 * @work: work struct
296 *
297 * Fire an error uevent so userspace can see that a hang or error
298 * was detected.
299 */
300static void i915_error_work_func(struct work_struct *work)
301{
302 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
303 error_work);
304 struct drm_device *dev = dev_priv->dev;
Ben Gamarif316a422009-09-14 17:48:46 -0400305 char *error_event[] = { "ERROR=1", NULL };
306 char *reset_event[] = { "RESET=1", NULL };
307 char *reset_done_event[] = { "ERROR=0", NULL };
Jesse Barnes8a905232009-07-11 16:48:03 -0400308
309 DRM_DEBUG("generating error event\n");
Ben Gamarif316a422009-09-14 17:48:46 -0400310 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -0400311
Ben Gamarif316a422009-09-14 17:48:46 -0400312 if (dev_priv->mm.wedged) {
313 if (IS_I965G(dev)) {
314 DRM_DEBUG("resetting chip\n");
315 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_event);
316 if (!i965_reset(dev, GDRST_RENDER)) {
317 dev_priv->mm.wedged = 0;
318 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, reset_done_event);
319 }
320 } else {
321 printk("reboot required\n");
322 }
323 }
Jesse Barnes8a905232009-07-11 16:48:03 -0400324}
325
326/**
327 * i915_capture_error_state - capture an error record for later analysis
328 * @dev: drm device
329 *
330 * Should be called when an error is detected (either a hang or an error
331 * interrupt) to capture error state from the time of the error. Fills
332 * out a structure which becomes available in debugfs for user level tools
333 * to pick up.
334 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700335static void i915_capture_error_state(struct drm_device *dev)
336{
337 struct drm_i915_private *dev_priv = dev->dev_private;
338 struct drm_i915_error_state *error;
339 unsigned long flags;
340
341 spin_lock_irqsave(&dev_priv->error_lock, flags);
342 if (dev_priv->first_error)
343 goto out;
344
345 error = kmalloc(sizeof(*error), GFP_ATOMIC);
346 if (!error) {
347 DRM_DEBUG("out ot memory, not capturing error state\n");
348 goto out;
349 }
350
351 error->eir = I915_READ(EIR);
352 error->pgtbl_er = I915_READ(PGTBL_ER);
353 error->pipeastat = I915_READ(PIPEASTAT);
354 error->pipebstat = I915_READ(PIPEBSTAT);
355 error->instpm = I915_READ(INSTPM);
356 if (!IS_I965G(dev)) {
357 error->ipeir = I915_READ(IPEIR);
358 error->ipehr = I915_READ(IPEHR);
359 error->instdone = I915_READ(INSTDONE);
360 error->acthd = I915_READ(ACTHD);
361 } else {
362 error->ipeir = I915_READ(IPEIR_I965);
363 error->ipehr = I915_READ(IPEHR_I965);
364 error->instdone = I915_READ(INSTDONE_I965);
365 error->instps = I915_READ(INSTPS);
366 error->instdone1 = I915_READ(INSTDONE1);
367 error->acthd = I915_READ(ACTHD_I965);
368 }
369
Jesse Barnes8a905232009-07-11 16:48:03 -0400370 do_gettimeofday(&error->time);
371
Jesse Barnes63eeaf32009-06-18 16:56:52 -0700372 dev_priv->first_error = error;
373
374out:
375 spin_unlock_irqrestore(&dev_priv->error_lock, flags);
376}
377
Jesse Barnes8a905232009-07-11 16:48:03 -0400378/**
379 * i915_handle_error - handle an error interrupt
380 * @dev: drm device
381 *
382 * Do some basic checking of regsiter state at error interrupt time and
383 * dump it to the syslog. Also call i915_capture_error_state() to make
384 * sure we get a record and make it available in debugfs. Fire a uevent
385 * so userspace knows something bad happened (should trigger collection
386 * of a ring dump etc.).
387 */
388static void i915_handle_error(struct drm_device *dev)
389{
390 struct drm_i915_private *dev_priv = dev->dev_private;
391 u32 eir = I915_READ(EIR);
392 u32 pipea_stats = I915_READ(PIPEASTAT);
393 u32 pipeb_stats = I915_READ(PIPEBSTAT);
394
395 i915_capture_error_state(dev);
396
397 printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
398 eir);
399
400 if (IS_G4X(dev)) {
401 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
402 u32 ipeir = I915_READ(IPEIR_I965);
403
404 printk(KERN_ERR " IPEIR: 0x%08x\n",
405 I915_READ(IPEIR_I965));
406 printk(KERN_ERR " IPEHR: 0x%08x\n",
407 I915_READ(IPEHR_I965));
408 printk(KERN_ERR " INSTDONE: 0x%08x\n",
409 I915_READ(INSTDONE_I965));
410 printk(KERN_ERR " INSTPS: 0x%08x\n",
411 I915_READ(INSTPS));
412 printk(KERN_ERR " INSTDONE1: 0x%08x\n",
413 I915_READ(INSTDONE1));
414 printk(KERN_ERR " ACTHD: 0x%08x\n",
415 I915_READ(ACTHD_I965));
416 I915_WRITE(IPEIR_I965, ipeir);
417 (void)I915_READ(IPEIR_I965);
418 }
419 if (eir & GM45_ERROR_PAGE_TABLE) {
420 u32 pgtbl_err = I915_READ(PGTBL_ER);
421 printk(KERN_ERR "page table error\n");
422 printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
423 pgtbl_err);
424 I915_WRITE(PGTBL_ER, pgtbl_err);
425 (void)I915_READ(PGTBL_ER);
426 }
427 }
428
429 if (IS_I9XX(dev)) {
430 if (eir & I915_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 (eir & I915_ERROR_MEMORY_REFRESH) {
441 printk(KERN_ERR "memory refresh error\n");
442 printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
443 pipea_stats);
444 printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
445 pipeb_stats);
446 /* pipestat has already been acked */
447 }
448 if (eir & I915_ERROR_INSTRUCTION) {
449 printk(KERN_ERR "instruction error\n");
450 printk(KERN_ERR " INSTPM: 0x%08x\n",
451 I915_READ(INSTPM));
452 if (!IS_I965G(dev)) {
453 u32 ipeir = I915_READ(IPEIR);
454
455 printk(KERN_ERR " IPEIR: 0x%08x\n",
456 I915_READ(IPEIR));
457 printk(KERN_ERR " IPEHR: 0x%08x\n",
458 I915_READ(IPEHR));
459 printk(KERN_ERR " INSTDONE: 0x%08x\n",
460 I915_READ(INSTDONE));
461 printk(KERN_ERR " ACTHD: 0x%08x\n",
462 I915_READ(ACTHD));
463 I915_WRITE(IPEIR, ipeir);
464 (void)I915_READ(IPEIR);
465 } else {
466 u32 ipeir = I915_READ(IPEIR_I965);
467
468 printk(KERN_ERR " IPEIR: 0x%08x\n",
469 I915_READ(IPEIR_I965));
470 printk(KERN_ERR " IPEHR: 0x%08x\n",
471 I915_READ(IPEHR_I965));
472 printk(KERN_ERR " INSTDONE: 0x%08x\n",
473 I915_READ(INSTDONE_I965));
474 printk(KERN_ERR " INSTPS: 0x%08x\n",
475 I915_READ(INSTPS));
476 printk(KERN_ERR " INSTDONE1: 0x%08x\n",
477 I915_READ(INSTDONE1));
478 printk(KERN_ERR " ACTHD: 0x%08x\n",
479 I915_READ(ACTHD_I965));
480 I915_WRITE(IPEIR_I965, ipeir);
481 (void)I915_READ(IPEIR_I965);
482 }
483 }
484
485 I915_WRITE(EIR, eir);
486 (void)I915_READ(EIR);
487 eir = I915_READ(EIR);
488 if (eir) {
489 /*
490 * some errors might have become stuck,
491 * mask them.
492 */
493 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
494 I915_WRITE(EMR, I915_READ(EMR) | eir);
495 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
496 }
497
Ben Gamari11ed50e2009-09-14 17:48:45 -0400498 if (dev_priv->mm.wedged) {
499 /*
500 * Wakeup waiting processes so they don't hang
501 */
502 printk("i915: Waking up sleeping processes\n");
503 DRM_WAKEUP(&dev_priv->irq_queue);
504 }
505
Eric Anholt9c9fe1f2009-08-03 16:09:16 -0700506 queue_work(dev_priv->wq, &dev_priv->error_work);
Jesse Barnes8a905232009-07-11 16:48:03 -0400507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
510{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000511 struct drm_device *dev = (struct drm_device *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000513 struct drm_i915_master_private *master_priv;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800514 u32 iir, new_iir;
515 u32 pipea_stats, pipeb_stats;
Keith Packard05eff842008-11-19 14:03:05 -0800516 u32 vblank_status;
517 u32 vblank_enable;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700518 int vblank = 0;
Keith Packard7c463582008-11-04 02:03:27 -0800519 unsigned long irqflags;
Keith Packard05eff842008-11-19 14:03:05 -0800520 int irq_received;
521 int ret = IRQ_NONE;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000522
Eric Anholt630681d2008-10-06 15:14:12 -0700523 atomic_inc(&dev_priv->irq_received);
524
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800525 if (IS_IGDNG(dev))
526 return igdng_irq_handler(dev);
527
Eric Anholted4cb412008-07-29 12:10:39 -0700528 iir = I915_READ(IIR);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000529
Keith Packard05eff842008-11-19 14:03:05 -0800530 if (IS_I965G(dev)) {
531 vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
532 vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
533 } else {
534 vblank_status = I915_VBLANK_INTERRUPT_STATUS;
535 vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Keith Packard05eff842008-11-19 14:03:05 -0800538 for (;;) {
539 irq_received = iir != 0;
540
541 /* Can't rely on pipestat interrupt bit in iir as it might
542 * have been cleared after the pipestat interrupt was received.
543 * It doesn't set the bit in iir again, but it still produces
544 * interrupts (for non-MSI).
545 */
546 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
547 pipea_stats = I915_READ(PIPEASTAT);
548 pipeb_stats = I915_READ(PIPEBSTAT);
Jesse Barnes79e53942008-11-07 14:24:08 -0800549
Jesse Barnes8a905232009-07-11 16:48:03 -0400550 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
551 i915_handle_error(dev);
552
Eric Anholtcdfbc412008-11-04 15:50:30 -0800553 /*
554 * Clear the PIPE(A|B)STAT regs before the IIR
555 */
Keith Packard05eff842008-11-19 14:03:05 -0800556 if (pipea_stats & 0x8000ffff) {
Shaohua Li7662c8b2009-06-26 11:23:55 +0800557 if (pipea_stats & PIPE_FIFO_UNDERRUN_STATUS)
558 DRM_DEBUG("pipe a underrun\n");
Eric Anholtcdfbc412008-11-04 15:50:30 -0800559 I915_WRITE(PIPEASTAT, pipea_stats);
Keith Packard05eff842008-11-19 14:03:05 -0800560 irq_received = 1;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800561 }
Keith Packard7c463582008-11-04 02:03:27 -0800562
Keith Packard05eff842008-11-19 14:03:05 -0800563 if (pipeb_stats & 0x8000ffff) {
Shaohua Li7662c8b2009-06-26 11:23:55 +0800564 if (pipeb_stats & PIPE_FIFO_UNDERRUN_STATUS)
565 DRM_DEBUG("pipe b underrun\n");
Eric Anholtcdfbc412008-11-04 15:50:30 -0800566 I915_WRITE(PIPEBSTAT, pipeb_stats);
Keith Packard05eff842008-11-19 14:03:05 -0800567 irq_received = 1;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800568 }
Keith Packard05eff842008-11-19 14:03:05 -0800569 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
570
571 if (!irq_received)
572 break;
573
574 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Jesse Barnes5ca58282009-03-31 14:11:15 -0700576 /* Consume port. Then clear IIR or we'll miss events */
577 if ((I915_HAS_HOTPLUG(dev)) &&
578 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
579 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
580
581 DRM_DEBUG("hotplug event received, stat 0x%08x\n",
582 hotplug_status);
583 if (hotplug_status & dev_priv->hotplug_supported_mask)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -0700584 queue_work(dev_priv->wq,
585 &dev_priv->hotplug_work);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700586
587 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
588 I915_READ(PORT_HOTPLUG_STAT);
Shaohua Li04302962009-08-24 10:25:23 +0800589
590 /* EOS interrupts occurs */
591 if (IS_IGD(dev) &&
592 (hotplug_status & CRT_EOS_INT_STATUS)) {
593 u32 temp;
594
595 DRM_DEBUG("EOS interrupt occurs\n");
596 /* status is already cleared */
597 temp = I915_READ(ADPA);
598 temp &= ~ADPA_DAC_ENABLE;
599 I915_WRITE(ADPA, temp);
600
601 temp = I915_READ(PORT_HOTPLUG_EN);
602 temp &= ~CRT_EOS_INT_EN;
603 I915_WRITE(PORT_HOTPLUG_EN, temp);
604
605 temp = I915_READ(PORT_HOTPLUG_STAT);
606 if (temp & CRT_EOS_INT_STATUS)
607 I915_WRITE(PORT_HOTPLUG_STAT,
608 CRT_EOS_INT_STATUS);
609 }
Jesse Barnes5ca58282009-03-31 14:11:15 -0700610 }
611
Eric Anholtcdfbc412008-11-04 15:50:30 -0800612 I915_WRITE(IIR, iir);
613 new_iir = I915_READ(IIR); /* Flush posted writes */
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100614
Dave Airlie7c1c2872008-11-28 14:22:24 +1000615 if (dev->primary->master) {
616 master_priv = dev->primary->master->driver_priv;
617 if (master_priv->sarea_priv)
618 master_priv->sarea_priv->last_dispatch =
619 READ_BREADCRUMB(dev_priv);
620 }
Keith Packard7c463582008-11-04 02:03:27 -0800621
Eric Anholtcdfbc412008-11-04 15:50:30 -0800622 if (iir & I915_USER_INTERRUPT) {
623 dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
624 DRM_WAKEUP(&dev_priv->irq_queue);
Ben Gamarif65d9422009-09-14 17:48:44 -0400625 dev_priv->hangcheck_count = 0;
626 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
Eric Anholtcdfbc412008-11-04 15:50:30 -0800627 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700628
Keith Packard05eff842008-11-19 14:03:05 -0800629 if (pipea_stats & vblank_status) {
Eric Anholtcdfbc412008-11-04 15:50:30 -0800630 vblank++;
631 drm_handle_vblank(dev, 0);
632 }
Eric Anholt673a3942008-07-30 12:06:12 -0700633
Keith Packard05eff842008-11-19 14:03:05 -0800634 if (pipeb_stats & vblank_status) {
Eric Anholtcdfbc412008-11-04 15:50:30 -0800635 vblank++;
636 drm_handle_vblank(dev, 1);
637 }
Keith Packard7c463582008-11-04 02:03:27 -0800638
Eric Anholtcdfbc412008-11-04 15:50:30 -0800639 if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
640 (iir & I915_ASLE_INTERRUPT))
641 opregion_asle_intr(dev);
Keith Packard7c463582008-11-04 02:03:27 -0800642
Eric Anholtcdfbc412008-11-04 15:50:30 -0800643 /* With MSI, interrupts are only generated when iir
644 * transitions from zero to nonzero. If another bit got
645 * set while we were handling the existing iir bits, then
646 * we would never get another interrupt.
647 *
648 * This is fine on non-MSI as well, as if we hit this path
649 * we avoid exiting the interrupt handler only to generate
650 * another one.
651 *
652 * Note that for MSI this could cause a stray interrupt report
653 * if an interrupt landed in the time between writing IIR and
654 * the posting read. This should be rare enough to never
655 * trigger the 99% of 100,000 interrupts test for disabling
656 * stray interrupts.
657 */
658 iir = new_iir;
Keith Packard05eff842008-11-19 14:03:05 -0800659 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700660
Keith Packard05eff842008-11-19 14:03:05 -0800661 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Dave Airlieaf6061a2008-05-07 12:15:39 +1000664static int i915_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000667 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 RING_LOCALS;
669
670 i915_kernel_lost_context(dev);
671
Márton Németh3e684ea2008-01-24 15:58:57 +1000672 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400674 dev_priv->counter++;
Alan Hourihanec29b6692006-08-12 16:29:24 +1000675 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400676 dev_priv->counter = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000677 if (master_priv->sarea_priv)
678 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Alan Hourihanec29b6692006-08-12 16:29:24 +1000679
Keith Packard0baf8232008-11-08 11:44:14 +1000680 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700681 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000682 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000683 OUT_RING(dev_priv->counter);
Jesse Barnes585fb112008-07-29 11:54:06 -0700684 OUT_RING(MI_USER_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 ADVANCE_LP_RING();
Dave Airliebc5f4522007-11-05 12:50:58 +1000686
Alan Hourihanec29b6692006-08-12 16:29:24 +1000687 return dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Eric Anholt673a3942008-07-30 12:06:12 -0700690void i915_user_irq_get(struct drm_device *dev)
Eric Anholted4cb412008-07-29 12:10:39 -0700691{
692 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700693 unsigned long irqflags;
Eric Anholted4cb412008-07-29 12:10:39 -0700694
Keith Packarde9d21d72008-10-16 11:31:38 -0700695 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800696 if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
697 if (IS_IGDNG(dev))
698 igdng_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
699 else
700 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
701 }
Keith Packarde9d21d72008-10-16 11:31:38 -0700702 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700703}
704
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700705void i915_user_irq_put(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);
Eric Anholted4cb412008-07-29 12:10:39 -0700711 BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800712 if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
713 if (IS_IGDNG(dev))
714 igdng_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
715 else
716 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
717 }
Keith Packarde9d21d72008-10-16 11:31:38 -0700718 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700719}
720
Dave Airlie84b1fd12007-07-11 15:53:27 +1000721static int i915_wait_irq(struct drm_device * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000724 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int ret = 0;
726
Márton Németh3e684ea2008-01-24 15:58:57 +1000727 DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 READ_BREADCRUMB(dev_priv));
729
Eric Anholted4cb412008-07-29 12:10:39 -0700730 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000731 if (master_priv->sarea_priv)
732 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
Eric Anholted4cb412008-07-29 12:10:39 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Dave Airlie7c1c2872008-11-28 14:22:24 +1000736 if (master_priv->sarea_priv)
737 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Eric Anholted4cb412008-07-29 12:10:39 -0700739 i915_user_irq_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
741 READ_BREADCRUMB(dev_priv) >= irq_nr);
Eric Anholted4cb412008-07-29 12:10:39 -0700742 i915_user_irq_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Eric Anholt20caafa2007-08-25 19:22:43 +1000744 if (ret == -EBUSY) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000745 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
747 }
748
Dave Airlieaf6061a2008-05-07 12:15:39 +1000749 return ret;
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/* Needs the lock as it touches the ring.
753 */
Eric Anholtc153f452007-09-03 12:06:45 +1000754int i915_irq_emit(struct drm_device *dev, void *data,
755 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000758 drm_i915_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 int result;
760
Eric Anholt07f4f8b2009-04-16 13:46:12 -0700761 if (!dev_priv || !dev_priv->ring.virtual_start) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000762 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000763 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Eric Anholt299eb932009-02-24 22:14:12 -0800765
766 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
767
Eric Anholt546b0972008-09-01 16:45:29 -0700768 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 result = i915_emit_irq(dev);
Eric Anholt546b0972008-09-01 16:45:29 -0700770 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Eric Anholtc153f452007-09-03 12:06:45 +1000772 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000774 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
777 return 0;
778}
779
780/* Doesn't need the hardware lock.
781 */
Eric Anholtc153f452007-09-03 12:06:45 +1000782int i915_irq_wait(struct drm_device *dev, void *data,
783 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000786 drm_i915_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000789 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000790 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
Eric Anholtc153f452007-09-03 12:06:45 +1000793 return i915_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Keith Packard42f52ef2008-10-18 19:39:29 -0700796/* Called from drm generic code, passed 'crtc' which
797 * we use as a pipe index
798 */
799int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700800{
801 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700802 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -0800803 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
804 u32 pipeconf;
805
806 pipeconf = I915_READ(pipeconf_reg);
807 if (!(pipeconf & PIPEACONF_ENABLE))
808 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700809
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800810 if (IS_IGDNG(dev))
811 return 0;
812
Keith Packarde9d21d72008-10-16 11:31:38 -0700813 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Keith Packarde9d21d72008-10-16 11:31:38 -0700814 if (IS_I965G(dev))
Keith Packard7c463582008-11-04 02:03:27 -0800815 i915_enable_pipestat(dev_priv, pipe,
816 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700817 else
Keith Packard7c463582008-11-04 02:03:27 -0800818 i915_enable_pipestat(dev_priv, pipe,
819 PIPE_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700820 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700821 return 0;
822}
823
Keith Packard42f52ef2008-10-18 19:39:29 -0700824/* Called from drm generic code, passed 'crtc' which
825 * we use as a pipe index
826 */
827void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700828{
829 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700830 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700831
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800832 if (IS_IGDNG(dev))
833 return;
834
Keith Packarde9d21d72008-10-16 11:31:38 -0700835 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Keith Packard7c463582008-11-04 02:03:27 -0800836 i915_disable_pipestat(dev_priv, pipe,
837 PIPE_VBLANK_INTERRUPT_ENABLE |
838 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700839 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700840}
841
Jesse Barnes79e53942008-11-07 14:24:08 -0800842void i915_enable_interrupt (struct drm_device *dev)
843{
844 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wange170b032009-06-05 15:38:40 +0800845
846 if (!IS_IGDNG(dev))
847 opregion_enable_asle(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800848 dev_priv->irq_enabled = 1;
849}
850
851
Dave Airlie702880f2006-06-24 17:07:34 +1000852/* Set the vblank monitor pipe
853 */
Eric Anholtc153f452007-09-03 12:06:45 +1000854int i915_vblank_pipe_set(struct drm_device *dev, void *data,
855 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000856{
Dave Airlie702880f2006-06-24 17:07:34 +1000857 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie702880f2006-06-24 17:07:34 +1000858
859 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000860 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000861 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000862 }
863
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +1000864 return 0;
Dave Airlie702880f2006-06-24 17:07:34 +1000865}
866
Eric Anholtc153f452007-09-03 12:06:45 +1000867int i915_vblank_pipe_get(struct drm_device *dev, void *data,
868 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000869{
Dave Airlie702880f2006-06-24 17:07:34 +1000870 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000871 drm_i915_vblank_pipe_t *pipe = data;
Dave Airlie702880f2006-06-24 17:07:34 +1000872
873 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000874 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000875 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000876 }
877
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700878 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Eric Anholtc153f452007-09-03 12:06:45 +1000879
Dave Airlie702880f2006-06-24 17:07:34 +1000880 return 0;
881}
882
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000883/**
884 * Schedule buffer swap at given vertical blank.
885 */
Eric Anholtc153f452007-09-03 12:06:45 +1000886int i915_vblank_swap(struct drm_device *dev, void *data,
887 struct drm_file *file_priv)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000888{
Eric Anholtbd95e0a2008-11-04 12:01:24 -0800889 /* The delayed swap mechanism was fundamentally racy, and has been
890 * removed. The model was that the client requested a delayed flip/swap
891 * from the kernel, then waited for vblank before continuing to perform
892 * rendering. The problem was that the kernel might wake the client
893 * up before it dispatched the vblank swap (since the lock has to be
894 * held while touching the ringbuffer), in which case the client would
895 * clear and start the next frame before the swap occurred, and
896 * flicker would occur in addition to likely missing the vblank.
897 *
898 * In the absence of this ioctl, userland falls back to a correct path
899 * of waiting for a vblank, then dispatching the swap on its own.
900 * Context switching to userland and back is plenty fast enough for
901 * meeting the requirements of vblank swapping.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700902 */
Eric Anholtbd95e0a2008-11-04 12:01:24 -0800903 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000904}
905
Ben Gamarif65d9422009-09-14 17:48:44 -0400906struct drm_i915_gem_request *i915_get_tail_request(struct drm_device *dev) {
907 drm_i915_private_t *dev_priv = dev->dev_private;
908 return list_entry(dev_priv->mm.request_list.prev, struct drm_i915_gem_request, list);
909}
910
911/**
912 * This is called when the chip hasn't reported back with completed
913 * batchbuffers in a long time. The first time this is called we simply record
914 * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
915 * again, we assume the chip is wedged and try to fix it.
916 */
917void i915_hangcheck_elapsed(unsigned long data)
918{
919 struct drm_device *dev = (struct drm_device *)data;
920 drm_i915_private_t *dev_priv = dev->dev_private;
921 uint32_t acthd;
922
923 if (!IS_I965G(dev))
924 acthd = I915_READ(ACTHD);
925 else
926 acthd = I915_READ(ACTHD_I965);
927
928 /* If all work is done then ACTHD clearly hasn't advanced. */
929 if (list_empty(&dev_priv->mm.request_list) ||
930 i915_seqno_passed(i915_get_gem_seqno(dev), i915_get_tail_request(dev)->seqno)) {
931 dev_priv->hangcheck_count = 0;
932 return;
933 }
934
935 if (dev_priv->last_acthd == acthd && dev_priv->hangcheck_count > 0) {
936 DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
937 dev_priv->mm.wedged = true; /* Hopefully this is atomic */
938 i915_handle_error(dev);
939 return;
940 }
941
942 /* Reset timer case chip hangs without another request being added */
943 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
944
945 if (acthd != dev_priv->last_acthd)
946 dev_priv->hangcheck_count = 0;
947 else
948 dev_priv->hangcheck_count++;
949
950 dev_priv->last_acthd = acthd;
951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953/* drm_dma.h hooks
954*/
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800955static void igdng_irq_preinstall(struct drm_device *dev)
956{
957 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
958
959 I915_WRITE(HWSTAM, 0xeffe);
960
961 /* XXX hotplug from PCH */
962
963 I915_WRITE(DEIMR, 0xffffffff);
964 I915_WRITE(DEIER, 0x0);
965 (void) I915_READ(DEIER);
966
967 /* and GT */
968 I915_WRITE(GTIMR, 0xffffffff);
969 I915_WRITE(GTIER, 0x0);
970 (void) I915_READ(GTIER);
971}
972
973static int igdng_irq_postinstall(struct drm_device *dev)
974{
975 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
976 /* enable kind of interrupts always enabled */
977 u32 display_mask = DE_MASTER_IRQ_CONTROL /*| DE_PCH_EVENT */;
978 u32 render_mask = GT_USER_INTERRUPT;
979
980 dev_priv->irq_mask_reg = ~display_mask;
981 dev_priv->de_irq_enable_reg = display_mask;
982
983 /* should always can generate irq */
984 I915_WRITE(DEIIR, I915_READ(DEIIR));
985 I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
986 I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
987 (void) I915_READ(DEIER);
988
989 /* user interrupt should be enabled, but masked initial */
990 dev_priv->gt_irq_mask_reg = 0xffffffff;
991 dev_priv->gt_irq_enable_reg = render_mask;
992
993 I915_WRITE(GTIIR, I915_READ(GTIIR));
994 I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
995 I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
996 (void) I915_READ(GTIER);
997
998 return 0;
999}
1000
Dave Airlie84b1fd12007-07-11 15:53:27 +10001001void i915_driver_irq_preinstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1004
Jesse Barnes79e53942008-11-07 14:24:08 -08001005 atomic_set(&dev_priv->irq_received, 0);
1006
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001007 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Jesse Barnes8a905232009-07-11 16:48:03 -04001008 INIT_WORK(&dev_priv->error_work, i915_error_work_func);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001009
1010 if (IS_IGDNG(dev)) {
1011 igdng_irq_preinstall(dev);
1012 return;
1013 }
1014
Jesse Barnes5ca58282009-03-31 14:11:15 -07001015 if (I915_HAS_HOTPLUG(dev)) {
1016 I915_WRITE(PORT_HOTPLUG_EN, 0);
1017 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1018 }
1019
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001020 I915_WRITE(HWSTAM, 0xeffe);
Keith Packard7c463582008-11-04 02:03:27 -08001021 I915_WRITE(PIPEASTAT, 0);
1022 I915_WRITE(PIPEBSTAT, 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001023 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07001024 I915_WRITE(IER, 0x0);
Keith Packard7c463582008-11-04 02:03:27 -08001025 (void) I915_READ(IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001028int i915_driver_irq_postinstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes5ca58282009-03-31 14:11:15 -07001031 u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001032 u32 error_mask;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001033
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001034 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
1035
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001036 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001037
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001038 if (IS_IGDNG(dev))
1039 return igdng_irq_postinstall(dev);
1040
Keith Packard7c463582008-11-04 02:03:27 -08001041 /* Unmask the interrupts that we always want on. */
1042 dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001043
Keith Packard7c463582008-11-04 02:03:27 -08001044 dev_priv->pipestat[0] = 0;
1045 dev_priv->pipestat[1] = 0;
1046
Jesse Barnes5ca58282009-03-31 14:11:15 -07001047 if (I915_HAS_HOTPLUG(dev)) {
1048 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
1049
1050 /* Leave other bits alone */
1051 hotplug_en |= HOTPLUG_EN_MASK;
1052 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
1053
1054 dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
1055 TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
1056 SDVOB_HOTPLUG_INT_STATUS;
1057 if (IS_G4X(dev)) {
1058 dev_priv->hotplug_supported_mask |=
1059 HDMIB_HOTPLUG_INT_STATUS |
1060 HDMIC_HOTPLUG_INT_STATUS |
1061 HDMID_HOTPLUG_INT_STATUS;
1062 }
1063 /* Enable in IER... */
1064 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
1065 /* and unmask in IMR */
1066 i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
1067 }
1068
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001069 /*
1070 * Enable some error detection, note the instruction error mask
1071 * bit is reserved, so we leave it masked.
1072 */
1073 if (IS_G4X(dev)) {
1074 error_mask = ~(GM45_ERROR_PAGE_TABLE |
1075 GM45_ERROR_MEM_PRIV |
1076 GM45_ERROR_CP_PRIV |
1077 I915_ERROR_MEMORY_REFRESH);
1078 } else {
1079 error_mask = ~(I915_ERROR_PAGE_TABLE |
1080 I915_ERROR_MEMORY_REFRESH);
1081 }
1082 I915_WRITE(EMR, error_mask);
1083
Keith Packard7c463582008-11-04 02:03:27 -08001084 /* Disable pipe interrupt enables, clear pending pipe status */
1085 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
1086 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
1087 /* Clear pending interrupt status */
1088 I915_WRITE(IIR, I915_READ(IIR));
1089
Jesse Barnes5ca58282009-03-31 14:11:15 -07001090 I915_WRITE(IER, enable_mask);
Keith Packard7c463582008-11-04 02:03:27 -08001091 I915_WRITE(IMR, dev_priv->irq_mask_reg);
Eric Anholted4cb412008-07-29 12:10:39 -07001092 (void) I915_READ(IER);
1093
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001094 opregion_enable_asle(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001095
1096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001099static void igdng_irq_uninstall(struct drm_device *dev)
1100{
1101 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1102 I915_WRITE(HWSTAM, 0xffffffff);
1103
1104 I915_WRITE(DEIMR, 0xffffffff);
1105 I915_WRITE(DEIER, 0x0);
1106 I915_WRITE(DEIIR, I915_READ(DEIIR));
1107
1108 I915_WRITE(GTIMR, 0xffffffff);
1109 I915_WRITE(GTIER, 0x0);
1110 I915_WRITE(GTIIR, I915_READ(GTIIR));
1111}
1112
Dave Airlie84b1fd12007-07-11 15:53:27 +10001113void i915_driver_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie91e37382006-02-18 15:17:04 +11001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!dev_priv)
1118 return;
1119
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001120 dev_priv->vblank_pipe = 0;
1121
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001122 if (IS_IGDNG(dev)) {
1123 igdng_irq_uninstall(dev);
1124 return;
1125 }
1126
Jesse Barnes5ca58282009-03-31 14:11:15 -07001127 if (I915_HAS_HOTPLUG(dev)) {
1128 I915_WRITE(PORT_HOTPLUG_EN, 0);
1129 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
1130 }
1131
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001132 I915_WRITE(HWSTAM, 0xffffffff);
Keith Packard7c463582008-11-04 02:03:27 -08001133 I915_WRITE(PIPEASTAT, 0);
1134 I915_WRITE(PIPEBSTAT, 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001135 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -07001136 I915_WRITE(IER, 0x0);
Dave Airlie91e37382006-02-18 15:17:04 +11001137
Keith Packard7c463582008-11-04 02:03:27 -08001138 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
1139 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
1140 I915_WRITE(IIR, I915_READ(IIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}