irq.c 27 KB

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