irq-mips-gic.c 20 KB

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