irq-mips-gic.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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. return irq_create_mapping(gic_irq_domain,
  214. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_FDC));
  215. }
  216. static void gic_handle_shared_int(bool chained)
  217. {
  218. unsigned int i, intr, virq;
  219. unsigned long *pcpu_mask;
  220. unsigned long pending_reg, intrmask_reg;
  221. DECLARE_BITMAP(pending, GIC_MAX_INTRS);
  222. DECLARE_BITMAP(intrmask, GIC_MAX_INTRS);
  223. /* Get per-cpu bitmaps */
  224. pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask;
  225. pending_reg = GIC_REG(SHARED, GIC_SH_PEND);
  226. intrmask_reg = GIC_REG(SHARED, GIC_SH_MASK);
  227. for (i = 0; i < BITS_TO_LONGS(gic_shared_intrs); i++) {
  228. pending[i] = gic_read(pending_reg);
  229. intrmask[i] = gic_read(intrmask_reg);
  230. pending_reg += 0x4;
  231. intrmask_reg += 0x4;
  232. }
  233. bitmap_and(pending, pending, intrmask, gic_shared_intrs);
  234. bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs);
  235. intr = find_first_bit(pending, gic_shared_intrs);
  236. while (intr != gic_shared_intrs) {
  237. virq = irq_linear_revmap(gic_irq_domain,
  238. GIC_SHARED_TO_HWIRQ(intr));
  239. if (chained)
  240. generic_handle_irq(virq);
  241. else
  242. do_IRQ(virq);
  243. /* go to next pending bit */
  244. bitmap_clear(pending, intr, 1);
  245. intr = find_first_bit(pending, gic_shared_intrs);
  246. }
  247. }
  248. static void gic_mask_irq(struct irq_data *d)
  249. {
  250. gic_reset_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
  251. }
  252. static void gic_unmask_irq(struct irq_data *d)
  253. {
  254. gic_set_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
  255. }
  256. static void gic_ack_irq(struct irq_data *d)
  257. {
  258. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  259. gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_CLR(irq));
  260. }
  261. static int gic_set_type(struct irq_data *d, unsigned int type)
  262. {
  263. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  264. unsigned long flags;
  265. bool is_edge;
  266. spin_lock_irqsave(&gic_lock, flags);
  267. switch (type & IRQ_TYPE_SENSE_MASK) {
  268. case IRQ_TYPE_EDGE_FALLING:
  269. gic_set_polarity(irq, GIC_POL_NEG);
  270. gic_set_trigger(irq, GIC_TRIG_EDGE);
  271. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  272. is_edge = true;
  273. break;
  274. case IRQ_TYPE_EDGE_RISING:
  275. gic_set_polarity(irq, GIC_POL_POS);
  276. gic_set_trigger(irq, GIC_TRIG_EDGE);
  277. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  278. is_edge = true;
  279. break;
  280. case IRQ_TYPE_EDGE_BOTH:
  281. /* polarity is irrelevant in this case */
  282. gic_set_trigger(irq, GIC_TRIG_EDGE);
  283. gic_set_dual_edge(irq, GIC_TRIG_DUAL_ENABLE);
  284. is_edge = true;
  285. break;
  286. case IRQ_TYPE_LEVEL_LOW:
  287. gic_set_polarity(irq, GIC_POL_NEG);
  288. gic_set_trigger(irq, GIC_TRIG_LEVEL);
  289. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  290. is_edge = false;
  291. break;
  292. case IRQ_TYPE_LEVEL_HIGH:
  293. default:
  294. gic_set_polarity(irq, GIC_POL_POS);
  295. gic_set_trigger(irq, GIC_TRIG_LEVEL);
  296. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  297. is_edge = false;
  298. break;
  299. }
  300. if (is_edge) {
  301. __irq_set_chip_handler_name_locked(d->irq,
  302. &gic_edge_irq_controller,
  303. handle_edge_irq, NULL);
  304. } else {
  305. __irq_set_chip_handler_name_locked(d->irq,
  306. &gic_level_irq_controller,
  307. handle_level_irq, NULL);
  308. }
  309. spin_unlock_irqrestore(&gic_lock, flags);
  310. return 0;
  311. }
  312. #ifdef CONFIG_SMP
  313. static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
  314. bool force)
  315. {
  316. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  317. cpumask_t tmp = CPU_MASK_NONE;
  318. unsigned long flags;
  319. int i;
  320. cpumask_and(&tmp, cpumask, cpu_online_mask);
  321. if (cpumask_empty(&tmp))
  322. return -EINVAL;
  323. /* Assumption : cpumask refers to a single CPU */
  324. spin_lock_irqsave(&gic_lock, flags);
  325. /* Re-route this IRQ */
  326. gic_map_to_vpe(irq, cpumask_first(&tmp));
  327. /* Update the pcpu_masks */
  328. for (i = 0; i < NR_CPUS; i++)
  329. clear_bit(irq, pcpu_masks[i].pcpu_mask);
  330. set_bit(irq, pcpu_masks[cpumask_first(&tmp)].pcpu_mask);
  331. cpumask_copy(d->affinity, cpumask);
  332. spin_unlock_irqrestore(&gic_lock, flags);
  333. return IRQ_SET_MASK_OK_NOCOPY;
  334. }
  335. #endif
  336. static struct irq_chip gic_level_irq_controller = {
  337. .name = "MIPS GIC",
  338. .irq_mask = gic_mask_irq,
  339. .irq_unmask = gic_unmask_irq,
  340. .irq_set_type = gic_set_type,
  341. #ifdef CONFIG_SMP
  342. .irq_set_affinity = gic_set_affinity,
  343. #endif
  344. };
  345. static struct irq_chip gic_edge_irq_controller = {
  346. .name = "MIPS GIC",
  347. .irq_ack = gic_ack_irq,
  348. .irq_mask = gic_mask_irq,
  349. .irq_unmask = gic_unmask_irq,
  350. .irq_set_type = gic_set_type,
  351. #ifdef CONFIG_SMP
  352. .irq_set_affinity = gic_set_affinity,
  353. #endif
  354. };
  355. static void gic_handle_local_int(bool chained)
  356. {
  357. unsigned long pending, masked;
  358. unsigned int intr, virq;
  359. pending = gic_read(GIC_REG(VPE_LOCAL, GIC_VPE_PEND));
  360. masked = gic_read(GIC_REG(VPE_LOCAL, GIC_VPE_MASK));
  361. bitmap_and(&pending, &pending, &masked, GIC_NUM_LOCAL_INTRS);
  362. intr = find_first_bit(&pending, GIC_NUM_LOCAL_INTRS);
  363. while (intr != GIC_NUM_LOCAL_INTRS) {
  364. virq = irq_linear_revmap(gic_irq_domain,
  365. GIC_LOCAL_TO_HWIRQ(intr));
  366. if (chained)
  367. generic_handle_irq(virq);
  368. else
  369. do_IRQ(virq);
  370. /* go to next pending bit */
  371. bitmap_clear(&pending, intr, 1);
  372. intr = find_first_bit(&pending, GIC_NUM_LOCAL_INTRS);
  373. }
  374. }
  375. static void gic_mask_local_irq(struct irq_data *d)
  376. {
  377. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  378. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_RMASK), 1 << intr);
  379. }
  380. static void gic_unmask_local_irq(struct irq_data *d)
  381. {
  382. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  383. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_SMASK), 1 << intr);
  384. }
  385. static struct irq_chip gic_local_irq_controller = {
  386. .name = "MIPS GIC Local",
  387. .irq_mask = gic_mask_local_irq,
  388. .irq_unmask = gic_unmask_local_irq,
  389. };
  390. static void gic_mask_local_irq_all_vpes(struct irq_data *d)
  391. {
  392. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  393. int i;
  394. unsigned long flags;
  395. spin_lock_irqsave(&gic_lock, flags);
  396. for (i = 0; i < gic_vpes; i++) {
  397. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
  398. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << intr);
  399. }
  400. spin_unlock_irqrestore(&gic_lock, flags);
  401. }
  402. static void gic_unmask_local_irq_all_vpes(struct irq_data *d)
  403. {
  404. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  405. int i;
  406. unsigned long flags;
  407. spin_lock_irqsave(&gic_lock, flags);
  408. for (i = 0; i < gic_vpes; i++) {
  409. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
  410. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_SMASK), 1 << intr);
  411. }
  412. spin_unlock_irqrestore(&gic_lock, flags);
  413. }
  414. static struct irq_chip gic_all_vpes_local_irq_controller = {
  415. .name = "MIPS GIC Local",
  416. .irq_mask = gic_mask_local_irq_all_vpes,
  417. .irq_unmask = gic_unmask_local_irq_all_vpes,
  418. };
  419. static void __gic_irq_dispatch(void)
  420. {
  421. gic_handle_local_int(false);
  422. gic_handle_shared_int(false);
  423. }
  424. static void gic_irq_dispatch(unsigned int irq, struct irq_desc *desc)
  425. {
  426. gic_handle_local_int(true);
  427. gic_handle_shared_int(true);
  428. }
  429. #ifdef CONFIG_MIPS_GIC_IPI
  430. static int gic_resched_int_base;
  431. static int gic_call_int_base;
  432. unsigned int plat_ipi_resched_int_xlate(unsigned int cpu)
  433. {
  434. return gic_resched_int_base + cpu;
  435. }
  436. unsigned int plat_ipi_call_int_xlate(unsigned int cpu)
  437. {
  438. return gic_call_int_base + cpu;
  439. }
  440. static irqreturn_t ipi_resched_interrupt(int irq, void *dev_id)
  441. {
  442. scheduler_ipi();
  443. return IRQ_HANDLED;
  444. }
  445. static irqreturn_t ipi_call_interrupt(int irq, void *dev_id)
  446. {
  447. generic_smp_call_function_interrupt();
  448. return IRQ_HANDLED;
  449. }
  450. static struct irqaction irq_resched = {
  451. .handler = ipi_resched_interrupt,
  452. .flags = IRQF_PERCPU,
  453. .name = "IPI resched"
  454. };
  455. static struct irqaction irq_call = {
  456. .handler = ipi_call_interrupt,
  457. .flags = IRQF_PERCPU,
  458. .name = "IPI call"
  459. };
  460. static __init void gic_ipi_init_one(unsigned int intr, int cpu,
  461. struct irqaction *action)
  462. {
  463. int virq = irq_create_mapping(gic_irq_domain,
  464. GIC_SHARED_TO_HWIRQ(intr));
  465. int i;
  466. gic_map_to_vpe(intr, cpu);
  467. for (i = 0; i < NR_CPUS; i++)
  468. clear_bit(intr, pcpu_masks[i].pcpu_mask);
  469. set_bit(intr, pcpu_masks[cpu].pcpu_mask);
  470. irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
  471. irq_set_handler(virq, handle_percpu_irq);
  472. setup_irq(virq, action);
  473. }
  474. static __init void gic_ipi_init(void)
  475. {
  476. int i;
  477. /* Use last 2 * NR_CPUS interrupts as IPIs */
  478. gic_resched_int_base = gic_shared_intrs - nr_cpu_ids;
  479. gic_call_int_base = gic_resched_int_base - nr_cpu_ids;
  480. for (i = 0; i < nr_cpu_ids; i++) {
  481. gic_ipi_init_one(gic_call_int_base + i, i, &irq_call);
  482. gic_ipi_init_one(gic_resched_int_base + i, i, &irq_resched);
  483. }
  484. }
  485. #else
  486. static inline void gic_ipi_init(void)
  487. {
  488. }
  489. #endif
  490. static void __init gic_basic_init(void)
  491. {
  492. unsigned int i;
  493. board_bind_eic_interrupt = &gic_bind_eic_interrupt;
  494. /* Setup defaults */
  495. for (i = 0; i < gic_shared_intrs; i++) {
  496. gic_set_polarity(i, GIC_POL_POS);
  497. gic_set_trigger(i, GIC_TRIG_LEVEL);
  498. gic_reset_mask(i);
  499. }
  500. for (i = 0; i < gic_vpes; i++) {
  501. unsigned int j;
  502. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
  503. for (j = 0; j < GIC_NUM_LOCAL_INTRS; j++) {
  504. if (!gic_local_irq_is_routable(j))
  505. continue;
  506. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << j);
  507. }
  508. }
  509. }
  510. static int gic_local_irq_domain_map(struct irq_domain *d, unsigned int virq,
  511. irq_hw_number_t hw)
  512. {
  513. int intr = GIC_HWIRQ_TO_LOCAL(hw);
  514. int ret = 0;
  515. int i;
  516. unsigned long flags;
  517. if (!gic_local_irq_is_routable(intr))
  518. return -EPERM;
  519. /*
  520. * HACK: These are all really percpu interrupts, but the rest
  521. * of the MIPS kernel code does not use the percpu IRQ API for
  522. * the CP0 timer and performance counter interrupts.
  523. */
  524. switch (intr) {
  525. case GIC_LOCAL_INT_TIMER:
  526. case GIC_LOCAL_INT_PERFCTR:
  527. case GIC_LOCAL_INT_FDC:
  528. irq_set_chip_and_handler(virq,
  529. &gic_all_vpes_local_irq_controller,
  530. handle_percpu_irq);
  531. break;
  532. default:
  533. irq_set_chip_and_handler(virq,
  534. &gic_local_irq_controller,
  535. handle_percpu_devid_irq);
  536. irq_set_percpu_devid(virq);
  537. break;
  538. }
  539. spin_lock_irqsave(&gic_lock, flags);
  540. for (i = 0; i < gic_vpes; i++) {
  541. u32 val = GIC_MAP_TO_PIN_MSK | gic_cpu_pin;
  542. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
  543. switch (intr) {
  544. case GIC_LOCAL_INT_WD:
  545. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_WD_MAP), val);
  546. break;
  547. case GIC_LOCAL_INT_COMPARE:
  548. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_MAP), val);
  549. break;
  550. case GIC_LOCAL_INT_TIMER:
  551. /* CONFIG_MIPS_CMP workaround (see __gic_init) */
  552. val = GIC_MAP_TO_PIN_MSK | timer_cpu_pin;
  553. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_TIMER_MAP), val);
  554. break;
  555. case GIC_LOCAL_INT_PERFCTR:
  556. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_PERFCTR_MAP), val);
  557. break;
  558. case GIC_LOCAL_INT_SWINT0:
  559. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_SWINT0_MAP), val);
  560. break;
  561. case GIC_LOCAL_INT_SWINT1:
  562. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_SWINT1_MAP), val);
  563. break;
  564. case GIC_LOCAL_INT_FDC:
  565. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_FDC_MAP), val);
  566. break;
  567. default:
  568. pr_err("Invalid local IRQ %d\n", intr);
  569. ret = -EINVAL;
  570. break;
  571. }
  572. }
  573. spin_unlock_irqrestore(&gic_lock, flags);
  574. return ret;
  575. }
  576. static int gic_shared_irq_domain_map(struct irq_domain *d, unsigned int virq,
  577. irq_hw_number_t hw)
  578. {
  579. int intr = GIC_HWIRQ_TO_SHARED(hw);
  580. unsigned long flags;
  581. irq_set_chip_and_handler(virq, &gic_level_irq_controller,
  582. handle_level_irq);
  583. spin_lock_irqsave(&gic_lock, flags);
  584. gic_map_to_pin(intr, gic_cpu_pin);
  585. /* Map to VPE 0 by default */
  586. gic_map_to_vpe(intr, 0);
  587. set_bit(intr, pcpu_masks[0].pcpu_mask);
  588. spin_unlock_irqrestore(&gic_lock, flags);
  589. return 0;
  590. }
  591. static int gic_irq_domain_map(struct irq_domain *d, unsigned int virq,
  592. irq_hw_number_t hw)
  593. {
  594. if (GIC_HWIRQ_TO_LOCAL(hw) < GIC_NUM_LOCAL_INTRS)
  595. return gic_local_irq_domain_map(d, virq, hw);
  596. return gic_shared_irq_domain_map(d, virq, hw);
  597. }
  598. static int gic_irq_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
  599. const u32 *intspec, unsigned int intsize,
  600. irq_hw_number_t *out_hwirq,
  601. unsigned int *out_type)
  602. {
  603. if (intsize != 3)
  604. return -EINVAL;
  605. if (intspec[0] == GIC_SHARED)
  606. *out_hwirq = GIC_SHARED_TO_HWIRQ(intspec[1]);
  607. else if (intspec[0] == GIC_LOCAL)
  608. *out_hwirq = GIC_LOCAL_TO_HWIRQ(intspec[1]);
  609. else
  610. return -EINVAL;
  611. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  612. return 0;
  613. }
  614. static const struct irq_domain_ops gic_irq_domain_ops = {
  615. .map = gic_irq_domain_map,
  616. .xlate = gic_irq_domain_xlate,
  617. };
  618. static void __init __gic_init(unsigned long gic_base_addr,
  619. unsigned long gic_addrspace_size,
  620. unsigned int cpu_vec, unsigned int irqbase,
  621. struct device_node *node)
  622. {
  623. unsigned int gicconfig;
  624. gic_base = ioremap_nocache(gic_base_addr, gic_addrspace_size);
  625. gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
  626. gic_shared_intrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >>
  627. GIC_SH_CONFIG_NUMINTRS_SHF;
  628. gic_shared_intrs = ((gic_shared_intrs + 1) * 8);
  629. gic_vpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >>
  630. GIC_SH_CONFIG_NUMVPES_SHF;
  631. gic_vpes = gic_vpes + 1;
  632. if (cpu_has_veic) {
  633. /* Always use vector 1 in EIC mode */
  634. gic_cpu_pin = 0;
  635. timer_cpu_pin = gic_cpu_pin;
  636. set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET,
  637. __gic_irq_dispatch);
  638. } else {
  639. gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET;
  640. irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec,
  641. gic_irq_dispatch);
  642. /*
  643. * With the CMP implementation of SMP (deprecated), other CPUs
  644. * are started by the bootloader and put into a timer based
  645. * waiting poll loop. We must not re-route those CPU's local
  646. * timer interrupts as the wait instruction will never finish,
  647. * so just handle whatever CPU interrupt it is routed to by
  648. * default.
  649. *
  650. * This workaround should be removed when CMP support is
  651. * dropped.
  652. */
  653. if (IS_ENABLED(CONFIG_MIPS_CMP) &&
  654. gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER)) {
  655. timer_cpu_pin = gic_read(GIC_REG(VPE_LOCAL,
  656. GIC_VPE_TIMER_MAP)) &
  657. GIC_MAP_MSK;
  658. irq_set_chained_handler(MIPS_CPU_IRQ_BASE +
  659. GIC_CPU_PIN_OFFSET +
  660. timer_cpu_pin,
  661. gic_irq_dispatch);
  662. } else {
  663. timer_cpu_pin = gic_cpu_pin;
  664. }
  665. }
  666. gic_irq_domain = irq_domain_add_simple(node, GIC_NUM_LOCAL_INTRS +
  667. gic_shared_intrs, irqbase,
  668. &gic_irq_domain_ops, NULL);
  669. if (!gic_irq_domain)
  670. panic("Failed to add GIC IRQ domain");
  671. gic_basic_init();
  672. gic_ipi_init();
  673. }
  674. void __init gic_init(unsigned long gic_base_addr,
  675. unsigned long gic_addrspace_size,
  676. unsigned int cpu_vec, unsigned int irqbase)
  677. {
  678. __gic_init(gic_base_addr, gic_addrspace_size, cpu_vec, irqbase, NULL);
  679. }
  680. static int __init gic_of_init(struct device_node *node,
  681. struct device_node *parent)
  682. {
  683. struct resource res;
  684. unsigned int cpu_vec, i = 0, reserved = 0;
  685. phys_addr_t gic_base;
  686. size_t gic_len;
  687. /* Find the first available CPU vector. */
  688. while (!of_property_read_u32_index(node, "mti,reserved-cpu-vectors",
  689. i++, &cpu_vec))
  690. reserved |= BIT(cpu_vec);
  691. for (cpu_vec = 2; cpu_vec < 8; cpu_vec++) {
  692. if (!(reserved & BIT(cpu_vec)))
  693. break;
  694. }
  695. if (cpu_vec == 8) {
  696. pr_err("No CPU vectors available for GIC\n");
  697. return -ENODEV;
  698. }
  699. if (of_address_to_resource(node, 0, &res)) {
  700. /*
  701. * Probe the CM for the GIC base address if not specified
  702. * in the device-tree.
  703. */
  704. if (mips_cm_present()) {
  705. gic_base = read_gcr_gic_base() &
  706. ~CM_GCR_GIC_BASE_GICEN_MSK;
  707. gic_len = 0x20000;
  708. } else {
  709. pr_err("Failed to get GIC memory range\n");
  710. return -ENODEV;
  711. }
  712. } else {
  713. gic_base = res.start;
  714. gic_len = resource_size(&res);
  715. }
  716. if (mips_cm_present())
  717. write_gcr_gic_base(gic_base | CM_GCR_GIC_BASE_GICEN_MSK);
  718. gic_present = true;
  719. __gic_init(gic_base, gic_len, cpu_vec, 0, node);
  720. return 0;
  721. }
  722. IRQCHIP_DECLARE(mips_gic, "mti,gic", gic_of_init);