1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2000, 2001, 2002, 2003 Broadcom Corporation
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun #include <linux/kernel.h>
6*4882a593Smuzhiyun #include <linux/init.h>
7*4882a593Smuzhiyun #include <linux/linkage.h>
8*4882a593Smuzhiyun #include <linux/interrupt.h>
9*4882a593Smuzhiyun #include <linux/spinlock.h>
10*4882a593Smuzhiyun #include <linux/smp.h>
11*4882a593Smuzhiyun #include <linux/mm.h>
12*4882a593Smuzhiyun #include <linux/kernel_stat.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <asm/errno.h>
15*4882a593Smuzhiyun #include <asm/signal.h>
16*4882a593Smuzhiyun #include <asm/time.h>
17*4882a593Smuzhiyun #include <asm/io.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <asm/sibyte/sb1250_regs.h>
20*4882a593Smuzhiyun #include <asm/sibyte/sb1250_int.h>
21*4882a593Smuzhiyun #include <asm/sibyte/sb1250_uart.h>
22*4882a593Smuzhiyun #include <asm/sibyte/sb1250_scd.h>
23*4882a593Smuzhiyun #include <asm/sibyte/sb1250.h>
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun * These are the routines that handle all the low level interrupt stuff.
27*4882a593Smuzhiyun * Actions handled here are: initialization of the interrupt map, requesting of
28*4882a593Smuzhiyun * interrupt lines by handlers, dispatching if interrupts to handlers, probing
29*4882a593Smuzhiyun * for interrupt lines
30*4882a593Smuzhiyun */
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #ifdef CONFIG_SIBYTE_HAS_LDT
33*4882a593Smuzhiyun extern unsigned long ldt_eoi_space;
34*4882a593Smuzhiyun #endif
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun /* Store the CPU id (not the logical number) */
37*4882a593Smuzhiyun int sb1250_irq_owner[SB1250_NR_IRQS];
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun static DEFINE_RAW_SPINLOCK(sb1250_imr_lock);
40*4882a593Smuzhiyun
sb1250_mask_irq(int cpu,int irq)41*4882a593Smuzhiyun void sb1250_mask_irq(int cpu, int irq)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun unsigned long flags;
44*4882a593Smuzhiyun u64 cur_ints;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun raw_spin_lock_irqsave(&sb1250_imr_lock, flags);
47*4882a593Smuzhiyun cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
48*4882a593Smuzhiyun R_IMR_INTERRUPT_MASK));
49*4882a593Smuzhiyun cur_ints |= (((u64) 1) << irq);
50*4882a593Smuzhiyun ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
51*4882a593Smuzhiyun R_IMR_INTERRUPT_MASK));
52*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&sb1250_imr_lock, flags);
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
sb1250_unmask_irq(int cpu,int irq)55*4882a593Smuzhiyun void sb1250_unmask_irq(int cpu, int irq)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun unsigned long flags;
58*4882a593Smuzhiyun u64 cur_ints;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun raw_spin_lock_irqsave(&sb1250_imr_lock, flags);
61*4882a593Smuzhiyun cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
62*4882a593Smuzhiyun R_IMR_INTERRUPT_MASK));
63*4882a593Smuzhiyun cur_ints &= ~(((u64) 1) << irq);
64*4882a593Smuzhiyun ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
65*4882a593Smuzhiyun R_IMR_INTERRUPT_MASK));
66*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&sb1250_imr_lock, flags);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #ifdef CONFIG_SMP
sb1250_set_affinity(struct irq_data * d,const struct cpumask * mask,bool force)70*4882a593Smuzhiyun static int sb1250_set_affinity(struct irq_data *d, const struct cpumask *mask,
71*4882a593Smuzhiyun bool force)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun int i = 0, old_cpu, cpu, int_on;
74*4882a593Smuzhiyun unsigned int irq = d->irq;
75*4882a593Smuzhiyun u64 cur_ints;
76*4882a593Smuzhiyun unsigned long flags;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun i = cpumask_first_and(mask, cpu_online_mask);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* Convert logical CPU to physical CPU */
81*4882a593Smuzhiyun cpu = cpu_logical_map(i);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* Protect against other affinity changers and IMR manipulation */
84*4882a593Smuzhiyun raw_spin_lock_irqsave(&sb1250_imr_lock, flags);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /* Swizzle each CPU's IMR (but leave the IP selection alone) */
87*4882a593Smuzhiyun old_cpu = sb1250_irq_owner[irq];
88*4882a593Smuzhiyun cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(old_cpu) +
89*4882a593Smuzhiyun R_IMR_INTERRUPT_MASK));
90*4882a593Smuzhiyun int_on = !(cur_ints & (((u64) 1) << irq));
91*4882a593Smuzhiyun if (int_on) {
92*4882a593Smuzhiyun /* If it was on, mask it */
93*4882a593Smuzhiyun cur_ints |= (((u64) 1) << irq);
94*4882a593Smuzhiyun ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(old_cpu) +
95*4882a593Smuzhiyun R_IMR_INTERRUPT_MASK));
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun sb1250_irq_owner[irq] = cpu;
98*4882a593Smuzhiyun if (int_on) {
99*4882a593Smuzhiyun /* unmask for the new CPU */
100*4882a593Smuzhiyun cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
101*4882a593Smuzhiyun R_IMR_INTERRUPT_MASK));
102*4882a593Smuzhiyun cur_ints &= ~(((u64) 1) << irq);
103*4882a593Smuzhiyun ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
104*4882a593Smuzhiyun R_IMR_INTERRUPT_MASK));
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&sb1250_imr_lock, flags);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun return 0;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun #endif
111*4882a593Smuzhiyun
disable_sb1250_irq(struct irq_data * d)112*4882a593Smuzhiyun static void disable_sb1250_irq(struct irq_data *d)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun unsigned int irq = d->irq;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun sb1250_mask_irq(sb1250_irq_owner[irq], irq);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
enable_sb1250_irq(struct irq_data * d)119*4882a593Smuzhiyun static void enable_sb1250_irq(struct irq_data *d)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun unsigned int irq = d->irq;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun sb1250_unmask_irq(sb1250_irq_owner[irq], irq);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun
ack_sb1250_irq(struct irq_data * d)127*4882a593Smuzhiyun static void ack_sb1250_irq(struct irq_data *d)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun unsigned int irq = d->irq;
130*4882a593Smuzhiyun #ifdef CONFIG_SIBYTE_HAS_LDT
131*4882a593Smuzhiyun u64 pending;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /*
134*4882a593Smuzhiyun * If the interrupt was an HT interrupt, now is the time to
135*4882a593Smuzhiyun * clear it. NOTE: we assume the HT bridge was set up to
136*4882a593Smuzhiyun * deliver the interrupts to all CPUs (which makes affinity
137*4882a593Smuzhiyun * changing easier for us)
138*4882a593Smuzhiyun */
139*4882a593Smuzhiyun pending = __raw_readq(IOADDR(A_IMR_REGISTER(sb1250_irq_owner[irq],
140*4882a593Smuzhiyun R_IMR_LDT_INTERRUPT)));
141*4882a593Smuzhiyun pending &= ((u64)1 << (irq));
142*4882a593Smuzhiyun if (pending) {
143*4882a593Smuzhiyun int i;
144*4882a593Smuzhiyun for (i=0; i<NR_CPUS; i++) {
145*4882a593Smuzhiyun int cpu;
146*4882a593Smuzhiyun #ifdef CONFIG_SMP
147*4882a593Smuzhiyun cpu = cpu_logical_map(i);
148*4882a593Smuzhiyun #else
149*4882a593Smuzhiyun cpu = i;
150*4882a593Smuzhiyun #endif
151*4882a593Smuzhiyun /*
152*4882a593Smuzhiyun * Clear for all CPUs so an affinity switch
153*4882a593Smuzhiyun * doesn't find an old status
154*4882a593Smuzhiyun */
155*4882a593Smuzhiyun __raw_writeq(pending,
156*4882a593Smuzhiyun IOADDR(A_IMR_REGISTER(cpu,
157*4882a593Smuzhiyun R_IMR_LDT_INTERRUPT_CLR)));
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun * Generate EOI. For Pass 1 parts, EOI is a nop. For
162*4882a593Smuzhiyun * Pass 2, the LDT world may be edge-triggered, but
163*4882a593Smuzhiyun * this EOI shouldn't hurt. If they are
164*4882a593Smuzhiyun * level-sensitive, the EOI is required.
165*4882a593Smuzhiyun */
166*4882a593Smuzhiyun *(uint32_t *)(ldt_eoi_space+(irq<<16)+(7<<2)) = 0;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun #endif
169*4882a593Smuzhiyun sb1250_mask_irq(sb1250_irq_owner[irq], irq);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun static struct irq_chip sb1250_irq_type = {
173*4882a593Smuzhiyun .name = "SB1250-IMR",
174*4882a593Smuzhiyun .irq_mask_ack = ack_sb1250_irq,
175*4882a593Smuzhiyun .irq_unmask = enable_sb1250_irq,
176*4882a593Smuzhiyun .irq_mask = disable_sb1250_irq,
177*4882a593Smuzhiyun #ifdef CONFIG_SMP
178*4882a593Smuzhiyun .irq_set_affinity = sb1250_set_affinity
179*4882a593Smuzhiyun #endif
180*4882a593Smuzhiyun };
181*4882a593Smuzhiyun
init_sb1250_irqs(void)182*4882a593Smuzhiyun void __init init_sb1250_irqs(void)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun int i;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun for (i = 0; i < SB1250_NR_IRQS; i++) {
187*4882a593Smuzhiyun irq_set_chip_and_handler(i, &sb1250_irq_type,
188*4882a593Smuzhiyun handle_level_irq);
189*4882a593Smuzhiyun sb1250_irq_owner[i] = 0;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /*
195*4882a593Smuzhiyun * arch_init_irq is called early in the boot sequence from init/main.c via
196*4882a593Smuzhiyun * init_IRQ. It is responsible for setting up the interrupt mapper and
197*4882a593Smuzhiyun * installing the handler that will be responsible for dispatching interrupts
198*4882a593Smuzhiyun * to the "right" place.
199*4882a593Smuzhiyun */
200*4882a593Smuzhiyun /*
201*4882a593Smuzhiyun * For now, map all interrupts to IP[2]. We could save
202*4882a593Smuzhiyun * some cycles by parceling out system interrupts to different
203*4882a593Smuzhiyun * IP lines, but keep it simple for bringup. We'll also direct
204*4882a593Smuzhiyun * all interrupts to a single CPU; we should probably route
205*4882a593Smuzhiyun * PCI and LDT to one cpu and everything else to the other
206*4882a593Smuzhiyun * to balance the load a bit.
207*4882a593Smuzhiyun *
208*4882a593Smuzhiyun * On the second cpu, everything is set to IP5, which is
209*4882a593Smuzhiyun * ignored, EXCEPT the mailbox interrupt. That one is
210*4882a593Smuzhiyun * set to IP[2] so it is handled. This is needed so we
211*4882a593Smuzhiyun * can do cross-cpu function calls, as required by SMP
212*4882a593Smuzhiyun */
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun #define IMR_IP2_VAL K_INT_MAP_I0
215*4882a593Smuzhiyun #define IMR_IP3_VAL K_INT_MAP_I1
216*4882a593Smuzhiyun #define IMR_IP4_VAL K_INT_MAP_I2
217*4882a593Smuzhiyun #define IMR_IP5_VAL K_INT_MAP_I3
218*4882a593Smuzhiyun #define IMR_IP6_VAL K_INT_MAP_I4
219*4882a593Smuzhiyun
arch_init_irq(void)220*4882a593Smuzhiyun void __init arch_init_irq(void)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun unsigned int i;
224*4882a593Smuzhiyun u64 tmp;
225*4882a593Smuzhiyun unsigned int imask = STATUSF_IP4 | STATUSF_IP3 | STATUSF_IP2 |
226*4882a593Smuzhiyun STATUSF_IP1 | STATUSF_IP0;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /* Default everything to IP2 */
229*4882a593Smuzhiyun for (i = 0; i < SB1250_NR_IRQS; i++) { /* was I0 */
230*4882a593Smuzhiyun __raw_writeq(IMR_IP2_VAL,
231*4882a593Smuzhiyun IOADDR(A_IMR_REGISTER(0,
232*4882a593Smuzhiyun R_IMR_INTERRUPT_MAP_BASE) +
233*4882a593Smuzhiyun (i << 3)));
234*4882a593Smuzhiyun __raw_writeq(IMR_IP2_VAL,
235*4882a593Smuzhiyun IOADDR(A_IMR_REGISTER(1,
236*4882a593Smuzhiyun R_IMR_INTERRUPT_MAP_BASE) +
237*4882a593Smuzhiyun (i << 3)));
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun init_sb1250_irqs();
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /*
243*4882a593Smuzhiyun * Map the high 16 bits of the mailbox registers to IP[3], for
244*4882a593Smuzhiyun * inter-cpu messages
245*4882a593Smuzhiyun */
246*4882a593Smuzhiyun /* Was I1 */
247*4882a593Smuzhiyun __raw_writeq(IMR_IP3_VAL,
248*4882a593Smuzhiyun IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MAP_BASE) +
249*4882a593Smuzhiyun (K_INT_MBOX_0 << 3)));
250*4882a593Smuzhiyun __raw_writeq(IMR_IP3_VAL,
251*4882a593Smuzhiyun IOADDR(A_IMR_REGISTER(1, R_IMR_INTERRUPT_MAP_BASE) +
252*4882a593Smuzhiyun (K_INT_MBOX_0 << 3)));
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /* Clear the mailboxes. The firmware may leave them dirty */
255*4882a593Smuzhiyun __raw_writeq(0xffffffffffffffffULL,
256*4882a593Smuzhiyun IOADDR(A_IMR_REGISTER(0, R_IMR_MAILBOX_CLR_CPU)));
257*4882a593Smuzhiyun __raw_writeq(0xffffffffffffffffULL,
258*4882a593Smuzhiyun IOADDR(A_IMR_REGISTER(1, R_IMR_MAILBOX_CLR_CPU)));
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /* Mask everything except the mailbox registers for both cpus */
261*4882a593Smuzhiyun tmp = ~((u64) 0) ^ (((u64) 1) << K_INT_MBOX_0);
262*4882a593Smuzhiyun __raw_writeq(tmp, IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MASK)));
263*4882a593Smuzhiyun __raw_writeq(tmp, IOADDR(A_IMR_REGISTER(1, R_IMR_INTERRUPT_MASK)));
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun /*
266*4882a593Smuzhiyun * Note that the timer interrupts are also mapped, but this is
267*4882a593Smuzhiyun * done in sb1250_time_init(). Also, the profiling driver
268*4882a593Smuzhiyun * does its own management of IP7.
269*4882a593Smuzhiyun */
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /* Enable necessary IPs, disable the rest */
272*4882a593Smuzhiyun change_c0_status(ST0_IM, imask);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun extern void sb1250_mailbox_interrupt(void);
276*4882a593Smuzhiyun
dispatch_ip2(void)277*4882a593Smuzhiyun static inline void dispatch_ip2(void)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun unsigned int cpu = smp_processor_id();
280*4882a593Smuzhiyun unsigned long long mask;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun /*
283*4882a593Smuzhiyun * Default...we've hit an IP[2] interrupt, which means we've got to
284*4882a593Smuzhiyun * check the 1250 interrupt registers to figure out what to do. Need
285*4882a593Smuzhiyun * to detect which CPU we're on, now that smp_affinity is supported.
286*4882a593Smuzhiyun */
287*4882a593Smuzhiyun mask = __raw_readq(IOADDR(A_IMR_REGISTER(cpu,
288*4882a593Smuzhiyun R_IMR_INTERRUPT_STATUS_BASE)));
289*4882a593Smuzhiyun if (mask)
290*4882a593Smuzhiyun do_IRQ(fls64(mask) - 1);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun
plat_irq_dispatch(void)293*4882a593Smuzhiyun asmlinkage void plat_irq_dispatch(void)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun unsigned int cpu = smp_processor_id();
296*4882a593Smuzhiyun unsigned int pending;
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /*
299*4882a593Smuzhiyun * What a pain. We have to be really careful saving the upper 32 bits
300*4882a593Smuzhiyun * of any * register across function calls if we don't want them
301*4882a593Smuzhiyun * trashed--since were running in -o32, the calling routing never saves
302*4882a593Smuzhiyun * the full 64 bits of a register across a function call. Being the
303*4882a593Smuzhiyun * interrupt handler, we're guaranteed that interrupts are disabled
304*4882a593Smuzhiyun * during this code so we don't have to worry about random interrupts
305*4882a593Smuzhiyun * blasting the high 32 bits.
306*4882a593Smuzhiyun */
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun pending = read_c0_cause() & read_c0_status() & ST0_IM;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun if (pending & CAUSEF_IP7) /* CPU performance counter interrupt */
311*4882a593Smuzhiyun do_IRQ(MIPS_CPU_IRQ_BASE + 7);
312*4882a593Smuzhiyun else if (pending & CAUSEF_IP4)
313*4882a593Smuzhiyun do_IRQ(K_INT_TIMER_0 + cpu); /* sb1250_timer_interrupt() */
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun #ifdef CONFIG_SMP
316*4882a593Smuzhiyun else if (pending & CAUSEF_IP3)
317*4882a593Smuzhiyun sb1250_mailbox_interrupt();
318*4882a593Smuzhiyun #endif
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun else if (pending & CAUSEF_IP2)
321*4882a593Smuzhiyun dispatch_ip2();
322*4882a593Smuzhiyun else
323*4882a593Smuzhiyun spurious_interrupt();
324*4882a593Smuzhiyun }
325