blob: f01ba1f9077084852a051e53cb8d4624befedfa2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ip27-irq.c: Highlevel interrupt handling for IP27 architecture.
3 *
4 * Copyright (C) 1999, 2000 Ralf Baechle (ralf@gnu.org)
5 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
6 * Copyright (C) 1999 - 2001 Kanoj Sarcar
7 */
Ralf Baechled3ffd082005-08-08 12:42:26 +00008
9#undef DEBUG
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
12#include <linux/irq.h>
13#include <linux/errno.h>
14#include <linux/signal.h>
15#include <linux/sched.h>
16#include <linux/types.h>
17#include <linux/interrupt.h>
18#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/timex.h>
20#include <linux/slab.h>
21#include <linux/random.h>
22#include <linux/smp_lock.h>
Ralf Baechled3ffd082005-08-08 12:42:26 +000023#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel_stat.h>
25#include <linux/delay.h>
26#include <linux/bitops.h>
27
28#include <asm/bootinfo.h>
29#include <asm/io.h>
30#include <asm/mipsregs.h>
31#include <asm/system.h>
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/processor.h>
34#include <asm/pci/bridge.h>
35#include <asm/sn/addrs.h>
36#include <asm/sn/agent.h>
37#include <asm/sn/arch.h>
38#include <asm/sn/hub.h>
39#include <asm/sn/intr.h>
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
42 * Linux has a controller-independent x86 interrupt architecture.
43 * every controller has a 'controller-template', that is used
44 * by the main code to do the right thing. Each driver-visible
45 * interrupt source is transparently wired to the apropriate
46 * controller. Thus drivers need not be aware of the
47 * interrupt-controller.
48 *
49 * Various interrupt controllers we handle: 8259 PIC, SMP IO-APIC,
50 * PIIX4's internal 8259 PIC and SGI's Visual Workstation Cobalt (IO-)APIC.
51 * (IO-APICs assumed to be messaging to Pentium local-APICs)
52 *
53 * the code is designed to be easily extended with new/different
54 * interrupt controllers, without having to do assembly magic.
55 */
56
57extern asmlinkage void ip27_irq(void);
58
59extern struct bridge_controller *irq_to_bridge[];
60extern int irq_to_slot[];
61
62/*
63 * use these macros to get the encoded nasid and widget id
64 * from the irq value
65 */
66#define IRQ_TO_BRIDGE(i) irq_to_bridge[(i)]
67#define SLOT_FROM_PCI_IRQ(i) irq_to_slot[i]
68
69static inline int alloc_level(int cpu, int irq)
70{
Ralf Baechle4f12bfe2005-03-21 18:59:38 +000071 struct hub_data *hub = hub_data(cpu_to_node(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 struct slice_data *si = cpu_data[cpu].data;
Ralf Baechle4f12bfe2005-03-21 18:59:38 +000073 int level;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Ralf Baechle4f12bfe2005-03-21 18:59:38 +000075 level = find_first_zero_bit(hub->irq_alloc_mask, LEVELS_PER_SLICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (level >= LEVELS_PER_SLICE)
77 panic("Cpu %d flooded with devices\n", cpu);
78
Ralf Baechle4f12bfe2005-03-21 18:59:38 +000079 __set_bit(level, hub->irq_alloc_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 si->level_to_irq[level] = irq;
81
82 return level;
83}
84
85static inline int find_level(cpuid_t *cpunum, int irq)
86{
87 int cpu, i;
88
Andrew Morton394e3902006-03-23 03:01:05 -080089 for_each_online_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct slice_data *si = cpu_data[cpu].data;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 for (i = BASE_PCI_IRQ; i < LEVELS_PER_SLICE; i++)
93 if (si->level_to_irq[i] == irq) {
94 *cpunum = cpu;
95
96 return i;
97 }
98 }
99
100 panic("Could not identify cpu/level for irq %d\n", irq);
101}
102
103/*
104 * Find first bit set
105 */
106static int ms1bit(unsigned long x)
107{
108 int b = 0, s;
109
110 s = 16; if (x >> 16 == 0) s = 0; b += s; x >>= s;
111 s = 8; if (x >> 8 == 0) s = 0; b += s; x >>= s;
112 s = 4; if (x >> 4 == 0) s = 0; b += s; x >>= s;
113 s = 2; if (x >> 2 == 0) s = 0; b += s; x >>= s;
114 s = 1; if (x >> 1 == 0) s = 0; b += s;
115
116 return b;
117}
118
119/*
Thomas Gleixnerf40298f2006-07-01 19:29:20 -0700120 * This code is unnecessarily complex, because we do IRQF_DISABLED
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * intr enabling. Basically, once we grab the set of intrs we need
122 * to service, we must mask _all_ these interrupts; firstly, to make
123 * sure the same intr does not intr again, causing recursion that
124 * can lead to stack overflow. Secondly, we can not just mask the
125 * one intr we are do_IRQing, because the non-masked intrs in the
126 * first set might intr again, causing multiple servicings of the
127 * same intr. This effect is mostly seen for intercpu intrs.
128 * Kanoj 05.13.00
129 */
130
Ralf Baechle937a8012006-10-07 19:44:33 +0100131static void ip27_do_irq_mask0(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 int irq, swlevel;
134 hubreg_t pend0, mask0;
135 cpuid_t cpu = smp_processor_id();
136 int pi_int_mask0 =
137 (cputoslice(cpu) == 0) ? PI_INT_MASK0_A : PI_INT_MASK0_B;
138
139 /* copied from Irix intpend0() */
140 pend0 = LOCAL_HUB_L(PI_INT_PEND0);
141 mask0 = LOCAL_HUB_L(pi_int_mask0);
142
143 pend0 &= mask0; /* Pick intrs we should look at */
144 if (!pend0)
145 return;
146
147 swlevel = ms1bit(pend0);
148#ifdef CONFIG_SMP
149 if (pend0 & (1UL << CPU_RESCHED_A_IRQ)) {
150 LOCAL_HUB_CLR_INTR(CPU_RESCHED_A_IRQ);
151 } else if (pend0 & (1UL << CPU_RESCHED_B_IRQ)) {
152 LOCAL_HUB_CLR_INTR(CPU_RESCHED_B_IRQ);
153 } else if (pend0 & (1UL << CPU_CALL_A_IRQ)) {
154 LOCAL_HUB_CLR_INTR(CPU_CALL_A_IRQ);
155 smp_call_function_interrupt();
156 } else if (pend0 & (1UL << CPU_CALL_B_IRQ)) {
157 LOCAL_HUB_CLR_INTR(CPU_CALL_B_IRQ);
158 smp_call_function_interrupt();
159 } else
160#endif
161 {
162 /* "map" swlevel to irq */
163 struct slice_data *si = cpu_data[cpu].data;
164
165 irq = si->level_to_irq[swlevel];
Ralf Baechle937a8012006-10-07 19:44:33 +0100166 do_IRQ(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
169 LOCAL_HUB_L(PI_INT_PEND0);
170}
171
Ralf Baechle937a8012006-10-07 19:44:33 +0100172static void ip27_do_irq_mask1(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 int irq, swlevel;
175 hubreg_t pend1, mask1;
176 cpuid_t cpu = smp_processor_id();
177 int pi_int_mask1 = (cputoslice(cpu) == 0) ? PI_INT_MASK1_A : PI_INT_MASK1_B;
178 struct slice_data *si = cpu_data[cpu].data;
179
180 /* copied from Irix intpend0() */
181 pend1 = LOCAL_HUB_L(PI_INT_PEND1);
182 mask1 = LOCAL_HUB_L(pi_int_mask1);
183
184 pend1 &= mask1; /* Pick intrs we should look at */
185 if (!pend1)
186 return;
187
188 swlevel = ms1bit(pend1);
189 /* "map" swlevel to irq */
190 irq = si->level_to_irq[swlevel];
191 LOCAL_HUB_CLR_INTR(swlevel);
Ralf Baechle937a8012006-10-07 19:44:33 +0100192 do_IRQ(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 LOCAL_HUB_L(PI_INT_PEND1);
195}
196
Ralf Baechle937a8012006-10-07 19:44:33 +0100197static void ip27_prof_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 panic("CPU %d got a profiling interrupt", smp_processor_id());
200}
201
Ralf Baechle937a8012006-10-07 19:44:33 +0100202static void ip27_hub_error(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 panic("CPU %d got a hub error interrupt", smp_processor_id());
205}
206
207static int intr_connect_level(int cpu, int bit)
208{
209 nasid_t nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
210 struct slice_data *si = cpu_data[cpu].data;
Ralf Baechle4f12bfe2005-03-21 18:59:38 +0000211 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Ralf Baechle4f12bfe2005-03-21 18:59:38 +0000213 set_bit(bit, si->irq_enable_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Ralf Baechle4f12bfe2005-03-21 18:59:38 +0000215 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (!cputoslice(cpu)) {
217 REMOTE_HUB_S(nasid, PI_INT_MASK0_A, si->irq_enable_mask[0]);
218 REMOTE_HUB_S(nasid, PI_INT_MASK1_A, si->irq_enable_mask[1]);
219 } else {
220 REMOTE_HUB_S(nasid, PI_INT_MASK0_B, si->irq_enable_mask[0]);
221 REMOTE_HUB_S(nasid, PI_INT_MASK1_B, si->irq_enable_mask[1]);
222 }
Ralf Baechle4f12bfe2005-03-21 18:59:38 +0000223 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 return 0;
226}
227
228static int intr_disconnect_level(int cpu, int bit)
229{
230 nasid_t nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
231 struct slice_data *si = cpu_data[cpu].data;
232
Ralf Baechle4f12bfe2005-03-21 18:59:38 +0000233 clear_bit(bit, si->irq_enable_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 if (!cputoslice(cpu)) {
236 REMOTE_HUB_S(nasid, PI_INT_MASK0_A, si->irq_enable_mask[0]);
237 REMOTE_HUB_S(nasid, PI_INT_MASK1_A, si->irq_enable_mask[1]);
238 } else {
239 REMOTE_HUB_S(nasid, PI_INT_MASK0_B, si->irq_enable_mask[0]);
240 REMOTE_HUB_S(nasid, PI_INT_MASK1_B, si->irq_enable_mask[1]);
241 }
242
243 return 0;
244}
245
246/* Startup one of the (PCI ...) IRQs routes over a bridge. */
247static unsigned int startup_bridge_irq(unsigned int irq)
248{
249 struct bridge_controller *bc;
250 bridgereg_t device;
251 bridge_t *bridge;
252 int pin, swlevel;
253 cpuid_t cpu;
254
255 pin = SLOT_FROM_PCI_IRQ(irq);
256 bc = IRQ_TO_BRIDGE(irq);
257 bridge = bc->base;
258
Ralf Baechled3ffd082005-08-08 12:42:26 +0000259 pr_debug("bridge_startup(): irq= 0x%x pin=%d\n", irq, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /*
261 * "map" irq to a swlevel greater than 6 since the first 6 bits
262 * of INT_PEND0 are taken
263 */
264 swlevel = find_level(&cpu, irq);
265 bridge->b_int_addr[pin].addr = (0x20000 | swlevel | (bc->nasid << 8));
266 bridge->b_int_enable |= (1 << pin);
267 bridge->b_int_enable |= 0x7ffffe00; /* more stuff in int_enable */
268
269 /*
270 * Enable sending of an interrupt clear packt to the hub on a high to
271 * low transition of the interrupt pin.
272 *
273 * IRIX sets additional bits in the address which are documented as
274 * reserved in the bridge docs.
275 */
276 bridge->b_int_mode |= (1UL << pin);
277
278 /*
279 * We assume the bridge to have a 1:1 mapping between devices
280 * (slots) and intr pins.
281 */
282 device = bridge->b_int_device;
283 device &= ~(7 << (pin*3));
284 device |= (pin << (pin*3));
285 bridge->b_int_device = device;
286
287 bridge->b_wid_tflush;
288
289 return 0; /* Never anything pending. */
290}
291
292/* Shutdown one of the (PCI ...) IRQs routes over a bridge. */
293static void shutdown_bridge_irq(unsigned int irq)
294{
295 struct bridge_controller *bc = IRQ_TO_BRIDGE(irq);
Ralf Baechle4f12bfe2005-03-21 18:59:38 +0000296 struct hub_data *hub = hub_data(cpu_to_node(bc->irq_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 bridge_t *bridge = bc->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int pin, swlevel;
299 cpuid_t cpu;
300
Ralf Baechled3ffd082005-08-08 12:42:26 +0000301 pr_debug("bridge_shutdown: irq 0x%x\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 pin = SLOT_FROM_PCI_IRQ(irq);
303
304 /*
305 * map irq to a swlevel greater than 6 since the first 6 bits
306 * of INT_PEND0 are taken
307 */
308 swlevel = find_level(&cpu, irq);
309 intr_disconnect_level(cpu, swlevel);
310
Ralf Baechle4f12bfe2005-03-21 18:59:38 +0000311 __clear_bit(swlevel, hub->irq_alloc_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 bridge->b_int_enable &= ~(1 << pin);
314 bridge->b_wid_tflush;
315}
316
317static inline void enable_bridge_irq(unsigned int irq)
318{
319 cpuid_t cpu;
320 int swlevel;
321
322 swlevel = find_level(&cpu, irq); /* Criminal offence */
323 intr_connect_level(cpu, swlevel);
324}
325
326static inline void disable_bridge_irq(unsigned int irq)
327{
328 cpuid_t cpu;
329 int swlevel;
330
331 swlevel = find_level(&cpu, irq); /* Criminal offence */
332 intr_disconnect_level(cpu, swlevel);
333}
334
335static void mask_and_ack_bridge_irq(unsigned int irq)
336{
337 disable_bridge_irq(irq);
338}
339
340static void end_bridge_irq(unsigned int irq)
341{
342 if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)) &&
343 irq_desc[irq].action)
344 enable_bridge_irq(irq);
345}
346
Ralf Baechle94dee172006-07-02 14:41:42 +0100347static struct irq_chip bridge_irq_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 .typename = "bridge",
349 .startup = startup_bridge_irq,
350 .shutdown = shutdown_bridge_irq,
351 .enable = enable_bridge_irq,
352 .disable = disable_bridge_irq,
353 .ack = mask_and_ack_bridge_irq,
354 .end = end_bridge_irq,
355};
356
357static unsigned long irq_map[NR_IRQS / BITS_PER_LONG];
358
Ralf Baechle3c009442006-06-16 17:10:49 +0200359int allocate_irqno(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 int irq;
362
363again:
364 irq = find_first_zero_bit(irq_map, NR_IRQS);
365
366 if (irq >= NR_IRQS)
367 return -ENOSPC;
368
369 if (test_and_set_bit(irq, irq_map))
370 goto again;
371
372 return irq;
373}
374
375void free_irqno(unsigned int irq)
376{
377 clear_bit(irq, irq_map);
378}
379
380void __devinit register_bridge_irq(unsigned int irq)
381{
382 irq_desc[irq].status = IRQ_DISABLED;
383 irq_desc[irq].action = 0;
384 irq_desc[irq].depth = 1;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700385 irq_desc[irq].chip = &bridge_irq_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388int __devinit request_bridge_irq(struct bridge_controller *bc)
389{
390 int irq = allocate_irqno();
391 int swlevel, cpu;
392 nasid_t nasid;
393
394 if (irq < 0)
395 return irq;
396
397 /*
398 * "map" irq to a swlevel greater than 6 since the first 6 bits
399 * of INT_PEND0 are taken
400 */
401 cpu = bc->irq_cpu;
402 swlevel = alloc_level(cpu, irq);
403 if (unlikely(swlevel < 0)) {
404 free_irqno(irq);
405
406 return -EAGAIN;
407 }
408
409 /* Make sure it's not already pending when we connect it. */
410 nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
411 REMOTE_HUB_CLR_INTR(nasid, swlevel);
412
413 intr_connect_level(cpu, swlevel);
414
415 register_bridge_irq(irq);
416
417 return irq;
418}
419
Ralf Baechle937a8012006-10-07 19:44:33 +0100420extern void ip27_rt_timer_interrupt(void);
Ralf Baechlee4ac58a2006-04-03 17:56:36 +0100421
Ralf Baechle937a8012006-10-07 19:44:33 +0100422asmlinkage void plat_irq_dispatch(void)
Ralf Baechlee4ac58a2006-04-03 17:56:36 +0100423{
424 unsigned long pending = read_c0_cause() & read_c0_status();
425
426 if (pending & CAUSEF_IP4)
Ralf Baechle937a8012006-10-07 19:44:33 +0100427 ip27_rt_timer_interrupt();
Ralf Baechlee4ac58a2006-04-03 17:56:36 +0100428 else if (pending & CAUSEF_IP2) /* PI_INT_PEND_0 or CC_PEND_{A|B} */
Ralf Baechle937a8012006-10-07 19:44:33 +0100429 ip27_do_irq_mask0();
Ralf Baechlee4ac58a2006-04-03 17:56:36 +0100430 else if (pending & CAUSEF_IP3) /* PI_INT_PEND_1 */
Ralf Baechle937a8012006-10-07 19:44:33 +0100431 ip27_do_irq_mask1();
Ralf Baechlee4ac58a2006-04-03 17:56:36 +0100432 else if (pending & CAUSEF_IP5)
Ralf Baechle937a8012006-10-07 19:44:33 +0100433 ip27_prof_timer();
Ralf Baechlee4ac58a2006-04-03 17:56:36 +0100434 else if (pending & CAUSEF_IP6)
Ralf Baechle937a8012006-10-07 19:44:33 +0100435 ip27_hub_error();
Ralf Baechlee4ac58a2006-04-03 17:56:36 +0100436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438void __init arch_init_irq(void)
439{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442void install_ipi(void)
443{
444 int slice = LOCAL_HUB_L(PI_CPU_NUM);
445 int cpu = smp_processor_id();
446 struct slice_data *si = cpu_data[cpu].data;
Ralf Baechle4f12bfe2005-03-21 18:59:38 +0000447 struct hub_data *hub = hub_data(cpu_to_node(cpu));
448 int resched, call;
449
450 resched = CPU_RESCHED_A_IRQ + slice;
451 __set_bit(resched, hub->irq_alloc_mask);
452 __set_bit(resched, si->irq_enable_mask);
453 LOCAL_HUB_CLR_INTR(resched);
454
455 call = CPU_CALL_A_IRQ + slice;
456 __set_bit(call, hub->irq_alloc_mask);
457 __set_bit(call, si->irq_enable_mask);
458 LOCAL_HUB_CLR_INTR(call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 if (slice == 0) {
Ralf Baechle4f12bfe2005-03-21 18:59:38 +0000461 LOCAL_HUB_S(PI_INT_MASK0_A, si->irq_enable_mask[0]);
462 LOCAL_HUB_S(PI_INT_MASK1_A, si->irq_enable_mask[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 } else {
Ralf Baechle4f12bfe2005-03-21 18:59:38 +0000464 LOCAL_HUB_S(PI_INT_MASK0_B, si->irq_enable_mask[0]);
465 LOCAL_HUB_S(PI_INT_MASK1_B, si->irq_enable_mask[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467}