1 /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
4 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
33 #include "intel_drv.h"
35 #define MAX_NOPID ((u32)~0)
38 * i915_get_pipe - return the the pipe associated with a given plane
40 * @plane: plane to look for
42 * The Intel Mesa & 2D drivers call the vblank routines with a plane number
43 * rather than a pipe number, since they may not always be equal. This routine
44 * maps the given @plane back to a pipe number.
47 i915_get_pipe(struct drm_device *dev, int plane)
49 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
52 dspcntr = plane ? I915_READ(DSPBCNTR) : I915_READ(DSPACNTR);
54 return dspcntr & DISPPLANE_SEL_PIPE_MASK ? 1 : 0;
58 * i915_get_plane - return the the plane associated with a given pipe
60 * @pipe: pipe to look for
62 * The Intel Mesa & 2D drivers call the vblank routines with a plane number
63 * rather than a plane number, since they may not always be equal. This routine
64 * maps the given @pipe back to a plane number.
67 i915_get_plane(struct drm_device *dev, int pipe)
69 if (i915_get_pipe(dev, 0) == pipe)
75 * i915_pipe_enabled - check if a pipe is enabled
77 * @pipe: pipe to check
79 * Reading certain registers when the pipe is disabled can hang the chip.
80 * Use this routine to make sure the PLL is running and the pipe is active
81 * before reading such registers if unsure.
84 i915_pipe_enabled(struct drm_device *dev, int pipe)
86 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
87 unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
89 if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
96 * Emit a synchronous flip.
98 * This function must be called with the drawable spinlock held.
101 i915_dispatch_vsync_flip(struct drm_device *dev, struct drm_drawable_info *drw,
104 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
105 struct drm_i915_sarea *sarea_priv = master_priv->sarea_priv;
107 int pf_planes = 1 << plane;
109 DRM_SPINLOCK_ASSERT(&dev->drw_lock);
111 /* If the window is visible on the other plane, we have to flip on that
115 x1 = sarea_priv->planeA_x;
116 y1 = sarea_priv->planeA_y;
117 x2 = x1 + sarea_priv->planeA_w;
118 y2 = y1 + sarea_priv->planeA_h;
120 x1 = sarea_priv->planeB_x;
121 y1 = sarea_priv->planeB_y;
122 x2 = x1 + sarea_priv->planeB_w;
123 y2 = y1 + sarea_priv->planeB_h;
126 if (x2 > 0 && y2 > 0) {
127 int i, num_rects = drw->num_rects;
128 struct drm_clip_rect *rect = drw->rects;
130 for (i = 0; i < num_rects; i++)
131 if (!(rect[i].x1 >= x2 || rect[i].y1 >= y2 ||
132 rect[i].x2 <= x1 || rect[i].y2 <= y1)) {
139 i915_dispatch_flip(dev, pf_planes, 1);
143 * Emit blits for scheduled buffer swaps.
145 * This function will be called with the HW lock held.
147 static void i915_vblank_tasklet(struct drm_device *dev)
149 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
150 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
151 struct list_head *list, *tmp, hits, *hit;
152 int nhits, nrects, slice[2], upper[2], lower[2], i, num_pages;
154 struct drm_drawable_info *drw;
155 struct drm_i915_sarea *sarea_priv = master_priv->sarea_priv;
156 u32 cpp = dev_priv->cpp, offsets[3];
157 u32 cmd = (cpp == 4) ? (XY_SRC_COPY_BLT_CMD |
158 XY_SRC_COPY_BLT_WRITE_ALPHA |
159 XY_SRC_COPY_BLT_WRITE_RGB)
160 : XY_SRC_COPY_BLT_CMD;
161 u32 pitchropcpp = (sarea_priv->pitch * cpp) | (0xcc << 16) |
162 (cpp << 23) | (1 << 24);
165 counter[0] = drm_vblank_count(dev, 0);
166 counter[1] = drm_vblank_count(dev, 1);
170 INIT_LIST_HEAD(&hits);
174 /* No irqsave/restore necessary. This tasklet may be run in an
175 * interrupt context or normal context, but we don't have to worry
176 * about getting interrupted by something acquiring the lock, because
177 * we are the interrupt context thing that acquires the lock.
179 DRM_SPINLOCK(&dev_priv->swaps_lock);
181 /* Find buffer swaps scheduled for this vertical blank */
182 list_for_each_safe(list, tmp, &dev_priv->vbl_swaps.head) {
183 struct drm_i915_vbl_swap *vbl_swap =
184 list_entry(list, struct drm_i915_vbl_swap, head);
185 int pipe = i915_get_pipe(dev, vbl_swap->plane);
187 if ((counter[pipe] - vbl_swap->sequence) > (1<<23))
190 master_priv = vbl_swap->minor->master->driver_priv;
191 sarea_priv = master_priv->sarea_priv;
193 pitchropcpp = (sarea_priv->pitch * cpp) | (0xcc << 16) |
194 (cpp << 23) | (1 << 24);
197 dev_priv->swaps_pending--;
198 drm_vblank_put(dev, pipe);
200 DRM_SPINUNLOCK(&dev_priv->swaps_lock);
201 DRM_SPINLOCK(&dev->drw_lock);
203 drw = drm_get_drawable_info(dev, vbl_swap->drw_id);
206 DRM_SPINUNLOCK(&dev->drw_lock);
207 drm_free(vbl_swap, sizeof(*vbl_swap), DRM_MEM_DRIVER);
208 DRM_SPINLOCK(&dev_priv->swaps_lock);
212 list_for_each(hit, &hits) {
213 struct drm_i915_vbl_swap *swap_cmp =
214 list_entry(hit, struct drm_i915_vbl_swap, head);
215 struct drm_drawable_info *drw_cmp =
216 drm_get_drawable_info(dev, swap_cmp->drw_id);
219 drw_cmp->rects[0].y1 > drw->rects[0].y1) {
220 list_add_tail(list, hit);
225 DRM_SPINUNLOCK(&dev->drw_lock);
227 /* List of hits was empty, or we reached the end of it */
229 list_add_tail(list, hits.prev);
233 DRM_SPINLOCK(&dev_priv->swaps_lock);
236 DRM_SPINUNLOCK(&dev_priv->swaps_lock);
242 i915_kernel_lost_context(dev);
244 upper[0] = upper[1] = 0;
245 slice[0] = max(sarea_priv->planeA_h / nhits, 1);
246 slice[1] = max(sarea_priv->planeB_h / nhits, 1);
247 lower[0] = sarea_priv->planeA_y + slice[0];
248 lower[1] = sarea_priv->planeB_y + slice[0];
250 offsets[0] = sarea_priv->front_offset;
251 offsets[1] = sarea_priv->back_offset;
252 offsets[2] = sarea_priv->third_offset;
253 num_pages = sarea_priv->third_handle ? 3 : 2;
255 DRM_SPINLOCK(&dev->drw_lock);
257 /* Emit blits for buffer swaps, partitioning both outputs into as many
258 * slices as there are buffer swaps scheduled in order to avoid tearing
259 * (based on the assumption that a single buffer swap would always
260 * complete before scanout starts).
262 for (i = 0; i++ < nhits;
263 upper[0] = lower[0], lower[0] += slice[0],
264 upper[1] = lower[1], lower[1] += slice[1]) {
265 int init_drawrect = 1;
268 lower[0] = lower[1] = sarea_priv->height;
270 list_for_each(hit, &hits) {
271 struct drm_i915_vbl_swap *swap_hit =
272 list_entry(hit, struct drm_i915_vbl_swap, head);
273 struct drm_clip_rect *rect;
274 int num_rects, plane, front, back;
275 unsigned short top, bottom;
277 drw = drm_get_drawable_info(dev, swap_hit->drw_id);
282 plane = swap_hit->plane;
284 if (swap_hit->flip) {
285 i915_dispatch_vsync_flip(dev, drw, plane);
292 OUT_RING(GFX_OP_DRAWRECT_INFO);
295 OUT_RING(sarea_priv->width | sarea_priv->height << 16);
296 OUT_RING(sarea_priv->width | sarea_priv->height << 16);
301 sarea_priv->ctxOwner = DRM_KERNEL_CONTEXT;
308 bottom = lower[plane];
310 front = (master_priv->sarea_priv->pf_current_page >>
312 back = (front + 1) % num_pages;
314 for (num_rects = drw->num_rects; num_rects--; rect++) {
315 int y1 = max(rect->y1, top);
316 int y2 = min(rect->y2, bottom);
324 OUT_RING(pitchropcpp);
325 OUT_RING((y1 << 16) | rect->x1);
326 OUT_RING((y2 << 16) | rect->x2);
327 OUT_RING(offsets[front]);
328 OUT_RING((y1 << 16) | rect->x1);
329 OUT_RING(pitchropcpp & 0xffff);
330 OUT_RING(offsets[back]);
337 DRM_SPINUNLOCK(&dev->drw_lock);
339 list_for_each_safe(hit, tmp, &hits) {
340 struct drm_i915_vbl_swap *swap_hit =
341 list_entry(hit, struct drm_i915_vbl_swap, head);
345 drm_free(swap_hit, sizeof(*swap_hit), DRM_MEM_DRIVER);
349 static int i915_in_vblank(struct drm_device *dev, int pipe)
351 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
352 unsigned long pipedsl, vblank, vtotal;
353 unsigned long vbl_start, vbl_end, cur_line;
355 pipedsl = pipe ? PIPEBDSL : PIPEADSL;
356 vblank = pipe ? VBLANK_B : VBLANK_A;
357 vtotal = pipe ? VTOTAL_B : VTOTAL_A;
359 vbl_start = I915_READ(vblank) & VBLANK_START_MASK;
360 vbl_end = (I915_READ(vblank) >> VBLANK_END_SHIFT) & VBLANK_END_MASK;
362 cur_line = I915_READ(pipedsl);
364 if (cur_line >= vbl_start)
370 u32 i915_get_vblank_counter(struct drm_device *dev, int plane)
372 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
373 unsigned long high_frame;
374 unsigned long low_frame;
375 u32 high1, high2, low, count;
378 pipe = i915_get_pipe(dev, plane);
379 high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
380 low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
382 if (!i915_pipe_enabled(dev, pipe)) {
383 printk(KERN_ERR "trying to get vblank count for disabled "
389 * High & low register fields aren't synchronized, so make sure
390 * we get a low value that's stable across two reads of the high
394 high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
395 PIPE_FRAME_HIGH_SHIFT);
396 low = ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
397 PIPE_FRAME_LOW_SHIFT);
398 high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
399 PIPE_FRAME_HIGH_SHIFT);
400 } while (high1 != high2);
402 count = (high1 << 8) | low;
405 * If we're in the middle of the vblank period, the
406 * above regs won't have been updated yet, so return
407 * an incremented count to stay accurate
410 if (i915_in_vblank(dev, pipe))
413 /* count may be reset by other driver(e.g. 2D driver),
414 we have no way to know if it is wrapped or resetted
415 when count is zero. do a rough guess.
417 if (count == 0 && dev->last_vblank[pipe] < dev->max_vblank_count/2)
418 dev->last_vblank[pipe] = 0;
423 #define HOTPLUG_CMD_CRT 1
424 #define HOTPLUG_CMD_CRT_DIS 2
425 #define HOTPLUG_CMD_SDVOB 4
426 #define HOTPLUG_CMD_SDVOC 8
428 static struct drm_device *hotplug_dev;
429 static int hotplug_cmd = 0;
430 static spinlock_t hotplug_lock = SPIN_LOCK_UNLOCKED;
432 static void i915_hotplug_crt(struct drm_device *dev, bool isconnected)
434 struct drm_output *output;
435 struct intel_output *iout;
437 mutex_lock(&dev->mode_config.mutex);
439 /* find the crt output */
440 list_for_each_entry(output, &dev->mode_config.output_list, head) {
441 iout = output->driver_private;
442 if (iout->type == INTEL_OUTPUT_ANALOG)
451 drm_hotplug_stage_two(dev, output, isconnected);
454 mutex_unlock(&dev->mode_config.mutex);
457 static void i915_hotplug_sdvo(struct drm_device *dev, int sdvoB)
459 struct drm_output *output = 0;
460 enum drm_output_status status;
462 mutex_lock(&dev->mode_config.mutex);
464 output = intel_sdvo_find(dev, sdvoB);
469 status = output->funcs->detect(output);
471 if (status != output_status_connected)
472 drm_hotplug_stage_two(dev, output, false);
474 drm_hotplug_stage_two(dev, output, true);
476 intel_sdvo_set_hotplug(output, 1);
479 mutex_unlock(&dev->mode_config.mutex);
482 * This code is called in a more safe envirmoent to handle the hotplugs.
483 * Add code here for hotplug love to userspace.
485 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
486 static void i915_hotplug_work_func(void *work)
488 static void i915_hotplug_work_func(struct work_struct *work)
491 struct drm_device *dev = hotplug_dev;
497 spin_lock(&hotplug_lock);
498 crt = hotplug_cmd & HOTPLUG_CMD_CRT;
499 crtDis = hotplug_cmd & HOTPLUG_CMD_CRT_DIS;
500 sdvoB = hotplug_cmd & HOTPLUG_CMD_SDVOB;
501 sdvoC = hotplug_cmd & HOTPLUG_CMD_SDVOC;
503 spin_unlock(&hotplug_lock);
506 i915_hotplug_crt(dev, true);
508 i915_hotplug_crt(dev, false);
511 i915_hotplug_sdvo(dev, 1);
514 i915_hotplug_sdvo(dev, 0);
516 drm_handle_hotplug(dev);
519 static int i915_run_hotplug_tasklet(struct drm_device *dev, uint32_t stat)
521 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
522 static DECLARE_WORK(hotplug, i915_hotplug_work_func, NULL);
524 static DECLARE_WORK(hotplug, i915_hotplug_work_func);
526 struct drm_i915_private *dev_priv = dev->dev_private;
530 if (stat & CRT_HOTPLUG_INT_STATUS) {
531 DRM_DEBUG("CRT event\n");
533 if (stat & CRT_HOTPLUG_MONITOR_MASK) {
534 spin_lock(&hotplug_lock);
535 hotplug_cmd |= HOTPLUG_CMD_CRT;
536 spin_unlock(&hotplug_lock);
538 spin_lock(&hotplug_lock);
539 hotplug_cmd |= HOTPLUG_CMD_CRT_DIS;
540 spin_unlock(&hotplug_lock);
544 if (stat & SDVOB_HOTPLUG_INT_STATUS) {
545 DRM_DEBUG("sDVOB event\n");
547 spin_lock(&hotplug_lock);
548 hotplug_cmd |= HOTPLUG_CMD_SDVOB;
549 spin_unlock(&hotplug_lock);
552 if (stat & SDVOC_HOTPLUG_INT_STATUS) {
553 DRM_DEBUG("sDVOC event\n");
555 spin_lock(&hotplug_lock);
556 hotplug_cmd |= HOTPLUG_CMD_SDVOC;
557 spin_unlock(&hotplug_lock);
560 queue_work(dev_priv->wq, &hotplug);
565 irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
567 struct drm_device *dev = (struct drm_device *) arg;
568 struct drm_i915_master_private *master_priv;
569 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
571 u32 pipea_stats, pipeb_stats;
575 /* On i8xx/i915 hw the IIR and IER are 16bit on i9xx its 32bit */
576 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev))
577 iir = I915_READ(I915REG_INT_IDENTITY_R);
579 iir = I915_READ16(I915REG_INT_IDENTITY_R);
581 iir &= (dev_priv->irq_enable_reg | I915_USER_INTERRUPT);
584 DRM_DEBUG("flag=%08x\n", iir);
587 DRM_DEBUG ("iir 0x%08x im 0x%08x ie 0x%08x pipea 0x%08x pipeb 0x%08x\n",
589 I915_READ(I915REG_INT_MASK_R),
590 I915_READ(I915REG_INT_ENABLE_R),
591 I915_READ(I915REG_PIPEASTAT),
592 I915_READ(I915REG_PIPEBSTAT));
597 * Clear the PIPE(A|B)STAT regs before the IIR otherwise
598 * we may get extra interrupts.
600 if (iir & I915_DISPLAY_PIPE_A_EVENT_INTERRUPT) {
601 pipea_stats = I915_READ(I915REG_PIPEASTAT);
602 if (pipea_stats & (I915_START_VBLANK_INTERRUPT_STATUS|
603 I915_VBLANK_INTERRUPT_STATUS))
606 drm_handle_vblank(dev, i915_get_plane(dev, 0));
609 /* This is a global event, and not a pipe A event */
610 if (pipea_stats & I915_HOTPLUG_INTERRUPT_STATUS)
613 I915_WRITE(I915REG_PIPEASTAT, pipea_stats);
616 if (iir & I915_DISPLAY_PIPE_B_EVENT_INTERRUPT) {
617 pipeb_stats = I915_READ(I915REG_PIPEBSTAT);
618 if (pipeb_stats & (I915_START_VBLANK_INTERRUPT_STATUS|
619 I915_VBLANK_INTERRUPT_STATUS))
622 drm_handle_vblank(dev, i915_get_plane(dev, 1));
624 I915_WRITE(I915REG_PIPEBSTAT, pipeb_stats);
627 /* Clear the generated interrupt */
628 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
629 I915_WRITE(I915REG_INT_IDENTITY_R, iir);
630 (void) I915_READ(I915REG_INT_IDENTITY_R);
632 I915_WRITE16(I915REG_INT_IDENTITY_R, iir);
633 (void) I915_READ16(I915REG_INT_IDENTITY_R);
636 if (dev->primary->master) {
637 master_priv = dev->primary->master->driver_priv;
638 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
641 if (iir & I915_USER_INTERRUPT) {
642 DRM_WAKEUP(&dev_priv->irq_queue);
643 #ifdef I915_HAVE_FENCE
644 i915_fence_handler(dev);
649 if (dev_priv->swaps_pending > 0)
650 drm_locked_tasklet(dev, i915_vblank_tasklet);
653 if ((iir & I915_DISPLAY_PORT_INTERRUPT) || hotplug) {
656 DRM_INFO("Hotplug event received\n");
658 if (!IS_I9XX(dev) || IS_I915G(dev) || IS_I915GM(dev)) {
659 temp2 |= SDVOB_HOTPLUG_INT_STATUS |
660 SDVOC_HOTPLUG_INT_STATUS;
662 temp2 = I915_READ(PORT_HOTPLUG_STAT);
664 I915_WRITE(PORT_HOTPLUG_STAT, temp2);
666 i915_run_hotplug_tasklet(dev, temp2);
672 int i915_emit_irq(struct drm_device *dev)
674 struct drm_i915_private *dev_priv = dev->dev_private;
677 i915_kernel_lost_context(dev);
681 i915_emit_breadcrumb(dev);
685 OUT_RING(GFX_OP_USER_INTERRUPT);
688 return dev_priv->counter;
691 void i915_user_irq_on(struct drm_device *dev)
693 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
695 DRM_SPINLOCK(&dev_priv->user_irq_lock);
696 if (dev_priv->irq_enabled && (++dev_priv->user_irq_refcount == 1)){
697 dev_priv->irq_enable_reg |= I915_USER_INTERRUPT;
698 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev))
699 I915_WRITE(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg);
701 I915_WRITE16(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg);
703 DRM_SPINUNLOCK(&dev_priv->user_irq_lock);
707 void i915_user_irq_off(struct drm_device *dev)
709 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
711 DRM_SPINLOCK(&dev_priv->user_irq_lock);
712 if (dev_priv->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
713 // dev_priv->irq_enable_reg &= ~I915_USER_INTERRUPT;
714 // if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev))
715 // I915_WRITE(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg);
717 // I915_WRITE16(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg);
719 DRM_SPINUNLOCK(&dev_priv->user_irq_lock);
723 static int i915_wait_irq(struct drm_device * dev, int irq_nr)
725 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
726 struct drm_i915_master_private *master_priv;
729 DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
730 READ_BREADCRUMB(dev_priv));
732 if (READ_BREADCRUMB(dev_priv) >= irq_nr)
735 i915_user_irq_on(dev);
736 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
737 READ_BREADCRUMB(dev_priv) >= irq_nr);
738 i915_user_irq_off(dev);
741 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
742 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
745 if (dev->primary->master) {
746 master_priv = dev->primary->master->driver_priv;
747 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
753 /* Needs the lock as it touches the ring.
755 int i915_irq_emit(struct drm_device *dev, void *data,
756 struct drm_file *file_priv)
758 struct drm_i915_private *dev_priv = dev->dev_private;
759 struct drm_i915_irq_emit *emit = data;
762 LOCK_TEST_WITH_RETURN(dev, file_priv);
765 DRM_ERROR("called with no initialization\n");
769 result = i915_emit_irq(dev);
771 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
772 DRM_ERROR("copy_to_user\n");
779 /* Doesn't need the hardware lock.
781 int i915_irq_wait(struct drm_device *dev, void *data,
782 struct drm_file *file_priv)
784 struct drm_i915_private *dev_priv = dev->dev_private;
785 struct drm_i915_irq_wait *irqwait = data;
788 DRM_ERROR("called with no initialization\n");
792 return i915_wait_irq(dev, irqwait->irq_seq);
795 int i915_enable_vblank(struct drm_device *dev, int plane)
797 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
798 int pipe = i915_get_pipe(dev, plane);
799 u32 pipestat_reg = 0;
804 pipestat_reg = I915REG_PIPEASTAT;
805 dev_priv->irq_enable_reg |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
808 pipestat_reg = I915REG_PIPEBSTAT;
809 dev_priv->irq_enable_reg |= I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
812 DRM_ERROR("tried to enable vblank on non-existent pipe %d\n",
819 pipestat = I915_READ (pipestat_reg);
821 * Older chips didn't have the start vblank interrupt,
825 pipestat |= I915_START_VBLANK_INTERRUPT_ENABLE;
827 pipestat |= I915_VBLANK_INTERRUPT_ENABLE;
829 * Clear any pending status
831 pipestat |= (I915_START_VBLANK_INTERRUPT_STATUS |
832 I915_VBLANK_INTERRUPT_STATUS);
833 I915_WRITE(pipestat_reg, pipestat);
836 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev))
837 I915_WRITE(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg);
839 I915_WRITE16(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg);
845 void i915_disable_vblank(struct drm_device *dev, int plane)
847 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
848 int pipe = i915_get_pipe(dev, plane);
849 u32 pipestat_reg = 0;
854 pipestat_reg = I915REG_PIPEASTAT;
855 dev_priv->irq_enable_reg &= ~I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
858 pipestat_reg = I915REG_PIPEBSTAT;
859 dev_priv->irq_enable_reg &= ~I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
862 DRM_ERROR("tried to disable vblank on non-existent pipe %d\n",
867 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev))
868 I915_WRITE(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg);
870 I915_WRITE16(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg);
874 pipestat = I915_READ (pipestat_reg);
875 pipestat &= ~(I915_START_VBLANK_INTERRUPT_ENABLE |
876 I915_VBLANK_INTERRUPT_ENABLE);
878 * Clear any pending status
880 pipestat |= (I915_START_VBLANK_INTERRUPT_STATUS |
881 I915_VBLANK_INTERRUPT_STATUS);
882 I915_WRITE(pipestat_reg, pipestat);
886 void i915_enable_interrupt (struct drm_device *dev)
888 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
889 struct drm_output *o;
891 dev_priv->irq_enable_reg |= I915_USER_INTERRUPT;
893 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
894 if (dev->mode_config.num_output)
895 dev_priv->irq_enable_reg |= I915_DISPLAY_PORT_INTERRUPT;
897 if (dev->mode_config.num_output)
898 dev_priv->irq_enable_reg |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
900 /* Enable global interrupts for hotplug - not a pipeA event */
901 I915_WRITE(I915REG_PIPEASTAT, I915_READ(I915REG_PIPEASTAT) | I915_HOTPLUG_INTERRUPT_ENABLE | I915_HOTPLUG_CLEAR);
904 if (dev_priv->irq_enable_reg & (I915_DISPLAY_PORT_INTERRUPT | I915_DISPLAY_PIPE_A_EVENT_INTERRUPT)) {
907 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
908 temp = I915_READ(PORT_HOTPLUG_EN);
910 /* Activate the CRT */
911 temp |= CRT_HOTPLUG_INT_EN;
916 o = intel_sdvo_find(dev, 1);
917 if (o && intel_sdvo_supports_hotplug(o)) {
918 intel_sdvo_set_hotplug(o, 1);
919 temp |= SDVOB_HOTPLUG_INT_EN;
923 o = intel_sdvo_find(dev, 0);
924 if (o && intel_sdvo_supports_hotplug(o)) {
925 intel_sdvo_set_hotplug(o, 1);
926 temp |= SDVOC_HOTPLUG_INT_EN;
929 I915_WRITE(SDVOB, I915_READ(SDVOB) | SDVO_INTERRUPT_ENABLE);
930 I915_WRITE(SDVOC, I915_READ(SDVOC) | SDVO_INTERRUPT_ENABLE);
935 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
936 I915_WRITE(PORT_HOTPLUG_EN, temp);
938 DRM_DEBUG("HEN %08x\n",I915_READ(PORT_HOTPLUG_EN));
939 DRM_DEBUG("HST %08x\n",I915_READ(PORT_HOTPLUG_STAT));
941 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
945 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev))
946 I915_WRITE(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg);
948 I915_WRITE16(I915REG_INT_ENABLE_R, dev_priv->irq_enable_reg);
950 dev_priv->irq_enabled = 1;
953 /* Set the vblank monitor pipe
955 int i915_vblank_pipe_set(struct drm_device *dev, void *data,
956 struct drm_file *file_priv)
958 struct drm_i915_private *dev_priv = dev->dev_private;
959 struct drm_i915_vblank_pipe *pipe = data;
962 DRM_ERROR("called with no initialization\n");
966 if (pipe->pipe & ~(DRM_I915_VBLANK_PIPE_A|DRM_I915_VBLANK_PIPE_B)) {
967 DRM_ERROR("called with invalid pipe 0x%x\n", pipe->pipe);
971 dev_priv->vblank_pipe = pipe->pipe;
976 int i915_vblank_pipe_get(struct drm_device *dev, void *data,
977 struct drm_file *file_priv)
979 struct drm_i915_private *dev_priv = dev->dev_private;
980 struct drm_i915_vblank_pipe *pipe = data;
984 DRM_ERROR("called with no initialization\n");
988 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev))
989 flag = I915_READ(I915REG_INT_ENABLE_R);
991 flag = I915_READ16(I915REG_INT_ENABLE_R);
994 if (flag & I915_DISPLAY_PIPE_A_EVENT_INTERRUPT)
995 pipe->pipe |= DRM_I915_VBLANK_PIPE_A;
996 if (flag & I915_DISPLAY_PIPE_B_EVENT_INTERRUPT)
997 pipe->pipe |= DRM_I915_VBLANK_PIPE_B;
1003 * Schedule buffer swap at given vertical blank.
1005 int i915_vblank_swap(struct drm_device *dev, void *data,
1006 struct drm_file *file_priv)
1008 struct drm_i915_private *dev_priv = dev->dev_private;
1009 struct drm_i915_master_private *master_priv;
1010 struct drm_i915_vblank_swap *swap = data;
1011 struct drm_i915_vbl_swap *vbl_swap;
1012 unsigned int pipe, seqtype, curseq, plane;
1013 unsigned long irqflags;
1014 struct list_head *list;
1018 DRM_ERROR("%s called with no initialization\n", __func__);
1022 if (!dev->primary->master)
1025 master_priv = dev->primary->master->driver_priv;
1027 if (master_priv->sarea_priv->rotation) {
1028 DRM_DEBUG("Rotation not supported\n");
1032 if (swap->seqtype & ~(_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE |
1033 _DRM_VBLANK_SECONDARY | _DRM_VBLANK_NEXTONMISS |
1034 _DRM_VBLANK_FLIP)) {
1035 DRM_ERROR("Invalid sequence type 0x%x\n", swap->seqtype);
1039 plane = (swap->seqtype & _DRM_VBLANK_SECONDARY) ? 1 : 0;
1040 pipe = i915_get_pipe(dev, plane);
1042 seqtype = swap->seqtype & (_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE);
1044 if (!(dev_priv->vblank_pipe & (1 << pipe))) {
1045 DRM_ERROR("Invalid pipe %d\n", pipe);
1049 DRM_SPINLOCK_IRQSAVE(&dev->drw_lock, irqflags);
1051 /* It makes no sense to schedule a swap for a drawable that doesn't have
1052 * valid information at this point. E.g. this could mean that the X
1053 * server is too old to push drawable information to the DRM, in which
1054 * case all such swaps would become ineffective.
1056 if (!drm_get_drawable_info(dev, swap->drawable)) {
1057 DRM_SPINUNLOCK_IRQRESTORE(&dev->drw_lock, irqflags);
1058 DRM_DEBUG("Invalid drawable ID %d\n", swap->drawable);
1062 DRM_SPINUNLOCK_IRQRESTORE(&dev->drw_lock, irqflags);
1064 drm_update_vblank_count(dev, pipe);
1065 curseq = drm_vblank_count(dev, pipe);
1067 if (seqtype == _DRM_VBLANK_RELATIVE)
1068 swap->sequence += curseq;
1070 if ((curseq - swap->sequence) <= (1<<23)) {
1071 if (swap->seqtype & _DRM_VBLANK_NEXTONMISS) {
1072 swap->sequence = curseq + 1;
1074 DRM_DEBUG("Missed target sequence\n");
1079 if (swap->seqtype & _DRM_VBLANK_FLIP) {
1082 if ((curseq - swap->sequence) <= (1<<23)) {
1083 struct drm_drawable_info *drw;
1085 LOCK_TEST_WITH_RETURN(dev, file_priv);
1087 DRM_SPINLOCK_IRQSAVE(&dev->drw_lock, irqflags);
1089 drw = drm_get_drawable_info(dev, swap->drawable);
1092 DRM_SPINUNLOCK_IRQRESTORE(&dev->drw_lock,
1094 DRM_DEBUG("Invalid drawable ID %d\n",
1099 i915_dispatch_vsync_flip(dev, drw, plane);
1101 DRM_SPINUNLOCK_IRQRESTORE(&dev->drw_lock, irqflags);
1107 DRM_SPINLOCK_IRQSAVE(&dev_priv->swaps_lock, irqflags);
1109 list_for_each(list, &dev_priv->vbl_swaps.head) {
1110 vbl_swap = list_entry(list, struct drm_i915_vbl_swap, head);
1112 if (vbl_swap->drw_id == swap->drawable &&
1113 vbl_swap->plane == plane &&
1114 vbl_swap->sequence == swap->sequence) {
1115 vbl_swap->flip = (swap->seqtype & _DRM_VBLANK_FLIP);
1116 DRM_SPINUNLOCK_IRQRESTORE(&dev_priv->swaps_lock, irqflags);
1117 DRM_DEBUG("Already scheduled\n");
1122 DRM_SPINUNLOCK_IRQRESTORE(&dev_priv->swaps_lock, irqflags);
1124 if (dev_priv->swaps_pending >= 100) {
1125 DRM_DEBUG("Too many swaps queued\n");
1129 vbl_swap = drm_calloc(1, sizeof(*vbl_swap), DRM_MEM_DRIVER);
1132 DRM_ERROR("Failed to allocate memory to queue swap\n");
1138 ret = drm_vblank_get(dev, pipe);
1140 drm_free(vbl_swap, sizeof(*vbl_swap), DRM_MEM_DRIVER);
1144 vbl_swap->drw_id = swap->drawable;
1145 vbl_swap->plane = plane;
1146 vbl_swap->sequence = swap->sequence;
1147 vbl_swap->flip = (swap->seqtype & _DRM_VBLANK_FLIP);
1148 vbl_swap->minor = file_priv->minor;
1153 DRM_SPINLOCK_IRQSAVE(&dev_priv->swaps_lock, irqflags);
1155 list_add_tail(&vbl_swap->head, &dev_priv->vbl_swaps.head);
1156 dev_priv->swaps_pending++;
1158 DRM_SPINUNLOCK_IRQRESTORE(&dev_priv->swaps_lock, irqflags);
1165 void i915_driver_irq_preinstall(struct drm_device * dev)
1167 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
1169 I915_WRITE16(I915REG_HWSTAM, 0xeffe);
1170 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
1171 I915_WRITE(I915REG_INT_MASK_R, 0x0);
1172 I915_WRITE(I915REG_INT_ENABLE_R, 0x0);
1174 I915_WRITE16(I915REG_INT_MASK_R, 0x0);
1175 I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
1180 int i915_driver_irq_postinstall(struct drm_device * dev)
1182 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
1183 int ret, num_pipes = 2;
1185 DRM_SPININIT(&dev_priv->swaps_lock, "swap");
1186 INIT_LIST_HEAD(&dev_priv->vbl_swaps.head);
1187 dev_priv->swaps_pending = 0;
1189 DRM_SPININIT(&dev_priv->user_irq_lock, "userirq");
1190 dev_priv->user_irq_refcount = 0;
1191 dev_priv->irq_enable_reg = 0;
1193 ret = drm_vblank_init(dev, num_pipes);
1197 ret = drm_hotplug_init(dev);
1201 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
1203 i915_enable_interrupt(dev);
1204 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
1207 * Initialize the hardware status page IRQ location.
1210 I915_WRITE(I915REG_INSTPM, (1 << 5) | (1 << 21));
1214 void i915_driver_irq_uninstall(struct drm_device * dev)
1216 struct drm_i915_private *dev_priv = (struct drm_i915_private *) dev->dev_private;
1222 dev_priv->irq_enabled = 0;
1224 temp = I915_READ(I915REG_PIPEASTAT);
1225 I915_WRITE(I915REG_PIPEASTAT, temp);
1226 temp = I915_READ(I915REG_PIPEBSTAT);
1227 I915_WRITE(I915REG_PIPEBSTAT, temp);
1228 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
1229 I915_WRITE(I915REG_HWSTAM, 0xffffffff);
1230 I915_WRITE(I915REG_INT_MASK_R, 0xffffffff);
1231 I915_WRITE(I915REG_INT_ENABLE_R, 0x0);
1233 temp = I915_READ(I915REG_INT_IDENTITY_R);
1234 I915_WRITE(I915REG_INT_IDENTITY_R, temp);
1236 I915_WRITE16(I915REG_HWSTAM, 0xffff);
1237 I915_WRITE16(I915REG_INT_MASK_R, 0xffff);
1238 I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
1240 temp = I915_READ16(I915REG_INT_IDENTITY_R);
1241 I915_WRITE16(I915REG_INT_IDENTITY_R, temp);