irq.c 29 KB

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