radeon_irq.c 9.0 KB
Newer Older
D
Dave Airlie 已提交
1 2
/* radeon_irq.c -- IRQ handling for radeon -*- linux-c -*- */
/*
L
Linus Torvalds 已提交
3
 * Copyright (C) The Weather Channel, Inc.  2002.  All Rights Reserved.
D
Dave Airlie 已提交
4
 *
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 * The Weather Channel (TM) funded Tungsten Graphics to develop the
 * initial release of the Radeon 8500 driver under the XFree86 license.
 * This notice must be preserved.
 *
 * 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:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * 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
 * PRECISION INSIGHT 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>
 *    Michel D�zer <michel@daenzer.net>
 */

#include "drmP.h"
#include "drm.h"
#include "radeon_drm.h"
#include "radeon_drv.h"

D
Dave Airlie 已提交
38 39
static __inline__ u32 radeon_acknowledge_irqs(drm_radeon_private_t * dev_priv,
					      u32 mask)
D
Dave Airlie 已提交
40 41 42 43 44 45 46
{
	u32 irqs = RADEON_READ(RADEON_GEN_INT_STATUS) & mask;
	if (irqs)
		RADEON_WRITE(RADEON_GEN_INT_STATUS, irqs);
	return irqs;
}

L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/* Interrupts - Used for device synchronization and flushing in the
 * following circumstances:
 *
 * - Exclusive FB access with hw idle:
 *    - Wait for GUI Idle (?) interrupt, then do normal flush.
 *
 * - Frame throttling, NV_fence:
 *    - Drop marker irq's into command stream ahead of time.
 *    - Wait on irq's with lock *not held*
 *    - Check each for termination condition
 *
 * - Internally in cp_getbuffer, etc:
 *    - as above, but wait with lock held???
 *
 * NOTE: These functions are misleadingly named -- the irq's aren't
 * tied to dma at all, this is just a hangover from dri prehistory.
 */

D
Dave Airlie 已提交
65
irqreturn_t radeon_driver_irq_handler(DRM_IRQ_ARGS)
L
Linus Torvalds 已提交
66
{
67
	struct drm_device *dev = (struct drm_device *) arg;
D
Dave Airlie 已提交
68 69 70
	drm_radeon_private_t *dev_priv =
	    (drm_radeon_private_t *) dev->dev_private;
	u32 stat;
L
Linus Torvalds 已提交
71 72 73 74

	/* Only consider the bits we're interested in - others could be used
	 * outside the DRM
	 */
D
Dave Airlie 已提交
75
	stat = radeon_acknowledge_irqs(dev_priv, (RADEON_SW_INT_TEST_ACK |
76 77
						  RADEON_CRTC_VBLANK_STAT |
						  RADEON_CRTC2_VBLANK_STAT));
L
Linus Torvalds 已提交
78 79 80
	if (!stat)
		return IRQ_NONE;

81 82
	stat &= dev_priv->irq_enable_reg;

L
Linus Torvalds 已提交
83 84
	/* SW interrupt */
	if (stat & RADEON_SW_INT_TEST) {
D
Dave Airlie 已提交
85
		DRM_WAKEUP(&dev_priv->swi_queue);
L
Linus Torvalds 已提交
86 87 88
	}

	/* VBLANK interrupt */
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	if (stat & (RADEON_CRTC_VBLANK_STAT|RADEON_CRTC2_VBLANK_STAT)) {
		int vblank_crtc = dev_priv->vblank_crtc;

		if ((vblank_crtc &
		     (DRM_RADEON_VBLANK_CRTC1 | DRM_RADEON_VBLANK_CRTC2)) ==
		    (DRM_RADEON_VBLANK_CRTC1 | DRM_RADEON_VBLANK_CRTC2)) {
			if (stat & RADEON_CRTC_VBLANK_STAT)
				atomic_inc(&dev->vbl_received);
			if (stat & RADEON_CRTC2_VBLANK_STAT)
				atomic_inc(&dev->vbl_received2);
		} else if (((stat & RADEON_CRTC_VBLANK_STAT) &&
			   (vblank_crtc & DRM_RADEON_VBLANK_CRTC1)) ||
			   ((stat & RADEON_CRTC2_VBLANK_STAT) &&
			    (vblank_crtc & DRM_RADEON_VBLANK_CRTC2)))
			atomic_inc(&dev->vbl_received);

L
Linus Torvalds 已提交
105
		DRM_WAKEUP(&dev->vbl_queue);
D
Dave Airlie 已提交
106
		drm_vbl_send_signals(dev);
L
Linus Torvalds 已提交
107 108 109 110 111
	}

	return IRQ_HANDLED;
}

112
static int radeon_emit_irq(struct drm_device * dev)
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	unsigned int ret;
	RING_LOCALS;

	atomic_inc(&dev_priv->swi_emitted);
	ret = atomic_read(&dev_priv->swi_emitted);

D
Dave Airlie 已提交
121 122 123 124 125
	BEGIN_RING(4);
	OUT_RING_REG(RADEON_LAST_SWI_REG, ret);
	OUT_RING_REG(RADEON_GEN_INT_STATUS, RADEON_SW_INT_FIRE);
	ADVANCE_RING();
	COMMIT_RING();
L
Linus Torvalds 已提交
126 127 128 129

	return ret;
}

130
static int radeon_wait_irq(struct drm_device * dev, int swi_nr)
L
Linus Torvalds 已提交
131
{
D
Dave Airlie 已提交
132 133
	drm_radeon_private_t *dev_priv =
	    (drm_radeon_private_t *) dev->dev_private;
L
Linus Torvalds 已提交
134 135
	int ret = 0;

D
Dave Airlie 已提交
136 137
	if (RADEON_READ(RADEON_LAST_SWI_REG) >= swi_nr)
		return 0;
L
Linus Torvalds 已提交
138 139 140

	dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;

D
Dave Airlie 已提交
141 142
	DRM_WAIT_ON(ret, dev_priv->swi_queue, 3 * DRM_HZ,
		    RADEON_READ(RADEON_LAST_SWI_REG) >= swi_nr);
L
Linus Torvalds 已提交
143 144 145 146

	return ret;
}

147
int radeon_driver_vblank_do_wait(struct drm_device * dev, unsigned int *sequence,
148
				 int crtc)
L
Linus Torvalds 已提交
149
{
D
Dave Airlie 已提交
150 151
	drm_radeon_private_t *dev_priv =
	    (drm_radeon_private_t *) dev->dev_private;
L
Linus Torvalds 已提交
152 153
	unsigned int cur_vblank;
	int ret = 0;
154 155
	int ack = 0;
	atomic_t *counter;
D
Dave Airlie 已提交
156 157
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
158 159 160
		return DRM_ERR(EINVAL);
	}

161 162 163 164 165 166 167 168 169 170
	if (crtc == DRM_RADEON_VBLANK_CRTC1) {
		counter = &dev->vbl_received;
		ack |= RADEON_CRTC_VBLANK_STAT;
	} else if (crtc == DRM_RADEON_VBLANK_CRTC2) {
		counter = &dev->vbl_received2;
		ack |= RADEON_CRTC2_VBLANK_STAT;
	} else
		return DRM_ERR(EINVAL);

	radeon_acknowledge_irqs(dev_priv, ack);
L
Linus Torvalds 已提交
171 172 173 174 175

	dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;

	/* Assume that the user has missed the current sequence number
	 * by about a day rather than she wants to wait for years
D
Dave Airlie 已提交
176
	 * using vertical blanks...
L
Linus Torvalds 已提交
177
	 */
D
Dave Airlie 已提交
178
	DRM_WAIT_ON(ret, dev->vbl_queue, 3 * DRM_HZ,
179
		    (((cur_vblank = atomic_read(counter))
D
Dave Airlie 已提交
180
		      - *sequence) <= (1 << 23)));
L
Linus Torvalds 已提交
181 182 183 184 185 186

	*sequence = cur_vblank;

	return ret;
}

187
int radeon_driver_vblank_wait(struct drm_device *dev, unsigned int *sequence)
188 189 190 191
{
	return radeon_driver_vblank_do_wait(dev, sequence, DRM_RADEON_VBLANK_CRTC1);
}

192
int radeon_driver_vblank_wait2(struct drm_device *dev, unsigned int *sequence)
193 194 195 196
{
	return radeon_driver_vblank_do_wait(dev, sequence, DRM_RADEON_VBLANK_CRTC2);
}

L
Linus Torvalds 已提交
197 198
/* Needs the lock as it touches the ring.
 */
D
Dave Airlie 已提交
199
int radeon_irq_emit(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
200 201 202 203 204 205
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_irq_emit_t emit;
	int result;

D
Dave Airlie 已提交
206
	LOCK_TEST_WITH_RETURN(dev, filp);
L
Linus Torvalds 已提交
207

D
Dave Airlie 已提交
208 209
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
210 211 212
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
213 214
	DRM_COPY_FROM_USER_IOCTL(emit, (drm_radeon_irq_emit_t __user *) data,
				 sizeof(emit));
L
Linus Torvalds 已提交
215

D
Dave Airlie 已提交
216
	result = radeon_emit_irq(dev);
L
Linus Torvalds 已提交
217

D
Dave Airlie 已提交
218 219
	if (DRM_COPY_TO_USER(emit.irq_seq, &result, sizeof(int))) {
		DRM_ERROR("copy_to_user\n");
L
Linus Torvalds 已提交
220 221 222 223 224 225 226 227
		return DRM_ERR(EFAULT);
	}

	return 0;
}

/* Doesn't need the hardware lock.
 */
D
Dave Airlie 已提交
228
int radeon_irq_wait(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
229 230 231 232 233
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_irq_wait_t irqwait;

D
Dave Airlie 已提交
234 235
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
236 237 238
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
239 240
	DRM_COPY_FROM_USER_IOCTL(irqwait, (drm_radeon_irq_wait_t __user *) data,
				 sizeof(irqwait));
L
Linus Torvalds 已提交
241

D
Dave Airlie 已提交
242
	return radeon_wait_irq(dev, irqwait.irq_seq);
L
Linus Torvalds 已提交
243 244
}

245
static void radeon_enable_interrupt(struct drm_device *dev)
246 247 248 249 250 251 252 253 254 255 256 257 258 259
{
	drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;

	dev_priv->irq_enable_reg = RADEON_SW_INT_ENABLE;
	if (dev_priv->vblank_crtc & DRM_RADEON_VBLANK_CRTC1)
		dev_priv->irq_enable_reg |= RADEON_CRTC_VBLANK_MASK;

	if (dev_priv->vblank_crtc & DRM_RADEON_VBLANK_CRTC2)
		dev_priv->irq_enable_reg |= RADEON_CRTC2_VBLANK_MASK;

	RADEON_WRITE(RADEON_GEN_INT_CNTL, dev_priv->irq_enable_reg);
	dev_priv->irq_enabled = 1;
}

L
Linus Torvalds 已提交
260 261
/* drm_dma.h hooks
*/
262
void radeon_driver_irq_preinstall(struct drm_device * dev)
D
Dave Airlie 已提交
263
{
L
Linus Torvalds 已提交
264
	drm_radeon_private_t *dev_priv =
D
Dave Airlie 已提交
265
	    (drm_radeon_private_t *) dev->dev_private;
L
Linus Torvalds 已提交
266

D
Dave Airlie 已提交
267 268
	/* Disable *all* interrupts */
	RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
L
Linus Torvalds 已提交
269 270

	/* Clear bits if they're already high */
D
Dave Airlie 已提交
271
	radeon_acknowledge_irqs(dev_priv, (RADEON_SW_INT_TEST_ACK |
272 273
					   RADEON_CRTC_VBLANK_STAT |
					   RADEON_CRTC2_VBLANK_STAT));
L
Linus Torvalds 已提交
274 275
}

276
void radeon_driver_irq_postinstall(struct drm_device * dev)
D
Dave Airlie 已提交
277
{
L
Linus Torvalds 已提交
278
	drm_radeon_private_t *dev_priv =
D
Dave Airlie 已提交
279
	    (drm_radeon_private_t *) dev->dev_private;
L
Linus Torvalds 已提交
280

D
Dave Airlie 已提交
281 282
	atomic_set(&dev_priv->swi_emitted, 0);
	DRM_INIT_WAITQUEUE(&dev_priv->swi_queue);
L
Linus Torvalds 已提交
283

284
	radeon_enable_interrupt(dev);
L
Linus Torvalds 已提交
285 286
}

287
void radeon_driver_irq_uninstall(struct drm_device * dev)
D
Dave Airlie 已提交
288
{
L
Linus Torvalds 已提交
289
	drm_radeon_private_t *dev_priv =
D
Dave Airlie 已提交
290
	    (drm_radeon_private_t *) dev->dev_private;
L
Linus Torvalds 已提交
291 292 293
	if (!dev_priv)
		return;

294 295
	dev_priv->irq_enabled = 0;

L
Linus Torvalds 已提交
296
	/* Disable *all* interrupts */
D
Dave Airlie 已提交
297
	RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
L
Linus Torvalds 已提交
298
}
299 300


301
int radeon_vblank_crtc_get(struct drm_device *dev)
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
{
	drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
	u32 flag;
	u32 value;

	flag = RADEON_READ(RADEON_GEN_INT_CNTL);
	value = 0;

	if (flag & RADEON_CRTC_VBLANK_MASK)
		value |= DRM_RADEON_VBLANK_CRTC1;

	if (flag & RADEON_CRTC2_VBLANK_MASK)
		value |= DRM_RADEON_VBLANK_CRTC2;
	return value;
}

318
int radeon_vblank_crtc_set(struct drm_device *dev, int64_t value)
319 320 321 322 323 324 325 326 327 328
{
	drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
	if (value & ~(DRM_RADEON_VBLANK_CRTC1 | DRM_RADEON_VBLANK_CRTC2)) {
		DRM_ERROR("called with invalid crtc 0x%x\n", (unsigned int)value);
		return DRM_ERR(EINVAL);
	}
	dev_priv->vblank_crtc = (unsigned int)value;
	radeon_enable_interrupt(dev);
	return 0;
}