irq.c 28 KB

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