irq-gic.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 unsigned int gic_irq_startup(unsigned int irq)
  72. {
  73. irq -= _irqbase;
  74. pr_debug("CPU%d: %s: irq%d\n", smp_processor_id(), __func__, irq);
  75. GIC_SET_INTR_MASK(irq);
  76. return 0;
  77. }
  78. static void gic_irq_ack(unsigned int irq)
  79. {
  80. irq -= _irqbase;
  81. pr_debug("CPU%d: %s: irq%d\n", smp_processor_id(), __func__, irq);
  82. GIC_CLR_INTR_MASK(irq);
  83. if (gic_irq_flags[irq] & GIC_IRQ_FLAG_EDGE)
  84. GICWRITE(GIC_REG(SHARED, GIC_SH_WEDGE), irq);
  85. }
  86. static void gic_mask_irq(unsigned int irq)
  87. {
  88. irq -= _irqbase;
  89. pr_debug("CPU%d: %s: irq%d\n", smp_processor_id(), __func__, irq);
  90. GIC_CLR_INTR_MASK(irq);
  91. }
  92. static void gic_unmask_irq(unsigned int irq)
  93. {
  94. irq -= _irqbase;
  95. pr_debug("CPU%d: %s: irq%d\n", smp_processor_id(), __func__, irq);
  96. GIC_SET_INTR_MASK(irq);
  97. }
  98. #ifdef CONFIG_SMP
  99. static DEFINE_SPINLOCK(gic_lock);
  100. static int gic_set_affinity(unsigned int irq, const struct cpumask *cpumask)
  101. {
  102. cpumask_t tmp = CPU_MASK_NONE;
  103. unsigned long flags;
  104. int i;
  105. irq -= _irqbase;
  106. pr_debug("%s(%d) called\n", __func__, irq);
  107. cpumask_and(&tmp, cpumask, cpu_online_mask);
  108. if (cpus_empty(tmp))
  109. return -1;
  110. /* Assumption : cpumask refers to a single CPU */
  111. spin_lock_irqsave(&gic_lock, flags);
  112. for (;;) {
  113. /* Re-route this IRQ */
  114. GIC_SH_MAP_TO_VPE_SMASK(irq, first_cpu(tmp));
  115. /* Update the pcpu_masks */
  116. for (i = 0; i < NR_CPUS; i++)
  117. clear_bit(irq, pcpu_masks[i].pcpu_mask);
  118. set_bit(irq, pcpu_masks[first_cpu(tmp)].pcpu_mask);
  119. }
  120. cpumask_copy(irq_desc[irq].affinity, cpumask);
  121. spin_unlock_irqrestore(&gic_lock, flags);
  122. return 0;
  123. }
  124. #endif
  125. static struct irq_chip gic_irq_controller = {
  126. .name = "MIPS GIC",
  127. .startup = gic_irq_startup,
  128. .ack = gic_irq_ack,
  129. .mask = gic_mask_irq,
  130. .mask_ack = gic_mask_irq,
  131. .unmask = gic_unmask_irq,
  132. .eoi = gic_unmask_irq,
  133. #ifdef CONFIG_SMP
  134. .set_affinity = gic_set_affinity,
  135. #endif
  136. };
  137. static void __init gic_setup_intr(unsigned int intr, unsigned int cpu,
  138. unsigned int pin, unsigned int polarity, unsigned int trigtype,
  139. unsigned int flags)
  140. {
  141. /* Setup Intr to Pin mapping */
  142. if (pin & GIC_MAP_TO_NMI_MSK) {
  143. GICWRITE(GIC_REG_ADDR(SHARED, GIC_SH_MAP_TO_PIN(intr)), pin);
  144. /* FIXME: hack to route NMI to all cpu's */
  145. for (cpu = 0; cpu < NR_CPUS; cpu += 32) {
  146. GICWRITE(GIC_REG_ADDR(SHARED,
  147. GIC_SH_MAP_TO_VPE_REG_OFF(intr, cpu)),
  148. 0xffffffff);
  149. }
  150. } else {
  151. GICWRITE(GIC_REG_ADDR(SHARED, GIC_SH_MAP_TO_PIN(intr)),
  152. GIC_MAP_TO_PIN_MSK | pin);
  153. /* Setup Intr to CPU mapping */
  154. GIC_SH_MAP_TO_VPE_SMASK(intr, cpu);
  155. }
  156. /* Setup Intr Polarity */
  157. GIC_SET_POLARITY(intr, polarity);
  158. /* Setup Intr Trigger Type */
  159. GIC_SET_TRIGGER(intr, trigtype);
  160. /* Init Intr Masks */
  161. GIC_CLR_INTR_MASK(intr);
  162. /* Initialise per-cpu Interrupt software masks */
  163. if (flags & GIC_FLAG_IPI)
  164. set_bit(intr, pcpu_masks[cpu].pcpu_mask);
  165. if (flags & GIC_FLAG_TRANSPARENT)
  166. GIC_SET_INTR_MASK(intr);
  167. if (trigtype == GIC_TRIG_EDGE)
  168. gic_irq_flags[intr] |= GIC_IRQ_FLAG_EDGE;
  169. }
  170. static void __init gic_basic_init(int numintrs, int numvpes,
  171. struct gic_intr_map *intrmap, int mapsize)
  172. {
  173. unsigned int i, cpu;
  174. /* Setup defaults */
  175. for (i = 0; i < numintrs; i++) {
  176. GIC_SET_POLARITY(i, GIC_POL_POS);
  177. GIC_SET_TRIGGER(i, GIC_TRIG_LEVEL);
  178. GIC_CLR_INTR_MASK(i);
  179. if (i < GIC_NUM_INTRS)
  180. gic_irq_flags[i] = 0;
  181. }
  182. /* Setup specifics */
  183. for (i = 0; i < mapsize; i++) {
  184. cpu = intrmap[i].cpunum;
  185. if (cpu == GIC_UNUSED)
  186. continue;
  187. if (cpu == 0 && i != 0 && intrmap[i].flags == 0)
  188. continue;
  189. gic_setup_intr(i,
  190. intrmap[i].cpunum,
  191. intrmap[i].pin,
  192. intrmap[i].polarity,
  193. intrmap[i].trigtype,
  194. intrmap[i].flags);
  195. }
  196. vpe_local_setup(numvpes);
  197. for (i = _irqbase; i < (_irqbase + numintrs); i++)
  198. set_irq_chip(i, &gic_irq_controller);
  199. }
  200. void __init gic_init(unsigned long gic_base_addr,
  201. unsigned long gic_addrspace_size,
  202. struct gic_intr_map *intr_map, unsigned int intr_map_size,
  203. unsigned int irqbase)
  204. {
  205. unsigned int gicconfig;
  206. int numvpes, numintrs;
  207. _gic_base = (unsigned long) ioremap_nocache(gic_base_addr,
  208. gic_addrspace_size);
  209. _irqbase = irqbase;
  210. GICREAD(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig);
  211. numintrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >>
  212. GIC_SH_CONFIG_NUMINTRS_SHF;
  213. numintrs = ((numintrs + 1) * 8);
  214. numvpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >>
  215. GIC_SH_CONFIG_NUMVPES_SHF;
  216. pr_debug("%s called\n", __func__);
  217. gic_basic_init(numintrs, numvpes, intr_map, intr_map_size);
  218. }