irq-mips-gic.c 21 KB

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