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