irq.c 26 KB

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