irq.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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/export.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/smp.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. int distribute_irqs = 1;
  87. static inline notrace unsigned long get_irq_happened(void)
  88. {
  89. unsigned long happened;
  90. __asm__ __volatile__("lbz %0,%1(13)"
  91. : "=r" (happened) : "i" (offsetof(struct paca_struct, irq_happened)));
  92. return happened;
  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. static inline notrace int decrementer_check_overflow(void)
  100. {
  101. u64 now = get_tb_or_rtc();
  102. u64 *next_tb = &__get_cpu_var(decrementers_next_tb);
  103. if (now >= *next_tb)
  104. set_dec(1);
  105. return now >= *next_tb;
  106. }
  107. /* This is called whenever we are re-enabling interrupts
  108. * and returns either 0 (nothing to do) or 500/900 if there's
  109. * either an EE or a DEC to generate.
  110. *
  111. * This is called in two contexts: From arch_local_irq_restore()
  112. * before soft-enabling interrupts, and from the exception exit
  113. * path when returning from an interrupt from a soft-disabled to
  114. * a soft enabled context. In both case we have interrupts hard
  115. * disabled.
  116. *
  117. * We take care of only clearing the bits we handled in the
  118. * PACA irq_happened field since we can only re-emit one at a
  119. * time and we don't want to "lose" one.
  120. */
  121. notrace unsigned int __check_irq_replay(void)
  122. {
  123. /*
  124. * We use local_paca rather than get_paca() to avoid all
  125. * the debug_smp_processor_id() business in this low level
  126. * function
  127. */
  128. unsigned char happened = local_paca->irq_happened;
  129. /* Clear bit 0 which we wouldn't clear otherwise */
  130. local_paca->irq_happened &= ~PACA_IRQ_HARD_DIS;
  131. /*
  132. * Force the delivery of pending soft-disabled interrupts on PS3.
  133. * Any HV call will have this side effect.
  134. */
  135. if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
  136. u64 tmp, tmp2;
  137. lv1_get_version_info(&tmp, &tmp2);
  138. }
  139. /*
  140. * We may have missed a decrementer interrupt. We check the
  141. * decrementer itself rather than the paca irq_happened field
  142. * in case we also had a rollover while hard disabled
  143. */
  144. local_paca->irq_happened &= ~PACA_IRQ_DEC;
  145. if (decrementer_check_overflow())
  146. return 0x900;
  147. /* Finally check if an external interrupt happened */
  148. local_paca->irq_happened &= ~PACA_IRQ_EE;
  149. if (happened & PACA_IRQ_EE)
  150. return 0x500;
  151. #ifdef CONFIG_PPC_BOOK3E
  152. /* Finally check if an EPR external interrupt happened
  153. * this bit is typically set if we need to handle another
  154. * "edge" interrupt from within the MPIC "EPR" handler
  155. */
  156. local_paca->irq_happened &= ~PACA_IRQ_EE_EDGE;
  157. if (happened & PACA_IRQ_EE_EDGE)
  158. return 0x500;
  159. local_paca->irq_happened &= ~PACA_IRQ_DBELL;
  160. if (happened & PACA_IRQ_DBELL)
  161. return 0x280;
  162. #endif /* CONFIG_PPC_BOOK3E */
  163. /* There should be nothing left ! */
  164. BUG_ON(local_paca->irq_happened != 0);
  165. return 0;
  166. }
  167. notrace void arch_local_irq_restore(unsigned long en)
  168. {
  169. unsigned char irq_happened;
  170. unsigned int replay;
  171. /* Write the new soft-enabled value */
  172. set_soft_enabled(en);
  173. if (!en)
  174. return;
  175. /*
  176. * From this point onward, we can take interrupts, preempt,
  177. * etc... unless we got hard-disabled. We check if an event
  178. * happened. If none happened, we know we can just return.
  179. *
  180. * We may have preempted before the check below, in which case
  181. * we are checking the "new" CPU instead of the old one. This
  182. * is only a problem if an event happened on the "old" CPU.
  183. *
  184. * External interrupt events on non-iseries will have caused
  185. * interrupts to be hard-disabled, so there is no problem, we
  186. * cannot have preempted.
  187. *
  188. * That leaves us with EEs on iSeries or decrementer interrupts,
  189. * which I decided to safely ignore. The preemption would have
  190. * itself been the result of an interrupt, upon which return we
  191. * will have checked for pending events on the old CPU.
  192. */
  193. irq_happened = get_irq_happened();
  194. if (!irq_happened)
  195. return;
  196. /*
  197. * We need to hard disable to get a trusted value from
  198. * __check_irq_replay(). We also need to soft-disable
  199. * again to avoid warnings in there due to the use of
  200. * per-cpu variables.
  201. *
  202. * We know that if the value in irq_happened is exactly 0x01
  203. * then we are already hard disabled (there are other less
  204. * common cases that we'll ignore for now), so we skip the
  205. * (expensive) mtmsrd.
  206. */
  207. if (unlikely(irq_happened != PACA_IRQ_HARD_DIS))
  208. __hard_irq_disable();
  209. set_soft_enabled(0);
  210. /*
  211. * Check if anything needs to be re-emitted. We haven't
  212. * soft-enabled yet to avoid warnings in decrementer_check_overflow
  213. * accessing per-cpu variables
  214. */
  215. replay = __check_irq_replay();
  216. /* We can soft-enable now */
  217. set_soft_enabled(1);
  218. /*
  219. * And replay if we have to. This will return with interrupts
  220. * hard-enabled.
  221. */
  222. if (replay) {
  223. __replay_interrupt(replay);
  224. return;
  225. }
  226. /* Finally, let's ensure we are hard enabled */
  227. __hard_irq_enable();
  228. }
  229. EXPORT_SYMBOL(arch_local_irq_restore);
  230. /*
  231. * This is specifically called by assembly code to re-enable interrupts
  232. * if they are currently disabled. This is typically called before
  233. * schedule() or do_signal() when returning to userspace. We do it
  234. * in C to avoid the burden of dealing with lockdep etc...
  235. */
  236. void restore_interrupts(void)
  237. {
  238. if (irqs_disabled())
  239. local_irq_enable();
  240. }
  241. #endif /* CONFIG_PPC64 */
  242. int arch_show_interrupts(struct seq_file *p, int prec)
  243. {
  244. int j;
  245. #if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT)
  246. if (tau_initialized) {
  247. seq_printf(p, "%*s: ", prec, "TAU");
  248. for_each_online_cpu(j)
  249. seq_printf(p, "%10u ", tau_interrupts(j));
  250. seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
  251. }
  252. #endif /* CONFIG_PPC32 && CONFIG_TAU_INT */
  253. seq_printf(p, "%*s: ", prec, "LOC");
  254. for_each_online_cpu(j)
  255. seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs);
  256. seq_printf(p, " Local timer interrupts\n");
  257. seq_printf(p, "%*s: ", prec, "SPU");
  258. for_each_online_cpu(j)
  259. seq_printf(p, "%10u ", per_cpu(irq_stat, j).spurious_irqs);
  260. seq_printf(p, " Spurious interrupts\n");
  261. seq_printf(p, "%*s: ", prec, "CNT");
  262. for_each_online_cpu(j)
  263. seq_printf(p, "%10u ", per_cpu(irq_stat, j).pmu_irqs);
  264. seq_printf(p, " Performance monitoring interrupts\n");
  265. seq_printf(p, "%*s: ", prec, "MCE");
  266. for_each_online_cpu(j)
  267. seq_printf(p, "%10u ", per_cpu(irq_stat, j).mce_exceptions);
  268. seq_printf(p, " Machine check exceptions\n");
  269. return 0;
  270. }
  271. /*
  272. * /proc/stat helpers
  273. */
  274. u64 arch_irq_stat_cpu(unsigned int cpu)
  275. {
  276. u64 sum = per_cpu(irq_stat, cpu).timer_irqs;
  277. sum += per_cpu(irq_stat, cpu).pmu_irqs;
  278. sum += per_cpu(irq_stat, cpu).mce_exceptions;
  279. sum += per_cpu(irq_stat, cpu).spurious_irqs;
  280. return sum;
  281. }
  282. #ifdef CONFIG_HOTPLUG_CPU
  283. void migrate_irqs(void)
  284. {
  285. struct irq_desc *desc;
  286. unsigned int irq;
  287. static int warned;
  288. cpumask_var_t mask;
  289. const struct cpumask *map = cpu_online_mask;
  290. alloc_cpumask_var(&mask, GFP_KERNEL);
  291. for_each_irq(irq) {
  292. struct irq_data *data;
  293. struct irq_chip *chip;
  294. desc = irq_to_desc(irq);
  295. if (!desc)
  296. continue;
  297. data = irq_desc_get_irq_data(desc);
  298. if (irqd_is_per_cpu(data))
  299. continue;
  300. chip = irq_data_get_irq_chip(data);
  301. cpumask_and(mask, data->affinity, map);
  302. if (cpumask_any(mask) >= nr_cpu_ids) {
  303. printk("Breaking affinity for irq %i\n", irq);
  304. cpumask_copy(mask, map);
  305. }
  306. if (chip->irq_set_affinity)
  307. chip->irq_set_affinity(data, mask, true);
  308. else if (desc->action && !(warned++))
  309. printk("Cannot set affinity for irq %i\n", irq);
  310. }
  311. free_cpumask_var(mask);
  312. local_irq_enable();
  313. mdelay(1);
  314. local_irq_disable();
  315. }
  316. #endif
  317. static inline void handle_one_irq(unsigned int irq)
  318. {
  319. struct thread_info *curtp, *irqtp;
  320. unsigned long saved_sp_limit;
  321. struct irq_desc *desc;
  322. desc = irq_to_desc(irq);
  323. if (!desc)
  324. return;
  325. /* Switch to the irq stack to handle this */
  326. curtp = current_thread_info();
  327. irqtp = hardirq_ctx[smp_processor_id()];
  328. if (curtp == irqtp) {
  329. /* We're already on the irq stack, just handle it */
  330. desc->handle_irq(irq, desc);
  331. return;
  332. }
  333. saved_sp_limit = current->thread.ksp_limit;
  334. irqtp->task = curtp->task;
  335. irqtp->flags = 0;
  336. /* Copy the softirq bits in preempt_count so that the
  337. * softirq checks work in the hardirq context. */
  338. irqtp->preempt_count = (irqtp->preempt_count & ~SOFTIRQ_MASK) |
  339. (curtp->preempt_count & SOFTIRQ_MASK);
  340. current->thread.ksp_limit = (unsigned long)irqtp +
  341. _ALIGN_UP(sizeof(struct thread_info), 16);
  342. call_handle_irq(irq, desc, irqtp, desc->handle_irq);
  343. current->thread.ksp_limit = saved_sp_limit;
  344. irqtp->task = NULL;
  345. /* Set any flag that may have been set on the
  346. * alternate stack
  347. */
  348. if (irqtp->flags)
  349. set_bits(irqtp->flags, &curtp->flags);
  350. }
  351. static inline void check_stack_overflow(void)
  352. {
  353. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  354. long sp;
  355. sp = __get_SP() & (THREAD_SIZE-1);
  356. /* check for stack overflow: is there less than 2KB free? */
  357. if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
  358. printk("do_IRQ: stack overflow: %ld\n",
  359. sp - sizeof(struct thread_info));
  360. dump_stack();
  361. }
  362. #endif
  363. }
  364. void do_IRQ(struct pt_regs *regs)
  365. {
  366. struct pt_regs *old_regs = set_irq_regs(regs);
  367. unsigned int irq;
  368. trace_irq_entry(regs);
  369. irq_enter();
  370. check_stack_overflow();
  371. /*
  372. * Query the platform PIC for the interrupt & ack it.
  373. *
  374. * This will typically lower the interrupt line to the CPU
  375. */
  376. irq = ppc_md.get_irq();
  377. /* We can hard enable interrupts now */
  378. may_hard_irq_enable();
  379. /* And finally process it */
  380. if (irq != NO_IRQ && irq != NO_IRQ_IGNORE)
  381. handle_one_irq(irq);
  382. else if (irq != NO_IRQ_IGNORE)
  383. __get_cpu_var(irq_stat).spurious_irqs++;
  384. irq_exit();
  385. set_irq_regs(old_regs);
  386. #ifdef CONFIG_PPC_ISERIES
  387. if (firmware_has_feature(FW_FEATURE_ISERIES) &&
  388. get_lppaca()->int_dword.fields.decr_int) {
  389. get_lppaca()->int_dword.fields.decr_int = 0;
  390. /* Signal a fake decrementer interrupt */
  391. timer_interrupt(regs);
  392. }
  393. #endif
  394. trace_irq_exit(regs);
  395. }
  396. void __init init_IRQ(void)
  397. {
  398. if (ppc_md.init_IRQ)
  399. ppc_md.init_IRQ();
  400. exc_lvl_ctx_init();
  401. irq_ctx_init();
  402. }
  403. #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
  404. struct thread_info *critirq_ctx[NR_CPUS] __read_mostly;
  405. struct thread_info *dbgirq_ctx[NR_CPUS] __read_mostly;
  406. struct thread_info *mcheckirq_ctx[NR_CPUS] __read_mostly;
  407. void exc_lvl_ctx_init(void)
  408. {
  409. struct thread_info *tp;
  410. int i, cpu_nr;
  411. for_each_possible_cpu(i) {
  412. #ifdef CONFIG_PPC64
  413. cpu_nr = i;
  414. #else
  415. cpu_nr = get_hard_smp_processor_id(i);
  416. #endif
  417. memset((void *)critirq_ctx[cpu_nr], 0, THREAD_SIZE);
  418. tp = critirq_ctx[cpu_nr];
  419. tp->cpu = cpu_nr;
  420. tp->preempt_count = 0;
  421. #ifdef CONFIG_BOOKE
  422. memset((void *)dbgirq_ctx[cpu_nr], 0, THREAD_SIZE);
  423. tp = dbgirq_ctx[cpu_nr];
  424. tp->cpu = cpu_nr;
  425. tp->preempt_count = 0;
  426. memset((void *)mcheckirq_ctx[cpu_nr], 0, THREAD_SIZE);
  427. tp = mcheckirq_ctx[cpu_nr];
  428. tp->cpu = cpu_nr;
  429. tp->preempt_count = HARDIRQ_OFFSET;
  430. #endif
  431. }
  432. }
  433. #endif
  434. struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
  435. struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
  436. void irq_ctx_init(void)
  437. {
  438. struct thread_info *tp;
  439. int i;
  440. for_each_possible_cpu(i) {
  441. memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
  442. tp = softirq_ctx[i];
  443. tp->cpu = i;
  444. tp->preempt_count = 0;
  445. memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
  446. tp = hardirq_ctx[i];
  447. tp->cpu = i;
  448. tp->preempt_count = HARDIRQ_OFFSET;
  449. }
  450. }
  451. static inline void do_softirq_onstack(void)
  452. {
  453. struct thread_info *curtp, *irqtp;
  454. unsigned long saved_sp_limit = current->thread.ksp_limit;
  455. curtp = current_thread_info();
  456. irqtp = softirq_ctx[smp_processor_id()];
  457. irqtp->task = curtp->task;
  458. irqtp->flags = 0;
  459. current->thread.ksp_limit = (unsigned long)irqtp +
  460. _ALIGN_UP(sizeof(struct thread_info), 16);
  461. call_do_softirq(irqtp);
  462. current->thread.ksp_limit = saved_sp_limit;
  463. irqtp->task = NULL;
  464. /* Set any flag that may have been set on the
  465. * alternate stack
  466. */
  467. if (irqtp->flags)
  468. set_bits(irqtp->flags, &curtp->flags);
  469. }
  470. void do_softirq(void)
  471. {
  472. unsigned long flags;
  473. if (in_interrupt())
  474. return;
  475. local_irq_save(flags);
  476. if (local_softirq_pending())
  477. do_softirq_onstack();
  478. local_irq_restore(flags);
  479. }
  480. /*
  481. * IRQ controller and virtual interrupts
  482. */
  483. /* The main irq map itself is an array of NR_IRQ entries containing the
  484. * associate host and irq number. An entry with a host of NULL is free.
  485. * An entry can be allocated if it's free, the allocator always then sets
  486. * hwirq first to the host's invalid irq number and then fills ops.
  487. */
  488. struct irq_map_entry {
  489. irq_hw_number_t hwirq;
  490. struct irq_host *host;
  491. };
  492. static LIST_HEAD(irq_hosts);
  493. static DEFINE_RAW_SPINLOCK(irq_big_lock);
  494. static DEFINE_MUTEX(revmap_trees_mutex);
  495. static struct irq_map_entry irq_map[NR_IRQS];
  496. static unsigned int irq_virq_count = NR_IRQS;
  497. static struct irq_host *irq_default_host;
  498. irq_hw_number_t irqd_to_hwirq(struct irq_data *d)
  499. {
  500. return irq_map[d->irq].hwirq;
  501. }
  502. EXPORT_SYMBOL_GPL(irqd_to_hwirq);
  503. irq_hw_number_t virq_to_hw(unsigned int virq)
  504. {
  505. return irq_map[virq].hwirq;
  506. }
  507. EXPORT_SYMBOL_GPL(virq_to_hw);
  508. bool virq_is_host(unsigned int virq, struct irq_host *host)
  509. {
  510. return irq_map[virq].host == host;
  511. }
  512. EXPORT_SYMBOL_GPL(virq_is_host);
  513. static int default_irq_host_match(struct irq_host *h, struct device_node *np)
  514. {
  515. return h->of_node != NULL && h->of_node == np;
  516. }
  517. struct irq_host *irq_alloc_host(struct device_node *of_node,
  518. unsigned int revmap_type,
  519. unsigned int revmap_arg,
  520. struct irq_host_ops *ops,
  521. irq_hw_number_t inval_irq)
  522. {
  523. struct irq_host *host;
  524. unsigned int size = sizeof(struct irq_host);
  525. unsigned int i;
  526. unsigned int *rmap;
  527. unsigned long flags;
  528. /* Allocate structure and revmap table if using linear mapping */
  529. if (revmap_type == IRQ_HOST_MAP_LINEAR)
  530. size += revmap_arg * sizeof(unsigned int);
  531. host = kzalloc(size, GFP_KERNEL);
  532. if (host == NULL)
  533. return NULL;
  534. /* Fill structure */
  535. host->revmap_type = revmap_type;
  536. host->inval_irq = inval_irq;
  537. host->ops = ops;
  538. host->of_node = of_node_get(of_node);
  539. if (host->ops->match == NULL)
  540. host->ops->match = default_irq_host_match;
  541. raw_spin_lock_irqsave(&irq_big_lock, flags);
  542. /* If it's a legacy controller, check for duplicates and
  543. * mark it as allocated (we use irq 0 host pointer for that
  544. */
  545. if (revmap_type == IRQ_HOST_MAP_LEGACY) {
  546. if (irq_map[0].host != NULL) {
  547. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  548. of_node_put(host->of_node);
  549. kfree(host);
  550. return NULL;
  551. }
  552. irq_map[0].host = host;
  553. }
  554. list_add(&host->link, &irq_hosts);
  555. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  556. /* Additional setups per revmap type */
  557. switch(revmap_type) {
  558. case IRQ_HOST_MAP_LEGACY:
  559. /* 0 is always the invalid number for legacy */
  560. host->inval_irq = 0;
  561. /* setup us as the host for all legacy interrupts */
  562. for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
  563. irq_map[i].hwirq = i;
  564. smp_wmb();
  565. irq_map[i].host = host;
  566. smp_wmb();
  567. /* Legacy flags are left to default at this point,
  568. * one can then use irq_create_mapping() to
  569. * explicitly change them
  570. */
  571. ops->map(host, i, i);
  572. /* Clear norequest flags */
  573. irq_clear_status_flags(i, IRQ_NOREQUEST);
  574. }
  575. break;
  576. case IRQ_HOST_MAP_LINEAR:
  577. rmap = (unsigned int *)(host + 1);
  578. for (i = 0; i < revmap_arg; i++)
  579. rmap[i] = NO_IRQ;
  580. host->revmap_data.linear.size = revmap_arg;
  581. smp_wmb();
  582. host->revmap_data.linear.revmap = rmap;
  583. break;
  584. case IRQ_HOST_MAP_TREE:
  585. INIT_RADIX_TREE(&host->revmap_data.tree, GFP_KERNEL);
  586. break;
  587. default:
  588. break;
  589. }
  590. pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
  591. return host;
  592. }
  593. struct irq_host *irq_find_host(struct device_node *node)
  594. {
  595. struct irq_host *h, *found = NULL;
  596. unsigned long flags;
  597. /* We might want to match the legacy controller last since
  598. * it might potentially be set to match all interrupts in
  599. * the absence of a device node. This isn't a problem so far
  600. * yet though...
  601. */
  602. raw_spin_lock_irqsave(&irq_big_lock, flags);
  603. list_for_each_entry(h, &irq_hosts, link)
  604. if (h->ops->match(h, node)) {
  605. found = h;
  606. break;
  607. }
  608. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  609. return found;
  610. }
  611. EXPORT_SYMBOL_GPL(irq_find_host);
  612. void irq_set_default_host(struct irq_host *host)
  613. {
  614. pr_debug("irq: Default host set to @0x%p\n", host);
  615. irq_default_host = host;
  616. }
  617. void irq_set_virq_count(unsigned int count)
  618. {
  619. pr_debug("irq: Trying to set virq count to %d\n", count);
  620. BUG_ON(count < NUM_ISA_INTERRUPTS);
  621. if (count < NR_IRQS)
  622. irq_virq_count = count;
  623. }
  624. static int irq_setup_virq(struct irq_host *host, unsigned int virq,
  625. irq_hw_number_t hwirq)
  626. {
  627. int res;
  628. res = irq_alloc_desc_at(virq, 0);
  629. if (res != virq) {
  630. pr_debug("irq: -> allocating desc failed\n");
  631. goto error;
  632. }
  633. /* map it */
  634. smp_wmb();
  635. irq_map[virq].hwirq = hwirq;
  636. smp_mb();
  637. if (host->ops->map(host, virq, hwirq)) {
  638. pr_debug("irq: -> mapping failed, freeing\n");
  639. goto errdesc;
  640. }
  641. irq_clear_status_flags(virq, IRQ_NOREQUEST);
  642. return 0;
  643. errdesc:
  644. irq_free_descs(virq, 1);
  645. error:
  646. irq_free_virt(virq, 1);
  647. return -1;
  648. }
  649. unsigned int irq_create_direct_mapping(struct irq_host *host)
  650. {
  651. unsigned int virq;
  652. if (host == NULL)
  653. host = irq_default_host;
  654. BUG_ON(host == NULL);
  655. WARN_ON(host->revmap_type != IRQ_HOST_MAP_NOMAP);
  656. virq = irq_alloc_virt(host, 1, 0);
  657. if (virq == NO_IRQ) {
  658. pr_debug("irq: create_direct virq allocation failed\n");
  659. return NO_IRQ;
  660. }
  661. pr_debug("irq: create_direct obtained virq %d\n", virq);
  662. if (irq_setup_virq(host, virq, virq))
  663. return NO_IRQ;
  664. return virq;
  665. }
  666. unsigned int irq_create_mapping(struct irq_host *host,
  667. irq_hw_number_t hwirq)
  668. {
  669. unsigned int virq, hint;
  670. pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
  671. /* Look for default host if nececssary */
  672. if (host == NULL)
  673. host = irq_default_host;
  674. if (host == NULL) {
  675. printk(KERN_WARNING "irq_create_mapping called for"
  676. " NULL host, hwirq=%lx\n", hwirq);
  677. WARN_ON(1);
  678. return NO_IRQ;
  679. }
  680. pr_debug("irq: -> using host @%p\n", host);
  681. /* Check if mapping already exists */
  682. virq = irq_find_mapping(host, hwirq);
  683. if (virq != NO_IRQ) {
  684. pr_debug("irq: -> existing mapping on virq %d\n", virq);
  685. return virq;
  686. }
  687. /* Get a virtual interrupt number */
  688. if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
  689. /* Handle legacy */
  690. virq = (unsigned int)hwirq;
  691. if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
  692. return NO_IRQ;
  693. return virq;
  694. } else {
  695. /* Allocate a virtual interrupt number */
  696. hint = hwirq % irq_virq_count;
  697. virq = irq_alloc_virt(host, 1, hint);
  698. if (virq == NO_IRQ) {
  699. pr_debug("irq: -> virq allocation failed\n");
  700. return NO_IRQ;
  701. }
  702. }
  703. if (irq_setup_virq(host, virq, hwirq))
  704. return NO_IRQ;
  705. pr_debug("irq: irq %lu on host %s mapped to virtual irq %u\n",
  706. hwirq, host->of_node ? host->of_node->full_name : "null", virq);
  707. return virq;
  708. }
  709. EXPORT_SYMBOL_GPL(irq_create_mapping);
  710. unsigned int irq_create_of_mapping(struct device_node *controller,
  711. const u32 *intspec, unsigned int intsize)
  712. {
  713. struct irq_host *host;
  714. irq_hw_number_t hwirq;
  715. unsigned int type = IRQ_TYPE_NONE;
  716. unsigned int virq;
  717. if (controller == NULL)
  718. host = irq_default_host;
  719. else
  720. host = irq_find_host(controller);
  721. if (host == NULL) {
  722. printk(KERN_WARNING "irq: no irq host found for %s !\n",
  723. controller->full_name);
  724. return NO_IRQ;
  725. }
  726. /* If host has no translation, then we assume interrupt line */
  727. if (host->ops->xlate == NULL)
  728. hwirq = intspec[0];
  729. else {
  730. if (host->ops->xlate(host, controller, intspec, intsize,
  731. &hwirq, &type))
  732. return NO_IRQ;
  733. }
  734. /* Create mapping */
  735. virq = irq_create_mapping(host, hwirq);
  736. if (virq == NO_IRQ)
  737. return virq;
  738. /* Set type if specified and different than the current one */
  739. if (type != IRQ_TYPE_NONE &&
  740. type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
  741. irq_set_irq_type(virq, type);
  742. return virq;
  743. }
  744. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  745. void irq_dispose_mapping(unsigned int virq)
  746. {
  747. struct irq_host *host;
  748. irq_hw_number_t hwirq;
  749. if (virq == NO_IRQ)
  750. return;
  751. host = irq_map[virq].host;
  752. if (WARN_ON(host == NULL))
  753. return;
  754. /* Never unmap legacy interrupts */
  755. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  756. return;
  757. irq_set_status_flags(virq, IRQ_NOREQUEST);
  758. /* remove chip and handler */
  759. irq_set_chip_and_handler(virq, NULL, NULL);
  760. /* Make sure it's completed */
  761. synchronize_irq(virq);
  762. /* Tell the PIC about it */
  763. if (host->ops->unmap)
  764. host->ops->unmap(host, virq);
  765. smp_mb();
  766. /* Clear reverse map */
  767. hwirq = irq_map[virq].hwirq;
  768. switch(host->revmap_type) {
  769. case IRQ_HOST_MAP_LINEAR:
  770. if (hwirq < host->revmap_data.linear.size)
  771. host->revmap_data.linear.revmap[hwirq] = NO_IRQ;
  772. break;
  773. case IRQ_HOST_MAP_TREE:
  774. mutex_lock(&revmap_trees_mutex);
  775. radix_tree_delete(&host->revmap_data.tree, hwirq);
  776. mutex_unlock(&revmap_trees_mutex);
  777. break;
  778. }
  779. /* Destroy map */
  780. smp_mb();
  781. irq_map[virq].hwirq = host->inval_irq;
  782. irq_free_descs(virq, 1);
  783. /* Free it */
  784. irq_free_virt(virq, 1);
  785. }
  786. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  787. unsigned int irq_find_mapping(struct irq_host *host,
  788. irq_hw_number_t hwirq)
  789. {
  790. unsigned int i;
  791. unsigned int hint = hwirq % irq_virq_count;
  792. /* Look for default host if nececssary */
  793. if (host == NULL)
  794. host = irq_default_host;
  795. if (host == NULL)
  796. return NO_IRQ;
  797. /* legacy -> bail early */
  798. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  799. return hwirq;
  800. /* Slow path does a linear search of the map */
  801. if (hint < NUM_ISA_INTERRUPTS)
  802. hint = NUM_ISA_INTERRUPTS;
  803. i = hint;
  804. do {
  805. if (irq_map[i].host == host &&
  806. irq_map[i].hwirq == hwirq)
  807. return i;
  808. i++;
  809. if (i >= irq_virq_count)
  810. i = NUM_ISA_INTERRUPTS;
  811. } while(i != hint);
  812. return NO_IRQ;
  813. }
  814. EXPORT_SYMBOL_GPL(irq_find_mapping);
  815. #ifdef CONFIG_SMP
  816. int irq_choose_cpu(const struct cpumask *mask)
  817. {
  818. int cpuid;
  819. if (cpumask_equal(mask, cpu_all_mask)) {
  820. static int irq_rover;
  821. static DEFINE_RAW_SPINLOCK(irq_rover_lock);
  822. unsigned long flags;
  823. /* Round-robin distribution... */
  824. do_round_robin:
  825. raw_spin_lock_irqsave(&irq_rover_lock, flags);
  826. irq_rover = cpumask_next(irq_rover, cpu_online_mask);
  827. if (irq_rover >= nr_cpu_ids)
  828. irq_rover = cpumask_first(cpu_online_mask);
  829. cpuid = irq_rover;
  830. raw_spin_unlock_irqrestore(&irq_rover_lock, flags);
  831. } else {
  832. cpuid = cpumask_first_and(mask, cpu_online_mask);
  833. if (cpuid >= nr_cpu_ids)
  834. goto do_round_robin;
  835. }
  836. return get_hard_smp_processor_id(cpuid);
  837. }
  838. #else
  839. int irq_choose_cpu(const struct cpumask *mask)
  840. {
  841. return hard_smp_processor_id();
  842. }
  843. #endif
  844. unsigned int irq_radix_revmap_lookup(struct irq_host *host,
  845. irq_hw_number_t hwirq)
  846. {
  847. struct irq_map_entry *ptr;
  848. unsigned int virq;
  849. if (WARN_ON_ONCE(host->revmap_type != IRQ_HOST_MAP_TREE))
  850. return irq_find_mapping(host, hwirq);
  851. /*
  852. * The ptr returned references the static global irq_map.
  853. * but freeing an irq can delete nodes along the path to
  854. * do the lookup via call_rcu.
  855. */
  856. rcu_read_lock();
  857. ptr = radix_tree_lookup(&host->revmap_data.tree, hwirq);
  858. rcu_read_unlock();
  859. /*
  860. * If found in radix tree, then fine.
  861. * Else fallback to linear lookup - this should not happen in practice
  862. * as it means that we failed to insert the node in the radix tree.
  863. */
  864. if (ptr)
  865. virq = ptr - irq_map;
  866. else
  867. virq = irq_find_mapping(host, hwirq);
  868. return virq;
  869. }
  870. void irq_radix_revmap_insert(struct irq_host *host, unsigned int virq,
  871. irq_hw_number_t hwirq)
  872. {
  873. if (WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE))
  874. return;
  875. if (virq != NO_IRQ) {
  876. mutex_lock(&revmap_trees_mutex);
  877. radix_tree_insert(&host->revmap_data.tree, hwirq,
  878. &irq_map[virq]);
  879. mutex_unlock(&revmap_trees_mutex);
  880. }
  881. }
  882. unsigned int irq_linear_revmap(struct irq_host *host,
  883. irq_hw_number_t hwirq)
  884. {
  885. unsigned int *revmap;
  886. if (WARN_ON_ONCE(host->revmap_type != IRQ_HOST_MAP_LINEAR))
  887. return irq_find_mapping(host, hwirq);
  888. /* Check revmap bounds */
  889. if (unlikely(hwirq >= host->revmap_data.linear.size))
  890. return irq_find_mapping(host, hwirq);
  891. /* Check if revmap was allocated */
  892. revmap = host->revmap_data.linear.revmap;
  893. if (unlikely(revmap == NULL))
  894. return irq_find_mapping(host, hwirq);
  895. /* Fill up revmap with slow path if no mapping found */
  896. if (unlikely(revmap[hwirq] == NO_IRQ))
  897. revmap[hwirq] = irq_find_mapping(host, hwirq);
  898. return revmap[hwirq];
  899. }
  900. unsigned int irq_alloc_virt(struct irq_host *host,
  901. unsigned int count,
  902. unsigned int hint)
  903. {
  904. unsigned long flags;
  905. unsigned int i, j, found = NO_IRQ;
  906. if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
  907. return NO_IRQ;
  908. raw_spin_lock_irqsave(&irq_big_lock, flags);
  909. /* Use hint for 1 interrupt if any */
  910. if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
  911. hint < irq_virq_count && irq_map[hint].host == NULL) {
  912. found = hint;
  913. goto hint_found;
  914. }
  915. /* Look for count consecutive numbers in the allocatable
  916. * (non-legacy) space
  917. */
  918. for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
  919. if (irq_map[i].host != NULL)
  920. j = 0;
  921. else
  922. j++;
  923. if (j == count) {
  924. found = i - count + 1;
  925. break;
  926. }
  927. }
  928. if (found == NO_IRQ) {
  929. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  930. return NO_IRQ;
  931. }
  932. hint_found:
  933. for (i = found; i < (found + count); i++) {
  934. irq_map[i].hwirq = host->inval_irq;
  935. smp_wmb();
  936. irq_map[i].host = host;
  937. }
  938. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  939. return found;
  940. }
  941. void irq_free_virt(unsigned int virq, unsigned int count)
  942. {
  943. unsigned long flags;
  944. unsigned int i;
  945. WARN_ON (virq < NUM_ISA_INTERRUPTS);
  946. WARN_ON (count == 0 || (virq + count) > irq_virq_count);
  947. if (virq < NUM_ISA_INTERRUPTS) {
  948. if (virq + count < NUM_ISA_INTERRUPTS)
  949. return;
  950. count =- NUM_ISA_INTERRUPTS - virq;
  951. virq = NUM_ISA_INTERRUPTS;
  952. }
  953. if (count > irq_virq_count || virq > irq_virq_count - count) {
  954. if (virq > irq_virq_count)
  955. return;
  956. count = irq_virq_count - virq;
  957. }
  958. raw_spin_lock_irqsave(&irq_big_lock, flags);
  959. for (i = virq; i < (virq + count); i++) {
  960. struct irq_host *host;
  961. host = irq_map[i].host;
  962. irq_map[i].hwirq = host->inval_irq;
  963. smp_wmb();
  964. irq_map[i].host = NULL;
  965. }
  966. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  967. }
  968. int arch_early_irq_init(void)
  969. {
  970. return 0;
  971. }
  972. #ifdef CONFIG_VIRQ_DEBUG
  973. static int virq_debug_show(struct seq_file *m, void *private)
  974. {
  975. unsigned long flags;
  976. struct irq_desc *desc;
  977. const char *p;
  978. static const char none[] = "none";
  979. void *data;
  980. int i;
  981. seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
  982. "chip name", "chip data", "host name");
  983. for (i = 1; i < nr_irqs; i++) {
  984. desc = irq_to_desc(i);
  985. if (!desc)
  986. continue;
  987. raw_spin_lock_irqsave(&desc->lock, flags);
  988. if (desc->action && desc->action->handler) {
  989. struct irq_chip *chip;
  990. seq_printf(m, "%5d ", i);
  991. seq_printf(m, "0x%05lx ", irq_map[i].hwirq);
  992. chip = irq_desc_get_chip(desc);
  993. if (chip && chip->name)
  994. p = chip->name;
  995. else
  996. p = none;
  997. seq_printf(m, "%-15s ", p);
  998. data = irq_desc_get_chip_data(desc);
  999. seq_printf(m, "0x%16p ", data);
  1000. if (irq_map[i].host && irq_map[i].host->of_node)
  1001. p = irq_map[i].host->of_node->full_name;
  1002. else
  1003. p = none;
  1004. seq_printf(m, "%s\n", p);
  1005. }
  1006. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1007. }
  1008. return 0;
  1009. }
  1010. static int virq_debug_open(struct inode *inode, struct file *file)
  1011. {
  1012. return single_open(file, virq_debug_show, inode->i_private);
  1013. }
  1014. static const struct file_operations virq_debug_fops = {
  1015. .open = virq_debug_open,
  1016. .read = seq_read,
  1017. .llseek = seq_lseek,
  1018. .release = single_release,
  1019. };
  1020. static int __init irq_debugfs_init(void)
  1021. {
  1022. if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
  1023. NULL, &virq_debug_fops) == NULL)
  1024. return -ENOMEM;
  1025. return 0;
  1026. }
  1027. __initcall(irq_debugfs_init);
  1028. #endif /* CONFIG_VIRQ_DEBUG */
  1029. #ifdef CONFIG_PPC64
  1030. static int __init setup_noirqdistrib(char *str)
  1031. {
  1032. distribute_irqs = 0;
  1033. return 1;
  1034. }
  1035. __setup("noirqdistrib", setup_noirqdistrib);
  1036. #endif /* CONFIG_PPC64 */