irq.c 26 KB

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