Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* i830_dma.c -- DMA support for the I830 -*- linux-c -*- |
| 2 | * |
| 3 | * Copyright 2002 Tungsten Graphics, Inc. |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 12 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * The above copyright notice and this permission notice (including the next |
| 14 | * paragraph) shall be included in all copies or substantial portions of the |
| 15 | * Software. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 16 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | * DEALINGS IN THE SOFTWARE. |
| 24 | * |
| 25 | * Authors: Keith Whitwell <keith@tungstengraphics.com> |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | #include "drmP.h" |
| 30 | #include "drm.h" |
| 31 | #include "i830_drm.h" |
| 32 | #include "i830_drv.h" |
| 33 | #include <linux/interrupt.h> /* For task queue support */ |
| 34 | #include <linux/delay.h> |
| 35 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 36 | irqreturn_t i830_driver_irq_handler(DRM_IRQ_ARGS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Dave Airlie | eddca55 | 2007-07-11 16:09:54 +1000 | [diff] [blame] | 38 | struct drm_device *dev = (struct drm_device *) arg; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 39 | drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private; |
| 40 | u16 temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 42 | temp = I830_READ16(I830REG_INT_IDENTITY_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | DRM_DEBUG("%x\n", temp); |
| 44 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 45 | if (!(temp & 2)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | return IRQ_NONE; |
| 47 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 48 | I830_WRITE16(I830REG_INT_IDENTITY_R, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | atomic_inc(&dev_priv->irq_received); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 51 | wake_up_interruptible(&dev_priv->irq_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | return IRQ_HANDLED; |
| 54 | } |
| 55 | |
Dave Airlie | eddca55 | 2007-07-11 16:09:54 +1000 | [diff] [blame] | 56 | static int i830_emit_irq(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | drm_i830_private_t *dev_priv = dev->dev_private; |
| 59 | RING_LOCALS; |
| 60 | |
| 61 | DRM_DEBUG("%s\n", __FUNCTION__); |
| 62 | |
| 63 | atomic_inc(&dev_priv->irq_emitted); |
| 64 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 65 | BEGIN_LP_RING(2); |
| 66 | OUT_RING(0); |
| 67 | OUT_RING(GFX_OP_USER_INTERRUPT); |
| 68 | ADVANCE_LP_RING(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | return atomic_read(&dev_priv->irq_emitted); |
| 71 | } |
| 72 | |
Dave Airlie | eddca55 | 2007-07-11 16:09:54 +1000 | [diff] [blame] | 73 | static int i830_wait_irq(struct drm_device * dev, int irq_nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 75 | drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | DECLARE_WAITQUEUE(entry, current); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 77 | unsigned long end = jiffies + HZ * 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | int ret = 0; |
| 79 | |
| 80 | DRM_DEBUG("%s\n", __FUNCTION__); |
| 81 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 82 | if (atomic_read(&dev_priv->irq_received) >= irq_nr) |
| 83 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | dev_priv->sarea_priv->perf_boxes |= I830_BOX_WAIT; |
| 86 | |
| 87 | add_wait_queue(&dev_priv->irq_queue, &entry); |
| 88 | |
| 89 | for (;;) { |
| 90 | __set_current_state(TASK_INTERRUPTIBLE); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 91 | if (atomic_read(&dev_priv->irq_received) >= irq_nr) |
| 92 | break; |
| 93 | if ((signed)(end - jiffies) <= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | DRM_ERROR("timeout iir %x imr %x ier %x hwstam %x\n", |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 95 | I830_READ16(I830REG_INT_IDENTITY_R), |
| 96 | I830_READ16(I830REG_INT_MASK_R), |
| 97 | I830_READ16(I830REG_INT_ENABLE_R), |
| 98 | I830_READ16(I830REG_HWSTAM)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 100 | ret = -EBUSY; /* Lockup? Missed irq? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | break; |
| 102 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 103 | schedule_timeout(HZ * 3); |
| 104 | if (signal_pending(current)) { |
| 105 | ret = -EINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | __set_current_state(TASK_RUNNING); |
| 111 | remove_wait_queue(&dev_priv->irq_queue, &entry); |
| 112 | return ret; |
| 113 | } |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | /* Needs the lock as it touches the ring. |
| 116 | */ |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame^] | 117 | int i830_irq_emit(struct inode *inode, struct drm_file *file_priv, |
| 118 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame^] | 120 | struct drm_device *dev = file_priv->head->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | drm_i830_private_t *dev_priv = dev->dev_private; |
| 122 | drm_i830_irq_emit_t emit; |
| 123 | int result; |
| 124 | |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame^] | 125 | LOCK_TEST_WITH_RETURN(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 127 | if (!dev_priv) { |
| 128 | DRM_ERROR("%s called with no initialization\n", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | return -EINVAL; |
| 130 | } |
| 131 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 132 | if (copy_from_user |
| 133 | (&emit, (drm_i830_irq_emit_t __user *) arg, sizeof(emit))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | return -EFAULT; |
| 135 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 136 | result = i830_emit_irq(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 138 | if (copy_to_user(emit.irq_seq, &result, sizeof(int))) { |
| 139 | DRM_ERROR("copy_to_user\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | return -EFAULT; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | /* Doesn't need the hardware lock. |
| 147 | */ |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame^] | 148 | int i830_irq_wait(struct inode *inode, struct drm_file *file_priv, |
| 149 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame^] | 151 | struct drm_device *dev = file_priv->head->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | drm_i830_private_t *dev_priv = dev->dev_private; |
| 153 | drm_i830_irq_wait_t irqwait; |
| 154 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 155 | if (!dev_priv) { |
| 156 | DRM_ERROR("%s called with no initialization\n", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return -EINVAL; |
| 158 | } |
| 159 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 160 | if (copy_from_user(&irqwait, (drm_i830_irq_wait_t __user *) arg, |
| 161 | sizeof(irqwait))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return -EFAULT; |
| 163 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 164 | return i830_wait_irq(dev, irqwait.irq_seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | /* drm_dma.h hooks |
| 168 | */ |
Dave Airlie | eddca55 | 2007-07-11 16:09:54 +1000 | [diff] [blame] | 169 | void i830_driver_irq_preinstall(struct drm_device * dev) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 170 | { |
| 171 | drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 173 | I830_WRITE16(I830REG_HWSTAM, 0xffff); |
| 174 | I830_WRITE16(I830REG_INT_MASK_R, 0x0); |
| 175 | I830_WRITE16(I830REG_INT_ENABLE_R, 0x0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | atomic_set(&dev_priv->irq_received, 0); |
| 177 | atomic_set(&dev_priv->irq_emitted, 0); |
| 178 | init_waitqueue_head(&dev_priv->irq_queue); |
| 179 | } |
| 180 | |
Dave Airlie | eddca55 | 2007-07-11 16:09:54 +1000 | [diff] [blame] | 181 | void i830_driver_irq_postinstall(struct drm_device * dev) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 182 | { |
| 183 | drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 185 | I830_WRITE16(I830REG_INT_ENABLE_R, 0x2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Dave Airlie | eddca55 | 2007-07-11 16:09:54 +1000 | [diff] [blame] | 188 | void i830_driver_irq_uninstall(struct drm_device * dev) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 189 | { |
| 190 | drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if (!dev_priv) |
| 192 | return; |
| 193 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 194 | I830_WRITE16(I830REG_INT_MASK_R, 0xffff); |
| 195 | I830_WRITE16(I830REG_INT_ENABLE_R, 0x0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |