irq.c 27 KB

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