irq.c 11.7 KB
Newer Older
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
/*
 *  arch/ppc/kernel/irq.c
 *
 *  Derived from arch/i386/kernel/irq.c
 *    Copyright (C) 1992 Linus Torvalds
 *  Adapted from arch/i386 by Gary Thomas
 *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
 *  Updated and modified by Cort Dougan (cort@cs.nmt.edu)
 *    Copyright (C) 1996 Cort Dougan
 *  Adapted for Power Macintosh by Paul Mackerras
 *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
 *  Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
 * 
 * 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 of the License, or (at your option) any later version.
 *
 * This file contains the code used by various IRQ handling routines:
 * asking for different IRQ's should be done through these routines
 * instead of just grabbing them. Thus setups with different IRQ numbers
 * shouldn't result in any weird surprises, and installing new handlers
 * should be easier.
 */

#include <linux/errno.h>
27
#include <linux/module.h>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <linux/threads.h>
#include <linux/kernel_stat.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/timex.h>
#include <linux/config.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/proc_fs.h>
#include <linux/random.h>
43
#include <linux/kallsyms.h>
44
#include <linux/profile.h>
45
#include <linux/bitops.h>
46 47 48 49 50 51 52 53 54 55 56

#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/pgtable.h>
#include <asm/irq.h>
#include <asm/cache.h>
#include <asm/prom.h>
#include <asm/ptrace.h>
#include <asm/iSeries/LparData.h>
#include <asm/machdep.h>
57
#include <asm/paca.h>
58 59 60 61 62

#ifdef CONFIG_SMP
extern void iSeries_smp_message_recv( struct pt_regs * );
#endif

63
extern irq_desc_t irq_desc[NR_IRQS];
64

65
int distribute_irqs = 1;
Andrew Morton's avatar
Andrew Morton committed
66
int __irq_offset_value;
David Gibson's avatar
David Gibson committed
67 68
int ppc_spurious_interrupts;
unsigned long lpevent_count;
69
u64 ppc64_interrupt_controller;
70 71 72

int show_interrupts(struct seq_file *p, void *v)
{
73
	int i = *(loff_t *) v, j;
74
	struct irqaction * action;
Andrew Morton's avatar
Andrew Morton committed
75
	irq_desc_t *desc;
76
	unsigned long flags;
77

78 79 80 81 82 83 84
	if (i == 0) {
		seq_printf(p, "           ");
		for (j=0; j<NR_CPUS; j++) {
			if (cpu_online(j))
				seq_printf(p, "CPU%d       ",j);
		}
		seq_putc(p, '\n');
85
	}
86

87
	if (i < NR_IRQS) {
Andrew Morton's avatar
Andrew Morton committed
88 89 90
		desc = get_irq_desc(i);
		spin_lock_irqsave(&desc->lock, flags);
		action = desc->action;
91
		if (!action || !action->handler)
92
			goto skip;
93
		seq_printf(p, "%3d: ", i);
94
#ifdef CONFIG_SMP
95 96
		for (j = 0; j < NR_CPUS; j++) {
			if (cpu_online(j))
97
				seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
98
		}
99
#else
100 101
		seq_printf(p, "%10u ", kstat_irqs(i));
#endif /* CONFIG_SMP */
Andrew Morton's avatar
Andrew Morton committed
102 103
		if (desc->handler)
			seq_printf(p, " %s ", desc->handler->typename );
104 105
		else
			seq_printf(p, "  None      ");
Andrew Morton's avatar
Andrew Morton committed
106
		seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge  ");
107 108 109 110
		seq_printf(p, "    %s",action->name);
		for (action=action->next; action; action = action->next)
			seq_printf(p, ", %s", action->name);
		seq_putc(p, '\n');
111
skip:
Andrew Morton's avatar
Andrew Morton committed
112
		spin_unlock_irqrestore(&desc->lock, flags);
113 114
	} else if (i == NR_IRQS)
		seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
115 116 117
	return 0;
}

118
extern int noirqdebug;
119 120 121 122 123 124 125 126 127 128

/*
 * Eventually, this should take an array of interrupts and an array size
 * so it can dispatch multiple interrupts.
 */
void ppc_irq_dispatch_handler(struct pt_regs *regs, int irq)
{
	int status;
	struct irqaction *action;
	int cpu = smp_processor_id();
Andrew Morton's avatar
Andrew Morton committed
129
	irq_desc_t *desc = get_irq_desc(irq);
130
	irqreturn_t action_ret;
131 132 133
#ifdef CONFIG_IRQSTACKS
	struct thread_info *curtp, *irqtp;
#endif
134

135
	kstat_cpu(cpu).irqs[irq]++;
136 137 138 139

	if (desc->status & IRQ_PER_CPU) {
		/* no locking required for CPU-local interrupts: */
		ack_irq(irq);
140
		action_ret = handle_IRQ_event(irq, regs, desc->action);
141 142 143 144
		desc->handler->end(irq);
		return;
	}

145 146 147 148 149 150 151
	spin_lock(&desc->lock);
	ack_irq(irq);	
	/*
	   REPLAY is when Linux resends an IRQ that was dropped earlier
	   WAITING is used by probe to mark irqs that are being tested
	   */
	status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
152
	status |= IRQ_PENDING; /* we _want_ to handle it */
153 154 155 156 157 158

	/*
	 * If the IRQ is disabled for whatever reason, we cannot
	 * use the action we have.
	 */
	action = NULL;
159
	if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
		action = desc->action;
		if (!action || !action->handler) {
			ppc_spurious_interrupts++;
			printk(KERN_DEBUG "Unhandled interrupt %x, disabled\n", irq);
			/* We can't call disable_irq here, it would deadlock */
			if (!desc->depth)
				desc->depth = 1;
			desc->status |= IRQ_DISABLED;
			/* This is not a real spurrious interrupt, we
			 * have to eoi it, so we jump to out
			 */
			mask_irq(irq);
			goto out;
		}
		status &= ~IRQ_PENDING; /* we commit to handling */
175
		status |= IRQ_INPROGRESS; /* we are handling it */
176 177 178 179 180 181 182 183 184
	}
	desc->status = status;

	/*
	 * If there is no IRQ handler or it was disabled, exit early.
	   Since we set PENDING, if another processor is handling
	   a different instance of this same irq, the other processor
	   will take care of it.
	 */
185
	if (unlikely(!action))
186 187 188 189 190 191 192 193 194 195 196 197 198 199
		goto out;

	/*
	 * Edge triggered interrupts need to remember
	 * pending events.
	 * This applies to any hw interrupts that allow a second
	 * instance of the same irq to arrive while we are in do_IRQ
	 * or in the handler. But the code here only handles the _second_
	 * instance of the irq, not the third or fourth. So it is mostly
	 * useful for irq hardware that does not mask cleanly in an
	 * SMP environment.
	 */
	for (;;) {
		spin_unlock(&desc->lock);
200 201 202 203 204 205 206 207

#ifdef CONFIG_IRQSTACKS
		/* Switch to the irq stack to handle this */
		curtp = current_thread_info();
		irqtp = hardirq_ctx[smp_processor_id()];
		if (curtp != irqtp) {
			irqtp->task = curtp->task;
			irqtp->flags = 0;
208
			action_ret = call_handle_IRQ_event(irq, regs, action, irqtp);
209 210 211 212 213
			irqtp->task = NULL;
			if (irqtp->flags)
				set_bits(irqtp->flags, &curtp->flags);
		} else
#endif
214
			action_ret = handle_IRQ_event(irq, regs, action);
215

216
		spin_lock(&desc->lock);
217 218
		if (!noirqdebug)
			note_interrupt(irq, desc, action_ret);
219
		if (likely(!(desc->status & IRQ_PENDING)))
220 221 222 223
			break;
		desc->status &= ~IRQ_PENDING;
	}
out:
224
	desc->status &= ~IRQ_INPROGRESS;
225 226 227 228
	/*
	 * The ->end() handler has to deal with interrupts which got
	 * disabled while the handler was running.
	 */
Andrew Morton's avatar
Andrew Morton committed
229 230 231 232 233
	if (desc->handler) {
		if (desc->handler->end)
			desc->handler->end(irq);
		else if (desc->handler->enable)
			desc->handler->enable(irq);
234 235 236 237
	}
	spin_unlock(&desc->lock);
}

238
#ifdef CONFIG_PPC_ISERIES
239
void do_IRQ(struct pt_regs *regs)
240
{
241
	struct paca_struct *lpaca;
242
	struct ItLpQueue *lpq;
243

244
	irq_enter();
245

246
#ifdef CONFIG_DEBUG_STACKOVERFLOW
247
	/* Debugging check for stack overflow: is there less than 2KB free? */
248 249 250
	{
		long sp;

251
		sp = __get_SP() & (THREAD_SIZE-1);
252

253
		if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
254 255 256 257 258 259 260
			printk("do_IRQ: stack overflow: %ld\n",
				sp - sizeof(struct thread_info));
			dump_stack();
		}
	}
#endif

261
	lpaca = get_paca();
262
#ifdef CONFIG_SMP
263 264
	if (lpaca->lppaca.int_dword.fields.ipi_cnt) {
		lpaca->lppaca.int_dword.fields.ipi_cnt = 0;
265 266
		iSeries_smp_message_recv(regs);
	}
267
#endif /* CONFIG_SMP */
David Gibson's avatar
David Gibson committed
268
	lpq = lpaca->lpqueue_ptr;
269
	if (lpq && ItLpQueue_isLpIntPending(lpq))
David Gibson's avatar
David Gibson committed
270
		lpevent_count += ItLpQueue_process(lpq, regs);
271 272 273

	irq_exit();

274 275
	if (lpaca->lppaca.int_dword.fields.decr_int) {
		lpaca->lppaca.int_dword.fields.decr_int = 0;
276 277 278 279
		/* Signal a fake decrementer interrupt */
		timer_interrupt(regs);
	}
}
280

281
#else	/* CONFIG_PPC_ISERIES */
282

283
void do_IRQ(struct pt_regs *regs)
284
{
285
	int irq;
286 287 288

	irq_enter();

289
#ifdef CONFIG_DEBUG_STACKOVERFLOW
290
	/* Debugging check for stack overflow: is there less than 2KB free? */
291 292 293 294 295
	{
		long sp;

		sp = __get_SP() & (THREAD_SIZE-1);

296
		if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
297 298 299 300 301 302 303
			printk("do_IRQ: stack overflow: %ld\n",
				sp - sizeof(struct thread_info));
			dump_stack();
		}
	}
#endif

304 305 306
	irq = ppc_md.get_irq(regs);

	if (irq >= 0)
307
		ppc_irq_dispatch_handler(regs, irq);
308
	else
309 310 311
		/* That's not SMP safe ... but who cares ? */
		ppc_spurious_interrupts++;

Anton Blanchard's avatar
Anton Blanchard committed
312
	irq_exit();
313
}
314
#endif	/* CONFIG_PPC_ISERIES */
315 316 317 318 319

void __init init_IRQ(void)
{
	static int once = 0;

320
	if (once)
321
		return;
322 323

	once++;
324

325
	ppc_md.init_IRQ();
326
	irq_ctx_init();
327 328
}

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
#ifndef CONFIG_PPC_ISERIES
/*
 * Virtual IRQ mapping code, used on systems with XICS interrupt controllers.
 */

#define UNDEFINED_IRQ 0xffffffff
unsigned int virt_irq_to_real_map[NR_IRQS];

/*
 * Don't use virtual irqs 0, 1, 2 for devices.
 * The pcnet32 driver considers interrupt numbers < 2 to be invalid,
 * and 2 is the XICS IPI interrupt.
 * We limit virtual irqs to 17 less than NR_IRQS so that when we
 * offset them by 16 (to reserve the first 16 for ISA interrupts)
 * we don't end up with an interrupt number >= NR_IRQS.
 */
#define MIN_VIRT_IRQ	3
Andrew Morton's avatar
Andrew Morton committed
346
#define MAX_VIRT_IRQ	(NR_IRQS - NUM_ISA_INTERRUPTS - 1)
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
#define NR_VIRT_IRQS	(MAX_VIRT_IRQ - MIN_VIRT_IRQ + 1)

void
virt_irq_init(void)
{
	int i;
	for (i = 0; i < NR_IRQS; i++)
		virt_irq_to_real_map[i] = UNDEFINED_IRQ;
}

/* Create a mapping for a real_irq if it doesn't already exist.
 * Return the virtual irq as a convenience.
 */
int virt_irq_create_mapping(unsigned int real_irq)
{
	unsigned int virq, first_virq;
	static int warned;

365
	if (ppc64_interrupt_controller == IC_OPEN_PIC)
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
		return real_irq;	/* no mapping for openpic (for now) */

	/* don't map interrupts < MIN_VIRT_IRQ */
	if (real_irq < MIN_VIRT_IRQ) {
		virt_irq_to_real_map[real_irq] = real_irq;
		return real_irq;
	}

	/* map to a number between MIN_VIRT_IRQ and MAX_VIRT_IRQ */
	virq = real_irq;
	if (virq > MAX_VIRT_IRQ)
		virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;

	/* search for this number or a free slot */
	first_virq = virq;
	while (virt_irq_to_real_map[virq] != UNDEFINED_IRQ) {
		if (virt_irq_to_real_map[virq] == real_irq)
			return virq;
		if (++virq > MAX_VIRT_IRQ)
			virq = MIN_VIRT_IRQ;
		if (virq == first_virq)
			goto nospace;	/* oops, no free slots */
	}

	virt_irq_to_real_map[virq] = real_irq;
	return virq;

 nospace:
	if (!warned) {
		printk(KERN_CRIT "Interrupt table is full\n");
		printk(KERN_CRIT "Increase NR_IRQS (currently %d) "
		       "in your kernel sources and rebuild.\n", NR_IRQS);
		warned = 1;
	}
	return NO_IRQ;
}

403 404 405 406 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
/*
 * In most cases will get a hit on the very first slot checked in the
 * virt_irq_to_real_map.  Only when there are a large number of
 * IRQs will this be expensive.
 */
unsigned int real_irq_to_virt_slowpath(unsigned int real_irq)
{
	unsigned int virq;
	unsigned int first_virq;

	virq = real_irq;

	if (virq > MAX_VIRT_IRQ)
		virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;

	first_virq = virq;

	do {
		if (virt_irq_to_real_map[virq] == real_irq)
			return virq;

		virq++;

		if (virq >= MAX_VIRT_IRQ)
			virq = 0;

	} while (first_virq != virq);

	return NO_IRQ;

}

435 436 437 438 439 440 441 442 443 444 445
#endif /* CONFIG_PPC_ISERIES */

#ifdef CONFIG_IRQSTACKS
struct thread_info *softirq_ctx[NR_CPUS];
struct thread_info *hardirq_ctx[NR_CPUS];

void irq_ctx_init(void)
{
	struct thread_info *tp;
	int i;

446
	for_each_cpu(i) {
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
		memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
		tp = softirq_ctx[i];
		tp->cpu = i;
		tp->preempt_count = SOFTIRQ_OFFSET;

		memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
		tp = hardirq_ctx[i];
		tp->cpu = i;
		tp->preempt_count = HARDIRQ_OFFSET;
	}
}

void do_softirq(void)
{
	unsigned long flags;
	struct thread_info *curtp, *irqtp;

	if (in_interrupt())
		return;

	local_irq_save(flags);

	if (local_softirq_pending()) {
		curtp = current_thread_info();
		irqtp = softirq_ctx[smp_processor_id()];
		irqtp->task = curtp->task;
		call_do_softirq(irqtp);
		irqtp->task = NULL;
	}

	local_irq_restore(flags);
}
EXPORT_SYMBOL(do_softirq);

#endif /* CONFIG_IRQSTACKS */

483 484 485 486 487 488 489
static int __init setup_noirqdistrib(char *str)
{
	distribute_irqs = 0;
	return 1;
}

__setup("noirqdistrib", setup_noirqdistrib);