irq.c 26 KB

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