irq.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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/module.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/system.h>
  58. #include <asm/io.h>
  59. #include <asm/pgtable.h>
  60. #include <asm/irq.h>
  61. #include <asm/cache.h>
  62. #include <asm/prom.h>
  63. #include <asm/ptrace.h>
  64. #include <asm/machdep.h>
  65. #include <asm/udbg.h>
  66. #include <asm/dbell.h>
  67. #include <asm/smp.h>
  68. #ifdef CONFIG_PPC64
  69. #include <asm/paca.h>
  70. #include <asm/firmware.h>
  71. #include <asm/lv1call.h>
  72. #endif
  73. #define CREATE_TRACE_POINTS
  74. #include <asm/trace.h>
  75. DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
  76. EXPORT_PER_CPU_SYMBOL(irq_stat);
  77. int __irq_offset_value;
  78. #ifdef CONFIG_PPC32
  79. EXPORT_SYMBOL(__irq_offset_value);
  80. atomic_t ppc_n_lost_interrupts;
  81. #ifdef CONFIG_TAU_INT
  82. extern int tau_initialized;
  83. extern int tau_interrupts(int);
  84. #endif
  85. #endif /* CONFIG_PPC32 */
  86. #ifdef CONFIG_PPC64
  87. #ifndef CONFIG_SPARSE_IRQ
  88. EXPORT_SYMBOL(irq_desc);
  89. #endif
  90. int distribute_irqs = 1;
  91. static inline notrace unsigned long get_hard_enabled(void)
  92. {
  93. unsigned long enabled;
  94. __asm__ __volatile__("lbz %0,%1(13)"
  95. : "=r" (enabled) : "i" (offsetof(struct paca_struct, hard_enabled)));
  96. return enabled;
  97. }
  98. static inline notrace void set_soft_enabled(unsigned long enable)
  99. {
  100. __asm__ __volatile__("stb %0,%1(13)"
  101. : : "r" (enable), "i" (offsetof(struct paca_struct, soft_enabled)));
  102. }
  103. notrace void arch_local_irq_restore(unsigned long en)
  104. {
  105. /*
  106. * get_paca()->soft_enabled = en;
  107. * Is it ever valid to use local_irq_restore(0) when soft_enabled is 1?
  108. * That was allowed before, and in such a case we do need to take care
  109. * that gcc will set soft_enabled directly via r13, not choose to use
  110. * an intermediate register, lest we're preempted to a different cpu.
  111. */
  112. set_soft_enabled(en);
  113. if (!en)
  114. return;
  115. #ifdef CONFIG_PPC_STD_MMU_64
  116. if (firmware_has_feature(FW_FEATURE_ISERIES)) {
  117. /*
  118. * Do we need to disable preemption here? Not really: in the
  119. * unlikely event that we're preempted to a different cpu in
  120. * between getting r13, loading its lppaca_ptr, and loading
  121. * its any_int, we might call iseries_handle_interrupts without
  122. * an interrupt pending on the new cpu, but that's no disaster,
  123. * is it? And the business of preempting us off the old cpu
  124. * would itself involve a local_irq_restore which handles the
  125. * interrupt to that cpu.
  126. *
  127. * But use "local_paca->lppaca_ptr" instead of "get_lppaca()"
  128. * to avoid any preemption checking added into get_paca().
  129. */
  130. if (local_paca->lppaca_ptr->int_dword.any_int)
  131. iseries_handle_interrupts();
  132. }
  133. #endif /* CONFIG_PPC_STD_MMU_64 */
  134. /*
  135. * if (get_paca()->hard_enabled) return;
  136. * But again we need to take care that gcc gets hard_enabled directly
  137. * via r13, not choose to use an intermediate register, lest we're
  138. * preempted to a different cpu in between the two instructions.
  139. */
  140. if (get_hard_enabled())
  141. return;
  142. #if defined(CONFIG_BOOKE) && defined(CONFIG_SMP)
  143. /* Check for pending doorbell interrupts and resend to ourself */
  144. doorbell_check_self();
  145. #endif
  146. /*
  147. * Need to hard-enable interrupts here. Since currently disabled,
  148. * no need to take further asm precautions against preemption; but
  149. * use local_paca instead of get_paca() to avoid preemption checking.
  150. */
  151. local_paca->hard_enabled = en;
  152. #ifndef CONFIG_BOOKE
  153. /* On server, re-trigger the decrementer if it went negative since
  154. * some processors only trigger on edge transitions of the sign bit.
  155. *
  156. * BookE has a level sensitive decrementer (latches in TSR) so we
  157. * don't need that
  158. */
  159. if ((int)mfspr(SPRN_DEC) < 0)
  160. mtspr(SPRN_DEC, 1);
  161. #endif /* CONFIG_BOOKE */
  162. /*
  163. * Force the delivery of pending soft-disabled interrupts on PS3.
  164. * Any HV call will have this side effect.
  165. */
  166. if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
  167. u64 tmp;
  168. lv1_get_version_info(&tmp);
  169. }
  170. __hard_irq_enable();
  171. }
  172. EXPORT_SYMBOL(arch_local_irq_restore);
  173. #endif /* CONFIG_PPC64 */
  174. static int show_other_interrupts(struct seq_file *p, int prec)
  175. {
  176. int j;
  177. #if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT)
  178. if (tau_initialized) {
  179. seq_printf(p, "%*s: ", prec, "TAU");
  180. for_each_online_cpu(j)
  181. seq_printf(p, "%10u ", tau_interrupts(j));
  182. seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
  183. }
  184. #endif /* CONFIG_PPC32 && CONFIG_TAU_INT */
  185. seq_printf(p, "%*s: ", prec, "LOC");
  186. for_each_online_cpu(j)
  187. seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs);
  188. seq_printf(p, " Local timer interrupts\n");
  189. seq_printf(p, "%*s: ", prec, "SPU");
  190. for_each_online_cpu(j)
  191. seq_printf(p, "%10u ", per_cpu(irq_stat, j).spurious_irqs);
  192. seq_printf(p, " Spurious interrupts\n");
  193. seq_printf(p, "%*s: ", prec, "CNT");
  194. for_each_online_cpu(j)
  195. seq_printf(p, "%10u ", per_cpu(irq_stat, j).pmu_irqs);
  196. seq_printf(p, " Performance monitoring interrupts\n");
  197. seq_printf(p, "%*s: ", prec, "MCE");
  198. for_each_online_cpu(j)
  199. seq_printf(p, "%10u ", per_cpu(irq_stat, j).mce_exceptions);
  200. seq_printf(p, " Machine check exceptions\n");
  201. return 0;
  202. }
  203. int show_interrupts(struct seq_file *p, void *v)
  204. {
  205. unsigned long flags, any_count = 0;
  206. int i = *(loff_t *) v, j, prec;
  207. struct irqaction *action;
  208. struct irq_desc *desc;
  209. struct irq_chip *chip;
  210. if (i > nr_irqs)
  211. return 0;
  212. for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
  213. j *= 10;
  214. if (i == nr_irqs)
  215. return show_other_interrupts(p, prec);
  216. /* print header */
  217. if (i == 0) {
  218. seq_printf(p, "%*s", prec + 8, "");
  219. for_each_online_cpu(j)
  220. seq_printf(p, "CPU%-8d", j);
  221. seq_putc(p, '\n');
  222. }
  223. desc = irq_to_desc(i);
  224. if (!desc)
  225. return 0;
  226. raw_spin_lock_irqsave(&desc->lock, flags);
  227. for_each_online_cpu(j)
  228. any_count |= kstat_irqs_cpu(i, j);
  229. action = desc->action;
  230. if (!action && !any_count)
  231. goto out;
  232. seq_printf(p, "%*d: ", prec, i);
  233. for_each_online_cpu(j)
  234. seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
  235. chip = get_irq_desc_chip(desc);
  236. if (chip)
  237. seq_printf(p, " %-16s", chip->name);
  238. else
  239. seq_printf(p, " %-16s", "None");
  240. seq_printf(p, " %-8s", (desc->status & IRQ_LEVEL) ? "Level" : "Edge");
  241. if (action) {
  242. seq_printf(p, " %s", action->name);
  243. while ((action = action->next) != NULL)
  244. seq_printf(p, ", %s", action->name);
  245. }
  246. seq_putc(p, '\n');
  247. out:
  248. raw_spin_unlock_irqrestore(&desc->lock, flags);
  249. return 0;
  250. }
  251. /*
  252. * /proc/stat helpers
  253. */
  254. u64 arch_irq_stat_cpu(unsigned int cpu)
  255. {
  256. u64 sum = per_cpu(irq_stat, cpu).timer_irqs;
  257. sum += per_cpu(irq_stat, cpu).pmu_irqs;
  258. sum += per_cpu(irq_stat, cpu).mce_exceptions;
  259. sum += per_cpu(irq_stat, cpu).spurious_irqs;
  260. return sum;
  261. }
  262. #ifdef CONFIG_HOTPLUG_CPU
  263. void fixup_irqs(const struct cpumask *map)
  264. {
  265. struct irq_desc *desc;
  266. unsigned int irq;
  267. static int warned;
  268. cpumask_var_t mask;
  269. alloc_cpumask_var(&mask, GFP_KERNEL);
  270. for_each_irq(irq) {
  271. struct irq_chip *chip;
  272. desc = irq_to_desc(irq);
  273. if (!desc)
  274. continue;
  275. if (desc->status & IRQ_PER_CPU)
  276. continue;
  277. chip = get_irq_desc_chip(desc);
  278. cpumask_and(mask, desc->irq_data.affinity, map);
  279. if (cpumask_any(mask) >= nr_cpu_ids) {
  280. printk("Breaking affinity for irq %i\n", irq);
  281. cpumask_copy(mask, map);
  282. }
  283. if (chip->irq_set_affinity)
  284. chip->irq_set_affinity(&desc->irq_data, mask, true);
  285. else if (desc->action && !(warned++))
  286. printk("Cannot set affinity for irq %i\n", irq);
  287. }
  288. free_cpumask_var(mask);
  289. local_irq_enable();
  290. mdelay(1);
  291. local_irq_disable();
  292. }
  293. #endif
  294. static inline void handle_one_irq(unsigned int irq)
  295. {
  296. struct thread_info *curtp, *irqtp;
  297. unsigned long saved_sp_limit;
  298. struct irq_desc *desc;
  299. /* Switch to the irq stack to handle this */
  300. curtp = current_thread_info();
  301. irqtp = hardirq_ctx[smp_processor_id()];
  302. if (curtp == irqtp) {
  303. /* We're already on the irq stack, just handle it */
  304. generic_handle_irq(irq);
  305. return;
  306. }
  307. desc = irq_to_desc(irq);
  308. saved_sp_limit = current->thread.ksp_limit;
  309. irqtp->task = curtp->task;
  310. irqtp->flags = 0;
  311. /* Copy the softirq bits in preempt_count so that the
  312. * softirq checks work in the hardirq context. */
  313. irqtp->preempt_count = (irqtp->preempt_count & ~SOFTIRQ_MASK) |
  314. (curtp->preempt_count & SOFTIRQ_MASK);
  315. current->thread.ksp_limit = (unsigned long)irqtp +
  316. _ALIGN_UP(sizeof(struct thread_info), 16);
  317. call_handle_irq(irq, desc, irqtp, desc->handle_irq);
  318. current->thread.ksp_limit = saved_sp_limit;
  319. irqtp->task = NULL;
  320. /* Set any flag that may have been set on the
  321. * alternate stack
  322. */
  323. if (irqtp->flags)
  324. set_bits(irqtp->flags, &curtp->flags);
  325. }
  326. static inline void check_stack_overflow(void)
  327. {
  328. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  329. long sp;
  330. sp = __get_SP() & (THREAD_SIZE-1);
  331. /* check for stack overflow: is there less than 2KB free? */
  332. if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
  333. printk("do_IRQ: stack overflow: %ld\n",
  334. sp - sizeof(struct thread_info));
  335. dump_stack();
  336. }
  337. #endif
  338. }
  339. void do_IRQ(struct pt_regs *regs)
  340. {
  341. struct pt_regs *old_regs = set_irq_regs(regs);
  342. unsigned int irq;
  343. trace_irq_entry(regs);
  344. irq_enter();
  345. check_stack_overflow();
  346. irq = ppc_md.get_irq();
  347. if (irq != NO_IRQ && irq != NO_IRQ_IGNORE)
  348. handle_one_irq(irq);
  349. else if (irq != NO_IRQ_IGNORE)
  350. __get_cpu_var(irq_stat).spurious_irqs++;
  351. irq_exit();
  352. set_irq_regs(old_regs);
  353. #ifdef CONFIG_PPC_ISERIES
  354. if (firmware_has_feature(FW_FEATURE_ISERIES) &&
  355. get_lppaca()->int_dword.fields.decr_int) {
  356. get_lppaca()->int_dword.fields.decr_int = 0;
  357. /* Signal a fake decrementer interrupt */
  358. timer_interrupt(regs);
  359. }
  360. #endif
  361. trace_irq_exit(regs);
  362. }
  363. void __init init_IRQ(void)
  364. {
  365. if (ppc_md.init_IRQ)
  366. ppc_md.init_IRQ();
  367. exc_lvl_ctx_init();
  368. irq_ctx_init();
  369. }
  370. #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
  371. struct thread_info *critirq_ctx[NR_CPUS] __read_mostly;
  372. struct thread_info *dbgirq_ctx[NR_CPUS] __read_mostly;
  373. struct thread_info *mcheckirq_ctx[NR_CPUS] __read_mostly;
  374. void exc_lvl_ctx_init(void)
  375. {
  376. struct thread_info *tp;
  377. int i, hw_cpu;
  378. for_each_possible_cpu(i) {
  379. hw_cpu = get_hard_smp_processor_id(i);
  380. memset((void *)critirq_ctx[hw_cpu], 0, THREAD_SIZE);
  381. tp = critirq_ctx[hw_cpu];
  382. tp->cpu = i;
  383. tp->preempt_count = 0;
  384. #ifdef CONFIG_BOOKE
  385. memset((void *)dbgirq_ctx[hw_cpu], 0, THREAD_SIZE);
  386. tp = dbgirq_ctx[hw_cpu];
  387. tp->cpu = i;
  388. tp->preempt_count = 0;
  389. memset((void *)mcheckirq_ctx[hw_cpu], 0, THREAD_SIZE);
  390. tp = mcheckirq_ctx[hw_cpu];
  391. tp->cpu = i;
  392. tp->preempt_count = HARDIRQ_OFFSET;
  393. #endif
  394. }
  395. }
  396. #endif
  397. struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
  398. struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
  399. void irq_ctx_init(void)
  400. {
  401. struct thread_info *tp;
  402. int i;
  403. for_each_possible_cpu(i) {
  404. memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
  405. tp = softirq_ctx[i];
  406. tp->cpu = i;
  407. tp->preempt_count = 0;
  408. memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
  409. tp = hardirq_ctx[i];
  410. tp->cpu = i;
  411. tp->preempt_count = HARDIRQ_OFFSET;
  412. }
  413. }
  414. static inline void do_softirq_onstack(void)
  415. {
  416. struct thread_info *curtp, *irqtp;
  417. unsigned long saved_sp_limit = current->thread.ksp_limit;
  418. curtp = current_thread_info();
  419. irqtp = softirq_ctx[smp_processor_id()];
  420. irqtp->task = curtp->task;
  421. current->thread.ksp_limit = (unsigned long)irqtp +
  422. _ALIGN_UP(sizeof(struct thread_info), 16);
  423. call_do_softirq(irqtp);
  424. current->thread.ksp_limit = saved_sp_limit;
  425. irqtp->task = NULL;
  426. }
  427. void do_softirq(void)
  428. {
  429. unsigned long flags;
  430. if (in_interrupt())
  431. return;
  432. local_irq_save(flags);
  433. if (local_softirq_pending())
  434. do_softirq_onstack();
  435. local_irq_restore(flags);
  436. }
  437. /*
  438. * IRQ controller and virtual interrupts
  439. */
  440. static LIST_HEAD(irq_hosts);
  441. static DEFINE_RAW_SPINLOCK(irq_big_lock);
  442. static unsigned int revmap_trees_allocated;
  443. static DEFINE_MUTEX(revmap_trees_mutex);
  444. struct irq_map_entry irq_map[NR_IRQS];
  445. static unsigned int irq_virq_count = NR_IRQS;
  446. static struct irq_host *irq_default_host;
  447. irq_hw_number_t virq_to_hw(unsigned int virq)
  448. {
  449. return irq_map[virq].hwirq;
  450. }
  451. EXPORT_SYMBOL_GPL(virq_to_hw);
  452. static int default_irq_host_match(struct irq_host *h, struct device_node *np)
  453. {
  454. return h->of_node != NULL && h->of_node == np;
  455. }
  456. struct irq_host *irq_alloc_host(struct device_node *of_node,
  457. unsigned int revmap_type,
  458. unsigned int revmap_arg,
  459. struct irq_host_ops *ops,
  460. irq_hw_number_t inval_irq)
  461. {
  462. struct irq_host *host;
  463. unsigned int size = sizeof(struct irq_host);
  464. unsigned int i;
  465. unsigned int *rmap;
  466. unsigned long flags;
  467. /* Allocate structure and revmap table if using linear mapping */
  468. if (revmap_type == IRQ_HOST_MAP_LINEAR)
  469. size += revmap_arg * sizeof(unsigned int);
  470. host = zalloc_maybe_bootmem(size, GFP_KERNEL);
  471. if (host == NULL)
  472. return NULL;
  473. /* Fill structure */
  474. host->revmap_type = revmap_type;
  475. host->inval_irq = inval_irq;
  476. host->ops = ops;
  477. host->of_node = of_node_get(of_node);
  478. if (host->ops->match == NULL)
  479. host->ops->match = default_irq_host_match;
  480. raw_spin_lock_irqsave(&irq_big_lock, flags);
  481. /* If it's a legacy controller, check for duplicates and
  482. * mark it as allocated (we use irq 0 host pointer for that
  483. */
  484. if (revmap_type == IRQ_HOST_MAP_LEGACY) {
  485. if (irq_map[0].host != NULL) {
  486. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  487. /* If we are early boot, we can't free the structure,
  488. * too bad...
  489. * this will be fixed once slab is made available early
  490. * instead of the current cruft
  491. */
  492. if (mem_init_done) {
  493. of_node_put(host->of_node);
  494. kfree(host);
  495. }
  496. return NULL;
  497. }
  498. irq_map[0].host = host;
  499. }
  500. list_add(&host->link, &irq_hosts);
  501. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  502. /* Additional setups per revmap type */
  503. switch(revmap_type) {
  504. case IRQ_HOST_MAP_LEGACY:
  505. /* 0 is always the invalid number for legacy */
  506. host->inval_irq = 0;
  507. /* setup us as the host for all legacy interrupts */
  508. for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
  509. irq_map[i].hwirq = i;
  510. smp_wmb();
  511. irq_map[i].host = host;
  512. smp_wmb();
  513. /* Clear norequest flags */
  514. irq_to_desc(i)->status &= ~IRQ_NOREQUEST;
  515. /* Legacy flags are left to default at this point,
  516. * one can then use irq_create_mapping() to
  517. * explicitly change them
  518. */
  519. ops->map(host, i, i);
  520. }
  521. break;
  522. case IRQ_HOST_MAP_LINEAR:
  523. rmap = (unsigned int *)(host + 1);
  524. for (i = 0; i < revmap_arg; i++)
  525. rmap[i] = NO_IRQ;
  526. host->revmap_data.linear.size = revmap_arg;
  527. smp_wmb();
  528. host->revmap_data.linear.revmap = rmap;
  529. break;
  530. default:
  531. break;
  532. }
  533. pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
  534. return host;
  535. }
  536. struct irq_host *irq_find_host(struct device_node *node)
  537. {
  538. struct irq_host *h, *found = NULL;
  539. unsigned long flags;
  540. /* We might want to match the legacy controller last since
  541. * it might potentially be set to match all interrupts in
  542. * the absence of a device node. This isn't a problem so far
  543. * yet though...
  544. */
  545. raw_spin_lock_irqsave(&irq_big_lock, flags);
  546. list_for_each_entry(h, &irq_hosts, link)
  547. if (h->ops->match(h, node)) {
  548. found = h;
  549. break;
  550. }
  551. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  552. return found;
  553. }
  554. EXPORT_SYMBOL_GPL(irq_find_host);
  555. void irq_set_default_host(struct irq_host *host)
  556. {
  557. pr_debug("irq: Default host set to @0x%p\n", host);
  558. irq_default_host = host;
  559. }
  560. void irq_set_virq_count(unsigned int count)
  561. {
  562. pr_debug("irq: Trying to set virq count to %d\n", count);
  563. BUG_ON(count < NUM_ISA_INTERRUPTS);
  564. if (count < NR_IRQS)
  565. irq_virq_count = count;
  566. }
  567. static int irq_setup_virq(struct irq_host *host, unsigned int virq,
  568. irq_hw_number_t hwirq)
  569. {
  570. int res;
  571. res = irq_alloc_desc_at(virq, 0);
  572. if (res != virq) {
  573. pr_debug("irq: -> allocating desc failed\n");
  574. goto error;
  575. }
  576. irq_clear_status_flags(virq, IRQ_NOREQUEST);
  577. /* map it */
  578. smp_wmb();
  579. irq_map[virq].hwirq = hwirq;
  580. smp_mb();
  581. if (host->ops->map(host, virq, hwirq)) {
  582. pr_debug("irq: -> mapping failed, freeing\n");
  583. goto errdesc;
  584. }
  585. return 0;
  586. errdesc:
  587. irq_free_descs(virq, 1);
  588. error:
  589. irq_free_virt(virq, 1);
  590. return -1;
  591. }
  592. unsigned int irq_create_direct_mapping(struct irq_host *host)
  593. {
  594. unsigned int virq;
  595. if (host == NULL)
  596. host = irq_default_host;
  597. BUG_ON(host == NULL);
  598. WARN_ON(host->revmap_type != IRQ_HOST_MAP_NOMAP);
  599. virq = irq_alloc_virt(host, 1, 0);
  600. if (virq == NO_IRQ) {
  601. pr_debug("irq: create_direct virq allocation failed\n");
  602. return NO_IRQ;
  603. }
  604. pr_debug("irq: create_direct obtained virq %d\n", virq);
  605. if (irq_setup_virq(host, virq, virq))
  606. return NO_IRQ;
  607. return virq;
  608. }
  609. unsigned int irq_create_mapping(struct irq_host *host,
  610. irq_hw_number_t hwirq)
  611. {
  612. unsigned int virq, hint;
  613. pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
  614. /* Look for default host if nececssary */
  615. if (host == NULL)
  616. host = irq_default_host;
  617. if (host == NULL) {
  618. printk(KERN_WARNING "irq_create_mapping called for"
  619. " NULL host, hwirq=%lx\n", hwirq);
  620. WARN_ON(1);
  621. return NO_IRQ;
  622. }
  623. pr_debug("irq: -> using host @%p\n", host);
  624. /* Check if mapping already exist, if it does, call
  625. * host->ops->map() to update the flags
  626. */
  627. virq = irq_find_mapping(host, hwirq);
  628. if (virq != NO_IRQ) {
  629. if (host->ops->remap)
  630. host->ops->remap(host, virq, hwirq);
  631. pr_debug("irq: -> existing mapping on virq %d\n", virq);
  632. return virq;
  633. }
  634. /* Get a virtual interrupt number */
  635. if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
  636. /* Handle legacy */
  637. virq = (unsigned int)hwirq;
  638. if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
  639. return NO_IRQ;
  640. return virq;
  641. } else {
  642. /* Allocate a virtual interrupt number */
  643. hint = hwirq % irq_virq_count;
  644. virq = irq_alloc_virt(host, 1, hint);
  645. if (virq == NO_IRQ) {
  646. pr_debug("irq: -> virq allocation failed\n");
  647. return NO_IRQ;
  648. }
  649. }
  650. if (irq_setup_virq(host, virq, hwirq))
  651. return NO_IRQ;
  652. printk(KERN_DEBUG "irq: irq %lu on host %s mapped to virtual irq %u\n",
  653. hwirq, host->of_node ? host->of_node->full_name : "null", virq);
  654. return virq;
  655. }
  656. EXPORT_SYMBOL_GPL(irq_create_mapping);
  657. unsigned int irq_create_of_mapping(struct device_node *controller,
  658. const u32 *intspec, unsigned int intsize)
  659. {
  660. struct irq_host *host;
  661. irq_hw_number_t hwirq;
  662. unsigned int type = IRQ_TYPE_NONE;
  663. unsigned int virq;
  664. if (controller == NULL)
  665. host = irq_default_host;
  666. else
  667. host = irq_find_host(controller);
  668. if (host == NULL) {
  669. printk(KERN_WARNING "irq: no irq host found for %s !\n",
  670. controller->full_name);
  671. return NO_IRQ;
  672. }
  673. /* If host has no translation, then we assume interrupt line */
  674. if (host->ops->xlate == NULL)
  675. hwirq = intspec[0];
  676. else {
  677. if (host->ops->xlate(host, controller, intspec, intsize,
  678. &hwirq, &type))
  679. return NO_IRQ;
  680. }
  681. /* Create mapping */
  682. virq = irq_create_mapping(host, hwirq);
  683. if (virq == NO_IRQ)
  684. return virq;
  685. /* Set type if specified and different than the current one */
  686. if (type != IRQ_TYPE_NONE &&
  687. type != (irq_to_desc(virq)->status & IRQF_TRIGGER_MASK))
  688. set_irq_type(virq, type);
  689. return virq;
  690. }
  691. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  692. void irq_dispose_mapping(unsigned int virq)
  693. {
  694. struct irq_host *host;
  695. irq_hw_number_t hwirq;
  696. if (virq == NO_IRQ)
  697. return;
  698. host = irq_map[virq].host;
  699. WARN_ON (host == NULL);
  700. if (host == NULL)
  701. return;
  702. /* Never unmap legacy interrupts */
  703. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  704. return;
  705. /* remove chip and handler */
  706. set_irq_chip_and_handler(virq, NULL, NULL);
  707. /* Make sure it's completed */
  708. synchronize_irq(virq);
  709. /* Tell the PIC about it */
  710. if (host->ops->unmap)
  711. host->ops->unmap(host, virq);
  712. smp_mb();
  713. /* Clear reverse map */
  714. hwirq = irq_map[virq].hwirq;
  715. switch(host->revmap_type) {
  716. case IRQ_HOST_MAP_LINEAR:
  717. if (hwirq < host->revmap_data.linear.size)
  718. host->revmap_data.linear.revmap[hwirq] = NO_IRQ;
  719. break;
  720. case IRQ_HOST_MAP_TREE:
  721. /*
  722. * Check if radix tree allocated yet, if not then nothing to
  723. * remove.
  724. */
  725. smp_rmb();
  726. if (revmap_trees_allocated < 1)
  727. break;
  728. mutex_lock(&revmap_trees_mutex);
  729. radix_tree_delete(&host->revmap_data.tree, hwirq);
  730. mutex_unlock(&revmap_trees_mutex);
  731. break;
  732. }
  733. /* Destroy map */
  734. smp_mb();
  735. irq_map[virq].hwirq = host->inval_irq;
  736. irq_set_status_flags(virq, IRQ_NOREQUEST);
  737. irq_free_descs(virq, 1);
  738. /* Free it */
  739. irq_free_virt(virq, 1);
  740. }
  741. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  742. unsigned int irq_find_mapping(struct irq_host *host,
  743. irq_hw_number_t hwirq)
  744. {
  745. unsigned int i;
  746. unsigned int hint = hwirq % irq_virq_count;
  747. /* Look for default host if nececssary */
  748. if (host == NULL)
  749. host = irq_default_host;
  750. if (host == NULL)
  751. return NO_IRQ;
  752. /* legacy -> bail early */
  753. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  754. return hwirq;
  755. /* Slow path does a linear search of the map */
  756. if (hint < NUM_ISA_INTERRUPTS)
  757. hint = NUM_ISA_INTERRUPTS;
  758. i = hint;
  759. do {
  760. if (irq_map[i].host == host &&
  761. irq_map[i].hwirq == hwirq)
  762. return i;
  763. i++;
  764. if (i >= irq_virq_count)
  765. i = NUM_ISA_INTERRUPTS;
  766. } while(i != hint);
  767. return NO_IRQ;
  768. }
  769. EXPORT_SYMBOL_GPL(irq_find_mapping);
  770. unsigned int irq_radix_revmap_lookup(struct irq_host *host,
  771. irq_hw_number_t hwirq)
  772. {
  773. struct irq_map_entry *ptr;
  774. unsigned int virq;
  775. WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
  776. /*
  777. * Check if the radix tree exists and has bee initialized.
  778. * If not, we fallback to slow mode
  779. */
  780. if (revmap_trees_allocated < 2)
  781. return irq_find_mapping(host, hwirq);
  782. /* Now try to resolve */
  783. /*
  784. * No rcu_read_lock(ing) needed, the ptr returned can't go under us
  785. * as it's referencing an entry in the static irq_map table.
  786. */
  787. ptr = radix_tree_lookup(&host->revmap_data.tree, hwirq);
  788. /*
  789. * If found in radix tree, then fine.
  790. * Else fallback to linear lookup - this should not happen in practice
  791. * as it means that we failed to insert the node in the radix tree.
  792. */
  793. if (ptr)
  794. virq = ptr - irq_map;
  795. else
  796. virq = irq_find_mapping(host, hwirq);
  797. return virq;
  798. }
  799. void irq_radix_revmap_insert(struct irq_host *host, unsigned int virq,
  800. irq_hw_number_t hwirq)
  801. {
  802. WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
  803. /*
  804. * Check if the radix tree exists yet.
  805. * If not, then the irq will be inserted into the tree when it gets
  806. * initialized.
  807. */
  808. smp_rmb();
  809. if (revmap_trees_allocated < 1)
  810. return;
  811. if (virq != NO_IRQ) {
  812. mutex_lock(&revmap_trees_mutex);
  813. radix_tree_insert(&host->revmap_data.tree, hwirq,
  814. &irq_map[virq]);
  815. mutex_unlock(&revmap_trees_mutex);
  816. }
  817. }
  818. unsigned int irq_linear_revmap(struct irq_host *host,
  819. irq_hw_number_t hwirq)
  820. {
  821. unsigned int *revmap;
  822. WARN_ON(host->revmap_type != IRQ_HOST_MAP_LINEAR);
  823. /* Check revmap bounds */
  824. if (unlikely(hwirq >= host->revmap_data.linear.size))
  825. return irq_find_mapping(host, hwirq);
  826. /* Check if revmap was allocated */
  827. revmap = host->revmap_data.linear.revmap;
  828. if (unlikely(revmap == NULL))
  829. return irq_find_mapping(host, hwirq);
  830. /* Fill up revmap with slow path if no mapping found */
  831. if (unlikely(revmap[hwirq] == NO_IRQ))
  832. revmap[hwirq] = irq_find_mapping(host, hwirq);
  833. return revmap[hwirq];
  834. }
  835. unsigned int irq_alloc_virt(struct irq_host *host,
  836. unsigned int count,
  837. unsigned int hint)
  838. {
  839. unsigned long flags;
  840. unsigned int i, j, found = NO_IRQ;
  841. if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
  842. return NO_IRQ;
  843. raw_spin_lock_irqsave(&irq_big_lock, flags);
  844. /* Use hint for 1 interrupt if any */
  845. if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
  846. hint < irq_virq_count && irq_map[hint].host == NULL) {
  847. found = hint;
  848. goto hint_found;
  849. }
  850. /* Look for count consecutive numbers in the allocatable
  851. * (non-legacy) space
  852. */
  853. for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
  854. if (irq_map[i].host != NULL)
  855. j = 0;
  856. else
  857. j++;
  858. if (j == count) {
  859. found = i - count + 1;
  860. break;
  861. }
  862. }
  863. if (found == NO_IRQ) {
  864. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  865. return NO_IRQ;
  866. }
  867. hint_found:
  868. for (i = found; i < (found + count); i++) {
  869. irq_map[i].hwirq = host->inval_irq;
  870. smp_wmb();
  871. irq_map[i].host = host;
  872. }
  873. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  874. return found;
  875. }
  876. void irq_free_virt(unsigned int virq, unsigned int count)
  877. {
  878. unsigned long flags;
  879. unsigned int i;
  880. WARN_ON (virq < NUM_ISA_INTERRUPTS);
  881. WARN_ON (count == 0 || (virq + count) > irq_virq_count);
  882. raw_spin_lock_irqsave(&irq_big_lock, flags);
  883. for (i = virq; i < (virq + count); i++) {
  884. struct irq_host *host;
  885. if (i < NUM_ISA_INTERRUPTS ||
  886. (virq + count) > irq_virq_count)
  887. continue;
  888. host = irq_map[i].host;
  889. irq_map[i].hwirq = host->inval_irq;
  890. smp_wmb();
  891. irq_map[i].host = NULL;
  892. }
  893. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  894. }
  895. int arch_early_irq_init(void)
  896. {
  897. return 0;
  898. }
  899. /* We need to create the radix trees late */
  900. static int irq_late_init(void)
  901. {
  902. struct irq_host *h;
  903. unsigned int i;
  904. /*
  905. * No mutual exclusion with respect to accessors of the tree is needed
  906. * here as the synchronization is done via the state variable
  907. * revmap_trees_allocated.
  908. */
  909. list_for_each_entry(h, &irq_hosts, link) {
  910. if (h->revmap_type == IRQ_HOST_MAP_TREE)
  911. INIT_RADIX_TREE(&h->revmap_data.tree, GFP_KERNEL);
  912. }
  913. /*
  914. * Make sure the radix trees inits are visible before setting
  915. * the flag
  916. */
  917. smp_wmb();
  918. revmap_trees_allocated = 1;
  919. /*
  920. * Insert the reverse mapping for those interrupts already present
  921. * in irq_map[].
  922. */
  923. mutex_lock(&revmap_trees_mutex);
  924. for (i = 0; i < irq_virq_count; i++) {
  925. if (irq_map[i].host &&
  926. (irq_map[i].host->revmap_type == IRQ_HOST_MAP_TREE))
  927. radix_tree_insert(&irq_map[i].host->revmap_data.tree,
  928. irq_map[i].hwirq, &irq_map[i]);
  929. }
  930. mutex_unlock(&revmap_trees_mutex);
  931. /*
  932. * Make sure the radix trees insertions are visible before setting
  933. * the flag
  934. */
  935. smp_wmb();
  936. revmap_trees_allocated = 2;
  937. return 0;
  938. }
  939. arch_initcall(irq_late_init);
  940. #ifdef CONFIG_VIRQ_DEBUG
  941. static int virq_debug_show(struct seq_file *m, void *private)
  942. {
  943. unsigned long flags;
  944. struct irq_desc *desc;
  945. const char *p;
  946. static const char none[] = "none";
  947. int i;
  948. seq_printf(m, "%-5s %-7s %-15s %s\n", "virq", "hwirq",
  949. "chip name", "host name");
  950. for (i = 1; i < nr_irqs; i++) {
  951. desc = irq_to_desc(i);
  952. if (!desc)
  953. continue;
  954. raw_spin_lock_irqsave(&desc->lock, flags);
  955. if (desc->action && desc->action->handler) {
  956. struct irq_chip *chip;
  957. seq_printf(m, "%5d ", i);
  958. seq_printf(m, "0x%05lx ", virq_to_hw(i));
  959. chip = get_irq_desc_chip(desc);
  960. if (chip && chip->name)
  961. p = chip->name;
  962. else
  963. p = none;
  964. seq_printf(m, "%-15s ", p);
  965. if (irq_map[i].host && irq_map[i].host->of_node)
  966. p = irq_map[i].host->of_node->full_name;
  967. else
  968. p = none;
  969. seq_printf(m, "%s\n", p);
  970. }
  971. raw_spin_unlock_irqrestore(&desc->lock, flags);
  972. }
  973. return 0;
  974. }
  975. static int virq_debug_open(struct inode *inode, struct file *file)
  976. {
  977. return single_open(file, virq_debug_show, inode->i_private);
  978. }
  979. static const struct file_operations virq_debug_fops = {
  980. .open = virq_debug_open,
  981. .read = seq_read,
  982. .llseek = seq_lseek,
  983. .release = single_release,
  984. };
  985. static int __init irq_debugfs_init(void)
  986. {
  987. if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
  988. NULL, &virq_debug_fops) == NULL)
  989. return -ENOMEM;
  990. return 0;
  991. }
  992. __initcall(irq_debugfs_init);
  993. #endif /* CONFIG_VIRQ_DEBUG */
  994. #ifdef CONFIG_PPC64
  995. static int __init setup_noirqdistrib(char *str)
  996. {
  997. distribute_irqs = 0;
  998. return 1;
  999. }
  1000. __setup("noirqdistrib", setup_noirqdistrib);
  1001. #endif /* CONFIG_PPC64 */