irq-mips-gic.c 20 KB

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