irq-gic.c 10 KB

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