irq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * arch/ppc/kernel/irq.c
  3. *
  4. * Derived from arch/i386/kernel/irq.c
  5. * Copyright (C) 1992 Linus Torvalds
  6. * Adapted from arch/i386 by Gary Thomas
  7. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  8. * Updated and modified by Cort Dougan (cort@cs.nmt.edu)
  9. * Copyright (C) 1996 Cort Dougan
  10. * Adapted for Power Macintosh by Paul Mackerras
  11. * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
  12. * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. * This file contains the code used by various IRQ handling routines:
  20. * asking for different IRQ's should be done through these routines
  21. * instead of just grabbing them. Thus setups with different IRQ numbers
  22. * shouldn't result in any weird surprises, and installing new handlers
  23. * should be easier.
  24. */
  25. #include <linux/errno.h>
  26. #include <linux/module.h>
  27. #include <linux/threads.h>
  28. #include <linux/kernel_stat.h>
  29. #include <linux/signal.h>
  30. #include <linux/sched.h>
  31. #include <linux/ioport.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/timex.h>
  34. #include <linux/config.h>
  35. #include <linux/init.h>
  36. #include <linux/slab.h>
  37. #include <linux/pci.h>
  38. #include <linux/delay.h>
  39. #include <linux/irq.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/random.h>
  42. #include <linux/kallsyms.h>
  43. #include <linux/profile.h>
  44. #include <linux/bitops.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/system.h>
  47. #include <asm/io.h>
  48. #include <asm/pgtable.h>
  49. #include <asm/irq.h>
  50. #include <asm/cache.h>
  51. #include <asm/prom.h>
  52. #include <asm/ptrace.h>
  53. #include <asm/iSeries/ItLpQueue.h>
  54. #include <asm/machdep.h>
  55. #include <asm/paca.h>
  56. #ifdef CONFIG_SMP
  57. extern void iSeries_smp_message_recv( struct pt_regs * );
  58. #endif
  59. extern irq_desc_t irq_desc[NR_IRQS];
  60. EXPORT_SYMBOL(irq_desc);
  61. int distribute_irqs = 1;
  62. int __irq_offset_value;
  63. int ppc_spurious_interrupts;
  64. u64 ppc64_interrupt_controller;
  65. int show_interrupts(struct seq_file *p, void *v)
  66. {
  67. int i = *(loff_t *) v, j;
  68. struct irqaction * action;
  69. irq_desc_t *desc;
  70. unsigned long flags;
  71. if (i == 0) {
  72. seq_printf(p, " ");
  73. for (j=0; j<NR_CPUS; j++) {
  74. if (cpu_online(j))
  75. seq_printf(p, "CPU%d ",j);
  76. }
  77. seq_putc(p, '\n');
  78. }
  79. if (i < NR_IRQS) {
  80. desc = get_irq_desc(i);
  81. spin_lock_irqsave(&desc->lock, flags);
  82. action = desc->action;
  83. if (!action || !action->handler)
  84. goto skip;
  85. seq_printf(p, "%3d: ", i);
  86. #ifdef CONFIG_SMP
  87. for (j = 0; j < NR_CPUS; j++) {
  88. if (cpu_online(j))
  89. seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
  90. }
  91. #else
  92. seq_printf(p, "%10u ", kstat_irqs(i));
  93. #endif /* CONFIG_SMP */
  94. if (desc->handler)
  95. seq_printf(p, " %s ", desc->handler->typename );
  96. else
  97. seq_printf(p, " None ");
  98. seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge ");
  99. seq_printf(p, " %s",action->name);
  100. for (action=action->next; action; action = action->next)
  101. seq_printf(p, ", %s", action->name);
  102. seq_putc(p, '\n');
  103. skip:
  104. spin_unlock_irqrestore(&desc->lock, flags);
  105. } else if (i == NR_IRQS)
  106. seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
  107. return 0;
  108. }
  109. #ifdef CONFIG_HOTPLUG_CPU
  110. void fixup_irqs(cpumask_t map)
  111. {
  112. unsigned int irq;
  113. static int warned;
  114. for_each_irq(irq) {
  115. cpumask_t mask;
  116. if (irq_desc[irq].status & IRQ_PER_CPU)
  117. continue;
  118. cpus_and(mask, irq_affinity[irq], map);
  119. if (any_online_cpu(mask) == NR_CPUS) {
  120. printk("Breaking affinity for irq %i\n", irq);
  121. mask = map;
  122. }
  123. if (irq_desc[irq].handler->set_affinity)
  124. irq_desc[irq].handler->set_affinity(irq, mask);
  125. else if (irq_desc[irq].action && !(warned++))
  126. printk("Cannot set affinity for irq %i\n", irq);
  127. }
  128. local_irq_enable();
  129. mdelay(1);
  130. local_irq_disable();
  131. }
  132. #endif
  133. extern int noirqdebug;
  134. /*
  135. * Eventually, this should take an array of interrupts and an array size
  136. * so it can dispatch multiple interrupts.
  137. */
  138. void ppc_irq_dispatch_handler(struct pt_regs *regs, int irq)
  139. {
  140. int status;
  141. struct irqaction *action;
  142. int cpu = smp_processor_id();
  143. irq_desc_t *desc = get_irq_desc(irq);
  144. irqreturn_t action_ret;
  145. #ifdef CONFIG_IRQSTACKS
  146. struct thread_info *curtp, *irqtp;
  147. #endif
  148. kstat_cpu(cpu).irqs[irq]++;
  149. if (desc->status & IRQ_PER_CPU) {
  150. /* no locking required for CPU-local interrupts: */
  151. ack_irq(irq);
  152. action_ret = handle_IRQ_event(irq, regs, desc->action);
  153. desc->handler->end(irq);
  154. return;
  155. }
  156. spin_lock(&desc->lock);
  157. ack_irq(irq);
  158. /*
  159. REPLAY is when Linux resends an IRQ that was dropped earlier
  160. WAITING is used by probe to mark irqs that are being tested
  161. */
  162. status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
  163. status |= IRQ_PENDING; /* we _want_ to handle it */
  164. /*
  165. * If the IRQ is disabled for whatever reason, we cannot
  166. * use the action we have.
  167. */
  168. action = NULL;
  169. if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
  170. action = desc->action;
  171. if (!action || !action->handler) {
  172. ppc_spurious_interrupts++;
  173. printk(KERN_DEBUG "Unhandled interrupt %x, disabled\n", irq);
  174. /* We can't call disable_irq here, it would deadlock */
  175. if (!desc->depth)
  176. desc->depth = 1;
  177. desc->status |= IRQ_DISABLED;
  178. /* This is not a real spurrious interrupt, we
  179. * have to eoi it, so we jump to out
  180. */
  181. mask_irq(irq);
  182. goto out;
  183. }
  184. status &= ~IRQ_PENDING; /* we commit to handling */
  185. status |= IRQ_INPROGRESS; /* we are handling it */
  186. }
  187. desc->status = status;
  188. /*
  189. * If there is no IRQ handler or it was disabled, exit early.
  190. Since we set PENDING, if another processor is handling
  191. a different instance of this same irq, the other processor
  192. will take care of it.
  193. */
  194. if (unlikely(!action))
  195. goto out;
  196. /*
  197. * Edge triggered interrupts need to remember
  198. * pending events.
  199. * This applies to any hw interrupts that allow a second
  200. * instance of the same irq to arrive while we are in do_IRQ
  201. * or in the handler. But the code here only handles the _second_
  202. * instance of the irq, not the third or fourth. So it is mostly
  203. * useful for irq hardware that does not mask cleanly in an
  204. * SMP environment.
  205. */
  206. for (;;) {
  207. spin_unlock(&desc->lock);
  208. #ifdef CONFIG_IRQSTACKS
  209. /* Switch to the irq stack to handle this */
  210. curtp = current_thread_info();
  211. irqtp = hardirq_ctx[smp_processor_id()];
  212. if (curtp != irqtp) {
  213. irqtp->task = curtp->task;
  214. irqtp->flags = 0;
  215. action_ret = call_handle_IRQ_event(irq, regs, action, irqtp);
  216. irqtp->task = NULL;
  217. if (irqtp->flags)
  218. set_bits(irqtp->flags, &curtp->flags);
  219. } else
  220. #endif
  221. action_ret = handle_IRQ_event(irq, regs, action);
  222. spin_lock(&desc->lock);
  223. if (!noirqdebug)
  224. note_interrupt(irq, desc, action_ret, regs);
  225. if (likely(!(desc->status & IRQ_PENDING)))
  226. break;
  227. desc->status &= ~IRQ_PENDING;
  228. }
  229. out:
  230. desc->status &= ~IRQ_INPROGRESS;
  231. /*
  232. * The ->end() handler has to deal with interrupts which got
  233. * disabled while the handler was running.
  234. */
  235. if (desc->handler) {
  236. if (desc->handler->end)
  237. desc->handler->end(irq);
  238. else if (desc->handler->enable)
  239. desc->handler->enable(irq);
  240. }
  241. spin_unlock(&desc->lock);
  242. }
  243. #ifdef CONFIG_PPC_ISERIES
  244. void do_IRQ(struct pt_regs *regs)
  245. {
  246. struct paca_struct *lpaca;
  247. irq_enter();
  248. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  249. /* Debugging check for stack overflow: is there less than 2KB free? */
  250. {
  251. long sp;
  252. sp = __get_SP() & (THREAD_SIZE-1);
  253. if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
  254. printk("do_IRQ: stack overflow: %ld\n",
  255. sp - sizeof(struct thread_info));
  256. dump_stack();
  257. }
  258. }
  259. #endif
  260. lpaca = get_paca();
  261. #ifdef CONFIG_SMP
  262. if (lpaca->lppaca.int_dword.fields.ipi_cnt) {
  263. lpaca->lppaca.int_dword.fields.ipi_cnt = 0;
  264. iSeries_smp_message_recv(regs);
  265. }
  266. #endif /* CONFIG_SMP */
  267. if (hvlpevent_is_pending())
  268. process_hvlpevents(regs);
  269. irq_exit();
  270. if (lpaca->lppaca.int_dword.fields.decr_int) {
  271. lpaca->lppaca.int_dword.fields.decr_int = 0;
  272. /* Signal a fake decrementer interrupt */
  273. timer_interrupt(regs);
  274. }
  275. }
  276. #else /* CONFIG_PPC_ISERIES */
  277. void do_IRQ(struct pt_regs *regs)
  278. {
  279. int irq;
  280. irq_enter();
  281. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  282. /* Debugging check for stack overflow: is there less than 2KB free? */
  283. {
  284. long sp;
  285. sp = __get_SP() & (THREAD_SIZE-1);
  286. if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
  287. printk("do_IRQ: stack overflow: %ld\n",
  288. sp - sizeof(struct thread_info));
  289. dump_stack();
  290. }
  291. }
  292. #endif
  293. irq = ppc_md.get_irq(regs);
  294. if (irq >= 0)
  295. ppc_irq_dispatch_handler(regs, irq);
  296. else
  297. /* That's not SMP safe ... but who cares ? */
  298. ppc_spurious_interrupts++;
  299. irq_exit();
  300. }
  301. #endif /* CONFIG_PPC_ISERIES */
  302. void __init init_IRQ(void)
  303. {
  304. static int once = 0;
  305. if (once)
  306. return;
  307. once++;
  308. ppc_md.init_IRQ();
  309. irq_ctx_init();
  310. }
  311. #ifndef CONFIG_PPC_ISERIES
  312. /*
  313. * Virtual IRQ mapping code, used on systems with XICS interrupt controllers.
  314. */
  315. #define UNDEFINED_IRQ 0xffffffff
  316. unsigned int virt_irq_to_real_map[NR_IRQS];
  317. /*
  318. * Don't use virtual irqs 0, 1, 2 for devices.
  319. * The pcnet32 driver considers interrupt numbers < 2 to be invalid,
  320. * and 2 is the XICS IPI interrupt.
  321. * We limit virtual irqs to 17 less than NR_IRQS so that when we
  322. * offset them by 16 (to reserve the first 16 for ISA interrupts)
  323. * we don't end up with an interrupt number >= NR_IRQS.
  324. */
  325. #define MIN_VIRT_IRQ 3
  326. #define MAX_VIRT_IRQ (NR_IRQS - NUM_ISA_INTERRUPTS - 1)
  327. #define NR_VIRT_IRQS (MAX_VIRT_IRQ - MIN_VIRT_IRQ + 1)
  328. void
  329. virt_irq_init(void)
  330. {
  331. int i;
  332. for (i = 0; i < NR_IRQS; i++)
  333. virt_irq_to_real_map[i] = UNDEFINED_IRQ;
  334. }
  335. /* Create a mapping for a real_irq if it doesn't already exist.
  336. * Return the virtual irq as a convenience.
  337. */
  338. int virt_irq_create_mapping(unsigned int real_irq)
  339. {
  340. unsigned int virq, first_virq;
  341. static int warned;
  342. if (ppc64_interrupt_controller == IC_OPEN_PIC)
  343. return real_irq; /* no mapping for openpic (for now) */
  344. if (ppc64_interrupt_controller == IC_BPA_IIC)
  345. return real_irq; /* no mapping for iic either */
  346. /* don't map interrupts < MIN_VIRT_IRQ */
  347. if (real_irq < MIN_VIRT_IRQ) {
  348. virt_irq_to_real_map[real_irq] = real_irq;
  349. return real_irq;
  350. }
  351. /* map to a number between MIN_VIRT_IRQ and MAX_VIRT_IRQ */
  352. virq = real_irq;
  353. if (virq > MAX_VIRT_IRQ)
  354. virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;
  355. /* search for this number or a free slot */
  356. first_virq = virq;
  357. while (virt_irq_to_real_map[virq] != UNDEFINED_IRQ) {
  358. if (virt_irq_to_real_map[virq] == real_irq)
  359. return virq;
  360. if (++virq > MAX_VIRT_IRQ)
  361. virq = MIN_VIRT_IRQ;
  362. if (virq == first_virq)
  363. goto nospace; /* oops, no free slots */
  364. }
  365. virt_irq_to_real_map[virq] = real_irq;
  366. return virq;
  367. nospace:
  368. if (!warned) {
  369. printk(KERN_CRIT "Interrupt table is full\n");
  370. printk(KERN_CRIT "Increase NR_IRQS (currently %d) "
  371. "in your kernel sources and rebuild.\n", NR_IRQS);
  372. warned = 1;
  373. }
  374. return NO_IRQ;
  375. }
  376. /*
  377. * In most cases will get a hit on the very first slot checked in the
  378. * virt_irq_to_real_map. Only when there are a large number of
  379. * IRQs will this be expensive.
  380. */
  381. unsigned int real_irq_to_virt_slowpath(unsigned int real_irq)
  382. {
  383. unsigned int virq;
  384. unsigned int first_virq;
  385. virq = real_irq;
  386. if (virq > MAX_VIRT_IRQ)
  387. virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;
  388. first_virq = virq;
  389. do {
  390. if (virt_irq_to_real_map[virq] == real_irq)
  391. return virq;
  392. virq++;
  393. if (virq >= MAX_VIRT_IRQ)
  394. virq = 0;
  395. } while (first_virq != virq);
  396. return NO_IRQ;
  397. }
  398. #endif /* CONFIG_PPC_ISERIES */
  399. #ifdef CONFIG_IRQSTACKS
  400. struct thread_info *softirq_ctx[NR_CPUS];
  401. struct thread_info *hardirq_ctx[NR_CPUS];
  402. void irq_ctx_init(void)
  403. {
  404. struct thread_info *tp;
  405. int i;
  406. for_each_cpu(i) {
  407. memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
  408. tp = softirq_ctx[i];
  409. tp->cpu = i;
  410. tp->preempt_count = SOFTIRQ_OFFSET;
  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. void do_softirq(void)
  418. {
  419. unsigned long flags;
  420. struct thread_info *curtp, *irqtp;
  421. if (in_interrupt())
  422. return;
  423. local_irq_save(flags);
  424. if (local_softirq_pending()) {
  425. curtp = current_thread_info();
  426. irqtp = softirq_ctx[smp_processor_id()];
  427. irqtp->task = curtp->task;
  428. call_do_softirq(irqtp);
  429. irqtp->task = NULL;
  430. }
  431. local_irq_restore(flags);
  432. }
  433. EXPORT_SYMBOL(do_softirq);
  434. #endif /* CONFIG_IRQSTACKS */
  435. static int __init setup_noirqdistrib(char *str)
  436. {
  437. distribute_irqs = 0;
  438. return 1;
  439. }
  440. __setup("noirqdistrib", setup_noirqdistrib);