via_irq.c 11.0 KB
Newer Older
D
Dave Airlie 已提交
1 2 3 4 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/* via_irq.c
 *
 * Copyright 2004 BEAM Ltd.
 * Copyright 2002 Tungsten Graphics, Inc.
 * Copyright 2005 Thomas Hellstrom.
 * 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:
 *
 * 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
 * BEAM LTD, 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:
 *    Terry Barnaby <terry1@beam.ltd.uk>
 *    Keith Whitwell <keith@tungstengraphics.com>
 *    Thomas Hellstrom <unichrome@shipmail.org>
 *
 * This code provides standard DRM access to the Via Unichrome / Pro Vertical blank
 * interrupt, as well as an infrastructure to handle other interrupts of the chip.
 * The refresh rate is also calculated for video playback sync purposes.
 */

#include "drmP.h"
#include "drm.h"
#include "via_drm.h"
#include "via_drv.h"

#define VIA_REG_INTERRUPT       0x200

/* VIA_REG_INTERRUPT */
#define VIA_IRQ_GLOBAL          (1 << 31)
#define VIA_IRQ_VBLANK_ENABLE   (1 << 19)
#define VIA_IRQ_VBLANK_PENDING  (1 << 3)
#define VIA_IRQ_HQV0_ENABLE     (1 << 11)
#define VIA_IRQ_HQV1_ENABLE     (1 << 25)
#define VIA_IRQ_HQV0_PENDING    (1 << 9)
#define VIA_IRQ_HQV1_PENDING    (1 << 10)
D
Dave Airlie 已提交
53 54 55 56 57 58 59 60 61
#define VIA_IRQ_DMA0_DD_ENABLE  (1 << 20)
#define VIA_IRQ_DMA0_TD_ENABLE  (1 << 21)
#define VIA_IRQ_DMA1_DD_ENABLE  (1 << 22)
#define VIA_IRQ_DMA1_TD_ENABLE  (1 << 23)
#define VIA_IRQ_DMA0_DD_PENDING (1 << 4)
#define VIA_IRQ_DMA0_TD_PENDING (1 << 5)
#define VIA_IRQ_DMA1_DD_PENDING (1 << 6)
#define VIA_IRQ_DMA1_TD_PENDING (1 << 7)

D
Dave Airlie 已提交
62 63 64 65

/*
 * Device-specific IRQs go here. This type might need to be extended with
 * the register if there are multiple IRQ control registers.
D
Dave Airlie 已提交
66
 * Currently we activate the HQV interrupts of  Unichrome Pro group A.
D
Dave Airlie 已提交
67 68 69
 */

static maskarray_t via_pro_group_a_irqs[] = {
D
Dave Airlie 已提交
70 71 72
	{VIA_IRQ_HQV0_ENABLE, VIA_IRQ_HQV0_PENDING, 0x000003D0, 0x00008010,
	 0x00000000},
	{VIA_IRQ_HQV1_ENABLE, VIA_IRQ_HQV1_PENDING, 0x000013D0, 0x00008010,
D
Dave Airlie 已提交
73 74 75 76 77
	 0x00000000},
	{VIA_IRQ_DMA0_TD_ENABLE, VIA_IRQ_DMA0_TD_PENDING, VIA_PCI_DMA_CSR0,
	 VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
	{VIA_IRQ_DMA1_TD_ENABLE, VIA_IRQ_DMA1_TD_PENDING, VIA_PCI_DMA_CSR1,
	 VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
D
Dave Airlie 已提交
78 79 80
};
static int via_num_pro_group_a =
    sizeof(via_pro_group_a_irqs) / sizeof(maskarray_t);
D
Dave Airlie 已提交
81
static int via_irqmap_pro_group_a[] = {0, 1, -1, 2, -1, 3};
D
Dave Airlie 已提交
82

D
Dave Airlie 已提交
83 84 85 86 87 88
static maskarray_t via_unichrome_irqs[] = {
	{VIA_IRQ_DMA0_TD_ENABLE, VIA_IRQ_DMA0_TD_PENDING, VIA_PCI_DMA_CSR0,
	 VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
	{VIA_IRQ_DMA1_TD_ENABLE, VIA_IRQ_DMA1_TD_PENDING, VIA_PCI_DMA_CSR1,
	 VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008}
};
D
Dave Airlie 已提交
89
static int via_num_unichrome = sizeof(via_unichrome_irqs) / sizeof(maskarray_t);
D
Dave Airlie 已提交
90
static int via_irqmap_unichrome[] = {-1, -1, -1, 0, -1, 1};
D
Dave Airlie 已提交
91 92

static unsigned time_diff(struct timeval *now, struct timeval *then)
D
Dave Airlie 已提交
93
{
D
Dave Airlie 已提交
94
	return (now->tv_usec >= then->tv_usec) ?
95 96 97 98 99 100 101 102 103 104 105
		now->tv_usec - then->tv_usec :
		1000000 - (then->tv_usec - now->tv_usec);
}

u32 via_get_vblank_counter(struct drm_device *dev, int crtc)
{
	drm_via_private_t *dev_priv = dev->dev_private;
	if (crtc != 0)
		return 0;

	return atomic_read(&dev_priv->vbl_received);
D
Dave Airlie 已提交
106 107 108 109
}

irqreturn_t via_driver_irq_handler(DRM_IRQ_ARGS)
{
110
	struct drm_device *dev = (struct drm_device *) arg;
D
Dave Airlie 已提交
111 112 113 114 115 116 117 118 119
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
	u32 status;
	int handled = 0;
	struct timeval cur_vblank;
	drm_via_irq_t *cur_irq = dev_priv->via_irqs;
	int i;

	status = VIA_READ(VIA_REG_INTERRUPT);
	if (status & VIA_IRQ_VBLANK_PENDING) {
120 121
		atomic_inc(&dev_priv->vbl_received);
		if (!(atomic_read(&dev_priv->vbl_received) & 0x0F)) {
D
Dave Airlie 已提交
122
			do_gettimeofday(&cur_vblank);
D
Dave Airlie 已提交
123 124 125 126
			if (dev_priv->last_vblank_valid) {
				dev_priv->usec_per_vblank =
				    time_diff(&cur_vblank,
					      &dev_priv->last_vblank) >> 4;
D
Dave Airlie 已提交
127 128 129
			}
			dev_priv->last_vblank = cur_vblank;
			dev_priv->last_vblank_valid = 1;
D
Dave Airlie 已提交
130
		}
131
		if (!(atomic_read(&dev_priv->vbl_received) & 0xFF)) {
D
Dave Airlie 已提交
132
			DRM_DEBUG("US per vblank is: %u\n",
D
Dave Airlie 已提交
133
				  dev_priv->usec_per_vblank);
D
Dave Airlie 已提交
134
		}
135
		drm_handle_vblank(dev, 0);
D
Dave Airlie 已提交
136 137 138
		handled = 1;
	}

D
Dave Airlie 已提交
139
	for (i = 0; i < dev_priv->num_irqs; ++i) {
D
Dave Airlie 已提交
140
		if (status & cur_irq->pending_mask) {
D
Dave Airlie 已提交
141 142
			atomic_inc(&cur_irq->irq_received);
			DRM_WAKEUP(&cur_irq->irq_queue);
D
Dave Airlie 已提交
143
			handled = 1;
D
Dave Airlie 已提交
144 145 146 147 148
			if (dev_priv->irq_map[drm_via_irq_dma0_td] == i) {
				via_dmablit_handler(dev, 0, 1);
			} else if (dev_priv->irq_map[drm_via_irq_dma1_td] == i) {
				via_dmablit_handler(dev, 1, 1);
			}
D
Dave Airlie 已提交
149 150 151
		}
		cur_irq++;
	}
D
Dave Airlie 已提交
152

D
Dave Airlie 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	/* Acknowlege interrupts */
	VIA_WRITE(VIA_REG_INTERRUPT, status);

	if (handled)
		return IRQ_HANDLED;
	else
		return IRQ_NONE;
}

static __inline__ void viadrv_acknowledge_irqs(drm_via_private_t * dev_priv)
{
	u32 status;

	if (dev_priv) {
		/* Acknowlege interrupts */
		status = VIA_READ(VIA_REG_INTERRUPT);
D
Dave Airlie 已提交
169
		VIA_WRITE(VIA_REG_INTERRUPT, status |
D
Dave Airlie 已提交
170 171 172 173
			  dev_priv->irq_pending_mask);
	}
}

174
int via_enable_vblank(struct drm_device *dev, int crtc)
D
Dave Airlie 已提交
175
{
176 177
	drm_via_private_t *dev_priv = dev->dev_private;
	u32 status;
D
Dave Airlie 已提交
178

179 180
	if (crtc != 0) {
		DRM_ERROR("%s:  bad crtc %d\n", __FUNCTION__, crtc);
D
Dave Airlie 已提交
181 182 183
		return -EINVAL;
	}

184 185
	status = VIA_READ(VIA_REG_INTERRUPT);
	VIA_WRITE(VIA_REG_INTERRUPT, status & VIA_IRQ_VBLANK_ENABLE);
D
Dave Airlie 已提交
186

187 188
	VIA_WRITE8(0x83d4, 0x11);
	VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) | 0x30);
D
Dave Airlie 已提交
189

190 191
	return 0;
}
D
Dave Airlie 已提交
192

193 194 195 196 197 198 199 200 201
void via_disable_vblank(struct drm_device *dev, int crtc)
{
	drm_via_private_t *dev_priv = dev->dev_private;

	VIA_WRITE8(0x83d4, 0x11);
	VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) & ~0x30);

	if (crtc != 0)
		DRM_ERROR("%s:  bad crtc %d\n", __FUNCTION__, crtc);
D
Dave Airlie 已提交
202 203
}

204
static int
205
via_driver_irq_wait(struct drm_device * dev, unsigned int irq, int force_sequence,
D
Dave Airlie 已提交
206 207 208 209
		    unsigned int *sequence)
{
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
	unsigned int cur_irq_sequence;
210
	drm_via_irq_t *cur_irq;
D
Dave Airlie 已提交
211
	int ret = 0;
212
	maskarray_t *masks;
D
Dave Airlie 已提交
213
	int real_irq;
D
Dave Airlie 已提交
214

215
	DRM_DEBUG("\n");
D
Dave Airlie 已提交
216 217

	if (!dev_priv) {
218
		DRM_ERROR("called with no initialization\n");
E
Eric Anholt 已提交
219
		return -EINVAL;
D
Dave Airlie 已提交
220 221
	}

D
Dave Airlie 已提交
222
	if (irq >= drm_via_irq_num) {
223
		DRM_ERROR("Trying to wait on unknown irq %d\n", irq);
E
Eric Anholt 已提交
224
		return -EINVAL;
D
Dave Airlie 已提交
225
	}
D
Dave Airlie 已提交
226

D
Dave Airlie 已提交
227 228 229
	real_irq = dev_priv->irq_map[irq];

	if (real_irq < 0) {
230 231
		DRM_ERROR("Video IRQ %d not available on this hardware.\n",
			  irq);
E
Eric Anholt 已提交
232
		return -EINVAL;
D
Dave Airlie 已提交
233
	}
234 235

	masks = dev_priv->irq_masks;
236
	cur_irq = dev_priv->via_irqs + real_irq;
D
Dave Airlie 已提交
237

D
Dave Airlie 已提交
238
	if (masks[real_irq][2] && !force_sequence) {
D
Dave Airlie 已提交
239
		DRM_WAIT_ON(ret, cur_irq->irq_queue, 3 * DRM_HZ,
D
Dave Airlie 已提交
240 241
			    ((VIA_READ(masks[irq][2]) & masks[irq][3]) ==
			     masks[irq][4]));
D
Dave Airlie 已提交
242 243 244
		cur_irq_sequence = atomic_read(&cur_irq->irq_received);
	} else {
		DRM_WAIT_ON(ret, cur_irq->irq_queue, 3 * DRM_HZ,
D
Dave Airlie 已提交
245 246 247
			    (((cur_irq_sequence =
			       atomic_read(&cur_irq->irq_received)) -
			      *sequence) <= (1 << 23)));
D
Dave Airlie 已提交
248 249 250 251 252 253 254 255 256
	}
	*sequence = cur_irq_sequence;
	return ret;
}

/*
 * drm_dma.h hooks
 */

257
void via_driver_irq_preinstall(struct drm_device * dev)
D
Dave Airlie 已提交
258 259 260
{
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
	u32 status;
261
	drm_via_irq_t *cur_irq;
D
Dave Airlie 已提交
262 263
	int i;

264
	DRM_DEBUG("dev_priv: %p\n", dev_priv);
D
Dave Airlie 已提交
265
	if (dev_priv) {
266
		cur_irq = dev_priv->via_irqs;
D
Dave Airlie 已提交
267 268 269 270

		dev_priv->irq_enable_mask = VIA_IRQ_VBLANK_ENABLE;
		dev_priv->irq_pending_mask = VIA_IRQ_VBLANK_PENDING;

T
Thomas Hellstrom 已提交
271 272 273 274 275 276 277 278 279 280
		if (dev_priv->chipset == VIA_PRO_GROUP_A ||
		    dev_priv->chipset == VIA_DX9_0) {
			dev_priv->irq_masks = via_pro_group_a_irqs;
			dev_priv->num_irqs = via_num_pro_group_a;
			dev_priv->irq_map = via_irqmap_pro_group_a;
		} else {
			dev_priv->irq_masks = via_unichrome_irqs;
			dev_priv->num_irqs = via_num_unichrome;
			dev_priv->irq_map = via_irqmap_unichrome;
		}
D
Dave Airlie 已提交
281 282

		for (i = 0; i < dev_priv->num_irqs; ++i) {
D
Dave Airlie 已提交
283
			atomic_set(&cur_irq->irq_received, 0);
D
Dave Airlie 已提交
284
			cur_irq->enable_mask = dev_priv->irq_masks[i][0];
D
Dave Airlie 已提交
285
			cur_irq->pending_mask = dev_priv->irq_masks[i][1];
D
Dave Airlie 已提交
286
			DRM_INIT_WAITQUEUE(&cur_irq->irq_queue);
D
Dave Airlie 已提交
287 288 289
			dev_priv->irq_enable_mask |= cur_irq->enable_mask;
			dev_priv->irq_pending_mask |= cur_irq->pending_mask;
			cur_irq++;
D
Dave Airlie 已提交
290

D
Dave Airlie 已提交
291 292
			DRM_DEBUG("Initializing IRQ %d\n", i);
		}
D
Dave Airlie 已提交
293 294

		dev_priv->last_vblank_valid = 0;
D
Dave Airlie 已提交
295

D
Dave Airlie 已提交
296
		/* Clear VSync interrupt regs */
D
Dave Airlie 已提交
297
		status = VIA_READ(VIA_REG_INTERRUPT);
D
Dave Airlie 已提交
298
		VIA_WRITE(VIA_REG_INTERRUPT, status &
D
Dave Airlie 已提交
299
			  ~(dev_priv->irq_enable_mask));
D
Dave Airlie 已提交
300

D
Dave Airlie 已提交
301 302 303 304 305
		/* Clear bits if they're already high */
		viadrv_acknowledge_irqs(dev_priv);
	}
}

306
int via_driver_irq_postinstall(struct drm_device * dev)
D
Dave Airlie 已提交
307 308 309 310
{
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
	u32 status;

311 312 313
	DRM_DEBUG("via_driver_irq_postinstall\n");
	if (!dev_priv)
		return -EINVAL;
D
Dave Airlie 已提交
314

315 316 317 318
	drm_vblank_init(dev, 1);
	status = VIA_READ(VIA_REG_INTERRUPT);
	VIA_WRITE(VIA_REG_INTERRUPT, status | VIA_IRQ_GLOBAL
		  | dev_priv->irq_enable_mask);
D
Dave Airlie 已提交
319

320 321 322
	/* Some magic, oh for some data sheets ! */
	VIA_WRITE8(0x83d4, 0x11);
	VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) | 0x30);
D
Dave Airlie 已提交
323

324
	return 0;
D
Dave Airlie 已提交
325 326
}

327
void via_driver_irq_uninstall(struct drm_device * dev)
D
Dave Airlie 已提交
328 329 330 331
{
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
	u32 status;

332
	DRM_DEBUG("\n");
D
Dave Airlie 已提交
333 334 335 336 337 338 339 340
	if (dev_priv) {

		/* Some more magic, oh for some data sheets ! */

		VIA_WRITE8(0x83d4, 0x11);
		VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) & ~0x30);

		status = VIA_READ(VIA_REG_INTERRUPT);
D
Dave Airlie 已提交
341
		VIA_WRITE(VIA_REG_INTERRUPT, status &
D
Dave Airlie 已提交
342 343 344 345
			  ~(VIA_IRQ_VBLANK_ENABLE | dev_priv->irq_enable_mask));
	}
}

346
int via_wait_irq(struct drm_device *dev, void *data, struct drm_file *file_priv)
D
Dave Airlie 已提交
347
{
348
	drm_via_irqwait_t *irqwait = data;
D
Dave Airlie 已提交
349 350 351 352 353 354 355
	struct timeval now;
	int ret = 0;
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
	drm_via_irq_t *cur_irq = dev_priv->via_irqs;
	int force_sequence;

	if (!dev->irq)
E
Eric Anholt 已提交
356
		return -EINVAL;
D
Dave Airlie 已提交
357

358
	if (irqwait->request.irq >= dev_priv->num_irqs) {
359
		DRM_ERROR("Trying to wait on unknown irq %d\n",
360
			  irqwait->request.irq);
E
Eric Anholt 已提交
361
		return -EINVAL;
D
Dave Airlie 已提交
362 363
	}

364
	cur_irq += irqwait->request.irq;
D
Dave Airlie 已提交
365

366
	switch (irqwait->request.type & ~VIA_IRQ_FLAGS_MASK) {
D
Dave Airlie 已提交
367
	case VIA_IRQ_RELATIVE:
368 369
		irqwait->request.sequence += atomic_read(&cur_irq->irq_received);
		irqwait->request.type &= ~_DRM_VBLANK_RELATIVE;
D
Dave Airlie 已提交
370 371 372
	case VIA_IRQ_ABSOLUTE:
		break;
	default:
E
Eric Anholt 已提交
373
		return -EINVAL;
D
Dave Airlie 已提交
374 375
	}

376
	if (irqwait->request.type & VIA_IRQ_SIGNAL) {
377
		DRM_ERROR("Signals on Via IRQs not implemented yet.\n");
E
Eric Anholt 已提交
378
		return -EINVAL;
D
Dave Airlie 已提交
379 380
	}

381
	force_sequence = (irqwait->request.type & VIA_IRQ_FORCE_SEQUENCE);
D
Dave Airlie 已提交
382

383 384
	ret = via_driver_irq_wait(dev, irqwait->request.irq, force_sequence,
				  &irqwait->request.sequence);
D
Dave Airlie 已提交
385
	do_gettimeofday(&now);
386 387
	irqwait->reply.tval_sec = now.tv_sec;
	irqwait->reply.tval_usec = now.tv_usec;
D
Dave Airlie 已提交
388 389 390

	return ret;
}