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