irq.c 27 KB

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