irq-mips-gic.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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), mips_cm_vp_id(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. unsigned gic_read_local_vp_id(void)
  203. {
  204. unsigned long ident;
  205. ident = gic_read(GIC_REG(VPE_LOCAL, GIC_VP_IDENT));
  206. return ident & GIC_VP_IDENT_VCNUM_MSK;
  207. }
  208. static bool gic_local_irq_is_routable(int intr)
  209. {
  210. u32 vpe_ctl;
  211. /* All local interrupts are routable in EIC mode. */
  212. if (cpu_has_veic)
  213. return true;
  214. vpe_ctl = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_CTL));
  215. switch (intr) {
  216. case GIC_LOCAL_INT_TIMER:
  217. return vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK;
  218. case GIC_LOCAL_INT_PERFCTR:
  219. return vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK;
  220. case GIC_LOCAL_INT_FDC:
  221. return vpe_ctl & GIC_VPE_CTL_FDC_RTBL_MSK;
  222. case GIC_LOCAL_INT_SWINT0:
  223. case GIC_LOCAL_INT_SWINT1:
  224. return vpe_ctl & GIC_VPE_CTL_SWINT_RTBL_MSK;
  225. default:
  226. return true;
  227. }
  228. }
  229. static void gic_bind_eic_interrupt(int irq, int set)
  230. {
  231. /* Convert irq vector # to hw int # */
  232. irq -= GIC_PIN_TO_VEC_OFFSET;
  233. /* Set irq to use shadow set */
  234. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_EIC_SHADOW_SET_BASE) +
  235. GIC_VPE_EIC_SS(irq), set);
  236. }
  237. static void gic_send_ipi(struct irq_data *d, unsigned int cpu)
  238. {
  239. irq_hw_number_t hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(d));
  240. gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_SET(hwirq));
  241. }
  242. int gic_get_c0_compare_int(void)
  243. {
  244. if (!gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER))
  245. return MIPS_CPU_IRQ_BASE + cp0_compare_irq;
  246. return irq_create_mapping(gic_irq_domain,
  247. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_TIMER));
  248. }
  249. int gic_get_c0_perfcount_int(void)
  250. {
  251. if (!gic_local_irq_is_routable(GIC_LOCAL_INT_PERFCTR)) {
  252. /* Is the performance counter shared with the timer? */
  253. if (cp0_perfcount_irq < 0)
  254. return -1;
  255. return MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
  256. }
  257. return irq_create_mapping(gic_irq_domain,
  258. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_PERFCTR));
  259. }
  260. int gic_get_c0_fdc_int(void)
  261. {
  262. if (!gic_local_irq_is_routable(GIC_LOCAL_INT_FDC)) {
  263. /* Is the FDC IRQ even present? */
  264. if (cp0_fdc_irq < 0)
  265. return -1;
  266. return MIPS_CPU_IRQ_BASE + cp0_fdc_irq;
  267. }
  268. return irq_create_mapping(gic_irq_domain,
  269. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_FDC));
  270. }
  271. int gic_get_usm_range(struct resource *gic_usm_res)
  272. {
  273. if (!gic_present)
  274. return -1;
  275. gic_usm_res->start = __gic_base_addr + USM_VISIBLE_SECTION_OFS;
  276. gic_usm_res->end = gic_usm_res->start + (USM_VISIBLE_SECTION_SIZE - 1);
  277. return 0;
  278. }
  279. static void gic_handle_shared_int(bool chained)
  280. {
  281. unsigned int i, intr, virq, gic_reg_step = mips_cm_is64 ? 8 : 4;
  282. unsigned long *pcpu_mask;
  283. unsigned long pending_reg, intrmask_reg;
  284. DECLARE_BITMAP(pending, GIC_MAX_INTRS);
  285. DECLARE_BITMAP(intrmask, GIC_MAX_INTRS);
  286. /* Get per-cpu bitmaps */
  287. pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask;
  288. pending_reg = GIC_REG(SHARED, GIC_SH_PEND);
  289. intrmask_reg = GIC_REG(SHARED, GIC_SH_MASK);
  290. for (i = 0; i < BITS_TO_LONGS(gic_shared_intrs); i++) {
  291. pending[i] = gic_read(pending_reg);
  292. intrmask[i] = gic_read(intrmask_reg);
  293. pending_reg += gic_reg_step;
  294. intrmask_reg += gic_reg_step;
  295. if (!IS_ENABLED(CONFIG_64BIT) || mips_cm_is64)
  296. continue;
  297. pending[i] |= (u64)gic_read(pending_reg) << 32;
  298. intrmask[i] |= (u64)gic_read(intrmask_reg) << 32;
  299. pending_reg += gic_reg_step;
  300. intrmask_reg += gic_reg_step;
  301. }
  302. bitmap_and(pending, pending, intrmask, gic_shared_intrs);
  303. bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs);
  304. for_each_set_bit(intr, pending, gic_shared_intrs) {
  305. virq = irq_linear_revmap(gic_irq_domain,
  306. GIC_SHARED_TO_HWIRQ(intr));
  307. if (chained)
  308. generic_handle_irq(virq);
  309. else
  310. do_IRQ(virq);
  311. }
  312. }
  313. static void gic_mask_irq(struct irq_data *d)
  314. {
  315. gic_reset_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
  316. }
  317. static void gic_unmask_irq(struct irq_data *d)
  318. {
  319. gic_set_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
  320. }
  321. static void gic_ack_irq(struct irq_data *d)
  322. {
  323. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  324. gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_CLR(irq));
  325. }
  326. static int gic_set_type(struct irq_data *d, unsigned int type)
  327. {
  328. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  329. unsigned long flags;
  330. bool is_edge;
  331. spin_lock_irqsave(&gic_lock, flags);
  332. switch (type & IRQ_TYPE_SENSE_MASK) {
  333. case IRQ_TYPE_EDGE_FALLING:
  334. gic_set_polarity(irq, GIC_POL_NEG);
  335. gic_set_trigger(irq, GIC_TRIG_EDGE);
  336. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  337. is_edge = true;
  338. break;
  339. case IRQ_TYPE_EDGE_RISING:
  340. gic_set_polarity(irq, GIC_POL_POS);
  341. gic_set_trigger(irq, GIC_TRIG_EDGE);
  342. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  343. is_edge = true;
  344. break;
  345. case IRQ_TYPE_EDGE_BOTH:
  346. /* polarity is irrelevant in this case */
  347. gic_set_trigger(irq, GIC_TRIG_EDGE);
  348. gic_set_dual_edge(irq, GIC_TRIG_DUAL_ENABLE);
  349. is_edge = true;
  350. break;
  351. case IRQ_TYPE_LEVEL_LOW:
  352. gic_set_polarity(irq, GIC_POL_NEG);
  353. gic_set_trigger(irq, GIC_TRIG_LEVEL);
  354. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  355. is_edge = false;
  356. break;
  357. case IRQ_TYPE_LEVEL_HIGH:
  358. default:
  359. gic_set_polarity(irq, GIC_POL_POS);
  360. gic_set_trigger(irq, GIC_TRIG_LEVEL);
  361. gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
  362. is_edge = false;
  363. break;
  364. }
  365. if (is_edge)
  366. irq_set_chip_handler_name_locked(d, &gic_edge_irq_controller,
  367. handle_edge_irq, NULL);
  368. else
  369. irq_set_chip_handler_name_locked(d, &gic_level_irq_controller,
  370. handle_level_irq, NULL);
  371. spin_unlock_irqrestore(&gic_lock, flags);
  372. return 0;
  373. }
  374. #ifdef CONFIG_SMP
  375. static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
  376. bool force)
  377. {
  378. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  379. cpumask_t tmp = CPU_MASK_NONE;
  380. unsigned long flags;
  381. int i;
  382. cpumask_and(&tmp, cpumask, cpu_online_mask);
  383. if (cpumask_empty(&tmp))
  384. return -EINVAL;
  385. /* Assumption : cpumask refers to a single CPU */
  386. spin_lock_irqsave(&gic_lock, flags);
  387. /* Re-route this IRQ */
  388. gic_map_to_vpe(irq, mips_cm_vp_id(cpumask_first(&tmp)));
  389. /* Update the pcpu_masks */
  390. for (i = 0; i < min(gic_vpes, NR_CPUS); i++)
  391. clear_bit(irq, pcpu_masks[i].pcpu_mask);
  392. set_bit(irq, pcpu_masks[cpumask_first(&tmp)].pcpu_mask);
  393. cpumask_copy(irq_data_get_affinity_mask(d), cpumask);
  394. spin_unlock_irqrestore(&gic_lock, flags);
  395. return IRQ_SET_MASK_OK_NOCOPY;
  396. }
  397. #endif
  398. static struct irq_chip gic_level_irq_controller = {
  399. .name = "MIPS GIC",
  400. .irq_mask = gic_mask_irq,
  401. .irq_unmask = gic_unmask_irq,
  402. .irq_set_type = gic_set_type,
  403. #ifdef CONFIG_SMP
  404. .irq_set_affinity = gic_set_affinity,
  405. #endif
  406. };
  407. static struct irq_chip gic_edge_irq_controller = {
  408. .name = "MIPS GIC",
  409. .irq_ack = gic_ack_irq,
  410. .irq_mask = gic_mask_irq,
  411. .irq_unmask = gic_unmask_irq,
  412. .irq_set_type = gic_set_type,
  413. #ifdef CONFIG_SMP
  414. .irq_set_affinity = gic_set_affinity,
  415. #endif
  416. .ipi_send_single = gic_send_ipi,
  417. };
  418. static void gic_handle_local_int(bool chained)
  419. {
  420. unsigned long pending, masked;
  421. unsigned int intr, virq;
  422. pending = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_PEND));
  423. masked = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_MASK));
  424. bitmap_and(&pending, &pending, &masked, GIC_NUM_LOCAL_INTRS);
  425. for_each_set_bit(intr, &pending, GIC_NUM_LOCAL_INTRS) {
  426. virq = irq_linear_revmap(gic_irq_domain,
  427. GIC_LOCAL_TO_HWIRQ(intr));
  428. if (chained)
  429. generic_handle_irq(virq);
  430. else
  431. do_IRQ(virq);
  432. }
  433. }
  434. static void gic_mask_local_irq(struct irq_data *d)
  435. {
  436. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  437. gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_RMASK), 1 << intr);
  438. }
  439. static void gic_unmask_local_irq(struct irq_data *d)
  440. {
  441. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  442. gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_SMASK), 1 << intr);
  443. }
  444. static struct irq_chip gic_local_irq_controller = {
  445. .name = "MIPS GIC Local",
  446. .irq_mask = gic_mask_local_irq,
  447. .irq_unmask = gic_unmask_local_irq,
  448. };
  449. static void gic_mask_local_irq_all_vpes(struct irq_data *d)
  450. {
  451. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  452. int i;
  453. unsigned long flags;
  454. spin_lock_irqsave(&gic_lock, flags);
  455. for (i = 0; i < gic_vpes; i++) {
  456. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
  457. mips_cm_vp_id(i));
  458. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << intr);
  459. }
  460. spin_unlock_irqrestore(&gic_lock, flags);
  461. }
  462. static void gic_unmask_local_irq_all_vpes(struct irq_data *d)
  463. {
  464. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  465. int i;
  466. unsigned long flags;
  467. spin_lock_irqsave(&gic_lock, flags);
  468. for (i = 0; i < gic_vpes; i++) {
  469. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
  470. mips_cm_vp_id(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),
  503. mips_cm_vp_id(i));
  504. for (j = 0; j < GIC_NUM_LOCAL_INTRS; j++) {
  505. if (!gic_local_irq_is_routable(j))
  506. continue;
  507. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << j);
  508. }
  509. }
  510. }
  511. static int gic_local_irq_domain_map(struct irq_domain *d, unsigned int virq,
  512. irq_hw_number_t hw)
  513. {
  514. int intr = GIC_HWIRQ_TO_LOCAL(hw);
  515. int ret = 0;
  516. int i;
  517. unsigned long flags;
  518. if (!gic_local_irq_is_routable(intr))
  519. return -EPERM;
  520. spin_lock_irqsave(&gic_lock, flags);
  521. for (i = 0; i < gic_vpes; i++) {
  522. u32 val = GIC_MAP_TO_PIN_MSK | gic_cpu_pin;
  523. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
  524. mips_cm_vp_id(i));
  525. switch (intr) {
  526. case GIC_LOCAL_INT_WD:
  527. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_WD_MAP), val);
  528. break;
  529. case GIC_LOCAL_INT_COMPARE:
  530. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_MAP),
  531. val);
  532. break;
  533. case GIC_LOCAL_INT_TIMER:
  534. /* CONFIG_MIPS_CMP workaround (see __gic_init) */
  535. val = GIC_MAP_TO_PIN_MSK | timer_cpu_pin;
  536. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_TIMER_MAP),
  537. val);
  538. break;
  539. case GIC_LOCAL_INT_PERFCTR:
  540. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_PERFCTR_MAP),
  541. val);
  542. break;
  543. case GIC_LOCAL_INT_SWINT0:
  544. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SWINT0_MAP),
  545. val);
  546. break;
  547. case GIC_LOCAL_INT_SWINT1:
  548. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SWINT1_MAP),
  549. val);
  550. break;
  551. case GIC_LOCAL_INT_FDC:
  552. gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_FDC_MAP), val);
  553. break;
  554. default:
  555. pr_err("Invalid local IRQ %d\n", intr);
  556. ret = -EINVAL;
  557. break;
  558. }
  559. }
  560. spin_unlock_irqrestore(&gic_lock, flags);
  561. return ret;
  562. }
  563. static int gic_shared_irq_domain_map(struct irq_domain *d, unsigned int virq,
  564. irq_hw_number_t hw, unsigned int vpe)
  565. {
  566. int intr = GIC_HWIRQ_TO_SHARED(hw);
  567. unsigned long flags;
  568. int i;
  569. spin_lock_irqsave(&gic_lock, flags);
  570. gic_map_to_pin(intr, gic_cpu_pin);
  571. gic_map_to_vpe(intr, mips_cm_vp_id(vpe));
  572. for (i = 0; i < min(gic_vpes, NR_CPUS); i++)
  573. clear_bit(intr, pcpu_masks[i].pcpu_mask);
  574. set_bit(intr, pcpu_masks[vpe].pcpu_mask);
  575. spin_unlock_irqrestore(&gic_lock, flags);
  576. return 0;
  577. }
  578. static int gic_setup_dev_chip(struct irq_domain *d, unsigned int virq,
  579. unsigned int hwirq)
  580. {
  581. struct irq_chip *chip;
  582. int err;
  583. if (hwirq >= GIC_SHARED_HWIRQ_BASE) {
  584. err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
  585. &gic_level_irq_controller,
  586. NULL);
  587. } else {
  588. switch (GIC_HWIRQ_TO_LOCAL(hwirq)) {
  589. case GIC_LOCAL_INT_TIMER:
  590. case GIC_LOCAL_INT_PERFCTR:
  591. case GIC_LOCAL_INT_FDC:
  592. /*
  593. * HACK: These are all really percpu interrupts, but
  594. * the rest of the MIPS kernel code does not use the
  595. * percpu IRQ API for them.
  596. */
  597. chip = &gic_all_vpes_local_irq_controller;
  598. irq_set_handler(virq, handle_percpu_irq);
  599. break;
  600. default:
  601. chip = &gic_local_irq_controller;
  602. irq_set_handler(virq, handle_percpu_devid_irq);
  603. irq_set_percpu_devid(virq);
  604. break;
  605. }
  606. err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
  607. chip, NULL);
  608. }
  609. return err;
  610. }
  611. static int gic_irq_domain_alloc(struct irq_domain *d, unsigned int virq,
  612. unsigned int nr_irqs, void *arg)
  613. {
  614. struct gic_irq_spec *spec = arg;
  615. irq_hw_number_t hwirq, base_hwirq;
  616. int cpu, ret, i;
  617. if (spec->type == GIC_DEVICE) {
  618. /* verify that shared irqs don't conflict with an IPI irq */
  619. if ((spec->hwirq >= GIC_SHARED_HWIRQ_BASE) &&
  620. test_bit(GIC_HWIRQ_TO_SHARED(spec->hwirq), ipi_resrv))
  621. return -EBUSY;
  622. return gic_setup_dev_chip(d, virq, spec->hwirq);
  623. } else {
  624. base_hwirq = find_first_bit(ipi_resrv, gic_shared_intrs);
  625. if (base_hwirq == gic_shared_intrs) {
  626. return -ENOMEM;
  627. }
  628. /* check that we have enough space */
  629. for (i = base_hwirq; i < nr_irqs; i++) {
  630. if (!test_bit(i, ipi_resrv))
  631. return -EBUSY;
  632. }
  633. bitmap_clear(ipi_resrv, base_hwirq, nr_irqs);
  634. /* map the hwirq for each cpu consecutively */
  635. i = 0;
  636. for_each_cpu(cpu, spec->ipimask) {
  637. hwirq = GIC_SHARED_TO_HWIRQ(base_hwirq + i);
  638. ret = irq_domain_set_hwirq_and_chip(d, virq + i, hwirq,
  639. &gic_level_irq_controller,
  640. NULL);
  641. if (ret)
  642. goto error;
  643. irq_set_handler(virq + i, handle_level_irq);
  644. ret = gic_shared_irq_domain_map(d, virq + i, hwirq, cpu);
  645. if (ret)
  646. goto error;
  647. i++;
  648. }
  649. /*
  650. * tell the parent about the base hwirq we allocated so it can
  651. * set its own domain data
  652. */
  653. spec->hwirq = base_hwirq;
  654. }
  655. return 0;
  656. error:
  657. bitmap_set(ipi_resrv, base_hwirq, nr_irqs);
  658. return ret;
  659. }
  660. void gic_irq_domain_free(struct irq_domain *d, unsigned int virq,
  661. unsigned int nr_irqs)
  662. {
  663. irq_hw_number_t base_hwirq;
  664. struct irq_data *data;
  665. data = irq_get_irq_data(virq);
  666. if (!data)
  667. return;
  668. base_hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(data));
  669. bitmap_set(ipi_resrv, base_hwirq, nr_irqs);
  670. }
  671. int gic_irq_domain_match(struct irq_domain *d, struct device_node *node,
  672. enum irq_domain_bus_token bus_token)
  673. {
  674. /* this domain should'nt be accessed directly */
  675. return 0;
  676. }
  677. static const struct irq_domain_ops gic_irq_domain_ops = {
  678. .alloc = gic_irq_domain_alloc,
  679. .free = gic_irq_domain_free,
  680. .match = gic_irq_domain_match,
  681. };
  682. static int gic_dev_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
  683. const u32 *intspec, unsigned int intsize,
  684. irq_hw_number_t *out_hwirq,
  685. unsigned int *out_type)
  686. {
  687. if (intsize != 3)
  688. return -EINVAL;
  689. if (intspec[0] == GIC_SHARED)
  690. *out_hwirq = GIC_SHARED_TO_HWIRQ(intspec[1]);
  691. else if (intspec[0] == GIC_LOCAL)
  692. *out_hwirq = GIC_LOCAL_TO_HWIRQ(intspec[1]);
  693. else
  694. return -EINVAL;
  695. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  696. return 0;
  697. }
  698. static int gic_dev_domain_alloc(struct irq_domain *d, unsigned int virq,
  699. unsigned int nr_irqs, void *arg)
  700. {
  701. struct irq_fwspec *fwspec = arg;
  702. struct gic_irq_spec spec = {
  703. .type = GIC_DEVICE,
  704. };
  705. int i, ret;
  706. if (fwspec->param[0] == GIC_SHARED)
  707. spec.hwirq = GIC_SHARED_TO_HWIRQ(fwspec->param[1]);
  708. else
  709. spec.hwirq = GIC_LOCAL_TO_HWIRQ(fwspec->param[1]);
  710. ret = irq_domain_alloc_irqs_parent(d, virq, nr_irqs, &spec);
  711. if (ret)
  712. return ret;
  713. for (i = 0; i < nr_irqs; i++) {
  714. ret = gic_setup_dev_chip(d, virq + i, spec.hwirq + i);
  715. if (ret)
  716. goto error;
  717. }
  718. return 0;
  719. error:
  720. irq_domain_free_irqs_parent(d, virq, nr_irqs);
  721. return ret;
  722. }
  723. void gic_dev_domain_free(struct irq_domain *d, unsigned int virq,
  724. unsigned int nr_irqs)
  725. {
  726. /* no real allocation is done for dev irqs, so no need to free anything */
  727. return;
  728. }
  729. static void gic_dev_domain_activate(struct irq_domain *domain,
  730. struct irq_data *d)
  731. {
  732. if (GIC_HWIRQ_TO_LOCAL(d->hwirq) < GIC_NUM_LOCAL_INTRS)
  733. gic_local_irq_domain_map(domain, d->irq, d->hwirq);
  734. else
  735. gic_shared_irq_domain_map(domain, d->irq, d->hwirq, 0);
  736. }
  737. static struct irq_domain_ops gic_dev_domain_ops = {
  738. .xlate = gic_dev_domain_xlate,
  739. .alloc = gic_dev_domain_alloc,
  740. .free = gic_dev_domain_free,
  741. .activate = gic_dev_domain_activate,
  742. };
  743. static int gic_ipi_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
  744. const u32 *intspec, unsigned int intsize,
  745. irq_hw_number_t *out_hwirq,
  746. unsigned int *out_type)
  747. {
  748. /*
  749. * There's nothing to translate here. hwirq is dynamically allocated and
  750. * the irq type is always edge triggered.
  751. * */
  752. *out_hwirq = 0;
  753. *out_type = IRQ_TYPE_EDGE_RISING;
  754. return 0;
  755. }
  756. static int gic_ipi_domain_alloc(struct irq_domain *d, unsigned int virq,
  757. unsigned int nr_irqs, void *arg)
  758. {
  759. struct cpumask *ipimask = arg;
  760. struct gic_irq_spec spec = {
  761. .type = GIC_IPI,
  762. .ipimask = ipimask
  763. };
  764. int ret, i;
  765. ret = irq_domain_alloc_irqs_parent(d, virq, nr_irqs, &spec);
  766. if (ret)
  767. return ret;
  768. /* the parent should have set spec.hwirq to the base_hwirq it allocated */
  769. for (i = 0; i < nr_irqs; i++) {
  770. ret = irq_domain_set_hwirq_and_chip(d, virq + i,
  771. GIC_SHARED_TO_HWIRQ(spec.hwirq + i),
  772. &gic_edge_irq_controller,
  773. NULL);
  774. if (ret)
  775. goto error;
  776. ret = irq_set_irq_type(virq + i, IRQ_TYPE_EDGE_RISING);
  777. if (ret)
  778. goto error;
  779. }
  780. return 0;
  781. error:
  782. irq_domain_free_irqs_parent(d, virq, nr_irqs);
  783. return ret;
  784. }
  785. void gic_ipi_domain_free(struct irq_domain *d, unsigned int virq,
  786. unsigned int nr_irqs)
  787. {
  788. irq_domain_free_irqs_parent(d, virq, nr_irqs);
  789. }
  790. int gic_ipi_domain_match(struct irq_domain *d, struct device_node *node,
  791. enum irq_domain_bus_token bus_token)
  792. {
  793. bool is_ipi;
  794. switch (bus_token) {
  795. case DOMAIN_BUS_IPI:
  796. is_ipi = d->bus_token == bus_token;
  797. return (!node || to_of_node(d->fwnode) == node) && is_ipi;
  798. break;
  799. default:
  800. return 0;
  801. }
  802. }
  803. static struct irq_domain_ops gic_ipi_domain_ops = {
  804. .xlate = gic_ipi_domain_xlate,
  805. .alloc = gic_ipi_domain_alloc,
  806. .free = gic_ipi_domain_free,
  807. .match = gic_ipi_domain_match,
  808. };
  809. static void __init __gic_init(unsigned long gic_base_addr,
  810. unsigned long gic_addrspace_size,
  811. unsigned int cpu_vec, unsigned int irqbase,
  812. struct device_node *node)
  813. {
  814. unsigned int gicconfig, cpu;
  815. unsigned int v[2];
  816. __gic_base_addr = gic_base_addr;
  817. gic_base = ioremap_nocache(gic_base_addr, gic_addrspace_size);
  818. gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
  819. gic_shared_intrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >>
  820. GIC_SH_CONFIG_NUMINTRS_SHF;
  821. gic_shared_intrs = ((gic_shared_intrs + 1) * 8);
  822. gic_vpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >>
  823. GIC_SH_CONFIG_NUMVPES_SHF;
  824. gic_vpes = gic_vpes + 1;
  825. if (cpu_has_veic) {
  826. /* Set EIC mode for all VPEs */
  827. for_each_present_cpu(cpu) {
  828. gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
  829. mips_cm_vp_id(cpu));
  830. gic_write(GIC_REG(VPE_OTHER, GIC_VPE_CTL),
  831. GIC_VPE_CTL_EIC_MODE_MSK);
  832. }
  833. /* Always use vector 1 in EIC mode */
  834. gic_cpu_pin = 0;
  835. timer_cpu_pin = gic_cpu_pin;
  836. set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET,
  837. __gic_irq_dispatch);
  838. } else {
  839. gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET;
  840. irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec,
  841. gic_irq_dispatch);
  842. /*
  843. * With the CMP implementation of SMP (deprecated), other CPUs
  844. * are started by the bootloader and put into a timer based
  845. * waiting poll loop. We must not re-route those CPU's local
  846. * timer interrupts as the wait instruction will never finish,
  847. * so just handle whatever CPU interrupt it is routed to by
  848. * default.
  849. *
  850. * This workaround should be removed when CMP support is
  851. * dropped.
  852. */
  853. if (IS_ENABLED(CONFIG_MIPS_CMP) &&
  854. gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER)) {
  855. timer_cpu_pin = gic_read32(GIC_REG(VPE_LOCAL,
  856. GIC_VPE_TIMER_MAP)) &
  857. GIC_MAP_MSK;
  858. irq_set_chained_handler(MIPS_CPU_IRQ_BASE +
  859. GIC_CPU_PIN_OFFSET +
  860. timer_cpu_pin,
  861. gic_irq_dispatch);
  862. } else {
  863. timer_cpu_pin = gic_cpu_pin;
  864. }
  865. }
  866. gic_irq_domain = irq_domain_add_simple(node, GIC_NUM_LOCAL_INTRS +
  867. gic_shared_intrs, irqbase,
  868. &gic_irq_domain_ops, NULL);
  869. if (!gic_irq_domain)
  870. panic("Failed to add GIC IRQ domain");
  871. gic_irq_domain->name = "mips-gic-irq";
  872. gic_dev_domain = irq_domain_add_hierarchy(gic_irq_domain, 0,
  873. GIC_NUM_LOCAL_INTRS + gic_shared_intrs,
  874. node, &gic_dev_domain_ops, NULL);
  875. if (!gic_dev_domain)
  876. panic("Failed to add GIC DEV domain");
  877. gic_dev_domain->name = "mips-gic-dev";
  878. gic_ipi_domain = irq_domain_add_hierarchy(gic_irq_domain,
  879. IRQ_DOMAIN_FLAG_IPI_PER_CPU,
  880. GIC_NUM_LOCAL_INTRS + gic_shared_intrs,
  881. node, &gic_ipi_domain_ops, NULL);
  882. if (!gic_ipi_domain)
  883. panic("Failed to add GIC IPI domain");
  884. gic_ipi_domain->name = "mips-gic-ipi";
  885. gic_ipi_domain->bus_token = DOMAIN_BUS_IPI;
  886. if (node &&
  887. !of_property_read_u32_array(node, "mti,reserved-ipi-vectors", v, 2)) {
  888. bitmap_set(ipi_resrv, v[0], v[1]);
  889. } else {
  890. /* Make the last 2 * gic_vpes available for IPIs */
  891. bitmap_set(ipi_resrv,
  892. gic_shared_intrs - 2 * gic_vpes,
  893. 2 * gic_vpes);
  894. }
  895. gic_basic_init();
  896. }
  897. void __init gic_init(unsigned long gic_base_addr,
  898. unsigned long gic_addrspace_size,
  899. unsigned int cpu_vec, unsigned int irqbase)
  900. {
  901. __gic_init(gic_base_addr, gic_addrspace_size, cpu_vec, irqbase, NULL);
  902. }
  903. static int __init gic_of_init(struct device_node *node,
  904. struct device_node *parent)
  905. {
  906. struct resource res;
  907. unsigned int cpu_vec, i = 0, reserved = 0;
  908. phys_addr_t gic_base;
  909. size_t gic_len;
  910. /* Find the first available CPU vector. */
  911. while (!of_property_read_u32_index(node, "mti,reserved-cpu-vectors",
  912. i++, &cpu_vec))
  913. reserved |= BIT(cpu_vec);
  914. for (cpu_vec = 2; cpu_vec < 8; cpu_vec++) {
  915. if (!(reserved & BIT(cpu_vec)))
  916. break;
  917. }
  918. if (cpu_vec == 8) {
  919. pr_err("No CPU vectors available for GIC\n");
  920. return -ENODEV;
  921. }
  922. if (of_address_to_resource(node, 0, &res)) {
  923. /*
  924. * Probe the CM for the GIC base address if not specified
  925. * in the device-tree.
  926. */
  927. if (mips_cm_present()) {
  928. gic_base = read_gcr_gic_base() &
  929. ~CM_GCR_GIC_BASE_GICEN_MSK;
  930. gic_len = 0x20000;
  931. } else {
  932. pr_err("Failed to get GIC memory range\n");
  933. return -ENODEV;
  934. }
  935. } else {
  936. gic_base = res.start;
  937. gic_len = resource_size(&res);
  938. }
  939. if (mips_cm_present()) {
  940. write_gcr_gic_base(gic_base | CM_GCR_GIC_BASE_GICEN_MSK);
  941. /* Ensure GIC region is enabled before trying to access it */
  942. __sync();
  943. }
  944. gic_present = true;
  945. __gic_init(gic_base, gic_len, cpu_vec, 0, node);
  946. return 0;
  947. }
  948. IRQCHIP_DECLARE(mips_gic, "mti,gic", gic_of_init);