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
6 * Copyright (C) 2008 Ralf Baechle (ralf@linux-mips.org)
7 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
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/mips-gic.h>
15 #include <linux/of_address.h>
16 #include <linux/sched.h>
17 #include <linux/smp.h>
19 #include <asm/mips-cm.h>
20 #include <asm/setup.h>
21 #include <asm/traps.h>
23 #include <dt-bindings/interrupt-controller/mips-gic.h>
27 unsigned int gic_present;
29 struct gic_pcpu_mask {
30 DECLARE_BITMAP(pcpu_mask, GIC_MAX_INTRS);
33 static void __iomem *gic_base;
34 static struct gic_pcpu_mask pcpu_masks[NR_CPUS];
35 static DEFINE_SPINLOCK(gic_lock);
36 static struct irq_domain *gic_irq_domain;
37 static int gic_shared_intrs;
39 static unsigned int gic_cpu_pin;
40 static unsigned int timer_cpu_pin;
41 static struct irq_chip gic_level_irq_controller, gic_edge_irq_controller;
43 static void __gic_irq_dispatch(void);
45 static inline u32 gic_read32(unsigned int reg)
47 return __raw_readl(gic_base + reg);
50 static inline u64 gic_read64(unsigned int reg)
52 return __raw_readq(gic_base + reg);
55 static inline unsigned long gic_read(unsigned int reg)
58 return gic_read32(reg);
60 return gic_read64(reg);
63 static inline void gic_write32(unsigned int reg, u32 val)
65 return __raw_writel(val, gic_base + reg);
68 static inline void gic_write64(unsigned int reg, u64 val)
70 return __raw_writeq(val, gic_base + reg);
73 static inline void gic_write(unsigned int reg, unsigned long val)
76 return gic_write32(reg, (u32)val);
78 return gic_write64(reg, (u64)val);
81 static inline void gic_update_bits(unsigned int reg, unsigned long mask,
86 regval = gic_read(reg);
89 gic_write(reg, regval);
92 static inline void gic_reset_mask(unsigned int intr)
94 gic_write(GIC_REG(SHARED, GIC_SH_RMASK) + GIC_INTR_OFS(intr),
95 1ul << GIC_INTR_BIT(intr));
98 static inline void gic_set_mask(unsigned int intr)
100 gic_write(GIC_REG(SHARED, GIC_SH_SMASK) + GIC_INTR_OFS(intr),
101 1ul << GIC_INTR_BIT(intr));
104 static inline void gic_set_polarity(unsigned int intr, unsigned int pol)
106 gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_POLARITY) +
107 GIC_INTR_OFS(intr), 1ul << GIC_INTR_BIT(intr),
108 (unsigned long)pol << GIC_INTR_BIT(intr));
111 static inline void gic_set_trigger(unsigned int intr, unsigned int trig)
113 gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_TRIGGER) +
114 GIC_INTR_OFS(intr), 1ul << GIC_INTR_BIT(intr),
115 (unsigned long)trig << GIC_INTR_BIT(intr));
118 static inline void gic_set_dual_edge(unsigned int intr, unsigned int dual)
120 gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_DUAL) + GIC_INTR_OFS(intr),
121 1ul << GIC_INTR_BIT(intr),
122 (unsigned long)dual << GIC_INTR_BIT(intr));
125 static inline void gic_map_to_pin(unsigned int intr, unsigned int pin)
127 gic_write32(GIC_REG(SHARED, GIC_SH_INTR_MAP_TO_PIN_BASE) +
128 GIC_SH_MAP_TO_PIN(intr), GIC_MAP_TO_PIN_MSK | pin);
131 static inline void gic_map_to_vpe(unsigned int intr, unsigned int vpe)
133 gic_write(GIC_REG(SHARED, GIC_SH_INTR_MAP_TO_VPE_BASE) +
134 GIC_SH_MAP_TO_VPE_REG_OFF(intr, vpe),
135 GIC_SH_MAP_TO_VPE_REG_BIT(vpe));
138 #ifdef CONFIG_CLKSRC_MIPS_GIC
139 cycle_t gic_read_count(void)
141 unsigned int hi, hi2, lo;
144 return (cycle_t)gic_read(GIC_REG(SHARED, GIC_SH_COUNTER));
147 hi = gic_read32(GIC_REG(SHARED, GIC_SH_COUNTER_63_32));
148 lo = gic_read32(GIC_REG(SHARED, GIC_SH_COUNTER_31_00));
149 hi2 = gic_read32(GIC_REG(SHARED, GIC_SH_COUNTER_63_32));
152 return (((cycle_t) hi) << 32) + lo;
155 unsigned int gic_get_count_width(void)
157 unsigned int bits, config;
159 config = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
160 bits = 32 + 4 * ((config & GIC_SH_CONFIG_COUNTBITS_MSK) >>
161 GIC_SH_CONFIG_COUNTBITS_SHF);
166 void gic_write_compare(cycle_t cnt)
169 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE), cnt);
171 gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_HI),
173 gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_LO),
174 (int)(cnt & 0xffffffff));
178 void gic_write_cpu_compare(cycle_t cnt, int cpu)
182 local_irq_save(flags);
184 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), cpu);
187 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE), cnt);
189 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_HI),
191 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_LO),
192 (int)(cnt & 0xffffffff));
195 local_irq_restore(flags);
198 cycle_t gic_read_compare(void)
203 return (cycle_t)gic_read(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE));
205 hi = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_HI));
206 lo = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_LO));
208 return (((cycle_t) hi) << 32) + lo;
211 void gic_start_count(void)
215 /* Start the counter */
216 gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
217 gicconfig &= ~(1 << GIC_SH_CONFIG_COUNTSTOP_SHF);
218 gic_write(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig);
221 void gic_stop_count(void)
225 /* Stop the counter */
226 gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
227 gicconfig |= 1 << GIC_SH_CONFIG_COUNTSTOP_SHF;
228 gic_write(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig);
233 static bool gic_local_irq_is_routable(int intr)
237 /* All local interrupts are routable in EIC mode. */
241 vpe_ctl = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_CTL));
243 case GIC_LOCAL_INT_TIMER:
244 return vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK;
245 case GIC_LOCAL_INT_PERFCTR:
246 return vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK;
247 case GIC_LOCAL_INT_FDC:
248 return vpe_ctl & GIC_VPE_CTL_FDC_RTBL_MSK;
249 case GIC_LOCAL_INT_SWINT0:
250 case GIC_LOCAL_INT_SWINT1:
251 return vpe_ctl & GIC_VPE_CTL_SWINT_RTBL_MSK;
257 static void gic_bind_eic_interrupt(int irq, int set)
259 /* Convert irq vector # to hw int # */
260 irq -= GIC_PIN_TO_VEC_OFFSET;
262 /* Set irq to use shadow set */
263 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_EIC_SHADOW_SET_BASE) +
264 GIC_VPE_EIC_SS(irq), set);
267 void gic_send_ipi(unsigned int intr)
269 gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_SET(intr));
272 int gic_get_c0_compare_int(void)
274 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER))
275 return MIPS_CPU_IRQ_BASE + cp0_compare_irq;
276 return irq_create_mapping(gic_irq_domain,
277 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_TIMER));
280 int gic_get_c0_perfcount_int(void)
282 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_PERFCTR)) {
283 /* Is the performance counter shared with the timer? */
284 if (cp0_perfcount_irq < 0)
286 return MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
288 return irq_create_mapping(gic_irq_domain,
289 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_PERFCTR));
292 int gic_get_c0_fdc_int(void)
294 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_FDC)) {
295 /* Is the FDC IRQ even present? */
298 return MIPS_CPU_IRQ_BASE + cp0_fdc_irq;
301 return irq_create_mapping(gic_irq_domain,
302 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_FDC));
305 static void gic_handle_shared_int(bool chained)
307 unsigned int i, intr, virq, gic_reg_step = mips_cm_is64 ? 8 : 4;
308 unsigned long *pcpu_mask;
309 unsigned long pending_reg, intrmask_reg;
310 DECLARE_BITMAP(pending, GIC_MAX_INTRS);
311 DECLARE_BITMAP(intrmask, GIC_MAX_INTRS);
313 /* Get per-cpu bitmaps */
314 pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask;
316 pending_reg = GIC_REG(SHARED, GIC_SH_PEND);
317 intrmask_reg = GIC_REG(SHARED, GIC_SH_MASK);
319 for (i = 0; i < BITS_TO_LONGS(gic_shared_intrs); i++) {
320 pending[i] = gic_read(pending_reg);
321 intrmask[i] = gic_read(intrmask_reg);
322 pending_reg += gic_reg_step;
323 intrmask_reg += gic_reg_step;
326 bitmap_and(pending, pending, intrmask, gic_shared_intrs);
327 bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs);
329 intr = find_first_bit(pending, gic_shared_intrs);
330 while (intr != gic_shared_intrs) {
331 virq = irq_linear_revmap(gic_irq_domain,
332 GIC_SHARED_TO_HWIRQ(intr));
334 generic_handle_irq(virq);
338 /* go to next pending bit */
339 bitmap_clear(pending, intr, 1);
340 intr = find_first_bit(pending, gic_shared_intrs);
344 static void gic_mask_irq(struct irq_data *d)
346 gic_reset_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
349 static void gic_unmask_irq(struct irq_data *d)
351 gic_set_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
354 static void gic_ack_irq(struct irq_data *d)
356 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
358 gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_CLR(irq));
361 static int gic_set_type(struct irq_data *d, unsigned int type)
363 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
367 spin_lock_irqsave(&gic_lock, flags);
368 switch (type & IRQ_TYPE_SENSE_MASK) {
369 case IRQ_TYPE_EDGE_FALLING:
370 gic_set_polarity(irq, GIC_POL_NEG);
371 gic_set_trigger(irq, GIC_TRIG_EDGE);
372 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
375 case IRQ_TYPE_EDGE_RISING:
376 gic_set_polarity(irq, GIC_POL_POS);
377 gic_set_trigger(irq, GIC_TRIG_EDGE);
378 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
381 case IRQ_TYPE_EDGE_BOTH:
382 /* polarity is irrelevant in this case */
383 gic_set_trigger(irq, GIC_TRIG_EDGE);
384 gic_set_dual_edge(irq, GIC_TRIG_DUAL_ENABLE);
387 case IRQ_TYPE_LEVEL_LOW:
388 gic_set_polarity(irq, GIC_POL_NEG);
389 gic_set_trigger(irq, GIC_TRIG_LEVEL);
390 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
393 case IRQ_TYPE_LEVEL_HIGH:
395 gic_set_polarity(irq, GIC_POL_POS);
396 gic_set_trigger(irq, GIC_TRIG_LEVEL);
397 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
403 __irq_set_chip_handler_name_locked(d->irq,
404 &gic_edge_irq_controller,
405 handle_edge_irq, NULL);
407 __irq_set_chip_handler_name_locked(d->irq,
408 &gic_level_irq_controller,
409 handle_level_irq, NULL);
411 spin_unlock_irqrestore(&gic_lock, flags);
417 static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
420 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
421 cpumask_t tmp = CPU_MASK_NONE;
425 cpumask_and(&tmp, cpumask, cpu_online_mask);
426 if (cpumask_empty(&tmp))
429 /* Assumption : cpumask refers to a single CPU */
430 spin_lock_irqsave(&gic_lock, flags);
432 /* Re-route this IRQ */
433 gic_map_to_vpe(irq, cpumask_first(&tmp));
435 /* Update the pcpu_masks */
436 for (i = 0; i < NR_CPUS; i++)
437 clear_bit(irq, pcpu_masks[i].pcpu_mask);
438 set_bit(irq, pcpu_masks[cpumask_first(&tmp)].pcpu_mask);
440 cpumask_copy(d->affinity, cpumask);
441 spin_unlock_irqrestore(&gic_lock, flags);
443 return IRQ_SET_MASK_OK_NOCOPY;
447 static struct irq_chip gic_level_irq_controller = {
449 .irq_mask = gic_mask_irq,
450 .irq_unmask = gic_unmask_irq,
451 .irq_set_type = gic_set_type,
453 .irq_set_affinity = gic_set_affinity,
457 static struct irq_chip gic_edge_irq_controller = {
459 .irq_ack = gic_ack_irq,
460 .irq_mask = gic_mask_irq,
461 .irq_unmask = gic_unmask_irq,
462 .irq_set_type = gic_set_type,
464 .irq_set_affinity = gic_set_affinity,
468 static void gic_handle_local_int(bool chained)
470 unsigned long pending, masked;
471 unsigned int intr, virq;
473 pending = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_PEND));
474 masked = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_MASK));
476 bitmap_and(&pending, &pending, &masked, GIC_NUM_LOCAL_INTRS);
478 intr = find_first_bit(&pending, GIC_NUM_LOCAL_INTRS);
479 while (intr != GIC_NUM_LOCAL_INTRS) {
480 virq = irq_linear_revmap(gic_irq_domain,
481 GIC_LOCAL_TO_HWIRQ(intr));
483 generic_handle_irq(virq);
487 /* go to next pending bit */
488 bitmap_clear(&pending, intr, 1);
489 intr = find_first_bit(&pending, GIC_NUM_LOCAL_INTRS);
493 static void gic_mask_local_irq(struct irq_data *d)
495 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
497 gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_RMASK), 1 << intr);
500 static void gic_unmask_local_irq(struct irq_data *d)
502 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
504 gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_SMASK), 1 << intr);
507 static struct irq_chip gic_local_irq_controller = {
508 .name = "MIPS GIC Local",
509 .irq_mask = gic_mask_local_irq,
510 .irq_unmask = gic_unmask_local_irq,
513 static void gic_mask_local_irq_all_vpes(struct irq_data *d)
515 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
519 spin_lock_irqsave(&gic_lock, flags);
520 for (i = 0; i < gic_vpes; i++) {
521 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
522 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << intr);
524 spin_unlock_irqrestore(&gic_lock, flags);
527 static void gic_unmask_local_irq_all_vpes(struct irq_data *d)
529 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
533 spin_lock_irqsave(&gic_lock, flags);
534 for (i = 0; i < gic_vpes; i++) {
535 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
536 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SMASK), 1 << intr);
538 spin_unlock_irqrestore(&gic_lock, flags);
541 static struct irq_chip gic_all_vpes_local_irq_controller = {
542 .name = "MIPS GIC Local",
543 .irq_mask = gic_mask_local_irq_all_vpes,
544 .irq_unmask = gic_unmask_local_irq_all_vpes,
547 static void __gic_irq_dispatch(void)
549 gic_handle_local_int(false);
550 gic_handle_shared_int(false);
553 static void gic_irq_dispatch(unsigned int irq, struct irq_desc *desc)
555 gic_handle_local_int(true);
556 gic_handle_shared_int(true);
559 #ifdef CONFIG_MIPS_GIC_IPI
560 static int gic_resched_int_base;
561 static int gic_call_int_base;
563 unsigned int plat_ipi_resched_int_xlate(unsigned int cpu)
565 return gic_resched_int_base + cpu;
568 unsigned int plat_ipi_call_int_xlate(unsigned int cpu)
570 return gic_call_int_base + cpu;
573 static irqreturn_t ipi_resched_interrupt(int irq, void *dev_id)
580 static irqreturn_t ipi_call_interrupt(int irq, void *dev_id)
582 generic_smp_call_function_interrupt();
587 static struct irqaction irq_resched = {
588 .handler = ipi_resched_interrupt,
589 .flags = IRQF_PERCPU,
590 .name = "IPI resched"
593 static struct irqaction irq_call = {
594 .handler = ipi_call_interrupt,
595 .flags = IRQF_PERCPU,
599 static __init void gic_ipi_init_one(unsigned int intr, int cpu,
600 struct irqaction *action)
602 int virq = irq_create_mapping(gic_irq_domain,
603 GIC_SHARED_TO_HWIRQ(intr));
606 gic_map_to_vpe(intr, cpu);
607 for (i = 0; i < NR_CPUS; i++)
608 clear_bit(intr, pcpu_masks[i].pcpu_mask);
609 set_bit(intr, pcpu_masks[cpu].pcpu_mask);
611 irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
613 irq_set_handler(virq, handle_percpu_irq);
614 setup_irq(virq, action);
617 static __init void gic_ipi_init(void)
621 /* Use last 2 * NR_CPUS interrupts as IPIs */
622 gic_resched_int_base = gic_shared_intrs - nr_cpu_ids;
623 gic_call_int_base = gic_resched_int_base - nr_cpu_ids;
625 for (i = 0; i < nr_cpu_ids; i++) {
626 gic_ipi_init_one(gic_call_int_base + i, i, &irq_call);
627 gic_ipi_init_one(gic_resched_int_base + i, i, &irq_resched);
631 static inline void gic_ipi_init(void)
636 static void __init gic_basic_init(void)
640 board_bind_eic_interrupt = &gic_bind_eic_interrupt;
643 for (i = 0; i < gic_shared_intrs; i++) {
644 gic_set_polarity(i, GIC_POL_POS);
645 gic_set_trigger(i, GIC_TRIG_LEVEL);
649 for (i = 0; i < gic_vpes; i++) {
652 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
653 for (j = 0; j < GIC_NUM_LOCAL_INTRS; j++) {
654 if (!gic_local_irq_is_routable(j))
656 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << j);
661 static int gic_local_irq_domain_map(struct irq_domain *d, unsigned int virq,
664 int intr = GIC_HWIRQ_TO_LOCAL(hw);
669 if (!gic_local_irq_is_routable(intr))
673 * HACK: These are all really percpu interrupts, but the rest
674 * of the MIPS kernel code does not use the percpu IRQ API for
675 * the CP0 timer and performance counter interrupts.
678 case GIC_LOCAL_INT_TIMER:
679 case GIC_LOCAL_INT_PERFCTR:
680 case GIC_LOCAL_INT_FDC:
681 irq_set_chip_and_handler(virq,
682 &gic_all_vpes_local_irq_controller,
686 irq_set_chip_and_handler(virq,
687 &gic_local_irq_controller,
688 handle_percpu_devid_irq);
689 irq_set_percpu_devid(virq);
693 spin_lock_irqsave(&gic_lock, flags);
694 for (i = 0; i < gic_vpes; i++) {
695 u32 val = GIC_MAP_TO_PIN_MSK | gic_cpu_pin;
697 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
700 case GIC_LOCAL_INT_WD:
701 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_WD_MAP), val);
703 case GIC_LOCAL_INT_COMPARE:
704 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_MAP),
707 case GIC_LOCAL_INT_TIMER:
708 /* CONFIG_MIPS_CMP workaround (see __gic_init) */
709 val = GIC_MAP_TO_PIN_MSK | timer_cpu_pin;
710 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_TIMER_MAP),
713 case GIC_LOCAL_INT_PERFCTR:
714 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_PERFCTR_MAP),
717 case GIC_LOCAL_INT_SWINT0:
718 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SWINT0_MAP),
721 case GIC_LOCAL_INT_SWINT1:
722 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SWINT1_MAP),
725 case GIC_LOCAL_INT_FDC:
726 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_FDC_MAP), val);
729 pr_err("Invalid local IRQ %d\n", intr);
734 spin_unlock_irqrestore(&gic_lock, flags);
739 static int gic_shared_irq_domain_map(struct irq_domain *d, unsigned int virq,
742 int intr = GIC_HWIRQ_TO_SHARED(hw);
745 irq_set_chip_and_handler(virq, &gic_level_irq_controller,
748 spin_lock_irqsave(&gic_lock, flags);
749 gic_map_to_pin(intr, gic_cpu_pin);
750 /* Map to VPE 0 by default */
751 gic_map_to_vpe(intr, 0);
752 set_bit(intr, pcpu_masks[0].pcpu_mask);
753 spin_unlock_irqrestore(&gic_lock, flags);
758 static int gic_irq_domain_map(struct irq_domain *d, unsigned int virq,
761 if (GIC_HWIRQ_TO_LOCAL(hw) < GIC_NUM_LOCAL_INTRS)
762 return gic_local_irq_domain_map(d, virq, hw);
763 return gic_shared_irq_domain_map(d, virq, hw);
766 static int gic_irq_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
767 const u32 *intspec, unsigned int intsize,
768 irq_hw_number_t *out_hwirq,
769 unsigned int *out_type)
774 if (intspec[0] == GIC_SHARED)
775 *out_hwirq = GIC_SHARED_TO_HWIRQ(intspec[1]);
776 else if (intspec[0] == GIC_LOCAL)
777 *out_hwirq = GIC_LOCAL_TO_HWIRQ(intspec[1]);
780 *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
785 static const struct irq_domain_ops gic_irq_domain_ops = {
786 .map = gic_irq_domain_map,
787 .xlate = gic_irq_domain_xlate,
790 static void __init __gic_init(unsigned long gic_base_addr,
791 unsigned long gic_addrspace_size,
792 unsigned int cpu_vec, unsigned int irqbase,
793 struct device_node *node)
795 unsigned int gicconfig;
797 gic_base = ioremap_nocache(gic_base_addr, gic_addrspace_size);
799 gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
800 gic_shared_intrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >>
801 GIC_SH_CONFIG_NUMINTRS_SHF;
802 gic_shared_intrs = ((gic_shared_intrs + 1) * 8);
804 gic_vpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >>
805 GIC_SH_CONFIG_NUMVPES_SHF;
806 gic_vpes = gic_vpes + 1;
809 /* Always use vector 1 in EIC mode */
811 timer_cpu_pin = gic_cpu_pin;
812 set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET,
815 gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET;
816 irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec,
819 * With the CMP implementation of SMP (deprecated), other CPUs
820 * are started by the bootloader and put into a timer based
821 * waiting poll loop. We must not re-route those CPU's local
822 * timer interrupts as the wait instruction will never finish,
823 * so just handle whatever CPU interrupt it is routed to by
826 * This workaround should be removed when CMP support is
829 if (IS_ENABLED(CONFIG_MIPS_CMP) &&
830 gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER)) {
831 timer_cpu_pin = gic_read32(GIC_REG(VPE_LOCAL,
832 GIC_VPE_TIMER_MAP)) &
834 irq_set_chained_handler(MIPS_CPU_IRQ_BASE +
839 timer_cpu_pin = gic_cpu_pin;
843 gic_irq_domain = irq_domain_add_simple(node, GIC_NUM_LOCAL_INTRS +
844 gic_shared_intrs, irqbase,
845 &gic_irq_domain_ops, NULL);
847 panic("Failed to add GIC IRQ domain");
854 void __init gic_init(unsigned long gic_base_addr,
855 unsigned long gic_addrspace_size,
856 unsigned int cpu_vec, unsigned int irqbase)
858 __gic_init(gic_base_addr, gic_addrspace_size, cpu_vec, irqbase, NULL);
861 static int __init gic_of_init(struct device_node *node,
862 struct device_node *parent)
865 unsigned int cpu_vec, i = 0, reserved = 0;
866 phys_addr_t gic_base;
869 /* Find the first available CPU vector. */
870 while (!of_property_read_u32_index(node, "mti,reserved-cpu-vectors",
872 reserved |= BIT(cpu_vec);
873 for (cpu_vec = 2; cpu_vec < 8; cpu_vec++) {
874 if (!(reserved & BIT(cpu_vec)))
878 pr_err("No CPU vectors available for GIC\n");
882 if (of_address_to_resource(node, 0, &res)) {
884 * Probe the CM for the GIC base address if not specified
885 * in the device-tree.
887 if (mips_cm_present()) {
888 gic_base = read_gcr_gic_base() &
889 ~CM_GCR_GIC_BASE_GICEN_MSK;
892 pr_err("Failed to get GIC memory range\n");
896 gic_base = res.start;
897 gic_len = resource_size(&res);
900 if (mips_cm_present())
901 write_gcr_gic_base(gic_base | CM_GCR_GIC_BASE_GICEN_MSK);
904 __gic_init(gic_base, gic_len, cpu_vec, 0, node);
908 IRQCHIP_DECLARE(mips_gic, "mti,gic", gic_of_init);