1 // SPDX-License-Identifier: GPL-2.0
2 #include <loongson.h>
3 #include <irq.h>
4 #include <linux/interrupt.h>
5 #include <linux/init.h>
6
7 #include <asm/irq_cpu.h>
8 #include <asm/i8259.h>
9 #include <asm/mipsregs.h>
10
11 #include "smp.h"
12
13 extern void loongson3_send_irq_by_ipi(int cpu, int irqs);
14
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;
18
plat_set_irq_affinity(struct irq_data * d,const struct cpumask * affinity,bool force)19 int plat_set_irq_affinity(struct irq_data *d, const struct cpumask *affinity,
20 bool force)
21 {
22 unsigned int cpu;
23 struct cpumask new_affinity;
24
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);
30
31 if (cpumask_empty(&new_affinity))
32 return -EINVAL;
33
34 cpumask_copy(d->common->affinity, &new_affinity);
35
36 return IRQ_SET_MASK_OK_NOCOPY;
37 }
38
ht_irqdispatch(void)39 static void ht_irqdispatch(void)
40 {
41 unsigned int i, irq;
42 struct irq_data *irqd;
43 struct cpumask affinity;
44
45 irq = LOONGSON_HT1_INT_VECTOR(0);
46 LOONGSON_HT1_INT_VECTOR(0) = irq; /* Acknowledge the IRQs */
47
48 for (i = 0; i < ARRAY_SIZE(ht_irq); i++) {
49 if (!(irq & (0x1 << ht_irq[i])))
50 continue;
51
52 /* handled by local core */
53 if (local_irq & (0x1 << ht_irq[i])) {
54 do_IRQ(ht_irq[i]);
55 continue;
56 }
57
58 irqd = irq_get_irq_data(ht_irq[i]);
59 cpumask_and(&affinity, irqd->common->affinity, cpu_active_mask);
60 if (cpumask_empty(&affinity)) {
61 do_IRQ(ht_irq[i]);
62 continue;
63 }
64
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);
68
69 if (irq_cpu[ht_irq[i]] == 0) {
70 do_IRQ(ht_irq[i]);
71 continue;
72 }
73
74 /* balanced by other cores */
75 loongson3_send_irq_by_ipi(irq_cpu[ht_irq[i]], (0x1 << ht_irq[i]));
76 }
77 }
78
79 #define UNUSED_IPS (CAUSEF_IP5 | CAUSEF_IP4 | CAUSEF_IP1 | CAUSEF_IP0)
80
mach_irq_dispatch(unsigned int pending)81 void mach_irq_dispatch(unsigned int pending)
82 {
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);
88 #endif
89 if (pending & CAUSEF_IP3)
90 ht_irqdispatch();
91 if (pending & CAUSEF_IP2)
92 do_IRQ(LOONGSON_UART_IRQ);
93 if (pending & UNUSED_IPS) {
94 pr_err("%s : spurious interrupt\n", __func__);
95 spurious_interrupt();
96 }
97 }
98
mask_loongson_irq(struct irq_data * d)99 static inline void mask_loongson_irq(struct irq_data *d) { }
unmask_loongson_irq(struct irq_data * d)100 static inline void unmask_loongson_irq(struct irq_data *d) { }
101
102 /* For MIPS IRQs which shared by all cores */
103 static struct irq_chip loongson_irq_chip = {
104 .name = "Loongson",
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,
110 };
111
irq_router_init(void)112 void irq_router_init(void)
113 {
114 int i;
115
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;
128 }
129
mach_init_irq(void)130 void __init mach_init_irq(void)
131 {
132 struct irq_chip *chip;
133
134 clear_c0_status(ST0_IM | ST0_BEV);
135
136 irq_router_init();
137 mips_cpu_irq_init();
138 init_i8259_irqs();
139 chip = irq_get_chip(I8259A_IRQ_BASE);
140 chip->irq_set_affinity = plat_set_irq_affinity;
141
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);
146
147 set_c0_status(STATUSF_IP2 | STATUSF_IP3 | STATUSF_IP6);
148 }
149
150 #ifdef CONFIG_HOTPLUG_CPU
151
fixup_irqs(void)152 void fixup_irqs(void)
153 {
154 irq_cpu_offline();
155 clear_c0_status(ST0_IM);
156 }
157
158 #endif
159