irq-mips-gic.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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 ([email protected])
  7. * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
  8. */
  9. #define pr_fmt(fmt) "irq-mips-gic: " fmt
  10. #include <linux/bitfield.h>
  11. #include <linux/bitmap.h>
  12. #include <linux/clocksource.h>
  13. #include <linux/cpuhotplug.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/irqchip.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/of_address.h>
  20. #include <linux/percpu.h>
  21. #include <linux/sched.h>
  22. #include <linux/smp.h>
  23. #include <asm/mips-cps.h>
  24. #include <asm/setup.h>
  25. #include <asm/traps.h>
  26. #include <dt-bindings/interrupt-controller/mips-gic.h>
  27. #define GIC_MAX_INTRS 256
  28. #define GIC_MAX_LONGS BITS_TO_LONGS(GIC_MAX_INTRS)
  29. /* Add 2 to convert GIC CPU pin to core interrupt */
  30. #define GIC_CPU_PIN_OFFSET 2
  31. /* Mapped interrupt to pin X, then GIC will generate the vector (X+1). */
  32. #define GIC_PIN_TO_VEC_OFFSET 1
  33. /* Convert between local/shared IRQ number and GIC HW IRQ number. */
  34. #define GIC_LOCAL_HWIRQ_BASE 0
  35. #define GIC_LOCAL_TO_HWIRQ(x) (GIC_LOCAL_HWIRQ_BASE + (x))
  36. #define GIC_HWIRQ_TO_LOCAL(x) ((x) - GIC_LOCAL_HWIRQ_BASE)
  37. #define GIC_SHARED_HWIRQ_BASE GIC_NUM_LOCAL_INTRS
  38. #define GIC_SHARED_TO_HWIRQ(x) (GIC_SHARED_HWIRQ_BASE + (x))
  39. #define GIC_HWIRQ_TO_SHARED(x) ((x) - GIC_SHARED_HWIRQ_BASE)
  40. void __iomem *mips_gic_base;
  41. static DEFINE_PER_CPU_READ_MOSTLY(unsigned long[GIC_MAX_LONGS], pcpu_masks);
  42. static DEFINE_RAW_SPINLOCK(gic_lock);
  43. static struct irq_domain *gic_irq_domain;
  44. static int gic_shared_intrs;
  45. static unsigned int gic_cpu_pin;
  46. static unsigned int timer_cpu_pin;
  47. static struct irq_chip gic_level_irq_controller, gic_edge_irq_controller;
  48. #ifdef CONFIG_GENERIC_IRQ_IPI
  49. static DECLARE_BITMAP(ipi_resrv, GIC_MAX_INTRS);
  50. static DECLARE_BITMAP(ipi_available, GIC_MAX_INTRS);
  51. #endif /* CONFIG_GENERIC_IRQ_IPI */
  52. static struct gic_all_vpes_chip_data {
  53. u32 map;
  54. bool mask;
  55. } gic_all_vpes_chip_data[GIC_NUM_LOCAL_INTRS];
  56. static void gic_clear_pcpu_masks(unsigned int intr)
  57. {
  58. unsigned int i;
  59. /* Clear the interrupt's bit in all pcpu_masks */
  60. for_each_possible_cpu(i)
  61. clear_bit(intr, per_cpu_ptr(pcpu_masks, i));
  62. }
  63. static bool gic_local_irq_is_routable(int intr)
  64. {
  65. u32 vpe_ctl;
  66. /* All local interrupts are routable in EIC mode. */
  67. if (cpu_has_veic)
  68. return true;
  69. vpe_ctl = read_gic_vl_ctl();
  70. switch (intr) {
  71. case GIC_LOCAL_INT_TIMER:
  72. return vpe_ctl & GIC_VX_CTL_TIMER_ROUTABLE;
  73. case GIC_LOCAL_INT_PERFCTR:
  74. return vpe_ctl & GIC_VX_CTL_PERFCNT_ROUTABLE;
  75. case GIC_LOCAL_INT_FDC:
  76. return vpe_ctl & GIC_VX_CTL_FDC_ROUTABLE;
  77. case GIC_LOCAL_INT_SWINT0:
  78. case GIC_LOCAL_INT_SWINT1:
  79. return vpe_ctl & GIC_VX_CTL_SWINT_ROUTABLE;
  80. default:
  81. return true;
  82. }
  83. }
  84. static void gic_bind_eic_interrupt(int irq, int set)
  85. {
  86. /* Convert irq vector # to hw int # */
  87. irq -= GIC_PIN_TO_VEC_OFFSET;
  88. /* Set irq to use shadow set */
  89. write_gic_vl_eic_shadow_set(irq, set);
  90. }
  91. static void gic_send_ipi(struct irq_data *d, unsigned int cpu)
  92. {
  93. irq_hw_number_t hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(d));
  94. write_gic_wedge(GIC_WEDGE_RW | hwirq);
  95. }
  96. int gic_get_c0_compare_int(void)
  97. {
  98. if (!gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER))
  99. return MIPS_CPU_IRQ_BASE + cp0_compare_irq;
  100. return irq_create_mapping(gic_irq_domain,
  101. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_TIMER));
  102. }
  103. int gic_get_c0_perfcount_int(void)
  104. {
  105. if (!gic_local_irq_is_routable(GIC_LOCAL_INT_PERFCTR)) {
  106. /* Is the performance counter shared with the timer? */
  107. if (cp0_perfcount_irq < 0)
  108. return -1;
  109. return MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
  110. }
  111. return irq_create_mapping(gic_irq_domain,
  112. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_PERFCTR));
  113. }
  114. int gic_get_c0_fdc_int(void)
  115. {
  116. if (!gic_local_irq_is_routable(GIC_LOCAL_INT_FDC)) {
  117. /* Is the FDC IRQ even present? */
  118. if (cp0_fdc_irq < 0)
  119. return -1;
  120. return MIPS_CPU_IRQ_BASE + cp0_fdc_irq;
  121. }
  122. return irq_create_mapping(gic_irq_domain,
  123. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_FDC));
  124. }
  125. static void gic_handle_shared_int(bool chained)
  126. {
  127. unsigned int intr;
  128. unsigned long *pcpu_mask;
  129. DECLARE_BITMAP(pending, GIC_MAX_INTRS);
  130. /* Get per-cpu bitmaps */
  131. pcpu_mask = this_cpu_ptr(pcpu_masks);
  132. if (mips_cm_is64)
  133. __ioread64_copy(pending, addr_gic_pend(),
  134. DIV_ROUND_UP(gic_shared_intrs, 64));
  135. else
  136. __ioread32_copy(pending, addr_gic_pend(),
  137. DIV_ROUND_UP(gic_shared_intrs, 32));
  138. bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs);
  139. for_each_set_bit(intr, pending, gic_shared_intrs) {
  140. if (chained)
  141. generic_handle_domain_irq(gic_irq_domain,
  142. GIC_SHARED_TO_HWIRQ(intr));
  143. else
  144. do_domain_IRQ(gic_irq_domain,
  145. GIC_SHARED_TO_HWIRQ(intr));
  146. }
  147. }
  148. static void gic_mask_irq(struct irq_data *d)
  149. {
  150. unsigned int intr = GIC_HWIRQ_TO_SHARED(d->hwirq);
  151. write_gic_rmask(intr);
  152. gic_clear_pcpu_masks(intr);
  153. }
  154. static void gic_unmask_irq(struct irq_data *d)
  155. {
  156. unsigned int intr = GIC_HWIRQ_TO_SHARED(d->hwirq);
  157. unsigned int cpu;
  158. write_gic_smask(intr);
  159. gic_clear_pcpu_masks(intr);
  160. cpu = cpumask_first(irq_data_get_effective_affinity_mask(d));
  161. set_bit(intr, per_cpu_ptr(pcpu_masks, cpu));
  162. }
  163. static void gic_ack_irq(struct irq_data *d)
  164. {
  165. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  166. write_gic_wedge(irq);
  167. }
  168. static int gic_set_type(struct irq_data *d, unsigned int type)
  169. {
  170. unsigned int irq, pol, trig, dual;
  171. unsigned long flags;
  172. irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  173. raw_spin_lock_irqsave(&gic_lock, flags);
  174. switch (type & IRQ_TYPE_SENSE_MASK) {
  175. case IRQ_TYPE_EDGE_FALLING:
  176. pol = GIC_POL_FALLING_EDGE;
  177. trig = GIC_TRIG_EDGE;
  178. dual = GIC_DUAL_SINGLE;
  179. break;
  180. case IRQ_TYPE_EDGE_RISING:
  181. pol = GIC_POL_RISING_EDGE;
  182. trig = GIC_TRIG_EDGE;
  183. dual = GIC_DUAL_SINGLE;
  184. break;
  185. case IRQ_TYPE_EDGE_BOTH:
  186. pol = 0; /* Doesn't matter */
  187. trig = GIC_TRIG_EDGE;
  188. dual = GIC_DUAL_DUAL;
  189. break;
  190. case IRQ_TYPE_LEVEL_LOW:
  191. pol = GIC_POL_ACTIVE_LOW;
  192. trig = GIC_TRIG_LEVEL;
  193. dual = GIC_DUAL_SINGLE;
  194. break;
  195. case IRQ_TYPE_LEVEL_HIGH:
  196. default:
  197. pol = GIC_POL_ACTIVE_HIGH;
  198. trig = GIC_TRIG_LEVEL;
  199. dual = GIC_DUAL_SINGLE;
  200. break;
  201. }
  202. change_gic_pol(irq, pol);
  203. change_gic_trig(irq, trig);
  204. change_gic_dual(irq, dual);
  205. if (trig == GIC_TRIG_EDGE)
  206. irq_set_chip_handler_name_locked(d, &gic_edge_irq_controller,
  207. handle_edge_irq, NULL);
  208. else
  209. irq_set_chip_handler_name_locked(d, &gic_level_irq_controller,
  210. handle_level_irq, NULL);
  211. raw_spin_unlock_irqrestore(&gic_lock, flags);
  212. return 0;
  213. }
  214. #ifdef CONFIG_SMP
  215. static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
  216. bool force)
  217. {
  218. unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
  219. unsigned long flags;
  220. unsigned int cpu;
  221. cpu = cpumask_first_and(cpumask, cpu_online_mask);
  222. if (cpu >= NR_CPUS)
  223. return -EINVAL;
  224. /* Assumption : cpumask refers to a single CPU */
  225. raw_spin_lock_irqsave(&gic_lock, flags);
  226. /* Re-route this IRQ */
  227. write_gic_map_vp(irq, BIT(mips_cm_vp_id(cpu)));
  228. /* Update the pcpu_masks */
  229. gic_clear_pcpu_masks(irq);
  230. if (read_gic_mask(irq))
  231. set_bit(irq, per_cpu_ptr(pcpu_masks, cpu));
  232. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  233. raw_spin_unlock_irqrestore(&gic_lock, flags);
  234. return IRQ_SET_MASK_OK;
  235. }
  236. #endif
  237. static struct irq_chip gic_level_irq_controller = {
  238. .name = "MIPS GIC",
  239. .irq_mask = gic_mask_irq,
  240. .irq_unmask = gic_unmask_irq,
  241. .irq_set_type = gic_set_type,
  242. #ifdef CONFIG_SMP
  243. .irq_set_affinity = gic_set_affinity,
  244. #endif
  245. };
  246. static struct irq_chip gic_edge_irq_controller = {
  247. .name = "MIPS GIC",
  248. .irq_ack = gic_ack_irq,
  249. .irq_mask = gic_mask_irq,
  250. .irq_unmask = gic_unmask_irq,
  251. .irq_set_type = gic_set_type,
  252. #ifdef CONFIG_SMP
  253. .irq_set_affinity = gic_set_affinity,
  254. #endif
  255. .ipi_send_single = gic_send_ipi,
  256. };
  257. static void gic_handle_local_int(bool chained)
  258. {
  259. unsigned long pending, masked;
  260. unsigned int intr;
  261. pending = read_gic_vl_pend();
  262. masked = read_gic_vl_mask();
  263. bitmap_and(&pending, &pending, &masked, GIC_NUM_LOCAL_INTRS);
  264. for_each_set_bit(intr, &pending, GIC_NUM_LOCAL_INTRS) {
  265. if (chained)
  266. generic_handle_domain_irq(gic_irq_domain,
  267. GIC_LOCAL_TO_HWIRQ(intr));
  268. else
  269. do_domain_IRQ(gic_irq_domain,
  270. GIC_LOCAL_TO_HWIRQ(intr));
  271. }
  272. }
  273. static void gic_mask_local_irq(struct irq_data *d)
  274. {
  275. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  276. write_gic_vl_rmask(BIT(intr));
  277. }
  278. static void gic_unmask_local_irq(struct irq_data *d)
  279. {
  280. int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  281. write_gic_vl_smask(BIT(intr));
  282. }
  283. static struct irq_chip gic_local_irq_controller = {
  284. .name = "MIPS GIC Local",
  285. .irq_mask = gic_mask_local_irq,
  286. .irq_unmask = gic_unmask_local_irq,
  287. };
  288. static void gic_mask_local_irq_all_vpes(struct irq_data *d)
  289. {
  290. struct gic_all_vpes_chip_data *cd;
  291. unsigned long flags;
  292. int intr, cpu;
  293. intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  294. cd = irq_data_get_irq_chip_data(d);
  295. cd->mask = false;
  296. raw_spin_lock_irqsave(&gic_lock, flags);
  297. for_each_online_cpu(cpu) {
  298. write_gic_vl_other(mips_cm_vp_id(cpu));
  299. write_gic_vo_rmask(BIT(intr));
  300. }
  301. raw_spin_unlock_irqrestore(&gic_lock, flags);
  302. }
  303. static void gic_unmask_local_irq_all_vpes(struct irq_data *d)
  304. {
  305. struct gic_all_vpes_chip_data *cd;
  306. unsigned long flags;
  307. int intr, cpu;
  308. intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
  309. cd = irq_data_get_irq_chip_data(d);
  310. cd->mask = true;
  311. raw_spin_lock_irqsave(&gic_lock, flags);
  312. for_each_online_cpu(cpu) {
  313. write_gic_vl_other(mips_cm_vp_id(cpu));
  314. write_gic_vo_smask(BIT(intr));
  315. }
  316. raw_spin_unlock_irqrestore(&gic_lock, flags);
  317. }
  318. static void gic_all_vpes_irq_cpu_online(void)
  319. {
  320. static const unsigned int local_intrs[] = {
  321. GIC_LOCAL_INT_TIMER,
  322. GIC_LOCAL_INT_PERFCTR,
  323. GIC_LOCAL_INT_FDC,
  324. };
  325. unsigned long flags;
  326. int i;
  327. raw_spin_lock_irqsave(&gic_lock, flags);
  328. for (i = 0; i < ARRAY_SIZE(local_intrs); i++) {
  329. unsigned int intr = local_intrs[i];
  330. struct gic_all_vpes_chip_data *cd;
  331. if (!gic_local_irq_is_routable(intr))
  332. continue;
  333. cd = &gic_all_vpes_chip_data[intr];
  334. write_gic_vl_map(mips_gic_vx_map_reg(intr), cd->map);
  335. if (cd->mask)
  336. write_gic_vl_smask(BIT(intr));
  337. }
  338. raw_spin_unlock_irqrestore(&gic_lock, flags);
  339. }
  340. static struct irq_chip gic_all_vpes_local_irq_controller = {
  341. .name = "MIPS GIC Local",
  342. .irq_mask = gic_mask_local_irq_all_vpes,
  343. .irq_unmask = gic_unmask_local_irq_all_vpes,
  344. };
  345. static void __gic_irq_dispatch(void)
  346. {
  347. gic_handle_local_int(false);
  348. gic_handle_shared_int(false);
  349. }
  350. static void gic_irq_dispatch(struct irq_desc *desc)
  351. {
  352. gic_handle_local_int(true);
  353. gic_handle_shared_int(true);
  354. }
  355. static int gic_shared_irq_domain_map(struct irq_domain *d, unsigned int virq,
  356. irq_hw_number_t hw, unsigned int cpu)
  357. {
  358. int intr = GIC_HWIRQ_TO_SHARED(hw);
  359. struct irq_data *data;
  360. unsigned long flags;
  361. data = irq_get_irq_data(virq);
  362. raw_spin_lock_irqsave(&gic_lock, flags);
  363. write_gic_map_pin(intr, GIC_MAP_PIN_MAP_TO_PIN | gic_cpu_pin);
  364. write_gic_map_vp(intr, BIT(mips_cm_vp_id(cpu)));
  365. irq_data_update_effective_affinity(data, cpumask_of(cpu));
  366. raw_spin_unlock_irqrestore(&gic_lock, flags);
  367. return 0;
  368. }
  369. static int gic_irq_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
  370. const u32 *intspec, unsigned int intsize,
  371. irq_hw_number_t *out_hwirq,
  372. unsigned int *out_type)
  373. {
  374. if (intsize != 3)
  375. return -EINVAL;
  376. if (intspec[0] == GIC_SHARED)
  377. *out_hwirq = GIC_SHARED_TO_HWIRQ(intspec[1]);
  378. else if (intspec[0] == GIC_LOCAL)
  379. *out_hwirq = GIC_LOCAL_TO_HWIRQ(intspec[1]);
  380. else
  381. return -EINVAL;
  382. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  383. return 0;
  384. }
  385. static int gic_irq_domain_map(struct irq_domain *d, unsigned int virq,
  386. irq_hw_number_t hwirq)
  387. {
  388. struct gic_all_vpes_chip_data *cd;
  389. unsigned long flags;
  390. unsigned int intr;
  391. int err, cpu;
  392. u32 map;
  393. if (hwirq >= GIC_SHARED_HWIRQ_BASE) {
  394. #ifdef CONFIG_GENERIC_IRQ_IPI
  395. /* verify that shared irqs don't conflict with an IPI irq */
  396. if (test_bit(GIC_HWIRQ_TO_SHARED(hwirq), ipi_resrv))
  397. return -EBUSY;
  398. #endif /* CONFIG_GENERIC_IRQ_IPI */
  399. err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
  400. &gic_level_irq_controller,
  401. NULL);
  402. if (err)
  403. return err;
  404. irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq)));
  405. return gic_shared_irq_domain_map(d, virq, hwirq, 0);
  406. }
  407. intr = GIC_HWIRQ_TO_LOCAL(hwirq);
  408. map = GIC_MAP_PIN_MAP_TO_PIN | gic_cpu_pin;
  409. /*
  410. * If adding support for more per-cpu interrupts, keep the the
  411. * array in gic_all_vpes_irq_cpu_online() in sync.
  412. */
  413. switch (intr) {
  414. case GIC_LOCAL_INT_TIMER:
  415. /* CONFIG_MIPS_CMP workaround (see __gic_init) */
  416. map = GIC_MAP_PIN_MAP_TO_PIN | timer_cpu_pin;
  417. fallthrough;
  418. case GIC_LOCAL_INT_PERFCTR:
  419. case GIC_LOCAL_INT_FDC:
  420. /*
  421. * HACK: These are all really percpu interrupts, but
  422. * the rest of the MIPS kernel code does not use the
  423. * percpu IRQ API for them.
  424. */
  425. cd = &gic_all_vpes_chip_data[intr];
  426. cd->map = map;
  427. err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
  428. &gic_all_vpes_local_irq_controller,
  429. cd);
  430. if (err)
  431. return err;
  432. irq_set_handler(virq, handle_percpu_irq);
  433. break;
  434. default:
  435. err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
  436. &gic_local_irq_controller,
  437. NULL);
  438. if (err)
  439. return err;
  440. irq_set_handler(virq, handle_percpu_devid_irq);
  441. irq_set_percpu_devid(virq);
  442. break;
  443. }
  444. if (!gic_local_irq_is_routable(intr))
  445. return -EPERM;
  446. raw_spin_lock_irqsave(&gic_lock, flags);
  447. for_each_online_cpu(cpu) {
  448. write_gic_vl_other(mips_cm_vp_id(cpu));
  449. write_gic_vo_map(mips_gic_vx_map_reg(intr), map);
  450. }
  451. raw_spin_unlock_irqrestore(&gic_lock, flags);
  452. return 0;
  453. }
  454. static int gic_irq_domain_alloc(struct irq_domain *d, unsigned int virq,
  455. unsigned int nr_irqs, void *arg)
  456. {
  457. struct irq_fwspec *fwspec = arg;
  458. irq_hw_number_t hwirq;
  459. if (fwspec->param[0] == GIC_SHARED)
  460. hwirq = GIC_SHARED_TO_HWIRQ(fwspec->param[1]);
  461. else
  462. hwirq = GIC_LOCAL_TO_HWIRQ(fwspec->param[1]);
  463. return gic_irq_domain_map(d, virq, hwirq);
  464. }
  465. void gic_irq_domain_free(struct irq_domain *d, unsigned int virq,
  466. unsigned int nr_irqs)
  467. {
  468. }
  469. static const struct irq_domain_ops gic_irq_domain_ops = {
  470. .xlate = gic_irq_domain_xlate,
  471. .alloc = gic_irq_domain_alloc,
  472. .free = gic_irq_domain_free,
  473. .map = gic_irq_domain_map,
  474. };
  475. #ifdef CONFIG_GENERIC_IRQ_IPI
  476. static int gic_ipi_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
  477. const u32 *intspec, unsigned int intsize,
  478. irq_hw_number_t *out_hwirq,
  479. unsigned int *out_type)
  480. {
  481. /*
  482. * There's nothing to translate here. hwirq is dynamically allocated and
  483. * the irq type is always edge triggered.
  484. * */
  485. *out_hwirq = 0;
  486. *out_type = IRQ_TYPE_EDGE_RISING;
  487. return 0;
  488. }
  489. static int gic_ipi_domain_alloc(struct irq_domain *d, unsigned int virq,
  490. unsigned int nr_irqs, void *arg)
  491. {
  492. struct cpumask *ipimask = arg;
  493. irq_hw_number_t hwirq, base_hwirq;
  494. int cpu, ret, i;
  495. base_hwirq = find_first_bit(ipi_available, gic_shared_intrs);
  496. if (base_hwirq == gic_shared_intrs)
  497. return -ENOMEM;
  498. /* check that we have enough space */
  499. for (i = base_hwirq; i < nr_irqs; i++) {
  500. if (!test_bit(i, ipi_available))
  501. return -EBUSY;
  502. }
  503. bitmap_clear(ipi_available, base_hwirq, nr_irqs);
  504. /* map the hwirq for each cpu consecutively */
  505. i = 0;
  506. for_each_cpu(cpu, ipimask) {
  507. hwirq = GIC_SHARED_TO_HWIRQ(base_hwirq + i);
  508. ret = irq_domain_set_hwirq_and_chip(d, virq + i, hwirq,
  509. &gic_edge_irq_controller,
  510. NULL);
  511. if (ret)
  512. goto error;
  513. ret = irq_domain_set_hwirq_and_chip(d->parent, virq + i, hwirq,
  514. &gic_edge_irq_controller,
  515. NULL);
  516. if (ret)
  517. goto error;
  518. ret = irq_set_irq_type(virq + i, IRQ_TYPE_EDGE_RISING);
  519. if (ret)
  520. goto error;
  521. ret = gic_shared_irq_domain_map(d, virq + i, hwirq, cpu);
  522. if (ret)
  523. goto error;
  524. i++;
  525. }
  526. return 0;
  527. error:
  528. bitmap_set(ipi_available, base_hwirq, nr_irqs);
  529. return ret;
  530. }
  531. static void gic_ipi_domain_free(struct irq_domain *d, unsigned int virq,
  532. unsigned int nr_irqs)
  533. {
  534. irq_hw_number_t base_hwirq;
  535. struct irq_data *data;
  536. data = irq_get_irq_data(virq);
  537. if (!data)
  538. return;
  539. base_hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(data));
  540. bitmap_set(ipi_available, base_hwirq, nr_irqs);
  541. }
  542. static int gic_ipi_domain_match(struct irq_domain *d, struct device_node *node,
  543. enum irq_domain_bus_token bus_token)
  544. {
  545. bool is_ipi;
  546. switch (bus_token) {
  547. case DOMAIN_BUS_IPI:
  548. is_ipi = d->bus_token == bus_token;
  549. return (!node || to_of_node(d->fwnode) == node) && is_ipi;
  550. break;
  551. default:
  552. return 0;
  553. }
  554. }
  555. static const struct irq_domain_ops gic_ipi_domain_ops = {
  556. .xlate = gic_ipi_domain_xlate,
  557. .alloc = gic_ipi_domain_alloc,
  558. .free = gic_ipi_domain_free,
  559. .match = gic_ipi_domain_match,
  560. };
  561. static int gic_register_ipi_domain(struct device_node *node)
  562. {
  563. struct irq_domain *gic_ipi_domain;
  564. unsigned int v[2], num_ipis;
  565. gic_ipi_domain = irq_domain_add_hierarchy(gic_irq_domain,
  566. IRQ_DOMAIN_FLAG_IPI_PER_CPU,
  567. GIC_NUM_LOCAL_INTRS + gic_shared_intrs,
  568. node, &gic_ipi_domain_ops, NULL);
  569. if (!gic_ipi_domain) {
  570. pr_err("Failed to add IPI domain");
  571. return -ENXIO;
  572. }
  573. irq_domain_update_bus_token(gic_ipi_domain, DOMAIN_BUS_IPI);
  574. if (node &&
  575. !of_property_read_u32_array(node, "mti,reserved-ipi-vectors", v, 2)) {
  576. bitmap_set(ipi_resrv, v[0], v[1]);
  577. } else {
  578. /*
  579. * Reserve 2 interrupts per possible CPU/VP for use as IPIs,
  580. * meeting the requirements of arch/mips SMP.
  581. */
  582. num_ipis = 2 * num_possible_cpus();
  583. bitmap_set(ipi_resrv, gic_shared_intrs - num_ipis, num_ipis);
  584. }
  585. bitmap_copy(ipi_available, ipi_resrv, GIC_MAX_INTRS);
  586. return 0;
  587. }
  588. #else /* !CONFIG_GENERIC_IRQ_IPI */
  589. static inline int gic_register_ipi_domain(struct device_node *node)
  590. {
  591. return 0;
  592. }
  593. #endif /* !CONFIG_GENERIC_IRQ_IPI */
  594. static int gic_cpu_startup(unsigned int cpu)
  595. {
  596. /* Enable or disable EIC */
  597. change_gic_vl_ctl(GIC_VX_CTL_EIC,
  598. cpu_has_veic ? GIC_VX_CTL_EIC : 0);
  599. /* Clear all local IRQ masks (ie. disable all local interrupts) */
  600. write_gic_vl_rmask(~0);
  601. /* Enable desired interrupts */
  602. gic_all_vpes_irq_cpu_online();
  603. return 0;
  604. }
  605. static int __init gic_of_init(struct device_node *node,
  606. struct device_node *parent)
  607. {
  608. unsigned int cpu_vec, i, gicconfig;
  609. unsigned long reserved;
  610. phys_addr_t gic_base;
  611. struct resource res;
  612. size_t gic_len;
  613. int ret;
  614. /* Find the first available CPU vector. */
  615. i = 0;
  616. reserved = (C_SW0 | C_SW1) >> __ffs(C_SW0);
  617. while (!of_property_read_u32_index(node, "mti,reserved-cpu-vectors",
  618. i++, &cpu_vec))
  619. reserved |= BIT(cpu_vec);
  620. cpu_vec = find_first_zero_bit(&reserved, hweight_long(ST0_IM));
  621. if (cpu_vec == hweight_long(ST0_IM)) {
  622. pr_err("No CPU vectors available\n");
  623. return -ENODEV;
  624. }
  625. if (of_address_to_resource(node, 0, &res)) {
  626. /*
  627. * Probe the CM for the GIC base address if not specified
  628. * in the device-tree.
  629. */
  630. if (mips_cm_present()) {
  631. gic_base = read_gcr_gic_base() &
  632. ~CM_GCR_GIC_BASE_GICEN;
  633. gic_len = 0x20000;
  634. pr_warn("Using inherited base address %pa\n",
  635. &gic_base);
  636. } else {
  637. pr_err("Failed to get memory range\n");
  638. return -ENODEV;
  639. }
  640. } else {
  641. gic_base = res.start;
  642. gic_len = resource_size(&res);
  643. }
  644. if (mips_cm_present()) {
  645. write_gcr_gic_base(gic_base | CM_GCR_GIC_BASE_GICEN);
  646. /* Ensure GIC region is enabled before trying to access it */
  647. __sync();
  648. }
  649. mips_gic_base = ioremap(gic_base, gic_len);
  650. if (!mips_gic_base) {
  651. pr_err("Failed to ioremap gic_base\n");
  652. return -ENOMEM;
  653. }
  654. gicconfig = read_gic_config();
  655. gic_shared_intrs = FIELD_GET(GIC_CONFIG_NUMINTERRUPTS, gicconfig);
  656. gic_shared_intrs = (gic_shared_intrs + 1) * 8;
  657. if (cpu_has_veic) {
  658. /* Always use vector 1 in EIC mode */
  659. gic_cpu_pin = 0;
  660. timer_cpu_pin = gic_cpu_pin;
  661. set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET,
  662. __gic_irq_dispatch);
  663. } else {
  664. gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET;
  665. irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec,
  666. gic_irq_dispatch);
  667. /*
  668. * With the CMP implementation of SMP (deprecated), other CPUs
  669. * are started by the bootloader and put into a timer based
  670. * waiting poll loop. We must not re-route those CPU's local
  671. * timer interrupts as the wait instruction will never finish,
  672. * so just handle whatever CPU interrupt it is routed to by
  673. * default.
  674. *
  675. * This workaround should be removed when CMP support is
  676. * dropped.
  677. */
  678. if (IS_ENABLED(CONFIG_MIPS_CMP) &&
  679. gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER)) {
  680. timer_cpu_pin = read_gic_vl_timer_map() & GIC_MAP_PIN_MAP;
  681. irq_set_chained_handler(MIPS_CPU_IRQ_BASE +
  682. GIC_CPU_PIN_OFFSET +
  683. timer_cpu_pin,
  684. gic_irq_dispatch);
  685. } else {
  686. timer_cpu_pin = gic_cpu_pin;
  687. }
  688. }
  689. gic_irq_domain = irq_domain_add_simple(node, GIC_NUM_LOCAL_INTRS +
  690. gic_shared_intrs, 0,
  691. &gic_irq_domain_ops, NULL);
  692. if (!gic_irq_domain) {
  693. pr_err("Failed to add IRQ domain");
  694. return -ENXIO;
  695. }
  696. ret = gic_register_ipi_domain(node);
  697. if (ret)
  698. return ret;
  699. board_bind_eic_interrupt = &gic_bind_eic_interrupt;
  700. /* Setup defaults */
  701. for (i = 0; i < gic_shared_intrs; i++) {
  702. change_gic_pol(i, GIC_POL_ACTIVE_HIGH);
  703. change_gic_trig(i, GIC_TRIG_LEVEL);
  704. write_gic_rmask(i);
  705. }
  706. return cpuhp_setup_state(CPUHP_AP_IRQ_MIPS_GIC_STARTING,
  707. "irqchip/mips/gic:starting",
  708. gic_cpu_startup, NULL);
  709. }
  710. IRQCHIP_DECLARE(mips_gic, "mti,gic", gic_of_init);