irq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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(struct irq_data *d)
  49. {
  50. unsigned long eirr_bit = EIEM_MASK(d->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. static void cpu_unmask_irq(struct irq_data *d)
  67. {
  68. __cpu_unmask_irq(d->irq);
  69. }
  70. void cpu_ack_irq(struct irq_data *d)
  71. {
  72. unsigned long mask = EIEM_MASK(d->irq);
  73. int cpu = smp_processor_id();
  74. /* Clear in EIEM so we can no longer process */
  75. per_cpu(local_ack_eiem, cpu) &= ~mask;
  76. /* disable the interrupt */
  77. set_eiem(cpu_eiem & per_cpu(local_ack_eiem, cpu));
  78. /* and now ack it */
  79. mtctl(mask, 23);
  80. }
  81. void cpu_eoi_irq(struct irq_data *d)
  82. {
  83. unsigned long mask = EIEM_MASK(d->irq);
  84. int cpu = smp_processor_id();
  85. /* set it in the eiems---it's no longer in process */
  86. per_cpu(local_ack_eiem, cpu) |= mask;
  87. /* enable the interrupt */
  88. set_eiem(cpu_eiem & per_cpu(local_ack_eiem, cpu));
  89. }
  90. #ifdef CONFIG_SMP
  91. int cpu_check_affinity(struct irq_data *d, const struct cpumask *dest)
  92. {
  93. int cpu_dest;
  94. /* timer and ipi have to always be received on all CPUs */
  95. if (irqd_is_per_cpu(d))
  96. return -EINVAL;
  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(struct irq_data *d, const struct cpumask *dest,
  102. bool force)
  103. {
  104. int cpu_dest;
  105. cpu_dest = cpu_check_affinity(d, dest);
  106. if (cpu_dest < 0)
  107. return -1;
  108. cpumask_copy(d->affinity, dest);
  109. return 0;
  110. }
  111. #endif
  112. static struct irq_chip cpu_interrupt_type = {
  113. .name = "CPU",
  114. .irq_mask = cpu_mask_irq,
  115. .irq_unmask = cpu_unmask_irq,
  116. .irq_ack = cpu_ack_irq,
  117. .irq_eoi = cpu_eoi_irq,
  118. #ifdef CONFIG_SMP
  119. .irq_set_affinity = cpu_set_affinity_irq,
  120. #endif
  121. /* XXX: Needs to be written. We managed without it so far, but
  122. * we really ought to write it.
  123. */
  124. .irq_retrigger = NULL,
  125. };
  126. DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
  127. #define irq_stats(x) (&per_cpu(irq_stat, x))
  128. /*
  129. * /proc/interrupts printing for arch specific interrupts
  130. */
  131. int arch_show_interrupts(struct seq_file *p, int prec)
  132. {
  133. int j;
  134. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  135. seq_printf(p, "%*s: ", prec, "STK");
  136. for_each_online_cpu(j)
  137. seq_printf(p, "%10u ", irq_stats(j)->kernel_stack_usage);
  138. seq_puts(p, " Kernel stack usage\n");
  139. # ifdef CONFIG_IRQSTACKS
  140. seq_printf(p, "%*s: ", prec, "IST");
  141. for_each_online_cpu(j)
  142. seq_printf(p, "%10u ", irq_stats(j)->irq_stack_usage);
  143. seq_puts(p, " Interrupt stack usage\n");
  144. seq_printf(p, "%*s: ", prec, "ISC");
  145. for_each_online_cpu(j)
  146. seq_printf(p, "%10u ", irq_stats(j)->irq_stack_counter);
  147. seq_puts(p, " Interrupt stack usage counter\n");
  148. # endif
  149. #endif
  150. #ifdef CONFIG_SMP
  151. seq_printf(p, "%*s: ", prec, "RES");
  152. for_each_online_cpu(j)
  153. seq_printf(p, "%10u ", irq_stats(j)->irq_resched_count);
  154. seq_puts(p, " Rescheduling interrupts\n");
  155. seq_printf(p, "%*s: ", prec, "CAL");
  156. for_each_online_cpu(j)
  157. seq_printf(p, "%10u ", irq_stats(j)->irq_call_count);
  158. seq_puts(p, " Function call interrupts\n");
  159. #endif
  160. seq_printf(p, "%*s: ", prec, "UAH");
  161. for_each_online_cpu(j)
  162. seq_printf(p, "%10u ", irq_stats(j)->irq_unaligned_count);
  163. seq_puts(p, " Unaligned access handler traps\n");
  164. seq_printf(p, "%*s: ", prec, "FPA");
  165. for_each_online_cpu(j)
  166. seq_printf(p, "%10u ", irq_stats(j)->irq_fpassist_count);
  167. seq_puts(p, " Floating point assist traps\n");
  168. seq_printf(p, "%*s: ", prec, "TLB");
  169. for_each_online_cpu(j)
  170. seq_printf(p, "%10u ", irq_stats(j)->irq_tlb_count);
  171. seq_puts(p, " TLB shootdowns\n");
  172. return 0;
  173. }
  174. int show_interrupts(struct seq_file *p, void *v)
  175. {
  176. int i = *(loff_t *) v, j;
  177. unsigned long flags;
  178. if (i == 0) {
  179. seq_puts(p, " ");
  180. for_each_online_cpu(j)
  181. seq_printf(p, " CPU%d", j);
  182. #ifdef PARISC_IRQ_CR16_COUNTS
  183. seq_printf(p, " [min/avg/max] (CPU cycle counts)");
  184. #endif
  185. seq_putc(p, '\n');
  186. }
  187. if (i < NR_IRQS) {
  188. struct irq_desc *desc = irq_to_desc(i);
  189. struct irqaction *action;
  190. raw_spin_lock_irqsave(&desc->lock, flags);
  191. action = desc->action;
  192. if (!action)
  193. goto skip;
  194. seq_printf(p, "%3d: ", i);
  195. #ifdef CONFIG_SMP
  196. for_each_online_cpu(j)
  197. seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
  198. #else
  199. seq_printf(p, "%10u ", kstat_irqs(i));
  200. #endif
  201. seq_printf(p, " %14s", irq_desc_get_chip(desc)->name);
  202. #ifndef PARISC_IRQ_CR16_COUNTS
  203. seq_printf(p, " %s", action->name);
  204. while ((action = action->next))
  205. seq_printf(p, ", %s", action->name);
  206. #else
  207. for ( ;action; action = action->next) {
  208. unsigned int k, avg, min, max;
  209. min = max = action->cr16_hist[0];
  210. for (avg = k = 0; k < PARISC_CR16_HIST_SIZE; k++) {
  211. int hist = action->cr16_hist[k];
  212. if (hist) {
  213. avg += hist;
  214. } else
  215. break;
  216. if (hist > max) max = hist;
  217. if (hist < min) min = hist;
  218. }
  219. avg /= k;
  220. seq_printf(p, " %s[%d/%d/%d]", action->name,
  221. min,avg,max);
  222. }
  223. #endif
  224. seq_putc(p, '\n');
  225. skip:
  226. raw_spin_unlock_irqrestore(&desc->lock, flags);
  227. }
  228. if (i == NR_IRQS)
  229. arch_show_interrupts(p, 3);
  230. return 0;
  231. }
  232. /*
  233. ** The following form a "set": Virtual IRQ, Transaction Address, Trans Data.
  234. ** Respectively, these map to IRQ region+EIRR, Processor HPA, EIRR bit.
  235. **
  236. ** To use txn_XXX() interfaces, get a Virtual IRQ first.
  237. ** Then use that to get the Transaction address and data.
  238. */
  239. int cpu_claim_irq(unsigned int irq, struct irq_chip *type, void *data)
  240. {
  241. if (irq_has_action(irq))
  242. return -EBUSY;
  243. if (irq_get_chip(irq) != &cpu_interrupt_type)
  244. return -EBUSY;
  245. /* for iosapic interrupts */
  246. if (type) {
  247. irq_set_chip_and_handler(irq, type, handle_percpu_irq);
  248. irq_set_chip_data(irq, data);
  249. __cpu_unmask_irq(irq);
  250. }
  251. return 0;
  252. }
  253. int txn_claim_irq(int irq)
  254. {
  255. return cpu_claim_irq(irq, NULL, NULL) ? -1 : irq;
  256. }
  257. /*
  258. * The bits_wide parameter accommodates the limitations of the HW/SW which
  259. * use these bits:
  260. * Legacy PA I/O (GSC/NIO): 5 bits (architected EIM register)
  261. * V-class (EPIC): 6 bits
  262. * N/L/A-class (iosapic): 8 bits
  263. * PCI 2.2 MSI: 16 bits
  264. * Some PCI devices: 32 bits (Symbios SCSI/ATM/HyperFabric)
  265. *
  266. * On the service provider side:
  267. * o PA 1.1 (and PA2.0 narrow mode) 5-bits (width of EIR register)
  268. * o PA 2.0 wide mode 6-bits (per processor)
  269. * o IA64 8-bits (0-256 total)
  270. *
  271. * So a Legacy PA I/O device on a PA 2.0 box can't use all the bits supported
  272. * by the processor...and the N/L-class I/O subsystem supports more bits than
  273. * PA2.0 has. The first case is the problem.
  274. */
  275. int txn_alloc_irq(unsigned int bits_wide)
  276. {
  277. int irq;
  278. /* never return irq 0 cause that's the interval timer */
  279. for (irq = CPU_IRQ_BASE + 1; irq <= CPU_IRQ_MAX; irq++) {
  280. if (cpu_claim_irq(irq, NULL, NULL) < 0)
  281. continue;
  282. if ((irq - CPU_IRQ_BASE) >= (1 << bits_wide))
  283. continue;
  284. return irq;
  285. }
  286. /* unlikely, but be prepared */
  287. return -1;
  288. }
  289. unsigned long txn_affinity_addr(unsigned int irq, int cpu)
  290. {
  291. #ifdef CONFIG_SMP
  292. struct irq_data *d = irq_get_irq_data(irq);
  293. cpumask_copy(d->affinity, cpumask_of(cpu));
  294. #endif
  295. return per_cpu(cpu_data, cpu).txn_addr;
  296. }
  297. unsigned long txn_alloc_addr(unsigned int virt_irq)
  298. {
  299. static int next_cpu = -1;
  300. next_cpu++; /* assign to "next" CPU we want this bugger on */
  301. /* validate entry */
  302. while ((next_cpu < nr_cpu_ids) &&
  303. (!per_cpu(cpu_data, next_cpu).txn_addr ||
  304. !cpu_online(next_cpu)))
  305. next_cpu++;
  306. if (next_cpu >= nr_cpu_ids)
  307. next_cpu = 0; /* nothing else, assign monarch */
  308. return txn_affinity_addr(virt_irq, next_cpu);
  309. }
  310. unsigned int txn_alloc_data(unsigned int virt_irq)
  311. {
  312. return virt_irq - CPU_IRQ_BASE;
  313. }
  314. static inline int eirr_to_irq(unsigned long eirr)
  315. {
  316. int bit = fls_long(eirr);
  317. return (BITS_PER_LONG - bit) + TIMER_IRQ;
  318. }
  319. int sysctl_panic_on_stackoverflow = 1;
  320. static inline void stack_overflow_check(struct pt_regs *regs)
  321. {
  322. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  323. #define STACK_MARGIN (256*6)
  324. /* Our stack starts directly behind the thread_info struct. */
  325. unsigned long stack_start = (unsigned long) current_thread_info();
  326. unsigned long sp = regs->gr[30];
  327. unsigned long stack_usage;
  328. unsigned int *last_usage;
  329. int cpu = smp_processor_id();
  330. /* if sr7 != 0, we interrupted a userspace process which we do not want
  331. * to check for stack overflow. We will only check the kernel stack. */
  332. if (regs->sr[7])
  333. return;
  334. /* calculate kernel stack usage */
  335. stack_usage = sp - stack_start;
  336. #ifdef CONFIG_IRQSTACKS
  337. if (likely(stack_usage <= THREAD_SIZE))
  338. goto check_kernel_stack; /* found kernel stack */
  339. /* check irq stack usage */
  340. stack_start = (unsigned long) &per_cpu(irq_stack_union, cpu).stack;
  341. stack_usage = sp - stack_start;
  342. last_usage = &per_cpu(irq_stat.irq_stack_usage, cpu);
  343. if (unlikely(stack_usage > *last_usage))
  344. *last_usage = stack_usage;
  345. if (likely(stack_usage < (IRQ_STACK_SIZE - STACK_MARGIN)))
  346. return;
  347. pr_emerg("stackcheck: %s will most likely overflow irq stack "
  348. "(sp:%lx, stk bottom-top:%lx-%lx)\n",
  349. current->comm, sp, stack_start, stack_start + IRQ_STACK_SIZE);
  350. goto panic_check;
  351. check_kernel_stack:
  352. #endif
  353. /* check kernel stack usage */
  354. last_usage = &per_cpu(irq_stat.kernel_stack_usage, cpu);
  355. if (unlikely(stack_usage > *last_usage))
  356. *last_usage = stack_usage;
  357. if (likely(stack_usage < (THREAD_SIZE - STACK_MARGIN)))
  358. return;
  359. pr_emerg("stackcheck: %s will most likely overflow kernel stack "
  360. "(sp:%lx, stk bottom-top:%lx-%lx)\n",
  361. current->comm, sp, stack_start, stack_start + THREAD_SIZE);
  362. #ifdef CONFIG_IRQSTACKS
  363. panic_check:
  364. #endif
  365. if (sysctl_panic_on_stackoverflow)
  366. panic("low stack detected by irq handler - check messages\n");
  367. #endif
  368. }
  369. #ifdef CONFIG_IRQSTACKS
  370. DEFINE_PER_CPU(union irq_stack_union, irq_stack_union) = {
  371. .lock = __RAW_SPIN_LOCK_UNLOCKED((irq_stack_union).lock)
  372. };
  373. static void execute_on_irq_stack(void *func, unsigned long param1)
  374. {
  375. union irq_stack_union *union_ptr;
  376. unsigned long irq_stack;
  377. raw_spinlock_t *irq_stack_in_use;
  378. union_ptr = &per_cpu(irq_stack_union, smp_processor_id());
  379. irq_stack = (unsigned long) &union_ptr->stack;
  380. irq_stack = ALIGN(irq_stack + sizeof(irq_stack_union.lock),
  381. 64); /* align for stack frame usage */
  382. /* We may be called recursive. If we are already using the irq stack,
  383. * just continue to use it. Use spinlocks to serialize
  384. * the irq stack usage.
  385. */
  386. irq_stack_in_use = &union_ptr->lock;
  387. if (!raw_spin_trylock(irq_stack_in_use)) {
  388. void (*direct_call)(unsigned long p1) = func;
  389. /* We are using the IRQ stack already.
  390. * Do direct call on current stack. */
  391. direct_call(param1);
  392. return;
  393. }
  394. /* This is where we switch to the IRQ stack. */
  395. call_on_stack(param1, func, irq_stack);
  396. __inc_irq_stat(irq_stack_counter);
  397. /* free up irq stack usage. */
  398. do_raw_spin_unlock(irq_stack_in_use);
  399. }
  400. asmlinkage void do_softirq(void)
  401. {
  402. __u32 pending;
  403. unsigned long flags;
  404. if (in_interrupt())
  405. return;
  406. local_irq_save(flags);
  407. pending = local_softirq_pending();
  408. if (pending)
  409. execute_on_irq_stack(__do_softirq, 0);
  410. local_irq_restore(flags);
  411. }
  412. #endif /* CONFIG_IRQSTACKS */
  413. /* ONLY called from entry.S:intr_extint() */
  414. void do_cpu_irq_mask(struct pt_regs *regs)
  415. {
  416. struct pt_regs *old_regs;
  417. unsigned long eirr_val;
  418. int irq, cpu = smp_processor_id();
  419. #ifdef CONFIG_SMP
  420. struct irq_desc *desc;
  421. cpumask_t dest;
  422. #endif
  423. old_regs = set_irq_regs(regs);
  424. local_irq_disable();
  425. irq_enter();
  426. eirr_val = mfctl(23) & cpu_eiem & per_cpu(local_ack_eiem, cpu);
  427. if (!eirr_val)
  428. goto set_out;
  429. irq = eirr_to_irq(eirr_val);
  430. #ifdef CONFIG_SMP
  431. desc = irq_to_desc(irq);
  432. cpumask_copy(&dest, desc->irq_data.affinity);
  433. if (irqd_is_per_cpu(&desc->irq_data) &&
  434. !cpu_isset(smp_processor_id(), dest)) {
  435. int cpu = first_cpu(dest);
  436. printk(KERN_DEBUG "redirecting irq %d from CPU %d to %d\n",
  437. irq, smp_processor_id(), cpu);
  438. gsc_writel(irq + CPU_IRQ_BASE,
  439. per_cpu(cpu_data, cpu).hpa);
  440. goto set_out;
  441. }
  442. #endif
  443. stack_overflow_check(regs);
  444. #ifdef CONFIG_IRQSTACKS
  445. execute_on_irq_stack(&generic_handle_irq, irq);
  446. #else
  447. generic_handle_irq(irq);
  448. #endif /* CONFIG_IRQSTACKS */
  449. out:
  450. irq_exit();
  451. set_irq_regs(old_regs);
  452. return;
  453. set_out:
  454. set_eiem(cpu_eiem & per_cpu(local_ack_eiem, cpu));
  455. goto out;
  456. }
  457. static struct irqaction timer_action = {
  458. .handler = timer_interrupt,
  459. .name = "timer",
  460. .flags = IRQF_TIMER | IRQF_PERCPU | IRQF_IRQPOLL,
  461. };
  462. #ifdef CONFIG_SMP
  463. static struct irqaction ipi_action = {
  464. .handler = ipi_interrupt,
  465. .name = "IPI",
  466. .flags = IRQF_PERCPU,
  467. };
  468. #endif
  469. static void claim_cpu_irqs(void)
  470. {
  471. int i;
  472. for (i = CPU_IRQ_BASE; i <= CPU_IRQ_MAX; i++) {
  473. irq_set_chip_and_handler(i, &cpu_interrupt_type,
  474. handle_percpu_irq);
  475. }
  476. irq_set_handler(TIMER_IRQ, handle_percpu_irq);
  477. setup_irq(TIMER_IRQ, &timer_action);
  478. #ifdef CONFIG_SMP
  479. irq_set_handler(IPI_IRQ, handle_percpu_irq);
  480. setup_irq(IPI_IRQ, &ipi_action);
  481. #endif
  482. }
  483. void __init init_IRQ(void)
  484. {
  485. local_irq_disable(); /* PARANOID - should already be disabled */
  486. mtctl(~0UL, 23); /* EIRR : clear all pending external intr */
  487. #ifdef CONFIG_SMP
  488. if (!cpu_eiem) {
  489. claim_cpu_irqs();
  490. cpu_eiem = EIEM_MASK(IPI_IRQ) | EIEM_MASK(TIMER_IRQ);
  491. }
  492. #else
  493. claim_cpu_irqs();
  494. cpu_eiem = EIEM_MASK(TIMER_IRQ);
  495. #endif
  496. set_eiem(cpu_eiem); /* EIEM : enable all external intr */
  497. }