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/irq.h>
21 #include <linux/interrupt.h>
22 #include <linux/sysdev.h>
23 #include <linux/errno.h>
24 #include <asm/ptrace.h>
25 #include <asm/signal.h>
29 #include <asm/sections.h>
30 #include <asm/open_pic.h>
31 #include <asm/i8259.h>
33 #include "open_pic_defs.h"
36 static volatile struct OpenPIC
*OpenPIC2
= NULL
;
38 * We define OpenPIC_InitSenses table thusly:
39 * bit 0x1: sense, 0 for edge and 1 for level.
40 * bit 0x2: polarity, 0 for negative, 1 for positive.
42 extern u_int OpenPIC_NumInitSenses
;
43 extern u_char
*OpenPIC_InitSenses
;
44 extern int use_of_interrupt_tree
;
46 static u_int NumProcessors
;
47 static u_int NumSources
;
48 static int open_pic2_irq_offset
;
49 static volatile OpenPIC_Source
*ISR
[NR_IRQS
];
51 /* Global Operations */
52 static void openpic2_disable_8259_pass_through(void);
53 static void openpic2_set_priority(u_int pri
);
54 static void openpic2_set_spurious(u_int vector
);
56 /* Timer Interrupts */
57 static void openpic2_inittimer(u_int timer
, u_int pri
, u_int vector
);
58 static void openpic2_maptimer(u_int timer
, u_int cpumask
);
60 /* Interrupt Sources */
61 static void openpic2_enable_irq(u_int irq
);
62 static void openpic2_disable_irq(u_int irq
);
63 static void openpic2_initirq(u_int irq
, u_int pri
, u_int vector
, int polarity
,
65 static void openpic2_mapirq(u_int irq
, u_int cpumask
, u_int keepmask
);
68 * These functions are not used but the code is kept here
69 * for completeness and future reference.
71 static void openpic2_reset(void);
73 static void openpic2_enable_8259_pass_through(void);
74 static u_int
openpic2_get_priority(void);
75 static u_int
openpic2_get_spurious(void);
76 static void openpic2_set_sense(u_int irq
, int sense
);
80 * Description of the openpic for the higher-level irq code
82 static void openpic2_end_irq(unsigned int irq_nr
);
83 static void openpic2_ack_irq(unsigned int irq_nr
);
85 struct hw_interrupt_type open_pic2
= {
96 * Accesses to the current processor's openpic registers
97 * On cascaded controller, this is only CPU 0
99 #define THIS_CPU Processor[0]
100 #define DECL_THIS_CPU
101 #define CHECK_THIS_CPU
104 #define check_arg_ipi(ipi) \
105 if (ipi < 0 || ipi >= OPENPIC_NUM_IPI) \
106 printk("open_pic.c:%d: illegal ipi %d\n", __LINE__, ipi);
107 #define check_arg_timer(timer) \
108 if (timer < 0 || timer >= OPENPIC_NUM_TIMERS) \
109 printk("open_pic.c:%d: illegal timer %d\n", __LINE__, timer);
110 #define check_arg_vec(vec) \
111 if (vec < 0 || vec >= OPENPIC_NUM_VECTORS) \
112 printk("open_pic.c:%d: illegal vector %d\n", __LINE__, vec);
113 #define check_arg_pri(pri) \
114 if (pri < 0 || pri >= OPENPIC_NUM_PRI) \
115 printk("open_pic.c:%d: illegal priority %d\n", __LINE__, pri);
117 * Print out a backtrace if it's out of range, since if it's larger than NR_IRQ's
118 * data has probably been corrupted and we're going to panic or deadlock later
121 extern unsigned long* _get_SP(void);
122 #define check_arg_irq(irq) \
123 if (irq < open_pic2_irq_offset || irq >= NumSources+open_pic2_irq_offset \
124 || ISR[irq - open_pic2_irq_offset] == 0) { \
125 printk("open_pic.c:%d: illegal irq %d\n", __LINE__, irq); \
126 /*print_backtrace(_get_SP());*/ }
127 #define check_arg_cpu(cpu) \
128 if (cpu < 0 || cpu >= NumProcessors){ \
129 printk("open_pic2.c:%d: illegal cpu %d\n", __LINE__, cpu); \
130 /*print_backtrace(_get_SP());*/ }
132 #define check_arg_ipi(ipi) do {} while (0)
133 #define check_arg_timer(timer) do {} while (0)
134 #define check_arg_vec(vec) do {} while (0)
135 #define check_arg_pri(pri) do {} while (0)
136 #define check_arg_irq(irq) do {} while (0)
137 #define check_arg_cpu(cpu) do {} while (0)
140 static u_int
openpic2_read(volatile u_int
*addr
)
148 static inline void openpic2_write(volatile u_int
*addr
, u_int val
)
153 static inline u_int
openpic2_readfield(volatile u_int
*addr
, u_int mask
)
155 u_int val
= openpic2_read(addr
);
159 inline void openpic2_writefield(volatile u_int
*addr
, u_int mask
,
162 u_int val
= openpic2_read(addr
);
163 openpic2_write(addr
, (val
& ~mask
) | (field
& mask
));
166 static inline void openpic2_clearfield(volatile u_int
*addr
, u_int mask
)
168 openpic2_writefield(addr
, mask
, 0);
171 static inline void openpic2_setfield(volatile u_int
*addr
, u_int mask
)
173 openpic2_writefield(addr
, mask
, mask
);
176 static void openpic2_safe_writefield(volatile u_int
*addr
, u_int mask
,
179 openpic2_setfield(addr
, OPENPIC_MASK
);
180 while (openpic2_read(addr
) & OPENPIC_ACTIVITY
);
181 openpic2_writefield(addr
, mask
| OPENPIC_MASK
, field
| OPENPIC_MASK
);
184 static void openpic2_reset(void)
186 openpic2_setfield(&OpenPIC2
->Global
.Global_Configuration0
,
187 OPENPIC_CONFIG_RESET
);
188 while (openpic2_readfield(&OpenPIC2
->Global
.Global_Configuration0
,
189 OPENPIC_CONFIG_RESET
))
193 void __init
openpic2_set_sources(int first_irq
, int num_irqs
, void *first_ISR
)
195 volatile OpenPIC_Source
*src
= first_ISR
;
198 last_irq
= first_irq
+ num_irqs
;
199 if (last_irq
> NumSources
)
200 NumSources
= last_irq
;
202 src
= &((struct OpenPIC
*)OpenPIC2_Addr
)->Source
[first_irq
];
203 for (i
= first_irq
; i
< last_irq
; ++i
, ++src
)
208 * The `offset' parameter defines where the interrupts handled by the
209 * OpenPIC start in the space of interrupt numbers that the kernel knows
210 * about. In other words, the OpenPIC's IRQ0 is numbered `offset' in the
211 * kernel's interrupt numbering scheme.
212 * We assume there is only one OpenPIC.
214 void __init
openpic2_init(int offset
)
220 if (!OpenPIC2_Addr
) {
221 printk("No OpenPIC2 found !\n");
224 OpenPIC2
= (volatile struct OpenPIC
*)OpenPIC2_Addr
;
226 if (ppc_md
.progress
) ppc_md
.progress("openpic: enter", 0x122);
228 t
= openpic2_read(&OpenPIC2
->Global
.Feature_Reporting0
);
229 switch (t
& OPENPIC_FEATURE_VERSION_MASK
) {
243 NumProcessors
= ((t
& OPENPIC_FEATURE_LAST_PROCESSOR_MASK
) >>
244 OPENPIC_FEATURE_LAST_PROCESSOR_SHIFT
) + 1;
246 openpic2_set_sources(0,
247 ((t
& OPENPIC_FEATURE_LAST_SOURCE_MASK
) >>
248 OPENPIC_FEATURE_LAST_SOURCE_SHIFT
) + 1,
250 printk("OpenPIC (2) Version %s (%d CPUs and %d IRQ sources) at %p\n",
251 version
, NumProcessors
, NumSources
, OpenPIC2
);
252 timerfreq
= openpic2_read(&OpenPIC2
->Global
.Timer_Frequency
);
254 printk("OpenPIC timer frequency is %d.%06d MHz\n",
255 timerfreq
/ 1000000, timerfreq
% 1000000);
257 open_pic2_irq_offset
= offset
;
259 /* Initialize timer interrupts */
260 if ( ppc_md
.progress
) ppc_md
.progress("openpic2: timer",0x3ba);
261 for (i
= 0; i
< OPENPIC_NUM_TIMERS
; i
++) {
262 /* Disabled, Priority 0 */
263 openpic2_inittimer(i
, 0, OPENPIC2_VEC_TIMER
+i
+offset
);
265 openpic2_maptimer(i
, 0);
268 /* Initialize external interrupts */
269 if (ppc_md
.progress
) ppc_md
.progress("openpic2: external",0x3bc);
271 openpic2_set_priority(0xf);
273 /* Init all external sources, including possibly the cascade. */
274 for (i
= 0; i
< NumSources
; i
++) {
280 /* the bootloader may have left it enabled (bad !) */
281 openpic2_disable_irq(i
+offset
);
283 sense
= (i
< OpenPIC_NumInitSenses
)? OpenPIC_InitSenses
[i
]: \
284 (IRQ_SENSE_LEVEL
| IRQ_POLARITY_NEGATIVE
);
286 if (sense
& IRQ_SENSE_MASK
)
287 irq_desc
[i
+offset
].status
= IRQ_LEVEL
;
289 /* Enabled, Priority 8 */
290 openpic2_initirq(i
, 8, i
+offset
, (sense
& IRQ_POLARITY_MASK
),
291 (sense
& IRQ_SENSE_MASK
));
293 openpic2_mapirq(i
, 1<<0, 0);
296 /* Init descriptors */
297 for (i
= offset
; i
< NumSources
+ offset
; i
++)
298 irq_desc
[i
].handler
= &open_pic2
;
300 /* Initialize the spurious interrupt */
301 if (ppc_md
.progress
) ppc_md
.progress("openpic2: spurious",0x3bd);
302 openpic2_set_spurious(OPENPIC2_VEC_SPURIOUS
+offset
);
304 openpic2_disable_8259_pass_through();
305 openpic2_set_priority(0);
307 if (ppc_md
.progress
) ppc_md
.progress("openpic2: exit",0x222);
311 static void openpic2_enable_8259_pass_through(void)
313 openpic2_clearfield(&OpenPIC2
->Global
.Global_Configuration0
,
314 OPENPIC_CONFIG_8259_PASSTHROUGH_DISABLE
);
318 /* This can't be __init, it is used in openpic_sleep_restore_intrs */
319 static void openpic2_disable_8259_pass_through(void)
321 openpic2_setfield(&OpenPIC2
->Global
.Global_Configuration0
,
322 OPENPIC_CONFIG_8259_PASSTHROUGH_DISABLE
);
326 * Find out the current interrupt
328 u_int
openpic2_irq(void)
334 vec
= openpic2_readfield(&OpenPIC2
->THIS_CPU
.Interrupt_Acknowledge
,
335 OPENPIC_VECTOR_MASK
);
339 void openpic2_eoi(void)
344 openpic2_write(&OpenPIC2
->THIS_CPU
.EOI
, 0);
345 /* Handle PCI write posting */
346 (void)openpic2_read(&OpenPIC2
->THIS_CPU
.EOI
);
350 static u_int
openpic2_get_priority(void)
355 return openpic2_readfield(&OpenPIC2
->THIS_CPU
.Current_Task_Priority
,
356 OPENPIC_CURRENT_TASK_PRIORITY_MASK
);
360 static void __init
openpic2_set_priority(u_int pri
)
366 openpic2_writefield(&OpenPIC2
->THIS_CPU
.Current_Task_Priority
,
367 OPENPIC_CURRENT_TASK_PRIORITY_MASK
, pri
);
371 * Get/set the spurious vector
374 static u_int
openpic2_get_spurious(void)
376 return openpic2_readfield(&OpenPIC2
->Global
.Spurious_Vector
,
377 OPENPIC_VECTOR_MASK
);
381 /* This can't be __init, it is used in openpic_sleep_restore_intrs */
382 static void openpic2_set_spurious(u_int vec
)
385 openpic2_writefield(&OpenPIC2
->Global
.Spurious_Vector
, OPENPIC_VECTOR_MASK
,
389 static DEFINE_SPINLOCK(openpic2_setup_lock
);
392 * Initialize a timer interrupt (and disable it)
394 * timer: OpenPIC timer number
395 * pri: interrupt source priority
396 * vec: the vector it will produce
398 static void __init
openpic2_inittimer(u_int timer
, u_int pri
, u_int vec
)
400 check_arg_timer(timer
);
403 openpic2_safe_writefield(&OpenPIC2
->Global
.Timer
[timer
].Vector_Priority
,
404 OPENPIC_PRIORITY_MASK
| OPENPIC_VECTOR_MASK
,
405 (pri
<< OPENPIC_PRIORITY_SHIFT
) | vec
);
409 * Map a timer interrupt to one or more CPUs
411 static void __init
openpic2_maptimer(u_int timer
, u_int cpumask
)
413 check_arg_timer(timer
);
414 openpic2_write(&OpenPIC2
->Global
.Timer
[timer
].Destination
,
419 * Initalize the interrupt source which will generate an NMI.
420 * This raises the interrupt's priority from 8 to 9.
422 * irq: The logical IRQ which generates an NMI.
425 openpic2_init_nmi_irq(u_int irq
)
428 openpic2_safe_writefield(&ISR
[irq
- open_pic2_irq_offset
]->Vector_Priority
,
429 OPENPIC_PRIORITY_MASK
,
430 9 << OPENPIC_PRIORITY_SHIFT
);
435 * All functions below take an offset'ed irq argument
441 * Enable/disable an external interrupt source
443 * Externally called, irq is an offseted system-wide interrupt number
445 static void openpic2_enable_irq(u_int irq
)
450 vpp
= &ISR
[irq
- open_pic2_irq_offset
]->Vector_Priority
;
451 openpic2_clearfield(vpp
, OPENPIC_MASK
);
452 /* make sure mask gets to controller before we return to user */
454 mb(); /* sync is probably useless here */
455 } while (openpic2_readfield(vpp
, OPENPIC_MASK
));
458 static void openpic2_disable_irq(u_int irq
)
464 vpp
= &ISR
[irq
- open_pic2_irq_offset
]->Vector_Priority
;
465 openpic2_setfield(vpp
, OPENPIC_MASK
);
466 /* make sure mask gets to controller before we return to user */
468 mb(); /* sync is probably useless here */
469 vp
= openpic2_readfield(vpp
, OPENPIC_MASK
| OPENPIC_ACTIVITY
);
470 } while((vp
& OPENPIC_ACTIVITY
) && !(vp
& OPENPIC_MASK
));
475 * Initialize an interrupt source (and disable it!)
477 * irq: OpenPIC interrupt number
478 * pri: interrupt source priority
479 * vec: the vector it will produce
480 * pol: polarity (1 for positive, 0 for negative)
481 * sense: 1 for level, 0 for edge
484 openpic2_initirq(u_int irq
, u_int pri
, u_int vec
, int pol
, int sense
)
486 openpic2_safe_writefield(&ISR
[irq
]->Vector_Priority
,
487 OPENPIC_PRIORITY_MASK
| OPENPIC_VECTOR_MASK
|
488 OPENPIC_SENSE_MASK
| OPENPIC_POLARITY_MASK
,
489 (pri
<< OPENPIC_PRIORITY_SHIFT
) | vec
|
490 (pol
? OPENPIC_POLARITY_POSITIVE
:
491 OPENPIC_POLARITY_NEGATIVE
) |
492 (sense
? OPENPIC_SENSE_LEVEL
: OPENPIC_SENSE_EDGE
));
496 * Map an interrupt source to one or more CPUs
498 static void openpic2_mapirq(u_int irq
, u_int physmask
, u_int keepmask
)
503 physmask
|= openpic2_read(&ISR
[irq
]->Destination
) & keepmask
;
504 openpic2_write(&ISR
[irq
]->Destination
, physmask
);
509 * Set the sense for an interrupt source (and disable it!)
511 * sense: 1 for level, 0 for edge
513 static void openpic2_set_sense(u_int irq
, int sense
)
516 openpic2_safe_writefield(&ISR
[irq
]->Vector_Priority
,
518 (sense
? OPENPIC_SENSE_LEVEL
: 0));
522 /* No spinlocks, should not be necessary with the OpenPIC
523 * (1 register = 1 interrupt and we have the desc lock).
525 static void openpic2_ack_irq(unsigned int irq_nr
)
527 openpic2_disable_irq(irq_nr
);
531 static void openpic2_end_irq(unsigned int irq_nr
)
533 if (!(irq_desc
[irq_nr
].status
& (IRQ_DISABLED
|IRQ_INPROGRESS
)))
534 openpic2_enable_irq(irq_nr
);
538 openpic2_get_irq(struct pt_regs
*regs
)
540 int irq
= openpic2_irq();
542 if (irq
== (OPENPIC2_VEC_SPURIOUS
+ open_pic2_irq_offset
))
550 * We implement the IRQ controller as a sysdev and put it
551 * to sleep at powerdown stage (the callback is named suspend,
552 * but it's old semantics, for the Device Model, it's really
553 * powerdown). The possible problem is that another sysdev that
554 * happens to be suspend after this one will have interrupts off,
555 * that may be an issue... For now, this isn't an issue on pmac
559 static u32 save_ipi_vp
[OPENPIC_NUM_IPI
];
560 static u32 save_irq_src_vp
[OPENPIC_MAX_SOURCES
];
561 static u32 save_irq_src_dest
[OPENPIC_MAX_SOURCES
];
562 static u32 save_cpu_task_pri
[OPENPIC_MAX_PROCESSORS
];
563 static int openpic_suspend_count
;
565 static void openpic2_cached_enable_irq(u_int irq
)
568 save_irq_src_vp
[irq
- open_pic2_irq_offset
] &= ~OPENPIC_MASK
;
571 static void openpic2_cached_disable_irq(u_int irq
)
574 save_irq_src_vp
[irq
- open_pic2_irq_offset
] |= OPENPIC_MASK
;
577 /* WARNING: Can be called directly by the cpufreq code with NULL parameter,
578 * we need something better to deal with that... Maybe switch to S1 for
581 int openpic2_suspend(struct sys_device
*sysdev
, u32 state
)
586 spin_lock_irqsave(&openpic2_setup_lock
, flags
);
588 if (openpic_suspend_count
++ > 0) {
589 spin_unlock_irqrestore(&openpic2_setup_lock
, flags
);
593 open_pic2
.enable
= openpic2_cached_enable_irq
;
594 open_pic2
.disable
= openpic2_cached_disable_irq
;
596 for (i
=0; i
<NumProcessors
; i
++) {
597 save_cpu_task_pri
[i
] = openpic2_read(&OpenPIC2
->Processor
[i
].Current_Task_Priority
);
598 openpic2_writefield(&OpenPIC2
->Processor
[i
].Current_Task_Priority
,
599 OPENPIC_CURRENT_TASK_PRIORITY_MASK
, 0xf);
602 for (i
=0; i
<OPENPIC_NUM_IPI
; i
++)
603 save_ipi_vp
[i
] = openpic2_read(&OpenPIC2
->Global
.IPI_Vector_Priority(i
));
604 for (i
=0; i
<NumSources
; i
++) {
607 save_irq_src_vp
[i
] = openpic2_read(&ISR
[i
]->Vector_Priority
) & ~OPENPIC_ACTIVITY
;
608 save_irq_src_dest
[i
] = openpic2_read(&ISR
[i
]->Destination
);
611 spin_unlock_irqrestore(&openpic2_setup_lock
, flags
);
616 /* WARNING: Can be called directly by the cpufreq code with NULL parameter,
617 * we need something better to deal with that... Maybe switch to S1 for
620 int openpic2_resume(struct sys_device
*sysdev
)
624 u32 vppmask
= OPENPIC_PRIORITY_MASK
| OPENPIC_VECTOR_MASK
|
625 OPENPIC_SENSE_MASK
| OPENPIC_POLARITY_MASK
|
628 spin_lock_irqsave(&openpic2_setup_lock
, flags
);
630 if ((--openpic_suspend_count
) > 0) {
631 spin_unlock_irqrestore(&openpic2_setup_lock
, flags
);
637 /* OpenPIC sometimes seem to need some time to be fully back up... */
639 openpic2_set_spurious(OPENPIC2_VEC_SPURIOUS
+open_pic2_irq_offset
);
640 } while(openpic2_readfield(&OpenPIC2
->Global
.Spurious_Vector
, OPENPIC_VECTOR_MASK
)
641 != (OPENPIC2_VEC_SPURIOUS
+ open_pic2_irq_offset
));
643 openpic2_disable_8259_pass_through();
645 for (i
=0; i
<OPENPIC_NUM_IPI
; i
++)
646 openpic2_write(&OpenPIC2
->Global
.IPI_Vector_Priority(i
),
648 for (i
=0; i
<NumSources
; i
++) {
651 openpic2_write(&ISR
[i
]->Destination
, save_irq_src_dest
[i
]);
652 openpic2_write(&ISR
[i
]->Vector_Priority
, save_irq_src_vp
[i
]);
653 /* make sure mask gets to controller before we return to user */
655 openpic2_write(&ISR
[i
]->Vector_Priority
, save_irq_src_vp
[i
]);
656 } while (openpic2_readfield(&ISR
[i
]->Vector_Priority
, vppmask
)
657 != (save_irq_src_vp
[i
] & vppmask
));
659 for (i
=0; i
<NumProcessors
; i
++)
660 openpic2_write(&OpenPIC2
->Processor
[i
].Current_Task_Priority
,
661 save_cpu_task_pri
[i
]);
663 open_pic2
.enable
= openpic2_enable_irq
;
664 open_pic2
.disable
= openpic2_disable_irq
;
666 spin_unlock_irqrestore(&openpic2_setup_lock
, flags
);
671 #endif /* CONFIG_PM */
674 static struct sysdev_class openpic2_sysclass
= {
675 set_kset_name("openpic2"),
678 static struct sys_device device_openpic2
= {
680 .cls
= &openpic2_sysclass
,
683 static struct sysdev_driver driver_openpic2
= {
685 .suspend
= &openpic2_suspend
,
686 .resume
= &openpic2_resume
,
687 #endif /* CONFIG_PM */
690 static int __init
init_openpic2_sysfs(void)
696 printk(KERN_DEBUG
"Registering openpic2 with sysfs...\n");
697 rc
= sysdev_class_register(&openpic2_sysclass
);
699 printk(KERN_ERR
"Failed registering openpic sys class\n");
702 rc
= sysdev_register(&device_openpic2
);
704 printk(KERN_ERR
"Failed registering openpic sys device\n");
707 rc
= sysdev_driver_register(&openpic2_sysclass
, &driver_openpic2
);
709 printk(KERN_ERR
"Failed registering openpic sys driver\n");
715 subsys_initcall(init_openpic2_sysfs
);