xref: /linux/arch/powerpc/sysdev/mpic.c (revision d5dedd4507d307eb3f35f21b6e16f336fdc0d82a)
114cf11afSPaul Mackerras /*
214cf11afSPaul Mackerras  *  arch/powerpc/kernel/mpic.c
314cf11afSPaul Mackerras  *
414cf11afSPaul Mackerras  *  Driver for interrupt controllers following the OpenPIC standard, the
514cf11afSPaul Mackerras  *  common implementation beeing IBM's MPIC. This driver also can deal
614cf11afSPaul Mackerras  *  with various broken implementations of this HW.
714cf11afSPaul Mackerras  *
814cf11afSPaul Mackerras  *  Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
914cf11afSPaul Mackerras  *
1014cf11afSPaul Mackerras  *  This file is subject to the terms and conditions of the GNU General Public
1114cf11afSPaul Mackerras  *  License.  See the file COPYING in the main directory of this archive
1214cf11afSPaul Mackerras  *  for more details.
1314cf11afSPaul Mackerras  */
1414cf11afSPaul Mackerras 
1514cf11afSPaul Mackerras #undef DEBUG
161beb6a7dSBenjamin Herrenschmidt #undef DEBUG_IPI
171beb6a7dSBenjamin Herrenschmidt #undef DEBUG_IRQ
181beb6a7dSBenjamin Herrenschmidt #undef DEBUG_LOW
1914cf11afSPaul Mackerras 
2014cf11afSPaul Mackerras #include <linux/types.h>
2114cf11afSPaul Mackerras #include <linux/kernel.h>
2214cf11afSPaul Mackerras #include <linux/init.h>
2314cf11afSPaul Mackerras #include <linux/irq.h>
2414cf11afSPaul Mackerras #include <linux/smp.h>
2514cf11afSPaul Mackerras #include <linux/interrupt.h>
2614cf11afSPaul Mackerras #include <linux/bootmem.h>
2714cf11afSPaul Mackerras #include <linux/spinlock.h>
2814cf11afSPaul Mackerras #include <linux/pci.h>
2914cf11afSPaul Mackerras 
3014cf11afSPaul Mackerras #include <asm/ptrace.h>
3114cf11afSPaul Mackerras #include <asm/signal.h>
3214cf11afSPaul Mackerras #include <asm/io.h>
3314cf11afSPaul Mackerras #include <asm/pgtable.h>
3414cf11afSPaul Mackerras #include <asm/irq.h>
3514cf11afSPaul Mackerras #include <asm/machdep.h>
3614cf11afSPaul Mackerras #include <asm/mpic.h>
3714cf11afSPaul Mackerras #include <asm/smp.h>
3814cf11afSPaul Mackerras 
39a7de7c74SMichael Ellerman #include "mpic.h"
40a7de7c74SMichael Ellerman 
4114cf11afSPaul Mackerras #ifdef DEBUG
4214cf11afSPaul Mackerras #define DBG(fmt...) printk(fmt)
4314cf11afSPaul Mackerras #else
4414cf11afSPaul Mackerras #define DBG(fmt...)
4514cf11afSPaul Mackerras #endif
4614cf11afSPaul Mackerras 
4714cf11afSPaul Mackerras static struct mpic *mpics;
4814cf11afSPaul Mackerras static struct mpic *mpic_primary;
4914cf11afSPaul Mackerras static DEFINE_SPINLOCK(mpic_lock);
5014cf11afSPaul Mackerras 
51c0c0d996SPaul Mackerras #ifdef CONFIG_PPC32	/* XXX for now */
52e40c7f02SAndy Whitcroft #ifdef CONFIG_IRQ_ALL_CPUS
53e40c7f02SAndy Whitcroft #define distribute_irqs	(1)
54e40c7f02SAndy Whitcroft #else
55e40c7f02SAndy Whitcroft #define distribute_irqs	(0)
56e40c7f02SAndy Whitcroft #endif
57c0c0d996SPaul Mackerras #endif
5814cf11afSPaul Mackerras 
597233593bSZang Roy-r61911 #ifdef CONFIG_MPIC_WEIRD
607233593bSZang Roy-r61911 static u32 mpic_infos[][MPIC_IDX_END] = {
617233593bSZang Roy-r61911 	[0] = {	/* Original OpenPIC compatible MPIC */
627233593bSZang Roy-r61911 		MPIC_GREG_BASE,
637233593bSZang Roy-r61911 		MPIC_GREG_FEATURE_0,
647233593bSZang Roy-r61911 		MPIC_GREG_GLOBAL_CONF_0,
657233593bSZang Roy-r61911 		MPIC_GREG_VENDOR_ID,
667233593bSZang Roy-r61911 		MPIC_GREG_IPI_VECTOR_PRI_0,
677233593bSZang Roy-r61911 		MPIC_GREG_IPI_STRIDE,
687233593bSZang Roy-r61911 		MPIC_GREG_SPURIOUS,
697233593bSZang Roy-r61911 		MPIC_GREG_TIMER_FREQ,
707233593bSZang Roy-r61911 
717233593bSZang Roy-r61911 		MPIC_TIMER_BASE,
727233593bSZang Roy-r61911 		MPIC_TIMER_STRIDE,
737233593bSZang Roy-r61911 		MPIC_TIMER_CURRENT_CNT,
747233593bSZang Roy-r61911 		MPIC_TIMER_BASE_CNT,
757233593bSZang Roy-r61911 		MPIC_TIMER_VECTOR_PRI,
767233593bSZang Roy-r61911 		MPIC_TIMER_DESTINATION,
777233593bSZang Roy-r61911 
787233593bSZang Roy-r61911 		MPIC_CPU_BASE,
797233593bSZang Roy-r61911 		MPIC_CPU_STRIDE,
807233593bSZang Roy-r61911 		MPIC_CPU_IPI_DISPATCH_0,
817233593bSZang Roy-r61911 		MPIC_CPU_IPI_DISPATCH_STRIDE,
827233593bSZang Roy-r61911 		MPIC_CPU_CURRENT_TASK_PRI,
837233593bSZang Roy-r61911 		MPIC_CPU_WHOAMI,
847233593bSZang Roy-r61911 		MPIC_CPU_INTACK,
857233593bSZang Roy-r61911 		MPIC_CPU_EOI,
86f365355eSOlof Johansson 		MPIC_CPU_MCACK,
877233593bSZang Roy-r61911 
887233593bSZang Roy-r61911 		MPIC_IRQ_BASE,
897233593bSZang Roy-r61911 		MPIC_IRQ_STRIDE,
907233593bSZang Roy-r61911 		MPIC_IRQ_VECTOR_PRI,
917233593bSZang Roy-r61911 		MPIC_VECPRI_VECTOR_MASK,
927233593bSZang Roy-r61911 		MPIC_VECPRI_POLARITY_POSITIVE,
937233593bSZang Roy-r61911 		MPIC_VECPRI_POLARITY_NEGATIVE,
947233593bSZang Roy-r61911 		MPIC_VECPRI_SENSE_LEVEL,
957233593bSZang Roy-r61911 		MPIC_VECPRI_SENSE_EDGE,
967233593bSZang Roy-r61911 		MPIC_VECPRI_POLARITY_MASK,
977233593bSZang Roy-r61911 		MPIC_VECPRI_SENSE_MASK,
987233593bSZang Roy-r61911 		MPIC_IRQ_DESTINATION
997233593bSZang Roy-r61911 	},
1007233593bSZang Roy-r61911 	[1] = {	/* Tsi108/109 PIC */
1017233593bSZang Roy-r61911 		TSI108_GREG_BASE,
1027233593bSZang Roy-r61911 		TSI108_GREG_FEATURE_0,
1037233593bSZang Roy-r61911 		TSI108_GREG_GLOBAL_CONF_0,
1047233593bSZang Roy-r61911 		TSI108_GREG_VENDOR_ID,
1057233593bSZang Roy-r61911 		TSI108_GREG_IPI_VECTOR_PRI_0,
1067233593bSZang Roy-r61911 		TSI108_GREG_IPI_STRIDE,
1077233593bSZang Roy-r61911 		TSI108_GREG_SPURIOUS,
1087233593bSZang Roy-r61911 		TSI108_GREG_TIMER_FREQ,
1097233593bSZang Roy-r61911 
1107233593bSZang Roy-r61911 		TSI108_TIMER_BASE,
1117233593bSZang Roy-r61911 		TSI108_TIMER_STRIDE,
1127233593bSZang Roy-r61911 		TSI108_TIMER_CURRENT_CNT,
1137233593bSZang Roy-r61911 		TSI108_TIMER_BASE_CNT,
1147233593bSZang Roy-r61911 		TSI108_TIMER_VECTOR_PRI,
1157233593bSZang Roy-r61911 		TSI108_TIMER_DESTINATION,
1167233593bSZang Roy-r61911 
1177233593bSZang Roy-r61911 		TSI108_CPU_BASE,
1187233593bSZang Roy-r61911 		TSI108_CPU_STRIDE,
1197233593bSZang Roy-r61911 		TSI108_CPU_IPI_DISPATCH_0,
1207233593bSZang Roy-r61911 		TSI108_CPU_IPI_DISPATCH_STRIDE,
1217233593bSZang Roy-r61911 		TSI108_CPU_CURRENT_TASK_PRI,
1227233593bSZang Roy-r61911 		TSI108_CPU_WHOAMI,
1237233593bSZang Roy-r61911 		TSI108_CPU_INTACK,
1247233593bSZang Roy-r61911 		TSI108_CPU_EOI,
125f365355eSOlof Johansson 		TSI108_CPU_MCACK,
1267233593bSZang Roy-r61911 
1277233593bSZang Roy-r61911 		TSI108_IRQ_BASE,
1287233593bSZang Roy-r61911 		TSI108_IRQ_STRIDE,
1297233593bSZang Roy-r61911 		TSI108_IRQ_VECTOR_PRI,
1307233593bSZang Roy-r61911 		TSI108_VECPRI_VECTOR_MASK,
1317233593bSZang Roy-r61911 		TSI108_VECPRI_POLARITY_POSITIVE,
1327233593bSZang Roy-r61911 		TSI108_VECPRI_POLARITY_NEGATIVE,
1337233593bSZang Roy-r61911 		TSI108_VECPRI_SENSE_LEVEL,
1347233593bSZang Roy-r61911 		TSI108_VECPRI_SENSE_EDGE,
1357233593bSZang Roy-r61911 		TSI108_VECPRI_POLARITY_MASK,
1367233593bSZang Roy-r61911 		TSI108_VECPRI_SENSE_MASK,
1377233593bSZang Roy-r61911 		TSI108_IRQ_DESTINATION
1387233593bSZang Roy-r61911 	},
1397233593bSZang Roy-r61911 };
1407233593bSZang Roy-r61911 
1417233593bSZang Roy-r61911 #define MPIC_INFO(name) mpic->hw_set[MPIC_IDX_##name]
1427233593bSZang Roy-r61911 
1437233593bSZang Roy-r61911 #else /* CONFIG_MPIC_WEIRD */
1447233593bSZang Roy-r61911 
1457233593bSZang Roy-r61911 #define MPIC_INFO(name) MPIC_##name
1467233593bSZang Roy-r61911 
1477233593bSZang Roy-r61911 #endif /* CONFIG_MPIC_WEIRD */
1487233593bSZang Roy-r61911 
14914cf11afSPaul Mackerras /*
15014cf11afSPaul Mackerras  * Register accessor functions
15114cf11afSPaul Mackerras  */
15214cf11afSPaul Mackerras 
15314cf11afSPaul Mackerras 
154fbf0274eSBenjamin Herrenschmidt static inline u32 _mpic_read(enum mpic_reg_type type,
155fbf0274eSBenjamin Herrenschmidt 			     struct mpic_reg_bank *rb,
15614cf11afSPaul Mackerras 			     unsigned int reg)
15714cf11afSPaul Mackerras {
158fbf0274eSBenjamin Herrenschmidt 	switch(type) {
159fbf0274eSBenjamin Herrenschmidt #ifdef CONFIG_PPC_DCR
160fbf0274eSBenjamin Herrenschmidt 	case mpic_access_dcr:
16183f34df4SMichael Ellerman 		return dcr_read(rb->dhost, reg);
162fbf0274eSBenjamin Herrenschmidt #endif
163fbf0274eSBenjamin Herrenschmidt 	case mpic_access_mmio_be:
164fbf0274eSBenjamin Herrenschmidt 		return in_be32(rb->base + (reg >> 2));
165fbf0274eSBenjamin Herrenschmidt 	case mpic_access_mmio_le:
166fbf0274eSBenjamin Herrenschmidt 	default:
167fbf0274eSBenjamin Herrenschmidt 		return in_le32(rb->base + (reg >> 2));
168fbf0274eSBenjamin Herrenschmidt 	}
16914cf11afSPaul Mackerras }
17014cf11afSPaul Mackerras 
171fbf0274eSBenjamin Herrenschmidt static inline void _mpic_write(enum mpic_reg_type type,
172fbf0274eSBenjamin Herrenschmidt 			       struct mpic_reg_bank *rb,
17314cf11afSPaul Mackerras  			       unsigned int reg, u32 value)
17414cf11afSPaul Mackerras {
175fbf0274eSBenjamin Herrenschmidt 	switch(type) {
176fbf0274eSBenjamin Herrenschmidt #ifdef CONFIG_PPC_DCR
177fbf0274eSBenjamin Herrenschmidt 	case mpic_access_dcr:
178d9d1063dSJohannes Berg 		dcr_write(rb->dhost, reg, value);
179d9d1063dSJohannes Berg 		break;
180fbf0274eSBenjamin Herrenschmidt #endif
181fbf0274eSBenjamin Herrenschmidt 	case mpic_access_mmio_be:
182d9d1063dSJohannes Berg 		out_be32(rb->base + (reg >> 2), value);
183d9d1063dSJohannes Berg 		break;
184fbf0274eSBenjamin Herrenschmidt 	case mpic_access_mmio_le:
185fbf0274eSBenjamin Herrenschmidt 	default:
186d9d1063dSJohannes Berg 		out_le32(rb->base + (reg >> 2), value);
187d9d1063dSJohannes Berg 		break;
188fbf0274eSBenjamin Herrenschmidt 	}
18914cf11afSPaul Mackerras }
19014cf11afSPaul Mackerras 
19114cf11afSPaul Mackerras static inline u32 _mpic_ipi_read(struct mpic *mpic, unsigned int ipi)
19214cf11afSPaul Mackerras {
193fbf0274eSBenjamin Herrenschmidt 	enum mpic_reg_type type = mpic->reg_type;
1947233593bSZang Roy-r61911 	unsigned int offset = MPIC_INFO(GREG_IPI_VECTOR_PRI_0) +
1957233593bSZang Roy-r61911 			      (ipi * MPIC_INFO(GREG_IPI_STRIDE));
19614cf11afSPaul Mackerras 
197fbf0274eSBenjamin Herrenschmidt 	if ((mpic->flags & MPIC_BROKEN_IPI) && type == mpic_access_mmio_le)
198fbf0274eSBenjamin Herrenschmidt 		type = mpic_access_mmio_be;
199fbf0274eSBenjamin Herrenschmidt 	return _mpic_read(type, &mpic->gregs, offset);
20014cf11afSPaul Mackerras }
20114cf11afSPaul Mackerras 
20214cf11afSPaul Mackerras static inline void _mpic_ipi_write(struct mpic *mpic, unsigned int ipi, u32 value)
20314cf11afSPaul Mackerras {
2047233593bSZang Roy-r61911 	unsigned int offset = MPIC_INFO(GREG_IPI_VECTOR_PRI_0) +
2057233593bSZang Roy-r61911 			      (ipi * MPIC_INFO(GREG_IPI_STRIDE));
20614cf11afSPaul Mackerras 
207fbf0274eSBenjamin Herrenschmidt 	_mpic_write(mpic->reg_type, &mpic->gregs, offset, value);
20814cf11afSPaul Mackerras }
20914cf11afSPaul Mackerras 
21014cf11afSPaul Mackerras static inline u32 _mpic_cpu_read(struct mpic *mpic, unsigned int reg)
21114cf11afSPaul Mackerras {
21214cf11afSPaul Mackerras 	unsigned int cpu = 0;
21314cf11afSPaul Mackerras 
21414cf11afSPaul Mackerras 	if (mpic->flags & MPIC_PRIMARY)
21514cf11afSPaul Mackerras 		cpu = hard_smp_processor_id();
216fbf0274eSBenjamin Herrenschmidt 	return _mpic_read(mpic->reg_type, &mpic->cpuregs[cpu], reg);
21714cf11afSPaul Mackerras }
21814cf11afSPaul Mackerras 
21914cf11afSPaul Mackerras static inline void _mpic_cpu_write(struct mpic *mpic, unsigned int reg, u32 value)
22014cf11afSPaul Mackerras {
22114cf11afSPaul Mackerras 	unsigned int cpu = 0;
22214cf11afSPaul Mackerras 
22314cf11afSPaul Mackerras 	if (mpic->flags & MPIC_PRIMARY)
22414cf11afSPaul Mackerras 		cpu = hard_smp_processor_id();
22514cf11afSPaul Mackerras 
226fbf0274eSBenjamin Herrenschmidt 	_mpic_write(mpic->reg_type, &mpic->cpuregs[cpu], reg, value);
22714cf11afSPaul Mackerras }
22814cf11afSPaul Mackerras 
22914cf11afSPaul Mackerras static inline u32 _mpic_irq_read(struct mpic *mpic, unsigned int src_no, unsigned int reg)
23014cf11afSPaul Mackerras {
23114cf11afSPaul Mackerras 	unsigned int	isu = src_no >> mpic->isu_shift;
23214cf11afSPaul Mackerras 	unsigned int	idx = src_no & mpic->isu_mask;
23314cf11afSPaul Mackerras 
2340d72ba93SOlof Johansson #ifdef CONFIG_MPIC_BROKEN_REGREAD
2350d72ba93SOlof Johansson 	if (reg == 0)
2360d72ba93SOlof Johansson 		return mpic->isu_reg0_shadow[idx];
2370d72ba93SOlof Johansson 	else
2380d72ba93SOlof Johansson #endif
239fbf0274eSBenjamin Herrenschmidt 		return _mpic_read(mpic->reg_type, &mpic->isus[isu],
2407233593bSZang Roy-r61911 				  reg + (idx * MPIC_INFO(IRQ_STRIDE)));
24114cf11afSPaul Mackerras }
24214cf11afSPaul Mackerras 
24314cf11afSPaul Mackerras static inline void _mpic_irq_write(struct mpic *mpic, unsigned int src_no,
24414cf11afSPaul Mackerras 				   unsigned int reg, u32 value)
24514cf11afSPaul Mackerras {
24614cf11afSPaul Mackerras 	unsigned int	isu = src_no >> mpic->isu_shift;
24714cf11afSPaul Mackerras 	unsigned int	idx = src_no & mpic->isu_mask;
24814cf11afSPaul Mackerras 
249fbf0274eSBenjamin Herrenschmidt 	_mpic_write(mpic->reg_type, &mpic->isus[isu],
2507233593bSZang Roy-r61911 		    reg + (idx * MPIC_INFO(IRQ_STRIDE)), value);
2510d72ba93SOlof Johansson 
2520d72ba93SOlof Johansson #ifdef CONFIG_MPIC_BROKEN_REGREAD
2530d72ba93SOlof Johansson 	if (reg == 0)
2540d72ba93SOlof Johansson 		mpic->isu_reg0_shadow[idx] = value;
2550d72ba93SOlof Johansson #endif
25614cf11afSPaul Mackerras }
25714cf11afSPaul Mackerras 
258fbf0274eSBenjamin Herrenschmidt #define mpic_read(b,r)		_mpic_read(mpic->reg_type,&(b),(r))
259fbf0274eSBenjamin Herrenschmidt #define mpic_write(b,r,v)	_mpic_write(mpic->reg_type,&(b),(r),(v))
26014cf11afSPaul Mackerras #define mpic_ipi_read(i)	_mpic_ipi_read(mpic,(i))
26114cf11afSPaul Mackerras #define mpic_ipi_write(i,v)	_mpic_ipi_write(mpic,(i),(v))
26214cf11afSPaul Mackerras #define mpic_cpu_read(i)	_mpic_cpu_read(mpic,(i))
26314cf11afSPaul Mackerras #define mpic_cpu_write(i,v)	_mpic_cpu_write(mpic,(i),(v))
26414cf11afSPaul Mackerras #define mpic_irq_read(s,r)	_mpic_irq_read(mpic,(s),(r))
26514cf11afSPaul Mackerras #define mpic_irq_write(s,r,v)	_mpic_irq_write(mpic,(s),(r),(v))
26614cf11afSPaul Mackerras 
26714cf11afSPaul Mackerras 
26814cf11afSPaul Mackerras /*
26914cf11afSPaul Mackerras  * Low level utility functions
27014cf11afSPaul Mackerras  */
27114cf11afSPaul Mackerras 
27214cf11afSPaul Mackerras 
273c51a3fdcSBecky Bruce static void _mpic_map_mmio(struct mpic *mpic, phys_addr_t phys_addr,
274fbf0274eSBenjamin Herrenschmidt 			   struct mpic_reg_bank *rb, unsigned int offset,
275fbf0274eSBenjamin Herrenschmidt 			   unsigned int size)
276fbf0274eSBenjamin Herrenschmidt {
277fbf0274eSBenjamin Herrenschmidt 	rb->base = ioremap(phys_addr + offset, size);
278fbf0274eSBenjamin Herrenschmidt 	BUG_ON(rb->base == NULL);
279fbf0274eSBenjamin Herrenschmidt }
280fbf0274eSBenjamin Herrenschmidt 
281fbf0274eSBenjamin Herrenschmidt #ifdef CONFIG_PPC_DCR
282fbf0274eSBenjamin Herrenschmidt static void _mpic_map_dcr(struct mpic *mpic, struct mpic_reg_bank *rb,
283fbf0274eSBenjamin Herrenschmidt 			  unsigned int offset, unsigned int size)
284fbf0274eSBenjamin Herrenschmidt {
2850411a5e2SMichael Ellerman 	const u32 *dbasep;
2860411a5e2SMichael Ellerman 
2870411a5e2SMichael Ellerman 	dbasep = of_get_property(mpic->irqhost->of_node, "dcr-reg", NULL);
2880411a5e2SMichael Ellerman 
2890411a5e2SMichael Ellerman 	rb->dhost = dcr_map(mpic->irqhost->of_node, *dbasep + offset, size);
290fbf0274eSBenjamin Herrenschmidt 	BUG_ON(!DCR_MAP_OK(rb->dhost));
291fbf0274eSBenjamin Herrenschmidt }
292fbf0274eSBenjamin Herrenschmidt 
293c51a3fdcSBecky Bruce static inline void mpic_map(struct mpic *mpic, phys_addr_t phys_addr,
294fbf0274eSBenjamin Herrenschmidt 			    struct mpic_reg_bank *rb, unsigned int offset,
295fbf0274eSBenjamin Herrenschmidt 			    unsigned int size)
296fbf0274eSBenjamin Herrenschmidt {
297fbf0274eSBenjamin Herrenschmidt 	if (mpic->flags & MPIC_USES_DCR)
298fbf0274eSBenjamin Herrenschmidt 		_mpic_map_dcr(mpic, rb, offset, size);
299fbf0274eSBenjamin Herrenschmidt 	else
300fbf0274eSBenjamin Herrenschmidt 		_mpic_map_mmio(mpic, phys_addr, rb, offset, size);
301fbf0274eSBenjamin Herrenschmidt }
302fbf0274eSBenjamin Herrenschmidt #else /* CONFIG_PPC_DCR */
303fbf0274eSBenjamin Herrenschmidt #define mpic_map(m,p,b,o,s)	_mpic_map_mmio(m,p,b,o,s)
304fbf0274eSBenjamin Herrenschmidt #endif /* !CONFIG_PPC_DCR */
305fbf0274eSBenjamin Herrenschmidt 
306fbf0274eSBenjamin Herrenschmidt 
30714cf11afSPaul Mackerras 
30814cf11afSPaul Mackerras /* Check if we have one of those nice broken MPICs with a flipped endian on
30914cf11afSPaul Mackerras  * reads from IPI registers
31014cf11afSPaul Mackerras  */
31114cf11afSPaul Mackerras static void __init mpic_test_broken_ipi(struct mpic *mpic)
31214cf11afSPaul Mackerras {
31314cf11afSPaul Mackerras 	u32 r;
31414cf11afSPaul Mackerras 
3157233593bSZang Roy-r61911 	mpic_write(mpic->gregs, MPIC_INFO(GREG_IPI_VECTOR_PRI_0), MPIC_VECPRI_MASK);
3167233593bSZang Roy-r61911 	r = mpic_read(mpic->gregs, MPIC_INFO(GREG_IPI_VECTOR_PRI_0));
31714cf11afSPaul Mackerras 
31814cf11afSPaul Mackerras 	if (r == le32_to_cpu(MPIC_VECPRI_MASK)) {
31914cf11afSPaul Mackerras 		printk(KERN_INFO "mpic: Detected reversed IPI registers\n");
32014cf11afSPaul Mackerras 		mpic->flags |= MPIC_BROKEN_IPI;
32114cf11afSPaul Mackerras 	}
32214cf11afSPaul Mackerras }
32314cf11afSPaul Mackerras 
3246cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
32514cf11afSPaul Mackerras 
32614cf11afSPaul Mackerras /* Test if an interrupt is sourced from HyperTransport (used on broken U3s)
32714cf11afSPaul Mackerras  * to force the edge setting on the MPIC and do the ack workaround.
32814cf11afSPaul Mackerras  */
3291beb6a7dSBenjamin Herrenschmidt static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
33014cf11afSPaul Mackerras {
3311beb6a7dSBenjamin Herrenschmidt 	if (source >= 128 || !mpic->fixups)
33214cf11afSPaul Mackerras 		return 0;
3331beb6a7dSBenjamin Herrenschmidt 	return mpic->fixups[source].base != NULL;
33414cf11afSPaul Mackerras }
33514cf11afSPaul Mackerras 
336c4b22f26SSegher Boessenkool 
3371beb6a7dSBenjamin Herrenschmidt static inline void mpic_ht_end_irq(struct mpic *mpic, unsigned int source)
33814cf11afSPaul Mackerras {
3391beb6a7dSBenjamin Herrenschmidt 	struct mpic_irq_fixup *fixup = &mpic->fixups[source];
34014cf11afSPaul Mackerras 
3411beb6a7dSBenjamin Herrenschmidt 	if (fixup->applebase) {
3421beb6a7dSBenjamin Herrenschmidt 		unsigned int soff = (fixup->index >> 3) & ~3;
3431beb6a7dSBenjamin Herrenschmidt 		unsigned int mask = 1U << (fixup->index & 0x1f);
3441beb6a7dSBenjamin Herrenschmidt 		writel(mask, fixup->applebase + soff);
3451beb6a7dSBenjamin Herrenschmidt 	} else {
34614cf11afSPaul Mackerras 		spin_lock(&mpic->fixup_lock);
3471beb6a7dSBenjamin Herrenschmidt 		writeb(0x11 + 2 * fixup->index, fixup->base + 2);
348c4b22f26SSegher Boessenkool 		writel(fixup->data, fixup->base + 4);
34914cf11afSPaul Mackerras 		spin_unlock(&mpic->fixup_lock);
35014cf11afSPaul Mackerras 	}
3511beb6a7dSBenjamin Herrenschmidt }
35214cf11afSPaul Mackerras 
3531beb6a7dSBenjamin Herrenschmidt static void mpic_startup_ht_interrupt(struct mpic *mpic, unsigned int source,
3541beb6a7dSBenjamin Herrenschmidt 				      unsigned int irqflags)
3551beb6a7dSBenjamin Herrenschmidt {
3561beb6a7dSBenjamin Herrenschmidt 	struct mpic_irq_fixup *fixup = &mpic->fixups[source];
3571beb6a7dSBenjamin Herrenschmidt 	unsigned long flags;
3581beb6a7dSBenjamin Herrenschmidt 	u32 tmp;
35914cf11afSPaul Mackerras 
3601beb6a7dSBenjamin Herrenschmidt 	if (fixup->base == NULL)
3611beb6a7dSBenjamin Herrenschmidt 		return;
3621beb6a7dSBenjamin Herrenschmidt 
36306fe98e6SBenjamin Herrenschmidt 	DBG("startup_ht_interrupt(0x%x, 0x%x) index: %d\n",
3641beb6a7dSBenjamin Herrenschmidt 	    source, irqflags, fixup->index);
3651beb6a7dSBenjamin Herrenschmidt 	spin_lock_irqsave(&mpic->fixup_lock, flags);
3661beb6a7dSBenjamin Herrenschmidt 	/* Enable and configure */
3671beb6a7dSBenjamin Herrenschmidt 	writeb(0x10 + 2 * fixup->index, fixup->base + 2);
3681beb6a7dSBenjamin Herrenschmidt 	tmp = readl(fixup->base + 4);
3691beb6a7dSBenjamin Herrenschmidt 	tmp &= ~(0x23U);
3701beb6a7dSBenjamin Herrenschmidt 	if (irqflags & IRQ_LEVEL)
3711beb6a7dSBenjamin Herrenschmidt 		tmp |= 0x22;
3721beb6a7dSBenjamin Herrenschmidt 	writel(tmp, fixup->base + 4);
3731beb6a7dSBenjamin Herrenschmidt 	spin_unlock_irqrestore(&mpic->fixup_lock, flags);
3743669e930SJohannes Berg 
3753669e930SJohannes Berg #ifdef CONFIG_PM
3763669e930SJohannes Berg 	/* use the lowest bit inverted to the actual HW,
3773669e930SJohannes Berg 	 * set if this fixup was enabled, clear otherwise */
3783669e930SJohannes Berg 	mpic->save_data[source].fixup_data = tmp | 1;
3793669e930SJohannes Berg #endif
3801beb6a7dSBenjamin Herrenschmidt }
3811beb6a7dSBenjamin Herrenschmidt 
3821beb6a7dSBenjamin Herrenschmidt static void mpic_shutdown_ht_interrupt(struct mpic *mpic, unsigned int source,
3831beb6a7dSBenjamin Herrenschmidt 				       unsigned int irqflags)
3841beb6a7dSBenjamin Herrenschmidt {
3851beb6a7dSBenjamin Herrenschmidt 	struct mpic_irq_fixup *fixup = &mpic->fixups[source];
3861beb6a7dSBenjamin Herrenschmidt 	unsigned long flags;
3871beb6a7dSBenjamin Herrenschmidt 	u32 tmp;
3881beb6a7dSBenjamin Herrenschmidt 
3891beb6a7dSBenjamin Herrenschmidt 	if (fixup->base == NULL)
3901beb6a7dSBenjamin Herrenschmidt 		return;
3911beb6a7dSBenjamin Herrenschmidt 
39206fe98e6SBenjamin Herrenschmidt 	DBG("shutdown_ht_interrupt(0x%x, 0x%x)\n", source, irqflags);
3931beb6a7dSBenjamin Herrenschmidt 
3941beb6a7dSBenjamin Herrenschmidt 	/* Disable */
3951beb6a7dSBenjamin Herrenschmidt 	spin_lock_irqsave(&mpic->fixup_lock, flags);
3961beb6a7dSBenjamin Herrenschmidt 	writeb(0x10 + 2 * fixup->index, fixup->base + 2);
3971beb6a7dSBenjamin Herrenschmidt 	tmp = readl(fixup->base + 4);
39872b13819SSegher Boessenkool 	tmp |= 1;
3991beb6a7dSBenjamin Herrenschmidt 	writel(tmp, fixup->base + 4);
4001beb6a7dSBenjamin Herrenschmidt 	spin_unlock_irqrestore(&mpic->fixup_lock, flags);
4013669e930SJohannes Berg 
4023669e930SJohannes Berg #ifdef CONFIG_PM
4033669e930SJohannes Berg 	/* use the lowest bit inverted to the actual HW,
4043669e930SJohannes Berg 	 * set if this fixup was enabled, clear otherwise */
4053669e930SJohannes Berg 	mpic->save_data[source].fixup_data = tmp & ~1;
4063669e930SJohannes Berg #endif
4071beb6a7dSBenjamin Herrenschmidt }
4081beb6a7dSBenjamin Herrenschmidt 
409812fd1fdSMichael Ellerman #ifdef CONFIG_PCI_MSI
410812fd1fdSMichael Ellerman static void __init mpic_scan_ht_msi(struct mpic *mpic, u8 __iomem *devbase,
411812fd1fdSMichael Ellerman 				    unsigned int devfn)
412812fd1fdSMichael Ellerman {
413812fd1fdSMichael Ellerman 	u8 __iomem *base;
414812fd1fdSMichael Ellerman 	u8 pos, flags;
415812fd1fdSMichael Ellerman 	u64 addr = 0;
416812fd1fdSMichael Ellerman 
417812fd1fdSMichael Ellerman 	for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
418812fd1fdSMichael Ellerman 	     pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
419812fd1fdSMichael Ellerman 		u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
420812fd1fdSMichael Ellerman 		if (id == PCI_CAP_ID_HT) {
421812fd1fdSMichael Ellerman 			id = readb(devbase + pos + 3);
422812fd1fdSMichael Ellerman 			if ((id & HT_5BIT_CAP_MASK) == HT_CAPTYPE_MSI_MAPPING)
423812fd1fdSMichael Ellerman 				break;
424812fd1fdSMichael Ellerman 		}
425812fd1fdSMichael Ellerman 	}
426812fd1fdSMichael Ellerman 
427812fd1fdSMichael Ellerman 	if (pos == 0)
428812fd1fdSMichael Ellerman 		return;
429812fd1fdSMichael Ellerman 
430812fd1fdSMichael Ellerman 	base = devbase + pos;
431812fd1fdSMichael Ellerman 
432812fd1fdSMichael Ellerman 	flags = readb(base + HT_MSI_FLAGS);
433812fd1fdSMichael Ellerman 	if (!(flags & HT_MSI_FLAGS_FIXED)) {
434812fd1fdSMichael Ellerman 		addr = readl(base + HT_MSI_ADDR_LO) & HT_MSI_ADDR_LO_MASK;
435812fd1fdSMichael Ellerman 		addr = addr | ((u64)readl(base + HT_MSI_ADDR_HI) << 32);
436812fd1fdSMichael Ellerman 	}
437812fd1fdSMichael Ellerman 
438fe333321SIngo Molnar 	printk(KERN_DEBUG "mpic:   - HT:%02x.%x %s MSI mapping found @ 0x%llx\n",
439812fd1fdSMichael Ellerman 		PCI_SLOT(devfn), PCI_FUNC(devfn),
440812fd1fdSMichael Ellerman 		flags & HT_MSI_FLAGS_ENABLE ? "enabled" : "disabled", addr);
441812fd1fdSMichael Ellerman 
442812fd1fdSMichael Ellerman 	if (!(flags & HT_MSI_FLAGS_ENABLE))
443812fd1fdSMichael Ellerman 		writeb(flags | HT_MSI_FLAGS_ENABLE, base + HT_MSI_FLAGS);
444812fd1fdSMichael Ellerman }
445812fd1fdSMichael Ellerman #else
446812fd1fdSMichael Ellerman static void __init mpic_scan_ht_msi(struct mpic *mpic, u8 __iomem *devbase,
447812fd1fdSMichael Ellerman 				    unsigned int devfn)
448812fd1fdSMichael Ellerman {
449812fd1fdSMichael Ellerman 	return;
450812fd1fdSMichael Ellerman }
451812fd1fdSMichael Ellerman #endif
452812fd1fdSMichael Ellerman 
4531beb6a7dSBenjamin Herrenschmidt static void __init mpic_scan_ht_pic(struct mpic *mpic, u8 __iomem *devbase,
4541beb6a7dSBenjamin Herrenschmidt 				    unsigned int devfn, u32 vdid)
45514cf11afSPaul Mackerras {
456c4b22f26SSegher Boessenkool 	int i, irq, n;
4571beb6a7dSBenjamin Herrenschmidt 	u8 __iomem *base;
45814cf11afSPaul Mackerras 	u32 tmp;
459c4b22f26SSegher Boessenkool 	u8 pos;
46014cf11afSPaul Mackerras 
4611beb6a7dSBenjamin Herrenschmidt 	for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
4621beb6a7dSBenjamin Herrenschmidt 	     pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
4631beb6a7dSBenjamin Herrenschmidt 		u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
46446ff3463SBrice Goglin 		if (id == PCI_CAP_ID_HT) {
465c4b22f26SSegher Boessenkool 			id = readb(devbase + pos + 3);
466beb7cc82SMichael Ellerman 			if ((id & HT_5BIT_CAP_MASK) == HT_CAPTYPE_IRQ)
467c4b22f26SSegher Boessenkool 				break;
468c4b22f26SSegher Boessenkool 		}
469c4b22f26SSegher Boessenkool 	}
470c4b22f26SSegher Boessenkool 	if (pos == 0)
471c4b22f26SSegher Boessenkool 		return;
472c4b22f26SSegher Boessenkool 
4731beb6a7dSBenjamin Herrenschmidt 	base = devbase + pos;
4741beb6a7dSBenjamin Herrenschmidt 	writeb(0x01, base + 2);
4751beb6a7dSBenjamin Herrenschmidt 	n = (readl(base + 4) >> 16) & 0xff;
476c4b22f26SSegher Boessenkool 
4771beb6a7dSBenjamin Herrenschmidt 	printk(KERN_INFO "mpic:   - HT:%02x.%x [0x%02x] vendor %04x device %04x"
4781beb6a7dSBenjamin Herrenschmidt 	       " has %d irqs\n",
4791beb6a7dSBenjamin Herrenschmidt 	       devfn >> 3, devfn & 0x7, pos, vdid & 0xffff, vdid >> 16, n + 1);
480c4b22f26SSegher Boessenkool 
481c4b22f26SSegher Boessenkool 	for (i = 0; i <= n; i++) {
4821beb6a7dSBenjamin Herrenschmidt 		writeb(0x10 + 2 * i, base + 2);
4831beb6a7dSBenjamin Herrenschmidt 		tmp = readl(base + 4);
48414cf11afSPaul Mackerras 		irq = (tmp >> 16) & 0xff;
4851beb6a7dSBenjamin Herrenschmidt 		DBG("HT PIC index 0x%x, irq 0x%x, tmp: %08x\n", i, irq, tmp);
4861beb6a7dSBenjamin Herrenschmidt 		/* mask it , will be unmasked later */
4871beb6a7dSBenjamin Herrenschmidt 		tmp |= 0x1;
4881beb6a7dSBenjamin Herrenschmidt 		writel(tmp, base + 4);
4891beb6a7dSBenjamin Herrenschmidt 		mpic->fixups[irq].index = i;
4901beb6a7dSBenjamin Herrenschmidt 		mpic->fixups[irq].base = base;
4911beb6a7dSBenjamin Herrenschmidt 		/* Apple HT PIC has a non-standard way of doing EOIs */
4921beb6a7dSBenjamin Herrenschmidt 		if ((vdid & 0xffff) == 0x106b)
4931beb6a7dSBenjamin Herrenschmidt 			mpic->fixups[irq].applebase = devbase + 0x60;
4941beb6a7dSBenjamin Herrenschmidt 		else
4951beb6a7dSBenjamin Herrenschmidt 			mpic->fixups[irq].applebase = NULL;
4961beb6a7dSBenjamin Herrenschmidt 		writeb(0x11 + 2 * i, base + 2);
4971beb6a7dSBenjamin Herrenschmidt 		mpic->fixups[irq].data = readl(base + 4) | 0x80000000;
49814cf11afSPaul Mackerras 	}
49914cf11afSPaul Mackerras }
50014cf11afSPaul Mackerras 
50114cf11afSPaul Mackerras 
5021beb6a7dSBenjamin Herrenschmidt static void __init mpic_scan_ht_pics(struct mpic *mpic)
50314cf11afSPaul Mackerras {
50414cf11afSPaul Mackerras 	unsigned int devfn;
50514cf11afSPaul Mackerras 	u8 __iomem *cfgspace;
50614cf11afSPaul Mackerras 
5071beb6a7dSBenjamin Herrenschmidt 	printk(KERN_INFO "mpic: Setting up HT PICs workarounds for U3/U4\n");
50814cf11afSPaul Mackerras 
50914cf11afSPaul Mackerras 	/* Allocate fixups array */
51014cf11afSPaul Mackerras 	mpic->fixups = alloc_bootmem(128 * sizeof(struct mpic_irq_fixup));
51114cf11afSPaul Mackerras 	BUG_ON(mpic->fixups == NULL);
51214cf11afSPaul Mackerras 	memset(mpic->fixups, 0, 128 * sizeof(struct mpic_irq_fixup));
51314cf11afSPaul Mackerras 
51414cf11afSPaul Mackerras 	/* Init spinlock */
51514cf11afSPaul Mackerras 	spin_lock_init(&mpic->fixup_lock);
51614cf11afSPaul Mackerras 
517c4b22f26SSegher Boessenkool 	/* Map U3 config space. We assume all IO-APICs are on the primary bus
518c4b22f26SSegher Boessenkool 	 * so we only need to map 64kB.
51914cf11afSPaul Mackerras 	 */
520c4b22f26SSegher Boessenkool 	cfgspace = ioremap(0xf2000000, 0x10000);
52114cf11afSPaul Mackerras 	BUG_ON(cfgspace == NULL);
52214cf11afSPaul Mackerras 
5231beb6a7dSBenjamin Herrenschmidt 	/* Now we scan all slots. We do a very quick scan, we read the header
5241beb6a7dSBenjamin Herrenschmidt 	 * type, vendor ID and device ID only, that's plenty enough
52514cf11afSPaul Mackerras 	 */
526c4b22f26SSegher Boessenkool 	for (devfn = 0; devfn < 0x100; devfn++) {
52714cf11afSPaul Mackerras 		u8 __iomem *devbase = cfgspace + (devfn << 8);
52814cf11afSPaul Mackerras 		u8 hdr_type = readb(devbase + PCI_HEADER_TYPE);
52914cf11afSPaul Mackerras 		u32 l = readl(devbase + PCI_VENDOR_ID);
5301beb6a7dSBenjamin Herrenschmidt 		u16 s;
53114cf11afSPaul Mackerras 
53214cf11afSPaul Mackerras 		DBG("devfn %x, l: %x\n", devfn, l);
53314cf11afSPaul Mackerras 
53414cf11afSPaul Mackerras 		/* If no device, skip */
53514cf11afSPaul Mackerras 		if (l == 0xffffffff || l == 0x00000000 ||
53614cf11afSPaul Mackerras 		    l == 0x0000ffff || l == 0xffff0000)
53714cf11afSPaul Mackerras 			goto next;
5381beb6a7dSBenjamin Herrenschmidt 		/* Check if is supports capability lists */
5391beb6a7dSBenjamin Herrenschmidt 		s = readw(devbase + PCI_STATUS);
5401beb6a7dSBenjamin Herrenschmidt 		if (!(s & PCI_STATUS_CAP_LIST))
5411beb6a7dSBenjamin Herrenschmidt 			goto next;
54214cf11afSPaul Mackerras 
5431beb6a7dSBenjamin Herrenschmidt 		mpic_scan_ht_pic(mpic, devbase, devfn, l);
544812fd1fdSMichael Ellerman 		mpic_scan_ht_msi(mpic, devbase, devfn);
54514cf11afSPaul Mackerras 
54614cf11afSPaul Mackerras 	next:
54714cf11afSPaul Mackerras 		/* next device, if function 0 */
548c4b22f26SSegher Boessenkool 		if (PCI_FUNC(devfn) == 0 && (hdr_type & 0x80) == 0)
54914cf11afSPaul Mackerras 			devfn += 7;
55014cf11afSPaul Mackerras 	}
55114cf11afSPaul Mackerras }
55214cf11afSPaul Mackerras 
5536cfef5b2SMichael Ellerman #else /* CONFIG_MPIC_U3_HT_IRQS */
5546e99e458SBenjamin Herrenschmidt 
5556e99e458SBenjamin Herrenschmidt static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
5566e99e458SBenjamin Herrenschmidt {
5576e99e458SBenjamin Herrenschmidt 	return 0;
5586e99e458SBenjamin Herrenschmidt }
5596e99e458SBenjamin Herrenschmidt 
5606e99e458SBenjamin Herrenschmidt static void __init mpic_scan_ht_pics(struct mpic *mpic)
5616e99e458SBenjamin Herrenschmidt {
5626e99e458SBenjamin Herrenschmidt }
5636e99e458SBenjamin Herrenschmidt 
5646cfef5b2SMichael Ellerman #endif /* CONFIG_MPIC_U3_HT_IRQS */
56514cf11afSPaul Mackerras 
5663c10c9c4SKumar Gala #ifdef CONFIG_SMP
5673c10c9c4SKumar Gala static int irq_choose_cpu(unsigned int virt_irq)
5683c10c9c4SKumar Gala {
569e65e49d0SMike Travis 	cpumask_t mask;
5703c10c9c4SKumar Gala 	int cpuid;
5713c10c9c4SKumar Gala 
572e65e49d0SMike Travis 	cpumask_copy(&mask, irq_desc[virt_irq].affinity);
5733c10c9c4SKumar Gala 	if (cpus_equal(mask, CPU_MASK_ALL)) {
5743c10c9c4SKumar Gala 		static int irq_rover;
5753c10c9c4SKumar Gala 		static DEFINE_SPINLOCK(irq_rover_lock);
5763c10c9c4SKumar Gala 		unsigned long flags;
5773c10c9c4SKumar Gala 
5783c10c9c4SKumar Gala 		/* Round-robin distribution... */
5793c10c9c4SKumar Gala 	do_round_robin:
5803c10c9c4SKumar Gala 		spin_lock_irqsave(&irq_rover_lock, flags);
5813c10c9c4SKumar Gala 
5823c10c9c4SKumar Gala 		while (!cpu_online(irq_rover)) {
5833c10c9c4SKumar Gala 			if (++irq_rover >= NR_CPUS)
5843c10c9c4SKumar Gala 				irq_rover = 0;
5853c10c9c4SKumar Gala 		}
5863c10c9c4SKumar Gala 		cpuid = irq_rover;
5873c10c9c4SKumar Gala 		do {
5883c10c9c4SKumar Gala 			if (++irq_rover >= NR_CPUS)
5893c10c9c4SKumar Gala 				irq_rover = 0;
5903c10c9c4SKumar Gala 		} while (!cpu_online(irq_rover));
5913c10c9c4SKumar Gala 
5923c10c9c4SKumar Gala 		spin_unlock_irqrestore(&irq_rover_lock, flags);
5933c10c9c4SKumar Gala 	} else {
5943c10c9c4SKumar Gala 		cpumask_t tmp;
5953c10c9c4SKumar Gala 
5963c10c9c4SKumar Gala 		cpus_and(tmp, cpu_online_map, mask);
5973c10c9c4SKumar Gala 
5983c10c9c4SKumar Gala 		if (cpus_empty(tmp))
5993c10c9c4SKumar Gala 			goto do_round_robin;
6003c10c9c4SKumar Gala 
6013c10c9c4SKumar Gala 		cpuid = first_cpu(tmp);
6023c10c9c4SKumar Gala 	}
6033c10c9c4SKumar Gala 
6047a0d7940SKumar Gala 	return get_hard_smp_processor_id(cpuid);
6053c10c9c4SKumar Gala }
6063c10c9c4SKumar Gala #else
6073c10c9c4SKumar Gala static int irq_choose_cpu(unsigned int virt_irq)
6083c10c9c4SKumar Gala {
6093c10c9c4SKumar Gala 	return hard_smp_processor_id();
6103c10c9c4SKumar Gala }
6113c10c9c4SKumar Gala #endif
61214cf11afSPaul Mackerras 
6130ebfff14SBenjamin Herrenschmidt #define mpic_irq_to_hw(virq)	((unsigned int)irq_map[virq].hwirq)
6140ebfff14SBenjamin Herrenschmidt 
61514cf11afSPaul Mackerras /* Find an mpic associated with a given linux interrupt */
61614cf11afSPaul Mackerras static struct mpic *mpic_find(unsigned int irq, unsigned int *is_ipi)
61714cf11afSPaul Mackerras {
6180ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
6197df2457dSOlof Johansson 	struct mpic *mpic;
62014cf11afSPaul Mackerras 
6210ebfff14SBenjamin Herrenschmidt 	if (irq < NUM_ISA_INTERRUPTS)
62214cf11afSPaul Mackerras 		return NULL;
6230ebfff14SBenjamin Herrenschmidt 
6247df2457dSOlof Johansson 	mpic = irq_desc[irq].chip_data;
6257df2457dSOlof Johansson 
6267df2457dSOlof Johansson 	if (is_ipi)
6277df2457dSOlof Johansson 		*is_ipi = (src >= mpic->ipi_vecs[0] &&
6287df2457dSOlof Johansson 			   src <= mpic->ipi_vecs[3]);
6297df2457dSOlof Johansson 
6307df2457dSOlof Johansson 	return mpic;
63114cf11afSPaul Mackerras }
63214cf11afSPaul Mackerras 
63314cf11afSPaul Mackerras /* Convert a cpu mask from logical to physical cpu numbers. */
63414cf11afSPaul Mackerras static inline u32 mpic_physmask(u32 cpumask)
63514cf11afSPaul Mackerras {
63614cf11afSPaul Mackerras 	int i;
63714cf11afSPaul Mackerras 	u32 mask = 0;
63814cf11afSPaul Mackerras 
63914cf11afSPaul Mackerras 	for (i = 0; i < NR_CPUS; ++i, cpumask >>= 1)
64014cf11afSPaul Mackerras 		mask |= (cpumask & 1) << get_hard_smp_processor_id(i);
64114cf11afSPaul Mackerras 	return mask;
64214cf11afSPaul Mackerras }
64314cf11afSPaul Mackerras 
64414cf11afSPaul Mackerras #ifdef CONFIG_SMP
64514cf11afSPaul Mackerras /* Get the mpic structure from the IPI number */
64614cf11afSPaul Mackerras static inline struct mpic * mpic_from_ipi(unsigned int ipi)
64714cf11afSPaul Mackerras {
648b9e5b4e6SBenjamin Herrenschmidt 	return irq_desc[ipi].chip_data;
64914cf11afSPaul Mackerras }
65014cf11afSPaul Mackerras #endif
65114cf11afSPaul Mackerras 
65214cf11afSPaul Mackerras /* Get the mpic structure from the irq number */
65314cf11afSPaul Mackerras static inline struct mpic * mpic_from_irq(unsigned int irq)
65414cf11afSPaul Mackerras {
655b9e5b4e6SBenjamin Herrenschmidt 	return irq_desc[irq].chip_data;
65614cf11afSPaul Mackerras }
65714cf11afSPaul Mackerras 
65814cf11afSPaul Mackerras /* Send an EOI */
65914cf11afSPaul Mackerras static inline void mpic_eoi(struct mpic *mpic)
66014cf11afSPaul Mackerras {
6617233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_EOI), 0);
6627233593bSZang Roy-r61911 	(void)mpic_cpu_read(MPIC_INFO(CPU_WHOAMI));
66314cf11afSPaul Mackerras }
66414cf11afSPaul Mackerras 
66514cf11afSPaul Mackerras /*
66614cf11afSPaul Mackerras  * Linux descriptor level callbacks
66714cf11afSPaul Mackerras  */
66814cf11afSPaul Mackerras 
66914cf11afSPaul Mackerras 
67005af7bd2SMichael Ellerman void mpic_unmask_irq(unsigned int irq)
67114cf11afSPaul Mackerras {
67214cf11afSPaul Mackerras 	unsigned int loops = 100000;
67314cf11afSPaul Mackerras 	struct mpic *mpic = mpic_from_irq(irq);
6740ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
67514cf11afSPaul Mackerras 
676bd561c79SPaul Mackerras 	DBG("%p: %s: enable_irq: %d (src %d)\n", mpic, mpic->name, irq, src);
67714cf11afSPaul Mackerras 
6787233593bSZang Roy-r61911 	mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
6797233593bSZang Roy-r61911 		       mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) &
680e5356640SBenjamin Herrenschmidt 		       ~MPIC_VECPRI_MASK);
68114cf11afSPaul Mackerras 	/* make sure mask gets to controller before we return to user */
68214cf11afSPaul Mackerras 	do {
68314cf11afSPaul Mackerras 		if (!loops--) {
68414cf11afSPaul Mackerras 			printk(KERN_ERR "mpic_enable_irq timeout\n");
68514cf11afSPaul Mackerras 			break;
68614cf11afSPaul Mackerras 		}
6877233593bSZang Roy-r61911 	} while(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK);
6881beb6a7dSBenjamin Herrenschmidt }
6891beb6a7dSBenjamin Herrenschmidt 
69005af7bd2SMichael Ellerman void mpic_mask_irq(unsigned int irq)
69114cf11afSPaul Mackerras {
69214cf11afSPaul Mackerras 	unsigned int loops = 100000;
69314cf11afSPaul Mackerras 	struct mpic *mpic = mpic_from_irq(irq);
6940ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
69514cf11afSPaul Mackerras 
69614cf11afSPaul Mackerras 	DBG("%s: disable_irq: %d (src %d)\n", mpic->name, irq, src);
69714cf11afSPaul Mackerras 
6987233593bSZang Roy-r61911 	mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
6997233593bSZang Roy-r61911 		       mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) |
700e5356640SBenjamin Herrenschmidt 		       MPIC_VECPRI_MASK);
70114cf11afSPaul Mackerras 
70214cf11afSPaul Mackerras 	/* make sure mask gets to controller before we return to user */
70314cf11afSPaul Mackerras 	do {
70414cf11afSPaul Mackerras 		if (!loops--) {
70514cf11afSPaul Mackerras 			printk(KERN_ERR "mpic_enable_irq timeout\n");
70614cf11afSPaul Mackerras 			break;
70714cf11afSPaul Mackerras 		}
7087233593bSZang Roy-r61911 	} while(!(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK));
70914cf11afSPaul Mackerras }
71014cf11afSPaul Mackerras 
71105af7bd2SMichael Ellerman void mpic_end_irq(unsigned int irq)
71214cf11afSPaul Mackerras {
71314cf11afSPaul Mackerras 	struct mpic *mpic = mpic_from_irq(irq);
71414cf11afSPaul Mackerras 
7151beb6a7dSBenjamin Herrenschmidt #ifdef DEBUG_IRQ
71614cf11afSPaul Mackerras 	DBG("%s: end_irq: %d\n", mpic->name, irq);
7171beb6a7dSBenjamin Herrenschmidt #endif
71814cf11afSPaul Mackerras 	/* We always EOI on end_irq() even for edge interrupts since that
71914cf11afSPaul Mackerras 	 * should only lower the priority, the MPIC should have properly
72014cf11afSPaul Mackerras 	 * latched another edge interrupt coming in anyway
72114cf11afSPaul Mackerras 	 */
72214cf11afSPaul Mackerras 
72314cf11afSPaul Mackerras 	mpic_eoi(mpic);
72414cf11afSPaul Mackerras }
72514cf11afSPaul Mackerras 
7266cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
727b9e5b4e6SBenjamin Herrenschmidt 
728b9e5b4e6SBenjamin Herrenschmidt static void mpic_unmask_ht_irq(unsigned int irq)
729b9e5b4e6SBenjamin Herrenschmidt {
730b9e5b4e6SBenjamin Herrenschmidt 	struct mpic *mpic = mpic_from_irq(irq);
7310ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
732b9e5b4e6SBenjamin Herrenschmidt 
733b9e5b4e6SBenjamin Herrenschmidt 	mpic_unmask_irq(irq);
734b9e5b4e6SBenjamin Herrenschmidt 
735b9e5b4e6SBenjamin Herrenschmidt 	if (irq_desc[irq].status & IRQ_LEVEL)
736b9e5b4e6SBenjamin Herrenschmidt 		mpic_ht_end_irq(mpic, src);
737b9e5b4e6SBenjamin Herrenschmidt }
738b9e5b4e6SBenjamin Herrenschmidt 
739b9e5b4e6SBenjamin Herrenschmidt static unsigned int mpic_startup_ht_irq(unsigned int irq)
740b9e5b4e6SBenjamin Herrenschmidt {
741b9e5b4e6SBenjamin Herrenschmidt 	struct mpic *mpic = mpic_from_irq(irq);
7420ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
743b9e5b4e6SBenjamin Herrenschmidt 
744b9e5b4e6SBenjamin Herrenschmidt 	mpic_unmask_irq(irq);
745b9e5b4e6SBenjamin Herrenschmidt 	mpic_startup_ht_interrupt(mpic, src, irq_desc[irq].status);
746b9e5b4e6SBenjamin Herrenschmidt 
747b9e5b4e6SBenjamin Herrenschmidt 	return 0;
748b9e5b4e6SBenjamin Herrenschmidt }
749b9e5b4e6SBenjamin Herrenschmidt 
750b9e5b4e6SBenjamin Herrenschmidt static void mpic_shutdown_ht_irq(unsigned int irq)
751b9e5b4e6SBenjamin Herrenschmidt {
752b9e5b4e6SBenjamin Herrenschmidt 	struct mpic *mpic = mpic_from_irq(irq);
7530ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
754b9e5b4e6SBenjamin Herrenschmidt 
755b9e5b4e6SBenjamin Herrenschmidt 	mpic_shutdown_ht_interrupt(mpic, src, irq_desc[irq].status);
756b9e5b4e6SBenjamin Herrenschmidt 	mpic_mask_irq(irq);
757b9e5b4e6SBenjamin Herrenschmidt }
758b9e5b4e6SBenjamin Herrenschmidt 
759b9e5b4e6SBenjamin Herrenschmidt static void mpic_end_ht_irq(unsigned int irq)
760b9e5b4e6SBenjamin Herrenschmidt {
761b9e5b4e6SBenjamin Herrenschmidt 	struct mpic *mpic = mpic_from_irq(irq);
7620ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
763b9e5b4e6SBenjamin Herrenschmidt 
764b9e5b4e6SBenjamin Herrenschmidt #ifdef DEBUG_IRQ
765b9e5b4e6SBenjamin Herrenschmidt 	DBG("%s: end_irq: %d\n", mpic->name, irq);
766b9e5b4e6SBenjamin Herrenschmidt #endif
767b9e5b4e6SBenjamin Herrenschmidt 	/* We always EOI on end_irq() even for edge interrupts since that
768b9e5b4e6SBenjamin Herrenschmidt 	 * should only lower the priority, the MPIC should have properly
769b9e5b4e6SBenjamin Herrenschmidt 	 * latched another edge interrupt coming in anyway
770b9e5b4e6SBenjamin Herrenschmidt 	 */
771b9e5b4e6SBenjamin Herrenschmidt 
772b9e5b4e6SBenjamin Herrenschmidt 	if (irq_desc[irq].status & IRQ_LEVEL)
773b9e5b4e6SBenjamin Herrenschmidt 		mpic_ht_end_irq(mpic, src);
774b9e5b4e6SBenjamin Herrenschmidt 	mpic_eoi(mpic);
775b9e5b4e6SBenjamin Herrenschmidt }
7766cfef5b2SMichael Ellerman #endif /* !CONFIG_MPIC_U3_HT_IRQS */
777b9e5b4e6SBenjamin Herrenschmidt 
77814cf11afSPaul Mackerras #ifdef CONFIG_SMP
77914cf11afSPaul Mackerras 
780b9e5b4e6SBenjamin Herrenschmidt static void mpic_unmask_ipi(unsigned int irq)
78114cf11afSPaul Mackerras {
78214cf11afSPaul Mackerras 	struct mpic *mpic = mpic_from_ipi(irq);
7837df2457dSOlof Johansson 	unsigned int src = mpic_irq_to_hw(irq) - mpic->ipi_vecs[0];
78414cf11afSPaul Mackerras 
78514cf11afSPaul Mackerras 	DBG("%s: enable_ipi: %d (ipi %d)\n", mpic->name, irq, src);
78614cf11afSPaul Mackerras 	mpic_ipi_write(src, mpic_ipi_read(src) & ~MPIC_VECPRI_MASK);
78714cf11afSPaul Mackerras }
78814cf11afSPaul Mackerras 
789b9e5b4e6SBenjamin Herrenschmidt static void mpic_mask_ipi(unsigned int irq)
79014cf11afSPaul Mackerras {
79114cf11afSPaul Mackerras 	/* NEVER disable an IPI... that's just plain wrong! */
79214cf11afSPaul Mackerras }
79314cf11afSPaul Mackerras 
79414cf11afSPaul Mackerras static void mpic_end_ipi(unsigned int irq)
79514cf11afSPaul Mackerras {
79614cf11afSPaul Mackerras 	struct mpic *mpic = mpic_from_ipi(irq);
79714cf11afSPaul Mackerras 
79814cf11afSPaul Mackerras 	/*
79914cf11afSPaul Mackerras 	 * IPIs are marked IRQ_PER_CPU. This has the side effect of
80014cf11afSPaul Mackerras 	 * preventing the IRQ_PENDING/IRQ_INPROGRESS logic from
80114cf11afSPaul Mackerras 	 * applying to them. We EOI them late to avoid re-entering.
8026714465eSThomas Gleixner 	 * We mark IPI's with IRQF_DISABLED as they must run with
80314cf11afSPaul Mackerras 	 * irqs disabled.
80414cf11afSPaul Mackerras 	 */
80514cf11afSPaul Mackerras 	mpic_eoi(mpic);
80614cf11afSPaul Mackerras }
80714cf11afSPaul Mackerras 
80814cf11afSPaul Mackerras #endif /* CONFIG_SMP */
80914cf11afSPaul Mackerras 
810*d5dedd45SYinghai Lu int mpic_set_affinity(unsigned int irq, const struct cpumask *cpumask)
81114cf11afSPaul Mackerras {
81214cf11afSPaul Mackerras 	struct mpic *mpic = mpic_from_irq(irq);
8130ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
81414cf11afSPaul Mackerras 
8153c10c9c4SKumar Gala 	if (mpic->flags & MPIC_SINGLE_DEST_CPU) {
8163c10c9c4SKumar Gala 		int cpuid = irq_choose_cpu(irq);
8173c10c9c4SKumar Gala 
8183c10c9c4SKumar Gala 		mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION), 1 << cpuid);
8193c10c9c4SKumar Gala 	} else {
82014cf11afSPaul Mackerras 		cpumask_t tmp;
82114cf11afSPaul Mackerras 
8220de26520SRusty Russell 		cpumask_and(&tmp, cpumask, cpu_online_mask);
82314cf11afSPaul Mackerras 
8247233593bSZang Roy-r61911 		mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION),
82514cf11afSPaul Mackerras 			       mpic_physmask(cpus_addr(tmp)[0]));
82614cf11afSPaul Mackerras 	}
827*d5dedd45SYinghai Lu 
828*d5dedd45SYinghai Lu 	return 0;
8293c10c9c4SKumar Gala }
83014cf11afSPaul Mackerras 
8317233593bSZang Roy-r61911 static unsigned int mpic_type_to_vecpri(struct mpic *mpic, unsigned int type)
8320ebfff14SBenjamin Herrenschmidt {
8330ebfff14SBenjamin Herrenschmidt 	/* Now convert sense value */
8346e99e458SBenjamin Herrenschmidt 	switch(type & IRQ_TYPE_SENSE_MASK) {
8350ebfff14SBenjamin Herrenschmidt 	case IRQ_TYPE_EDGE_RISING:
8367233593bSZang Roy-r61911 		return MPIC_INFO(VECPRI_SENSE_EDGE) |
8377233593bSZang Roy-r61911 		       MPIC_INFO(VECPRI_POLARITY_POSITIVE);
8380ebfff14SBenjamin Herrenschmidt 	case IRQ_TYPE_EDGE_FALLING:
8396e99e458SBenjamin Herrenschmidt 	case IRQ_TYPE_EDGE_BOTH:
8407233593bSZang Roy-r61911 		return MPIC_INFO(VECPRI_SENSE_EDGE) |
8417233593bSZang Roy-r61911 		       MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
8420ebfff14SBenjamin Herrenschmidt 	case IRQ_TYPE_LEVEL_HIGH:
8437233593bSZang Roy-r61911 		return MPIC_INFO(VECPRI_SENSE_LEVEL) |
8447233593bSZang Roy-r61911 		       MPIC_INFO(VECPRI_POLARITY_POSITIVE);
8450ebfff14SBenjamin Herrenschmidt 	case IRQ_TYPE_LEVEL_LOW:
8460ebfff14SBenjamin Herrenschmidt 	default:
8477233593bSZang Roy-r61911 		return MPIC_INFO(VECPRI_SENSE_LEVEL) |
8487233593bSZang Roy-r61911 		       MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
8490ebfff14SBenjamin Herrenschmidt 	}
8506e99e458SBenjamin Herrenschmidt }
8516e99e458SBenjamin Herrenschmidt 
85205af7bd2SMichael Ellerman int mpic_set_irq_type(unsigned int virq, unsigned int flow_type)
8536e99e458SBenjamin Herrenschmidt {
8546e99e458SBenjamin Herrenschmidt 	struct mpic *mpic = mpic_from_irq(virq);
8556e99e458SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(virq);
8566e99e458SBenjamin Herrenschmidt 	struct irq_desc *desc = get_irq_desc(virq);
8576e99e458SBenjamin Herrenschmidt 	unsigned int vecpri, vold, vnew;
8586e99e458SBenjamin Herrenschmidt 
85906fe98e6SBenjamin Herrenschmidt 	DBG("mpic: set_irq_type(mpic:@%p,virq:%d,src:0x%x,type:0x%x)\n",
8606e99e458SBenjamin Herrenschmidt 	    mpic, virq, src, flow_type);
8616e99e458SBenjamin Herrenschmidt 
8626e99e458SBenjamin Herrenschmidt 	if (src >= mpic->irq_count)
8636e99e458SBenjamin Herrenschmidt 		return -EINVAL;
8646e99e458SBenjamin Herrenschmidt 
8656e99e458SBenjamin Herrenschmidt 	if (flow_type == IRQ_TYPE_NONE)
8666e99e458SBenjamin Herrenschmidt 		if (mpic->senses && src < mpic->senses_count)
8676e99e458SBenjamin Herrenschmidt 			flow_type = mpic->senses[src];
8686e99e458SBenjamin Herrenschmidt 	if (flow_type == IRQ_TYPE_NONE)
8696e99e458SBenjamin Herrenschmidt 		flow_type = IRQ_TYPE_LEVEL_LOW;
8706e99e458SBenjamin Herrenschmidt 
8716e99e458SBenjamin Herrenschmidt 	desc->status &= ~(IRQ_TYPE_SENSE_MASK | IRQ_LEVEL);
8726e99e458SBenjamin Herrenschmidt 	desc->status |= flow_type & IRQ_TYPE_SENSE_MASK;
8736e99e458SBenjamin Herrenschmidt 	if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
8746e99e458SBenjamin Herrenschmidt 		desc->status |= IRQ_LEVEL;
8756e99e458SBenjamin Herrenschmidt 
8766e99e458SBenjamin Herrenschmidt 	if (mpic_is_ht_interrupt(mpic, src))
8776e99e458SBenjamin Herrenschmidt 		vecpri = MPIC_VECPRI_POLARITY_POSITIVE |
8786e99e458SBenjamin Herrenschmidt 			MPIC_VECPRI_SENSE_EDGE;
8796e99e458SBenjamin Herrenschmidt 	else
8807233593bSZang Roy-r61911 		vecpri = mpic_type_to_vecpri(mpic, flow_type);
8816e99e458SBenjamin Herrenschmidt 
8827233593bSZang Roy-r61911 	vold = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
8837233593bSZang Roy-r61911 	vnew = vold & ~(MPIC_INFO(VECPRI_POLARITY_MASK) |
8847233593bSZang Roy-r61911 			MPIC_INFO(VECPRI_SENSE_MASK));
8856e99e458SBenjamin Herrenschmidt 	vnew |= vecpri;
8866e99e458SBenjamin Herrenschmidt 	if (vold != vnew)
8877233593bSZang Roy-r61911 		mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI), vnew);
8886e99e458SBenjamin Herrenschmidt 
8896e99e458SBenjamin Herrenschmidt 	return 0;
8900ebfff14SBenjamin Herrenschmidt }
8910ebfff14SBenjamin Herrenschmidt 
89238958dd9SOlof Johansson void mpic_set_vector(unsigned int virq, unsigned int vector)
89338958dd9SOlof Johansson {
89438958dd9SOlof Johansson 	struct mpic *mpic = mpic_from_irq(virq);
89538958dd9SOlof Johansson 	unsigned int src = mpic_irq_to_hw(virq);
89638958dd9SOlof Johansson 	unsigned int vecpri;
89738958dd9SOlof Johansson 
89838958dd9SOlof Johansson 	DBG("mpic: set_vector(mpic:@%p,virq:%d,src:%d,vector:0x%x)\n",
89938958dd9SOlof Johansson 	    mpic, virq, src, vector);
90038958dd9SOlof Johansson 
90138958dd9SOlof Johansson 	if (src >= mpic->irq_count)
90238958dd9SOlof Johansson 		return;
90338958dd9SOlof Johansson 
90438958dd9SOlof Johansson 	vecpri = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
90538958dd9SOlof Johansson 	vecpri = vecpri & ~MPIC_INFO(VECPRI_VECTOR_MASK);
90638958dd9SOlof Johansson 	vecpri |= vector;
90738958dd9SOlof Johansson 	mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI), vecpri);
90838958dd9SOlof Johansson }
90938958dd9SOlof Johansson 
910b9e5b4e6SBenjamin Herrenschmidt static struct irq_chip mpic_irq_chip = {
911b9e5b4e6SBenjamin Herrenschmidt 	.mask		= mpic_mask_irq,
912b9e5b4e6SBenjamin Herrenschmidt 	.unmask		= mpic_unmask_irq,
913b9e5b4e6SBenjamin Herrenschmidt 	.eoi		= mpic_end_irq,
9146e99e458SBenjamin Herrenschmidt 	.set_type	= mpic_set_irq_type,
915b9e5b4e6SBenjamin Herrenschmidt };
916b9e5b4e6SBenjamin Herrenschmidt 
917b9e5b4e6SBenjamin Herrenschmidt #ifdef CONFIG_SMP
918b9e5b4e6SBenjamin Herrenschmidt static struct irq_chip mpic_ipi_chip = {
919b9e5b4e6SBenjamin Herrenschmidt 	.mask		= mpic_mask_ipi,
920b9e5b4e6SBenjamin Herrenschmidt 	.unmask		= mpic_unmask_ipi,
921b9e5b4e6SBenjamin Herrenschmidt 	.eoi		= mpic_end_ipi,
922b9e5b4e6SBenjamin Herrenschmidt };
923b9e5b4e6SBenjamin Herrenschmidt #endif /* CONFIG_SMP */
924b9e5b4e6SBenjamin Herrenschmidt 
9256cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
926b9e5b4e6SBenjamin Herrenschmidt static struct irq_chip mpic_irq_ht_chip = {
927b9e5b4e6SBenjamin Herrenschmidt 	.startup	= mpic_startup_ht_irq,
928b9e5b4e6SBenjamin Herrenschmidt 	.shutdown	= mpic_shutdown_ht_irq,
929b9e5b4e6SBenjamin Herrenschmidt 	.mask		= mpic_mask_irq,
930b9e5b4e6SBenjamin Herrenschmidt 	.unmask		= mpic_unmask_ht_irq,
931b9e5b4e6SBenjamin Herrenschmidt 	.eoi		= mpic_end_ht_irq,
9326e99e458SBenjamin Herrenschmidt 	.set_type	= mpic_set_irq_type,
933b9e5b4e6SBenjamin Herrenschmidt };
9346cfef5b2SMichael Ellerman #endif /* CONFIG_MPIC_U3_HT_IRQS */
935b9e5b4e6SBenjamin Herrenschmidt 
93614cf11afSPaul Mackerras 
9370ebfff14SBenjamin Herrenschmidt static int mpic_host_match(struct irq_host *h, struct device_node *node)
9380ebfff14SBenjamin Herrenschmidt {
9390ebfff14SBenjamin Herrenschmidt 	/* Exact match, unless mpic node is NULL */
94052964f87SMichael Ellerman 	return h->of_node == NULL || h->of_node == node;
9410ebfff14SBenjamin Herrenschmidt }
9420ebfff14SBenjamin Herrenschmidt 
9430ebfff14SBenjamin Herrenschmidt static int mpic_host_map(struct irq_host *h, unsigned int virq,
9446e99e458SBenjamin Herrenschmidt 			 irq_hw_number_t hw)
9450ebfff14SBenjamin Herrenschmidt {
9460ebfff14SBenjamin Herrenschmidt 	struct mpic *mpic = h->host_data;
9476e99e458SBenjamin Herrenschmidt 	struct irq_chip *chip;
9480ebfff14SBenjamin Herrenschmidt 
94906fe98e6SBenjamin Herrenschmidt 	DBG("mpic: map virq %d, hwirq 0x%lx\n", virq, hw);
9500ebfff14SBenjamin Herrenschmidt 
9517df2457dSOlof Johansson 	if (hw == mpic->spurious_vec)
9520ebfff14SBenjamin Herrenschmidt 		return -EINVAL;
9537fd72186SBenjamin Herrenschmidt 	if (mpic->protected && test_bit(hw, mpic->protected))
9547fd72186SBenjamin Herrenschmidt 		return -EINVAL;
95506fe98e6SBenjamin Herrenschmidt 
9560ebfff14SBenjamin Herrenschmidt #ifdef CONFIG_SMP
9577df2457dSOlof Johansson 	else if (hw >= mpic->ipi_vecs[0]) {
9580ebfff14SBenjamin Herrenschmidt 		WARN_ON(!(mpic->flags & MPIC_PRIMARY));
9590ebfff14SBenjamin Herrenschmidt 
96006fe98e6SBenjamin Herrenschmidt 		DBG("mpic: mapping as IPI\n");
9610ebfff14SBenjamin Herrenschmidt 		set_irq_chip_data(virq, mpic);
9620ebfff14SBenjamin Herrenschmidt 		set_irq_chip_and_handler(virq, &mpic->hc_ipi,
9630ebfff14SBenjamin Herrenschmidt 					 handle_percpu_irq);
9640ebfff14SBenjamin Herrenschmidt 		return 0;
9650ebfff14SBenjamin Herrenschmidt 	}
9660ebfff14SBenjamin Herrenschmidt #endif /* CONFIG_SMP */
9670ebfff14SBenjamin Herrenschmidt 
9680ebfff14SBenjamin Herrenschmidt 	if (hw >= mpic->irq_count)
9690ebfff14SBenjamin Herrenschmidt 		return -EINVAL;
9700ebfff14SBenjamin Herrenschmidt 
971a7de7c74SMichael Ellerman 	mpic_msi_reserve_hwirq(mpic, hw);
972a7de7c74SMichael Ellerman 
9736e99e458SBenjamin Herrenschmidt 	/* Default chip */
9740ebfff14SBenjamin Herrenschmidt 	chip = &mpic->hc_irq;
9750ebfff14SBenjamin Herrenschmidt 
9766cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
9770ebfff14SBenjamin Herrenschmidt 	/* Check for HT interrupts, override vecpri */
9786e99e458SBenjamin Herrenschmidt 	if (mpic_is_ht_interrupt(mpic, hw))
9790ebfff14SBenjamin Herrenschmidt 		chip = &mpic->hc_ht_irq;
9806cfef5b2SMichael Ellerman #endif /* CONFIG_MPIC_U3_HT_IRQS */
9810ebfff14SBenjamin Herrenschmidt 
98206fe98e6SBenjamin Herrenschmidt 	DBG("mpic: mapping to irq chip @%p\n", chip);
9830ebfff14SBenjamin Herrenschmidt 
9840ebfff14SBenjamin Herrenschmidt 	set_irq_chip_data(virq, mpic);
9850ebfff14SBenjamin Herrenschmidt 	set_irq_chip_and_handler(virq, chip, handle_fasteoi_irq);
9866e99e458SBenjamin Herrenschmidt 
9876e99e458SBenjamin Herrenschmidt 	/* Set default irq type */
9886e99e458SBenjamin Herrenschmidt 	set_irq_type(virq, IRQ_TYPE_NONE);
9896e99e458SBenjamin Herrenschmidt 
9900ebfff14SBenjamin Herrenschmidt 	return 0;
9910ebfff14SBenjamin Herrenschmidt }
9920ebfff14SBenjamin Herrenschmidt 
9930ebfff14SBenjamin Herrenschmidt static int mpic_host_xlate(struct irq_host *h, struct device_node *ct,
9940ebfff14SBenjamin Herrenschmidt 			   u32 *intspec, unsigned int intsize,
9950ebfff14SBenjamin Herrenschmidt 			   irq_hw_number_t *out_hwirq, unsigned int *out_flags)
9960ebfff14SBenjamin Herrenschmidt 
9970ebfff14SBenjamin Herrenschmidt {
9980ebfff14SBenjamin Herrenschmidt 	static unsigned char map_mpic_senses[4] = {
9990ebfff14SBenjamin Herrenschmidt 		IRQ_TYPE_EDGE_RISING,
10000ebfff14SBenjamin Herrenschmidt 		IRQ_TYPE_LEVEL_LOW,
10010ebfff14SBenjamin Herrenschmidt 		IRQ_TYPE_LEVEL_HIGH,
10020ebfff14SBenjamin Herrenschmidt 		IRQ_TYPE_EDGE_FALLING,
10030ebfff14SBenjamin Herrenschmidt 	};
10040ebfff14SBenjamin Herrenschmidt 
10050ebfff14SBenjamin Herrenschmidt 	*out_hwirq = intspec[0];
100606fe98e6SBenjamin Herrenschmidt 	if (intsize > 1) {
100706fe98e6SBenjamin Herrenschmidt 		u32 mask = 0x3;
100806fe98e6SBenjamin Herrenschmidt 
100906fe98e6SBenjamin Herrenschmidt 		/* Apple invented a new race of encoding on machines with
101006fe98e6SBenjamin Herrenschmidt 		 * an HT APIC. They encode, among others, the index within
101106fe98e6SBenjamin Herrenschmidt 		 * the HT APIC. We don't care about it here since thankfully,
101206fe98e6SBenjamin Herrenschmidt 		 * it appears that they have the APIC already properly
101306fe98e6SBenjamin Herrenschmidt 		 * configured, and thus our current fixup code that reads the
101406fe98e6SBenjamin Herrenschmidt 		 * APIC config works fine. However, we still need to mask out
101506fe98e6SBenjamin Herrenschmidt 		 * bits in the specifier to make sure we only get bit 0 which
101606fe98e6SBenjamin Herrenschmidt 		 * is the level/edge bit (the only sense bit exposed by Apple),
101706fe98e6SBenjamin Herrenschmidt 		 * as their bit 1 means something else.
101806fe98e6SBenjamin Herrenschmidt 		 */
101906fe98e6SBenjamin Herrenschmidt 		if (machine_is(powermac))
102006fe98e6SBenjamin Herrenschmidt 			mask = 0x1;
102106fe98e6SBenjamin Herrenschmidt 		*out_flags = map_mpic_senses[intspec[1] & mask];
102206fe98e6SBenjamin Herrenschmidt 	} else
10230ebfff14SBenjamin Herrenschmidt 		*out_flags = IRQ_TYPE_NONE;
10240ebfff14SBenjamin Herrenschmidt 
102506fe98e6SBenjamin Herrenschmidt 	DBG("mpic: xlate (%d cells: 0x%08x 0x%08x) to line 0x%lx sense 0x%x\n",
102606fe98e6SBenjamin Herrenschmidt 	    intsize, intspec[0], intspec[1], *out_hwirq, *out_flags);
102706fe98e6SBenjamin Herrenschmidt 
10280ebfff14SBenjamin Herrenschmidt 	return 0;
10290ebfff14SBenjamin Herrenschmidt }
10300ebfff14SBenjamin Herrenschmidt 
10310ebfff14SBenjamin Herrenschmidt static struct irq_host_ops mpic_host_ops = {
10320ebfff14SBenjamin Herrenschmidt 	.match = mpic_host_match,
10330ebfff14SBenjamin Herrenschmidt 	.map = mpic_host_map,
10340ebfff14SBenjamin Herrenschmidt 	.xlate = mpic_host_xlate,
10350ebfff14SBenjamin Herrenschmidt };
10360ebfff14SBenjamin Herrenschmidt 
103714cf11afSPaul Mackerras /*
103814cf11afSPaul Mackerras  * Exported functions
103914cf11afSPaul Mackerras  */
104014cf11afSPaul Mackerras 
10410ebfff14SBenjamin Herrenschmidt struct mpic * __init mpic_alloc(struct device_node *node,
1042a959ff56SBenjamin Herrenschmidt 				phys_addr_t phys_addr,
104314cf11afSPaul Mackerras 				unsigned int flags,
104414cf11afSPaul Mackerras 				unsigned int isu_size,
104514cf11afSPaul Mackerras 				unsigned int irq_count,
104614cf11afSPaul Mackerras 				const char *name)
104714cf11afSPaul Mackerras {
104814cf11afSPaul Mackerras 	struct mpic	*mpic;
1049d9d1063dSJohannes Berg 	u32		greg_feature;
105014cf11afSPaul Mackerras 	const char	*vers;
105114cf11afSPaul Mackerras 	int		i;
10527df2457dSOlof Johansson 	int		intvec_top;
1053a959ff56SBenjamin Herrenschmidt 	u64		paddr = phys_addr;
105414cf11afSPaul Mackerras 
105514cf11afSPaul Mackerras 	mpic = alloc_bootmem(sizeof(struct mpic));
105614cf11afSPaul Mackerras 	if (mpic == NULL)
105714cf11afSPaul Mackerras 		return NULL;
105814cf11afSPaul Mackerras 
105914cf11afSPaul Mackerras 	memset(mpic, 0, sizeof(struct mpic));
106014cf11afSPaul Mackerras 	mpic->name = name;
106114cf11afSPaul Mackerras 
106219fc65b5SMichael Ellerman 	mpic->irqhost = irq_alloc_host(node, IRQ_HOST_MAP_LINEAR,
106352964f87SMichael Ellerman 				       isu_size, &mpic_host_ops,
10647df2457dSOlof Johansson 				       flags & MPIC_LARGE_VECTORS ? 2048 : 256);
106519fc65b5SMichael Ellerman 	if (mpic->irqhost == NULL)
10660ebfff14SBenjamin Herrenschmidt 		return NULL;
10670ebfff14SBenjamin Herrenschmidt 
10680ebfff14SBenjamin Herrenschmidt 	mpic->irqhost->host_data = mpic;
1069b9e5b4e6SBenjamin Herrenschmidt 	mpic->hc_irq = mpic_irq_chip;
107014cf11afSPaul Mackerras 	mpic->hc_irq.typename = name;
107114cf11afSPaul Mackerras 	if (flags & MPIC_PRIMARY)
107214cf11afSPaul Mackerras 		mpic->hc_irq.set_affinity = mpic_set_affinity;
10736cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
1074b9e5b4e6SBenjamin Herrenschmidt 	mpic->hc_ht_irq = mpic_irq_ht_chip;
1075b9e5b4e6SBenjamin Herrenschmidt 	mpic->hc_ht_irq.typename = name;
1076b9e5b4e6SBenjamin Herrenschmidt 	if (flags & MPIC_PRIMARY)
1077b9e5b4e6SBenjamin Herrenschmidt 		mpic->hc_ht_irq.set_affinity = mpic_set_affinity;
10786cfef5b2SMichael Ellerman #endif /* CONFIG_MPIC_U3_HT_IRQS */
1079fbf0274eSBenjamin Herrenschmidt 
108014cf11afSPaul Mackerras #ifdef CONFIG_SMP
1081b9e5b4e6SBenjamin Herrenschmidt 	mpic->hc_ipi = mpic_ipi_chip;
10820ebfff14SBenjamin Herrenschmidt 	mpic->hc_ipi.typename = name;
108314cf11afSPaul Mackerras #endif /* CONFIG_SMP */
108414cf11afSPaul Mackerras 
108514cf11afSPaul Mackerras 	mpic->flags = flags;
108614cf11afSPaul Mackerras 	mpic->isu_size = isu_size;
108714cf11afSPaul Mackerras 	mpic->irq_count = irq_count;
108814cf11afSPaul Mackerras 	mpic->num_sources = 0; /* so far */
108914cf11afSPaul Mackerras 
10907df2457dSOlof Johansson 	if (flags & MPIC_LARGE_VECTORS)
10917df2457dSOlof Johansson 		intvec_top = 2047;
10927df2457dSOlof Johansson 	else
10937df2457dSOlof Johansson 		intvec_top = 255;
10947df2457dSOlof Johansson 
10957df2457dSOlof Johansson 	mpic->timer_vecs[0] = intvec_top - 8;
10967df2457dSOlof Johansson 	mpic->timer_vecs[1] = intvec_top - 7;
10977df2457dSOlof Johansson 	mpic->timer_vecs[2] = intvec_top - 6;
10987df2457dSOlof Johansson 	mpic->timer_vecs[3] = intvec_top - 5;
10997df2457dSOlof Johansson 	mpic->ipi_vecs[0]   = intvec_top - 4;
11007df2457dSOlof Johansson 	mpic->ipi_vecs[1]   = intvec_top - 3;
11017df2457dSOlof Johansson 	mpic->ipi_vecs[2]   = intvec_top - 2;
11027df2457dSOlof Johansson 	mpic->ipi_vecs[3]   = intvec_top - 1;
11037df2457dSOlof Johansson 	mpic->spurious_vec  = intvec_top;
11047df2457dSOlof Johansson 
1105a959ff56SBenjamin Herrenschmidt 	/* Check for "big-endian" in device-tree */
1106e2eb6392SStephen Rothwell 	if (node && of_get_property(node, "big-endian", NULL) != NULL)
1107a959ff56SBenjamin Herrenschmidt 		mpic->flags |= MPIC_BIG_ENDIAN;
1108a959ff56SBenjamin Herrenschmidt 
11097fd72186SBenjamin Herrenschmidt 	/* Look for protected sources */
11107fd72186SBenjamin Herrenschmidt 	if (node) {
1111d9d1063dSJohannes Berg 		int psize;
1112d9d1063dSJohannes Berg 		unsigned int bits, mapsize;
11137fd72186SBenjamin Herrenschmidt 		const u32 *psrc =
11147fd72186SBenjamin Herrenschmidt 			of_get_property(node, "protected-sources", &psize);
11157fd72186SBenjamin Herrenschmidt 		if (psrc) {
11167fd72186SBenjamin Herrenschmidt 			psize /= 4;
11177fd72186SBenjamin Herrenschmidt 			bits = intvec_top + 1;
11187fd72186SBenjamin Herrenschmidt 			mapsize = BITS_TO_LONGS(bits) * sizeof(unsigned long);
11197fd72186SBenjamin Herrenschmidt 			mpic->protected = alloc_bootmem(mapsize);
11207fd72186SBenjamin Herrenschmidt 			BUG_ON(mpic->protected == NULL);
11217fd72186SBenjamin Herrenschmidt 			memset(mpic->protected, 0, mapsize);
11227fd72186SBenjamin Herrenschmidt 			for (i = 0; i < psize; i++) {
11237fd72186SBenjamin Herrenschmidt 				if (psrc[i] > intvec_top)
11247fd72186SBenjamin Herrenschmidt 					continue;
11257fd72186SBenjamin Herrenschmidt 				__set_bit(psrc[i], mpic->protected);
11267fd72186SBenjamin Herrenschmidt 			}
11277fd72186SBenjamin Herrenschmidt 		}
11287fd72186SBenjamin Herrenschmidt 	}
1129a959ff56SBenjamin Herrenschmidt 
11307233593bSZang Roy-r61911 #ifdef CONFIG_MPIC_WEIRD
11317233593bSZang Roy-r61911 	mpic->hw_set = mpic_infos[MPIC_GET_REGSET(flags)];
11327233593bSZang Roy-r61911 #endif
11337233593bSZang Roy-r61911 
1134fbf0274eSBenjamin Herrenschmidt 	/* default register type */
1135fbf0274eSBenjamin Herrenschmidt 	mpic->reg_type = (flags & MPIC_BIG_ENDIAN) ?
1136fbf0274eSBenjamin Herrenschmidt 		mpic_access_mmio_be : mpic_access_mmio_le;
1137fbf0274eSBenjamin Herrenschmidt 
1138a959ff56SBenjamin Herrenschmidt 	/* If no physical address is passed in, a device-node is mandatory */
1139a959ff56SBenjamin Herrenschmidt 	BUG_ON(paddr == 0 && node == NULL);
1140a959ff56SBenjamin Herrenschmidt 
1141a959ff56SBenjamin Herrenschmidt 	/* If no physical address passed in, check if it's dcr based */
11420411a5e2SMichael Ellerman 	if (paddr == 0 && of_get_property(node, "dcr-reg", NULL) != NULL) {
1143fbf0274eSBenjamin Herrenschmidt #ifdef CONFIG_PPC_DCR
11440411a5e2SMichael Ellerman 		mpic->flags |= MPIC_USES_DCR;
1145fbf0274eSBenjamin Herrenschmidt 		mpic->reg_type = mpic_access_dcr;
1146fbf0274eSBenjamin Herrenschmidt #else
11470411a5e2SMichael Ellerman 		BUG();
1148fbf0274eSBenjamin Herrenschmidt #endif /* CONFIG_PPC_DCR */
11490411a5e2SMichael Ellerman 	}
1150fbf0274eSBenjamin Herrenschmidt 
1151a959ff56SBenjamin Herrenschmidt 	/* If the MPIC is not DCR based, and no physical address was passed
1152a959ff56SBenjamin Herrenschmidt 	 * in, try to obtain one
1153a959ff56SBenjamin Herrenschmidt 	 */
1154a959ff56SBenjamin Herrenschmidt 	if (paddr == 0 && !(mpic->flags & MPIC_USES_DCR)) {
1155d9d1063dSJohannes Berg 		const u32 *reg = of_get_property(node, "reg", NULL);
1156a959ff56SBenjamin Herrenschmidt 		BUG_ON(reg == NULL);
1157a959ff56SBenjamin Herrenschmidt 		paddr = of_translate_address(node, reg);
1158a959ff56SBenjamin Herrenschmidt 		BUG_ON(paddr == OF_BAD_ADDR);
1159a959ff56SBenjamin Herrenschmidt 	}
1160a959ff56SBenjamin Herrenschmidt 
116114cf11afSPaul Mackerras 	/* Map the global registers */
1162a959ff56SBenjamin Herrenschmidt 	mpic_map(mpic, paddr, &mpic->gregs, MPIC_INFO(GREG_BASE), 0x1000);
1163a959ff56SBenjamin Herrenschmidt 	mpic_map(mpic, paddr, &mpic->tmregs, MPIC_INFO(TIMER_BASE), 0x1000);
116414cf11afSPaul Mackerras 
116514cf11afSPaul Mackerras 	/* Reset */
116614cf11afSPaul Mackerras 	if (flags & MPIC_WANTS_RESET) {
11677233593bSZang Roy-r61911 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
11687233593bSZang Roy-r61911 			   mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
116914cf11afSPaul Mackerras 			   | MPIC_GREG_GCONF_RESET);
11707233593bSZang Roy-r61911 		while( mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
117114cf11afSPaul Mackerras 		       & MPIC_GREG_GCONF_RESET)
117214cf11afSPaul Mackerras 			mb();
117314cf11afSPaul Mackerras 	}
117414cf11afSPaul Mackerras 
1175d91e4ea7SKumar Gala 	/* CoreInt */
1176d91e4ea7SKumar Gala 	if (flags & MPIC_ENABLE_COREINT)
1177d91e4ea7SKumar Gala 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1178d91e4ea7SKumar Gala 			   mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1179d91e4ea7SKumar Gala 			   | MPIC_GREG_GCONF_COREINT);
1180d91e4ea7SKumar Gala 
1181f365355eSOlof Johansson 	if (flags & MPIC_ENABLE_MCK)
1182f365355eSOlof Johansson 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1183f365355eSOlof Johansson 			   mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1184f365355eSOlof Johansson 			   | MPIC_GREG_GCONF_MCK);
1185f365355eSOlof Johansson 
118614cf11afSPaul Mackerras 	/* Read feature register, calculate num CPUs and, for non-ISU
118714cf11afSPaul Mackerras 	 * MPICs, num sources as well. On ISU MPICs, sources are counted
118814cf11afSPaul Mackerras 	 * as ISUs are added
118914cf11afSPaul Mackerras 	 */
1190d9d1063dSJohannes Berg 	greg_feature = mpic_read(mpic->gregs, MPIC_INFO(GREG_FEATURE_0));
1191d9d1063dSJohannes Berg 	mpic->num_cpus = ((greg_feature & MPIC_GREG_FEATURE_LAST_CPU_MASK)
119214cf11afSPaul Mackerras 			  >> MPIC_GREG_FEATURE_LAST_CPU_SHIFT) + 1;
11935073e7eeSAnton Vorontsov 	if (isu_size == 0) {
1194475ca391SKumar Gala 		if (flags & MPIC_BROKEN_FRR_NIRQS)
1195475ca391SKumar Gala 			mpic->num_sources = mpic->irq_count;
1196475ca391SKumar Gala 		else
1197d9d1063dSJohannes Berg 			mpic->num_sources =
1198d9d1063dSJohannes Berg 				((greg_feature & MPIC_GREG_FEATURE_LAST_SRC_MASK)
119914cf11afSPaul Mackerras 				 >> MPIC_GREG_FEATURE_LAST_SRC_SHIFT) + 1;
12005073e7eeSAnton Vorontsov 	}
120114cf11afSPaul Mackerras 
120214cf11afSPaul Mackerras 	/* Map the per-CPU registers */
120314cf11afSPaul Mackerras 	for (i = 0; i < mpic->num_cpus; i++) {
1204a959ff56SBenjamin Herrenschmidt 		mpic_map(mpic, paddr, &mpic->cpuregs[i],
1205fbf0274eSBenjamin Herrenschmidt 			 MPIC_INFO(CPU_BASE) + i * MPIC_INFO(CPU_STRIDE),
1206fbf0274eSBenjamin Herrenschmidt 			 0x1000);
120714cf11afSPaul Mackerras 	}
120814cf11afSPaul Mackerras 
120914cf11afSPaul Mackerras 	/* Initialize main ISU if none provided */
121014cf11afSPaul Mackerras 	if (mpic->isu_size == 0) {
121114cf11afSPaul Mackerras 		mpic->isu_size = mpic->num_sources;
1212a959ff56SBenjamin Herrenschmidt 		mpic_map(mpic, paddr, &mpic->isus[0],
1213fbf0274eSBenjamin Herrenschmidt 			 MPIC_INFO(IRQ_BASE), MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
121414cf11afSPaul Mackerras 	}
121514cf11afSPaul Mackerras 	mpic->isu_shift = 1 + __ilog2(mpic->isu_size - 1);
121614cf11afSPaul Mackerras 	mpic->isu_mask = (1 << mpic->isu_shift) - 1;
121714cf11afSPaul Mackerras 
121814cf11afSPaul Mackerras 	/* Display version */
1219d9d1063dSJohannes Berg 	switch (greg_feature & MPIC_GREG_FEATURE_VERSION_MASK) {
122014cf11afSPaul Mackerras 	case 1:
122114cf11afSPaul Mackerras 		vers = "1.0";
122214cf11afSPaul Mackerras 		break;
122314cf11afSPaul Mackerras 	case 2:
122414cf11afSPaul Mackerras 		vers = "1.2";
122514cf11afSPaul Mackerras 		break;
122614cf11afSPaul Mackerras 	case 3:
122714cf11afSPaul Mackerras 		vers = "1.3";
122814cf11afSPaul Mackerras 		break;
122914cf11afSPaul Mackerras 	default:
123014cf11afSPaul Mackerras 		vers = "<unknown>";
123114cf11afSPaul Mackerras 		break;
123214cf11afSPaul Mackerras 	}
1233a959ff56SBenjamin Herrenschmidt 	printk(KERN_INFO "mpic: Setting up MPIC \"%s\" version %s at %llx,"
1234a959ff56SBenjamin Herrenschmidt 	       " max %d CPUs\n",
1235a959ff56SBenjamin Herrenschmidt 	       name, vers, (unsigned long long)paddr, mpic->num_cpus);
1236a959ff56SBenjamin Herrenschmidt 	printk(KERN_INFO "mpic: ISU size: %d, shift: %d, mask: %x\n",
1237a959ff56SBenjamin Herrenschmidt 	       mpic->isu_size, mpic->isu_shift, mpic->isu_mask);
123814cf11afSPaul Mackerras 
123914cf11afSPaul Mackerras 	mpic->next = mpics;
124014cf11afSPaul Mackerras 	mpics = mpic;
124114cf11afSPaul Mackerras 
12420ebfff14SBenjamin Herrenschmidt 	if (flags & MPIC_PRIMARY) {
124314cf11afSPaul Mackerras 		mpic_primary = mpic;
12440ebfff14SBenjamin Herrenschmidt 		irq_set_default_host(mpic->irqhost);
12450ebfff14SBenjamin Herrenschmidt 	}
124614cf11afSPaul Mackerras 
124714cf11afSPaul Mackerras 	return mpic;
124814cf11afSPaul Mackerras }
124914cf11afSPaul Mackerras 
125014cf11afSPaul Mackerras void __init mpic_assign_isu(struct mpic *mpic, unsigned int isu_num,
1251a959ff56SBenjamin Herrenschmidt 			    phys_addr_t paddr)
125214cf11afSPaul Mackerras {
125314cf11afSPaul Mackerras 	unsigned int isu_first = isu_num * mpic->isu_size;
125414cf11afSPaul Mackerras 
125514cf11afSPaul Mackerras 	BUG_ON(isu_num >= MPIC_MAX_ISU);
125614cf11afSPaul Mackerras 
1257a959ff56SBenjamin Herrenschmidt 	mpic_map(mpic, paddr, &mpic->isus[isu_num], 0,
12587233593bSZang Roy-r61911 		 MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
125914cf11afSPaul Mackerras 	if ((isu_first + mpic->isu_size) > mpic->num_sources)
126014cf11afSPaul Mackerras 		mpic->num_sources = isu_first + mpic->isu_size;
126114cf11afSPaul Mackerras }
126214cf11afSPaul Mackerras 
12630ebfff14SBenjamin Herrenschmidt void __init mpic_set_default_senses(struct mpic *mpic, u8 *senses, int count)
12640ebfff14SBenjamin Herrenschmidt {
12650ebfff14SBenjamin Herrenschmidt 	mpic->senses = senses;
12660ebfff14SBenjamin Herrenschmidt 	mpic->senses_count = count;
12670ebfff14SBenjamin Herrenschmidt }
12680ebfff14SBenjamin Herrenschmidt 
126914cf11afSPaul Mackerras void __init mpic_init(struct mpic *mpic)
127014cf11afSPaul Mackerras {
127114cf11afSPaul Mackerras 	int i;
1272cc353c30SArnd Bergmann 	int cpu;
127314cf11afSPaul Mackerras 
127414cf11afSPaul Mackerras 	BUG_ON(mpic->num_sources == 0);
127514cf11afSPaul Mackerras 
127614cf11afSPaul Mackerras 	printk(KERN_INFO "mpic: Initializing for %d sources\n", mpic->num_sources);
127714cf11afSPaul Mackerras 
127814cf11afSPaul Mackerras 	/* Set current processor priority to max */
12797233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
128014cf11afSPaul Mackerras 
128114cf11afSPaul Mackerras 	/* Initialize timers: just disable them all */
128214cf11afSPaul Mackerras 	for (i = 0; i < 4; i++) {
128314cf11afSPaul Mackerras 		mpic_write(mpic->tmregs,
12847233593bSZang Roy-r61911 			   i * MPIC_INFO(TIMER_STRIDE) +
12857233593bSZang Roy-r61911 			   MPIC_INFO(TIMER_DESTINATION), 0);
128614cf11afSPaul Mackerras 		mpic_write(mpic->tmregs,
12877233593bSZang Roy-r61911 			   i * MPIC_INFO(TIMER_STRIDE) +
12887233593bSZang Roy-r61911 			   MPIC_INFO(TIMER_VECTOR_PRI),
128914cf11afSPaul Mackerras 			   MPIC_VECPRI_MASK |
12907df2457dSOlof Johansson 			   (mpic->timer_vecs[0] + i));
129114cf11afSPaul Mackerras 	}
129214cf11afSPaul Mackerras 
129314cf11afSPaul Mackerras 	/* Initialize IPIs to our reserved vectors and mark them disabled for now */
129414cf11afSPaul Mackerras 	mpic_test_broken_ipi(mpic);
129514cf11afSPaul Mackerras 	for (i = 0; i < 4; i++) {
129614cf11afSPaul Mackerras 		mpic_ipi_write(i,
129714cf11afSPaul Mackerras 			       MPIC_VECPRI_MASK |
129814cf11afSPaul Mackerras 			       (10 << MPIC_VECPRI_PRIORITY_SHIFT) |
12997df2457dSOlof Johansson 			       (mpic->ipi_vecs[0] + i));
130014cf11afSPaul Mackerras 	}
130114cf11afSPaul Mackerras 
130214cf11afSPaul Mackerras 	/* Initialize interrupt sources */
130314cf11afSPaul Mackerras 	if (mpic->irq_count == 0)
130414cf11afSPaul Mackerras 		mpic->irq_count = mpic->num_sources;
130514cf11afSPaul Mackerras 
13061beb6a7dSBenjamin Herrenschmidt 	/* Do the HT PIC fixups on U3 broken mpic */
130714cf11afSPaul Mackerras 	DBG("MPIC flags: %x\n", mpic->flags);
130805af7bd2SMichael Ellerman 	if ((mpic->flags & MPIC_U3_HT_IRQS) && (mpic->flags & MPIC_PRIMARY)) {
13091beb6a7dSBenjamin Herrenschmidt 		mpic_scan_ht_pics(mpic);
131005af7bd2SMichael Ellerman 		mpic_u3msi_init(mpic);
131105af7bd2SMichael Ellerman 	}
131214cf11afSPaul Mackerras 
131338958dd9SOlof Johansson 	mpic_pasemi_msi_init(mpic);
131438958dd9SOlof Johansson 
1315cc353c30SArnd Bergmann 	if (mpic->flags & MPIC_PRIMARY)
1316cc353c30SArnd Bergmann 		cpu = hard_smp_processor_id();
1317cc353c30SArnd Bergmann 	else
1318cc353c30SArnd Bergmann 		cpu = 0;
1319cc353c30SArnd Bergmann 
132014cf11afSPaul Mackerras 	for (i = 0; i < mpic->num_sources; i++) {
132114cf11afSPaul Mackerras 		/* start with vector = source number, and masked */
13226e99e458SBenjamin Herrenschmidt 		u32 vecpri = MPIC_VECPRI_MASK | i |
13236e99e458SBenjamin Herrenschmidt 			(8 << MPIC_VECPRI_PRIORITY_SHIFT);
132414cf11afSPaul Mackerras 
13257fd72186SBenjamin Herrenschmidt 		/* check if protected */
13267fd72186SBenjamin Herrenschmidt 		if (mpic->protected && test_bit(i, mpic->protected))
13277fd72186SBenjamin Herrenschmidt 			continue;
132814cf11afSPaul Mackerras 		/* init hw */
13297233593bSZang Roy-r61911 		mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI), vecpri);
1330cc353c30SArnd Bergmann 		mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION), 1 << cpu);
133114cf11afSPaul Mackerras 	}
133214cf11afSPaul Mackerras 
13337df2457dSOlof Johansson 	/* Init spurious vector */
13347df2457dSOlof Johansson 	mpic_write(mpic->gregs, MPIC_INFO(GREG_SPURIOUS), mpic->spurious_vec);
133514cf11afSPaul Mackerras 
13367233593bSZang Roy-r61911 	/* Disable 8259 passthrough, if supported */
13377233593bSZang Roy-r61911 	if (!(mpic->flags & MPIC_NO_PTHROU_DIS))
13387233593bSZang Roy-r61911 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
13397233593bSZang Roy-r61911 			   mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
134014cf11afSPaul Mackerras 			   | MPIC_GREG_GCONF_8259_PTHROU_DIS);
134114cf11afSPaul Mackerras 
1342d87bf3beSOlof Johansson 	if (mpic->flags & MPIC_NO_BIAS)
1343d87bf3beSOlof Johansson 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1344d87bf3beSOlof Johansson 			mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1345d87bf3beSOlof Johansson 			| MPIC_GREG_GCONF_NO_BIAS);
1346d87bf3beSOlof Johansson 
134714cf11afSPaul Mackerras 	/* Set current processor priority to 0 */
13487233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
13493669e930SJohannes Berg 
13503669e930SJohannes Berg #ifdef CONFIG_PM
13513669e930SJohannes Berg 	/* allocate memory to save mpic state */
13523669e930SJohannes Berg 	mpic->save_data = alloc_bootmem(mpic->num_sources * sizeof(struct mpic_irq_save));
13533669e930SJohannes Berg 	BUG_ON(mpic->save_data == NULL);
13543669e930SJohannes Berg #endif
135514cf11afSPaul Mackerras }
135614cf11afSPaul Mackerras 
1357868ea0c9SMark A. Greer void __init mpic_set_clk_ratio(struct mpic *mpic, u32 clock_ratio)
1358868ea0c9SMark A. Greer {
1359868ea0c9SMark A. Greer 	u32 v;
136014cf11afSPaul Mackerras 
1361868ea0c9SMark A. Greer 	v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
1362868ea0c9SMark A. Greer 	v &= ~MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO_MASK;
1363868ea0c9SMark A. Greer 	v |= MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO(clock_ratio);
1364868ea0c9SMark A. Greer 	mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
1365868ea0c9SMark A. Greer }
1366868ea0c9SMark A. Greer 
1367868ea0c9SMark A. Greer void __init mpic_set_serial_int(struct mpic *mpic, int enable)
1368868ea0c9SMark A. Greer {
1369ba1826e5SBenjamin Herrenschmidt 	unsigned long flags;
1370868ea0c9SMark A. Greer 	u32 v;
1371868ea0c9SMark A. Greer 
1372ba1826e5SBenjamin Herrenschmidt 	spin_lock_irqsave(&mpic_lock, flags);
1373868ea0c9SMark A. Greer 	v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
1374868ea0c9SMark A. Greer 	if (enable)
1375868ea0c9SMark A. Greer 		v |= MPIC_GREG_GLOBAL_CONF_1_SIE;
1376868ea0c9SMark A. Greer 	else
1377868ea0c9SMark A. Greer 		v &= ~MPIC_GREG_GLOBAL_CONF_1_SIE;
1378868ea0c9SMark A. Greer 	mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
1379ba1826e5SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&mpic_lock, flags);
1380868ea0c9SMark A. Greer }
138114cf11afSPaul Mackerras 
138214cf11afSPaul Mackerras void mpic_irq_set_priority(unsigned int irq, unsigned int pri)
138314cf11afSPaul Mackerras {
1384d9d1063dSJohannes Berg 	unsigned int is_ipi;
138514cf11afSPaul Mackerras 	struct mpic *mpic = mpic_find(irq, &is_ipi);
13860ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
138714cf11afSPaul Mackerras 	unsigned long flags;
138814cf11afSPaul Mackerras 	u32 reg;
138914cf11afSPaul Mackerras 
139006a901c5SStephen Rothwell 	if (!mpic)
139106a901c5SStephen Rothwell 		return;
139206a901c5SStephen Rothwell 
139314cf11afSPaul Mackerras 	spin_lock_irqsave(&mpic_lock, flags);
139414cf11afSPaul Mackerras 	if (is_ipi) {
13957df2457dSOlof Johansson 		reg = mpic_ipi_read(src - mpic->ipi_vecs[0]) &
1396e5356640SBenjamin Herrenschmidt 			~MPIC_VECPRI_PRIORITY_MASK;
13977df2457dSOlof Johansson 		mpic_ipi_write(src - mpic->ipi_vecs[0],
139814cf11afSPaul Mackerras 			       reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
139914cf11afSPaul Mackerras 	} else {
14007233593bSZang Roy-r61911 		reg = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI))
1401e5356640SBenjamin Herrenschmidt 			& ~MPIC_VECPRI_PRIORITY_MASK;
14027233593bSZang Roy-r61911 		mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
140314cf11afSPaul Mackerras 			       reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
140414cf11afSPaul Mackerras 	}
140514cf11afSPaul Mackerras 	spin_unlock_irqrestore(&mpic_lock, flags);
140614cf11afSPaul Mackerras }
140714cf11afSPaul Mackerras 
140814cf11afSPaul Mackerras void mpic_setup_this_cpu(void)
140914cf11afSPaul Mackerras {
141014cf11afSPaul Mackerras #ifdef CONFIG_SMP
141114cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
141214cf11afSPaul Mackerras 	unsigned long flags;
141314cf11afSPaul Mackerras 	u32 msk = 1 << hard_smp_processor_id();
141414cf11afSPaul Mackerras 	unsigned int i;
141514cf11afSPaul Mackerras 
141614cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
141714cf11afSPaul Mackerras 
141814cf11afSPaul Mackerras 	DBG("%s: setup_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
141914cf11afSPaul Mackerras 
142014cf11afSPaul Mackerras 	spin_lock_irqsave(&mpic_lock, flags);
142114cf11afSPaul Mackerras 
142214cf11afSPaul Mackerras  	/* let the mpic know we want intrs. default affinity is 0xffffffff
142314cf11afSPaul Mackerras 	 * until changed via /proc. That's how it's done on x86. If we want
142414cf11afSPaul Mackerras 	 * it differently, then we should make sure we also change the default
1425a53da52fSIngo Molnar 	 * values of irq_desc[].affinity in irq.c.
142614cf11afSPaul Mackerras  	 */
142714cf11afSPaul Mackerras 	if (distribute_irqs) {
142814cf11afSPaul Mackerras 	 	for (i = 0; i < mpic->num_sources ; i++)
14297233593bSZang Roy-r61911 			mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
14307233593bSZang Roy-r61911 				mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) | msk);
143114cf11afSPaul Mackerras 	}
143214cf11afSPaul Mackerras 
143314cf11afSPaul Mackerras 	/* Set current processor priority to 0 */
14347233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
143514cf11afSPaul Mackerras 
143614cf11afSPaul Mackerras 	spin_unlock_irqrestore(&mpic_lock, flags);
143714cf11afSPaul Mackerras #endif /* CONFIG_SMP */
143814cf11afSPaul Mackerras }
143914cf11afSPaul Mackerras 
144014cf11afSPaul Mackerras int mpic_cpu_get_priority(void)
144114cf11afSPaul Mackerras {
144214cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
144314cf11afSPaul Mackerras 
14447233593bSZang Roy-r61911 	return mpic_cpu_read(MPIC_INFO(CPU_CURRENT_TASK_PRI));
144514cf11afSPaul Mackerras }
144614cf11afSPaul Mackerras 
144714cf11afSPaul Mackerras void mpic_cpu_set_priority(int prio)
144814cf11afSPaul Mackerras {
144914cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
145014cf11afSPaul Mackerras 
145114cf11afSPaul Mackerras 	prio &= MPIC_CPU_TASKPRI_MASK;
14527233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), prio);
145314cf11afSPaul Mackerras }
145414cf11afSPaul Mackerras 
145514cf11afSPaul Mackerras void mpic_teardown_this_cpu(int secondary)
145614cf11afSPaul Mackerras {
145714cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
145814cf11afSPaul Mackerras 	unsigned long flags;
145914cf11afSPaul Mackerras 	u32 msk = 1 << hard_smp_processor_id();
146014cf11afSPaul Mackerras 	unsigned int i;
146114cf11afSPaul Mackerras 
146214cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
146314cf11afSPaul Mackerras 
146414cf11afSPaul Mackerras 	DBG("%s: teardown_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
146514cf11afSPaul Mackerras 	spin_lock_irqsave(&mpic_lock, flags);
146614cf11afSPaul Mackerras 
146714cf11afSPaul Mackerras 	/* let the mpic know we don't want intrs.  */
146814cf11afSPaul Mackerras 	for (i = 0; i < mpic->num_sources ; i++)
14697233593bSZang Roy-r61911 		mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
14707233593bSZang Roy-r61911 			mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) & ~msk);
147114cf11afSPaul Mackerras 
147214cf11afSPaul Mackerras 	/* Set current processor priority to max */
14737233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
14747132799bSValentine Barshak 	/* We need to EOI the IPI since not all platforms reset the MPIC
14757132799bSValentine Barshak 	 * on boot and new interrupts wouldn't get delivered otherwise.
14767132799bSValentine Barshak 	 */
14777132799bSValentine Barshak 	mpic_eoi(mpic);
147814cf11afSPaul Mackerras 
147914cf11afSPaul Mackerras 	spin_unlock_irqrestore(&mpic_lock, flags);
148014cf11afSPaul Mackerras }
148114cf11afSPaul Mackerras 
148214cf11afSPaul Mackerras 
148314cf11afSPaul Mackerras void mpic_send_ipi(unsigned int ipi_no, unsigned int cpu_mask)
148414cf11afSPaul Mackerras {
148514cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
148614cf11afSPaul Mackerras 
148714cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
148814cf11afSPaul Mackerras 
14891beb6a7dSBenjamin Herrenschmidt #ifdef DEBUG_IPI
149014cf11afSPaul Mackerras 	DBG("%s: send_ipi(ipi_no: %d)\n", mpic->name, ipi_no);
14911beb6a7dSBenjamin Herrenschmidt #endif
149214cf11afSPaul Mackerras 
14937233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_IPI_DISPATCH_0) +
14947233593bSZang Roy-r61911 		       ipi_no * MPIC_INFO(CPU_IPI_DISPATCH_STRIDE),
149514cf11afSPaul Mackerras 		       mpic_physmask(cpu_mask & cpus_addr(cpu_online_map)[0]));
149614cf11afSPaul Mackerras }
149714cf11afSPaul Mackerras 
1498f365355eSOlof Johansson static unsigned int _mpic_get_one_irq(struct mpic *mpic, int reg)
149914cf11afSPaul Mackerras {
15000ebfff14SBenjamin Herrenschmidt 	u32 src;
150114cf11afSPaul Mackerras 
1502f365355eSOlof Johansson 	src = mpic_cpu_read(reg) & MPIC_INFO(VECPRI_VECTOR_MASK);
15031beb6a7dSBenjamin Herrenschmidt #ifdef DEBUG_LOW
1504f365355eSOlof Johansson 	DBG("%s: get_one_irq(reg 0x%x): %d\n", mpic->name, reg, src);
15051beb6a7dSBenjamin Herrenschmidt #endif
15065cddd2e3SJosh Boyer 	if (unlikely(src == mpic->spurious_vec)) {
15075cddd2e3SJosh Boyer 		if (mpic->flags & MPIC_SPV_EOI)
15085cddd2e3SJosh Boyer 			mpic_eoi(mpic);
15090ebfff14SBenjamin Herrenschmidt 		return NO_IRQ;
15105cddd2e3SJosh Boyer 	}
15117fd72186SBenjamin Herrenschmidt 	if (unlikely(mpic->protected && test_bit(src, mpic->protected))) {
15127fd72186SBenjamin Herrenschmidt 		if (printk_ratelimit())
15137fd72186SBenjamin Herrenschmidt 			printk(KERN_WARNING "%s: Got protected source %d !\n",
15147fd72186SBenjamin Herrenschmidt 			       mpic->name, (int)src);
15157fd72186SBenjamin Herrenschmidt 		mpic_eoi(mpic);
15167fd72186SBenjamin Herrenschmidt 		return NO_IRQ;
15177fd72186SBenjamin Herrenschmidt 	}
15187fd72186SBenjamin Herrenschmidt 
15190ebfff14SBenjamin Herrenschmidt 	return irq_linear_revmap(mpic->irqhost, src);
152014cf11afSPaul Mackerras }
152114cf11afSPaul Mackerras 
1522f365355eSOlof Johansson unsigned int mpic_get_one_irq(struct mpic *mpic)
1523f365355eSOlof Johansson {
1524f365355eSOlof Johansson 	return _mpic_get_one_irq(mpic, MPIC_INFO(CPU_INTACK));
1525f365355eSOlof Johansson }
1526f365355eSOlof Johansson 
152735a84c2fSOlaf Hering unsigned int mpic_get_irq(void)
152814cf11afSPaul Mackerras {
152914cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
153014cf11afSPaul Mackerras 
153114cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
153214cf11afSPaul Mackerras 
153335a84c2fSOlaf Hering 	return mpic_get_one_irq(mpic);
153414cf11afSPaul Mackerras }
153514cf11afSPaul Mackerras 
1536d91e4ea7SKumar Gala unsigned int mpic_get_coreint_irq(void)
1537d91e4ea7SKumar Gala {
1538d91e4ea7SKumar Gala #ifdef CONFIG_BOOKE
1539d91e4ea7SKumar Gala 	struct mpic *mpic = mpic_primary;
1540d91e4ea7SKumar Gala 	u32 src;
1541d91e4ea7SKumar Gala 
1542d91e4ea7SKumar Gala 	BUG_ON(mpic == NULL);
1543d91e4ea7SKumar Gala 
1544d91e4ea7SKumar Gala 	src = mfspr(SPRN_EPR);
1545d91e4ea7SKumar Gala 
1546d91e4ea7SKumar Gala 	if (unlikely(src == mpic->spurious_vec)) {
1547d91e4ea7SKumar Gala 		if (mpic->flags & MPIC_SPV_EOI)
1548d91e4ea7SKumar Gala 			mpic_eoi(mpic);
1549d91e4ea7SKumar Gala 		return NO_IRQ;
1550d91e4ea7SKumar Gala 	}
1551d91e4ea7SKumar Gala 	if (unlikely(mpic->protected && test_bit(src, mpic->protected))) {
1552d91e4ea7SKumar Gala 		if (printk_ratelimit())
1553d91e4ea7SKumar Gala 			printk(KERN_WARNING "%s: Got protected source %d !\n",
1554d91e4ea7SKumar Gala 			       mpic->name, (int)src);
1555d91e4ea7SKumar Gala 		return NO_IRQ;
1556d91e4ea7SKumar Gala 	}
1557d91e4ea7SKumar Gala 
1558d91e4ea7SKumar Gala 	return irq_linear_revmap(mpic->irqhost, src);
1559d91e4ea7SKumar Gala #else
1560d91e4ea7SKumar Gala 	return NO_IRQ;
1561d91e4ea7SKumar Gala #endif
1562d91e4ea7SKumar Gala }
1563d91e4ea7SKumar Gala 
1564f365355eSOlof Johansson unsigned int mpic_get_mcirq(void)
1565f365355eSOlof Johansson {
1566f365355eSOlof Johansson 	struct mpic *mpic = mpic_primary;
1567f365355eSOlof Johansson 
1568f365355eSOlof Johansson 	BUG_ON(mpic == NULL);
1569f365355eSOlof Johansson 
1570f365355eSOlof Johansson 	return _mpic_get_one_irq(mpic, MPIC_INFO(CPU_MCACK));
1571f365355eSOlof Johansson }
157214cf11afSPaul Mackerras 
157314cf11afSPaul Mackerras #ifdef CONFIG_SMP
157414cf11afSPaul Mackerras void mpic_request_ipis(void)
157514cf11afSPaul Mackerras {
157614cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
157778608dd3SMilton Miller 	int i;
157814cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
157914cf11afSPaul Mackerras 
15800ebfff14SBenjamin Herrenschmidt 	printk(KERN_INFO "mpic: requesting IPIs ... \n");
158114cf11afSPaul Mackerras 
15820ebfff14SBenjamin Herrenschmidt 	for (i = 0; i < 4; i++) {
15830ebfff14SBenjamin Herrenschmidt 		unsigned int vipi = irq_create_mapping(mpic->irqhost,
15847df2457dSOlof Johansson 						       mpic->ipi_vecs[0] + i);
15850ebfff14SBenjamin Herrenschmidt 		if (vipi == NO_IRQ) {
158678608dd3SMilton Miller 			printk(KERN_ERR "Failed to map %s\n", smp_ipi_name[i]);
158778608dd3SMilton Miller 			continue;
15880ebfff14SBenjamin Herrenschmidt 		}
158978608dd3SMilton Miller 		smp_request_message_ipi(vipi, i);
15900ebfff14SBenjamin Herrenschmidt 	}
159114cf11afSPaul Mackerras }
1592a9c59264SPaul Mackerras 
1593a9c59264SPaul Mackerras void smp_mpic_message_pass(int target, int msg)
1594a9c59264SPaul Mackerras {
1595a9c59264SPaul Mackerras 	/* make sure we're sending something that translates to an IPI */
1596a9c59264SPaul Mackerras 	if ((unsigned int)msg > 3) {
1597a9c59264SPaul Mackerras 		printk("SMP %d: smp_message_pass: unknown msg %d\n",
1598a9c59264SPaul Mackerras 		       smp_processor_id(), msg);
1599a9c59264SPaul Mackerras 		return;
1600a9c59264SPaul Mackerras 	}
1601a9c59264SPaul Mackerras 	switch (target) {
1602a9c59264SPaul Mackerras 	case MSG_ALL:
1603a9c59264SPaul Mackerras 		mpic_send_ipi(msg, 0xffffffff);
1604a9c59264SPaul Mackerras 		break;
1605a9c59264SPaul Mackerras 	case MSG_ALL_BUT_SELF:
1606a9c59264SPaul Mackerras 		mpic_send_ipi(msg, 0xffffffff & ~(1 << smp_processor_id()));
1607a9c59264SPaul Mackerras 		break;
1608a9c59264SPaul Mackerras 	default:
1609a9c59264SPaul Mackerras 		mpic_send_ipi(msg, 1 << target);
1610a9c59264SPaul Mackerras 		break;
1611a9c59264SPaul Mackerras 	}
1612a9c59264SPaul Mackerras }
1613775aeff4SMichael Ellerman 
1614775aeff4SMichael Ellerman int __init smp_mpic_probe(void)
1615775aeff4SMichael Ellerman {
1616775aeff4SMichael Ellerman 	int nr_cpus;
1617775aeff4SMichael Ellerman 
1618775aeff4SMichael Ellerman 	DBG("smp_mpic_probe()...\n");
1619775aeff4SMichael Ellerman 
1620775aeff4SMichael Ellerman 	nr_cpus = cpus_weight(cpu_possible_map);
1621775aeff4SMichael Ellerman 
1622775aeff4SMichael Ellerman 	DBG("nr_cpus: %d\n", nr_cpus);
1623775aeff4SMichael Ellerman 
1624775aeff4SMichael Ellerman 	if (nr_cpus > 1)
1625775aeff4SMichael Ellerman 		mpic_request_ipis();
1626775aeff4SMichael Ellerman 
1627775aeff4SMichael Ellerman 	return nr_cpus;
1628775aeff4SMichael Ellerman }
1629775aeff4SMichael Ellerman 
1630775aeff4SMichael Ellerman void __devinit smp_mpic_setup_cpu(int cpu)
1631775aeff4SMichael Ellerman {
1632775aeff4SMichael Ellerman 	mpic_setup_this_cpu();
1633775aeff4SMichael Ellerman }
163414cf11afSPaul Mackerras #endif /* CONFIG_SMP */
16353669e930SJohannes Berg 
16363669e930SJohannes Berg #ifdef CONFIG_PM
16373669e930SJohannes Berg static int mpic_suspend(struct sys_device *dev, pm_message_t state)
16383669e930SJohannes Berg {
16393669e930SJohannes Berg 	struct mpic *mpic = container_of(dev, struct mpic, sysdev);
16403669e930SJohannes Berg 	int i;
16413669e930SJohannes Berg 
16423669e930SJohannes Berg 	for (i = 0; i < mpic->num_sources; i++) {
16433669e930SJohannes Berg 		mpic->save_data[i].vecprio =
16443669e930SJohannes Berg 			mpic_irq_read(i, MPIC_INFO(IRQ_VECTOR_PRI));
16453669e930SJohannes Berg 		mpic->save_data[i].dest =
16463669e930SJohannes Berg 			mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION));
16473669e930SJohannes Berg 	}
16483669e930SJohannes Berg 
16493669e930SJohannes Berg 	return 0;
16503669e930SJohannes Berg }
16513669e930SJohannes Berg 
16523669e930SJohannes Berg static int mpic_resume(struct sys_device *dev)
16533669e930SJohannes Berg {
16543669e930SJohannes Berg 	struct mpic *mpic = container_of(dev, struct mpic, sysdev);
16553669e930SJohannes Berg 	int i;
16563669e930SJohannes Berg 
16573669e930SJohannes Berg 	for (i = 0; i < mpic->num_sources; i++) {
16583669e930SJohannes Berg 		mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI),
16593669e930SJohannes Berg 			       mpic->save_data[i].vecprio);
16603669e930SJohannes Berg 		mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
16613669e930SJohannes Berg 			       mpic->save_data[i].dest);
16623669e930SJohannes Berg 
16633669e930SJohannes Berg #ifdef CONFIG_MPIC_U3_HT_IRQS
16643669e930SJohannes Berg 	{
16653669e930SJohannes Berg 		struct mpic_irq_fixup *fixup = &mpic->fixups[i];
16663669e930SJohannes Berg 
16673669e930SJohannes Berg 		if (fixup->base) {
16683669e930SJohannes Berg 			/* we use the lowest bit in an inverted meaning */
16693669e930SJohannes Berg 			if ((mpic->save_data[i].fixup_data & 1) == 0)
16703669e930SJohannes Berg 				continue;
16713669e930SJohannes Berg 
16723669e930SJohannes Berg 			/* Enable and configure */
16733669e930SJohannes Berg 			writeb(0x10 + 2 * fixup->index, fixup->base + 2);
16743669e930SJohannes Berg 
16753669e930SJohannes Berg 			writel(mpic->save_data[i].fixup_data & ~1,
16763669e930SJohannes Berg 			       fixup->base + 4);
16773669e930SJohannes Berg 		}
16783669e930SJohannes Berg 	}
16793669e930SJohannes Berg #endif
16803669e930SJohannes Berg 	} /* end for loop */
16813669e930SJohannes Berg 
16823669e930SJohannes Berg 	return 0;
16833669e930SJohannes Berg }
16843669e930SJohannes Berg #endif
16853669e930SJohannes Berg 
16863669e930SJohannes Berg static struct sysdev_class mpic_sysclass = {
16873669e930SJohannes Berg #ifdef CONFIG_PM
16883669e930SJohannes Berg 	.resume = mpic_resume,
16893669e930SJohannes Berg 	.suspend = mpic_suspend,
16903669e930SJohannes Berg #endif
1691af5ca3f4SKay Sievers 	.name = "mpic",
16923669e930SJohannes Berg };
16933669e930SJohannes Berg 
16943669e930SJohannes Berg static int mpic_init_sys(void)
16953669e930SJohannes Berg {
16963669e930SJohannes Berg 	struct mpic *mpic = mpics;
16973669e930SJohannes Berg 	int error, id = 0;
16983669e930SJohannes Berg 
16993669e930SJohannes Berg 	error = sysdev_class_register(&mpic_sysclass);
17003669e930SJohannes Berg 
17013669e930SJohannes Berg 	while (mpic && !error) {
17023669e930SJohannes Berg 		mpic->sysdev.cls = &mpic_sysclass;
17033669e930SJohannes Berg 		mpic->sysdev.id = id++;
17043669e930SJohannes Berg 		error = sysdev_register(&mpic->sysdev);
17053669e930SJohannes Berg 		mpic = mpic->next;
17063669e930SJohannes Berg 	}
17073669e930SJohannes Berg 	return error;
17083669e930SJohannes Berg }
17093669e930SJohannes Berg 
17103669e930SJohannes Berg device_initcall(mpic_init_sys);
1711