irq.c 28 KB

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