irq.c 29 KB

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