irq.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * Derived from arch/i386/kernel/irq.c
  3. * Copyright (C) 1992 Linus Torvalds
  4. * Adapted from arch/i386 by Gary Thomas
  5. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  6. * Updated and modified by Cort Dougan <cort@fsmlabs.com>
  7. * Copyright (C) 1996-2001 Cort Dougan
  8. * Adapted for Power Macintosh by Paul Mackerras
  9. * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * This file contains the code used by various IRQ handling routines:
  17. * asking for different IRQ's should be done through these routines
  18. * instead of just grabbing them. Thus setups with different IRQ numbers
  19. * shouldn't result in any weird surprises, and installing new handlers
  20. * should be easier.
  21. *
  22. * The MPC8xx has an interrupt mask in the SIU. If a bit is set, the
  23. * interrupt is _enabled_. As expected, IRQ0 is bit 0 in the 32-bit
  24. * mask register (of which only 16 are defined), hence the weird shifting
  25. * and complement of the cached_irq_mask. I want to be able to stuff
  26. * this right into the SIU SMASK register.
  27. * Many of the prep/chrp functions are conditional compiled on CONFIG_8xx
  28. * to reduce code space and undefined function references.
  29. */
  30. #undef DEBUG
  31. #include <linux/export.h>
  32. #include <linux/threads.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/signal.h>
  35. #include <linux/sched.h>
  36. #include <linux/ptrace.h>
  37. #include <linux/ioport.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/timex.h>
  40. #include <linux/init.h>
  41. #include <linux/slab.h>
  42. #include <linux/delay.h>
  43. #include <linux/irq.h>
  44. #include <linux/seq_file.h>
  45. #include <linux/cpumask.h>
  46. #include <linux/profile.h>
  47. #include <linux/bitops.h>
  48. #include <linux/list.h>
  49. #include <linux/radix-tree.h>
  50. #include <linux/mutex.h>
  51. #include <linux/bootmem.h>
  52. #include <linux/pci.h>
  53. #include <linux/debugfs.h>
  54. #include <linux/of.h>
  55. #include <linux/of_irq.h>
  56. #include <asm/uaccess.h>
  57. #include <asm/io.h>
  58. #include <asm/pgtable.h>
  59. #include <asm/irq.h>
  60. #include <asm/cache.h>
  61. #include <asm/prom.h>
  62. #include <asm/ptrace.h>
  63. #include <asm/machdep.h>
  64. #include <asm/udbg.h>
  65. #include <asm/smp.h>
  66. #include <asm/debug.h>
  67. #ifdef CONFIG_PPC64
  68. #include <asm/paca.h>
  69. #include <asm/firmware.h>
  70. #include <asm/lv1call.h>
  71. #endif
  72. #define CREATE_TRACE_POINTS
  73. #include <asm/trace.h>
  74. DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
  75. EXPORT_PER_CPU_SYMBOL(irq_stat);
  76. int __irq_offset_value;
  77. #ifdef CONFIG_PPC32
  78. EXPORT_SYMBOL(__irq_offset_value);
  79. atomic_t ppc_n_lost_interrupts;
  80. #ifdef CONFIG_TAU_INT
  81. extern int tau_initialized;
  82. extern int tau_interrupts(int);
  83. #endif
  84. #endif /* CONFIG_PPC32 */
  85. #ifdef CONFIG_PPC64
  86. int distribute_irqs = 1;
  87. static inline notrace unsigned long get_irq_happened(void)
  88. {
  89. unsigned long happened;
  90. __asm__ __volatile__("lbz %0,%1(13)"
  91. : "=r" (happened) : "i" (offsetof(struct paca_struct, irq_happened)));
  92. return happened;
  93. }
  94. static inline notrace void set_soft_enabled(unsigned long enable)
  95. {
  96. __asm__ __volatile__("stb %0,%1(13)"
  97. : : "r" (enable), "i" (offsetof(struct paca_struct, soft_enabled)));
  98. }
  99. static inline notrace int decrementer_check_overflow(void)
  100. {
  101. u64 now = get_tb_or_rtc();
  102. u64 *next_tb = &__get_cpu_var(decrementers_next_tb);
  103. if (now >= *next_tb)
  104. set_dec(1);
  105. return now >= *next_tb;
  106. }
  107. /* This is called whenever we are re-enabling interrupts
  108. * and returns either 0 (nothing to do) or 500/900 if there's
  109. * either an EE or a DEC to generate.
  110. *
  111. * This is called in two contexts: From arch_local_irq_restore()
  112. * before soft-enabling interrupts, and from the exception exit
  113. * path when returning from an interrupt from a soft-disabled to
  114. * a soft enabled context. In both case we have interrupts hard
  115. * disabled.
  116. *
  117. * We take care of only clearing the bits we handled in the
  118. * PACA irq_happened field since we can only re-emit one at a
  119. * time and we don't want to "lose" one.
  120. */
  121. notrace unsigned int __check_irq_replay(void)
  122. {
  123. /*
  124. * We use local_paca rather than get_paca() to avoid all
  125. * the debug_smp_processor_id() business in this low level
  126. * function
  127. */
  128. unsigned char happened = local_paca->irq_happened;
  129. /* Clear bit 0 which we wouldn't clear otherwise */
  130. local_paca->irq_happened &= ~PACA_IRQ_HARD_DIS;
  131. /*
  132. * Force the delivery of pending soft-disabled interrupts on PS3.
  133. * Any HV call will have this side effect.
  134. */
  135. if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
  136. u64 tmp, tmp2;
  137. lv1_get_version_info(&tmp, &tmp2);
  138. }
  139. /*
  140. * We may have missed a decrementer interrupt. We check the
  141. * decrementer itself rather than the paca irq_happened field
  142. * in case we also had a rollover while hard disabled
  143. */
  144. local_paca->irq_happened &= ~PACA_IRQ_DEC;
  145. if (decrementer_check_overflow())
  146. return 0x900;
  147. /* Finally check if an external interrupt happened */
  148. local_paca->irq_happened &= ~PACA_IRQ_EE;
  149. if (happened & PACA_IRQ_EE)
  150. return 0x500;
  151. #ifdef CONFIG_PPC_BOOK3E
  152. /* Finally check if an EPR external interrupt happened
  153. * this bit is typically set if we need to handle another
  154. * "edge" interrupt from within the MPIC "EPR" handler
  155. */
  156. local_paca->irq_happened &= ~PACA_IRQ_EE_EDGE;
  157. if (happened & PACA_IRQ_EE_EDGE)
  158. return 0x500;
  159. local_paca->irq_happened &= ~PACA_IRQ_DBELL;
  160. if (happened & PACA_IRQ_DBELL)
  161. return 0x280;
  162. #endif /* CONFIG_PPC_BOOK3E */
  163. /* There should be nothing left ! */
  164. BUG_ON(local_paca->irq_happened != 0);
  165. return 0;
  166. }
  167. notrace void arch_local_irq_restore(unsigned long en)
  168. {
  169. unsigned char irq_happened;
  170. unsigned int replay;
  171. /* Write the new soft-enabled value */
  172. set_soft_enabled(en);
  173. if (!en)
  174. return;
  175. /*
  176. * From this point onward, we can take interrupts, preempt,
  177. * etc... unless we got hard-disabled. We check if an event
  178. * happened. If none happened, we know we can just return.
  179. *
  180. * We may have preempted before the check below, in which case
  181. * we are checking the "new" CPU instead of the old one. This
  182. * is only a problem if an event happened on the "old" CPU.
  183. *
  184. * External interrupt events will have caused interrupts to
  185. * be hard-disabled, so there is no problem, we
  186. * cannot have preempted.
  187. */
  188. irq_happened = get_irq_happened();
  189. if (!irq_happened)
  190. return;
  191. /*
  192. * We need to hard disable to get a trusted value from
  193. * __check_irq_replay(). We also need to soft-disable
  194. * again to avoid warnings in there due to the use of
  195. * per-cpu variables.
  196. *
  197. * We know that if the value in irq_happened is exactly 0x01
  198. * then we are already hard disabled (there are other less
  199. * common cases that we'll ignore for now), so we skip the
  200. * (expensive) mtmsrd.
  201. */
  202. if (unlikely(irq_happened != PACA_IRQ_HARD_DIS))
  203. __hard_irq_disable();
  204. #ifdef CONFIG_TRACE_IRQFLAG
  205. else {
  206. /*
  207. * We should already be hard disabled here. We had bugs
  208. * where that wasn't the case so let's dbl check it and
  209. * warn if we are wrong. Only do that when IRQ tracing
  210. * is enabled as mfmsr() can be costly.
  211. */
  212. if (WARN_ON(mfmsr() & MSR_EE))
  213. __hard_irq_disable();
  214. }
  215. #endif /* CONFIG_TRACE_IRQFLAG */
  216. set_soft_enabled(0);
  217. /*
  218. * Check if anything needs to be re-emitted. We haven't
  219. * soft-enabled yet to avoid warnings in decrementer_check_overflow
  220. * accessing per-cpu variables
  221. */
  222. replay = __check_irq_replay();
  223. /* We can soft-enable now */
  224. set_soft_enabled(1);
  225. /*
  226. * And replay if we have to. This will return with interrupts
  227. * hard-enabled.
  228. */
  229. if (replay) {
  230. __replay_interrupt(replay);
  231. return;
  232. }
  233. /* Finally, let's ensure we are hard enabled */
  234. __hard_irq_enable();
  235. }
  236. EXPORT_SYMBOL(arch_local_irq_restore);
  237. /*
  238. * This is specifically called by assembly code to re-enable interrupts
  239. * if they are currently disabled. This is typically called before
  240. * schedule() or do_signal() when returning to userspace. We do it
  241. * in C to avoid the burden of dealing with lockdep etc...
  242. *
  243. * NOTE: This is called with interrupts hard disabled but not marked
  244. * as such in paca->irq_happened, so we need to resync this.
  245. */
  246. void restore_interrupts(void)
  247. {
  248. if (irqs_disabled()) {
  249. local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
  250. local_irq_enable();
  251. } else
  252. __hard_irq_enable();
  253. }
  254. #endif /* CONFIG_PPC64 */
  255. int arch_show_interrupts(struct seq_file *p, int prec)
  256. {
  257. int j;
  258. #if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT)
  259. if (tau_initialized) {
  260. seq_printf(p, "%*s: ", prec, "TAU");
  261. for_each_online_cpu(j)
  262. seq_printf(p, "%10u ", tau_interrupts(j));
  263. seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
  264. }
  265. #endif /* CONFIG_PPC32 && CONFIG_TAU_INT */
  266. seq_printf(p, "%*s: ", prec, "LOC");
  267. for_each_online_cpu(j)
  268. seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs);
  269. seq_printf(p, " Local timer interrupts\n");
  270. seq_printf(p, "%*s: ", prec, "SPU");
  271. for_each_online_cpu(j)
  272. seq_printf(p, "%10u ", per_cpu(irq_stat, j).spurious_irqs);
  273. seq_printf(p, " Spurious interrupts\n");
  274. seq_printf(p, "%*s: ", prec, "CNT");
  275. for_each_online_cpu(j)
  276. seq_printf(p, "%10u ", per_cpu(irq_stat, j).pmu_irqs);
  277. seq_printf(p, " Performance monitoring interrupts\n");
  278. seq_printf(p, "%*s: ", prec, "MCE");
  279. for_each_online_cpu(j)
  280. seq_printf(p, "%10u ", per_cpu(irq_stat, j).mce_exceptions);
  281. seq_printf(p, " Machine check exceptions\n");
  282. return 0;
  283. }
  284. /*
  285. * /proc/stat helpers
  286. */
  287. u64 arch_irq_stat_cpu(unsigned int cpu)
  288. {
  289. u64 sum = per_cpu(irq_stat, cpu).timer_irqs;
  290. sum += per_cpu(irq_stat, cpu).pmu_irqs;
  291. sum += per_cpu(irq_stat, cpu).mce_exceptions;
  292. sum += per_cpu(irq_stat, cpu).spurious_irqs;
  293. return sum;
  294. }
  295. #ifdef CONFIG_HOTPLUG_CPU
  296. void migrate_irqs(void)
  297. {
  298. struct irq_desc *desc;
  299. unsigned int irq;
  300. static int warned;
  301. cpumask_var_t mask;
  302. const struct cpumask *map = cpu_online_mask;
  303. alloc_cpumask_var(&mask, GFP_KERNEL);
  304. for_each_irq_desc(irq, desc) {
  305. struct irq_data *data;
  306. struct irq_chip *chip;
  307. data = irq_desc_get_irq_data(desc);
  308. if (irqd_is_per_cpu(data))
  309. continue;
  310. chip = irq_data_get_irq_chip(data);
  311. cpumask_and(mask, data->affinity, map);
  312. if (cpumask_any(mask) >= nr_cpu_ids) {
  313. printk("Breaking affinity for irq %i\n", irq);
  314. cpumask_copy(mask, map);
  315. }
  316. if (chip->irq_set_affinity)
  317. chip->irq_set_affinity(data, mask, true);
  318. else if (desc->action && !(warned++))
  319. printk("Cannot set affinity for irq %i\n", irq);
  320. }
  321. free_cpumask_var(mask);
  322. local_irq_enable();
  323. mdelay(1);
  324. local_irq_disable();
  325. }
  326. #endif
  327. static inline void handle_one_irq(unsigned int irq)
  328. {
  329. struct thread_info *curtp, *irqtp;
  330. unsigned long saved_sp_limit;
  331. struct irq_desc *desc;
  332. desc = irq_to_desc(irq);
  333. if (!desc)
  334. return;
  335. /* Switch to the irq stack to handle this */
  336. curtp = current_thread_info();
  337. irqtp = hardirq_ctx[smp_processor_id()];
  338. if (curtp == irqtp) {
  339. /* We're already on the irq stack, just handle it */
  340. desc->handle_irq(irq, desc);
  341. return;
  342. }
  343. saved_sp_limit = current->thread.ksp_limit;
  344. irqtp->task = curtp->task;
  345. irqtp->flags = 0;
  346. /* Copy the softirq bits in preempt_count so that the
  347. * softirq checks work in the hardirq context. */
  348. irqtp->preempt_count = (irqtp->preempt_count & ~SOFTIRQ_MASK) |
  349. (curtp->preempt_count & SOFTIRQ_MASK);
  350. current->thread.ksp_limit = (unsigned long)irqtp +
  351. _ALIGN_UP(sizeof(struct thread_info), 16);
  352. call_handle_irq(irq, desc, irqtp, desc->handle_irq);
  353. current->thread.ksp_limit = saved_sp_limit;
  354. irqtp->task = NULL;
  355. /* Set any flag that may have been set on the
  356. * alternate stack
  357. */
  358. if (irqtp->flags)
  359. set_bits(irqtp->flags, &curtp->flags);
  360. }
  361. static inline void check_stack_overflow(void)
  362. {
  363. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  364. long sp;
  365. sp = __get_SP() & (THREAD_SIZE-1);
  366. /* check for stack overflow: is there less than 2KB free? */
  367. if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
  368. printk("do_IRQ: stack overflow: %ld\n",
  369. sp - sizeof(struct thread_info));
  370. dump_stack();
  371. }
  372. #endif
  373. }
  374. void do_IRQ(struct pt_regs *regs)
  375. {
  376. struct pt_regs *old_regs = set_irq_regs(regs);
  377. unsigned int irq;
  378. trace_irq_entry(regs);
  379. irq_enter();
  380. check_stack_overflow();
  381. /*
  382. * Query the platform PIC for the interrupt & ack it.
  383. *
  384. * This will typically lower the interrupt line to the CPU
  385. */
  386. irq = ppc_md.get_irq();
  387. /* We can hard enable interrupts now */
  388. may_hard_irq_enable();
  389. /* And finally process it */
  390. if (irq != NO_IRQ)
  391. handle_one_irq(irq);
  392. else
  393. __get_cpu_var(irq_stat).spurious_irqs++;
  394. irq_exit();
  395. set_irq_regs(old_regs);
  396. trace_irq_exit(regs);
  397. }
  398. void __init init_IRQ(void)
  399. {
  400. if (ppc_md.init_IRQ)
  401. ppc_md.init_IRQ();
  402. exc_lvl_ctx_init();
  403. irq_ctx_init();
  404. }
  405. #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
  406. struct thread_info *critirq_ctx[NR_CPUS] __read_mostly;
  407. struct thread_info *dbgirq_ctx[NR_CPUS] __read_mostly;
  408. struct thread_info *mcheckirq_ctx[NR_CPUS] __read_mostly;
  409. void exc_lvl_ctx_init(void)
  410. {
  411. struct thread_info *tp;
  412. int i, cpu_nr;
  413. for_each_possible_cpu(i) {
  414. #ifdef CONFIG_PPC64
  415. cpu_nr = i;
  416. #else
  417. cpu_nr = get_hard_smp_processor_id(i);
  418. #endif
  419. memset((void *)critirq_ctx[cpu_nr], 0, THREAD_SIZE);
  420. tp = critirq_ctx[cpu_nr];
  421. tp->cpu = cpu_nr;
  422. tp->preempt_count = 0;
  423. #ifdef CONFIG_BOOKE
  424. memset((void *)dbgirq_ctx[cpu_nr], 0, THREAD_SIZE);
  425. tp = dbgirq_ctx[cpu_nr];
  426. tp->cpu = cpu_nr;
  427. tp->preempt_count = 0;
  428. memset((void *)mcheckirq_ctx[cpu_nr], 0, THREAD_SIZE);
  429. tp = mcheckirq_ctx[cpu_nr];
  430. tp->cpu = cpu_nr;
  431. tp->preempt_count = HARDIRQ_OFFSET;
  432. #endif
  433. }
  434. }
  435. #endif
  436. struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
  437. struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
  438. void irq_ctx_init(void)
  439. {
  440. struct thread_info *tp;
  441. int i;
  442. for_each_possible_cpu(i) {
  443. memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
  444. tp = softirq_ctx[i];
  445. tp->cpu = i;
  446. tp->preempt_count = 0;
  447. memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
  448. tp = hardirq_ctx[i];
  449. tp->cpu = i;
  450. tp->preempt_count = HARDIRQ_OFFSET;
  451. }
  452. }
  453. static inline void do_softirq_onstack(void)
  454. {
  455. struct thread_info *curtp, *irqtp;
  456. unsigned long saved_sp_limit = current->thread.ksp_limit;
  457. curtp = current_thread_info();
  458. irqtp = softirq_ctx[smp_processor_id()];
  459. irqtp->task = curtp->task;
  460. irqtp->flags = 0;
  461. current->thread.ksp_limit = (unsigned long)irqtp +
  462. _ALIGN_UP(sizeof(struct thread_info), 16);
  463. call_do_softirq(irqtp);
  464. current->thread.ksp_limit = saved_sp_limit;
  465. irqtp->task = NULL;
  466. /* Set any flag that may have been set on the
  467. * alternate stack
  468. */
  469. if (irqtp->flags)
  470. set_bits(irqtp->flags, &curtp->flags);
  471. }
  472. void do_softirq(void)
  473. {
  474. unsigned long flags;
  475. if (in_interrupt())
  476. return;
  477. local_irq_save(flags);
  478. if (local_softirq_pending())
  479. do_softirq_onstack();
  480. local_irq_restore(flags);
  481. }
  482. irq_hw_number_t virq_to_hw(unsigned int virq)
  483. {
  484. struct irq_data *irq_data = irq_get_irq_data(virq);
  485. return WARN_ON(!irq_data) ? 0 : irq_data->hwirq;
  486. }
  487. EXPORT_SYMBOL_GPL(virq_to_hw);
  488. #ifdef CONFIG_SMP
  489. int irq_choose_cpu(const struct cpumask *mask)
  490. {
  491. int cpuid;
  492. if (cpumask_equal(mask, cpu_online_mask)) {
  493. static int irq_rover;
  494. static DEFINE_RAW_SPINLOCK(irq_rover_lock);
  495. unsigned long flags;
  496. /* Round-robin distribution... */
  497. do_round_robin:
  498. raw_spin_lock_irqsave(&irq_rover_lock, flags);
  499. irq_rover = cpumask_next(irq_rover, cpu_online_mask);
  500. if (irq_rover >= nr_cpu_ids)
  501. irq_rover = cpumask_first(cpu_online_mask);
  502. cpuid = irq_rover;
  503. raw_spin_unlock_irqrestore(&irq_rover_lock, flags);
  504. } else {
  505. cpuid = cpumask_first_and(mask, cpu_online_mask);
  506. if (cpuid >= nr_cpu_ids)
  507. goto do_round_robin;
  508. }
  509. return get_hard_smp_processor_id(cpuid);
  510. }
  511. #else
  512. int irq_choose_cpu(const struct cpumask *mask)
  513. {
  514. return hard_smp_processor_id();
  515. }
  516. #endif
  517. int arch_early_irq_init(void)
  518. {
  519. return 0;
  520. }
  521. #ifdef CONFIG_PPC64
  522. static int __init setup_noirqdistrib(char *str)
  523. {
  524. distribute_irqs = 0;
  525. return 1;
  526. }
  527. __setup("noirqdistrib", setup_noirqdistrib);
  528. #endif /* CONFIG_PPC64 */