irq-mips-gic.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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/clocksource.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/irqchip.h>
  15. #include <linux/irqchip/mips-gic.h>
  16. #include <linux/of_address.h>
  17. #include <linux/sched.h>
  18. #include <linux/smp.h>
  19. #include <asm/mips-cm.h>
  20. #include <asm/setup.h>
  21. #include <asm/traps.h>
  22. #include <dt-bindings/interrupt-controller/mips-gic.h>
  23. unsigned int gic_present;
  24. struct gic_pcpu_mask {
  25. DECLARE_BITMAP(pcpu_mask, GIC_MAX_INTRS);
  26. };
  27. static void __iomem *gic_base;
  28. static struct gic_pcpu_mask pcpu_masks[NR_CPUS];
  29. static DEFINE_SPINLOCK(gic_lock);
  30. static struct irq_domain *gic_irq_domain;
  31. static int gic_shared_intrs;
  32. static int gic_vpes;
  33. static unsigned int gic_cpu_pin;
  34. static unsigned int timer_cpu_pin;
  35. static struct irq_chip gic_level_irq_controller, gic_edge_irq_controller;
  36. static void __gic_irq_dispatch(void);
  37. static inline u32 gic_read32(unsigned int reg)
  38. {
  39. return __raw_readl(gic_base + reg);
  40. }
  41. static inline u64 gic_read64(unsigned int reg)
  42. {
  43. return __raw_readq(gic_base + reg);
  44. }
  45. static inline unsigned long gic_read(unsigned int reg)
  46. {
  47. if (!mips_cm_is64)
  48. return gic_read32(reg);
  49. else
  50. return gic_read64(reg);
  51. }
  52. static inline void gic_write32(unsigned int reg, u32 val)
  53. {
  54. return __raw_writel(val, gic_base + reg);
  55. }
  56. static inline void gic_write64(unsigned int reg, u64 val)
  57. {
  58. return __raw_writeq(val, gic_base + reg);
  59. }
  60. static inline void gic_write(unsigned int reg, unsigned long val)
  61. {
  62. if (!mips_cm_is64)
  63. return gic_write32(reg, (u32)val);
  64. else
  65. return gic_write64(reg, (u64)val);
  66. }
  67. static inline void gic_update_bits(unsigned int reg, unsigned long mask,
  68. unsigned long val)
  69. {
  70. unsigned long regval;
  71. regval = gic_read(reg);
  72. regval &= ~mask;
  73. regval |= val;
  74. gic_write(reg, regval);
  75. }
  76. static inline void gic_reset_mask(unsigned int intr)
  77. {
  78. gic_write(GIC_REG(SHARED, GIC_SH_RMASK) + GIC_INTR_OFS(intr),
  79. 1ul << GIC_INTR_BIT(intr));
  80. }
  81. static inline void gic_set_mask(unsigned int intr)
  82. {
  83. gic_write(GIC_REG(SHARED, GIC_SH_SMASK) + GIC_INTR_OFS(intr),
  84. 1ul << GIC_INTR_BIT(intr));
  85. }
  86. static inline void gic_set_polarity(unsigned int intr, unsigned int pol)
  87. {
  88. gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_POLARITY) +
  89. GIC_INTR_OFS(intr), 1ul << GIC_INTR_BIT(intr),
  90. (unsigned long)pol << GIC_INTR_BIT(intr));
  91. }
  92. static inline void gic_set_trigger(unsigned int intr, unsigned int trig)
  93. {
  94. gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_TRIGGER) +
  95. GIC_INTR_OFS(intr), 1ul << GIC_INTR_BIT(intr),
  96. (unsigned long)trig << GIC_INTR_BIT(intr));
  97. }
  98. static inline void gic_set_dual_edge(unsigned int intr, unsigned int dual)
  99. {
  100. gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_DUAL) + GIC_INTR_OFS(intr),
  101. 1ul << GIC_INTR_BIT(intr),
  102. (unsigned long)dual << GIC_INTR_BIT(intr));
  103. }
  104. static inline void gic_map_to_pin(unsigned int intr, unsigned int pin)
  105. {
  106. gic_write32(GIC_REG(SHARED, GIC_SH_INTR_MAP_TO_PIN_BASE) +
  107. GIC_SH_MAP_TO_PIN(intr), GIC_MAP_TO_PIN_MSK | pin);
  108. }
  109. static inline void gic_map_to_vpe(unsigned int intr, unsigned int vpe)
  110. {
  111. gic_write(GIC_REG(SHARED, GIC_SH_INTR_MAP_TO_VPE_BASE) +
  112. GIC_SH_MAP_TO_VPE_REG_OFF(intr, vpe),
  113. GIC_SH_MAP_TO_VPE_REG_BIT(vpe));
  114. }
  115. #ifdef CONFIG_CLKSRC_MIPS_GIC
  116. cycle_t gic_read_count(void)
  117. {
  118. unsigned int hi, hi2, lo;
  119. if (mips_cm_is64)
  120. return (cycle_t)gic_read(GIC_REG(SHARED, GIC_SH_COUNTER));
  121. do {
  122. hi = gic_read32(GIC_REG(SHARED, GIC_SH_COUNTER_63_32));
  123. lo = gic_read32(GIC_REG(SHARED, GIC_SH_COUNTER_31_00));
  124. hi2 = gic_read32(GIC_REG(SHARED, GIC_SH_COUNTER_63_32));
  125. } while (hi2 != hi);
  126. return (((cycle_t) hi) << 32) + lo;
  127. }
  128. unsigned int gic_get_count_width(void)
  129. {
  130. unsigned int bits, config;
  131. config = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
  132. bits = 32 + 4 * ((config & GIC_SH_CONFIG_COUNTBITS_MSK) >>
  133. GIC_SH_CONFIG_COUNTBITS_SHF);
  134. return bits;
  135. }
  136. void gic_write_compare(cycle_t cnt)
  137. {
  138. if (mips_cm_is64) {
  139. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE), cnt);
  140. } else {
  141. gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_HI),
  142. (int)(cnt >> 32));
  143. gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_LO),
  144. (int)(cnt & 0xffffffff));
  145. }
  146. }
  147. void gic_write_cpu_compare(cycle_t cnt, int cpu)
  148. {
  149. unsigned long flags;
  150. local_irq_save(flags);
  151. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), cpu);
  152. if (mips_cm_is64) {
  153. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE), cnt);
  154. } else {
  155. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_HI),
  156. (int)(cnt >> 32));
  157. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_LO),
  158. (int)(cnt & 0xffffffff));
  159. }
  160. local_irq_restore(flags);
  161. }
  162. cycle_t gic_read_compare(void)
  163. {
  164. unsigned int hi, lo;
  165. if (mips_cm_is64)
  166. return (cycle_t)gic_read(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE));
  167. hi = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_HI));
  168. lo = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_LO));
  169. return (((cycle_t) hi) << 32) + lo;
  170. }
  171. void gic_start_count(void)
  172. {
  173. u32 gicconfig;
  174. /* Start the counter */
  175. gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
  176. gicconfig &= ~(1 << GIC_SH_CONFIG_COUNTSTOP_SHF);
  177. gic_write(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig);
  178. }
  179. void gic_stop_count(void)
  180. {
  181. u32 gicconfig;
  182. /* Stop the counter */
  183. gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
  184. gicconfig |= 1 << GIC_SH_CONFIG_COUNTSTOP_SHF;
  185. gic_write(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig);
  186. }
  187. #endif
  188. static bool gic_local_irq_is_routable(int intr)
  189. {
  190. u32 vpe_ctl;
  191. /* All local interrupts are routable in EIC mode. */
  192. if (cpu_has_veic)
  193. return true;
  194. vpe_ctl = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_CTL));
  195. switch (intr) {
  196. case GIC_LOCAL_INT_TIMER:
  197. return vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK;
  198. case GIC_LOCAL_INT_PERFCTR:
  199. return vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK;
  200. case GIC_LOCAL_INT_FDC:
  201. return vpe_ctl & GIC_VPE_CTL_FDC_RTBL_MSK;
  202. case GIC_LOCAL_INT_SWINT0:
  203. case GIC_LOCAL_INT_SWINT1:
  204. return vpe_ctl & GIC_VPE_CTL_SWINT_RTBL_MSK;
  205. default:
  206. return true;
  207. }
  208. }
  209. static void gic_bind_eic_interrupt(int irq, int set)
  210. {
  211. /* Convert irq vector # to hw int # */
  212. irq -= GIC_PIN_TO_VEC_OFFSET;
  213. /* Set irq to use shadow set */
  214. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_EIC_SHADOW_SET_BASE) +
  215. GIC_VPE_EIC_SS(irq), set);
  216. }
  217. void gic_send_ipi(unsigned int intr)
  218. {
  219. gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_SET(intr));
  220. }
  221. int gic_get_c0_compare_int(void)
  222. {
  223. if (!gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER))
  224. return MIPS_CPU_IRQ_BASE + cp0_compare_irq;
  225. return irq_create_mapping(gic_irq_domain,
  226. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_TIMER));
  227. }
  228. int gic_get_c0_perfcount_int(void)
  229. {
  230. if (!gic_local_irq_is_routable(GIC_LOCAL_INT_PERFCTR)) {
  231. /* Is the performance counter shared with the timer? */
  232. if (cp0_perfcount_irq < 0)
  233. return -1;
  234. return MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
  235. }
  236. return irq_create_mapping(gic_irq_domain,
  237. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_PERFCTR));
  238. }
  239. int gic_get_c0_fdc_int(void)
  240. {
  241. if (!gic_local_irq_is_routable(GIC_LOCAL_INT_FDC)) {
  242. /* Is the FDC IRQ even present? */
  243. if (cp0_fdc_irq < 0)
  244. return -1;
  245. return MIPS_CPU_IRQ_BASE + cp0_fdc_irq;
  246. }
  247. return irq_create_mapping(gic_irq_domain,
  248. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_FDC));
  249. }
  250. static void gic_handle_shared_int(bool chained)
  251. {
  252. unsigned int i, intr, virq, gic_reg_step = mips_cm_is64 ? 8 : 4;
  253. unsigned long *pcpu_mask;
  254. unsigned long pending_reg, intrmask_reg;
  255. DECLARE_BITMAP(pending, GIC_MAX_INTRS);
  256. DECLARE_BITMAP(intrmask, GIC_MAX_INTRS);
  257. /* Get per-cpu bitmaps */
  258. pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask;
  259. pending_reg = GIC_REG(SHARED, GIC_SH_PEND);
  260. intrmask_reg = GIC_REG(SHARED, GIC_SH_MASK);
  261. for (i = 0; i < BITS_TO_LONGS(gic_shared_intrs); i++) {
  262. pending[i] = gic_read(pending_reg);
  263. intrmask[i] = gic_read(intrmask_reg);
  264. pending_reg += gic_reg_step;
  265. intrmask_reg += gic_reg_step;
  266. if (!config_enabled(CONFIG_64BIT) || mips_cm_is64)
  267. continue;
  268. pending[i] |= (u64)gic_read(pending_reg) << 32;
  269. intrmask[i] |= (u64)gic_read(intrmask_reg) << 32;
  270. pending_reg += gic_reg_step;
  271. intrmask_reg += gic_reg_step;
  272. }
  273. bitmap_and(pending, pending, intrmask, gic_shared_intrs);
  274. bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs);
  275. intr = find_first_bit(pending, gic_shared_intrs);
  276. while (intr != gic_shared_intrs) {
  277. virq = irq_linear_revmap(gic_irq_domain,
  278. GIC_SHARED_TO_HWIRQ(intr));
  279. if (chained)
  280. generic_handle_irq(virq);
  281. else
  282. do_IRQ(virq);
  283. /* go to next pending bit */
  284. bitmap_clear(pending, intr, 1);
  285. intr = find_first_bit(pending, gic_shared_intrs);
  286. }
  287. }
  288. static void gic_mask_irq(struct irq_data *d)
  289. {
  290. gic_reset_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
  291. }
  292. static void gic_unmask_irq(struct irq_data *d)
  293. {
  294. gic_set_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
  295. }
  296. static void gic_ack_irq(struct irq_data *d)
  297. {
  298. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  299. gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_CLR(irq));
  300. }
  301. static int gic_set_type(struct irq_data *d, unsigned int type)
  302. {
  303. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  304. unsigned long flags;
  305. bool is_edge;
  306. spin_lock_irqsave(&gic_lock, flags);
  307. switch (type & IRQ_TYPE_SENSE_MASK) {
  308. case IRQ_TYPE_EDGE_FALLING:
  309. gic_set_polarity(irq, GIC_POL_NEG);
  310. gic_set_trigger(irq, GIC_TRIG_EDGE);
  311. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  312. is_edge = true;
  313. break;
  314. case IRQ_TYPE_EDGE_RISING:
  315. gic_set_polarity(irq, GIC_POL_POS);
  316. gic_set_trigger(irq, GIC_TRIG_EDGE);
  317. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  318. is_edge = true;
  319. break;
  320. case IRQ_TYPE_EDGE_BOTH:
  321. /* polarity is irrelevant in this case */
  322. gic_set_trigger(irq, GIC_TRIG_EDGE);
  323. gic_set_dual_edge(irq, GIC_TRIG_DUAL_ENABLE);
  324. is_edge = true;
  325. break;
  326. case IRQ_TYPE_LEVEL_LOW:
  327. gic_set_polarity(irq, GIC_POL_NEG);
  328. gic_set_trigger(irq, GIC_TRIG_LEVEL);
  329. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  330. is_edge = false;
  331. break;
  332. case IRQ_TYPE_LEVEL_HIGH:
  333. default:
  334. gic_set_polarity(irq, GIC_POL_POS);
  335. gic_set_trigger(irq, GIC_TRIG_LEVEL);
  336. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  337. is_edge = false;
  338. break;
  339. }
  340. if (is_edge)
  341. irq_set_chip_handler_name_locked(d, &gic_edge_irq_controller,
  342. handle_edge_irq, NULL);
  343. else
  344. irq_set_chip_handler_name_locked(d, &gic_level_irq_controller,
  345. handle_level_irq, NULL);
  346. spin_unlock_irqrestore(&gic_lock, flags);
  347. return 0;
  348. }
  349. #ifdef CONFIG_SMP
  350. static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
  351. bool force)
  352. {
  353. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  354. cpumask_t tmp = CPU_MASK_NONE;
  355. unsigned long flags;
  356. int i;
  357. cpumask_and(&tmp, cpumask, cpu_online_mask);
  358. if (cpumask_empty(&tmp))
  359. return -EINVAL;
  360. /* Assumption : cpumask refers to a single CPU */
  361. spin_lock_irqsave(&gic_lock, flags);
  362. /* Re-route this IRQ */
  363. gic_map_to_vpe(irq, mips_cm_vp_id(cpumask_first(&tmp)));
  364. /* Update the pcpu_masks */
  365. for (i = 0; i < NR_CPUS; i++)
  366. clear_bit(irq, pcpu_masks[i].pcpu_mask);
  367. set_bit(irq, pcpu_masks[cpumask_first(&tmp)].pcpu_mask);
  368. cpumask_copy(irq_data_get_affinity_mask(d), cpumask);
  369. spin_unlock_irqrestore(&gic_lock, flags);
  370. return IRQ_SET_MASK_OK_NOCOPY;
  371. }
  372. #endif
  373. static struct irq_chip gic_level_irq_controller = {
  374. .name = "MIPS GIC",
  375. .irq_mask = gic_mask_irq,
  376. .irq_unmask = gic_unmask_irq,
  377. .irq_set_type = gic_set_type,
  378. #ifdef CONFIG_SMP
  379. .irq_set_affinity = gic_set_affinity,
  380. #endif
  381. };
  382. static struct irq_chip gic_edge_irq_controller = {
  383. .name = "MIPS GIC",
  384. .irq_ack = gic_ack_irq,
  385. .irq_mask = gic_mask_irq,
  386. .irq_unmask = gic_unmask_irq,
  387. .irq_set_type = gic_set_type,
  388. #ifdef CONFIG_SMP
  389. .irq_set_affinity = gic_set_affinity,
  390. #endif
  391. };
  392. static void gic_handle_local_int(bool chained)
  393. {
  394. unsigned long pending, masked;
  395. unsigned int intr, virq;
  396. pending = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_PEND));
  397. masked = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_MASK));
  398. bitmap_and(&pending, &pending, &masked, GIC_NUM_LOCAL_INTRS);
  399. intr = find_first_bit(&pending, GIC_NUM_LOCAL_INTRS);
  400. while (intr != GIC_NUM_LOCAL_INTRS) {
  401. virq = irq_linear_revmap(gic_irq_domain,
  402. GIC_LOCAL_TO_HWIRQ(intr));
  403. if (chained)
  404. generic_handle_irq(virq);
  405. else
  406. do_IRQ(virq);
  407. /* go to next pending bit */
  408. bitmap_clear(&pending, intr, 1);
  409. intr = find_first_bit(&pending, GIC_NUM_LOCAL_INTRS);
  410. }
  411. }
  412. static void gic_mask_local_irq(struct irq_data *d)
  413. {
  414. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  415. gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_RMASK), 1 << intr);
  416. }
  417. static void gic_unmask_local_irq(struct irq_data *d)
  418. {
  419. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  420. gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_SMASK), 1 << intr);
  421. }
  422. static struct irq_chip gic_local_irq_controller = {
  423. .name = "MIPS GIC Local",
  424. .irq_mask = gic_mask_local_irq,
  425. .irq_unmask = gic_unmask_local_irq,
  426. };
  427. static void gic_mask_local_irq_all_vpes(struct irq_data *d)
  428. {
  429. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  430. int i;
  431. unsigned long flags;
  432. spin_lock_irqsave(&gic_lock, flags);
  433. for (i = 0; i < gic_vpes; i++) {
  434. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
  435. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << intr);
  436. }
  437. spin_unlock_irqrestore(&gic_lock, flags);
  438. }
  439. static void gic_unmask_local_irq_all_vpes(struct irq_data *d)
  440. {
  441. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  442. int i;
  443. unsigned long flags;
  444. spin_lock_irqsave(&gic_lock, flags);
  445. for (i = 0; i < gic_vpes; i++) {
  446. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
  447. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SMASK), 1 << intr);
  448. }
  449. spin_unlock_irqrestore(&gic_lock, flags);
  450. }
  451. static struct irq_chip gic_all_vpes_local_irq_controller = {
  452. .name = "MIPS GIC Local",
  453. .irq_mask = gic_mask_local_irq_all_vpes,
  454. .irq_unmask = gic_unmask_local_irq_all_vpes,
  455. };
  456. static void __gic_irq_dispatch(void)
  457. {
  458. gic_handle_local_int(false);
  459. gic_handle_shared_int(false);
  460. }
  461. static void gic_irq_dispatch(struct irq_desc *desc)
  462. {
  463. gic_handle_local_int(true);
  464. gic_handle_shared_int(true);
  465. }
  466. #ifdef CONFIG_MIPS_GIC_IPI
  467. static int gic_resched_int_base;
  468. static int gic_call_int_base;
  469. unsigned int plat_ipi_resched_int_xlate(unsigned int cpu)
  470. {
  471. return gic_resched_int_base + cpu;
  472. }
  473. unsigned int plat_ipi_call_int_xlate(unsigned int cpu)
  474. {
  475. return gic_call_int_base + cpu;
  476. }
  477. static irqreturn_t ipi_resched_interrupt(int irq, void *dev_id)
  478. {
  479. scheduler_ipi();
  480. return IRQ_HANDLED;
  481. }
  482. static irqreturn_t ipi_call_interrupt(int irq, void *dev_id)
  483. {
  484. generic_smp_call_function_interrupt();
  485. return IRQ_HANDLED;
  486. }
  487. static struct irqaction irq_resched = {
  488. .handler = ipi_resched_interrupt,
  489. .flags = IRQF_PERCPU,
  490. .name = "IPI resched"
  491. };
  492. static struct irqaction irq_call = {
  493. .handler = ipi_call_interrupt,
  494. .flags = IRQF_PERCPU,
  495. .name = "IPI call"
  496. };
  497. static __init void gic_ipi_init_one(unsigned int intr, int cpu,
  498. struct irqaction *action)
  499. {
  500. int virq = irq_create_mapping(gic_irq_domain,
  501. GIC_SHARED_TO_HWIRQ(intr));
  502. int i;
  503. gic_map_to_vpe(intr, mips_cm_vp_id(cpu));
  504. for (i = 0; i < NR_CPUS; i++)
  505. clear_bit(intr, pcpu_masks[i].pcpu_mask);
  506. set_bit(intr, pcpu_masks[cpu].pcpu_mask);
  507. irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
  508. irq_set_handler(virq, handle_percpu_irq);
  509. setup_irq(virq, action);
  510. }
  511. static __init void gic_ipi_init(void)
  512. {
  513. int i;
  514. /* Use last 2 * NR_CPUS interrupts as IPIs */
  515. gic_resched_int_base = gic_shared_intrs - nr_cpu_ids;
  516. gic_call_int_base = gic_resched_int_base - nr_cpu_ids;
  517. for (i = 0; i < nr_cpu_ids; i++) {
  518. gic_ipi_init_one(gic_call_int_base + i, i, &irq_call);
  519. gic_ipi_init_one(gic_resched_int_base + i, i, &irq_resched);
  520. }
  521. }
  522. #else
  523. static inline void gic_ipi_init(void)
  524. {
  525. }
  526. #endif
  527. static void __init gic_basic_init(void)
  528. {
  529. unsigned int i;
  530. board_bind_eic_interrupt = &gic_bind_eic_interrupt;
  531. /* Setup defaults */
  532. for (i = 0; i < gic_shared_intrs; i++) {
  533. gic_set_polarity(i, GIC_POL_POS);
  534. gic_set_trigger(i, GIC_TRIG_LEVEL);
  535. gic_reset_mask(i);
  536. }
  537. for (i = 0; i < gic_vpes; i++) {
  538. unsigned int j;
  539. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
  540. for (j = 0; j < GIC_NUM_LOCAL_INTRS; j++) {
  541. if (!gic_local_irq_is_routable(j))
  542. continue;
  543. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << j);
  544. }
  545. }
  546. }
  547. static int gic_local_irq_domain_map(struct irq_domain *d, unsigned int virq,
  548. irq_hw_number_t hw)
  549. {
  550. int intr = GIC_HWIRQ_TO_LOCAL(hw);
  551. int ret = 0;
  552. int i;
  553. unsigned long flags;
  554. if (!gic_local_irq_is_routable(intr))
  555. return -EPERM;
  556. /*
  557. * HACK: These are all really percpu interrupts, but the rest
  558. * of the MIPS kernel code does not use the percpu IRQ API for
  559. * the CP0 timer and performance counter interrupts.
  560. */
  561. switch (intr) {
  562. case GIC_LOCAL_INT_TIMER:
  563. case GIC_LOCAL_INT_PERFCTR:
  564. case GIC_LOCAL_INT_FDC:
  565. irq_set_chip_and_handler(virq,
  566. &gic_all_vpes_local_irq_controller,
  567. handle_percpu_irq);
  568. break;
  569. default:
  570. irq_set_chip_and_handler(virq,
  571. &gic_local_irq_controller,
  572. handle_percpu_devid_irq);
  573. irq_set_percpu_devid(virq);
  574. break;
  575. }
  576. spin_lock_irqsave(&gic_lock, flags);
  577. for (i = 0; i < gic_vpes; i++) {
  578. u32 val = GIC_MAP_TO_PIN_MSK | gic_cpu_pin;
  579. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
  580. switch (intr) {
  581. case GIC_LOCAL_INT_WD:
  582. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_WD_MAP), val);
  583. break;
  584. case GIC_LOCAL_INT_COMPARE:
  585. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_MAP),
  586. val);
  587. break;
  588. case GIC_LOCAL_INT_TIMER:
  589. /* CONFIG_MIPS_CMP workaround (see __gic_init) */
  590. val = GIC_MAP_TO_PIN_MSK | timer_cpu_pin;
  591. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_TIMER_MAP),
  592. val);
  593. break;
  594. case GIC_LOCAL_INT_PERFCTR:
  595. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_PERFCTR_MAP),
  596. val);
  597. break;
  598. case GIC_LOCAL_INT_SWINT0:
  599. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SWINT0_MAP),
  600. val);
  601. break;
  602. case GIC_LOCAL_INT_SWINT1:
  603. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SWINT1_MAP),
  604. val);
  605. break;
  606. case GIC_LOCAL_INT_FDC:
  607. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_FDC_MAP), val);
  608. break;
  609. default:
  610. pr_err("Invalid local IRQ %d\n", intr);
  611. ret = -EINVAL;
  612. break;
  613. }
  614. }
  615. spin_unlock_irqrestore(&gic_lock, flags);
  616. return ret;
  617. }
  618. static int gic_shared_irq_domain_map(struct irq_domain *d, unsigned int virq,
  619. irq_hw_number_t hw)
  620. {
  621. int intr = GIC_HWIRQ_TO_SHARED(hw);
  622. unsigned long flags;
  623. irq_set_chip_and_handler(virq, &gic_level_irq_controller,
  624. handle_level_irq);
  625. spin_lock_irqsave(&gic_lock, flags);
  626. gic_map_to_pin(intr, gic_cpu_pin);
  627. /* Map to VPE 0 by default */
  628. gic_map_to_vpe(intr, 0);
  629. set_bit(intr, pcpu_masks[0].pcpu_mask);
  630. spin_unlock_irqrestore(&gic_lock, flags);
  631. return 0;
  632. }
  633. static int gic_irq_domain_map(struct irq_domain *d, unsigned int virq,
  634. irq_hw_number_t hw)
  635. {
  636. if (GIC_HWIRQ_TO_LOCAL(hw) < GIC_NUM_LOCAL_INTRS)
  637. return gic_local_irq_domain_map(d, virq, hw);
  638. return gic_shared_irq_domain_map(d, virq, hw);
  639. }
  640. static int gic_irq_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
  641. const u32 *intspec, unsigned int intsize,
  642. irq_hw_number_t *out_hwirq,
  643. unsigned int *out_type)
  644. {
  645. if (intsize != 3)
  646. return -EINVAL;
  647. if (intspec[0] == GIC_SHARED)
  648. *out_hwirq = GIC_SHARED_TO_HWIRQ(intspec[1]);
  649. else if (intspec[0] == GIC_LOCAL)
  650. *out_hwirq = GIC_LOCAL_TO_HWIRQ(intspec[1]);
  651. else
  652. return -EINVAL;
  653. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  654. return 0;
  655. }
  656. static const struct irq_domain_ops gic_irq_domain_ops = {
  657. .map = gic_irq_domain_map,
  658. .xlate = gic_irq_domain_xlate,
  659. };
  660. static void __init __gic_init(unsigned long gic_base_addr,
  661. unsigned long gic_addrspace_size,
  662. unsigned int cpu_vec, unsigned int irqbase,
  663. struct device_node *node)
  664. {
  665. unsigned int gicconfig;
  666. gic_base = ioremap_nocache(gic_base_addr, gic_addrspace_size);
  667. gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
  668. gic_shared_intrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >>
  669. GIC_SH_CONFIG_NUMINTRS_SHF;
  670. gic_shared_intrs = ((gic_shared_intrs + 1) * 8);
  671. gic_vpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >>
  672. GIC_SH_CONFIG_NUMVPES_SHF;
  673. gic_vpes = gic_vpes + 1;
  674. if (cpu_has_veic) {
  675. /* Always use vector 1 in EIC mode */
  676. gic_cpu_pin = 0;
  677. timer_cpu_pin = gic_cpu_pin;
  678. set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET,
  679. __gic_irq_dispatch);
  680. } else {
  681. gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET;
  682. irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec,
  683. gic_irq_dispatch);
  684. /*
  685. * With the CMP implementation of SMP (deprecated), other CPUs
  686. * are started by the bootloader and put into a timer based
  687. * waiting poll loop. We must not re-route those CPU's local
  688. * timer interrupts as the wait instruction will never finish,
  689. * so just handle whatever CPU interrupt it is routed to by
  690. * default.
  691. *
  692. * This workaround should be removed when CMP support is
  693. * dropped.
  694. */
  695. if (IS_ENABLED(CONFIG_MIPS_CMP) &&
  696. gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER)) {
  697. timer_cpu_pin = gic_read32(GIC_REG(VPE_LOCAL,
  698. GIC_VPE_TIMER_MAP)) &
  699. GIC_MAP_MSK;
  700. irq_set_chained_handler(MIPS_CPU_IRQ_BASE +
  701. GIC_CPU_PIN_OFFSET +
  702. timer_cpu_pin,
  703. gic_irq_dispatch);
  704. } else {
  705. timer_cpu_pin = gic_cpu_pin;
  706. }
  707. }
  708. gic_irq_domain = irq_domain_add_simple(node, GIC_NUM_LOCAL_INTRS +
  709. gic_shared_intrs, irqbase,
  710. &gic_irq_domain_ops, NULL);
  711. if (!gic_irq_domain)
  712. panic("Failed to add GIC IRQ domain");
  713. gic_basic_init();
  714. gic_ipi_init();
  715. }
  716. void __init gic_init(unsigned long gic_base_addr,
  717. unsigned long gic_addrspace_size,
  718. unsigned int cpu_vec, unsigned int irqbase)
  719. {
  720. __gic_init(gic_base_addr, gic_addrspace_size, cpu_vec, irqbase, NULL);
  721. }
  722. static int __init gic_of_init(struct device_node *node,
  723. struct device_node *parent)
  724. {
  725. struct resource res;
  726. unsigned int cpu_vec, i = 0, reserved = 0;
  727. phys_addr_t gic_base;
  728. size_t gic_len;
  729. /* Find the first available CPU vector. */
  730. while (!of_property_read_u32_index(node, "mti,reserved-cpu-vectors",
  731. i++, &cpu_vec))
  732. reserved |= BIT(cpu_vec);
  733. for (cpu_vec = 2; cpu_vec < 8; cpu_vec++) {
  734. if (!(reserved & BIT(cpu_vec)))
  735. break;
  736. }
  737. if (cpu_vec == 8) {
  738. pr_err("No CPU vectors available for GIC\n");
  739. return -ENODEV;
  740. }
  741. if (of_address_to_resource(node, 0, &res)) {
  742. /*
  743. * Probe the CM for the GIC base address if not specified
  744. * in the device-tree.
  745. */
  746. if (mips_cm_present()) {
  747. gic_base = read_gcr_gic_base() &
  748. ~CM_GCR_GIC_BASE_GICEN_MSK;
  749. gic_len = 0x20000;
  750. } else {
  751. pr_err("Failed to get GIC memory range\n");
  752. return -ENODEV;
  753. }
  754. } else {
  755. gic_base = res.start;
  756. gic_len = resource_size(&res);
  757. }
  758. if (mips_cm_present())
  759. write_gcr_gic_base(gic_base | CM_GCR_GIC_BASE_GICEN_MSK);
  760. gic_present = true;
  761. __gic_init(gic_base, gic_len, cpu_vec, 0, node);
  762. return 0;
  763. }
  764. IRQCHIP_DECLARE(mips_gic, "mti,gic", gic_of_init);