powerpc: Reduce footprint of xics_ipi_struct
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / powerpc / kernel / irq.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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)
756e7104
SR
6 * Updated and modified by Cort Dougan <cort@fsmlabs.com>
7 * Copyright (C) 1996-2001 Cort Dougan
1da177e4
LT
8 * Adapted for Power Macintosh by Paul Mackerras
9 * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
756e7104 10 *
1da177e4
LT
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 * This file contains the code used by various IRQ handling routines:
17 * asking for different IRQ's should be done through these routines
18 * instead of just grabbing them. Thus setups with different IRQ numbers
19 * shouldn't result in any weird surprises, and installing new handlers
20 * should be easier.
756e7104
SR
21 *
22 * The MPC8xx has an interrupt mask in the SIU. If a bit is set, the
23 * interrupt is _enabled_. As expected, IRQ0 is bit 0 in the 32-bit
24 * mask register (of which only 16 are defined), hence the weird shifting
25 * and complement of the cached_irq_mask. I want to be able to stuff
26 * this right into the SIU SMASK register.
27 * Many of the prep/chrp functions are conditional compiled on CONFIG_8xx
28 * to reduce code space and undefined function references.
1da177e4
LT
29 */
30
0ebfff14
BH
31#undef DEBUG
32
1da177e4
LT
33#include <linux/module.h>
34#include <linux/threads.h>
35#include <linux/kernel_stat.h>
36#include <linux/signal.h>
37#include <linux/sched.h>
756e7104 38#include <linux/ptrace.h>
1da177e4
LT
39#include <linux/ioport.h>
40#include <linux/interrupt.h>
41#include <linux/timex.h>
1da177e4
LT
42#include <linux/init.h>
43#include <linux/slab.h>
1da177e4
LT
44#include <linux/delay.h>
45#include <linux/irq.h>
756e7104
SR
46#include <linux/seq_file.h>
47#include <linux/cpumask.h>
1da177e4
LT
48#include <linux/profile.h>
49#include <linux/bitops.h>
0ebfff14
BH
50#include <linux/list.h>
51#include <linux/radix-tree.h>
52#include <linux/mutex.h>
53#include <linux/bootmem.h>
45934c47 54#include <linux/pci.h>
60b332e7 55#include <linux/debugfs.h>
cdd6c482 56#include <linux/perf_event.h>
1da177e4
LT
57
58#include <asm/uaccess.h>
59#include <asm/system.h>
60#include <asm/io.h>
61#include <asm/pgtable.h>
62#include <asm/irq.h>
63#include <asm/cache.h>
64#include <asm/prom.h>
65#include <asm/ptrace.h>
1da177e4 66#include <asm/machdep.h>
0ebfff14 67#include <asm/udbg.h>
d04c56f7 68#ifdef CONFIG_PPC64
1da177e4 69#include <asm/paca.h>
d04c56f7 70#include <asm/firmware.h>
0874dd40 71#include <asm/lv1call.h>
756e7104 72#endif
1bf4af16
AB
73#define CREATE_TRACE_POINTS
74#include <asm/trace.h>
1da177e4 75
8c007bfd
AB
76DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
77EXPORT_PER_CPU_SYMBOL(irq_stat);
78
868accb7 79int __irq_offset_value;
756e7104
SR
80static int ppc_spurious_interrupts;
81
756e7104 82#ifdef CONFIG_PPC32
b9e5b4e6
BH
83EXPORT_SYMBOL(__irq_offset_value);
84atomic_t ppc_n_lost_interrupts;
756e7104 85
756e7104
SR
86#ifdef CONFIG_TAU_INT
87extern int tau_initialized;
88extern int tau_interrupts(int);
89#endif
b9e5b4e6 90#endif /* CONFIG_PPC32 */
756e7104 91
756e7104 92#ifdef CONFIG_PPC64
cd015707
ME
93
94#ifndef CONFIG_SPARSE_IRQ
1da177e4 95EXPORT_SYMBOL(irq_desc);
cd015707 96#endif
1da177e4
LT
97
98int distribute_irqs = 1;
d04c56f7 99
4e491d14 100static inline notrace unsigned long get_hard_enabled(void)
ef2b343e
HD
101{
102 unsigned long enabled;
103
104 __asm__ __volatile__("lbz %0,%1(13)"
105 : "=r" (enabled) : "i" (offsetof(struct paca_struct, hard_enabled)));
106
107 return enabled;
108}
109
4e491d14 110static inline notrace void set_soft_enabled(unsigned long enable)
ef2b343e
HD
111{
112 __asm__ __volatile__("stb %0,%1(13)"
113 : : "r" (enable), "i" (offsetof(struct paca_struct, soft_enabled)));
114}
115
4e491d14 116notrace void raw_local_irq_restore(unsigned long en)
d04c56f7 117{
ef2b343e
HD
118 /*
119 * get_paca()->soft_enabled = en;
120 * Is it ever valid to use local_irq_restore(0) when soft_enabled is 1?
121 * That was allowed before, and in such a case we do need to take care
122 * that gcc will set soft_enabled directly via r13, not choose to use
123 * an intermediate register, lest we're preempted to a different cpu.
124 */
125 set_soft_enabled(en);
d04c56f7
PM
126 if (!en)
127 return;
128
94491685 129#ifdef CONFIG_PPC_STD_MMU_64
d04c56f7 130 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
ef2b343e
HD
131 /*
132 * Do we need to disable preemption here? Not really: in the
133 * unlikely event that we're preempted to a different cpu in
134 * between getting r13, loading its lppaca_ptr, and loading
135 * its any_int, we might call iseries_handle_interrupts without
136 * an interrupt pending on the new cpu, but that's no disaster,
137 * is it? And the business of preempting us off the old cpu
138 * would itself involve a local_irq_restore which handles the
139 * interrupt to that cpu.
140 *
141 * But use "local_paca->lppaca_ptr" instead of "get_lppaca()"
142 * to avoid any preemption checking added into get_paca().
143 */
144 if (local_paca->lppaca_ptr->int_dword.any_int)
d04c56f7 145 iseries_handle_interrupts();
d04c56f7 146 }
94491685 147#endif /* CONFIG_PPC_STD_MMU_64 */
d04c56f7 148
cdd6c482
IM
149 if (test_perf_event_pending()) {
150 clear_perf_event_pending();
151 perf_event_do_pending();
b6c5a71d 152 }
93a6d3ce 153
ef2b343e
HD
154 /*
155 * if (get_paca()->hard_enabled) return;
156 * But again we need to take care that gcc gets hard_enabled directly
157 * via r13, not choose to use an intermediate register, lest we're
158 * preempted to a different cpu in between the two instructions.
159 */
160 if (get_hard_enabled())
d04c56f7 161 return;
ef2b343e
HD
162
163 /*
164 * Need to hard-enable interrupts here. Since currently disabled,
165 * no need to take further asm precautions against preemption; but
166 * use local_paca instead of get_paca() to avoid preemption checking.
167 */
168 local_paca->hard_enabled = en;
d04c56f7
PM
169 if ((int)mfspr(SPRN_DEC) < 0)
170 mtspr(SPRN_DEC, 1);
0874dd40
TS
171
172 /*
173 * Force the delivery of pending soft-disabled interrupts on PS3.
174 * Any HV call will have this side effect.
175 */
176 if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
177 u64 tmp;
178 lv1_get_version_info(&tmp);
179 }
180
e1fa2e13 181 __hard_irq_enable();
d04c56f7 182}
945feb17 183EXPORT_SYMBOL(raw_local_irq_restore);
756e7104 184#endif /* CONFIG_PPC64 */
1da177e4
LT
185
186int show_interrupts(struct seq_file *p, void *v)
187{
756e7104
SR
188 int i = *(loff_t *)v, j;
189 struct irqaction *action;
97f7d6bc 190 struct irq_desc *desc;
1da177e4
LT
191 unsigned long flags;
192
193 if (i == 0) {
756e7104
SR
194 seq_puts(p, " ");
195 for_each_online_cpu(j)
196 seq_printf(p, "CPU%d ", j);
1da177e4 197 seq_putc(p, '\n');
750ab112 198 } else if (i == nr_irqs) {
9c4cb825 199#if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT)
756e7104
SR
200 if (tau_initialized){
201 seq_puts(p, "TAU: ");
394e3902
AM
202 for_each_online_cpu(j)
203 seq_printf(p, "%10u ", tau_interrupts(j));
756e7104
SR
204 seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
205 }
9c4cb825 206#endif /* CONFIG_PPC32 && CONFIG_TAU_INT*/
1da177e4 207 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
750ab112
ME
208
209 return 0;
756e7104 210 }
750ab112
ME
211
212 desc = irq_to_desc(i);
213 if (!desc)
214 return 0;
215
239007b8 216 raw_spin_lock_irqsave(&desc->lock, flags);
750ab112
ME
217
218 action = desc->action;
219 if (!action || !action->handler)
220 goto skip;
221
222 seq_printf(p, "%3d: ", i);
223#ifdef CONFIG_SMP
224 for_each_online_cpu(j)
225 seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
226#else
227 seq_printf(p, "%10u ", kstat_irqs(i));
228#endif /* CONFIG_SMP */
229
230 if (desc->chip)
b27df672 231 seq_printf(p, " %s ", desc->chip->name);
750ab112
ME
232 else
233 seq_puts(p, " None ");
234
235 seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge ");
236 seq_printf(p, " %s", action->name);
237
238 for (action = action->next; action; action = action->next)
239 seq_printf(p, ", %s", action->name);
240 seq_putc(p, '\n');
241
242skip:
239007b8 243 raw_spin_unlock_irqrestore(&desc->lock, flags);
750ab112 244
1da177e4
LT
245 return 0;
246}
247
248#ifdef CONFIG_HOTPLUG_CPU
249void fixup_irqs(cpumask_t map)
250{
6cff46f4 251 struct irq_desc *desc;
1da177e4
LT
252 unsigned int irq;
253 static int warned;
254
255 for_each_irq(irq) {
256 cpumask_t mask;
257
6cff46f4
ME
258 desc = irq_to_desc(irq);
259 if (desc && desc->status & IRQ_PER_CPU)
1da177e4
LT
260 continue;
261
6cff46f4 262 cpumask_and(&mask, desc->affinity, &map);
1da177e4
LT
263 if (any_online_cpu(mask) == NR_CPUS) {
264 printk("Breaking affinity for irq %i\n", irq);
265 mask = map;
266 }
6cff46f4
ME
267 if (desc->chip->set_affinity)
268 desc->chip->set_affinity(irq, &mask);
269 else if (desc->action && !(warned++))
1da177e4
LT
270 printk("Cannot set affinity for irq %i\n", irq);
271 }
272
273 local_irq_enable();
274 mdelay(1);
275 local_irq_disable();
276}
277#endif
278
f2694ba5
ME
279#ifdef CONFIG_IRQSTACKS
280static inline void handle_one_irq(unsigned int irq)
281{
282 struct thread_info *curtp, *irqtp;
283 unsigned long saved_sp_limit;
284 struct irq_desc *desc;
f2694ba5
ME
285
286 /* Switch to the irq stack to handle this */
287 curtp = current_thread_info();
288 irqtp = hardirq_ctx[smp_processor_id()];
289
290 if (curtp == irqtp) {
291 /* We're already on the irq stack, just handle it */
292 generic_handle_irq(irq);
293 return;
294 }
295
6cff46f4 296 desc = irq_to_desc(irq);
f2694ba5
ME
297 saved_sp_limit = current->thread.ksp_limit;
298
f2694ba5
ME
299 irqtp->task = curtp->task;
300 irqtp->flags = 0;
301
302 /* Copy the softirq bits in preempt_count so that the
303 * softirq checks work in the hardirq context. */
304 irqtp->preempt_count = (irqtp->preempt_count & ~SOFTIRQ_MASK) |
305 (curtp->preempt_count & SOFTIRQ_MASK);
306
307 current->thread.ksp_limit = (unsigned long)irqtp +
308 _ALIGN_UP(sizeof(struct thread_info), 16);
309
835363e6 310 call_handle_irq(irq, desc, irqtp, desc->handle_irq);
f2694ba5
ME
311 current->thread.ksp_limit = saved_sp_limit;
312 irqtp->task = NULL;
313
314 /* Set any flag that may have been set on the
315 * alternate stack
316 */
317 if (irqtp->flags)
318 set_bits(irqtp->flags, &curtp->flags);
319}
320#else
321static inline void handle_one_irq(unsigned int irq)
322{
323 generic_handle_irq(irq);
324}
325#endif
326
d7cb10d6
ME
327static inline void check_stack_overflow(void)
328{
329#ifdef CONFIG_DEBUG_STACKOVERFLOW
330 long sp;
331
332 sp = __get_SP() & (THREAD_SIZE-1);
333
334 /* check for stack overflow: is there less than 2KB free? */
335 if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
336 printk("do_IRQ: stack overflow: %ld\n",
337 sp - sizeof(struct thread_info));
338 dump_stack();
339 }
340#endif
341}
342
1da177e4
LT
343void do_IRQ(struct pt_regs *regs)
344{
7d12e780 345 struct pt_regs *old_regs = set_irq_regs(regs);
0ebfff14 346 unsigned int irq;
1da177e4 347
1bf4af16
AB
348 trace_irq_entry(regs);
349
4b218e9b 350 irq_enter();
1da177e4 351
d7cb10d6 352 check_stack_overflow();
1da177e4 353
35a84c2f 354 irq = ppc_md.get_irq();
1da177e4 355
f2694ba5
ME
356 if (irq != NO_IRQ && irq != NO_IRQ_IGNORE)
357 handle_one_irq(irq);
358 else if (irq != NO_IRQ_IGNORE)
e199500c
SR
359 /* That's not SMP safe ... but who cares ? */
360 ppc_spurious_interrupts++;
361
4b218e9b 362 irq_exit();
7d12e780 363 set_irq_regs(old_regs);
756e7104 364
e199500c 365#ifdef CONFIG_PPC_ISERIES
b06a3183
SR
366 if (firmware_has_feature(FW_FEATURE_ISERIES) &&
367 get_lppaca()->int_dword.fields.decr_int) {
3356bb9f
DG
368 get_lppaca()->int_dword.fields.decr_int = 0;
369 /* Signal a fake decrementer interrupt */
370 timer_interrupt(regs);
e199500c
SR
371 }
372#endif
1bf4af16
AB
373
374 trace_irq_exit(regs);
e199500c 375}
1da177e4
LT
376
377void __init init_IRQ(void)
378{
70584578
SR
379 if (ppc_md.init_IRQ)
380 ppc_md.init_IRQ();
bcf0b088
KG
381
382 exc_lvl_ctx_init();
383
1da177e4
LT
384 irq_ctx_init();
385}
386
bcf0b088
KG
387#if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
388struct thread_info *critirq_ctx[NR_CPUS] __read_mostly;
389struct thread_info *dbgirq_ctx[NR_CPUS] __read_mostly;
390struct thread_info *mcheckirq_ctx[NR_CPUS] __read_mostly;
391
392void exc_lvl_ctx_init(void)
393{
394 struct thread_info *tp;
395 int i;
396
397 for_each_possible_cpu(i) {
398 memset((void *)critirq_ctx[i], 0, THREAD_SIZE);
399 tp = critirq_ctx[i];
400 tp->cpu = i;
401 tp->preempt_count = 0;
402
403#ifdef CONFIG_BOOKE
404 memset((void *)dbgirq_ctx[i], 0, THREAD_SIZE);
405 tp = dbgirq_ctx[i];
406 tp->cpu = i;
407 tp->preempt_count = 0;
408
409 memset((void *)mcheckirq_ctx[i], 0, THREAD_SIZE);
410 tp = mcheckirq_ctx[i];
411 tp->cpu = i;
412 tp->preempt_count = HARDIRQ_OFFSET;
413#endif
414 }
415}
416#endif
1da177e4 417
1da177e4 418#ifdef CONFIG_IRQSTACKS
22722051
AM
419struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
420struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
1da177e4
LT
421
422void irq_ctx_init(void)
423{
424 struct thread_info *tp;
425 int i;
426
0e551954 427 for_each_possible_cpu(i) {
1da177e4
LT
428 memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
429 tp = softirq_ctx[i];
430 tp->cpu = i;
e6768a4f 431 tp->preempt_count = 0;
1da177e4
LT
432
433 memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
434 tp = hardirq_ctx[i];
435 tp->cpu = i;
436 tp->preempt_count = HARDIRQ_OFFSET;
437 }
438}
439
c6622f63
PM
440static inline void do_softirq_onstack(void)
441{
442 struct thread_info *curtp, *irqtp;
85218827 443 unsigned long saved_sp_limit = current->thread.ksp_limit;
c6622f63
PM
444
445 curtp = current_thread_info();
446 irqtp = softirq_ctx[smp_processor_id()];
447 irqtp->task = curtp->task;
85218827
KG
448 current->thread.ksp_limit = (unsigned long)irqtp +
449 _ALIGN_UP(sizeof(struct thread_info), 16);
c6622f63 450 call_do_softirq(irqtp);
85218827 451 current->thread.ksp_limit = saved_sp_limit;
c6622f63
PM
452 irqtp->task = NULL;
453}
1da177e4 454
c6622f63
PM
455#else
456#define do_softirq_onstack() __do_softirq()
457#endif /* CONFIG_IRQSTACKS */
458
1da177e4
LT
459void do_softirq(void)
460{
461 unsigned long flags;
1da177e4
LT
462
463 if (in_interrupt())
1da177e4
LT
464 return;
465
1da177e4 466 local_irq_save(flags);
1da177e4 467
912b2539 468 if (local_softirq_pending())
c6622f63 469 do_softirq_onstack();
1da177e4
LT
470
471 local_irq_restore(flags);
1da177e4 472}
1da177e4 473
1da177e4 474
1da177e4 475/*
0ebfff14 476 * IRQ controller and virtual interrupts
1da177e4
LT
477 */
478
0ebfff14 479static LIST_HEAD(irq_hosts);
057b184a 480static DEFINE_SPINLOCK(irq_big_lock);
967e012e 481static unsigned int revmap_trees_allocated;
150c6c8f 482static DEFINE_MUTEX(revmap_trees_mutex);
0ebfff14
BH
483struct irq_map_entry irq_map[NR_IRQS];
484static unsigned int irq_virq_count = NR_IRQS;
485static struct irq_host *irq_default_host;
1da177e4 486
35923f12
OJ
487irq_hw_number_t virq_to_hw(unsigned int virq)
488{
489 return irq_map[virq].hwirq;
490}
491EXPORT_SYMBOL_GPL(virq_to_hw);
492
68158006
ME
493static int default_irq_host_match(struct irq_host *h, struct device_node *np)
494{
495 return h->of_node != NULL && h->of_node == np;
496}
497
5669c3cf 498struct irq_host *irq_alloc_host(struct device_node *of_node,
52964f87
ME
499 unsigned int revmap_type,
500 unsigned int revmap_arg,
501 struct irq_host_ops *ops,
502 irq_hw_number_t inval_irq)
1da177e4 503{
0ebfff14
BH
504 struct irq_host *host;
505 unsigned int size = sizeof(struct irq_host);
506 unsigned int i;
507 unsigned int *rmap;
508 unsigned long flags;
509
510 /* Allocate structure and revmap table if using linear mapping */
511 if (revmap_type == IRQ_HOST_MAP_LINEAR)
512 size += revmap_arg * sizeof(unsigned int);
5669c3cf 513 host = zalloc_maybe_bootmem(size, GFP_KERNEL);
0ebfff14
BH
514 if (host == NULL)
515 return NULL;
7d01c880 516
0ebfff14
BH
517 /* Fill structure */
518 host->revmap_type = revmap_type;
519 host->inval_irq = inval_irq;
520 host->ops = ops;
19fc65b5 521 host->of_node = of_node_get(of_node);
7d01c880 522
68158006
ME
523 if (host->ops->match == NULL)
524 host->ops->match = default_irq_host_match;
7d01c880 525
0ebfff14
BH
526 spin_lock_irqsave(&irq_big_lock, flags);
527
528 /* If it's a legacy controller, check for duplicates and
529 * mark it as allocated (we use irq 0 host pointer for that
530 */
531 if (revmap_type == IRQ_HOST_MAP_LEGACY) {
532 if (irq_map[0].host != NULL) {
533 spin_unlock_irqrestore(&irq_big_lock, flags);
534 /* If we are early boot, we can't free the structure,
535 * too bad...
536 * this will be fixed once slab is made available early
537 * instead of the current cruft
538 */
539 if (mem_init_done)
540 kfree(host);
541 return NULL;
542 }
543 irq_map[0].host = host;
544 }
545
546 list_add(&host->link, &irq_hosts);
547 spin_unlock_irqrestore(&irq_big_lock, flags);
548
549 /* Additional setups per revmap type */
550 switch(revmap_type) {
551 case IRQ_HOST_MAP_LEGACY:
552 /* 0 is always the invalid number for legacy */
553 host->inval_irq = 0;
554 /* setup us as the host for all legacy interrupts */
555 for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
7866291d 556 irq_map[i].hwirq = i;
0ebfff14
BH
557 smp_wmb();
558 irq_map[i].host = host;
559 smp_wmb();
560
6e99e458 561 /* Clear norequest flags */
6cff46f4 562 irq_to_desc(i)->status &= ~IRQ_NOREQUEST;
0ebfff14
BH
563
564 /* Legacy flags are left to default at this point,
565 * one can then use irq_create_mapping() to
c03983ac 566 * explicitly change them
0ebfff14 567 */
6e99e458 568 ops->map(host, i, i);
0ebfff14
BH
569 }
570 break;
571 case IRQ_HOST_MAP_LINEAR:
572 rmap = (unsigned int *)(host + 1);
573 for (i = 0; i < revmap_arg; i++)
f5921697 574 rmap[i] = NO_IRQ;
0ebfff14
BH
575 host->revmap_data.linear.size = revmap_arg;
576 smp_wmb();
577 host->revmap_data.linear.revmap = rmap;
578 break;
579 default:
580 break;
581 }
582
583 pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
584
585 return host;
1da177e4
LT
586}
587
0ebfff14 588struct irq_host *irq_find_host(struct device_node *node)
1da177e4 589{
0ebfff14
BH
590 struct irq_host *h, *found = NULL;
591 unsigned long flags;
592
593 /* We might want to match the legacy controller last since
594 * it might potentially be set to match all interrupts in
595 * the absence of a device node. This isn't a problem so far
596 * yet though...
597 */
598 spin_lock_irqsave(&irq_big_lock, flags);
599 list_for_each_entry(h, &irq_hosts, link)
68158006 600 if (h->ops->match(h, node)) {
0ebfff14
BH
601 found = h;
602 break;
603 }
604 spin_unlock_irqrestore(&irq_big_lock, flags);
605 return found;
606}
607EXPORT_SYMBOL_GPL(irq_find_host);
608
609void irq_set_default_host(struct irq_host *host)
610{
611 pr_debug("irq: Default host set to @0x%p\n", host);
1da177e4 612
0ebfff14
BH
613 irq_default_host = host;
614}
1da177e4 615
0ebfff14
BH
616void irq_set_virq_count(unsigned int count)
617{
618 pr_debug("irq: Trying to set virq count to %d\n", count);
fef1c772 619
0ebfff14
BH
620 BUG_ON(count < NUM_ISA_INTERRUPTS);
621 if (count < NR_IRQS)
622 irq_virq_count = count;
623}
624
6fde40f3
ME
625static int irq_setup_virq(struct irq_host *host, unsigned int virq,
626 irq_hw_number_t hwirq)
627{
cd015707
ME
628 struct irq_desc *desc;
629
630 desc = irq_to_desc_alloc_node(virq, 0);
631 if (!desc) {
632 pr_debug("irq: -> allocating desc failed\n");
633 goto error;
634 }
635
6fde40f3 636 /* Clear IRQ_NOREQUEST flag */
cd015707 637 desc->status &= ~IRQ_NOREQUEST;
6fde40f3
ME
638
639 /* map it */
640 smp_wmb();
641 irq_map[virq].hwirq = hwirq;
642 smp_mb();
643
644 if (host->ops->map(host, virq, hwirq)) {
645 pr_debug("irq: -> mapping failed, freeing\n");
cd015707 646 goto error;
6fde40f3
ME
647 }
648
649 return 0;
cd015707
ME
650
651error:
652 irq_free_virt(virq, 1);
653 return -1;
6fde40f3 654}
8ec8f2e8 655
ee51de56
ME
656unsigned int irq_create_direct_mapping(struct irq_host *host)
657{
658 unsigned int virq;
659
660 if (host == NULL)
661 host = irq_default_host;
662
663 BUG_ON(host == NULL);
664 WARN_ON(host->revmap_type != IRQ_HOST_MAP_NOMAP);
665
666 virq = irq_alloc_virt(host, 1, 0);
667 if (virq == NO_IRQ) {
668 pr_debug("irq: create_direct virq allocation failed\n");
669 return NO_IRQ;
670 }
671
672 pr_debug("irq: create_direct obtained virq %d\n", virq);
673
674 if (irq_setup_virq(host, virq, virq))
675 return NO_IRQ;
676
677 return virq;
678}
679
0ebfff14 680unsigned int irq_create_mapping(struct irq_host *host,
6e99e458 681 irq_hw_number_t hwirq)
0ebfff14
BH
682{
683 unsigned int virq, hint;
684
6e99e458 685 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
0ebfff14
BH
686
687 /* Look for default host if nececssary */
688 if (host == NULL)
689 host = irq_default_host;
690 if (host == NULL) {
691 printk(KERN_WARNING "irq_create_mapping called for"
692 " NULL host, hwirq=%lx\n", hwirq);
693 WARN_ON(1);
694 return NO_IRQ;
1da177e4 695 }
0ebfff14 696 pr_debug("irq: -> using host @%p\n", host);
1da177e4 697
0ebfff14
BH
698 /* Check if mapping already exist, if it does, call
699 * host->ops->map() to update the flags
700 */
701 virq = irq_find_mapping(host, hwirq);
f5921697 702 if (virq != NO_IRQ) {
acc900ef
IK
703 if (host->ops->remap)
704 host->ops->remap(host, virq, hwirq);
0ebfff14 705 pr_debug("irq: -> existing mapping on virq %d\n", virq);
0ebfff14 706 return virq;
1da177e4
LT
707 }
708
0ebfff14
BH
709 /* Get a virtual interrupt number */
710 if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
711 /* Handle legacy */
712 virq = (unsigned int)hwirq;
713 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
714 return NO_IRQ;
715 return virq;
716 } else {
717 /* Allocate a virtual interrupt number */
718 hint = hwirq % irq_virq_count;
719 virq = irq_alloc_virt(host, 1, hint);
720 if (virq == NO_IRQ) {
721 pr_debug("irq: -> virq allocation failed\n");
722 return NO_IRQ;
723 }
724 }
0ebfff14 725
6fde40f3 726 if (irq_setup_virq(host, virq, hwirq))
0ebfff14 727 return NO_IRQ;
6fde40f3 728
c7d07fdd
ME
729 printk(KERN_DEBUG "irq: irq %lu on host %s mapped to virtual irq %u\n",
730 hwirq, host->of_node ? host->of_node->full_name : "null", virq);
731
1da177e4 732 return virq;
0ebfff14
BH
733}
734EXPORT_SYMBOL_GPL(irq_create_mapping);
735
f3d2ab41 736unsigned int irq_create_of_mapping(struct device_node *controller,
40d50cf7 737 const u32 *intspec, unsigned int intsize)
0ebfff14
BH
738{
739 struct irq_host *host;
740 irq_hw_number_t hwirq;
6e99e458
BH
741 unsigned int type = IRQ_TYPE_NONE;
742 unsigned int virq;
1da177e4 743
0ebfff14
BH
744 if (controller == NULL)
745 host = irq_default_host;
746 else
747 host = irq_find_host(controller);
6e99e458
BH
748 if (host == NULL) {
749 printk(KERN_WARNING "irq: no irq host found for %s !\n",
750 controller->full_name);
0ebfff14 751 return NO_IRQ;
6e99e458 752 }
0ebfff14
BH
753
754 /* If host has no translation, then we assume interrupt line */
755 if (host->ops->xlate == NULL)
756 hwirq = intspec[0];
757 else {
758 if (host->ops->xlate(host, controller, intspec, intsize,
6e99e458 759 &hwirq, &type))
0ebfff14 760 return NO_IRQ;
1da177e4 761 }
0ebfff14 762
6e99e458
BH
763 /* Create mapping */
764 virq = irq_create_mapping(host, hwirq);
765 if (virq == NO_IRQ)
766 return virq;
767
768 /* Set type if specified and different than the current one */
769 if (type != IRQ_TYPE_NONE &&
6cff46f4 770 type != (irq_to_desc(virq)->status & IRQF_TRIGGER_MASK))
6e99e458
BH
771 set_irq_type(virq, type);
772 return virq;
1da177e4 773}
0ebfff14 774EXPORT_SYMBOL_GPL(irq_create_of_mapping);
1da177e4 775
0ebfff14 776unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
1da177e4 777{
0ebfff14 778 struct of_irq oirq;
1da177e4 779
0ebfff14
BH
780 if (of_irq_map_one(dev, index, &oirq))
781 return NO_IRQ;
1da177e4 782
0ebfff14
BH
783 return irq_create_of_mapping(oirq.controller, oirq.specifier,
784 oirq.size);
785}
786EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
1da177e4 787
0ebfff14
BH
788void irq_dispose_mapping(unsigned int virq)
789{
5414c6be 790 struct irq_host *host;
0ebfff14 791 irq_hw_number_t hwirq;
1da177e4 792
5414c6be
ME
793 if (virq == NO_IRQ)
794 return;
795
796 host = irq_map[virq].host;
0ebfff14
BH
797 WARN_ON (host == NULL);
798 if (host == NULL)
799 return;
1da177e4 800
0ebfff14
BH
801 /* Never unmap legacy interrupts */
802 if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
803 return;
1da177e4 804
0ebfff14
BH
805 /* remove chip and handler */
806 set_irq_chip_and_handler(virq, NULL, NULL);
807
808 /* Make sure it's completed */
809 synchronize_irq(virq);
810
811 /* Tell the PIC about it */
812 if (host->ops->unmap)
813 host->ops->unmap(host, virq);
814 smp_mb();
815
816 /* Clear reverse map */
817 hwirq = irq_map[virq].hwirq;
818 switch(host->revmap_type) {
819 case IRQ_HOST_MAP_LINEAR:
820 if (hwirq < host->revmap_data.linear.size)
f5921697 821 host->revmap_data.linear.revmap[hwirq] = NO_IRQ;
0ebfff14
BH
822 break;
823 case IRQ_HOST_MAP_TREE:
967e012e
SD
824 /*
825 * Check if radix tree allocated yet, if not then nothing to
826 * remove.
827 */
828 smp_rmb();
829 if (revmap_trees_allocated < 1)
0ebfff14 830 break;
150c6c8f 831 mutex_lock(&revmap_trees_mutex);
0ebfff14 832 radix_tree_delete(&host->revmap_data.tree, hwirq);
150c6c8f 833 mutex_unlock(&revmap_trees_mutex);
0ebfff14
BH
834 break;
835 }
1da177e4 836
0ebfff14
BH
837 /* Destroy map */
838 smp_mb();
839 irq_map[virq].hwirq = host->inval_irq;
1da177e4 840
0ebfff14 841 /* Set some flags */
6cff46f4 842 irq_to_desc(virq)->status |= IRQ_NOREQUEST;
1da177e4 843
0ebfff14
BH
844 /* Free it */
845 irq_free_virt(virq, 1);
1da177e4 846}
0ebfff14 847EXPORT_SYMBOL_GPL(irq_dispose_mapping);
1da177e4 848
0ebfff14
BH
849unsigned int irq_find_mapping(struct irq_host *host,
850 irq_hw_number_t hwirq)
851{
852 unsigned int i;
853 unsigned int hint = hwirq % irq_virq_count;
854
855 /* Look for default host if nececssary */
856 if (host == NULL)
857 host = irq_default_host;
858 if (host == NULL)
859 return NO_IRQ;
860
861 /* legacy -> bail early */
862 if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
863 return hwirq;
864
865 /* Slow path does a linear search of the map */
866 if (hint < NUM_ISA_INTERRUPTS)
867 hint = NUM_ISA_INTERRUPTS;
868 i = hint;
869 do {
870 if (irq_map[i].host == host &&
871 irq_map[i].hwirq == hwirq)
872 return i;
873 i++;
874 if (i >= irq_virq_count)
875 i = NUM_ISA_INTERRUPTS;
876 } while(i != hint);
877 return NO_IRQ;
878}
879EXPORT_SYMBOL_GPL(irq_find_mapping);
1da177e4 880
0ebfff14 881
967e012e
SD
882unsigned int irq_radix_revmap_lookup(struct irq_host *host,
883 irq_hw_number_t hwirq)
1da177e4 884{
0ebfff14
BH
885 struct irq_map_entry *ptr;
886 unsigned int virq;
1da177e4 887
0ebfff14 888 WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
1da177e4 889
967e012e
SD
890 /*
891 * Check if the radix tree exists and has bee initialized.
892 * If not, we fallback to slow mode
0ebfff14 893 */
967e012e 894 if (revmap_trees_allocated < 2)
0ebfff14
BH
895 return irq_find_mapping(host, hwirq);
896
0ebfff14 897 /* Now try to resolve */
150c6c8f
SD
898 /*
899 * No rcu_read_lock(ing) needed, the ptr returned can't go under us
900 * as it's referencing an entry in the static irq_map table.
901 */
967e012e 902 ptr = radix_tree_lookup(&host->revmap_data.tree, hwirq);
8ec8f2e8 903
967e012e
SD
904 /*
905 * If found in radix tree, then fine.
906 * Else fallback to linear lookup - this should not happen in practice
907 * as it means that we failed to insert the node in the radix tree.
908 */
909 if (ptr)
0ebfff14 910 virq = ptr - irq_map;
967e012e
SD
911 else
912 virq = irq_find_mapping(host, hwirq);
913
914 return virq;
915}
916
917void irq_radix_revmap_insert(struct irq_host *host, unsigned int virq,
918 irq_hw_number_t hwirq)
919{
967e012e
SD
920
921 WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
922
923 /*
924 * Check if the radix tree exists yet.
925 * If not, then the irq will be inserted into the tree when it gets
926 * initialized.
927 */
928 smp_rmb();
929 if (revmap_trees_allocated < 1)
930 return;
0ebfff14 931
8ec8f2e8 932 if (virq != NO_IRQ) {
150c6c8f 933 mutex_lock(&revmap_trees_mutex);
967e012e
SD
934 radix_tree_insert(&host->revmap_data.tree, hwirq,
935 &irq_map[virq]);
150c6c8f 936 mutex_unlock(&revmap_trees_mutex);
8ec8f2e8 937 }
1da177e4
LT
938}
939
0ebfff14
BH
940unsigned int irq_linear_revmap(struct irq_host *host,
941 irq_hw_number_t hwirq)
c6622f63 942{
0ebfff14 943 unsigned int *revmap;
c6622f63 944
0ebfff14
BH
945 WARN_ON(host->revmap_type != IRQ_HOST_MAP_LINEAR);
946
947 /* Check revmap bounds */
948 if (unlikely(hwirq >= host->revmap_data.linear.size))
949 return irq_find_mapping(host, hwirq);
950
951 /* Check if revmap was allocated */
952 revmap = host->revmap_data.linear.revmap;
953 if (unlikely(revmap == NULL))
954 return irq_find_mapping(host, hwirq);
955
956 /* Fill up revmap with slow path if no mapping found */
957 if (unlikely(revmap[hwirq] == NO_IRQ))
958 revmap[hwirq] = irq_find_mapping(host, hwirq);
959
960 return revmap[hwirq];
c6622f63
PM
961}
962
0ebfff14
BH
963unsigned int irq_alloc_virt(struct irq_host *host,
964 unsigned int count,
965 unsigned int hint)
966{
967 unsigned long flags;
968 unsigned int i, j, found = NO_IRQ;
c6622f63 969
0ebfff14
BH
970 if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
971 return NO_IRQ;
972
973 spin_lock_irqsave(&irq_big_lock, flags);
974
975 /* Use hint for 1 interrupt if any */
976 if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
977 hint < irq_virq_count && irq_map[hint].host == NULL) {
978 found = hint;
979 goto hint_found;
980 }
981
982 /* Look for count consecutive numbers in the allocatable
983 * (non-legacy) space
984 */
e1251465
ME
985 for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
986 if (irq_map[i].host != NULL)
987 j = 0;
988 else
989 j++;
990
991 if (j == count) {
992 found = i - count + 1;
993 break;
994 }
0ebfff14
BH
995 }
996 if (found == NO_IRQ) {
997 spin_unlock_irqrestore(&irq_big_lock, flags);
998 return NO_IRQ;
999 }
1000 hint_found:
1001 for (i = found; i < (found + count); i++) {
1002 irq_map[i].hwirq = host->inval_irq;
1003 smp_wmb();
1004 irq_map[i].host = host;
1005 }
1006 spin_unlock_irqrestore(&irq_big_lock, flags);
1007 return found;
1008}
1009
1010void irq_free_virt(unsigned int virq, unsigned int count)
1da177e4
LT
1011{
1012 unsigned long flags;
0ebfff14 1013 unsigned int i;
1da177e4 1014
0ebfff14
BH
1015 WARN_ON (virq < NUM_ISA_INTERRUPTS);
1016 WARN_ON (count == 0 || (virq + count) > irq_virq_count);
1da177e4 1017
0ebfff14
BH
1018 spin_lock_irqsave(&irq_big_lock, flags);
1019 for (i = virq; i < (virq + count); i++) {
1020 struct irq_host *host;
1da177e4 1021
0ebfff14
BH
1022 if (i < NUM_ISA_INTERRUPTS ||
1023 (virq + count) > irq_virq_count)
1024 continue;
1da177e4 1025
0ebfff14
BH
1026 host = irq_map[i].host;
1027 irq_map[i].hwirq = host->inval_irq;
1028 smp_wmb();
1029 irq_map[i].host = NULL;
1030 }
1031 spin_unlock_irqrestore(&irq_big_lock, flags);
1da177e4 1032}
0ebfff14 1033
cd015707 1034int arch_early_irq_init(void)
0ebfff14 1035{
cd015707
ME
1036 struct irq_desc *desc;
1037 int i;
0ebfff14 1038
cd015707
ME
1039 for (i = 0; i < NR_IRQS; i++) {
1040 desc = irq_to_desc(i);
1041 if (desc)
1042 desc->status |= IRQ_NOREQUEST;
1043 }
1044
1045 return 0;
1046}
1047
1048int arch_init_chip_data(struct irq_desc *desc, int node)
1049{
1050 desc->status |= IRQ_NOREQUEST;
1051 return 0;
0ebfff14
BH
1052}
1053
1054/* We need to create the radix trees late */
1055static int irq_late_init(void)
1056{
1057 struct irq_host *h;
967e012e 1058 unsigned int i;
0ebfff14 1059
967e012e
SD
1060 /*
1061 * No mutual exclusion with respect to accessors of the tree is needed
1062 * here as the synchronization is done via the state variable
1063 * revmap_trees_allocated.
1064 */
0ebfff14
BH
1065 list_for_each_entry(h, &irq_hosts, link) {
1066 if (h->revmap_type == IRQ_HOST_MAP_TREE)
967e012e
SD
1067 INIT_RADIX_TREE(&h->revmap_data.tree, GFP_KERNEL);
1068 }
1069
1070 /*
1071 * Make sure the radix trees inits are visible before setting
1072 * the flag
1073 */
1074 smp_wmb();
1075 revmap_trees_allocated = 1;
1076
1077 /*
1078 * Insert the reverse mapping for those interrupts already present
1079 * in irq_map[].
1080 */
150c6c8f 1081 mutex_lock(&revmap_trees_mutex);
967e012e
SD
1082 for (i = 0; i < irq_virq_count; i++) {
1083 if (irq_map[i].host &&
1084 (irq_map[i].host->revmap_type == IRQ_HOST_MAP_TREE))
1085 radix_tree_insert(&irq_map[i].host->revmap_data.tree,
1086 irq_map[i].hwirq, &irq_map[i]);
0ebfff14 1087 }
150c6c8f 1088 mutex_unlock(&revmap_trees_mutex);
0ebfff14 1089
967e012e
SD
1090 /*
1091 * Make sure the radix trees insertions are visible before setting
1092 * the flag
1093 */
1094 smp_wmb();
1095 revmap_trees_allocated = 2;
1096
0ebfff14
BH
1097 return 0;
1098}
1099arch_initcall(irq_late_init);
1100
60b332e7
ME
1101#ifdef CONFIG_VIRQ_DEBUG
1102static int virq_debug_show(struct seq_file *m, void *private)
1103{
1104 unsigned long flags;
97f7d6bc 1105 struct irq_desc *desc;
60b332e7
ME
1106 const char *p;
1107 char none[] = "none";
1108 int i;
1109
1110 seq_printf(m, "%-5s %-7s %-15s %s\n", "virq", "hwirq",
1111 "chip name", "host name");
1112
76f1d94f 1113 for (i = 1; i < nr_irqs; i++) {
6cff46f4 1114 desc = irq_to_desc(i);
76f1d94f
ME
1115 if (!desc)
1116 continue;
1117
239007b8 1118 raw_spin_lock_irqsave(&desc->lock, flags);
60b332e7
ME
1119
1120 if (desc->action && desc->action->handler) {
1121 seq_printf(m, "%5d ", i);
1122 seq_printf(m, "0x%05lx ", virq_to_hw(i));
1123
b27df672
TG
1124 if (desc->chip && desc->chip->name)
1125 p = desc->chip->name;
60b332e7
ME
1126 else
1127 p = none;
1128 seq_printf(m, "%-15s ", p);
1129
1130 if (irq_map[i].host && irq_map[i].host->of_node)
1131 p = irq_map[i].host->of_node->full_name;
1132 else
1133 p = none;
1134 seq_printf(m, "%s\n", p);
1135 }
1136
239007b8 1137 raw_spin_unlock_irqrestore(&desc->lock, flags);
60b332e7
ME
1138 }
1139
1140 return 0;
1141}
1142
1143static int virq_debug_open(struct inode *inode, struct file *file)
1144{
1145 return single_open(file, virq_debug_show, inode->i_private);
1146}
1147
1148static const struct file_operations virq_debug_fops = {
1149 .open = virq_debug_open,
1150 .read = seq_read,
1151 .llseek = seq_lseek,
1152 .release = single_release,
1153};
1154
1155static int __init irq_debugfs_init(void)
1156{
1157 if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
476ff8a0 1158 NULL, &virq_debug_fops) == NULL)
60b332e7
ME
1159 return -ENOMEM;
1160
1161 return 0;
1162}
1163__initcall(irq_debugfs_init);
1164#endif /* CONFIG_VIRQ_DEBUG */
1165
c6622f63 1166#ifdef CONFIG_PPC64
1da177e4
LT
1167static int __init setup_noirqdistrib(char *str)
1168{
1169 distribute_irqs = 0;
1170 return 1;
1171}
1172
1173__setup("noirqdistrib", setup_noirqdistrib);
756e7104 1174#endif /* CONFIG_PPC64 */