1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/interrupt.h>
5 #include <linux/init.h>
7 #include <asm/irq_cpu.h>
9 #include <asm/mipsregs.h>
13 extern void loongson3_send_irq_by_ipi(int cpu, int irqs);
15 unsigned int irq_cpu[16] = {[0 ... 15] = -1};
16 unsigned int ht_irq[] = {0, 1, 3, 4, 5, 6, 7, 8, 12, 14, 15};
17 unsigned int local_irq = 1<<0 | 1<<1 | 1<<2 | 1<<7 | 1<<8 | 1<<12;
19 int plat_set_irq_affinity(struct irq_data *d, const struct cpumask *affinity,
23 struct cpumask new_affinity;
25 /* I/O devices are connected on package-0 */
26 cpumask_copy(&new_affinity, affinity);
27 for_each_cpu(cpu, affinity)
28 if (cpu_data[cpu].package > 0)
29 cpumask_clear_cpu(cpu, &new_affinity);
31 if (cpumask_empty(&new_affinity))
34 cpumask_copy(d->common->affinity, &new_affinity);
36 return IRQ_SET_MASK_OK_NOCOPY;
39 static void ht_irqdispatch(void)
42 struct irq_data *irqd;
43 struct cpumask affinity;
45 irq = LOONGSON_HT1_INT_VECTOR(0);
46 LOONGSON_HT1_INT_VECTOR(0) = irq; /* Acknowledge the IRQs */
48 for (i = 0; i < ARRAY_SIZE(ht_irq); i++) {
49 if (!(irq & (0x1 << ht_irq[i])))
52 /* handled by local core */
53 if (local_irq & (0x1 << ht_irq[i])) {
58 irqd = irq_get_irq_data(ht_irq[i]);
59 cpumask_and(&affinity, irqd->common->affinity, cpu_active_mask);
60 if (cpumask_empty(&affinity)) {
65 irq_cpu[ht_irq[i]] = cpumask_next(irq_cpu[ht_irq[i]], &affinity);
66 if (irq_cpu[ht_irq[i]] >= nr_cpu_ids)
67 irq_cpu[ht_irq[i]] = cpumask_first(&affinity);
69 if (irq_cpu[ht_irq[i]] == 0) {
74 /* balanced by other cores */
75 loongson3_send_irq_by_ipi(irq_cpu[ht_irq[i]], (0x1 << ht_irq[i]));
79 #define UNUSED_IPS (CAUSEF_IP5 | CAUSEF_IP4 | CAUSEF_IP1 | CAUSEF_IP0)
81 void mach_irq_dispatch(unsigned int pending)
83 if (pending & CAUSEF_IP7)
84 do_IRQ(LOONGSON_TIMER_IRQ);
85 #if defined(CONFIG_SMP)
86 if (pending & CAUSEF_IP6)
87 loongson3_ipi_interrupt(NULL);
89 if (pending & CAUSEF_IP3)
91 if (pending & CAUSEF_IP2)
92 do_IRQ(LOONGSON_UART_IRQ);
93 if (pending & UNUSED_IPS) {
94 pr_err("%s : spurious interrupt\n", __func__);
99 static inline void mask_loongson_irq(struct irq_data *d) { }
100 static inline void unmask_loongson_irq(struct irq_data *d) { }
102 /* For MIPS IRQs which shared by all cores */
103 static struct irq_chip loongson_irq_chip = {
105 .irq_ack = mask_loongson_irq,
106 .irq_mask = mask_loongson_irq,
107 .irq_mask_ack = mask_loongson_irq,
108 .irq_unmask = unmask_loongson_irq,
109 .irq_eoi = unmask_loongson_irq,
112 void irq_router_init(void)
116 /* route LPC int to cpu core0 int 0 */
117 LOONGSON_INT_ROUTER_LPC =
118 LOONGSON_INT_COREx_INTy(loongson_sysconf.boot_cpu_id, 0);
119 /* route HT1 int0 ~ int7 to cpu core0 INT1*/
120 for (i = 0; i < 8; i++)
121 LOONGSON_INT_ROUTER_HT1(i) =
122 LOONGSON_INT_COREx_INTy(loongson_sysconf.boot_cpu_id, 1);
123 /* enable HT1 interrupt */
124 LOONGSON_HT1_INTN_EN(0) = 0xffffffff;
125 /* enable router interrupt intenset */
126 LOONGSON_INT_ROUTER_INTENSET =
127 LOONGSON_INT_ROUTER_INTEN | (0xffff << 16) | 0x1 << 10;
130 void __init mach_init_irq(void)
132 struct irq_chip *chip;
134 clear_c0_status(ST0_IM | ST0_BEV);
139 chip = irq_get_chip(I8259A_IRQ_BASE);
140 chip->irq_set_affinity = plat_set_irq_affinity;
142 irq_set_chip_and_handler(LOONGSON_UART_IRQ,
143 &loongson_irq_chip, handle_percpu_irq);
144 irq_set_chip_and_handler(LOONGSON_BRIDGE_IRQ,
145 &loongson_irq_chip, handle_percpu_irq);
147 set_c0_status(STATUSF_IP2 | STATUSF_IP3 | STATUSF_IP6);
150 #ifdef CONFIG_HOTPLUG_CPU
152 void fixup_irqs(void)
155 clear_c0_status(ST0_IM);