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