2 * arch/ppc/kernel/open_pic.c -- OpenPIC Interrupt Handling
4 * Copyright (C) 1997 Geert Uytterhoeven
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive
10 * This is a duplicate of open_pic.c that deals with U3s MPIC on
11 * G5 PowerMacs. It's the same file except it's using big endian
15 #include <linux/config.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/sysdev.h>
22 #include <linux/errno.h>
23 #include <asm/ptrace.h>
24 #include <asm/signal.h>
27 #include <asm/sections.h>
28 #include <asm/open_pic.h>
29 #include <asm/i8259.h>
30 #include <asm/machdep.h>
32 #include "open_pic_defs.h"
35 static volatile struct OpenPIC *OpenPIC2 = NULL;
37 * We define OpenPIC_InitSenses table thusly:
38 * bit 0x1: sense, 0 for edge and 1 for level.
39 * bit 0x2: polarity, 0 for negative, 1 for positive.
41 extern u_int OpenPIC_NumInitSenses;
42 extern u_char *OpenPIC_InitSenses;
43 extern int use_of_interrupt_tree;
45 static u_int NumProcessors;
46 static u_int NumSources;
47 static int open_pic2_irq_offset;
48 static volatile OpenPIC_Source *ISR[NR_IRQS];
50 /* Global Operations */
51 static void openpic2_disable_8259_pass_through(void);
52 static void openpic2_set_priority(u_int pri);
53 static void openpic2_set_spurious(u_int vector);
55 /* Timer Interrupts */
56 static void openpic2_inittimer(u_int timer, u_int pri, u_int vector);
57 static void openpic2_maptimer(u_int timer, u_int cpumask);
59 /* Interrupt Sources */
60 static void openpic2_enable_irq(u_int irq);
61 static void openpic2_disable_irq(u_int irq);
62 static void openpic2_initirq(u_int irq, u_int pri, u_int vector, int polarity,
64 static void openpic2_mapirq(u_int irq, u_int cpumask, u_int keepmask);
67 * These functions are not used but the code is kept here
68 * for completeness and future reference.
70 static void openpic2_reset(void);
72 static void openpic2_enable_8259_pass_through(void);
73 static u_int openpic2_get_priority(void);
74 static u_int openpic2_get_spurious(void);
75 static void openpic2_set_sense(u_int irq, int sense);
79 * Description of the openpic for the higher-level irq code
81 static void openpic2_end_irq(unsigned int irq_nr);
82 static void openpic2_ack_irq(unsigned int irq_nr);
84 struct hw_interrupt_type open_pic2 = {
85 .typename = " OpenPIC2 ",
86 .enable = openpic2_enable_irq,
87 .disable = openpic2_disable_irq,
88 .ack = openpic2_ack_irq,
89 .end = openpic2_end_irq,
93 * Accesses to the current processor's openpic registers
94 * On cascaded controller, this is only CPU 0
96 #define THIS_CPU Processor[0]
98 #define CHECK_THIS_CPU
101 #define check_arg_ipi(ipi) \
102 if (ipi < 0 || ipi >= OPENPIC_NUM_IPI) \
103 printk("open_pic.c:%d: illegal ipi %d\n", __LINE__, ipi);
104 #define check_arg_timer(timer) \
105 if (timer < 0 || timer >= OPENPIC_NUM_TIMERS) \
106 printk("open_pic.c:%d: illegal timer %d\n", __LINE__, timer);
107 #define check_arg_vec(vec) \
108 if (vec < 0 || vec >= OPENPIC_NUM_VECTORS) \
109 printk("open_pic.c:%d: illegal vector %d\n", __LINE__, vec);
110 #define check_arg_pri(pri) \
111 if (pri < 0 || pri >= OPENPIC_NUM_PRI) \
112 printk("open_pic.c:%d: illegal priority %d\n", __LINE__, pri);
114 * Print out a backtrace if it's out of range, since if it's larger than NR_IRQ's
115 * data has probably been corrupted and we're going to panic or deadlock later
118 extern unsigned long* _get_SP(void);
119 #define check_arg_irq(irq) \
120 if (irq < open_pic2_irq_offset || irq >= NumSources+open_pic2_irq_offset \
121 || ISR[irq - open_pic2_irq_offset] == 0) { \
122 printk("open_pic.c:%d: illegal irq %d\n", __LINE__, irq); \
123 /*print_backtrace(_get_SP());*/ }
124 #define check_arg_cpu(cpu) \
125 if (cpu < 0 || cpu >= NumProcessors){ \
126 printk("open_pic2.c:%d: illegal cpu %d\n", __LINE__, cpu); \
127 /*print_backtrace(_get_SP());*/ }
129 #define check_arg_ipi(ipi) do {} while (0)
130 #define check_arg_timer(timer) do {} while (0)
131 #define check_arg_vec(vec) do {} while (0)
132 #define check_arg_pri(pri) do {} while (0)
133 #define check_arg_irq(irq) do {} while (0)
134 #define check_arg_cpu(cpu) do {} while (0)
137 static u_int openpic2_read(volatile u_int *addr)
145 static inline void openpic2_write(volatile u_int *addr, u_int val)
150 static inline u_int openpic2_readfield(volatile u_int *addr, u_int mask)
152 u_int val = openpic2_read(addr);
156 inline void openpic2_writefield(volatile u_int *addr, u_int mask,
159 u_int val = openpic2_read(addr);
160 openpic2_write(addr, (val & ~mask) | (field & mask));
163 static inline void openpic2_clearfield(volatile u_int *addr, u_int mask)
165 openpic2_writefield(addr, mask, 0);
168 static inline void openpic2_setfield(volatile u_int *addr, u_int mask)
170 openpic2_writefield(addr, mask, mask);
173 static void openpic2_safe_writefield(volatile u_int *addr, u_int mask,
176 openpic2_setfield(addr, OPENPIC_MASK);
177 while (openpic2_read(addr) & OPENPIC_ACTIVITY);
178 openpic2_writefield(addr, mask | OPENPIC_MASK, field | OPENPIC_MASK);
181 static void openpic2_reset(void)
183 openpic2_setfield(&OpenPIC2->Global.Global_Configuration0,
184 OPENPIC_CONFIG_RESET);
185 while (openpic2_readfield(&OpenPIC2->Global.Global_Configuration0,
186 OPENPIC_CONFIG_RESET))
190 void __init openpic2_set_sources(int first_irq, int num_irqs, void *first_ISR)
192 volatile OpenPIC_Source *src = first_ISR;
195 last_irq = first_irq + num_irqs;
196 if (last_irq > NumSources)
197 NumSources = last_irq;
199 src = &((struct OpenPIC *)OpenPIC2_Addr)->Source[first_irq];
200 for (i = first_irq; i < last_irq; ++i, ++src)
205 * The `offset' parameter defines where the interrupts handled by the
206 * OpenPIC start in the space of interrupt numbers that the kernel knows
207 * about. In other words, the OpenPIC's IRQ0 is numbered `offset' in the
208 * kernel's interrupt numbering scheme.
209 * We assume there is only one OpenPIC.
211 void __init openpic2_init(int offset)
217 if (!OpenPIC2_Addr) {
218 printk("No OpenPIC2 found !\n");
221 OpenPIC2 = (volatile struct OpenPIC *)OpenPIC2_Addr;
223 if (ppc_md.progress) ppc_md.progress("openpic: enter", 0x122);
225 t = openpic2_read(&OpenPIC2->Global.Feature_Reporting0);
226 switch (t & OPENPIC_FEATURE_VERSION_MASK) {
240 NumProcessors = ((t & OPENPIC_FEATURE_LAST_PROCESSOR_MASK) >>
241 OPENPIC_FEATURE_LAST_PROCESSOR_SHIFT) + 1;
243 openpic2_set_sources(0,
244 ((t & OPENPIC_FEATURE_LAST_SOURCE_MASK) >>
245 OPENPIC_FEATURE_LAST_SOURCE_SHIFT) + 1,
247 printk("OpenPIC (2) Version %s (%d CPUs and %d IRQ sources) at %p\n",
248 version, NumProcessors, NumSources, OpenPIC2);
249 timerfreq = openpic2_read(&OpenPIC2->Global.Timer_Frequency);
251 printk("OpenPIC timer frequency is %d.%06d MHz\n",
252 timerfreq / 1000000, timerfreq % 1000000);
254 open_pic2_irq_offset = offset;
256 /* Initialize timer interrupts */
257 if ( ppc_md.progress ) ppc_md.progress("openpic2: timer",0x3ba);
258 for (i = 0; i < OPENPIC_NUM_TIMERS; i++) {
259 /* Disabled, Priority 0 */
260 openpic2_inittimer(i, 0, OPENPIC2_VEC_TIMER+i+offset);
262 openpic2_maptimer(i, 0);
265 /* Initialize external interrupts */
266 if (ppc_md.progress) ppc_md.progress("openpic2: external",0x3bc);
268 openpic2_set_priority(0xf);
270 /* Init all external sources, including possibly the cascade. */
271 for (i = 0; i < NumSources; i++) {
277 /* the bootloader may have left it enabled (bad !) */
278 openpic2_disable_irq(i+offset);
280 sense = (i < OpenPIC_NumInitSenses)? OpenPIC_InitSenses[i]: \
281 (IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE);
283 if (sense & IRQ_SENSE_MASK)
284 irq_desc[i+offset].status = IRQ_LEVEL;
286 /* Enabled, Priority 8 */
287 openpic2_initirq(i, 8, i+offset, (sense & IRQ_POLARITY_MASK),
288 (sense & IRQ_SENSE_MASK));
290 openpic2_mapirq(i, 1<<0, 0);
293 /* Init descriptors */
294 for (i = offset; i < NumSources + offset; i++)
295 irq_desc[i].handler = &open_pic2;
297 /* Initialize the spurious interrupt */
298 if (ppc_md.progress) ppc_md.progress("openpic2: spurious",0x3bd);
299 openpic2_set_spurious(OPENPIC2_VEC_SPURIOUS+offset);
301 openpic2_disable_8259_pass_through();
302 openpic2_set_priority(0);
304 if (ppc_md.progress) ppc_md.progress("openpic2: exit",0x222);
308 static void openpic2_enable_8259_pass_through(void)
310 openpic2_clearfield(&OpenPIC2->Global.Global_Configuration0,
311 OPENPIC_CONFIG_8259_PASSTHROUGH_DISABLE);
315 /* This can't be __init, it is used in openpic_sleep_restore_intrs */
316 static void openpic2_disable_8259_pass_through(void)
318 openpic2_setfield(&OpenPIC2->Global.Global_Configuration0,
319 OPENPIC_CONFIG_8259_PASSTHROUGH_DISABLE);
323 * Find out the current interrupt
325 u_int openpic2_irq(void)
331 vec = openpic2_readfield(&OpenPIC2->THIS_CPU.Interrupt_Acknowledge,
332 OPENPIC_VECTOR_MASK);
336 void openpic2_eoi(void)
341 openpic2_write(&OpenPIC2->THIS_CPU.EOI, 0);
342 /* Handle PCI write posting */
343 (void)openpic2_read(&OpenPIC2->THIS_CPU.EOI);
347 static u_int openpic2_get_priority(void)
352 return openpic2_readfield(&OpenPIC2->THIS_CPU.Current_Task_Priority,
353 OPENPIC_CURRENT_TASK_PRIORITY_MASK);
357 static void __init openpic2_set_priority(u_int pri)
363 openpic2_writefield(&OpenPIC2->THIS_CPU.Current_Task_Priority,
364 OPENPIC_CURRENT_TASK_PRIORITY_MASK, pri);
368 * Get/set the spurious vector
371 static u_int openpic2_get_spurious(void)
373 return openpic2_readfield(&OpenPIC2->Global.Spurious_Vector,
374 OPENPIC_VECTOR_MASK);
378 /* This can't be __init, it is used in openpic_sleep_restore_intrs */
379 static void openpic2_set_spurious(u_int vec)
382 openpic2_writefield(&OpenPIC2->Global.Spurious_Vector, OPENPIC_VECTOR_MASK,
386 static DEFINE_SPINLOCK(openpic2_setup_lock);
389 * Initialize a timer interrupt (and disable it)
391 * timer: OpenPIC timer number
392 * pri: interrupt source priority
393 * vec: the vector it will produce
395 static void __init openpic2_inittimer(u_int timer, u_int pri, u_int vec)
397 check_arg_timer(timer);
400 openpic2_safe_writefield(&OpenPIC2->Global.Timer[timer].Vector_Priority,
401 OPENPIC_PRIORITY_MASK | OPENPIC_VECTOR_MASK,
402 (pri << OPENPIC_PRIORITY_SHIFT) | vec);
406 * Map a timer interrupt to one or more CPUs
408 static void __init openpic2_maptimer(u_int timer, u_int cpumask)
410 check_arg_timer(timer);
411 openpic2_write(&OpenPIC2->Global.Timer[timer].Destination,
416 * Initalize the interrupt source which will generate an NMI.
417 * This raises the interrupt's priority from 8 to 9.
419 * irq: The logical IRQ which generates an NMI.
422 openpic2_init_nmi_irq(u_int irq)
425 openpic2_safe_writefield(&ISR[irq - open_pic2_irq_offset]->Vector_Priority,
426 OPENPIC_PRIORITY_MASK,
427 9 << OPENPIC_PRIORITY_SHIFT);
432 * All functions below take an offset'ed irq argument
438 * Enable/disable an external interrupt source
440 * Externally called, irq is an offseted system-wide interrupt number
442 static void openpic2_enable_irq(u_int irq)
447 vpp = &ISR[irq - open_pic2_irq_offset]->Vector_Priority;
448 openpic2_clearfield(vpp, OPENPIC_MASK);
449 /* make sure mask gets to controller before we return to user */
451 mb(); /* sync is probably useless here */
452 } while (openpic2_readfield(vpp, OPENPIC_MASK));
455 static void openpic2_disable_irq(u_int irq)
461 vpp = &ISR[irq - open_pic2_irq_offset]->Vector_Priority;
462 openpic2_setfield(vpp, OPENPIC_MASK);
463 /* make sure mask gets to controller before we return to user */
465 mb(); /* sync is probably useless here */
466 vp = openpic2_readfield(vpp, OPENPIC_MASK | OPENPIC_ACTIVITY);
467 } while((vp & OPENPIC_ACTIVITY) && !(vp & OPENPIC_MASK));
472 * Initialize an interrupt source (and disable it!)
474 * irq: OpenPIC interrupt number
475 * pri: interrupt source priority
476 * vec: the vector it will produce
477 * pol: polarity (1 for positive, 0 for negative)
478 * sense: 1 for level, 0 for edge
481 openpic2_initirq(u_int irq, u_int pri, u_int vec, int pol, int sense)
483 openpic2_safe_writefield(&ISR[irq]->Vector_Priority,
484 OPENPIC_PRIORITY_MASK | OPENPIC_VECTOR_MASK |
485 OPENPIC_SENSE_MASK | OPENPIC_POLARITY_MASK,
486 (pri << OPENPIC_PRIORITY_SHIFT) | vec |
487 (pol ? OPENPIC_POLARITY_POSITIVE :
488 OPENPIC_POLARITY_NEGATIVE) |
489 (sense ? OPENPIC_SENSE_LEVEL : OPENPIC_SENSE_EDGE));
493 * Map an interrupt source to one or more CPUs
495 static void openpic2_mapirq(u_int irq, u_int physmask, u_int keepmask)
500 physmask |= openpic2_read(&ISR[irq]->Destination) & keepmask;
501 openpic2_write(&ISR[irq]->Destination, physmask);
506 * Set the sense for an interrupt source (and disable it!)
508 * sense: 1 for level, 0 for edge
510 static void openpic2_set_sense(u_int irq, int sense)
513 openpic2_safe_writefield(&ISR[irq]->Vector_Priority,
515 (sense ? OPENPIC_SENSE_LEVEL : 0));
519 /* No spinlocks, should not be necessary with the OpenPIC
520 * (1 register = 1 interrupt and we have the desc lock).
522 static void openpic2_ack_irq(unsigned int irq_nr)
524 openpic2_disable_irq(irq_nr);
528 static void openpic2_end_irq(unsigned int irq_nr)
530 if (!(irq_desc[irq_nr].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
531 openpic2_enable_irq(irq_nr);
535 openpic2_get_irq(struct pt_regs *regs)
537 int irq = openpic2_irq();
539 if (irq == (OPENPIC2_VEC_SPURIOUS + open_pic2_irq_offset))
547 * We implement the IRQ controller as a sysdev and put it
548 * to sleep at powerdown stage (the callback is named suspend,
549 * but it's old semantics, for the Device Model, it's really
550 * powerdown). The possible problem is that another sysdev that
551 * happens to be suspend after this one will have interrupts off,
552 * that may be an issue... For now, this isn't an issue on pmac
556 static u32 save_ipi_vp[OPENPIC_NUM_IPI];
557 static u32 save_irq_src_vp[OPENPIC_MAX_SOURCES];
558 static u32 save_irq_src_dest[OPENPIC_MAX_SOURCES];
559 static u32 save_cpu_task_pri[OPENPIC_MAX_PROCESSORS];
560 static int openpic_suspend_count;
562 static void openpic2_cached_enable_irq(u_int irq)
565 save_irq_src_vp[irq - open_pic2_irq_offset] &= ~OPENPIC_MASK;
568 static void openpic2_cached_disable_irq(u_int irq)
571 save_irq_src_vp[irq - open_pic2_irq_offset] |= OPENPIC_MASK;
574 /* WARNING: Can be called directly by the cpufreq code with NULL parameter,
575 * we need something better to deal with that... Maybe switch to S1 for
578 int openpic2_suspend(struct sys_device *sysdev, pm_message_t state)
583 spin_lock_irqsave(&openpic2_setup_lock, flags);
585 if (openpic_suspend_count++ > 0) {
586 spin_unlock_irqrestore(&openpic2_setup_lock, flags);
590 open_pic2.enable = openpic2_cached_enable_irq;
591 open_pic2.disable = openpic2_cached_disable_irq;
593 for (i=0; i<NumProcessors; i++) {
594 save_cpu_task_pri[i] = openpic2_read(&OpenPIC2->Processor[i].Current_Task_Priority);
595 openpic2_writefield(&OpenPIC2->Processor[i].Current_Task_Priority,
596 OPENPIC_CURRENT_TASK_PRIORITY_MASK, 0xf);
599 for (i=0; i<OPENPIC_NUM_IPI; i++)
600 save_ipi_vp[i] = openpic2_read(&OpenPIC2->Global.IPI_Vector_Priority(i));
601 for (i=0; i<NumSources; i++) {
604 save_irq_src_vp[i] = openpic2_read(&ISR[i]->Vector_Priority) & ~OPENPIC_ACTIVITY;
605 save_irq_src_dest[i] = openpic2_read(&ISR[i]->Destination);
608 spin_unlock_irqrestore(&openpic2_setup_lock, flags);
613 /* WARNING: Can be called directly by the cpufreq code with NULL parameter,
614 * we need something better to deal with that... Maybe switch to S1 for
617 int openpic2_resume(struct sys_device *sysdev)
621 u32 vppmask = OPENPIC_PRIORITY_MASK | OPENPIC_VECTOR_MASK |
622 OPENPIC_SENSE_MASK | OPENPIC_POLARITY_MASK |
625 spin_lock_irqsave(&openpic2_setup_lock, flags);
627 if ((--openpic_suspend_count) > 0) {
628 spin_unlock_irqrestore(&openpic2_setup_lock, flags);
634 /* OpenPIC sometimes seem to need some time to be fully back up... */
636 openpic2_set_spurious(OPENPIC2_VEC_SPURIOUS+open_pic2_irq_offset);
637 } while(openpic2_readfield(&OpenPIC2->Global.Spurious_Vector, OPENPIC_VECTOR_MASK)
638 != (OPENPIC2_VEC_SPURIOUS + open_pic2_irq_offset));
640 openpic2_disable_8259_pass_through();
642 for (i=0; i<OPENPIC_NUM_IPI; i++)
643 openpic2_write(&OpenPIC2->Global.IPI_Vector_Priority(i),
645 for (i=0; i<NumSources; i++) {
648 openpic2_write(&ISR[i]->Destination, save_irq_src_dest[i]);
649 openpic2_write(&ISR[i]->Vector_Priority, save_irq_src_vp[i]);
650 /* make sure mask gets to controller before we return to user */
652 openpic2_write(&ISR[i]->Vector_Priority, save_irq_src_vp[i]);
653 } while (openpic2_readfield(&ISR[i]->Vector_Priority, vppmask)
654 != (save_irq_src_vp[i] & vppmask));
656 for (i=0; i<NumProcessors; i++)
657 openpic2_write(&OpenPIC2->Processor[i].Current_Task_Priority,
658 save_cpu_task_pri[i]);
660 open_pic2.enable = openpic2_enable_irq;
661 open_pic2.disable = openpic2_disable_irq;
663 spin_unlock_irqrestore(&openpic2_setup_lock, flags);
668 #endif /* CONFIG_PM */
671 static struct sysdev_class openpic2_sysclass = {
672 set_kset_name("openpic2"),
675 static struct sys_device device_openpic2 = {
677 .cls = &openpic2_sysclass,
680 static struct sysdev_driver driver_openpic2 = {
682 .suspend = &openpic2_suspend,
683 .resume = &openpic2_resume,
684 #endif /* CONFIG_PM */
687 static int __init init_openpic2_sysfs(void)
693 printk(KERN_DEBUG "Registering openpic2 with sysfs...\n");
694 rc = sysdev_class_register(&openpic2_sysclass);
696 printk(KERN_ERR "Failed registering openpic sys class\n");
699 rc = sysdev_register(&device_openpic2);
701 printk(KERN_ERR "Failed registering openpic sys device\n");
704 rc = sysdev_driver_register(&openpic2_sysclass, &driver_openpic2);
706 printk(KERN_ERR "Failed registering openpic sys driver\n");
712 subsys_initcall(init_openpic2_sysfs);