blob: cbf8ee81cf5b3bef136ff38138317a7224bc7468 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: sun4d_irq.c,v 1.29 2001/12/11 04:55:51 davem Exp $
2 * arch/sparc/kernel/sun4d_irq.c:
3 * SS1000/SC2000 interrupt handling.
4 *
5 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 * Heavily based on arch/sparc/kernel/irq.c.
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/errno.h>
10#include <linux/linkage.h>
11#include <linux/kernel_stat.h>
12#include <linux/signal.h>
13#include <linux/sched.h>
14#include <linux/ptrace.h>
15#include <linux/interrupt.h>
16#include <linux/slab.h>
17#include <linux/random.h>
18#include <linux/init.h>
19#include <linux/smp.h>
20#include <linux/smp_lock.h>
21#include <linux/spinlock.h>
22#include <linux/seq_file.h>
23
24#include <asm/ptrace.h>
25#include <asm/processor.h>
26#include <asm/system.h>
27#include <asm/psr.h>
28#include <asm/smp.h>
29#include <asm/vaddrs.h>
30#include <asm/timer.h>
31#include <asm/openprom.h>
32#include <asm/oplib.h>
33#include <asm/traps.h>
34#include <asm/irq.h>
35#include <asm/io.h>
36#include <asm/pgalloc.h>
37#include <asm/pgtable.h>
38#include <asm/sbus.h>
39#include <asm/sbi.h>
40#include <asm/cacheflush.h>
41
42/* If you trust current SCSI layer to handle different SCSI IRQs, enable this. I don't trust it... -jj */
43/* #define DISTRIBUTE_IRQS */
44
45struct sun4d_timer_regs *sun4d_timers;
46#define TIMER_IRQ 10
47
48#define MAX_STATIC_ALLOC 4
49extern struct irqaction static_irqaction[MAX_STATIC_ALLOC];
50extern int static_irq_count;
51unsigned char cpu_leds[32];
52#ifdef CONFIG_SMP
53unsigned char sbus_tid[32];
54#endif
55
Bob Breuera54123e2006-03-23 22:36:19 -080056static struct irqaction *irq_action[NR_IRQS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070057extern spinlock_t irq_action_lock;
58
59struct sbus_action {
60 struct irqaction *action;
61 /* For SMP this needs to be extended */
62} *sbus_actions;
63
64static int pil_to_sbus[] = {
65 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
66};
67
68static int sbus_to_pil[] = {
69 0, 2, 3, 5, 7, 9, 11, 13,
70};
71
72static int nsbi;
73#ifdef CONFIG_SMP
74DEFINE_SPINLOCK(sun4d_imsk_lock);
75#endif
76
77int show_sun4d_interrupts(struct seq_file *p, void *v)
78{
79 int i = *(loff_t *) v, j = 0, k = 0, sbusl;
80 struct irqaction * action;
81 unsigned long flags;
82#ifdef CONFIG_SMP
83 int x;
84#endif
85
86 spin_lock_irqsave(&irq_action_lock, flags);
87 if (i < NR_IRQS) {
88 sbusl = pil_to_sbus[i];
89 if (!sbusl) {
90 action = *(i + irq_action);
91 if (!action)
92 goto out_unlock;
93 } else {
94 for (j = 0; j < nsbi; j++) {
95 for (k = 0; k < 4; k++)
96 if ((action = sbus_actions [(j << 5) + (sbusl << 2) + k].action))
97 goto found_it;
98 }
99 goto out_unlock;
100 }
101found_it: seq_printf(p, "%3d: ", i);
102#ifndef CONFIG_SMP
103 seq_printf(p, "%10u ", kstat_irqs(i));
104#else
Andrew Morton394e3902006-03-23 03:01:05 -0800105 for_each_online_cpu(x)
106 seq_printf(p, "%10u ",
107 kstat_cpu(cpu_logical_map(x)).irqs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#endif
109 seq_printf(p, "%c %s",
110 (action->flags & SA_INTERRUPT) ? '+' : ' ',
111 action->name);
112 action = action->next;
113 for (;;) {
114 for (; action; action = action->next) {
115 seq_printf(p, ",%s %s",
116 (action->flags & SA_INTERRUPT) ? " +" : "",
117 action->name);
118 }
119 if (!sbusl) break;
120 k++;
121 if (k < 4)
122 action = sbus_actions [(j << 5) + (sbusl << 2) + k].action;
123 else {
124 j++;
125 if (j == nsbi) break;
126 k = 0;
127 action = sbus_actions [(j << 5) + (sbusl << 2)].action;
128 }
129 }
130 seq_putc(p, '\n');
131 }
132out_unlock:
133 spin_unlock_irqrestore(&irq_action_lock, flags);
134 return 0;
135}
136
137void sun4d_free_irq(unsigned int irq, void *dev_id)
138{
139 struct irqaction *action, **actionp;
140 struct irqaction *tmp = NULL;
141 unsigned long flags;
142
143 spin_lock_irqsave(&irq_action_lock, flags);
144 if (irq < 15)
145 actionp = irq + irq_action;
146 else
147 actionp = &(sbus_actions[irq - (1 << 5)].action);
148 action = *actionp;
149 if (!action) {
150 printk("Trying to free free IRQ%d\n",irq);
151 goto out_unlock;
152 }
153 if (dev_id) {
154 for (; action; action = action->next) {
155 if (action->dev_id == dev_id)
156 break;
157 tmp = action;
158 }
159 if (!action) {
160 printk("Trying to free free shared IRQ%d\n",irq);
161 goto out_unlock;
162 }
163 } else if (action->flags & SA_SHIRQ) {
164 printk("Trying to free shared IRQ%d with NULL device ID\n", irq);
165 goto out_unlock;
166 }
167 if (action->flags & SA_STATIC_ALLOC)
168 {
169 /* This interrupt is marked as specially allocated
170 * so it is a bad idea to free it.
171 */
172 printk("Attempt to free statically allocated IRQ%d (%s)\n",
173 irq, action->name);
174 goto out_unlock;
175 }
176
177 if (action && tmp)
178 tmp->next = action->next;
179 else
180 *actionp = action->next;
181
182 spin_unlock_irqrestore(&irq_action_lock, flags);
183
184 synchronize_irq(irq);
185
186 spin_lock_irqsave(&irq_action_lock, flags);
187
188 kfree(action);
189
190 if (!(*actionp))
191 disable_irq(irq);
192
193out_unlock:
194 spin_unlock_irqrestore(&irq_action_lock, flags);
195}
196
197extern void unexpected_irq(int, void *, struct pt_regs *);
198
199void sun4d_handler_irq(int irq, struct pt_regs * regs)
200{
201 struct irqaction * action;
202 int cpu = smp_processor_id();
203 /* SBUS IRQ level (1 - 7) */
204 int sbusl = pil_to_sbus[irq];
205
206 /* FIXME: Is this necessary?? */
207 cc_get_ipen();
208
209 cc_set_iclr(1 << irq);
210
211 irq_enter();
212 kstat_cpu(cpu).irqs[irq]++;
213 if (!sbusl) {
214 action = *(irq + irq_action);
215 if (!action)
216 unexpected_irq(irq, NULL, regs);
217 do {
218 action->handler(irq, action->dev_id, regs);
219 action = action->next;
220 } while (action);
221 } else {
222 int bus_mask = bw_get_intr_mask(sbusl) & 0x3ffff;
223 int sbino;
224 struct sbus_action *actionp;
225 unsigned mask, slot;
226 int sbil = (sbusl << 2);
227
228 bw_clear_intr_mask(sbusl, bus_mask);
229
230 /* Loop for each pending SBI */
231 for (sbino = 0; bus_mask; sbino++, bus_mask >>= 1)
232 if (bus_mask & 1) {
233 mask = acquire_sbi(SBI2DEVID(sbino), 0xf << sbil);
234 mask &= (0xf << sbil);
235 actionp = sbus_actions + (sbino << 5) + (sbil);
236 /* Loop for each pending SBI slot */
237 for (slot = (1 << sbil); mask; slot <<= 1, actionp++)
238 if (mask & slot) {
239 mask &= ~slot;
240 action = actionp->action;
241
242 if (!action)
243 unexpected_irq(irq, NULL, regs);
244 do {
245 action->handler(irq, action->dev_id, regs);
246 action = action->next;
247 } while (action);
248 release_sbi(SBI2DEVID(sbino), slot);
249 }
250 }
251 }
252 irq_exit();
253}
254
255unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq)
256{
257 int sbusl = pil_to_sbus[irq];
258
259 if (sbusl)
260 return ((sdev->bus->board + 1) << 5) + (sbusl << 2) + sdev->slot;
261 else
262 return irq;
263}
264
265unsigned int sun4d_sbint_to_irq(struct sbus_dev *sdev, unsigned int sbint)
266{
267 if (sbint >= sizeof(sbus_to_pil)) {
268 printk(KERN_ERR "%s: bogus SBINT %d\n", sdev->prom_name, sbint);
269 BUG();
270 }
271 return sun4d_build_irq(sdev, sbus_to_pil[sbint]);
272}
273
274int sun4d_request_irq(unsigned int irq,
275 irqreturn_t (*handler)(int, void *, struct pt_regs *),
276 unsigned long irqflags, const char * devname, void *dev_id)
277{
278 struct irqaction *action, *tmp = NULL, **actionp;
279 unsigned long flags;
280 int ret;
281
282 if(irq > 14 && irq < (1 << 5)) {
283 ret = -EINVAL;
284 goto out;
285 }
286
287 if (!handler) {
288 ret = -EINVAL;
289 goto out;
290 }
291
292 spin_lock_irqsave(&irq_action_lock, flags);
293
294 if (irq >= (1 << 5))
295 actionp = &(sbus_actions[irq - (1 << 5)].action);
296 else
297 actionp = irq + irq_action;
298 action = *actionp;
299
300 if (action) {
301 if ((action->flags & SA_SHIRQ) && (irqflags & SA_SHIRQ)) {
302 for (tmp = action; tmp->next; tmp = tmp->next);
303 } else {
304 ret = -EBUSY;
305 goto out_unlock;
306 }
307 if ((action->flags & SA_INTERRUPT) ^ (irqflags & SA_INTERRUPT)) {
308 printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq);
309 ret = -EBUSY;
310 goto out_unlock;
311 }
312 action = NULL; /* Or else! */
313 }
314
315 /* If this is flagged as statically allocated then we use our
316 * private struct which is never freed.
317 */
318 if (irqflags & SA_STATIC_ALLOC) {
319 if (static_irq_count < MAX_STATIC_ALLOC)
320 action = &static_irqaction[static_irq_count++];
321 else
322 printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n", irq, devname);
323 }
324
325 if (action == NULL)
326 action = (struct irqaction *)kmalloc(sizeof(struct irqaction),
327 GFP_ATOMIC);
328
329 if (!action) {
330 ret = -ENOMEM;
331 goto out_unlock;
332 }
333
334 action->handler = handler;
335 action->flags = irqflags;
336 cpus_clear(action->mask);
337 action->name = devname;
338 action->next = NULL;
339 action->dev_id = dev_id;
340
341 if (tmp)
342 tmp->next = action;
343 else
344 *actionp = action;
345
346 enable_irq(irq);
347
348 ret = 0;
349out_unlock:
350 spin_unlock_irqrestore(&irq_action_lock, flags);
351out:
352 return ret;
353}
354
355static void sun4d_disable_irq(unsigned int irq)
356{
357#ifdef CONFIG_SMP
358 int tid = sbus_tid[(irq >> 5) - 1];
359 unsigned long flags;
360#endif
361
362 if (irq < NR_IRQS) return;
363#ifdef CONFIG_SMP
364 spin_lock_irqsave(&sun4d_imsk_lock, flags);
365 cc_set_imsk_other(tid, cc_get_imsk_other(tid) | (1 << sbus_to_pil[(irq >> 2) & 7]));
366 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
367#else
368 cc_set_imsk(cc_get_imsk() | (1 << sbus_to_pil[(irq >> 2) & 7]));
369#endif
370}
371
372static void sun4d_enable_irq(unsigned int irq)
373{
374#ifdef CONFIG_SMP
375 int tid = sbus_tid[(irq >> 5) - 1];
376 unsigned long flags;
377#endif
378
379 if (irq < NR_IRQS) return;
380#ifdef CONFIG_SMP
381 spin_lock_irqsave(&sun4d_imsk_lock, flags);
382 cc_set_imsk_other(tid, cc_get_imsk_other(tid) & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
383 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
384#else
385 cc_set_imsk(cc_get_imsk() & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
386#endif
387}
388
389#ifdef CONFIG_SMP
390static void sun4d_set_cpu_int(int cpu, int level)
391{
392 sun4d_send_ipi(cpu, level);
393}
394
395static void sun4d_clear_ipi(int cpu, int level)
396{
397}
398
399static void sun4d_set_udt(int cpu)
400{
401}
402
403/* Setup IRQ distribution scheme. */
404void __init sun4d_distribute_irqs(void)
405{
406#ifdef DISTRIBUTE_IRQS
407 struct sbus_bus *sbus;
408 unsigned long sbus_serving_map;
409
410 sbus_serving_map = cpu_present_map;
411 for_each_sbus(sbus) {
412 if ((sbus->board * 2) == boot_cpu_id && (cpu_present_map & (1 << (sbus->board * 2 + 1))))
413 sbus_tid[sbus->board] = (sbus->board * 2 + 1);
414 else if (cpu_present_map & (1 << (sbus->board * 2)))
415 sbus_tid[sbus->board] = (sbus->board * 2);
416 else if (cpu_present_map & (1 << (sbus->board * 2 + 1)))
417 sbus_tid[sbus->board] = (sbus->board * 2 + 1);
418 else
419 sbus_tid[sbus->board] = 0xff;
420 if (sbus_tid[sbus->board] != 0xff)
421 sbus_serving_map &= ~(1 << sbus_tid[sbus->board]);
422 }
423 for_each_sbus(sbus)
424 if (sbus_tid[sbus->board] == 0xff) {
425 int i = 31;
426
427 if (!sbus_serving_map)
428 sbus_serving_map = cpu_present_map;
429 while (!(sbus_serving_map & (1 << i)))
430 i--;
431 sbus_tid[sbus->board] = i;
432 sbus_serving_map &= ~(1 << i);
433 }
434 for_each_sbus(sbus) {
435 printk("sbus%d IRQs directed to CPU%d\n", sbus->board, sbus_tid[sbus->board]);
436 set_sbi_tid(sbus->devid, sbus_tid[sbus->board] << 3);
437 }
438#else
439 struct sbus_bus *sbus;
440 int cpuid = cpu_logical_map(1);
441
442 if (cpuid == -1)
443 cpuid = cpu_logical_map(0);
444 for_each_sbus(sbus) {
445 sbus_tid[sbus->board] = cpuid;
446 set_sbi_tid(sbus->devid, cpuid << 3);
447 }
448 printk("All sbus IRQs directed to CPU%d\n", cpuid);
449#endif
450}
451#endif
452
453static void sun4d_clear_clock_irq(void)
454{
455 volatile unsigned int clear_intr;
456 clear_intr = sun4d_timers->l10_timer_limit;
457}
458
459static void sun4d_clear_profile_irq(int cpu)
460{
461 bw_get_prof_limit(cpu);
462}
463
464static void sun4d_load_profile_irq(int cpu, unsigned int limit)
465{
466 bw_set_prof_limit(cpu, limit);
467}
468
469static void __init sun4d_init_timers(irqreturn_t (*counter_fn)(int, void *, struct pt_regs *))
470{
471 int irq;
472 int cpu;
473 struct resource r;
474 int mid;
475
476 /* Map the User Timer registers. */
477 memset(&r, 0, sizeof(r));
478#ifdef CONFIG_SMP
479 r.start = CSR_BASE(boot_cpu_id)+BW_TIMER_LIMIT;
480#else
481 r.start = CSR_BASE(0)+BW_TIMER_LIMIT;
482#endif
483 r.flags = 0xf;
484 sun4d_timers = (struct sun4d_timer_regs *) sbus_ioremap(&r, 0,
485 PAGE_SIZE, "user timer");
486
487 sun4d_timers->l10_timer_limit = (((1000000/HZ) + 1) << 10);
488 master_l10_counter = &sun4d_timers->l10_cur_count;
489 master_l10_limit = &sun4d_timers->l10_timer_limit;
490
491 irq = request_irq(TIMER_IRQ,
492 counter_fn,
493 (SA_INTERRUPT | SA_STATIC_ALLOC),
494 "timer", NULL);
495 if (irq) {
496 prom_printf("time_init: unable to attach IRQ%d\n",TIMER_IRQ);
497 prom_halt();
498 }
499
500 /* Enable user timer free run for CPU 0 in BW */
501 /* bw_set_ctrl(0, bw_get_ctrl(0) | BW_CTRL_USER_TIMER); */
502
503 cpu = 0;
504 while (!cpu_find_by_instance(cpu, NULL, &mid)) {
505 sun4d_load_profile_irq(mid >> 3, 0);
506 cpu++;
507 }
508
509#ifdef CONFIG_SMP
510 {
511 unsigned long flags;
512 extern unsigned long lvl14_save[4];
513 struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)];
514 extern unsigned int real_irq_entry[], smp4d_ticker[];
515 extern unsigned int patchme_maybe_smp_msg[];
516
517 /* Adjust so that we jump directly to smp4d_ticker */
518 lvl14_save[2] += smp4d_ticker - real_irq_entry;
519
520 /* For SMP we use the level 14 ticker, however the bootup code
521 * has copied the firmwares level 14 vector into boot cpu's
522 * trap table, we must fix this now or we get squashed.
523 */
524 local_irq_save(flags);
525 patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
526 trap_table->inst_one = lvl14_save[0];
527 trap_table->inst_two = lvl14_save[1];
528 trap_table->inst_three = lvl14_save[2];
529 trap_table->inst_four = lvl14_save[3];
530 local_flush_cache_all();
531 local_irq_restore(flags);
532 }
533#endif
534}
535
536void __init sun4d_init_sbi_irq(void)
537{
538 struct sbus_bus *sbus;
539 unsigned mask;
540
541 nsbi = 0;
542 for_each_sbus(sbus)
543 nsbi++;
544 sbus_actions = (struct sbus_action *)kmalloc (nsbi * 8 * 4 * sizeof(struct sbus_action), GFP_ATOMIC);
545 memset (sbus_actions, 0, (nsbi * 8 * 4 * sizeof(struct sbus_action)));
546 for_each_sbus(sbus) {
547#ifdef CONFIG_SMP
548 extern unsigned char boot_cpu_id;
549
550 set_sbi_tid(sbus->devid, boot_cpu_id << 3);
551 sbus_tid[sbus->board] = boot_cpu_id;
552#endif
553 /* Get rid of pending irqs from PROM */
554 mask = acquire_sbi(sbus->devid, 0xffffffff);
555 if (mask) {
556 printk ("Clearing pending IRQs %08x on SBI %d\n", mask, sbus->board);
557 release_sbi(sbus->devid, mask);
558 }
559 }
560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562void __init sun4d_init_IRQ(void)
563{
564 local_irq_disable();
565
566 BTFIXUPSET_CALL(sbint_to_irq, sun4d_sbint_to_irq, BTFIXUPCALL_NORM);
567 BTFIXUPSET_CALL(enable_irq, sun4d_enable_irq, BTFIXUPCALL_NORM);
568 BTFIXUPSET_CALL(disable_irq, sun4d_disable_irq, BTFIXUPCALL_NORM);
569 BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM);
570 BTFIXUPSET_CALL(clear_profile_irq, sun4d_clear_profile_irq, BTFIXUPCALL_NORM);
571 BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 sparc_init_timers = sun4d_init_timers;
573#ifdef CONFIG_SMP
574 BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM);
575 BTFIXUPSET_CALL(clear_cpu_int, sun4d_clear_ipi, BTFIXUPCALL_NOP);
576 BTFIXUPSET_CALL(set_irq_udt, sun4d_set_udt, BTFIXUPCALL_NOP);
577#endif
578 /* Cannot enable interrupts until OBP ticker is disabled. */
579}