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