irq-gic.c 6.6 KB

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