irq.c 25 KB

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