irq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/errno.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/kernel_stat.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/types.h>
  32. #include <asm/io.h>
  33. #include <asm/smp.h>
  34. #undef PARISC_IRQ_CR16_COUNTS
  35. extern irqreturn_t timer_interrupt(int, void *);
  36. extern irqreturn_t ipi_interrupt(int, void *);
  37. #define EIEM_MASK(irq) (1UL<<(CPU_IRQ_MAX - irq))
  38. /* Bits in EIEM correlate with cpu_irq_action[].
  39. ** Numbered *Big Endian*! (ie bit 0 is MSB)
  40. */
  41. static volatile unsigned long cpu_eiem = 0;
  42. /*
  43. ** ack bitmap ... habitually set to 1, but reset to zero
  44. ** between ->ack() and ->end() of the interrupt to prevent
  45. ** re-interruption of a processing interrupt.
  46. */
  47. static volatile unsigned long global_ack_eiem = ~0UL;
  48. /*
  49. ** Local bitmap, same as above but for per-cpu interrupts
  50. */
  51. static DEFINE_PER_CPU(unsigned long, local_ack_eiem) = ~0UL;
  52. static void cpu_disable_irq(unsigned int irq)
  53. {
  54. unsigned long eirr_bit = EIEM_MASK(irq);
  55. cpu_eiem &= ~eirr_bit;
  56. /* Do nothing on the other CPUs. If they get this interrupt,
  57. * The & cpu_eiem in the do_cpu_irq_mask() ensures they won't
  58. * handle it, and the set_eiem() at the bottom will ensure it
  59. * then gets disabled */
  60. }
  61. static void cpu_enable_irq(unsigned int irq)
  62. {
  63. unsigned long eirr_bit = EIEM_MASK(irq);
  64. cpu_eiem |= eirr_bit;
  65. /* This is just a simple NOP IPI. But what it does is cause
  66. * all the other CPUs to do a set_eiem(cpu_eiem) at the end
  67. * of the interrupt handler */
  68. smp_send_all_nop();
  69. }
  70. static unsigned int cpu_startup_irq(unsigned int irq)
  71. {
  72. cpu_enable_irq(irq);
  73. return 0;
  74. }
  75. void no_ack_irq(unsigned int irq) { }
  76. void no_end_irq(unsigned int irq) { }
  77. void cpu_ack_irq(unsigned int irq)
  78. {
  79. unsigned long mask = EIEM_MASK(irq);
  80. int cpu = smp_processor_id();
  81. /* Clear in EIEM so we can no longer process */
  82. if (CHECK_IRQ_PER_CPU(irq_desc[irq].status))
  83. per_cpu(local_ack_eiem, cpu) &= ~mask;
  84. else
  85. global_ack_eiem &= ~mask;
  86. /* disable the interrupt */
  87. set_eiem(cpu_eiem & global_ack_eiem & per_cpu(local_ack_eiem, cpu));
  88. /* and now ack it */
  89. mtctl(mask, 23);
  90. }
  91. void cpu_end_irq(unsigned int irq)
  92. {
  93. unsigned long mask = EIEM_MASK(irq);
  94. int cpu = smp_processor_id();
  95. /* set it in the eiems---it's no longer in process */
  96. if (CHECK_IRQ_PER_CPU(irq_desc[irq].status))
  97. per_cpu(local_ack_eiem, cpu) |= mask;
  98. else
  99. global_ack_eiem |= mask;
  100. /* enable the interrupt */
  101. set_eiem(cpu_eiem & global_ack_eiem & per_cpu(local_ack_eiem, cpu));
  102. }
  103. #ifdef CONFIG_SMP
  104. int cpu_check_affinity(unsigned int irq, cpumask_t *dest)
  105. {
  106. int cpu_dest;
  107. /* timer and ipi have to always be received on all CPUs */
  108. if (CHECK_IRQ_PER_CPU(irq)) {
  109. /* Bad linux design decision. The mask has already
  110. * been set; we must reset it */
  111. irq_desc[irq].affinity = CPU_MASK_ALL;
  112. return -EINVAL;
  113. }
  114. /* whatever mask they set, we just allow one CPU */
  115. cpu_dest = first_cpu(*dest);
  116. *dest = cpumask_of_cpu(cpu_dest);
  117. return 0;
  118. }
  119. static void cpu_set_affinity_irq(unsigned int irq, cpumask_t dest)
  120. {
  121. if (cpu_check_affinity(irq, &dest))
  122. return;
  123. irq_desc[irq].affinity = dest;
  124. }
  125. #endif
  126. static struct hw_interrupt_type cpu_interrupt_type = {
  127. .typename = "CPU",
  128. .startup = cpu_startup_irq,
  129. .shutdown = cpu_disable_irq,
  130. .enable = cpu_enable_irq,
  131. .disable = cpu_disable_irq,
  132. .ack = cpu_ack_irq,
  133. .end = cpu_end_irq,
  134. #ifdef CONFIG_SMP
  135. .set_affinity = cpu_set_affinity_irq,
  136. #endif
  137. /* XXX: Needs to be written. We managed without it so far, but
  138. * we really ought to write it.
  139. */
  140. .retrigger = NULL,
  141. };
  142. int show_interrupts(struct seq_file *p, void *v)
  143. {
  144. int i = *(loff_t *) v, j;
  145. unsigned long flags;
  146. if (i == 0) {
  147. seq_puts(p, " ");
  148. for_each_online_cpu(j)
  149. seq_printf(p, " CPU%d", j);
  150. #ifdef PARISC_IRQ_CR16_COUNTS
  151. seq_printf(p, " [min/avg/max] (CPU cycle counts)");
  152. #endif
  153. seq_putc(p, '\n');
  154. }
  155. if (i < NR_IRQS) {
  156. struct irqaction *action;
  157. spin_lock_irqsave(&irq_desc[i].lock, flags);
  158. action = irq_desc[i].action;
  159. if (!action)
  160. goto skip;
  161. seq_printf(p, "%3d: ", i);
  162. #ifdef CONFIG_SMP
  163. for_each_online_cpu(j)
  164. seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
  165. #else
  166. seq_printf(p, "%10u ", kstat_irqs(i));
  167. #endif
  168. seq_printf(p, " %14s", irq_desc[i].chip->typename);
  169. #ifndef PARISC_IRQ_CR16_COUNTS
  170. seq_printf(p, " %s", action->name);
  171. while ((action = action->next))
  172. seq_printf(p, ", %s", action->name);
  173. #else
  174. for ( ;action; action = action->next) {
  175. unsigned int k, avg, min, max;
  176. min = max = action->cr16_hist[0];
  177. for (avg = k = 0; k < PARISC_CR16_HIST_SIZE; k++) {
  178. int hist = action->cr16_hist[k];
  179. if (hist) {
  180. avg += hist;
  181. } else
  182. break;
  183. if (hist > max) max = hist;
  184. if (hist < min) min = hist;
  185. }
  186. avg /= k;
  187. seq_printf(p, " %s[%d/%d/%d]", action->name,
  188. min,avg,max);
  189. }
  190. #endif
  191. seq_putc(p, '\n');
  192. skip:
  193. spin_unlock_irqrestore(&irq_desc[i].lock, flags);
  194. }
  195. return 0;
  196. }
  197. /*
  198. ** The following form a "set": Virtual IRQ, Transaction Address, Trans Data.
  199. ** Respectively, these map to IRQ region+EIRR, Processor HPA, EIRR bit.
  200. **
  201. ** To use txn_XXX() interfaces, get a Virtual IRQ first.
  202. ** Then use that to get the Transaction address and data.
  203. */
  204. int cpu_claim_irq(unsigned int irq, struct irq_chip *type, void *data)
  205. {
  206. if (irq_desc[irq].action)
  207. return -EBUSY;
  208. if (irq_desc[irq].chip != &cpu_interrupt_type)
  209. return -EBUSY;
  210. if (type) {
  211. irq_desc[irq].chip = type;
  212. irq_desc[irq].chip_data = data;
  213. cpu_interrupt_type.enable(irq);
  214. }
  215. return 0;
  216. }
  217. int txn_claim_irq(int irq)
  218. {
  219. return cpu_claim_irq(irq, NULL, NULL) ? -1 : irq;
  220. }
  221. /*
  222. * The bits_wide parameter accommodates the limitations of the HW/SW which
  223. * use these bits:
  224. * Legacy PA I/O (GSC/NIO): 5 bits (architected EIM register)
  225. * V-class (EPIC): 6 bits
  226. * N/L/A-class (iosapic): 8 bits
  227. * PCI 2.2 MSI: 16 bits
  228. * Some PCI devices: 32 bits (Symbios SCSI/ATM/HyperFabric)
  229. *
  230. * On the service provider side:
  231. * o PA 1.1 (and PA2.0 narrow mode) 5-bits (width of EIR register)
  232. * o PA 2.0 wide mode 6-bits (per processor)
  233. * o IA64 8-bits (0-256 total)
  234. *
  235. * So a Legacy PA I/O device on a PA 2.0 box can't use all the bits supported
  236. * by the processor...and the N/L-class I/O subsystem supports more bits than
  237. * PA2.0 has. The first case is the problem.
  238. */
  239. int txn_alloc_irq(unsigned int bits_wide)
  240. {
  241. int irq;
  242. /* never return irq 0 cause that's the interval timer */
  243. for (irq = CPU_IRQ_BASE + 1; irq <= CPU_IRQ_MAX; irq++) {
  244. if (cpu_claim_irq(irq, NULL, NULL) < 0)
  245. continue;
  246. if ((irq - CPU_IRQ_BASE) >= (1 << bits_wide))
  247. continue;
  248. return irq;
  249. }
  250. /* unlikely, but be prepared */
  251. return -1;
  252. }
  253. unsigned long txn_affinity_addr(unsigned int irq, int cpu)
  254. {
  255. #ifdef CONFIG_SMP
  256. irq_desc[irq].affinity = cpumask_of_cpu(cpu);
  257. #endif
  258. return cpu_data[cpu].txn_addr;
  259. }
  260. unsigned long txn_alloc_addr(unsigned int virt_irq)
  261. {
  262. static int next_cpu = -1;
  263. next_cpu++; /* assign to "next" CPU we want this bugger on */
  264. /* validate entry */
  265. while ((next_cpu < NR_CPUS) && (!cpu_data[next_cpu].txn_addr ||
  266. !cpu_online(next_cpu)))
  267. next_cpu++;
  268. if (next_cpu >= NR_CPUS)
  269. next_cpu = 0; /* nothing else, assign monarch */
  270. return txn_affinity_addr(virt_irq, next_cpu);
  271. }
  272. unsigned int txn_alloc_data(unsigned int virt_irq)
  273. {
  274. return virt_irq - CPU_IRQ_BASE;
  275. }
  276. static inline int eirr_to_irq(unsigned long eirr)
  277. {
  278. #ifdef CONFIG_64BIT
  279. int bit = fls64(eirr);
  280. #else
  281. int bit = fls(eirr);
  282. #endif
  283. return (BITS_PER_LONG - bit) + TIMER_IRQ;
  284. }
  285. /* ONLY called from entry.S:intr_extint() */
  286. void do_cpu_irq_mask(struct pt_regs *regs)
  287. {
  288. struct pt_regs *old_regs;
  289. unsigned long eirr_val;
  290. int irq, cpu = smp_processor_id();
  291. #ifdef CONFIG_SMP
  292. cpumask_t dest;
  293. #endif
  294. old_regs = set_irq_regs(regs);
  295. local_irq_disable();
  296. irq_enter();
  297. eirr_val = mfctl(23) & cpu_eiem & global_ack_eiem &
  298. per_cpu(local_ack_eiem, cpu);
  299. if (!eirr_val)
  300. goto set_out;
  301. irq = eirr_to_irq(eirr_val);
  302. #ifdef CONFIG_SMP
  303. dest = irq_desc[irq].affinity;
  304. if (CHECK_IRQ_PER_CPU(irq_desc[irq].status) &&
  305. !cpu_isset(smp_processor_id(), dest)) {
  306. int cpu = first_cpu(dest);
  307. printk(KERN_DEBUG "redirecting irq %d from CPU %d to %d\n",
  308. irq, smp_processor_id(), cpu);
  309. gsc_writel(irq + CPU_IRQ_BASE,
  310. cpu_data[cpu].hpa);
  311. goto set_out;
  312. }
  313. #endif
  314. __do_IRQ(irq);
  315. out:
  316. irq_exit();
  317. set_irq_regs(old_regs);
  318. return;
  319. set_out:
  320. set_eiem(cpu_eiem & global_ack_eiem & per_cpu(local_ack_eiem, cpu));
  321. goto out;
  322. }
  323. static struct irqaction timer_action = {
  324. .handler = timer_interrupt,
  325. .name = "timer",
  326. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_PERCPU,
  327. };
  328. #ifdef CONFIG_SMP
  329. static struct irqaction ipi_action = {
  330. .handler = ipi_interrupt,
  331. .name = "IPI",
  332. .flags = IRQF_DISABLED | IRQF_PERCPU,
  333. };
  334. #endif
  335. static void claim_cpu_irqs(void)
  336. {
  337. int i;
  338. for (i = CPU_IRQ_BASE; i <= CPU_IRQ_MAX; i++) {
  339. irq_desc[i].chip = &cpu_interrupt_type;
  340. }
  341. irq_desc[TIMER_IRQ].action = &timer_action;
  342. irq_desc[TIMER_IRQ].status |= IRQ_PER_CPU;
  343. #ifdef CONFIG_SMP
  344. irq_desc[IPI_IRQ].action = &ipi_action;
  345. irq_desc[IPI_IRQ].status = IRQ_PER_CPU;
  346. #endif
  347. }
  348. void __init init_IRQ(void)
  349. {
  350. local_irq_disable(); /* PARANOID - should already be disabled */
  351. mtctl(~0UL, 23); /* EIRR : clear all pending external intr */
  352. claim_cpu_irqs();
  353. #ifdef CONFIG_SMP
  354. if (!cpu_eiem)
  355. cpu_eiem = EIEM_MASK(IPI_IRQ) | EIEM_MASK(TIMER_IRQ);
  356. #else
  357. cpu_eiem = EIEM_MASK(TIMER_IRQ);
  358. #endif
  359. set_eiem(cpu_eiem); /* EIEM : enable all external intr */
  360. }
  361. void ack_bad_irq(unsigned int irq)
  362. {
  363. printk("unexpected IRQ %d\n", irq);
  364. }