irq.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * Derived from arch/i386/kernel/irq.c
  3. * Copyright (C) 1992 Linus Torvalds
  4. * Adapted from arch/i386 by Gary Thomas
  5. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  6. * Updated and modified by Cort Dougan <cort@fsmlabs.com>
  7. * Copyright (C) 1996-2001 Cort Dougan
  8. * Adapted for Power Macintosh by Paul Mackerras
  9. * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * This file contains the code used by various IRQ handling routines:
  17. * asking for different IRQ's should be done through these routines
  18. * instead of just grabbing them. Thus setups with different IRQ numbers
  19. * shouldn't result in any weird surprises, and installing new handlers
  20. * should be easier.
  21. *
  22. * The MPC8xx has an interrupt mask in the SIU. If a bit is set, the
  23. * interrupt is _enabled_. As expected, IRQ0 is bit 0 in the 32-bit
  24. * mask register (of which only 16 are defined), hence the weird shifting
  25. * and complement of the cached_irq_mask. I want to be able to stuff
  26. * this right into the SIU SMASK register.
  27. * Many of the prep/chrp functions are conditional compiled on CONFIG_8xx
  28. * to reduce code space and undefined function references.
  29. */
  30. #undef DEBUG
  31. #include <linux/export.h>
  32. #include <linux/threads.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/signal.h>
  35. #include <linux/sched.h>
  36. #include <linux/ptrace.h>
  37. #include <linux/ioport.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/timex.h>
  40. #include <linux/init.h>
  41. #include <linux/slab.h>
  42. #include <linux/delay.h>
  43. #include <linux/irq.h>
  44. #include <linux/seq_file.h>
  45. #include <linux/cpumask.h>
  46. #include <linux/profile.h>
  47. #include <linux/bitops.h>
  48. #include <linux/list.h>
  49. #include <linux/radix-tree.h>
  50. #include <linux/mutex.h>
  51. #include <linux/bootmem.h>
  52. #include <linux/pci.h>
  53. #include <linux/debugfs.h>
  54. #include <linux/of.h>
  55. #include <linux/of_irq.h>
  56. #include <asm/uaccess.h>
  57. #include <asm/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. static inline notrace void decrementer_check_overflow(void)
  103. {
  104. u64 now = get_tb_or_rtc();
  105. u64 *next_tb = &__get_cpu_var(decrementers_next_tb);
  106. if (now >= *next_tb)
  107. set_dec(1);
  108. }
  109. notrace void arch_local_irq_restore(unsigned long en)
  110. {
  111. /*
  112. * get_paca()->soft_enabled = en;
  113. * Is it ever valid to use local_irq_restore(0) when soft_enabled is 1?
  114. * That was allowed before, and in such a case we do need to take care
  115. * that gcc will set soft_enabled directly via r13, not choose to use
  116. * an intermediate register, lest we're preempted to a different cpu.
  117. */
  118. set_soft_enabled(en);
  119. if (!en)
  120. return;
  121. #ifdef CONFIG_PPC_STD_MMU_64
  122. if (firmware_has_feature(FW_FEATURE_ISERIES)) {
  123. /*
  124. * Do we need to disable preemption here? Not really: in the
  125. * unlikely event that we're preempted to a different cpu in
  126. * between getting r13, loading its lppaca_ptr, and loading
  127. * its any_int, we might call iseries_handle_interrupts without
  128. * an interrupt pending on the new cpu, but that's no disaster,
  129. * is it? And the business of preempting us off the old cpu
  130. * would itself involve a local_irq_restore which handles the
  131. * interrupt to that cpu.
  132. *
  133. * But use "local_paca->lppaca_ptr" instead of "get_lppaca()"
  134. * to avoid any preemption checking added into get_paca().
  135. */
  136. if (local_paca->lppaca_ptr->int_dword.any_int)
  137. iseries_handle_interrupts();
  138. }
  139. #endif /* CONFIG_PPC_STD_MMU_64 */
  140. /*
  141. * if (get_paca()->hard_enabled) return;
  142. * But again we need to take care that gcc gets hard_enabled directly
  143. * via r13, not choose to use an intermediate register, lest we're
  144. * preempted to a different cpu in between the two instructions.
  145. */
  146. if (get_hard_enabled())
  147. return;
  148. /*
  149. * Need to hard-enable interrupts here. Since currently disabled,
  150. * no need to take further asm precautions against preemption; but
  151. * use local_paca instead of get_paca() to avoid preemption checking.
  152. */
  153. local_paca->hard_enabled = en;
  154. /*
  155. * Trigger the decrementer if we have a pending event. Some processors
  156. * only trigger on edge transitions of the sign bit. We might also
  157. * have disabled interrupts long enough that the decrementer wrapped
  158. * to positive.
  159. */
  160. decrementer_check_overflow();
  161. /*
  162. * Force the delivery of pending soft-disabled interrupts on PS3.
  163. * Any HV call will have this side effect.
  164. */
  165. if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
  166. u64 tmp, tmp2;
  167. lv1_get_version_info(&tmp, &tmp2);
  168. }
  169. __hard_irq_enable();
  170. }
  171. EXPORT_SYMBOL(arch_local_irq_restore);
  172. #endif /* CONFIG_PPC64 */
  173. int arch_show_interrupts(struct seq_file *p, int prec)
  174. {
  175. int j;
  176. #if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT)
  177. if (tau_initialized) {
  178. seq_printf(p, "%*s: ", prec, "TAU");
  179. for_each_online_cpu(j)
  180. seq_printf(p, "%10u ", tau_interrupts(j));
  181. seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
  182. }
  183. #endif /* CONFIG_PPC32 && CONFIG_TAU_INT */
  184. seq_printf(p, "%*s: ", prec, "LOC");
  185. for_each_online_cpu(j)
  186. seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs);
  187. seq_printf(p, " Local timer interrupts\n");
  188. seq_printf(p, "%*s: ", prec, "SPU");
  189. for_each_online_cpu(j)
  190. seq_printf(p, "%10u ", per_cpu(irq_stat, j).spurious_irqs);
  191. seq_printf(p, " Spurious interrupts\n");
  192. seq_printf(p, "%*s: ", prec, "CNT");
  193. for_each_online_cpu(j)
  194. seq_printf(p, "%10u ", per_cpu(irq_stat, j).pmu_irqs);
  195. seq_printf(p, " Performance monitoring interrupts\n");
  196. seq_printf(p, "%*s: ", prec, "MCE");
  197. for_each_online_cpu(j)
  198. seq_printf(p, "%10u ", per_cpu(irq_stat, j).mce_exceptions);
  199. seq_printf(p, " Machine check exceptions\n");
  200. return 0;
  201. }
  202. /*
  203. * /proc/stat helpers
  204. */
  205. u64 arch_irq_stat_cpu(unsigned int cpu)
  206. {
  207. u64 sum = per_cpu(irq_stat, cpu).timer_irqs;
  208. sum += per_cpu(irq_stat, cpu).pmu_irqs;
  209. sum += per_cpu(irq_stat, cpu).mce_exceptions;
  210. sum += per_cpu(irq_stat, cpu).spurious_irqs;
  211. return sum;
  212. }
  213. #ifdef CONFIG_HOTPLUG_CPU
  214. void migrate_irqs(void)
  215. {
  216. struct irq_desc *desc;
  217. unsigned int irq;
  218. static int warned;
  219. cpumask_var_t mask;
  220. const struct cpumask *map = cpu_online_mask;
  221. alloc_cpumask_var(&mask, GFP_KERNEL);
  222. for_each_irq(irq) {
  223. struct irq_data *data;
  224. struct irq_chip *chip;
  225. desc = irq_to_desc(irq);
  226. if (!desc)
  227. continue;
  228. data = irq_desc_get_irq_data(desc);
  229. if (irqd_is_per_cpu(data))
  230. continue;
  231. chip = irq_data_get_irq_chip(data);
  232. cpumask_and(mask, data->affinity, map);
  233. if (cpumask_any(mask) >= nr_cpu_ids) {
  234. printk("Breaking affinity for irq %i\n", irq);
  235. cpumask_copy(mask, map);
  236. }
  237. if (chip->irq_set_affinity)
  238. chip->irq_set_affinity(data, mask, true);
  239. else if (desc->action && !(warned++))
  240. printk("Cannot set affinity for irq %i\n", irq);
  241. }
  242. free_cpumask_var(mask);
  243. local_irq_enable();
  244. mdelay(1);
  245. local_irq_disable();
  246. }
  247. #endif
  248. static inline void handle_one_irq(unsigned int irq)
  249. {
  250. struct thread_info *curtp, *irqtp;
  251. unsigned long saved_sp_limit;
  252. struct irq_desc *desc;
  253. desc = irq_to_desc(irq);
  254. if (!desc)
  255. return;
  256. /* Switch to the irq stack to handle this */
  257. curtp = current_thread_info();
  258. irqtp = hardirq_ctx[smp_processor_id()];
  259. if (curtp == irqtp) {
  260. /* We're already on the irq stack, just handle it */
  261. desc->handle_irq(irq, desc);
  262. return;
  263. }
  264. saved_sp_limit = current->thread.ksp_limit;
  265. irqtp->task = curtp->task;
  266. irqtp->flags = 0;
  267. /* Copy the softirq bits in preempt_count so that the
  268. * softirq checks work in the hardirq context. */
  269. irqtp->preempt_count = (irqtp->preempt_count & ~SOFTIRQ_MASK) |
  270. (curtp->preempt_count & SOFTIRQ_MASK);
  271. current->thread.ksp_limit = (unsigned long)irqtp +
  272. _ALIGN_UP(sizeof(struct thread_info), 16);
  273. call_handle_irq(irq, desc, irqtp, desc->handle_irq);
  274. current->thread.ksp_limit = saved_sp_limit;
  275. irqtp->task = NULL;
  276. /* Set any flag that may have been set on the
  277. * alternate stack
  278. */
  279. if (irqtp->flags)
  280. set_bits(irqtp->flags, &curtp->flags);
  281. }
  282. static inline void check_stack_overflow(void)
  283. {
  284. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  285. long sp;
  286. sp = __get_SP() & (THREAD_SIZE-1);
  287. /* check for stack overflow: is there less than 2KB free? */
  288. if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
  289. printk("do_IRQ: stack overflow: %ld\n",
  290. sp - sizeof(struct thread_info));
  291. dump_stack();
  292. }
  293. #endif
  294. }
  295. void do_IRQ(struct pt_regs *regs)
  296. {
  297. struct pt_regs *old_regs = set_irq_regs(regs);
  298. unsigned int irq;
  299. trace_irq_entry(regs);
  300. irq_enter();
  301. check_stack_overflow();
  302. irq = ppc_md.get_irq();
  303. if (irq != NO_IRQ && irq != NO_IRQ_IGNORE)
  304. handle_one_irq(irq);
  305. else if (irq != NO_IRQ_IGNORE)
  306. __get_cpu_var(irq_stat).spurious_irqs++;
  307. irq_exit();
  308. set_irq_regs(old_regs);
  309. #ifdef CONFIG_PPC_ISERIES
  310. if (firmware_has_feature(FW_FEATURE_ISERIES) &&
  311. get_lppaca()->int_dword.fields.decr_int) {
  312. get_lppaca()->int_dword.fields.decr_int = 0;
  313. /* Signal a fake decrementer interrupt */
  314. timer_interrupt(regs);
  315. }
  316. #endif
  317. trace_irq_exit(regs);
  318. }
  319. void __init init_IRQ(void)
  320. {
  321. if (ppc_md.init_IRQ)
  322. ppc_md.init_IRQ();
  323. exc_lvl_ctx_init();
  324. irq_ctx_init();
  325. }
  326. #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
  327. struct thread_info *critirq_ctx[NR_CPUS] __read_mostly;
  328. struct thread_info *dbgirq_ctx[NR_CPUS] __read_mostly;
  329. struct thread_info *mcheckirq_ctx[NR_CPUS] __read_mostly;
  330. void exc_lvl_ctx_init(void)
  331. {
  332. struct thread_info *tp;
  333. int i, cpu_nr;
  334. for_each_possible_cpu(i) {
  335. #ifdef CONFIG_PPC64
  336. cpu_nr = i;
  337. #else
  338. cpu_nr = get_hard_smp_processor_id(i);
  339. #endif
  340. memset((void *)critirq_ctx[cpu_nr], 0, THREAD_SIZE);
  341. tp = critirq_ctx[cpu_nr];
  342. tp->cpu = cpu_nr;
  343. tp->preempt_count = 0;
  344. #ifdef CONFIG_BOOKE
  345. memset((void *)dbgirq_ctx[cpu_nr], 0, THREAD_SIZE);
  346. tp = dbgirq_ctx[cpu_nr];
  347. tp->cpu = cpu_nr;
  348. tp->preempt_count = 0;
  349. memset((void *)mcheckirq_ctx[cpu_nr], 0, THREAD_SIZE);
  350. tp = mcheckirq_ctx[cpu_nr];
  351. tp->cpu = cpu_nr;
  352. tp->preempt_count = HARDIRQ_OFFSET;
  353. #endif
  354. }
  355. }
  356. #endif
  357. struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
  358. struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
  359. void irq_ctx_init(void)
  360. {
  361. struct thread_info *tp;
  362. int i;
  363. for_each_possible_cpu(i) {
  364. memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
  365. tp = softirq_ctx[i];
  366. tp->cpu = i;
  367. tp->preempt_count = 0;
  368. memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
  369. tp = hardirq_ctx[i];
  370. tp->cpu = i;
  371. tp->preempt_count = HARDIRQ_OFFSET;
  372. }
  373. }
  374. static inline void do_softirq_onstack(void)
  375. {
  376. struct thread_info *curtp, *irqtp;
  377. unsigned long saved_sp_limit = current->thread.ksp_limit;
  378. curtp = current_thread_info();
  379. irqtp = softirq_ctx[smp_processor_id()];
  380. irqtp->task = curtp->task;
  381. irqtp->flags = 0;
  382. current->thread.ksp_limit = (unsigned long)irqtp +
  383. _ALIGN_UP(sizeof(struct thread_info), 16);
  384. call_do_softirq(irqtp);
  385. current->thread.ksp_limit = saved_sp_limit;
  386. irqtp->task = NULL;
  387. /* Set any flag that may have been set on the
  388. * alternate stack
  389. */
  390. if (irqtp->flags)
  391. set_bits(irqtp->flags, &curtp->flags);
  392. }
  393. void do_softirq(void)
  394. {
  395. unsigned long flags;
  396. if (in_interrupt())
  397. return;
  398. local_irq_save(flags);
  399. if (local_softirq_pending())
  400. do_softirq_onstack();
  401. local_irq_restore(flags);
  402. }
  403. /*
  404. * IRQ controller and virtual interrupts
  405. */
  406. static LIST_HEAD(irq_domain_list);
  407. static DEFINE_MUTEX(irq_domain_mutex);
  408. static DEFINE_MUTEX(revmap_trees_mutex);
  409. static unsigned int irq_virq_count = NR_IRQS;
  410. static struct irq_domain *irq_default_host;
  411. irq_hw_number_t irqd_to_hwirq(struct irq_data *d)
  412. {
  413. return d->hwirq;
  414. }
  415. EXPORT_SYMBOL_GPL(irqd_to_hwirq);
  416. irq_hw_number_t virq_to_hw(unsigned int virq)
  417. {
  418. struct irq_data *irq_data = irq_get_irq_data(virq);
  419. return WARN_ON(!irq_data) ? 0 : irq_data->hwirq;
  420. }
  421. EXPORT_SYMBOL_GPL(virq_to_hw);
  422. bool virq_is_host(unsigned int virq, struct irq_domain *host)
  423. {
  424. struct irq_data *irq_data = irq_get_irq_data(virq);
  425. return irq_data ? irq_data->domain == host : false;
  426. }
  427. EXPORT_SYMBOL_GPL(virq_is_host);
  428. static int default_irq_host_match(struct irq_domain *h, struct device_node *np)
  429. {
  430. return h->of_node != NULL && h->of_node == np;
  431. }
  432. struct irq_domain *irq_alloc_host(struct device_node *of_node,
  433. unsigned int revmap_type,
  434. unsigned int revmap_arg,
  435. struct irq_domain_ops *ops,
  436. irq_hw_number_t inval_irq)
  437. {
  438. struct irq_domain *host, *h;
  439. unsigned int size = sizeof(struct irq_domain);
  440. unsigned int i;
  441. unsigned int *rmap;
  442. /* Allocate structure and revmap table if using linear mapping */
  443. if (revmap_type == IRQ_DOMAIN_MAP_LINEAR)
  444. size += revmap_arg * sizeof(unsigned int);
  445. host = kzalloc(size, GFP_KERNEL);
  446. if (host == NULL)
  447. return NULL;
  448. /* Fill structure */
  449. host->revmap_type = revmap_type;
  450. host->inval_irq = inval_irq;
  451. host->ops = ops;
  452. host->of_node = of_node_get(of_node);
  453. if (host->ops->match == NULL)
  454. host->ops->match = default_irq_host_match;
  455. mutex_lock(&irq_domain_mutex);
  456. /* Make sure only one legacy controller can be created */
  457. if (revmap_type == IRQ_DOMAIN_MAP_LEGACY) {
  458. list_for_each_entry(h, &irq_domain_list, link) {
  459. if (WARN_ON(h->revmap_type == IRQ_DOMAIN_MAP_LEGACY)) {
  460. mutex_unlock(&irq_domain_mutex);
  461. of_node_put(host->of_node);
  462. kfree(host);
  463. return NULL;
  464. }
  465. }
  466. }
  467. list_add(&host->link, &irq_domain_list);
  468. mutex_unlock(&irq_domain_mutex);
  469. /* Additional setups per revmap type */
  470. switch(revmap_type) {
  471. case IRQ_DOMAIN_MAP_LEGACY:
  472. /* 0 is always the invalid number for legacy */
  473. host->inval_irq = 0;
  474. /* setup us as the host for all legacy interrupts */
  475. for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
  476. struct irq_data *irq_data = irq_get_irq_data(i);
  477. irq_data->hwirq = i;
  478. irq_data->domain = host;
  479. /* Legacy flags are left to default at this point,
  480. * one can then use irq_create_mapping() to
  481. * explicitly change them
  482. */
  483. ops->map(host, i, i);
  484. /* Clear norequest flags */
  485. irq_clear_status_flags(i, IRQ_NOREQUEST);
  486. }
  487. break;
  488. case IRQ_DOMAIN_MAP_LINEAR:
  489. rmap = (unsigned int *)(host + 1);
  490. for (i = 0; i < revmap_arg; i++)
  491. rmap[i] = NO_IRQ;
  492. host->revmap_data.linear.size = revmap_arg;
  493. host->revmap_data.linear.revmap = rmap;
  494. break;
  495. case IRQ_DOMAIN_MAP_TREE:
  496. INIT_RADIX_TREE(&host->revmap_data.tree, GFP_KERNEL);
  497. break;
  498. default:
  499. break;
  500. }
  501. pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
  502. return host;
  503. }
  504. struct irq_domain *irq_find_host(struct device_node *node)
  505. {
  506. struct irq_domain *h, *found = NULL;
  507. /* We might want to match the legacy controller last since
  508. * it might potentially be set to match all interrupts in
  509. * the absence of a device node. This isn't a problem so far
  510. * yet though...
  511. */
  512. mutex_lock(&irq_domain_mutex);
  513. list_for_each_entry(h, &irq_domain_list, link)
  514. if (h->ops->match(h, node)) {
  515. found = h;
  516. break;
  517. }
  518. mutex_unlock(&irq_domain_mutex);
  519. return found;
  520. }
  521. EXPORT_SYMBOL_GPL(irq_find_host);
  522. void irq_set_default_host(struct irq_domain *host)
  523. {
  524. pr_debug("irq: Default host set to @0x%p\n", host);
  525. irq_default_host = host;
  526. }
  527. void irq_set_virq_count(unsigned int count)
  528. {
  529. pr_debug("irq: Trying to set virq count to %d\n", count);
  530. BUG_ON(count < NUM_ISA_INTERRUPTS);
  531. if (count < NR_IRQS)
  532. irq_virq_count = count;
  533. }
  534. static int irq_setup_virq(struct irq_domain *host, unsigned int virq,
  535. irq_hw_number_t hwirq)
  536. {
  537. struct irq_data *irq_data = irq_get_irq_data(virq);
  538. irq_data->hwirq = hwirq;
  539. irq_data->domain = host;
  540. if (host->ops->map(host, virq, hwirq)) {
  541. pr_debug("irq: -> mapping failed, freeing\n");
  542. irq_data->domain = NULL;
  543. irq_data->hwirq = 0;
  544. return -1;
  545. }
  546. irq_clear_status_flags(virq, IRQ_NOREQUEST);
  547. return 0;
  548. }
  549. unsigned int irq_create_direct_mapping(struct irq_domain *host)
  550. {
  551. unsigned int virq;
  552. if (host == NULL)
  553. host = irq_default_host;
  554. BUG_ON(host == NULL);
  555. WARN_ON(host->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
  556. virq = irq_alloc_desc_from(1, 0);
  557. if (virq == NO_IRQ) {
  558. pr_debug("irq: create_direct virq allocation failed\n");
  559. return NO_IRQ;
  560. }
  561. if (virq >= irq_virq_count) {
  562. pr_err("ERROR: no free irqs available below %i maximum\n",
  563. irq_virq_count);
  564. irq_free_desc(virq);
  565. return 0;
  566. }
  567. pr_debug("irq: create_direct obtained virq %d\n", virq);
  568. if (irq_setup_virq(host, virq, virq)) {
  569. irq_free_desc(virq);
  570. return NO_IRQ;
  571. }
  572. return virq;
  573. }
  574. unsigned int irq_create_mapping(struct irq_domain *host,
  575. irq_hw_number_t hwirq)
  576. {
  577. unsigned int virq, hint;
  578. pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
  579. /* Look for default host if nececssary */
  580. if (host == NULL)
  581. host = irq_default_host;
  582. if (host == NULL) {
  583. printk(KERN_WARNING "irq_create_mapping called for"
  584. " NULL host, hwirq=%lx\n", hwirq);
  585. WARN_ON(1);
  586. return NO_IRQ;
  587. }
  588. pr_debug("irq: -> using host @%p\n", host);
  589. /* Check if mapping already exists */
  590. virq = irq_find_mapping(host, hwirq);
  591. if (virq != NO_IRQ) {
  592. pr_debug("irq: -> existing mapping on virq %d\n", virq);
  593. return virq;
  594. }
  595. /* Get a virtual interrupt number */
  596. if (host->revmap_type == IRQ_DOMAIN_MAP_LEGACY) {
  597. /* Handle legacy */
  598. virq = (unsigned int)hwirq;
  599. if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
  600. return NO_IRQ;
  601. return virq;
  602. } else {
  603. /* Allocate a virtual interrupt number */
  604. hint = hwirq % irq_virq_count;
  605. if (hint == 0)
  606. hint = 1;
  607. virq = irq_alloc_desc_from(hint, 0);
  608. if (!virq)
  609. virq = irq_alloc_desc_from(1, 0);
  610. if (virq == NO_IRQ) {
  611. pr_debug("irq: -> virq allocation failed\n");
  612. return NO_IRQ;
  613. }
  614. }
  615. if (irq_setup_virq(host, virq, hwirq)) {
  616. if (host->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
  617. irq_free_desc(virq);
  618. return NO_IRQ;
  619. }
  620. pr_debug("irq: irq %lu on host %s mapped to virtual irq %u\n",
  621. hwirq, host->of_node ? host->of_node->full_name : "null", virq);
  622. return virq;
  623. }
  624. EXPORT_SYMBOL_GPL(irq_create_mapping);
  625. unsigned int irq_create_of_mapping(struct device_node *controller,
  626. const u32 *intspec, unsigned int intsize)
  627. {
  628. struct irq_domain *host;
  629. irq_hw_number_t hwirq;
  630. unsigned int type = IRQ_TYPE_NONE;
  631. unsigned int virq;
  632. if (controller == NULL)
  633. host = irq_default_host;
  634. else
  635. host = irq_find_host(controller);
  636. if (host == NULL) {
  637. printk(KERN_WARNING "irq: no irq host found for %s !\n",
  638. controller->full_name);
  639. return NO_IRQ;
  640. }
  641. /* If host has no translation, then we assume interrupt line */
  642. if (host->ops->xlate == NULL)
  643. hwirq = intspec[0];
  644. else {
  645. if (host->ops->xlate(host, controller, intspec, intsize,
  646. &hwirq, &type))
  647. return NO_IRQ;
  648. }
  649. /* Create mapping */
  650. virq = irq_create_mapping(host, hwirq);
  651. if (virq == NO_IRQ)
  652. return virq;
  653. /* Set type if specified and different than the current one */
  654. if (type != IRQ_TYPE_NONE &&
  655. type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
  656. irq_set_irq_type(virq, type);
  657. return virq;
  658. }
  659. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  660. void irq_dispose_mapping(unsigned int virq)
  661. {
  662. struct irq_data *irq_data = irq_get_irq_data(virq);
  663. struct irq_domain *host;
  664. irq_hw_number_t hwirq;
  665. if (virq == NO_IRQ || !irq_data)
  666. return;
  667. host = irq_data->domain;
  668. if (WARN_ON(host == NULL))
  669. return;
  670. /* Never unmap legacy interrupts */
  671. if (host->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
  672. return;
  673. irq_set_status_flags(virq, IRQ_NOREQUEST);
  674. /* remove chip and handler */
  675. irq_set_chip_and_handler(virq, NULL, NULL);
  676. /* Make sure it's completed */
  677. synchronize_irq(virq);
  678. /* Tell the PIC about it */
  679. if (host->ops->unmap)
  680. host->ops->unmap(host, virq);
  681. smp_mb();
  682. /* Clear reverse map */
  683. hwirq = irq_data->hwirq;
  684. switch(host->revmap_type) {
  685. case IRQ_DOMAIN_MAP_LINEAR:
  686. if (hwirq < host->revmap_data.linear.size)
  687. host->revmap_data.linear.revmap[hwirq] = NO_IRQ;
  688. break;
  689. case IRQ_DOMAIN_MAP_TREE:
  690. mutex_lock(&revmap_trees_mutex);
  691. radix_tree_delete(&host->revmap_data.tree, hwirq);
  692. mutex_unlock(&revmap_trees_mutex);
  693. break;
  694. }
  695. /* Destroy map */
  696. irq_data->hwirq = host->inval_irq;
  697. irq_free_desc(virq);
  698. }
  699. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  700. unsigned int irq_find_mapping(struct irq_domain *host,
  701. irq_hw_number_t hwirq)
  702. {
  703. unsigned int i;
  704. unsigned int hint = hwirq % irq_virq_count;
  705. /* Look for default host if nececssary */
  706. if (host == NULL)
  707. host = irq_default_host;
  708. if (host == NULL)
  709. return NO_IRQ;
  710. /* legacy -> bail early */
  711. if (host->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
  712. return hwirq;
  713. /* Slow path does a linear search of the map */
  714. if (hint == 0)
  715. hint = 1;
  716. i = hint;
  717. do {
  718. struct irq_data *data = irq_get_irq_data(i);
  719. if (data && (data->domain == host) && (data->hwirq == hwirq))
  720. return i;
  721. i++;
  722. if (i >= irq_virq_count)
  723. i = 1;
  724. } while(i != hint);
  725. return NO_IRQ;
  726. }
  727. EXPORT_SYMBOL_GPL(irq_find_mapping);
  728. #ifdef CONFIG_SMP
  729. int irq_choose_cpu(const struct cpumask *mask)
  730. {
  731. int cpuid;
  732. if (cpumask_equal(mask, cpu_all_mask)) {
  733. static int irq_rover;
  734. static DEFINE_RAW_SPINLOCK(irq_rover_lock);
  735. unsigned long flags;
  736. /* Round-robin distribution... */
  737. do_round_robin:
  738. raw_spin_lock_irqsave(&irq_rover_lock, flags);
  739. irq_rover = cpumask_next(irq_rover, cpu_online_mask);
  740. if (irq_rover >= nr_cpu_ids)
  741. irq_rover = cpumask_first(cpu_online_mask);
  742. cpuid = irq_rover;
  743. raw_spin_unlock_irqrestore(&irq_rover_lock, flags);
  744. } else {
  745. cpuid = cpumask_first_and(mask, cpu_online_mask);
  746. if (cpuid >= nr_cpu_ids)
  747. goto do_round_robin;
  748. }
  749. return get_hard_smp_processor_id(cpuid);
  750. }
  751. #else
  752. int irq_choose_cpu(const struct cpumask *mask)
  753. {
  754. return hard_smp_processor_id();
  755. }
  756. #endif
  757. unsigned int irq_radix_revmap_lookup(struct irq_domain *host,
  758. irq_hw_number_t hwirq)
  759. {
  760. struct irq_data *irq_data;
  761. if (WARN_ON_ONCE(host->revmap_type != IRQ_DOMAIN_MAP_TREE))
  762. return irq_find_mapping(host, hwirq);
  763. /*
  764. * Freeing an irq can delete nodes along the path to
  765. * do the lookup via call_rcu.
  766. */
  767. rcu_read_lock();
  768. irq_data = radix_tree_lookup(&host->revmap_data.tree, hwirq);
  769. rcu_read_unlock();
  770. /*
  771. * If found in radix tree, then fine.
  772. * Else fallback to linear lookup - this should not happen in practice
  773. * as it means that we failed to insert the node in the radix tree.
  774. */
  775. return irq_data ? irq_data->irq : irq_find_mapping(host, hwirq);
  776. }
  777. void irq_radix_revmap_insert(struct irq_domain *host, unsigned int virq,
  778. irq_hw_number_t hwirq)
  779. {
  780. struct irq_data *irq_data = irq_get_irq_data(virq);
  781. if (WARN_ON(host->revmap_type != IRQ_DOMAIN_MAP_TREE))
  782. return;
  783. if (virq != NO_IRQ) {
  784. mutex_lock(&revmap_trees_mutex);
  785. radix_tree_insert(&host->revmap_data.tree, hwirq, irq_data);
  786. mutex_unlock(&revmap_trees_mutex);
  787. }
  788. }
  789. unsigned int irq_linear_revmap(struct irq_domain *host,
  790. irq_hw_number_t hwirq)
  791. {
  792. unsigned int *revmap;
  793. if (WARN_ON_ONCE(host->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
  794. return irq_find_mapping(host, hwirq);
  795. /* Check revmap bounds */
  796. if (unlikely(hwirq >= host->revmap_data.linear.size))
  797. return irq_find_mapping(host, hwirq);
  798. /* Check if revmap was allocated */
  799. revmap = host->revmap_data.linear.revmap;
  800. if (unlikely(revmap == NULL))
  801. return irq_find_mapping(host, hwirq);
  802. /* Fill up revmap with slow path if no mapping found */
  803. if (unlikely(revmap[hwirq] == NO_IRQ))
  804. revmap[hwirq] = irq_find_mapping(host, hwirq);
  805. return revmap[hwirq];
  806. }
  807. int arch_early_irq_init(void)
  808. {
  809. return 0;
  810. }
  811. #ifdef CONFIG_VIRQ_DEBUG
  812. static int virq_debug_show(struct seq_file *m, void *private)
  813. {
  814. unsigned long flags;
  815. struct irq_desc *desc;
  816. const char *p;
  817. static const char none[] = "none";
  818. void *data;
  819. int i;
  820. seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
  821. "chip name", "chip data", "host name");
  822. for (i = 1; i < nr_irqs; i++) {
  823. desc = irq_to_desc(i);
  824. if (!desc)
  825. continue;
  826. raw_spin_lock_irqsave(&desc->lock, flags);
  827. if (desc->action && desc->action->handler) {
  828. struct irq_chip *chip;
  829. seq_printf(m, "%5d ", i);
  830. seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
  831. chip = irq_desc_get_chip(desc);
  832. if (chip && chip->name)
  833. p = chip->name;
  834. else
  835. p = none;
  836. seq_printf(m, "%-15s ", p);
  837. data = irq_desc_get_chip_data(desc);
  838. seq_printf(m, "0x%16p ", data);
  839. if (desc->irq_data.domain->of_node)
  840. p = desc->irq_data.domain->of_node->full_name;
  841. else
  842. p = none;
  843. seq_printf(m, "%s\n", p);
  844. }
  845. raw_spin_unlock_irqrestore(&desc->lock, flags);
  846. }
  847. return 0;
  848. }
  849. static int virq_debug_open(struct inode *inode, struct file *file)
  850. {
  851. return single_open(file, virq_debug_show, inode->i_private);
  852. }
  853. static const struct file_operations virq_debug_fops = {
  854. .open = virq_debug_open,
  855. .read = seq_read,
  856. .llseek = seq_lseek,
  857. .release = single_release,
  858. };
  859. static int __init irq_debugfs_init(void)
  860. {
  861. if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
  862. NULL, &virq_debug_fops) == NULL)
  863. return -ENOMEM;
  864. return 0;
  865. }
  866. __initcall(irq_debugfs_init);
  867. #endif /* CONFIG_VIRQ_DEBUG */
  868. #ifdef CONFIG_PPC64
  869. static int __init setup_noirqdistrib(char *str)
  870. {
  871. distribute_irqs = 0;
  872. return 1;
  873. }
  874. __setup("noirqdistrib", setup_noirqdistrib);
  875. #endif /* CONFIG_PPC64 */