xref: /linux/arch/powerpc/sysdev/mpic.c (revision 6cff46f4bc6cc4a8a4154b0b6a2e669db08e8fd2)
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;
23311a6b292SMichael Ellerman 	unsigned int	val;
23414cf11afSPaul Mackerras 
23511a6b292SMichael Ellerman 	val = _mpic_read(mpic->reg_type, &mpic->isus[isu],
23611a6b292SMichael Ellerman 			 reg + (idx * MPIC_INFO(IRQ_STRIDE)));
2370d72ba93SOlof Johansson #ifdef CONFIG_MPIC_BROKEN_REGREAD
2380d72ba93SOlof Johansson 	if (reg == 0)
23911a6b292SMichael Ellerman 		val = (val & (MPIC_VECPRI_MASK | MPIC_VECPRI_ACTIVITY)) |
24011a6b292SMichael Ellerman 			mpic->isu_reg0_shadow[src_no];
2410d72ba93SOlof Johansson #endif
24211a6b292SMichael Ellerman 	return val;
24314cf11afSPaul Mackerras }
24414cf11afSPaul Mackerras 
24514cf11afSPaul Mackerras static inline void _mpic_irq_write(struct mpic *mpic, unsigned int src_no,
24614cf11afSPaul Mackerras 				   unsigned int reg, u32 value)
24714cf11afSPaul Mackerras {
24814cf11afSPaul Mackerras 	unsigned int	isu = src_no >> mpic->isu_shift;
24914cf11afSPaul Mackerras 	unsigned int	idx = src_no & mpic->isu_mask;
25014cf11afSPaul Mackerras 
251fbf0274eSBenjamin Herrenschmidt 	_mpic_write(mpic->reg_type, &mpic->isus[isu],
2527233593bSZang Roy-r61911 		    reg + (idx * MPIC_INFO(IRQ_STRIDE)), value);
2530d72ba93SOlof Johansson 
2540d72ba93SOlof Johansson #ifdef CONFIG_MPIC_BROKEN_REGREAD
2550d72ba93SOlof Johansson 	if (reg == 0)
25611a6b292SMichael Ellerman 		mpic->isu_reg0_shadow[src_no] =
25711a6b292SMichael Ellerman 			value & ~(MPIC_VECPRI_MASK | MPIC_VECPRI_ACTIVITY);
2580d72ba93SOlof Johansson #endif
25914cf11afSPaul Mackerras }
26014cf11afSPaul Mackerras 
261fbf0274eSBenjamin Herrenschmidt #define mpic_read(b,r)		_mpic_read(mpic->reg_type,&(b),(r))
262fbf0274eSBenjamin Herrenschmidt #define mpic_write(b,r,v)	_mpic_write(mpic->reg_type,&(b),(r),(v))
26314cf11afSPaul Mackerras #define mpic_ipi_read(i)	_mpic_ipi_read(mpic,(i))
26414cf11afSPaul Mackerras #define mpic_ipi_write(i,v)	_mpic_ipi_write(mpic,(i),(v))
26514cf11afSPaul Mackerras #define mpic_cpu_read(i)	_mpic_cpu_read(mpic,(i))
26614cf11afSPaul Mackerras #define mpic_cpu_write(i,v)	_mpic_cpu_write(mpic,(i),(v))
26714cf11afSPaul Mackerras #define mpic_irq_read(s,r)	_mpic_irq_read(mpic,(s),(r))
26814cf11afSPaul Mackerras #define mpic_irq_write(s,r,v)	_mpic_irq_write(mpic,(s),(r),(v))
26914cf11afSPaul Mackerras 
27014cf11afSPaul Mackerras 
27114cf11afSPaul Mackerras /*
27214cf11afSPaul Mackerras  * Low level utility functions
27314cf11afSPaul Mackerras  */
27414cf11afSPaul Mackerras 
27514cf11afSPaul Mackerras 
276c51a3fdcSBecky Bruce static void _mpic_map_mmio(struct mpic *mpic, phys_addr_t phys_addr,
277fbf0274eSBenjamin Herrenschmidt 			   struct mpic_reg_bank *rb, unsigned int offset,
278fbf0274eSBenjamin Herrenschmidt 			   unsigned int size)
279fbf0274eSBenjamin Herrenschmidt {
280fbf0274eSBenjamin Herrenschmidt 	rb->base = ioremap(phys_addr + offset, size);
281fbf0274eSBenjamin Herrenschmidt 	BUG_ON(rb->base == NULL);
282fbf0274eSBenjamin Herrenschmidt }
283fbf0274eSBenjamin Herrenschmidt 
284fbf0274eSBenjamin Herrenschmidt #ifdef CONFIG_PPC_DCR
2855a2642f6SBenjamin Herrenschmidt static void _mpic_map_dcr(struct mpic *mpic, struct device_node *node,
2865a2642f6SBenjamin Herrenschmidt 			  struct mpic_reg_bank *rb,
287fbf0274eSBenjamin Herrenschmidt 			  unsigned int offset, unsigned int size)
288fbf0274eSBenjamin Herrenschmidt {
2890411a5e2SMichael Ellerman 	const u32 *dbasep;
2900411a5e2SMichael Ellerman 
2915a2642f6SBenjamin Herrenschmidt 	dbasep = of_get_property(node, "dcr-reg", NULL);
2920411a5e2SMichael Ellerman 
2935a2642f6SBenjamin Herrenschmidt 	rb->dhost = dcr_map(node, *dbasep + offset, size);
294fbf0274eSBenjamin Herrenschmidt 	BUG_ON(!DCR_MAP_OK(rb->dhost));
295fbf0274eSBenjamin Herrenschmidt }
296fbf0274eSBenjamin Herrenschmidt 
2975a2642f6SBenjamin Herrenschmidt static inline void mpic_map(struct mpic *mpic, struct device_node *node,
2985a2642f6SBenjamin Herrenschmidt 			    phys_addr_t phys_addr, struct mpic_reg_bank *rb,
2995a2642f6SBenjamin Herrenschmidt 			    unsigned int offset, unsigned int size)
300fbf0274eSBenjamin Herrenschmidt {
301fbf0274eSBenjamin Herrenschmidt 	if (mpic->flags & MPIC_USES_DCR)
3025a2642f6SBenjamin Herrenschmidt 		_mpic_map_dcr(mpic, node, rb, offset, size);
303fbf0274eSBenjamin Herrenschmidt 	else
304fbf0274eSBenjamin Herrenschmidt 		_mpic_map_mmio(mpic, phys_addr, rb, offset, size);
305fbf0274eSBenjamin Herrenschmidt }
306fbf0274eSBenjamin Herrenschmidt #else /* CONFIG_PPC_DCR */
3075a2642f6SBenjamin Herrenschmidt #define mpic_map(m,n,p,b,o,s)	_mpic_map_mmio(m,p,b,o,s)
308fbf0274eSBenjamin Herrenschmidt #endif /* !CONFIG_PPC_DCR */
309fbf0274eSBenjamin Herrenschmidt 
310fbf0274eSBenjamin Herrenschmidt 
31114cf11afSPaul Mackerras 
31214cf11afSPaul Mackerras /* Check if we have one of those nice broken MPICs with a flipped endian on
31314cf11afSPaul Mackerras  * reads from IPI registers
31414cf11afSPaul Mackerras  */
31514cf11afSPaul Mackerras static void __init mpic_test_broken_ipi(struct mpic *mpic)
31614cf11afSPaul Mackerras {
31714cf11afSPaul Mackerras 	u32 r;
31814cf11afSPaul Mackerras 
3197233593bSZang Roy-r61911 	mpic_write(mpic->gregs, MPIC_INFO(GREG_IPI_VECTOR_PRI_0), MPIC_VECPRI_MASK);
3207233593bSZang Roy-r61911 	r = mpic_read(mpic->gregs, MPIC_INFO(GREG_IPI_VECTOR_PRI_0));
32114cf11afSPaul Mackerras 
32214cf11afSPaul Mackerras 	if (r == le32_to_cpu(MPIC_VECPRI_MASK)) {
32314cf11afSPaul Mackerras 		printk(KERN_INFO "mpic: Detected reversed IPI registers\n");
32414cf11afSPaul Mackerras 		mpic->flags |= MPIC_BROKEN_IPI;
32514cf11afSPaul Mackerras 	}
32614cf11afSPaul Mackerras }
32714cf11afSPaul Mackerras 
3286cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
32914cf11afSPaul Mackerras 
33014cf11afSPaul Mackerras /* Test if an interrupt is sourced from HyperTransport (used on broken U3s)
33114cf11afSPaul Mackerras  * to force the edge setting on the MPIC and do the ack workaround.
33214cf11afSPaul Mackerras  */
3331beb6a7dSBenjamin Herrenschmidt static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
33414cf11afSPaul Mackerras {
3351beb6a7dSBenjamin Herrenschmidt 	if (source >= 128 || !mpic->fixups)
33614cf11afSPaul Mackerras 		return 0;
3371beb6a7dSBenjamin Herrenschmidt 	return mpic->fixups[source].base != NULL;
33814cf11afSPaul Mackerras }
33914cf11afSPaul Mackerras 
340c4b22f26SSegher Boessenkool 
3411beb6a7dSBenjamin Herrenschmidt static inline void mpic_ht_end_irq(struct mpic *mpic, unsigned int source)
34214cf11afSPaul Mackerras {
3431beb6a7dSBenjamin Herrenschmidt 	struct mpic_irq_fixup *fixup = &mpic->fixups[source];
34414cf11afSPaul Mackerras 
3451beb6a7dSBenjamin Herrenschmidt 	if (fixup->applebase) {
3461beb6a7dSBenjamin Herrenschmidt 		unsigned int soff = (fixup->index >> 3) & ~3;
3471beb6a7dSBenjamin Herrenschmidt 		unsigned int mask = 1U << (fixup->index & 0x1f);
3481beb6a7dSBenjamin Herrenschmidt 		writel(mask, fixup->applebase + soff);
3491beb6a7dSBenjamin Herrenschmidt 	} else {
35014cf11afSPaul Mackerras 		spin_lock(&mpic->fixup_lock);
3511beb6a7dSBenjamin Herrenschmidt 		writeb(0x11 + 2 * fixup->index, fixup->base + 2);
352c4b22f26SSegher Boessenkool 		writel(fixup->data, fixup->base + 4);
35314cf11afSPaul Mackerras 		spin_unlock(&mpic->fixup_lock);
35414cf11afSPaul Mackerras 	}
3551beb6a7dSBenjamin Herrenschmidt }
35614cf11afSPaul Mackerras 
3571beb6a7dSBenjamin Herrenschmidt static void mpic_startup_ht_interrupt(struct mpic *mpic, unsigned int source,
3581beb6a7dSBenjamin Herrenschmidt 				      unsigned int irqflags)
3591beb6a7dSBenjamin Herrenschmidt {
3601beb6a7dSBenjamin Herrenschmidt 	struct mpic_irq_fixup *fixup = &mpic->fixups[source];
3611beb6a7dSBenjamin Herrenschmidt 	unsigned long flags;
3621beb6a7dSBenjamin Herrenschmidt 	u32 tmp;
36314cf11afSPaul Mackerras 
3641beb6a7dSBenjamin Herrenschmidt 	if (fixup->base == NULL)
3651beb6a7dSBenjamin Herrenschmidt 		return;
3661beb6a7dSBenjamin Herrenschmidt 
36706fe98e6SBenjamin Herrenschmidt 	DBG("startup_ht_interrupt(0x%x, 0x%x) index: %d\n",
3681beb6a7dSBenjamin Herrenschmidt 	    source, irqflags, fixup->index);
3691beb6a7dSBenjamin Herrenschmidt 	spin_lock_irqsave(&mpic->fixup_lock, flags);
3701beb6a7dSBenjamin Herrenschmidt 	/* Enable and configure */
3711beb6a7dSBenjamin Herrenschmidt 	writeb(0x10 + 2 * fixup->index, fixup->base + 2);
3721beb6a7dSBenjamin Herrenschmidt 	tmp = readl(fixup->base + 4);
3731beb6a7dSBenjamin Herrenschmidt 	tmp &= ~(0x23U);
3741beb6a7dSBenjamin Herrenschmidt 	if (irqflags & IRQ_LEVEL)
3751beb6a7dSBenjamin Herrenschmidt 		tmp |= 0x22;
3761beb6a7dSBenjamin Herrenschmidt 	writel(tmp, fixup->base + 4);
3771beb6a7dSBenjamin Herrenschmidt 	spin_unlock_irqrestore(&mpic->fixup_lock, flags);
3783669e930SJohannes Berg 
3793669e930SJohannes Berg #ifdef CONFIG_PM
3803669e930SJohannes Berg 	/* use the lowest bit inverted to the actual HW,
3813669e930SJohannes Berg 	 * set if this fixup was enabled, clear otherwise */
3823669e930SJohannes Berg 	mpic->save_data[source].fixup_data = tmp | 1;
3833669e930SJohannes Berg #endif
3841beb6a7dSBenjamin Herrenschmidt }
3851beb6a7dSBenjamin Herrenschmidt 
3861beb6a7dSBenjamin Herrenschmidt static void mpic_shutdown_ht_interrupt(struct mpic *mpic, unsigned int source,
3871beb6a7dSBenjamin Herrenschmidt 				       unsigned int irqflags)
3881beb6a7dSBenjamin Herrenschmidt {
3891beb6a7dSBenjamin Herrenschmidt 	struct mpic_irq_fixup *fixup = &mpic->fixups[source];
3901beb6a7dSBenjamin Herrenschmidt 	unsigned long flags;
3911beb6a7dSBenjamin Herrenschmidt 	u32 tmp;
3921beb6a7dSBenjamin Herrenschmidt 
3931beb6a7dSBenjamin Herrenschmidt 	if (fixup->base == NULL)
3941beb6a7dSBenjamin Herrenschmidt 		return;
3951beb6a7dSBenjamin Herrenschmidt 
39606fe98e6SBenjamin Herrenschmidt 	DBG("shutdown_ht_interrupt(0x%x, 0x%x)\n", source, irqflags);
3971beb6a7dSBenjamin Herrenschmidt 
3981beb6a7dSBenjamin Herrenschmidt 	/* Disable */
3991beb6a7dSBenjamin Herrenschmidt 	spin_lock_irqsave(&mpic->fixup_lock, flags);
4001beb6a7dSBenjamin Herrenschmidt 	writeb(0x10 + 2 * fixup->index, fixup->base + 2);
4011beb6a7dSBenjamin Herrenschmidt 	tmp = readl(fixup->base + 4);
40272b13819SSegher Boessenkool 	tmp |= 1;
4031beb6a7dSBenjamin Herrenschmidt 	writel(tmp, fixup->base + 4);
4041beb6a7dSBenjamin Herrenschmidt 	spin_unlock_irqrestore(&mpic->fixup_lock, flags);
4053669e930SJohannes Berg 
4063669e930SJohannes Berg #ifdef CONFIG_PM
4073669e930SJohannes Berg 	/* use the lowest bit inverted to the actual HW,
4083669e930SJohannes Berg 	 * set if this fixup was enabled, clear otherwise */
4093669e930SJohannes Berg 	mpic->save_data[source].fixup_data = tmp & ~1;
4103669e930SJohannes Berg #endif
4111beb6a7dSBenjamin Herrenschmidt }
4121beb6a7dSBenjamin Herrenschmidt 
413812fd1fdSMichael Ellerman #ifdef CONFIG_PCI_MSI
414812fd1fdSMichael Ellerman static void __init mpic_scan_ht_msi(struct mpic *mpic, u8 __iomem *devbase,
415812fd1fdSMichael Ellerman 				    unsigned int devfn)
416812fd1fdSMichael Ellerman {
417812fd1fdSMichael Ellerman 	u8 __iomem *base;
418812fd1fdSMichael Ellerman 	u8 pos, flags;
419812fd1fdSMichael Ellerman 	u64 addr = 0;
420812fd1fdSMichael Ellerman 
421812fd1fdSMichael Ellerman 	for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
422812fd1fdSMichael Ellerman 	     pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
423812fd1fdSMichael Ellerman 		u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
424812fd1fdSMichael Ellerman 		if (id == PCI_CAP_ID_HT) {
425812fd1fdSMichael Ellerman 			id = readb(devbase + pos + 3);
426812fd1fdSMichael Ellerman 			if ((id & HT_5BIT_CAP_MASK) == HT_CAPTYPE_MSI_MAPPING)
427812fd1fdSMichael Ellerman 				break;
428812fd1fdSMichael Ellerman 		}
429812fd1fdSMichael Ellerman 	}
430812fd1fdSMichael Ellerman 
431812fd1fdSMichael Ellerman 	if (pos == 0)
432812fd1fdSMichael Ellerman 		return;
433812fd1fdSMichael Ellerman 
434812fd1fdSMichael Ellerman 	base = devbase + pos;
435812fd1fdSMichael Ellerman 
436812fd1fdSMichael Ellerman 	flags = readb(base + HT_MSI_FLAGS);
437812fd1fdSMichael Ellerman 	if (!(flags & HT_MSI_FLAGS_FIXED)) {
438812fd1fdSMichael Ellerman 		addr = readl(base + HT_MSI_ADDR_LO) & HT_MSI_ADDR_LO_MASK;
439812fd1fdSMichael Ellerman 		addr = addr | ((u64)readl(base + HT_MSI_ADDR_HI) << 32);
440812fd1fdSMichael Ellerman 	}
441812fd1fdSMichael Ellerman 
442fe333321SIngo Molnar 	printk(KERN_DEBUG "mpic:   - HT:%02x.%x %s MSI mapping found @ 0x%llx\n",
443812fd1fdSMichael Ellerman 		PCI_SLOT(devfn), PCI_FUNC(devfn),
444812fd1fdSMichael Ellerman 		flags & HT_MSI_FLAGS_ENABLE ? "enabled" : "disabled", addr);
445812fd1fdSMichael Ellerman 
446812fd1fdSMichael Ellerman 	if (!(flags & HT_MSI_FLAGS_ENABLE))
447812fd1fdSMichael Ellerman 		writeb(flags | HT_MSI_FLAGS_ENABLE, base + HT_MSI_FLAGS);
448812fd1fdSMichael Ellerman }
449812fd1fdSMichael Ellerman #else
450812fd1fdSMichael Ellerman static void __init mpic_scan_ht_msi(struct mpic *mpic, u8 __iomem *devbase,
451812fd1fdSMichael Ellerman 				    unsigned int devfn)
452812fd1fdSMichael Ellerman {
453812fd1fdSMichael Ellerman 	return;
454812fd1fdSMichael Ellerman }
455812fd1fdSMichael Ellerman #endif
456812fd1fdSMichael Ellerman 
4571beb6a7dSBenjamin Herrenschmidt static void __init mpic_scan_ht_pic(struct mpic *mpic, u8 __iomem *devbase,
4581beb6a7dSBenjamin Herrenschmidt 				    unsigned int devfn, u32 vdid)
45914cf11afSPaul Mackerras {
460c4b22f26SSegher Boessenkool 	int i, irq, n;
4611beb6a7dSBenjamin Herrenschmidt 	u8 __iomem *base;
46214cf11afSPaul Mackerras 	u32 tmp;
463c4b22f26SSegher Boessenkool 	u8 pos;
46414cf11afSPaul Mackerras 
4651beb6a7dSBenjamin Herrenschmidt 	for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
4661beb6a7dSBenjamin Herrenschmidt 	     pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
4671beb6a7dSBenjamin Herrenschmidt 		u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
46846ff3463SBrice Goglin 		if (id == PCI_CAP_ID_HT) {
469c4b22f26SSegher Boessenkool 			id = readb(devbase + pos + 3);
470beb7cc82SMichael Ellerman 			if ((id & HT_5BIT_CAP_MASK) == HT_CAPTYPE_IRQ)
471c4b22f26SSegher Boessenkool 				break;
472c4b22f26SSegher Boessenkool 		}
473c4b22f26SSegher Boessenkool 	}
474c4b22f26SSegher Boessenkool 	if (pos == 0)
475c4b22f26SSegher Boessenkool 		return;
476c4b22f26SSegher Boessenkool 
4771beb6a7dSBenjamin Herrenschmidt 	base = devbase + pos;
4781beb6a7dSBenjamin Herrenschmidt 	writeb(0x01, base + 2);
4791beb6a7dSBenjamin Herrenschmidt 	n = (readl(base + 4) >> 16) & 0xff;
480c4b22f26SSegher Boessenkool 
4811beb6a7dSBenjamin Herrenschmidt 	printk(KERN_INFO "mpic:   - HT:%02x.%x [0x%02x] vendor %04x device %04x"
4821beb6a7dSBenjamin Herrenschmidt 	       " has %d irqs\n",
4831beb6a7dSBenjamin Herrenschmidt 	       devfn >> 3, devfn & 0x7, pos, vdid & 0xffff, vdid >> 16, n + 1);
484c4b22f26SSegher Boessenkool 
485c4b22f26SSegher Boessenkool 	for (i = 0; i <= n; i++) {
4861beb6a7dSBenjamin Herrenschmidt 		writeb(0x10 + 2 * i, base + 2);
4871beb6a7dSBenjamin Herrenschmidt 		tmp = readl(base + 4);
48814cf11afSPaul Mackerras 		irq = (tmp >> 16) & 0xff;
4891beb6a7dSBenjamin Herrenschmidt 		DBG("HT PIC index 0x%x, irq 0x%x, tmp: %08x\n", i, irq, tmp);
4901beb6a7dSBenjamin Herrenschmidt 		/* mask it , will be unmasked later */
4911beb6a7dSBenjamin Herrenschmidt 		tmp |= 0x1;
4921beb6a7dSBenjamin Herrenschmidt 		writel(tmp, base + 4);
4931beb6a7dSBenjamin Herrenschmidt 		mpic->fixups[irq].index = i;
4941beb6a7dSBenjamin Herrenschmidt 		mpic->fixups[irq].base = base;
4951beb6a7dSBenjamin Herrenschmidt 		/* Apple HT PIC has a non-standard way of doing EOIs */
4961beb6a7dSBenjamin Herrenschmidt 		if ((vdid & 0xffff) == 0x106b)
4971beb6a7dSBenjamin Herrenschmidt 			mpic->fixups[irq].applebase = devbase + 0x60;
4981beb6a7dSBenjamin Herrenschmidt 		else
4991beb6a7dSBenjamin Herrenschmidt 			mpic->fixups[irq].applebase = NULL;
5001beb6a7dSBenjamin Herrenschmidt 		writeb(0x11 + 2 * i, base + 2);
5011beb6a7dSBenjamin Herrenschmidt 		mpic->fixups[irq].data = readl(base + 4) | 0x80000000;
50214cf11afSPaul Mackerras 	}
50314cf11afSPaul Mackerras }
50414cf11afSPaul Mackerras 
50514cf11afSPaul Mackerras 
5061beb6a7dSBenjamin Herrenschmidt static void __init mpic_scan_ht_pics(struct mpic *mpic)
50714cf11afSPaul Mackerras {
50814cf11afSPaul Mackerras 	unsigned int devfn;
50914cf11afSPaul Mackerras 	u8 __iomem *cfgspace;
51014cf11afSPaul Mackerras 
5111beb6a7dSBenjamin Herrenschmidt 	printk(KERN_INFO "mpic: Setting up HT PICs workarounds for U3/U4\n");
51214cf11afSPaul Mackerras 
51314cf11afSPaul Mackerras 	/* Allocate fixups array */
514ea96025aSAnton Vorontsov 	mpic->fixups = kzalloc(128 * sizeof(*mpic->fixups), GFP_KERNEL);
51514cf11afSPaul Mackerras 	BUG_ON(mpic->fixups == NULL);
51614cf11afSPaul Mackerras 
51714cf11afSPaul Mackerras 	/* Init spinlock */
51814cf11afSPaul Mackerras 	spin_lock_init(&mpic->fixup_lock);
51914cf11afSPaul Mackerras 
520c4b22f26SSegher Boessenkool 	/* Map U3 config space. We assume all IO-APICs are on the primary bus
521c4b22f26SSegher Boessenkool 	 * so we only need to map 64kB.
52214cf11afSPaul Mackerras 	 */
523c4b22f26SSegher Boessenkool 	cfgspace = ioremap(0xf2000000, 0x10000);
52414cf11afSPaul Mackerras 	BUG_ON(cfgspace == NULL);
52514cf11afSPaul Mackerras 
5261beb6a7dSBenjamin Herrenschmidt 	/* Now we scan all slots. We do a very quick scan, we read the header
5271beb6a7dSBenjamin Herrenschmidt 	 * type, vendor ID and device ID only, that's plenty enough
52814cf11afSPaul Mackerras 	 */
529c4b22f26SSegher Boessenkool 	for (devfn = 0; devfn < 0x100; devfn++) {
53014cf11afSPaul Mackerras 		u8 __iomem *devbase = cfgspace + (devfn << 8);
53114cf11afSPaul Mackerras 		u8 hdr_type = readb(devbase + PCI_HEADER_TYPE);
53214cf11afSPaul Mackerras 		u32 l = readl(devbase + PCI_VENDOR_ID);
5331beb6a7dSBenjamin Herrenschmidt 		u16 s;
53414cf11afSPaul Mackerras 
53514cf11afSPaul Mackerras 		DBG("devfn %x, l: %x\n", devfn, l);
53614cf11afSPaul Mackerras 
53714cf11afSPaul Mackerras 		/* If no device, skip */
53814cf11afSPaul Mackerras 		if (l == 0xffffffff || l == 0x00000000 ||
53914cf11afSPaul Mackerras 		    l == 0x0000ffff || l == 0xffff0000)
54014cf11afSPaul Mackerras 			goto next;
5411beb6a7dSBenjamin Herrenschmidt 		/* Check if is supports capability lists */
5421beb6a7dSBenjamin Herrenschmidt 		s = readw(devbase + PCI_STATUS);
5431beb6a7dSBenjamin Herrenschmidt 		if (!(s & PCI_STATUS_CAP_LIST))
5441beb6a7dSBenjamin Herrenschmidt 			goto next;
54514cf11afSPaul Mackerras 
5461beb6a7dSBenjamin Herrenschmidt 		mpic_scan_ht_pic(mpic, devbase, devfn, l);
547812fd1fdSMichael Ellerman 		mpic_scan_ht_msi(mpic, devbase, devfn);
54814cf11afSPaul Mackerras 
54914cf11afSPaul Mackerras 	next:
55014cf11afSPaul Mackerras 		/* next device, if function 0 */
551c4b22f26SSegher Boessenkool 		if (PCI_FUNC(devfn) == 0 && (hdr_type & 0x80) == 0)
55214cf11afSPaul Mackerras 			devfn += 7;
55314cf11afSPaul Mackerras 	}
55414cf11afSPaul Mackerras }
55514cf11afSPaul Mackerras 
5566cfef5b2SMichael Ellerman #else /* CONFIG_MPIC_U3_HT_IRQS */
5576e99e458SBenjamin Herrenschmidt 
5586e99e458SBenjamin Herrenschmidt static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
5596e99e458SBenjamin Herrenschmidt {
5606e99e458SBenjamin Herrenschmidt 	return 0;
5616e99e458SBenjamin Herrenschmidt }
5626e99e458SBenjamin Herrenschmidt 
5636e99e458SBenjamin Herrenschmidt static void __init mpic_scan_ht_pics(struct mpic *mpic)
5646e99e458SBenjamin Herrenschmidt {
5656e99e458SBenjamin Herrenschmidt }
5666e99e458SBenjamin Herrenschmidt 
5676cfef5b2SMichael Ellerman #endif /* CONFIG_MPIC_U3_HT_IRQS */
56814cf11afSPaul Mackerras 
5693c10c9c4SKumar Gala #ifdef CONFIG_SMP
5703c10c9c4SKumar Gala static int irq_choose_cpu(unsigned int virt_irq)
5713c10c9c4SKumar Gala {
572e65e49d0SMike Travis 	cpumask_t mask;
5733c10c9c4SKumar Gala 	int cpuid;
5743c10c9c4SKumar Gala 
575*6cff46f4SMichael Ellerman 	cpumask_copy(&mask, irq_to_desc(virt_irq)->affinity);
5763c10c9c4SKumar Gala 	if (cpus_equal(mask, CPU_MASK_ALL)) {
5773c10c9c4SKumar Gala 		static int irq_rover;
5783c10c9c4SKumar Gala 		static DEFINE_SPINLOCK(irq_rover_lock);
5793c10c9c4SKumar Gala 		unsigned long flags;
5803c10c9c4SKumar Gala 
5813c10c9c4SKumar Gala 		/* Round-robin distribution... */
5823c10c9c4SKumar Gala 	do_round_robin:
5833c10c9c4SKumar Gala 		spin_lock_irqsave(&irq_rover_lock, flags);
5843c10c9c4SKumar Gala 
5853c10c9c4SKumar Gala 		while (!cpu_online(irq_rover)) {
5863c10c9c4SKumar Gala 			if (++irq_rover >= NR_CPUS)
5873c10c9c4SKumar Gala 				irq_rover = 0;
5883c10c9c4SKumar Gala 		}
5893c10c9c4SKumar Gala 		cpuid = irq_rover;
5903c10c9c4SKumar Gala 		do {
5913c10c9c4SKumar Gala 			if (++irq_rover >= NR_CPUS)
5923c10c9c4SKumar Gala 				irq_rover = 0;
5933c10c9c4SKumar Gala 		} while (!cpu_online(irq_rover));
5943c10c9c4SKumar Gala 
5953c10c9c4SKumar Gala 		spin_unlock_irqrestore(&irq_rover_lock, flags);
5963c10c9c4SKumar Gala 	} else {
5973c10c9c4SKumar Gala 		cpumask_t tmp;
5983c10c9c4SKumar Gala 
5993c10c9c4SKumar Gala 		cpus_and(tmp, cpu_online_map, mask);
6003c10c9c4SKumar Gala 
6013c10c9c4SKumar Gala 		if (cpus_empty(tmp))
6023c10c9c4SKumar Gala 			goto do_round_robin;
6033c10c9c4SKumar Gala 
6043c10c9c4SKumar Gala 		cpuid = first_cpu(tmp);
6053c10c9c4SKumar Gala 	}
6063c10c9c4SKumar Gala 
6077a0d7940SKumar Gala 	return get_hard_smp_processor_id(cpuid);
6083c10c9c4SKumar Gala }
6093c10c9c4SKumar Gala #else
6103c10c9c4SKumar Gala static int irq_choose_cpu(unsigned int virt_irq)
6113c10c9c4SKumar Gala {
6123c10c9c4SKumar Gala 	return hard_smp_processor_id();
6133c10c9c4SKumar Gala }
6143c10c9c4SKumar Gala #endif
61514cf11afSPaul Mackerras 
6160ebfff14SBenjamin Herrenschmidt #define mpic_irq_to_hw(virq)	((unsigned int)irq_map[virq].hwirq)
6170ebfff14SBenjamin Herrenschmidt 
61814cf11afSPaul Mackerras /* Find an mpic associated with a given linux interrupt */
619d69a78d7STony Breeds static struct mpic *mpic_find(unsigned int irq)
62014cf11afSPaul Mackerras {
6210ebfff14SBenjamin Herrenschmidt 	if (irq < NUM_ISA_INTERRUPTS)
62214cf11afSPaul Mackerras 		return NULL;
6230ebfff14SBenjamin Herrenschmidt 
624*6cff46f4SMichael Ellerman 	return irq_to_desc(irq)->chip_data;
62514cf11afSPaul Mackerras }
62614cf11afSPaul Mackerras 
627d69a78d7STony Breeds /* Determine if the linux irq is an IPI */
628d69a78d7STony Breeds static unsigned int mpic_is_ipi(struct mpic *mpic, unsigned int irq)
629d69a78d7STony Breeds {
630d69a78d7STony Breeds 	unsigned int src = mpic_irq_to_hw(irq);
631d69a78d7STony Breeds 
632d69a78d7STony Breeds 	return (src >= mpic->ipi_vecs[0] && src <= mpic->ipi_vecs[3]);
633d69a78d7STony Breeds }
634d69a78d7STony Breeds 
635d69a78d7STony Breeds 
63614cf11afSPaul Mackerras /* Convert a cpu mask from logical to physical cpu numbers. */
63714cf11afSPaul Mackerras static inline u32 mpic_physmask(u32 cpumask)
63814cf11afSPaul Mackerras {
63914cf11afSPaul Mackerras 	int i;
64014cf11afSPaul Mackerras 	u32 mask = 0;
64114cf11afSPaul Mackerras 
64214cf11afSPaul Mackerras 	for (i = 0; i < NR_CPUS; ++i, cpumask >>= 1)
64314cf11afSPaul Mackerras 		mask |= (cpumask & 1) << get_hard_smp_processor_id(i);
64414cf11afSPaul Mackerras 	return mask;
64514cf11afSPaul Mackerras }
64614cf11afSPaul Mackerras 
64714cf11afSPaul Mackerras #ifdef CONFIG_SMP
64814cf11afSPaul Mackerras /* Get the mpic structure from the IPI number */
64914cf11afSPaul Mackerras static inline struct mpic * mpic_from_ipi(unsigned int ipi)
65014cf11afSPaul Mackerras {
651*6cff46f4SMichael Ellerman 	return irq_to_desc(ipi)->chip_data;
65214cf11afSPaul Mackerras }
65314cf11afSPaul Mackerras #endif
65414cf11afSPaul Mackerras 
65514cf11afSPaul Mackerras /* Get the mpic structure from the irq number */
65614cf11afSPaul Mackerras static inline struct mpic * mpic_from_irq(unsigned int irq)
65714cf11afSPaul Mackerras {
658*6cff46f4SMichael Ellerman 	return irq_to_desc(irq)->chip_data;
65914cf11afSPaul Mackerras }
66014cf11afSPaul Mackerras 
66114cf11afSPaul Mackerras /* Send an EOI */
66214cf11afSPaul Mackerras static inline void mpic_eoi(struct mpic *mpic)
66314cf11afSPaul Mackerras {
6647233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_EOI), 0);
6657233593bSZang Roy-r61911 	(void)mpic_cpu_read(MPIC_INFO(CPU_WHOAMI));
66614cf11afSPaul Mackerras }
66714cf11afSPaul Mackerras 
66814cf11afSPaul Mackerras /*
66914cf11afSPaul Mackerras  * Linux descriptor level callbacks
67014cf11afSPaul Mackerras  */
67114cf11afSPaul Mackerras 
67214cf11afSPaul Mackerras 
67305af7bd2SMichael Ellerman void mpic_unmask_irq(unsigned int irq)
67414cf11afSPaul Mackerras {
67514cf11afSPaul Mackerras 	unsigned int loops = 100000;
67614cf11afSPaul Mackerras 	struct mpic *mpic = mpic_from_irq(irq);
6770ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
67814cf11afSPaul Mackerras 
679bd561c79SPaul Mackerras 	DBG("%p: %s: enable_irq: %d (src %d)\n", mpic, mpic->name, irq, src);
68014cf11afSPaul Mackerras 
6817233593bSZang Roy-r61911 	mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
6827233593bSZang Roy-r61911 		       mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) &
683e5356640SBenjamin Herrenschmidt 		       ~MPIC_VECPRI_MASK);
68414cf11afSPaul Mackerras 	/* make sure mask gets to controller before we return to user */
68514cf11afSPaul Mackerras 	do {
68614cf11afSPaul Mackerras 		if (!loops--) {
68714cf11afSPaul Mackerras 			printk(KERN_ERR "mpic_enable_irq timeout\n");
68814cf11afSPaul Mackerras 			break;
68914cf11afSPaul Mackerras 		}
6907233593bSZang Roy-r61911 	} while(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK);
6911beb6a7dSBenjamin Herrenschmidt }
6921beb6a7dSBenjamin Herrenschmidt 
69305af7bd2SMichael Ellerman void mpic_mask_irq(unsigned int irq)
69414cf11afSPaul Mackerras {
69514cf11afSPaul Mackerras 	unsigned int loops = 100000;
69614cf11afSPaul Mackerras 	struct mpic *mpic = mpic_from_irq(irq);
6970ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
69814cf11afSPaul Mackerras 
69914cf11afSPaul Mackerras 	DBG("%s: disable_irq: %d (src %d)\n", mpic->name, irq, src);
70014cf11afSPaul Mackerras 
7017233593bSZang Roy-r61911 	mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
7027233593bSZang Roy-r61911 		       mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) |
703e5356640SBenjamin Herrenschmidt 		       MPIC_VECPRI_MASK);
70414cf11afSPaul Mackerras 
70514cf11afSPaul Mackerras 	/* make sure mask gets to controller before we return to user */
70614cf11afSPaul Mackerras 	do {
70714cf11afSPaul Mackerras 		if (!loops--) {
70814cf11afSPaul Mackerras 			printk(KERN_ERR "mpic_enable_irq timeout\n");
70914cf11afSPaul Mackerras 			break;
71014cf11afSPaul Mackerras 		}
7117233593bSZang Roy-r61911 	} while(!(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK));
71214cf11afSPaul Mackerras }
71314cf11afSPaul Mackerras 
71405af7bd2SMichael Ellerman void mpic_end_irq(unsigned int irq)
71514cf11afSPaul Mackerras {
71614cf11afSPaul Mackerras 	struct mpic *mpic = mpic_from_irq(irq);
71714cf11afSPaul Mackerras 
7181beb6a7dSBenjamin Herrenschmidt #ifdef DEBUG_IRQ
71914cf11afSPaul Mackerras 	DBG("%s: end_irq: %d\n", mpic->name, irq);
7201beb6a7dSBenjamin Herrenschmidt #endif
72114cf11afSPaul Mackerras 	/* We always EOI on end_irq() even for edge interrupts since that
72214cf11afSPaul Mackerras 	 * should only lower the priority, the MPIC should have properly
72314cf11afSPaul Mackerras 	 * latched another edge interrupt coming in anyway
72414cf11afSPaul Mackerras 	 */
72514cf11afSPaul Mackerras 
72614cf11afSPaul Mackerras 	mpic_eoi(mpic);
72714cf11afSPaul Mackerras }
72814cf11afSPaul Mackerras 
7296cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
730b9e5b4e6SBenjamin Herrenschmidt 
731b9e5b4e6SBenjamin Herrenschmidt static void mpic_unmask_ht_irq(unsigned int irq)
732b9e5b4e6SBenjamin Herrenschmidt {
733b9e5b4e6SBenjamin Herrenschmidt 	struct mpic *mpic = mpic_from_irq(irq);
7340ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
735b9e5b4e6SBenjamin Herrenschmidt 
736b9e5b4e6SBenjamin Herrenschmidt 	mpic_unmask_irq(irq);
737b9e5b4e6SBenjamin Herrenschmidt 
738*6cff46f4SMichael Ellerman 	if (irq_to_desc(irq)->status & IRQ_LEVEL)
739b9e5b4e6SBenjamin Herrenschmidt 		mpic_ht_end_irq(mpic, src);
740b9e5b4e6SBenjamin Herrenschmidt }
741b9e5b4e6SBenjamin Herrenschmidt 
742b9e5b4e6SBenjamin Herrenschmidt static unsigned int mpic_startup_ht_irq(unsigned int irq)
743b9e5b4e6SBenjamin Herrenschmidt {
744b9e5b4e6SBenjamin Herrenschmidt 	struct mpic *mpic = mpic_from_irq(irq);
7450ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
746b9e5b4e6SBenjamin Herrenschmidt 
747b9e5b4e6SBenjamin Herrenschmidt 	mpic_unmask_irq(irq);
748*6cff46f4SMichael Ellerman 	mpic_startup_ht_interrupt(mpic, src, irq_to_desc(irq)->status);
749b9e5b4e6SBenjamin Herrenschmidt 
750b9e5b4e6SBenjamin Herrenschmidt 	return 0;
751b9e5b4e6SBenjamin Herrenschmidt }
752b9e5b4e6SBenjamin Herrenschmidt 
753b9e5b4e6SBenjamin Herrenschmidt static void mpic_shutdown_ht_irq(unsigned int irq)
754b9e5b4e6SBenjamin Herrenschmidt {
755b9e5b4e6SBenjamin Herrenschmidt 	struct mpic *mpic = mpic_from_irq(irq);
7560ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
757b9e5b4e6SBenjamin Herrenschmidt 
758*6cff46f4SMichael Ellerman 	mpic_shutdown_ht_interrupt(mpic, src, irq_to_desc(irq)->status);
759b9e5b4e6SBenjamin Herrenschmidt 	mpic_mask_irq(irq);
760b9e5b4e6SBenjamin Herrenschmidt }
761b9e5b4e6SBenjamin Herrenschmidt 
762b9e5b4e6SBenjamin Herrenschmidt static void mpic_end_ht_irq(unsigned int irq)
763b9e5b4e6SBenjamin Herrenschmidt {
764b9e5b4e6SBenjamin Herrenschmidt 	struct mpic *mpic = mpic_from_irq(irq);
7650ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
766b9e5b4e6SBenjamin Herrenschmidt 
767b9e5b4e6SBenjamin Herrenschmidt #ifdef DEBUG_IRQ
768b9e5b4e6SBenjamin Herrenschmidt 	DBG("%s: end_irq: %d\n", mpic->name, irq);
769b9e5b4e6SBenjamin Herrenschmidt #endif
770b9e5b4e6SBenjamin Herrenschmidt 	/* We always EOI on end_irq() even for edge interrupts since that
771b9e5b4e6SBenjamin Herrenschmidt 	 * should only lower the priority, the MPIC should have properly
772b9e5b4e6SBenjamin Herrenschmidt 	 * latched another edge interrupt coming in anyway
773b9e5b4e6SBenjamin Herrenschmidt 	 */
774b9e5b4e6SBenjamin Herrenschmidt 
775*6cff46f4SMichael Ellerman 	if (irq_to_desc(irq)->status & IRQ_LEVEL)
776b9e5b4e6SBenjamin Herrenschmidt 		mpic_ht_end_irq(mpic, src);
777b9e5b4e6SBenjamin Herrenschmidt 	mpic_eoi(mpic);
778b9e5b4e6SBenjamin Herrenschmidt }
7796cfef5b2SMichael Ellerman #endif /* !CONFIG_MPIC_U3_HT_IRQS */
780b9e5b4e6SBenjamin Herrenschmidt 
78114cf11afSPaul Mackerras #ifdef CONFIG_SMP
78214cf11afSPaul Mackerras 
783b9e5b4e6SBenjamin Herrenschmidt static void mpic_unmask_ipi(unsigned int irq)
78414cf11afSPaul Mackerras {
78514cf11afSPaul Mackerras 	struct mpic *mpic = mpic_from_ipi(irq);
7867df2457dSOlof Johansson 	unsigned int src = mpic_irq_to_hw(irq) - mpic->ipi_vecs[0];
78714cf11afSPaul Mackerras 
78814cf11afSPaul Mackerras 	DBG("%s: enable_ipi: %d (ipi %d)\n", mpic->name, irq, src);
78914cf11afSPaul Mackerras 	mpic_ipi_write(src, mpic_ipi_read(src) & ~MPIC_VECPRI_MASK);
79014cf11afSPaul Mackerras }
79114cf11afSPaul Mackerras 
792b9e5b4e6SBenjamin Herrenschmidt static void mpic_mask_ipi(unsigned int irq)
79314cf11afSPaul Mackerras {
79414cf11afSPaul Mackerras 	/* NEVER disable an IPI... that's just plain wrong! */
79514cf11afSPaul Mackerras }
79614cf11afSPaul Mackerras 
79714cf11afSPaul Mackerras static void mpic_end_ipi(unsigned int irq)
79814cf11afSPaul Mackerras {
79914cf11afSPaul Mackerras 	struct mpic *mpic = mpic_from_ipi(irq);
80014cf11afSPaul Mackerras 
80114cf11afSPaul Mackerras 	/*
80214cf11afSPaul Mackerras 	 * IPIs are marked IRQ_PER_CPU. This has the side effect of
80314cf11afSPaul Mackerras 	 * preventing the IRQ_PENDING/IRQ_INPROGRESS logic from
80414cf11afSPaul Mackerras 	 * applying to them. We EOI them late to avoid re-entering.
8056714465eSThomas Gleixner 	 * We mark IPI's with IRQF_DISABLED as they must run with
80614cf11afSPaul Mackerras 	 * irqs disabled.
80714cf11afSPaul Mackerras 	 */
80814cf11afSPaul Mackerras 	mpic_eoi(mpic);
80914cf11afSPaul Mackerras }
81014cf11afSPaul Mackerras 
81114cf11afSPaul Mackerras #endif /* CONFIG_SMP */
81214cf11afSPaul Mackerras 
813d5dedd45SYinghai Lu int mpic_set_affinity(unsigned int irq, const struct cpumask *cpumask)
81414cf11afSPaul Mackerras {
81514cf11afSPaul Mackerras 	struct mpic *mpic = mpic_from_irq(irq);
8160ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
81714cf11afSPaul Mackerras 
8183c10c9c4SKumar Gala 	if (mpic->flags & MPIC_SINGLE_DEST_CPU) {
8193c10c9c4SKumar Gala 		int cpuid = irq_choose_cpu(irq);
8203c10c9c4SKumar Gala 
8213c10c9c4SKumar Gala 		mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION), 1 << cpuid);
8223c10c9c4SKumar Gala 	} else {
82314cf11afSPaul Mackerras 		cpumask_t tmp;
82414cf11afSPaul Mackerras 
8250de26520SRusty Russell 		cpumask_and(&tmp, cpumask, cpu_online_mask);
82614cf11afSPaul Mackerras 
8277233593bSZang Roy-r61911 		mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION),
82814cf11afSPaul Mackerras 			       mpic_physmask(cpus_addr(tmp)[0]));
82914cf11afSPaul Mackerras 	}
830d5dedd45SYinghai Lu 
831d5dedd45SYinghai Lu 	return 0;
8323c10c9c4SKumar Gala }
83314cf11afSPaul Mackerras 
8347233593bSZang Roy-r61911 static unsigned int mpic_type_to_vecpri(struct mpic *mpic, unsigned int type)
8350ebfff14SBenjamin Herrenschmidt {
8360ebfff14SBenjamin Herrenschmidt 	/* Now convert sense value */
8376e99e458SBenjamin Herrenschmidt 	switch(type & IRQ_TYPE_SENSE_MASK) {
8380ebfff14SBenjamin Herrenschmidt 	case IRQ_TYPE_EDGE_RISING:
8397233593bSZang Roy-r61911 		return MPIC_INFO(VECPRI_SENSE_EDGE) |
8407233593bSZang Roy-r61911 		       MPIC_INFO(VECPRI_POLARITY_POSITIVE);
8410ebfff14SBenjamin Herrenschmidt 	case IRQ_TYPE_EDGE_FALLING:
8426e99e458SBenjamin Herrenschmidt 	case IRQ_TYPE_EDGE_BOTH:
8437233593bSZang Roy-r61911 		return MPIC_INFO(VECPRI_SENSE_EDGE) |
8447233593bSZang Roy-r61911 		       MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
8450ebfff14SBenjamin Herrenschmidt 	case IRQ_TYPE_LEVEL_HIGH:
8467233593bSZang Roy-r61911 		return MPIC_INFO(VECPRI_SENSE_LEVEL) |
8477233593bSZang Roy-r61911 		       MPIC_INFO(VECPRI_POLARITY_POSITIVE);
8480ebfff14SBenjamin Herrenschmidt 	case IRQ_TYPE_LEVEL_LOW:
8490ebfff14SBenjamin Herrenschmidt 	default:
8507233593bSZang Roy-r61911 		return MPIC_INFO(VECPRI_SENSE_LEVEL) |
8517233593bSZang Roy-r61911 		       MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
8520ebfff14SBenjamin Herrenschmidt 	}
8536e99e458SBenjamin Herrenschmidt }
8546e99e458SBenjamin Herrenschmidt 
85505af7bd2SMichael Ellerman int mpic_set_irq_type(unsigned int virq, unsigned int flow_type)
8566e99e458SBenjamin Herrenschmidt {
8576e99e458SBenjamin Herrenschmidt 	struct mpic *mpic = mpic_from_irq(virq);
8586e99e458SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(virq);
859*6cff46f4SMichael Ellerman 	struct irq_desc *desc = irq_to_desc(virq);
8606e99e458SBenjamin Herrenschmidt 	unsigned int vecpri, vold, vnew;
8616e99e458SBenjamin Herrenschmidt 
86206fe98e6SBenjamin Herrenschmidt 	DBG("mpic: set_irq_type(mpic:@%p,virq:%d,src:0x%x,type:0x%x)\n",
8636e99e458SBenjamin Herrenschmidt 	    mpic, virq, src, flow_type);
8646e99e458SBenjamin Herrenschmidt 
8656e99e458SBenjamin Herrenschmidt 	if (src >= mpic->irq_count)
8666e99e458SBenjamin Herrenschmidt 		return -EINVAL;
8676e99e458SBenjamin Herrenschmidt 
8686e99e458SBenjamin Herrenschmidt 	if (flow_type == IRQ_TYPE_NONE)
8696e99e458SBenjamin Herrenschmidt 		if (mpic->senses && src < mpic->senses_count)
8706e99e458SBenjamin Herrenschmidt 			flow_type = mpic->senses[src];
8716e99e458SBenjamin Herrenschmidt 	if (flow_type == IRQ_TYPE_NONE)
8726e99e458SBenjamin Herrenschmidt 		flow_type = IRQ_TYPE_LEVEL_LOW;
8736e99e458SBenjamin Herrenschmidt 
8746e99e458SBenjamin Herrenschmidt 	desc->status &= ~(IRQ_TYPE_SENSE_MASK | IRQ_LEVEL);
8756e99e458SBenjamin Herrenschmidt 	desc->status |= flow_type & IRQ_TYPE_SENSE_MASK;
8766e99e458SBenjamin Herrenschmidt 	if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
8776e99e458SBenjamin Herrenschmidt 		desc->status |= IRQ_LEVEL;
8786e99e458SBenjamin Herrenschmidt 
8796e99e458SBenjamin Herrenschmidt 	if (mpic_is_ht_interrupt(mpic, src))
8806e99e458SBenjamin Herrenschmidt 		vecpri = MPIC_VECPRI_POLARITY_POSITIVE |
8816e99e458SBenjamin Herrenschmidt 			MPIC_VECPRI_SENSE_EDGE;
8826e99e458SBenjamin Herrenschmidt 	else
8837233593bSZang Roy-r61911 		vecpri = mpic_type_to_vecpri(mpic, flow_type);
8846e99e458SBenjamin Herrenschmidt 
8857233593bSZang Roy-r61911 	vold = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
8867233593bSZang Roy-r61911 	vnew = vold & ~(MPIC_INFO(VECPRI_POLARITY_MASK) |
8877233593bSZang Roy-r61911 			MPIC_INFO(VECPRI_SENSE_MASK));
8886e99e458SBenjamin Herrenschmidt 	vnew |= vecpri;
8896e99e458SBenjamin Herrenschmidt 	if (vold != vnew)
8907233593bSZang Roy-r61911 		mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI), vnew);
8916e99e458SBenjamin Herrenschmidt 
8926e99e458SBenjamin Herrenschmidt 	return 0;
8930ebfff14SBenjamin Herrenschmidt }
8940ebfff14SBenjamin Herrenschmidt 
89538958dd9SOlof Johansson void mpic_set_vector(unsigned int virq, unsigned int vector)
89638958dd9SOlof Johansson {
89738958dd9SOlof Johansson 	struct mpic *mpic = mpic_from_irq(virq);
89838958dd9SOlof Johansson 	unsigned int src = mpic_irq_to_hw(virq);
89938958dd9SOlof Johansson 	unsigned int vecpri;
90038958dd9SOlof Johansson 
90138958dd9SOlof Johansson 	DBG("mpic: set_vector(mpic:@%p,virq:%d,src:%d,vector:0x%x)\n",
90238958dd9SOlof Johansson 	    mpic, virq, src, vector);
90338958dd9SOlof Johansson 
90438958dd9SOlof Johansson 	if (src >= mpic->irq_count)
90538958dd9SOlof Johansson 		return;
90638958dd9SOlof Johansson 
90738958dd9SOlof Johansson 	vecpri = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
90838958dd9SOlof Johansson 	vecpri = vecpri & ~MPIC_INFO(VECPRI_VECTOR_MASK);
90938958dd9SOlof Johansson 	vecpri |= vector;
91038958dd9SOlof Johansson 	mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI), vecpri);
91138958dd9SOlof Johansson }
91238958dd9SOlof Johansson 
913b9e5b4e6SBenjamin Herrenschmidt static struct irq_chip mpic_irq_chip = {
914b9e5b4e6SBenjamin Herrenschmidt 	.mask		= mpic_mask_irq,
915b9e5b4e6SBenjamin Herrenschmidt 	.unmask		= mpic_unmask_irq,
916b9e5b4e6SBenjamin Herrenschmidt 	.eoi		= mpic_end_irq,
9176e99e458SBenjamin Herrenschmidt 	.set_type	= mpic_set_irq_type,
918b9e5b4e6SBenjamin Herrenschmidt };
919b9e5b4e6SBenjamin Herrenschmidt 
920b9e5b4e6SBenjamin Herrenschmidt #ifdef CONFIG_SMP
921b9e5b4e6SBenjamin Herrenschmidt static struct irq_chip mpic_ipi_chip = {
922b9e5b4e6SBenjamin Herrenschmidt 	.mask		= mpic_mask_ipi,
923b9e5b4e6SBenjamin Herrenschmidt 	.unmask		= mpic_unmask_ipi,
924b9e5b4e6SBenjamin Herrenschmidt 	.eoi		= mpic_end_ipi,
925b9e5b4e6SBenjamin Herrenschmidt };
926b9e5b4e6SBenjamin Herrenschmidt #endif /* CONFIG_SMP */
927b9e5b4e6SBenjamin Herrenschmidt 
9286cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
929b9e5b4e6SBenjamin Herrenschmidt static struct irq_chip mpic_irq_ht_chip = {
930b9e5b4e6SBenjamin Herrenschmidt 	.startup	= mpic_startup_ht_irq,
931b9e5b4e6SBenjamin Herrenschmidt 	.shutdown	= mpic_shutdown_ht_irq,
932b9e5b4e6SBenjamin Herrenschmidt 	.mask		= mpic_mask_irq,
933b9e5b4e6SBenjamin Herrenschmidt 	.unmask		= mpic_unmask_ht_irq,
934b9e5b4e6SBenjamin Herrenschmidt 	.eoi		= mpic_end_ht_irq,
9356e99e458SBenjamin Herrenschmidt 	.set_type	= mpic_set_irq_type,
936b9e5b4e6SBenjamin Herrenschmidt };
9376cfef5b2SMichael Ellerman #endif /* CONFIG_MPIC_U3_HT_IRQS */
938b9e5b4e6SBenjamin Herrenschmidt 
93914cf11afSPaul Mackerras 
9400ebfff14SBenjamin Herrenschmidt static int mpic_host_match(struct irq_host *h, struct device_node *node)
9410ebfff14SBenjamin Herrenschmidt {
9420ebfff14SBenjamin Herrenschmidt 	/* Exact match, unless mpic node is NULL */
94352964f87SMichael Ellerman 	return h->of_node == NULL || h->of_node == node;
9440ebfff14SBenjamin Herrenschmidt }
9450ebfff14SBenjamin Herrenschmidt 
9460ebfff14SBenjamin Herrenschmidt static int mpic_host_map(struct irq_host *h, unsigned int virq,
9476e99e458SBenjamin Herrenschmidt 			 irq_hw_number_t hw)
9480ebfff14SBenjamin Herrenschmidt {
9490ebfff14SBenjamin Herrenschmidt 	struct mpic *mpic = h->host_data;
9506e99e458SBenjamin Herrenschmidt 	struct irq_chip *chip;
9510ebfff14SBenjamin Herrenschmidt 
95206fe98e6SBenjamin Herrenschmidt 	DBG("mpic: map virq %d, hwirq 0x%lx\n", virq, hw);
9530ebfff14SBenjamin Herrenschmidt 
9547df2457dSOlof Johansson 	if (hw == mpic->spurious_vec)
9550ebfff14SBenjamin Herrenschmidt 		return -EINVAL;
9567fd72186SBenjamin Herrenschmidt 	if (mpic->protected && test_bit(hw, mpic->protected))
9577fd72186SBenjamin Herrenschmidt 		return -EINVAL;
95806fe98e6SBenjamin Herrenschmidt 
9590ebfff14SBenjamin Herrenschmidt #ifdef CONFIG_SMP
9607df2457dSOlof Johansson 	else if (hw >= mpic->ipi_vecs[0]) {
9610ebfff14SBenjamin Herrenschmidt 		WARN_ON(!(mpic->flags & MPIC_PRIMARY));
9620ebfff14SBenjamin Herrenschmidt 
96306fe98e6SBenjamin Herrenschmidt 		DBG("mpic: mapping as IPI\n");
9640ebfff14SBenjamin Herrenschmidt 		set_irq_chip_data(virq, mpic);
9650ebfff14SBenjamin Herrenschmidt 		set_irq_chip_and_handler(virq, &mpic->hc_ipi,
9660ebfff14SBenjamin Herrenschmidt 					 handle_percpu_irq);
9670ebfff14SBenjamin Herrenschmidt 		return 0;
9680ebfff14SBenjamin Herrenschmidt 	}
9690ebfff14SBenjamin Herrenschmidt #endif /* CONFIG_SMP */
9700ebfff14SBenjamin Herrenschmidt 
9710ebfff14SBenjamin Herrenschmidt 	if (hw >= mpic->irq_count)
9720ebfff14SBenjamin Herrenschmidt 		return -EINVAL;
9730ebfff14SBenjamin Herrenschmidt 
974a7de7c74SMichael Ellerman 	mpic_msi_reserve_hwirq(mpic, hw);
975a7de7c74SMichael Ellerman 
9766e99e458SBenjamin Herrenschmidt 	/* Default chip */
9770ebfff14SBenjamin Herrenschmidt 	chip = &mpic->hc_irq;
9780ebfff14SBenjamin Herrenschmidt 
9796cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
9800ebfff14SBenjamin Herrenschmidt 	/* Check for HT interrupts, override vecpri */
9816e99e458SBenjamin Herrenschmidt 	if (mpic_is_ht_interrupt(mpic, hw))
9820ebfff14SBenjamin Herrenschmidt 		chip = &mpic->hc_ht_irq;
9836cfef5b2SMichael Ellerman #endif /* CONFIG_MPIC_U3_HT_IRQS */
9840ebfff14SBenjamin Herrenschmidt 
98506fe98e6SBenjamin Herrenschmidt 	DBG("mpic: mapping to irq chip @%p\n", chip);
9860ebfff14SBenjamin Herrenschmidt 
9870ebfff14SBenjamin Herrenschmidt 	set_irq_chip_data(virq, mpic);
9880ebfff14SBenjamin Herrenschmidt 	set_irq_chip_and_handler(virq, chip, handle_fasteoi_irq);
9896e99e458SBenjamin Herrenschmidt 
9906e99e458SBenjamin Herrenschmidt 	/* Set default irq type */
9916e99e458SBenjamin Herrenschmidt 	set_irq_type(virq, IRQ_TYPE_NONE);
9926e99e458SBenjamin Herrenschmidt 
9930ebfff14SBenjamin Herrenschmidt 	return 0;
9940ebfff14SBenjamin Herrenschmidt }
9950ebfff14SBenjamin Herrenschmidt 
9960ebfff14SBenjamin Herrenschmidt static int mpic_host_xlate(struct irq_host *h, struct device_node *ct,
9970ebfff14SBenjamin Herrenschmidt 			   u32 *intspec, unsigned int intsize,
9980ebfff14SBenjamin Herrenschmidt 			   irq_hw_number_t *out_hwirq, unsigned int *out_flags)
9990ebfff14SBenjamin Herrenschmidt 
10000ebfff14SBenjamin Herrenschmidt {
10010ebfff14SBenjamin Herrenschmidt 	static unsigned char map_mpic_senses[4] = {
10020ebfff14SBenjamin Herrenschmidt 		IRQ_TYPE_EDGE_RISING,
10030ebfff14SBenjamin Herrenschmidt 		IRQ_TYPE_LEVEL_LOW,
10040ebfff14SBenjamin Herrenschmidt 		IRQ_TYPE_LEVEL_HIGH,
10050ebfff14SBenjamin Herrenschmidt 		IRQ_TYPE_EDGE_FALLING,
10060ebfff14SBenjamin Herrenschmidt 	};
10070ebfff14SBenjamin Herrenschmidt 
10080ebfff14SBenjamin Herrenschmidt 	*out_hwirq = intspec[0];
100906fe98e6SBenjamin Herrenschmidt 	if (intsize > 1) {
101006fe98e6SBenjamin Herrenschmidt 		u32 mask = 0x3;
101106fe98e6SBenjamin Herrenschmidt 
101206fe98e6SBenjamin Herrenschmidt 		/* Apple invented a new race of encoding on machines with
101306fe98e6SBenjamin Herrenschmidt 		 * an HT APIC. They encode, among others, the index within
101406fe98e6SBenjamin Herrenschmidt 		 * the HT APIC. We don't care about it here since thankfully,
101506fe98e6SBenjamin Herrenschmidt 		 * it appears that they have the APIC already properly
101606fe98e6SBenjamin Herrenschmidt 		 * configured, and thus our current fixup code that reads the
101706fe98e6SBenjamin Herrenschmidt 		 * APIC config works fine. However, we still need to mask out
101806fe98e6SBenjamin Herrenschmidt 		 * bits in the specifier to make sure we only get bit 0 which
101906fe98e6SBenjamin Herrenschmidt 		 * is the level/edge bit (the only sense bit exposed by Apple),
102006fe98e6SBenjamin Herrenschmidt 		 * as their bit 1 means something else.
102106fe98e6SBenjamin Herrenschmidt 		 */
102206fe98e6SBenjamin Herrenschmidt 		if (machine_is(powermac))
102306fe98e6SBenjamin Herrenschmidt 			mask = 0x1;
102406fe98e6SBenjamin Herrenschmidt 		*out_flags = map_mpic_senses[intspec[1] & mask];
102506fe98e6SBenjamin Herrenschmidt 	} else
10260ebfff14SBenjamin Herrenschmidt 		*out_flags = IRQ_TYPE_NONE;
10270ebfff14SBenjamin Herrenschmidt 
102806fe98e6SBenjamin Herrenschmidt 	DBG("mpic: xlate (%d cells: 0x%08x 0x%08x) to line 0x%lx sense 0x%x\n",
102906fe98e6SBenjamin Herrenschmidt 	    intsize, intspec[0], intspec[1], *out_hwirq, *out_flags);
103006fe98e6SBenjamin Herrenschmidt 
10310ebfff14SBenjamin Herrenschmidt 	return 0;
10320ebfff14SBenjamin Herrenschmidt }
10330ebfff14SBenjamin Herrenschmidt 
10340ebfff14SBenjamin Herrenschmidt static struct irq_host_ops mpic_host_ops = {
10350ebfff14SBenjamin Herrenschmidt 	.match = mpic_host_match,
10360ebfff14SBenjamin Herrenschmidt 	.map = mpic_host_map,
10370ebfff14SBenjamin Herrenschmidt 	.xlate = mpic_host_xlate,
10380ebfff14SBenjamin Herrenschmidt };
10390ebfff14SBenjamin Herrenschmidt 
104014cf11afSPaul Mackerras /*
104114cf11afSPaul Mackerras  * Exported functions
104214cf11afSPaul Mackerras  */
104314cf11afSPaul Mackerras 
10440ebfff14SBenjamin Herrenschmidt struct mpic * __init mpic_alloc(struct device_node *node,
1045a959ff56SBenjamin Herrenschmidt 				phys_addr_t phys_addr,
104614cf11afSPaul Mackerras 				unsigned int flags,
104714cf11afSPaul Mackerras 				unsigned int isu_size,
104814cf11afSPaul Mackerras 				unsigned int irq_count,
104914cf11afSPaul Mackerras 				const char *name)
105014cf11afSPaul Mackerras {
105114cf11afSPaul Mackerras 	struct mpic	*mpic;
1052d9d1063dSJohannes Berg 	u32		greg_feature;
105314cf11afSPaul Mackerras 	const char	*vers;
105414cf11afSPaul Mackerras 	int		i;
10557df2457dSOlof Johansson 	int		intvec_top;
1056a959ff56SBenjamin Herrenschmidt 	u64		paddr = phys_addr;
105714cf11afSPaul Mackerras 
105885355bb2SKumar Gala 	mpic = kzalloc(sizeof(struct mpic), GFP_KERNEL);
105914cf11afSPaul Mackerras 	if (mpic == NULL)
106014cf11afSPaul Mackerras 		return NULL;
106114cf11afSPaul Mackerras 
106214cf11afSPaul Mackerras 	mpic->name = name;
106314cf11afSPaul Mackerras 
1064b9e5b4e6SBenjamin Herrenschmidt 	mpic->hc_irq = mpic_irq_chip;
106514cf11afSPaul Mackerras 	mpic->hc_irq.typename = name;
106614cf11afSPaul Mackerras 	if (flags & MPIC_PRIMARY)
106714cf11afSPaul Mackerras 		mpic->hc_irq.set_affinity = mpic_set_affinity;
10686cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
1069b9e5b4e6SBenjamin Herrenschmidt 	mpic->hc_ht_irq = mpic_irq_ht_chip;
1070b9e5b4e6SBenjamin Herrenschmidt 	mpic->hc_ht_irq.typename = name;
1071b9e5b4e6SBenjamin Herrenschmidt 	if (flags & MPIC_PRIMARY)
1072b9e5b4e6SBenjamin Herrenschmidt 		mpic->hc_ht_irq.set_affinity = mpic_set_affinity;
10736cfef5b2SMichael Ellerman #endif /* CONFIG_MPIC_U3_HT_IRQS */
1074fbf0274eSBenjamin Herrenschmidt 
107514cf11afSPaul Mackerras #ifdef CONFIG_SMP
1076b9e5b4e6SBenjamin Herrenschmidt 	mpic->hc_ipi = mpic_ipi_chip;
10770ebfff14SBenjamin Herrenschmidt 	mpic->hc_ipi.typename = name;
107814cf11afSPaul Mackerras #endif /* CONFIG_SMP */
107914cf11afSPaul Mackerras 
108014cf11afSPaul Mackerras 	mpic->flags = flags;
108114cf11afSPaul Mackerras 	mpic->isu_size = isu_size;
108214cf11afSPaul Mackerras 	mpic->irq_count = irq_count;
108314cf11afSPaul Mackerras 	mpic->num_sources = 0; /* so far */
108414cf11afSPaul Mackerras 
10857df2457dSOlof Johansson 	if (flags & MPIC_LARGE_VECTORS)
10867df2457dSOlof Johansson 		intvec_top = 2047;
10877df2457dSOlof Johansson 	else
10887df2457dSOlof Johansson 		intvec_top = 255;
10897df2457dSOlof Johansson 
10907df2457dSOlof Johansson 	mpic->timer_vecs[0] = intvec_top - 8;
10917df2457dSOlof Johansson 	mpic->timer_vecs[1] = intvec_top - 7;
10927df2457dSOlof Johansson 	mpic->timer_vecs[2] = intvec_top - 6;
10937df2457dSOlof Johansson 	mpic->timer_vecs[3] = intvec_top - 5;
10947df2457dSOlof Johansson 	mpic->ipi_vecs[0]   = intvec_top - 4;
10957df2457dSOlof Johansson 	mpic->ipi_vecs[1]   = intvec_top - 3;
10967df2457dSOlof Johansson 	mpic->ipi_vecs[2]   = intvec_top - 2;
10977df2457dSOlof Johansson 	mpic->ipi_vecs[3]   = intvec_top - 1;
10987df2457dSOlof Johansson 	mpic->spurious_vec  = intvec_top;
10997df2457dSOlof Johansson 
1100a959ff56SBenjamin Herrenschmidt 	/* Check for "big-endian" in device-tree */
1101e2eb6392SStephen Rothwell 	if (node && of_get_property(node, "big-endian", NULL) != NULL)
1102a959ff56SBenjamin Herrenschmidt 		mpic->flags |= MPIC_BIG_ENDIAN;
1103a959ff56SBenjamin Herrenschmidt 
11047fd72186SBenjamin Herrenschmidt 	/* Look for protected sources */
11057fd72186SBenjamin Herrenschmidt 	if (node) {
1106d9d1063dSJohannes Berg 		int psize;
1107d9d1063dSJohannes Berg 		unsigned int bits, mapsize;
11087fd72186SBenjamin Herrenschmidt 		const u32 *psrc =
11097fd72186SBenjamin Herrenschmidt 			of_get_property(node, "protected-sources", &psize);
11107fd72186SBenjamin Herrenschmidt 		if (psrc) {
11117fd72186SBenjamin Herrenschmidt 			psize /= 4;
11127fd72186SBenjamin Herrenschmidt 			bits = intvec_top + 1;
11137fd72186SBenjamin Herrenschmidt 			mapsize = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1114ea96025aSAnton Vorontsov 			mpic->protected = kzalloc(mapsize, GFP_KERNEL);
11157fd72186SBenjamin Herrenschmidt 			BUG_ON(mpic->protected == NULL);
11167fd72186SBenjamin Herrenschmidt 			for (i = 0; i < psize; i++) {
11177fd72186SBenjamin Herrenschmidt 				if (psrc[i] > intvec_top)
11187fd72186SBenjamin Herrenschmidt 					continue;
11197fd72186SBenjamin Herrenschmidt 				__set_bit(psrc[i], mpic->protected);
11207fd72186SBenjamin Herrenschmidt 			}
11217fd72186SBenjamin Herrenschmidt 		}
11227fd72186SBenjamin Herrenschmidt 	}
1123a959ff56SBenjamin Herrenschmidt 
11247233593bSZang Roy-r61911 #ifdef CONFIG_MPIC_WEIRD
11257233593bSZang Roy-r61911 	mpic->hw_set = mpic_infos[MPIC_GET_REGSET(flags)];
11267233593bSZang Roy-r61911 #endif
11277233593bSZang Roy-r61911 
1128fbf0274eSBenjamin Herrenschmidt 	/* default register type */
1129fbf0274eSBenjamin Herrenschmidt 	mpic->reg_type = (flags & MPIC_BIG_ENDIAN) ?
1130fbf0274eSBenjamin Herrenschmidt 		mpic_access_mmio_be : mpic_access_mmio_le;
1131fbf0274eSBenjamin Herrenschmidt 
1132a959ff56SBenjamin Herrenschmidt 	/* If no physical address is passed in, a device-node is mandatory */
1133a959ff56SBenjamin Herrenschmidt 	BUG_ON(paddr == 0 && node == NULL);
1134a959ff56SBenjamin Herrenschmidt 
1135a959ff56SBenjamin Herrenschmidt 	/* If no physical address passed in, check if it's dcr based */
11360411a5e2SMichael Ellerman 	if (paddr == 0 && of_get_property(node, "dcr-reg", NULL) != NULL) {
1137fbf0274eSBenjamin Herrenschmidt #ifdef CONFIG_PPC_DCR
11380411a5e2SMichael Ellerman 		mpic->flags |= MPIC_USES_DCR;
1139fbf0274eSBenjamin Herrenschmidt 		mpic->reg_type = mpic_access_dcr;
1140fbf0274eSBenjamin Herrenschmidt #else
11410411a5e2SMichael Ellerman 		BUG();
1142fbf0274eSBenjamin Herrenschmidt #endif /* CONFIG_PPC_DCR */
11430411a5e2SMichael Ellerman 	}
1144fbf0274eSBenjamin Herrenschmidt 
1145a959ff56SBenjamin Herrenschmidt 	/* If the MPIC is not DCR based, and no physical address was passed
1146a959ff56SBenjamin Herrenschmidt 	 * in, try to obtain one
1147a959ff56SBenjamin Herrenschmidt 	 */
1148a959ff56SBenjamin Herrenschmidt 	if (paddr == 0 && !(mpic->flags & MPIC_USES_DCR)) {
1149d9d1063dSJohannes Berg 		const u32 *reg = of_get_property(node, "reg", NULL);
1150a959ff56SBenjamin Herrenschmidt 		BUG_ON(reg == NULL);
1151a959ff56SBenjamin Herrenschmidt 		paddr = of_translate_address(node, reg);
1152a959ff56SBenjamin Herrenschmidt 		BUG_ON(paddr == OF_BAD_ADDR);
1153a959ff56SBenjamin Herrenschmidt 	}
1154a959ff56SBenjamin Herrenschmidt 
115514cf11afSPaul Mackerras 	/* Map the global registers */
11565a2642f6SBenjamin Herrenschmidt 	mpic_map(mpic, node, paddr, &mpic->gregs, MPIC_INFO(GREG_BASE), 0x1000);
11575a2642f6SBenjamin Herrenschmidt 	mpic_map(mpic, node, paddr, &mpic->tmregs, MPIC_INFO(TIMER_BASE), 0x1000);
115814cf11afSPaul Mackerras 
115914cf11afSPaul Mackerras 	/* Reset */
116014cf11afSPaul Mackerras 	if (flags & MPIC_WANTS_RESET) {
11617233593bSZang Roy-r61911 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
11627233593bSZang Roy-r61911 			   mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
116314cf11afSPaul Mackerras 			   | MPIC_GREG_GCONF_RESET);
11647233593bSZang Roy-r61911 		while( mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
116514cf11afSPaul Mackerras 		       & MPIC_GREG_GCONF_RESET)
116614cf11afSPaul Mackerras 			mb();
116714cf11afSPaul Mackerras 	}
116814cf11afSPaul Mackerras 
1169d91e4ea7SKumar Gala 	/* CoreInt */
1170d91e4ea7SKumar Gala 	if (flags & MPIC_ENABLE_COREINT)
1171d91e4ea7SKumar Gala 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1172d91e4ea7SKumar Gala 			   mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1173d91e4ea7SKumar Gala 			   | MPIC_GREG_GCONF_COREINT);
1174d91e4ea7SKumar Gala 
1175f365355eSOlof Johansson 	if (flags & MPIC_ENABLE_MCK)
1176f365355eSOlof Johansson 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1177f365355eSOlof Johansson 			   mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1178f365355eSOlof Johansson 			   | MPIC_GREG_GCONF_MCK);
1179f365355eSOlof Johansson 
118014cf11afSPaul Mackerras 	/* Read feature register, calculate num CPUs and, for non-ISU
118114cf11afSPaul Mackerras 	 * MPICs, num sources as well. On ISU MPICs, sources are counted
118214cf11afSPaul Mackerras 	 * as ISUs are added
118314cf11afSPaul Mackerras 	 */
1184d9d1063dSJohannes Berg 	greg_feature = mpic_read(mpic->gregs, MPIC_INFO(GREG_FEATURE_0));
1185d9d1063dSJohannes Berg 	mpic->num_cpus = ((greg_feature & MPIC_GREG_FEATURE_LAST_CPU_MASK)
118614cf11afSPaul Mackerras 			  >> MPIC_GREG_FEATURE_LAST_CPU_SHIFT) + 1;
11875073e7eeSAnton Vorontsov 	if (isu_size == 0) {
1188475ca391SKumar Gala 		if (flags & MPIC_BROKEN_FRR_NIRQS)
1189475ca391SKumar Gala 			mpic->num_sources = mpic->irq_count;
1190475ca391SKumar Gala 		else
1191d9d1063dSJohannes Berg 			mpic->num_sources =
1192d9d1063dSJohannes Berg 				((greg_feature & MPIC_GREG_FEATURE_LAST_SRC_MASK)
119314cf11afSPaul Mackerras 				 >> MPIC_GREG_FEATURE_LAST_SRC_SHIFT) + 1;
11945073e7eeSAnton Vorontsov 	}
119514cf11afSPaul Mackerras 
119614cf11afSPaul Mackerras 	/* Map the per-CPU registers */
119714cf11afSPaul Mackerras 	for (i = 0; i < mpic->num_cpus; i++) {
11985a2642f6SBenjamin Herrenschmidt 		mpic_map(mpic, node, paddr, &mpic->cpuregs[i],
1199fbf0274eSBenjamin Herrenschmidt 			 MPIC_INFO(CPU_BASE) + i * MPIC_INFO(CPU_STRIDE),
1200fbf0274eSBenjamin Herrenschmidt 			 0x1000);
120114cf11afSPaul Mackerras 	}
120214cf11afSPaul Mackerras 
120314cf11afSPaul Mackerras 	/* Initialize main ISU if none provided */
120414cf11afSPaul Mackerras 	if (mpic->isu_size == 0) {
120514cf11afSPaul Mackerras 		mpic->isu_size = mpic->num_sources;
12065a2642f6SBenjamin Herrenschmidt 		mpic_map(mpic, node, paddr, &mpic->isus[0],
1207fbf0274eSBenjamin Herrenschmidt 			 MPIC_INFO(IRQ_BASE), MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
120814cf11afSPaul Mackerras 	}
120914cf11afSPaul Mackerras 	mpic->isu_shift = 1 + __ilog2(mpic->isu_size - 1);
121014cf11afSPaul Mackerras 	mpic->isu_mask = (1 << mpic->isu_shift) - 1;
121114cf11afSPaul Mackerras 
121231207dabSKumar Gala 	mpic->irqhost = irq_alloc_host(node, IRQ_HOST_MAP_LINEAR,
121331207dabSKumar Gala 				       isu_size ? isu_size : mpic->num_sources,
121431207dabSKumar Gala 				       &mpic_host_ops,
121531207dabSKumar Gala 				       flags & MPIC_LARGE_VECTORS ? 2048 : 256);
121631207dabSKumar Gala 	if (mpic->irqhost == NULL)
121731207dabSKumar Gala 		return NULL;
121831207dabSKumar Gala 
121931207dabSKumar Gala 	mpic->irqhost->host_data = mpic;
122031207dabSKumar Gala 
122114cf11afSPaul Mackerras 	/* Display version */
1222d9d1063dSJohannes Berg 	switch (greg_feature & MPIC_GREG_FEATURE_VERSION_MASK) {
122314cf11afSPaul Mackerras 	case 1:
122414cf11afSPaul Mackerras 		vers = "1.0";
122514cf11afSPaul Mackerras 		break;
122614cf11afSPaul Mackerras 	case 2:
122714cf11afSPaul Mackerras 		vers = "1.2";
122814cf11afSPaul Mackerras 		break;
122914cf11afSPaul Mackerras 	case 3:
123014cf11afSPaul Mackerras 		vers = "1.3";
123114cf11afSPaul Mackerras 		break;
123214cf11afSPaul Mackerras 	default:
123314cf11afSPaul Mackerras 		vers = "<unknown>";
123414cf11afSPaul Mackerras 		break;
123514cf11afSPaul Mackerras 	}
1236a959ff56SBenjamin Herrenschmidt 	printk(KERN_INFO "mpic: Setting up MPIC \"%s\" version %s at %llx,"
1237a959ff56SBenjamin Herrenschmidt 	       " max %d CPUs\n",
1238a959ff56SBenjamin Herrenschmidt 	       name, vers, (unsigned long long)paddr, mpic->num_cpus);
1239a959ff56SBenjamin Herrenschmidt 	printk(KERN_INFO "mpic: ISU size: %d, shift: %d, mask: %x\n",
1240a959ff56SBenjamin Herrenschmidt 	       mpic->isu_size, mpic->isu_shift, mpic->isu_mask);
124114cf11afSPaul Mackerras 
124214cf11afSPaul Mackerras 	mpic->next = mpics;
124314cf11afSPaul Mackerras 	mpics = mpic;
124414cf11afSPaul Mackerras 
12450ebfff14SBenjamin Herrenschmidt 	if (flags & MPIC_PRIMARY) {
124614cf11afSPaul Mackerras 		mpic_primary = mpic;
12470ebfff14SBenjamin Herrenschmidt 		irq_set_default_host(mpic->irqhost);
12480ebfff14SBenjamin Herrenschmidt 	}
124914cf11afSPaul Mackerras 
125014cf11afSPaul Mackerras 	return mpic;
125114cf11afSPaul Mackerras }
125214cf11afSPaul Mackerras 
125314cf11afSPaul Mackerras void __init mpic_assign_isu(struct mpic *mpic, unsigned int isu_num,
1254a959ff56SBenjamin Herrenschmidt 			    phys_addr_t paddr)
125514cf11afSPaul Mackerras {
125614cf11afSPaul Mackerras 	unsigned int isu_first = isu_num * mpic->isu_size;
125714cf11afSPaul Mackerras 
125814cf11afSPaul Mackerras 	BUG_ON(isu_num >= MPIC_MAX_ISU);
125914cf11afSPaul Mackerras 
12605a2642f6SBenjamin Herrenschmidt 	mpic_map(mpic, mpic->irqhost->of_node,
12615a2642f6SBenjamin Herrenschmidt 		 paddr, &mpic->isus[isu_num], 0,
12627233593bSZang Roy-r61911 		 MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
12635a2642f6SBenjamin Herrenschmidt 
126414cf11afSPaul Mackerras 	if ((isu_first + mpic->isu_size) > mpic->num_sources)
126514cf11afSPaul Mackerras 		mpic->num_sources = isu_first + mpic->isu_size;
126614cf11afSPaul Mackerras }
126714cf11afSPaul Mackerras 
12680ebfff14SBenjamin Herrenschmidt void __init mpic_set_default_senses(struct mpic *mpic, u8 *senses, int count)
12690ebfff14SBenjamin Herrenschmidt {
12700ebfff14SBenjamin Herrenschmidt 	mpic->senses = senses;
12710ebfff14SBenjamin Herrenschmidt 	mpic->senses_count = count;
12720ebfff14SBenjamin Herrenschmidt }
12730ebfff14SBenjamin Herrenschmidt 
127414cf11afSPaul Mackerras void __init mpic_init(struct mpic *mpic)
127514cf11afSPaul Mackerras {
127614cf11afSPaul Mackerras 	int i;
1277cc353c30SArnd Bergmann 	int cpu;
127814cf11afSPaul Mackerras 
127914cf11afSPaul Mackerras 	BUG_ON(mpic->num_sources == 0);
128014cf11afSPaul Mackerras 
128114cf11afSPaul Mackerras 	printk(KERN_INFO "mpic: Initializing for %d sources\n", mpic->num_sources);
128214cf11afSPaul Mackerras 
128314cf11afSPaul Mackerras 	/* Set current processor priority to max */
12847233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
128514cf11afSPaul Mackerras 
128614cf11afSPaul Mackerras 	/* Initialize timers: just disable them all */
128714cf11afSPaul Mackerras 	for (i = 0; i < 4; i++) {
128814cf11afSPaul Mackerras 		mpic_write(mpic->tmregs,
12897233593bSZang Roy-r61911 			   i * MPIC_INFO(TIMER_STRIDE) +
12907233593bSZang Roy-r61911 			   MPIC_INFO(TIMER_DESTINATION), 0);
129114cf11afSPaul Mackerras 		mpic_write(mpic->tmregs,
12927233593bSZang Roy-r61911 			   i * MPIC_INFO(TIMER_STRIDE) +
12937233593bSZang Roy-r61911 			   MPIC_INFO(TIMER_VECTOR_PRI),
129414cf11afSPaul Mackerras 			   MPIC_VECPRI_MASK |
12957df2457dSOlof Johansson 			   (mpic->timer_vecs[0] + i));
129614cf11afSPaul Mackerras 	}
129714cf11afSPaul Mackerras 
129814cf11afSPaul Mackerras 	/* Initialize IPIs to our reserved vectors and mark them disabled for now */
129914cf11afSPaul Mackerras 	mpic_test_broken_ipi(mpic);
130014cf11afSPaul Mackerras 	for (i = 0; i < 4; i++) {
130114cf11afSPaul Mackerras 		mpic_ipi_write(i,
130214cf11afSPaul Mackerras 			       MPIC_VECPRI_MASK |
130314cf11afSPaul Mackerras 			       (10 << MPIC_VECPRI_PRIORITY_SHIFT) |
13047df2457dSOlof Johansson 			       (mpic->ipi_vecs[0] + i));
130514cf11afSPaul Mackerras 	}
130614cf11afSPaul Mackerras 
130714cf11afSPaul Mackerras 	/* Initialize interrupt sources */
130814cf11afSPaul Mackerras 	if (mpic->irq_count == 0)
130914cf11afSPaul Mackerras 		mpic->irq_count = mpic->num_sources;
131014cf11afSPaul Mackerras 
13111beb6a7dSBenjamin Herrenschmidt 	/* Do the HT PIC fixups on U3 broken mpic */
131214cf11afSPaul Mackerras 	DBG("MPIC flags: %x\n", mpic->flags);
131305af7bd2SMichael Ellerman 	if ((mpic->flags & MPIC_U3_HT_IRQS) && (mpic->flags & MPIC_PRIMARY)) {
13141beb6a7dSBenjamin Herrenschmidt 		mpic_scan_ht_pics(mpic);
131505af7bd2SMichael Ellerman 		mpic_u3msi_init(mpic);
131605af7bd2SMichael Ellerman 	}
131714cf11afSPaul Mackerras 
131838958dd9SOlof Johansson 	mpic_pasemi_msi_init(mpic);
131938958dd9SOlof Johansson 
1320cc353c30SArnd Bergmann 	if (mpic->flags & MPIC_PRIMARY)
1321cc353c30SArnd Bergmann 		cpu = hard_smp_processor_id();
1322cc353c30SArnd Bergmann 	else
1323cc353c30SArnd Bergmann 		cpu = 0;
1324cc353c30SArnd Bergmann 
132514cf11afSPaul Mackerras 	for (i = 0; i < mpic->num_sources; i++) {
132614cf11afSPaul Mackerras 		/* start with vector = source number, and masked */
13276e99e458SBenjamin Herrenschmidt 		u32 vecpri = MPIC_VECPRI_MASK | i |
13286e99e458SBenjamin Herrenschmidt 			(8 << MPIC_VECPRI_PRIORITY_SHIFT);
132914cf11afSPaul Mackerras 
13307fd72186SBenjamin Herrenschmidt 		/* check if protected */
13317fd72186SBenjamin Herrenschmidt 		if (mpic->protected && test_bit(i, mpic->protected))
13327fd72186SBenjamin Herrenschmidt 			continue;
133314cf11afSPaul Mackerras 		/* init hw */
13347233593bSZang Roy-r61911 		mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI), vecpri);
1335cc353c30SArnd Bergmann 		mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION), 1 << cpu);
133614cf11afSPaul Mackerras 	}
133714cf11afSPaul Mackerras 
13387df2457dSOlof Johansson 	/* Init spurious vector */
13397df2457dSOlof Johansson 	mpic_write(mpic->gregs, MPIC_INFO(GREG_SPURIOUS), mpic->spurious_vec);
134014cf11afSPaul Mackerras 
13417233593bSZang Roy-r61911 	/* Disable 8259 passthrough, if supported */
13427233593bSZang Roy-r61911 	if (!(mpic->flags & MPIC_NO_PTHROU_DIS))
13437233593bSZang Roy-r61911 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
13447233593bSZang Roy-r61911 			   mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
134514cf11afSPaul Mackerras 			   | MPIC_GREG_GCONF_8259_PTHROU_DIS);
134614cf11afSPaul Mackerras 
1347d87bf3beSOlof Johansson 	if (mpic->flags & MPIC_NO_BIAS)
1348d87bf3beSOlof Johansson 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1349d87bf3beSOlof Johansson 			mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1350d87bf3beSOlof Johansson 			| MPIC_GREG_GCONF_NO_BIAS);
1351d87bf3beSOlof Johansson 
135214cf11afSPaul Mackerras 	/* Set current processor priority to 0 */
13537233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
13543669e930SJohannes Berg 
13553669e930SJohannes Berg #ifdef CONFIG_PM
13563669e930SJohannes Berg 	/* allocate memory to save mpic state */
1357ea96025aSAnton Vorontsov 	mpic->save_data = kmalloc(mpic->num_sources * sizeof(*mpic->save_data),
1358ea96025aSAnton Vorontsov 				  GFP_KERNEL);
13593669e930SJohannes Berg 	BUG_ON(mpic->save_data == NULL);
13603669e930SJohannes Berg #endif
136114cf11afSPaul Mackerras }
136214cf11afSPaul Mackerras 
1363868ea0c9SMark A. Greer void __init mpic_set_clk_ratio(struct mpic *mpic, u32 clock_ratio)
1364868ea0c9SMark A. Greer {
1365868ea0c9SMark A. Greer 	u32 v;
136614cf11afSPaul Mackerras 
1367868ea0c9SMark A. Greer 	v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
1368868ea0c9SMark A. Greer 	v &= ~MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO_MASK;
1369868ea0c9SMark A. Greer 	v |= MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO(clock_ratio);
1370868ea0c9SMark A. Greer 	mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
1371868ea0c9SMark A. Greer }
1372868ea0c9SMark A. Greer 
1373868ea0c9SMark A. Greer void __init mpic_set_serial_int(struct mpic *mpic, int enable)
1374868ea0c9SMark A. Greer {
1375ba1826e5SBenjamin Herrenschmidt 	unsigned long flags;
1376868ea0c9SMark A. Greer 	u32 v;
1377868ea0c9SMark A. Greer 
1378ba1826e5SBenjamin Herrenschmidt 	spin_lock_irqsave(&mpic_lock, flags);
1379868ea0c9SMark A. Greer 	v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
1380868ea0c9SMark A. Greer 	if (enable)
1381868ea0c9SMark A. Greer 		v |= MPIC_GREG_GLOBAL_CONF_1_SIE;
1382868ea0c9SMark A. Greer 	else
1383868ea0c9SMark A. Greer 		v &= ~MPIC_GREG_GLOBAL_CONF_1_SIE;
1384868ea0c9SMark A. Greer 	mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
1385ba1826e5SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&mpic_lock, flags);
1386868ea0c9SMark A. Greer }
138714cf11afSPaul Mackerras 
138814cf11afSPaul Mackerras void mpic_irq_set_priority(unsigned int irq, unsigned int pri)
138914cf11afSPaul Mackerras {
1390d69a78d7STony Breeds 	struct mpic *mpic = mpic_find(irq);
13910ebfff14SBenjamin Herrenschmidt 	unsigned int src = mpic_irq_to_hw(irq);
139214cf11afSPaul Mackerras 	unsigned long flags;
139314cf11afSPaul Mackerras 	u32 reg;
139414cf11afSPaul Mackerras 
139506a901c5SStephen Rothwell 	if (!mpic)
139606a901c5SStephen Rothwell 		return;
139706a901c5SStephen Rothwell 
139814cf11afSPaul Mackerras 	spin_lock_irqsave(&mpic_lock, flags);
1399d69a78d7STony Breeds 	if (mpic_is_ipi(mpic, irq)) {
14007df2457dSOlof Johansson 		reg = mpic_ipi_read(src - mpic->ipi_vecs[0]) &
1401e5356640SBenjamin Herrenschmidt 			~MPIC_VECPRI_PRIORITY_MASK;
14027df2457dSOlof Johansson 		mpic_ipi_write(src - mpic->ipi_vecs[0],
140314cf11afSPaul Mackerras 			       reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
140414cf11afSPaul Mackerras 	} else {
14057233593bSZang Roy-r61911 		reg = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI))
1406e5356640SBenjamin Herrenschmidt 			& ~MPIC_VECPRI_PRIORITY_MASK;
14077233593bSZang Roy-r61911 		mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
140814cf11afSPaul Mackerras 			       reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
140914cf11afSPaul Mackerras 	}
141014cf11afSPaul Mackerras 	spin_unlock_irqrestore(&mpic_lock, flags);
141114cf11afSPaul Mackerras }
141214cf11afSPaul Mackerras 
141314cf11afSPaul Mackerras void mpic_setup_this_cpu(void)
141414cf11afSPaul Mackerras {
141514cf11afSPaul Mackerras #ifdef CONFIG_SMP
141614cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
141714cf11afSPaul Mackerras 	unsigned long flags;
141814cf11afSPaul Mackerras 	u32 msk = 1 << hard_smp_processor_id();
141914cf11afSPaul Mackerras 	unsigned int i;
142014cf11afSPaul Mackerras 
142114cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
142214cf11afSPaul Mackerras 
142314cf11afSPaul Mackerras 	DBG("%s: setup_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
142414cf11afSPaul Mackerras 
142514cf11afSPaul Mackerras 	spin_lock_irqsave(&mpic_lock, flags);
142614cf11afSPaul Mackerras 
142714cf11afSPaul Mackerras  	/* let the mpic know we want intrs. default affinity is 0xffffffff
142814cf11afSPaul Mackerras 	 * until changed via /proc. That's how it's done on x86. If we want
142914cf11afSPaul Mackerras 	 * it differently, then we should make sure we also change the default
1430a53da52fSIngo Molnar 	 * values of irq_desc[].affinity in irq.c.
143114cf11afSPaul Mackerras  	 */
143214cf11afSPaul Mackerras 	if (distribute_irqs) {
143314cf11afSPaul Mackerras 	 	for (i = 0; i < mpic->num_sources ; i++)
14347233593bSZang Roy-r61911 			mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
14357233593bSZang Roy-r61911 				mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) | msk);
143614cf11afSPaul Mackerras 	}
143714cf11afSPaul Mackerras 
143814cf11afSPaul Mackerras 	/* Set current processor priority to 0 */
14397233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
144014cf11afSPaul Mackerras 
144114cf11afSPaul Mackerras 	spin_unlock_irqrestore(&mpic_lock, flags);
144214cf11afSPaul Mackerras #endif /* CONFIG_SMP */
144314cf11afSPaul Mackerras }
144414cf11afSPaul Mackerras 
144514cf11afSPaul Mackerras int mpic_cpu_get_priority(void)
144614cf11afSPaul Mackerras {
144714cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
144814cf11afSPaul Mackerras 
14497233593bSZang Roy-r61911 	return mpic_cpu_read(MPIC_INFO(CPU_CURRENT_TASK_PRI));
145014cf11afSPaul Mackerras }
145114cf11afSPaul Mackerras 
145214cf11afSPaul Mackerras void mpic_cpu_set_priority(int prio)
145314cf11afSPaul Mackerras {
145414cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
145514cf11afSPaul Mackerras 
145614cf11afSPaul Mackerras 	prio &= MPIC_CPU_TASKPRI_MASK;
14577233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), prio);
145814cf11afSPaul Mackerras }
145914cf11afSPaul Mackerras 
146014cf11afSPaul Mackerras void mpic_teardown_this_cpu(int secondary)
146114cf11afSPaul Mackerras {
146214cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
146314cf11afSPaul Mackerras 	unsigned long flags;
146414cf11afSPaul Mackerras 	u32 msk = 1 << hard_smp_processor_id();
146514cf11afSPaul Mackerras 	unsigned int i;
146614cf11afSPaul Mackerras 
146714cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
146814cf11afSPaul Mackerras 
146914cf11afSPaul Mackerras 	DBG("%s: teardown_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
147014cf11afSPaul Mackerras 	spin_lock_irqsave(&mpic_lock, flags);
147114cf11afSPaul Mackerras 
147214cf11afSPaul Mackerras 	/* let the mpic know we don't want intrs.  */
147314cf11afSPaul Mackerras 	for (i = 0; i < mpic->num_sources ; i++)
14747233593bSZang Roy-r61911 		mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
14757233593bSZang Roy-r61911 			mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) & ~msk);
147614cf11afSPaul Mackerras 
147714cf11afSPaul Mackerras 	/* Set current processor priority to max */
14787233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
14797132799bSValentine Barshak 	/* We need to EOI the IPI since not all platforms reset the MPIC
14807132799bSValentine Barshak 	 * on boot and new interrupts wouldn't get delivered otherwise.
14817132799bSValentine Barshak 	 */
14827132799bSValentine Barshak 	mpic_eoi(mpic);
148314cf11afSPaul Mackerras 
148414cf11afSPaul Mackerras 	spin_unlock_irqrestore(&mpic_lock, flags);
148514cf11afSPaul Mackerras }
148614cf11afSPaul Mackerras 
148714cf11afSPaul Mackerras 
148814cf11afSPaul Mackerras void mpic_send_ipi(unsigned int ipi_no, unsigned int cpu_mask)
148914cf11afSPaul Mackerras {
149014cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
149114cf11afSPaul Mackerras 
149214cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
149314cf11afSPaul Mackerras 
14941beb6a7dSBenjamin Herrenschmidt #ifdef DEBUG_IPI
149514cf11afSPaul Mackerras 	DBG("%s: send_ipi(ipi_no: %d)\n", mpic->name, ipi_no);
14961beb6a7dSBenjamin Herrenschmidt #endif
149714cf11afSPaul Mackerras 
14987233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_IPI_DISPATCH_0) +
14997233593bSZang Roy-r61911 		       ipi_no * MPIC_INFO(CPU_IPI_DISPATCH_STRIDE),
150014cf11afSPaul Mackerras 		       mpic_physmask(cpu_mask & cpus_addr(cpu_online_map)[0]));
150114cf11afSPaul Mackerras }
150214cf11afSPaul Mackerras 
1503f365355eSOlof Johansson static unsigned int _mpic_get_one_irq(struct mpic *mpic, int reg)
150414cf11afSPaul Mackerras {
15050ebfff14SBenjamin Herrenschmidt 	u32 src;
150614cf11afSPaul Mackerras 
1507f365355eSOlof Johansson 	src = mpic_cpu_read(reg) & MPIC_INFO(VECPRI_VECTOR_MASK);
15081beb6a7dSBenjamin Herrenschmidt #ifdef DEBUG_LOW
1509f365355eSOlof Johansson 	DBG("%s: get_one_irq(reg 0x%x): %d\n", mpic->name, reg, src);
15101beb6a7dSBenjamin Herrenschmidt #endif
15115cddd2e3SJosh Boyer 	if (unlikely(src == mpic->spurious_vec)) {
15125cddd2e3SJosh Boyer 		if (mpic->flags & MPIC_SPV_EOI)
15135cddd2e3SJosh Boyer 			mpic_eoi(mpic);
15140ebfff14SBenjamin Herrenschmidt 		return NO_IRQ;
15155cddd2e3SJosh Boyer 	}
15167fd72186SBenjamin Herrenschmidt 	if (unlikely(mpic->protected && test_bit(src, mpic->protected))) {
15177fd72186SBenjamin Herrenschmidt 		if (printk_ratelimit())
15187fd72186SBenjamin Herrenschmidt 			printk(KERN_WARNING "%s: Got protected source %d !\n",
15197fd72186SBenjamin Herrenschmidt 			       mpic->name, (int)src);
15207fd72186SBenjamin Herrenschmidt 		mpic_eoi(mpic);
15217fd72186SBenjamin Herrenschmidt 		return NO_IRQ;
15227fd72186SBenjamin Herrenschmidt 	}
15237fd72186SBenjamin Herrenschmidt 
15240ebfff14SBenjamin Herrenschmidt 	return irq_linear_revmap(mpic->irqhost, src);
152514cf11afSPaul Mackerras }
152614cf11afSPaul Mackerras 
1527f365355eSOlof Johansson unsigned int mpic_get_one_irq(struct mpic *mpic)
1528f365355eSOlof Johansson {
1529f365355eSOlof Johansson 	return _mpic_get_one_irq(mpic, MPIC_INFO(CPU_INTACK));
1530f365355eSOlof Johansson }
1531f365355eSOlof Johansson 
153235a84c2fSOlaf Hering unsigned int mpic_get_irq(void)
153314cf11afSPaul Mackerras {
153414cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
153514cf11afSPaul Mackerras 
153614cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
153714cf11afSPaul Mackerras 
153835a84c2fSOlaf Hering 	return mpic_get_one_irq(mpic);
153914cf11afSPaul Mackerras }
154014cf11afSPaul Mackerras 
1541d91e4ea7SKumar Gala unsigned int mpic_get_coreint_irq(void)
1542d91e4ea7SKumar Gala {
1543d91e4ea7SKumar Gala #ifdef CONFIG_BOOKE
1544d91e4ea7SKumar Gala 	struct mpic *mpic = mpic_primary;
1545d91e4ea7SKumar Gala 	u32 src;
1546d91e4ea7SKumar Gala 
1547d91e4ea7SKumar Gala 	BUG_ON(mpic == NULL);
1548d91e4ea7SKumar Gala 
1549d91e4ea7SKumar Gala 	src = mfspr(SPRN_EPR);
1550d91e4ea7SKumar Gala 
1551d91e4ea7SKumar Gala 	if (unlikely(src == mpic->spurious_vec)) {
1552d91e4ea7SKumar Gala 		if (mpic->flags & MPIC_SPV_EOI)
1553d91e4ea7SKumar Gala 			mpic_eoi(mpic);
1554d91e4ea7SKumar Gala 		return NO_IRQ;
1555d91e4ea7SKumar Gala 	}
1556d91e4ea7SKumar Gala 	if (unlikely(mpic->protected && test_bit(src, mpic->protected))) {
1557d91e4ea7SKumar Gala 		if (printk_ratelimit())
1558d91e4ea7SKumar Gala 			printk(KERN_WARNING "%s: Got protected source %d !\n",
1559d91e4ea7SKumar Gala 			       mpic->name, (int)src);
1560d91e4ea7SKumar Gala 		return NO_IRQ;
1561d91e4ea7SKumar Gala 	}
1562d91e4ea7SKumar Gala 
1563d91e4ea7SKumar Gala 	return irq_linear_revmap(mpic->irqhost, src);
1564d91e4ea7SKumar Gala #else
1565d91e4ea7SKumar Gala 	return NO_IRQ;
1566d91e4ea7SKumar Gala #endif
1567d91e4ea7SKumar Gala }
1568d91e4ea7SKumar Gala 
1569f365355eSOlof Johansson unsigned int mpic_get_mcirq(void)
1570f365355eSOlof Johansson {
1571f365355eSOlof Johansson 	struct mpic *mpic = mpic_primary;
1572f365355eSOlof Johansson 
1573f365355eSOlof Johansson 	BUG_ON(mpic == NULL);
1574f365355eSOlof Johansson 
1575f365355eSOlof Johansson 	return _mpic_get_one_irq(mpic, MPIC_INFO(CPU_MCACK));
1576f365355eSOlof Johansson }
157714cf11afSPaul Mackerras 
157814cf11afSPaul Mackerras #ifdef CONFIG_SMP
157914cf11afSPaul Mackerras void mpic_request_ipis(void)
158014cf11afSPaul Mackerras {
158114cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
158278608dd3SMilton Miller 	int i;
158314cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
158414cf11afSPaul Mackerras 
15850ebfff14SBenjamin Herrenschmidt 	printk(KERN_INFO "mpic: requesting IPIs ... \n");
158614cf11afSPaul Mackerras 
15870ebfff14SBenjamin Herrenschmidt 	for (i = 0; i < 4; i++) {
15880ebfff14SBenjamin Herrenschmidt 		unsigned int vipi = irq_create_mapping(mpic->irqhost,
15897df2457dSOlof Johansson 						       mpic->ipi_vecs[0] + i);
15900ebfff14SBenjamin Herrenschmidt 		if (vipi == NO_IRQ) {
159178608dd3SMilton Miller 			printk(KERN_ERR "Failed to map %s\n", smp_ipi_name[i]);
159278608dd3SMilton Miller 			continue;
15930ebfff14SBenjamin Herrenschmidt 		}
159478608dd3SMilton Miller 		smp_request_message_ipi(vipi, i);
15950ebfff14SBenjamin Herrenschmidt 	}
159614cf11afSPaul Mackerras }
1597a9c59264SPaul Mackerras 
1598a9c59264SPaul Mackerras void smp_mpic_message_pass(int target, int msg)
1599a9c59264SPaul Mackerras {
1600a9c59264SPaul Mackerras 	/* make sure we're sending something that translates to an IPI */
1601a9c59264SPaul Mackerras 	if ((unsigned int)msg > 3) {
1602a9c59264SPaul Mackerras 		printk("SMP %d: smp_message_pass: unknown msg %d\n",
1603a9c59264SPaul Mackerras 		       smp_processor_id(), msg);
1604a9c59264SPaul Mackerras 		return;
1605a9c59264SPaul Mackerras 	}
1606a9c59264SPaul Mackerras 	switch (target) {
1607a9c59264SPaul Mackerras 	case MSG_ALL:
1608a9c59264SPaul Mackerras 		mpic_send_ipi(msg, 0xffffffff);
1609a9c59264SPaul Mackerras 		break;
1610a9c59264SPaul Mackerras 	case MSG_ALL_BUT_SELF:
1611a9c59264SPaul Mackerras 		mpic_send_ipi(msg, 0xffffffff & ~(1 << smp_processor_id()));
1612a9c59264SPaul Mackerras 		break;
1613a9c59264SPaul Mackerras 	default:
1614a9c59264SPaul Mackerras 		mpic_send_ipi(msg, 1 << target);
1615a9c59264SPaul Mackerras 		break;
1616a9c59264SPaul Mackerras 	}
1617a9c59264SPaul Mackerras }
1618775aeff4SMichael Ellerman 
1619775aeff4SMichael Ellerman int __init smp_mpic_probe(void)
1620775aeff4SMichael Ellerman {
1621775aeff4SMichael Ellerman 	int nr_cpus;
1622775aeff4SMichael Ellerman 
1623775aeff4SMichael Ellerman 	DBG("smp_mpic_probe()...\n");
1624775aeff4SMichael Ellerman 
1625775aeff4SMichael Ellerman 	nr_cpus = cpus_weight(cpu_possible_map);
1626775aeff4SMichael Ellerman 
1627775aeff4SMichael Ellerman 	DBG("nr_cpus: %d\n", nr_cpus);
1628775aeff4SMichael Ellerman 
1629775aeff4SMichael Ellerman 	if (nr_cpus > 1)
1630775aeff4SMichael Ellerman 		mpic_request_ipis();
1631775aeff4SMichael Ellerman 
1632775aeff4SMichael Ellerman 	return nr_cpus;
1633775aeff4SMichael Ellerman }
1634775aeff4SMichael Ellerman 
1635775aeff4SMichael Ellerman void __devinit smp_mpic_setup_cpu(int cpu)
1636775aeff4SMichael Ellerman {
1637775aeff4SMichael Ellerman 	mpic_setup_this_cpu();
1638775aeff4SMichael Ellerman }
163914cf11afSPaul Mackerras #endif /* CONFIG_SMP */
16403669e930SJohannes Berg 
16413669e930SJohannes Berg #ifdef CONFIG_PM
16423669e930SJohannes Berg static int mpic_suspend(struct sys_device *dev, pm_message_t state)
16433669e930SJohannes Berg {
16443669e930SJohannes Berg 	struct mpic *mpic = container_of(dev, struct mpic, sysdev);
16453669e930SJohannes Berg 	int i;
16463669e930SJohannes Berg 
16473669e930SJohannes Berg 	for (i = 0; i < mpic->num_sources; i++) {
16483669e930SJohannes Berg 		mpic->save_data[i].vecprio =
16493669e930SJohannes Berg 			mpic_irq_read(i, MPIC_INFO(IRQ_VECTOR_PRI));
16503669e930SJohannes Berg 		mpic->save_data[i].dest =
16513669e930SJohannes Berg 			mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION));
16523669e930SJohannes Berg 	}
16533669e930SJohannes Berg 
16543669e930SJohannes Berg 	return 0;
16553669e930SJohannes Berg }
16563669e930SJohannes Berg 
16573669e930SJohannes Berg static int mpic_resume(struct sys_device *dev)
16583669e930SJohannes Berg {
16593669e930SJohannes Berg 	struct mpic *mpic = container_of(dev, struct mpic, sysdev);
16603669e930SJohannes Berg 	int i;
16613669e930SJohannes Berg 
16623669e930SJohannes Berg 	for (i = 0; i < mpic->num_sources; i++) {
16633669e930SJohannes Berg 		mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI),
16643669e930SJohannes Berg 			       mpic->save_data[i].vecprio);
16653669e930SJohannes Berg 		mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
16663669e930SJohannes Berg 			       mpic->save_data[i].dest);
16673669e930SJohannes Berg 
16683669e930SJohannes Berg #ifdef CONFIG_MPIC_U3_HT_IRQS
16693669e930SJohannes Berg 	{
16703669e930SJohannes Berg 		struct mpic_irq_fixup *fixup = &mpic->fixups[i];
16713669e930SJohannes Berg 
16723669e930SJohannes Berg 		if (fixup->base) {
16733669e930SJohannes Berg 			/* we use the lowest bit in an inverted meaning */
16743669e930SJohannes Berg 			if ((mpic->save_data[i].fixup_data & 1) == 0)
16753669e930SJohannes Berg 				continue;
16763669e930SJohannes Berg 
16773669e930SJohannes Berg 			/* Enable and configure */
16783669e930SJohannes Berg 			writeb(0x10 + 2 * fixup->index, fixup->base + 2);
16793669e930SJohannes Berg 
16803669e930SJohannes Berg 			writel(mpic->save_data[i].fixup_data & ~1,
16813669e930SJohannes Berg 			       fixup->base + 4);
16823669e930SJohannes Berg 		}
16833669e930SJohannes Berg 	}
16843669e930SJohannes Berg #endif
16853669e930SJohannes Berg 	} /* end for loop */
16863669e930SJohannes Berg 
16873669e930SJohannes Berg 	return 0;
16883669e930SJohannes Berg }
16893669e930SJohannes Berg #endif
16903669e930SJohannes Berg 
16913669e930SJohannes Berg static struct sysdev_class mpic_sysclass = {
16923669e930SJohannes Berg #ifdef CONFIG_PM
16933669e930SJohannes Berg 	.resume = mpic_resume,
16943669e930SJohannes Berg 	.suspend = mpic_suspend,
16953669e930SJohannes Berg #endif
1696af5ca3f4SKay Sievers 	.name = "mpic",
16973669e930SJohannes Berg };
16983669e930SJohannes Berg 
16993669e930SJohannes Berg static int mpic_init_sys(void)
17003669e930SJohannes Berg {
17013669e930SJohannes Berg 	struct mpic *mpic = mpics;
17023669e930SJohannes Berg 	int error, id = 0;
17033669e930SJohannes Berg 
17043669e930SJohannes Berg 	error = sysdev_class_register(&mpic_sysclass);
17053669e930SJohannes Berg 
17063669e930SJohannes Berg 	while (mpic && !error) {
17073669e930SJohannes Berg 		mpic->sysdev.cls = &mpic_sysclass;
17083669e930SJohannes Berg 		mpic->sysdev.id = id++;
17093669e930SJohannes Berg 		error = sysdev_register(&mpic->sysdev);
17103669e930SJohannes Berg 		mpic = mpic->next;
17113669e930SJohannes Berg 	}
17123669e930SJohannes Berg 	return error;
17133669e930SJohannes Berg }
17143669e930SJohannes Berg 
17153669e930SJohannes Berg device_initcall(mpic_init_sys);
1716