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
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>
33 #include <asm/pgtable.h>
35 #include <asm/machdep.h>
40 #define DBG(fmt...) printk(fmt)
45 static struct mpic *mpics;
46 static struct mpic *mpic_primary;
47 static DEFINE_SPINLOCK(mpic_lock);
49 #ifdef CONFIG_PPC32 /* XXX for now */
50 #ifdef CONFIG_IRQ_ALL_CPUS
51 #define distribute_irqs (1)
53 #define distribute_irqs (0)
58 * Register accessor functions
62 static inline u32 _mpic_read(unsigned int be, volatile u32 __iomem *base,
66 return in_be32(base + (reg >> 2));
68 return in_le32(base + (reg >> 2));
71 static inline void _mpic_write(unsigned int be, volatile u32 __iomem *base,
72 unsigned int reg, u32 value)
75 out_be32(base + (reg >> 2), value);
77 out_le32(base + (reg >> 2), value);
80 static inline u32 _mpic_ipi_read(struct mpic *mpic, unsigned int ipi)
82 unsigned int be = (mpic->flags & MPIC_BIG_ENDIAN) != 0;
83 unsigned int offset = MPIC_GREG_IPI_VECTOR_PRI_0 + (ipi * 0x10);
85 if (mpic->flags & MPIC_BROKEN_IPI)
87 return _mpic_read(be, mpic->gregs, offset);
90 static inline void _mpic_ipi_write(struct mpic *mpic, unsigned int ipi, u32 value)
92 unsigned int offset = MPIC_GREG_IPI_VECTOR_PRI_0 + (ipi * 0x10);
94 _mpic_write(mpic->flags & MPIC_BIG_ENDIAN, mpic->gregs, offset, value);
97 static inline u32 _mpic_cpu_read(struct mpic *mpic, unsigned int reg)
101 if (mpic->flags & MPIC_PRIMARY)
102 cpu = hard_smp_processor_id();
103 return _mpic_read(mpic->flags & MPIC_BIG_ENDIAN,
104 mpic->cpuregs[cpu], reg);
107 static inline void _mpic_cpu_write(struct mpic *mpic, unsigned int reg, u32 value)
109 unsigned int cpu = 0;
111 if (mpic->flags & MPIC_PRIMARY)
112 cpu = hard_smp_processor_id();
114 _mpic_write(mpic->flags & MPIC_BIG_ENDIAN, mpic->cpuregs[cpu], reg, value);
117 static inline u32 _mpic_irq_read(struct mpic *mpic, unsigned int src_no, unsigned int reg)
119 unsigned int isu = src_no >> mpic->isu_shift;
120 unsigned int idx = src_no & mpic->isu_mask;
122 return _mpic_read(mpic->flags & MPIC_BIG_ENDIAN, mpic->isus[isu],
123 reg + (idx * MPIC_IRQ_STRIDE));
126 static inline void _mpic_irq_write(struct mpic *mpic, unsigned int src_no,
127 unsigned int reg, u32 value)
129 unsigned int isu = src_no >> mpic->isu_shift;
130 unsigned int idx = src_no & mpic->isu_mask;
132 _mpic_write(mpic->flags & MPIC_BIG_ENDIAN, mpic->isus[isu],
133 reg + (idx * MPIC_IRQ_STRIDE), value);
136 #define mpic_read(b,r) _mpic_read(mpic->flags & MPIC_BIG_ENDIAN,(b),(r))
137 #define mpic_write(b,r,v) _mpic_write(mpic->flags & MPIC_BIG_ENDIAN,(b),(r),(v))
138 #define mpic_ipi_read(i) _mpic_ipi_read(mpic,(i))
139 #define mpic_ipi_write(i,v) _mpic_ipi_write(mpic,(i),(v))
140 #define mpic_cpu_read(i) _mpic_cpu_read(mpic,(i))
141 #define mpic_cpu_write(i,v) _mpic_cpu_write(mpic,(i),(v))
142 #define mpic_irq_read(s,r) _mpic_irq_read(mpic,(s),(r))
143 #define mpic_irq_write(s,r,v) _mpic_irq_write(mpic,(s),(r),(v))
147 * Low level utility functions
152 /* Check if we have one of those nice broken MPICs with a flipped endian on
153 * reads from IPI registers
155 static void __init mpic_test_broken_ipi(struct mpic *mpic)
159 mpic_write(mpic->gregs, MPIC_GREG_IPI_VECTOR_PRI_0, MPIC_VECPRI_MASK);
160 r = mpic_read(mpic->gregs, MPIC_GREG_IPI_VECTOR_PRI_0);
162 if (r == le32_to_cpu(MPIC_VECPRI_MASK)) {
163 printk(KERN_INFO "mpic: Detected reversed IPI registers\n");
164 mpic->flags |= MPIC_BROKEN_IPI;
168 #ifdef CONFIG_MPIC_BROKEN_U3
170 /* Test if an interrupt is sourced from HyperTransport (used on broken U3s)
171 * to force the edge setting on the MPIC and do the ack workaround.
173 static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
175 if (source >= 128 || !mpic->fixups)
177 return mpic->fixups[source].base != NULL;
181 static inline void mpic_ht_end_irq(struct mpic *mpic, unsigned int source)
183 struct mpic_irq_fixup *fixup = &mpic->fixups[source];
185 if (fixup->applebase) {
186 unsigned int soff = (fixup->index >> 3) & ~3;
187 unsigned int mask = 1U << (fixup->index & 0x1f);
188 writel(mask, fixup->applebase + soff);
190 spin_lock(&mpic->fixup_lock);
191 writeb(0x11 + 2 * fixup->index, fixup->base + 2);
192 writel(fixup->data, fixup->base + 4);
193 spin_unlock(&mpic->fixup_lock);
197 static void mpic_startup_ht_interrupt(struct mpic *mpic, unsigned int source,
198 unsigned int irqflags)
200 struct mpic_irq_fixup *fixup = &mpic->fixups[source];
204 if (fixup->base == NULL)
207 DBG("startup_ht_interrupt(%u, %u) index: %d\n",
208 source, irqflags, fixup->index);
209 spin_lock_irqsave(&mpic->fixup_lock, flags);
210 /* Enable and configure */
211 writeb(0x10 + 2 * fixup->index, fixup->base + 2);
212 tmp = readl(fixup->base + 4);
214 if (irqflags & IRQ_LEVEL)
216 writel(tmp, fixup->base + 4);
217 spin_unlock_irqrestore(&mpic->fixup_lock, flags);
220 static void mpic_shutdown_ht_interrupt(struct mpic *mpic, unsigned int source,
221 unsigned int irqflags)
223 struct mpic_irq_fixup *fixup = &mpic->fixups[source];
227 if (fixup->base == NULL)
230 DBG("shutdown_ht_interrupt(%u, %u)\n", source, irqflags);
233 spin_lock_irqsave(&mpic->fixup_lock, flags);
234 writeb(0x10 + 2 * fixup->index, fixup->base + 2);
235 tmp = readl(fixup->base + 4);
237 writel(tmp, fixup->base + 4);
238 spin_unlock_irqrestore(&mpic->fixup_lock, flags);
241 static void __init mpic_scan_ht_pic(struct mpic *mpic, u8 __iomem *devbase,
242 unsigned int devfn, u32 vdid)
249 for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
250 pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
251 u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
252 if (id == PCI_CAP_ID_HT_IRQCONF) {
253 id = readb(devbase + pos + 3);
261 base = devbase + pos;
262 writeb(0x01, base + 2);
263 n = (readl(base + 4) >> 16) & 0xff;
265 printk(KERN_INFO "mpic: - HT:%02x.%x [0x%02x] vendor %04x device %04x"
267 devfn >> 3, devfn & 0x7, pos, vdid & 0xffff, vdid >> 16, n + 1);
269 for (i = 0; i <= n; i++) {
270 writeb(0x10 + 2 * i, base + 2);
271 tmp = readl(base + 4);
272 irq = (tmp >> 16) & 0xff;
273 DBG("HT PIC index 0x%x, irq 0x%x, tmp: %08x\n", i, irq, tmp);
274 /* mask it , will be unmasked later */
276 writel(tmp, base + 4);
277 mpic->fixups[irq].index = i;
278 mpic->fixups[irq].base = base;
279 /* Apple HT PIC has a non-standard way of doing EOIs */
280 if ((vdid & 0xffff) == 0x106b)
281 mpic->fixups[irq].applebase = devbase + 0x60;
283 mpic->fixups[irq].applebase = NULL;
284 writeb(0x11 + 2 * i, base + 2);
285 mpic->fixups[irq].data = readl(base + 4) | 0x80000000;
290 static void __init mpic_scan_ht_pics(struct mpic *mpic)
293 u8 __iomem *cfgspace;
295 printk(KERN_INFO "mpic: Setting up HT PICs workarounds for U3/U4\n");
297 /* Allocate fixups array */
298 mpic->fixups = alloc_bootmem(128 * sizeof(struct mpic_irq_fixup));
299 BUG_ON(mpic->fixups == NULL);
300 memset(mpic->fixups, 0, 128 * sizeof(struct mpic_irq_fixup));
303 spin_lock_init(&mpic->fixup_lock);
305 /* Map U3 config space. We assume all IO-APICs are on the primary bus
306 * so we only need to map 64kB.
308 cfgspace = ioremap(0xf2000000, 0x10000);
309 BUG_ON(cfgspace == NULL);
311 /* Now we scan all slots. We do a very quick scan, we read the header
312 * type, vendor ID and device ID only, that's plenty enough
314 for (devfn = 0; devfn < 0x100; devfn++) {
315 u8 __iomem *devbase = cfgspace + (devfn << 8);
316 u8 hdr_type = readb(devbase + PCI_HEADER_TYPE);
317 u32 l = readl(devbase + PCI_VENDOR_ID);
320 DBG("devfn %x, l: %x\n", devfn, l);
322 /* If no device, skip */
323 if (l == 0xffffffff || l == 0x00000000 ||
324 l == 0x0000ffff || l == 0xffff0000)
326 /* Check if is supports capability lists */
327 s = readw(devbase + PCI_STATUS);
328 if (!(s & PCI_STATUS_CAP_LIST))
331 mpic_scan_ht_pic(mpic, devbase, devfn, l);
334 /* next device, if function 0 */
335 if (PCI_FUNC(devfn) == 0 && (hdr_type & 0x80) == 0)
340 #endif /* CONFIG_MPIC_BROKEN_U3 */
343 #define mpic_irq_to_hw(virq) ((unsigned int)irq_map[virq].hwirq)
345 /* Find an mpic associated with a given linux interrupt */
346 static struct mpic *mpic_find(unsigned int irq, unsigned int *is_ipi)
348 unsigned int src = mpic_irq_to_hw(irq);
350 if (irq < NUM_ISA_INTERRUPTS)
353 *is_ipi = (src >= MPIC_VEC_IPI_0 && src <= MPIC_VEC_IPI_3);
355 return irq_desc[irq].chip_data;
358 /* Convert a cpu mask from logical to physical cpu numbers. */
359 static inline u32 mpic_physmask(u32 cpumask)
364 for (i = 0; i < NR_CPUS; ++i, cpumask >>= 1)
365 mask |= (cpumask & 1) << get_hard_smp_processor_id(i);
370 /* Get the mpic structure from the IPI number */
371 static inline struct mpic * mpic_from_ipi(unsigned int ipi)
373 return irq_desc[ipi].chip_data;
377 /* Get the mpic structure from the irq number */
378 static inline struct mpic * mpic_from_irq(unsigned int irq)
380 return irq_desc[irq].chip_data;
384 static inline void mpic_eoi(struct mpic *mpic)
386 mpic_cpu_write(MPIC_CPU_EOI, 0);
387 (void)mpic_cpu_read(MPIC_CPU_WHOAMI);
391 static irqreturn_t mpic_ipi_action(int irq, void *dev_id, struct pt_regs *regs)
393 smp_message_recv(mpic_irq_to_hw(irq) - MPIC_VEC_IPI_0, regs);
396 #endif /* CONFIG_SMP */
399 * Linux descriptor level callbacks
403 static void mpic_unmask_irq(unsigned int irq)
405 unsigned int loops = 100000;
406 struct mpic *mpic = mpic_from_irq(irq);
407 unsigned int src = mpic_irq_to_hw(irq);
410 DBG("%p: %s: enable_irq: %d (src %d)\n", mpic, mpic->name, irq, src);
412 spin_lock_irqsave(&mpic_lock, flags);
413 mpic_irq_write(src, MPIC_IRQ_VECTOR_PRI,
414 mpic_irq_read(src, MPIC_IRQ_VECTOR_PRI) &
416 /* make sure mask gets to controller before we return to user */
419 printk(KERN_ERR "mpic_enable_irq timeout\n");
422 } while(mpic_irq_read(src, MPIC_IRQ_VECTOR_PRI) & MPIC_VECPRI_MASK);
423 spin_unlock_irqrestore(&mpic_lock, flags);
426 static void mpic_mask_irq(unsigned int irq)
428 unsigned int loops = 100000;
429 struct mpic *mpic = mpic_from_irq(irq);
430 unsigned int src = mpic_irq_to_hw(irq);
433 DBG("%s: disable_irq: %d (src %d)\n", mpic->name, irq, src);
435 spin_lock_irqsave(&mpic_lock, flags);
436 mpic_irq_write(src, MPIC_IRQ_VECTOR_PRI,
437 mpic_irq_read(src, MPIC_IRQ_VECTOR_PRI) |
440 /* make sure mask gets to controller before we return to user */
443 printk(KERN_ERR "mpic_enable_irq timeout\n");
446 } while(!(mpic_irq_read(src, MPIC_IRQ_VECTOR_PRI) & MPIC_VECPRI_MASK));
447 spin_unlock_irqrestore(&mpic_lock, flags);
450 static void mpic_end_irq(unsigned int irq)
452 struct mpic *mpic = mpic_from_irq(irq);
455 DBG("%s: end_irq: %d\n", mpic->name, irq);
457 /* We always EOI on end_irq() even for edge interrupts since that
458 * should only lower the priority, the MPIC should have properly
459 * latched another edge interrupt coming in anyway
465 #ifdef CONFIG_MPIC_BROKEN_U3
467 static void mpic_unmask_ht_irq(unsigned int irq)
469 struct mpic *mpic = mpic_from_irq(irq);
470 unsigned int src = mpic_irq_to_hw(irq);
472 mpic_unmask_irq(irq);
474 if (irq_desc[irq].status & IRQ_LEVEL)
475 mpic_ht_end_irq(mpic, src);
478 static unsigned int mpic_startup_ht_irq(unsigned int irq)
480 struct mpic *mpic = mpic_from_irq(irq);
481 unsigned int src = mpic_irq_to_hw(irq);
483 mpic_unmask_irq(irq);
484 mpic_startup_ht_interrupt(mpic, src, irq_desc[irq].status);
489 static void mpic_shutdown_ht_irq(unsigned int irq)
491 struct mpic *mpic = mpic_from_irq(irq);
492 unsigned int src = mpic_irq_to_hw(irq);
494 mpic_shutdown_ht_interrupt(mpic, src, irq_desc[irq].status);
498 static void mpic_end_ht_irq(unsigned int irq)
500 struct mpic *mpic = mpic_from_irq(irq);
501 unsigned int src = mpic_irq_to_hw(irq);
504 DBG("%s: end_irq: %d\n", mpic->name, irq);
506 /* We always EOI on end_irq() even for edge interrupts since that
507 * should only lower the priority, the MPIC should have properly
508 * latched another edge interrupt coming in anyway
511 if (irq_desc[irq].status & IRQ_LEVEL)
512 mpic_ht_end_irq(mpic, src);
516 #endif /* CONFIG_MPIC_BROKEN_U3 */
520 static void mpic_unmask_ipi(unsigned int irq)
522 struct mpic *mpic = mpic_from_ipi(irq);
523 unsigned int src = mpic_irq_to_hw(irq) - MPIC_VEC_IPI_0;
525 DBG("%s: enable_ipi: %d (ipi %d)\n", mpic->name, irq, src);
526 mpic_ipi_write(src, mpic_ipi_read(src) & ~MPIC_VECPRI_MASK);
529 static void mpic_mask_ipi(unsigned int irq)
531 /* NEVER disable an IPI... that's just plain wrong! */
534 static void mpic_end_ipi(unsigned int irq)
536 struct mpic *mpic = mpic_from_ipi(irq);
539 * IPIs are marked IRQ_PER_CPU. This has the side effect of
540 * preventing the IRQ_PENDING/IRQ_INPROGRESS logic from
541 * applying to them. We EOI them late to avoid re-entering.
542 * We mark IPI's with IRQF_DISABLED as they must run with
548 #endif /* CONFIG_SMP */
550 static void mpic_set_affinity(unsigned int irq, cpumask_t cpumask)
552 struct mpic *mpic = mpic_from_irq(irq);
553 unsigned int src = mpic_irq_to_hw(irq);
557 cpus_and(tmp, cpumask, cpu_online_map);
559 mpic_irq_write(src, MPIC_IRQ_DESTINATION,
560 mpic_physmask(cpus_addr(tmp)[0]));
563 static unsigned int mpic_flags_to_vecpri(unsigned int flags, int *level)
567 /* Now convert sense value */
568 switch(flags & IRQ_TYPE_SENSE_MASK) {
569 case IRQ_TYPE_EDGE_RISING:
570 vecpri = MPIC_VECPRI_SENSE_EDGE |
571 MPIC_VECPRI_POLARITY_POSITIVE;
574 case IRQ_TYPE_EDGE_FALLING:
575 vecpri = MPIC_VECPRI_SENSE_EDGE |
576 MPIC_VECPRI_POLARITY_NEGATIVE;
579 case IRQ_TYPE_LEVEL_HIGH:
580 vecpri = MPIC_VECPRI_SENSE_LEVEL |
581 MPIC_VECPRI_POLARITY_POSITIVE;
584 case IRQ_TYPE_LEVEL_LOW:
586 vecpri = MPIC_VECPRI_SENSE_LEVEL |
587 MPIC_VECPRI_POLARITY_NEGATIVE;
593 static struct irq_chip mpic_irq_chip = {
594 .mask = mpic_mask_irq,
595 .unmask = mpic_unmask_irq,
600 static struct irq_chip mpic_ipi_chip = {
601 .mask = mpic_mask_ipi,
602 .unmask = mpic_unmask_ipi,
605 #endif /* CONFIG_SMP */
607 #ifdef CONFIG_MPIC_BROKEN_U3
608 static struct irq_chip mpic_irq_ht_chip = {
609 .startup = mpic_startup_ht_irq,
610 .shutdown = mpic_shutdown_ht_irq,
611 .mask = mpic_mask_irq,
612 .unmask = mpic_unmask_ht_irq,
613 .eoi = mpic_end_ht_irq,
615 #endif /* CONFIG_MPIC_BROKEN_U3 */
618 static int mpic_host_match(struct irq_host *h, struct device_node *node)
620 struct mpic *mpic = h->host_data;
622 /* Exact match, unless mpic node is NULL */
623 return mpic->of_node == NULL || mpic->of_node == node;
626 static int mpic_host_map(struct irq_host *h, unsigned int virq,
627 irq_hw_number_t hw, unsigned int flags)
629 struct irq_desc *desc = get_irq_desc(virq);
630 struct irq_chip *chip;
631 struct mpic *mpic = h->host_data;
632 u32 v, vecpri = MPIC_VECPRI_SENSE_LEVEL |
633 MPIC_VECPRI_POLARITY_NEGATIVE;
635 unsigned long iflags;
637 pr_debug("mpic: map virq %d, hwirq 0x%lx, flags: 0x%x\n",
640 if (hw == MPIC_VEC_SPURRIOUS)
643 else if (hw >= MPIC_VEC_IPI_0) {
644 WARN_ON(!(mpic->flags & MPIC_PRIMARY));
646 pr_debug("mpic: mapping as IPI\n");
647 set_irq_chip_data(virq, mpic);
648 set_irq_chip_and_handler(virq, &mpic->hc_ipi,
652 #endif /* CONFIG_SMP */
654 if (hw >= mpic->irq_count)
657 /* If no sense provided, check default sense array */
658 if (((flags & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_NONE) &&
659 mpic->senses && hw < mpic->senses_count)
660 flags |= mpic->senses[hw];
662 vecpri = mpic_flags_to_vecpri(flags, &level);
664 desc->status |= IRQ_LEVEL;
665 chip = &mpic->hc_irq;
667 #ifdef CONFIG_MPIC_BROKEN_U3
668 /* Check for HT interrupts, override vecpri */
669 if (mpic_is_ht_interrupt(mpic, hw)) {
670 vecpri &= ~(MPIC_VECPRI_SENSE_MASK |
671 MPIC_VECPRI_POLARITY_MASK);
672 vecpri |= MPIC_VECPRI_POLARITY_POSITIVE;
673 chip = &mpic->hc_ht_irq;
677 /* Reconfigure irq. We must preserve the mask bit as we can be called
678 * while the interrupt is still active (This may change in the future
679 * but for now, it is the case).
681 spin_lock_irqsave(&mpic_lock, iflags);
682 v = mpic_irq_read(hw, MPIC_IRQ_VECTOR_PRI);
684 ~(MPIC_VECPRI_POLARITY_MASK | MPIC_VECPRI_SENSE_MASK)) |
687 mpic_irq_write(hw, MPIC_IRQ_VECTOR_PRI, vecpri);
688 spin_unlock_irqrestore(&mpic_lock, iflags);
690 pr_debug("mpic: mapping as IRQ, vecpri = 0x%08x (was 0x%08x)\n",
693 set_irq_chip_data(virq, mpic);
694 set_irq_chip_and_handler(virq, chip, handle_fasteoi_irq);
698 static int mpic_host_xlate(struct irq_host *h, struct device_node *ct,
699 u32 *intspec, unsigned int intsize,
700 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
703 static unsigned char map_mpic_senses[4] = {
704 IRQ_TYPE_EDGE_RISING,
707 IRQ_TYPE_EDGE_FALLING,
710 *out_hwirq = intspec[0];
711 if (intsize > 1 && intspec[1] < 4)
712 *out_flags = map_mpic_senses[intspec[1]];
714 *out_flags = IRQ_TYPE_NONE;
719 static struct irq_host_ops mpic_host_ops = {
720 .match = mpic_host_match,
721 .map = mpic_host_map,
722 .xlate = mpic_host_xlate,
729 struct mpic * __init mpic_alloc(struct device_node *node,
730 unsigned long phys_addr,
732 unsigned int isu_size,
733 unsigned int irq_count,
741 mpic = alloc_bootmem(sizeof(struct mpic));
745 memset(mpic, 0, sizeof(struct mpic));
747 mpic->of_node = node ? of_node_get(node) : NULL;
749 mpic->irqhost = irq_alloc_host(IRQ_HOST_MAP_LINEAR, 256,
752 if (mpic->irqhost == NULL) {
757 mpic->irqhost->host_data = mpic;
758 mpic->hc_irq = mpic_irq_chip;
759 mpic->hc_irq.typename = name;
760 if (flags & MPIC_PRIMARY)
761 mpic->hc_irq.set_affinity = mpic_set_affinity;
762 #ifdef CONFIG_MPIC_BROKEN_U3
763 mpic->hc_ht_irq = mpic_irq_ht_chip;
764 mpic->hc_ht_irq.typename = name;
765 if (flags & MPIC_PRIMARY)
766 mpic->hc_ht_irq.set_affinity = mpic_set_affinity;
767 #endif /* CONFIG_MPIC_BROKEN_U3 */
769 mpic->hc_ipi = mpic_ipi_chip;
770 mpic->hc_ipi.typename = name;
771 #endif /* CONFIG_SMP */
774 mpic->isu_size = isu_size;
775 mpic->irq_count = irq_count;
776 mpic->num_sources = 0; /* so far */
778 /* Map the global registers */
779 mpic->gregs = ioremap(phys_addr + MPIC_GREG_BASE, 0x1000);
780 mpic->tmregs = mpic->gregs + ((MPIC_TIMER_BASE - MPIC_GREG_BASE) >> 2);
781 BUG_ON(mpic->gregs == NULL);
784 if (flags & MPIC_WANTS_RESET) {
785 mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_0,
786 mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_0)
787 | MPIC_GREG_GCONF_RESET);
788 while( mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_0)
789 & MPIC_GREG_GCONF_RESET)
793 /* Read feature register, calculate num CPUs and, for non-ISU
794 * MPICs, num sources as well. On ISU MPICs, sources are counted
797 reg = mpic_read(mpic->gregs, MPIC_GREG_FEATURE_0);
798 mpic->num_cpus = ((reg & MPIC_GREG_FEATURE_LAST_CPU_MASK)
799 >> MPIC_GREG_FEATURE_LAST_CPU_SHIFT) + 1;
801 mpic->num_sources = ((reg & MPIC_GREG_FEATURE_LAST_SRC_MASK)
802 >> MPIC_GREG_FEATURE_LAST_SRC_SHIFT) + 1;
804 /* Map the per-CPU registers */
805 for (i = 0; i < mpic->num_cpus; i++) {
806 mpic->cpuregs[i] = ioremap(phys_addr + MPIC_CPU_BASE +
807 i * MPIC_CPU_STRIDE, 0x1000);
808 BUG_ON(mpic->cpuregs[i] == NULL);
811 /* Initialize main ISU if none provided */
812 if (mpic->isu_size == 0) {
813 mpic->isu_size = mpic->num_sources;
814 mpic->isus[0] = ioremap(phys_addr + MPIC_IRQ_BASE,
815 MPIC_IRQ_STRIDE * mpic->isu_size);
816 BUG_ON(mpic->isus[0] == NULL);
818 mpic->isu_shift = 1 + __ilog2(mpic->isu_size - 1);
819 mpic->isu_mask = (1 << mpic->isu_shift) - 1;
821 /* Display version */
822 switch (reg & MPIC_GREG_FEATURE_VERSION_MASK) {
836 printk(KERN_INFO "mpic: Setting up MPIC \"%s\" version %s at %lx, max %d CPUs\n",
837 name, vers, phys_addr, mpic->num_cpus);
838 printk(KERN_INFO "mpic: ISU size: %d, shift: %d, mask: %x\n", mpic->isu_size,
839 mpic->isu_shift, mpic->isu_mask);
844 if (flags & MPIC_PRIMARY) {
846 irq_set_default_host(mpic->irqhost);
852 void __init mpic_assign_isu(struct mpic *mpic, unsigned int isu_num,
853 unsigned long phys_addr)
855 unsigned int isu_first = isu_num * mpic->isu_size;
857 BUG_ON(isu_num >= MPIC_MAX_ISU);
859 mpic->isus[isu_num] = ioremap(phys_addr, MPIC_IRQ_STRIDE * mpic->isu_size);
860 if ((isu_first + mpic->isu_size) > mpic->num_sources)
861 mpic->num_sources = isu_first + mpic->isu_size;
864 void __init mpic_set_default_senses(struct mpic *mpic, u8 *senses, int count)
866 mpic->senses = senses;
867 mpic->senses_count = count;
870 void __init mpic_init(struct mpic *mpic)
874 BUG_ON(mpic->num_sources == 0);
875 WARN_ON(mpic->num_sources > MPIC_VEC_IPI_0);
877 /* Sanitize source count */
878 if (mpic->num_sources > MPIC_VEC_IPI_0)
879 mpic->num_sources = MPIC_VEC_IPI_0;
881 printk(KERN_INFO "mpic: Initializing for %d sources\n", mpic->num_sources);
883 /* Set current processor priority to max */
884 mpic_cpu_write(MPIC_CPU_CURRENT_TASK_PRI, 0xf);
886 /* Initialize timers: just disable them all */
887 for (i = 0; i < 4; i++) {
888 mpic_write(mpic->tmregs,
889 i * MPIC_TIMER_STRIDE + MPIC_TIMER_DESTINATION, 0);
890 mpic_write(mpic->tmregs,
891 i * MPIC_TIMER_STRIDE + MPIC_TIMER_VECTOR_PRI,
893 (MPIC_VEC_TIMER_0 + i));
896 /* Initialize IPIs to our reserved vectors and mark them disabled for now */
897 mpic_test_broken_ipi(mpic);
898 for (i = 0; i < 4; i++) {
901 (10 << MPIC_VECPRI_PRIORITY_SHIFT) |
902 (MPIC_VEC_IPI_0 + i));
905 /* Initialize interrupt sources */
906 if (mpic->irq_count == 0)
907 mpic->irq_count = mpic->num_sources;
909 #ifdef CONFIG_MPIC_BROKEN_U3
910 /* Do the HT PIC fixups on U3 broken mpic */
911 DBG("MPIC flags: %x\n", mpic->flags);
912 if ((mpic->flags & MPIC_BROKEN_U3) && (mpic->flags & MPIC_PRIMARY))
913 mpic_scan_ht_pics(mpic);
914 #endif /* CONFIG_MPIC_BROKEN_U3 */
916 for (i = 0; i < mpic->num_sources; i++) {
917 /* start with vector = source number, and masked */
918 u32 vecpri = MPIC_VECPRI_MASK | i | (8 << MPIC_VECPRI_PRIORITY_SHIFT);
921 /* do senses munging */
922 if (mpic->senses && i < mpic->senses_count)
923 vecpri |= mpic_flags_to_vecpri(mpic->senses[i],
926 vecpri |= MPIC_VECPRI_SENSE_LEVEL;
928 /* deal with broken U3 */
929 if (mpic->flags & MPIC_BROKEN_U3) {
930 #ifdef CONFIG_MPIC_BROKEN_U3
931 if (mpic_is_ht_interrupt(mpic, i)) {
932 vecpri &= ~(MPIC_VECPRI_SENSE_MASK |
933 MPIC_VECPRI_POLARITY_MASK);
934 vecpri |= MPIC_VECPRI_POLARITY_POSITIVE;
937 printk(KERN_ERR "mpic: BROKEN_U3 set, but CONFIG doesn't match\n");
941 DBG("setup source %d, vecpri: %08x, level: %d\n", i, vecpri,
945 mpic_irq_write(i, MPIC_IRQ_VECTOR_PRI, vecpri);
946 mpic_irq_write(i, MPIC_IRQ_DESTINATION,
947 1 << hard_smp_processor_id());
950 /* Init spurrious vector */
951 mpic_write(mpic->gregs, MPIC_GREG_SPURIOUS, MPIC_VEC_SPURRIOUS);
953 /* Disable 8259 passthrough */
954 mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_0,
955 mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_0)
956 | MPIC_GREG_GCONF_8259_PTHROU_DIS);
958 /* Set current processor priority to 0 */
959 mpic_cpu_write(MPIC_CPU_CURRENT_TASK_PRI, 0);
962 void __init mpic_set_clk_ratio(struct mpic *mpic, u32 clock_ratio)
966 v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
967 v &= ~MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO_MASK;
968 v |= MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO(clock_ratio);
969 mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
972 void __init mpic_set_serial_int(struct mpic *mpic, int enable)
977 spin_lock_irqsave(&mpic_lock, flags);
978 v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
980 v |= MPIC_GREG_GLOBAL_CONF_1_SIE;
982 v &= ~MPIC_GREG_GLOBAL_CONF_1_SIE;
983 mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
984 spin_unlock_irqrestore(&mpic_lock, flags);
987 void mpic_irq_set_priority(unsigned int irq, unsigned int pri)
990 struct mpic *mpic = mpic_find(irq, &is_ipi);
991 unsigned int src = mpic_irq_to_hw(irq);
995 spin_lock_irqsave(&mpic_lock, flags);
997 reg = mpic_ipi_read(src - MPIC_VEC_IPI_0) &
998 ~MPIC_VECPRI_PRIORITY_MASK;
999 mpic_ipi_write(src - MPIC_VEC_IPI_0,
1000 reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
1002 reg = mpic_irq_read(src, MPIC_IRQ_VECTOR_PRI)
1003 & ~MPIC_VECPRI_PRIORITY_MASK;
1004 mpic_irq_write(src, MPIC_IRQ_VECTOR_PRI,
1005 reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
1007 spin_unlock_irqrestore(&mpic_lock, flags);
1010 unsigned int mpic_irq_get_priority(unsigned int irq)
1013 struct mpic *mpic = mpic_find(irq, &is_ipi);
1014 unsigned int src = mpic_irq_to_hw(irq);
1015 unsigned long flags;
1018 spin_lock_irqsave(&mpic_lock, flags);
1020 reg = mpic_ipi_read(src = MPIC_VEC_IPI_0);
1022 reg = mpic_irq_read(src, MPIC_IRQ_VECTOR_PRI);
1023 spin_unlock_irqrestore(&mpic_lock, flags);
1024 return (reg & MPIC_VECPRI_PRIORITY_MASK) >> MPIC_VECPRI_PRIORITY_SHIFT;
1027 void mpic_setup_this_cpu(void)
1030 struct mpic *mpic = mpic_primary;
1031 unsigned long flags;
1032 u32 msk = 1 << hard_smp_processor_id();
1035 BUG_ON(mpic == NULL);
1037 DBG("%s: setup_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
1039 spin_lock_irqsave(&mpic_lock, flags);
1041 /* let the mpic know we want intrs. default affinity is 0xffffffff
1042 * until changed via /proc. That's how it's done on x86. If we want
1043 * it differently, then we should make sure we also change the default
1044 * values of irq_desc[].affinity in irq.c.
1046 if (distribute_irqs) {
1047 for (i = 0; i < mpic->num_sources ; i++)
1048 mpic_irq_write(i, MPIC_IRQ_DESTINATION,
1049 mpic_irq_read(i, MPIC_IRQ_DESTINATION) | msk);
1052 /* Set current processor priority to 0 */
1053 mpic_cpu_write(MPIC_CPU_CURRENT_TASK_PRI, 0);
1055 spin_unlock_irqrestore(&mpic_lock, flags);
1056 #endif /* CONFIG_SMP */
1059 int mpic_cpu_get_priority(void)
1061 struct mpic *mpic = mpic_primary;
1063 return mpic_cpu_read(MPIC_CPU_CURRENT_TASK_PRI);
1066 void mpic_cpu_set_priority(int prio)
1068 struct mpic *mpic = mpic_primary;
1070 prio &= MPIC_CPU_TASKPRI_MASK;
1071 mpic_cpu_write(MPIC_CPU_CURRENT_TASK_PRI, prio);
1075 * XXX: someone who knows mpic should check this.
1076 * do we need to eoi the ipi including for kexec cpu here (see xics comments)?
1077 * or can we reset the mpic in the new kernel?
1079 void mpic_teardown_this_cpu(int secondary)
1081 struct mpic *mpic = mpic_primary;
1082 unsigned long flags;
1083 u32 msk = 1 << hard_smp_processor_id();
1086 BUG_ON(mpic == NULL);
1088 DBG("%s: teardown_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
1089 spin_lock_irqsave(&mpic_lock, flags);
1091 /* let the mpic know we don't want intrs. */
1092 for (i = 0; i < mpic->num_sources ; i++)
1093 mpic_irq_write(i, MPIC_IRQ_DESTINATION,
1094 mpic_irq_read(i, MPIC_IRQ_DESTINATION) & ~msk);
1096 /* Set current processor priority to max */
1097 mpic_cpu_write(MPIC_CPU_CURRENT_TASK_PRI, 0xf);
1099 spin_unlock_irqrestore(&mpic_lock, flags);
1103 void mpic_send_ipi(unsigned int ipi_no, unsigned int cpu_mask)
1105 struct mpic *mpic = mpic_primary;
1107 BUG_ON(mpic == NULL);
1110 DBG("%s: send_ipi(ipi_no: %d)\n", mpic->name, ipi_no);
1113 mpic_cpu_write(MPIC_CPU_IPI_DISPATCH_0 + ipi_no * 0x10,
1114 mpic_physmask(cpu_mask & cpus_addr(cpu_online_map)[0]));
1117 unsigned int mpic_get_one_irq(struct mpic *mpic, struct pt_regs *regs)
1121 src = mpic_cpu_read(MPIC_CPU_INTACK) & MPIC_VECPRI_VECTOR_MASK;
1123 DBG("%s: get_one_irq(): %d\n", mpic->name, src);
1125 if (unlikely(src == MPIC_VEC_SPURRIOUS))
1127 return irq_linear_revmap(mpic->irqhost, src);
1130 unsigned int mpic_get_irq(struct pt_regs *regs)
1132 struct mpic *mpic = mpic_primary;
1134 BUG_ON(mpic == NULL);
1136 return mpic_get_one_irq(mpic, regs);
1141 void mpic_request_ipis(void)
1143 struct mpic *mpic = mpic_primary;
1145 static char *ipi_names[] = {
1146 "IPI0 (call function)",
1147 "IPI1 (reschedule)",
1149 "IPI3 (debugger break)",
1151 BUG_ON(mpic == NULL);
1153 printk(KERN_INFO "mpic: requesting IPIs ... \n");
1155 for (i = 0; i < 4; i++) {
1156 unsigned int vipi = irq_create_mapping(mpic->irqhost,
1157 MPIC_VEC_IPI_0 + i, 0);
1158 if (vipi == NO_IRQ) {
1159 printk(KERN_ERR "Failed to map IPI %d\n", i);
1162 request_irq(vipi, mpic_ipi_action, IRQF_DISABLED,
1163 ipi_names[i], mpic);
1167 void smp_mpic_message_pass(int target, int msg)
1169 /* make sure we're sending something that translates to an IPI */
1170 if ((unsigned int)msg > 3) {
1171 printk("SMP %d: smp_message_pass: unknown msg %d\n",
1172 smp_processor_id(), msg);
1177 mpic_send_ipi(msg, 0xffffffff);
1179 case MSG_ALL_BUT_SELF:
1180 mpic_send_ipi(msg, 0xffffffff & ~(1 << smp_processor_id()));
1183 mpic_send_ipi(msg, 1 << target);
1187 #endif /* CONFIG_SMP */