irq.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. /*
  142. * Need to hard-enable interrupts here. Since currently disabled,
  143. * no need to take further asm precautions against preemption; but
  144. * use local_paca instead of get_paca() to avoid preemption checking.
  145. */
  146. local_paca->hard_enabled = en;
  147. #ifndef CONFIG_BOOKE
  148. /* On server, re-trigger the decrementer if it went negative since
  149. * some processors only trigger on edge transitions of the sign bit.
  150. *
  151. * BookE has a level sensitive decrementer (latches in TSR) so we
  152. * don't need that
  153. */
  154. if ((int)mfspr(SPRN_DEC) < 0)
  155. mtspr(SPRN_DEC, 1);
  156. #endif /* CONFIG_BOOKE */
  157. /*
  158. * Force the delivery of pending soft-disabled interrupts on PS3.
  159. * Any HV call will have this side effect.
  160. */
  161. if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
  162. u64 tmp;
  163. lv1_get_version_info(&tmp);
  164. }
  165. __hard_irq_enable();
  166. }
  167. EXPORT_SYMBOL(arch_local_irq_restore);
  168. #endif /* CONFIG_PPC64 */
  169. int arch_show_interrupts(struct seq_file *p, int prec)
  170. {
  171. int j;
  172. #if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT)
  173. if (tau_initialized) {
  174. seq_printf(p, "%*s: ", prec, "TAU");
  175. for_each_online_cpu(j)
  176. seq_printf(p, "%10u ", tau_interrupts(j));
  177. seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
  178. }
  179. #endif /* CONFIG_PPC32 && CONFIG_TAU_INT */
  180. seq_printf(p, "%*s: ", prec, "LOC");
  181. for_each_online_cpu(j)
  182. seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs);
  183. seq_printf(p, " Local timer interrupts\n");
  184. seq_printf(p, "%*s: ", prec, "SPU");
  185. for_each_online_cpu(j)
  186. seq_printf(p, "%10u ", per_cpu(irq_stat, j).spurious_irqs);
  187. seq_printf(p, " Spurious interrupts\n");
  188. seq_printf(p, "%*s: ", prec, "CNT");
  189. for_each_online_cpu(j)
  190. seq_printf(p, "%10u ", per_cpu(irq_stat, j).pmu_irqs);
  191. seq_printf(p, " Performance monitoring interrupts\n");
  192. seq_printf(p, "%*s: ", prec, "MCE");
  193. for_each_online_cpu(j)
  194. seq_printf(p, "%10u ", per_cpu(irq_stat, j).mce_exceptions);
  195. seq_printf(p, " Machine check exceptions\n");
  196. return 0;
  197. }
  198. /*
  199. * /proc/stat helpers
  200. */
  201. u64 arch_irq_stat_cpu(unsigned int cpu)
  202. {
  203. u64 sum = per_cpu(irq_stat, cpu).timer_irqs;
  204. sum += per_cpu(irq_stat, cpu).pmu_irqs;
  205. sum += per_cpu(irq_stat, cpu).mce_exceptions;
  206. sum += per_cpu(irq_stat, cpu).spurious_irqs;
  207. return sum;
  208. }
  209. #ifdef CONFIG_HOTPLUG_CPU
  210. void migrate_irqs(void)
  211. {
  212. struct irq_desc *desc;
  213. unsigned int irq;
  214. static int warned;
  215. cpumask_var_t mask;
  216. const struct cpumask *map = cpu_online_mask;
  217. alloc_cpumask_var(&mask, GFP_KERNEL);
  218. for_each_irq(irq) {
  219. struct irq_data *data;
  220. struct irq_chip *chip;
  221. desc = irq_to_desc(irq);
  222. if (!desc)
  223. continue;
  224. data = irq_desc_get_irq_data(desc);
  225. if (irqd_is_per_cpu(data))
  226. continue;
  227. chip = irq_data_get_irq_chip(data);
  228. cpumask_and(mask, data->affinity, map);
  229. if (cpumask_any(mask) >= nr_cpu_ids) {
  230. printk("Breaking affinity for irq %i\n", irq);
  231. cpumask_copy(mask, map);
  232. }
  233. if (chip->irq_set_affinity)
  234. chip->irq_set_affinity(data, mask, true);
  235. else if (desc->action && !(warned++))
  236. printk("Cannot set affinity for irq %i\n", irq);
  237. }
  238. free_cpumask_var(mask);
  239. local_irq_enable();
  240. mdelay(1);
  241. local_irq_disable();
  242. }
  243. #endif
  244. static inline void handle_one_irq(unsigned int irq)
  245. {
  246. struct thread_info *curtp, *irqtp;
  247. unsigned long saved_sp_limit;
  248. struct irq_desc *desc;
  249. desc = irq_to_desc(irq);
  250. if (!desc)
  251. return;
  252. /* Switch to the irq stack to handle this */
  253. curtp = current_thread_info();
  254. irqtp = hardirq_ctx[smp_processor_id()];
  255. if (curtp == irqtp) {
  256. /* We're already on the irq stack, just handle it */
  257. desc->handle_irq(irq, desc);
  258. return;
  259. }
  260. saved_sp_limit = current->thread.ksp_limit;
  261. irqtp->task = curtp->task;
  262. irqtp->flags = 0;
  263. /* Copy the softirq bits in preempt_count so that the
  264. * softirq checks work in the hardirq context. */
  265. irqtp->preempt_count = (irqtp->preempt_count & ~SOFTIRQ_MASK) |
  266. (curtp->preempt_count & SOFTIRQ_MASK);
  267. current->thread.ksp_limit = (unsigned long)irqtp +
  268. _ALIGN_UP(sizeof(struct thread_info), 16);
  269. call_handle_irq(irq, desc, irqtp, desc->handle_irq);
  270. current->thread.ksp_limit = saved_sp_limit;
  271. irqtp->task = NULL;
  272. /* Set any flag that may have been set on the
  273. * alternate stack
  274. */
  275. if (irqtp->flags)
  276. set_bits(irqtp->flags, &curtp->flags);
  277. }
  278. static inline void check_stack_overflow(void)
  279. {
  280. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  281. long sp;
  282. sp = __get_SP() & (THREAD_SIZE-1);
  283. /* check for stack overflow: is there less than 2KB free? */
  284. if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
  285. printk("do_IRQ: stack overflow: %ld\n",
  286. sp - sizeof(struct thread_info));
  287. dump_stack();
  288. }
  289. #endif
  290. }
  291. void do_IRQ(struct pt_regs *regs)
  292. {
  293. struct pt_regs *old_regs = set_irq_regs(regs);
  294. unsigned int irq;
  295. trace_irq_entry(regs);
  296. irq_enter();
  297. check_stack_overflow();
  298. irq = ppc_md.get_irq();
  299. if (irq != NO_IRQ && irq != NO_IRQ_IGNORE)
  300. handle_one_irq(irq);
  301. else if (irq != NO_IRQ_IGNORE)
  302. __get_cpu_var(irq_stat).spurious_irqs++;
  303. irq_exit();
  304. set_irq_regs(old_regs);
  305. #ifdef CONFIG_PPC_ISERIES
  306. if (firmware_has_feature(FW_FEATURE_ISERIES) &&
  307. get_lppaca()->int_dword.fields.decr_int) {
  308. get_lppaca()->int_dword.fields.decr_int = 0;
  309. /* Signal a fake decrementer interrupt */
  310. timer_interrupt(regs);
  311. }
  312. #endif
  313. trace_irq_exit(regs);
  314. }
  315. void __init init_IRQ(void)
  316. {
  317. if (ppc_md.init_IRQ)
  318. ppc_md.init_IRQ();
  319. exc_lvl_ctx_init();
  320. irq_ctx_init();
  321. }
  322. #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
  323. struct thread_info *critirq_ctx[NR_CPUS] __read_mostly;
  324. struct thread_info *dbgirq_ctx[NR_CPUS] __read_mostly;
  325. struct thread_info *mcheckirq_ctx[NR_CPUS] __read_mostly;
  326. void exc_lvl_ctx_init(void)
  327. {
  328. struct thread_info *tp;
  329. int i, cpu_nr;
  330. for_each_possible_cpu(i) {
  331. #ifdef CONFIG_PPC64
  332. cpu_nr = i;
  333. #else
  334. cpu_nr = get_hard_smp_processor_id(i);
  335. #endif
  336. memset((void *)critirq_ctx[cpu_nr], 0, THREAD_SIZE);
  337. tp = critirq_ctx[cpu_nr];
  338. tp->cpu = cpu_nr;
  339. tp->preempt_count = 0;
  340. #ifdef CONFIG_BOOKE
  341. memset((void *)dbgirq_ctx[cpu_nr], 0, THREAD_SIZE);
  342. tp = dbgirq_ctx[cpu_nr];
  343. tp->cpu = cpu_nr;
  344. tp->preempt_count = 0;
  345. memset((void *)mcheckirq_ctx[cpu_nr], 0, THREAD_SIZE);
  346. tp = mcheckirq_ctx[cpu_nr];
  347. tp->cpu = cpu_nr;
  348. tp->preempt_count = HARDIRQ_OFFSET;
  349. #endif
  350. }
  351. }
  352. #endif
  353. struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
  354. struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
  355. void irq_ctx_init(void)
  356. {
  357. struct thread_info *tp;
  358. int i;
  359. for_each_possible_cpu(i) {
  360. memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
  361. tp = softirq_ctx[i];
  362. tp->cpu = i;
  363. tp->preempt_count = 0;
  364. memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
  365. tp = hardirq_ctx[i];
  366. tp->cpu = i;
  367. tp->preempt_count = HARDIRQ_OFFSET;
  368. }
  369. }
  370. static inline void do_softirq_onstack(void)
  371. {
  372. struct thread_info *curtp, *irqtp;
  373. unsigned long saved_sp_limit = current->thread.ksp_limit;
  374. curtp = current_thread_info();
  375. irqtp = softirq_ctx[smp_processor_id()];
  376. irqtp->task = curtp->task;
  377. irqtp->flags = 0;
  378. current->thread.ksp_limit = (unsigned long)irqtp +
  379. _ALIGN_UP(sizeof(struct thread_info), 16);
  380. call_do_softirq(irqtp);
  381. current->thread.ksp_limit = saved_sp_limit;
  382. irqtp->task = NULL;
  383. /* Set any flag that may have been set on the
  384. * alternate stack
  385. */
  386. if (irqtp->flags)
  387. set_bits(irqtp->flags, &curtp->flags);
  388. }
  389. void do_softirq(void)
  390. {
  391. unsigned long flags;
  392. if (in_interrupt())
  393. return;
  394. local_irq_save(flags);
  395. if (local_softirq_pending())
  396. do_softirq_onstack();
  397. local_irq_restore(flags);
  398. }
  399. /*
  400. * IRQ controller and virtual interrupts
  401. */
  402. /* The main irq map itself is an array of NR_IRQ entries containing the
  403. * associate host and irq number. An entry with a host of NULL is free.
  404. * An entry can be allocated if it's free, the allocator always then sets
  405. * hwirq first to the host's invalid irq number and then fills ops.
  406. */
  407. struct irq_map_entry {
  408. irq_hw_number_t hwirq;
  409. struct irq_host *host;
  410. };
  411. static LIST_HEAD(irq_hosts);
  412. static DEFINE_RAW_SPINLOCK(irq_big_lock);
  413. static DEFINE_MUTEX(revmap_trees_mutex);
  414. static struct irq_map_entry irq_map[NR_IRQS];
  415. static unsigned int irq_virq_count = NR_IRQS;
  416. static struct irq_host *irq_default_host;
  417. irq_hw_number_t irqd_to_hwirq(struct irq_data *d)
  418. {
  419. return irq_map[d->irq].hwirq;
  420. }
  421. EXPORT_SYMBOL_GPL(irqd_to_hwirq);
  422. irq_hw_number_t virq_to_hw(unsigned int virq)
  423. {
  424. return irq_map[virq].hwirq;
  425. }
  426. EXPORT_SYMBOL_GPL(virq_to_hw);
  427. bool virq_is_host(unsigned int virq, struct irq_host *host)
  428. {
  429. return irq_map[virq].host == host;
  430. }
  431. EXPORT_SYMBOL_GPL(virq_is_host);
  432. static int default_irq_host_match(struct irq_host *h, struct device_node *np)
  433. {
  434. return h->of_node != NULL && h->of_node == np;
  435. }
  436. struct irq_host *irq_alloc_host(struct device_node *of_node,
  437. unsigned int revmap_type,
  438. unsigned int revmap_arg,
  439. struct irq_host_ops *ops,
  440. irq_hw_number_t inval_irq)
  441. {
  442. struct irq_host *host;
  443. unsigned int size = sizeof(struct irq_host);
  444. unsigned int i;
  445. unsigned int *rmap;
  446. unsigned long flags;
  447. /* Allocate structure and revmap table if using linear mapping */
  448. if (revmap_type == IRQ_HOST_MAP_LINEAR)
  449. size += revmap_arg * sizeof(unsigned int);
  450. host = kzalloc(size, GFP_KERNEL);
  451. if (host == NULL)
  452. return NULL;
  453. /* Fill structure */
  454. host->revmap_type = revmap_type;
  455. host->inval_irq = inval_irq;
  456. host->ops = ops;
  457. host->of_node = of_node_get(of_node);
  458. if (host->ops->match == NULL)
  459. host->ops->match = default_irq_host_match;
  460. raw_spin_lock_irqsave(&irq_big_lock, flags);
  461. /* If it's a legacy controller, check for duplicates and
  462. * mark it as allocated (we use irq 0 host pointer for that
  463. */
  464. if (revmap_type == IRQ_HOST_MAP_LEGACY) {
  465. if (irq_map[0].host != NULL) {
  466. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  467. of_node_put(host->of_node);
  468. kfree(host);
  469. return NULL;
  470. }
  471. irq_map[0].host = host;
  472. }
  473. list_add(&host->link, &irq_hosts);
  474. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  475. /* Additional setups per revmap type */
  476. switch(revmap_type) {
  477. case IRQ_HOST_MAP_LEGACY:
  478. /* 0 is always the invalid number for legacy */
  479. host->inval_irq = 0;
  480. /* setup us as the host for all legacy interrupts */
  481. for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
  482. irq_map[i].hwirq = i;
  483. smp_wmb();
  484. irq_map[i].host = host;
  485. smp_wmb();
  486. /* Legacy flags are left to default at this point,
  487. * one can then use irq_create_mapping() to
  488. * explicitly change them
  489. */
  490. ops->map(host, i, i);
  491. /* Clear norequest flags */
  492. irq_clear_status_flags(i, IRQ_NOREQUEST);
  493. }
  494. break;
  495. case IRQ_HOST_MAP_LINEAR:
  496. rmap = (unsigned int *)(host + 1);
  497. for (i = 0; i < revmap_arg; i++)
  498. rmap[i] = NO_IRQ;
  499. host->revmap_data.linear.size = revmap_arg;
  500. smp_wmb();
  501. host->revmap_data.linear.revmap = rmap;
  502. break;
  503. case IRQ_HOST_MAP_TREE:
  504. INIT_RADIX_TREE(&host->revmap_data.tree, GFP_KERNEL);
  505. break;
  506. default:
  507. break;
  508. }
  509. pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
  510. return host;
  511. }
  512. struct irq_host *irq_find_host(struct device_node *node)
  513. {
  514. struct irq_host *h, *found = NULL;
  515. unsigned long flags;
  516. /* We might want to match the legacy controller last since
  517. * it might potentially be set to match all interrupts in
  518. * the absence of a device node. This isn't a problem so far
  519. * yet though...
  520. */
  521. raw_spin_lock_irqsave(&irq_big_lock, flags);
  522. list_for_each_entry(h, &irq_hosts, link)
  523. if (h->ops->match(h, node)) {
  524. found = h;
  525. break;
  526. }
  527. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  528. return found;
  529. }
  530. EXPORT_SYMBOL_GPL(irq_find_host);
  531. void irq_set_default_host(struct irq_host *host)
  532. {
  533. pr_debug("irq: Default host set to @0x%p\n", host);
  534. irq_default_host = host;
  535. }
  536. void irq_set_virq_count(unsigned int count)
  537. {
  538. pr_debug("irq: Trying to set virq count to %d\n", count);
  539. BUG_ON(count < NUM_ISA_INTERRUPTS);
  540. if (count < NR_IRQS)
  541. irq_virq_count = count;
  542. }
  543. static int irq_setup_virq(struct irq_host *host, unsigned int virq,
  544. irq_hw_number_t hwirq)
  545. {
  546. int res;
  547. res = irq_alloc_desc_at(virq, 0);
  548. if (res != virq) {
  549. pr_debug("irq: -> allocating desc failed\n");
  550. goto error;
  551. }
  552. /* map it */
  553. smp_wmb();
  554. irq_map[virq].hwirq = hwirq;
  555. smp_mb();
  556. if (host->ops->map(host, virq, hwirq)) {
  557. pr_debug("irq: -> mapping failed, freeing\n");
  558. goto errdesc;
  559. }
  560. irq_clear_status_flags(virq, IRQ_NOREQUEST);
  561. return 0;
  562. errdesc:
  563. irq_free_descs(virq, 1);
  564. error:
  565. irq_free_virt(virq, 1);
  566. return -1;
  567. }
  568. unsigned int irq_create_direct_mapping(struct irq_host *host)
  569. {
  570. unsigned int virq;
  571. if (host == NULL)
  572. host = irq_default_host;
  573. BUG_ON(host == NULL);
  574. WARN_ON(host->revmap_type != IRQ_HOST_MAP_NOMAP);
  575. virq = irq_alloc_virt(host, 1, 0);
  576. if (virq == NO_IRQ) {
  577. pr_debug("irq: create_direct virq allocation failed\n");
  578. return NO_IRQ;
  579. }
  580. pr_debug("irq: create_direct obtained virq %d\n", virq);
  581. if (irq_setup_virq(host, virq, virq))
  582. return NO_IRQ;
  583. return virq;
  584. }
  585. unsigned int irq_create_mapping(struct irq_host *host,
  586. irq_hw_number_t hwirq)
  587. {
  588. unsigned int virq, hint;
  589. pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
  590. /* Look for default host if nececssary */
  591. if (host == NULL)
  592. host = irq_default_host;
  593. if (host == NULL) {
  594. printk(KERN_WARNING "irq_create_mapping called for"
  595. " NULL host, hwirq=%lx\n", hwirq);
  596. WARN_ON(1);
  597. return NO_IRQ;
  598. }
  599. pr_debug("irq: -> using host @%p\n", host);
  600. /* Check if mapping already exists */
  601. virq = irq_find_mapping(host, hwirq);
  602. if (virq != NO_IRQ) {
  603. pr_debug("irq: -> existing mapping on virq %d\n", virq);
  604. return virq;
  605. }
  606. /* Get a virtual interrupt number */
  607. if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
  608. /* Handle legacy */
  609. virq = (unsigned int)hwirq;
  610. if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
  611. return NO_IRQ;
  612. return virq;
  613. } else {
  614. /* Allocate a virtual interrupt number */
  615. hint = hwirq % irq_virq_count;
  616. virq = irq_alloc_virt(host, 1, hint);
  617. if (virq == NO_IRQ) {
  618. pr_debug("irq: -> virq allocation failed\n");
  619. return NO_IRQ;
  620. }
  621. }
  622. if (irq_setup_virq(host, virq, hwirq))
  623. return NO_IRQ;
  624. pr_debug("irq: irq %lu on host %s mapped to virtual irq %u\n",
  625. hwirq, host->of_node ? host->of_node->full_name : "null", virq);
  626. return virq;
  627. }
  628. EXPORT_SYMBOL_GPL(irq_create_mapping);
  629. unsigned int irq_create_of_mapping(struct device_node *controller,
  630. const u32 *intspec, unsigned int intsize)
  631. {
  632. struct irq_host *host;
  633. irq_hw_number_t hwirq;
  634. unsigned int type = IRQ_TYPE_NONE;
  635. unsigned int virq;
  636. if (controller == NULL)
  637. host = irq_default_host;
  638. else
  639. host = irq_find_host(controller);
  640. if (host == NULL) {
  641. printk(KERN_WARNING "irq: no irq host found for %s !\n",
  642. controller->full_name);
  643. return NO_IRQ;
  644. }
  645. /* If host has no translation, then we assume interrupt line */
  646. if (host->ops->xlate == NULL)
  647. hwirq = intspec[0];
  648. else {
  649. if (host->ops->xlate(host, controller, intspec, intsize,
  650. &hwirq, &type))
  651. return NO_IRQ;
  652. }
  653. /* Create mapping */
  654. virq = irq_create_mapping(host, hwirq);
  655. if (virq == NO_IRQ)
  656. return virq;
  657. /* Set type if specified and different than the current one */
  658. if (type != IRQ_TYPE_NONE &&
  659. type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
  660. irq_set_irq_type(virq, type);
  661. return virq;
  662. }
  663. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  664. void irq_dispose_mapping(unsigned int virq)
  665. {
  666. struct irq_host *host;
  667. irq_hw_number_t hwirq;
  668. if (virq == NO_IRQ)
  669. return;
  670. host = irq_map[virq].host;
  671. if (WARN_ON(host == NULL))
  672. return;
  673. /* Never unmap legacy interrupts */
  674. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  675. return;
  676. irq_set_status_flags(virq, IRQ_NOREQUEST);
  677. /* remove chip and handler */
  678. irq_set_chip_and_handler(virq, NULL, NULL);
  679. /* Make sure it's completed */
  680. synchronize_irq(virq);
  681. /* Tell the PIC about it */
  682. if (host->ops->unmap)
  683. host->ops->unmap(host, virq);
  684. smp_mb();
  685. /* Clear reverse map */
  686. hwirq = irq_map[virq].hwirq;
  687. switch(host->revmap_type) {
  688. case IRQ_HOST_MAP_LINEAR:
  689. if (hwirq < host->revmap_data.linear.size)
  690. host->revmap_data.linear.revmap[hwirq] = NO_IRQ;
  691. break;
  692. case IRQ_HOST_MAP_TREE:
  693. mutex_lock(&revmap_trees_mutex);
  694. radix_tree_delete(&host->revmap_data.tree, hwirq);
  695. mutex_unlock(&revmap_trees_mutex);
  696. break;
  697. }
  698. /* Destroy map */
  699. smp_mb();
  700. irq_map[virq].hwirq = host->inval_irq;
  701. irq_free_descs(virq, 1);
  702. /* Free it */
  703. irq_free_virt(virq, 1);
  704. }
  705. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  706. unsigned int irq_find_mapping(struct irq_host *host,
  707. irq_hw_number_t hwirq)
  708. {
  709. unsigned int i;
  710. unsigned int hint = hwirq % irq_virq_count;
  711. /* Look for default host if nececssary */
  712. if (host == NULL)
  713. host = irq_default_host;
  714. if (host == NULL)
  715. return NO_IRQ;
  716. /* legacy -> bail early */
  717. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  718. return hwirq;
  719. /* Slow path does a linear search of the map */
  720. if (hint < NUM_ISA_INTERRUPTS)
  721. hint = NUM_ISA_INTERRUPTS;
  722. i = hint;
  723. do {
  724. if (irq_map[i].host == host &&
  725. irq_map[i].hwirq == hwirq)
  726. return i;
  727. i++;
  728. if (i >= irq_virq_count)
  729. i = NUM_ISA_INTERRUPTS;
  730. } while(i != hint);
  731. return NO_IRQ;
  732. }
  733. EXPORT_SYMBOL_GPL(irq_find_mapping);
  734. #ifdef CONFIG_SMP
  735. int irq_choose_cpu(const struct cpumask *mask)
  736. {
  737. int cpuid;
  738. if (cpumask_equal(mask, cpu_all_mask)) {
  739. static int irq_rover;
  740. static DEFINE_RAW_SPINLOCK(irq_rover_lock);
  741. unsigned long flags;
  742. /* Round-robin distribution... */
  743. do_round_robin:
  744. raw_spin_lock_irqsave(&irq_rover_lock, flags);
  745. irq_rover = cpumask_next(irq_rover, cpu_online_mask);
  746. if (irq_rover >= nr_cpu_ids)
  747. irq_rover = cpumask_first(cpu_online_mask);
  748. cpuid = irq_rover;
  749. raw_spin_unlock_irqrestore(&irq_rover_lock, flags);
  750. } else {
  751. cpuid = cpumask_first_and(mask, cpu_online_mask);
  752. if (cpuid >= nr_cpu_ids)
  753. goto do_round_robin;
  754. }
  755. return get_hard_smp_processor_id(cpuid);
  756. }
  757. #else
  758. int irq_choose_cpu(const struct cpumask *mask)
  759. {
  760. return hard_smp_processor_id();
  761. }
  762. #endif
  763. unsigned int irq_radix_revmap_lookup(struct irq_host *host,
  764. irq_hw_number_t hwirq)
  765. {
  766. struct irq_map_entry *ptr;
  767. unsigned int virq;
  768. if (WARN_ON_ONCE(host->revmap_type != IRQ_HOST_MAP_TREE))
  769. return irq_find_mapping(host, hwirq);
  770. /*
  771. * The ptr returned references the static global irq_map.
  772. * but freeing an irq can delete nodes along the path to
  773. * do the lookup via call_rcu.
  774. */
  775. rcu_read_lock();
  776. ptr = radix_tree_lookup(&host->revmap_data.tree, hwirq);
  777. rcu_read_unlock();
  778. /*
  779. * If found in radix tree, then fine.
  780. * Else fallback to linear lookup - this should not happen in practice
  781. * as it means that we failed to insert the node in the radix tree.
  782. */
  783. if (ptr)
  784. virq = ptr - irq_map;
  785. else
  786. virq = irq_find_mapping(host, hwirq);
  787. return virq;
  788. }
  789. void irq_radix_revmap_insert(struct irq_host *host, unsigned int virq,
  790. irq_hw_number_t hwirq)
  791. {
  792. if (WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE))
  793. return;
  794. if (virq != NO_IRQ) {
  795. mutex_lock(&revmap_trees_mutex);
  796. radix_tree_insert(&host->revmap_data.tree, hwirq,
  797. &irq_map[virq]);
  798. mutex_unlock(&revmap_trees_mutex);
  799. }
  800. }
  801. unsigned int irq_linear_revmap(struct irq_host *host,
  802. irq_hw_number_t hwirq)
  803. {
  804. unsigned int *revmap;
  805. if (WARN_ON_ONCE(host->revmap_type != IRQ_HOST_MAP_LINEAR))
  806. return irq_find_mapping(host, hwirq);
  807. /* Check revmap bounds */
  808. if (unlikely(hwirq >= host->revmap_data.linear.size))
  809. return irq_find_mapping(host, hwirq);
  810. /* Check if revmap was allocated */
  811. revmap = host->revmap_data.linear.revmap;
  812. if (unlikely(revmap == NULL))
  813. return irq_find_mapping(host, hwirq);
  814. /* Fill up revmap with slow path if no mapping found */
  815. if (unlikely(revmap[hwirq] == NO_IRQ))
  816. revmap[hwirq] = irq_find_mapping(host, hwirq);
  817. return revmap[hwirq];
  818. }
  819. unsigned int irq_alloc_virt(struct irq_host *host,
  820. unsigned int count,
  821. unsigned int hint)
  822. {
  823. unsigned long flags;
  824. unsigned int i, j, found = NO_IRQ;
  825. if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
  826. return NO_IRQ;
  827. raw_spin_lock_irqsave(&irq_big_lock, flags);
  828. /* Use hint for 1 interrupt if any */
  829. if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
  830. hint < irq_virq_count && irq_map[hint].host == NULL) {
  831. found = hint;
  832. goto hint_found;
  833. }
  834. /* Look for count consecutive numbers in the allocatable
  835. * (non-legacy) space
  836. */
  837. for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
  838. if (irq_map[i].host != NULL)
  839. j = 0;
  840. else
  841. j++;
  842. if (j == count) {
  843. found = i - count + 1;
  844. break;
  845. }
  846. }
  847. if (found == NO_IRQ) {
  848. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  849. return NO_IRQ;
  850. }
  851. hint_found:
  852. for (i = found; i < (found + count); i++) {
  853. irq_map[i].hwirq = host->inval_irq;
  854. smp_wmb();
  855. irq_map[i].host = host;
  856. }
  857. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  858. return found;
  859. }
  860. void irq_free_virt(unsigned int virq, unsigned int count)
  861. {
  862. unsigned long flags;
  863. unsigned int i;
  864. WARN_ON (virq < NUM_ISA_INTERRUPTS);
  865. WARN_ON (count == 0 || (virq + count) > irq_virq_count);
  866. if (virq < NUM_ISA_INTERRUPTS) {
  867. if (virq + count < NUM_ISA_INTERRUPTS)
  868. return;
  869. count =- NUM_ISA_INTERRUPTS - virq;
  870. virq = NUM_ISA_INTERRUPTS;
  871. }
  872. if (count > irq_virq_count || virq > irq_virq_count - count) {
  873. if (virq > irq_virq_count)
  874. return;
  875. count = irq_virq_count - virq;
  876. }
  877. raw_spin_lock_irqsave(&irq_big_lock, flags);
  878. for (i = virq; i < (virq + count); i++) {
  879. struct irq_host *host;
  880. host = irq_map[i].host;
  881. irq_map[i].hwirq = host->inval_irq;
  882. smp_wmb();
  883. irq_map[i].host = NULL;
  884. }
  885. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  886. }
  887. int arch_early_irq_init(void)
  888. {
  889. return 0;
  890. }
  891. #ifdef CONFIG_VIRQ_DEBUG
  892. static int virq_debug_show(struct seq_file *m, void *private)
  893. {
  894. unsigned long flags;
  895. struct irq_desc *desc;
  896. const char *p;
  897. static const char none[] = "none";
  898. void *data;
  899. int i;
  900. seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
  901. "chip name", "chip data", "host name");
  902. for (i = 1; i < nr_irqs; i++) {
  903. desc = irq_to_desc(i);
  904. if (!desc)
  905. continue;
  906. raw_spin_lock_irqsave(&desc->lock, flags);
  907. if (desc->action && desc->action->handler) {
  908. struct irq_chip *chip;
  909. seq_printf(m, "%5d ", i);
  910. seq_printf(m, "0x%05lx ", irq_map[i].hwirq);
  911. chip = irq_desc_get_chip(desc);
  912. if (chip && chip->name)
  913. p = chip->name;
  914. else
  915. p = none;
  916. seq_printf(m, "%-15s ", p);
  917. data = irq_desc_get_chip_data(desc);
  918. seq_printf(m, "0x%16p ", data);
  919. if (irq_map[i].host && irq_map[i].host->of_node)
  920. p = irq_map[i].host->of_node->full_name;
  921. else
  922. p = none;
  923. seq_printf(m, "%s\n", p);
  924. }
  925. raw_spin_unlock_irqrestore(&desc->lock, flags);
  926. }
  927. return 0;
  928. }
  929. static int virq_debug_open(struct inode *inode, struct file *file)
  930. {
  931. return single_open(file, virq_debug_show, inode->i_private);
  932. }
  933. static const struct file_operations virq_debug_fops = {
  934. .open = virq_debug_open,
  935. .read = seq_read,
  936. .llseek = seq_lseek,
  937. .release = single_release,
  938. };
  939. static int __init irq_debugfs_init(void)
  940. {
  941. if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
  942. NULL, &virq_debug_fops) == NULL)
  943. return -ENOMEM;
  944. return 0;
  945. }
  946. __initcall(irq_debugfs_init);
  947. #endif /* CONFIG_VIRQ_DEBUG */
  948. #ifdef CONFIG_PPC64
  949. static int __init setup_noirqdistrib(char *str)
  950. {
  951. distribute_irqs = 0;
  952. return 1;
  953. }
  954. __setup("noirqdistrib", setup_noirqdistrib);
  955. #endif /* CONFIG_PPC64 */