irq.c 29 KB

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