irq.c 10.4 KB
Newer Older
L
Linus Torvalds 已提交
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
/* 
 * Code to handle x86 style IRQs plus some generic interrupt stuff.
 *
 * Copyright (C) 1992 Linus Torvalds
 * Copyright (C) 1994, 1995, 1996, 1997, 1998 Ralf Baechle
 * Copyright (C) 1999 SuSE GmbH (Philipp Rumpf, prumpf@tux.org)
 * Copyright (C) 1999-2000 Grant Grundler
 * Copyright (c) 2005 Matthew Wilcox
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2, or (at your option)
 *    any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <linux/bitops.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel_stat.h>
#include <linux/seq_file.h>
#include <linux/spinlock.h>
#include <linux/types.h>
J
James Bottomley 已提交
32
#include <asm/io.h>
L
Linus Torvalds 已提交
33

34 35
#include <asm/smp.h>

L
Linus Torvalds 已提交
36 37
#undef PARISC_IRQ_CR16_COUNTS

38 39
extern irqreturn_t timer_interrupt(int, void *);
extern irqreturn_t ipi_interrupt(int, void *);
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47

#define EIEM_MASK(irq)       (1UL<<(CPU_IRQ_MAX - irq))

/* Bits in EIEM correlate with cpu_irq_action[].
** Numbered *Big Endian*! (ie bit 0 is MSB)
*/
static volatile unsigned long cpu_eiem = 0;

J
James Bottomley 已提交
48 49 50 51 52 53 54 55 56 57 58
/*
** ack bitmap ... habitually set to 1, but reset to zero
** between ->ack() and ->end() of the interrupt to prevent
** re-interruption of a processing interrupt.
*/
static volatile unsigned long global_ack_eiem = ~0UL;
/*
** Local bitmap, same as above but for per-cpu interrupts
*/
static DEFINE_PER_CPU(unsigned long, local_ack_eiem) = ~0UL;

59
static void cpu_disable_irq(unsigned int irq)
L
Linus Torvalds 已提交
60 61 62 63
{
	unsigned long eirr_bit = EIEM_MASK(irq);

	cpu_eiem &= ~eirr_bit;
64 65 66 67
	/* Do nothing on the other CPUs.  If they get this interrupt,
	 * The & cpu_eiem in the do_cpu_irq_mask() ensures they won't
	 * handle it, and the set_eiem() at the bottom will ensure it
	 * then gets disabled */
L
Linus Torvalds 已提交
68 69 70 71 72 73 74
}

static void cpu_enable_irq(unsigned int irq)
{
	unsigned long eirr_bit = EIEM_MASK(irq);

	cpu_eiem |= eirr_bit;
75 76 77 78 79

	/* This is just a simple NOP IPI.  But what it does is cause
	 * all the other CPUs to do a set_eiem(cpu_eiem) at the end
	 * of the interrupt handler */
	smp_send_all_nop();
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88 89 90
}

static unsigned int cpu_startup_irq(unsigned int irq)
{
	cpu_enable_irq(irq);
	return 0;
}

void no_ack_irq(unsigned int irq) { }
void no_end_irq(unsigned int irq) { }

J
James Bottomley 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
void cpu_ack_irq(unsigned int irq)
{
	unsigned long mask = EIEM_MASK(irq);
	int cpu = smp_processor_id();

	/* Clear in EIEM so we can no longer process */
	if (CHECK_IRQ_PER_CPU(irq_desc[irq].status))
		per_cpu(local_ack_eiem, cpu) &= ~mask;
	else
		global_ack_eiem &= ~mask;

	/* disable the interrupt */
	set_eiem(cpu_eiem & global_ack_eiem & per_cpu(local_ack_eiem, cpu));
	/* and now ack it */
	mtctl(mask, 23);
}

void cpu_end_irq(unsigned int irq)
{
	unsigned long mask = EIEM_MASK(irq);
	int cpu = smp_processor_id();

	/* set it in the eiems---it's no longer in process */
	if (CHECK_IRQ_PER_CPU(irq_desc[irq].status))
		per_cpu(local_ack_eiem, cpu) |= mask;
	else
		global_ack_eiem |= mask;

	/* enable the interrupt */
	set_eiem(cpu_eiem & global_ack_eiem & per_cpu(local_ack_eiem, cpu));
}

J
James Bottomley 已提交
123 124 125 126 127 128
#ifdef CONFIG_SMP
int cpu_check_affinity(unsigned int irq, cpumask_t *dest)
{
	int cpu_dest;

	/* timer and ipi have to always be received on all CPUs */
J
James Bottomley 已提交
129
	if (CHECK_IRQ_PER_CPU(irq)) {
J
James Bottomley 已提交
130 131
		/* Bad linux design decision.  The mask has already
		 * been set; we must reset it */
132
		irq_desc[irq].affinity = CPU_MASK_ALL;
J
James Bottomley 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
		return -EINVAL;
	}

	/* whatever mask they set, we just allow one CPU */
	cpu_dest = first_cpu(*dest);
	*dest = cpumask_of_cpu(cpu_dest);

	return 0;
}

static void cpu_set_affinity_irq(unsigned int irq, cpumask_t dest)
{
	if (cpu_check_affinity(irq, &dest))
		return;

148
	irq_desc[irq].affinity = dest;
J
James Bottomley 已提交
149 150 151
}
#endif

L
Linus Torvalds 已提交
152 153 154 155 156 157
static struct hw_interrupt_type cpu_interrupt_type = {
	.typename	= "CPU",
	.startup	= cpu_startup_irq,
	.shutdown	= cpu_disable_irq,
	.enable		= cpu_enable_irq,
	.disable	= cpu_disable_irq,
J
James Bottomley 已提交
158 159
	.ack		= cpu_ack_irq,
	.end		= cpu_end_irq,
J
James Bottomley 已提交
160 161 162
#ifdef CONFIG_SMP
	.set_affinity	= cpu_set_affinity_irq,
#endif
163 164 165 166
	/* XXX: Needs to be written.  We managed without it so far, but
	 * we really ought to write it.
	 */
	.retrigger	= NULL,
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
};

int show_interrupts(struct seq_file *p, void *v)
{
	int i = *(loff_t *) v, j;
	unsigned long flags;

	if (i == 0) {
		seq_puts(p, "    ");
		for_each_online_cpu(j)
			seq_printf(p, "       CPU%d", j);

#ifdef PARISC_IRQ_CR16_COUNTS
		seq_printf(p, " [min/avg/max] (CPU cycle counts)");
#endif
		seq_putc(p, '\n');
	}

	if (i < NR_IRQS) {
		struct irqaction *action;

		spin_lock_irqsave(&irq_desc[i].lock, flags);
		action = irq_desc[i].action;
		if (!action)
			goto skip;
		seq_printf(p, "%3d: ", i);
#ifdef CONFIG_SMP
		for_each_online_cpu(j)
			seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
#else
		seq_printf(p, "%10u ", kstat_irqs(i));
#endif

200
		seq_printf(p, " %14s", irq_desc[i].chip->typename);
L
Linus Torvalds 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
#ifndef PARISC_IRQ_CR16_COUNTS
		seq_printf(p, "  %s", action->name);

		while ((action = action->next))
			seq_printf(p, ", %s", action->name);
#else
		for ( ;action; action = action->next) {
			unsigned int k, avg, min, max;

			min = max = action->cr16_hist[0];

			for (avg = k = 0; k < PARISC_CR16_HIST_SIZE; k++) {
				int hist = action->cr16_hist[k];

				if (hist) {
					avg += hist;
				} else
					break;

				if (hist > max) max = hist;
				if (hist < min) min = hist;
			}

			avg /= k;
			seq_printf(p, " %s[%d/%d/%d]", action->name,
					min,avg,max);
		}
#endif

		seq_putc(p, '\n');
 skip:
		spin_unlock_irqrestore(&irq_desc[i].lock, flags);
	}

	return 0;
}



/*
** The following form a "set": Virtual IRQ, Transaction Address, Trans Data.
** Respectively, these map to IRQ region+EIRR, Processor HPA, EIRR bit.
**
** To use txn_XXX() interfaces, get a Virtual IRQ first.
** Then use that to get the Transaction address and data.
*/

248
int cpu_claim_irq(unsigned int irq, struct irq_chip *type, void *data)
L
Linus Torvalds 已提交
249 250 251
{
	if (irq_desc[irq].action)
		return -EBUSY;
252
	if (irq_desc[irq].chip != &cpu_interrupt_type)
L
Linus Torvalds 已提交
253 254 255
		return -EBUSY;

	if (type) {
256 257
		irq_desc[irq].chip = type;
		irq_desc[irq].chip_data = data;
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
		cpu_interrupt_type.enable(irq);
	}
	return 0;
}

int txn_claim_irq(int irq)
{
	return cpu_claim_irq(irq, NULL, NULL) ? -1 : irq;
}

/*
 * The bits_wide parameter accommodates the limitations of the HW/SW which
 * use these bits:
 * Legacy PA I/O (GSC/NIO): 5 bits (architected EIM register)
 * V-class (EPIC):          6 bits
 * N/L/A-class (iosapic):   8 bits
 * PCI 2.2 MSI:            16 bits
 * Some PCI devices:       32 bits (Symbios SCSI/ATM/HyperFabric)
 *
 * On the service provider side:
 * o PA 1.1 (and PA2.0 narrow mode)     5-bits (width of EIR register)
 * o PA 2.0 wide mode                   6-bits (per processor)
 * o IA64                               8-bits (0-256 total)
 *
 * So a Legacy PA I/O device on a PA 2.0 box can't use all the bits supported
 * by the processor...and the N/L-class I/O subsystem supports more bits than
 * PA2.0 has. The first case is the problem.
 */
int txn_alloc_irq(unsigned int bits_wide)
{
	int irq;

	/* never return irq 0 cause that's the interval timer */
	for (irq = CPU_IRQ_BASE + 1; irq <= CPU_IRQ_MAX; irq++) {
		if (cpu_claim_irq(irq, NULL, NULL) < 0)
			continue;
		if ((irq - CPU_IRQ_BASE) >= (1 << bits_wide))
			continue;
		return irq;
	}

	/* unlikely, but be prepared */
	return -1;
}

303

J
James Bottomley 已提交
304 305
unsigned long txn_affinity_addr(unsigned int irq, int cpu)
{
306
#ifdef CONFIG_SMP
307
	irq_desc[irq].affinity = cpumask_of_cpu(cpu);
308
#endif
J
James Bottomley 已提交
309 310 311 312

	return cpu_data[cpu].txn_addr;
}

313

L
Linus Torvalds 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327
unsigned long txn_alloc_addr(unsigned int virt_irq)
{
	static int next_cpu = -1;

	next_cpu++; /* assign to "next" CPU we want this bugger on */

	/* validate entry */
	while ((next_cpu < NR_CPUS) && (!cpu_data[next_cpu].txn_addr || 
		!cpu_online(next_cpu)))
		next_cpu++;

	if (next_cpu >= NR_CPUS) 
		next_cpu = 0;	/* nothing else, assign monarch */

J
James Bottomley 已提交
328
	return txn_affinity_addr(virt_irq, next_cpu);
L
Linus Torvalds 已提交
329 330 331 332 333 334 335 336
}


unsigned int txn_alloc_data(unsigned int virt_irq)
{
	return virt_irq - CPU_IRQ_BASE;
}

J
James Bottomley 已提交
337 338
static inline int eirr_to_irq(unsigned long eirr)
{
K
Kyle McMartin 已提交
339
	int bit = fls_long(eirr);
J
James Bottomley 已提交
340 341 342
	return (BITS_PER_LONG - bit) + TIMER_IRQ;
}

L
Linus Torvalds 已提交
343 344 345
/* ONLY called from entry.S:intr_extint() */
void do_cpu_irq_mask(struct pt_regs *regs)
{
M
Matthew Wilcox 已提交
346
	struct pt_regs *old_regs;
L
Linus Torvalds 已提交
347
	unsigned long eirr_val;
J
James Bottomley 已提交
348
	int irq, cpu = smp_processor_id();
349
#ifdef CONFIG_SMP
J
James Bottomley 已提交
350
	cpumask_t dest;
351
#endif
L
Linus Torvalds 已提交
352

M
Matthew Wilcox 已提交
353
	old_regs = set_irq_regs(regs);
J
James Bottomley 已提交
354 355
	local_irq_disable();
	irq_enter();
L
Linus Torvalds 已提交
356

J
James Bottomley 已提交
357 358 359 360 361
	eirr_val = mfctl(23) & cpu_eiem & global_ack_eiem &
		per_cpu(local_ack_eiem, cpu);
	if (!eirr_val)
		goto set_out;
	irq = eirr_to_irq(eirr_val);
J
James Bottomley 已提交
362

J
James Bottomley 已提交
363 364 365 366 367 368 369 370 371 372 373
#ifdef CONFIG_SMP
	dest = irq_desc[irq].affinity;
	if (CHECK_IRQ_PER_CPU(irq_desc[irq].status) &&
	    !cpu_isset(smp_processor_id(), dest)) {
		int cpu = first_cpu(dest);

		printk(KERN_DEBUG "redirecting irq %d from CPU %d to %d\n",
		       irq, smp_processor_id(), cpu);
		gsc_writel(irq + CPU_IRQ_BASE,
			   cpu_data[cpu].hpa);
		goto set_out;
L
Linus Torvalds 已提交
374
	}
J
James Bottomley 已提交
375
#endif
376
	__do_IRQ(irq);
377

J
James Bottomley 已提交
378
 out:
L
Linus Torvalds 已提交
379
	irq_exit();
M
Matthew Wilcox 已提交
380
	set_irq_regs(old_regs);
J
James Bottomley 已提交
381
	return;
L
Linus Torvalds 已提交
382

J
James Bottomley 已提交
383 384 385 386
 set_out:
	set_eiem(cpu_eiem & global_ack_eiem & per_cpu(local_ack_eiem, cpu));
	goto out;
}
L
Linus Torvalds 已提交
387 388 389 390

static struct irqaction timer_action = {
	.handler = timer_interrupt,
	.name = "timer",
B
Bernhard Walle 已提交
391
	.flags = IRQF_DISABLED | IRQF_TIMER | IRQF_PERCPU | IRQF_IRQPOLL,
L
Linus Torvalds 已提交
392 393 394 395 396 397
};

#ifdef CONFIG_SMP
static struct irqaction ipi_action = {
	.handler = ipi_interrupt,
	.name = "IPI",
J
James Bottomley 已提交
398
	.flags = IRQF_DISABLED | IRQF_PERCPU,
L
Linus Torvalds 已提交
399 400 401 402 403 404 405
};
#endif

static void claim_cpu_irqs(void)
{
	int i;
	for (i = CPU_IRQ_BASE; i <= CPU_IRQ_MAX; i++) {
406
		irq_desc[i].chip = &cpu_interrupt_type;
L
Linus Torvalds 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
	}

	irq_desc[TIMER_IRQ].action = &timer_action;
	irq_desc[TIMER_IRQ].status |= IRQ_PER_CPU;
#ifdef CONFIG_SMP
	irq_desc[IPI_IRQ].action = &ipi_action;
	irq_desc[IPI_IRQ].status = IRQ_PER_CPU;
#endif
}

void __init init_IRQ(void)
{
	local_irq_disable();	/* PARANOID - should already be disabled */
	mtctl(~0UL, 23);	/* EIRR : clear all pending external intr */
	claim_cpu_irqs();
#ifdef CONFIG_SMP
	if (!cpu_eiem)
		cpu_eiem = EIEM_MASK(IPI_IRQ) | EIEM_MASK(TIMER_IRQ);
#else
	cpu_eiem = EIEM_MASK(TIMER_IRQ);
#endif
        set_eiem(cpu_eiem);	/* EIEM : enable all external intr */

}

void ack_bad_irq(unsigned int irq)
{
	printk("unexpected IRQ %d\n", irq);
}