irq.c 26 KB

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