irq.c 28 KB

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