irq.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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 <asm/uaccess.h>
  54. #include <asm/system.h>
  55. #include <asm/io.h>
  56. #include <asm/pgtable.h>
  57. #include <asm/irq.h>
  58. #include <asm/cache.h>
  59. #include <asm/prom.h>
  60. #include <asm/ptrace.h>
  61. #include <asm/machdep.h>
  62. #include <asm/udbg.h>
  63. #ifdef CONFIG_PPC64
  64. #include <asm/paca.h>
  65. #include <asm/firmware.h>
  66. #include <asm/lv1call.h>
  67. #endif
  68. int __irq_offset_value;
  69. static int ppc_spurious_interrupts;
  70. #ifdef CONFIG_PPC32
  71. EXPORT_SYMBOL(__irq_offset_value);
  72. atomic_t ppc_n_lost_interrupts;
  73. #ifndef CONFIG_PPC_MERGE
  74. #define NR_MASK_WORDS ((NR_IRQS + 31) / 32)
  75. unsigned long ppc_cached_irq_mask[NR_MASK_WORDS];
  76. #endif
  77. #ifdef CONFIG_TAU_INT
  78. extern int tau_initialized;
  79. extern int tau_interrupts(int);
  80. #endif
  81. #endif /* CONFIG_PPC32 */
  82. #if defined(CONFIG_SMP) && !defined(CONFIG_PPC_MERGE)
  83. extern atomic_t ipi_recv;
  84. extern atomic_t ipi_sent;
  85. #endif
  86. #ifdef CONFIG_PPC64
  87. EXPORT_SYMBOL(irq_desc);
  88. int distribute_irqs = 1;
  89. static inline unsigned long get_hard_enabled(void)
  90. {
  91. unsigned long enabled;
  92. __asm__ __volatile__("lbz %0,%1(13)"
  93. : "=r" (enabled) : "i" (offsetof(struct paca_struct, hard_enabled)));
  94. return enabled;
  95. }
  96. static inline void set_soft_enabled(unsigned long enable)
  97. {
  98. __asm__ __volatile__("stb %0,%1(13)"
  99. : : "r" (enable), "i" (offsetof(struct paca_struct, soft_enabled)));
  100. }
  101. void local_irq_restore(unsigned long en)
  102. {
  103. /*
  104. * get_paca()->soft_enabled = en;
  105. * Is it ever valid to use local_irq_restore(0) when soft_enabled is 1?
  106. * That was allowed before, and in such a case we do need to take care
  107. * that gcc will set soft_enabled directly via r13, not choose to use
  108. * an intermediate register, lest we're preempted to a different cpu.
  109. */
  110. set_soft_enabled(en);
  111. if (!en)
  112. return;
  113. if (firmware_has_feature(FW_FEATURE_ISERIES)) {
  114. /*
  115. * Do we need to disable preemption here? Not really: in the
  116. * unlikely event that we're preempted to a different cpu in
  117. * between getting r13, loading its lppaca_ptr, and loading
  118. * its any_int, we might call iseries_handle_interrupts without
  119. * an interrupt pending on the new cpu, but that's no disaster,
  120. * is it? And the business of preempting us off the old cpu
  121. * would itself involve a local_irq_restore which handles the
  122. * interrupt to that cpu.
  123. *
  124. * But use "local_paca->lppaca_ptr" instead of "get_lppaca()"
  125. * to avoid any preemption checking added into get_paca().
  126. */
  127. if (local_paca->lppaca_ptr->int_dword.any_int)
  128. iseries_handle_interrupts();
  129. return;
  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. call_handle_irq(irq, desc, irqtp, handler);
  279. irqtp->task = NULL;
  280. if (irqtp->flags)
  281. set_bits(irqtp->flags, &curtp->flags);
  282. } else
  283. #endif
  284. generic_handle_irq(irq);
  285. } else if (irq != NO_IRQ_IGNORE)
  286. /* That's not SMP safe ... but who cares ? */
  287. ppc_spurious_interrupts++;
  288. irq_exit();
  289. set_irq_regs(old_regs);
  290. #ifdef CONFIG_PPC_ISERIES
  291. if (firmware_has_feature(FW_FEATURE_ISERIES) &&
  292. get_lppaca()->int_dword.fields.decr_int) {
  293. get_lppaca()->int_dword.fields.decr_int = 0;
  294. /* Signal a fake decrementer interrupt */
  295. timer_interrupt(regs);
  296. }
  297. #endif
  298. }
  299. void __init init_IRQ(void)
  300. {
  301. ppc_md.init_IRQ();
  302. #ifdef CONFIG_PPC64
  303. irq_ctx_init();
  304. #endif
  305. }
  306. #ifdef CONFIG_IRQSTACKS
  307. struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
  308. struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
  309. void irq_ctx_init(void)
  310. {
  311. struct thread_info *tp;
  312. int i;
  313. for_each_possible_cpu(i) {
  314. memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
  315. tp = softirq_ctx[i];
  316. tp->cpu = i;
  317. tp->preempt_count = SOFTIRQ_OFFSET;
  318. memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
  319. tp = hardirq_ctx[i];
  320. tp->cpu = i;
  321. tp->preempt_count = HARDIRQ_OFFSET;
  322. }
  323. }
  324. static inline void do_softirq_onstack(void)
  325. {
  326. struct thread_info *curtp, *irqtp;
  327. curtp = current_thread_info();
  328. irqtp = softirq_ctx[smp_processor_id()];
  329. irqtp->task = curtp->task;
  330. call_do_softirq(irqtp);
  331. irqtp->task = NULL;
  332. }
  333. #else
  334. #define do_softirq_onstack() __do_softirq()
  335. #endif /* CONFIG_IRQSTACKS */
  336. void do_softirq(void)
  337. {
  338. unsigned long flags;
  339. if (in_interrupt())
  340. return;
  341. local_irq_save(flags);
  342. if (local_softirq_pending())
  343. do_softirq_onstack();
  344. local_irq_restore(flags);
  345. }
  346. EXPORT_SYMBOL(do_softirq);
  347. /*
  348. * IRQ controller and virtual interrupts
  349. */
  350. #ifdef CONFIG_PPC_MERGE
  351. static LIST_HEAD(irq_hosts);
  352. static DEFINE_SPINLOCK(irq_big_lock);
  353. static DEFINE_PER_CPU(unsigned int, irq_radix_reader);
  354. static unsigned int irq_radix_writer;
  355. struct irq_map_entry irq_map[NR_IRQS];
  356. static unsigned int irq_virq_count = NR_IRQS;
  357. static struct irq_host *irq_default_host;
  358. irq_hw_number_t virq_to_hw(unsigned int virq)
  359. {
  360. return irq_map[virq].hwirq;
  361. }
  362. EXPORT_SYMBOL_GPL(virq_to_hw);
  363. struct irq_host *irq_alloc_host(unsigned int revmap_type,
  364. unsigned int revmap_arg,
  365. struct irq_host_ops *ops,
  366. irq_hw_number_t inval_irq)
  367. {
  368. struct irq_host *host;
  369. unsigned int size = sizeof(struct irq_host);
  370. unsigned int i;
  371. unsigned int *rmap;
  372. unsigned long flags;
  373. /* Allocate structure and revmap table if using linear mapping */
  374. if (revmap_type == IRQ_HOST_MAP_LINEAR)
  375. size += revmap_arg * sizeof(unsigned int);
  376. if (mem_init_done)
  377. host = kzalloc(size, GFP_KERNEL);
  378. else {
  379. host = alloc_bootmem(size);
  380. if (host)
  381. memset(host, 0, size);
  382. }
  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. spin_lock_irqsave(&irq_big_lock, flags);
  390. /* If it's a legacy controller, check for duplicates and
  391. * mark it as allocated (we use irq 0 host pointer for that
  392. */
  393. if (revmap_type == IRQ_HOST_MAP_LEGACY) {
  394. if (irq_map[0].host != NULL) {
  395. spin_unlock_irqrestore(&irq_big_lock, flags);
  396. /* If we are early boot, we can't free the structure,
  397. * too bad...
  398. * this will be fixed once slab is made available early
  399. * instead of the current cruft
  400. */
  401. if (mem_init_done)
  402. kfree(host);
  403. return NULL;
  404. }
  405. irq_map[0].host = host;
  406. }
  407. list_add(&host->link, &irq_hosts);
  408. spin_unlock_irqrestore(&irq_big_lock, flags);
  409. /* Additional setups per revmap type */
  410. switch(revmap_type) {
  411. case IRQ_HOST_MAP_LEGACY:
  412. /* 0 is always the invalid number for legacy */
  413. host->inval_irq = 0;
  414. /* setup us as the host for all legacy interrupts */
  415. for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
  416. irq_map[i].hwirq = 0;
  417. smp_wmb();
  418. irq_map[i].host = host;
  419. smp_wmb();
  420. /* Clear norequest flags */
  421. get_irq_desc(i)->status &= ~IRQ_NOREQUEST;
  422. /* Legacy flags are left to default at this point,
  423. * one can then use irq_create_mapping() to
  424. * explicitely change them
  425. */
  426. ops->map(host, i, i);
  427. }
  428. break;
  429. case IRQ_HOST_MAP_LINEAR:
  430. rmap = (unsigned int *)(host + 1);
  431. for (i = 0; i < revmap_arg; i++)
  432. rmap[i] = NO_IRQ;
  433. host->revmap_data.linear.size = revmap_arg;
  434. smp_wmb();
  435. host->revmap_data.linear.revmap = rmap;
  436. break;
  437. default:
  438. break;
  439. }
  440. pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
  441. return host;
  442. }
  443. struct irq_host *irq_find_host(struct device_node *node)
  444. {
  445. struct irq_host *h, *found = NULL;
  446. unsigned long flags;
  447. /* We might want to match the legacy controller last since
  448. * it might potentially be set to match all interrupts in
  449. * the absence of a device node. This isn't a problem so far
  450. * yet though...
  451. */
  452. spin_lock_irqsave(&irq_big_lock, flags);
  453. list_for_each_entry(h, &irq_hosts, link)
  454. if (h->ops->match == NULL || h->ops->match(h, node)) {
  455. found = h;
  456. break;
  457. }
  458. spin_unlock_irqrestore(&irq_big_lock, flags);
  459. return found;
  460. }
  461. EXPORT_SYMBOL_GPL(irq_find_host);
  462. void irq_set_default_host(struct irq_host *host)
  463. {
  464. pr_debug("irq: Default host set to @0x%p\n", host);
  465. irq_default_host = host;
  466. }
  467. void irq_set_virq_count(unsigned int count)
  468. {
  469. pr_debug("irq: Trying to set virq count to %d\n", count);
  470. BUG_ON(count < NUM_ISA_INTERRUPTS);
  471. if (count < NR_IRQS)
  472. irq_virq_count = count;
  473. }
  474. /* radix tree not lockless safe ! we use a brlock-type mecanism
  475. * for now, until we can use a lockless radix tree
  476. */
  477. static void irq_radix_wrlock(unsigned long *flags)
  478. {
  479. unsigned int cpu, ok;
  480. spin_lock_irqsave(&irq_big_lock, *flags);
  481. irq_radix_writer = 1;
  482. smp_mb();
  483. do {
  484. barrier();
  485. ok = 1;
  486. for_each_possible_cpu(cpu) {
  487. if (per_cpu(irq_radix_reader, cpu)) {
  488. ok = 0;
  489. break;
  490. }
  491. }
  492. if (!ok)
  493. cpu_relax();
  494. } while(!ok);
  495. }
  496. static void irq_radix_wrunlock(unsigned long flags)
  497. {
  498. smp_wmb();
  499. irq_radix_writer = 0;
  500. spin_unlock_irqrestore(&irq_big_lock, flags);
  501. }
  502. static void irq_radix_rdlock(unsigned long *flags)
  503. {
  504. local_irq_save(*flags);
  505. __get_cpu_var(irq_radix_reader) = 1;
  506. smp_mb();
  507. if (likely(irq_radix_writer == 0))
  508. return;
  509. __get_cpu_var(irq_radix_reader) = 0;
  510. smp_wmb();
  511. spin_lock(&irq_big_lock);
  512. __get_cpu_var(irq_radix_reader) = 1;
  513. spin_unlock(&irq_big_lock);
  514. }
  515. static void irq_radix_rdunlock(unsigned long flags)
  516. {
  517. __get_cpu_var(irq_radix_reader) = 0;
  518. local_irq_restore(flags);
  519. }
  520. unsigned int irq_create_mapping(struct irq_host *host,
  521. irq_hw_number_t hwirq)
  522. {
  523. unsigned int virq, hint;
  524. pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
  525. /* Look for default host if nececssary */
  526. if (host == NULL)
  527. host = irq_default_host;
  528. if (host == NULL) {
  529. printk(KERN_WARNING "irq_create_mapping called for"
  530. " NULL host, hwirq=%lx\n", hwirq);
  531. WARN_ON(1);
  532. return NO_IRQ;
  533. }
  534. pr_debug("irq: -> using host @%p\n", host);
  535. /* Check if mapping already exist, if it does, call
  536. * host->ops->map() to update the flags
  537. */
  538. virq = irq_find_mapping(host, hwirq);
  539. if (virq != NO_IRQ) {
  540. if (host->ops->remap)
  541. host->ops->remap(host, virq, hwirq);
  542. pr_debug("irq: -> existing mapping on virq %d\n", virq);
  543. return virq;
  544. }
  545. /* Get a virtual interrupt number */
  546. if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
  547. /* Handle legacy */
  548. virq = (unsigned int)hwirq;
  549. if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
  550. return NO_IRQ;
  551. return virq;
  552. } else {
  553. /* Allocate a virtual interrupt number */
  554. hint = hwirq % irq_virq_count;
  555. virq = irq_alloc_virt(host, 1, hint);
  556. if (virq == NO_IRQ) {
  557. pr_debug("irq: -> virq allocation failed\n");
  558. return NO_IRQ;
  559. }
  560. }
  561. pr_debug("irq: -> obtained virq %d\n", virq);
  562. /* Clear IRQ_NOREQUEST flag */
  563. get_irq_desc(virq)->status &= ~IRQ_NOREQUEST;
  564. /* map it */
  565. smp_wmb();
  566. irq_map[virq].hwirq = hwirq;
  567. smp_mb();
  568. if (host->ops->map(host, virq, hwirq)) {
  569. pr_debug("irq: -> mapping failed, freeing\n");
  570. irq_free_virt(virq, 1);
  571. return NO_IRQ;
  572. }
  573. return virq;
  574. }
  575. EXPORT_SYMBOL_GPL(irq_create_mapping);
  576. unsigned int irq_create_of_mapping(struct device_node *controller,
  577. u32 *intspec, unsigned int intsize)
  578. {
  579. struct irq_host *host;
  580. irq_hw_number_t hwirq;
  581. unsigned int type = IRQ_TYPE_NONE;
  582. unsigned int virq;
  583. if (controller == NULL)
  584. host = irq_default_host;
  585. else
  586. host = irq_find_host(controller);
  587. if (host == NULL) {
  588. printk(KERN_WARNING "irq: no irq host found for %s !\n",
  589. controller->full_name);
  590. return NO_IRQ;
  591. }
  592. /* If host has no translation, then we assume interrupt line */
  593. if (host->ops->xlate == NULL)
  594. hwirq = intspec[0];
  595. else {
  596. if (host->ops->xlate(host, controller, intspec, intsize,
  597. &hwirq, &type))
  598. return NO_IRQ;
  599. }
  600. /* Create mapping */
  601. virq = irq_create_mapping(host, hwirq);
  602. if (virq == NO_IRQ)
  603. return virq;
  604. /* Set type if specified and different than the current one */
  605. if (type != IRQ_TYPE_NONE &&
  606. type != (get_irq_desc(virq)->status & IRQF_TRIGGER_MASK))
  607. set_irq_type(virq, type);
  608. return virq;
  609. }
  610. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  611. unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
  612. {
  613. struct of_irq oirq;
  614. if (of_irq_map_one(dev, index, &oirq))
  615. return NO_IRQ;
  616. return irq_create_of_mapping(oirq.controller, oirq.specifier,
  617. oirq.size);
  618. }
  619. EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
  620. void irq_dispose_mapping(unsigned int virq)
  621. {
  622. struct irq_host *host;
  623. irq_hw_number_t hwirq;
  624. unsigned long flags;
  625. if (virq == NO_IRQ)
  626. return;
  627. host = irq_map[virq].host;
  628. WARN_ON (host == NULL);
  629. if (host == NULL)
  630. return;
  631. /* Never unmap legacy interrupts */
  632. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  633. return;
  634. /* remove chip and handler */
  635. set_irq_chip_and_handler(virq, NULL, NULL);
  636. /* Make sure it's completed */
  637. synchronize_irq(virq);
  638. /* Tell the PIC about it */
  639. if (host->ops->unmap)
  640. host->ops->unmap(host, virq);
  641. smp_mb();
  642. /* Clear reverse map */
  643. hwirq = irq_map[virq].hwirq;
  644. switch(host->revmap_type) {
  645. case IRQ_HOST_MAP_LINEAR:
  646. if (hwirq < host->revmap_data.linear.size)
  647. host->revmap_data.linear.revmap[hwirq] = NO_IRQ;
  648. break;
  649. case IRQ_HOST_MAP_TREE:
  650. /* Check if radix tree allocated yet */
  651. if (host->revmap_data.tree.gfp_mask == 0)
  652. break;
  653. irq_radix_wrlock(&flags);
  654. radix_tree_delete(&host->revmap_data.tree, hwirq);
  655. irq_radix_wrunlock(flags);
  656. break;
  657. }
  658. /* Destroy map */
  659. smp_mb();
  660. irq_map[virq].hwirq = host->inval_irq;
  661. /* Set some flags */
  662. get_irq_desc(virq)->status |= IRQ_NOREQUEST;
  663. /* Free it */
  664. irq_free_virt(virq, 1);
  665. }
  666. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  667. unsigned int irq_find_mapping(struct irq_host *host,
  668. irq_hw_number_t hwirq)
  669. {
  670. unsigned int i;
  671. unsigned int hint = hwirq % irq_virq_count;
  672. /* Look for default host if nececssary */
  673. if (host == NULL)
  674. host = irq_default_host;
  675. if (host == NULL)
  676. return NO_IRQ;
  677. /* legacy -> bail early */
  678. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  679. return hwirq;
  680. /* Slow path does a linear search of the map */
  681. if (hint < NUM_ISA_INTERRUPTS)
  682. hint = NUM_ISA_INTERRUPTS;
  683. i = hint;
  684. do {
  685. if (irq_map[i].host == host &&
  686. irq_map[i].hwirq == hwirq)
  687. return i;
  688. i++;
  689. if (i >= irq_virq_count)
  690. i = NUM_ISA_INTERRUPTS;
  691. } while(i != hint);
  692. return NO_IRQ;
  693. }
  694. EXPORT_SYMBOL_GPL(irq_find_mapping);
  695. unsigned int irq_radix_revmap(struct irq_host *host,
  696. irq_hw_number_t hwirq)
  697. {
  698. struct radix_tree_root *tree;
  699. struct irq_map_entry *ptr;
  700. unsigned int virq;
  701. unsigned long flags;
  702. WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
  703. /* Check if the radix tree exist yet. We test the value of
  704. * the gfp_mask for that. Sneaky but saves another int in the
  705. * structure. If not, we fallback to slow mode
  706. */
  707. tree = &host->revmap_data.tree;
  708. if (tree->gfp_mask == 0)
  709. return irq_find_mapping(host, hwirq);
  710. /* Now try to resolve */
  711. irq_radix_rdlock(&flags);
  712. ptr = radix_tree_lookup(tree, hwirq);
  713. irq_radix_rdunlock(flags);
  714. /* Found it, return */
  715. if (ptr) {
  716. virq = ptr - irq_map;
  717. return virq;
  718. }
  719. /* If not there, try to insert it */
  720. virq = irq_find_mapping(host, hwirq);
  721. if (virq != NO_IRQ) {
  722. irq_radix_wrlock(&flags);
  723. radix_tree_insert(tree, hwirq, &irq_map[virq]);
  724. irq_radix_wrunlock(flags);
  725. }
  726. return virq;
  727. }
  728. unsigned int irq_linear_revmap(struct irq_host *host,
  729. irq_hw_number_t hwirq)
  730. {
  731. unsigned int *revmap;
  732. WARN_ON(host->revmap_type != IRQ_HOST_MAP_LINEAR);
  733. /* Check revmap bounds */
  734. if (unlikely(hwirq >= host->revmap_data.linear.size))
  735. return irq_find_mapping(host, hwirq);
  736. /* Check if revmap was allocated */
  737. revmap = host->revmap_data.linear.revmap;
  738. if (unlikely(revmap == NULL))
  739. return irq_find_mapping(host, hwirq);
  740. /* Fill up revmap with slow path if no mapping found */
  741. if (unlikely(revmap[hwirq] == NO_IRQ))
  742. revmap[hwirq] = irq_find_mapping(host, hwirq);
  743. return revmap[hwirq];
  744. }
  745. unsigned int irq_alloc_virt(struct irq_host *host,
  746. unsigned int count,
  747. unsigned int hint)
  748. {
  749. unsigned long flags;
  750. unsigned int i, j, found = NO_IRQ;
  751. if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
  752. return NO_IRQ;
  753. spin_lock_irqsave(&irq_big_lock, flags);
  754. /* Use hint for 1 interrupt if any */
  755. if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
  756. hint < irq_virq_count && irq_map[hint].host == NULL) {
  757. found = hint;
  758. goto hint_found;
  759. }
  760. /* Look for count consecutive numbers in the allocatable
  761. * (non-legacy) space
  762. */
  763. for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
  764. if (irq_map[i].host != NULL)
  765. j = 0;
  766. else
  767. j++;
  768. if (j == count) {
  769. found = i - count + 1;
  770. break;
  771. }
  772. }
  773. if (found == NO_IRQ) {
  774. spin_unlock_irqrestore(&irq_big_lock, flags);
  775. return NO_IRQ;
  776. }
  777. hint_found:
  778. for (i = found; i < (found + count); i++) {
  779. irq_map[i].hwirq = host->inval_irq;
  780. smp_wmb();
  781. irq_map[i].host = host;
  782. }
  783. spin_unlock_irqrestore(&irq_big_lock, flags);
  784. return found;
  785. }
  786. void irq_free_virt(unsigned int virq, unsigned int count)
  787. {
  788. unsigned long flags;
  789. unsigned int i;
  790. WARN_ON (virq < NUM_ISA_INTERRUPTS);
  791. WARN_ON (count == 0 || (virq + count) > irq_virq_count);
  792. spin_lock_irqsave(&irq_big_lock, flags);
  793. for (i = virq; i < (virq + count); i++) {
  794. struct irq_host *host;
  795. if (i < NUM_ISA_INTERRUPTS ||
  796. (virq + count) > irq_virq_count)
  797. continue;
  798. host = irq_map[i].host;
  799. irq_map[i].hwirq = host->inval_irq;
  800. smp_wmb();
  801. irq_map[i].host = NULL;
  802. }
  803. spin_unlock_irqrestore(&irq_big_lock, flags);
  804. }
  805. void irq_early_init(void)
  806. {
  807. unsigned int i;
  808. for (i = 0; i < NR_IRQS; i++)
  809. get_irq_desc(i)->status |= IRQ_NOREQUEST;
  810. }
  811. /* We need to create the radix trees late */
  812. static int irq_late_init(void)
  813. {
  814. struct irq_host *h;
  815. unsigned long flags;
  816. irq_radix_wrlock(&flags);
  817. list_for_each_entry(h, &irq_hosts, link) {
  818. if (h->revmap_type == IRQ_HOST_MAP_TREE)
  819. INIT_RADIX_TREE(&h->revmap_data.tree, GFP_ATOMIC);
  820. }
  821. irq_radix_wrunlock(flags);
  822. return 0;
  823. }
  824. arch_initcall(irq_late_init);
  825. #endif /* CONFIG_PPC_MERGE */
  826. #ifdef CONFIG_PPC64
  827. static int __init setup_noirqdistrib(char *str)
  828. {
  829. distribute_irqs = 0;
  830. return 1;
  831. }
  832. __setup("noirqdistrib", setup_noirqdistrib);
  833. #endif /* CONFIG_PPC64 */