irq.c 27 KB

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