irq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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);
  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. irq_enter();
  249. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  250. /* Debugging check for stack overflow: is there less than 2KB free? */
  251. {
  252. long sp;
  253. sp = __get_SP() & (THREAD_SIZE-1);
  254. if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
  255. printk("do_IRQ: stack overflow: %ld\n",
  256. sp - sizeof(struct thread_info));
  257. dump_stack();
  258. }
  259. }
  260. #endif
  261. lpaca = get_paca();
  262. #ifdef CONFIG_SMP
  263. if (lpaca->lppaca.int_dword.fields.ipi_cnt) {
  264. lpaca->lppaca.int_dword.fields.ipi_cnt = 0;
  265. iSeries_smp_message_recv(regs);
  266. }
  267. #endif /* CONFIG_SMP */
  268. if (hvlpevent_is_pending())
  269. lpevent_count += process_hvlpevents(regs);
  270. irq_exit();
  271. if (lpaca->lppaca.int_dword.fields.decr_int) {
  272. lpaca->lppaca.int_dword.fields.decr_int = 0;
  273. /* Signal a fake decrementer interrupt */
  274. timer_interrupt(regs);
  275. }
  276. }
  277. #else /* CONFIG_PPC_ISERIES */
  278. void do_IRQ(struct pt_regs *regs)
  279. {
  280. int irq;
  281. irq_enter();
  282. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  283. /* Debugging check for stack overflow: is there less than 2KB free? */
  284. {
  285. long sp;
  286. sp = __get_SP() & (THREAD_SIZE-1);
  287. if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
  288. printk("do_IRQ: stack overflow: %ld\n",
  289. sp - sizeof(struct thread_info));
  290. dump_stack();
  291. }
  292. }
  293. #endif
  294. irq = ppc_md.get_irq(regs);
  295. if (irq >= 0)
  296. ppc_irq_dispatch_handler(regs, irq);
  297. else
  298. /* That's not SMP safe ... but who cares ? */
  299. ppc_spurious_interrupts++;
  300. irq_exit();
  301. }
  302. #endif /* CONFIG_PPC_ISERIES */
  303. void __init init_IRQ(void)
  304. {
  305. static int once = 0;
  306. if (once)
  307. return;
  308. once++;
  309. ppc_md.init_IRQ();
  310. irq_ctx_init();
  311. }
  312. #ifndef CONFIG_PPC_ISERIES
  313. /*
  314. * Virtual IRQ mapping code, used on systems with XICS interrupt controllers.
  315. */
  316. #define UNDEFINED_IRQ 0xffffffff
  317. unsigned int virt_irq_to_real_map[NR_IRQS];
  318. /*
  319. * Don't use virtual irqs 0, 1, 2 for devices.
  320. * The pcnet32 driver considers interrupt numbers < 2 to be invalid,
  321. * and 2 is the XICS IPI interrupt.
  322. * We limit virtual irqs to 17 less than NR_IRQS so that when we
  323. * offset them by 16 (to reserve the first 16 for ISA interrupts)
  324. * we don't end up with an interrupt number >= NR_IRQS.
  325. */
  326. #define MIN_VIRT_IRQ 3
  327. #define MAX_VIRT_IRQ (NR_IRQS - NUM_ISA_INTERRUPTS - 1)
  328. #define NR_VIRT_IRQS (MAX_VIRT_IRQ - MIN_VIRT_IRQ + 1)
  329. void
  330. virt_irq_init(void)
  331. {
  332. int i;
  333. for (i = 0; i < NR_IRQS; i++)
  334. virt_irq_to_real_map[i] = UNDEFINED_IRQ;
  335. }
  336. /* Create a mapping for a real_irq if it doesn't already exist.
  337. * Return the virtual irq as a convenience.
  338. */
  339. int virt_irq_create_mapping(unsigned int real_irq)
  340. {
  341. unsigned int virq, first_virq;
  342. static int warned;
  343. if (ppc64_interrupt_controller == IC_OPEN_PIC)
  344. return real_irq; /* no mapping for openpic (for now) */
  345. if (ppc64_interrupt_controller == IC_BPA_IIC)
  346. return real_irq; /* no mapping for iic either */
  347. /* don't map interrupts < MIN_VIRT_IRQ */
  348. if (real_irq < MIN_VIRT_IRQ) {
  349. virt_irq_to_real_map[real_irq] = real_irq;
  350. return real_irq;
  351. }
  352. /* map to a number between MIN_VIRT_IRQ and MAX_VIRT_IRQ */
  353. virq = real_irq;
  354. if (virq > MAX_VIRT_IRQ)
  355. virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;
  356. /* search for this number or a free slot */
  357. first_virq = virq;
  358. while (virt_irq_to_real_map[virq] != UNDEFINED_IRQ) {
  359. if (virt_irq_to_real_map[virq] == real_irq)
  360. return virq;
  361. if (++virq > MAX_VIRT_IRQ)
  362. virq = MIN_VIRT_IRQ;
  363. if (virq == first_virq)
  364. goto nospace; /* oops, no free slots */
  365. }
  366. virt_irq_to_real_map[virq] = real_irq;
  367. return virq;
  368. nospace:
  369. if (!warned) {
  370. printk(KERN_CRIT "Interrupt table is full\n");
  371. printk(KERN_CRIT "Increase NR_IRQS (currently %d) "
  372. "in your kernel sources and rebuild.\n", NR_IRQS);
  373. warned = 1;
  374. }
  375. return NO_IRQ;
  376. }
  377. /*
  378. * In most cases will get a hit on the very first slot checked in the
  379. * virt_irq_to_real_map. Only when there are a large number of
  380. * IRQs will this be expensive.
  381. */
  382. unsigned int real_irq_to_virt_slowpath(unsigned int real_irq)
  383. {
  384. unsigned int virq;
  385. unsigned int first_virq;
  386. virq = real_irq;
  387. if (virq > MAX_VIRT_IRQ)
  388. virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;
  389. first_virq = virq;
  390. do {
  391. if (virt_irq_to_real_map[virq] == real_irq)
  392. return virq;
  393. virq++;
  394. if (virq >= MAX_VIRT_IRQ)
  395. virq = 0;
  396. } while (first_virq != virq);
  397. return NO_IRQ;
  398. }
  399. #endif /* CONFIG_PPC_ISERIES */
  400. #ifdef CONFIG_IRQSTACKS
  401. struct thread_info *softirq_ctx[NR_CPUS];
  402. struct thread_info *hardirq_ctx[NR_CPUS];
  403. void irq_ctx_init(void)
  404. {
  405. struct thread_info *tp;
  406. int i;
  407. for_each_cpu(i) {
  408. memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
  409. tp = softirq_ctx[i];
  410. tp->cpu = i;
  411. tp->preempt_count = SOFTIRQ_OFFSET;
  412. memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
  413. tp = hardirq_ctx[i];
  414. tp->cpu = i;
  415. tp->preempt_count = HARDIRQ_OFFSET;
  416. }
  417. }
  418. void do_softirq(void)
  419. {
  420. unsigned long flags;
  421. struct thread_info *curtp, *irqtp;
  422. if (in_interrupt())
  423. return;
  424. local_irq_save(flags);
  425. if (local_softirq_pending()) {
  426. curtp = current_thread_info();
  427. irqtp = softirq_ctx[smp_processor_id()];
  428. irqtp->task = curtp->task;
  429. call_do_softirq(irqtp);
  430. irqtp->task = NULL;
  431. }
  432. local_irq_restore(flags);
  433. }
  434. EXPORT_SYMBOL(do_softirq);
  435. #endif /* CONFIG_IRQSTACKS */
  436. static int __init setup_noirqdistrib(char *str)
  437. {
  438. distribute_irqs = 0;
  439. return 1;
  440. }
  441. __setup("noirqdistrib", setup_noirqdistrib);