irq.c 27 KB

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