irq-mips-gic.c 23 KB

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