blob: baa49eb93228e0774274c976e28f30e57bda8058 [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)
Stephen Rothwell756e7102005-11-09 18:07:45 +110010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * 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.
Stephen Rothwell756e7102005-11-09 18:07:45 +110021 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100031#undef DEBUG
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#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>
Stephen Rothwell756e7102005-11-09 18:07:45 +110038#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/ioport.h>
40#include <linux/interrupt.h>
41#include <linux/timex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/init.h>
43#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/delay.h>
45#include <linux/irq.h>
Stephen Rothwell756e7102005-11-09 18:07:45 +110046#include <linux/seq_file.h>
47#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/profile.h>
49#include <linux/bitops.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100050#include <linux/list.h>
51#include <linux/radix-tree.h>
52#include <linux/mutex.h>
53#include <linux/bootmem.h>
Jake Moilanen45934c42006-07-27 13:17:25 -050054#include <linux/pci.h>
Michael Ellerman60b332e2007-08-28 18:47:57 +100055#include <linux/debugfs.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020056#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <asm/machdep.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100067#include <asm/udbg.h>
Paul Mackerrasd04c56f2006-10-04 16:47:49 +100068#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <asm/paca.h>
Paul Mackerrasd04c56f2006-10-04 16:47:49 +100070#include <asm/firmware.h>
Takao Shinohara0874dd42007-05-01 07:01:07 +100071#include <asm/lv1call.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#endif
73
Stephen Rothwell868accb2005-11-10 18:38:46 +110074int __irq_offset_value;
Stephen Rothwell756e7102005-11-09 18:07:45 +110075static int ppc_spurious_interrupts;
76
Stephen Rothwell756e7102005-11-09 18:07:45 +110077#ifdef CONFIG_PPC32
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100078EXPORT_SYMBOL(__irq_offset_value);
Stephen Rothwell756e7102005-11-09 18:07:45 +110079atomic_t ppc_n_lost_interrupts;
80
81#ifdef CONFIG_TAU_INT
82extern int tau_initialized;
83extern int tau_interrupts(int);
84#endif
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100085#endif /* CONFIG_PPC32 */
Stephen Rothwell756e7102005-11-09 18:07:45 +110086
Stephen Rothwell756e7102005-11-09 18:07:45 +110087#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -070088EXPORT_SYMBOL(irq_desc);
89
90int distribute_irqs = 1;
Paul Mackerrasd04c56f2006-10-04 16:47:49 +100091
Steven Rostedt4e491d12008-05-14 23:49:44 -040092static inline notrace unsigned long get_hard_enabled(void)
Hugh Dickinsef2b3432006-11-10 21:32:40 +000093{
94 unsigned long enabled;
95
96 __asm__ __volatile__("lbz %0,%1(13)"
97 : "=r" (enabled) : "i" (offsetof(struct paca_struct, hard_enabled)));
98
99 return enabled;
100}
101
Steven Rostedt4e491d12008-05-14 23:49:44 -0400102static inline notrace void set_soft_enabled(unsigned long enable)
Hugh Dickinsef2b3432006-11-10 21:32:40 +0000103{
104 __asm__ __volatile__("stb %0,%1(13)"
105 : : "r" (enable), "i" (offsetof(struct paca_struct, soft_enabled)));
106}
107
Steven Rostedt4e491d12008-05-14 23:49:44 -0400108notrace void raw_local_irq_restore(unsigned long en)
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000109{
Hugh Dickinsef2b3432006-11-10 21:32:40 +0000110 /*
111 * get_paca()->soft_enabled = en;
112 * Is it ever valid to use local_irq_restore(0) when soft_enabled is 1?
113 * That was allowed before, and in such a case we do need to take care
114 * that gcc will set soft_enabled directly via r13, not choose to use
115 * an intermediate register, lest we're preempted to a different cpu.
116 */
117 set_soft_enabled(en);
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000118 if (!en)
119 return;
120
Benjamin Herrenschmidt94491682009-06-02 21:17:45 +0000121#ifdef CONFIG_PPC_STD_MMU_64
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000122 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
Hugh Dickinsef2b3432006-11-10 21:32:40 +0000123 /*
124 * Do we need to disable preemption here? Not really: in the
125 * unlikely event that we're preempted to a different cpu in
126 * between getting r13, loading its lppaca_ptr, and loading
127 * its any_int, we might call iseries_handle_interrupts without
128 * an interrupt pending on the new cpu, but that's no disaster,
129 * is it? And the business of preempting us off the old cpu
130 * would itself involve a local_irq_restore which handles the
131 * interrupt to that cpu.
132 *
133 * But use "local_paca->lppaca_ptr" instead of "get_lppaca()"
134 * to avoid any preemption checking added into get_paca().
135 */
136 if (local_paca->lppaca_ptr->int_dword.any_int)
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000137 iseries_handle_interrupts();
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000138 }
Benjamin Herrenschmidt94491682009-06-02 21:17:45 +0000139#endif /* CONFIG_PPC_STD_MMU_64 */
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000140
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200141 if (test_perf_event_pending()) {
142 clear_perf_event_pending();
143 perf_event_do_pending();
Paul Mackerrasb6c5a71d2009-03-16 21:00:00 +1100144 }
Paul Mackerras93a6d3c2009-01-09 16:52:19 +1100145
Hugh Dickinsef2b3432006-11-10 21:32:40 +0000146 /*
147 * if (get_paca()->hard_enabled) return;
148 * But again we need to take care that gcc gets hard_enabled directly
149 * via r13, not choose to use an intermediate register, lest we're
150 * preempted to a different cpu in between the two instructions.
151 */
152 if (get_hard_enabled())
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000153 return;
Hugh Dickinsef2b3432006-11-10 21:32:40 +0000154
155 /*
156 * Need to hard-enable interrupts here. Since currently disabled,
157 * no need to take further asm precautions against preemption; but
158 * use local_paca instead of get_paca() to avoid preemption checking.
159 */
160 local_paca->hard_enabled = en;
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000161 if ((int)mfspr(SPRN_DEC) < 0)
162 mtspr(SPRN_DEC, 1);
Takao Shinohara0874dd42007-05-01 07:01:07 +1000163
164 /*
165 * Force the delivery of pending soft-disabled interrupts on PS3.
166 * Any HV call will have this side effect.
167 */
168 if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
169 u64 tmp;
170 lv1_get_version_info(&tmp);
171 }
172
Benjamin Herrenschmidte1fa2e12007-05-10 22:22:45 -0700173 __hard_irq_enable();
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000174}
Benjamin Herrenschmidt945feb12008-04-17 14:35:01 +1000175EXPORT_SYMBOL(raw_local_irq_restore);
Stephen Rothwell756e7102005-11-09 18:07:45 +1100176#endif /* CONFIG_PPC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178int show_interrupts(struct seq_file *p, void *v)
179{
Stephen Rothwell756e7102005-11-09 18:07:45 +1100180 int i = *(loff_t *)v, j;
181 struct irqaction *action;
Thomas Gleixner97f7d6b2009-03-10 14:45:54 +0000182 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 unsigned long flags;
184
185 if (i == 0) {
Stephen Rothwell756e7102005-11-09 18:07:45 +1100186 seq_puts(p, " ");
187 for_each_online_cpu(j)
188 seq_printf(p, "CPU%d ", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 seq_putc(p, '\n');
190 }
191
192 if (i < NR_IRQS) {
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000193 desc = irq_to_desc(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 spin_lock_irqsave(&desc->lock, flags);
195 action = desc->action;
196 if (!action || !action->handler)
197 goto skip;
198 seq_printf(p, "%3d: ", i);
199#ifdef CONFIG_SMP
Stephen Rothwell756e7102005-11-09 18:07:45 +1100200 for_each_online_cpu(j)
Yinghai Ludee41022009-01-11 00:29:15 -0800201 seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#else
203 seq_printf(p, "%10u ", kstat_irqs(i));
204#endif /* CONFIG_SMP */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700205 if (desc->chip)
206 seq_printf(p, " %s ", desc->chip->typename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 else
Stephen Rothwell756e7102005-11-09 18:07:45 +1100208 seq_puts(p, " None ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge ");
Stephen Rothwell756e7102005-11-09 18:07:45 +1100210 seq_printf(p, " %s", action->name);
211 for (action = action->next; action; action = action->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 seq_printf(p, ", %s", action->name);
213 seq_putc(p, '\n');
214skip:
215 spin_unlock_irqrestore(&desc->lock, flags);
Stephen Rothwell756e7102005-11-09 18:07:45 +1100216 } else if (i == NR_IRQS) {
Kumar Gala9c4cb822008-08-02 02:44:11 +1000217#if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT)
Stephen Rothwell756e7102005-11-09 18:07:45 +1100218 if (tau_initialized){
219 seq_puts(p, "TAU: ");
Andrew Morton394e3902006-03-23 03:01:05 -0800220 for_each_online_cpu(j)
221 seq_printf(p, "%10u ", tau_interrupts(j));
Stephen Rothwell756e7102005-11-09 18:07:45 +1100222 seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
223 }
Kumar Gala9c4cb822008-08-02 02:44:11 +1000224#endif /* CONFIG_PPC32 && CONFIG_TAU_INT*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
Stephen Rothwell756e7102005-11-09 18:07:45 +1100226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 0;
228}
229
230#ifdef CONFIG_HOTPLUG_CPU
231void fixup_irqs(cpumask_t map)
232{
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000233 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 unsigned int irq;
235 static int warned;
236
237 for_each_irq(irq) {
238 cpumask_t mask;
239
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000240 desc = irq_to_desc(irq);
241 if (desc && desc->status & IRQ_PER_CPU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 continue;
243
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000244 cpumask_and(&mask, desc->affinity, &map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (any_online_cpu(mask) == NR_CPUS) {
246 printk("Breaking affinity for irq %i\n", irq);
247 mask = map;
248 }
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000249 if (desc->chip->set_affinity)
250 desc->chip->set_affinity(irq, &mask);
251 else if (desc->action && !(warned++))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 printk("Cannot set affinity for irq %i\n", irq);
253 }
254
255 local_irq_enable();
256 mdelay(1);
257 local_irq_disable();
258}
259#endif
260
Michael Ellermanf2694ba2009-04-28 01:57:43 +0000261#ifdef CONFIG_IRQSTACKS
262static inline void handle_one_irq(unsigned int irq)
263{
264 struct thread_info *curtp, *irqtp;
265 unsigned long saved_sp_limit;
266 struct irq_desc *desc;
Michael Ellermanf2694ba2009-04-28 01:57:43 +0000267
268 /* Switch to the irq stack to handle this */
269 curtp = current_thread_info();
270 irqtp = hardirq_ctx[smp_processor_id()];
271
272 if (curtp == irqtp) {
273 /* We're already on the irq stack, just handle it */
274 generic_handle_irq(irq);
275 return;
276 }
277
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000278 desc = irq_to_desc(irq);
Michael Ellermanf2694ba2009-04-28 01:57:43 +0000279 saved_sp_limit = current->thread.ksp_limit;
280
Michael Ellermanf2694ba2009-04-28 01:57:43 +0000281 irqtp->task = curtp->task;
282 irqtp->flags = 0;
283
284 /* Copy the softirq bits in preempt_count so that the
285 * softirq checks work in the hardirq context. */
286 irqtp->preempt_count = (irqtp->preempt_count & ~SOFTIRQ_MASK) |
287 (curtp->preempt_count & SOFTIRQ_MASK);
288
289 current->thread.ksp_limit = (unsigned long)irqtp +
290 _ALIGN_UP(sizeof(struct thread_info), 16);
291
Michael Ellerman835363e2009-04-22 15:31:43 +0000292 call_handle_irq(irq, desc, irqtp, desc->handle_irq);
Michael Ellermanf2694ba2009-04-28 01:57:43 +0000293 current->thread.ksp_limit = saved_sp_limit;
294 irqtp->task = NULL;
295
296 /* Set any flag that may have been set on the
297 * alternate stack
298 */
299 if (irqtp->flags)
300 set_bits(irqtp->flags, &curtp->flags);
301}
302#else
303static inline void handle_one_irq(unsigned int irq)
304{
305 generic_handle_irq(irq);
306}
307#endif
308
Michael Ellermand7cb10d2009-04-22 15:31:37 +0000309static inline void check_stack_overflow(void)
310{
311#ifdef CONFIG_DEBUG_STACKOVERFLOW
312 long sp;
313
314 sp = __get_SP() & (THREAD_SIZE-1);
315
316 /* check for stack overflow: is there less than 2KB free? */
317 if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
318 printk("do_IRQ: stack overflow: %ld\n",
319 sp - sizeof(struct thread_info));
320 dump_stack();
321 }
322#endif
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325void do_IRQ(struct pt_regs *regs)
326{
David Howells7d12e782006-10-05 14:55:46 +0100327 struct pt_regs *old_regs = set_irq_regs(regs);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000328 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Scott Wood4b218e92007-08-21 02:36:19 +1000330 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Michael Ellermand7cb10d2009-04-22 15:31:37 +0000332 check_stack_overflow();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Olaf Hering35a84c22006-10-07 22:08:26 +1000334 irq = ppc_md.get_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Michael Ellermanf2694ba2009-04-28 01:57:43 +0000336 if (irq != NO_IRQ && irq != NO_IRQ_IGNORE)
337 handle_one_irq(irq);
338 else if (irq != NO_IRQ_IGNORE)
Stephen Rothwelle1995002005-11-16 18:53:29 +1100339 /* That's not SMP safe ... but who cares ? */
340 ppc_spurious_interrupts++;
Stephen Rothwell756e7102005-11-09 18:07:45 +1100341
Scott Wood4b218e92007-08-21 02:36:19 +1000342 irq_exit();
David Howells7d12e782006-10-05 14:55:46 +0100343 set_irq_regs(old_regs);
Stephen Rothwelle1995002005-11-16 18:53:29 +1100344
345#ifdef CONFIG_PPC_ISERIES
Stephen Rothwellb06a3182006-11-21 14:16:13 +1100346 if (firmware_has_feature(FW_FEATURE_ISERIES) &&
347 get_lppaca()->int_dword.fields.decr_int) {
David Gibson3356bb92006-01-13 10:26:42 +1100348 get_lppaca()->int_dword.fields.decr_int = 0;
349 /* Signal a fake decrementer interrupt */
350 timer_interrupt(regs);
Stephen Rothwelle1995002005-11-16 18:53:29 +1100351 }
352#endif
353}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355void __init init_IRQ(void)
356{
Sonny Rao70584572007-07-10 03:31:44 +1000357 if (ppc_md.init_IRQ)
358 ppc_md.init_IRQ();
Kumar Galabcf0b082008-04-30 03:49:55 -0500359
360 exc_lvl_ctx_init();
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 irq_ctx_init();
363}
364
Kumar Galabcf0b082008-04-30 03:49:55 -0500365#if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
366struct thread_info *critirq_ctx[NR_CPUS] __read_mostly;
367struct thread_info *dbgirq_ctx[NR_CPUS] __read_mostly;
368struct thread_info *mcheckirq_ctx[NR_CPUS] __read_mostly;
369
370void exc_lvl_ctx_init(void)
371{
372 struct thread_info *tp;
373 int i;
374
375 for_each_possible_cpu(i) {
376 memset((void *)critirq_ctx[i], 0, THREAD_SIZE);
377 tp = critirq_ctx[i];
378 tp->cpu = i;
379 tp->preempt_count = 0;
380
381#ifdef CONFIG_BOOKE
382 memset((void *)dbgirq_ctx[i], 0, THREAD_SIZE);
383 tp = dbgirq_ctx[i];
384 tp->cpu = i;
385 tp->preempt_count = 0;
386
387 memset((void *)mcheckirq_ctx[i], 0, THREAD_SIZE);
388 tp = mcheckirq_ctx[i];
389 tp->cpu = i;
390 tp->preempt_count = HARDIRQ_OFFSET;
391#endif
392 }
393}
394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396#ifdef CONFIG_IRQSTACKS
Andreas Mohr22722052006-06-23 02:05:30 -0700397struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
398struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400void irq_ctx_init(void)
401{
402 struct thread_info *tp;
403 int i;
404
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800405 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
407 tp = softirq_ctx[i];
408 tp->cpu = i;
Benjamin Herrenschmidte6768a42008-04-09 17:21:28 +1000409 tp->preempt_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
412 tp = hardirq_ctx[i];
413 tp->cpu = i;
414 tp->preempt_count = HARDIRQ_OFFSET;
415 }
416}
417
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100418static inline void do_softirq_onstack(void)
419{
420 struct thread_info *curtp, *irqtp;
Kumar Gala85218822008-04-28 16:21:22 +1000421 unsigned long saved_sp_limit = current->thread.ksp_limit;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100422
423 curtp = current_thread_info();
424 irqtp = softirq_ctx[smp_processor_id()];
425 irqtp->task = curtp->task;
Kumar Gala85218822008-04-28 16:21:22 +1000426 current->thread.ksp_limit = (unsigned long)irqtp +
427 _ALIGN_UP(sizeof(struct thread_info), 16);
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100428 call_do_softirq(irqtp);
Kumar Gala85218822008-04-28 16:21:22 +1000429 current->thread.ksp_limit = saved_sp_limit;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100430 irqtp->task = NULL;
431}
432
433#else
434#define do_softirq_onstack() __do_softirq()
435#endif /* CONFIG_IRQSTACKS */
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437void do_softirq(void)
438{
439 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (in_interrupt())
442 return;
443
444 local_irq_save(flags);
445
Paul Mackerras829035f2006-07-03 00:25:40 -0700446 if (local_softirq_pending())
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100447 do_softirq_onstack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 local_irq_restore(flags);
450}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000452
453/*
454 * IRQ controller and virtual interrupts
455 */
456
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000457static LIST_HEAD(irq_hosts);
Thomas Gleixner057b1842007-04-29 16:10:39 +0000458static DEFINE_SPINLOCK(irq_big_lock);
Sebastien Dugue967e0122008-09-04 22:37:07 +1000459static unsigned int revmap_trees_allocated;
Sebastien Dugue150c6c82008-09-04 22:37:08 +1000460static DEFINE_MUTEX(revmap_trees_mutex);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000461struct irq_map_entry irq_map[NR_IRQS];
462static unsigned int irq_virq_count = NR_IRQS;
463static struct irq_host *irq_default_host;
464
Olof Johansson35923f12007-06-04 14:47:04 +1000465irq_hw_number_t virq_to_hw(unsigned int virq)
466{
467 return irq_map[virq].hwirq;
468}
469EXPORT_SYMBOL_GPL(virq_to_hw);
470
Michael Ellerman68158002007-08-28 18:47:55 +1000471static int default_irq_host_match(struct irq_host *h, struct device_node *np)
472{
473 return h->of_node != NULL && h->of_node == np;
474}
475
Stephen Rothwell5669c3c2007-10-02 13:37:53 +1000476struct irq_host *irq_alloc_host(struct device_node *of_node,
Michael Ellerman52964f82007-08-28 18:47:54 +1000477 unsigned int revmap_type,
478 unsigned int revmap_arg,
479 struct irq_host_ops *ops,
480 irq_hw_number_t inval_irq)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000481{
482 struct irq_host *host;
483 unsigned int size = sizeof(struct irq_host);
484 unsigned int i;
485 unsigned int *rmap;
486 unsigned long flags;
487
488 /* Allocate structure and revmap table if using linear mapping */
489 if (revmap_type == IRQ_HOST_MAP_LINEAR)
490 size += revmap_arg * sizeof(unsigned int);
Stephen Rothwell5669c3c2007-10-02 13:37:53 +1000491 host = zalloc_maybe_bootmem(size, GFP_KERNEL);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000492 if (host == NULL)
493 return NULL;
494
495 /* Fill structure */
496 host->revmap_type = revmap_type;
497 host->inval_irq = inval_irq;
498 host->ops = ops;
Michael Ellerman19fc65b2008-05-26 12:12:32 +1000499 host->of_node = of_node_get(of_node);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000500
Michael Ellerman68158002007-08-28 18:47:55 +1000501 if (host->ops->match == NULL)
502 host->ops->match = default_irq_host_match;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000503
504 spin_lock_irqsave(&irq_big_lock, flags);
505
506 /* If it's a legacy controller, check for duplicates and
507 * mark it as allocated (we use irq 0 host pointer for that
508 */
509 if (revmap_type == IRQ_HOST_MAP_LEGACY) {
510 if (irq_map[0].host != NULL) {
511 spin_unlock_irqrestore(&irq_big_lock, flags);
512 /* If we are early boot, we can't free the structure,
513 * too bad...
514 * this will be fixed once slab is made available early
515 * instead of the current cruft
516 */
517 if (mem_init_done)
518 kfree(host);
519 return NULL;
520 }
521 irq_map[0].host = host;
522 }
523
524 list_add(&host->link, &irq_hosts);
525 spin_unlock_irqrestore(&irq_big_lock, flags);
526
527 /* Additional setups per revmap type */
528 switch(revmap_type) {
529 case IRQ_HOST_MAP_LEGACY:
530 /* 0 is always the invalid number for legacy */
531 host->inval_irq = 0;
532 /* setup us as the host for all legacy interrupts */
533 for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
Michael Ellerman78662912007-08-28 18:47:56 +1000534 irq_map[i].hwirq = i;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000535 smp_wmb();
536 irq_map[i].host = host;
537 smp_wmb();
538
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700539 /* Clear norequest flags */
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000540 irq_to_desc(i)->status &= ~IRQ_NOREQUEST;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000541
542 /* Legacy flags are left to default at this point,
543 * one can then use irq_create_mapping() to
Jean Delvarec03983a2007-10-19 23:22:55 +0200544 * explicitly change them
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000545 */
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700546 ops->map(host, i, i);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000547 }
548 break;
549 case IRQ_HOST_MAP_LINEAR:
550 rmap = (unsigned int *)(host + 1);
551 for (i = 0; i < revmap_arg; i++)
Michael Ellermanf5921692007-06-01 17:23:26 +1000552 rmap[i] = NO_IRQ;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000553 host->revmap_data.linear.size = revmap_arg;
554 smp_wmb();
555 host->revmap_data.linear.revmap = rmap;
556 break;
557 default:
558 break;
559 }
560
561 pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
562
563 return host;
564}
565
566struct irq_host *irq_find_host(struct device_node *node)
567{
568 struct irq_host *h, *found = NULL;
569 unsigned long flags;
570
571 /* We might want to match the legacy controller last since
572 * it might potentially be set to match all interrupts in
573 * the absence of a device node. This isn't a problem so far
574 * yet though...
575 */
576 spin_lock_irqsave(&irq_big_lock, flags);
577 list_for_each_entry(h, &irq_hosts, link)
Michael Ellerman68158002007-08-28 18:47:55 +1000578 if (h->ops->match(h, node)) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000579 found = h;
580 break;
581 }
582 spin_unlock_irqrestore(&irq_big_lock, flags);
583 return found;
584}
585EXPORT_SYMBOL_GPL(irq_find_host);
586
587void irq_set_default_host(struct irq_host *host)
588{
589 pr_debug("irq: Default host set to @0x%p\n", host);
590
591 irq_default_host = host;
592}
593
594void irq_set_virq_count(unsigned int count)
595{
596 pr_debug("irq: Trying to set virq count to %d\n", count);
597
598 BUG_ON(count < NUM_ISA_INTERRUPTS);
599 if (count < NR_IRQS)
600 irq_virq_count = count;
601}
602
Michael Ellerman6fde40f2007-06-04 22:59:59 +1000603static int irq_setup_virq(struct irq_host *host, unsigned int virq,
604 irq_hw_number_t hwirq)
605{
606 /* Clear IRQ_NOREQUEST flag */
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000607 irq_to_desc(virq)->status &= ~IRQ_NOREQUEST;
Michael Ellerman6fde40f2007-06-04 22:59:59 +1000608
609 /* map it */
610 smp_wmb();
611 irq_map[virq].hwirq = hwirq;
612 smp_mb();
613
614 if (host->ops->map(host, virq, hwirq)) {
615 pr_debug("irq: -> mapping failed, freeing\n");
616 irq_free_virt(virq, 1);
617 return -1;
618 }
619
620 return 0;
621}
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000622
Michael Ellermanee51de52007-06-04 23:00:00 +1000623unsigned int irq_create_direct_mapping(struct irq_host *host)
624{
625 unsigned int virq;
626
627 if (host == NULL)
628 host = irq_default_host;
629
630 BUG_ON(host == NULL);
631 WARN_ON(host->revmap_type != IRQ_HOST_MAP_NOMAP);
632
633 virq = irq_alloc_virt(host, 1, 0);
634 if (virq == NO_IRQ) {
635 pr_debug("irq: create_direct virq allocation failed\n");
636 return NO_IRQ;
637 }
638
639 pr_debug("irq: create_direct obtained virq %d\n", virq);
640
641 if (irq_setup_virq(host, virq, virq))
642 return NO_IRQ;
643
644 return virq;
645}
646
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000647unsigned int irq_create_mapping(struct irq_host *host,
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700648 irq_hw_number_t hwirq)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000649{
650 unsigned int virq, hint;
651
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700652 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000653
654 /* Look for default host if nececssary */
655 if (host == NULL)
656 host = irq_default_host;
657 if (host == NULL) {
658 printk(KERN_WARNING "irq_create_mapping called for"
659 " NULL host, hwirq=%lx\n", hwirq);
660 WARN_ON(1);
661 return NO_IRQ;
662 }
663 pr_debug("irq: -> using host @%p\n", host);
664
665 /* Check if mapping already exist, if it does, call
666 * host->ops->map() to update the flags
667 */
668 virq = irq_find_mapping(host, hwirq);
Michael Ellermanf5921692007-06-01 17:23:26 +1000669 if (virq != NO_IRQ) {
Ishizaki Kouacc900e2007-01-12 09:58:39 +0900670 if (host->ops->remap)
671 host->ops->remap(host, virq, hwirq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000672 pr_debug("irq: -> existing mapping on virq %d\n", virq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000673 return virq;
674 }
675
676 /* Get a virtual interrupt number */
677 if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
678 /* Handle legacy */
679 virq = (unsigned int)hwirq;
680 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
681 return NO_IRQ;
682 return virq;
683 } else {
684 /* Allocate a virtual interrupt number */
685 hint = hwirq % irq_virq_count;
686 virq = irq_alloc_virt(host, 1, hint);
687 if (virq == NO_IRQ) {
688 pr_debug("irq: -> virq allocation failed\n");
689 return NO_IRQ;
690 }
691 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000692
Michael Ellerman6fde40f2007-06-04 22:59:59 +1000693 if (irq_setup_virq(host, virq, hwirq))
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000694 return NO_IRQ;
Michael Ellerman6fde40f2007-06-04 22:59:59 +1000695
Michael Ellermanc7d07fd2009-04-05 16:05:02 +0000696 printk(KERN_DEBUG "irq: irq %lu on host %s mapped to virtual irq %u\n",
697 hwirq, host->of_node ? host->of_node->full_name : "null", virq);
698
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000699 return virq;
700}
701EXPORT_SYMBOL_GPL(irq_create_mapping);
702
Al Virof3d2ab42006-10-09 16:22:09 +0100703unsigned int irq_create_of_mapping(struct device_node *controller,
704 u32 *intspec, unsigned int intsize)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000705{
706 struct irq_host *host;
707 irq_hw_number_t hwirq;
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700708 unsigned int type = IRQ_TYPE_NONE;
709 unsigned int virq;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000710
711 if (controller == NULL)
712 host = irq_default_host;
713 else
714 host = irq_find_host(controller);
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700715 if (host == NULL) {
716 printk(KERN_WARNING "irq: no irq host found for %s !\n",
717 controller->full_name);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000718 return NO_IRQ;
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700719 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000720
721 /* If host has no translation, then we assume interrupt line */
722 if (host->ops->xlate == NULL)
723 hwirq = intspec[0];
724 else {
725 if (host->ops->xlate(host, controller, intspec, intsize,
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700726 &hwirq, &type))
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000727 return NO_IRQ;
728 }
729
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700730 /* Create mapping */
731 virq = irq_create_mapping(host, hwirq);
732 if (virq == NO_IRQ)
733 return virq;
734
735 /* Set type if specified and different than the current one */
736 if (type != IRQ_TYPE_NONE &&
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000737 type != (irq_to_desc(virq)->status & IRQF_TRIGGER_MASK))
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700738 set_irq_type(virq, type);
739 return virq;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000740}
741EXPORT_SYMBOL_GPL(irq_create_of_mapping);
742
743unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
744{
745 struct of_irq oirq;
746
747 if (of_irq_map_one(dev, index, &oirq))
748 return NO_IRQ;
749
750 return irq_create_of_mapping(oirq.controller, oirq.specifier,
751 oirq.size);
752}
753EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
754
755void irq_dispose_mapping(unsigned int virq)
756{
Michael Ellerman5414c6b2006-10-24 13:37:34 +1000757 struct irq_host *host;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000758 irq_hw_number_t hwirq;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000759
Michael Ellerman5414c6b2006-10-24 13:37:34 +1000760 if (virq == NO_IRQ)
761 return;
762
763 host = irq_map[virq].host;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000764 WARN_ON (host == NULL);
765 if (host == NULL)
766 return;
767
768 /* Never unmap legacy interrupts */
769 if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
770 return;
771
772 /* remove chip and handler */
773 set_irq_chip_and_handler(virq, NULL, NULL);
774
775 /* Make sure it's completed */
776 synchronize_irq(virq);
777
778 /* Tell the PIC about it */
779 if (host->ops->unmap)
780 host->ops->unmap(host, virq);
781 smp_mb();
782
783 /* Clear reverse map */
784 hwirq = irq_map[virq].hwirq;
785 switch(host->revmap_type) {
786 case IRQ_HOST_MAP_LINEAR:
787 if (hwirq < host->revmap_data.linear.size)
Michael Ellermanf5921692007-06-01 17:23:26 +1000788 host->revmap_data.linear.revmap[hwirq] = NO_IRQ;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000789 break;
790 case IRQ_HOST_MAP_TREE:
Sebastien Dugue967e0122008-09-04 22:37:07 +1000791 /*
792 * Check if radix tree allocated yet, if not then nothing to
793 * remove.
794 */
795 smp_rmb();
796 if (revmap_trees_allocated < 1)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000797 break;
Sebastien Dugue150c6c82008-09-04 22:37:08 +1000798 mutex_lock(&revmap_trees_mutex);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000799 radix_tree_delete(&host->revmap_data.tree, hwirq);
Sebastien Dugue150c6c82008-09-04 22:37:08 +1000800 mutex_unlock(&revmap_trees_mutex);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000801 break;
802 }
803
804 /* Destroy map */
805 smp_mb();
806 irq_map[virq].hwirq = host->inval_irq;
807
808 /* Set some flags */
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000809 irq_to_desc(virq)->status |= IRQ_NOREQUEST;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000810
811 /* Free it */
812 irq_free_virt(virq, 1);
813}
814EXPORT_SYMBOL_GPL(irq_dispose_mapping);
815
816unsigned int irq_find_mapping(struct irq_host *host,
817 irq_hw_number_t hwirq)
818{
819 unsigned int i;
820 unsigned int hint = hwirq % irq_virq_count;
821
822 /* Look for default host if nececssary */
823 if (host == NULL)
824 host = irq_default_host;
825 if (host == NULL)
826 return NO_IRQ;
827
828 /* legacy -> bail early */
829 if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
830 return hwirq;
831
832 /* Slow path does a linear search of the map */
833 if (hint < NUM_ISA_INTERRUPTS)
834 hint = NUM_ISA_INTERRUPTS;
835 i = hint;
836 do {
837 if (irq_map[i].host == host &&
838 irq_map[i].hwirq == hwirq)
839 return i;
840 i++;
841 if (i >= irq_virq_count)
842 i = NUM_ISA_INTERRUPTS;
843 } while(i != hint);
844 return NO_IRQ;
845}
846EXPORT_SYMBOL_GPL(irq_find_mapping);
847
848
Sebastien Dugue967e0122008-09-04 22:37:07 +1000849unsigned int irq_radix_revmap_lookup(struct irq_host *host,
850 irq_hw_number_t hwirq)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000851{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000852 struct irq_map_entry *ptr;
853 unsigned int virq;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000854
855 WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
856
Sebastien Dugue967e0122008-09-04 22:37:07 +1000857 /*
858 * Check if the radix tree exists and has bee initialized.
859 * If not, we fallback to slow mode
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000860 */
Sebastien Dugue967e0122008-09-04 22:37:07 +1000861 if (revmap_trees_allocated < 2)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000862 return irq_find_mapping(host, hwirq);
863
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000864 /* Now try to resolve */
Sebastien Dugue150c6c82008-09-04 22:37:08 +1000865 /*
866 * No rcu_read_lock(ing) needed, the ptr returned can't go under us
867 * as it's referencing an entry in the static irq_map table.
868 */
Sebastien Dugue967e0122008-09-04 22:37:07 +1000869 ptr = radix_tree_lookup(&host->revmap_data.tree, hwirq);
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000870
Sebastien Dugue967e0122008-09-04 22:37:07 +1000871 /*
872 * If found in radix tree, then fine.
873 * Else fallback to linear lookup - this should not happen in practice
874 * as it means that we failed to insert the node in the radix tree.
875 */
876 if (ptr)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000877 virq = ptr - irq_map;
Sebastien Dugue967e0122008-09-04 22:37:07 +1000878 else
879 virq = irq_find_mapping(host, hwirq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000880
Sebastien Dugue967e0122008-09-04 22:37:07 +1000881 return virq;
882}
883
884void irq_radix_revmap_insert(struct irq_host *host, unsigned int virq,
885 irq_hw_number_t hwirq)
886{
Sebastien Dugue967e0122008-09-04 22:37:07 +1000887
888 WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
889
890 /*
891 * Check if the radix tree exists yet.
892 * If not, then the irq will be inserted into the tree when it gets
893 * initialized.
894 */
895 smp_rmb();
896 if (revmap_trees_allocated < 1)
897 return;
898
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000899 if (virq != NO_IRQ) {
Sebastien Dugue150c6c82008-09-04 22:37:08 +1000900 mutex_lock(&revmap_trees_mutex);
Sebastien Dugue967e0122008-09-04 22:37:07 +1000901 radix_tree_insert(&host->revmap_data.tree, hwirq,
902 &irq_map[virq]);
Sebastien Dugue150c6c82008-09-04 22:37:08 +1000903 mutex_unlock(&revmap_trees_mutex);
Benjamin Herrenschmidt8ec8f2e2006-08-28 11:17:37 +1000904 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000905}
906
907unsigned int irq_linear_revmap(struct irq_host *host,
908 irq_hw_number_t hwirq)
909{
910 unsigned int *revmap;
911
912 WARN_ON(host->revmap_type != IRQ_HOST_MAP_LINEAR);
913
914 /* Check revmap bounds */
915 if (unlikely(hwirq >= host->revmap_data.linear.size))
916 return irq_find_mapping(host, hwirq);
917
918 /* Check if revmap was allocated */
919 revmap = host->revmap_data.linear.revmap;
920 if (unlikely(revmap == NULL))
921 return irq_find_mapping(host, hwirq);
922
923 /* Fill up revmap with slow path if no mapping found */
924 if (unlikely(revmap[hwirq] == NO_IRQ))
925 revmap[hwirq] = irq_find_mapping(host, hwirq);
926
927 return revmap[hwirq];
928}
929
930unsigned int irq_alloc_virt(struct irq_host *host,
931 unsigned int count,
932 unsigned int hint)
933{
934 unsigned long flags;
935 unsigned int i, j, found = NO_IRQ;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000936
937 if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
938 return NO_IRQ;
939
940 spin_lock_irqsave(&irq_big_lock, flags);
941
942 /* Use hint for 1 interrupt if any */
943 if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
944 hint < irq_virq_count && irq_map[hint].host == NULL) {
945 found = hint;
946 goto hint_found;
947 }
948
949 /* Look for count consecutive numbers in the allocatable
950 * (non-legacy) space
951 */
Michael Ellermane1251462006-08-02 10:48:50 +1000952 for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
953 if (irq_map[i].host != NULL)
954 j = 0;
955 else
956 j++;
957
958 if (j == count) {
959 found = i - count + 1;
960 break;
961 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000962 }
963 if (found == NO_IRQ) {
964 spin_unlock_irqrestore(&irq_big_lock, flags);
965 return NO_IRQ;
966 }
967 hint_found:
968 for (i = found; i < (found + count); i++) {
969 irq_map[i].hwirq = host->inval_irq;
970 smp_wmb();
971 irq_map[i].host = host;
972 }
973 spin_unlock_irqrestore(&irq_big_lock, flags);
974 return found;
975}
976
977void irq_free_virt(unsigned int virq, unsigned int count)
978{
979 unsigned long flags;
980 unsigned int i;
981
982 WARN_ON (virq < NUM_ISA_INTERRUPTS);
983 WARN_ON (count == 0 || (virq + count) > irq_virq_count);
984
985 spin_lock_irqsave(&irq_big_lock, flags);
986 for (i = virq; i < (virq + count); i++) {
987 struct irq_host *host;
988
989 if (i < NUM_ISA_INTERRUPTS ||
990 (virq + count) > irq_virq_count)
991 continue;
992
993 host = irq_map[i].host;
994 irq_map[i].hwirq = host->inval_irq;
995 smp_wmb();
996 irq_map[i].host = NULL;
997 }
998 spin_unlock_irqrestore(&irq_big_lock, flags);
999}
1000
1001void irq_early_init(void)
1002{
1003 unsigned int i;
1004
1005 for (i = 0; i < NR_IRQS; i++)
Michael Ellerman6cff46f2009-10-13 19:44:51 +00001006 irq_to_desc(i)->status |= IRQ_NOREQUEST;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001007}
1008
1009/* We need to create the radix trees late */
1010static int irq_late_init(void)
1011{
1012 struct irq_host *h;
Sebastien Dugue967e0122008-09-04 22:37:07 +10001013 unsigned int i;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001014
Sebastien Dugue967e0122008-09-04 22:37:07 +10001015 /*
1016 * No mutual exclusion with respect to accessors of the tree is needed
1017 * here as the synchronization is done via the state variable
1018 * revmap_trees_allocated.
1019 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001020 list_for_each_entry(h, &irq_hosts, link) {
1021 if (h->revmap_type == IRQ_HOST_MAP_TREE)
Sebastien Dugue967e0122008-09-04 22:37:07 +10001022 INIT_RADIX_TREE(&h->revmap_data.tree, GFP_KERNEL);
1023 }
1024
1025 /*
1026 * Make sure the radix trees inits are visible before setting
1027 * the flag
1028 */
1029 smp_wmb();
1030 revmap_trees_allocated = 1;
1031
1032 /*
1033 * Insert the reverse mapping for those interrupts already present
1034 * in irq_map[].
1035 */
Sebastien Dugue150c6c82008-09-04 22:37:08 +10001036 mutex_lock(&revmap_trees_mutex);
Sebastien Dugue967e0122008-09-04 22:37:07 +10001037 for (i = 0; i < irq_virq_count; i++) {
1038 if (irq_map[i].host &&
1039 (irq_map[i].host->revmap_type == IRQ_HOST_MAP_TREE))
1040 radix_tree_insert(&irq_map[i].host->revmap_data.tree,
1041 irq_map[i].hwirq, &irq_map[i]);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001042 }
Sebastien Dugue150c6c82008-09-04 22:37:08 +10001043 mutex_unlock(&revmap_trees_mutex);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001044
Sebastien Dugue967e0122008-09-04 22:37:07 +10001045 /*
1046 * Make sure the radix trees insertions are visible before setting
1047 * the flag
1048 */
1049 smp_wmb();
1050 revmap_trees_allocated = 2;
1051
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001052 return 0;
1053}
1054arch_initcall(irq_late_init);
1055
Michael Ellerman60b332e2007-08-28 18:47:57 +10001056#ifdef CONFIG_VIRQ_DEBUG
1057static int virq_debug_show(struct seq_file *m, void *private)
1058{
1059 unsigned long flags;
Thomas Gleixner97f7d6b2009-03-10 14:45:54 +00001060 struct irq_desc *desc;
Michael Ellerman60b332e2007-08-28 18:47:57 +10001061 const char *p;
1062 char none[] = "none";
1063 int i;
1064
1065 seq_printf(m, "%-5s %-7s %-15s %s\n", "virq", "hwirq",
1066 "chip name", "host name");
1067
Michael Ellerman76f1d942009-10-13 19:44:56 +00001068 for (i = 1; i < nr_irqs; i++) {
Michael Ellerman6cff46f2009-10-13 19:44:51 +00001069 desc = irq_to_desc(i);
Michael Ellerman76f1d942009-10-13 19:44:56 +00001070 if (!desc)
1071 continue;
1072
Michael Ellerman60b332e2007-08-28 18:47:57 +10001073 spin_lock_irqsave(&desc->lock, flags);
1074
1075 if (desc->action && desc->action->handler) {
1076 seq_printf(m, "%5d ", i);
1077 seq_printf(m, "0x%05lx ", virq_to_hw(i));
1078
1079 if (desc->chip && desc->chip->typename)
1080 p = desc->chip->typename;
1081 else
1082 p = none;
1083 seq_printf(m, "%-15s ", p);
1084
1085 if (irq_map[i].host && irq_map[i].host->of_node)
1086 p = irq_map[i].host->of_node->full_name;
1087 else
1088 p = none;
1089 seq_printf(m, "%s\n", p);
1090 }
1091
1092 spin_unlock_irqrestore(&desc->lock, flags);
1093 }
1094
1095 return 0;
1096}
1097
1098static int virq_debug_open(struct inode *inode, struct file *file)
1099{
1100 return single_open(file, virq_debug_show, inode->i_private);
1101}
1102
1103static const struct file_operations virq_debug_fops = {
1104 .open = virq_debug_open,
1105 .read = seq_read,
1106 .llseek = seq_lseek,
1107 .release = single_release,
1108};
1109
1110static int __init irq_debugfs_init(void)
1111{
1112 if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
Emil Medve476ff8a2008-05-23 05:49:22 +10001113 NULL, &virq_debug_fops) == NULL)
Michael Ellerman60b332e2007-08-28 18:47:57 +10001114 return -ENOMEM;
1115
1116 return 0;
1117}
1118__initcall(irq_debugfs_init);
1119#endif /* CONFIG_VIRQ_DEBUG */
1120
Paul Mackerrasc6622f62006-02-24 10:06:59 +11001121#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122static int __init setup_noirqdistrib(char *str)
1123{
1124 distribute_irqs = 0;
1125 return 1;
1126}
1127
1128__setup("noirqdistrib", setup_noirqdistrib);
Stephen Rothwell756e7102005-11-09 18:07:45 +11001129#endif /* CONFIG_PPC64 */