irq.c 28 KB

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