irq-gic.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2008 Ralf Baechle (ralf@linux-mips.org)
  7. * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
  8. */
  9. #include <linux/bitmap.h>
  10. #include <linux/init.h>
  11. #include <linux/smp.h>
  12. #include <linux/irq.h>
  13. #include <asm/io.h>
  14. #include <asm/gic.h>
  15. #include <asm/setup.h>
  16. #include <asm/traps.h>
  17. #include <asm/gcmpregs.h>
  18. #include <linux/hardirq.h>
  19. #include <asm-generic/bitops/find.h>
  20. unsigned long _gic_base;
  21. unsigned int gic_irq_base;
  22. unsigned int gic_irq_flags[GIC_NUM_INTRS];
  23. /* The index into this array is the vector # of the interrupt. */
  24. struct gic_shared_intr_map gic_shared_intr_map[GIC_NUM_INTRS];
  25. static struct gic_pcpu_mask pcpu_masks[NR_CPUS];
  26. static struct gic_pending_regs pending_regs[NR_CPUS];
  27. static struct gic_intrmask_regs intrmask_regs[NR_CPUS];
  28. unsigned int gic_get_timer_pending(void)
  29. {
  30. unsigned int vpe_pending;
  31. GICWRITE(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), 0);
  32. GICREAD(GIC_REG(VPE_OTHER, GIC_VPE_PEND), vpe_pending);
  33. return (vpe_pending & GIC_VPE_PEND_TIMER_MSK);
  34. }
  35. void gic_bind_eic_interrupt(int irq, int set)
  36. {
  37. /* Convert irq vector # to hw int # */
  38. irq -= GIC_PIN_TO_VEC_OFFSET;
  39. /* Set irq to use shadow set */
  40. GICWRITE(GIC_REG_ADDR(VPE_LOCAL, GIC_VPE_EIC_SS(irq)), set);
  41. }
  42. void gic_send_ipi(unsigned int intr)
  43. {
  44. GICWRITE(GIC_REG(SHARED, GIC_SH_WEDGE), 0x80000000 | intr);
  45. }
  46. static void gic_eic_irq_dispatch(void)
  47. {
  48. unsigned int cause = read_c0_cause();
  49. int irq;
  50. irq = (cause & ST0_IM) >> STATUSB_IP2;
  51. if (irq == 0)
  52. irq = -1;
  53. if (irq >= 0)
  54. do_IRQ(gic_irq_base + irq);
  55. else
  56. spurious_interrupt();
  57. }
  58. static void __init vpe_local_setup(unsigned int numvpes)
  59. {
  60. unsigned long timer_intr = GIC_INT_TMR;
  61. unsigned long perf_intr = GIC_INT_PERFCTR;
  62. unsigned int vpe_ctl;
  63. int i;
  64. if (cpu_has_veic) {
  65. /*
  66. * GIC timer interrupt -> CPU HW Int X (vector X+2) ->
  67. * map to pin X+2-1 (since GIC adds 1)
  68. */
  69. timer_intr += (GIC_CPU_TO_VEC_OFFSET - GIC_PIN_TO_VEC_OFFSET);
  70. /*
  71. * GIC perfcnt interrupt -> CPU HW Int X (vector X+2) ->
  72. * map to pin X+2-1 (since GIC adds 1)
  73. */
  74. perf_intr += (GIC_CPU_TO_VEC_OFFSET - GIC_PIN_TO_VEC_OFFSET);
  75. }
  76. /*
  77. * Setup the default performance counter timer interrupts
  78. * for all VPEs
  79. */
  80. for (i = 0; i < numvpes; i++) {
  81. GICWRITE(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
  82. /* Are Interrupts locally routable? */
  83. GICREAD(GIC_REG(VPE_OTHER, GIC_VPE_CTL), vpe_ctl);
  84. if (vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK)
  85. GICWRITE(GIC_REG(VPE_OTHER, GIC_VPE_TIMER_MAP),
  86. GIC_MAP_TO_PIN_MSK | timer_intr);
  87. if (cpu_has_veic) {
  88. set_vi_handler(timer_intr + GIC_PIN_TO_VEC_OFFSET,
  89. gic_eic_irq_dispatch);
  90. gic_shared_intr_map[timer_intr + GIC_PIN_TO_VEC_OFFSET].local_intr_mask |= GIC_VPE_RMASK_TIMER_MSK;
  91. }
  92. if (vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK)
  93. GICWRITE(GIC_REG(VPE_OTHER, GIC_VPE_PERFCTR_MAP),
  94. GIC_MAP_TO_PIN_MSK | perf_intr);
  95. if (cpu_has_veic) {
  96. set_vi_handler(perf_intr + GIC_PIN_TO_VEC_OFFSET, gic_eic_irq_dispatch);
  97. gic_shared_intr_map[perf_intr + GIC_PIN_TO_VEC_OFFSET].local_intr_mask |= GIC_VPE_RMASK_PERFCNT_MSK;
  98. }
  99. }
  100. }
  101. unsigned int gic_get_int(void)
  102. {
  103. unsigned int i;
  104. unsigned long *pending, *intrmask, *pcpu_mask;
  105. unsigned long *pending_abs, *intrmask_abs;
  106. /* Get per-cpu bitmaps */
  107. pending = pending_regs[smp_processor_id()].pending;
  108. intrmask = intrmask_regs[smp_processor_id()].intrmask;
  109. pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask;
  110. pending_abs = (unsigned long *) GIC_REG_ABS_ADDR(SHARED,
  111. GIC_SH_PEND_31_0_OFS);
  112. intrmask_abs = (unsigned long *) GIC_REG_ABS_ADDR(SHARED,
  113. GIC_SH_MASK_31_0_OFS);
  114. for (i = 0; i < BITS_TO_LONGS(GIC_NUM_INTRS); i++) {
  115. GICREAD(*pending_abs, pending[i]);
  116. GICREAD(*intrmask_abs, intrmask[i]);
  117. pending_abs++;
  118. intrmask_abs++;
  119. }
  120. bitmap_and(pending, pending, intrmask, GIC_NUM_INTRS);
  121. bitmap_and(pending, pending, pcpu_mask, GIC_NUM_INTRS);
  122. return find_first_bit(pending, GIC_NUM_INTRS);
  123. }
  124. static void gic_mask_irq(struct irq_data *d)
  125. {
  126. GIC_CLR_INTR_MASK(d->irq - gic_irq_base);
  127. }
  128. static void gic_unmask_irq(struct irq_data *d)
  129. {
  130. GIC_SET_INTR_MASK(d->irq - gic_irq_base);
  131. }
  132. #ifdef CONFIG_SMP
  133. static DEFINE_SPINLOCK(gic_lock);
  134. static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
  135. bool force)
  136. {
  137. unsigned int irq = (d->irq - gic_irq_base);
  138. cpumask_t tmp = CPU_MASK_NONE;
  139. unsigned long flags;
  140. int i;
  141. cpumask_and(&tmp, cpumask, cpu_online_mask);
  142. if (cpus_empty(tmp))
  143. return -1;
  144. /* Assumption : cpumask refers to a single CPU */
  145. spin_lock_irqsave(&gic_lock, flags);
  146. for (;;) {
  147. /* Re-route this IRQ */
  148. GIC_SH_MAP_TO_VPE_SMASK(irq, first_cpu(tmp));
  149. /* Update the pcpu_masks */
  150. for (i = 0; i < NR_CPUS; i++)
  151. clear_bit(irq, pcpu_masks[i].pcpu_mask);
  152. set_bit(irq, pcpu_masks[first_cpu(tmp)].pcpu_mask);
  153. }
  154. cpumask_copy(d->affinity, cpumask);
  155. spin_unlock_irqrestore(&gic_lock, flags);
  156. return IRQ_SET_MASK_OK_NOCOPY;
  157. }
  158. #endif
  159. static struct irq_chip gic_irq_controller = {
  160. .name = "MIPS GIC",
  161. .irq_ack = gic_irq_ack,
  162. .irq_mask = gic_mask_irq,
  163. .irq_mask_ack = gic_mask_irq,
  164. .irq_unmask = gic_unmask_irq,
  165. .irq_eoi = gic_finish_irq,
  166. #ifdef CONFIG_SMP
  167. .irq_set_affinity = gic_set_affinity,
  168. #endif
  169. };
  170. static void __init gic_setup_intr(unsigned int intr, unsigned int cpu,
  171. unsigned int pin, unsigned int polarity, unsigned int trigtype,
  172. unsigned int flags)
  173. {
  174. struct gic_shared_intr_map *map_ptr;
  175. /* Setup Intr to Pin mapping */
  176. if (pin & GIC_MAP_TO_NMI_MSK) {
  177. GICWRITE(GIC_REG_ADDR(SHARED, GIC_SH_MAP_TO_PIN(intr)), pin);
  178. /* FIXME: hack to route NMI to all cpu's */
  179. for (cpu = 0; cpu < NR_CPUS; cpu += 32) {
  180. GICWRITE(GIC_REG_ADDR(SHARED,
  181. GIC_SH_MAP_TO_VPE_REG_OFF(intr, cpu)),
  182. 0xffffffff);
  183. }
  184. } else {
  185. GICWRITE(GIC_REG_ADDR(SHARED, GIC_SH_MAP_TO_PIN(intr)),
  186. GIC_MAP_TO_PIN_MSK | pin);
  187. /* Setup Intr to CPU mapping */
  188. GIC_SH_MAP_TO_VPE_SMASK(intr, cpu);
  189. if (cpu_has_veic) {
  190. set_vi_handler(pin + GIC_PIN_TO_VEC_OFFSET,
  191. gic_eic_irq_dispatch);
  192. map_ptr = &gic_shared_intr_map[pin + GIC_PIN_TO_VEC_OFFSET];
  193. if (map_ptr->num_shared_intr >= GIC_MAX_SHARED_INTR)
  194. BUG();
  195. map_ptr->intr_list[map_ptr->num_shared_intr++] = intr;
  196. }
  197. }
  198. /* Setup Intr Polarity */
  199. GIC_SET_POLARITY(intr, polarity);
  200. /* Setup Intr Trigger Type */
  201. GIC_SET_TRIGGER(intr, trigtype);
  202. /* Init Intr Masks */
  203. GIC_CLR_INTR_MASK(intr);
  204. /* Initialise per-cpu Interrupt software masks */
  205. if (flags & GIC_FLAG_IPI)
  206. set_bit(intr, pcpu_masks[cpu].pcpu_mask);
  207. if ((flags & GIC_FLAG_TRANSPARENT) && (cpu_has_veic == 0))
  208. GIC_SET_INTR_MASK(intr);
  209. if (trigtype == GIC_TRIG_EDGE)
  210. gic_irq_flags[intr] |= GIC_TRIG_EDGE;
  211. }
  212. static void __init gic_basic_init(int numintrs, int numvpes,
  213. struct gic_intr_map *intrmap, int mapsize)
  214. {
  215. unsigned int i, cpu;
  216. unsigned int pin_offset = 0;
  217. board_bind_eic_interrupt = &gic_bind_eic_interrupt;
  218. /* Setup defaults */
  219. for (i = 0; i < numintrs; i++) {
  220. GIC_SET_POLARITY(i, GIC_POL_POS);
  221. GIC_SET_TRIGGER(i, GIC_TRIG_LEVEL);
  222. GIC_CLR_INTR_MASK(i);
  223. if (i < GIC_NUM_INTRS) {
  224. gic_irq_flags[i] = 0;
  225. gic_shared_intr_map[i].num_shared_intr = 0;
  226. gic_shared_intr_map[i].local_intr_mask = 0;
  227. }
  228. }
  229. /*
  230. * In EIC mode, the HW_INT# is offset by (2-1). Need to subtract
  231. * one because the GIC will add one (since 0=no intr).
  232. */
  233. if (cpu_has_veic)
  234. pin_offset = (GIC_CPU_TO_VEC_OFFSET - GIC_PIN_TO_VEC_OFFSET);
  235. /* Setup specifics */
  236. for (i = 0; i < mapsize; i++) {
  237. cpu = intrmap[i].cpunum;
  238. if (cpu == GIC_UNUSED)
  239. continue;
  240. if (cpu == 0 && i != 0 && intrmap[i].flags == 0)
  241. continue;
  242. gic_setup_intr(i,
  243. intrmap[i].cpunum,
  244. intrmap[i].pin + pin_offset,
  245. intrmap[i].polarity,
  246. intrmap[i].trigtype,
  247. intrmap[i].flags);
  248. }
  249. vpe_local_setup(numvpes);
  250. }
  251. void __init gic_init(unsigned long gic_base_addr,
  252. unsigned long gic_addrspace_size,
  253. struct gic_intr_map *intr_map, unsigned int intr_map_size,
  254. unsigned int irqbase)
  255. {
  256. unsigned int gicconfig;
  257. int numvpes, numintrs;
  258. _gic_base = (unsigned long) ioremap_nocache(gic_base_addr,
  259. gic_addrspace_size);
  260. gic_irq_base = irqbase;
  261. GICREAD(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig);
  262. numintrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >>
  263. GIC_SH_CONFIG_NUMINTRS_SHF;
  264. numintrs = ((numintrs + 1) * 8);
  265. numvpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >>
  266. GIC_SH_CONFIG_NUMVPES_SHF;
  267. numvpes = numvpes + 1;
  268. gic_basic_init(numintrs, numvpes, intr_map, intr_map_size);
  269. gic_platform_init(numintrs, &gic_irq_controller);
  270. }