irq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Code to handle x86 style IRQs plus some generic interrupt stuff.
  3. *
  4. * Copyright (C) 1992 Linus Torvalds
  5. * Copyright (C) 1994, 1995, 1996, 1997, 1998 Ralf Baechle
  6. * Copyright (C) 1999 SuSE GmbH (Philipp Rumpf, prumpf@tux.org)
  7. * Copyright (C) 1999-2000 Grant Grundler
  8. * Copyright (c) 2005 Matthew Wilcox
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2, or (at your option)
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/bitops.h>
  25. #include <linux/config.h>
  26. #include <linux/errno.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kernel_stat.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/types.h>
  33. #include <asm/io.h>
  34. #include <asm/smp.h>
  35. #undef PARISC_IRQ_CR16_COUNTS
  36. extern irqreturn_t timer_interrupt(int, void *, struct pt_regs *);
  37. extern irqreturn_t ipi_interrupt(int, void *, struct pt_regs *);
  38. #define EIEM_MASK(irq) (1UL<<(CPU_IRQ_MAX - irq))
  39. /* Bits in EIEM correlate with cpu_irq_action[].
  40. ** Numbered *Big Endian*! (ie bit 0 is MSB)
  41. */
  42. static volatile unsigned long cpu_eiem = 0;
  43. static void cpu_disable_irq(unsigned int irq)
  44. {
  45. unsigned long eirr_bit = EIEM_MASK(irq);
  46. cpu_eiem &= ~eirr_bit;
  47. /* Do nothing on the other CPUs. If they get this interrupt,
  48. * The & cpu_eiem in the do_cpu_irq_mask() ensures they won't
  49. * handle it, and the set_eiem() at the bottom will ensure it
  50. * then gets disabled */
  51. }
  52. static void cpu_enable_irq(unsigned int irq)
  53. {
  54. unsigned long eirr_bit = EIEM_MASK(irq);
  55. cpu_eiem |= eirr_bit;
  56. /* FIXME: while our interrupts aren't nested, we cannot reset
  57. * the eiem mask if we're already in an interrupt. Once we
  58. * implement nested interrupts, this can go away
  59. */
  60. if (!in_interrupt())
  61. set_eiem(cpu_eiem);
  62. /* This is just a simple NOP IPI. But what it does is cause
  63. * all the other CPUs to do a set_eiem(cpu_eiem) at the end
  64. * of the interrupt handler */
  65. smp_send_all_nop();
  66. }
  67. static unsigned int cpu_startup_irq(unsigned int irq)
  68. {
  69. cpu_enable_irq(irq);
  70. return 0;
  71. }
  72. void no_ack_irq(unsigned int irq) { }
  73. void no_end_irq(unsigned int irq) { }
  74. #ifdef CONFIG_SMP
  75. int cpu_check_affinity(unsigned int irq, cpumask_t *dest)
  76. {
  77. int cpu_dest;
  78. /* timer and ipi have to always be received on all CPUs */
  79. if (irq == TIMER_IRQ || irq == IPI_IRQ) {
  80. /* Bad linux design decision. The mask has already
  81. * been set; we must reset it */
  82. irq_affinity[irq] = CPU_MASK_ALL;
  83. return -EINVAL;
  84. }
  85. /* whatever mask they set, we just allow one CPU */
  86. cpu_dest = first_cpu(*dest);
  87. *dest = cpumask_of_cpu(cpu_dest);
  88. return 0;
  89. }
  90. static void cpu_set_affinity_irq(unsigned int irq, cpumask_t dest)
  91. {
  92. if (cpu_check_affinity(irq, &dest))
  93. return;
  94. irq_affinity[irq] = dest;
  95. }
  96. #endif
  97. static struct hw_interrupt_type cpu_interrupt_type = {
  98. .typename = "CPU",
  99. .startup = cpu_startup_irq,
  100. .shutdown = cpu_disable_irq,
  101. .enable = cpu_enable_irq,
  102. .disable = cpu_disable_irq,
  103. .ack = no_ack_irq,
  104. .end = no_end_irq,
  105. #ifdef CONFIG_SMP
  106. .set_affinity = cpu_set_affinity_irq,
  107. #endif
  108. };
  109. int show_interrupts(struct seq_file *p, void *v)
  110. {
  111. int i = *(loff_t *) v, j;
  112. unsigned long flags;
  113. if (i == 0) {
  114. seq_puts(p, " ");
  115. for_each_online_cpu(j)
  116. seq_printf(p, " CPU%d", j);
  117. #ifdef PARISC_IRQ_CR16_COUNTS
  118. seq_printf(p, " [min/avg/max] (CPU cycle counts)");
  119. #endif
  120. seq_putc(p, '\n');
  121. }
  122. if (i < NR_IRQS) {
  123. struct irqaction *action;
  124. spin_lock_irqsave(&irq_desc[i].lock, flags);
  125. action = irq_desc[i].action;
  126. if (!action)
  127. goto skip;
  128. seq_printf(p, "%3d: ", i);
  129. #ifdef CONFIG_SMP
  130. for_each_online_cpu(j)
  131. seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
  132. #else
  133. seq_printf(p, "%10u ", kstat_irqs(i));
  134. #endif
  135. seq_printf(p, " %14s", irq_desc[i].handler->typename);
  136. #ifndef PARISC_IRQ_CR16_COUNTS
  137. seq_printf(p, " %s", action->name);
  138. while ((action = action->next))
  139. seq_printf(p, ", %s", action->name);
  140. #else
  141. for ( ;action; action = action->next) {
  142. unsigned int k, avg, min, max;
  143. min = max = action->cr16_hist[0];
  144. for (avg = k = 0; k < PARISC_CR16_HIST_SIZE; k++) {
  145. int hist = action->cr16_hist[k];
  146. if (hist) {
  147. avg += hist;
  148. } else
  149. break;
  150. if (hist > max) max = hist;
  151. if (hist < min) min = hist;
  152. }
  153. avg /= k;
  154. seq_printf(p, " %s[%d/%d/%d]", action->name,
  155. min,avg,max);
  156. }
  157. #endif
  158. seq_putc(p, '\n');
  159. skip:
  160. spin_unlock_irqrestore(&irq_desc[i].lock, flags);
  161. }
  162. return 0;
  163. }
  164. /*
  165. ** The following form a "set": Virtual IRQ, Transaction Address, Trans Data.
  166. ** Respectively, these map to IRQ region+EIRR, Processor HPA, EIRR bit.
  167. **
  168. ** To use txn_XXX() interfaces, get a Virtual IRQ first.
  169. ** Then use that to get the Transaction address and data.
  170. */
  171. int cpu_claim_irq(unsigned int irq, struct hw_interrupt_type *type, void *data)
  172. {
  173. if (irq_desc[irq].action)
  174. return -EBUSY;
  175. if (irq_desc[irq].handler != &cpu_interrupt_type)
  176. return -EBUSY;
  177. if (type) {
  178. irq_desc[irq].handler = type;
  179. irq_desc[irq].handler_data = data;
  180. cpu_interrupt_type.enable(irq);
  181. }
  182. return 0;
  183. }
  184. int txn_claim_irq(int irq)
  185. {
  186. return cpu_claim_irq(irq, NULL, NULL) ? -1 : irq;
  187. }
  188. /*
  189. * The bits_wide parameter accommodates the limitations of the HW/SW which
  190. * use these bits:
  191. * Legacy PA I/O (GSC/NIO): 5 bits (architected EIM register)
  192. * V-class (EPIC): 6 bits
  193. * N/L/A-class (iosapic): 8 bits
  194. * PCI 2.2 MSI: 16 bits
  195. * Some PCI devices: 32 bits (Symbios SCSI/ATM/HyperFabric)
  196. *
  197. * On the service provider side:
  198. * o PA 1.1 (and PA2.0 narrow mode) 5-bits (width of EIR register)
  199. * o PA 2.0 wide mode 6-bits (per processor)
  200. * o IA64 8-bits (0-256 total)
  201. *
  202. * So a Legacy PA I/O device on a PA 2.0 box can't use all the bits supported
  203. * by the processor...and the N/L-class I/O subsystem supports more bits than
  204. * PA2.0 has. The first case is the problem.
  205. */
  206. int txn_alloc_irq(unsigned int bits_wide)
  207. {
  208. int irq;
  209. /* never return irq 0 cause that's the interval timer */
  210. for (irq = CPU_IRQ_BASE + 1; irq <= CPU_IRQ_MAX; irq++) {
  211. if (cpu_claim_irq(irq, NULL, NULL) < 0)
  212. continue;
  213. if ((irq - CPU_IRQ_BASE) >= (1 << bits_wide))
  214. continue;
  215. return irq;
  216. }
  217. /* unlikely, but be prepared */
  218. return -1;
  219. }
  220. unsigned long txn_affinity_addr(unsigned int irq, int cpu)
  221. {
  222. #ifdef CONFIG_SMP
  223. irq_affinity[irq] = cpumask_of_cpu(cpu);
  224. #endif
  225. return cpu_data[cpu].txn_addr;
  226. }
  227. unsigned long txn_alloc_addr(unsigned int virt_irq)
  228. {
  229. static int next_cpu = -1;
  230. next_cpu++; /* assign to "next" CPU we want this bugger on */
  231. /* validate entry */
  232. while ((next_cpu < NR_CPUS) && (!cpu_data[next_cpu].txn_addr ||
  233. !cpu_online(next_cpu)))
  234. next_cpu++;
  235. if (next_cpu >= NR_CPUS)
  236. next_cpu = 0; /* nothing else, assign monarch */
  237. return txn_affinity_addr(virt_irq, next_cpu);
  238. }
  239. unsigned int txn_alloc_data(unsigned int virt_irq)
  240. {
  241. return virt_irq - CPU_IRQ_BASE;
  242. }
  243. /* ONLY called from entry.S:intr_extint() */
  244. void do_cpu_irq_mask(struct pt_regs *regs)
  245. {
  246. unsigned long eirr_val;
  247. irq_enter();
  248. /*
  249. * Don't allow TIMER or IPI nested interrupts.
  250. * Allowing any single interrupt to nest can lead to that CPU
  251. * handling interrupts with all enabled interrupts unmasked.
  252. */
  253. set_eiem(0UL);
  254. /* 1) only process IRQs that are enabled/unmasked (cpu_eiem)
  255. * 2) We loop here on EIRR contents in order to avoid
  256. * nested interrupts or having to take another interrupt
  257. * when we could have just handled it right away.
  258. */
  259. for (;;) {
  260. unsigned long bit = (1UL << (BITS_PER_LONG - 1));
  261. unsigned int irq;
  262. eirr_val = mfctl(23) & cpu_eiem;
  263. if (!eirr_val)
  264. break;
  265. mtctl(eirr_val, 23); /* reset bits we are going to process */
  266. /* Work our way from MSb to LSb...same order we alloc EIRs */
  267. for (irq = TIMER_IRQ; eirr_val && bit; bit>>=1, irq++) {
  268. #ifdef CONFIG_SMP
  269. cpumask_t dest = irq_affinity[irq];
  270. #endif
  271. if (!(bit & eirr_val))
  272. continue;
  273. /* clear bit in mask - can exit loop sooner */
  274. eirr_val &= ~bit;
  275. #ifdef CONFIG_SMP
  276. /* FIXME: because generic set affinity mucks
  277. * with the affinity before sending it to us
  278. * we can get the situation where the affinity is
  279. * wrong for our CPU type interrupts */
  280. if (irq != TIMER_IRQ && irq != IPI_IRQ &&
  281. !cpu_isset(smp_processor_id(), dest)) {
  282. int cpu = first_cpu(dest);
  283. printk(KERN_DEBUG "redirecting irq %d from CPU %d to %d\n",
  284. irq, smp_processor_id(), cpu);
  285. gsc_writel(irq + CPU_IRQ_BASE,
  286. cpu_data[cpu].hpa);
  287. continue;
  288. }
  289. #endif
  290. __do_IRQ(irq, regs);
  291. }
  292. }
  293. set_eiem(cpu_eiem); /* restore original mask */
  294. irq_exit();
  295. }
  296. static struct irqaction timer_action = {
  297. .handler = timer_interrupt,
  298. .name = "timer",
  299. .flags = SA_INTERRUPT,
  300. };
  301. #ifdef CONFIG_SMP
  302. static struct irqaction ipi_action = {
  303. .handler = ipi_interrupt,
  304. .name = "IPI",
  305. .flags = SA_INTERRUPT,
  306. };
  307. #endif
  308. static void claim_cpu_irqs(void)
  309. {
  310. int i;
  311. for (i = CPU_IRQ_BASE; i <= CPU_IRQ_MAX; i++) {
  312. irq_desc[i].handler = &cpu_interrupt_type;
  313. }
  314. irq_desc[TIMER_IRQ].action = &timer_action;
  315. irq_desc[TIMER_IRQ].status |= IRQ_PER_CPU;
  316. #ifdef CONFIG_SMP
  317. irq_desc[IPI_IRQ].action = &ipi_action;
  318. irq_desc[IPI_IRQ].status = IRQ_PER_CPU;
  319. #endif
  320. }
  321. void __init init_IRQ(void)
  322. {
  323. local_irq_disable(); /* PARANOID - should already be disabled */
  324. mtctl(~0UL, 23); /* EIRR : clear all pending external intr */
  325. claim_cpu_irqs();
  326. #ifdef CONFIG_SMP
  327. if (!cpu_eiem)
  328. cpu_eiem = EIEM_MASK(IPI_IRQ) | EIEM_MASK(TIMER_IRQ);
  329. #else
  330. cpu_eiem = EIEM_MASK(TIMER_IRQ);
  331. #endif
  332. set_eiem(cpu_eiem); /* EIEM : enable all external intr */
  333. }
  334. void hw_resend_irq(struct hw_interrupt_type *type, unsigned int irq)
  335. {
  336. /* XXX: Needs to be written. We managed without it so far, but
  337. * we really ought to write it.
  338. */
  339. }
  340. void ack_bad_irq(unsigned int irq)
  341. {
  342. printk("unexpected IRQ %d\n", irq);
  343. }