irq.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/uaccess.h>
  20. #include <hv/drv_pcie_rc_intf.h>
  21. /*
  22. * The set of interrupts we enable for raw_local_irq_enable().
  23. * This is initialized to have just a single interrupt that the kernel
  24. * doesn't actually use as a sentinel. During kernel init,
  25. * interrupts are added as the kernel gets prepared to support them.
  26. * NOTE: we could probably initialize them all statically up front.
  27. */
  28. DEFINE_PER_CPU(unsigned long long, interrupts_enabled_mask) =
  29. INITIAL_INTERRUPTS_ENABLED;
  30. EXPORT_PER_CPU_SYMBOL(interrupts_enabled_mask);
  31. /* Define per-tile device interrupt state */
  32. DEFINE_PER_CPU(HV_IntrState, dev_intr_state);
  33. DEFINE_PER_CPU(irq_cpustat_t, irq_stat) ____cacheline_internodealigned_in_smp;
  34. EXPORT_PER_CPU_SYMBOL(irq_stat);
  35. /*
  36. * Interrupt dispatcher, invoked upon a hypervisor device interrupt downcall
  37. */
  38. void tile_dev_intr(struct pt_regs *regs, int intnum)
  39. {
  40. int irq;
  41. /*
  42. * Get the device interrupt pending mask from where the hypervisor
  43. * has tucked it away for us.
  44. */
  45. unsigned long pending_dev_intr_mask = __insn_mfspr(SPR_SYSTEM_SAVE_1_3);
  46. /* Track time spent here in an interrupt context. */
  47. struct pt_regs *old_regs = set_irq_regs(regs);
  48. irq_enter();
  49. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  50. /* Debugging check for stack overflow: less than 1/8th stack free? */
  51. {
  52. long sp = stack_pointer - (long) current_thread_info();
  53. if (unlikely(sp < (sizeof(struct thread_info) + STACK_WARN))) {
  54. printk(KERN_EMERG "tile_dev_intr: "
  55. "stack overflow: %ld\n",
  56. sp - sizeof(struct thread_info));
  57. dump_stack();
  58. }
  59. }
  60. #endif
  61. for (irq = 0; pending_dev_intr_mask; ++irq) {
  62. if (pending_dev_intr_mask & 0x1) {
  63. generic_handle_irq(irq);
  64. /* Count device irqs; IPIs are counted elsewhere. */
  65. if (irq > HV_MAX_IPI_INTERRUPT)
  66. __get_cpu_var(irq_stat).irq_dev_intr_count++;
  67. }
  68. pending_dev_intr_mask >>= 1;
  69. }
  70. /*
  71. * Track time spent against the current process again and
  72. * process any softirqs if they are waiting.
  73. */
  74. irq_exit();
  75. set_irq_regs(old_regs);
  76. }
  77. /* Mask an interrupt. */
  78. static void hv_dev_irq_mask(unsigned int irq)
  79. {
  80. HV_IntrState *p_intr_state = &__get_cpu_var(dev_intr_state);
  81. hv_disable_intr(p_intr_state, 1 << irq);
  82. }
  83. /* Unmask an interrupt. */
  84. static void hv_dev_irq_unmask(unsigned int irq)
  85. {
  86. /* Re-enable the hypervisor to generate interrupts. */
  87. HV_IntrState *p_intr_state = &__get_cpu_var(dev_intr_state);
  88. hv_enable_intr(p_intr_state, 1 << irq);
  89. }
  90. /*
  91. * The HV doesn't latch incoming interrupts while an interrupt is
  92. * disabled, so we need to reenable interrupts before running the
  93. * handler.
  94. *
  95. * ISSUE: Enabling the interrupt this early avoids any race conditions
  96. * but introduces the possibility of nested interrupt stack overflow.
  97. * An imminent change to the HV IRQ model will fix this.
  98. */
  99. static void hv_dev_irq_ack(unsigned int irq)
  100. {
  101. hv_dev_irq_unmask(irq);
  102. }
  103. /*
  104. * Since ack() reenables interrupts, there's nothing to do at eoi().
  105. */
  106. static void hv_dev_irq_eoi(unsigned int irq)
  107. {
  108. }
  109. static struct irq_chip hv_dev_irq_chip = {
  110. .typename = "hv_dev_irq_chip",
  111. .ack = hv_dev_irq_ack,
  112. .mask = hv_dev_irq_mask,
  113. .unmask = hv_dev_irq_unmask,
  114. .eoi = hv_dev_irq_eoi,
  115. };
  116. static struct irqaction resched_action = {
  117. .handler = handle_reschedule_ipi,
  118. .name = "resched",
  119. .dev_id = handle_reschedule_ipi /* unique token */,
  120. };
  121. void __init init_IRQ(void)
  122. {
  123. /* Bind IPI irqs. Does this belong somewhere else in init? */
  124. tile_irq_activate(IRQ_RESCHEDULE);
  125. BUG_ON(setup_irq(IRQ_RESCHEDULE, &resched_action));
  126. }
  127. void __cpuinit init_per_tile_IRQs(void)
  128. {
  129. int rc;
  130. /* Set the pointer to the per-tile device interrupt state. */
  131. HV_IntrState *sv_ptr = &__get_cpu_var(dev_intr_state);
  132. rc = hv_dev_register_intr_state(sv_ptr);
  133. if (rc != HV_OK)
  134. panic("hv_dev_register_intr_state: error %d", rc);
  135. }
  136. void tile_irq_activate(unsigned int irq)
  137. {
  138. /*
  139. * Paravirtualized drivers can call up to the HV to find out
  140. * which irq they're associated with. The HV interface
  141. * doesn't provide a generic call for discovering all valid
  142. * IRQs, so drivers must call this method to initialize newly
  143. * discovered IRQs.
  144. *
  145. * We could also just initialize all 32 IRQs at startup, but
  146. * doing so would lead to a kernel fault if an unexpected
  147. * interrupt fires and jumps to a NULL action. By defering
  148. * the set_irq_chip_and_handler() call, unexpected IRQs are
  149. * handled properly by handle_bad_irq().
  150. */
  151. hv_dev_irq_mask(irq);
  152. set_irq_chip_and_handler(irq, &hv_dev_irq_chip, handle_percpu_irq);
  153. }
  154. void ack_bad_irq(unsigned int irq)
  155. {
  156. printk(KERN_ERR "unexpected IRQ trap at vector %02x\n", irq);
  157. }
  158. /*
  159. * Generic, controller-independent functions:
  160. */
  161. int show_interrupts(struct seq_file *p, void *v)
  162. {
  163. int i = *(loff_t *) v, j;
  164. struct irqaction *action;
  165. unsigned long flags;
  166. if (i == 0) {
  167. seq_printf(p, " ");
  168. for (j = 0; j < NR_CPUS; j++)
  169. if (cpu_online(j))
  170. seq_printf(p, "CPU%-8d", j);
  171. seq_putc(p, '\n');
  172. }
  173. if (i < NR_IRQS) {
  174. raw_spin_lock_irqsave(&irq_desc[i].lock, flags);
  175. action = irq_desc[i].action;
  176. if (!action)
  177. goto skip;
  178. seq_printf(p, "%3d: ", i);
  179. #ifndef CONFIG_SMP
  180. seq_printf(p, "%10u ", kstat_irqs(i));
  181. #else
  182. for_each_online_cpu(j)
  183. seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
  184. #endif
  185. seq_printf(p, " %14s", irq_desc[i].chip->typename);
  186. seq_printf(p, " %s", action->name);
  187. for (action = action->next; action; action = action->next)
  188. seq_printf(p, ", %s", action->name);
  189. seq_putc(p, '\n');
  190. skip:
  191. raw_spin_unlock_irqrestore(&irq_desc[i].lock, flags);
  192. }
  193. return 0;
  194. }