i830_irq.c 5.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/* i830_dma.c -- DMA support for the I830 -*- linux-c -*-
 *
 * Copyright 2002 Tungsten Graphics, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
D
Dave Airlie 已提交
12
 *
L
Linus Torvalds 已提交
13 14 15
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
D
Dave Airlie 已提交
16
 *
L
Linus Torvalds 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors: Keith Whitwell <keith@tungstengraphics.com>
 *
 */

#include "drmP.h"
#include "drm.h"
#include "i830_drm.h"
#include "i830_drv.h"
#include <linux/interrupt.h>	/* For task queue support */
#include <linux/delay.h>

D
Dave Airlie 已提交
36
irqreturn_t i830_driver_irq_handler(DRM_IRQ_ARGS)
L
Linus Torvalds 已提交
37
{
38
	struct drm_device *dev = (struct drm_device *) arg;
D
Dave Airlie 已提交
39 40
	drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
	u16 temp;
L
Linus Torvalds 已提交
41

D
Dave Airlie 已提交
42
	temp = I830_READ16(I830REG_INT_IDENTITY_R);
L
Linus Torvalds 已提交
43 44
	DRM_DEBUG("%x\n", temp);

D
Dave Airlie 已提交
45
	if (!(temp & 2))
L
Linus Torvalds 已提交
46 47
		return IRQ_NONE;

D
Dave Airlie 已提交
48
	I830_WRITE16(I830REG_INT_IDENTITY_R, temp);
L
Linus Torvalds 已提交
49 50

	atomic_inc(&dev_priv->irq_received);
D
Dave Airlie 已提交
51
	wake_up_interruptible(&dev_priv->irq_queue);
L
Linus Torvalds 已提交
52 53 54 55

	return IRQ_HANDLED;
}

56
static int i830_emit_irq(struct drm_device * dev)
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64
{
	drm_i830_private_t *dev_priv = dev->dev_private;
	RING_LOCALS;

	DRM_DEBUG("%s\n", __FUNCTION__);

	atomic_inc(&dev_priv->irq_emitted);

D
Dave Airlie 已提交
65 66 67 68
	BEGIN_LP_RING(2);
	OUT_RING(0);
	OUT_RING(GFX_OP_USER_INTERRUPT);
	ADVANCE_LP_RING();
L
Linus Torvalds 已提交
69 70 71 72

	return atomic_read(&dev_priv->irq_emitted);
}

73
static int i830_wait_irq(struct drm_device * dev, int irq_nr)
L
Linus Torvalds 已提交
74
{
D
Dave Airlie 已提交
75
	drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
L
Linus Torvalds 已提交
76
	DECLARE_WAITQUEUE(entry, current);
D
Dave Airlie 已提交
77
	unsigned long end = jiffies + HZ * 3;
L
Linus Torvalds 已提交
78 79 80 81
	int ret = 0;

	DRM_DEBUG("%s\n", __FUNCTION__);

D
Dave Airlie 已提交
82 83
	if (atomic_read(&dev_priv->irq_received) >= irq_nr)
		return 0;
L
Linus Torvalds 已提交
84 85 86 87 88 89 90

	dev_priv->sarea_priv->perf_boxes |= I830_BOX_WAIT;

	add_wait_queue(&dev_priv->irq_queue, &entry);

	for (;;) {
		__set_current_state(TASK_INTERRUPTIBLE);
D
Dave Airlie 已提交
91 92 93
		if (atomic_read(&dev_priv->irq_received) >= irq_nr)
			break;
		if ((signed)(end - jiffies) <= 0) {
L
Linus Torvalds 已提交
94
			DRM_ERROR("timeout iir %x imr %x ier %x hwstam %x\n",
D
Dave Airlie 已提交
95 96 97 98
				  I830_READ16(I830REG_INT_IDENTITY_R),
				  I830_READ16(I830REG_INT_MASK_R),
				  I830_READ16(I830REG_INT_ENABLE_R),
				  I830_READ16(I830REG_HWSTAM));
L
Linus Torvalds 已提交
99

D
Dave Airlie 已提交
100
			ret = -EBUSY;	/* Lockup?  Missed irq? */
L
Linus Torvalds 已提交
101 102
			break;
		}
D
Dave Airlie 已提交
103 104 105
		schedule_timeout(HZ * 3);
		if (signal_pending(current)) {
			ret = -EINTR;
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115 116
			break;
		}
	}

	__set_current_state(TASK_RUNNING);
	remove_wait_queue(&dev_priv->irq_queue, &entry);
	return ret;
}

/* Needs the lock as it touches the ring.
 */
D
Dave Airlie 已提交
117 118
int i830_irq_emit(struct inode *inode, struct file *filp, unsigned int cmd,
		  unsigned long arg)
L
Linus Torvalds 已提交
119
{
120 121
	struct drm_file *priv = filp->private_data;
	struct drm_device *dev = priv->head->dev;
L
Linus Torvalds 已提交
122 123 124 125 126 127
	drm_i830_private_t *dev_priv = dev->dev_private;
	drm_i830_irq_emit_t emit;
	int result;

	LOCK_TEST_WITH_RETURN(dev, filp);

D
Dave Airlie 已提交
128 129
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
130 131 132
		return -EINVAL;
	}

D
Dave Airlie 已提交
133 134
	if (copy_from_user
	    (&emit, (drm_i830_irq_emit_t __user *) arg, sizeof(emit)))
L
Linus Torvalds 已提交
135 136
		return -EFAULT;

D
Dave Airlie 已提交
137
	result = i830_emit_irq(dev);
L
Linus Torvalds 已提交
138

D
Dave Airlie 已提交
139 140
	if (copy_to_user(emit.irq_seq, &result, sizeof(int))) {
		DRM_ERROR("copy_to_user\n");
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148
		return -EFAULT;
	}

	return 0;
}

/* Doesn't need the hardware lock.
 */
D
Dave Airlie 已提交
149 150
int i830_irq_wait(struct inode *inode, struct file *filp, unsigned int cmd,
		  unsigned long arg)
L
Linus Torvalds 已提交
151
{
152 153
	struct drm_file *priv = filp->private_data;
	struct drm_device *dev = priv->head->dev;
L
Linus Torvalds 已提交
154 155 156
	drm_i830_private_t *dev_priv = dev->dev_private;
	drm_i830_irq_wait_t irqwait;

D
Dave Airlie 已提交
157 158
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
159 160 161
		return -EINVAL;
	}

D
Dave Airlie 已提交
162 163
	if (copy_from_user(&irqwait, (drm_i830_irq_wait_t __user *) arg,
			   sizeof(irqwait)))
L
Linus Torvalds 已提交
164 165
		return -EFAULT;

D
Dave Airlie 已提交
166
	return i830_wait_irq(dev, irqwait.irq_seq);
L
Linus Torvalds 已提交
167 168 169 170
}

/* drm_dma.h hooks
*/
171
void i830_driver_irq_preinstall(struct drm_device * dev)
D
Dave Airlie 已提交
172 173
{
	drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
L
Linus Torvalds 已提交
174

D
Dave Airlie 已提交
175 176 177
	I830_WRITE16(I830REG_HWSTAM, 0xffff);
	I830_WRITE16(I830REG_INT_MASK_R, 0x0);
	I830_WRITE16(I830REG_INT_ENABLE_R, 0x0);
L
Linus Torvalds 已提交
178 179 180 181 182
	atomic_set(&dev_priv->irq_received, 0);
	atomic_set(&dev_priv->irq_emitted, 0);
	init_waitqueue_head(&dev_priv->irq_queue);
}

183
void i830_driver_irq_postinstall(struct drm_device * dev)
D
Dave Airlie 已提交
184 185
{
	drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
L
Linus Torvalds 已提交
186

D
Dave Airlie 已提交
187
	I830_WRITE16(I830REG_INT_ENABLE_R, 0x2);
L
Linus Torvalds 已提交
188 189
}

190
void i830_driver_irq_uninstall(struct drm_device * dev)
D
Dave Airlie 已提交
191 192
{
	drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
L
Linus Torvalds 已提交
193 194 195
	if (!dev_priv)
		return;

D
Dave Airlie 已提交
196 197
	I830_WRITE16(I830REG_INT_MASK_R, 0xffff);
	I830_WRITE16(I830REG_INT_ENABLE_R, 0x0);
L
Linus Torvalds 已提交
198
}