irq.c 27 KB

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