irq.c 28 KB

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