irq.c 29 KB

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