irq.c 27 KB

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