irq.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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(const struct cpumask *map)
  251. {
  252. struct irq_desc *desc;
  253. unsigned int irq;
  254. static int warned;
  255. cpumask_var_t mask;
  256. alloc_cpumask_var(&mask, GFP_KERNEL);
  257. for_each_irq(irq) {
  258. desc = irq_to_desc(irq);
  259. if (desc && desc->status & IRQ_PER_CPU)
  260. continue;
  261. cpumask_and(mask, desc->affinity, map);
  262. if (cpumask_any(mask) >= nr_cpu_ids) {
  263. printk("Breaking affinity for irq %i\n", irq);
  264. cpumask_copy(mask, map);
  265. }
  266. if (desc->chip->set_affinity)
  267. desc->chip->set_affinity(irq, mask);
  268. else if (desc->action && !(warned++))
  269. printk("Cannot set affinity for irq %i\n", irq);
  270. }
  271. free_cpumask_var(mask);
  272. local_irq_enable();
  273. mdelay(1);
  274. local_irq_disable();
  275. }
  276. #endif
  277. #ifdef CONFIG_IRQSTACKS
  278. static inline void handle_one_irq(unsigned int irq)
  279. {
  280. struct thread_info *curtp, *irqtp;
  281. unsigned long saved_sp_limit;
  282. struct irq_desc *desc;
  283. /* Switch to the irq stack to handle this */
  284. curtp = current_thread_info();
  285. irqtp = hardirq_ctx[smp_processor_id()];
  286. if (curtp == irqtp) {
  287. /* We're already on the irq stack, just handle it */
  288. generic_handle_irq(irq);
  289. return;
  290. }
  291. desc = irq_to_desc(irq);
  292. saved_sp_limit = current->thread.ksp_limit;
  293. irqtp->task = curtp->task;
  294. irqtp->flags = 0;
  295. /* Copy the softirq bits in preempt_count so that the
  296. * softirq checks work in the hardirq context. */
  297. irqtp->preempt_count = (irqtp->preempt_count & ~SOFTIRQ_MASK) |
  298. (curtp->preempt_count & SOFTIRQ_MASK);
  299. current->thread.ksp_limit = (unsigned long)irqtp +
  300. _ALIGN_UP(sizeof(struct thread_info), 16);
  301. call_handle_irq(irq, desc, irqtp, desc->handle_irq);
  302. current->thread.ksp_limit = saved_sp_limit;
  303. irqtp->task = NULL;
  304. /* Set any flag that may have been set on the
  305. * alternate stack
  306. */
  307. if (irqtp->flags)
  308. set_bits(irqtp->flags, &curtp->flags);
  309. }
  310. #else
  311. static inline void handle_one_irq(unsigned int irq)
  312. {
  313. generic_handle_irq(irq);
  314. }
  315. #endif
  316. static inline void check_stack_overflow(void)
  317. {
  318. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  319. long sp;
  320. sp = __get_SP() & (THREAD_SIZE-1);
  321. /* check for stack overflow: is there less than 2KB free? */
  322. if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
  323. printk("do_IRQ: stack overflow: %ld\n",
  324. sp - sizeof(struct thread_info));
  325. dump_stack();
  326. }
  327. #endif
  328. }
  329. void do_IRQ(struct pt_regs *regs)
  330. {
  331. struct pt_regs *old_regs = set_irq_regs(regs);
  332. unsigned int irq;
  333. trace_irq_entry(regs);
  334. irq_enter();
  335. check_stack_overflow();
  336. irq = ppc_md.get_irq();
  337. if (irq != NO_IRQ && irq != NO_IRQ_IGNORE)
  338. handle_one_irq(irq);
  339. else if (irq != NO_IRQ_IGNORE)
  340. __get_cpu_var(irq_stat).spurious_irqs++;
  341. irq_exit();
  342. set_irq_regs(old_regs);
  343. #ifdef CONFIG_PPC_ISERIES
  344. if (firmware_has_feature(FW_FEATURE_ISERIES) &&
  345. get_lppaca()->int_dword.fields.decr_int) {
  346. get_lppaca()->int_dword.fields.decr_int = 0;
  347. /* Signal a fake decrementer interrupt */
  348. timer_interrupt(regs);
  349. }
  350. #endif
  351. trace_irq_exit(regs);
  352. }
  353. void __init init_IRQ(void)
  354. {
  355. if (ppc_md.init_IRQ)
  356. ppc_md.init_IRQ();
  357. exc_lvl_ctx_init();
  358. irq_ctx_init();
  359. }
  360. #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
  361. struct thread_info *critirq_ctx[NR_CPUS] __read_mostly;
  362. struct thread_info *dbgirq_ctx[NR_CPUS] __read_mostly;
  363. struct thread_info *mcheckirq_ctx[NR_CPUS] __read_mostly;
  364. void exc_lvl_ctx_init(void)
  365. {
  366. struct thread_info *tp;
  367. int i;
  368. for_each_possible_cpu(i) {
  369. memset((void *)critirq_ctx[i], 0, THREAD_SIZE);
  370. tp = critirq_ctx[i];
  371. tp->cpu = i;
  372. tp->preempt_count = 0;
  373. #ifdef CONFIG_BOOKE
  374. memset((void *)dbgirq_ctx[i], 0, THREAD_SIZE);
  375. tp = dbgirq_ctx[i];
  376. tp->cpu = i;
  377. tp->preempt_count = 0;
  378. memset((void *)mcheckirq_ctx[i], 0, THREAD_SIZE);
  379. tp = mcheckirq_ctx[i];
  380. tp->cpu = i;
  381. tp->preempt_count = HARDIRQ_OFFSET;
  382. #endif
  383. }
  384. }
  385. #endif
  386. #ifdef CONFIG_IRQSTACKS
  387. struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
  388. struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
  389. void irq_ctx_init(void)
  390. {
  391. struct thread_info *tp;
  392. int i;
  393. for_each_possible_cpu(i) {
  394. memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
  395. tp = softirq_ctx[i];
  396. tp->cpu = i;
  397. tp->preempt_count = 0;
  398. memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
  399. tp = hardirq_ctx[i];
  400. tp->cpu = i;
  401. tp->preempt_count = HARDIRQ_OFFSET;
  402. }
  403. }
  404. static inline void do_softirq_onstack(void)
  405. {
  406. struct thread_info *curtp, *irqtp;
  407. unsigned long saved_sp_limit = current->thread.ksp_limit;
  408. curtp = current_thread_info();
  409. irqtp = softirq_ctx[smp_processor_id()];
  410. irqtp->task = curtp->task;
  411. current->thread.ksp_limit = (unsigned long)irqtp +
  412. _ALIGN_UP(sizeof(struct thread_info), 16);
  413. call_do_softirq(irqtp);
  414. current->thread.ksp_limit = saved_sp_limit;
  415. irqtp->task = NULL;
  416. }
  417. #else
  418. #define do_softirq_onstack() __do_softirq()
  419. #endif /* CONFIG_IRQSTACKS */
  420. void do_softirq(void)
  421. {
  422. unsigned long flags;
  423. if (in_interrupt())
  424. return;
  425. local_irq_save(flags);
  426. if (local_softirq_pending())
  427. do_softirq_onstack();
  428. local_irq_restore(flags);
  429. }
  430. /*
  431. * IRQ controller and virtual interrupts
  432. */
  433. static LIST_HEAD(irq_hosts);
  434. static DEFINE_RAW_SPINLOCK(irq_big_lock);
  435. static unsigned int revmap_trees_allocated;
  436. static DEFINE_MUTEX(revmap_trees_mutex);
  437. struct irq_map_entry irq_map[NR_IRQS];
  438. static unsigned int irq_virq_count = NR_IRQS;
  439. static struct irq_host *irq_default_host;
  440. irq_hw_number_t virq_to_hw(unsigned int virq)
  441. {
  442. return irq_map[virq].hwirq;
  443. }
  444. EXPORT_SYMBOL_GPL(virq_to_hw);
  445. static int default_irq_host_match(struct irq_host *h, struct device_node *np)
  446. {
  447. return h->of_node != NULL && h->of_node == np;
  448. }
  449. struct irq_host *irq_alloc_host(struct device_node *of_node,
  450. unsigned int revmap_type,
  451. unsigned int revmap_arg,
  452. struct irq_host_ops *ops,
  453. irq_hw_number_t inval_irq)
  454. {
  455. struct irq_host *host;
  456. unsigned int size = sizeof(struct irq_host);
  457. unsigned int i;
  458. unsigned int *rmap;
  459. unsigned long flags;
  460. /* Allocate structure and revmap table if using linear mapping */
  461. if (revmap_type == IRQ_HOST_MAP_LINEAR)
  462. size += revmap_arg * sizeof(unsigned int);
  463. host = zalloc_maybe_bootmem(size, GFP_KERNEL);
  464. if (host == NULL)
  465. return NULL;
  466. /* Fill structure */
  467. host->revmap_type = revmap_type;
  468. host->inval_irq = inval_irq;
  469. host->ops = ops;
  470. host->of_node = of_node_get(of_node);
  471. if (host->ops->match == NULL)
  472. host->ops->match = default_irq_host_match;
  473. raw_spin_lock_irqsave(&irq_big_lock, flags);
  474. /* If it's a legacy controller, check for duplicates and
  475. * mark it as allocated (we use irq 0 host pointer for that
  476. */
  477. if (revmap_type == IRQ_HOST_MAP_LEGACY) {
  478. if (irq_map[0].host != NULL) {
  479. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  480. /* If we are early boot, we can't free the structure,
  481. * too bad...
  482. * this will be fixed once slab is made available early
  483. * instead of the current cruft
  484. */
  485. if (mem_init_done)
  486. kfree(host);
  487. return NULL;
  488. }
  489. irq_map[0].host = host;
  490. }
  491. list_add(&host->link, &irq_hosts);
  492. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  493. /* Additional setups per revmap type */
  494. switch(revmap_type) {
  495. case IRQ_HOST_MAP_LEGACY:
  496. /* 0 is always the invalid number for legacy */
  497. host->inval_irq = 0;
  498. /* setup us as the host for all legacy interrupts */
  499. for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
  500. irq_map[i].hwirq = i;
  501. smp_wmb();
  502. irq_map[i].host = host;
  503. smp_wmb();
  504. /* Clear norequest flags */
  505. irq_to_desc(i)->status &= ~IRQ_NOREQUEST;
  506. /* Legacy flags are left to default at this point,
  507. * one can then use irq_create_mapping() to
  508. * explicitly change them
  509. */
  510. ops->map(host, i, i);
  511. }
  512. break;
  513. case IRQ_HOST_MAP_LINEAR:
  514. rmap = (unsigned int *)(host + 1);
  515. for (i = 0; i < revmap_arg; i++)
  516. rmap[i] = NO_IRQ;
  517. host->revmap_data.linear.size = revmap_arg;
  518. smp_wmb();
  519. host->revmap_data.linear.revmap = rmap;
  520. break;
  521. default:
  522. break;
  523. }
  524. pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
  525. return host;
  526. }
  527. struct irq_host *irq_find_host(struct device_node *node)
  528. {
  529. struct irq_host *h, *found = NULL;
  530. unsigned long flags;
  531. /* We might want to match the legacy controller last since
  532. * it might potentially be set to match all interrupts in
  533. * the absence of a device node. This isn't a problem so far
  534. * yet though...
  535. */
  536. raw_spin_lock_irqsave(&irq_big_lock, flags);
  537. list_for_each_entry(h, &irq_hosts, link)
  538. if (h->ops->match(h, node)) {
  539. found = h;
  540. break;
  541. }
  542. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  543. return found;
  544. }
  545. EXPORT_SYMBOL_GPL(irq_find_host);
  546. void irq_set_default_host(struct irq_host *host)
  547. {
  548. pr_debug("irq: Default host set to @0x%p\n", host);
  549. irq_default_host = host;
  550. }
  551. void irq_set_virq_count(unsigned int count)
  552. {
  553. pr_debug("irq: Trying to set virq count to %d\n", count);
  554. BUG_ON(count < NUM_ISA_INTERRUPTS);
  555. if (count < NR_IRQS)
  556. irq_virq_count = count;
  557. }
  558. static int irq_setup_virq(struct irq_host *host, unsigned int virq,
  559. irq_hw_number_t hwirq)
  560. {
  561. struct irq_desc *desc;
  562. desc = irq_to_desc_alloc_node(virq, 0);
  563. if (!desc) {
  564. pr_debug("irq: -> allocating desc failed\n");
  565. goto error;
  566. }
  567. /* Clear IRQ_NOREQUEST flag */
  568. desc->status &= ~IRQ_NOREQUEST;
  569. /* map it */
  570. smp_wmb();
  571. irq_map[virq].hwirq = hwirq;
  572. smp_mb();
  573. if (host->ops->map(host, virq, hwirq)) {
  574. pr_debug("irq: -> mapping failed, freeing\n");
  575. goto error;
  576. }
  577. return 0;
  578. error:
  579. irq_free_virt(virq, 1);
  580. return -1;
  581. }
  582. unsigned int irq_create_direct_mapping(struct irq_host *host)
  583. {
  584. unsigned int virq;
  585. if (host == NULL)
  586. host = irq_default_host;
  587. BUG_ON(host == NULL);
  588. WARN_ON(host->revmap_type != IRQ_HOST_MAP_NOMAP);
  589. virq = irq_alloc_virt(host, 1, 0);
  590. if (virq == NO_IRQ) {
  591. pr_debug("irq: create_direct virq allocation failed\n");
  592. return NO_IRQ;
  593. }
  594. pr_debug("irq: create_direct obtained virq %d\n", virq);
  595. if (irq_setup_virq(host, virq, virq))
  596. return NO_IRQ;
  597. return virq;
  598. }
  599. unsigned int irq_create_mapping(struct irq_host *host,
  600. irq_hw_number_t hwirq)
  601. {
  602. unsigned int virq, hint;
  603. pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
  604. /* Look for default host if nececssary */
  605. if (host == NULL)
  606. host = irq_default_host;
  607. if (host == NULL) {
  608. printk(KERN_WARNING "irq_create_mapping called for"
  609. " NULL host, hwirq=%lx\n", hwirq);
  610. WARN_ON(1);
  611. return NO_IRQ;
  612. }
  613. pr_debug("irq: -> using host @%p\n", host);
  614. /* Check if mapping already exist, if it does, call
  615. * host->ops->map() to update the flags
  616. */
  617. virq = irq_find_mapping(host, hwirq);
  618. if (virq != NO_IRQ) {
  619. if (host->ops->remap)
  620. host->ops->remap(host, virq, hwirq);
  621. pr_debug("irq: -> existing mapping on virq %d\n", virq);
  622. return virq;
  623. }
  624. /* Get a virtual interrupt number */
  625. if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
  626. /* Handle legacy */
  627. virq = (unsigned int)hwirq;
  628. if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
  629. return NO_IRQ;
  630. return virq;
  631. } else {
  632. /* Allocate a virtual interrupt number */
  633. hint = hwirq % irq_virq_count;
  634. virq = irq_alloc_virt(host, 1, hint);
  635. if (virq == NO_IRQ) {
  636. pr_debug("irq: -> virq allocation failed\n");
  637. return NO_IRQ;
  638. }
  639. }
  640. if (irq_setup_virq(host, virq, hwirq))
  641. return NO_IRQ;
  642. printk(KERN_DEBUG "irq: irq %lu on host %s mapped to virtual irq %u\n",
  643. hwirq, host->of_node ? host->of_node->full_name : "null", virq);
  644. return virq;
  645. }
  646. EXPORT_SYMBOL_GPL(irq_create_mapping);
  647. unsigned int irq_create_of_mapping(struct device_node *controller,
  648. const u32 *intspec, unsigned int intsize)
  649. {
  650. struct irq_host *host;
  651. irq_hw_number_t hwirq;
  652. unsigned int type = IRQ_TYPE_NONE;
  653. unsigned int virq;
  654. if (controller == NULL)
  655. host = irq_default_host;
  656. else
  657. host = irq_find_host(controller);
  658. if (host == NULL) {
  659. printk(KERN_WARNING "irq: no irq host found for %s !\n",
  660. controller->full_name);
  661. return NO_IRQ;
  662. }
  663. /* If host has no translation, then we assume interrupt line */
  664. if (host->ops->xlate == NULL)
  665. hwirq = intspec[0];
  666. else {
  667. if (host->ops->xlate(host, controller, intspec, intsize,
  668. &hwirq, &type))
  669. return NO_IRQ;
  670. }
  671. /* Create mapping */
  672. virq = irq_create_mapping(host, hwirq);
  673. if (virq == NO_IRQ)
  674. return virq;
  675. /* Set type if specified and different than the current one */
  676. if (type != IRQ_TYPE_NONE &&
  677. type != (irq_to_desc(virq)->status & IRQF_TRIGGER_MASK))
  678. set_irq_type(virq, type);
  679. return virq;
  680. }
  681. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  682. unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
  683. {
  684. struct of_irq oirq;
  685. if (of_irq_map_one(dev, index, &oirq))
  686. return NO_IRQ;
  687. return irq_create_of_mapping(oirq.controller, oirq.specifier,
  688. oirq.size);
  689. }
  690. EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
  691. void irq_dispose_mapping(unsigned int virq)
  692. {
  693. struct irq_host *host;
  694. irq_hw_number_t hwirq;
  695. if (virq == NO_IRQ)
  696. return;
  697. host = irq_map[virq].host;
  698. WARN_ON (host == NULL);
  699. if (host == NULL)
  700. return;
  701. /* Never unmap legacy interrupts */
  702. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  703. return;
  704. /* remove chip and handler */
  705. set_irq_chip_and_handler(virq, NULL, NULL);
  706. /* Make sure it's completed */
  707. synchronize_irq(virq);
  708. /* Tell the PIC about it */
  709. if (host->ops->unmap)
  710. host->ops->unmap(host, virq);
  711. smp_mb();
  712. /* Clear reverse map */
  713. hwirq = irq_map[virq].hwirq;
  714. switch(host->revmap_type) {
  715. case IRQ_HOST_MAP_LINEAR:
  716. if (hwirq < host->revmap_data.linear.size)
  717. host->revmap_data.linear.revmap[hwirq] = NO_IRQ;
  718. break;
  719. case IRQ_HOST_MAP_TREE:
  720. /*
  721. * Check if radix tree allocated yet, if not then nothing to
  722. * remove.
  723. */
  724. smp_rmb();
  725. if (revmap_trees_allocated < 1)
  726. break;
  727. mutex_lock(&revmap_trees_mutex);
  728. radix_tree_delete(&host->revmap_data.tree, hwirq);
  729. mutex_unlock(&revmap_trees_mutex);
  730. break;
  731. }
  732. /* Destroy map */
  733. smp_mb();
  734. irq_map[virq].hwirq = host->inval_irq;
  735. /* Set some flags */
  736. irq_to_desc(virq)->status |= IRQ_NOREQUEST;
  737. /* Free it */
  738. irq_free_virt(virq, 1);
  739. }
  740. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  741. unsigned int irq_find_mapping(struct irq_host *host,
  742. irq_hw_number_t hwirq)
  743. {
  744. unsigned int i;
  745. unsigned int hint = hwirq % irq_virq_count;
  746. /* Look for default host if nececssary */
  747. if (host == NULL)
  748. host = irq_default_host;
  749. if (host == NULL)
  750. return NO_IRQ;
  751. /* legacy -> bail early */
  752. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  753. return hwirq;
  754. /* Slow path does a linear search of the map */
  755. if (hint < NUM_ISA_INTERRUPTS)
  756. hint = NUM_ISA_INTERRUPTS;
  757. i = hint;
  758. do {
  759. if (irq_map[i].host == host &&
  760. irq_map[i].hwirq == hwirq)
  761. return i;
  762. i++;
  763. if (i >= irq_virq_count)
  764. i = NUM_ISA_INTERRUPTS;
  765. } while(i != hint);
  766. return NO_IRQ;
  767. }
  768. EXPORT_SYMBOL_GPL(irq_find_mapping);
  769. unsigned int irq_radix_revmap_lookup(struct irq_host *host,
  770. irq_hw_number_t hwirq)
  771. {
  772. struct irq_map_entry *ptr;
  773. unsigned int virq;
  774. WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
  775. /*
  776. * Check if the radix tree exists and has bee initialized.
  777. * If not, we fallback to slow mode
  778. */
  779. if (revmap_trees_allocated < 2)
  780. return irq_find_mapping(host, hwirq);
  781. /* Now try to resolve */
  782. /*
  783. * No rcu_read_lock(ing) needed, the ptr returned can't go under us
  784. * as it's referencing an entry in the static irq_map table.
  785. */
  786. ptr = radix_tree_lookup(&host->revmap_data.tree, hwirq);
  787. /*
  788. * If found in radix tree, then fine.
  789. * Else fallback to linear lookup - this should not happen in practice
  790. * as it means that we failed to insert the node in the radix tree.
  791. */
  792. if (ptr)
  793. virq = ptr - irq_map;
  794. else
  795. virq = irq_find_mapping(host, hwirq);
  796. return virq;
  797. }
  798. void irq_radix_revmap_insert(struct irq_host *host, unsigned int virq,
  799. irq_hw_number_t hwirq)
  800. {
  801. WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
  802. /*
  803. * Check if the radix tree exists yet.
  804. * If not, then the irq will be inserted into the tree when it gets
  805. * initialized.
  806. */
  807. smp_rmb();
  808. if (revmap_trees_allocated < 1)
  809. return;
  810. if (virq != NO_IRQ) {
  811. mutex_lock(&revmap_trees_mutex);
  812. radix_tree_insert(&host->revmap_data.tree, hwirq,
  813. &irq_map[virq]);
  814. mutex_unlock(&revmap_trees_mutex);
  815. }
  816. }
  817. unsigned int irq_linear_revmap(struct irq_host *host,
  818. irq_hw_number_t hwirq)
  819. {
  820. unsigned int *revmap;
  821. WARN_ON(host->revmap_type != IRQ_HOST_MAP_LINEAR);
  822. /* Check revmap bounds */
  823. if (unlikely(hwirq >= host->revmap_data.linear.size))
  824. return irq_find_mapping(host, hwirq);
  825. /* Check if revmap was allocated */
  826. revmap = host->revmap_data.linear.revmap;
  827. if (unlikely(revmap == NULL))
  828. return irq_find_mapping(host, hwirq);
  829. /* Fill up revmap with slow path if no mapping found */
  830. if (unlikely(revmap[hwirq] == NO_IRQ))
  831. revmap[hwirq] = irq_find_mapping(host, hwirq);
  832. return revmap[hwirq];
  833. }
  834. unsigned int irq_alloc_virt(struct irq_host *host,
  835. unsigned int count,
  836. unsigned int hint)
  837. {
  838. unsigned long flags;
  839. unsigned int i, j, found = NO_IRQ;
  840. if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
  841. return NO_IRQ;
  842. raw_spin_lock_irqsave(&irq_big_lock, flags);
  843. /* Use hint for 1 interrupt if any */
  844. if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
  845. hint < irq_virq_count && irq_map[hint].host == NULL) {
  846. found = hint;
  847. goto hint_found;
  848. }
  849. /* Look for count consecutive numbers in the allocatable
  850. * (non-legacy) space
  851. */
  852. for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
  853. if (irq_map[i].host != NULL)
  854. j = 0;
  855. else
  856. j++;
  857. if (j == count) {
  858. found = i - count + 1;
  859. break;
  860. }
  861. }
  862. if (found == NO_IRQ) {
  863. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  864. return NO_IRQ;
  865. }
  866. hint_found:
  867. for (i = found; i < (found + count); i++) {
  868. irq_map[i].hwirq = host->inval_irq;
  869. smp_wmb();
  870. irq_map[i].host = host;
  871. }
  872. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  873. return found;
  874. }
  875. void irq_free_virt(unsigned int virq, unsigned int count)
  876. {
  877. unsigned long flags;
  878. unsigned int i;
  879. WARN_ON (virq < NUM_ISA_INTERRUPTS);
  880. WARN_ON (count == 0 || (virq + count) > irq_virq_count);
  881. raw_spin_lock_irqsave(&irq_big_lock, flags);
  882. for (i = virq; i < (virq + count); i++) {
  883. struct irq_host *host;
  884. if (i < NUM_ISA_INTERRUPTS ||
  885. (virq + count) > irq_virq_count)
  886. continue;
  887. host = irq_map[i].host;
  888. irq_map[i].hwirq = host->inval_irq;
  889. smp_wmb();
  890. irq_map[i].host = NULL;
  891. }
  892. raw_spin_unlock_irqrestore(&irq_big_lock, flags);
  893. }
  894. int arch_early_irq_init(void)
  895. {
  896. struct irq_desc *desc;
  897. int i;
  898. for (i = 0; i < NR_IRQS; i++) {
  899. desc = irq_to_desc(i);
  900. if (desc)
  901. desc->status |= IRQ_NOREQUEST;
  902. }
  903. return 0;
  904. }
  905. int arch_init_chip_data(struct irq_desc *desc, int node)
  906. {
  907. desc->status |= IRQ_NOREQUEST;
  908. return 0;
  909. }
  910. /* We need to create the radix trees late */
  911. static int irq_late_init(void)
  912. {
  913. struct irq_host *h;
  914. unsigned int i;
  915. /*
  916. * No mutual exclusion with respect to accessors of the tree is needed
  917. * here as the synchronization is done via the state variable
  918. * revmap_trees_allocated.
  919. */
  920. list_for_each_entry(h, &irq_hosts, link) {
  921. if (h->revmap_type == IRQ_HOST_MAP_TREE)
  922. INIT_RADIX_TREE(&h->revmap_data.tree, GFP_KERNEL);
  923. }
  924. /*
  925. * Make sure the radix trees inits are visible before setting
  926. * the flag
  927. */
  928. smp_wmb();
  929. revmap_trees_allocated = 1;
  930. /*
  931. * Insert the reverse mapping for those interrupts already present
  932. * in irq_map[].
  933. */
  934. mutex_lock(&revmap_trees_mutex);
  935. for (i = 0; i < irq_virq_count; i++) {
  936. if (irq_map[i].host &&
  937. (irq_map[i].host->revmap_type == IRQ_HOST_MAP_TREE))
  938. radix_tree_insert(&irq_map[i].host->revmap_data.tree,
  939. irq_map[i].hwirq, &irq_map[i]);
  940. }
  941. mutex_unlock(&revmap_trees_mutex);
  942. /*
  943. * Make sure the radix trees insertions are visible before setting
  944. * the flag
  945. */
  946. smp_wmb();
  947. revmap_trees_allocated = 2;
  948. return 0;
  949. }
  950. arch_initcall(irq_late_init);
  951. #ifdef CONFIG_VIRQ_DEBUG
  952. static int virq_debug_show(struct seq_file *m, void *private)
  953. {
  954. unsigned long flags;
  955. struct irq_desc *desc;
  956. const char *p;
  957. char none[] = "none";
  958. int i;
  959. seq_printf(m, "%-5s %-7s %-15s %s\n", "virq", "hwirq",
  960. "chip name", "host name");
  961. for (i = 1; i < nr_irqs; i++) {
  962. desc = irq_to_desc(i);
  963. if (!desc)
  964. continue;
  965. raw_spin_lock_irqsave(&desc->lock, flags);
  966. if (desc->action && desc->action->handler) {
  967. seq_printf(m, "%5d ", i);
  968. seq_printf(m, "0x%05lx ", virq_to_hw(i));
  969. if (desc->chip && desc->chip->name)
  970. p = desc->chip->name;
  971. else
  972. p = none;
  973. seq_printf(m, "%-15s ", p);
  974. if (irq_map[i].host && irq_map[i].host->of_node)
  975. p = irq_map[i].host->of_node->full_name;
  976. else
  977. p = none;
  978. seq_printf(m, "%s\n", p);
  979. }
  980. raw_spin_unlock_irqrestore(&desc->lock, flags);
  981. }
  982. return 0;
  983. }
  984. static int virq_debug_open(struct inode *inode, struct file *file)
  985. {
  986. return single_open(file, virq_debug_show, inode->i_private);
  987. }
  988. static const struct file_operations virq_debug_fops = {
  989. .open = virq_debug_open,
  990. .read = seq_read,
  991. .llseek = seq_lseek,
  992. .release = single_release,
  993. };
  994. static int __init irq_debugfs_init(void)
  995. {
  996. if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
  997. NULL, &virq_debug_fops) == NULL)
  998. return -ENOMEM;
  999. return 0;
  1000. }
  1001. __initcall(irq_debugfs_init);
  1002. #endif /* CONFIG_VIRQ_DEBUG */
  1003. #ifdef CONFIG_PPC64
  1004. static int __init setup_noirqdistrib(char *str)
  1005. {
  1006. distribute_irqs = 0;
  1007. return 1;
  1008. }
  1009. __setup("noirqdistrib", setup_noirqdistrib);
  1010. #endif /* CONFIG_PPC64 */