irq.c 12 KB

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