2 * Copyright (C) 2000,2001,2002,2003,2004 Broadcom Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/linkage.h>
21 #include <linux/interrupt.h>
22 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/kernel_stat.h>
27 #include <asm/errno.h>
28 #include <asm/irq_regs.h>
29 #include <asm/signal.h>
30 #include <asm/system.h>
33 #include <asm/sibyte/bcm1480_regs.h>
34 #include <asm/sibyte/bcm1480_int.h>
35 #include <asm/sibyte/bcm1480_scd.h>
37 #include <asm/sibyte/sb1250_uart.h>
38 #include <asm/sibyte/sb1250.h>
41 * These are the routines that handle all the low level interrupt stuff.
42 * Actions handled here are: initialization of the interrupt map, requesting of
43 * interrupt lines by handlers, dispatching if interrupts to handlers, probing
48 static void end_bcm1480_irq(unsigned int irq);
49 static void enable_bcm1480_irq(unsigned int irq);
50 static void disable_bcm1480_irq(unsigned int irq);
51 static void ack_bcm1480_irq(unsigned int irq);
53 static void bcm1480_set_affinity(unsigned int irq, cpumask_t mask);
57 extern unsigned long ht_eoi_space;
61 #include <asm/gdb-stub.h>
62 extern void breakpoint(void);
64 #ifdef CONFIG_GDB_CONSOLE
65 extern void register_gdb_console(void);
68 /* kgdb is on when configured. Pass "nokgdb" kernel arg to turn it off */
69 static int kgdb_flag = 1;
70 static int __init nokgdb(char *str)
75 __setup("nokgdb", nokgdb);
77 /* Default to UART1 */
79 #ifdef CONFIG_SERIAL_SB1250_DUART
80 extern char sb1250_duart_present[];
84 static struct irq_chip bcm1480_irq_type = {
85 .name = "BCM1480-IMR",
86 .ack = ack_bcm1480_irq,
87 .mask = disable_bcm1480_irq,
88 .mask_ack = ack_bcm1480_irq,
89 .unmask = enable_bcm1480_irq,
90 .end = end_bcm1480_irq,
92 .set_affinity = bcm1480_set_affinity
96 /* Store the CPU id (not the logical number) */
97 int bcm1480_irq_owner[BCM1480_NR_IRQS];
99 DEFINE_SPINLOCK(bcm1480_imr_lock);
101 void bcm1480_mask_irq(int cpu, int irq)
103 unsigned long flags, hl_spacing;
106 spin_lock_irqsave(&bcm1480_imr_lock, flags);
108 if ((irq >= BCM1480_NR_IRQS_HALF) && (irq <= BCM1480_NR_IRQS)) {
109 hl_spacing = BCM1480_IMR_HL_SPACING;
110 irq -= BCM1480_NR_IRQS_HALF;
112 cur_ints = ____raw_readq(IOADDR(A_BCM1480_IMR_MAPPER(cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + hl_spacing));
113 cur_ints |= (((u64) 1) << irq);
114 ____raw_writeq(cur_ints, IOADDR(A_BCM1480_IMR_MAPPER(cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + hl_spacing));
115 spin_unlock_irqrestore(&bcm1480_imr_lock, flags);
118 void bcm1480_unmask_irq(int cpu, int irq)
120 unsigned long flags, hl_spacing;
123 spin_lock_irqsave(&bcm1480_imr_lock, flags);
125 if ((irq >= BCM1480_NR_IRQS_HALF) && (irq <= BCM1480_NR_IRQS)) {
126 hl_spacing = BCM1480_IMR_HL_SPACING;
127 irq -= BCM1480_NR_IRQS_HALF;
129 cur_ints = ____raw_readq(IOADDR(A_BCM1480_IMR_MAPPER(cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + hl_spacing));
130 cur_ints &= ~(((u64) 1) << irq);
131 ____raw_writeq(cur_ints, IOADDR(A_BCM1480_IMR_MAPPER(cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + hl_spacing));
132 spin_unlock_irqrestore(&bcm1480_imr_lock, flags);
136 static void bcm1480_set_affinity(unsigned int irq, cpumask_t mask)
138 int i = 0, old_cpu, cpu, int_on, k;
140 struct irq_desc *desc = irq_desc + irq;
142 unsigned int irq_dirty;
144 if (cpus_weight(mask) != 1) {
145 printk("attempted to set irq affinity for irq %d to multiple CPUs\n", irq);
150 /* Convert logical CPU to physical CPU */
151 cpu = cpu_logical_map(i);
153 /* Protect against other affinity changers and IMR manipulation */
154 spin_lock_irqsave(&desc->lock, flags);
155 spin_lock(&bcm1480_imr_lock);
157 /* Swizzle each CPU's IMR (but leave the IP selection alone) */
158 old_cpu = bcm1480_irq_owner[irq];
160 if ((irq_dirty >= BCM1480_NR_IRQS_HALF) && (irq_dirty <= BCM1480_NR_IRQS)) {
161 irq_dirty -= BCM1480_NR_IRQS_HALF;
164 for (k=0; k<2; k++) { /* Loop through high and low interrupt mask register */
165 cur_ints = ____raw_readq(IOADDR(A_BCM1480_IMR_MAPPER(old_cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + (k*BCM1480_IMR_HL_SPACING)));
166 int_on = !(cur_ints & (((u64) 1) << irq_dirty));
168 /* If it was on, mask it */
169 cur_ints |= (((u64) 1) << irq_dirty);
170 ____raw_writeq(cur_ints, IOADDR(A_BCM1480_IMR_MAPPER(old_cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + (k*BCM1480_IMR_HL_SPACING)));
172 bcm1480_irq_owner[irq] = cpu;
174 /* unmask for the new CPU */
175 cur_ints = ____raw_readq(IOADDR(A_BCM1480_IMR_MAPPER(cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + (k*BCM1480_IMR_HL_SPACING)));
176 cur_ints &= ~(((u64) 1) << irq_dirty);
177 ____raw_writeq(cur_ints, IOADDR(A_BCM1480_IMR_MAPPER(cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + (k*BCM1480_IMR_HL_SPACING)));
180 spin_unlock(&bcm1480_imr_lock);
181 spin_unlock_irqrestore(&desc->lock, flags);
186 /*****************************************************************************/
188 static void disable_bcm1480_irq(unsigned int irq)
190 bcm1480_mask_irq(bcm1480_irq_owner[irq], irq);
193 static void enable_bcm1480_irq(unsigned int irq)
195 bcm1480_unmask_irq(bcm1480_irq_owner[irq], irq);
199 static void ack_bcm1480_irq(unsigned int irq)
202 unsigned int irq_dirty;
206 * If the interrupt was an HT interrupt, now is the time to
207 * clear it. NOTE: we assume the HT bridge was set up to
208 * deliver the interrupts to all CPUs (which makes affinity
209 * changing easier for us)
212 if ((irq_dirty >= BCM1480_NR_IRQS_HALF) && (irq_dirty <= BCM1480_NR_IRQS)) {
213 irq_dirty -= BCM1480_NR_IRQS_HALF;
215 for (k=0; k<2; k++) { /* Loop through high and low LDT interrupts */
216 pending = __raw_readq(IOADDR(A_BCM1480_IMR_REGISTER(bcm1480_irq_owner[irq],
217 R_BCM1480_IMR_LDT_INTERRUPT_H + (k*BCM1480_IMR_HL_SPACING))));
218 pending &= ((u64)1 << (irq_dirty));
222 for (i=0; i<NR_CPUS; i++) {
224 * Clear for all CPUs so an affinity switch
225 * doesn't find an old status
227 __raw_writeq(pending, IOADDR(A_BCM1480_IMR_REGISTER(cpu_logical_map(i),
228 R_BCM1480_IMR_LDT_INTERRUPT_CLR_H + (k*BCM1480_IMR_HL_SPACING))));
231 __raw_writeq(pending, IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_LDT_INTERRUPT_CLR_H + (k*BCM1480_IMR_HL_SPACING))));
235 * Generate EOI. For Pass 1 parts, EOI is a nop. For
236 * Pass 2, the LDT world may be edge-triggered, but
237 * this EOI shouldn't hurt. If they are
238 * level-sensitive, the EOI is required.
242 *(uint32_t *)(ht_eoi_space+(irq<<16)+(7<<2)) = 0;
246 bcm1480_mask_irq(bcm1480_irq_owner[irq], irq);
250 static void end_bcm1480_irq(unsigned int irq)
252 if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
253 bcm1480_unmask_irq(bcm1480_irq_owner[irq], irq);
258 void __init init_bcm1480_irqs(void)
262 for (i = 0; i < BCM1480_NR_IRQS; i++) {
263 set_irq_chip(i, &bcm1480_irq_type);
264 bcm1480_irq_owner[i] = 0;
269 * init_IRQ is called early in the boot sequence from init/main.c. It
270 * is responsible for setting up the interrupt mapper and installing the
271 * handler that will be responsible for dispatching interrupts to the
275 * For now, map all interrupts to IP[2]. We could save
276 * some cycles by parceling out system interrupts to different
277 * IP lines, but keep it simple for bringup. We'll also direct
278 * all interrupts to a single CPU; we should probably route
279 * PCI and LDT to one cpu and everything else to the other
280 * to balance the load a bit.
282 * On the second cpu, everything is set to IP5, which is
283 * ignored, EXCEPT the mailbox interrupt. That one is
284 * set to IP[2] so it is handled. This is needed so we
285 * can do cross-cpu function calls, as requred by SMP
288 #define IMR_IP2_VAL K_BCM1480_INT_MAP_I0
289 #define IMR_IP3_VAL K_BCM1480_INT_MAP_I1
290 #define IMR_IP4_VAL K_BCM1480_INT_MAP_I2
291 #define IMR_IP5_VAL K_BCM1480_INT_MAP_I3
292 #define IMR_IP6_VAL K_BCM1480_INT_MAP_I4
294 void __init arch_init_irq(void)
298 unsigned int imask = STATUSF_IP4 | STATUSF_IP3 | STATUSF_IP2 |
299 STATUSF_IP1 | STATUSF_IP0;
301 /* Default everything to IP2 */
302 /* Start with _high registers which has no bit 0 interrupt source */
303 for (i = 1; i < BCM1480_NR_IRQS_HALF; i++) { /* was I0 */
304 for (cpu = 0; cpu < 4; cpu++) {
305 __raw_writeq(IMR_IP2_VAL,
306 IOADDR(A_BCM1480_IMR_REGISTER(cpu,
307 R_BCM1480_IMR_INTERRUPT_MAP_BASE_H) + (i << 3)));
311 /* Now do _low registers */
312 for (i = 0; i < BCM1480_NR_IRQS_HALF; i++) {
313 for (cpu = 0; cpu < 4; cpu++) {
314 __raw_writeq(IMR_IP2_VAL,
315 IOADDR(A_BCM1480_IMR_REGISTER(cpu,
316 R_BCM1480_IMR_INTERRUPT_MAP_BASE_L) + (i << 3)));
323 * Map the high 16 bits of mailbox_0 registers to IP[3], for
327 for (cpu = 0; cpu < 4; cpu++) {
328 __raw_writeq(IMR_IP3_VAL, IOADDR(A_BCM1480_IMR_REGISTER(cpu, R_BCM1480_IMR_INTERRUPT_MAP_BASE_H) +
329 (K_BCM1480_INT_MBOX_0_0 << 3)));
333 /* Clear the mailboxes. The firmware may leave them dirty */
334 for (cpu = 0; cpu < 4; cpu++) {
335 __raw_writeq(0xffffffffffffffffULL,
336 IOADDR(A_BCM1480_IMR_REGISTER(cpu, R_BCM1480_IMR_MAILBOX_0_CLR_CPU)));
337 __raw_writeq(0xffffffffffffffffULL,
338 IOADDR(A_BCM1480_IMR_REGISTER(cpu, R_BCM1480_IMR_MAILBOX_1_CLR_CPU)));
342 /* Mask everything except the high 16 bit of mailbox_0 registers for all cpus */
343 tmp = ~((u64) 0) ^ ( (((u64) 1) << K_BCM1480_INT_MBOX_0_0));
344 for (cpu = 0; cpu < 4; cpu++) {
345 __raw_writeq(tmp, IOADDR(A_BCM1480_IMR_REGISTER(cpu, R_BCM1480_IMR_INTERRUPT_MASK_H)));
348 for (cpu = 0; cpu < 4; cpu++) {
349 __raw_writeq(tmp, IOADDR(A_BCM1480_IMR_REGISTER(cpu, R_BCM1480_IMR_INTERRUPT_MASK_L)));
353 * Note that the timer interrupts are also mapped, but this is
354 * done in bcm1480_time_init(). Also, the profiling driver
355 * does its own management of IP7.
359 imask |= STATUSF_IP6;
361 /* Enable necessary IPs, disable the rest */
362 change_c0_status(ST0_IM, imask);
366 kgdb_irq = K_BCM1480_INT_UART_0 + kgdb_port;
368 #ifdef CONFIG_SERIAL_SB1250_DUART
369 sb1250_duart_present[kgdb_port] = 0;
371 /* Setup uart 1 settings, mapper */
373 __raw_writeq(M_DUART_IMR_BRK, IOADDR(A_DUART_IMRREG(kgdb_port)));
375 __raw_writeq(IMR_IP6_VAL,
376 IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_INTERRUPT_MAP_BASE_H) +
378 bcm1480_unmask_irq(0, kgdb_irq);
380 #ifdef CONFIG_GDB_CONSOLE
381 register_gdb_console();
383 printk("Waiting for GDB on UART port %d\n", kgdb_port);
392 #include <linux/delay.h>
394 #define duart_out(reg, val) csr_out32(val, IOADDR(A_DUART_CHANREG(kgdb_port, reg)))
395 #define duart_in(reg) csr_in32(IOADDR(A_DUART_CHANREG(kgdb_port, reg)))
397 static void bcm1480_kgdb_interrupt(void)
400 * Clear break-change status (allow some time for the remote
401 * host to stop the break, since we would see another
402 * interrupt on the end-of-break too)
404 kstat.irqs[smp_processor_id()][kgdb_irq]++;
406 duart_out(R_DUART_CMD, V_DUART_MISC_CMD_RESET_BREAK_INT |
407 M_DUART_RX_EN | M_DUART_TX_EN);
408 set_async_breakpoint(&get_irq_regs()->cp0_epc);
411 #endif /* CONFIG_KGDB */
413 extern void bcm1480_mailbox_interrupt(void);
415 static inline void dispatch_ip2(void)
417 unsigned long long mask_h, mask_l;
418 unsigned int cpu = smp_processor_id();
422 * Default...we've hit an IP[2] interrupt, which means we've got to
423 * check the 1480 interrupt registers to figure out what to do. Need
424 * to detect which CPU we're on, now that smp_affinity is supported.
426 base = A_BCM1480_IMR_MAPPER(cpu);
427 mask_h = __raw_readq(
428 IOADDR(base + R_BCM1480_IMR_INTERRUPT_STATUS_BASE_H));
429 mask_l = __raw_readq(
430 IOADDR(base + R_BCM1480_IMR_INTERRUPT_STATUS_BASE_L));
434 do_IRQ(fls64(mask_h) - 1);
436 do_IRQ(63 + fls64(mask_l));
440 asmlinkage void plat_irq_dispatch(void)
442 unsigned int cpu = smp_processor_id();
443 unsigned int pending;
445 #ifdef CONFIG_SIBYTE_BCM1480_PROF
446 /* Set compare to count to silence count/compare timer interrupts */
447 write_c0_compare(read_c0_count());
450 pending = read_c0_cause() & read_c0_status();
452 #ifdef CONFIG_SIBYTE_BCM1480_PROF
453 if (pending & CAUSEF_IP7) /* Cpu performance counter interrupt */
458 if (pending & CAUSEF_IP4)
459 do_IRQ(K_BCM1480_INT_TIMER_0 + cpu);
461 else if (pending & CAUSEF_IP3)
462 bcm1480_mailbox_interrupt();
466 else if (pending & CAUSEF_IP6)
467 bcm1480_kgdb_interrupt(); /* KGDB (uart 1) */
470 else if (pending & CAUSEF_IP2)