irq-gic.c 9.8 KB

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