irq.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * This file contains the code used by various IRQ handling routines:
  18. * asking for different IRQ's should be done through these routines
  19. * instead of just grabbing them. Thus setups with different IRQ numbers
  20. * shouldn't result in any weird surprises, and installing new handlers
  21. * should be easier.
  22. *
  23. * The MPC8xx has an interrupt mask in the SIU. If a bit is set, the
  24. * interrupt is _enabled_. As expected, IRQ0 is bit 0 in the 32-bit
  25. * mask register (of which only 16 are defined), hence the weird shifting
  26. * and complement of the cached_irq_mask. I want to be able to stuff
  27. * this right into the SIU SMASK register.
  28. * Many of the prep/chrp functions are conditional compiled on CONFIG_8xx
  29. * to reduce code space and undefined function references.
  30. */
  31. #undef DEBUG
  32. #include <linux/module.h>
  33. #include <linux/threads.h>
  34. #include <linux/kernel_stat.h>
  35. #include <linux/signal.h>
  36. #include <linux/sched.h>
  37. #include <linux/ptrace.h>
  38. #include <linux/ioport.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/timex.h>
  41. #include <linux/init.h>
  42. #include <linux/slab.h>
  43. #include <linux/delay.h>
  44. #include <linux/irq.h>
  45. #include <linux/seq_file.h>
  46. #include <linux/cpumask.h>
  47. #include <linux/profile.h>
  48. #include <linux/bitops.h>
  49. #include <linux/list.h>
  50. #include <linux/radix-tree.h>
  51. #include <linux/mutex.h>
  52. #include <linux/bootmem.h>
  53. #include <linux/pci.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. ppc_md.init_IRQ();
  303. #ifdef CONFIG_PPC64
  304. irq_ctx_init();
  305. #endif
  306. }
  307. #ifdef CONFIG_IRQSTACKS
  308. struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
  309. struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
  310. void irq_ctx_init(void)
  311. {
  312. struct thread_info *tp;
  313. int i;
  314. for_each_possible_cpu(i) {
  315. memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
  316. tp = softirq_ctx[i];
  317. tp->cpu = i;
  318. tp->preempt_count = SOFTIRQ_OFFSET;
  319. memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
  320. tp = hardirq_ctx[i];
  321. tp->cpu = i;
  322. tp->preempt_count = HARDIRQ_OFFSET;
  323. }
  324. }
  325. static inline void do_softirq_onstack(void)
  326. {
  327. struct thread_info *curtp, *irqtp;
  328. curtp = current_thread_info();
  329. irqtp = softirq_ctx[smp_processor_id()];
  330. irqtp->task = curtp->task;
  331. call_do_softirq(irqtp);
  332. irqtp->task = NULL;
  333. }
  334. #else
  335. #define do_softirq_onstack() __do_softirq()
  336. #endif /* CONFIG_IRQSTACKS */
  337. void do_softirq(void)
  338. {
  339. unsigned long flags;
  340. if (in_interrupt())
  341. return;
  342. local_irq_save(flags);
  343. if (local_softirq_pending())
  344. do_softirq_onstack();
  345. local_irq_restore(flags);
  346. }
  347. EXPORT_SYMBOL(do_softirq);
  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. struct irq_host *irq_alloc_host(unsigned int revmap_type,
  360. unsigned int revmap_arg,
  361. struct irq_host_ops *ops,
  362. irq_hw_number_t inval_irq)
  363. {
  364. struct irq_host *host;
  365. unsigned int size = sizeof(struct irq_host);
  366. unsigned int i;
  367. unsigned int *rmap;
  368. unsigned long flags;
  369. /* Allocate structure and revmap table if using linear mapping */
  370. if (revmap_type == IRQ_HOST_MAP_LINEAR)
  371. size += revmap_arg * sizeof(unsigned int);
  372. if (mem_init_done)
  373. host = kzalloc(size, GFP_KERNEL);
  374. else {
  375. host = alloc_bootmem(size);
  376. if (host)
  377. memset(host, 0, size);
  378. }
  379. if (host == NULL)
  380. return NULL;
  381. /* Fill structure */
  382. host->revmap_type = revmap_type;
  383. host->inval_irq = inval_irq;
  384. host->ops = ops;
  385. spin_lock_irqsave(&irq_big_lock, flags);
  386. /* If it's a legacy controller, check for duplicates and
  387. * mark it as allocated (we use irq 0 host pointer for that
  388. */
  389. if (revmap_type == IRQ_HOST_MAP_LEGACY) {
  390. if (irq_map[0].host != NULL) {
  391. spin_unlock_irqrestore(&irq_big_lock, flags);
  392. /* If we are early boot, we can't free the structure,
  393. * too bad...
  394. * this will be fixed once slab is made available early
  395. * instead of the current cruft
  396. */
  397. if (mem_init_done)
  398. kfree(host);
  399. return NULL;
  400. }
  401. irq_map[0].host = host;
  402. }
  403. list_add(&host->link, &irq_hosts);
  404. spin_unlock_irqrestore(&irq_big_lock, flags);
  405. /* Additional setups per revmap type */
  406. switch(revmap_type) {
  407. case IRQ_HOST_MAP_LEGACY:
  408. /* 0 is always the invalid number for legacy */
  409. host->inval_irq = 0;
  410. /* setup us as the host for all legacy interrupts */
  411. for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
  412. irq_map[i].hwirq = 0;
  413. smp_wmb();
  414. irq_map[i].host = host;
  415. smp_wmb();
  416. /* Clear norequest flags */
  417. get_irq_desc(i)->status &= ~IRQ_NOREQUEST;
  418. /* Legacy flags are left to default at this point,
  419. * one can then use irq_create_mapping() to
  420. * explicitely change them
  421. */
  422. ops->map(host, i, i);
  423. }
  424. break;
  425. case IRQ_HOST_MAP_LINEAR:
  426. rmap = (unsigned int *)(host + 1);
  427. for (i = 0; i < revmap_arg; i++)
  428. rmap[i] = IRQ_NONE;
  429. host->revmap_data.linear.size = revmap_arg;
  430. smp_wmb();
  431. host->revmap_data.linear.revmap = rmap;
  432. break;
  433. default:
  434. break;
  435. }
  436. pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
  437. return host;
  438. }
  439. struct irq_host *irq_find_host(struct device_node *node)
  440. {
  441. struct irq_host *h, *found = NULL;
  442. unsigned long flags;
  443. /* We might want to match the legacy controller last since
  444. * it might potentially be set to match all interrupts in
  445. * the absence of a device node. This isn't a problem so far
  446. * yet though...
  447. */
  448. spin_lock_irqsave(&irq_big_lock, flags);
  449. list_for_each_entry(h, &irq_hosts, link)
  450. if (h->ops->match == NULL || h->ops->match(h, node)) {
  451. found = h;
  452. break;
  453. }
  454. spin_unlock_irqrestore(&irq_big_lock, flags);
  455. return found;
  456. }
  457. EXPORT_SYMBOL_GPL(irq_find_host);
  458. void irq_set_default_host(struct irq_host *host)
  459. {
  460. pr_debug("irq: Default host set to @0x%p\n", host);
  461. irq_default_host = host;
  462. }
  463. void irq_set_virq_count(unsigned int count)
  464. {
  465. pr_debug("irq: Trying to set virq count to %d\n", count);
  466. BUG_ON(count < NUM_ISA_INTERRUPTS);
  467. if (count < NR_IRQS)
  468. irq_virq_count = count;
  469. }
  470. /* radix tree not lockless safe ! we use a brlock-type mecanism
  471. * for now, until we can use a lockless radix tree
  472. */
  473. static void irq_radix_wrlock(unsigned long *flags)
  474. {
  475. unsigned int cpu, ok;
  476. spin_lock_irqsave(&irq_big_lock, *flags);
  477. irq_radix_writer = 1;
  478. smp_mb();
  479. do {
  480. barrier();
  481. ok = 1;
  482. for_each_possible_cpu(cpu) {
  483. if (per_cpu(irq_radix_reader, cpu)) {
  484. ok = 0;
  485. break;
  486. }
  487. }
  488. if (!ok)
  489. cpu_relax();
  490. } while(!ok);
  491. }
  492. static void irq_radix_wrunlock(unsigned long flags)
  493. {
  494. smp_wmb();
  495. irq_radix_writer = 0;
  496. spin_unlock_irqrestore(&irq_big_lock, flags);
  497. }
  498. static void irq_radix_rdlock(unsigned long *flags)
  499. {
  500. local_irq_save(*flags);
  501. __get_cpu_var(irq_radix_reader) = 1;
  502. smp_mb();
  503. if (likely(irq_radix_writer == 0))
  504. return;
  505. __get_cpu_var(irq_radix_reader) = 0;
  506. smp_wmb();
  507. spin_lock(&irq_big_lock);
  508. __get_cpu_var(irq_radix_reader) = 1;
  509. spin_unlock(&irq_big_lock);
  510. }
  511. static void irq_radix_rdunlock(unsigned long flags)
  512. {
  513. __get_cpu_var(irq_radix_reader) = 0;
  514. local_irq_restore(flags);
  515. }
  516. unsigned int irq_create_mapping(struct irq_host *host,
  517. irq_hw_number_t hwirq)
  518. {
  519. unsigned int virq, hint;
  520. pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
  521. /* Look for default host if nececssary */
  522. if (host == NULL)
  523. host = irq_default_host;
  524. if (host == NULL) {
  525. printk(KERN_WARNING "irq_create_mapping called for"
  526. " NULL host, hwirq=%lx\n", hwirq);
  527. WARN_ON(1);
  528. return NO_IRQ;
  529. }
  530. pr_debug("irq: -> using host @%p\n", host);
  531. /* Check if mapping already exist, if it does, call
  532. * host->ops->map() to update the flags
  533. */
  534. virq = irq_find_mapping(host, hwirq);
  535. if (virq != IRQ_NONE) {
  536. if (host->ops->remap)
  537. host->ops->remap(host, virq, hwirq);
  538. pr_debug("irq: -> existing mapping on virq %d\n", virq);
  539. return virq;
  540. }
  541. /* Get a virtual interrupt number */
  542. if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
  543. /* Handle legacy */
  544. virq = (unsigned int)hwirq;
  545. if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
  546. return NO_IRQ;
  547. return virq;
  548. } else {
  549. /* Allocate a virtual interrupt number */
  550. hint = hwirq % irq_virq_count;
  551. virq = irq_alloc_virt(host, 1, hint);
  552. if (virq == NO_IRQ) {
  553. pr_debug("irq: -> virq allocation failed\n");
  554. return NO_IRQ;
  555. }
  556. }
  557. pr_debug("irq: -> obtained virq %d\n", virq);
  558. /* Clear IRQ_NOREQUEST flag */
  559. get_irq_desc(virq)->status &= ~IRQ_NOREQUEST;
  560. /* map it */
  561. smp_wmb();
  562. irq_map[virq].hwirq = hwirq;
  563. smp_mb();
  564. if (host->ops->map(host, virq, hwirq)) {
  565. pr_debug("irq: -> mapping failed, freeing\n");
  566. irq_free_virt(virq, 1);
  567. return NO_IRQ;
  568. }
  569. return virq;
  570. }
  571. EXPORT_SYMBOL_GPL(irq_create_mapping);
  572. unsigned int irq_create_of_mapping(struct device_node *controller,
  573. u32 *intspec, unsigned int intsize)
  574. {
  575. struct irq_host *host;
  576. irq_hw_number_t hwirq;
  577. unsigned int type = IRQ_TYPE_NONE;
  578. unsigned int virq;
  579. if (controller == NULL)
  580. host = irq_default_host;
  581. else
  582. host = irq_find_host(controller);
  583. if (host == NULL) {
  584. printk(KERN_WARNING "irq: no irq host found for %s !\n",
  585. controller->full_name);
  586. return NO_IRQ;
  587. }
  588. /* If host has no translation, then we assume interrupt line */
  589. if (host->ops->xlate == NULL)
  590. hwirq = intspec[0];
  591. else {
  592. if (host->ops->xlate(host, controller, intspec, intsize,
  593. &hwirq, &type))
  594. return NO_IRQ;
  595. }
  596. /* Create mapping */
  597. virq = irq_create_mapping(host, hwirq);
  598. if (virq == NO_IRQ)
  599. return virq;
  600. /* Set type if specified and different than the current one */
  601. if (type != IRQ_TYPE_NONE &&
  602. type != (get_irq_desc(virq)->status & IRQF_TRIGGER_MASK))
  603. set_irq_type(virq, type);
  604. return virq;
  605. }
  606. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  607. unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
  608. {
  609. struct of_irq oirq;
  610. if (of_irq_map_one(dev, index, &oirq))
  611. return NO_IRQ;
  612. return irq_create_of_mapping(oirq.controller, oirq.specifier,
  613. oirq.size);
  614. }
  615. EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
  616. void irq_dispose_mapping(unsigned int virq)
  617. {
  618. struct irq_host *host;
  619. irq_hw_number_t hwirq;
  620. unsigned long flags;
  621. if (virq == NO_IRQ)
  622. return;
  623. host = irq_map[virq].host;
  624. WARN_ON (host == NULL);
  625. if (host == NULL)
  626. return;
  627. /* Never unmap legacy interrupts */
  628. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  629. return;
  630. /* remove chip and handler */
  631. set_irq_chip_and_handler(virq, NULL, NULL);
  632. /* Make sure it's completed */
  633. synchronize_irq(virq);
  634. /* Tell the PIC about it */
  635. if (host->ops->unmap)
  636. host->ops->unmap(host, virq);
  637. smp_mb();
  638. /* Clear reverse map */
  639. hwirq = irq_map[virq].hwirq;
  640. switch(host->revmap_type) {
  641. case IRQ_HOST_MAP_LINEAR:
  642. if (hwirq < host->revmap_data.linear.size)
  643. host->revmap_data.linear.revmap[hwirq] = IRQ_NONE;
  644. break;
  645. case IRQ_HOST_MAP_TREE:
  646. /* Check if radix tree allocated yet */
  647. if (host->revmap_data.tree.gfp_mask == 0)
  648. break;
  649. irq_radix_wrlock(&flags);
  650. radix_tree_delete(&host->revmap_data.tree, hwirq);
  651. irq_radix_wrunlock(flags);
  652. break;
  653. }
  654. /* Destroy map */
  655. smp_mb();
  656. irq_map[virq].hwirq = host->inval_irq;
  657. /* Set some flags */
  658. get_irq_desc(virq)->status |= IRQ_NOREQUEST;
  659. /* Free it */
  660. irq_free_virt(virq, 1);
  661. }
  662. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  663. unsigned int irq_find_mapping(struct irq_host *host,
  664. irq_hw_number_t hwirq)
  665. {
  666. unsigned int i;
  667. unsigned int hint = hwirq % irq_virq_count;
  668. /* Look for default host if nececssary */
  669. if (host == NULL)
  670. host = irq_default_host;
  671. if (host == NULL)
  672. return NO_IRQ;
  673. /* legacy -> bail early */
  674. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  675. return hwirq;
  676. /* Slow path does a linear search of the map */
  677. if (hint < NUM_ISA_INTERRUPTS)
  678. hint = NUM_ISA_INTERRUPTS;
  679. i = hint;
  680. do {
  681. if (irq_map[i].host == host &&
  682. irq_map[i].hwirq == hwirq)
  683. return i;
  684. i++;
  685. if (i >= irq_virq_count)
  686. i = NUM_ISA_INTERRUPTS;
  687. } while(i != hint);
  688. return NO_IRQ;
  689. }
  690. EXPORT_SYMBOL_GPL(irq_find_mapping);
  691. unsigned int irq_radix_revmap(struct irq_host *host,
  692. irq_hw_number_t hwirq)
  693. {
  694. struct radix_tree_root *tree;
  695. struct irq_map_entry *ptr;
  696. unsigned int virq;
  697. unsigned long flags;
  698. WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
  699. /* Check if the radix tree exist yet. We test the value of
  700. * the gfp_mask for that. Sneaky but saves another int in the
  701. * structure. If not, we fallback to slow mode
  702. */
  703. tree = &host->revmap_data.tree;
  704. if (tree->gfp_mask == 0)
  705. return irq_find_mapping(host, hwirq);
  706. /* Now try to resolve */
  707. irq_radix_rdlock(&flags);
  708. ptr = radix_tree_lookup(tree, hwirq);
  709. irq_radix_rdunlock(flags);
  710. /* Found it, return */
  711. if (ptr) {
  712. virq = ptr - irq_map;
  713. return virq;
  714. }
  715. /* If not there, try to insert it */
  716. virq = irq_find_mapping(host, hwirq);
  717. if (virq != NO_IRQ) {
  718. irq_radix_wrlock(&flags);
  719. radix_tree_insert(tree, hwirq, &irq_map[virq]);
  720. irq_radix_wrunlock(flags);
  721. }
  722. return virq;
  723. }
  724. unsigned int irq_linear_revmap(struct irq_host *host,
  725. irq_hw_number_t hwirq)
  726. {
  727. unsigned int *revmap;
  728. WARN_ON(host->revmap_type != IRQ_HOST_MAP_LINEAR);
  729. /* Check revmap bounds */
  730. if (unlikely(hwirq >= host->revmap_data.linear.size))
  731. return irq_find_mapping(host, hwirq);
  732. /* Check if revmap was allocated */
  733. revmap = host->revmap_data.linear.revmap;
  734. if (unlikely(revmap == NULL))
  735. return irq_find_mapping(host, hwirq);
  736. /* Fill up revmap with slow path if no mapping found */
  737. if (unlikely(revmap[hwirq] == NO_IRQ))
  738. revmap[hwirq] = irq_find_mapping(host, hwirq);
  739. return revmap[hwirq];
  740. }
  741. unsigned int irq_alloc_virt(struct irq_host *host,
  742. unsigned int count,
  743. unsigned int hint)
  744. {
  745. unsigned long flags;
  746. unsigned int i, j, found = NO_IRQ;
  747. if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
  748. return NO_IRQ;
  749. spin_lock_irqsave(&irq_big_lock, flags);
  750. /* Use hint for 1 interrupt if any */
  751. if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
  752. hint < irq_virq_count && irq_map[hint].host == NULL) {
  753. found = hint;
  754. goto hint_found;
  755. }
  756. /* Look for count consecutive numbers in the allocatable
  757. * (non-legacy) space
  758. */
  759. for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
  760. if (irq_map[i].host != NULL)
  761. j = 0;
  762. else
  763. j++;
  764. if (j == count) {
  765. found = i - count + 1;
  766. break;
  767. }
  768. }
  769. if (found == NO_IRQ) {
  770. spin_unlock_irqrestore(&irq_big_lock, flags);
  771. return NO_IRQ;
  772. }
  773. hint_found:
  774. for (i = found; i < (found + count); i++) {
  775. irq_map[i].hwirq = host->inval_irq;
  776. smp_wmb();
  777. irq_map[i].host = host;
  778. }
  779. spin_unlock_irqrestore(&irq_big_lock, flags);
  780. return found;
  781. }
  782. void irq_free_virt(unsigned int virq, unsigned int count)
  783. {
  784. unsigned long flags;
  785. unsigned int i;
  786. WARN_ON (virq < NUM_ISA_INTERRUPTS);
  787. WARN_ON (count == 0 || (virq + count) > irq_virq_count);
  788. spin_lock_irqsave(&irq_big_lock, flags);
  789. for (i = virq; i < (virq + count); i++) {
  790. struct irq_host *host;
  791. if (i < NUM_ISA_INTERRUPTS ||
  792. (virq + count) > irq_virq_count)
  793. continue;
  794. host = irq_map[i].host;
  795. irq_map[i].hwirq = host->inval_irq;
  796. smp_wmb();
  797. irq_map[i].host = NULL;
  798. }
  799. spin_unlock_irqrestore(&irq_big_lock, flags);
  800. }
  801. void irq_early_init(void)
  802. {
  803. unsigned int i;
  804. for (i = 0; i < NR_IRQS; i++)
  805. get_irq_desc(i)->status |= IRQ_NOREQUEST;
  806. }
  807. /* We need to create the radix trees late */
  808. static int irq_late_init(void)
  809. {
  810. struct irq_host *h;
  811. unsigned long flags;
  812. irq_radix_wrlock(&flags);
  813. list_for_each_entry(h, &irq_hosts, link) {
  814. if (h->revmap_type == IRQ_HOST_MAP_TREE)
  815. INIT_RADIX_TREE(&h->revmap_data.tree, GFP_ATOMIC);
  816. }
  817. irq_radix_wrunlock(flags);
  818. return 0;
  819. }
  820. arch_initcall(irq_late_init);
  821. #endif /* CONFIG_PPC_MERGE */
  822. #ifdef CONFIG_PPC64
  823. static int __init setup_noirqdistrib(char *str)
  824. {
  825. distribute_irqs = 0;
  826. return 1;
  827. }
  828. __setup("noirqdistrib", setup_noirqdistrib);
  829. #endif /* CONFIG_PPC64 */