irq.c 27 KB

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