irq-mips-gic.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. struct gic_irq_spec {
  28. enum {
  29. GIC_DEVICE,
  30. GIC_IPI
  31. } type;
  32. union {
  33. struct cpumask *ipimask;
  34. unsigned int hwirq;
  35. };
  36. };
  37. static unsigned long __gic_base_addr;
  38. static void __iomem *gic_base;
  39. static struct gic_pcpu_mask pcpu_masks[NR_CPUS];
  40. static DEFINE_SPINLOCK(gic_lock);
  41. static struct irq_domain *gic_irq_domain;
  42. static struct irq_domain *gic_dev_domain;
  43. static struct irq_domain *gic_ipi_domain;
  44. static int gic_shared_intrs;
  45. static int gic_vpes;
  46. static unsigned int gic_cpu_pin;
  47. static unsigned int timer_cpu_pin;
  48. static struct irq_chip gic_level_irq_controller, gic_edge_irq_controller;
  49. DECLARE_BITMAP(ipi_resrv, GIC_MAX_INTRS);
  50. static void __gic_irq_dispatch(void);
  51. static inline u32 gic_read32(unsigned int reg)
  52. {
  53. return __raw_readl(gic_base + reg);
  54. }
  55. static inline u64 gic_read64(unsigned int reg)
  56. {
  57. return __raw_readq(gic_base + reg);
  58. }
  59. static inline unsigned long gic_read(unsigned int reg)
  60. {
  61. if (!mips_cm_is64)
  62. return gic_read32(reg);
  63. else
  64. return gic_read64(reg);
  65. }
  66. static inline void gic_write32(unsigned int reg, u32 val)
  67. {
  68. return __raw_writel(val, gic_base + reg);
  69. }
  70. static inline void gic_write64(unsigned int reg, u64 val)
  71. {
  72. return __raw_writeq(val, gic_base + reg);
  73. }
  74. static inline void gic_write(unsigned int reg, unsigned long val)
  75. {
  76. if (!mips_cm_is64)
  77. return gic_write32(reg, (u32)val);
  78. else
  79. return gic_write64(reg, (u64)val);
  80. }
  81. static inline void gic_update_bits(unsigned int reg, unsigned long mask,
  82. unsigned long val)
  83. {
  84. unsigned long regval;
  85. regval = gic_read(reg);
  86. regval &= ~mask;
  87. regval |= val;
  88. gic_write(reg, regval);
  89. }
  90. static inline void gic_reset_mask(unsigned int intr)
  91. {
  92. gic_write(GIC_REG(SHARED, GIC_SH_RMASK) + GIC_INTR_OFS(intr),
  93. 1ul << GIC_INTR_BIT(intr));
  94. }
  95. static inline void gic_set_mask(unsigned int intr)
  96. {
  97. gic_write(GIC_REG(SHARED, GIC_SH_SMASK) + GIC_INTR_OFS(intr),
  98. 1ul << GIC_INTR_BIT(intr));
  99. }
  100. static inline void gic_set_polarity(unsigned int intr, unsigned int pol)
  101. {
  102. gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_POLARITY) +
  103. GIC_INTR_OFS(intr), 1ul << GIC_INTR_BIT(intr),
  104. (unsigned long)pol << GIC_INTR_BIT(intr));
  105. }
  106. static inline void gic_set_trigger(unsigned int intr, unsigned int trig)
  107. {
  108. gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_TRIGGER) +
  109. GIC_INTR_OFS(intr), 1ul << GIC_INTR_BIT(intr),
  110. (unsigned long)trig << GIC_INTR_BIT(intr));
  111. }
  112. static inline void gic_set_dual_edge(unsigned int intr, unsigned int dual)
  113. {
  114. gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_DUAL) + GIC_INTR_OFS(intr),
  115. 1ul << GIC_INTR_BIT(intr),
  116. (unsigned long)dual << GIC_INTR_BIT(intr));
  117. }
  118. static inline void gic_map_to_pin(unsigned int intr, unsigned int pin)
  119. {
  120. gic_write32(GIC_REG(SHARED, GIC_SH_INTR_MAP_TO_PIN_BASE) +
  121. GIC_SH_MAP_TO_PIN(intr), GIC_MAP_TO_PIN_MSK | pin);
  122. }
  123. static inline void gic_map_to_vpe(unsigned int intr, unsigned int vpe)
  124. {
  125. gic_write(GIC_REG(SHARED, GIC_SH_INTR_MAP_TO_VPE_BASE) +
  126. GIC_SH_MAP_TO_VPE_REG_OFF(intr, vpe),
  127. GIC_SH_MAP_TO_VPE_REG_BIT(vpe));
  128. }
  129. #ifdef CONFIG_CLKSRC_MIPS_GIC
  130. cycle_t gic_read_count(void)
  131. {
  132. unsigned int hi, hi2, lo;
  133. if (mips_cm_is64)
  134. return (cycle_t)gic_read(GIC_REG(SHARED, GIC_SH_COUNTER));
  135. do {
  136. hi = gic_read32(GIC_REG(SHARED, GIC_SH_COUNTER_63_32));
  137. lo = gic_read32(GIC_REG(SHARED, GIC_SH_COUNTER_31_00));
  138. hi2 = gic_read32(GIC_REG(SHARED, GIC_SH_COUNTER_63_32));
  139. } while (hi2 != hi);
  140. return (((cycle_t) hi) << 32) + lo;
  141. }
  142. unsigned int gic_get_count_width(void)
  143. {
  144. unsigned int bits, config;
  145. config = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
  146. bits = 32 + 4 * ((config & GIC_SH_CONFIG_COUNTBITS_MSK) >>
  147. GIC_SH_CONFIG_COUNTBITS_SHF);
  148. return bits;
  149. }
  150. void gic_write_compare(cycle_t cnt)
  151. {
  152. if (mips_cm_is64) {
  153. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE), cnt);
  154. } else {
  155. gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_HI),
  156. (int)(cnt >> 32));
  157. gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_LO),
  158. (int)(cnt & 0xffffffff));
  159. }
  160. }
  161. void gic_write_cpu_compare(cycle_t cnt, int cpu)
  162. {
  163. unsigned long flags;
  164. local_irq_save(flags);
  165. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), cpu);
  166. if (mips_cm_is64) {
  167. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE), cnt);
  168. } else {
  169. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_HI),
  170. (int)(cnt >> 32));
  171. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_LO),
  172. (int)(cnt & 0xffffffff));
  173. }
  174. local_irq_restore(flags);
  175. }
  176. cycle_t gic_read_compare(void)
  177. {
  178. unsigned int hi, lo;
  179. if (mips_cm_is64)
  180. return (cycle_t)gic_read(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE));
  181. hi = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_HI));
  182. lo = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_LO));
  183. return (((cycle_t) hi) << 32) + lo;
  184. }
  185. void gic_start_count(void)
  186. {
  187. u32 gicconfig;
  188. /* Start the counter */
  189. gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
  190. gicconfig &= ~(1 << GIC_SH_CONFIG_COUNTSTOP_SHF);
  191. gic_write(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig);
  192. }
  193. void gic_stop_count(void)
  194. {
  195. u32 gicconfig;
  196. /* Stop the counter */
  197. gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
  198. gicconfig |= 1 << GIC_SH_CONFIG_COUNTSTOP_SHF;
  199. gic_write(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig);
  200. }
  201. #endif
  202. static bool gic_local_irq_is_routable(int intr)
  203. {
  204. u32 vpe_ctl;
  205. /* All local interrupts are routable in EIC mode. */
  206. if (cpu_has_veic)
  207. return true;
  208. vpe_ctl = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_CTL));
  209. switch (intr) {
  210. case GIC_LOCAL_INT_TIMER:
  211. return vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK;
  212. case GIC_LOCAL_INT_PERFCTR:
  213. return vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK;
  214. case GIC_LOCAL_INT_FDC:
  215. return vpe_ctl & GIC_VPE_CTL_FDC_RTBL_MSK;
  216. case GIC_LOCAL_INT_SWINT0:
  217. case GIC_LOCAL_INT_SWINT1:
  218. return vpe_ctl & GIC_VPE_CTL_SWINT_RTBL_MSK;
  219. default:
  220. return true;
  221. }
  222. }
  223. static void gic_bind_eic_interrupt(int irq, int set)
  224. {
  225. /* Convert irq vector # to hw int # */
  226. irq -= GIC_PIN_TO_VEC_OFFSET;
  227. /* Set irq to use shadow set */
  228. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_EIC_SHADOW_SET_BASE) +
  229. GIC_VPE_EIC_SS(irq), set);
  230. }
  231. static void gic_send_ipi(struct irq_data *d, unsigned int cpu)
  232. {
  233. irq_hw_number_t hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(d));
  234. gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_SET(hwirq));
  235. }
  236. int gic_get_c0_compare_int(void)
  237. {
  238. if (!gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER))
  239. return MIPS_CPU_IRQ_BASE + cp0_compare_irq;
  240. return irq_create_mapping(gic_irq_domain,
  241. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_TIMER));
  242. }
  243. int gic_get_c0_perfcount_int(void)
  244. {
  245. if (!gic_local_irq_is_routable(GIC_LOCAL_INT_PERFCTR)) {
  246. /* Is the performance counter shared with the timer? */
  247. if (cp0_perfcount_irq < 0)
  248. return -1;
  249. return MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
  250. }
  251. return irq_create_mapping(gic_irq_domain,
  252. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_PERFCTR));
  253. }
  254. int gic_get_c0_fdc_int(void)
  255. {
  256. if (!gic_local_irq_is_routable(GIC_LOCAL_INT_FDC)) {
  257. /* Is the FDC IRQ even present? */
  258. if (cp0_fdc_irq < 0)
  259. return -1;
  260. return MIPS_CPU_IRQ_BASE + cp0_fdc_irq;
  261. }
  262. return irq_create_mapping(gic_irq_domain,
  263. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_FDC));
  264. }
  265. int gic_get_usm_range(struct resource *gic_usm_res)
  266. {
  267. if (!gic_present)
  268. return -1;
  269. gic_usm_res->start = __gic_base_addr + USM_VISIBLE_SECTION_OFS;
  270. gic_usm_res->end = gic_usm_res->start + (USM_VISIBLE_SECTION_SIZE - 1);
  271. return 0;
  272. }
  273. static void gic_handle_shared_int(bool chained)
  274. {
  275. unsigned int i, intr, virq, gic_reg_step = mips_cm_is64 ? 8 : 4;
  276. unsigned long *pcpu_mask;
  277. unsigned long pending_reg, intrmask_reg;
  278. DECLARE_BITMAP(pending, GIC_MAX_INTRS);
  279. DECLARE_BITMAP(intrmask, GIC_MAX_INTRS);
  280. /* Get per-cpu bitmaps */
  281. pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask;
  282. pending_reg = GIC_REG(SHARED, GIC_SH_PEND);
  283. intrmask_reg = GIC_REG(SHARED, GIC_SH_MASK);
  284. for (i = 0; i < BITS_TO_LONGS(gic_shared_intrs); i++) {
  285. pending[i] = gic_read(pending_reg);
  286. intrmask[i] = gic_read(intrmask_reg);
  287. pending_reg += gic_reg_step;
  288. intrmask_reg += gic_reg_step;
  289. if (!config_enabled(CONFIG_64BIT) || mips_cm_is64)
  290. continue;
  291. pending[i] |= (u64)gic_read(pending_reg) << 32;
  292. intrmask[i] |= (u64)gic_read(intrmask_reg) << 32;
  293. pending_reg += gic_reg_step;
  294. intrmask_reg += gic_reg_step;
  295. }
  296. bitmap_and(pending, pending, intrmask, gic_shared_intrs);
  297. bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs);
  298. intr = find_first_bit(pending, gic_shared_intrs);
  299. while (intr != gic_shared_intrs) {
  300. virq = irq_linear_revmap(gic_irq_domain,
  301. GIC_SHARED_TO_HWIRQ(intr));
  302. if (chained)
  303. generic_handle_irq(virq);
  304. else
  305. do_IRQ(virq);
  306. /* go to next pending bit */
  307. bitmap_clear(pending, intr, 1);
  308. intr = find_first_bit(pending, gic_shared_intrs);
  309. }
  310. }
  311. static void gic_mask_irq(struct irq_data *d)
  312. {
  313. gic_reset_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
  314. }
  315. static void gic_unmask_irq(struct irq_data *d)
  316. {
  317. gic_set_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
  318. }
  319. static void gic_ack_irq(struct irq_data *d)
  320. {
  321. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  322. gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_CLR(irq));
  323. }
  324. static int gic_set_type(struct irq_data *d, unsigned int type)
  325. {
  326. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  327. unsigned long flags;
  328. bool is_edge;
  329. spin_lock_irqsave(&gic_lock, flags);
  330. switch (type & IRQ_TYPE_SENSE_MASK) {
  331. case IRQ_TYPE_EDGE_FALLING:
  332. gic_set_polarity(irq, GIC_POL_NEG);
  333. gic_set_trigger(irq, GIC_TRIG_EDGE);
  334. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  335. is_edge = true;
  336. break;
  337. case IRQ_TYPE_EDGE_RISING:
  338. gic_set_polarity(irq, GIC_POL_POS);
  339. gic_set_trigger(irq, GIC_TRIG_EDGE);
  340. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  341. is_edge = true;
  342. break;
  343. case IRQ_TYPE_EDGE_BOTH:
  344. /* polarity is irrelevant in this case */
  345. gic_set_trigger(irq, GIC_TRIG_EDGE);
  346. gic_set_dual_edge(irq, GIC_TRIG_DUAL_ENABLE);
  347. is_edge = true;
  348. break;
  349. case IRQ_TYPE_LEVEL_LOW:
  350. gic_set_polarity(irq, GIC_POL_NEG);
  351. gic_set_trigger(irq, GIC_TRIG_LEVEL);
  352. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  353. is_edge = false;
  354. break;
  355. case IRQ_TYPE_LEVEL_HIGH:
  356. default:
  357. gic_set_polarity(irq, GIC_POL_POS);
  358. gic_set_trigger(irq, GIC_TRIG_LEVEL);
  359. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  360. is_edge = false;
  361. break;
  362. }
  363. if (is_edge)
  364. irq_set_chip_handler_name_locked(d, &gic_edge_irq_controller,
  365. handle_edge_irq, NULL);
  366. else
  367. irq_set_chip_handler_name_locked(d, &gic_level_irq_controller,
  368. handle_level_irq, NULL);
  369. spin_unlock_irqrestore(&gic_lock, flags);
  370. return 0;
  371. }
  372. #ifdef CONFIG_SMP
  373. static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
  374. bool force)
  375. {
  376. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  377. cpumask_t tmp = CPU_MASK_NONE;
  378. unsigned long flags;
  379. int i;
  380. cpumask_and(&tmp, cpumask, cpu_online_mask);
  381. if (cpumask_empty(&tmp))
  382. return -EINVAL;
  383. /* Assumption : cpumask refers to a single CPU */
  384. spin_lock_irqsave(&gic_lock, flags);
  385. /* Re-route this IRQ */
  386. gic_map_to_vpe(irq, mips_cm_vp_id(cpumask_first(&tmp)));
  387. /* Update the pcpu_masks */
  388. for (i = 0; i < gic_vpes; i++)
  389. clear_bit(irq, pcpu_masks[i].pcpu_mask);
  390. set_bit(irq, pcpu_masks[cpumask_first(&tmp)].pcpu_mask);
  391. cpumask_copy(irq_data_get_affinity_mask(d), cpumask);
  392. spin_unlock_irqrestore(&gic_lock, flags);
  393. return IRQ_SET_MASK_OK_NOCOPY;
  394. }
  395. #endif
  396. static struct irq_chip gic_level_irq_controller = {
  397. .name = "MIPS GIC",
  398. .irq_mask = gic_mask_irq,
  399. .irq_unmask = gic_unmask_irq,
  400. .irq_set_type = gic_set_type,
  401. #ifdef CONFIG_SMP
  402. .irq_set_affinity = gic_set_affinity,
  403. #endif
  404. };
  405. static struct irq_chip gic_edge_irq_controller = {
  406. .name = "MIPS GIC",
  407. .irq_ack = gic_ack_irq,
  408. .irq_mask = gic_mask_irq,
  409. .irq_unmask = gic_unmask_irq,
  410. .irq_set_type = gic_set_type,
  411. #ifdef CONFIG_SMP
  412. .irq_set_affinity = gic_set_affinity,
  413. #endif
  414. .ipi_send_single = gic_send_ipi,
  415. };
  416. static void gic_handle_local_int(bool chained)
  417. {
  418. unsigned long pending, masked;
  419. unsigned int intr, virq;
  420. pending = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_PEND));
  421. masked = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_MASK));
  422. bitmap_and(&pending, &pending, &masked, GIC_NUM_LOCAL_INTRS);
  423. intr = find_first_bit(&pending, GIC_NUM_LOCAL_INTRS);
  424. while (intr != GIC_NUM_LOCAL_INTRS) {
  425. virq = irq_linear_revmap(gic_irq_domain,
  426. GIC_LOCAL_TO_HWIRQ(intr));
  427. if (chained)
  428. generic_handle_irq(virq);
  429. else
  430. do_IRQ(virq);
  431. /* go to next pending bit */
  432. bitmap_clear(&pending, intr, 1);
  433. intr = find_first_bit(&pending, GIC_NUM_LOCAL_INTRS);
  434. }
  435. }
  436. static void gic_mask_local_irq(struct irq_data *d)
  437. {
  438. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  439. gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_RMASK), 1 << intr);
  440. }
  441. static void gic_unmask_local_irq(struct irq_data *d)
  442. {
  443. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  444. gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_SMASK), 1 << intr);
  445. }
  446. static struct irq_chip gic_local_irq_controller = {
  447. .name = "MIPS GIC Local",
  448. .irq_mask = gic_mask_local_irq,
  449. .irq_unmask = gic_unmask_local_irq,
  450. };
  451. static void gic_mask_local_irq_all_vpes(struct irq_data *d)
  452. {
  453. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  454. int i;
  455. unsigned long flags;
  456. spin_lock_irqsave(&gic_lock, flags);
  457. for (i = 0; i < gic_vpes; i++) {
  458. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
  459. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << intr);
  460. }
  461. spin_unlock_irqrestore(&gic_lock, flags);
  462. }
  463. static void gic_unmask_local_irq_all_vpes(struct irq_data *d)
  464. {
  465. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  466. int i;
  467. unsigned long flags;
  468. spin_lock_irqsave(&gic_lock, flags);
  469. for (i = 0; i < gic_vpes; i++) {
  470. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
  471. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SMASK), 1 << intr);
  472. }
  473. spin_unlock_irqrestore(&gic_lock, flags);
  474. }
  475. static struct irq_chip gic_all_vpes_local_irq_controller = {
  476. .name = "MIPS GIC Local",
  477. .irq_mask = gic_mask_local_irq_all_vpes,
  478. .irq_unmask = gic_unmask_local_irq_all_vpes,
  479. };
  480. static void __gic_irq_dispatch(void)
  481. {
  482. gic_handle_local_int(false);
  483. gic_handle_shared_int(false);
  484. }
  485. static void gic_irq_dispatch(struct irq_desc *desc)
  486. {
  487. gic_handle_local_int(true);
  488. gic_handle_shared_int(true);
  489. }
  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_write32(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_write32(GIC_REG(VPE_OTHER, GIC_VPE_WD_MAP), val);
  546. break;
  547. case GIC_LOCAL_INT_COMPARE:
  548. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_MAP),
  549. val);
  550. break;
  551. case GIC_LOCAL_INT_TIMER:
  552. /* CONFIG_MIPS_CMP workaround (see __gic_init) */
  553. val = GIC_MAP_TO_PIN_MSK | timer_cpu_pin;
  554. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_TIMER_MAP),
  555. val);
  556. break;
  557. case GIC_LOCAL_INT_PERFCTR:
  558. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_PERFCTR_MAP),
  559. val);
  560. break;
  561. case GIC_LOCAL_INT_SWINT0:
  562. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SWINT0_MAP),
  563. val);
  564. break;
  565. case GIC_LOCAL_INT_SWINT1:
  566. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SWINT1_MAP),
  567. val);
  568. break;
  569. case GIC_LOCAL_INT_FDC:
  570. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_FDC_MAP), val);
  571. break;
  572. default:
  573. pr_err("Invalid local IRQ %d\n", intr);
  574. ret = -EINVAL;
  575. break;
  576. }
  577. }
  578. spin_unlock_irqrestore(&gic_lock, flags);
  579. return ret;
  580. }
  581. static int gic_shared_irq_domain_map(struct irq_domain *d, unsigned int virq,
  582. irq_hw_number_t hw, unsigned int vpe)
  583. {
  584. int intr = GIC_HWIRQ_TO_SHARED(hw);
  585. unsigned long flags;
  586. int i;
  587. irq_set_chip_and_handler(virq, &gic_level_irq_controller,
  588. handle_level_irq);
  589. spin_lock_irqsave(&gic_lock, flags);
  590. gic_map_to_pin(intr, gic_cpu_pin);
  591. gic_map_to_vpe(intr, vpe);
  592. for (i = 0; i < gic_vpes; i++)
  593. clear_bit(intr, pcpu_masks[i].pcpu_mask);
  594. set_bit(intr, pcpu_masks[vpe].pcpu_mask);
  595. spin_unlock_irqrestore(&gic_lock, flags);
  596. return 0;
  597. }
  598. static int gic_irq_domain_map(struct irq_domain *d, unsigned int virq,
  599. irq_hw_number_t hw)
  600. {
  601. if (GIC_HWIRQ_TO_LOCAL(hw) < GIC_NUM_LOCAL_INTRS)
  602. return gic_local_irq_domain_map(d, virq, hw);
  603. return gic_shared_irq_domain_map(d, virq, hw, 0);
  604. }
  605. static int gic_irq_domain_alloc(struct irq_domain *d, unsigned int virq,
  606. unsigned int nr_irqs, void *arg)
  607. {
  608. struct gic_irq_spec *spec = arg;
  609. irq_hw_number_t hwirq, base_hwirq;
  610. int cpu, ret, i;
  611. if (spec->type == GIC_DEVICE) {
  612. /* verify that it doesn't conflict with an IPI irq */
  613. if (test_bit(spec->hwirq, ipi_resrv))
  614. return -EBUSY;
  615. } else {
  616. base_hwirq = find_first_bit(ipi_resrv, gic_shared_intrs);
  617. if (base_hwirq == gic_shared_intrs) {
  618. return -ENOMEM;
  619. }
  620. /* check that we have enough space */
  621. for (i = base_hwirq; i < nr_irqs; i++) {
  622. if (!test_bit(i, ipi_resrv))
  623. return -EBUSY;
  624. }
  625. bitmap_clear(ipi_resrv, base_hwirq, nr_irqs);
  626. /* map the hwirq for each cpu consecutively */
  627. i = 0;
  628. for_each_cpu(cpu, spec->ipimask) {
  629. hwirq = GIC_SHARED_TO_HWIRQ(base_hwirq + i);
  630. ret = irq_domain_set_hwirq_and_chip(d, virq + i, hwirq,
  631. &gic_edge_irq_controller,
  632. NULL);
  633. if (ret)
  634. goto error;
  635. ret = gic_shared_irq_domain_map(d, virq + i, hwirq, cpu);
  636. if (ret)
  637. goto error;
  638. i++;
  639. }
  640. /*
  641. * tell the parent about the base hwirq we allocated so it can
  642. * set its own domain data
  643. */
  644. spec->hwirq = base_hwirq;
  645. }
  646. return 0;
  647. error:
  648. bitmap_set(ipi_resrv, base_hwirq, nr_irqs);
  649. return ret;
  650. }
  651. void gic_irq_domain_free(struct irq_domain *d, unsigned int virq,
  652. unsigned int nr_irqs)
  653. {
  654. irq_hw_number_t base_hwirq;
  655. struct irq_data *data;
  656. data = irq_get_irq_data(virq);
  657. if (!data)
  658. return;
  659. base_hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(data));
  660. bitmap_set(ipi_resrv, base_hwirq, nr_irqs);
  661. }
  662. int gic_irq_domain_match(struct irq_domain *d, struct device_node *node,
  663. enum irq_domain_bus_token bus_token)
  664. {
  665. /* this domain should'nt be accessed directly */
  666. return 0;
  667. }
  668. static const struct irq_domain_ops gic_irq_domain_ops = {
  669. .map = gic_irq_domain_map,
  670. .alloc = gic_irq_domain_alloc,
  671. .free = gic_irq_domain_free,
  672. .match = gic_irq_domain_match,
  673. };
  674. static int gic_dev_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
  675. const u32 *intspec, unsigned int intsize,
  676. irq_hw_number_t *out_hwirq,
  677. unsigned int *out_type)
  678. {
  679. if (intsize != 3)
  680. return -EINVAL;
  681. if (intspec[0] == GIC_SHARED)
  682. *out_hwirq = GIC_SHARED_TO_HWIRQ(intspec[1]);
  683. else if (intspec[0] == GIC_LOCAL)
  684. *out_hwirq = GIC_LOCAL_TO_HWIRQ(intspec[1]);
  685. else
  686. return -EINVAL;
  687. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  688. return 0;
  689. }
  690. static int gic_dev_domain_alloc(struct irq_domain *d, unsigned int virq,
  691. unsigned int nr_irqs, void *arg)
  692. {
  693. struct irq_fwspec *fwspec = arg;
  694. struct gic_irq_spec spec = {
  695. .type = GIC_DEVICE,
  696. .hwirq = fwspec->param[1],
  697. };
  698. int i, ret;
  699. bool is_shared = fwspec->param[0] == GIC_SHARED;
  700. if (is_shared) {
  701. ret = irq_domain_alloc_irqs_parent(d, virq, nr_irqs, &spec);
  702. if (ret)
  703. return ret;
  704. }
  705. for (i = 0; i < nr_irqs; i++) {
  706. irq_hw_number_t hwirq;
  707. if (is_shared)
  708. hwirq = GIC_SHARED_TO_HWIRQ(spec.hwirq + i);
  709. else
  710. hwirq = GIC_LOCAL_TO_HWIRQ(spec.hwirq + i);
  711. ret = irq_domain_set_hwirq_and_chip(d, virq + i,
  712. hwirq,
  713. &gic_level_irq_controller,
  714. NULL);
  715. if (ret)
  716. return ret;
  717. }
  718. return 0;
  719. }
  720. void gic_dev_domain_free(struct irq_domain *d, unsigned int virq,
  721. unsigned int nr_irqs)
  722. {
  723. /* no real allocation is done for dev irqs, so no need to free anything */
  724. return;
  725. }
  726. static struct irq_domain_ops gic_dev_domain_ops = {
  727. .xlate = gic_dev_domain_xlate,
  728. .alloc = gic_dev_domain_alloc,
  729. .free = gic_dev_domain_free,
  730. };
  731. static int gic_ipi_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
  732. const u32 *intspec, unsigned int intsize,
  733. irq_hw_number_t *out_hwirq,
  734. unsigned int *out_type)
  735. {
  736. /*
  737. * There's nothing to translate here. hwirq is dynamically allocated and
  738. * the irq type is always edge triggered.
  739. * */
  740. *out_hwirq = 0;
  741. *out_type = IRQ_TYPE_EDGE_RISING;
  742. return 0;
  743. }
  744. static int gic_ipi_domain_alloc(struct irq_domain *d, unsigned int virq,
  745. unsigned int nr_irqs, void *arg)
  746. {
  747. struct cpumask *ipimask = arg;
  748. struct gic_irq_spec spec = {
  749. .type = GIC_IPI,
  750. .ipimask = ipimask
  751. };
  752. int ret, i;
  753. ret = irq_domain_alloc_irqs_parent(d, virq, nr_irqs, &spec);
  754. if (ret)
  755. return ret;
  756. /* the parent should have set spec.hwirq to the base_hwirq it allocated */
  757. for (i = 0; i < nr_irqs; i++) {
  758. ret = irq_domain_set_hwirq_and_chip(d, virq + i,
  759. GIC_SHARED_TO_HWIRQ(spec.hwirq + i),
  760. &gic_edge_irq_controller,
  761. NULL);
  762. if (ret)
  763. goto error;
  764. ret = irq_set_irq_type(virq + i, IRQ_TYPE_EDGE_RISING);
  765. if (ret)
  766. goto error;
  767. }
  768. return 0;
  769. error:
  770. irq_domain_free_irqs_parent(d, virq, nr_irqs);
  771. return ret;
  772. }
  773. void gic_ipi_domain_free(struct irq_domain *d, unsigned int virq,
  774. unsigned int nr_irqs)
  775. {
  776. irq_domain_free_irqs_parent(d, virq, nr_irqs);
  777. }
  778. int gic_ipi_domain_match(struct irq_domain *d, struct device_node *node,
  779. enum irq_domain_bus_token bus_token)
  780. {
  781. bool is_ipi;
  782. switch (bus_token) {
  783. case DOMAIN_BUS_IPI:
  784. is_ipi = d->bus_token == bus_token;
  785. return to_of_node(d->fwnode) == node && is_ipi;
  786. break;
  787. default:
  788. return 0;
  789. }
  790. }
  791. static struct irq_domain_ops gic_ipi_domain_ops = {
  792. .xlate = gic_ipi_domain_xlate,
  793. .alloc = gic_ipi_domain_alloc,
  794. .free = gic_ipi_domain_free,
  795. .match = gic_ipi_domain_match,
  796. };
  797. static void __init __gic_init(unsigned long gic_base_addr,
  798. unsigned long gic_addrspace_size,
  799. unsigned int cpu_vec, unsigned int irqbase,
  800. struct device_node *node)
  801. {
  802. unsigned int gicconfig;
  803. unsigned int v[2];
  804. __gic_base_addr = gic_base_addr;
  805. gic_base = ioremap_nocache(gic_base_addr, gic_addrspace_size);
  806. gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
  807. gic_shared_intrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >>
  808. GIC_SH_CONFIG_NUMINTRS_SHF;
  809. gic_shared_intrs = ((gic_shared_intrs + 1) * 8);
  810. gic_vpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >>
  811. GIC_SH_CONFIG_NUMVPES_SHF;
  812. gic_vpes = gic_vpes + 1;
  813. if (cpu_has_veic) {
  814. /* Always use vector 1 in EIC mode */
  815. gic_cpu_pin = 0;
  816. timer_cpu_pin = gic_cpu_pin;
  817. set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET,
  818. __gic_irq_dispatch);
  819. } else {
  820. gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET;
  821. irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec,
  822. gic_irq_dispatch);
  823. /*
  824. * With the CMP implementation of SMP (deprecated), other CPUs
  825. * are started by the bootloader and put into a timer based
  826. * waiting poll loop. We must not re-route those CPU's local
  827. * timer interrupts as the wait instruction will never finish,
  828. * so just handle whatever CPU interrupt it is routed to by
  829. * default.
  830. *
  831. * This workaround should be removed when CMP support is
  832. * dropped.
  833. */
  834. if (IS_ENABLED(CONFIG_MIPS_CMP) &&
  835. gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER)) {
  836. timer_cpu_pin = gic_read32(GIC_REG(VPE_LOCAL,
  837. GIC_VPE_TIMER_MAP)) &
  838. GIC_MAP_MSK;
  839. irq_set_chained_handler(MIPS_CPU_IRQ_BASE +
  840. GIC_CPU_PIN_OFFSET +
  841. timer_cpu_pin,
  842. gic_irq_dispatch);
  843. } else {
  844. timer_cpu_pin = gic_cpu_pin;
  845. }
  846. }
  847. gic_irq_domain = irq_domain_add_simple(node, GIC_NUM_LOCAL_INTRS +
  848. gic_shared_intrs, irqbase,
  849. &gic_irq_domain_ops, NULL);
  850. if (!gic_irq_domain)
  851. panic("Failed to add GIC IRQ domain");
  852. gic_dev_domain = irq_domain_add_hierarchy(gic_irq_domain, 0,
  853. GIC_NUM_LOCAL_INTRS + gic_shared_intrs,
  854. node, &gic_dev_domain_ops, NULL);
  855. if (!gic_dev_domain)
  856. panic("Failed to add GIC DEV domain");
  857. gic_ipi_domain = irq_domain_add_hierarchy(gic_irq_domain,
  858. IRQ_DOMAIN_FLAG_IPI_PER_CPU,
  859. GIC_NUM_LOCAL_INTRS + gic_shared_intrs,
  860. node, &gic_ipi_domain_ops, NULL);
  861. if (!gic_ipi_domain)
  862. panic("Failed to add GIC IPI domain");
  863. gic_ipi_domain->bus_token = DOMAIN_BUS_IPI;
  864. if (node &&
  865. !of_property_read_u32_array(node, "mti,reserved-ipi-vectors", v, 2)) {
  866. bitmap_set(ipi_resrv, v[0], v[1]);
  867. } else {
  868. /* Make the last 2 * gic_vpes available for IPIs */
  869. bitmap_set(ipi_resrv,
  870. gic_shared_intrs - 2 * gic_vpes,
  871. 2 * gic_vpes);
  872. }
  873. gic_basic_init();
  874. }
  875. void __init gic_init(unsigned long gic_base_addr,
  876. unsigned long gic_addrspace_size,
  877. unsigned int cpu_vec, unsigned int irqbase)
  878. {
  879. __gic_init(gic_base_addr, gic_addrspace_size, cpu_vec, irqbase, NULL);
  880. }
  881. static int __init gic_of_init(struct device_node *node,
  882. struct device_node *parent)
  883. {
  884. struct resource res;
  885. unsigned int cpu_vec, i = 0, reserved = 0;
  886. phys_addr_t gic_base;
  887. size_t gic_len;
  888. /* Find the first available CPU vector. */
  889. while (!of_property_read_u32_index(node, "mti,reserved-cpu-vectors",
  890. i++, &cpu_vec))
  891. reserved |= BIT(cpu_vec);
  892. for (cpu_vec = 2; cpu_vec < 8; cpu_vec++) {
  893. if (!(reserved & BIT(cpu_vec)))
  894. break;
  895. }
  896. if (cpu_vec == 8) {
  897. pr_err("No CPU vectors available for GIC\n");
  898. return -ENODEV;
  899. }
  900. if (of_address_to_resource(node, 0, &res)) {
  901. /*
  902. * Probe the CM for the GIC base address if not specified
  903. * in the device-tree.
  904. */
  905. if (mips_cm_present()) {
  906. gic_base = read_gcr_gic_base() &
  907. ~CM_GCR_GIC_BASE_GICEN_MSK;
  908. gic_len = 0x20000;
  909. } else {
  910. pr_err("Failed to get GIC memory range\n");
  911. return -ENODEV;
  912. }
  913. } else {
  914. gic_base = res.start;
  915. gic_len = resource_size(&res);
  916. }
  917. if (mips_cm_present())
  918. write_gcr_gic_base(gic_base | CM_GCR_GIC_BASE_GICEN_MSK);
  919. gic_present = true;
  920. __gic_init(gic_base, gic_len, cpu_vec, 0, node);
  921. return 0;
  922. }
  923. IRQCHIP_DECLARE(mips_gic, "mti,gic", gic_of_init);