irq.c 28 KB

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