irq.c 27 KB

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