irq-mips-gic.c 20 KB

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