blob: c3f58f2f9f525d15fcb12466c6d7321218b2fc80 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Derived from arch/i386/kernel/irq.c
3 * Copyright (C) 1992 Linus Torvalds
4 * Adapted from arch/i386 by Gary Thomas
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
Stephen Rothwell756e7102005-11-09 18:07:45 +11006 * Updated and modified by Cort Dougan <cort@fsmlabs.com>
7 * Copyright (C) 1996-2001 Cort Dougan
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Adapted for Power Macintosh by Paul Mackerras
9 * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
10 * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
Stephen Rothwell756e7102005-11-09 18:07:45 +110011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * This file contains the code used by various IRQ handling routines:
18 * asking for different IRQ's should be done through these routines
19 * instead of just grabbing them. Thus setups with different IRQ numbers
20 * shouldn't result in any weird surprises, and installing new handlers
21 * should be easier.
Stephen Rothwell756e7102005-11-09 18:07:45 +110022 *
23 * The MPC8xx has an interrupt mask in the SIU. If a bit is set, the
24 * interrupt is _enabled_. As expected, IRQ0 is bit 0 in the 32-bit
25 * mask register (of which only 16 are defined), hence the weird shifting
26 * and complement of the cached_irq_mask. I want to be able to stuff
27 * this right into the SIU SMASK register.
28 * Many of the prep/chrp functions are conditional compiled on CONFIG_8xx
29 * to reduce code space and undefined function references.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100032#undef DEBUG
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/threads.h>
36#include <linux/kernel_stat.h>
37#include <linux/signal.h>
38#include <linux/sched.h>
Stephen Rothwell756e7102005-11-09 18:07:45 +110039#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/ioport.h>
41#include <linux/interrupt.h>
42#include <linux/timex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/init.h>
44#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/delay.h>
46#include <linux/irq.h>
Stephen Rothwell756e7102005-11-09 18:07:45 +110047#include <linux/seq_file.h>
48#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/profile.h>
50#include <linux/bitops.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100051#include <linux/list.h>
52#include <linux/radix-tree.h>
53#include <linux/mutex.h>
54#include <linux/bootmem.h>
Jake Moilanen45934c42006-07-27 13:17:25 -050055#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#include <asm/uaccess.h>
58#include <asm/system.h>
59#include <asm/io.h>
60#include <asm/pgtable.h>
61#include <asm/irq.h>
62#include <asm/cache.h>
63#include <asm/prom.h>
64#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <asm/machdep.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100066#include <asm/udbg.h>
Stephen Rothwella50b56d2005-11-17 16:14:17 +110067#ifdef CONFIG_PPC_ISERIES
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <asm/paca.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#endif
70
Stephen Rothwell868accb2005-11-10 18:38:46 +110071int __irq_offset_value;
Stephen Rothwell756e7102005-11-09 18:07:45 +110072static int ppc_spurious_interrupts;
73
Stephen Rothwell756e7102005-11-09 18:07:45 +110074#ifdef CONFIG_PPC32
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100075EXPORT_SYMBOL(__irq_offset_value);
Stephen Rothwell756e7102005-11-09 18:07:45 +110076atomic_t ppc_n_lost_interrupts;
77
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100078#ifndef CONFIG_PPC_MERGE
79#define NR_MASK_WORDS ((NR_IRQS + 31) / 32)
80unsigned long ppc_cached_irq_mask[NR_MASK_WORDS];
81#endif
82
Stephen Rothwell756e7102005-11-09 18:07:45 +110083#ifdef CONFIG_TAU_INT
84extern int tau_initialized;
85extern int tau_interrupts(int);
86#endif
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100087#endif /* CONFIG_PPC32 */
Stephen Rothwell756e7102005-11-09 18:07:45 +110088
89#if defined(CONFIG_SMP) && !defined(CONFIG_PPC_MERGE)
90extern atomic_t ipi_recv;
91extern atomic_t ipi_sent;
92#endif
Stephen Rothwell756e7102005-11-09 18:07:45 +110093
94#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -070095EXPORT_SYMBOL(irq_desc);
96
97int distribute_irqs = 1;
Stephen Rothwell756e7102005-11-09 18:07:45 +110098#endif /* CONFIG_PPC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100int show_interrupts(struct seq_file *p, void *v)
101{
Stephen Rothwell756e7102005-11-09 18:07:45 +1100102 int i = *(loff_t *)v, j;
103 struct irqaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 irq_desc_t *desc;
105 unsigned long flags;
106
107 if (i == 0) {
Stephen Rothwell756e7102005-11-09 18:07:45 +1100108 seq_puts(p, " ");
109 for_each_online_cpu(j)
110 seq_printf(p, "CPU%d ", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 seq_putc(p, '\n');
112 }
113
114 if (i < NR_IRQS) {
115 desc = get_irq_desc(i);
116 spin_lock_irqsave(&desc->lock, flags);
117 action = desc->action;
118 if (!action || !action->handler)
119 goto skip;
120 seq_printf(p, "%3d: ", i);
121#ifdef CONFIG_SMP
Stephen Rothwell756e7102005-11-09 18:07:45 +1100122 for_each_online_cpu(j)
123 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#else
125 seq_printf(p, "%10u ", kstat_irqs(i));
126#endif /* CONFIG_SMP */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700127 if (desc->chip)
128 seq_printf(p, " %s ", desc->chip->typename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 else
Stephen Rothwell756e7102005-11-09 18:07:45 +1100130 seq_puts(p, " None ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge ");
Stephen Rothwell756e7102005-11-09 18:07:45 +1100132 seq_printf(p, " %s", action->name);
133 for (action = action->next; action; action = action->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 seq_printf(p, ", %s", action->name);
135 seq_putc(p, '\n');
136skip:
137 spin_unlock_irqrestore(&desc->lock, flags);
Stephen Rothwell756e7102005-11-09 18:07:45 +1100138 } else if (i == NR_IRQS) {
139#ifdef CONFIG_PPC32
140#ifdef CONFIG_TAU_INT
141 if (tau_initialized){
142 seq_puts(p, "TAU: ");
Andrew Morton394e3902006-03-23 03:01:05 -0800143 for_each_online_cpu(j)
144 seq_printf(p, "%10u ", tau_interrupts(j));
Stephen Rothwell756e7102005-11-09 18:07:45 +1100145 seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
146 }
147#endif
148#if defined(CONFIG_SMP) && !defined(CONFIG_PPC_MERGE)
149 /* should this be per processor send/receive? */
150 seq_printf(p, "IPI (recv/sent): %10u/%u\n",
151 atomic_read(&ipi_recv), atomic_read(&ipi_sent));
152#endif
153#endif /* CONFIG_PPC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
Stephen Rothwell756e7102005-11-09 18:07:45 +1100155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return 0;
157}
158
159#ifdef CONFIG_HOTPLUG_CPU
160void fixup_irqs(cpumask_t map)
161{
162 unsigned int irq;
163 static int warned;
164
165 for_each_irq(irq) {
166 cpumask_t mask;
167
168 if (irq_desc[irq].status & IRQ_PER_CPU)
169 continue;
170
Ingo Molnara53da522006-06-29 02:24:38 -0700171 cpus_and(mask, irq_desc[irq].affinity, map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (any_online_cpu(mask) == NR_CPUS) {
173 printk("Breaking affinity for irq %i\n", irq);
174 mask = map;
175 }
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700176 if (irq_desc[irq].chip->set_affinity)
177 irq_desc[irq].chip->set_affinity(irq, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 else if (irq_desc[irq].action && !(warned++))
179 printk("Cannot set affinity for irq %i\n", irq);
180 }
181
182 local_irq_enable();
183 mdelay(1);
184 local_irq_disable();
185}
186#endif
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188void do_IRQ(struct pt_regs *regs)
189{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000190 unsigned int irq;
Stephen Rothwellb709c082005-11-09 13:28:33 +1100191#ifdef CONFIG_IRQSTACKS
192 struct thread_info *curtp, *irqtp;
193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Stephen Rothwell756e7102005-11-09 18:07:45 +1100195 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197#ifdef CONFIG_DEBUG_STACKOVERFLOW
198 /* Debugging check for stack overflow: is there less than 2KB free? */
199 {
200 long sp;
201
202 sp = __get_SP() & (THREAD_SIZE-1);
203
204 if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
205 printk("do_IRQ: stack overflow: %ld\n",
206 sp - sizeof(struct thread_info));
207 dump_stack();
208 }
209 }
210#endif
211
Stephen Rothwell756e7102005-11-09 18:07:45 +1100212 /*
213 * Every platform is required to implement ppc_md.get_irq.
214 * This function will either return an irq number or -1 to
215 * indicate there are no more pending.
216 * The value -2 is for buggy hardware and means that this IRQ
217 * has already been handled. -- Tom
218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 irq = ppc_md.get_irq(regs);
220
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000221 if (irq != NO_IRQ && irq != NO_IRQ_IGNORE) {
Stephen Rothwellb709c082005-11-09 13:28:33 +1100222#ifdef CONFIG_IRQSTACKS
223 /* Switch to the irq stack to handle this */
224 curtp = current_thread_info();
225 irqtp = hardirq_ctx[smp_processor_id()];
226 if (curtp != irqtp) {
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000227 struct irq_desc *desc = irq_desc + irq;
228 void *handler = desc->handle_irq;
229 if (handler == NULL)
230 handler = &__do_IRQ;
Stephen Rothwellb709c082005-11-09 13:28:33 +1100231 irqtp->task = curtp->task;
232 irqtp->flags = 0;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000233 call_handle_irq(irq, desc, regs, irqtp, handler);
Stephen Rothwellb709c082005-11-09 13:28:33 +1100234 irqtp->task = NULL;
235 if (irqtp->flags)
236 set_bits(irqtp->flags, &curtp->flags);
237 } else
238#endif
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000239 generic_handle_irq(irq, regs);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000240 } else if (irq != NO_IRQ_IGNORE)
Stephen Rothwelle1995002005-11-16 18:53:29 +1100241 /* That's not SMP safe ... but who cares ? */
242 ppc_spurious_interrupts++;
Stephen Rothwell756e7102005-11-09 18:07:45 +1100243
Stephen Rothwelle1995002005-11-16 18:53:29 +1100244 irq_exit();
245
246#ifdef CONFIG_PPC_ISERIES
David Gibson3356bb9f72006-01-13 10:26:42 +1100247 if (get_lppaca()->int_dword.fields.decr_int) {
248 get_lppaca()->int_dword.fields.decr_int = 0;
249 /* Signal a fake decrementer interrupt */
250 timer_interrupt(regs);
Stephen Rothwelle1995002005-11-16 18:53:29 +1100251 }
252#endif
253}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255void __init init_IRQ(void)
256{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 ppc_md.init_IRQ();
Stephen Rothwell756e7102005-11-09 18:07:45 +1100258#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 irq_ctx_init();
Stephen Rothwell756e7102005-11-09 18:07:45 +1100260#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#ifdef CONFIG_IRQSTACKS
Andreas Mohr22722052006-06-23 02:05:30 -0700265struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
266struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268void irq_ctx_init(void)
269{
270 struct thread_info *tp;
271 int i;
272
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800273 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
275 tp = softirq_ctx[i];
276 tp->cpu = i;
277 tp->preempt_count = SOFTIRQ_OFFSET;
278
279 memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
280 tp = hardirq_ctx[i];
281 tp->cpu = i;
282 tp->preempt_count = HARDIRQ_OFFSET;
283 }
284}
285
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100286static inline void do_softirq_onstack(void)
287{
288 struct thread_info *curtp, *irqtp;
289
290 curtp = current_thread_info();
291 irqtp = softirq_ctx[smp_processor_id()];
292 irqtp->task = curtp->task;
293 call_do_softirq(irqtp);
294 irqtp->task = NULL;
295}
296
297#else
298#define do_softirq_onstack() __do_softirq()
299#endif /* CONFIG_IRQSTACKS */
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301void do_softirq(void)
302{
303 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (in_interrupt())
306 return;
307
308 local_irq_save(flags);
309
Paul Mackerras829035fd2006-07-03 00:25:40 -0700310 if (local_softirq_pending())
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100311 do_softirq_onstack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 local_irq_restore(flags);
314}
315EXPORT_SYMBOL(do_softirq);
316
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000317
318/*
319 * IRQ controller and virtual interrupts
320 */
321
322#ifdef CONFIG_PPC_MERGE
323
324static LIST_HEAD(irq_hosts);
325static spinlock_t irq_big_lock = SPIN_LOCK_UNLOCKED;
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000326static DEFINE_PER_CPU(unsigned int, irq_radix_reader);
327static unsigned int irq_radix_writer;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000328struct irq_map_entry irq_map[NR_IRQS];
329static unsigned int irq_virq_count = NR_IRQS;
330static struct irq_host *irq_default_host;
331
332struct irq_host *irq_alloc_host(unsigned int revmap_type,
333 unsigned int revmap_arg,
334 struct irq_host_ops *ops,
335 irq_hw_number_t inval_irq)
336{
337 struct irq_host *host;
338 unsigned int size = sizeof(struct irq_host);
339 unsigned int i;
340 unsigned int *rmap;
341 unsigned long flags;
342
343 /* Allocate structure and revmap table if using linear mapping */
344 if (revmap_type == IRQ_HOST_MAP_LINEAR)
345 size += revmap_arg * sizeof(unsigned int);
346 if (mem_init_done)
347 host = kzalloc(size, GFP_KERNEL);
348 else {
349 host = alloc_bootmem(size);
350 if (host)
351 memset(host, 0, size);
352 }
353 if (host == NULL)
354 return NULL;
355
356 /* Fill structure */
357 host->revmap_type = revmap_type;
358 host->inval_irq = inval_irq;
359 host->ops = ops;
360
361 spin_lock_irqsave(&irq_big_lock, flags);
362
363 /* If it's a legacy controller, check for duplicates and
364 * mark it as allocated (we use irq 0 host pointer for that
365 */
366 if (revmap_type == IRQ_HOST_MAP_LEGACY) {
367 if (irq_map[0].host != NULL) {
368 spin_unlock_irqrestore(&irq_big_lock, flags);
369 /* If we are early boot, we can't free the structure,
370 * too bad...
371 * this will be fixed once slab is made available early
372 * instead of the current cruft
373 */
374 if (mem_init_done)
375 kfree(host);
376 return NULL;
377 }
378 irq_map[0].host = host;
379 }
380
381 list_add(&host->link, &irq_hosts);
382 spin_unlock_irqrestore(&irq_big_lock, flags);
383
384 /* Additional setups per revmap type */
385 switch(revmap_type) {
386 case IRQ_HOST_MAP_LEGACY:
387 /* 0 is always the invalid number for legacy */
388 host->inval_irq = 0;
389 /* setup us as the host for all legacy interrupts */
390 for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
391 irq_map[i].hwirq = 0;
392 smp_wmb();
393 irq_map[i].host = host;
394 smp_wmb();
395
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700396 /* Clear norequest flags */
397 get_irq_desc(i)->status &= ~IRQ_NOREQUEST;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000398
399 /* Legacy flags are left to default at this point,
400 * one can then use irq_create_mapping() to
401 * explicitely change them
402 */
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700403 ops->map(host, i, i);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000404 }
405 break;
406 case IRQ_HOST_MAP_LINEAR:
407 rmap = (unsigned int *)(host + 1);
408 for (i = 0; i < revmap_arg; i++)
409 rmap[i] = IRQ_NONE;
410 host->revmap_data.linear.size = revmap_arg;
411 smp_wmb();
412 host->revmap_data.linear.revmap = rmap;
413 break;
414 default:
415 break;
416 }
417
418 pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
419
420 return host;
421}
422
423struct irq_host *irq_find_host(struct device_node *node)
424{
425 struct irq_host *h, *found = NULL;
426 unsigned long flags;
427
428 /* We might want to match the legacy controller last since
429 * it might potentially be set to match all interrupts in
430 * the absence of a device node. This isn't a problem so far
431 * yet though...
432 */
433 spin_lock_irqsave(&irq_big_lock, flags);
434 list_for_each_entry(h, &irq_hosts, link)
435 if (h->ops->match == NULL || h->ops->match(h, node)) {
436 found = h;
437 break;
438 }
439 spin_unlock_irqrestore(&irq_big_lock, flags);
440 return found;
441}
442EXPORT_SYMBOL_GPL(irq_find_host);
443
444void irq_set_default_host(struct irq_host *host)
445{
446 pr_debug("irq: Default host set to @0x%p\n", host);
447
448 irq_default_host = host;
449}
450
451void irq_set_virq_count(unsigned int count)
452{
453 pr_debug("irq: Trying to set virq count to %d\n", count);
454
455 BUG_ON(count < NUM_ISA_INTERRUPTS);
456 if (count < NR_IRQS)
457 irq_virq_count = count;
458}
459
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000460/* radix tree not lockless safe ! we use a brlock-type mecanism
461 * for now, until we can use a lockless radix tree
462 */
463static void irq_radix_wrlock(unsigned long *flags)
464{
465 unsigned int cpu, ok;
466
467 spin_lock_irqsave(&irq_big_lock, *flags);
468 irq_radix_writer = 1;
469 smp_mb();
470 do {
471 barrier();
472 ok = 1;
473 for_each_possible_cpu(cpu) {
474 if (per_cpu(irq_radix_reader, cpu)) {
475 ok = 0;
476 break;
477 }
478 }
479 if (!ok)
480 cpu_relax();
481 } while(!ok);
482}
483
484static void irq_radix_wrunlock(unsigned long flags)
485{
486 smp_wmb();
487 irq_radix_writer = 0;
488 spin_unlock_irqrestore(&irq_big_lock, flags);
489}
490
491static void irq_radix_rdlock(unsigned long *flags)
492{
493 local_irq_save(*flags);
494 __get_cpu_var(irq_radix_reader) = 1;
495 smp_mb();
496 if (likely(irq_radix_writer == 0))
497 return;
498 __get_cpu_var(irq_radix_reader) = 0;
499 smp_wmb();
500 spin_lock(&irq_big_lock);
501 __get_cpu_var(irq_radix_reader) = 1;
502 spin_unlock(&irq_big_lock);
503}
504
505static void irq_radix_rdunlock(unsigned long flags)
506{
507 __get_cpu_var(irq_radix_reader) = 0;
508 local_irq_restore(flags);
509}
510
511
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000512unsigned int irq_create_mapping(struct irq_host *host,
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700513 irq_hw_number_t hwirq)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000514{
515 unsigned int virq, hint;
516
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700517 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000518
519 /* Look for default host if nececssary */
520 if (host == NULL)
521 host = irq_default_host;
522 if (host == NULL) {
523 printk(KERN_WARNING "irq_create_mapping called for"
524 " NULL host, hwirq=%lx\n", hwirq);
525 WARN_ON(1);
526 return NO_IRQ;
527 }
528 pr_debug("irq: -> using host @%p\n", host);
529
530 /* Check if mapping already exist, if it does, call
531 * host->ops->map() to update the flags
532 */
533 virq = irq_find_mapping(host, hwirq);
534 if (virq != IRQ_NONE) {
535 pr_debug("irq: -> existing mapping on virq %d\n", virq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000536 return virq;
537 }
538
539 /* Get a virtual interrupt number */
540 if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
541 /* Handle legacy */
542 virq = (unsigned int)hwirq;
543 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
544 return NO_IRQ;
545 return virq;
546 } else {
547 /* Allocate a virtual interrupt number */
548 hint = hwirq % irq_virq_count;
549 virq = irq_alloc_virt(host, 1, hint);
550 if (virq == NO_IRQ) {
551 pr_debug("irq: -> virq allocation failed\n");
552 return NO_IRQ;
553 }
554 }
555 pr_debug("irq: -> obtained virq %d\n", virq);
556
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700557 /* Clear IRQ_NOREQUEST flag */
558 get_irq_desc(virq)->status &= ~IRQ_NOREQUEST;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000559
560 /* map it */
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700561 smp_wmb();
562 irq_map[virq].hwirq = hwirq;
563 smp_mb();
564 if (host->ops->map(host, virq, hwirq)) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000565 pr_debug("irq: -> mapping failed, freeing\n");
566 irq_free_virt(virq, 1);
567 return NO_IRQ;
568 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000569 return virq;
570}
571EXPORT_SYMBOL_GPL(irq_create_mapping);
572
573extern unsigned int irq_create_of_mapping(struct device_node *controller,
574 u32 *intspec, unsigned int intsize)
575{
576 struct irq_host *host;
577 irq_hw_number_t hwirq;
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700578 unsigned int type = IRQ_TYPE_NONE;
579 unsigned int virq;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000580
581 if (controller == NULL)
582 host = irq_default_host;
583 else
584 host = irq_find_host(controller);
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700585 if (host == NULL) {
586 printk(KERN_WARNING "irq: no irq host found for %s !\n",
587 controller->full_name);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000588 return NO_IRQ;
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700589 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000590
591 /* If host has no translation, then we assume interrupt line */
592 if (host->ops->xlate == NULL)
593 hwirq = intspec[0];
594 else {
595 if (host->ops->xlate(host, controller, intspec, intsize,
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700596 &hwirq, &type))
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000597 return NO_IRQ;
598 }
599
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700600 /* Create mapping */
601 virq = irq_create_mapping(host, hwirq);
602 if (virq == NO_IRQ)
603 return virq;
604
605 /* Set type if specified and different than the current one */
606 if (type != IRQ_TYPE_NONE &&
607 type != (get_irq_desc(virq)->status & IRQF_TRIGGER_MASK))
608 set_irq_type(virq, type);
609 return virq;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000610}
611EXPORT_SYMBOL_GPL(irq_create_of_mapping);
612
613unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
614{
615 struct of_irq oirq;
616
617 if (of_irq_map_one(dev, index, &oirq))
618 return NO_IRQ;
619
620 return irq_create_of_mapping(oirq.controller, oirq.specifier,
621 oirq.size);
622}
623EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
624
625void irq_dispose_mapping(unsigned int virq)
626{
627 struct irq_host *host = irq_map[virq].host;
628 irq_hw_number_t hwirq;
629 unsigned long flags;
630
631 WARN_ON (host == NULL);
632 if (host == NULL)
633 return;
634
635 /* Never unmap legacy interrupts */
636 if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
637 return;
638
639 /* remove chip and handler */
640 set_irq_chip_and_handler(virq, NULL, NULL);
641
642 /* Make sure it's completed */
643 synchronize_irq(virq);
644
645 /* Tell the PIC about it */
646 if (host->ops->unmap)
647 host->ops->unmap(host, virq);
648 smp_mb();
649
650 /* Clear reverse map */
651 hwirq = irq_map[virq].hwirq;
652 switch(host->revmap_type) {
653 case IRQ_HOST_MAP_LINEAR:
654 if (hwirq < host->revmap_data.linear.size)
655 host->revmap_data.linear.revmap[hwirq] = IRQ_NONE;
656 break;
657 case IRQ_HOST_MAP_TREE:
658 /* Check if radix tree allocated yet */
659 if (host->revmap_data.tree.gfp_mask == 0)
660 break;
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000661 irq_radix_wrlock(&flags);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000662 radix_tree_delete(&host->revmap_data.tree, hwirq);
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000663 irq_radix_wrunlock(flags);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000664 break;
665 }
666
667 /* Destroy map */
668 smp_mb();
669 irq_map[virq].hwirq = host->inval_irq;
670
671 /* Set some flags */
672 get_irq_desc(virq)->status |= IRQ_NOREQUEST;
673
674 /* Free it */
675 irq_free_virt(virq, 1);
676}
677EXPORT_SYMBOL_GPL(irq_dispose_mapping);
678
679unsigned int irq_find_mapping(struct irq_host *host,
680 irq_hw_number_t hwirq)
681{
682 unsigned int i;
683 unsigned int hint = hwirq % irq_virq_count;
684
685 /* Look for default host if nececssary */
686 if (host == NULL)
687 host = irq_default_host;
688 if (host == NULL)
689 return NO_IRQ;
690
691 /* legacy -> bail early */
692 if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
693 return hwirq;
694
695 /* Slow path does a linear search of the map */
696 if (hint < NUM_ISA_INTERRUPTS)
697 hint = NUM_ISA_INTERRUPTS;
698 i = hint;
699 do {
700 if (irq_map[i].host == host &&
701 irq_map[i].hwirq == hwirq)
702 return i;
703 i++;
704 if (i >= irq_virq_count)
705 i = NUM_ISA_INTERRUPTS;
706 } while(i != hint);
707 return NO_IRQ;
708}
709EXPORT_SYMBOL_GPL(irq_find_mapping);
710
711
712unsigned int irq_radix_revmap(struct irq_host *host,
713 irq_hw_number_t hwirq)
714{
715 struct radix_tree_root *tree;
716 struct irq_map_entry *ptr;
717 unsigned int virq;
718 unsigned long flags;
719
720 WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
721
722 /* Check if the radix tree exist yet. We test the value of
723 * the gfp_mask for that. Sneaky but saves another int in the
724 * structure. If not, we fallback to slow mode
725 */
726 tree = &host->revmap_data.tree;
727 if (tree->gfp_mask == 0)
728 return irq_find_mapping(host, hwirq);
729
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000730 /* Now try to resolve */
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000731 irq_radix_rdlock(&flags);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000732 ptr = radix_tree_lookup(tree, hwirq);
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000733 irq_radix_rdunlock(flags);
734
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000735 /* Found it, return */
736 if (ptr) {
737 virq = ptr - irq_map;
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000738 return virq;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000739 }
740
741 /* If not there, try to insert it */
742 virq = irq_find_mapping(host, hwirq);
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000743 if (virq != NO_IRQ) {
744 irq_radix_wrlock(&flags);
Benjamin Herrenschmidte5c14ce2006-08-17 03:59:27 +0200745 radix_tree_insert(tree, hwirq, &irq_map[virq]);
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000746 irq_radix_wrunlock(flags);
747 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000748 return virq;
749}
750
751unsigned int irq_linear_revmap(struct irq_host *host,
752 irq_hw_number_t hwirq)
753{
754 unsigned int *revmap;
755
756 WARN_ON(host->revmap_type != IRQ_HOST_MAP_LINEAR);
757
758 /* Check revmap bounds */
759 if (unlikely(hwirq >= host->revmap_data.linear.size))
760 return irq_find_mapping(host, hwirq);
761
762 /* Check if revmap was allocated */
763 revmap = host->revmap_data.linear.revmap;
764 if (unlikely(revmap == NULL))
765 return irq_find_mapping(host, hwirq);
766
767 /* Fill up revmap with slow path if no mapping found */
768 if (unlikely(revmap[hwirq] == NO_IRQ))
769 revmap[hwirq] = irq_find_mapping(host, hwirq);
770
771 return revmap[hwirq];
772}
773
774unsigned int irq_alloc_virt(struct irq_host *host,
775 unsigned int count,
776 unsigned int hint)
777{
778 unsigned long flags;
779 unsigned int i, j, found = NO_IRQ;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000780
781 if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
782 return NO_IRQ;
783
784 spin_lock_irqsave(&irq_big_lock, flags);
785
786 /* Use hint for 1 interrupt if any */
787 if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
788 hint < irq_virq_count && irq_map[hint].host == NULL) {
789 found = hint;
790 goto hint_found;
791 }
792
793 /* Look for count consecutive numbers in the allocatable
794 * (non-legacy) space
795 */
Michael Ellermane1251462006-08-02 10:48:50 +1000796 for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
797 if (irq_map[i].host != NULL)
798 j = 0;
799 else
800 j++;
801
802 if (j == count) {
803 found = i - count + 1;
804 break;
805 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000806 }
807 if (found == NO_IRQ) {
808 spin_unlock_irqrestore(&irq_big_lock, flags);
809 return NO_IRQ;
810 }
811 hint_found:
812 for (i = found; i < (found + count); i++) {
813 irq_map[i].hwirq = host->inval_irq;
814 smp_wmb();
815 irq_map[i].host = host;
816 }
817 spin_unlock_irqrestore(&irq_big_lock, flags);
818 return found;
819}
820
821void irq_free_virt(unsigned int virq, unsigned int count)
822{
823 unsigned long flags;
824 unsigned int i;
825
826 WARN_ON (virq < NUM_ISA_INTERRUPTS);
827 WARN_ON (count == 0 || (virq + count) > irq_virq_count);
828
829 spin_lock_irqsave(&irq_big_lock, flags);
830 for (i = virq; i < (virq + count); i++) {
831 struct irq_host *host;
832
833 if (i < NUM_ISA_INTERRUPTS ||
834 (virq + count) > irq_virq_count)
835 continue;
836
837 host = irq_map[i].host;
838 irq_map[i].hwirq = host->inval_irq;
839 smp_wmb();
840 irq_map[i].host = NULL;
841 }
842 spin_unlock_irqrestore(&irq_big_lock, flags);
843}
844
845void irq_early_init(void)
846{
847 unsigned int i;
848
849 for (i = 0; i < NR_IRQS; i++)
850 get_irq_desc(i)->status |= IRQ_NOREQUEST;
851}
852
853/* We need to create the radix trees late */
854static int irq_late_init(void)
855{
856 struct irq_host *h;
857 unsigned long flags;
858
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000859 irq_radix_wrlock(&flags);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000860 list_for_each_entry(h, &irq_hosts, link) {
861 if (h->revmap_type == IRQ_HOST_MAP_TREE)
862 INIT_RADIX_TREE(&h->revmap_data.tree, GFP_ATOMIC);
863 }
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000864 irq_radix_wrunlock(flags);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000865
866 return 0;
867}
868arch_initcall(irq_late_init);
869
870#endif /* CONFIG_PPC_MERGE */
871
Jake Moilanen204face42006-06-07 16:15:10 -0500872#ifdef CONFIG_PCI_MSI
873int pci_enable_msi(struct pci_dev * pdev)
874{
875 if (ppc_md.enable_msi)
876 return ppc_md.enable_msi(pdev);
877 else
878 return -1;
879}
Jake Moilanen45934c42006-07-27 13:17:25 -0500880EXPORT_SYMBOL(pci_enable_msi);
Jake Moilanen204face42006-06-07 16:15:10 -0500881
882void pci_disable_msi(struct pci_dev * pdev)
883{
884 if (ppc_md.disable_msi)
885 ppc_md.disable_msi(pdev);
886}
Jake Moilanen45934c42006-07-27 13:17:25 -0500887EXPORT_SYMBOL(pci_disable_msi);
Jake Moilanen204face42006-06-07 16:15:10 -0500888
889void pci_scan_msi_device(struct pci_dev *dev) {}
890int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec) {return -1;}
891void pci_disable_msix(struct pci_dev *dev) {}
892void msi_remove_pci_irq_vectors(struct pci_dev *dev) {}
893void disable_msi_mode(struct pci_dev *dev, int pos, int type) {}
894void pci_no_msi(void) {}
Jake Moilanen45934c42006-07-27 13:17:25 -0500895EXPORT_SYMBOL(pci_enable_msix);
896EXPORT_SYMBOL(pci_disable_msix);
Jake Moilanen204face42006-06-07 16:15:10 -0500897
898#endif
899
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100900#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static int __init setup_noirqdistrib(char *str)
902{
903 distribute_irqs = 0;
904 return 1;
905}
906
907__setup("noirqdistrib", setup_noirqdistrib);
Stephen Rothwell756e7102005-11-09 18:07:45 +1100908#endif /* CONFIG_PPC64 */