irq.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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 notrace 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 notrace 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. notrace 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. unsigned long saved_sp_limit = current->thread.ksp_limit;
  276. if (handler == NULL)
  277. handler = &__do_IRQ;
  278. irqtp->task = curtp->task;
  279. irqtp->flags = 0;
  280. /* Copy the softirq bits in preempt_count so that the
  281. * softirq checks work in the hardirq context.
  282. */
  283. irqtp->preempt_count =
  284. (irqtp->preempt_count & ~SOFTIRQ_MASK) |
  285. (curtp->preempt_count & SOFTIRQ_MASK);
  286. current->thread.ksp_limit = (unsigned long)irqtp +
  287. _ALIGN_UP(sizeof(struct thread_info), 16);
  288. call_handle_irq(irq, desc, irqtp, handler);
  289. current->thread.ksp_limit = saved_sp_limit;
  290. irqtp->task = NULL;
  291. /* Set any flag that may have been set on the
  292. * alternate stack
  293. */
  294. if (irqtp->flags)
  295. set_bits(irqtp->flags, &curtp->flags);
  296. } else
  297. #endif
  298. generic_handle_irq(irq);
  299. } else if (irq != NO_IRQ_IGNORE)
  300. /* That's not SMP safe ... but who cares ? */
  301. ppc_spurious_interrupts++;
  302. irq_exit();
  303. set_irq_regs(old_regs);
  304. #ifdef CONFIG_PPC_ISERIES
  305. if (firmware_has_feature(FW_FEATURE_ISERIES) &&
  306. get_lppaca()->int_dword.fields.decr_int) {
  307. get_lppaca()->int_dword.fields.decr_int = 0;
  308. /* Signal a fake decrementer interrupt */
  309. timer_interrupt(regs);
  310. }
  311. #endif
  312. }
  313. void __init init_IRQ(void)
  314. {
  315. if (ppc_md.init_IRQ)
  316. ppc_md.init_IRQ();
  317. exc_lvl_ctx_init();
  318. irq_ctx_init();
  319. }
  320. #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
  321. struct thread_info *critirq_ctx[NR_CPUS] __read_mostly;
  322. struct thread_info *dbgirq_ctx[NR_CPUS] __read_mostly;
  323. struct thread_info *mcheckirq_ctx[NR_CPUS] __read_mostly;
  324. void exc_lvl_ctx_init(void)
  325. {
  326. struct thread_info *tp;
  327. int i;
  328. for_each_possible_cpu(i) {
  329. memset((void *)critirq_ctx[i], 0, THREAD_SIZE);
  330. tp = critirq_ctx[i];
  331. tp->cpu = i;
  332. tp->preempt_count = 0;
  333. #ifdef CONFIG_BOOKE
  334. memset((void *)dbgirq_ctx[i], 0, THREAD_SIZE);
  335. tp = dbgirq_ctx[i];
  336. tp->cpu = i;
  337. tp->preempt_count = 0;
  338. memset((void *)mcheckirq_ctx[i], 0, THREAD_SIZE);
  339. tp = mcheckirq_ctx[i];
  340. tp->cpu = i;
  341. tp->preempt_count = HARDIRQ_OFFSET;
  342. #endif
  343. }
  344. }
  345. #endif
  346. #ifdef CONFIG_IRQSTACKS
  347. struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
  348. struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
  349. void irq_ctx_init(void)
  350. {
  351. struct thread_info *tp;
  352. int i;
  353. for_each_possible_cpu(i) {
  354. memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
  355. tp = softirq_ctx[i];
  356. tp->cpu = i;
  357. tp->preempt_count = 0;
  358. memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
  359. tp = hardirq_ctx[i];
  360. tp->cpu = i;
  361. tp->preempt_count = HARDIRQ_OFFSET;
  362. }
  363. }
  364. static inline void do_softirq_onstack(void)
  365. {
  366. struct thread_info *curtp, *irqtp;
  367. unsigned long saved_sp_limit = current->thread.ksp_limit;
  368. curtp = current_thread_info();
  369. irqtp = softirq_ctx[smp_processor_id()];
  370. irqtp->task = curtp->task;
  371. current->thread.ksp_limit = (unsigned long)irqtp +
  372. _ALIGN_UP(sizeof(struct thread_info), 16);
  373. call_do_softirq(irqtp);
  374. current->thread.ksp_limit = saved_sp_limit;
  375. irqtp->task = NULL;
  376. }
  377. #else
  378. #define do_softirq_onstack() __do_softirq()
  379. #endif /* CONFIG_IRQSTACKS */
  380. void do_softirq(void)
  381. {
  382. unsigned long flags;
  383. if (in_interrupt())
  384. return;
  385. local_irq_save(flags);
  386. if (local_softirq_pending())
  387. do_softirq_onstack();
  388. local_irq_restore(flags);
  389. }
  390. /*
  391. * IRQ controller and virtual interrupts
  392. */
  393. #ifdef CONFIG_PPC_MERGE
  394. static LIST_HEAD(irq_hosts);
  395. static DEFINE_SPINLOCK(irq_big_lock);
  396. static DEFINE_PER_CPU(unsigned int, irq_radix_reader);
  397. static unsigned int irq_radix_writer;
  398. struct irq_map_entry irq_map[NR_IRQS];
  399. static unsigned int irq_virq_count = NR_IRQS;
  400. static struct irq_host *irq_default_host;
  401. irq_hw_number_t virq_to_hw(unsigned int virq)
  402. {
  403. return irq_map[virq].hwirq;
  404. }
  405. EXPORT_SYMBOL_GPL(virq_to_hw);
  406. static int default_irq_host_match(struct irq_host *h, struct device_node *np)
  407. {
  408. return h->of_node != NULL && h->of_node == np;
  409. }
  410. struct irq_host *irq_alloc_host(struct device_node *of_node,
  411. unsigned int revmap_type,
  412. unsigned int revmap_arg,
  413. struct irq_host_ops *ops,
  414. irq_hw_number_t inval_irq)
  415. {
  416. struct irq_host *host;
  417. unsigned int size = sizeof(struct irq_host);
  418. unsigned int i;
  419. unsigned int *rmap;
  420. unsigned long flags;
  421. /* Allocate structure and revmap table if using linear mapping */
  422. if (revmap_type == IRQ_HOST_MAP_LINEAR)
  423. size += revmap_arg * sizeof(unsigned int);
  424. host = zalloc_maybe_bootmem(size, GFP_KERNEL);
  425. if (host == NULL)
  426. return NULL;
  427. /* Fill structure */
  428. host->revmap_type = revmap_type;
  429. host->inval_irq = inval_irq;
  430. host->ops = ops;
  431. host->of_node = of_node_get(of_node);
  432. if (host->ops->match == NULL)
  433. host->ops->match = default_irq_host_match;
  434. spin_lock_irqsave(&irq_big_lock, flags);
  435. /* If it's a legacy controller, check for duplicates and
  436. * mark it as allocated (we use irq 0 host pointer for that
  437. */
  438. if (revmap_type == IRQ_HOST_MAP_LEGACY) {
  439. if (irq_map[0].host != NULL) {
  440. spin_unlock_irqrestore(&irq_big_lock, flags);
  441. /* If we are early boot, we can't free the structure,
  442. * too bad...
  443. * this will be fixed once slab is made available early
  444. * instead of the current cruft
  445. */
  446. if (mem_init_done)
  447. kfree(host);
  448. return NULL;
  449. }
  450. irq_map[0].host = host;
  451. }
  452. list_add(&host->link, &irq_hosts);
  453. spin_unlock_irqrestore(&irq_big_lock, flags);
  454. /* Additional setups per revmap type */
  455. switch(revmap_type) {
  456. case IRQ_HOST_MAP_LEGACY:
  457. /* 0 is always the invalid number for legacy */
  458. host->inval_irq = 0;
  459. /* setup us as the host for all legacy interrupts */
  460. for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
  461. irq_map[i].hwirq = i;
  462. smp_wmb();
  463. irq_map[i].host = host;
  464. smp_wmb();
  465. /* Clear norequest flags */
  466. get_irq_desc(i)->status &= ~IRQ_NOREQUEST;
  467. /* Legacy flags are left to default at this point,
  468. * one can then use irq_create_mapping() to
  469. * explicitly change them
  470. */
  471. ops->map(host, i, i);
  472. }
  473. break;
  474. case IRQ_HOST_MAP_LINEAR:
  475. rmap = (unsigned int *)(host + 1);
  476. for (i = 0; i < revmap_arg; i++)
  477. rmap[i] = NO_IRQ;
  478. host->revmap_data.linear.size = revmap_arg;
  479. smp_wmb();
  480. host->revmap_data.linear.revmap = rmap;
  481. break;
  482. default:
  483. break;
  484. }
  485. pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
  486. return host;
  487. }
  488. struct irq_host *irq_find_host(struct device_node *node)
  489. {
  490. struct irq_host *h, *found = NULL;
  491. unsigned long flags;
  492. /* We might want to match the legacy controller last since
  493. * it might potentially be set to match all interrupts in
  494. * the absence of a device node. This isn't a problem so far
  495. * yet though...
  496. */
  497. spin_lock_irqsave(&irq_big_lock, flags);
  498. list_for_each_entry(h, &irq_hosts, link)
  499. if (h->ops->match(h, node)) {
  500. found = h;
  501. break;
  502. }
  503. spin_unlock_irqrestore(&irq_big_lock, flags);
  504. return found;
  505. }
  506. EXPORT_SYMBOL_GPL(irq_find_host);
  507. void irq_set_default_host(struct irq_host *host)
  508. {
  509. pr_debug("irq: Default host set to @0x%p\n", host);
  510. irq_default_host = host;
  511. }
  512. void irq_set_virq_count(unsigned int count)
  513. {
  514. pr_debug("irq: Trying to set virq count to %d\n", count);
  515. BUG_ON(count < NUM_ISA_INTERRUPTS);
  516. if (count < NR_IRQS)
  517. irq_virq_count = count;
  518. }
  519. /* radix tree not lockless safe ! we use a brlock-type mecanism
  520. * for now, until we can use a lockless radix tree
  521. */
  522. static void irq_radix_wrlock(unsigned long *flags)
  523. {
  524. unsigned int cpu, ok;
  525. spin_lock_irqsave(&irq_big_lock, *flags);
  526. irq_radix_writer = 1;
  527. smp_mb();
  528. do {
  529. barrier();
  530. ok = 1;
  531. for_each_possible_cpu(cpu) {
  532. if (per_cpu(irq_radix_reader, cpu)) {
  533. ok = 0;
  534. break;
  535. }
  536. }
  537. if (!ok)
  538. cpu_relax();
  539. } while(!ok);
  540. }
  541. static void irq_radix_wrunlock(unsigned long flags)
  542. {
  543. smp_wmb();
  544. irq_radix_writer = 0;
  545. spin_unlock_irqrestore(&irq_big_lock, flags);
  546. }
  547. static void irq_radix_rdlock(unsigned long *flags)
  548. {
  549. local_irq_save(*flags);
  550. __get_cpu_var(irq_radix_reader) = 1;
  551. smp_mb();
  552. if (likely(irq_radix_writer == 0))
  553. return;
  554. __get_cpu_var(irq_radix_reader) = 0;
  555. smp_wmb();
  556. spin_lock(&irq_big_lock);
  557. __get_cpu_var(irq_radix_reader) = 1;
  558. spin_unlock(&irq_big_lock);
  559. }
  560. static void irq_radix_rdunlock(unsigned long flags)
  561. {
  562. __get_cpu_var(irq_radix_reader) = 0;
  563. local_irq_restore(flags);
  564. }
  565. static int irq_setup_virq(struct irq_host *host, unsigned int virq,
  566. irq_hw_number_t hwirq)
  567. {
  568. /* Clear IRQ_NOREQUEST flag */
  569. get_irq_desc(virq)->status &= ~IRQ_NOREQUEST;
  570. /* map it */
  571. smp_wmb();
  572. irq_map[virq].hwirq = hwirq;
  573. smp_mb();
  574. if (host->ops->map(host, virq, hwirq)) {
  575. pr_debug("irq: -> mapping failed, freeing\n");
  576. irq_free_virt(virq, 1);
  577. return -1;
  578. }
  579. return 0;
  580. }
  581. unsigned int irq_create_direct_mapping(struct irq_host *host)
  582. {
  583. unsigned int virq;
  584. if (host == NULL)
  585. host = irq_default_host;
  586. BUG_ON(host == NULL);
  587. WARN_ON(host->revmap_type != IRQ_HOST_MAP_NOMAP);
  588. virq = irq_alloc_virt(host, 1, 0);
  589. if (virq == NO_IRQ) {
  590. pr_debug("irq: create_direct virq allocation failed\n");
  591. return NO_IRQ;
  592. }
  593. pr_debug("irq: create_direct obtained virq %d\n", virq);
  594. if (irq_setup_virq(host, virq, virq))
  595. return NO_IRQ;
  596. return virq;
  597. }
  598. unsigned int irq_create_mapping(struct irq_host *host,
  599. irq_hw_number_t hwirq)
  600. {
  601. unsigned int virq, hint;
  602. pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
  603. /* Look for default host if nececssary */
  604. if (host == NULL)
  605. host = irq_default_host;
  606. if (host == NULL) {
  607. printk(KERN_WARNING "irq_create_mapping called for"
  608. " NULL host, hwirq=%lx\n", hwirq);
  609. WARN_ON(1);
  610. return NO_IRQ;
  611. }
  612. pr_debug("irq: -> using host @%p\n", host);
  613. /* Check if mapping already exist, if it does, call
  614. * host->ops->map() to update the flags
  615. */
  616. virq = irq_find_mapping(host, hwirq);
  617. if (virq != NO_IRQ) {
  618. if (host->ops->remap)
  619. host->ops->remap(host, virq, hwirq);
  620. pr_debug("irq: -> existing mapping on virq %d\n", virq);
  621. return virq;
  622. }
  623. /* Get a virtual interrupt number */
  624. if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
  625. /* Handle legacy */
  626. virq = (unsigned int)hwirq;
  627. if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
  628. return NO_IRQ;
  629. return virq;
  630. } else {
  631. /* Allocate a virtual interrupt number */
  632. hint = hwirq % irq_virq_count;
  633. virq = irq_alloc_virt(host, 1, hint);
  634. if (virq == NO_IRQ) {
  635. pr_debug("irq: -> virq allocation failed\n");
  636. return NO_IRQ;
  637. }
  638. }
  639. pr_debug("irq: -> obtained virq %d\n", virq);
  640. if (irq_setup_virq(host, virq, hwirq))
  641. return NO_IRQ;
  642. return virq;
  643. }
  644. EXPORT_SYMBOL_GPL(irq_create_mapping);
  645. unsigned int irq_create_of_mapping(struct device_node *controller,
  646. u32 *intspec, unsigned int intsize)
  647. {
  648. struct irq_host *host;
  649. irq_hw_number_t hwirq;
  650. unsigned int type = IRQ_TYPE_NONE;
  651. unsigned int virq;
  652. if (controller == NULL)
  653. host = irq_default_host;
  654. else
  655. host = irq_find_host(controller);
  656. if (host == NULL) {
  657. printk(KERN_WARNING "irq: no irq host found for %s !\n",
  658. controller->full_name);
  659. return NO_IRQ;
  660. }
  661. /* If host has no translation, then we assume interrupt line */
  662. if (host->ops->xlate == NULL)
  663. hwirq = intspec[0];
  664. else {
  665. if (host->ops->xlate(host, controller, intspec, intsize,
  666. &hwirq, &type))
  667. return NO_IRQ;
  668. }
  669. /* Create mapping */
  670. virq = irq_create_mapping(host, hwirq);
  671. if (virq == NO_IRQ)
  672. return virq;
  673. /* Set type if specified and different than the current one */
  674. if (type != IRQ_TYPE_NONE &&
  675. type != (get_irq_desc(virq)->status & IRQF_TRIGGER_MASK))
  676. set_irq_type(virq, type);
  677. return virq;
  678. }
  679. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  680. unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
  681. {
  682. struct of_irq oirq;
  683. if (of_irq_map_one(dev, index, &oirq))
  684. return NO_IRQ;
  685. return irq_create_of_mapping(oirq.controller, oirq.specifier,
  686. oirq.size);
  687. }
  688. EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
  689. void irq_dispose_mapping(unsigned int virq)
  690. {
  691. struct irq_host *host;
  692. irq_hw_number_t hwirq;
  693. unsigned long flags;
  694. if (virq == NO_IRQ)
  695. return;
  696. host = irq_map[virq].host;
  697. WARN_ON (host == NULL);
  698. if (host == NULL)
  699. return;
  700. /* Never unmap legacy interrupts */
  701. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  702. return;
  703. /* remove chip and handler */
  704. set_irq_chip_and_handler(virq, NULL, NULL);
  705. /* Make sure it's completed */
  706. synchronize_irq(virq);
  707. /* Tell the PIC about it */
  708. if (host->ops->unmap)
  709. host->ops->unmap(host, virq);
  710. smp_mb();
  711. /* Clear reverse map */
  712. hwirq = irq_map[virq].hwirq;
  713. switch(host->revmap_type) {
  714. case IRQ_HOST_MAP_LINEAR:
  715. if (hwirq < host->revmap_data.linear.size)
  716. host->revmap_data.linear.revmap[hwirq] = NO_IRQ;
  717. break;
  718. case IRQ_HOST_MAP_TREE:
  719. /* Check if radix tree allocated yet */
  720. if (host->revmap_data.tree.gfp_mask == 0)
  721. break;
  722. irq_radix_wrlock(&flags);
  723. radix_tree_delete(&host->revmap_data.tree, hwirq);
  724. irq_radix_wrunlock(flags);
  725. break;
  726. }
  727. /* Destroy map */
  728. smp_mb();
  729. irq_map[virq].hwirq = host->inval_irq;
  730. /* Set some flags */
  731. get_irq_desc(virq)->status |= IRQ_NOREQUEST;
  732. /* Free it */
  733. irq_free_virt(virq, 1);
  734. }
  735. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  736. unsigned int irq_find_mapping(struct irq_host *host,
  737. irq_hw_number_t hwirq)
  738. {
  739. unsigned int i;
  740. unsigned int hint = hwirq % irq_virq_count;
  741. /* Look for default host if nececssary */
  742. if (host == NULL)
  743. host = irq_default_host;
  744. if (host == NULL)
  745. return NO_IRQ;
  746. /* legacy -> bail early */
  747. if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
  748. return hwirq;
  749. /* Slow path does a linear search of the map */
  750. if (hint < NUM_ISA_INTERRUPTS)
  751. hint = NUM_ISA_INTERRUPTS;
  752. i = hint;
  753. do {
  754. if (irq_map[i].host == host &&
  755. irq_map[i].hwirq == hwirq)
  756. return i;
  757. i++;
  758. if (i >= irq_virq_count)
  759. i = NUM_ISA_INTERRUPTS;
  760. } while(i != hint);
  761. return NO_IRQ;
  762. }
  763. EXPORT_SYMBOL_GPL(irq_find_mapping);
  764. unsigned int irq_radix_revmap(struct irq_host *host,
  765. irq_hw_number_t hwirq)
  766. {
  767. struct radix_tree_root *tree;
  768. struct irq_map_entry *ptr;
  769. unsigned int virq;
  770. unsigned long flags;
  771. WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
  772. /* Check if the radix tree exist yet. We test the value of
  773. * the gfp_mask for that. Sneaky but saves another int in the
  774. * structure. If not, we fallback to slow mode
  775. */
  776. tree = &host->revmap_data.tree;
  777. if (tree->gfp_mask == 0)
  778. return irq_find_mapping(host, hwirq);
  779. /* Now try to resolve */
  780. irq_radix_rdlock(&flags);
  781. ptr = radix_tree_lookup(tree, hwirq);
  782. irq_radix_rdunlock(flags);
  783. /* Found it, return */
  784. if (ptr) {
  785. virq = ptr - irq_map;
  786. return virq;
  787. }
  788. /* If not there, try to insert it */
  789. virq = irq_find_mapping(host, hwirq);
  790. if (virq != NO_IRQ) {
  791. irq_radix_wrlock(&flags);
  792. radix_tree_insert(tree, hwirq, &irq_map[virq]);
  793. irq_radix_wrunlock(flags);
  794. }
  795. return virq;
  796. }
  797. unsigned int irq_linear_revmap(struct irq_host *host,
  798. irq_hw_number_t hwirq)
  799. {
  800. unsigned int *revmap;
  801. WARN_ON(host->revmap_type != IRQ_HOST_MAP_LINEAR);
  802. /* Check revmap bounds */
  803. if (unlikely(hwirq >= host->revmap_data.linear.size))
  804. return irq_find_mapping(host, hwirq);
  805. /* Check if revmap was allocated */
  806. revmap = host->revmap_data.linear.revmap;
  807. if (unlikely(revmap == NULL))
  808. return irq_find_mapping(host, hwirq);
  809. /* Fill up revmap with slow path if no mapping found */
  810. if (unlikely(revmap[hwirq] == NO_IRQ))
  811. revmap[hwirq] = irq_find_mapping(host, hwirq);
  812. return revmap[hwirq];
  813. }
  814. unsigned int irq_alloc_virt(struct irq_host *host,
  815. unsigned int count,
  816. unsigned int hint)
  817. {
  818. unsigned long flags;
  819. unsigned int i, j, found = NO_IRQ;
  820. if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
  821. return NO_IRQ;
  822. spin_lock_irqsave(&irq_big_lock, flags);
  823. /* Use hint for 1 interrupt if any */
  824. if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
  825. hint < irq_virq_count && irq_map[hint].host == NULL) {
  826. found = hint;
  827. goto hint_found;
  828. }
  829. /* Look for count consecutive numbers in the allocatable
  830. * (non-legacy) space
  831. */
  832. for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
  833. if (irq_map[i].host != NULL)
  834. j = 0;
  835. else
  836. j++;
  837. if (j == count) {
  838. found = i - count + 1;
  839. break;
  840. }
  841. }
  842. if (found == NO_IRQ) {
  843. spin_unlock_irqrestore(&irq_big_lock, flags);
  844. return NO_IRQ;
  845. }
  846. hint_found:
  847. for (i = found; i < (found + count); i++) {
  848. irq_map[i].hwirq = host->inval_irq;
  849. smp_wmb();
  850. irq_map[i].host = host;
  851. }
  852. spin_unlock_irqrestore(&irq_big_lock, flags);
  853. return found;
  854. }
  855. void irq_free_virt(unsigned int virq, unsigned int count)
  856. {
  857. unsigned long flags;
  858. unsigned int i;
  859. WARN_ON (virq < NUM_ISA_INTERRUPTS);
  860. WARN_ON (count == 0 || (virq + count) > irq_virq_count);
  861. spin_lock_irqsave(&irq_big_lock, flags);
  862. for (i = virq; i < (virq + count); i++) {
  863. struct irq_host *host;
  864. if (i < NUM_ISA_INTERRUPTS ||
  865. (virq + count) > irq_virq_count)
  866. continue;
  867. host = irq_map[i].host;
  868. irq_map[i].hwirq = host->inval_irq;
  869. smp_wmb();
  870. irq_map[i].host = NULL;
  871. }
  872. spin_unlock_irqrestore(&irq_big_lock, flags);
  873. }
  874. void irq_early_init(void)
  875. {
  876. unsigned int i;
  877. for (i = 0; i < NR_IRQS; i++)
  878. get_irq_desc(i)->status |= IRQ_NOREQUEST;
  879. }
  880. /* We need to create the radix trees late */
  881. static int irq_late_init(void)
  882. {
  883. struct irq_host *h;
  884. unsigned long flags;
  885. irq_radix_wrlock(&flags);
  886. list_for_each_entry(h, &irq_hosts, link) {
  887. if (h->revmap_type == IRQ_HOST_MAP_TREE)
  888. INIT_RADIX_TREE(&h->revmap_data.tree, GFP_ATOMIC);
  889. }
  890. irq_radix_wrunlock(flags);
  891. return 0;
  892. }
  893. arch_initcall(irq_late_init);
  894. #ifdef CONFIG_VIRQ_DEBUG
  895. static int virq_debug_show(struct seq_file *m, void *private)
  896. {
  897. unsigned long flags;
  898. irq_desc_t *desc;
  899. const char *p;
  900. char none[] = "none";
  901. int i;
  902. seq_printf(m, "%-5s %-7s %-15s %s\n", "virq", "hwirq",
  903. "chip name", "host name");
  904. for (i = 1; i < NR_IRQS; i++) {
  905. desc = get_irq_desc(i);
  906. spin_lock_irqsave(&desc->lock, flags);
  907. if (desc->action && desc->action->handler) {
  908. seq_printf(m, "%5d ", i);
  909. seq_printf(m, "0x%05lx ", virq_to_hw(i));
  910. if (desc->chip && desc->chip->typename)
  911. p = desc->chip->typename;
  912. else
  913. p = none;
  914. seq_printf(m, "%-15s ", p);
  915. if (irq_map[i].host && irq_map[i].host->of_node)
  916. p = irq_map[i].host->of_node->full_name;
  917. else
  918. p = none;
  919. seq_printf(m, "%s\n", p);
  920. }
  921. spin_unlock_irqrestore(&desc->lock, flags);
  922. }
  923. return 0;
  924. }
  925. static int virq_debug_open(struct inode *inode, struct file *file)
  926. {
  927. return single_open(file, virq_debug_show, inode->i_private);
  928. }
  929. static const struct file_operations virq_debug_fops = {
  930. .open = virq_debug_open,
  931. .read = seq_read,
  932. .llseek = seq_lseek,
  933. .release = single_release,
  934. };
  935. static int __init irq_debugfs_init(void)
  936. {
  937. if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
  938. NULL, &virq_debug_fops) == NULL)
  939. return -ENOMEM;
  940. return 0;
  941. }
  942. __initcall(irq_debugfs_init);
  943. #endif /* CONFIG_VIRQ_DEBUG */
  944. #endif /* CONFIG_PPC_MERGE */
  945. #ifdef CONFIG_PPC64
  946. static int __init setup_noirqdistrib(char *str)
  947. {
  948. distribute_irqs = 0;
  949. return 1;
  950. }
  951. __setup("noirqdistrib", setup_noirqdistrib);
  952. #endif /* CONFIG_PPC64 */