Initial commit
[wrt350n-kernel.git] / arch / powerpc / sysdev / mpic.c
blob6ffdda244bb1525f2fcd64413385997cb85e1954
1 /*
2 * arch/powerpc/kernel/mpic.c
4 * Driver for interrupt controllers following the OpenPIC standard, the
5 * common implementation beeing IBM's MPIC. This driver also can deal
6 * with various broken implementations of this HW.
8 * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file COPYING in the main directory of this archive
12 * for more details.
15 #undef DEBUG
16 #undef DEBUG_IPI
17 #undef DEBUG_IRQ
18 #undef DEBUG_LOW
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/irq.h>
24 #include <linux/smp.h>
25 #include <linux/interrupt.h>
26 #include <linux/bootmem.h>
27 #include <linux/spinlock.h>
28 #include <linux/pci.h>
30 #include <asm/ptrace.h>
31 #include <asm/signal.h>
32 #include <asm/io.h>
33 #include <asm/pgtable.h>
34 #include <asm/irq.h>
35 #include <asm/machdep.h>
36 #include <asm/mpic.h>
37 #include <asm/smp.h>
39 #include "mpic.h"
41 #ifdef DEBUG
42 #define DBG(fmt...) printk(fmt)
43 #else
44 #define DBG(fmt...)
45 #endif
47 static struct mpic *mpics;
48 static struct mpic *mpic_primary;
49 static DEFINE_SPINLOCK(mpic_lock);
51 #ifdef CONFIG_PPC32 /* XXX for now */
52 #ifdef CONFIG_IRQ_ALL_CPUS
53 #define distribute_irqs (1)
54 #else
55 #define distribute_irqs (0)
56 #endif
57 #endif
59 #ifdef CONFIG_MPIC_WEIRD
60 static u32 mpic_infos[][MPIC_IDX_END] = {
61 [0] = { /* Original OpenPIC compatible MPIC */
62 MPIC_GREG_BASE,
63 MPIC_GREG_FEATURE_0,
64 MPIC_GREG_GLOBAL_CONF_0,
65 MPIC_GREG_VENDOR_ID,
66 MPIC_GREG_IPI_VECTOR_PRI_0,
67 MPIC_GREG_IPI_STRIDE,
68 MPIC_GREG_SPURIOUS,
69 MPIC_GREG_TIMER_FREQ,
71 MPIC_TIMER_BASE,
72 MPIC_TIMER_STRIDE,
73 MPIC_TIMER_CURRENT_CNT,
74 MPIC_TIMER_BASE_CNT,
75 MPIC_TIMER_VECTOR_PRI,
76 MPIC_TIMER_DESTINATION,
78 MPIC_CPU_BASE,
79 MPIC_CPU_STRIDE,
80 MPIC_CPU_IPI_DISPATCH_0,
81 MPIC_CPU_IPI_DISPATCH_STRIDE,
82 MPIC_CPU_CURRENT_TASK_PRI,
83 MPIC_CPU_WHOAMI,
84 MPIC_CPU_INTACK,
85 MPIC_CPU_EOI,
86 MPIC_CPU_MCACK,
88 MPIC_IRQ_BASE,
89 MPIC_IRQ_STRIDE,
90 MPIC_IRQ_VECTOR_PRI,
91 MPIC_VECPRI_VECTOR_MASK,
92 MPIC_VECPRI_POLARITY_POSITIVE,
93 MPIC_VECPRI_POLARITY_NEGATIVE,
94 MPIC_VECPRI_SENSE_LEVEL,
95 MPIC_VECPRI_SENSE_EDGE,
96 MPIC_VECPRI_POLARITY_MASK,
97 MPIC_VECPRI_SENSE_MASK,
98 MPIC_IRQ_DESTINATION
100 [1] = { /* Tsi108/109 PIC */
101 TSI108_GREG_BASE,
102 TSI108_GREG_FEATURE_0,
103 TSI108_GREG_GLOBAL_CONF_0,
104 TSI108_GREG_VENDOR_ID,
105 TSI108_GREG_IPI_VECTOR_PRI_0,
106 TSI108_GREG_IPI_STRIDE,
107 TSI108_GREG_SPURIOUS,
108 TSI108_GREG_TIMER_FREQ,
110 TSI108_TIMER_BASE,
111 TSI108_TIMER_STRIDE,
112 TSI108_TIMER_CURRENT_CNT,
113 TSI108_TIMER_BASE_CNT,
114 TSI108_TIMER_VECTOR_PRI,
115 TSI108_TIMER_DESTINATION,
117 TSI108_CPU_BASE,
118 TSI108_CPU_STRIDE,
119 TSI108_CPU_IPI_DISPATCH_0,
120 TSI108_CPU_IPI_DISPATCH_STRIDE,
121 TSI108_CPU_CURRENT_TASK_PRI,
122 TSI108_CPU_WHOAMI,
123 TSI108_CPU_INTACK,
124 TSI108_CPU_EOI,
125 TSI108_CPU_MCACK,
127 TSI108_IRQ_BASE,
128 TSI108_IRQ_STRIDE,
129 TSI108_IRQ_VECTOR_PRI,
130 TSI108_VECPRI_VECTOR_MASK,
131 TSI108_VECPRI_POLARITY_POSITIVE,
132 TSI108_VECPRI_POLARITY_NEGATIVE,
133 TSI108_VECPRI_SENSE_LEVEL,
134 TSI108_VECPRI_SENSE_EDGE,
135 TSI108_VECPRI_POLARITY_MASK,
136 TSI108_VECPRI_SENSE_MASK,
137 TSI108_IRQ_DESTINATION
141 #define MPIC_INFO(name) mpic->hw_set[MPIC_IDX_##name]
143 #else /* CONFIG_MPIC_WEIRD */
145 #define MPIC_INFO(name) MPIC_##name
147 #endif /* CONFIG_MPIC_WEIRD */
150 * Register accessor functions
154 static inline u32 _mpic_read(enum mpic_reg_type type,
155 struct mpic_reg_bank *rb,
156 unsigned int reg)
158 switch(type) {
159 #ifdef CONFIG_PPC_DCR
160 case mpic_access_dcr:
161 return dcr_read(rb->dhost, reg);
162 #endif
163 case mpic_access_mmio_be:
164 return in_be32(rb->base + (reg >> 2));
165 case mpic_access_mmio_le:
166 default:
167 return in_le32(rb->base + (reg >> 2));
171 static inline void _mpic_write(enum mpic_reg_type type,
172 struct mpic_reg_bank *rb,
173 unsigned int reg, u32 value)
175 switch(type) {
176 #ifdef CONFIG_PPC_DCR
177 case mpic_access_dcr:
178 return dcr_write(rb->dhost, reg, value);
179 #endif
180 case mpic_access_mmio_be:
181 return out_be32(rb->base + (reg >> 2), value);
182 case mpic_access_mmio_le:
183 default:
184 return out_le32(rb->base + (reg >> 2), value);
188 static inline u32 _mpic_ipi_read(struct mpic *mpic, unsigned int ipi)
190 enum mpic_reg_type type = mpic->reg_type;
191 unsigned int offset = MPIC_INFO(GREG_IPI_VECTOR_PRI_0) +
192 (ipi * MPIC_INFO(GREG_IPI_STRIDE));
194 if ((mpic->flags & MPIC_BROKEN_IPI) && type == mpic_access_mmio_le)
195 type = mpic_access_mmio_be;
196 return _mpic_read(type, &mpic->gregs, offset);
199 static inline void _mpic_ipi_write(struct mpic *mpic, unsigned int ipi, u32 value)
201 unsigned int offset = MPIC_INFO(GREG_IPI_VECTOR_PRI_0) +
202 (ipi * MPIC_INFO(GREG_IPI_STRIDE));
204 _mpic_write(mpic->reg_type, &mpic->gregs, offset, value);
207 static inline u32 _mpic_cpu_read(struct mpic *mpic, unsigned int reg)
209 unsigned int cpu = 0;
211 if (mpic->flags & MPIC_PRIMARY)
212 cpu = hard_smp_processor_id();
213 return _mpic_read(mpic->reg_type, &mpic->cpuregs[cpu], reg);
216 static inline void _mpic_cpu_write(struct mpic *mpic, unsigned int reg, u32 value)
218 unsigned int cpu = 0;
220 if (mpic->flags & MPIC_PRIMARY)
221 cpu = hard_smp_processor_id();
223 _mpic_write(mpic->reg_type, &mpic->cpuregs[cpu], reg, value);
226 static inline u32 _mpic_irq_read(struct mpic *mpic, unsigned int src_no, unsigned int reg)
228 unsigned int isu = src_no >> mpic->isu_shift;
229 unsigned int idx = src_no & mpic->isu_mask;
231 #ifdef CONFIG_MPIC_BROKEN_REGREAD
232 if (reg == 0)
233 return mpic->isu_reg0_shadow[idx];
234 else
235 #endif
236 return _mpic_read(mpic->reg_type, &mpic->isus[isu],
237 reg + (idx * MPIC_INFO(IRQ_STRIDE)));
240 static inline void _mpic_irq_write(struct mpic *mpic, unsigned int src_no,
241 unsigned int reg, u32 value)
243 unsigned int isu = src_no >> mpic->isu_shift;
244 unsigned int idx = src_no & mpic->isu_mask;
246 _mpic_write(mpic->reg_type, &mpic->isus[isu],
247 reg + (idx * MPIC_INFO(IRQ_STRIDE)), value);
249 #ifdef CONFIG_MPIC_BROKEN_REGREAD
250 if (reg == 0)
251 mpic->isu_reg0_shadow[idx] = value;
252 #endif
255 #define mpic_read(b,r) _mpic_read(mpic->reg_type,&(b),(r))
256 #define mpic_write(b,r,v) _mpic_write(mpic->reg_type,&(b),(r),(v))
257 #define mpic_ipi_read(i) _mpic_ipi_read(mpic,(i))
258 #define mpic_ipi_write(i,v) _mpic_ipi_write(mpic,(i),(v))
259 #define mpic_cpu_read(i) _mpic_cpu_read(mpic,(i))
260 #define mpic_cpu_write(i,v) _mpic_cpu_write(mpic,(i),(v))
261 #define mpic_irq_read(s,r) _mpic_irq_read(mpic,(s),(r))
262 #define mpic_irq_write(s,r,v) _mpic_irq_write(mpic,(s),(r),(v))
266 * Low level utility functions
270 static void _mpic_map_mmio(struct mpic *mpic, phys_addr_t phys_addr,
271 struct mpic_reg_bank *rb, unsigned int offset,
272 unsigned int size)
274 rb->base = ioremap(phys_addr + offset, size);
275 BUG_ON(rb->base == NULL);
278 #ifdef CONFIG_PPC_DCR
279 static void _mpic_map_dcr(struct mpic *mpic, struct mpic_reg_bank *rb,
280 unsigned int offset, unsigned int size)
282 const u32 *dbasep;
284 dbasep = of_get_property(mpic->irqhost->of_node, "dcr-reg", NULL);
286 rb->dhost = dcr_map(mpic->irqhost->of_node, *dbasep + offset, size);
287 BUG_ON(!DCR_MAP_OK(rb->dhost));
290 static inline void mpic_map(struct mpic *mpic, phys_addr_t phys_addr,
291 struct mpic_reg_bank *rb, unsigned int offset,
292 unsigned int size)
294 if (mpic->flags & MPIC_USES_DCR)
295 _mpic_map_dcr(mpic, rb, offset, size);
296 else
297 _mpic_map_mmio(mpic, phys_addr, rb, offset, size);
299 #else /* CONFIG_PPC_DCR */
300 #define mpic_map(m,p,b,o,s) _mpic_map_mmio(m,p,b,o,s)
301 #endif /* !CONFIG_PPC_DCR */
305 /* Check if we have one of those nice broken MPICs with a flipped endian on
306 * reads from IPI registers
308 static void __init mpic_test_broken_ipi(struct mpic *mpic)
310 u32 r;
312 mpic_write(mpic->gregs, MPIC_INFO(GREG_IPI_VECTOR_PRI_0), MPIC_VECPRI_MASK);
313 r = mpic_read(mpic->gregs, MPIC_INFO(GREG_IPI_VECTOR_PRI_0));
315 if (r == le32_to_cpu(MPIC_VECPRI_MASK)) {
316 printk(KERN_INFO "mpic: Detected reversed IPI registers\n");
317 mpic->flags |= MPIC_BROKEN_IPI;
321 #ifdef CONFIG_MPIC_U3_HT_IRQS
323 /* Test if an interrupt is sourced from HyperTransport (used on broken U3s)
324 * to force the edge setting on the MPIC and do the ack workaround.
326 static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
328 if (source >= 128 || !mpic->fixups)
329 return 0;
330 return mpic->fixups[source].base != NULL;
334 static inline void mpic_ht_end_irq(struct mpic *mpic, unsigned int source)
336 struct mpic_irq_fixup *fixup = &mpic->fixups[source];
338 if (fixup->applebase) {
339 unsigned int soff = (fixup->index >> 3) & ~3;
340 unsigned int mask = 1U << (fixup->index & 0x1f);
341 writel(mask, fixup->applebase + soff);
342 } else {
343 spin_lock(&mpic->fixup_lock);
344 writeb(0x11 + 2 * fixup->index, fixup->base + 2);
345 writel(fixup->data, fixup->base + 4);
346 spin_unlock(&mpic->fixup_lock);
350 static void mpic_startup_ht_interrupt(struct mpic *mpic, unsigned int source,
351 unsigned int irqflags)
353 struct mpic_irq_fixup *fixup = &mpic->fixups[source];
354 unsigned long flags;
355 u32 tmp;
357 if (fixup->base == NULL)
358 return;
360 DBG("startup_ht_interrupt(0x%x, 0x%x) index: %d\n",
361 source, irqflags, fixup->index);
362 spin_lock_irqsave(&mpic->fixup_lock, flags);
363 /* Enable and configure */
364 writeb(0x10 + 2 * fixup->index, fixup->base + 2);
365 tmp = readl(fixup->base + 4);
366 tmp &= ~(0x23U);
367 if (irqflags & IRQ_LEVEL)
368 tmp |= 0x22;
369 writel(tmp, fixup->base + 4);
370 spin_unlock_irqrestore(&mpic->fixup_lock, flags);
372 #ifdef CONFIG_PM
373 /* use the lowest bit inverted to the actual HW,
374 * set if this fixup was enabled, clear otherwise */
375 mpic->save_data[source].fixup_data = tmp | 1;
376 #endif
379 static void mpic_shutdown_ht_interrupt(struct mpic *mpic, unsigned int source,
380 unsigned int irqflags)
382 struct mpic_irq_fixup *fixup = &mpic->fixups[source];
383 unsigned long flags;
384 u32 tmp;
386 if (fixup->base == NULL)
387 return;
389 DBG("shutdown_ht_interrupt(0x%x, 0x%x)\n", source, irqflags);
391 /* Disable */
392 spin_lock_irqsave(&mpic->fixup_lock, flags);
393 writeb(0x10 + 2 * fixup->index, fixup->base + 2);
394 tmp = readl(fixup->base + 4);
395 tmp |= 1;
396 writel(tmp, fixup->base + 4);
397 spin_unlock_irqrestore(&mpic->fixup_lock, flags);
399 #ifdef CONFIG_PM
400 /* use the lowest bit inverted to the actual HW,
401 * set if this fixup was enabled, clear otherwise */
402 mpic->save_data[source].fixup_data = tmp & ~1;
403 #endif
406 #ifdef CONFIG_PCI_MSI
407 static void __init mpic_scan_ht_msi(struct mpic *mpic, u8 __iomem *devbase,
408 unsigned int devfn)
410 u8 __iomem *base;
411 u8 pos, flags;
412 u64 addr = 0;
414 for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
415 pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
416 u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
417 if (id == PCI_CAP_ID_HT) {
418 id = readb(devbase + pos + 3);
419 if ((id & HT_5BIT_CAP_MASK) == HT_CAPTYPE_MSI_MAPPING)
420 break;
424 if (pos == 0)
425 return;
427 base = devbase + pos;
429 flags = readb(base + HT_MSI_FLAGS);
430 if (!(flags & HT_MSI_FLAGS_FIXED)) {
431 addr = readl(base + HT_MSI_ADDR_LO) & HT_MSI_ADDR_LO_MASK;
432 addr = addr | ((u64)readl(base + HT_MSI_ADDR_HI) << 32);
435 printk(KERN_DEBUG "mpic: - HT:%02x.%x %s MSI mapping found @ 0x%lx\n",
436 PCI_SLOT(devfn), PCI_FUNC(devfn),
437 flags & HT_MSI_FLAGS_ENABLE ? "enabled" : "disabled", addr);
439 if (!(flags & HT_MSI_FLAGS_ENABLE))
440 writeb(flags | HT_MSI_FLAGS_ENABLE, base + HT_MSI_FLAGS);
442 #else
443 static void __init mpic_scan_ht_msi(struct mpic *mpic, u8 __iomem *devbase,
444 unsigned int devfn)
446 return;
448 #endif
450 static void __init mpic_scan_ht_pic(struct mpic *mpic, u8 __iomem *devbase,
451 unsigned int devfn, u32 vdid)
453 int i, irq, n;
454 u8 __iomem *base;
455 u32 tmp;
456 u8 pos;
458 for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
459 pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
460 u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
461 if (id == PCI_CAP_ID_HT) {
462 id = readb(devbase + pos + 3);
463 if ((id & HT_5BIT_CAP_MASK) == HT_CAPTYPE_IRQ)
464 break;
467 if (pos == 0)
468 return;
470 base = devbase + pos;
471 writeb(0x01, base + 2);
472 n = (readl(base + 4) >> 16) & 0xff;
474 printk(KERN_INFO "mpic: - HT:%02x.%x [0x%02x] vendor %04x device %04x"
475 " has %d irqs\n",
476 devfn >> 3, devfn & 0x7, pos, vdid & 0xffff, vdid >> 16, n + 1);
478 for (i = 0; i <= n; i++) {
479 writeb(0x10 + 2 * i, base + 2);
480 tmp = readl(base + 4);
481 irq = (tmp >> 16) & 0xff;
482 DBG("HT PIC index 0x%x, irq 0x%x, tmp: %08x\n", i, irq, tmp);
483 /* mask it , will be unmasked later */
484 tmp |= 0x1;
485 writel(tmp, base + 4);
486 mpic->fixups[irq].index = i;
487 mpic->fixups[irq].base = base;
488 /* Apple HT PIC has a non-standard way of doing EOIs */
489 if ((vdid & 0xffff) == 0x106b)
490 mpic->fixups[irq].applebase = devbase + 0x60;
491 else
492 mpic->fixups[irq].applebase = NULL;
493 writeb(0x11 + 2 * i, base + 2);
494 mpic->fixups[irq].data = readl(base + 4) | 0x80000000;
499 static void __init mpic_scan_ht_pics(struct mpic *mpic)
501 unsigned int devfn;
502 u8 __iomem *cfgspace;
504 printk(KERN_INFO "mpic: Setting up HT PICs workarounds for U3/U4\n");
506 /* Allocate fixups array */
507 mpic->fixups = alloc_bootmem(128 * sizeof(struct mpic_irq_fixup));
508 BUG_ON(mpic->fixups == NULL);
509 memset(mpic->fixups, 0, 128 * sizeof(struct mpic_irq_fixup));
511 /* Init spinlock */
512 spin_lock_init(&mpic->fixup_lock);
514 /* Map U3 config space. We assume all IO-APICs are on the primary bus
515 * so we only need to map 64kB.
517 cfgspace = ioremap(0xf2000000, 0x10000);
518 BUG_ON(cfgspace == NULL);
520 /* Now we scan all slots. We do a very quick scan, we read the header
521 * type, vendor ID and device ID only, that's plenty enough
523 for (devfn = 0; devfn < 0x100; devfn++) {
524 u8 __iomem *devbase = cfgspace + (devfn << 8);
525 u8 hdr_type = readb(devbase + PCI_HEADER_TYPE);
526 u32 l = readl(devbase + PCI_VENDOR_ID);
527 u16 s;
529 DBG("devfn %x, l: %x\n", devfn, l);
531 /* If no device, skip */
532 if (l == 0xffffffff || l == 0x00000000 ||
533 l == 0x0000ffff || l == 0xffff0000)
534 goto next;
535 /* Check if is supports capability lists */
536 s = readw(devbase + PCI_STATUS);
537 if (!(s & PCI_STATUS_CAP_LIST))
538 goto next;
540 mpic_scan_ht_pic(mpic, devbase, devfn, l);
541 mpic_scan_ht_msi(mpic, devbase, devfn);
543 next:
544 /* next device, if function 0 */
545 if (PCI_FUNC(devfn) == 0 && (hdr_type & 0x80) == 0)
546 devfn += 7;
550 #else /* CONFIG_MPIC_U3_HT_IRQS */
552 static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
554 return 0;
557 static void __init mpic_scan_ht_pics(struct mpic *mpic)
561 #endif /* CONFIG_MPIC_U3_HT_IRQS */
564 #define mpic_irq_to_hw(virq) ((unsigned int)irq_map[virq].hwirq)
566 /* Find an mpic associated with a given linux interrupt */
567 static struct mpic *mpic_find(unsigned int irq, unsigned int *is_ipi)
569 unsigned int src = mpic_irq_to_hw(irq);
570 struct mpic *mpic;
572 if (irq < NUM_ISA_INTERRUPTS)
573 return NULL;
575 mpic = irq_desc[irq].chip_data;
577 if (is_ipi)
578 *is_ipi = (src >= mpic->ipi_vecs[0] &&
579 src <= mpic->ipi_vecs[3]);
581 return mpic;
584 /* Convert a cpu mask from logical to physical cpu numbers. */
585 static inline u32 mpic_physmask(u32 cpumask)
587 int i;
588 u32 mask = 0;
590 for (i = 0; i < NR_CPUS; ++i, cpumask >>= 1)
591 mask |= (cpumask & 1) << get_hard_smp_processor_id(i);
592 return mask;
595 #ifdef CONFIG_SMP
596 /* Get the mpic structure from the IPI number */
597 static inline struct mpic * mpic_from_ipi(unsigned int ipi)
599 return irq_desc[ipi].chip_data;
601 #endif
603 /* Get the mpic structure from the irq number */
604 static inline struct mpic * mpic_from_irq(unsigned int irq)
606 return irq_desc[irq].chip_data;
609 /* Send an EOI */
610 static inline void mpic_eoi(struct mpic *mpic)
612 mpic_cpu_write(MPIC_INFO(CPU_EOI), 0);
613 (void)mpic_cpu_read(MPIC_INFO(CPU_WHOAMI));
616 #ifdef CONFIG_SMP
617 static irqreturn_t mpic_ipi_action(int irq, void *data)
619 long ipi = (long)data;
621 smp_message_recv(ipi);
623 return IRQ_HANDLED;
625 #endif /* CONFIG_SMP */
628 * Linux descriptor level callbacks
632 void mpic_unmask_irq(unsigned int irq)
634 unsigned int loops = 100000;
635 struct mpic *mpic = mpic_from_irq(irq);
636 unsigned int src = mpic_irq_to_hw(irq);
638 DBG("%p: %s: enable_irq: %d (src %d)\n", mpic, mpic->name, irq, src);
640 mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
641 mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) &
642 ~MPIC_VECPRI_MASK);
643 /* make sure mask gets to controller before we return to user */
644 do {
645 if (!loops--) {
646 printk(KERN_ERR "mpic_enable_irq timeout\n");
647 break;
649 } while(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK);
652 void mpic_mask_irq(unsigned int irq)
654 unsigned int loops = 100000;
655 struct mpic *mpic = mpic_from_irq(irq);
656 unsigned int src = mpic_irq_to_hw(irq);
658 DBG("%s: disable_irq: %d (src %d)\n", mpic->name, irq, src);
660 mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
661 mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) |
662 MPIC_VECPRI_MASK);
664 /* make sure mask gets to controller before we return to user */
665 do {
666 if (!loops--) {
667 printk(KERN_ERR "mpic_enable_irq timeout\n");
668 break;
670 } while(!(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK));
673 void mpic_end_irq(unsigned int irq)
675 struct mpic *mpic = mpic_from_irq(irq);
677 #ifdef DEBUG_IRQ
678 DBG("%s: end_irq: %d\n", mpic->name, irq);
679 #endif
680 /* We always EOI on end_irq() even for edge interrupts since that
681 * should only lower the priority, the MPIC should have properly
682 * latched another edge interrupt coming in anyway
685 mpic_eoi(mpic);
688 #ifdef CONFIG_MPIC_U3_HT_IRQS
690 static void mpic_unmask_ht_irq(unsigned int irq)
692 struct mpic *mpic = mpic_from_irq(irq);
693 unsigned int src = mpic_irq_to_hw(irq);
695 mpic_unmask_irq(irq);
697 if (irq_desc[irq].status & IRQ_LEVEL)
698 mpic_ht_end_irq(mpic, src);
701 static unsigned int mpic_startup_ht_irq(unsigned int irq)
703 struct mpic *mpic = mpic_from_irq(irq);
704 unsigned int src = mpic_irq_to_hw(irq);
706 mpic_unmask_irq(irq);
707 mpic_startup_ht_interrupt(mpic, src, irq_desc[irq].status);
709 return 0;
712 static void mpic_shutdown_ht_irq(unsigned int irq)
714 struct mpic *mpic = mpic_from_irq(irq);
715 unsigned int src = mpic_irq_to_hw(irq);
717 mpic_shutdown_ht_interrupt(mpic, src, irq_desc[irq].status);
718 mpic_mask_irq(irq);
721 static void mpic_end_ht_irq(unsigned int irq)
723 struct mpic *mpic = mpic_from_irq(irq);
724 unsigned int src = mpic_irq_to_hw(irq);
726 #ifdef DEBUG_IRQ
727 DBG("%s: end_irq: %d\n", mpic->name, irq);
728 #endif
729 /* We always EOI on end_irq() even for edge interrupts since that
730 * should only lower the priority, the MPIC should have properly
731 * latched another edge interrupt coming in anyway
734 if (irq_desc[irq].status & IRQ_LEVEL)
735 mpic_ht_end_irq(mpic, src);
736 mpic_eoi(mpic);
738 #endif /* !CONFIG_MPIC_U3_HT_IRQS */
740 #ifdef CONFIG_SMP
742 static void mpic_unmask_ipi(unsigned int irq)
744 struct mpic *mpic = mpic_from_ipi(irq);
745 unsigned int src = mpic_irq_to_hw(irq) - mpic->ipi_vecs[0];
747 DBG("%s: enable_ipi: %d (ipi %d)\n", mpic->name, irq, src);
748 mpic_ipi_write(src, mpic_ipi_read(src) & ~MPIC_VECPRI_MASK);
751 static void mpic_mask_ipi(unsigned int irq)
753 /* NEVER disable an IPI... that's just plain wrong! */
756 static void mpic_end_ipi(unsigned int irq)
758 struct mpic *mpic = mpic_from_ipi(irq);
761 * IPIs are marked IRQ_PER_CPU. This has the side effect of
762 * preventing the IRQ_PENDING/IRQ_INPROGRESS logic from
763 * applying to them. We EOI them late to avoid re-entering.
764 * We mark IPI's with IRQF_DISABLED as they must run with
765 * irqs disabled.
767 mpic_eoi(mpic);
770 #endif /* CONFIG_SMP */
772 void mpic_set_affinity(unsigned int irq, cpumask_t cpumask)
774 struct mpic *mpic = mpic_from_irq(irq);
775 unsigned int src = mpic_irq_to_hw(irq);
777 cpumask_t tmp;
779 cpus_and(tmp, cpumask, cpu_online_map);
781 mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION),
782 mpic_physmask(cpus_addr(tmp)[0]));
785 static unsigned int mpic_type_to_vecpri(struct mpic *mpic, unsigned int type)
787 /* Now convert sense value */
788 switch(type & IRQ_TYPE_SENSE_MASK) {
789 case IRQ_TYPE_EDGE_RISING:
790 return MPIC_INFO(VECPRI_SENSE_EDGE) |
791 MPIC_INFO(VECPRI_POLARITY_POSITIVE);
792 case IRQ_TYPE_EDGE_FALLING:
793 case IRQ_TYPE_EDGE_BOTH:
794 return MPIC_INFO(VECPRI_SENSE_EDGE) |
795 MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
796 case IRQ_TYPE_LEVEL_HIGH:
797 return MPIC_INFO(VECPRI_SENSE_LEVEL) |
798 MPIC_INFO(VECPRI_POLARITY_POSITIVE);
799 case IRQ_TYPE_LEVEL_LOW:
800 default:
801 return MPIC_INFO(VECPRI_SENSE_LEVEL) |
802 MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
806 int mpic_set_irq_type(unsigned int virq, unsigned int flow_type)
808 struct mpic *mpic = mpic_from_irq(virq);
809 unsigned int src = mpic_irq_to_hw(virq);
810 struct irq_desc *desc = get_irq_desc(virq);
811 unsigned int vecpri, vold, vnew;
813 DBG("mpic: set_irq_type(mpic:@%p,virq:%d,src:0x%x,type:0x%x)\n",
814 mpic, virq, src, flow_type);
816 if (src >= mpic->irq_count)
817 return -EINVAL;
819 if (flow_type == IRQ_TYPE_NONE)
820 if (mpic->senses && src < mpic->senses_count)
821 flow_type = mpic->senses[src];
822 if (flow_type == IRQ_TYPE_NONE)
823 flow_type = IRQ_TYPE_LEVEL_LOW;
825 desc->status &= ~(IRQ_TYPE_SENSE_MASK | IRQ_LEVEL);
826 desc->status |= flow_type & IRQ_TYPE_SENSE_MASK;
827 if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
828 desc->status |= IRQ_LEVEL;
830 if (mpic_is_ht_interrupt(mpic, src))
831 vecpri = MPIC_VECPRI_POLARITY_POSITIVE |
832 MPIC_VECPRI_SENSE_EDGE;
833 else
834 vecpri = mpic_type_to_vecpri(mpic, flow_type);
836 vold = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
837 vnew = vold & ~(MPIC_INFO(VECPRI_POLARITY_MASK) |
838 MPIC_INFO(VECPRI_SENSE_MASK));
839 vnew |= vecpri;
840 if (vold != vnew)
841 mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI), vnew);
843 return 0;
846 void mpic_set_vector(unsigned int virq, unsigned int vector)
848 struct mpic *mpic = mpic_from_irq(virq);
849 unsigned int src = mpic_irq_to_hw(virq);
850 unsigned int vecpri;
852 DBG("mpic: set_vector(mpic:@%p,virq:%d,src:%d,vector:0x%x)\n",
853 mpic, virq, src, vector);
855 if (src >= mpic->irq_count)
856 return;
858 vecpri = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
859 vecpri = vecpri & ~MPIC_INFO(VECPRI_VECTOR_MASK);
860 vecpri |= vector;
861 mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI), vecpri);
864 static struct irq_chip mpic_irq_chip = {
865 .mask = mpic_mask_irq,
866 .unmask = mpic_unmask_irq,
867 .eoi = mpic_end_irq,
868 .set_type = mpic_set_irq_type,
871 #ifdef CONFIG_SMP
872 static struct irq_chip mpic_ipi_chip = {
873 .mask = mpic_mask_ipi,
874 .unmask = mpic_unmask_ipi,
875 .eoi = mpic_end_ipi,
877 #endif /* CONFIG_SMP */
879 #ifdef CONFIG_MPIC_U3_HT_IRQS
880 static struct irq_chip mpic_irq_ht_chip = {
881 .startup = mpic_startup_ht_irq,
882 .shutdown = mpic_shutdown_ht_irq,
883 .mask = mpic_mask_irq,
884 .unmask = mpic_unmask_ht_irq,
885 .eoi = mpic_end_ht_irq,
886 .set_type = mpic_set_irq_type,
888 #endif /* CONFIG_MPIC_U3_HT_IRQS */
891 static int mpic_host_match(struct irq_host *h, struct device_node *node)
893 /* Exact match, unless mpic node is NULL */
894 return h->of_node == NULL || h->of_node == node;
897 static int mpic_host_map(struct irq_host *h, unsigned int virq,
898 irq_hw_number_t hw)
900 struct mpic *mpic = h->host_data;
901 struct irq_chip *chip;
903 DBG("mpic: map virq %d, hwirq 0x%lx\n", virq, hw);
905 if (hw == mpic->spurious_vec)
906 return -EINVAL;
907 if (mpic->protected && test_bit(hw, mpic->protected))
908 return -EINVAL;
910 #ifdef CONFIG_SMP
911 else if (hw >= mpic->ipi_vecs[0]) {
912 WARN_ON(!(mpic->flags & MPIC_PRIMARY));
914 DBG("mpic: mapping as IPI\n");
915 set_irq_chip_data(virq, mpic);
916 set_irq_chip_and_handler(virq, &mpic->hc_ipi,
917 handle_percpu_irq);
918 return 0;
920 #endif /* CONFIG_SMP */
922 if (hw >= mpic->irq_count)
923 return -EINVAL;
925 mpic_msi_reserve_hwirq(mpic, hw);
927 /* Default chip */
928 chip = &mpic->hc_irq;
930 #ifdef CONFIG_MPIC_U3_HT_IRQS
931 /* Check for HT interrupts, override vecpri */
932 if (mpic_is_ht_interrupt(mpic, hw))
933 chip = &mpic->hc_ht_irq;
934 #endif /* CONFIG_MPIC_U3_HT_IRQS */
936 DBG("mpic: mapping to irq chip @%p\n", chip);
938 set_irq_chip_data(virq, mpic);
939 set_irq_chip_and_handler(virq, chip, handle_fasteoi_irq);
941 /* Set default irq type */
942 set_irq_type(virq, IRQ_TYPE_NONE);
944 return 0;
947 static int mpic_host_xlate(struct irq_host *h, struct device_node *ct,
948 u32 *intspec, unsigned int intsize,
949 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
952 static unsigned char map_mpic_senses[4] = {
953 IRQ_TYPE_EDGE_RISING,
954 IRQ_TYPE_LEVEL_LOW,
955 IRQ_TYPE_LEVEL_HIGH,
956 IRQ_TYPE_EDGE_FALLING,
959 *out_hwirq = intspec[0];
960 if (intsize > 1) {
961 u32 mask = 0x3;
963 /* Apple invented a new race of encoding on machines with
964 * an HT APIC. They encode, among others, the index within
965 * the HT APIC. We don't care about it here since thankfully,
966 * it appears that they have the APIC already properly
967 * configured, and thus our current fixup code that reads the
968 * APIC config works fine. However, we still need to mask out
969 * bits in the specifier to make sure we only get bit 0 which
970 * is the level/edge bit (the only sense bit exposed by Apple),
971 * as their bit 1 means something else.
973 if (machine_is(powermac))
974 mask = 0x1;
975 *out_flags = map_mpic_senses[intspec[1] & mask];
976 } else
977 *out_flags = IRQ_TYPE_NONE;
979 DBG("mpic: xlate (%d cells: 0x%08x 0x%08x) to line 0x%lx sense 0x%x\n",
980 intsize, intspec[0], intspec[1], *out_hwirq, *out_flags);
982 return 0;
985 static struct irq_host_ops mpic_host_ops = {
986 .match = mpic_host_match,
987 .map = mpic_host_map,
988 .xlate = mpic_host_xlate,
992 * Exported functions
995 struct mpic * __init mpic_alloc(struct device_node *node,
996 phys_addr_t phys_addr,
997 unsigned int flags,
998 unsigned int isu_size,
999 unsigned int irq_count,
1000 const char *name)
1002 struct mpic *mpic;
1003 u32 reg;
1004 const char *vers;
1005 int i;
1006 int intvec_top;
1007 u64 paddr = phys_addr;
1009 mpic = alloc_bootmem(sizeof(struct mpic));
1010 if (mpic == NULL)
1011 return NULL;
1013 memset(mpic, 0, sizeof(struct mpic));
1014 mpic->name = name;
1016 mpic->irqhost = irq_alloc_host(of_node_get(node), IRQ_HOST_MAP_LINEAR,
1017 isu_size, &mpic_host_ops,
1018 flags & MPIC_LARGE_VECTORS ? 2048 : 256);
1019 if (mpic->irqhost == NULL) {
1020 of_node_put(node);
1021 return NULL;
1024 mpic->irqhost->host_data = mpic;
1025 mpic->hc_irq = mpic_irq_chip;
1026 mpic->hc_irq.typename = name;
1027 if (flags & MPIC_PRIMARY)
1028 mpic->hc_irq.set_affinity = mpic_set_affinity;
1029 #ifdef CONFIG_MPIC_U3_HT_IRQS
1030 mpic->hc_ht_irq = mpic_irq_ht_chip;
1031 mpic->hc_ht_irq.typename = name;
1032 if (flags & MPIC_PRIMARY)
1033 mpic->hc_ht_irq.set_affinity = mpic_set_affinity;
1034 #endif /* CONFIG_MPIC_U3_HT_IRQS */
1036 #ifdef CONFIG_SMP
1037 mpic->hc_ipi = mpic_ipi_chip;
1038 mpic->hc_ipi.typename = name;
1039 #endif /* CONFIG_SMP */
1041 mpic->flags = flags;
1042 mpic->isu_size = isu_size;
1043 mpic->irq_count = irq_count;
1044 mpic->num_sources = 0; /* so far */
1046 if (flags & MPIC_LARGE_VECTORS)
1047 intvec_top = 2047;
1048 else
1049 intvec_top = 255;
1051 mpic->timer_vecs[0] = intvec_top - 8;
1052 mpic->timer_vecs[1] = intvec_top - 7;
1053 mpic->timer_vecs[2] = intvec_top - 6;
1054 mpic->timer_vecs[3] = intvec_top - 5;
1055 mpic->ipi_vecs[0] = intvec_top - 4;
1056 mpic->ipi_vecs[1] = intvec_top - 3;
1057 mpic->ipi_vecs[2] = intvec_top - 2;
1058 mpic->ipi_vecs[3] = intvec_top - 1;
1059 mpic->spurious_vec = intvec_top;
1061 /* Check for "big-endian" in device-tree */
1062 if (node && of_get_property(node, "big-endian", NULL) != NULL)
1063 mpic->flags |= MPIC_BIG_ENDIAN;
1065 /* Look for protected sources */
1066 if (node) {
1067 unsigned int psize, bits, mapsize;
1068 const u32 *psrc =
1069 of_get_property(node, "protected-sources", &psize);
1070 if (psrc) {
1071 psize /= 4;
1072 bits = intvec_top + 1;
1073 mapsize = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1074 mpic->protected = alloc_bootmem(mapsize);
1075 BUG_ON(mpic->protected == NULL);
1076 memset(mpic->protected, 0, mapsize);
1077 for (i = 0; i < psize; i++) {
1078 if (psrc[i] > intvec_top)
1079 continue;
1080 __set_bit(psrc[i], mpic->protected);
1085 #ifdef CONFIG_MPIC_WEIRD
1086 mpic->hw_set = mpic_infos[MPIC_GET_REGSET(flags)];
1087 #endif
1089 /* default register type */
1090 mpic->reg_type = (flags & MPIC_BIG_ENDIAN) ?
1091 mpic_access_mmio_be : mpic_access_mmio_le;
1093 /* If no physical address is passed in, a device-node is mandatory */
1094 BUG_ON(paddr == 0 && node == NULL);
1096 /* If no physical address passed in, check if it's dcr based */
1097 if (paddr == 0 && of_get_property(node, "dcr-reg", NULL) != NULL) {
1098 #ifdef CONFIG_PPC_DCR
1099 mpic->flags |= MPIC_USES_DCR;
1100 mpic->reg_type = mpic_access_dcr;
1101 #else
1102 BUG();
1103 #endif /* CONFIG_PPC_DCR */
1106 /* If the MPIC is not DCR based, and no physical address was passed
1107 * in, try to obtain one
1109 if (paddr == 0 && !(mpic->flags & MPIC_USES_DCR)) {
1110 const u32 *reg;
1111 reg = of_get_property(node, "reg", NULL);
1112 BUG_ON(reg == NULL);
1113 paddr = of_translate_address(node, reg);
1114 BUG_ON(paddr == OF_BAD_ADDR);
1117 /* Map the global registers */
1118 mpic_map(mpic, paddr, &mpic->gregs, MPIC_INFO(GREG_BASE), 0x1000);
1119 mpic_map(mpic, paddr, &mpic->tmregs, MPIC_INFO(TIMER_BASE), 0x1000);
1121 /* Reset */
1122 if (flags & MPIC_WANTS_RESET) {
1123 mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1124 mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1125 | MPIC_GREG_GCONF_RESET);
1126 while( mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1127 & MPIC_GREG_GCONF_RESET)
1128 mb();
1131 if (flags & MPIC_ENABLE_MCK)
1132 mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1133 mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1134 | MPIC_GREG_GCONF_MCK);
1136 /* Read feature register, calculate num CPUs and, for non-ISU
1137 * MPICs, num sources as well. On ISU MPICs, sources are counted
1138 * as ISUs are added
1140 reg = mpic_read(mpic->gregs, MPIC_INFO(GREG_FEATURE_0));
1141 mpic->num_cpus = ((reg & MPIC_GREG_FEATURE_LAST_CPU_MASK)
1142 >> MPIC_GREG_FEATURE_LAST_CPU_SHIFT) + 1;
1143 if (isu_size == 0)
1144 mpic->num_sources = ((reg & MPIC_GREG_FEATURE_LAST_SRC_MASK)
1145 >> MPIC_GREG_FEATURE_LAST_SRC_SHIFT) + 1;
1147 /* Map the per-CPU registers */
1148 for (i = 0; i < mpic->num_cpus; i++) {
1149 mpic_map(mpic, paddr, &mpic->cpuregs[i],
1150 MPIC_INFO(CPU_BASE) + i * MPIC_INFO(CPU_STRIDE),
1151 0x1000);
1154 /* Initialize main ISU if none provided */
1155 if (mpic->isu_size == 0) {
1156 mpic->isu_size = mpic->num_sources;
1157 mpic_map(mpic, paddr, &mpic->isus[0],
1158 MPIC_INFO(IRQ_BASE), MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
1160 mpic->isu_shift = 1 + __ilog2(mpic->isu_size - 1);
1161 mpic->isu_mask = (1 << mpic->isu_shift) - 1;
1163 /* Display version */
1164 switch (reg & MPIC_GREG_FEATURE_VERSION_MASK) {
1165 case 1:
1166 vers = "1.0";
1167 break;
1168 case 2:
1169 vers = "1.2";
1170 break;
1171 case 3:
1172 vers = "1.3";
1173 break;
1174 default:
1175 vers = "<unknown>";
1176 break;
1178 printk(KERN_INFO "mpic: Setting up MPIC \"%s\" version %s at %llx,"
1179 " max %d CPUs\n",
1180 name, vers, (unsigned long long)paddr, mpic->num_cpus);
1181 printk(KERN_INFO "mpic: ISU size: %d, shift: %d, mask: %x\n",
1182 mpic->isu_size, mpic->isu_shift, mpic->isu_mask);
1184 mpic->next = mpics;
1185 mpics = mpic;
1187 if (flags & MPIC_PRIMARY) {
1188 mpic_primary = mpic;
1189 irq_set_default_host(mpic->irqhost);
1192 return mpic;
1195 void __init mpic_assign_isu(struct mpic *mpic, unsigned int isu_num,
1196 phys_addr_t paddr)
1198 unsigned int isu_first = isu_num * mpic->isu_size;
1200 BUG_ON(isu_num >= MPIC_MAX_ISU);
1202 mpic_map(mpic, paddr, &mpic->isus[isu_num], 0,
1203 MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
1204 if ((isu_first + mpic->isu_size) > mpic->num_sources)
1205 mpic->num_sources = isu_first + mpic->isu_size;
1208 void __init mpic_set_default_senses(struct mpic *mpic, u8 *senses, int count)
1210 mpic->senses = senses;
1211 mpic->senses_count = count;
1214 void __init mpic_init(struct mpic *mpic)
1216 int i;
1218 BUG_ON(mpic->num_sources == 0);
1220 printk(KERN_INFO "mpic: Initializing for %d sources\n", mpic->num_sources);
1222 /* Set current processor priority to max */
1223 mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
1225 /* Initialize timers: just disable them all */
1226 for (i = 0; i < 4; i++) {
1227 mpic_write(mpic->tmregs,
1228 i * MPIC_INFO(TIMER_STRIDE) +
1229 MPIC_INFO(TIMER_DESTINATION), 0);
1230 mpic_write(mpic->tmregs,
1231 i * MPIC_INFO(TIMER_STRIDE) +
1232 MPIC_INFO(TIMER_VECTOR_PRI),
1233 MPIC_VECPRI_MASK |
1234 (mpic->timer_vecs[0] + i));
1237 /* Initialize IPIs to our reserved vectors and mark them disabled for now */
1238 mpic_test_broken_ipi(mpic);
1239 for (i = 0; i < 4; i++) {
1240 mpic_ipi_write(i,
1241 MPIC_VECPRI_MASK |
1242 (10 << MPIC_VECPRI_PRIORITY_SHIFT) |
1243 (mpic->ipi_vecs[0] + i));
1246 /* Initialize interrupt sources */
1247 if (mpic->irq_count == 0)
1248 mpic->irq_count = mpic->num_sources;
1250 /* Do the HT PIC fixups on U3 broken mpic */
1251 DBG("MPIC flags: %x\n", mpic->flags);
1252 if ((mpic->flags & MPIC_U3_HT_IRQS) && (mpic->flags & MPIC_PRIMARY)) {
1253 mpic_scan_ht_pics(mpic);
1254 mpic_u3msi_init(mpic);
1257 mpic_pasemi_msi_init(mpic);
1259 for (i = 0; i < mpic->num_sources; i++) {
1260 /* start with vector = source number, and masked */
1261 u32 vecpri = MPIC_VECPRI_MASK | i |
1262 (8 << MPIC_VECPRI_PRIORITY_SHIFT);
1264 /* check if protected */
1265 if (mpic->protected && test_bit(i, mpic->protected))
1266 continue;
1267 /* init hw */
1268 mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI), vecpri);
1269 mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
1270 1 << hard_smp_processor_id());
1273 /* Init spurious vector */
1274 mpic_write(mpic->gregs, MPIC_INFO(GREG_SPURIOUS), mpic->spurious_vec);
1276 /* Disable 8259 passthrough, if supported */
1277 if (!(mpic->flags & MPIC_NO_PTHROU_DIS))
1278 mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1279 mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1280 | MPIC_GREG_GCONF_8259_PTHROU_DIS);
1282 if (mpic->flags & MPIC_NO_BIAS)
1283 mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1284 mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1285 | MPIC_GREG_GCONF_NO_BIAS);
1287 /* Set current processor priority to 0 */
1288 mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
1290 #ifdef CONFIG_PM
1291 /* allocate memory to save mpic state */
1292 mpic->save_data = alloc_bootmem(mpic->num_sources * sizeof(struct mpic_irq_save));
1293 BUG_ON(mpic->save_data == NULL);
1294 #endif
1297 void __init mpic_set_clk_ratio(struct mpic *mpic, u32 clock_ratio)
1299 u32 v;
1301 v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
1302 v &= ~MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO_MASK;
1303 v |= MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO(clock_ratio);
1304 mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
1307 void __init mpic_set_serial_int(struct mpic *mpic, int enable)
1309 unsigned long flags;
1310 u32 v;
1312 spin_lock_irqsave(&mpic_lock, flags);
1313 v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
1314 if (enable)
1315 v |= MPIC_GREG_GLOBAL_CONF_1_SIE;
1316 else
1317 v &= ~MPIC_GREG_GLOBAL_CONF_1_SIE;
1318 mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
1319 spin_unlock_irqrestore(&mpic_lock, flags);
1322 void mpic_irq_set_priority(unsigned int irq, unsigned int pri)
1324 int is_ipi;
1325 struct mpic *mpic = mpic_find(irq, &is_ipi);
1326 unsigned int src = mpic_irq_to_hw(irq);
1327 unsigned long flags;
1328 u32 reg;
1330 spin_lock_irqsave(&mpic_lock, flags);
1331 if (is_ipi) {
1332 reg = mpic_ipi_read(src - mpic->ipi_vecs[0]) &
1333 ~MPIC_VECPRI_PRIORITY_MASK;
1334 mpic_ipi_write(src - mpic->ipi_vecs[0],
1335 reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
1336 } else {
1337 reg = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI))
1338 & ~MPIC_VECPRI_PRIORITY_MASK;
1339 mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
1340 reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
1342 spin_unlock_irqrestore(&mpic_lock, flags);
1345 unsigned int mpic_irq_get_priority(unsigned int irq)
1347 int is_ipi;
1348 struct mpic *mpic = mpic_find(irq, &is_ipi);
1349 unsigned int src = mpic_irq_to_hw(irq);
1350 unsigned long flags;
1351 u32 reg;
1353 spin_lock_irqsave(&mpic_lock, flags);
1354 if (is_ipi)
1355 reg = mpic_ipi_read(src = mpic->ipi_vecs[0]);
1356 else
1357 reg = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
1358 spin_unlock_irqrestore(&mpic_lock, flags);
1359 return (reg & MPIC_VECPRI_PRIORITY_MASK) >> MPIC_VECPRI_PRIORITY_SHIFT;
1362 void mpic_setup_this_cpu(void)
1364 #ifdef CONFIG_SMP
1365 struct mpic *mpic = mpic_primary;
1366 unsigned long flags;
1367 u32 msk = 1 << hard_smp_processor_id();
1368 unsigned int i;
1370 BUG_ON(mpic == NULL);
1372 DBG("%s: setup_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
1374 spin_lock_irqsave(&mpic_lock, flags);
1376 /* let the mpic know we want intrs. default affinity is 0xffffffff
1377 * until changed via /proc. That's how it's done on x86. If we want
1378 * it differently, then we should make sure we also change the default
1379 * values of irq_desc[].affinity in irq.c.
1381 if (distribute_irqs) {
1382 for (i = 0; i < mpic->num_sources ; i++)
1383 mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
1384 mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) | msk);
1387 /* Set current processor priority to 0 */
1388 mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
1390 spin_unlock_irqrestore(&mpic_lock, flags);
1391 #endif /* CONFIG_SMP */
1394 int mpic_cpu_get_priority(void)
1396 struct mpic *mpic = mpic_primary;
1398 return mpic_cpu_read(MPIC_INFO(CPU_CURRENT_TASK_PRI));
1401 void mpic_cpu_set_priority(int prio)
1403 struct mpic *mpic = mpic_primary;
1405 prio &= MPIC_CPU_TASKPRI_MASK;
1406 mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), prio);
1410 * XXX: someone who knows mpic should check this.
1411 * do we need to eoi the ipi including for kexec cpu here (see xics comments)?
1412 * or can we reset the mpic in the new kernel?
1414 void mpic_teardown_this_cpu(int secondary)
1416 struct mpic *mpic = mpic_primary;
1417 unsigned long flags;
1418 u32 msk = 1 << hard_smp_processor_id();
1419 unsigned int i;
1421 BUG_ON(mpic == NULL);
1423 DBG("%s: teardown_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
1424 spin_lock_irqsave(&mpic_lock, flags);
1426 /* let the mpic know we don't want intrs. */
1427 for (i = 0; i < mpic->num_sources ; i++)
1428 mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
1429 mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) & ~msk);
1431 /* Set current processor priority to max */
1432 mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
1434 spin_unlock_irqrestore(&mpic_lock, flags);
1438 void mpic_send_ipi(unsigned int ipi_no, unsigned int cpu_mask)
1440 struct mpic *mpic = mpic_primary;
1442 BUG_ON(mpic == NULL);
1444 #ifdef DEBUG_IPI
1445 DBG("%s: send_ipi(ipi_no: %d)\n", mpic->name, ipi_no);
1446 #endif
1448 mpic_cpu_write(MPIC_INFO(CPU_IPI_DISPATCH_0) +
1449 ipi_no * MPIC_INFO(CPU_IPI_DISPATCH_STRIDE),
1450 mpic_physmask(cpu_mask & cpus_addr(cpu_online_map)[0]));
1453 static unsigned int _mpic_get_one_irq(struct mpic *mpic, int reg)
1455 u32 src;
1457 src = mpic_cpu_read(reg) & MPIC_INFO(VECPRI_VECTOR_MASK);
1458 #ifdef DEBUG_LOW
1459 DBG("%s: get_one_irq(reg 0x%x): %d\n", mpic->name, reg, src);
1460 #endif
1461 if (unlikely(src == mpic->spurious_vec)) {
1462 if (mpic->flags & MPIC_SPV_EOI)
1463 mpic_eoi(mpic);
1464 return NO_IRQ;
1466 if (unlikely(mpic->protected && test_bit(src, mpic->protected))) {
1467 if (printk_ratelimit())
1468 printk(KERN_WARNING "%s: Got protected source %d !\n",
1469 mpic->name, (int)src);
1470 mpic_eoi(mpic);
1471 return NO_IRQ;
1474 return irq_linear_revmap(mpic->irqhost, src);
1477 unsigned int mpic_get_one_irq(struct mpic *mpic)
1479 return _mpic_get_one_irq(mpic, MPIC_INFO(CPU_INTACK));
1482 unsigned int mpic_get_irq(void)
1484 struct mpic *mpic = mpic_primary;
1486 BUG_ON(mpic == NULL);
1488 return mpic_get_one_irq(mpic);
1491 unsigned int mpic_get_mcirq(void)
1493 struct mpic *mpic = mpic_primary;
1495 BUG_ON(mpic == NULL);
1497 return _mpic_get_one_irq(mpic, MPIC_INFO(CPU_MCACK));
1500 #ifdef CONFIG_SMP
1501 void mpic_request_ipis(void)
1503 struct mpic *mpic = mpic_primary;
1504 long i, err;
1505 static char *ipi_names[] = {
1506 "IPI0 (call function)",
1507 "IPI1 (reschedule)",
1508 "IPI2 (unused)",
1509 "IPI3 (debugger break)",
1511 BUG_ON(mpic == NULL);
1513 printk(KERN_INFO "mpic: requesting IPIs ... \n");
1515 for (i = 0; i < 4; i++) {
1516 unsigned int vipi = irq_create_mapping(mpic->irqhost,
1517 mpic->ipi_vecs[0] + i);
1518 if (vipi == NO_IRQ) {
1519 printk(KERN_ERR "Failed to map IPI %ld\n", i);
1520 break;
1522 err = request_irq(vipi, mpic_ipi_action,
1523 IRQF_DISABLED|IRQF_PERCPU,
1524 ipi_names[i], (void *)i);
1525 if (err) {
1526 printk(KERN_ERR "Request of irq %d for IPI %ld failed\n",
1527 vipi, i);
1528 break;
1533 void smp_mpic_message_pass(int target, int msg)
1535 /* make sure we're sending something that translates to an IPI */
1536 if ((unsigned int)msg > 3) {
1537 printk("SMP %d: smp_message_pass: unknown msg %d\n",
1538 smp_processor_id(), msg);
1539 return;
1541 switch (target) {
1542 case MSG_ALL:
1543 mpic_send_ipi(msg, 0xffffffff);
1544 break;
1545 case MSG_ALL_BUT_SELF:
1546 mpic_send_ipi(msg, 0xffffffff & ~(1 << smp_processor_id()));
1547 break;
1548 default:
1549 mpic_send_ipi(msg, 1 << target);
1550 break;
1554 int __init smp_mpic_probe(void)
1556 int nr_cpus;
1558 DBG("smp_mpic_probe()...\n");
1560 nr_cpus = cpus_weight(cpu_possible_map);
1562 DBG("nr_cpus: %d\n", nr_cpus);
1564 if (nr_cpus > 1)
1565 mpic_request_ipis();
1567 return nr_cpus;
1570 void __devinit smp_mpic_setup_cpu(int cpu)
1572 mpic_setup_this_cpu();
1574 #endif /* CONFIG_SMP */
1576 #ifdef CONFIG_PM
1577 static int mpic_suspend(struct sys_device *dev, pm_message_t state)
1579 struct mpic *mpic = container_of(dev, struct mpic, sysdev);
1580 int i;
1582 for (i = 0; i < mpic->num_sources; i++) {
1583 mpic->save_data[i].vecprio =
1584 mpic_irq_read(i, MPIC_INFO(IRQ_VECTOR_PRI));
1585 mpic->save_data[i].dest =
1586 mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION));
1589 return 0;
1592 static int mpic_resume(struct sys_device *dev)
1594 struct mpic *mpic = container_of(dev, struct mpic, sysdev);
1595 int i;
1597 for (i = 0; i < mpic->num_sources; i++) {
1598 mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI),
1599 mpic->save_data[i].vecprio);
1600 mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
1601 mpic->save_data[i].dest);
1603 #ifdef CONFIG_MPIC_U3_HT_IRQS
1605 struct mpic_irq_fixup *fixup = &mpic->fixups[i];
1607 if (fixup->base) {
1608 /* we use the lowest bit in an inverted meaning */
1609 if ((mpic->save_data[i].fixup_data & 1) == 0)
1610 continue;
1612 /* Enable and configure */
1613 writeb(0x10 + 2 * fixup->index, fixup->base + 2);
1615 writel(mpic->save_data[i].fixup_data & ~1,
1616 fixup->base + 4);
1619 #endif
1620 } /* end for loop */
1622 return 0;
1624 #endif
1626 static struct sysdev_class mpic_sysclass = {
1627 #ifdef CONFIG_PM
1628 .resume = mpic_resume,
1629 .suspend = mpic_suspend,
1630 #endif
1631 .name = "mpic",
1634 static int mpic_init_sys(void)
1636 struct mpic *mpic = mpics;
1637 int error, id = 0;
1639 error = sysdev_class_register(&mpic_sysclass);
1641 while (mpic && !error) {
1642 mpic->sysdev.cls = &mpic_sysclass;
1643 mpic->sysdev.id = id++;
1644 error = sysdev_register(&mpic->sysdev);
1645 mpic = mpic->next;
1647 return error;
1650 device_initcall(mpic_init_sys);