irq-gic.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #undef DEBUG
  2. #include <linux/bitmap.h>
  3. #include <linux/init.h>
  4. #include <linux/smp.h>
  5. #include <asm/io.h>
  6. #include <asm/gic.h>
  7. #include <asm/gcmpregs.h>
  8. #include <asm/mips-boards/maltaint.h>
  9. #include <asm/irq.h>
  10. #include <linux/hardirq.h>
  11. #include <asm-generic/bitops/find.h>
  12. static unsigned long _gic_base;
  13. static unsigned int _irqbase;
  14. static unsigned int gic_irq_flags[GIC_NUM_INTRS];
  15. #define GIC_IRQ_FLAG_EDGE 0x0001
  16. struct gic_pcpu_mask pcpu_masks[NR_CPUS];
  17. static struct gic_pending_regs pending_regs[NR_CPUS];
  18. static struct gic_intrmask_regs intrmask_regs[NR_CPUS];
  19. void gic_send_ipi(unsigned int intr)
  20. {
  21. pr_debug("CPU%d: %s status %08x\n", smp_processor_id(), __func__,
  22. read_c0_status());
  23. GICWRITE(GIC_REG(SHARED, GIC_SH_WEDGE), 0x80000000 | intr);
  24. }
  25. /* This is Malta specific and needs to be exported */
  26. static void __init vpe_local_setup(unsigned int numvpes)
  27. {
  28. int i;
  29. unsigned long timer_interrupt = 5, perf_interrupt = 5;
  30. unsigned int vpe_ctl;
  31. /*
  32. * Setup the default performance counter timer interrupts
  33. * for all VPEs
  34. */
  35. for (i = 0; i < numvpes; i++) {
  36. GICWRITE(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
  37. /* Are Interrupts locally routable? */
  38. GICREAD(GIC_REG(VPE_OTHER, GIC_VPE_CTL), vpe_ctl);
  39. if (vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK)
  40. GICWRITE(GIC_REG(VPE_OTHER, GIC_VPE_TIMER_MAP),
  41. GIC_MAP_TO_PIN_MSK | timer_interrupt);
  42. if (vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK)
  43. GICWRITE(GIC_REG(VPE_OTHER, GIC_VPE_PERFCTR_MAP),
  44. GIC_MAP_TO_PIN_MSK | perf_interrupt);
  45. }
  46. }
  47. unsigned int gic_get_int(void)
  48. {
  49. unsigned int i;
  50. unsigned long *pending, *intrmask, *pcpu_mask;
  51. unsigned long *pending_abs, *intrmask_abs;
  52. /* Get per-cpu bitmaps */
  53. pending = pending_regs[smp_processor_id()].pending;
  54. intrmask = intrmask_regs[smp_processor_id()].intrmask;
  55. pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask;
  56. pending_abs = (unsigned long *) GIC_REG_ABS_ADDR(SHARED,
  57. GIC_SH_PEND_31_0_OFS);
  58. intrmask_abs = (unsigned long *) GIC_REG_ABS_ADDR(SHARED,
  59. GIC_SH_MASK_31_0_OFS);
  60. for (i = 0; i < BITS_TO_LONGS(GIC_NUM_INTRS); i++) {
  61. GICREAD(*pending_abs, pending[i]);
  62. GICREAD(*intrmask_abs, intrmask[i]);
  63. pending_abs++;
  64. intrmask_abs++;
  65. }
  66. bitmap_and(pending, pending, intrmask, GIC_NUM_INTRS);
  67. bitmap_and(pending, pending, pcpu_mask, GIC_NUM_INTRS);
  68. i = find_first_bit(pending, GIC_NUM_INTRS);
  69. pr_debug("CPU%d: %s pend=%d\n", smp_processor_id(), __func__, i);
  70. return i;
  71. }
  72. static unsigned int gic_irq_startup(unsigned int irq)
  73. {
  74. irq -= _irqbase;
  75. pr_debug("CPU%d: %s: irq%d\n", smp_processor_id(), __func__, irq);
  76. GIC_SET_INTR_MASK(irq);
  77. return 0;
  78. }
  79. static void gic_irq_ack(unsigned int irq)
  80. {
  81. irq -= _irqbase;
  82. pr_debug("CPU%d: %s: irq%d\n", smp_processor_id(), __func__, irq);
  83. GIC_CLR_INTR_MASK(irq);
  84. if (gic_irq_flags[irq] & GIC_IRQ_FLAG_EDGE)
  85. GICWRITE(GIC_REG(SHARED, GIC_SH_WEDGE), irq);
  86. }
  87. static void gic_mask_irq(unsigned int irq)
  88. {
  89. irq -= _irqbase;
  90. pr_debug("CPU%d: %s: irq%d\n", smp_processor_id(), __func__, irq);
  91. GIC_CLR_INTR_MASK(irq);
  92. }
  93. static void gic_unmask_irq(unsigned int irq)
  94. {
  95. irq -= _irqbase;
  96. pr_debug("CPU%d: %s: irq%d\n", smp_processor_id(), __func__, irq);
  97. GIC_SET_INTR_MASK(irq);
  98. }
  99. #ifdef CONFIG_SMP
  100. static DEFINE_SPINLOCK(gic_lock);
  101. static int gic_set_affinity(unsigned int irq, const struct cpumask *cpumask)
  102. {
  103. cpumask_t tmp = CPU_MASK_NONE;
  104. unsigned long flags;
  105. int i;
  106. irq -= _irqbase;
  107. pr_debug(KERN_DEBUG "%s(%d) called\n", __func__, irq);
  108. cpumask_and(&tmp, cpumask, cpu_online_mask);
  109. if (cpus_empty(tmp))
  110. return -1;
  111. /* Assumption : cpumask refers to a single CPU */
  112. spin_lock_irqsave(&gic_lock, flags);
  113. for (;;) {
  114. /* Re-route this IRQ */
  115. GIC_SH_MAP_TO_VPE_SMASK(irq, first_cpu(tmp));
  116. /* Update the pcpu_masks */
  117. for (i = 0; i < NR_CPUS; i++)
  118. clear_bit(irq, pcpu_masks[i].pcpu_mask);
  119. set_bit(irq, pcpu_masks[first_cpu(tmp)].pcpu_mask);
  120. }
  121. cpumask_copy(irq_desc[irq].affinity, cpumask);
  122. spin_unlock_irqrestore(&gic_lock, flags);
  123. return 0;
  124. }
  125. #endif
  126. static struct irq_chip gic_irq_controller = {
  127. .name = "MIPS GIC",
  128. .startup = gic_irq_startup,
  129. .ack = gic_irq_ack,
  130. .mask = gic_mask_irq,
  131. .mask_ack = gic_mask_irq,
  132. .unmask = gic_unmask_irq,
  133. .eoi = gic_unmask_irq,
  134. #ifdef CONFIG_SMP
  135. .set_affinity = gic_set_affinity,
  136. #endif
  137. };
  138. static void __init gic_setup_intr(unsigned int intr, unsigned int cpu,
  139. unsigned int pin, unsigned int polarity, unsigned int trigtype,
  140. unsigned int flags)
  141. {
  142. /* Setup Intr to Pin mapping */
  143. if (pin & GIC_MAP_TO_NMI_MSK) {
  144. GICWRITE(GIC_REG_ADDR(SHARED, GIC_SH_MAP_TO_PIN(intr)), pin);
  145. /* FIXME: hack to route NMI to all cpu's */
  146. for (cpu = 0; cpu < NR_CPUS; cpu += 32) {
  147. GICWRITE(GIC_REG_ADDR(SHARED,
  148. GIC_SH_MAP_TO_VPE_REG_OFF(intr, cpu)),
  149. 0xffffffff);
  150. }
  151. } else {
  152. GICWRITE(GIC_REG_ADDR(SHARED, GIC_SH_MAP_TO_PIN(intr)),
  153. GIC_MAP_TO_PIN_MSK | pin);
  154. /* Setup Intr to CPU mapping */
  155. GIC_SH_MAP_TO_VPE_SMASK(intr, cpu);
  156. }
  157. /* Setup Intr Polarity */
  158. GIC_SET_POLARITY(intr, polarity);
  159. /* Setup Intr Trigger Type */
  160. GIC_SET_TRIGGER(intr, trigtype);
  161. /* Init Intr Masks */
  162. GIC_CLR_INTR_MASK(intr);
  163. /* Initialise per-cpu Interrupt software masks */
  164. if (flags & GIC_FLAG_IPI)
  165. set_bit(intr, pcpu_masks[cpu].pcpu_mask);
  166. if (flags & GIC_FLAG_TRANSPARENT)
  167. GIC_SET_INTR_MASK(intr);
  168. if (trigtype == GIC_TRIG_EDGE)
  169. gic_irq_flags[intr] |= GIC_IRQ_FLAG_EDGE;
  170. }
  171. static void __init gic_basic_init(int numintrs, int numvpes,
  172. struct gic_intr_map *intrmap, int mapsize)
  173. {
  174. unsigned int i, cpu;
  175. /* Setup defaults */
  176. for (i = 0; i < numintrs; i++) {
  177. GIC_SET_POLARITY(i, GIC_POL_POS);
  178. GIC_SET_TRIGGER(i, GIC_TRIG_LEVEL);
  179. GIC_CLR_INTR_MASK(i);
  180. if (i < GIC_NUM_INTRS)
  181. gic_irq_flags[i] = 0;
  182. }
  183. /* Setup specifics */
  184. for (i = 0; i < mapsize; i++) {
  185. cpu = intrmap[i].cpunum;
  186. if (cpu == X)
  187. continue;
  188. if (cpu == 0 && i != 0 && intrmap[i].flags == 0)
  189. continue;
  190. gic_setup_intr(i,
  191. intrmap[i].cpunum,
  192. intrmap[i].pin,
  193. intrmap[i].polarity,
  194. intrmap[i].trigtype,
  195. intrmap[i].flags);
  196. }
  197. vpe_local_setup(numvpes);
  198. for (i = _irqbase; i < (_irqbase + numintrs); i++)
  199. set_irq_chip(i, &gic_irq_controller);
  200. }
  201. void __init gic_init(unsigned long gic_base_addr,
  202. unsigned long gic_addrspace_size,
  203. struct gic_intr_map *intr_map, unsigned int intr_map_size,
  204. unsigned int irqbase)
  205. {
  206. unsigned int gicconfig;
  207. int numvpes, numintrs;
  208. _gic_base = (unsigned long) ioremap_nocache(gic_base_addr,
  209. gic_addrspace_size);
  210. _irqbase = irqbase;
  211. GICREAD(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig);
  212. numintrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >>
  213. GIC_SH_CONFIG_NUMINTRS_SHF;
  214. numintrs = ((numintrs + 1) * 8);
  215. numvpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >>
  216. GIC_SH_CONFIG_NUMVPES_SHF;
  217. pr_debug("%s called\n", __func__);
  218. gic_basic_init(numintrs, numvpes, intr_map, intr_map_size);
  219. }