xref: /linux/arch/powerpc/sysdev/mpic.c (revision 5bdb6f2e5833c1c3e5ea21a2050fe0fada3a4a1d)
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.
922d168ceSScott Wood  *  Copyright 2010-2011 Freescale Semiconductor, Inc.
1014cf11afSPaul Mackerras  *
1114cf11afSPaul Mackerras  *  This file is subject to the terms and conditions of the GNU General Public
1214cf11afSPaul Mackerras  *  License.  See the file COPYING in the main directory of this archive
1314cf11afSPaul Mackerras  *  for more details.
1414cf11afSPaul Mackerras  */
1514cf11afSPaul Mackerras 
1614cf11afSPaul Mackerras #undef DEBUG
171beb6a7dSBenjamin Herrenschmidt #undef DEBUG_IPI
181beb6a7dSBenjamin Herrenschmidt #undef DEBUG_IRQ
191beb6a7dSBenjamin Herrenschmidt #undef DEBUG_LOW
2014cf11afSPaul Mackerras 
2114cf11afSPaul Mackerras #include <linux/types.h>
2214cf11afSPaul Mackerras #include <linux/kernel.h>
2314cf11afSPaul Mackerras #include <linux/init.h>
2414cf11afSPaul Mackerras #include <linux/irq.h>
2514cf11afSPaul Mackerras #include <linux/smp.h>
2614cf11afSPaul Mackerras #include <linux/interrupt.h>
2714cf11afSPaul Mackerras #include <linux/bootmem.h>
2814cf11afSPaul Mackerras #include <linux/spinlock.h>
2914cf11afSPaul Mackerras #include <linux/pci.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31f5a592f7SRafael J. Wysocki #include <linux/syscore_ops.h>
3276462232SChristian Dietrich #include <linux/ratelimit.h>
3314cf11afSPaul Mackerras 
3414cf11afSPaul Mackerras #include <asm/ptrace.h>
3514cf11afSPaul Mackerras #include <asm/signal.h>
3614cf11afSPaul Mackerras #include <asm/io.h>
3714cf11afSPaul Mackerras #include <asm/pgtable.h>
3814cf11afSPaul Mackerras #include <asm/irq.h>
3914cf11afSPaul Mackerras #include <asm/machdep.h>
4014cf11afSPaul Mackerras #include <asm/mpic.h>
4114cf11afSPaul Mackerras #include <asm/smp.h>
4214cf11afSPaul Mackerras 
43a7de7c74SMichael Ellerman #include "mpic.h"
44a7de7c74SMichael Ellerman 
4514cf11afSPaul Mackerras #ifdef DEBUG
4614cf11afSPaul Mackerras #define DBG(fmt...) printk(fmt)
4714cf11afSPaul Mackerras #else
4814cf11afSPaul Mackerras #define DBG(fmt...)
4914cf11afSPaul Mackerras #endif
5014cf11afSPaul Mackerras 
5114cf11afSPaul Mackerras static struct mpic *mpics;
5214cf11afSPaul Mackerras static struct mpic *mpic_primary;
53203041adSThomas Gleixner static DEFINE_RAW_SPINLOCK(mpic_lock);
5414cf11afSPaul Mackerras 
55c0c0d996SPaul Mackerras #ifdef CONFIG_PPC32	/* XXX for now */
56e40c7f02SAndy Whitcroft #ifdef CONFIG_IRQ_ALL_CPUS
57e40c7f02SAndy Whitcroft #define distribute_irqs	(1)
58e40c7f02SAndy Whitcroft #else
59e40c7f02SAndy Whitcroft #define distribute_irqs	(0)
60e40c7f02SAndy Whitcroft #endif
61c0c0d996SPaul Mackerras #endif
6214cf11afSPaul Mackerras 
637233593bSZang Roy-r61911 #ifdef CONFIG_MPIC_WEIRD
647233593bSZang Roy-r61911 static u32 mpic_infos[][MPIC_IDX_END] = {
657233593bSZang Roy-r61911 	[0] = {	/* Original OpenPIC compatible MPIC */
667233593bSZang Roy-r61911 		MPIC_GREG_BASE,
677233593bSZang Roy-r61911 		MPIC_GREG_FEATURE_0,
687233593bSZang Roy-r61911 		MPIC_GREG_GLOBAL_CONF_0,
697233593bSZang Roy-r61911 		MPIC_GREG_VENDOR_ID,
707233593bSZang Roy-r61911 		MPIC_GREG_IPI_VECTOR_PRI_0,
717233593bSZang Roy-r61911 		MPIC_GREG_IPI_STRIDE,
727233593bSZang Roy-r61911 		MPIC_GREG_SPURIOUS,
737233593bSZang Roy-r61911 		MPIC_GREG_TIMER_FREQ,
747233593bSZang Roy-r61911 
757233593bSZang Roy-r61911 		MPIC_TIMER_BASE,
767233593bSZang Roy-r61911 		MPIC_TIMER_STRIDE,
777233593bSZang Roy-r61911 		MPIC_TIMER_CURRENT_CNT,
787233593bSZang Roy-r61911 		MPIC_TIMER_BASE_CNT,
797233593bSZang Roy-r61911 		MPIC_TIMER_VECTOR_PRI,
807233593bSZang Roy-r61911 		MPIC_TIMER_DESTINATION,
817233593bSZang Roy-r61911 
827233593bSZang Roy-r61911 		MPIC_CPU_BASE,
837233593bSZang Roy-r61911 		MPIC_CPU_STRIDE,
847233593bSZang Roy-r61911 		MPIC_CPU_IPI_DISPATCH_0,
857233593bSZang Roy-r61911 		MPIC_CPU_IPI_DISPATCH_STRIDE,
867233593bSZang Roy-r61911 		MPIC_CPU_CURRENT_TASK_PRI,
877233593bSZang Roy-r61911 		MPIC_CPU_WHOAMI,
887233593bSZang Roy-r61911 		MPIC_CPU_INTACK,
897233593bSZang Roy-r61911 		MPIC_CPU_EOI,
90f365355eSOlof Johansson 		MPIC_CPU_MCACK,
917233593bSZang Roy-r61911 
927233593bSZang Roy-r61911 		MPIC_IRQ_BASE,
937233593bSZang Roy-r61911 		MPIC_IRQ_STRIDE,
947233593bSZang Roy-r61911 		MPIC_IRQ_VECTOR_PRI,
957233593bSZang Roy-r61911 		MPIC_VECPRI_VECTOR_MASK,
967233593bSZang Roy-r61911 		MPIC_VECPRI_POLARITY_POSITIVE,
977233593bSZang Roy-r61911 		MPIC_VECPRI_POLARITY_NEGATIVE,
987233593bSZang Roy-r61911 		MPIC_VECPRI_SENSE_LEVEL,
997233593bSZang Roy-r61911 		MPIC_VECPRI_SENSE_EDGE,
1007233593bSZang Roy-r61911 		MPIC_VECPRI_POLARITY_MASK,
1017233593bSZang Roy-r61911 		MPIC_VECPRI_SENSE_MASK,
1027233593bSZang Roy-r61911 		MPIC_IRQ_DESTINATION
1037233593bSZang Roy-r61911 	},
1047233593bSZang Roy-r61911 	[1] = {	/* Tsi108/109 PIC */
1057233593bSZang Roy-r61911 		TSI108_GREG_BASE,
1067233593bSZang Roy-r61911 		TSI108_GREG_FEATURE_0,
1077233593bSZang Roy-r61911 		TSI108_GREG_GLOBAL_CONF_0,
1087233593bSZang Roy-r61911 		TSI108_GREG_VENDOR_ID,
1097233593bSZang Roy-r61911 		TSI108_GREG_IPI_VECTOR_PRI_0,
1107233593bSZang Roy-r61911 		TSI108_GREG_IPI_STRIDE,
1117233593bSZang Roy-r61911 		TSI108_GREG_SPURIOUS,
1127233593bSZang Roy-r61911 		TSI108_GREG_TIMER_FREQ,
1137233593bSZang Roy-r61911 
1147233593bSZang Roy-r61911 		TSI108_TIMER_BASE,
1157233593bSZang Roy-r61911 		TSI108_TIMER_STRIDE,
1167233593bSZang Roy-r61911 		TSI108_TIMER_CURRENT_CNT,
1177233593bSZang Roy-r61911 		TSI108_TIMER_BASE_CNT,
1187233593bSZang Roy-r61911 		TSI108_TIMER_VECTOR_PRI,
1197233593bSZang Roy-r61911 		TSI108_TIMER_DESTINATION,
1207233593bSZang Roy-r61911 
1217233593bSZang Roy-r61911 		TSI108_CPU_BASE,
1227233593bSZang Roy-r61911 		TSI108_CPU_STRIDE,
1237233593bSZang Roy-r61911 		TSI108_CPU_IPI_DISPATCH_0,
1247233593bSZang Roy-r61911 		TSI108_CPU_IPI_DISPATCH_STRIDE,
1257233593bSZang Roy-r61911 		TSI108_CPU_CURRENT_TASK_PRI,
1267233593bSZang Roy-r61911 		TSI108_CPU_WHOAMI,
1277233593bSZang Roy-r61911 		TSI108_CPU_INTACK,
1287233593bSZang Roy-r61911 		TSI108_CPU_EOI,
129f365355eSOlof Johansson 		TSI108_CPU_MCACK,
1307233593bSZang Roy-r61911 
1317233593bSZang Roy-r61911 		TSI108_IRQ_BASE,
1327233593bSZang Roy-r61911 		TSI108_IRQ_STRIDE,
1337233593bSZang Roy-r61911 		TSI108_IRQ_VECTOR_PRI,
1347233593bSZang Roy-r61911 		TSI108_VECPRI_VECTOR_MASK,
1357233593bSZang Roy-r61911 		TSI108_VECPRI_POLARITY_POSITIVE,
1367233593bSZang Roy-r61911 		TSI108_VECPRI_POLARITY_NEGATIVE,
1377233593bSZang Roy-r61911 		TSI108_VECPRI_SENSE_LEVEL,
1387233593bSZang Roy-r61911 		TSI108_VECPRI_SENSE_EDGE,
1397233593bSZang Roy-r61911 		TSI108_VECPRI_POLARITY_MASK,
1407233593bSZang Roy-r61911 		TSI108_VECPRI_SENSE_MASK,
1417233593bSZang Roy-r61911 		TSI108_IRQ_DESTINATION
1427233593bSZang Roy-r61911 	},
1437233593bSZang Roy-r61911 };
1447233593bSZang Roy-r61911 
1457233593bSZang Roy-r61911 #define MPIC_INFO(name) mpic->hw_set[MPIC_IDX_##name]
1467233593bSZang Roy-r61911 
1477233593bSZang Roy-r61911 #else /* CONFIG_MPIC_WEIRD */
1487233593bSZang Roy-r61911 
1497233593bSZang Roy-r61911 #define MPIC_INFO(name) MPIC_##name
1507233593bSZang Roy-r61911 
1517233593bSZang Roy-r61911 #endif /* CONFIG_MPIC_WEIRD */
1527233593bSZang Roy-r61911 
153d6a2639bSMeador Inge static inline unsigned int mpic_processor_id(struct mpic *mpic)
154d6a2639bSMeador Inge {
155d6a2639bSMeador Inge 	unsigned int cpu = 0;
156d6a2639bSMeador Inge 
157d6a2639bSMeador Inge 	if (mpic->flags & MPIC_PRIMARY)
158d6a2639bSMeador Inge 		cpu = hard_smp_processor_id();
159d6a2639bSMeador Inge 
160d6a2639bSMeador Inge 	return cpu;
161d6a2639bSMeador Inge }
162d6a2639bSMeador Inge 
16314cf11afSPaul Mackerras /*
16414cf11afSPaul Mackerras  * Register accessor functions
16514cf11afSPaul Mackerras  */
16614cf11afSPaul Mackerras 
16714cf11afSPaul Mackerras 
168fbf0274eSBenjamin Herrenschmidt static inline u32 _mpic_read(enum mpic_reg_type type,
169fbf0274eSBenjamin Herrenschmidt 			     struct mpic_reg_bank *rb,
17014cf11afSPaul Mackerras 			     unsigned int reg)
17114cf11afSPaul Mackerras {
172fbf0274eSBenjamin Herrenschmidt 	switch(type) {
173fbf0274eSBenjamin Herrenschmidt #ifdef CONFIG_PPC_DCR
174fbf0274eSBenjamin Herrenschmidt 	case mpic_access_dcr:
17583f34df4SMichael Ellerman 		return dcr_read(rb->dhost, reg);
176fbf0274eSBenjamin Herrenschmidt #endif
177fbf0274eSBenjamin Herrenschmidt 	case mpic_access_mmio_be:
178fbf0274eSBenjamin Herrenschmidt 		return in_be32(rb->base + (reg >> 2));
179fbf0274eSBenjamin Herrenschmidt 	case mpic_access_mmio_le:
180fbf0274eSBenjamin Herrenschmidt 	default:
181fbf0274eSBenjamin Herrenschmidt 		return in_le32(rb->base + (reg >> 2));
182fbf0274eSBenjamin Herrenschmidt 	}
18314cf11afSPaul Mackerras }
18414cf11afSPaul Mackerras 
185fbf0274eSBenjamin Herrenschmidt static inline void _mpic_write(enum mpic_reg_type type,
186fbf0274eSBenjamin Herrenschmidt 			       struct mpic_reg_bank *rb,
18714cf11afSPaul Mackerras  			       unsigned int reg, u32 value)
18814cf11afSPaul Mackerras {
189fbf0274eSBenjamin Herrenschmidt 	switch(type) {
190fbf0274eSBenjamin Herrenschmidt #ifdef CONFIG_PPC_DCR
191fbf0274eSBenjamin Herrenschmidt 	case mpic_access_dcr:
192d9d1063dSJohannes Berg 		dcr_write(rb->dhost, reg, value);
193d9d1063dSJohannes Berg 		break;
194fbf0274eSBenjamin Herrenschmidt #endif
195fbf0274eSBenjamin Herrenschmidt 	case mpic_access_mmio_be:
196d9d1063dSJohannes Berg 		out_be32(rb->base + (reg >> 2), value);
197d9d1063dSJohannes Berg 		break;
198fbf0274eSBenjamin Herrenschmidt 	case mpic_access_mmio_le:
199fbf0274eSBenjamin Herrenschmidt 	default:
200d9d1063dSJohannes Berg 		out_le32(rb->base + (reg >> 2), value);
201d9d1063dSJohannes Berg 		break;
202fbf0274eSBenjamin Herrenschmidt 	}
20314cf11afSPaul Mackerras }
20414cf11afSPaul Mackerras 
20514cf11afSPaul Mackerras static inline u32 _mpic_ipi_read(struct mpic *mpic, unsigned int ipi)
20614cf11afSPaul Mackerras {
207fbf0274eSBenjamin Herrenschmidt 	enum mpic_reg_type type = mpic->reg_type;
2087233593bSZang Roy-r61911 	unsigned int offset = MPIC_INFO(GREG_IPI_VECTOR_PRI_0) +
2097233593bSZang Roy-r61911 			      (ipi * MPIC_INFO(GREG_IPI_STRIDE));
21014cf11afSPaul Mackerras 
211fbf0274eSBenjamin Herrenschmidt 	if ((mpic->flags & MPIC_BROKEN_IPI) && type == mpic_access_mmio_le)
212fbf0274eSBenjamin Herrenschmidt 		type = mpic_access_mmio_be;
213fbf0274eSBenjamin Herrenschmidt 	return _mpic_read(type, &mpic->gregs, offset);
21414cf11afSPaul Mackerras }
21514cf11afSPaul Mackerras 
21614cf11afSPaul Mackerras static inline void _mpic_ipi_write(struct mpic *mpic, unsigned int ipi, u32 value)
21714cf11afSPaul Mackerras {
2187233593bSZang Roy-r61911 	unsigned int offset = MPIC_INFO(GREG_IPI_VECTOR_PRI_0) +
2197233593bSZang Roy-r61911 			      (ipi * MPIC_INFO(GREG_IPI_STRIDE));
22014cf11afSPaul Mackerras 
221fbf0274eSBenjamin Herrenschmidt 	_mpic_write(mpic->reg_type, &mpic->gregs, offset, value);
22214cf11afSPaul Mackerras }
22314cf11afSPaul Mackerras 
224ea94187fSScott Wood static inline u32 _mpic_tm_read(struct mpic *mpic, unsigned int tm)
225ea94187fSScott Wood {
226ea94187fSScott Wood 	unsigned int offset = MPIC_INFO(TIMER_VECTOR_PRI) +
227ea94187fSScott Wood 			      ((tm & 3) * MPIC_INFO(TIMER_STRIDE));
228ea94187fSScott Wood 
229ea94187fSScott Wood 	if (tm >= 4)
230ea94187fSScott Wood 		offset += 0x1000 / 4;
231ea94187fSScott Wood 
232ea94187fSScott Wood 	return _mpic_read(mpic->reg_type, &mpic->tmregs, offset);
233ea94187fSScott Wood }
234ea94187fSScott Wood 
235ea94187fSScott Wood static inline void _mpic_tm_write(struct mpic *mpic, unsigned int tm, u32 value)
236ea94187fSScott Wood {
237ea94187fSScott Wood 	unsigned int offset = MPIC_INFO(TIMER_VECTOR_PRI) +
238ea94187fSScott Wood 			      ((tm & 3) * MPIC_INFO(TIMER_STRIDE));
239ea94187fSScott Wood 
240ea94187fSScott Wood 	if (tm >= 4)
241ea94187fSScott Wood 		offset += 0x1000 / 4;
242ea94187fSScott Wood 
243ea94187fSScott Wood 	_mpic_write(mpic->reg_type, &mpic->tmregs, offset, value);
244ea94187fSScott Wood }
245ea94187fSScott Wood 
24614cf11afSPaul Mackerras static inline u32 _mpic_cpu_read(struct mpic *mpic, unsigned int reg)
24714cf11afSPaul Mackerras {
248d6a2639bSMeador Inge 	unsigned int cpu = mpic_processor_id(mpic);
24914cf11afSPaul Mackerras 
250fbf0274eSBenjamin Herrenschmidt 	return _mpic_read(mpic->reg_type, &mpic->cpuregs[cpu], reg);
25114cf11afSPaul Mackerras }
25214cf11afSPaul Mackerras 
25314cf11afSPaul Mackerras static inline void _mpic_cpu_write(struct mpic *mpic, unsigned int reg, u32 value)
25414cf11afSPaul Mackerras {
255d6a2639bSMeador Inge 	unsigned int cpu = mpic_processor_id(mpic);
25614cf11afSPaul Mackerras 
257fbf0274eSBenjamin Herrenschmidt 	_mpic_write(mpic->reg_type, &mpic->cpuregs[cpu], reg, value);
25814cf11afSPaul Mackerras }
25914cf11afSPaul Mackerras 
26014cf11afSPaul Mackerras static inline u32 _mpic_irq_read(struct mpic *mpic, unsigned int src_no, unsigned int reg)
26114cf11afSPaul Mackerras {
26214cf11afSPaul Mackerras 	unsigned int	isu = src_no >> mpic->isu_shift;
26314cf11afSPaul Mackerras 	unsigned int	idx = src_no & mpic->isu_mask;
26411a6b292SMichael Ellerman 	unsigned int	val;
26514cf11afSPaul Mackerras 
26611a6b292SMichael Ellerman 	val = _mpic_read(mpic->reg_type, &mpic->isus[isu],
26711a6b292SMichael Ellerman 			 reg + (idx * MPIC_INFO(IRQ_STRIDE)));
2680d72ba93SOlof Johansson #ifdef CONFIG_MPIC_BROKEN_REGREAD
2690d72ba93SOlof Johansson 	if (reg == 0)
27011a6b292SMichael Ellerman 		val = (val & (MPIC_VECPRI_MASK | MPIC_VECPRI_ACTIVITY)) |
27111a6b292SMichael Ellerman 			mpic->isu_reg0_shadow[src_no];
2720d72ba93SOlof Johansson #endif
27311a6b292SMichael Ellerman 	return val;
27414cf11afSPaul Mackerras }
27514cf11afSPaul Mackerras 
27614cf11afSPaul Mackerras static inline void _mpic_irq_write(struct mpic *mpic, unsigned int src_no,
27714cf11afSPaul Mackerras 				   unsigned int reg, u32 value)
27814cf11afSPaul Mackerras {
27914cf11afSPaul Mackerras 	unsigned int	isu = src_no >> mpic->isu_shift;
28014cf11afSPaul Mackerras 	unsigned int	idx = src_no & mpic->isu_mask;
28114cf11afSPaul Mackerras 
282fbf0274eSBenjamin Herrenschmidt 	_mpic_write(mpic->reg_type, &mpic->isus[isu],
2837233593bSZang Roy-r61911 		    reg + (idx * MPIC_INFO(IRQ_STRIDE)), value);
2840d72ba93SOlof Johansson 
2850d72ba93SOlof Johansson #ifdef CONFIG_MPIC_BROKEN_REGREAD
2860d72ba93SOlof Johansson 	if (reg == 0)
28711a6b292SMichael Ellerman 		mpic->isu_reg0_shadow[src_no] =
28811a6b292SMichael Ellerman 			value & ~(MPIC_VECPRI_MASK | MPIC_VECPRI_ACTIVITY);
2890d72ba93SOlof Johansson #endif
29014cf11afSPaul Mackerras }
29114cf11afSPaul Mackerras 
292fbf0274eSBenjamin Herrenschmidt #define mpic_read(b,r)		_mpic_read(mpic->reg_type,&(b),(r))
293fbf0274eSBenjamin Herrenschmidt #define mpic_write(b,r,v)	_mpic_write(mpic->reg_type,&(b),(r),(v))
29414cf11afSPaul Mackerras #define mpic_ipi_read(i)	_mpic_ipi_read(mpic,(i))
29514cf11afSPaul Mackerras #define mpic_ipi_write(i,v)	_mpic_ipi_write(mpic,(i),(v))
296ea94187fSScott Wood #define mpic_tm_read(i)		_mpic_tm_read(mpic,(i))
297ea94187fSScott Wood #define mpic_tm_write(i,v)	_mpic_tm_write(mpic,(i),(v))
29814cf11afSPaul Mackerras #define mpic_cpu_read(i)	_mpic_cpu_read(mpic,(i))
29914cf11afSPaul Mackerras #define mpic_cpu_write(i,v)	_mpic_cpu_write(mpic,(i),(v))
30014cf11afSPaul Mackerras #define mpic_irq_read(s,r)	_mpic_irq_read(mpic,(s),(r))
30114cf11afSPaul Mackerras #define mpic_irq_write(s,r,v)	_mpic_irq_write(mpic,(s),(r),(v))
30214cf11afSPaul Mackerras 
30314cf11afSPaul Mackerras 
30414cf11afSPaul Mackerras /*
30514cf11afSPaul Mackerras  * Low level utility functions
30614cf11afSPaul Mackerras  */
30714cf11afSPaul Mackerras 
30814cf11afSPaul Mackerras 
309c51a3fdcSBecky Bruce static void _mpic_map_mmio(struct mpic *mpic, phys_addr_t phys_addr,
310fbf0274eSBenjamin Herrenschmidt 			   struct mpic_reg_bank *rb, unsigned int offset,
311fbf0274eSBenjamin Herrenschmidt 			   unsigned int size)
312fbf0274eSBenjamin Herrenschmidt {
313fbf0274eSBenjamin Herrenschmidt 	rb->base = ioremap(phys_addr + offset, size);
314fbf0274eSBenjamin Herrenschmidt 	BUG_ON(rb->base == NULL);
315fbf0274eSBenjamin Herrenschmidt }
316fbf0274eSBenjamin Herrenschmidt 
317fbf0274eSBenjamin Herrenschmidt #ifdef CONFIG_PPC_DCR
3185a2642f6SBenjamin Herrenschmidt static void _mpic_map_dcr(struct mpic *mpic, struct device_node *node,
3195a2642f6SBenjamin Herrenschmidt 			  struct mpic_reg_bank *rb,
320fbf0274eSBenjamin Herrenschmidt 			  unsigned int offset, unsigned int size)
321fbf0274eSBenjamin Herrenschmidt {
3220411a5e2SMichael Ellerman 	const u32 *dbasep;
3230411a5e2SMichael Ellerman 
3245a2642f6SBenjamin Herrenschmidt 	dbasep = of_get_property(node, "dcr-reg", NULL);
3250411a5e2SMichael Ellerman 
3265a2642f6SBenjamin Herrenschmidt 	rb->dhost = dcr_map(node, *dbasep + offset, size);
327fbf0274eSBenjamin Herrenschmidt 	BUG_ON(!DCR_MAP_OK(rb->dhost));
328fbf0274eSBenjamin Herrenschmidt }
329fbf0274eSBenjamin Herrenschmidt 
3305a2642f6SBenjamin Herrenschmidt static inline void mpic_map(struct mpic *mpic, struct device_node *node,
3315a2642f6SBenjamin Herrenschmidt 			    phys_addr_t phys_addr, struct mpic_reg_bank *rb,
3325a2642f6SBenjamin Herrenschmidt 			    unsigned int offset, unsigned int size)
333fbf0274eSBenjamin Herrenschmidt {
334fbf0274eSBenjamin Herrenschmidt 	if (mpic->flags & MPIC_USES_DCR)
3355a2642f6SBenjamin Herrenschmidt 		_mpic_map_dcr(mpic, node, rb, offset, size);
336fbf0274eSBenjamin Herrenschmidt 	else
337fbf0274eSBenjamin Herrenschmidt 		_mpic_map_mmio(mpic, phys_addr, rb, offset, size);
338fbf0274eSBenjamin Herrenschmidt }
339fbf0274eSBenjamin Herrenschmidt #else /* CONFIG_PPC_DCR */
3405a2642f6SBenjamin Herrenschmidt #define mpic_map(m,n,p,b,o,s)	_mpic_map_mmio(m,p,b,o,s)
341fbf0274eSBenjamin Herrenschmidt #endif /* !CONFIG_PPC_DCR */
342fbf0274eSBenjamin Herrenschmidt 
343fbf0274eSBenjamin Herrenschmidt 
34414cf11afSPaul Mackerras 
34514cf11afSPaul Mackerras /* Check if we have one of those nice broken MPICs with a flipped endian on
34614cf11afSPaul Mackerras  * reads from IPI registers
34714cf11afSPaul Mackerras  */
34814cf11afSPaul Mackerras static void __init mpic_test_broken_ipi(struct mpic *mpic)
34914cf11afSPaul Mackerras {
35014cf11afSPaul Mackerras 	u32 r;
35114cf11afSPaul Mackerras 
3527233593bSZang Roy-r61911 	mpic_write(mpic->gregs, MPIC_INFO(GREG_IPI_VECTOR_PRI_0), MPIC_VECPRI_MASK);
3537233593bSZang Roy-r61911 	r = mpic_read(mpic->gregs, MPIC_INFO(GREG_IPI_VECTOR_PRI_0));
35414cf11afSPaul Mackerras 
35514cf11afSPaul Mackerras 	if (r == le32_to_cpu(MPIC_VECPRI_MASK)) {
35614cf11afSPaul Mackerras 		printk(KERN_INFO "mpic: Detected reversed IPI registers\n");
35714cf11afSPaul Mackerras 		mpic->flags |= MPIC_BROKEN_IPI;
35814cf11afSPaul Mackerras 	}
35914cf11afSPaul Mackerras }
36014cf11afSPaul Mackerras 
3616cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
36214cf11afSPaul Mackerras 
36314cf11afSPaul Mackerras /* Test if an interrupt is sourced from HyperTransport (used on broken U3s)
36414cf11afSPaul Mackerras  * to force the edge setting on the MPIC and do the ack workaround.
36514cf11afSPaul Mackerras  */
3661beb6a7dSBenjamin Herrenschmidt static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
36714cf11afSPaul Mackerras {
3681beb6a7dSBenjamin Herrenschmidt 	if (source >= 128 || !mpic->fixups)
36914cf11afSPaul Mackerras 		return 0;
3701beb6a7dSBenjamin Herrenschmidt 	return mpic->fixups[source].base != NULL;
37114cf11afSPaul Mackerras }
37214cf11afSPaul Mackerras 
373c4b22f26SSegher Boessenkool 
3741beb6a7dSBenjamin Herrenschmidt static inline void mpic_ht_end_irq(struct mpic *mpic, unsigned int source)
37514cf11afSPaul Mackerras {
3761beb6a7dSBenjamin Herrenschmidt 	struct mpic_irq_fixup *fixup = &mpic->fixups[source];
37714cf11afSPaul Mackerras 
3781beb6a7dSBenjamin Herrenschmidt 	if (fixup->applebase) {
3791beb6a7dSBenjamin Herrenschmidt 		unsigned int soff = (fixup->index >> 3) & ~3;
3801beb6a7dSBenjamin Herrenschmidt 		unsigned int mask = 1U << (fixup->index & 0x1f);
3811beb6a7dSBenjamin Herrenschmidt 		writel(mask, fixup->applebase + soff);
3821beb6a7dSBenjamin Herrenschmidt 	} else {
383203041adSThomas Gleixner 		raw_spin_lock(&mpic->fixup_lock);
3841beb6a7dSBenjamin Herrenschmidt 		writeb(0x11 + 2 * fixup->index, fixup->base + 2);
385c4b22f26SSegher Boessenkool 		writel(fixup->data, fixup->base + 4);
386203041adSThomas Gleixner 		raw_spin_unlock(&mpic->fixup_lock);
38714cf11afSPaul Mackerras 	}
3881beb6a7dSBenjamin Herrenschmidt }
38914cf11afSPaul Mackerras 
3901beb6a7dSBenjamin Herrenschmidt static void mpic_startup_ht_interrupt(struct mpic *mpic, unsigned int source,
39124a3f2e8SThomas Gleixner 				      bool level)
3921beb6a7dSBenjamin Herrenschmidt {
3931beb6a7dSBenjamin Herrenschmidt 	struct mpic_irq_fixup *fixup = &mpic->fixups[source];
3941beb6a7dSBenjamin Herrenschmidt 	unsigned long flags;
3951beb6a7dSBenjamin Herrenschmidt 	u32 tmp;
39614cf11afSPaul Mackerras 
3971beb6a7dSBenjamin Herrenschmidt 	if (fixup->base == NULL)
3981beb6a7dSBenjamin Herrenschmidt 		return;
3991beb6a7dSBenjamin Herrenschmidt 
40024a3f2e8SThomas Gleixner 	DBG("startup_ht_interrupt(0x%x) index: %d\n",
40124a3f2e8SThomas Gleixner 	    source, fixup->index);
402203041adSThomas Gleixner 	raw_spin_lock_irqsave(&mpic->fixup_lock, flags);
4031beb6a7dSBenjamin Herrenschmidt 	/* Enable and configure */
4041beb6a7dSBenjamin Herrenschmidt 	writeb(0x10 + 2 * fixup->index, fixup->base + 2);
4051beb6a7dSBenjamin Herrenschmidt 	tmp = readl(fixup->base + 4);
4061beb6a7dSBenjamin Herrenschmidt 	tmp &= ~(0x23U);
40724a3f2e8SThomas Gleixner 	if (level)
4081beb6a7dSBenjamin Herrenschmidt 		tmp |= 0x22;
4091beb6a7dSBenjamin Herrenschmidt 	writel(tmp, fixup->base + 4);
410203041adSThomas Gleixner 	raw_spin_unlock_irqrestore(&mpic->fixup_lock, flags);
4113669e930SJohannes Berg 
4123669e930SJohannes Berg #ifdef CONFIG_PM
4133669e930SJohannes Berg 	/* use the lowest bit inverted to the actual HW,
4143669e930SJohannes Berg 	 * set if this fixup was enabled, clear otherwise */
4153669e930SJohannes Berg 	mpic->save_data[source].fixup_data = tmp | 1;
4163669e930SJohannes Berg #endif
4171beb6a7dSBenjamin Herrenschmidt }
4181beb6a7dSBenjamin Herrenschmidt 
41924a3f2e8SThomas Gleixner static void mpic_shutdown_ht_interrupt(struct mpic *mpic, unsigned int source)
4201beb6a7dSBenjamin Herrenschmidt {
4211beb6a7dSBenjamin Herrenschmidt 	struct mpic_irq_fixup *fixup = &mpic->fixups[source];
4221beb6a7dSBenjamin Herrenschmidt 	unsigned long flags;
4231beb6a7dSBenjamin Herrenschmidt 	u32 tmp;
4241beb6a7dSBenjamin Herrenschmidt 
4251beb6a7dSBenjamin Herrenschmidt 	if (fixup->base == NULL)
4261beb6a7dSBenjamin Herrenschmidt 		return;
4271beb6a7dSBenjamin Herrenschmidt 
42824a3f2e8SThomas Gleixner 	DBG("shutdown_ht_interrupt(0x%x)\n", source);
4291beb6a7dSBenjamin Herrenschmidt 
4301beb6a7dSBenjamin Herrenschmidt 	/* Disable */
431203041adSThomas Gleixner 	raw_spin_lock_irqsave(&mpic->fixup_lock, flags);
4321beb6a7dSBenjamin Herrenschmidt 	writeb(0x10 + 2 * fixup->index, fixup->base + 2);
4331beb6a7dSBenjamin Herrenschmidt 	tmp = readl(fixup->base + 4);
43472b13819SSegher Boessenkool 	tmp |= 1;
4351beb6a7dSBenjamin Herrenschmidt 	writel(tmp, fixup->base + 4);
436203041adSThomas Gleixner 	raw_spin_unlock_irqrestore(&mpic->fixup_lock, flags);
4373669e930SJohannes Berg 
4383669e930SJohannes Berg #ifdef CONFIG_PM
4393669e930SJohannes Berg 	/* use the lowest bit inverted to the actual HW,
4403669e930SJohannes Berg 	 * set if this fixup was enabled, clear otherwise */
4413669e930SJohannes Berg 	mpic->save_data[source].fixup_data = tmp & ~1;
4423669e930SJohannes Berg #endif
4431beb6a7dSBenjamin Herrenschmidt }
4441beb6a7dSBenjamin Herrenschmidt 
445812fd1fdSMichael Ellerman #ifdef CONFIG_PCI_MSI
446812fd1fdSMichael Ellerman static void __init mpic_scan_ht_msi(struct mpic *mpic, u8 __iomem *devbase,
447812fd1fdSMichael Ellerman 				    unsigned int devfn)
448812fd1fdSMichael Ellerman {
449812fd1fdSMichael Ellerman 	u8 __iomem *base;
450812fd1fdSMichael Ellerman 	u8 pos, flags;
451812fd1fdSMichael Ellerman 	u64 addr = 0;
452812fd1fdSMichael Ellerman 
453812fd1fdSMichael Ellerman 	for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
454812fd1fdSMichael Ellerman 	     pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
455812fd1fdSMichael Ellerman 		u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
456812fd1fdSMichael Ellerman 		if (id == PCI_CAP_ID_HT) {
457812fd1fdSMichael Ellerman 			id = readb(devbase + pos + 3);
458812fd1fdSMichael Ellerman 			if ((id & HT_5BIT_CAP_MASK) == HT_CAPTYPE_MSI_MAPPING)
459812fd1fdSMichael Ellerman 				break;
460812fd1fdSMichael Ellerman 		}
461812fd1fdSMichael Ellerman 	}
462812fd1fdSMichael Ellerman 
463812fd1fdSMichael Ellerman 	if (pos == 0)
464812fd1fdSMichael Ellerman 		return;
465812fd1fdSMichael Ellerman 
466812fd1fdSMichael Ellerman 	base = devbase + pos;
467812fd1fdSMichael Ellerman 
468812fd1fdSMichael Ellerman 	flags = readb(base + HT_MSI_FLAGS);
469812fd1fdSMichael Ellerman 	if (!(flags & HT_MSI_FLAGS_FIXED)) {
470812fd1fdSMichael Ellerman 		addr = readl(base + HT_MSI_ADDR_LO) & HT_MSI_ADDR_LO_MASK;
471812fd1fdSMichael Ellerman 		addr = addr | ((u64)readl(base + HT_MSI_ADDR_HI) << 32);
472812fd1fdSMichael Ellerman 	}
473812fd1fdSMichael Ellerman 
474fe333321SIngo Molnar 	printk(KERN_DEBUG "mpic:   - HT:%02x.%x %s MSI mapping found @ 0x%llx\n",
475812fd1fdSMichael Ellerman 		PCI_SLOT(devfn), PCI_FUNC(devfn),
476812fd1fdSMichael Ellerman 		flags & HT_MSI_FLAGS_ENABLE ? "enabled" : "disabled", addr);
477812fd1fdSMichael Ellerman 
478812fd1fdSMichael Ellerman 	if (!(flags & HT_MSI_FLAGS_ENABLE))
479812fd1fdSMichael Ellerman 		writeb(flags | HT_MSI_FLAGS_ENABLE, base + HT_MSI_FLAGS);
480812fd1fdSMichael Ellerman }
481812fd1fdSMichael Ellerman #else
482812fd1fdSMichael Ellerman static void __init mpic_scan_ht_msi(struct mpic *mpic, u8 __iomem *devbase,
483812fd1fdSMichael Ellerman 				    unsigned int devfn)
484812fd1fdSMichael Ellerman {
485812fd1fdSMichael Ellerman 	return;
486812fd1fdSMichael Ellerman }
487812fd1fdSMichael Ellerman #endif
488812fd1fdSMichael Ellerman 
4891beb6a7dSBenjamin Herrenschmidt static void __init mpic_scan_ht_pic(struct mpic *mpic, u8 __iomem *devbase,
4901beb6a7dSBenjamin Herrenschmidt 				    unsigned int devfn, u32 vdid)
49114cf11afSPaul Mackerras {
492c4b22f26SSegher Boessenkool 	int i, irq, n;
4931beb6a7dSBenjamin Herrenschmidt 	u8 __iomem *base;
49414cf11afSPaul Mackerras 	u32 tmp;
495c4b22f26SSegher Boessenkool 	u8 pos;
49614cf11afSPaul Mackerras 
4971beb6a7dSBenjamin Herrenschmidt 	for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
4981beb6a7dSBenjamin Herrenschmidt 	     pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
4991beb6a7dSBenjamin Herrenschmidt 		u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
50046ff3463SBrice Goglin 		if (id == PCI_CAP_ID_HT) {
501c4b22f26SSegher Boessenkool 			id = readb(devbase + pos + 3);
502beb7cc82SMichael Ellerman 			if ((id & HT_5BIT_CAP_MASK) == HT_CAPTYPE_IRQ)
503c4b22f26SSegher Boessenkool 				break;
504c4b22f26SSegher Boessenkool 		}
505c4b22f26SSegher Boessenkool 	}
506c4b22f26SSegher Boessenkool 	if (pos == 0)
507c4b22f26SSegher Boessenkool 		return;
508c4b22f26SSegher Boessenkool 
5091beb6a7dSBenjamin Herrenschmidt 	base = devbase + pos;
5101beb6a7dSBenjamin Herrenschmidt 	writeb(0x01, base + 2);
5111beb6a7dSBenjamin Herrenschmidt 	n = (readl(base + 4) >> 16) & 0xff;
512c4b22f26SSegher Boessenkool 
5131beb6a7dSBenjamin Herrenschmidt 	printk(KERN_INFO "mpic:   - HT:%02x.%x [0x%02x] vendor %04x device %04x"
5141beb6a7dSBenjamin Herrenschmidt 	       " has %d irqs\n",
5151beb6a7dSBenjamin Herrenschmidt 	       devfn >> 3, devfn & 0x7, pos, vdid & 0xffff, vdid >> 16, n + 1);
516c4b22f26SSegher Boessenkool 
517c4b22f26SSegher Boessenkool 	for (i = 0; i <= n; i++) {
5181beb6a7dSBenjamin Herrenschmidt 		writeb(0x10 + 2 * i, base + 2);
5191beb6a7dSBenjamin Herrenschmidt 		tmp = readl(base + 4);
52014cf11afSPaul Mackerras 		irq = (tmp >> 16) & 0xff;
5211beb6a7dSBenjamin Herrenschmidt 		DBG("HT PIC index 0x%x, irq 0x%x, tmp: %08x\n", i, irq, tmp);
5221beb6a7dSBenjamin Herrenschmidt 		/* mask it , will be unmasked later */
5231beb6a7dSBenjamin Herrenschmidt 		tmp |= 0x1;
5241beb6a7dSBenjamin Herrenschmidt 		writel(tmp, base + 4);
5251beb6a7dSBenjamin Herrenschmidt 		mpic->fixups[irq].index = i;
5261beb6a7dSBenjamin Herrenschmidt 		mpic->fixups[irq].base = base;
5271beb6a7dSBenjamin Herrenschmidt 		/* Apple HT PIC has a non-standard way of doing EOIs */
5281beb6a7dSBenjamin Herrenschmidt 		if ((vdid & 0xffff) == 0x106b)
5291beb6a7dSBenjamin Herrenschmidt 			mpic->fixups[irq].applebase = devbase + 0x60;
5301beb6a7dSBenjamin Herrenschmidt 		else
5311beb6a7dSBenjamin Herrenschmidt 			mpic->fixups[irq].applebase = NULL;
5321beb6a7dSBenjamin Herrenschmidt 		writeb(0x11 + 2 * i, base + 2);
5331beb6a7dSBenjamin Herrenschmidt 		mpic->fixups[irq].data = readl(base + 4) | 0x80000000;
53414cf11afSPaul Mackerras 	}
53514cf11afSPaul Mackerras }
53614cf11afSPaul Mackerras 
53714cf11afSPaul Mackerras 
5381beb6a7dSBenjamin Herrenschmidt static void __init mpic_scan_ht_pics(struct mpic *mpic)
53914cf11afSPaul Mackerras {
54014cf11afSPaul Mackerras 	unsigned int devfn;
54114cf11afSPaul Mackerras 	u8 __iomem *cfgspace;
54214cf11afSPaul Mackerras 
5431beb6a7dSBenjamin Herrenschmidt 	printk(KERN_INFO "mpic: Setting up HT PICs workarounds for U3/U4\n");
54414cf11afSPaul Mackerras 
54514cf11afSPaul Mackerras 	/* Allocate fixups array */
546ea96025aSAnton Vorontsov 	mpic->fixups = kzalloc(128 * sizeof(*mpic->fixups), GFP_KERNEL);
54714cf11afSPaul Mackerras 	BUG_ON(mpic->fixups == NULL);
54814cf11afSPaul Mackerras 
54914cf11afSPaul Mackerras 	/* Init spinlock */
550203041adSThomas Gleixner 	raw_spin_lock_init(&mpic->fixup_lock);
55114cf11afSPaul Mackerras 
552c4b22f26SSegher Boessenkool 	/* Map U3 config space. We assume all IO-APICs are on the primary bus
553c4b22f26SSegher Boessenkool 	 * so we only need to map 64kB.
55414cf11afSPaul Mackerras 	 */
555c4b22f26SSegher Boessenkool 	cfgspace = ioremap(0xf2000000, 0x10000);
55614cf11afSPaul Mackerras 	BUG_ON(cfgspace == NULL);
55714cf11afSPaul Mackerras 
5581beb6a7dSBenjamin Herrenschmidt 	/* Now we scan all slots. We do a very quick scan, we read the header
5591beb6a7dSBenjamin Herrenschmidt 	 * type, vendor ID and device ID only, that's plenty enough
56014cf11afSPaul Mackerras 	 */
561c4b22f26SSegher Boessenkool 	for (devfn = 0; devfn < 0x100; devfn++) {
56214cf11afSPaul Mackerras 		u8 __iomem *devbase = cfgspace + (devfn << 8);
56314cf11afSPaul Mackerras 		u8 hdr_type = readb(devbase + PCI_HEADER_TYPE);
56414cf11afSPaul Mackerras 		u32 l = readl(devbase + PCI_VENDOR_ID);
5651beb6a7dSBenjamin Herrenschmidt 		u16 s;
56614cf11afSPaul Mackerras 
56714cf11afSPaul Mackerras 		DBG("devfn %x, l: %x\n", devfn, l);
56814cf11afSPaul Mackerras 
56914cf11afSPaul Mackerras 		/* If no device, skip */
57014cf11afSPaul Mackerras 		if (l == 0xffffffff || l == 0x00000000 ||
57114cf11afSPaul Mackerras 		    l == 0x0000ffff || l == 0xffff0000)
57214cf11afSPaul Mackerras 			goto next;
5731beb6a7dSBenjamin Herrenschmidt 		/* Check if is supports capability lists */
5741beb6a7dSBenjamin Herrenschmidt 		s = readw(devbase + PCI_STATUS);
5751beb6a7dSBenjamin Herrenschmidt 		if (!(s & PCI_STATUS_CAP_LIST))
5761beb6a7dSBenjamin Herrenschmidt 			goto next;
57714cf11afSPaul Mackerras 
5781beb6a7dSBenjamin Herrenschmidt 		mpic_scan_ht_pic(mpic, devbase, devfn, l);
579812fd1fdSMichael Ellerman 		mpic_scan_ht_msi(mpic, devbase, devfn);
58014cf11afSPaul Mackerras 
58114cf11afSPaul Mackerras 	next:
58214cf11afSPaul Mackerras 		/* next device, if function 0 */
583c4b22f26SSegher Boessenkool 		if (PCI_FUNC(devfn) == 0 && (hdr_type & 0x80) == 0)
58414cf11afSPaul Mackerras 			devfn += 7;
58514cf11afSPaul Mackerras 	}
58614cf11afSPaul Mackerras }
58714cf11afSPaul Mackerras 
5886cfef5b2SMichael Ellerman #else /* CONFIG_MPIC_U3_HT_IRQS */
5896e99e458SBenjamin Herrenschmidt 
5906e99e458SBenjamin Herrenschmidt static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
5916e99e458SBenjamin Herrenschmidt {
5926e99e458SBenjamin Herrenschmidt 	return 0;
5936e99e458SBenjamin Herrenschmidt }
5946e99e458SBenjamin Herrenschmidt 
5956e99e458SBenjamin Herrenschmidt static void __init mpic_scan_ht_pics(struct mpic *mpic)
5966e99e458SBenjamin Herrenschmidt {
5976e99e458SBenjamin Herrenschmidt }
5986e99e458SBenjamin Herrenschmidt 
5996cfef5b2SMichael Ellerman #endif /* CONFIG_MPIC_U3_HT_IRQS */
60014cf11afSPaul Mackerras 
60114cf11afSPaul Mackerras /* Find an mpic associated with a given linux interrupt */
602d69a78d7STony Breeds static struct mpic *mpic_find(unsigned int irq)
60314cf11afSPaul Mackerras {
6040ebfff14SBenjamin Herrenschmidt 	if (irq < NUM_ISA_INTERRUPTS)
60514cf11afSPaul Mackerras 		return NULL;
6060ebfff14SBenjamin Herrenschmidt 
607ec775d0eSThomas Gleixner 	return irq_get_chip_data(irq);
60814cf11afSPaul Mackerras }
60914cf11afSPaul Mackerras 
610d69a78d7STony Breeds /* Determine if the linux irq is an IPI */
611d69a78d7STony Breeds static unsigned int mpic_is_ipi(struct mpic *mpic, unsigned int irq)
612d69a78d7STony Breeds {
613476eb491SGrant Likely 	unsigned int src = virq_to_hw(irq);
614d69a78d7STony Breeds 
615d69a78d7STony Breeds 	return (src >= mpic->ipi_vecs[0] && src <= mpic->ipi_vecs[3]);
616d69a78d7STony Breeds }
617d69a78d7STony Breeds 
618ea94187fSScott Wood /* Determine if the linux irq is a timer */
619ea94187fSScott Wood static unsigned int mpic_is_tm(struct mpic *mpic, unsigned int irq)
620ea94187fSScott Wood {
621ea94187fSScott Wood 	unsigned int src = virq_to_hw(irq);
622ea94187fSScott Wood 
623ea94187fSScott Wood 	return (src >= mpic->timer_vecs[0] && src <= mpic->timer_vecs[7]);
624ea94187fSScott Wood }
625d69a78d7STony Breeds 
62614cf11afSPaul Mackerras /* Convert a cpu mask from logical to physical cpu numbers. */
62714cf11afSPaul Mackerras static inline u32 mpic_physmask(u32 cpumask)
62814cf11afSPaul Mackerras {
62914cf11afSPaul Mackerras 	int i;
63014cf11afSPaul Mackerras 	u32 mask = 0;
63114cf11afSPaul Mackerras 
632ebc04215SMilton Miller 	for (i = 0; i < min(32, NR_CPUS); ++i, cpumask >>= 1)
63314cf11afSPaul Mackerras 		mask |= (cpumask & 1) << get_hard_smp_processor_id(i);
63414cf11afSPaul Mackerras 	return mask;
63514cf11afSPaul Mackerras }
63614cf11afSPaul Mackerras 
63714cf11afSPaul Mackerras #ifdef CONFIG_SMP
63814cf11afSPaul Mackerras /* Get the mpic structure from the IPI number */
639835c0553SLennert Buytenhek static inline struct mpic * mpic_from_ipi(struct irq_data *d)
64014cf11afSPaul Mackerras {
641835c0553SLennert Buytenhek 	return irq_data_get_irq_chip_data(d);
64214cf11afSPaul Mackerras }
64314cf11afSPaul Mackerras #endif
64414cf11afSPaul Mackerras 
64514cf11afSPaul Mackerras /* Get the mpic structure from the irq number */
64614cf11afSPaul Mackerras static inline struct mpic * mpic_from_irq(unsigned int irq)
64714cf11afSPaul Mackerras {
648ec775d0eSThomas Gleixner 	return irq_get_chip_data(irq);
649835c0553SLennert Buytenhek }
650835c0553SLennert Buytenhek 
651835c0553SLennert Buytenhek /* Get the mpic structure from the irq data */
652835c0553SLennert Buytenhek static inline struct mpic * mpic_from_irq_data(struct irq_data *d)
653835c0553SLennert Buytenhek {
654835c0553SLennert Buytenhek 	return irq_data_get_irq_chip_data(d);
65514cf11afSPaul Mackerras }
65614cf11afSPaul Mackerras 
65714cf11afSPaul Mackerras /* Send an EOI */
65814cf11afSPaul Mackerras static inline void mpic_eoi(struct mpic *mpic)
65914cf11afSPaul Mackerras {
6607233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_EOI), 0);
6617233593bSZang Roy-r61911 	(void)mpic_cpu_read(MPIC_INFO(CPU_WHOAMI));
66214cf11afSPaul Mackerras }
66314cf11afSPaul Mackerras 
66414cf11afSPaul Mackerras /*
66514cf11afSPaul Mackerras  * Linux descriptor level callbacks
66614cf11afSPaul Mackerras  */
66714cf11afSPaul Mackerras 
66814cf11afSPaul Mackerras 
669835c0553SLennert Buytenhek void mpic_unmask_irq(struct irq_data *d)
67014cf11afSPaul Mackerras {
67114cf11afSPaul Mackerras 	unsigned int loops = 100000;
672835c0553SLennert Buytenhek 	struct mpic *mpic = mpic_from_irq_data(d);
673476eb491SGrant Likely 	unsigned int src = irqd_to_hwirq(d);
67414cf11afSPaul Mackerras 
675835c0553SLennert Buytenhek 	DBG("%p: %s: enable_irq: %d (src %d)\n", mpic, mpic->name, d->irq, src);
67614cf11afSPaul Mackerras 
6777233593bSZang Roy-r61911 	mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
6787233593bSZang Roy-r61911 		       mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) &
679e5356640SBenjamin Herrenschmidt 		       ~MPIC_VECPRI_MASK);
68014cf11afSPaul Mackerras 	/* make sure mask gets to controller before we return to user */
68114cf11afSPaul Mackerras 	do {
68214cf11afSPaul Mackerras 		if (!loops--) {
6838bfc5e36SScott Wood 			printk(KERN_ERR "%s: timeout on hwirq %u\n",
6848bfc5e36SScott Wood 			       __func__, src);
68514cf11afSPaul Mackerras 			break;
68614cf11afSPaul Mackerras 		}
6877233593bSZang Roy-r61911 	} while(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK);
6881beb6a7dSBenjamin Herrenschmidt }
6891beb6a7dSBenjamin Herrenschmidt 
690835c0553SLennert Buytenhek void mpic_mask_irq(struct irq_data *d)
69114cf11afSPaul Mackerras {
69214cf11afSPaul Mackerras 	unsigned int loops = 100000;
693835c0553SLennert Buytenhek 	struct mpic *mpic = mpic_from_irq_data(d);
694476eb491SGrant Likely 	unsigned int src = irqd_to_hwirq(d);
69514cf11afSPaul Mackerras 
696835c0553SLennert Buytenhek 	DBG("%s: disable_irq: %d (src %d)\n", mpic->name, d->irq, src);
69714cf11afSPaul Mackerras 
6987233593bSZang Roy-r61911 	mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
6997233593bSZang Roy-r61911 		       mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) |
700e5356640SBenjamin Herrenschmidt 		       MPIC_VECPRI_MASK);
70114cf11afSPaul Mackerras 
70214cf11afSPaul Mackerras 	/* make sure mask gets to controller before we return to user */
70314cf11afSPaul Mackerras 	do {
70414cf11afSPaul Mackerras 		if (!loops--) {
7058bfc5e36SScott Wood 			printk(KERN_ERR "%s: timeout on hwirq %u\n",
7068bfc5e36SScott Wood 			       __func__, src);
70714cf11afSPaul Mackerras 			break;
70814cf11afSPaul Mackerras 		}
7097233593bSZang Roy-r61911 	} while(!(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK));
71014cf11afSPaul Mackerras }
71114cf11afSPaul Mackerras 
712835c0553SLennert Buytenhek void mpic_end_irq(struct irq_data *d)
71314cf11afSPaul Mackerras {
714835c0553SLennert Buytenhek 	struct mpic *mpic = mpic_from_irq_data(d);
71514cf11afSPaul Mackerras 
7161beb6a7dSBenjamin Herrenschmidt #ifdef DEBUG_IRQ
717835c0553SLennert Buytenhek 	DBG("%s: end_irq: %d\n", mpic->name, d->irq);
7181beb6a7dSBenjamin Herrenschmidt #endif
71914cf11afSPaul Mackerras 	/* We always EOI on end_irq() even for edge interrupts since that
72014cf11afSPaul Mackerras 	 * should only lower the priority, the MPIC should have properly
72114cf11afSPaul Mackerras 	 * latched another edge interrupt coming in anyway
72214cf11afSPaul Mackerras 	 */
72314cf11afSPaul Mackerras 
72414cf11afSPaul Mackerras 	mpic_eoi(mpic);
72514cf11afSPaul Mackerras }
72614cf11afSPaul Mackerras 
7276cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
728b9e5b4e6SBenjamin Herrenschmidt 
729835c0553SLennert Buytenhek static void mpic_unmask_ht_irq(struct irq_data *d)
730b9e5b4e6SBenjamin Herrenschmidt {
731835c0553SLennert Buytenhek 	struct mpic *mpic = mpic_from_irq_data(d);
732476eb491SGrant Likely 	unsigned int src = irqd_to_hwirq(d);
733b9e5b4e6SBenjamin Herrenschmidt 
734835c0553SLennert Buytenhek 	mpic_unmask_irq(d);
735b9e5b4e6SBenjamin Herrenschmidt 
73624a3f2e8SThomas Gleixner 	if (irqd_is_level_type(d))
737b9e5b4e6SBenjamin Herrenschmidt 		mpic_ht_end_irq(mpic, src);
738b9e5b4e6SBenjamin Herrenschmidt }
739b9e5b4e6SBenjamin Herrenschmidt 
740835c0553SLennert Buytenhek static unsigned int mpic_startup_ht_irq(struct irq_data *d)
741b9e5b4e6SBenjamin Herrenschmidt {
742835c0553SLennert Buytenhek 	struct mpic *mpic = mpic_from_irq_data(d);
743476eb491SGrant Likely 	unsigned int src = irqd_to_hwirq(d);
744b9e5b4e6SBenjamin Herrenschmidt 
745835c0553SLennert Buytenhek 	mpic_unmask_irq(d);
74624a3f2e8SThomas Gleixner 	mpic_startup_ht_interrupt(mpic, src, irqd_is_level_type(d));
747b9e5b4e6SBenjamin Herrenschmidt 
748b9e5b4e6SBenjamin Herrenschmidt 	return 0;
749b9e5b4e6SBenjamin Herrenschmidt }
750b9e5b4e6SBenjamin Herrenschmidt 
751835c0553SLennert Buytenhek static void mpic_shutdown_ht_irq(struct irq_data *d)
752b9e5b4e6SBenjamin Herrenschmidt {
753835c0553SLennert Buytenhek 	struct mpic *mpic = mpic_from_irq_data(d);
754476eb491SGrant Likely 	unsigned int src = irqd_to_hwirq(d);
755b9e5b4e6SBenjamin Herrenschmidt 
75624a3f2e8SThomas Gleixner 	mpic_shutdown_ht_interrupt(mpic, src);
757835c0553SLennert Buytenhek 	mpic_mask_irq(d);
758b9e5b4e6SBenjamin Herrenschmidt }
759b9e5b4e6SBenjamin Herrenschmidt 
760835c0553SLennert Buytenhek static void mpic_end_ht_irq(struct irq_data *d)
761b9e5b4e6SBenjamin Herrenschmidt {
762835c0553SLennert Buytenhek 	struct mpic *mpic = mpic_from_irq_data(d);
763476eb491SGrant Likely 	unsigned int src = irqd_to_hwirq(d);
764b9e5b4e6SBenjamin Herrenschmidt 
765b9e5b4e6SBenjamin Herrenschmidt #ifdef DEBUG_IRQ
766835c0553SLennert Buytenhek 	DBG("%s: end_irq: %d\n", mpic->name, d->irq);
767b9e5b4e6SBenjamin Herrenschmidt #endif
768b9e5b4e6SBenjamin Herrenschmidt 	/* We always EOI on end_irq() even for edge interrupts since that
769b9e5b4e6SBenjamin Herrenschmidt 	 * should only lower the priority, the MPIC should have properly
770b9e5b4e6SBenjamin Herrenschmidt 	 * latched another edge interrupt coming in anyway
771b9e5b4e6SBenjamin Herrenschmidt 	 */
772b9e5b4e6SBenjamin Herrenschmidt 
77324a3f2e8SThomas Gleixner 	if (irqd_is_level_type(d))
774b9e5b4e6SBenjamin Herrenschmidt 		mpic_ht_end_irq(mpic, src);
775b9e5b4e6SBenjamin Herrenschmidt 	mpic_eoi(mpic);
776b9e5b4e6SBenjamin Herrenschmidt }
7776cfef5b2SMichael Ellerman #endif /* !CONFIG_MPIC_U3_HT_IRQS */
778b9e5b4e6SBenjamin Herrenschmidt 
77914cf11afSPaul Mackerras #ifdef CONFIG_SMP
78014cf11afSPaul Mackerras 
781835c0553SLennert Buytenhek static void mpic_unmask_ipi(struct irq_data *d)
78214cf11afSPaul Mackerras {
783835c0553SLennert Buytenhek 	struct mpic *mpic = mpic_from_ipi(d);
784476eb491SGrant Likely 	unsigned int src = virq_to_hw(d->irq) - mpic->ipi_vecs[0];
78514cf11afSPaul Mackerras 
786835c0553SLennert Buytenhek 	DBG("%s: enable_ipi: %d (ipi %d)\n", mpic->name, d->irq, src);
78714cf11afSPaul Mackerras 	mpic_ipi_write(src, mpic_ipi_read(src) & ~MPIC_VECPRI_MASK);
78814cf11afSPaul Mackerras }
78914cf11afSPaul Mackerras 
790835c0553SLennert Buytenhek static void mpic_mask_ipi(struct irq_data *d)
79114cf11afSPaul Mackerras {
79214cf11afSPaul Mackerras 	/* NEVER disable an IPI... that's just plain wrong! */
79314cf11afSPaul Mackerras }
79414cf11afSPaul Mackerras 
795835c0553SLennert Buytenhek static void mpic_end_ipi(struct irq_data *d)
79614cf11afSPaul Mackerras {
797835c0553SLennert Buytenhek 	struct mpic *mpic = mpic_from_ipi(d);
79814cf11afSPaul Mackerras 
79914cf11afSPaul Mackerras 	/*
80014cf11afSPaul Mackerras 	 * IPIs are marked IRQ_PER_CPU. This has the side effect of
80114cf11afSPaul Mackerras 	 * preventing the IRQ_PENDING/IRQ_INPROGRESS logic from
80214cf11afSPaul Mackerras 	 * applying to them. We EOI them late to avoid re-entering.
80314cf11afSPaul Mackerras 	 */
80414cf11afSPaul Mackerras 	mpic_eoi(mpic);
80514cf11afSPaul Mackerras }
80614cf11afSPaul Mackerras 
80714cf11afSPaul Mackerras #endif /* CONFIG_SMP */
80814cf11afSPaul Mackerras 
809ea94187fSScott Wood static void mpic_unmask_tm(struct irq_data *d)
810ea94187fSScott Wood {
811ea94187fSScott Wood 	struct mpic *mpic = mpic_from_irq_data(d);
812ea94187fSScott Wood 	unsigned int src = virq_to_hw(d->irq) - mpic->timer_vecs[0];
813ea94187fSScott Wood 
81477ef4899SDmitry Eremin-Solenikov 	DBG("%s: enable_tm: %d (tm %d)\n", mpic->name, d->irq, src);
815ea94187fSScott Wood 	mpic_tm_write(src, mpic_tm_read(src) & ~MPIC_VECPRI_MASK);
816ea94187fSScott Wood 	mpic_tm_read(src);
817ea94187fSScott Wood }
818ea94187fSScott Wood 
819ea94187fSScott Wood static void mpic_mask_tm(struct irq_data *d)
820ea94187fSScott Wood {
821ea94187fSScott Wood 	struct mpic *mpic = mpic_from_irq_data(d);
822ea94187fSScott Wood 	unsigned int src = virq_to_hw(d->irq) - mpic->timer_vecs[0];
823ea94187fSScott Wood 
824ea94187fSScott Wood 	mpic_tm_write(src, mpic_tm_read(src) | MPIC_VECPRI_MASK);
825ea94187fSScott Wood 	mpic_tm_read(src);
826ea94187fSScott Wood }
827ea94187fSScott Wood 
828835c0553SLennert Buytenhek int mpic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
829835c0553SLennert Buytenhek 		      bool force)
83014cf11afSPaul Mackerras {
831835c0553SLennert Buytenhek 	struct mpic *mpic = mpic_from_irq_data(d);
832476eb491SGrant Likely 	unsigned int src = irqd_to_hwirq(d);
83314cf11afSPaul Mackerras 
8343c10c9c4SKumar Gala 	if (mpic->flags & MPIC_SINGLE_DEST_CPU) {
83538e1313fSYang Li 		int cpuid = irq_choose_cpu(cpumask);
8363c10c9c4SKumar Gala 
8373c10c9c4SKumar Gala 		mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION), 1 << cpuid);
8383c10c9c4SKumar Gala 	} else {
8392a116f3dSMilton Miller 		u32 mask = cpumask_bits(cpumask)[0];
84014cf11afSPaul Mackerras 
8412a116f3dSMilton Miller 		mask &= cpumask_bits(cpu_online_mask)[0];
84214cf11afSPaul Mackerras 
8437233593bSZang Roy-r61911 		mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION),
8442a116f3dSMilton Miller 			       mpic_physmask(mask));
84514cf11afSPaul Mackerras 	}
846d5dedd45SYinghai Lu 
847d5dedd45SYinghai Lu 	return 0;
8483c10c9c4SKumar Gala }
84914cf11afSPaul Mackerras 
8507233593bSZang Roy-r61911 static unsigned int mpic_type_to_vecpri(struct mpic *mpic, unsigned int type)
8510ebfff14SBenjamin Herrenschmidt {
8520ebfff14SBenjamin Herrenschmidt 	/* Now convert sense value */
8536e99e458SBenjamin Herrenschmidt 	switch(type & IRQ_TYPE_SENSE_MASK) {
8540ebfff14SBenjamin Herrenschmidt 	case IRQ_TYPE_EDGE_RISING:
8557233593bSZang Roy-r61911 		return MPIC_INFO(VECPRI_SENSE_EDGE) |
8567233593bSZang Roy-r61911 		       MPIC_INFO(VECPRI_POLARITY_POSITIVE);
8570ebfff14SBenjamin Herrenschmidt 	case IRQ_TYPE_EDGE_FALLING:
8586e99e458SBenjamin Herrenschmidt 	case IRQ_TYPE_EDGE_BOTH:
8597233593bSZang Roy-r61911 		return MPIC_INFO(VECPRI_SENSE_EDGE) |
8607233593bSZang Roy-r61911 		       MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
8610ebfff14SBenjamin Herrenschmidt 	case IRQ_TYPE_LEVEL_HIGH:
8627233593bSZang Roy-r61911 		return MPIC_INFO(VECPRI_SENSE_LEVEL) |
8637233593bSZang Roy-r61911 		       MPIC_INFO(VECPRI_POLARITY_POSITIVE);
8640ebfff14SBenjamin Herrenschmidt 	case IRQ_TYPE_LEVEL_LOW:
8650ebfff14SBenjamin Herrenschmidt 	default:
8667233593bSZang Roy-r61911 		return MPIC_INFO(VECPRI_SENSE_LEVEL) |
8677233593bSZang Roy-r61911 		       MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
8680ebfff14SBenjamin Herrenschmidt 	}
8696e99e458SBenjamin Herrenschmidt }
8706e99e458SBenjamin Herrenschmidt 
871835c0553SLennert Buytenhek int mpic_set_irq_type(struct irq_data *d, unsigned int flow_type)
8726e99e458SBenjamin Herrenschmidt {
873835c0553SLennert Buytenhek 	struct mpic *mpic = mpic_from_irq_data(d);
874476eb491SGrant Likely 	unsigned int src = irqd_to_hwirq(d);
8756e99e458SBenjamin Herrenschmidt 	unsigned int vecpri, vold, vnew;
8766e99e458SBenjamin Herrenschmidt 
87706fe98e6SBenjamin Herrenschmidt 	DBG("mpic: set_irq_type(mpic:@%p,virq:%d,src:0x%x,type:0x%x)\n",
878835c0553SLennert Buytenhek 	    mpic, d->irq, src, flow_type);
8796e99e458SBenjamin Herrenschmidt 
8806e99e458SBenjamin Herrenschmidt 	if (src >= mpic->irq_count)
8816e99e458SBenjamin Herrenschmidt 		return -EINVAL;
8826e99e458SBenjamin Herrenschmidt 
8836e99e458SBenjamin Herrenschmidt 	if (flow_type == IRQ_TYPE_NONE)
8846e99e458SBenjamin Herrenschmidt 		if (mpic->senses && src < mpic->senses_count)
8856e99e458SBenjamin Herrenschmidt 			flow_type = mpic->senses[src];
8866e99e458SBenjamin Herrenschmidt 	if (flow_type == IRQ_TYPE_NONE)
8876e99e458SBenjamin Herrenschmidt 		flow_type = IRQ_TYPE_LEVEL_LOW;
8886e99e458SBenjamin Herrenschmidt 
88924a3f2e8SThomas Gleixner 	irqd_set_trigger_type(d, flow_type);
8906e99e458SBenjamin Herrenschmidt 
8916e99e458SBenjamin Herrenschmidt 	if (mpic_is_ht_interrupt(mpic, src))
8926e99e458SBenjamin Herrenschmidt 		vecpri = MPIC_VECPRI_POLARITY_POSITIVE |
8936e99e458SBenjamin Herrenschmidt 			MPIC_VECPRI_SENSE_EDGE;
8946e99e458SBenjamin Herrenschmidt 	else
8957233593bSZang Roy-r61911 		vecpri = mpic_type_to_vecpri(mpic, flow_type);
8966e99e458SBenjamin Herrenschmidt 
8977233593bSZang Roy-r61911 	vold = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
8987233593bSZang Roy-r61911 	vnew = vold & ~(MPIC_INFO(VECPRI_POLARITY_MASK) |
8997233593bSZang Roy-r61911 			MPIC_INFO(VECPRI_SENSE_MASK));
9006e99e458SBenjamin Herrenschmidt 	vnew |= vecpri;
9016e99e458SBenjamin Herrenschmidt 	if (vold != vnew)
9027233593bSZang Roy-r61911 		mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI), vnew);
9036e99e458SBenjamin Herrenschmidt 
904e075cd70SJustin P. Mattock 	return IRQ_SET_MASK_OK_NOCOPY;
9050ebfff14SBenjamin Herrenschmidt }
9060ebfff14SBenjamin Herrenschmidt 
90738958dd9SOlof Johansson void mpic_set_vector(unsigned int virq, unsigned int vector)
90838958dd9SOlof Johansson {
90938958dd9SOlof Johansson 	struct mpic *mpic = mpic_from_irq(virq);
910476eb491SGrant Likely 	unsigned int src = virq_to_hw(virq);
91138958dd9SOlof Johansson 	unsigned int vecpri;
91238958dd9SOlof Johansson 
91338958dd9SOlof Johansson 	DBG("mpic: set_vector(mpic:@%p,virq:%d,src:%d,vector:0x%x)\n",
91438958dd9SOlof Johansson 	    mpic, virq, src, vector);
91538958dd9SOlof Johansson 
91638958dd9SOlof Johansson 	if (src >= mpic->irq_count)
91738958dd9SOlof Johansson 		return;
91838958dd9SOlof Johansson 
91938958dd9SOlof Johansson 	vecpri = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
92038958dd9SOlof Johansson 	vecpri = vecpri & ~MPIC_INFO(VECPRI_VECTOR_MASK);
92138958dd9SOlof Johansson 	vecpri |= vector;
92238958dd9SOlof Johansson 	mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI), vecpri);
92338958dd9SOlof Johansson }
92438958dd9SOlof Johansson 
925dfec2202SMeador Inge void mpic_set_destination(unsigned int virq, unsigned int cpuid)
926dfec2202SMeador Inge {
927dfec2202SMeador Inge 	struct mpic *mpic = mpic_from_irq(virq);
928476eb491SGrant Likely 	unsigned int src = virq_to_hw(virq);
929dfec2202SMeador Inge 
930dfec2202SMeador Inge 	DBG("mpic: set_destination(mpic:@%p,virq:%d,src:%d,cpuid:0x%x)\n",
931dfec2202SMeador Inge 	    mpic, virq, src, cpuid);
932dfec2202SMeador Inge 
933dfec2202SMeador Inge 	if (src >= mpic->irq_count)
934dfec2202SMeador Inge 		return;
935dfec2202SMeador Inge 
936dfec2202SMeador Inge 	mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION), 1 << cpuid);
937dfec2202SMeador Inge }
938dfec2202SMeador Inge 
939b9e5b4e6SBenjamin Herrenschmidt static struct irq_chip mpic_irq_chip = {
940835c0553SLennert Buytenhek 	.irq_mask	= mpic_mask_irq,
941835c0553SLennert Buytenhek 	.irq_unmask	= mpic_unmask_irq,
942835c0553SLennert Buytenhek 	.irq_eoi	= mpic_end_irq,
943835c0553SLennert Buytenhek 	.irq_set_type	= mpic_set_irq_type,
944b9e5b4e6SBenjamin Herrenschmidt };
945b9e5b4e6SBenjamin Herrenschmidt 
946b9e5b4e6SBenjamin Herrenschmidt #ifdef CONFIG_SMP
947b9e5b4e6SBenjamin Herrenschmidt static struct irq_chip mpic_ipi_chip = {
948835c0553SLennert Buytenhek 	.irq_mask	= mpic_mask_ipi,
949835c0553SLennert Buytenhek 	.irq_unmask	= mpic_unmask_ipi,
950835c0553SLennert Buytenhek 	.irq_eoi	= mpic_end_ipi,
951b9e5b4e6SBenjamin Herrenschmidt };
952b9e5b4e6SBenjamin Herrenschmidt #endif /* CONFIG_SMP */
953b9e5b4e6SBenjamin Herrenschmidt 
954ea94187fSScott Wood static struct irq_chip mpic_tm_chip = {
955ea94187fSScott Wood 	.irq_mask	= mpic_mask_tm,
956ea94187fSScott Wood 	.irq_unmask	= mpic_unmask_tm,
957ea94187fSScott Wood 	.irq_eoi	= mpic_end_irq,
958ea94187fSScott Wood };
959ea94187fSScott Wood 
9606cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
961b9e5b4e6SBenjamin Herrenschmidt static struct irq_chip mpic_irq_ht_chip = {
962835c0553SLennert Buytenhek 	.irq_startup	= mpic_startup_ht_irq,
963835c0553SLennert Buytenhek 	.irq_shutdown	= mpic_shutdown_ht_irq,
964835c0553SLennert Buytenhek 	.irq_mask	= mpic_mask_irq,
965835c0553SLennert Buytenhek 	.irq_unmask	= mpic_unmask_ht_irq,
966835c0553SLennert Buytenhek 	.irq_eoi	= mpic_end_ht_irq,
967835c0553SLennert Buytenhek 	.irq_set_type	= mpic_set_irq_type,
968b9e5b4e6SBenjamin Herrenschmidt };
9696cfef5b2SMichael Ellerman #endif /* CONFIG_MPIC_U3_HT_IRQS */
970b9e5b4e6SBenjamin Herrenschmidt 
97114cf11afSPaul Mackerras 
9720ebfff14SBenjamin Herrenschmidt static int mpic_host_match(struct irq_host *h, struct device_node *node)
9730ebfff14SBenjamin Herrenschmidt {
9740ebfff14SBenjamin Herrenschmidt 	/* Exact match, unless mpic node is NULL */
97552964f87SMichael Ellerman 	return h->of_node == NULL || h->of_node == node;
9760ebfff14SBenjamin Herrenschmidt }
9770ebfff14SBenjamin Herrenschmidt 
9780ebfff14SBenjamin Herrenschmidt static int mpic_host_map(struct irq_host *h, unsigned int virq,
9796e99e458SBenjamin Herrenschmidt 			 irq_hw_number_t hw)
9800ebfff14SBenjamin Herrenschmidt {
9810ebfff14SBenjamin Herrenschmidt 	struct mpic *mpic = h->host_data;
9826e99e458SBenjamin Herrenschmidt 	struct irq_chip *chip;
9830ebfff14SBenjamin Herrenschmidt 
98406fe98e6SBenjamin Herrenschmidt 	DBG("mpic: map virq %d, hwirq 0x%lx\n", virq, hw);
9850ebfff14SBenjamin Herrenschmidt 
9867df2457dSOlof Johansson 	if (hw == mpic->spurious_vec)
9870ebfff14SBenjamin Herrenschmidt 		return -EINVAL;
9887fd72186SBenjamin Herrenschmidt 	if (mpic->protected && test_bit(hw, mpic->protected))
9897fd72186SBenjamin Herrenschmidt 		return -EINVAL;
99006fe98e6SBenjamin Herrenschmidt 
9910ebfff14SBenjamin Herrenschmidt #ifdef CONFIG_SMP
9927df2457dSOlof Johansson 	else if (hw >= mpic->ipi_vecs[0]) {
9930ebfff14SBenjamin Herrenschmidt 		WARN_ON(!(mpic->flags & MPIC_PRIMARY));
9940ebfff14SBenjamin Herrenschmidt 
99506fe98e6SBenjamin Herrenschmidt 		DBG("mpic: mapping as IPI\n");
996ec775d0eSThomas Gleixner 		irq_set_chip_data(virq, mpic);
997ec775d0eSThomas Gleixner 		irq_set_chip_and_handler(virq, &mpic->hc_ipi,
9980ebfff14SBenjamin Herrenschmidt 					 handle_percpu_irq);
9990ebfff14SBenjamin Herrenschmidt 		return 0;
10000ebfff14SBenjamin Herrenschmidt 	}
10010ebfff14SBenjamin Herrenschmidt #endif /* CONFIG_SMP */
10020ebfff14SBenjamin Herrenschmidt 
1003ea94187fSScott Wood 	if (hw >= mpic->timer_vecs[0] && hw <= mpic->timer_vecs[7]) {
1004ea94187fSScott Wood 		WARN_ON(!(mpic->flags & MPIC_PRIMARY));
1005ea94187fSScott Wood 
1006ea94187fSScott Wood 		DBG("mpic: mapping as timer\n");
1007ea94187fSScott Wood 		irq_set_chip_data(virq, mpic);
1008ea94187fSScott Wood 		irq_set_chip_and_handler(virq, &mpic->hc_tm,
1009ea94187fSScott Wood 					 handle_fasteoi_irq);
1010ea94187fSScott Wood 		return 0;
1011ea94187fSScott Wood 	}
1012ea94187fSScott Wood 
10130ebfff14SBenjamin Herrenschmidt 	if (hw >= mpic->irq_count)
10140ebfff14SBenjamin Herrenschmidt 		return -EINVAL;
10150ebfff14SBenjamin Herrenschmidt 
1016a7de7c74SMichael Ellerman 	mpic_msi_reserve_hwirq(mpic, hw);
1017a7de7c74SMichael Ellerman 
10186e99e458SBenjamin Herrenschmidt 	/* Default chip */
10190ebfff14SBenjamin Herrenschmidt 	chip = &mpic->hc_irq;
10200ebfff14SBenjamin Herrenschmidt 
10216cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
10220ebfff14SBenjamin Herrenschmidt 	/* Check for HT interrupts, override vecpri */
10236e99e458SBenjamin Herrenschmidt 	if (mpic_is_ht_interrupt(mpic, hw))
10240ebfff14SBenjamin Herrenschmidt 		chip = &mpic->hc_ht_irq;
10256cfef5b2SMichael Ellerman #endif /* CONFIG_MPIC_U3_HT_IRQS */
10260ebfff14SBenjamin Herrenschmidt 
102706fe98e6SBenjamin Herrenschmidt 	DBG("mpic: mapping to irq chip @%p\n", chip);
10280ebfff14SBenjamin Herrenschmidt 
1029ec775d0eSThomas Gleixner 	irq_set_chip_data(virq, mpic);
1030ec775d0eSThomas Gleixner 	irq_set_chip_and_handler(virq, chip, handle_fasteoi_irq);
10316e99e458SBenjamin Herrenschmidt 
10326e99e458SBenjamin Herrenschmidt 	/* Set default irq type */
1033ec775d0eSThomas Gleixner 	irq_set_irq_type(virq, IRQ_TYPE_NONE);
10346e99e458SBenjamin Herrenschmidt 
1035dfec2202SMeador Inge 	/* If the MPIC was reset, then all vectors have already been
1036dfec2202SMeador Inge 	 * initialized.  Otherwise, a per source lazy initialization
1037dfec2202SMeador Inge 	 * is done here.
1038dfec2202SMeador Inge 	 */
1039dfec2202SMeador Inge 	if (!mpic_is_ipi(mpic, hw) && (mpic->flags & MPIC_NO_RESET)) {
1040dfec2202SMeador Inge 		mpic_set_vector(virq, hw);
1041d6a2639bSMeador Inge 		mpic_set_destination(virq, mpic_processor_id(mpic));
1042dfec2202SMeador Inge 		mpic_irq_set_priority(virq, 8);
1043dfec2202SMeador Inge 	}
1044dfec2202SMeador Inge 
10450ebfff14SBenjamin Herrenschmidt 	return 0;
10460ebfff14SBenjamin Herrenschmidt }
10470ebfff14SBenjamin Herrenschmidt 
10480ebfff14SBenjamin Herrenschmidt static int mpic_host_xlate(struct irq_host *h, struct device_node *ct,
104940d50cf7SRoman Fietze 			   const u32 *intspec, unsigned int intsize,
10500ebfff14SBenjamin Herrenschmidt 			   irq_hw_number_t *out_hwirq, unsigned int *out_flags)
10510ebfff14SBenjamin Herrenschmidt 
10520ebfff14SBenjamin Herrenschmidt {
105322d168ceSScott Wood 	struct mpic *mpic = h->host_data;
10540ebfff14SBenjamin Herrenschmidt 	static unsigned char map_mpic_senses[4] = {
10550ebfff14SBenjamin Herrenschmidt 		IRQ_TYPE_EDGE_RISING,
10560ebfff14SBenjamin Herrenschmidt 		IRQ_TYPE_LEVEL_LOW,
10570ebfff14SBenjamin Herrenschmidt 		IRQ_TYPE_LEVEL_HIGH,
10580ebfff14SBenjamin Herrenschmidt 		IRQ_TYPE_EDGE_FALLING,
10590ebfff14SBenjamin Herrenschmidt 	};
10600ebfff14SBenjamin Herrenschmidt 
10610ebfff14SBenjamin Herrenschmidt 	*out_hwirq = intspec[0];
106222d168ceSScott Wood 	if (intsize >= 4 && (mpic->flags & MPIC_FSL)) {
106322d168ceSScott Wood 		/*
106422d168ceSScott Wood 		 * Freescale MPIC with extended intspec:
106522d168ceSScott Wood 		 * First two cells are as usual.  Third specifies
106622d168ceSScott Wood 		 * an "interrupt type".  Fourth is type-specific data.
106722d168ceSScott Wood 		 *
106822d168ceSScott Wood 		 * See Documentation/devicetree/bindings/powerpc/fsl/mpic.txt
106922d168ceSScott Wood 		 */
107022d168ceSScott Wood 		switch (intspec[2]) {
107122d168ceSScott Wood 		case 0:
107222d168ceSScott Wood 		case 1: /* no EISR/EIMR support for now, treat as shared IRQ */
107322d168ceSScott Wood 			break;
107422d168ceSScott Wood 		case 2:
107522d168ceSScott Wood 			if (intspec[0] >= ARRAY_SIZE(mpic->ipi_vecs))
107622d168ceSScott Wood 				return -EINVAL;
107722d168ceSScott Wood 
107822d168ceSScott Wood 			*out_hwirq = mpic->ipi_vecs[intspec[0]];
107922d168ceSScott Wood 			break;
108022d168ceSScott Wood 		case 3:
108122d168ceSScott Wood 			if (intspec[0] >= ARRAY_SIZE(mpic->timer_vecs))
108222d168ceSScott Wood 				return -EINVAL;
108322d168ceSScott Wood 
108422d168ceSScott Wood 			*out_hwirq = mpic->timer_vecs[intspec[0]];
108522d168ceSScott Wood 			break;
108622d168ceSScott Wood 		default:
108722d168ceSScott Wood 			pr_debug("%s: unknown irq type %u\n",
108822d168ceSScott Wood 				 __func__, intspec[2]);
108922d168ceSScott Wood 			return -EINVAL;
109022d168ceSScott Wood 		}
109122d168ceSScott Wood 
109222d168ceSScott Wood 		*out_flags = map_mpic_senses[intspec[1] & 3];
109322d168ceSScott Wood 	} else if (intsize > 1) {
109406fe98e6SBenjamin Herrenschmidt 		u32 mask = 0x3;
109506fe98e6SBenjamin Herrenschmidt 
109606fe98e6SBenjamin Herrenschmidt 		/* Apple invented a new race of encoding on machines with
109706fe98e6SBenjamin Herrenschmidt 		 * an HT APIC. They encode, among others, the index within
109806fe98e6SBenjamin Herrenschmidt 		 * the HT APIC. We don't care about it here since thankfully,
109906fe98e6SBenjamin Herrenschmidt 		 * it appears that they have the APIC already properly
110006fe98e6SBenjamin Herrenschmidt 		 * configured, and thus our current fixup code that reads the
110106fe98e6SBenjamin Herrenschmidt 		 * APIC config works fine. However, we still need to mask out
110206fe98e6SBenjamin Herrenschmidt 		 * bits in the specifier to make sure we only get bit 0 which
110306fe98e6SBenjamin Herrenschmidt 		 * is the level/edge bit (the only sense bit exposed by Apple),
110406fe98e6SBenjamin Herrenschmidt 		 * as their bit 1 means something else.
110506fe98e6SBenjamin Herrenschmidt 		 */
110606fe98e6SBenjamin Herrenschmidt 		if (machine_is(powermac))
110706fe98e6SBenjamin Herrenschmidt 			mask = 0x1;
110806fe98e6SBenjamin Herrenschmidt 		*out_flags = map_mpic_senses[intspec[1] & mask];
110906fe98e6SBenjamin Herrenschmidt 	} else
11100ebfff14SBenjamin Herrenschmidt 		*out_flags = IRQ_TYPE_NONE;
11110ebfff14SBenjamin Herrenschmidt 
111206fe98e6SBenjamin Herrenschmidt 	DBG("mpic: xlate (%d cells: 0x%08x 0x%08x) to line 0x%lx sense 0x%x\n",
111306fe98e6SBenjamin Herrenschmidt 	    intsize, intspec[0], intspec[1], *out_hwirq, *out_flags);
111406fe98e6SBenjamin Herrenschmidt 
11150ebfff14SBenjamin Herrenschmidt 	return 0;
11160ebfff14SBenjamin Herrenschmidt }
11170ebfff14SBenjamin Herrenschmidt 
11180ebfff14SBenjamin Herrenschmidt static struct irq_host_ops mpic_host_ops = {
11190ebfff14SBenjamin Herrenschmidt 	.match = mpic_host_match,
11200ebfff14SBenjamin Herrenschmidt 	.map = mpic_host_map,
11210ebfff14SBenjamin Herrenschmidt 	.xlate = mpic_host_xlate,
11220ebfff14SBenjamin Herrenschmidt };
11230ebfff14SBenjamin Herrenschmidt 
1124dfec2202SMeador Inge static int mpic_reset_prohibited(struct device_node *node)
1125dfec2202SMeador Inge {
1126dfec2202SMeador Inge 	return node && of_get_property(node, "pic-no-reset", NULL);
1127dfec2202SMeador Inge }
1128dfec2202SMeador Inge 
112914cf11afSPaul Mackerras /*
113014cf11afSPaul Mackerras  * Exported functions
113114cf11afSPaul Mackerras  */
113214cf11afSPaul Mackerras 
11330ebfff14SBenjamin Herrenschmidt struct mpic * __init mpic_alloc(struct device_node *node,
1134a959ff56SBenjamin Herrenschmidt 				phys_addr_t phys_addr,
113514cf11afSPaul Mackerras 				unsigned int flags,
113614cf11afSPaul Mackerras 				unsigned int isu_size,
113714cf11afSPaul Mackerras 				unsigned int irq_count,
113814cf11afSPaul Mackerras 				const char *name)
113914cf11afSPaul Mackerras {
1140*5bdb6f2eSKyle Moffett 	int i, psize, intvec_top;
114114cf11afSPaul Mackerras 	struct mpic *mpic;
1142d9d1063dSJohannes Berg 	u32 greg_feature;
114314cf11afSPaul Mackerras 	const char *vers;
1144*5bdb6f2eSKyle Moffett 	const u32 *psrc;
11458bf41568SKyle Moffett 
1146*5bdb6f2eSKyle Moffett 	/* This code assumes that a non-NULL device node is passed in */
11478bf41568SKyle Moffett 	BUG_ON(!node);
11488bf41568SKyle Moffett 
1149*5bdb6f2eSKyle Moffett 	/* Pick the physical address from the device tree if unspecified */
1150*5bdb6f2eSKyle Moffett 	if (!phys_addr) {
11518bf41568SKyle Moffett 		/* Check if it is DCR-based */
11528bf41568SKyle Moffett 		if (of_get_property(node, "dcr-reg", NULL)) {
11538bf41568SKyle Moffett 			flags |= MPIC_USES_DCR;
11548bf41568SKyle Moffett 		} else {
11558bf41568SKyle Moffett 			struct resource r;
11568bf41568SKyle Moffett 			if (of_address_to_resource(node, 0, &r))
11578bf41568SKyle Moffett 				return NULL;
11588bf41568SKyle Moffett 			phys_addr = r.start;
11598bf41568SKyle Moffett 		}
11608bf41568SKyle Moffett 	}
116114cf11afSPaul Mackerras 
116285355bb2SKumar Gala 	mpic = kzalloc(sizeof(struct mpic), GFP_KERNEL);
116314cf11afSPaul Mackerras 	if (mpic == NULL)
116414cf11afSPaul Mackerras 		return NULL;
116514cf11afSPaul Mackerras 
116614cf11afSPaul Mackerras 	mpic->name = name;
116714cf11afSPaul Mackerras 
1168b9e5b4e6SBenjamin Herrenschmidt 	mpic->hc_irq = mpic_irq_chip;
1169b27df672SThomas Gleixner 	mpic->hc_irq.name = name;
117014cf11afSPaul Mackerras 	if (flags & MPIC_PRIMARY)
1171835c0553SLennert Buytenhek 		mpic->hc_irq.irq_set_affinity = mpic_set_affinity;
11726cfef5b2SMichael Ellerman #ifdef CONFIG_MPIC_U3_HT_IRQS
1173b9e5b4e6SBenjamin Herrenschmidt 	mpic->hc_ht_irq = mpic_irq_ht_chip;
1174b27df672SThomas Gleixner 	mpic->hc_ht_irq.name = name;
1175b9e5b4e6SBenjamin Herrenschmidt 	if (flags & MPIC_PRIMARY)
1176835c0553SLennert Buytenhek 		mpic->hc_ht_irq.irq_set_affinity = mpic_set_affinity;
11776cfef5b2SMichael Ellerman #endif /* CONFIG_MPIC_U3_HT_IRQS */
1178fbf0274eSBenjamin Herrenschmidt 
117914cf11afSPaul Mackerras #ifdef CONFIG_SMP
1180b9e5b4e6SBenjamin Herrenschmidt 	mpic->hc_ipi = mpic_ipi_chip;
1181b27df672SThomas Gleixner 	mpic->hc_ipi.name = name;
118214cf11afSPaul Mackerras #endif /* CONFIG_SMP */
118314cf11afSPaul Mackerras 
1184ea94187fSScott Wood 	mpic->hc_tm = mpic_tm_chip;
1185ea94187fSScott Wood 	mpic->hc_tm.name = name;
1186ea94187fSScott Wood 
118714cf11afSPaul Mackerras 	mpic->flags = flags;
118814cf11afSPaul Mackerras 	mpic->isu_size = isu_size;
118914cf11afSPaul Mackerras 	mpic->irq_count = irq_count;
119014cf11afSPaul Mackerras 	mpic->num_sources = 0; /* so far */
119114cf11afSPaul Mackerras 
11927df2457dSOlof Johansson 	if (flags & MPIC_LARGE_VECTORS)
11937df2457dSOlof Johansson 		intvec_top = 2047;
11947df2457dSOlof Johansson 	else
11957df2457dSOlof Johansson 		intvec_top = 255;
11967df2457dSOlof Johansson 
1197ea94187fSScott Wood 	mpic->timer_vecs[0] = intvec_top - 12;
1198ea94187fSScott Wood 	mpic->timer_vecs[1] = intvec_top - 11;
1199ea94187fSScott Wood 	mpic->timer_vecs[2] = intvec_top - 10;
1200ea94187fSScott Wood 	mpic->timer_vecs[3] = intvec_top - 9;
1201ea94187fSScott Wood 	mpic->timer_vecs[4] = intvec_top - 8;
1202ea94187fSScott Wood 	mpic->timer_vecs[5] = intvec_top - 7;
1203ea94187fSScott Wood 	mpic->timer_vecs[6] = intvec_top - 6;
1204ea94187fSScott Wood 	mpic->timer_vecs[7] = intvec_top - 5;
12057df2457dSOlof Johansson 	mpic->ipi_vecs[0]   = intvec_top - 4;
12067df2457dSOlof Johansson 	mpic->ipi_vecs[1]   = intvec_top - 3;
12077df2457dSOlof Johansson 	mpic->ipi_vecs[2]   = intvec_top - 2;
12087df2457dSOlof Johansson 	mpic->ipi_vecs[3]   = intvec_top - 1;
12097df2457dSOlof Johansson 	mpic->spurious_vec  = intvec_top;
12107df2457dSOlof Johansson 
1211a959ff56SBenjamin Herrenschmidt 	/* Check for "big-endian" in device-tree */
1212*5bdb6f2eSKyle Moffett 	if (of_get_property(node, "big-endian", NULL) != NULL)
1213a959ff56SBenjamin Herrenschmidt 		mpic->flags |= MPIC_BIG_ENDIAN;
1214*5bdb6f2eSKyle Moffett 	if (of_device_is_compatible(node, "fsl,mpic"))
121522d168ceSScott Wood 		mpic->flags |= MPIC_FSL;
1216a959ff56SBenjamin Herrenschmidt 
12177fd72186SBenjamin Herrenschmidt 	/* Look for protected sources */
1218*5bdb6f2eSKyle Moffett 	psrc = of_get_property(node, "protected-sources", &psize);
12197fd72186SBenjamin Herrenschmidt 	if (psrc) {
1220*5bdb6f2eSKyle Moffett 		/* Allocate a bitmap with one bit per interrupt */
1221*5bdb6f2eSKyle Moffett 		unsigned int mapsize = BITS_TO_LONGS(intvec_top + 1);
1222*5bdb6f2eSKyle Moffett 		mpic->protected = kzalloc(mapsize*sizeof(long), GFP_KERNEL);
12237fd72186SBenjamin Herrenschmidt 		BUG_ON(mpic->protected == NULL);
1224*5bdb6f2eSKyle Moffett 		for (i = 0; i < psize/sizeof(u32); i++) {
12257fd72186SBenjamin Herrenschmidt 			if (psrc[i] > intvec_top)
12267fd72186SBenjamin Herrenschmidt 				continue;
12277fd72186SBenjamin Herrenschmidt 			__set_bit(psrc[i], mpic->protected);
12287fd72186SBenjamin Herrenschmidt 		}
12297fd72186SBenjamin Herrenschmidt 	}
1230a959ff56SBenjamin Herrenschmidt 
12317233593bSZang Roy-r61911 #ifdef CONFIG_MPIC_WEIRD
12327233593bSZang Roy-r61911 	mpic->hw_set = mpic_infos[MPIC_GET_REGSET(flags)];
12337233593bSZang Roy-r61911 #endif
12347233593bSZang Roy-r61911 
1235fbf0274eSBenjamin Herrenschmidt 	/* default register type */
12368bf41568SKyle Moffett 	if (flags & MPIC_BIG_ENDIAN)
12378bf41568SKyle Moffett 		mpic->reg_type = mpic_access_mmio_be;
12388bf41568SKyle Moffett 	else
12398bf41568SKyle Moffett 		mpic->reg_type = mpic_access_mmio_le;
1240fbf0274eSBenjamin Herrenschmidt 
12418bf41568SKyle Moffett 	/*
12428bf41568SKyle Moffett 	 * An MPIC with a "dcr-reg" property must be accessed that way, but
12438bf41568SKyle Moffett 	 * only if the kernel includes DCR support.
12448bf41568SKyle Moffett 	 */
1245fbf0274eSBenjamin Herrenschmidt #ifdef CONFIG_PPC_DCR
12468bf41568SKyle Moffett 	if (flags & MPIC_USES_DCR)
1247fbf0274eSBenjamin Herrenschmidt 		mpic->reg_type = mpic_access_dcr;
1248fbf0274eSBenjamin Herrenschmidt #else
12498bf41568SKyle Moffett 	BUG_ON(flags & MPIC_USES_DCR);
12508bf41568SKyle Moffett #endif
1251a959ff56SBenjamin Herrenschmidt 
125214cf11afSPaul Mackerras 	/* Map the global registers */
12538bf41568SKyle Moffett 	mpic_map(mpic, node, phys_addr, &mpic->gregs, MPIC_INFO(GREG_BASE), 0x1000);
12548bf41568SKyle Moffett 	mpic_map(mpic, node, phys_addr, &mpic->tmregs, MPIC_INFO(TIMER_BASE), 0x1000);
125514cf11afSPaul Mackerras 
125614cf11afSPaul Mackerras 	/* Reset */
1257dfec2202SMeador Inge 
1258dfec2202SMeador Inge 	/* When using a device-node, reset requests are only honored if the MPIC
1259dfec2202SMeador Inge 	 * is allowed to reset.
1260dfec2202SMeador Inge 	 */
1261dfec2202SMeador Inge 	if (mpic_reset_prohibited(node))
1262dfec2202SMeador Inge 		mpic->flags |= MPIC_NO_RESET;
1263dfec2202SMeador Inge 
1264dfec2202SMeador Inge 	if ((flags & MPIC_WANTS_RESET) && !(mpic->flags & MPIC_NO_RESET)) {
1265dfec2202SMeador Inge 		printk(KERN_DEBUG "mpic: Resetting\n");
12667233593bSZang Roy-r61911 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
12677233593bSZang Roy-r61911 			   mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
126814cf11afSPaul Mackerras 			   | MPIC_GREG_GCONF_RESET);
12697233593bSZang Roy-r61911 		while( mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
127014cf11afSPaul Mackerras 		       & MPIC_GREG_GCONF_RESET)
127114cf11afSPaul Mackerras 			mb();
127214cf11afSPaul Mackerras 	}
127314cf11afSPaul Mackerras 
1274d91e4ea7SKumar Gala 	/* CoreInt */
1275d91e4ea7SKumar Gala 	if (flags & MPIC_ENABLE_COREINT)
1276d91e4ea7SKumar Gala 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1277d91e4ea7SKumar Gala 			   mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1278d91e4ea7SKumar Gala 			   | MPIC_GREG_GCONF_COREINT);
1279d91e4ea7SKumar Gala 
1280f365355eSOlof Johansson 	if (flags & MPIC_ENABLE_MCK)
1281f365355eSOlof Johansson 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1282f365355eSOlof Johansson 			   mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1283f365355eSOlof Johansson 			   | MPIC_GREG_GCONF_MCK);
1284f365355eSOlof Johansson 
128514b92470STimur Tabi 	/*
128614b92470STimur Tabi 	 * Read feature register.  For non-ISU MPICs, num sources as well. On
128714b92470STimur Tabi 	 * ISU MPICs, sources are counted as ISUs are added
128814cf11afSPaul Mackerras 	 */
1289d9d1063dSJohannes Berg 	greg_feature = mpic_read(mpic->gregs, MPIC_INFO(GREG_FEATURE_0));
12905073e7eeSAnton Vorontsov 	if (isu_size == 0) {
1291475ca391SKumar Gala 		if (flags & MPIC_BROKEN_FRR_NIRQS)
1292475ca391SKumar Gala 			mpic->num_sources = mpic->irq_count;
1293475ca391SKumar Gala 		else
1294d9d1063dSJohannes Berg 			mpic->num_sources =
1295d9d1063dSJohannes Berg 				((greg_feature & MPIC_GREG_FEATURE_LAST_SRC_MASK)
129614cf11afSPaul Mackerras 				 >> MPIC_GREG_FEATURE_LAST_SRC_SHIFT) + 1;
12975073e7eeSAnton Vorontsov 	}
129814cf11afSPaul Mackerras 
129914b92470STimur Tabi 	/*
130014b92470STimur Tabi 	 * The MPIC driver will crash if there are more cores than we
130114b92470STimur Tabi 	 * can initialize, so we may as well catch that problem here.
130214b92470STimur Tabi 	 */
130314b92470STimur Tabi 	BUG_ON(num_possible_cpus() > MPIC_MAX_CPUS);
130414b92470STimur Tabi 
130514cf11afSPaul Mackerras 	/* Map the per-CPU registers */
130614b92470STimur Tabi 	for_each_possible_cpu(i) {
130714b92470STimur Tabi 		unsigned int cpu = get_hard_smp_processor_id(i);
130814b92470STimur Tabi 
13098bf41568SKyle Moffett 		mpic_map(mpic, node, phys_addr, &mpic->cpuregs[cpu],
131014b92470STimur Tabi 			 MPIC_INFO(CPU_BASE) + cpu * MPIC_INFO(CPU_STRIDE),
1311fbf0274eSBenjamin Herrenschmidt 			 0x1000);
131214cf11afSPaul Mackerras 	}
131314cf11afSPaul Mackerras 
131414cf11afSPaul Mackerras 	/* Initialize main ISU if none provided */
131514cf11afSPaul Mackerras 	if (mpic->isu_size == 0) {
131614cf11afSPaul Mackerras 		mpic->isu_size = mpic->num_sources;
13178bf41568SKyle Moffett 		mpic_map(mpic, node, phys_addr, &mpic->isus[0],
1318fbf0274eSBenjamin Herrenschmidt 			 MPIC_INFO(IRQ_BASE), MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
131914cf11afSPaul Mackerras 	}
132014cf11afSPaul Mackerras 	mpic->isu_shift = 1 + __ilog2(mpic->isu_size - 1);
132114cf11afSPaul Mackerras 	mpic->isu_mask = (1 << mpic->isu_shift) - 1;
132214cf11afSPaul Mackerras 
132331207dabSKumar Gala 	mpic->irqhost = irq_alloc_host(node, IRQ_HOST_MAP_LINEAR,
132431207dabSKumar Gala 				       isu_size ? isu_size : mpic->num_sources,
132531207dabSKumar Gala 				       &mpic_host_ops,
132631207dabSKumar Gala 				       flags & MPIC_LARGE_VECTORS ? 2048 : 256);
132731207dabSKumar Gala 	if (mpic->irqhost == NULL)
132831207dabSKumar Gala 		return NULL;
132931207dabSKumar Gala 
133031207dabSKumar Gala 	mpic->irqhost->host_data = mpic;
133131207dabSKumar Gala 
133214cf11afSPaul Mackerras 	/* Display version */
1333d9d1063dSJohannes Berg 	switch (greg_feature & MPIC_GREG_FEATURE_VERSION_MASK) {
133414cf11afSPaul Mackerras 	case 1:
133514cf11afSPaul Mackerras 		vers = "1.0";
133614cf11afSPaul Mackerras 		break;
133714cf11afSPaul Mackerras 	case 2:
133814cf11afSPaul Mackerras 		vers = "1.2";
133914cf11afSPaul Mackerras 		break;
134014cf11afSPaul Mackerras 	case 3:
134114cf11afSPaul Mackerras 		vers = "1.3";
134214cf11afSPaul Mackerras 		break;
134314cf11afSPaul Mackerras 	default:
134414cf11afSPaul Mackerras 		vers = "<unknown>";
134514cf11afSPaul Mackerras 		break;
134614cf11afSPaul Mackerras 	}
1347a959ff56SBenjamin Herrenschmidt 	printk(KERN_INFO "mpic: Setting up MPIC \"%s\" version %s at %llx,"
1348a959ff56SBenjamin Herrenschmidt 	       " max %d CPUs\n",
13498bf41568SKyle Moffett 	       name, vers, (unsigned long long)phys_addr, num_possible_cpus());
1350a959ff56SBenjamin Herrenschmidt 	printk(KERN_INFO "mpic: ISU size: %d, shift: %d, mask: %x\n",
1351a959ff56SBenjamin Herrenschmidt 	       mpic->isu_size, mpic->isu_shift, mpic->isu_mask);
135214cf11afSPaul Mackerras 
135314cf11afSPaul Mackerras 	mpic->next = mpics;
135414cf11afSPaul Mackerras 	mpics = mpic;
135514cf11afSPaul Mackerras 
13560ebfff14SBenjamin Herrenschmidt 	if (flags & MPIC_PRIMARY) {
135714cf11afSPaul Mackerras 		mpic_primary = mpic;
13580ebfff14SBenjamin Herrenschmidt 		irq_set_default_host(mpic->irqhost);
13590ebfff14SBenjamin Herrenschmidt 	}
136014cf11afSPaul Mackerras 
136114cf11afSPaul Mackerras 	return mpic;
136214cf11afSPaul Mackerras }
136314cf11afSPaul Mackerras 
136414cf11afSPaul Mackerras void __init mpic_assign_isu(struct mpic *mpic, unsigned int isu_num,
1365a959ff56SBenjamin Herrenschmidt 			    phys_addr_t paddr)
136614cf11afSPaul Mackerras {
136714cf11afSPaul Mackerras 	unsigned int isu_first = isu_num * mpic->isu_size;
136814cf11afSPaul Mackerras 
136914cf11afSPaul Mackerras 	BUG_ON(isu_num >= MPIC_MAX_ISU);
137014cf11afSPaul Mackerras 
13715a2642f6SBenjamin Herrenschmidt 	mpic_map(mpic, mpic->irqhost->of_node,
13725a2642f6SBenjamin Herrenschmidt 		 paddr, &mpic->isus[isu_num], 0,
13737233593bSZang Roy-r61911 		 MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
13745a2642f6SBenjamin Herrenschmidt 
137514cf11afSPaul Mackerras 	if ((isu_first + mpic->isu_size) > mpic->num_sources)
137614cf11afSPaul Mackerras 		mpic->num_sources = isu_first + mpic->isu_size;
137714cf11afSPaul Mackerras }
137814cf11afSPaul Mackerras 
13790ebfff14SBenjamin Herrenschmidt void __init mpic_set_default_senses(struct mpic *mpic, u8 *senses, int count)
13800ebfff14SBenjamin Herrenschmidt {
13810ebfff14SBenjamin Herrenschmidt 	mpic->senses = senses;
13820ebfff14SBenjamin Herrenschmidt 	mpic->senses_count = count;
13830ebfff14SBenjamin Herrenschmidt }
13840ebfff14SBenjamin Herrenschmidt 
138514cf11afSPaul Mackerras void __init mpic_init(struct mpic *mpic)
138614cf11afSPaul Mackerras {
138714cf11afSPaul Mackerras 	int i;
1388cc353c30SArnd Bergmann 	int cpu;
138914cf11afSPaul Mackerras 
139014cf11afSPaul Mackerras 	BUG_ON(mpic->num_sources == 0);
139114cf11afSPaul Mackerras 
139214cf11afSPaul Mackerras 	printk(KERN_INFO "mpic: Initializing for %d sources\n", mpic->num_sources);
139314cf11afSPaul Mackerras 
139414cf11afSPaul Mackerras 	/* Set current processor priority to max */
13957233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
139614cf11afSPaul Mackerras 
1397ea94187fSScott Wood 	/* Initialize timers to our reserved vectors and mask them for now */
139814cf11afSPaul Mackerras 	for (i = 0; i < 4; i++) {
139914cf11afSPaul Mackerras 		mpic_write(mpic->tmregs,
14007233593bSZang Roy-r61911 			   i * MPIC_INFO(TIMER_STRIDE) +
1401ea94187fSScott Wood 			   MPIC_INFO(TIMER_DESTINATION),
1402ea94187fSScott Wood 			   1 << hard_smp_processor_id());
140314cf11afSPaul Mackerras 		mpic_write(mpic->tmregs,
14047233593bSZang Roy-r61911 			   i * MPIC_INFO(TIMER_STRIDE) +
14057233593bSZang Roy-r61911 			   MPIC_INFO(TIMER_VECTOR_PRI),
140614cf11afSPaul Mackerras 			   MPIC_VECPRI_MASK |
1407ea94187fSScott Wood 			   (9 << MPIC_VECPRI_PRIORITY_SHIFT) |
14087df2457dSOlof Johansson 			   (mpic->timer_vecs[0] + i));
140914cf11afSPaul Mackerras 	}
141014cf11afSPaul Mackerras 
141114cf11afSPaul Mackerras 	/* Initialize IPIs to our reserved vectors and mark them disabled for now */
141214cf11afSPaul Mackerras 	mpic_test_broken_ipi(mpic);
141314cf11afSPaul Mackerras 	for (i = 0; i < 4; i++) {
141414cf11afSPaul Mackerras 		mpic_ipi_write(i,
141514cf11afSPaul Mackerras 			       MPIC_VECPRI_MASK |
141614cf11afSPaul Mackerras 			       (10 << MPIC_VECPRI_PRIORITY_SHIFT) |
14177df2457dSOlof Johansson 			       (mpic->ipi_vecs[0] + i));
141814cf11afSPaul Mackerras 	}
141914cf11afSPaul Mackerras 
142014cf11afSPaul Mackerras 	/* Initialize interrupt sources */
142114cf11afSPaul Mackerras 	if (mpic->irq_count == 0)
142214cf11afSPaul Mackerras 		mpic->irq_count = mpic->num_sources;
142314cf11afSPaul Mackerras 
14241beb6a7dSBenjamin Herrenschmidt 	/* Do the HT PIC fixups on U3 broken mpic */
142514cf11afSPaul Mackerras 	DBG("MPIC flags: %x\n", mpic->flags);
142605af7bd2SMichael Ellerman 	if ((mpic->flags & MPIC_U3_HT_IRQS) && (mpic->flags & MPIC_PRIMARY)) {
14271beb6a7dSBenjamin Herrenschmidt 		mpic_scan_ht_pics(mpic);
142805af7bd2SMichael Ellerman 		mpic_u3msi_init(mpic);
142905af7bd2SMichael Ellerman 	}
143014cf11afSPaul Mackerras 
143138958dd9SOlof Johansson 	mpic_pasemi_msi_init(mpic);
143238958dd9SOlof Johansson 
1433d6a2639bSMeador Inge 	cpu = mpic_processor_id(mpic);
1434cc353c30SArnd Bergmann 
1435dfec2202SMeador Inge 	if (!(mpic->flags & MPIC_NO_RESET)) {
143614cf11afSPaul Mackerras 		for (i = 0; i < mpic->num_sources; i++) {
143714cf11afSPaul Mackerras 			/* start with vector = source number, and masked */
14386e99e458SBenjamin Herrenschmidt 			u32 vecpri = MPIC_VECPRI_MASK | i |
14396e99e458SBenjamin Herrenschmidt 				(8 << MPIC_VECPRI_PRIORITY_SHIFT);
144014cf11afSPaul Mackerras 
14417fd72186SBenjamin Herrenschmidt 			/* check if protected */
14427fd72186SBenjamin Herrenschmidt 			if (mpic->protected && test_bit(i, mpic->protected))
14437fd72186SBenjamin Herrenschmidt 				continue;
144414cf11afSPaul Mackerras 			/* init hw */
14457233593bSZang Roy-r61911 			mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI), vecpri);
1446cc353c30SArnd Bergmann 			mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION), 1 << cpu);
144714cf11afSPaul Mackerras 		}
1448dfec2202SMeador Inge 	}
144914cf11afSPaul Mackerras 
14507df2457dSOlof Johansson 	/* Init spurious vector */
14517df2457dSOlof Johansson 	mpic_write(mpic->gregs, MPIC_INFO(GREG_SPURIOUS), mpic->spurious_vec);
145214cf11afSPaul Mackerras 
14537233593bSZang Roy-r61911 	/* Disable 8259 passthrough, if supported */
14547233593bSZang Roy-r61911 	if (!(mpic->flags & MPIC_NO_PTHROU_DIS))
14557233593bSZang Roy-r61911 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
14567233593bSZang Roy-r61911 			   mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
145714cf11afSPaul Mackerras 			   | MPIC_GREG_GCONF_8259_PTHROU_DIS);
145814cf11afSPaul Mackerras 
1459d87bf3beSOlof Johansson 	if (mpic->flags & MPIC_NO_BIAS)
1460d87bf3beSOlof Johansson 		mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1461d87bf3beSOlof Johansson 			mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1462d87bf3beSOlof Johansson 			| MPIC_GREG_GCONF_NO_BIAS);
1463d87bf3beSOlof Johansson 
146414cf11afSPaul Mackerras 	/* Set current processor priority to 0 */
14657233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
14663669e930SJohannes Berg 
14673669e930SJohannes Berg #ifdef CONFIG_PM
14683669e930SJohannes Berg 	/* allocate memory to save mpic state */
1469ea96025aSAnton Vorontsov 	mpic->save_data = kmalloc(mpic->num_sources * sizeof(*mpic->save_data),
1470ea96025aSAnton Vorontsov 				  GFP_KERNEL);
14713669e930SJohannes Berg 	BUG_ON(mpic->save_data == NULL);
14723669e930SJohannes Berg #endif
147314cf11afSPaul Mackerras }
147414cf11afSPaul Mackerras 
1475868ea0c9SMark A. Greer void __init mpic_set_clk_ratio(struct mpic *mpic, u32 clock_ratio)
1476868ea0c9SMark A. Greer {
1477868ea0c9SMark A. Greer 	u32 v;
147814cf11afSPaul Mackerras 
1479868ea0c9SMark A. Greer 	v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
1480868ea0c9SMark A. Greer 	v &= ~MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO_MASK;
1481868ea0c9SMark A. Greer 	v |= MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO(clock_ratio);
1482868ea0c9SMark A. Greer 	mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
1483868ea0c9SMark A. Greer }
1484868ea0c9SMark A. Greer 
1485868ea0c9SMark A. Greer void __init mpic_set_serial_int(struct mpic *mpic, int enable)
1486868ea0c9SMark A. Greer {
1487ba1826e5SBenjamin Herrenschmidt 	unsigned long flags;
1488868ea0c9SMark A. Greer 	u32 v;
1489868ea0c9SMark A. Greer 
1490203041adSThomas Gleixner 	raw_spin_lock_irqsave(&mpic_lock, flags);
1491868ea0c9SMark A. Greer 	v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
1492868ea0c9SMark A. Greer 	if (enable)
1493868ea0c9SMark A. Greer 		v |= MPIC_GREG_GLOBAL_CONF_1_SIE;
1494868ea0c9SMark A. Greer 	else
1495868ea0c9SMark A. Greer 		v &= ~MPIC_GREG_GLOBAL_CONF_1_SIE;
1496868ea0c9SMark A. Greer 	mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
1497203041adSThomas Gleixner 	raw_spin_unlock_irqrestore(&mpic_lock, flags);
1498868ea0c9SMark A. Greer }
149914cf11afSPaul Mackerras 
150014cf11afSPaul Mackerras void mpic_irq_set_priority(unsigned int irq, unsigned int pri)
150114cf11afSPaul Mackerras {
1502d69a78d7STony Breeds 	struct mpic *mpic = mpic_find(irq);
1503476eb491SGrant Likely 	unsigned int src = virq_to_hw(irq);
150414cf11afSPaul Mackerras 	unsigned long flags;
150514cf11afSPaul Mackerras 	u32 reg;
150614cf11afSPaul Mackerras 
150706a901c5SStephen Rothwell 	if (!mpic)
150806a901c5SStephen Rothwell 		return;
150906a901c5SStephen Rothwell 
1510203041adSThomas Gleixner 	raw_spin_lock_irqsave(&mpic_lock, flags);
1511d69a78d7STony Breeds 	if (mpic_is_ipi(mpic, irq)) {
15127df2457dSOlof Johansson 		reg = mpic_ipi_read(src - mpic->ipi_vecs[0]) &
1513e5356640SBenjamin Herrenschmidt 			~MPIC_VECPRI_PRIORITY_MASK;
15147df2457dSOlof Johansson 		mpic_ipi_write(src - mpic->ipi_vecs[0],
151514cf11afSPaul Mackerras 			       reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
1516ea94187fSScott Wood 	} else if (mpic_is_tm(mpic, irq)) {
1517ea94187fSScott Wood 		reg = mpic_tm_read(src - mpic->timer_vecs[0]) &
1518ea94187fSScott Wood 			~MPIC_VECPRI_PRIORITY_MASK;
1519ea94187fSScott Wood 		mpic_tm_write(src - mpic->timer_vecs[0],
1520ea94187fSScott Wood 			      reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
152114cf11afSPaul Mackerras 	} else {
15227233593bSZang Roy-r61911 		reg = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI))
1523e5356640SBenjamin Herrenschmidt 			& ~MPIC_VECPRI_PRIORITY_MASK;
15247233593bSZang Roy-r61911 		mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
152514cf11afSPaul Mackerras 			       reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
152614cf11afSPaul Mackerras 	}
1527203041adSThomas Gleixner 	raw_spin_unlock_irqrestore(&mpic_lock, flags);
152814cf11afSPaul Mackerras }
152914cf11afSPaul Mackerras 
153014cf11afSPaul Mackerras void mpic_setup_this_cpu(void)
153114cf11afSPaul Mackerras {
153214cf11afSPaul Mackerras #ifdef CONFIG_SMP
153314cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
153414cf11afSPaul Mackerras 	unsigned long flags;
153514cf11afSPaul Mackerras 	u32 msk = 1 << hard_smp_processor_id();
153614cf11afSPaul Mackerras 	unsigned int i;
153714cf11afSPaul Mackerras 
153814cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
153914cf11afSPaul Mackerras 
154014cf11afSPaul Mackerras 	DBG("%s: setup_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
154114cf11afSPaul Mackerras 
1542203041adSThomas Gleixner 	raw_spin_lock_irqsave(&mpic_lock, flags);
154314cf11afSPaul Mackerras 
154414cf11afSPaul Mackerras  	/* let the mpic know we want intrs. default affinity is 0xffffffff
154514cf11afSPaul Mackerras 	 * until changed via /proc. That's how it's done on x86. If we want
154614cf11afSPaul Mackerras 	 * it differently, then we should make sure we also change the default
1547a53da52fSIngo Molnar 	 * values of irq_desc[].affinity in irq.c.
154814cf11afSPaul Mackerras  	 */
154914cf11afSPaul Mackerras 	if (distribute_irqs) {
155014cf11afSPaul Mackerras 	 	for (i = 0; i < mpic->num_sources ; i++)
15517233593bSZang Roy-r61911 			mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
15527233593bSZang Roy-r61911 				mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) | msk);
155314cf11afSPaul Mackerras 	}
155414cf11afSPaul Mackerras 
155514cf11afSPaul Mackerras 	/* Set current processor priority to 0 */
15567233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
155714cf11afSPaul Mackerras 
1558203041adSThomas Gleixner 	raw_spin_unlock_irqrestore(&mpic_lock, flags);
155914cf11afSPaul Mackerras #endif /* CONFIG_SMP */
156014cf11afSPaul Mackerras }
156114cf11afSPaul Mackerras 
156214cf11afSPaul Mackerras int mpic_cpu_get_priority(void)
156314cf11afSPaul Mackerras {
156414cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
156514cf11afSPaul Mackerras 
15667233593bSZang Roy-r61911 	return mpic_cpu_read(MPIC_INFO(CPU_CURRENT_TASK_PRI));
156714cf11afSPaul Mackerras }
156814cf11afSPaul Mackerras 
156914cf11afSPaul Mackerras void mpic_cpu_set_priority(int prio)
157014cf11afSPaul Mackerras {
157114cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
157214cf11afSPaul Mackerras 
157314cf11afSPaul Mackerras 	prio &= MPIC_CPU_TASKPRI_MASK;
15747233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), prio);
157514cf11afSPaul Mackerras }
157614cf11afSPaul Mackerras 
157714cf11afSPaul Mackerras void mpic_teardown_this_cpu(int secondary)
157814cf11afSPaul Mackerras {
157914cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
158014cf11afSPaul Mackerras 	unsigned long flags;
158114cf11afSPaul Mackerras 	u32 msk = 1 << hard_smp_processor_id();
158214cf11afSPaul Mackerras 	unsigned int i;
158314cf11afSPaul Mackerras 
158414cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
158514cf11afSPaul Mackerras 
158614cf11afSPaul Mackerras 	DBG("%s: teardown_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
1587203041adSThomas Gleixner 	raw_spin_lock_irqsave(&mpic_lock, flags);
158814cf11afSPaul Mackerras 
158914cf11afSPaul Mackerras 	/* let the mpic know we don't want intrs.  */
159014cf11afSPaul Mackerras 	for (i = 0; i < mpic->num_sources ; i++)
15917233593bSZang Roy-r61911 		mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
15927233593bSZang Roy-r61911 			mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) & ~msk);
159314cf11afSPaul Mackerras 
159414cf11afSPaul Mackerras 	/* Set current processor priority to max */
15957233593bSZang Roy-r61911 	mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
15967132799bSValentine Barshak 	/* We need to EOI the IPI since not all platforms reset the MPIC
15977132799bSValentine Barshak 	 * on boot and new interrupts wouldn't get delivered otherwise.
15987132799bSValentine Barshak 	 */
15997132799bSValentine Barshak 	mpic_eoi(mpic);
160014cf11afSPaul Mackerras 
1601203041adSThomas Gleixner 	raw_spin_unlock_irqrestore(&mpic_lock, flags);
160214cf11afSPaul Mackerras }
160314cf11afSPaul Mackerras 
160414cf11afSPaul Mackerras 
1605f365355eSOlof Johansson static unsigned int _mpic_get_one_irq(struct mpic *mpic, int reg)
160614cf11afSPaul Mackerras {
16070ebfff14SBenjamin Herrenschmidt 	u32 src;
160814cf11afSPaul Mackerras 
1609f365355eSOlof Johansson 	src = mpic_cpu_read(reg) & MPIC_INFO(VECPRI_VECTOR_MASK);
16101beb6a7dSBenjamin Herrenschmidt #ifdef DEBUG_LOW
1611f365355eSOlof Johansson 	DBG("%s: get_one_irq(reg 0x%x): %d\n", mpic->name, reg, src);
16121beb6a7dSBenjamin Herrenschmidt #endif
16135cddd2e3SJosh Boyer 	if (unlikely(src == mpic->spurious_vec)) {
16145cddd2e3SJosh Boyer 		if (mpic->flags & MPIC_SPV_EOI)
16155cddd2e3SJosh Boyer 			mpic_eoi(mpic);
16160ebfff14SBenjamin Herrenschmidt 		return NO_IRQ;
16175cddd2e3SJosh Boyer 	}
16187fd72186SBenjamin Herrenschmidt 	if (unlikely(mpic->protected && test_bit(src, mpic->protected))) {
161976462232SChristian Dietrich 		printk_ratelimited(KERN_WARNING "%s: Got protected source %d !\n",
16207fd72186SBenjamin Herrenschmidt 				   mpic->name, (int)src);
16217fd72186SBenjamin Herrenschmidt 		mpic_eoi(mpic);
16227fd72186SBenjamin Herrenschmidt 		return NO_IRQ;
16237fd72186SBenjamin Herrenschmidt 	}
16247fd72186SBenjamin Herrenschmidt 
16250ebfff14SBenjamin Herrenschmidt 	return irq_linear_revmap(mpic->irqhost, src);
162614cf11afSPaul Mackerras }
162714cf11afSPaul Mackerras 
1628f365355eSOlof Johansson unsigned int mpic_get_one_irq(struct mpic *mpic)
1629f365355eSOlof Johansson {
1630f365355eSOlof Johansson 	return _mpic_get_one_irq(mpic, MPIC_INFO(CPU_INTACK));
1631f365355eSOlof Johansson }
1632f365355eSOlof Johansson 
163335a84c2fSOlaf Hering unsigned int mpic_get_irq(void)
163414cf11afSPaul Mackerras {
163514cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
163614cf11afSPaul Mackerras 
163714cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
163814cf11afSPaul Mackerras 
163935a84c2fSOlaf Hering 	return mpic_get_one_irq(mpic);
164014cf11afSPaul Mackerras }
164114cf11afSPaul Mackerras 
1642d91e4ea7SKumar Gala unsigned int mpic_get_coreint_irq(void)
1643d91e4ea7SKumar Gala {
1644d91e4ea7SKumar Gala #ifdef CONFIG_BOOKE
1645d91e4ea7SKumar Gala 	struct mpic *mpic = mpic_primary;
1646d91e4ea7SKumar Gala 	u32 src;
1647d91e4ea7SKumar Gala 
1648d91e4ea7SKumar Gala 	BUG_ON(mpic == NULL);
1649d91e4ea7SKumar Gala 
1650d91e4ea7SKumar Gala 	src = mfspr(SPRN_EPR);
1651d91e4ea7SKumar Gala 
1652d91e4ea7SKumar Gala 	if (unlikely(src == mpic->spurious_vec)) {
1653d91e4ea7SKumar Gala 		if (mpic->flags & MPIC_SPV_EOI)
1654d91e4ea7SKumar Gala 			mpic_eoi(mpic);
1655d91e4ea7SKumar Gala 		return NO_IRQ;
1656d91e4ea7SKumar Gala 	}
1657d91e4ea7SKumar Gala 	if (unlikely(mpic->protected && test_bit(src, mpic->protected))) {
165876462232SChristian Dietrich 		printk_ratelimited(KERN_WARNING "%s: Got protected source %d !\n",
1659d91e4ea7SKumar Gala 				   mpic->name, (int)src);
1660d91e4ea7SKumar Gala 		return NO_IRQ;
1661d91e4ea7SKumar Gala 	}
1662d91e4ea7SKumar Gala 
1663d91e4ea7SKumar Gala 	return irq_linear_revmap(mpic->irqhost, src);
1664d91e4ea7SKumar Gala #else
1665d91e4ea7SKumar Gala 	return NO_IRQ;
1666d91e4ea7SKumar Gala #endif
1667d91e4ea7SKumar Gala }
1668d91e4ea7SKumar Gala 
1669f365355eSOlof Johansson unsigned int mpic_get_mcirq(void)
1670f365355eSOlof Johansson {
1671f365355eSOlof Johansson 	struct mpic *mpic = mpic_primary;
1672f365355eSOlof Johansson 
1673f365355eSOlof Johansson 	BUG_ON(mpic == NULL);
1674f365355eSOlof Johansson 
1675f365355eSOlof Johansson 	return _mpic_get_one_irq(mpic, MPIC_INFO(CPU_MCACK));
1676f365355eSOlof Johansson }
167714cf11afSPaul Mackerras 
167814cf11afSPaul Mackerras #ifdef CONFIG_SMP
167914cf11afSPaul Mackerras void mpic_request_ipis(void)
168014cf11afSPaul Mackerras {
168114cf11afSPaul Mackerras 	struct mpic *mpic = mpic_primary;
168278608dd3SMilton Miller 	int i;
168314cf11afSPaul Mackerras 	BUG_ON(mpic == NULL);
168414cf11afSPaul Mackerras 
16850ebfff14SBenjamin Herrenschmidt 	printk(KERN_INFO "mpic: requesting IPIs...\n");
168614cf11afSPaul Mackerras 
16870ebfff14SBenjamin Herrenschmidt 	for (i = 0; i < 4; i++) {
16880ebfff14SBenjamin Herrenschmidt 		unsigned int vipi = irq_create_mapping(mpic->irqhost,
16897df2457dSOlof Johansson 						       mpic->ipi_vecs[0] + i);
16900ebfff14SBenjamin Herrenschmidt 		if (vipi == NO_IRQ) {
169178608dd3SMilton Miller 			printk(KERN_ERR "Failed to map %s\n", smp_ipi_name[i]);
169278608dd3SMilton Miller 			continue;
16930ebfff14SBenjamin Herrenschmidt 		}
169478608dd3SMilton Miller 		smp_request_message_ipi(vipi, i);
16950ebfff14SBenjamin Herrenschmidt 	}
169614cf11afSPaul Mackerras }
1697a9c59264SPaul Mackerras 
16983caba98fSMilton Miller void smp_mpic_message_pass(int cpu, int msg)
16992ef613cbSBenjamin Herrenschmidt {
17002ef613cbSBenjamin Herrenschmidt 	struct mpic *mpic = mpic_primary;
17013caba98fSMilton Miller 	u32 physmask;
17022ef613cbSBenjamin Herrenschmidt 
17032ef613cbSBenjamin Herrenschmidt 	BUG_ON(mpic == NULL);
17042ef613cbSBenjamin Herrenschmidt 
1705a9c59264SPaul Mackerras 	/* make sure we're sending something that translates to an IPI */
1706a9c59264SPaul Mackerras 	if ((unsigned int)msg > 3) {
1707a9c59264SPaul Mackerras 		printk("SMP %d: smp_message_pass: unknown msg %d\n",
1708a9c59264SPaul Mackerras 		       smp_processor_id(), msg);
1709a9c59264SPaul Mackerras 		return;
1710a9c59264SPaul Mackerras 	}
17113caba98fSMilton Miller 
17123caba98fSMilton Miller #ifdef DEBUG_IPI
17133caba98fSMilton Miller 	DBG("%s: send_ipi(ipi_no: %d)\n", mpic->name, msg);
17143caba98fSMilton Miller #endif
17153caba98fSMilton Miller 
17163caba98fSMilton Miller 	physmask = 1 << get_hard_smp_processor_id(cpu);
17173caba98fSMilton Miller 
17183caba98fSMilton Miller 	mpic_cpu_write(MPIC_INFO(CPU_IPI_DISPATCH_0) +
17193caba98fSMilton Miller 		       msg * MPIC_INFO(CPU_IPI_DISPATCH_STRIDE), physmask);
1720a9c59264SPaul Mackerras }
1721775aeff4SMichael Ellerman 
1722775aeff4SMichael Ellerman int __init smp_mpic_probe(void)
1723775aeff4SMichael Ellerman {
1724775aeff4SMichael Ellerman 	int nr_cpus;
1725775aeff4SMichael Ellerman 
1726775aeff4SMichael Ellerman 	DBG("smp_mpic_probe()...\n");
1727775aeff4SMichael Ellerman 
17282ef613cbSBenjamin Herrenschmidt 	nr_cpus = cpumask_weight(cpu_possible_mask);
1729775aeff4SMichael Ellerman 
1730775aeff4SMichael Ellerman 	DBG("nr_cpus: %d\n", nr_cpus);
1731775aeff4SMichael Ellerman 
1732775aeff4SMichael Ellerman 	if (nr_cpus > 1)
1733775aeff4SMichael Ellerman 		mpic_request_ipis();
1734775aeff4SMichael Ellerman 
1735775aeff4SMichael Ellerman 	return nr_cpus;
1736775aeff4SMichael Ellerman }
1737775aeff4SMichael Ellerman 
1738775aeff4SMichael Ellerman void __devinit smp_mpic_setup_cpu(int cpu)
1739775aeff4SMichael Ellerman {
1740775aeff4SMichael Ellerman 	mpic_setup_this_cpu();
1741775aeff4SMichael Ellerman }
174266953ebeSMatthew McClintock 
174366953ebeSMatthew McClintock void mpic_reset_core(int cpu)
174466953ebeSMatthew McClintock {
174566953ebeSMatthew McClintock 	struct mpic *mpic = mpic_primary;
174666953ebeSMatthew McClintock 	u32 pir;
174766953ebeSMatthew McClintock 	int cpuid = get_hard_smp_processor_id(cpu);
174844f16fcfSMatthew McClintock 	int i;
174966953ebeSMatthew McClintock 
175066953ebeSMatthew McClintock 	/* Set target bit for core reset */
175166953ebeSMatthew McClintock 	pir = mpic_read(mpic->gregs, MPIC_INFO(GREG_PROCESSOR_INIT));
175266953ebeSMatthew McClintock 	pir |= (1 << cpuid);
175366953ebeSMatthew McClintock 	mpic_write(mpic->gregs, MPIC_INFO(GREG_PROCESSOR_INIT), pir);
175466953ebeSMatthew McClintock 	mpic_read(mpic->gregs, MPIC_INFO(GREG_PROCESSOR_INIT));
175566953ebeSMatthew McClintock 
175666953ebeSMatthew McClintock 	/* Restore target bit after reset complete */
175766953ebeSMatthew McClintock 	pir &= ~(1 << cpuid);
175866953ebeSMatthew McClintock 	mpic_write(mpic->gregs, MPIC_INFO(GREG_PROCESSOR_INIT), pir);
175966953ebeSMatthew McClintock 	mpic_read(mpic->gregs, MPIC_INFO(GREG_PROCESSOR_INIT));
176044f16fcfSMatthew McClintock 
176144f16fcfSMatthew McClintock 	/* Perform 15 EOI on each reset core to clear pending interrupts.
176244f16fcfSMatthew McClintock 	 * This is required for FSL CoreNet based devices */
176344f16fcfSMatthew McClintock 	if (mpic->flags & MPIC_FSL) {
176444f16fcfSMatthew McClintock 		for (i = 0; i < 15; i++) {
176544f16fcfSMatthew McClintock 			_mpic_write(mpic->reg_type, &mpic->cpuregs[cpuid],
176644f16fcfSMatthew McClintock 				      MPIC_CPU_EOI, 0);
176744f16fcfSMatthew McClintock 		}
176844f16fcfSMatthew McClintock 	}
176966953ebeSMatthew McClintock }
177014cf11afSPaul Mackerras #endif /* CONFIG_SMP */
17713669e930SJohannes Berg 
17723669e930SJohannes Berg #ifdef CONFIG_PM
1773f5a592f7SRafael J. Wysocki static void mpic_suspend_one(struct mpic *mpic)
17743669e930SJohannes Berg {
17753669e930SJohannes Berg 	int i;
17763669e930SJohannes Berg 
17773669e930SJohannes Berg 	for (i = 0; i < mpic->num_sources; i++) {
17783669e930SJohannes Berg 		mpic->save_data[i].vecprio =
17793669e930SJohannes Berg 			mpic_irq_read(i, MPIC_INFO(IRQ_VECTOR_PRI));
17803669e930SJohannes Berg 		mpic->save_data[i].dest =
17813669e930SJohannes Berg 			mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION));
17823669e930SJohannes Berg 	}
1783f5a592f7SRafael J. Wysocki }
1784f5a592f7SRafael J. Wysocki 
1785f5a592f7SRafael J. Wysocki static int mpic_suspend(void)
1786f5a592f7SRafael J. Wysocki {
1787f5a592f7SRafael J. Wysocki 	struct mpic *mpic = mpics;
1788f5a592f7SRafael J. Wysocki 
1789f5a592f7SRafael J. Wysocki 	while (mpic) {
1790f5a592f7SRafael J. Wysocki 		mpic_suspend_one(mpic);
1791f5a592f7SRafael J. Wysocki 		mpic = mpic->next;
1792f5a592f7SRafael J. Wysocki 	}
17933669e930SJohannes Berg 
17943669e930SJohannes Berg 	return 0;
17953669e930SJohannes Berg }
17963669e930SJohannes Berg 
1797f5a592f7SRafael J. Wysocki static void mpic_resume_one(struct mpic *mpic)
17983669e930SJohannes Berg {
17993669e930SJohannes Berg 	int i;
18003669e930SJohannes Berg 
18013669e930SJohannes Berg 	for (i = 0; i < mpic->num_sources; i++) {
18023669e930SJohannes Berg 		mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI),
18033669e930SJohannes Berg 			       mpic->save_data[i].vecprio);
18043669e930SJohannes Berg 		mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
18053669e930SJohannes Berg 			       mpic->save_data[i].dest);
18063669e930SJohannes Berg 
18073669e930SJohannes Berg #ifdef CONFIG_MPIC_U3_HT_IRQS
18087c9d9360SAlastair Bridgewater 	if (mpic->fixups) {
18093669e930SJohannes Berg 		struct mpic_irq_fixup *fixup = &mpic->fixups[i];
18103669e930SJohannes Berg 
18113669e930SJohannes Berg 		if (fixup->base) {
18123669e930SJohannes Berg 			/* we use the lowest bit in an inverted meaning */
18133669e930SJohannes Berg 			if ((mpic->save_data[i].fixup_data & 1) == 0)
18143669e930SJohannes Berg 				continue;
18153669e930SJohannes Berg 
18163669e930SJohannes Berg 			/* Enable and configure */
18173669e930SJohannes Berg 			writeb(0x10 + 2 * fixup->index, fixup->base + 2);
18183669e930SJohannes Berg 
18193669e930SJohannes Berg 			writel(mpic->save_data[i].fixup_data & ~1,
18203669e930SJohannes Berg 			       fixup->base + 4);
18213669e930SJohannes Berg 		}
18223669e930SJohannes Berg 	}
18233669e930SJohannes Berg #endif
18243669e930SJohannes Berg 	} /* end for loop */
18253669e930SJohannes Berg }
18263669e930SJohannes Berg 
1827f5a592f7SRafael J. Wysocki static void mpic_resume(void)
1828f5a592f7SRafael J. Wysocki {
1829f5a592f7SRafael J. Wysocki 	struct mpic *mpic = mpics;
1830f5a592f7SRafael J. Wysocki 
1831f5a592f7SRafael J. Wysocki 	while (mpic) {
1832f5a592f7SRafael J. Wysocki 		mpic_resume_one(mpic);
1833f5a592f7SRafael J. Wysocki 		mpic = mpic->next;
1834f5a592f7SRafael J. Wysocki 	}
1835f5a592f7SRafael J. Wysocki }
1836f5a592f7SRafael J. Wysocki 
1837f5a592f7SRafael J. Wysocki static struct syscore_ops mpic_syscore_ops = {
18383669e930SJohannes Berg 	.resume = mpic_resume,
18393669e930SJohannes Berg 	.suspend = mpic_suspend,
18403669e930SJohannes Berg };
18413669e930SJohannes Berg 
18423669e930SJohannes Berg static int mpic_init_sys(void)
18433669e930SJohannes Berg {
1844f5a592f7SRafael J. Wysocki 	register_syscore_ops(&mpic_syscore_ops);
1845f5a592f7SRafael J. Wysocki 	return 0;
18463669e930SJohannes Berg }
18473669e930SJohannes Berg 
18483669e930SJohannes Berg device_initcall(mpic_init_sys);
1849f5a592f7SRafael J. Wysocki #endif
1850