irq-gic.c 9.5 KB

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