irq.c 28 KB

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