irq.c 29 KB

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